|
| 1 | +/* global text */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Determines if a given text node is an icon ligature |
| 5 | + * |
| 6 | + * @method isIconLigature |
| 7 | + * @memberof axe.commons.text |
| 8 | + * @instance |
| 9 | + * @param {VirtualNode} textVNode The virtual text node |
| 10 | + * @param {Number} occuranceThreshold Number of times the font is encountered before auto-assigning the font as a ligature or not |
| 11 | + * @param {Number} differenceThreshold Percent of differences in pixel data or pixel width needed to determine if a font is a ligature font |
| 12 | + * @return {Boolean} |
| 13 | + */ |
| 14 | +text.isIconLigature = function( |
| 15 | + textVNode, |
| 16 | + differenceThreshold = 0.15, |
| 17 | + occuranceThreshold = 3 |
| 18 | +) { |
| 19 | + /** |
| 20 | + * Determine if the visible text is a ligature by comparing the |
| 21 | + * first letters image data to the entire strings image data. |
| 22 | + * If the two images are significantly different (typical set to 5% |
| 23 | + * statistical significance, but we'll be using a slightly higher value |
| 24 | + * of 15% to help keep the size of the canvas down) then we know the text |
| 25 | + * has been replaced by a ligature. |
| 26 | + * |
| 27 | + * Example: |
| 28 | + * If a text node was the string "File", looking at just the first |
| 29 | + * letter "F" would produce the following image: |
| 30 | + * |
| 31 | + * ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ |
| 32 | + * │ │ │█│█│█│█│█│█│█│█│█│█│█│ │ │ |
| 33 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 34 | + * │ │ │█│█│█│█│█│█│█│█│█│█│█│ │ │ |
| 35 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 36 | + * │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ |
| 37 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 38 | + * │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ |
| 39 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 40 | + * │ │ │█│█│█│█│█│█│█│ │ │ │ │ │ │ |
| 41 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 42 | + * │ │ │█│█│█│█│█│█│█│ │ │ │ │ │ │ |
| 43 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 44 | + * │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ |
| 45 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 46 | + * │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ |
| 47 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 48 | + * │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ |
| 49 | + * └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ |
| 50 | + * |
| 51 | + * But if the entire string "File" produced an image which had at least |
| 52 | + * a 15% difference in pixels, we would know that the string was replaced |
| 53 | + * by a ligature: |
| 54 | + * |
| 55 | + * ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ |
| 56 | + * │ │█│█│█│█│█│█│█│█│█│█│ │ │ │ │ |
| 57 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 58 | + * │ │█│ │ │ │ │ │ │ │ │█│█│ │ │ │ |
| 59 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 60 | + * │ │█│ │█│█│█│█│█│█│ │█│ │█│ │ │ |
| 61 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 62 | + * │ │█│ │ │ │ │ │ │ │ │█│█│█│█│ │ |
| 63 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 64 | + * │ │█│ │█│█│█│█│█│█│ │ │ │ │█│ │ |
| 65 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 66 | + * │ │█│ │ │ │ │ │ │ │ │ │ │ │█│ │ |
| 67 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 68 | + * │ │█│ │█│█│█│█│█│█│█│█│█│ │█│ │ |
| 69 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 70 | + * │ │█│ │ │ │ │ │ │ │ │ │ │ │█│ │ |
| 71 | + * ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
| 72 | + * │ │█│█│█│█│█│█│█│█│█│█│█│█│█│ │ |
| 73 | + * └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ |
| 74 | + */ |
| 75 | + const nodeValue = textVNode.actualNode.nodeValue; |
| 76 | + |
| 77 | + // text with unicode or non-bmp letters cannot be ligature icons |
| 78 | + if ( |
| 79 | + !text.sanitize(nodeValue) || |
| 80 | + text.hasUnicode(nodeValue, { emoji: true, nonBmp: true }) |
| 81 | + ) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + if (!axe._cache.get('context')) { |
| 86 | + axe._cache.set( |
| 87 | + 'context', |
| 88 | + document.createElement('canvas').getContext('2d') |
| 89 | + ); |
| 90 | + } |
| 91 | + const context = axe._cache.get('context'); |
| 92 | + const canvas = context.canvas; |
| 93 | + |
| 94 | + // keep track of each font encountered and the number of times it shows up |
| 95 | + // as a ligature. |
| 96 | + if (!axe._cache.get('fonts')) { |
| 97 | + axe._cache.set('fonts', {}); |
| 98 | + } |
| 99 | + const fonts = axe._cache.get('fonts'); |
| 100 | + |
| 101 | + const style = window.getComputedStyle(textVNode.parent.actualNode); |
| 102 | + const fontFamily = style.getPropertyValue('font-family'); |
| 103 | + |
| 104 | + if (!fonts[fontFamily]) { |
| 105 | + fonts[fontFamily] = { |
| 106 | + occurances: 0, |
| 107 | + numLigatures: 0 |
| 108 | + }; |
| 109 | + } |
| 110 | + const font = fonts[fontFamily]; |
| 111 | + |
| 112 | + // improve the performance by only comparing the image data of a font |
| 113 | + // a certain number of times |
| 114 | + if (font.occurances >= occuranceThreshold) { |
| 115 | + // if the font has always been a ligature assume it's a ligature font |
| 116 | + if (font.numLigatures / font.occurances === 1) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + // inversely, if it's never been a ligature assume it's not a ligature font |
| 120 | + else if (font.numLigatures === 0) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + // we could theoretically get into an odd middle ground scenario in which |
| 125 | + // the font family is being used as normal text sometimes and as icons |
| 126 | + // other times. in these cases we would need to always check the text |
| 127 | + // to know if it's an icon or not |
| 128 | + } |
| 129 | + font.occurances++; |
| 130 | + |
| 131 | + // 30px was chosen to account for common ligatures in normal fonts |
| 132 | + // such as fi, ff, ffi. If a ligature would add a single column of |
| 133 | + // pixels to a 30x30 grid, it would not meet the statistical significance |
| 134 | + // threshold of 15% (30x30 = 900; 30/900 = 3.333%). this also allows for |
| 135 | + // more than 1 column differences (60/900 = 6.666%) and things like |
| 136 | + // extending the top of the f in the fi ligature. |
| 137 | + let fontSize = 30; |
| 138 | + let fontStyle = `${fontSize}px ${fontFamily}`; |
| 139 | + |
| 140 | + // set the size of the canvas to the width of the first letter |
| 141 | + context.font = fontStyle; |
| 142 | + const firstChar = nodeValue.charAt(0); |
| 143 | + let width = context.measureText(firstChar).width; |
| 144 | + |
| 145 | + // ensure font meets the 30px width requirement (30px font-size doesn't |
| 146 | + // necessarily mean its 30px wide when drawn) |
| 147 | + if (width < 30) { |
| 148 | + const diff = 30 / width; |
| 149 | + width *= diff; |
| 150 | + fontSize *= diff; |
| 151 | + fontStyle = `${fontSize}px ${fontFamily}`; |
| 152 | + } |
| 153 | + canvas.width = width; |
| 154 | + canvas.height = fontSize; |
| 155 | + |
| 156 | + // changing the dimensions of a canvas resets all properties (include font) |
| 157 | + // and clears it |
| 158 | + context.font = fontStyle; |
| 159 | + context.textAlign = 'left'; |
| 160 | + context.textBaseline = 'top'; |
| 161 | + context.fillText(firstChar, 0, 0); |
| 162 | + const compareData = new Uint32Array( |
| 163 | + context.getImageData(0, 0, width, fontSize).data.buffer |
| 164 | + ); |
| 165 | + |
| 166 | + // if the font doesn't even have character data for a single char then |
| 167 | + // it has to be an icon ligature (e.g. Material Icon) |
| 168 | + if (!compareData.some(pixel => pixel)) { |
| 169 | + font.numLigatures++; |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + context.clearRect(0, 0, width, fontSize); |
| 174 | + context.fillText(nodeValue, 0, 0); |
| 175 | + const compareWith = new Uint32Array( |
| 176 | + context.getImageData(0, 0, width, fontSize).data.buffer |
| 177 | + ); |
| 178 | + |
| 179 | + // calculate the number of differences between the first letter and the |
| 180 | + // entire string, ignoring color differences |
| 181 | + const differences = compareData.reduce((diff, pixel, i) => { |
| 182 | + if (pixel === 0 && compareWith[i] === 0) { |
| 183 | + return diff; |
| 184 | + } |
| 185 | + if (pixel !== 0 && compareWith[i] !== 0) { |
| 186 | + return diff; |
| 187 | + } |
| 188 | + return ++diff; |
| 189 | + }, 0); |
| 190 | + |
| 191 | + // calculate the difference between the width of each character and the |
| 192 | + // combined with of all characters |
| 193 | + const expectedWidth = nodeValue.split('').reduce((width, char) => { |
| 194 | + return width + context.measureText(char).width; |
| 195 | + }, 0); |
| 196 | + const actualWidth = context.measureText(nodeValue).width; |
| 197 | + |
| 198 | + const pixelDifference = differences / compareData.length; |
| 199 | + const sizeDifference = 1 - actualWidth / expectedWidth; |
| 200 | + |
| 201 | + if ( |
| 202 | + pixelDifference >= differenceThreshold && |
| 203 | + sizeDifference >= differenceThreshold |
| 204 | + ) { |
| 205 | + font.numLigatures++; |
| 206 | + return true; |
| 207 | + } |
| 208 | + |
| 209 | + return false; |
| 210 | +}; |
0 commit comments