Browse Source

Hiddenchars (#221)

* Fix bug where an icons font is counted as two unused unicode ranges
* Display the number of hidden chars
Gaël Métais 8 năm trước cách đây
mục cha
commit
b201b32b46
3 tập tin đã thay đổi với 22 bổ sung5 xóa
  1. 5 0
      front/src/views/rule.html
  2. 1 1
      lib/metadata/policies.js
  3. 16 4
      lib/tools/redownload/redownload.js

+ 5 - 0
front/src/views/rule.html

@@ -455,6 +455,11 @@
                         </div>
                     </div>
                 </div>
+                <div ng-if="font.ligaturesOrHiddenChars > 0">
+                    <div><b>Ligatures or hidden chars</b></div>
+                    <div ng-class="{offenderProblem: (font.ligaturesOrHiddenChars > 25)}">{{font.ligaturesOrHiddenChars}} glyphs</div>
+                    <div></div>
+                </div>
             </div>
         </div>
     </div>

+ 1 - 1
lib/metadata/policies.js

@@ -1062,7 +1062,7 @@ var policies = {
     "unusedUnicodeRanges": {
         "tool": "redownload",
         "label": "Unused Unicode ranges",
-        "message": "<p>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.</p><p>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.</p><p>Tools such as <a href=\"https://www.fontsquirrel.com/tools/webfont-generator\" target=\"_blank\">Font Squirrel</a> can remove some unicode ranges from a font.</p><p>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.</p>",
+        "message": "<p>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.</p><p>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).</p><p>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.</p><p>Tools such as <a href=\"https://www.fontsquirrel.com/tools/webfont-generator\" target=\"_blank\">Font Squirrel</a> can remove some unicode ranges from a font.</p><p>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.</p>",
         "isOkThreshold": 0,
         "isBadThreshold": 8,
         "isAbnormalThreshold": 12,

+ 16 - 4
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;
             }
         });