-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(label-content-name-mismatch): ignore ligature fonts #1829
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
21dc3c4
fix(label-content-name-mismatch): ignore ligature fonts
straker d2c7dca
move to own function
straker d1aa526
finalize tests
straker 753c464
increase time
straker 7fa7281
use font api
straker 7f53109
use roboto to test text ligatures
straker 83ae8e0
ignore for windows
straker fda562a
more time?
straker d56270a
use woff files for google fonts
straker addc668
try hosting font
straker 746040e
no ligature font
straker ec8da4b
no integration
straker 0bbeecb
Merge branch 'develop' into ignoreLigatures
straker 5001b0c
dont flag programming ligs
straker 149d008
Merge branch 'ignoreLigatures' of https://github.com/dequelabs/axe-co…
straker 8c8fdee
no Uint32Array
straker 2b9aa1c
Revert "no Uint32Array"
straker ee8d552
dont use .some on Uint32Array
straker 04abd49
try fixing reduce
straker f9d8f63
polyfill some and reduce
straker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* global text */ | ||
|
||
/** | ||
* Determines if a given text node is an icon ligature | ||
* | ||
* @method isIconLigature | ||
* @memberof axe.commons.text | ||
* @instance | ||
* @param {VirtualNode} textVNode The virtual text node | ||
* @param {Number} occuranceThreshold Number of times the font is encountered before auto-assigning the font as a ligature or not | ||
* @param {Number} differenceThreshold Percent of differences in pixel data or pixel width needed to determine if a font is a ligature font | ||
* @return {Boolean} | ||
*/ | ||
text.isIconLigature = function( | ||
textVNode, | ||
differenceThreshold = 0.15, | ||
occuranceThreshold = 3 | ||
) { | ||
/** | ||
* Determine if the visible text is a ligature by comparing the | ||
* first letters image data to the entire strings image data. | ||
* If the two images are significantly different (typical set to 5% | ||
* statistical significance, but we'll be using a slightly higher value | ||
* of 15% to help keep the size of the canvas down) then we know the text | ||
* has been replaced by a ligature. | ||
* | ||
* Example: | ||
* If a text node was the string "File", looking at just the first | ||
* letter "F" would produce the following image: | ||
* | ||
* ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ | ||
* │ │ │█│█│█│█│█│█│█│█│█│█│█│ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│█│█│█│█│█│█│█│█│█│ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│█│█│█│█│█│ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│█│█│█│█│█│ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │ │█│█│ │ │ │ │ │ │ │ │ │ │ │ | ||
* └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ | ||
* | ||
* But if the entire string "File" produced an image which had at least | ||
* a 15% difference in pixels, we would know that the string was replaced | ||
* by a ligature: | ||
* | ||
* ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ | ||
* │ │█│█│█│█│█│█│█│█│█│█│ │ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │ │ │ │ │ │ │ │█│█│ │ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │█│█│█│█│█│█│ │█│ │█│ │ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │ │ │ │ │ │ │ │█│█│█│█│ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │█│█│█│█│█│█│ │ │ │ │█│ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │ │ │ │ │ │ │ │ │ │ │█│ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │█│█│█│█│█│█│█│█│█│ │█│ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│ │ │ │ │ │ │ │ │ │ │ │█│ │ | ||
* ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ | ||
* │ │█│█│█│█│█│█│█│█│█│█│█│█│█│ │ | ||
* └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ | ||
*/ | ||
const nodeValue = textVNode.actualNode.nodeValue; | ||
|
||
// text with unicode or non-bmp letters cannot be ligature icons | ||
if ( | ||
!text.sanitize(nodeValue) || | ||
text.hasUnicode(nodeValue, { emoji: true, nonBmp: true }) | ||
) { | ||
return false; | ||
} | ||
|
||
if (!axe._cache.get('context')) { | ||
axe._cache.set( | ||
'context', | ||
document.createElement('canvas').getContext('2d') | ||
); | ||
} | ||
const context = axe._cache.get('context'); | ||
const canvas = context.canvas; | ||
|
||
// keep track of each font encountered and the number of times it shows up | ||
// as a ligature. | ||
if (!axe._cache.get('fonts')) { | ||
axe._cache.set('fonts', {}); | ||
} | ||
const fonts = axe._cache.get('fonts'); | ||
|
||
const style = window.getComputedStyle(textVNode.parent.actualNode); | ||
const fontFamily = style.getPropertyValue('font-family'); | ||
|
||
if (!fonts[fontFamily]) { | ||
fonts[fontFamily] = { | ||
occurances: 0, | ||
numLigatures: 0 | ||
}; | ||
} | ||
const font = fonts[fontFamily]; | ||
|
||
// improve the performance by only comparing the image data of a font | ||
// a certain number of times | ||
if (font.occurances >= occuranceThreshold) { | ||
// if the font has always been a ligature assume it's a ligature font | ||
if (font.numLigatures / font.occurances === 1) { | ||
return true; | ||
} | ||
// inversely, if it's never been a ligature assume it's not a ligature font | ||
else if (font.numLigatures === 0) { | ||
return false; | ||
} | ||
|
||
// we could theoretically get into an odd middle ground scenario in which | ||
// the font family is being used as normal text sometimes and as icons | ||
// other times. in these cases we would need to always check the text | ||
// to know if it's an icon or not | ||
} | ||
font.occurances++; | ||
|
||
// 30px was chosen to account for common ligatures in normal fonts | ||
// such as fi, ff, ffi. If a ligature would add a single column of | ||
// pixels to a 30x30 grid, it would not meet the statistical significance | ||
// threshold of 15% (30x30 = 900; 30/900 = 3.333%). this also allows for | ||
// more than 1 column differences (60/900 = 6.666%) and things like | ||
// extending the top of the f in the fi ligature. | ||
straker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let fontSize = 30; | ||
let fontStyle = `${fontSize}px ${fontFamily}`; | ||
|
||
// set the size of the canvas to the width of the first letter | ||
context.font = fontStyle; | ||
const firstChar = nodeValue.charAt(0); | ||
let width = context.measureText(firstChar).width; | ||
|
||
// ensure font meets the 30px width requirement (30px font-size doesn't | ||
// necessarily mean its 30px wide when drawn) | ||
if (width < 30) { | ||
const diff = 30 / width; | ||
width *= diff; | ||
fontSize *= diff; | ||
fontStyle = `${fontSize}px ${fontFamily}`; | ||
} | ||
canvas.width = width; | ||
canvas.height = fontSize; | ||
|
||
// changing the dimensions of a canvas resets all properties (include font) | ||
// and clears it | ||
context.font = fontStyle; | ||
context.textAlign = 'left'; | ||
context.textBaseline = 'top'; | ||
context.fillText(firstChar, 0, 0); | ||
const compareData = new Uint32Array( | ||
context.getImageData(0, 0, width, fontSize).data.buffer | ||
); | ||
|
||
// if the font doesn't even have character data for a single char then | ||
// it has to be an icon ligature (e.g. Material Icon) | ||
if (!compareData.some(pixel => pixel)) { | ||
font.numLigatures++; | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I like this. |
||
} | ||
|
||
context.clearRect(0, 0, width, fontSize); | ||
context.fillText(nodeValue, 0, 0); | ||
const compareWith = new Uint32Array( | ||
context.getImageData(0, 0, width, fontSize).data.buffer | ||
); | ||
|
||
// calculate the number of differences between the first letter and the | ||
// entire string, ignoring color differences | ||
const differences = compareData.reduce((diff, pixel, i) => { | ||
if (pixel === 0 && compareWith[i] === 0) { | ||
return diff; | ||
} | ||
if (pixel !== 0 && compareWith[i] !== 0) { | ||
return diff; | ||
} | ||
return ++diff; | ||
}, 0); | ||
|
||
// calculate the difference between the width of each character and the | ||
// combined with of all characters | ||
const expectedWidth = nodeValue.split('').reduce((width, char) => { | ||
return width + context.measureText(char).width; | ||
}, 0); | ||
const actualWidth = context.measureText(nodeValue).width; | ||
|
||
const pixelDifference = differences / compareData.length; | ||
const sizeDifference = 1 - actualWidth / expectedWidth; | ||
|
||
if ( | ||
pixelDifference >= differenceThreshold && | ||
sizeDifference >= differenceThreshold | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now it's fine but later we might find we need to split pixel and size diff thresholds. |
||
) { | ||
font.numLigatures++; | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* global text */ | ||
|
||
/** | ||
* Returns an array of visible text virtual nodes | ||
|
||
* @method visibleTextNodes | ||
* @memberof axe.commons.text | ||
* @instance | ||
* @param {VirtualNode} vNode | ||
* @return {VitrualNode[]} | ||
*/ | ||
text.visibleTextNodes = function(vNode) { | ||
const parentVisible = axe.commons.dom.isVisible(vNode.actualNode); | ||
let nodes = []; | ||
vNode.children.forEach(child => { | ||
if (child.actualNode.nodeType === 3) { | ||
if (parentVisible) { | ||
nodes.push(child); | ||
} | ||
} else { | ||
nodes = nodes.concat(text.visibleTextNodes(child)); | ||
} | ||
}); | ||
return nodes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dislike this name "context" we have something else called context. Can you rename this to canvasContext or whatever? (Please open an issue)