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

Font Library: Load/Unload the font face in browser when toggling the variants #59066

Merged
merged 7 commits into from
Mar 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
setUIValuesNeeded,
mergeFontFamilies,
loadFontFaceInBrowser,
unloadFontFaceInBrowser,
getDisplaySrcFromFontFace,
makeFontFacesFormData,
makeFontFamilyFormData,
Expand Down Expand Up @@ -363,6 +364,12 @@ function FontLibraryProvider( { children } ) {
...fontFamilies,
[ font.source ]: newCustomFonts,
} );

if ( font.fontFace ) {
font.fontFace.forEach( ( face ) => {
unloadFontFaceInBrowser( face, 'all' );
} );
}
};

const activateCustomFontFamilies = ( fontsToAdd ) => {
Expand Down Expand Up @@ -398,6 +405,23 @@ function FontLibraryProvider( { children } ) {
...fontFamilies,
[ font.source ]: newFonts,
} );

const isFaceActivated = isFontActivated(
font.slug,
face.fontStyle,
face.fontWeight,
font.source
);

if ( isFaceActivated ) {
loadFontFaceInBrowser(
face,
getDisplaySrcFromFontFace( face.src ),
'all'
);
} else {
unloadFontFaceInBrowser( face, 'all' );
}
};

const loadFontFaceAsset = async ( fontFace ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ export async function loadFontFaceInBrowser( fontFace, source, addTo = 'all' ) {
}
}

/*
* Unloads the font face and remove it from the browser.
* It also removes it from the iframe document.
*
* Note that Font faces that were added to the set using the CSS @font-face rule
* remain connected to the corresponding CSS, and cannot be deleted.
Comment on lines +128 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the meaning of this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from MDN. The fonts can be added to the FontFace set as follows, and the first method cannot be removed from the FontFace set.

  • The CSS rule
  • The document.fonts.add

*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/delete.
*/
export function unloadFontFaceInBrowser( fontFace, removeFrom = 'all' ) {
const unloadFontFace = ( fonts ) => {
fonts.forEach( ( f ) => {
if (
f.family === formatFontFaceName( fontFace.fontFamily ) &&
f.weight === fontFace.fontWeight &&
f.style === fontFace.fontStyle
) {
fonts.delete( f );
}
} );
};

if ( removeFrom === 'document' || removeFrom === 'all' ) {
unloadFontFace( document.fonts );
}

if ( removeFrom === 'iframe' || removeFrom === 'all' ) {
const iframeDocument = document.querySelector(
'iframe[name="editor-canvas"]'
).contentDocument;
unloadFontFace( iframeDocument.fonts );
}
}

/**
* Retrieves the display source from a font face src.
*
Expand Down