Bladeren bron

fixed fonts removal regression

Gildas 7 jaren geleden
bovenliggende
commit
9249dbaee5
1 gewijzigde bestanden met toevoegingen van 46 en 4 verwijderingen
  1. 46 4
      lib/single-file/css-fonts-minifier.js

+ 46 - 4
lib/single-file/css-fonts-minifier.js

@@ -100,7 +100,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 			const docContent = doc.body.innerText + pseudoElementsContent;
 			doc.querySelectorAll("style").forEach(style => {
 				if (style.sheet) {
-					style.textContent = filterUnusedFonts(doc, style.sheet.cssRules, unusedFonts, filteredUsedFonts, docContent);
+					style.textContent = filterUnusedFonts(doc, style.sheet.cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
 					stats.rules.discarded -= style.sheet.cssRules.length;
 				}
 			});
@@ -281,27 +281,43 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		return cssText;
 	}
 
-	function filterUnusedFonts(doc, rules, unusedFonts, filteredUsedFonts, docContent) {
+	function filterUnusedFonts(doc, rules, declaredFonts, unusedFonts, filteredUsedFonts, docContent) {
 		let stylesheetContent = "";
 		if (rules) {
 			Array.from(rules).forEach(rule => {
 				if (rule.media) {
 					stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + "{";
-					stylesheetContent += filterUnusedFonts(doc, rule.cssRules, unusedFonts, filteredUsedFonts, docContent);
+					stylesheetContent += filterUnusedFonts(doc, rule.cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
 					stylesheetContent += "}";
 				} else if (rule.type == CSSRule.FONT_FACE_RULE) {
 					if (rule.style) {
 						const fontFamily = removeQuotes(rule.style.getPropertyValue("font-family"));
-						if (fontFamily && !unusedFonts.includes(fontFamily)) {
+						if (fontFamily && !unusedFonts.find(fontInfo => fontInfo.familyName == fontFamily)) {
 							let optionalTest;
 							const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(fontFamily);
 							if (optionalUsedFonts && optionalUsedFonts.length) {
 								const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
+								// const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
 								optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontStyle == fontStyle);
 								if (optionalTest) {
 									const fontVariant = rule.style.getPropertyValue("font-variant");
 									optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures");
 								}
+								/*							
+								if (optionalTest) {
+									if (isNaN(fontWeight)) {
+										optionalTest = true;
+									} else {
+										const usedFontWeights = optionalUsedFonts.map(fontInfo => fontInfo.fontWeight);
+										const declaredFontsWeights = declaredFonts
+											.filter(fontInfo => fontInfo.familyName == fontFamily && fontInfo.fontStyle == fontStyle && (fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures"))
+											.map(fontInfo => fontInfo.fontWeight)
+											.sort((w1, w2) => w1 - w2);
+										const usedComputedFontWeights = usedFontWeights.map(fontWeight => findFontWeight(fontWeight, declaredFontsWeights));
+										optionalTest = usedComputedFontWeights.includes(fontWeight);
+									}
+								}
+								*/
 							} else {
 								optionalTest = true;
 							}
@@ -318,6 +334,32 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		return stylesheetContent;
 	}
 
+	function findFontWeight(fontWeight, fontWeights) {
+		let foundWeight;
+		if (fontWeight >= 400 && fontWeight <= 500) {
+			foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
+			if (!foundWeight) {
+				foundWeight = fontWeights.slice().reverse().find(weight => weight < fontWeight);
+			}
+			if (!foundWeight) {
+				foundWeight = fontWeights.find(weight => weight > fontWeight);
+			}
+		}
+		if (fontWeight < 400) {
+			foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
+			if (!foundWeight) {
+				foundWeight = fontWeights.find(weight => weight > fontWeight);
+			}
+		}
+		if (fontWeight > 500) {
+			foundWeight = fontWeights.find(weight => weight >= fontWeight);
+			if (!foundWeight) {
+				foundWeight = fontWeights.slice().reverse().find(weight => weight < fontWeight);
+			}
+		}
+		return foundWeight;
+	}
+
 	function getPseudoElementsContent(doc, rules) {
 		if (rules) {
 			return Array.from(rules).map(rule => {