-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[canvas] Define OffscreenCanvasRenderingContext2D's font setter in detail #7847
Comments
cc @aosmond |
This is all the specification currently says about this:
That did not account for viewport units existing.
Also, is the proposal here to do this for workers only or always? |
You mean everything non-absolute, right? But yes.
If we were to change this on the main thread perhaps, yeah. In workers, however, there's only one implementation, and its behavior isn't particularly good, so I think it's highly unlikely that it'd break code. I'd argue that rejecting stuff like e.g. viewport units will improve the behavior over accepting it and resolving against zero (which is the current Blink behavior, and would hide the text altogether).
The idea would be to change this behavior only for workers. Browsers seem interoperable on the main thread and the current definition seems sensible there, afaict. |
Is it well-defined for the main thread? How do you get the viewport of an arbitrary element? E.g., consider x = new Document()
c = x.createElementNS("http://www.w3.org/1999/xhtml", "canvas")
console.log(c.getContext("2d")) And the current definition for font-relative lengths seems sensible either way, especially since you can end up without styling information in the main thread too. Unless that would require special-casing in workers I think I'd rather preserve it to give developers a more uniform experience when it comes to canvas. |
That (in Gecko, and also in Blink, afaict, since they do have this special resolver just for off-the-main-thread canvas) does require special-casing code in workers. All existing main-thread codepaths do have an answer for the document case (you need an answer for At least in Gecko, all the data structures used for stuff like style inheritance and unit resolution access stuff like the document and layout data-structures, so we'd need to special-case or at least factor all that out. |
This also affects cc @yiyix |
cc @fserb |
It seems that the two implementations do differ from the specs, which has a similar font-relative special casing than As for viewport units nothing is being said about it, here either, and Chrome indeed apparently uses I guess the easiest move here would be to change the specs to reflect what both implementations do for font-size relative units (i.e base it on |
I'm getting a dead link for https://drafts.fxtf.org/geometry/#dommatrix-parse
Personally I'd vote for rejecting everything non-absolute on workers, like with On the main thread, are viewport units the only issue? @annevk your example of: x = new Document()
c = x.createElementNS("http://www.w3.org/1999/xhtml", "canvas")
console.log(c.getContext("2d")) A lot of strange stuff happens for font rendering (https://jsfiddle.net/rLy6hnf7/). Chrome doesn't let the default font get set and font rendering does not appear to work at all. Firefox throws and error. Safari works for absolute units but produces strange results with relative units. This seems like perhaps a different issue than workers? |
@mysteryDate ah, it seems you hit the link when the CSSWG spec was down, seems back now. Yeah, in general I think the most principled approach is using something like what On the main thread, your fiddle seems to work for me in Firefox Nightly at least, but yeah what we're doing wrt viewport units at least doesn't seem to make a lot of sense to me. I'd move that to a different issue, though it's still worth figuring out a path to interop there. In particular, defining CSS on out of document elements is not great (getComputedStyle doesn't work there for example, |
Perhaps specification-wise the conditional can be whether the |
cc @whatwg/canvas |
That would most-likely not be web-compatible. It's very frequent to use detached canvases (for a lack of a layer API), and having a detached canvas in the same context (document) behave differently than a connected one would be both very surprising, and a change in the current behavior (that one is stable across all UAs). I guess that would require something more tedious like "belongs to an active Document" or alike (I didn't check the exact definition of "active Document" to be sure it's correct either, just throwing off a potential way toward). |
Rejecting non-absolute units when they don't make sense seems best to me! Something like "belongs to an active Document" is where my mind went first too, but I don't know how this is normally determined in the spec language. |
I'm not sure whether to throw or simply ignore the value. Also I'm not entirely sure why we should follow [Edit]: CSSWG is back. So for
Which if I read it correctly doesn't help much for viewport units. |
For the Other things like viewport-relative units don't make much sense, IMO, and we should probably ignore them. |
https://html.spec.whatwg.org/#dom-context-2d-font right now is very vague for this case. In the main thread, with an actual
<canvas>
element, we have all the context we need for to resolve all CSS units (needed forfont-size
): There's a viewport, a font we're inheriting from, a content language, etc.However in workers lots of these things CSS assumes do not hold / are not present. For example, what should viewport units do? What about writing-mode dependent units? What about font-relative units?
Blink seems to implement something a bit inconsistent, see https://crbug.com/1318927 which @jfkthame just filed. Some relative units work, some don't. Viewport units (if my reading of the code is correct) resolve against zero. It's a bit of a mess.
We have some pre-existing, fairly interoperable code to convert lengths without any context used by DOMMatrix. I propose we align with that and disallow non-absolute sizes in the
OffscreenCanvas.font
setter, both for simplicity and consistency.The alternative would be resolving against some sort of initial size (the regular CSS initial size? The canvas 10px initial size?), and then define what happens with all other units (font-metric-dependent units, viewport units, container units...). Given the best we can do is mostly resolving them against some fixed value, and that the failure mode for resolving these units against zero is terrible (no text at all) rather than the "font setter has no effect" that we already define when CSS-wide-keywords are used, I think not supporting non-absolute lengths is better.
I don't have a strong opinion on keyword font-sizes, other than if we do the above, then smaller/bigger should be disallowed, obviously.
Thoughts?
cc @jfkthame, @annevk, @lilles, @domenic
The text was updated successfully, but these errors were encountered: