Skip to content

Commit 249147d

Browse files
committed
Belt and braces for fallback font
Also reintroduce local option for font faces
1 parent b991832 commit 249147d

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

graphics-generator/src/main/java/io/quarkus/infra/performance/graphics/SvgAdjuster.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@
88

99
public class SvgAdjuster {
1010

11-
public static final String GRAPHICS_ELEMENT = "g";
1211
public static final String STYLE = "style";
13-
public static final String FONT_FAMILY_DECLARATION = "font-family:\\s*'" + Theme.FONT.getName() + "'";
1412

1513
public static String adjustSvg(String svg) {
1614
Document svgDoc = Jsoup.parse(svg, "", Parser.xmlParser());
1715

18-
Elements gElements = svgDoc.select(GRAPHICS_ELEMENT);
16+
// Some of the font information (for bold) is on the g elements, and some (for light and italic) is on the text elements
17+
addFallbackFonts(svgDoc, "text");
18+
addFallbackFonts(svgDoc, "g");
1919

20+
return svgDoc.outerHtml();
21+
}
22+
23+
private static void addFallbackFonts(Document svgDoc, String selector) {
24+
Elements elements = svgDoc.select(selector);
2025
for (String name : Theme.FONT.getNames()) {
2126
String fontFamilyDeclaration = "font-family:\\s*'" + name + "'";
2227
String fallbackString = "font-family: '" + name + "', " + Theme.FONT.getFamilyDeclaration();
23-
for (Element g : gElements) {
28+
for (Element g : elements) {
2429
String style = g.attr(STYLE);
2530
style = style.replaceAll(fontFamilyDeclaration, fallbackString);
2631
g.attr(STYLE, style);
2732
}
2833
}
29-
return svgDoc.outerHtml();
3034
}
3135
}

graphics-generator/src/main/java/io/quarkus/infra/performance/graphics/charts/fonts/EmbeddableFont.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private EmbeddableFont(String fontName, List<String> fallbacks, Map<FontStyle, S
4848
css = dfonts.stream().map(d -> generateFontFaceCSS(d)).collect(Collectors.joining(" "));
4949
fonts = dfonts.stream().collect(Collectors.toMap(d -> d.style, d -> d.font()));
5050

51-
familyDeclaration = "'" + fontName + "', " + fallbacks.stream().map(s -> "'" + s + "'").collect(Collectors.joining(", ")).replaceAll("'sans-serif'", "sans-serif");
51+
familyDeclaration = "'" + fontName + " Light', '" + fontName + "', " + fallbacks.stream().map(s -> "'" + s + "'").collect(Collectors.joining(", ")).replaceAll("'sans-serif'", "sans-serif");
5252

5353
}
5454

@@ -129,15 +129,18 @@ private static String generateFontFaceCSS(DownloadedFont downloadedFont) {
129129
// Base64 encode the font bytes
130130
// It would be nice to subset the characters and only include what's needed, to save file sizes
131131
String base64Font = Base64.getEncoder().encodeToString(downloadedFont.raw());
132+
String fontName = downloadedFont.font().getFontName();
132133
return """
133134
@font-face {
134135
font-family: '%s';
135136
src:
136-
url('data:font/ttf;base64,%s') format('truetype');
137-
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
137+
url('data:font/ttf;base64,%s') format('truetype'),
138+
local(%s),
139+
local(%s);
140+
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
138141
font-style: normal;
139142
}
140-
""".formatted(downloadedFont.font().getFontName(), base64Font);
143+
""".formatted(fontName, base64Font, fontName, fontName.replaceAll(" ", ""));
141144
}
142145

143146
public String getFamilyDeclaration() {

0 commit comments

Comments
 (0)