Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tab title font is different when exporting #596

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/flat-mice-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@codeimage/dom-export': patch
'@codeimage/app': patch
---

fix: add inter font on node export result
1 change: 1 addition & 0 deletions apps/codeimage/src/hooks/use-export-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function exportImage(
quality: quality,
experimental_optimizeFontLoading: true,
experimental_safariResourceFix: true,
experimental_includeExternalFonts: ['inter var'],
};

async function exportByMode(ref: HTMLElement) {
Expand Down
15 changes: 13 additions & 2 deletions packages/dom-export/src/lib/embedWebFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,18 @@ export function getUsedFontFamiliesByNode<T extends Element>(
fontsMap: FontsMap,
node: T,
cssStyleRules: CSSStyleRule[],
experimental_includeExternalFonts: string[],
): CSSStyleRule[] {
const fontFamilies = getUsedFontFamiliesRecursively([], fontsMap, node, null);
return cssStyleRules.reduce((acc, styleRule) => {
const {fontFamily, fontWeight, fontStyle} =
getStylesheetFontValues(styleRule);

const id = `${getFontName(fontFamily)[0]}-${fontWeight}-${fontStyle}`;
if (!fontFamilies.includes(id)) {
if (
!fontFamilies.includes(id) &&
!experimental_includeExternalFonts.some(font => font.startsWith(id))
) {
return acc;
}
return acc.concat(styleRule);
Expand All @@ -312,6 +316,7 @@ function getWebFontRules(
node: HTMLElement,
cssRules: CSSStyleRule[],
experimental_optimizeFontLoading: boolean,
experimental_includeExternalFonts: string[],
): CSSStyleRule[] {
const webFontsRules = cssRules
.filter(rule => rule.type === CSSRule.FONT_FACE_RULE)
Expand All @@ -321,7 +326,12 @@ function getWebFontRules(
return webFontsRules;
}
const fontsMap = getFontsMap(webFontsRules);
return getUsedFontFamiliesByNode(fontsMap, node, webFontsRules);
return getUsedFontFamiliesByNode(
fontsMap,
node,
webFontsRules,
experimental_includeExternalFonts,
);
}

async function parseWebFontRules<T extends HTMLElement>(
Expand All @@ -339,6 +349,7 @@ async function parseWebFontRules<T extends HTMLElement>(
node,
cssRules,
options.experimental_optimizeFontLoading ?? false,
options.experimental_includeExternalFonts ?? [],
);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/dom-export/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export interface Options {
*/
experimental_optimizeFontLoading?: boolean;

/**
*
* Add matching document external fonts even if not defined in the exported Node
*
*/
experimental_includeExternalFonts?: string[];

/**
*
* Apply fixes for Safari IOS and MacOS to load external resources correctly
Expand Down
Loading