fonts rework - issue fixes

This commit is contained in:
Azgaar 2021-09-06 01:21:48 +03:00
parent 66db527255
commit 7a844f8810
3 changed files with 27 additions and 27 deletions

View file

@ -121,17 +121,22 @@ const fonts = [
} }
]; ];
declareDefaultFonts(); // execute once on load
function declareFont(font) { function declareFont(font) {
const {family, src, ...rest} = font; const {family, src, ...rest} = font;
if (!src) return; if (!src) return;
const fontFace = new FontFace(family, src, {...rest, display: "block"}); const fontFace = new FontFace(family, src, {...rest, display: "block"});
const variant = font.variant || "normal";
const isAdded = document.fonts.check(`${variant} 1em ${family}`);
if (isAdded) return;
document.fonts.add(fontFace); document.fonts.add(fontFace);
addFontOption(font.family); addFontOption(family);
}
function declareDefaultFonts() {
fonts.forEach(font => {
if (font.src) declareFont(font);
else addFontOption(font.family);
});
} }
function getUsedFonts(svg) { function getUsedFonts(svg) {
@ -143,6 +148,9 @@ function getUsedFonts(svg) {
if (font) usedFontFamilies.add(font); if (font) usedFontFamilies.add(font);
} }
const provinceFont = provs.attr("font-family");
if (provinceFont) usedFontFamilies.add(provinceFont);
const legend = svg.querySelector("#legend"); const legend = svg.querySelector("#legend");
const legendFont = legend?.getAttribute("font-family"); const legendFont = legend?.getAttribute("font-family");
if (legendFont) usedFontFamilies.add(legendFont); if (legendFont) usedFontFamilies.add(legendFont);
@ -151,23 +159,16 @@ function getUsedFonts(svg) {
return usedFonts; return usedFonts;
} }
function declareUsedFonts() {
const fontsInUse = getUsedFonts(svg.node());
fontsInUse.forEach(font => declareFont(font));
}
function addFontOption(family) { function addFontOption(family) {
const options = document.getElementById("styleSelectFont");
// const existingOption = options.querySelector(`[value="${family}"]`);
// if (existingOption) return;
const option = document.createElement("option"); const option = document.createElement("option");
option.value = family; option.value = family;
option.innerText = family; option.innerText = family;
option.style.fontFamily = family; option.style.fontFamily = family;
document.getElementById("styleSelectFont").add(option); options.add(option);
}
addWebsafeFontOptions(); // execute once on load
function addWebsafeFontOptions() {
const localFonts = fonts.filter(font => font.local);
localFonts.forEach(font => addFontOption(font.family));
} }
async function fetchGoogleFont(family) { async function fetchGoogleFont(family) {

View file

@ -221,7 +221,8 @@ function parseLoadedData(data) {
if (data[34]) { if (data[34]) {
const usedFonts = JSON.parse(data[34]); const usedFonts = JSON.parse(data[34]);
usedFonts.forEach(usedFont => { usedFonts.forEach(usedFont => {
const defaultFont = fonts.find(font => font.family === usedFont.family); const {family: usedFamily, unicodeRange: usedRange, variant: usedVariant} = usedFont;
const defaultFont = fonts.find(({family, unicodeRange, variant}) => family === usedFamily && unicodeRange === usedRange && variant === usedVariant);
if (!defaultFont) fonts.push(usedFont); if (!defaultFont) fonts.push(usedFont);
declareFont(usedFont); declareFont(usedFont);
}); });

File diff suppressed because one or more lines are too long