change the format of font faces to something closer to the css

pull/21833/head
Bruno Windels 2020-06-24 14:54:14 +02:00
parent 3b13a623cd
commit b3fd1eda03
1 changed files with 38 additions and 7 deletions

View File

@ -49,15 +49,46 @@ function clearCustomTheme() {
}
}
const allowedFontFaceProps = [
"font-display",
"font-family",
"font-stretch",
"font-style",
"font-weight",
"font-variant",
"font-feature-settings",
"font-variation-settings",
"src",
"unicode-range"
];
function generateCustomFontFaceCSS(faces) {
return Object.entries(faces).map(([fontName, face]) => {
const src = Object.entries(face.src).map(([format, url]) => {
return `url('${url}') format('${format}')`;
return faces.map(face => {
const src = face.src && face.src.map(srcElement => {
let format;
if (srcElement.format) {
format = `format("${srcElement.format}")`;
}
if (srcElement.url) {
return `url("${srcElement.url}") ${format}`;
} else if (srcElement.local) {
return `local("${srcElement.local}") ${format}`;
}
return "";
}).join(", ");
return `@font-face {` +
` font-family: '${fontName}';` +
` src: ${src};` +
`}`;
const props = Object.keys(face).filter(prop => allowedFontFaceProps.includes(prop));
const body = props.map(prop => {
let value;
if (prop === "src") {
value = src;
} else if (prop === "font-family") {
value = `"${face[prop]}"`;
} else {
value = face[prop];
}
return `${prop}: ${value}`;
}).join(";");
return `@font-face {${body}}`;
}).join("\n");
}