Skip to content

fix(color-contrast): don't treat text in joining scripts as icon ligatures - #5508

Open
rohitmulani63-ops wants to merge 1 commit into
dequelabs:developfrom
rohitmulani63-ops:icon-ligature-joining-scripts
Open

rohitmulani63-ops wants to merge 1 commit into
dequelabs:developfrom
rohitmulani63-ops:icon-ligature-joining-scripts

Conversation

@rohitmulani63-ops

Copy link
Copy Markdown

isIconLigature flags 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 false early 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:

  • "should return false for arabic text after the 3rd time the font is an icon" is the one that fails without the fix. it uses the same cache setup as the existing 3rd-time tests
  • unit tests for arabic, mixed arabic/latin and the other joining scripts (path coverage, the result without the fix depends on the font CI has)
  • #fail17 in 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

…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
@rohitmulani63-ops
rohitmulani63-ops requested a review from a team as a code owner October 29, 2026 12:21
@chutchins25

Copy link
Copy Markdown
Contributor

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.

@straker straker left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

@rohitmulani63-ops

Copy link
Copy Markdown
Author

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:

  • the canvas isn't attached to the page, so with the default direction ('inherit') the text is laid out ltr even on a dir="rtl" page (a canvas with no parent is ltr). with textAlign = 'start' the second draw at width then always lands off the canvas, and i got the same results as today (the line from the issue is still flagged in segoe ui)
  • if i also set ctx.direction = 'rtl' for rtl text (after the canvas resize), the first letter lines up. the issue's line then passes in segoe ui and tahoma, but it gets flagged in arial and times new roman instead, because the joined letters still differ from the lone first letter enough that both the pixel and the width difference stay above 15%

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 start + two draws, and keep an exception for joining scripts like arabic, since the alignment alone doesn't seem to be enough. happy to go another way if you prefer, and to share the test page and numbers. i haven't tried firefox, safari or the readex pro font from the issue yet

@straker

straker commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

the canvas isn't attached to the page, so with the default direction ('inherit') the text is laid out ltr even on a dir="rtl" page (a canvas with no parent is ltr). with textAlign = 'start' the second draw at width then always lands off the canvas, and i got the same results as today (the line from the issue is still flagged in segoe ui)

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.

if i also set ctx.direction = 'rtl' for rtl text (after the canvas resize), the first letter lines up. the issue's line then passes in segoe ui and tahoma, but it gets flagged in arial and times new roman instead, because the joined letters still differ from the lone first letter enough that both the pixel and the width difference stay above 15%

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 false for is icon ligature. So if we can figure out the drawing direction while not attached to the DOM then I think it should work regardless of font family.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

color-contrast silently skips some Arabic text as an icon ligature

3 participants