Open atanassov opened 4 years ago
This sounds like a pretty good fit for the current API that we have exposed. E.g. you could imaging something like:
const node = new LayoutChild('lots of text');
const f1 = node.generateNextFragment({availableInlineSize: 10});
const f2 = node.generateNextFragment({availableInlineSize: 30}, f1.breakToken);
canvasCtx.fill(f1);
canvasCtx.fill(f2);
There is a few open questions about some requirements:
E.g. you could imagine an API like:
const node = new LayoutChild('lots of text', [
{startIndex: 5, endIndex: 10, fontWeight: 900},
{startIndex: 8, endIndex: 12, fontStyle: 'italic'},
]);
cc/ @kojiishi @fserb
Just found this! I can imagine this being awesome for Workers as well (with OffscreenCanvas).
Separately, my team has been working with some customers in one of the "growing number of use cases" (Canvas 2D in fact) to enable this capability, and we just published our ideas as an explainer today: https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/Canvas/FormattedText.md
Coincidentally, our explainer and the strawperson sample above are remarkably similar :)
We would love you folks to read it and let us know what you think. Note: it's tailored to work specifically for Canvas, since that seems to be the one place where you can't already leverage the power of CSS/HTML. I'm not opposed to having two separate ways of doing this (one in a LayoutWorklet, one exposed more generally), but we should probably work together on the more general one. Also, if there are important non-canvas use-cases to consider I would be interested in hearing them.
@sushraja-msft
This sounds like a very welcome addition. We have use cases that require precise measurement (dimensions and colours) of rendered styled text on top of various backgrounds, and having a way to achieve that with the ease of CSS would reduce the complexity a lot.
The CSS Working Group just discussed String based inline layout API
.
There've been growing number of use cases requiring the ability to perform inline layout on strings that aren't necessary part of any DOM fragment. Consider any application with a complex model using the UA as a render surface through canvas 2d. Current capabilities of canvas a limiting users to fill/strokeText which is quite primitive compared to current richness of html/css - white space handling, font fallback, bidi re-ordering, justification etc. etc.... as well as the ability to have good braking logic and support for hyphenation etc.
So, wouldn't it be great to expose the capabilities of layout (inline in particular) to such context? It seems like the current custom layout APIs are plenty capable of handling inline layout given a constraint space while letting the author decide where the completed fragments go. Can we do the same for string input?