Closed Bug 1943070 Opened 1 year ago Closed 1 month ago

Canvas2D: Add support for the lang attribute in CanvasTextDrawingStyles

Categories

(Core :: Graphics: Canvas2D, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
151 Branch
Tracking Status
firefox151 --- fixed

People

(Reporter: schenney, Assigned: jfkthame, NeedInfo)

References

(Blocks 1 open bug)

Details

(Keywords: dev-doc-needed)

Attachments

(2 files)

Attached file canvas-lang-test.html

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.

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.

Component: Untriaged → Graphics: Canvas2D
Product: Firefox → Core
Blocks: 2023561

Jonathan what would be the effort to implement such a feature ?

Flags: needinfo?(jfkthame)

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.

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

Flags: needinfo?(jfkthame)

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.

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: nobody → jfkthame
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

This is documented at https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lang, but currently shown as unsupported in Firefox.

Keywords: dev-doc-needed
Pushed by jkew@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/938bd0bb8bab https://hg.mozilla.org/integration/autoland/rev/47495a0c3043 Support the 'lang' attribute in CanvasTextDrawingStyles. r=gfx-reviewers,webidl,lsalzman,smaug
Pushed by agoloman@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/30885d5ddd99 https://hg.mozilla.org/integration/autoland/rev/d2a3d092ed23 Revert "Bug 1943070 - Support the 'lang' attribute in CanvasTextDrawingStyles. r=gfx-reviewers,webidl,lsalzman,smaug" for causing wpt failures @idlharness.any.worker.html.

Backed out for causing wpt failures @idlharness.any.worker.html.

Flags: needinfo?(jfkthame)
Pushed by jkew@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/8fe347f255d6 https://hg.mozilla.org/integration/autoland/rev/e9ec306d6563 Support the 'lang' attribute in CanvasTextDrawingStyles. r=gfx-reviewers,webidl,lsalzman,smaug
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 151 Branch
QA Whiteboard: [qa-triage-done-c152/b151]

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.

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.

  1. 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?

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: