RESOLVED FIXED285993
Add "lang" IDL attribute to "CanvasTextDrawingStyles"
https://bugs.webkit.org/show_bug.cgi?id=285993
Summary Add "lang" IDL attribute to "CanvasTextDrawingStyles"
Attachments
repro.html: canvas measureText vs DOM span width under each <html lang> (6.14 KB, text/html)
2026-09-12 03:09 PDT, chenglou92
no flags
Deleted User
Comment 1 2025-01-15 06:37:58 PST
Unentitled to assign this ticket to myself, but starting to work on it.
Radar WebKit Bug Importer
Comment 2 2025-01-22 06:34:16 PST
Deleted User
Comment 3 2025-03-06 07:40:39 PST
Deleted User
Comment 4 2025-03-18 01:47:54 PDT
chenglou92
Comment 5 2026-09-12 03:08:29 PDT
Adding a web-developer-facing repro, since the missing default `lang = "inherit"` behavior is observable today even though `ctx.lang` itself is not exposed. The page language already changes font selection for DOM text and for a connected `<canvas>`. It does not for: - an OffscreenCanvas created in a window, - the OffscreenCanvas returned by `transferControlToOffscreen()`, - a detached `<canvas lang="…">`. Those contexts resolve fonts with no language. `measureText()` then disagrees with the DOM whenever a generic family, `system-ui`, or glyph fallback depends on language. This affects libraries that predict DOM text layout from canvas measurements. With `<html lang="ko">`, all three test strings differed from the DOM width by 7.6px to 45.3px; with `ja` or `zh-Hans`, some did (up to 32.1px). Steps to reproduce 1. Open the attached `repro.html`. It uses system fonts only and runs on load. 2. The page sets `<html lang>` to `en`, `ko`, `ja` and `zh-Hans` in turn. After each change it creates fresh objects and measures one string with: - a DOM span, - a new `OffscreenCanvas`, - a detached `<canvas>` whose `lang` attribute equals the page language, - a detached `<canvas>` without `lang`, - a connected `<canvas>`, - the OffscreenCanvas returned by `transferControlToOffscreen()` on a connected `<canvas>`. 3. Compare each canvas column with the DOM span column. Red cells differ by more than 0.1px. Expected results - OffscreenCanvas matches the DOM span. Its inherited language is the document element's language when it is constructed, and "inherit" uses that inherited language. - Detached `<canvas lang>` matches the DOM span. "inherit" uses the canvas element's language, which is its `lang` attribute. - Connected `<canvas>` matches the DOM span. - OffscreenCanvas from `transferControlToOffscreen()` uses the canvas element's language. - Detached `<canvas>` without `lang` has an unknown language, so it is not expected to follow `<html lang>`. That column is informational only. Actual results Installed Safari 26.5.2 (21624.2.5.11.8), macOS 26.5.2 (25F84), built-in Liquid Retina XDR display at DPR 2, system languages zh-Hans-US, en-US: font text html lang DOM span OffscreenCanvas detached <canvas lang> detached <canvas> (no lang) connected <canvas> transferControlToOffscreen() 16px sans-serif Hello world 0123456789 en 172.578 172.570 172.570 172.570 172.570 172.570 16px sans-serif Hello world 0123456789 ko 164.969 172.570 172.570 172.570 164.960 172.570 16px sans-serif Hello world 0123456789 ja 204.719 172.570 172.570 172.570 204.704 172.570 16px sans-serif Hello world 0123456789 zh-Hans 179.672 172.570 172.570 172.570 179.666 172.570 16px system-ui 「今日」、。直角 en 127.016 127.004 127.004 127.004 127.004 127.004 16px system-ui 「今日」、。直角 ko 89.188 127.004 127.004 127.004 89.184 127.004 16px system-ui 「今日」、。直角 ja 118.203 127.004 127.004 127.004 118.190 127.004 16px system-ui 「今日」、。直角 zh-Hans 127.016 127.004 127.004 127.004 127.004 127.004 16px Arial 中文 日本語 「今日」、。 en 184.891 184.891 184.891 184.891 184.891 184.891 16px Arial 中文 日本語 「今日」、。 ko 139.609 184.891 184.891 184.891 139.595 184.891 16px Arial 中文 日本語 「今日」、。 ja 184.891 184.891 184.891 184.891 184.891 184.891 16px Arial 中文 日本語 「今日」、。 zh-Hans 184.891 184.891 184.891 184.891 184.891 184.891 Playwright's WebKit build 2272 (Safari/26.4 user agent), headless, macOS 26.5.2, system languages zh-Hans-US, en-US: - `'lang' in ctx` is false for both context types. - OffscreenCanvas, detached `<canvas lang>` and the OffscreenCanvas from `transferControlToOffscreen()` return the language-less width for every language. - The connected canvas matches the DOM within 0.02px, including a connected `<canvas lang="ko">` in an `<html lang="en">` document (Arial string 139.595 vs DOM 139.609). For comparison, headless Chromium 147.0.7727.15: - OffscreenCanvas, detached `<canvas lang>`, connected `<canvas>` and the transferred OffscreenCanvas match the DOM within 0.02px in all 12 rows. Installed Chrome 153.0.8010.36, same Mac (`navigator.languages` en-US, en, fr, zh-CN, zh): - The same four columns match the DOM within 0.01px in all 12 rows. Installed Firefox 155.0.1, same Mac, fresh profile (`navigator.languages` en-US, en): - OffscreenCanvas and the transferred OffscreenCanvas match the DOM within 0.001px in all 12 rows. - Detached `<canvas lang>` and connected `<canvas>` follow the page language, within 0.07px except the `ja` sans-serif row (198.200 vs DOM 198.433). Firefox implemented the attribute in Mozilla bug 1943070 (RESOLVED FIXED). Note: text measured with no language falls back to system fonts chosen from the system language preferences. On a Mac with different preferred languages, the `en` and `zh-Hans` rows may therefore differ from the ones above. The `ko` rows show the problem regardless. Specification (HTML, Canvas text styles, last updated 8 September 2026): - CanvasTextDrawingStyles IDL: `attribute DOMString lang; // (default: "inherit")` - CanvasTextDrawingStyles language: "Objects that implement the CanvasTextDrawingStyles interface have an associated language value used to localize font rendering. Valid values are a BCP 47 language tag, the empty string, or "inherit" where the language comes from the canvas element's language, or the associated document element when there is no canvas element. Initially, the language must be "inherit"." - Text preparation algorithm: "Let language be the target's language. If language is "inherit": Let sourceObject be object's font style source object. If sourceObject is a canvas element, then set language to the sourceObject's language. Otherwise: Assert: sourceObject is an OffscreenCanvas object. Set language to the sourceObject's inherited language." - OffscreenCanvas inherited language: "An OffscreenCanvas object has an internal inherited language and inherited direction set when the OffscreenCanvas is created." - OffscreenCanvas constructor: "If global is a Window object: Let element be the document element of global's associated Document. If element is not null: Set the inherited language of this to element's language." - transferControlToOffscreen(): "Set the offscreenCanvas's inherited language to the language of this canvas element." - The language of a detached element comes from its own `lang` attribute; without one it is unknown unless a pragma or HTTP language exists. Web platform tests On wpt.fyi aligned runs, Safari 26.6 and Safari Technology Preview 252 fail these tests, while Chrome and Firefox pass them: - `html/canvas/element/text/2d.text.lang.default`, `2d.text.measure.lang`, `2d.text.measure.lang.inherit` - `html/canvas/offscreen/text/2d.text.lang.default(.worker)`, `2d.text.measure.lang(.inherit, .worker)` - the reftests `canvas.2d.lang`, `canvas.2d.lang.dynamic`, `canvas.2d.lang.inherit.disconnected.canvas` - `canvas.2d.offscreen.lang(.inherit)`, `canvas.2d.offscreen.transferred.lang(.inherit, .inherit.document)`, `canvas.2d.offscreen.worker.lang(.inherit)` Safari passes the connected-canvas `canvas.2d.lang.inherit` reftest. Where the language is dropped (WebKit trunk 7189f73167ef) - `Source/WebCore/html/canvas/CanvasTextDrawingStyles.idl:27-35` has no `lang`. - `OffscreenCanvasRenderingContext2D::setFont` (`OffscreenCanvasRenderingContext2D.cpp:93-130`) starts from a fresh `FontCascadeDescription` with only a default family and size (`:116-119`) and resolves it with `Style::resolveForUnresolvedFont` (`:121`), which never sets a locale (`StyleResolveForFont.cpp:352-407`). So `usedLocale()` stays null and `script()` stays `USCRIPT_COMMON` (`FontDescription.cpp:40-70`, `FontDescription.h:168-169`). - `OffscreenCanvas` stores no inherited language: see the constructor (`OffscreenCanvas.cpp:128-133`) and `HTMLCanvasElement::transferControlToOffscreen()` (`HTMLCanvasElement.cpp:763-774`). - `CanvasRenderingContext2D::setFontWithoutUpdatingStyle` copies the canvas's computed font description when one exists (`CanvasRenderingContext2D.cpp:233-234`). That description carries the `-webkit-locale` mapped from `lang` (`HTMLElement.cpp:279-286`). A detached canvas has no computed style, so it gets the defaults (`:235-240`) even with a `lang` attribute. - The locale matters for CoreText glyph fallback (`FontCacheCoreText.cpp:771-782,818`), for `system-ui` (`FontCacheCoreText.cpp:570-579` → `SystemFontDatabaseCoreText::cascadeList` → `systemFontParameters`), and for generic families (`CSSFontSelector::resolveGenericFamily` → `FontDescription::platformResolveGenericFamily`, `CSSFontSelector.cpp:343-347`, which returns null for `USCRIPT_COMMON`, `FontDescriptionCocoa.cpp:79-84`). Two observations that may help whoever picks this up: - Threading. The window-side cases (a new OffscreenCanvas in a document, a detached `<canvas lang>`, and `transferControlToOffscreen()` on the main thread) don't need the worker-thread locale work in bug 292973. `FontDescription::setComputedLocale` is main-thread only (`FontDescription.cpp:108`). - A WPT test may disagree with the spec. `html/canvas/offscreen/text/2d.text.measure.lang.inherit.html` sets `<html lang="tr">` after constructing the OffscreenCanvas. Under the construction-time inherited language above, that canvas's language would be unknown, so a spec-following implementation may fail the test's `tr_width > en_width` assertion; Chrome and Firefox pass because they read the document language later.
chenglou92
Comment 6 2026-09-12 03:09:31 PDT
Created attachment 481362 [details] repro.html: canvas measureText vs DOM span width under each <html lang>
Sam Weinig
Comment 7 2026-10-23 10:40:19 PDT
*** Bug 328660 has been marked as a duplicate of this bug. ***
Sam Weinig
Comment 8 2026-10-23 11:29:59 PDT
EWS
Comment 9 2026-10-23 20:59:06 PDT
Committed 324596@main (e6cf8b888b49): <https://commits.webkit.org/324596@main> Reviewed commits have been landed. Closing PR #78060 and removing active labels.
Note You need to log in before you can comment on or make changes to this bug.