-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
OpenType Features and Variable Fonts for Canvas #3571
Comments
Perhaps one approach would be to expose a style-like object on the 2D context and define a subset of style properties relevant for font-styling to be permitted, such as |
Could we use https://drafts.csswg.org/css-font-loading/#fontface-interface somehow? Abstracting fonts into a reusable/mutable object rather than exposing various properties to poke at it seems more reasonable. cc @tabatkins |
While the Especially so for variable fonts, where a Also, @litherum commented in a separate thread that accessing/assigning only one such instance to the 2D context does not provide a means of specifying a cascade for fallback, as we would usually find it in the |
We could make it a sequence of |
The underlying primitive for font selection is CSS properties.
I think @drott's suggestion is a good one. Making the canvas API match the existing font selection facilities is best for developers, users, and implementors. Indeed, this is already how the existing canvas API was designed, and we should not deviate from that design. |
Oh, a point I missed before - this issue is bringing up a valuable request. The ability to use great fonts should be ubiquitous, and included in every subsystem that interacts with text. |
Note that there are two very distinct possibilities for a font-related interface (the confusion of which resulted in a very mixed-up TypedOM section that I eventually had to remove):
The latter is currently only exposed thru CSS properties; at some point in a future level of TypedOM there will be an object representing a font-selection query (for 'font' to reify to), but that doesn't exist yet. You probably want to continue |
+1 to what @tabatkins said |
Yes, @tabatkins is right. |
+1 this would be a great feature to add. |
Pardon my density but I'm a little fuzzy on how this issue can be moved forward? - in addition to ot features and variations I'd love to be able to use |
I think we're just missing a concrete proposal. |
using measuretext to measure text on canvas using a variable font is among the concrete needs people have... |
Indeed. If a font has a |
Based on @tabatkins' comment from 2018, it seems like the way forward is to add additional properties to CanvasRenderingContext2D mirroring the CSS |
Looking a little deeper into the above, let me parse out each individual
If all these were added, we'd have a bunch of new DOMString attributes on CanvasRenderingContext2D to specify font settings. Some of these would interact with the value of the
to the |
I'd suggest mirroring everything that can be selected by 'font', so that the This would allow you to do useful things like just changing the family while leaving the other settings intact; right now you'd have to set |
Yeah, that makes sense. I guess this would almost exactly mirror the |
They could use the |
Ideally we’d be able to style fonts for canvas just as we style them for HTML: the canvas would somehow “import” the font instance definition from an element defined with CSS (potentially the <canvas> element itself). This would make it very convenient for authors to switch freely between rendering text as HTML elements or on the canvas, which seems a very desirable goal. In documents that use both models, authors don’t want to worry whether they’ve correctly matched font instance definitions. Here’s some JavaScript showing current and imagined ways of cloning an HTML element’s font style to a canvas, which may be useful for discussion: The current method is how we do things now, where only a limited number of font properties can be replicated in canvas. The future-A method imagines expanding the The future-B method imagines a currentFont (psuedo-)property, a simpler way of getting the The future-C method imagines a new // set up div
let mydiv = document.getElementById("mydiv");
mydiv.style.fontFamily = "Amstelvar";
mydiv.style.fontSize = "48px";
mydiv.style.fontVariationSettings = `"XOPQ" 130, "YOPQ" 120, "XTRA" 420`;
let computedStyle = window.getComputedStyle(mydiv);
// set up canvas
let canvas = document.getElementById("mycanvas");
let ctx = canvas.getContext("2d");
// choose font selection tech for canvas
let tech = "current";
switch (tech) {
case "current":
ctx.font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
break;
case "future-A":
ctx.font = computedStyle.font;
break;
case "future-B":
ctx.font = mydiv.style.currentFont;
break;
case "future-C":
ctx.font = window.getComputedFont(mydiv.style);
break;
}
ctx.fillText("Test variable font instance in <canvas>", 0, 100); |
Don’t forget that |
Font styling for the 2D Context of Canvas is currently limited to the
font
state of the context object.I would like to start a discussion on extending Canvas' capabilities for font styling and OpenType feature activation. The reason being that a number of advancements in typographic quality on the web are not accessible through the
font
property of 2D context.In regular CSS activating OpenType features such as ligatures, small caps, contextual alternates, additional number/fraction/ordinal forms is performed through using
font-variant-*
properties, andfont-feature-settings
.The second limitation is the usage of Variable Fonts (Introducing Variable Fonts, Introduction to Variable Fonts on the Web, Variable Fonts Exploration).
In regular CSS, controlling parameters for the rendering of variable fonts is done through the
font-weight
,font-stretch
, andfont-style
properties, which can be specified as part of the font property/shorthand form on canvas. However, activating variation axes outside these three canonical axes is not possible, preventing a whole set of more artistic and creative fonts to be used on canvas. Control of the additional axes is done through thefont-optical-sizing
property as well as the low levelfont-variation-settings
.I believe it would be beneficial to to find a way for all of the tools and control that the CSS Fonts Specification offers to be applicable to the 2D context of Canvas.
The text was updated successfully, but these errors were encountered: