fix: improve SVG text alignment and positioning support in AssetManager

This commit is contained in:
Claudecio Martins
2026-10-12 13:44:31 -03:00
parent 5551d9e667
commit 539d5e4c33
5 changed files with 29 additions and 10 deletions
+2 -2
View File
@@ -7,7 +7,7 @@
<line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<!-- Etiqueta vermelha com sigla PDF --> <!-- Etiqueta vermelha com sigla PDF perfeitamente centralizada -->
<rect x="11" y="31" width="28" height="11" rx="3" fill="#d32f2f"/> <rect x="11" y="31" width="28" height="11" rx="3" fill="#d32f2f"/>
<text x="25" y="39" font-family="'Segoe UI', Arial, sans-serif" font-size="8.5" font-weight="bold" fill="#ffffff" text-anchor="middle">PDF</text> <text x="25" y="36.5" font-family="'Segoe UI', Arial, sans-serif" font-size="8.5px" font-weight="bold" fill="#ffffff" text-anchor="middle" dominant-baseline="central">PDF</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 996 B

After

Width:  |  Height:  |  Size: 1.0 KiB

+2 -2
View File
@@ -7,7 +7,7 @@
<line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<!-- Etiqueta azul com sigla XML --> <!-- Etiqueta azul com sigla XML perfeitamente centralizada -->
<rect x="11" y="31" width="28" height="11" rx="3" fill="#0288d1"/> <rect x="11" y="31" width="28" height="11" rx="3" fill="#0288d1"/>
<text x="25" y="39" font-family="'Segoe UI', Arial, sans-serif" font-size="8px" font-weight="bold" fill="#ffffff" text-anchor="middle">&lt;XML&gt;</text> <text x="25" y="36.5" font-family="'Segoe UI', Arial, sans-serif" font-size="8px" font-weight="bold" fill="#ffffff" text-anchor="middle" dominant-baseline="central">&lt;XML&gt;</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1000 B

After

Width:  |  Height:  |  Size: 1.0 KiB

@@ -519,7 +519,7 @@ public final class AssetManager {
double x = parseDouble(el.getAttribute("x"), 0); double x = parseDouble(el.getAttribute("x"), 0);
double y = parseDouble(el.getAttribute("y"), 0); double y = parseDouble(el.getAttribute("y"), 0);
String content = el.getTextContent(); String content = el.getTextContent();
Text t = new Text(x, y, content != null ? content.trim() : ""); Text t = new Text(content != null ? content.trim() : "");
double fs = parseDouble(el.getAttribute("font-size"), 12); double fs = parseDouble(el.getAttribute("font-size"), 12);
String fw = el.getAttribute("font-weight"); String fw = el.getAttribute("font-weight");
t.setFont(Font.font("Segoe UI", "bold".equalsIgnoreCase(fw) ? FontWeight.BOLD : FontWeight.NORMAL, fs)); t.setFont(Font.font("Segoe UI", "bold".equalsIgnoreCase(fw) ? FontWeight.BOLD : FontWeight.NORMAL, fs));
@@ -529,9 +529,28 @@ public final class AssetManager {
if (fillVal != null) { if (fillVal != null) {
t.setFill(parsePaint(fillVal, defsMap, Color.BLACK)); t.setFill(parsePaint(fillVal, defsMap, Color.BLACK));
} }
if ("middle".equalsIgnoreCase(el.getAttribute("text-anchor"))) {
String anchor = inlineStyle.getOrDefault("text-anchor", el.getAttribute("text-anchor"));
String baseline = inlineStyle.getOrDefault("dominant-baseline", el.getAttribute("dominant-baseline"));
boolean isCenterBaseline = "central".equalsIgnoreCase(baseline) || "middle".equalsIgnoreCase(baseline);
if (isCenterBaseline) {
t.setTextOrigin(javafx.geometry.VPos.CENTER); t.setTextOrigin(javafx.geometry.VPos.CENTER);
} else {
t.setTextOrigin(javafx.geometry.VPos.BASELINE);
} }
double textWidth = t.getLayoutBounds().getWidth();
double finalX = x;
if ("middle".equalsIgnoreCase(anchor)) {
finalX = x - (textWidth / 2.0);
} else if ("end".equalsIgnoreCase(anchor)) {
finalX = x - textWidth;
}
t.setX(finalX);
t.setY(y);
return t; return t;
} }
case "g" -> { case "g" -> {
@@ -7,7 +7,7 @@
<line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<!-- Etiqueta vermelha com sigla PDF --> <!-- Etiqueta vermelha com sigla PDF perfeitamente centralizada -->
<rect x="11" y="31" width="28" height="11" rx="3" fill="#d32f2f"/> <rect x="11" y="31" width="28" height="11" rx="3" fill="#d32f2f"/>
<text x="25" y="39" font-family="'Segoe UI', Arial, sans-serif" font-size="8.5" font-weight="bold" fill="#ffffff" text-anchor="middle">PDF</text> <text x="25" y="36.5" font-family="'Segoe UI', Arial, sans-serif" font-size="8.5px" font-weight="bold" fill="#ffffff" text-anchor="middle" dominant-baseline="central">PDF</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 996 B

After

Width:  |  Height:  |  Size: 1.0 KiB

@@ -7,7 +7,7 @@
<line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="18" x2="27" y2="18" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="22" x2="35" y2="22" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/> <line x1="13" y1="26" x2="35" y2="26" stroke="#b0bec5" stroke-width="2" stroke-linecap="round"/>
<!-- Etiqueta azul com sigla XML --> <!-- Etiqueta azul com sigla XML perfeitamente centralizada -->
<rect x="11" y="31" width="28" height="11" rx="3" fill="#0288d1"/> <rect x="11" y="31" width="28" height="11" rx="3" fill="#0288d1"/>
<text x="25" y="39" font-family="'Segoe UI', Arial, sans-serif" font-size="8px" font-weight="bold" fill="#ffffff" text-anchor="middle">&lt;XML&gt;</text> <text x="25" y="36.5" font-family="'Segoe UI', Arial, sans-serif" font-size="8px" font-weight="bold" fill="#ffffff" text-anchor="middle" dominant-baseline="central">&lt;XML&gt;</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1000 B

After

Width:  |  Height:  |  Size: 1.0 KiB