Repository navigation
fix(color-contrast): don't treat text in joining scripts as icon ligatures - #5508
rohitmulani63-ops wants to merge 1 commit into
Conversation
…tures Arabic and other joining scripts change letter shapes depending on the letters around them, so the full string never looks like its first letter drawn alone. isIconLigature then flagged normal Arabic text as an icon, and once a font was flagged three times, every text node in that font was skipped by color-contrast. Return false early for text that contains letters of a joining script. Closes: dequelabs#5507
|
Thanks for the pr. We have a lot of open prs at the moment so we'll try to get to this as soon as we can. |
There was a problem hiding this comment.
Thanks for the pr. However I don't think this is the correct fix. While investigating this I think the problem is that we aren't drawing the full text correctly onto the canvas. When we draw the first character we draw the ر character.
But when we draw the full text we end up with the last character ب.
Because of this we are comparing the wrong characters together.
The first issue seems to be that we're using left for textAlign, and that should change to start to support right-to-left languages. Additionally we need to change where our starting x draw point is for rtl languages. Using canvasContext.fillText(firstChar, 0, 0); draws the character off the canvas so we need to use width for the x value in rtl. So we don't have to worry about which direction the text is in, we can just draw the text twice, once at 0 and again at width.
canvasContext.font = fontStyle;
canvasContext.textAlign = 'start';
canvasContext.textBaseline = 'top';
canvasContext.fillText(firstChar, 0, 0);
// draw the text again to account for rtl languages. one of these draws will draw the character offscreen
canvasContext.fillText(firstChar, width, 0);
// ...
canvasContext.clearRect(0, 0, width, fontSize);
canvasContext.fillText(nodeValue, 0, 0);
canvasContext.fillText(nodeValue, width, 0);|
thanks for digging into this. you're right that we end up comparing ر against ب. before reworking i tried your change in chromium 152 on windows with a small copy of the canvas steps, and wanted to check a couple of results with you first:
was your test canvas inside an rtl element, or in a different browser? that might be why it lined up for you. one option could be to set the direction from the text plus your |
Oh right, I was attaching it manually to visually see what was being painted so that makes sense. I really want to avoid having to figure out what the direction should be (at worse case requires parsing the entire ancestor tree for every text node), so I'll have to see what could be done as an alternative.
When the canvas is attached to the DOM and drawing direction properly, updating the font to segoe ui, tahoma, arial, or times new roman all pass the check and return |
isIconLigatureflags some arabic text as an icon, so color-contrast skips it. arabic letters join and change shape, so the whole string never looks like its first letter drawn alone, and the pixel + width comparison goes over both 15% thresholds. after 3 hits the font is cached as an icon font, and from then on every text node in that font is skipped, latin included. that fits the 492 vs 1,012 numbers in the issue.i ran the same comparison in chromium on windows. in Segoe UI the three arabic lines from the issue score about 0.17 pixel and 0.23-0.28 width difference, so all three get flagged. a short phrase like
رقم الطلبis flagged in Segoe UI, Arial, Tahoma and Times New Roman. latin text scores about 0 in all of them.the fix returns
falseearly when the text has characters from a joining script (arabic, syriac, nko, mandaic, mongolian, phags-pa). it runs before the font cache, so this text can't mark a font as an icon font either. icon fonts name their ligatures with latin letters in practice, so real icons aren't affected. hebrew and thaana don't join, so they're left out. non-bmp joining scripts (adlam, hanifi rohingya etc.) aren't covered here.tests:
#fail17in the color-contrast integration test, a low-contrast arabic paragraph (coverage only, it doesn't reproduce the font issue)side note: in the same check some devanagari text also scores as a ligature (
हिन्दी में कम कंट्रास्ट, about 0.28 pixel and 0.45 width difference in every font i tried). that's a different cause, the vowel sign is drawn before the consonant, so i kept it out of this pr. happy to open a separate issue for it.Closes: #5507