Skip to content

Commit 2dd5838

Browse files
committed
Best guess font fallback based on name (in case font is not installed)
1 parent bbcc879 commit 2dd5838

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/utils/getFontStyles.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ export const getFontStyles = (fontObj, defaultSize = 12) => { // TODO remove def
22
const dflt = { fontSize: `${defaultSize}px` };
33
// TODO! no fontScale used?!
44
if (!fontObj) return dflt;
5-
5+
66
const fontProperties = fontObj?.Properties;
7-
7+
88
if (!fontProperties) return dflt;
9-
9+
10+
// Add generic CSS fallback based on font name
11+
const getFontFamily = (pname) => {
12+
if (!pname) return "inherit";
13+
const lower = pname.toLowerCase();
14+
if (lower.includes("courier") || lower.includes("mono") || lower.includes("consolas")) {
15+
return `"${pname}", monospace`;
16+
}
17+
if (lower.includes("times") || lower.includes("georgia") || lower.includes("serif")) {
18+
return `"${pname}", serif`;
19+
}
20+
return `"${pname}", sans-serif`;
21+
};
22+
1023
return {
11-
fontFamily: fontProperties.PName || "inherit",
24+
fontFamily: getFontFamily(fontProperties.PName),
1225
fontSize: fontProperties.Size
1326
? `${fontProperties.Size}px`
1427
: `${defaultSize}px`,

0 commit comments

Comments
 (0)