Jelajahi Sumber

takes into account loaded fonts

Gildas 7 tahun lalu
induk
melakukan
a8637d75b5
1 mengubah file dengan 8 tambahan dan 8 penghapusan
  1. 8 8
      lib/single-file/doc-helper.js

+ 8 - 8
lib/single-file/doc-helper.js

@@ -81,7 +81,7 @@ this.docHelper = this.docHelper || (() => {
 					elementsInfo = new Map(Array.from(elementsInfo).filter(([element]) => element.getAttribute(markerRemovedContent) != ""));
 				}
 				if (options.removeUnusedStyles) {
-					usedFonts = getUsedFonts(elementsInfo);
+					usedFonts = getUsedFonts(elementsInfo, Array.from(doc.fonts));
 				}
 				if (options.compressHTML) {
 					elementsInfo.forEach((elementInfo, element) => {
@@ -116,19 +116,19 @@ this.docHelper = this.docHelper || (() => {
 		};
 	}
 
-	function getUsedFonts(styles) {
+	function getUsedFonts(styles, loadedFonts) {
 		const usedFonts = new Set();
 		styles.forEach(style => {
 			const fontFamilyNames = style.fontFamily.split(",");
 			fontFamilyNames.forEach(fontFamilyName => {
-				style.fontFamily = removeQuotes(fontFamilyName).toLowerCase().trim();
-				usedFonts.add(getFontKey(style));
+				fontFamilyName = removeQuotes(fontFamilyName.trim()).toLowerCase();
+				if (loadedFonts.find(font => font.family.toLowerCase() == fontFamilyName && font.style == style.fontStyle && font.weight == style.fontWeight && font.weight == style.fontWeight && font.variant == style.fontVariant)) {
+					const { fontWeight, fontStyle, fontVariant } = style;
+					usedFonts.add([fontFamilyName, fontWeight, fontStyle, fontVariant]);
+				}
 			});
 		});
-		return Array.from(usedFonts).map(key => {
-			const [fontFamily, fontWeight, fontStyle, fontVariant] = JSON.parse(key);
-			return { fontFamily, fontWeight, fontStyle, fontVariant };
-		});
+		return Array.from(usedFonts);
 	}
 
 	function getElementsInfo(win, element, elementsInfo = new Map()) {