Canvas2D: Add support for the lang attribute in CanvasTextDrawingStyles
Categories
(Core :: Graphics: Canvas2D, enhancement)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox151 | --- | fixed |
People
(Reporter: schenney, Assigned: jfkthame, NeedInfo)
References
(Blocks 1 open bug)
Details
(Keywords: dev-doc-needed)
Attachments
(2 files)
Steps to reproduce:
HTML Canvas text does not provide any method to localize the language in offscreen canvas, but takes the language from the canvas element for DOM canvas. This results in incompatible text rendering between offscreen and DOM canvases.
A proposal has been made, https://github.com/mozilla/standards-positions/issues/1150, to add a lang IDL attribute to CanvasTextDrawingStyles.
There is a PR on the spec: https://github.com/whatwg/html/pull/10873
There is a request for position: https://github.com/mozilla/standards-positions/issues/1150
Actual results:
See the example attached to https://github.com/mozilla/standards-positions/issues/1150. The glyph used for offscreen are always from the documents' content language, while the canvas element uses the elements language.
Expected results:
It should be possible to control the language for canvas text rendering.
Comment 1•1 year ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::Graphics: Canvas2D' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
Comment 2•1 month ago
|
||
Jonathan what would be the effort to implement such a feature ?
I implemented in Chromium. Testing was maybe the biggest part of the job but the WPT now exist. The sub tasks are:
- Update the canvas idl.
- Add code for getter/setter and save/restore canvas state for canvas 2d.
- Make use of the lang string when canvas fonts are updated.
- Add the lang attribute to the set of attributes that are captured for a transferred offscreen canvas
- Add code to handle the "inherit" value for the 3 cases: regular 2D canvas, offscreen with canvas host, and offscreen in a worker.
I think that's it.
| Assignee | ||
Comment 4•1 month ago
|
||
Yes, this seems like it should be fairly straightforward, as outlined in comment 3.
(Does the spec consider how to deal with a value that is not a well-formed BCP 47 tag? In the text preparation algorithm, I see step 6: "If language is the empty string, then set language to explicitly unknown." Should that also be done if language is not a well-formed BCP 47 language tag?)
The canvas portion of the spec has nothing to say about malformed BCP 47 tags. When implementing I made canvas behave the same way as regular HTML content, where the spec says that an invalid tag must be retained as a unique unknown language (https://html.spec.whatwg.org/multipage/dom.html#the-lang-and-xml:lang-attributes).
When it comes to what font to use in that case the spec says nothing and the chromium implementation ultimately seems to rely on harfbuzz to give some reasonable font.
| Assignee | ||
Comment 6•1 month ago
|
||
I've put up a patch for this at https://phabricator.services.mozilla.com/D289096, but currently phabricator seems to be stalled and it's still in "draft" state. Hopefully that will be resolved soon and we can get this reviewed.
| Assignee | ||
Comment 7•1 month ago
|
||
Updated•1 month ago
|
| Assignee | ||
Comment 8•1 month ago
|
||
This is documented at https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lang, but currently shown as unsupported in Firefox.
Comment 10•1 month ago
|
||
Comment 11•1 month ago
|
||
Backed out for causing wpt failures @idlharness.any.worker.html.
Comment 12•1 month ago
|
||
Comment 13•1 month ago
|
||
| bugherder | ||
Updated•15 days ago
|
Comment 14•2 days ago
|
||
FF151 MDN work for this can be tracked inhttps://github.com/mdn/content/issues/43865.
The docs look good and the compat data has already been updated, so this should just be an MDN release note.
Comment 15•2 days ago
|
||
Could I please have some confirmation of part of the MDN docs on this? The docs say:
Due to technical limitations, the inherit value behaves differently for on-screen and off-screen canvases:
- For on-screen canvases, the lang value is inherited when the associated CanvasRenderingContext2D object is first created; the inherited lang value then changes dynamically if the lang attribute value is updated.
- For off-screen canvases, the lang value is inherited when the associated OffscreenCanvasRenderingContext2D object is first created, and then fixed for the lifetime of the OffscreenCanvas. It does not change if the lang attribute value is updated. Because of this, the language of an off-screen canvas can only be changed by setting the lang value explicitly.
The main point is clear - on screen canvases inherit their lang value on creation from their associated <canvas> or its nearest ancestor, and this attibute will update if the source attribute changes.
- For offscreen canvasses I THINK this is saying that
- the lang value is also inherited on creation,
- but since there is no associated canvas it gets the lang of the document - right?
- that value is not updated if the document lang changes, or if the offscreen canvas is associated with a canvas later that has a different lang value
- But you can still change the value manually be setting the lang attribute.
Right?
Description
•