Skip to content

Canvas Text Shaping - #137

Open
shallawa wants to merge 3 commits into
WebKit:mainfrom
shallawa:canvas-text-shaping
Open

shallawa wants to merge 3 commits into
WebKit:mainfrom
shallawa:canvas-text-shaping

Conversation

@shallawa

Copy link
Copy Markdown

Add an explainer for WebKit proposal for Canvas Text shaping. Another proposal has been discussed here[1].

[1] https://github.com/fserb/canvas2D/blob/master/spec/enhanced-textmetrics.md

Add an explainer for WebKit proposal for Canvas Text shaping. Another proposal
has been discussed here[1].

[1] https://github.com/fserb/canvas2D/blob/master/spec/enhanced-textmetrics.md

* CanvasTextShaping/CanvasTextShaping.md: Added.
* CanvasTextShaping/drawing-colored-clusters-display.png: Added.
* CanvasTextShaping/drawing-colored-justified-words-display.png: Added.
* CanvasTextShaping/drawing-text.png: Added.
* CanvasTextShaping/drawing-text-with-styles.png: Added.
* CanvasTextShaping/drawing-text-with-styles-display.png: Added.
* CanvasTextShaping/drawing-wrapped-justified-text-with-styled-display.png: Added.
Add an explainer for WebKit proposal for Canvas Text shaping. Another proposal
from Google has been discussed here[1].

[1] https://github.com/fserb/canvas2D/blob/master/spec/enhanced-textmetrics.md

Reviewed by NOBODY (OOPS!).

Explanation of why this fixes the bug (OOPS!).

* CanvasTextShaping/CanvasTextShaping.md: Added.
* CanvasTextShaping/drawing-colored-clusters-display.png: Added.
* CanvasTextShaping/drawing-colored-justified-words-display.png: Added.
* CanvasTextShaping/drawing-text.png: Added.
* CanvasTextShaping/drawing-text-with-styles.png: Added.
* CanvasTextShaping/drawing-text-with-styles-display.png: Added.
* CanvasTextShaping/drawing-wrapped-justified-text-with-styled-display.png: Added.
}
```

## Examples

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an example of text-on-path? I'm having trouble seeing how you would draw a sequence of GlyphRun along a non-linear path.

@shallawa shallawa Sep 16, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be something like this:

function fillClustersOnCircle(ctx, text, centerX, centerY, radius)
{
    const clusters = ctx.splitText(text);
    const clustersWidth = clusters.reduce((width, cluster) => width + cluster.textMetrics().width, 0);

    let distance = 0;
    for (const cluster of clusters) {
        const ratio = distance / clustersWidth;
        const angle = 2 * Math.PI * ratio;
        const x = radius * Math.cos(angle) + centerX;
        const y = radius * Math.sin(angle) + centerY;

        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle + Math.PI / 2);
        ctx.fillGlyphRun(cluster, 0, 0);
        ctx.restore();

        distance += cluster.textMetrics().width;
    }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having fillText(), fillGlyphRuns() and fillGlyphRun() give freedom to do different scenarios:

  • fillText() can be used when no special breaking or styling is required. All text can be displayed in one call.
  • fillGlyphRuns() can be used when special breaking (like clusters or word boundaries) or styling is required. But the text is displayed in the increasing horizontal direction of the context.
  • fillGlyphRun() can be used similar to fillGlyphRuns() but the display position is customizable.

Add an explainer for WebKit proposal for Canvas Text shaping. Another proposal
from Google has been discussed here[1].

[1] https://github.com/fserb/canvas2D/blob/master/spec/enhanced-textmetrics.md

Reviewed by NOBODY (OOPS!).

* CanvasTextShaping/CanvasTextShaping.md: Added.
* CanvasTextShaping/drawing-clusters-on-circle-display.png: Added.
* CanvasTextShaping/drawing-colored-clusters-display.png
* CanvasTextShaping/drawing-colored-justified-words-display.png
* CanvasTextShaping/drawing-text-with-styles-display.png
* CanvasTextShaping/drawing-text-with-styles.png
* CanvasTextShaping/drawing-text.png
* CanvasTextShaping/drawing-wrapped-justified-text-with-styled-display.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants