From ce16fa154c729d262b9660fea5a1cf32b8376cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Tue, 10 Jan 2017 13:54:43 +0800 Subject: [PATCH] Hiddenchars (#221) * Fix bug where an icons font is counted as two unused unicode ranges * Display the number of hidden chars --- front/src/views/rule.html | 5 +++++ lib/metadata/policies.js | 2 +- lib/tools/redownload/redownload.js | 20 ++++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/front/src/views/rule.html b/front/src/views/rule.html index f8d33d8..c8c00fb 100644 --- a/front/src/views/rule.html +++ b/front/src/views/rule.html @@ -455,6 +455,11 @@ +
+
Ligatures or hidden chars
+
{{font.ligaturesOrHiddenChars}} glyphs
+
+
diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js index 0bfa76f..d00f6e0 100644 --- a/lib/metadata/policies.js +++ b/lib/metadata/policies.js @@ -1062,7 +1062,7 @@ var policies = { "unusedUnicodeRanges": { "tool": "redownload", "label": "Unused Unicode ranges", - "message": "

This metric counts the number of unused Unicode ranges inside each font. For example, one font could include Cyrillic glyphs but none of them are used on the page.

Because of technical limitations, Yellow Lab Tools checks each font against the glyphs of the entire page. As a result, estimated use is >= to reality. For example, if you read that 10 glyphs are \"possibly used\", it means that these 10 glyphs are used on the page but nothing guaranties that they are displayed using this font.

Tools such as Font Squirrel can remove some unicode ranges from a font.

In the case of an icon font, make sure you only keep the icons that are used on the website and to remove the others. Several tools are able to extract SVG images from a font, then some other tools can generate a font from the SVGs you want to keep.

", + "message": "

This metric counts the number of unused Unicode ranges inside each font. For example, one font could include Cyrillic glyphs but none of them are used on the page.

It also reveals the number of ligatures (letters that are represented differently when close to each other) and hidden chars (glyphs not linked to the unicode system that can't be displayed on the web).

Because of technical limitations, Yellow Lab Tools checks each font against the glyphs of the entire page. As a result, estimated use is >= to reality. For example, if you read that 10 glyphs are \"possibly used\", it means that these 10 glyphs are used on the page but nothing guaranties that they are displayed using this font.

Tools such as Font Squirrel can remove some unicode ranges from a font.

In the case of an icon font, make sure you only keep the icons that are used on the website and to remove the others. Several tools are able to extract SVG images from a font, then some other tools can generate a font from the SVGs you want to keep.

", "isOkThreshold": 0, "isBadThreshold": 8, "isAbnormalThreshold": 12, diff --git a/lib/tools/redownload/redownload.js b/lib/tools/redownload/redownload.js index c79c71c..0e34fff 100644 --- a/lib/tools/redownload/redownload.js +++ b/lib/tools/redownload/redownload.js @@ -500,6 +500,8 @@ var Redownload = function() { var ranges = []; var others = null; var rangeNames = Object.keys(req.fontMetrics.unicodeRanges); + var unicodePointsCount = 0; + var unusedRangesInFont = 0; rangeNames.forEach(function(rangeName) { var range = req.fontMetrics.unicodeRanges[rangeName]; @@ -508,18 +510,20 @@ var Redownload = function() { if (rangeName === 'Others') { if (range.numGlyphsInCommonWithPageContent === 0 && range.charset.length > 50) { range.underused = true; - unusedUnicodeRanges ++; + unusedRangesInFont ++; } + unicodePointsCount += range.charset.length; others = range; } else if (range.charset.length > 0) { // Now lets detect if the current Unicode range is unused. // Reminder: range.coverage = glyphs declared in this range, divided by the range size if (range.coverage > 0.25 && range.numGlyphsInCommonWithPageContent === 0) { range.underused = true; - unusedUnicodeRanges ++; + unusedRangesInFont ++; } + unicodePointsCount += range.charset.length; ranges.push(range); } }); @@ -539,7 +543,7 @@ var Redownload = function() { // And if less than 5% of the icons are used, let's report it as underused if (others && others.numGlyphsInCommonWithPageContent / others.charset.length <= 0.05) { - unusedUnicodeRanges ++; + unusedRangesInFont = 1; } // Not an icons font @@ -549,13 +553,21 @@ var Redownload = function() { ranges.push(others); } + var ligaturesOrHiddenChars = req.fontMetrics.numGlyphs - unicodePointsCount; + if (ligaturesOrHiddenChars > 25) { + unusedUnicodeRanges ++; + } + list.push({ url: req.url, weight: req.weightCheck.bodySize, isIconFont: false, - unicodeRanges: ranges + unicodeRanges: ranges, + ligaturesOrHiddenChars: ligaturesOrHiddenChars }); } + + unusedUnicodeRanges += unusedRangesInFont; } });