fonts rework - add fonts to downloaded image

This commit is contained in:
Azgaar 2021-09-07 00:19:31 +03:00
parent 7a844f8810
commit 2492cad3e0
3 changed files with 40 additions and 46 deletions

View file

@ -277,12 +277,20 @@ async function getMapURL(type, options = {}) {
// TODO: add dataURL for all used fonts
const usedFonts = getUsedFonts(cloneEl);
const websafe = ["Georgia", "Times+New+Roman", "Comic+Sans+MS", "Lucida+Sans+Unicode", "Courier+New", "Verdana", "Arial", "Impact"];
const fontsToLoad = usedFonts.filter(font => !websafe.includes(font));
const fontsToLoad = usedFonts.filter(font => font.src);
if (fontsToLoad.length) {
const url = "https://fonts.googleapis.com/css?family=" + fontsToLoad.join("|");
const fontStyle = await convertFontToDataURI(url);
if (fontStyle) clone.select("defs").append("style").text(fontStyle.join("\n"));
const dataURLfonts = await loadFontsAsDataURI(fontsToLoad);
const fontFaces = dataURLfonts
.map(({family, src, unicodeRange = "", variant = "normal"}) => {
return `@font-face {font-family: "${family}"; src: ${src}; unicode-range: ${unicodeRange}; font-variant: ${variant};}`;
})
.join("\n");
const style = document.createElement("style");
style.setAttribute("type", "text/css");
style.innerHTML = fontFaces;
cloneEl.querySelector("defs").appendChild(style);
}
clone.remove();