Skip to content

Commit

Permalink
klighd.piccolo.freehep + klighd.piccolo.test: fixed misplaced 'white-…
Browse files Browse the repository at this point in the history
…space: pre' styling for multi-line text label in the freehep-based SVG generation

appears to me like some collateral damage of the solution of #44,
updated tests
  • Loading branch information
sailingKieler committed Aug 17, 2021
1 parent b536e46 commit f7c8c6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,15 +855,6 @@ protected void writeString(String str, double x, double y)
}
style.put("stroke", "none");

// related to KIPRA-1637: preserve multiple spaces when displaying text strings
if (isProperty(STYLABLE))
style.put("white-space", "pre");
else
// avoid using the deprecated property 'xml:space' (https://docs.w3cub.com/svg/attribute/xml:space/)
// and sneak in the (css-based) replacement (https://www.w3schools.com/cssref/pr_text_white-space.asp)
// via a 'style' property definition
style.put("style", "white-space: pre");

// convert tags to string values
str = XMLWriter.normalizeText(str);

Expand Down Expand Up @@ -910,7 +901,22 @@ private String getTextsString(String text, Properties style, String indentation)
final String[] lines = text.split("\\r?\\n|\\r");
final boolean isSingleLine = lines.length <= 1;
final StringBuffer content = new StringBuffer();


// in case of a single line text
// clone the styles object in order to achieve identical order while iterating over the entries
// otherwise take an empty properties table, ...
final Properties textLineStyle = isSingleLine ? (Properties) style.clone() : new Properties();

// related to KIPRA-1637: preserve multiple spaces when displaying text strings;
// this property must be set per text field and won't be considered properly by chromium if outsourced to parent
if (isProperty(STYLABLE))
textLineStyle.put("white-space", "pre");
else
// avoid using the deprecated property 'xml:space' (https://docs.w3cub.com/svg/attribute/xml:space/)
// and sneak in the (css-based) replacement (https://www.w3schools.com/cssref/pr_text_white-space.asp)
// via a 'style' property definition
textLineStyle.put("style", "white-space: pre");

int i = 0;
if (display != null) {
// Translate the font back to an swt font
Expand Down Expand Up @@ -942,7 +948,7 @@ private String getTextsString(String text, Properties style, String indentation)
content.append("\n" + indentation);
content.append("<text x=\"0\" y=\"").append(y).append("\"");
// style
content.append(isSingleLine ? " " + style(style) : "");
content.append(" " + style(textLineStyle));

final Double nextLineLength = noTextLengthPerLineCalcRequired ? nextLength :
// need to box the result here as the type of the ternary operation would be 'double' otherwise
Expand Down Expand Up @@ -1006,11 +1012,11 @@ private String getTextsString(String text, Properties style, String indentation)
for (final String line : lines) {
content.append("\n" + indentation);
content.append("<text x=\"0\" y=\"").append(y).append("\"");
content.append(isSingleLine ? " "
content.append(" "
// style
+ style(style)
// semantic data
+ attributes(false) : ""
+ (isSingleLine ? " " + attributes(false) : "")
);
content.append(textLineAttributes(line, i++));
content.append(">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ class FreeHEPSVGOffscreenRenderingTest {
<g fill="none" stroke-opacity="1" stroke="#000000">
<path d="M 0.5 0.5 L 99.5 0.5 L 99.5 99.5 L 0.5 99.5 L 0.5 0.5 z"/>
</g>
<g fill-opacity="1" font-style="normal" font-family="Helvetica" style="white-space: pre" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>">NodeLabel:</text>
<text x="0" y="<Y>">Some additional info</text>
<g fill-opacity="1" font-style="normal" font-family="Helvetica" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>" style="white-space: pre">NodeLabel:</text>
<text x="0" y="<Y>" style="white-space: pre">Some additional info</text>
</g>
</g>
'''.equalsSVGof[ // cs: deactivated generation of 'textLength' property settings on purpose, as we cannot inject reliable test size data per text line as of now
Expand All @@ -317,9 +317,9 @@ class FreeHEPSVGOffscreenRenderingTest {
<path d="M 0.5 0.5 L 99.5 0.5 L 99.5 99.5 L 0.5 99.5 L 0.5 0.5 z"/>
</g>
<g id="MyLabel" class="MyLabelClass" klighd:MyKey="MyValue" klighd:My2ndKey="KLabel:0">
<g fill-opacity="1" font-style="normal" font-family="Helvetica" style="white-space: pre" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>" klighd:MyTextLineKey="0">NodeLabel:</text>
<text x="0" y="<Y>" klighd:MyTextLineKey="1">Some additional info</text>
<g fill-opacity="1" font-style="normal" font-family="Helvetica" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>" style="white-space: pre" klighd:MyTextLineKey="0">NodeLabel:</text>
<text x="0" y="<Y>" style="white-space: pre" klighd:MyTextLineKey="1">Some additional info</text>
</g>
</g>
</g>
Expand Down Expand Up @@ -448,9 +448,9 @@ class FreeHEPSVGOffscreenRenderingTest {
</g>
</g>
<g id="MyLabel" class="MyLabelClass" klighd:MyKey="MyValue" klighd:My2ndKey="KLabel:0">
<g transform="matrix(1, 0, 0, 1, 106, 45)" fill-opacity="1" font-style="normal" font-family="Helvetica" style="white-space: pre" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>" klighd:MyTextLineKey="0">PortLabel:</text>
<text x="0" y="<Y>" klighd:MyTextLineKey="1">Some additional info</text>
<g transform="matrix(1, 0, 0, 1, 106, 45)" fill-opacity="1" font-style="normal" font-family="Helvetica" font-weight="normal" stroke="none" fill="#000000" font-size="<FONT_SIZE>" id="MyText" class="MyTextClass" klighd:MyKey="MyValue" klighd:My2ndKey="KText:0">
<text x="0" y="<Y>" style="white-space: pre" klighd:MyTextLineKey="0">PortLabel:</text>
<text x="0" y="<Y>" style="white-space: pre" klighd:MyTextLineKey="1">Some additional info</text>
</g>
</g>
</g>
Expand Down

0 comments on commit f7c8c6f

Please sign in to comment.