Skip to content

feat: add fontVariantCaps#1161

Merged
Brooooooklyn merged 1 commit intomainfrom
add-fontVariantCaps
Dec 6, 2025
Merged

feat: add fontVariantCaps#1161
Brooooooklyn merged 1 commit intomainfrom
add-fontVariantCaps

Conversation

@yisibl
Copy link
Copy Markdown
Collaborator

@yisibl yisibl commented Dec 6, 2025

  1. Support fontVariantCaps, corresponding to CanvasRenderingContext2D.fontVariantCaps in the Canvas API
    Chrome bug#40457093
  2. Support for <font-variant-css2>= normal | small-caps within the font shorthand.

e.g.

// ✅ Valid
ctx.font = 'small-caps 36px Roboto Mono';
ctx.font = 'normal 36px Roboto Mono';

//  ❌ Other `font-variant-caps` values are invalid, consistent with the specification
ctx.font = 'petite-caps 36px Roboto Mono';

Fixed: #804
Part of #113


Note

Adds fontVariantCaps across the JS API and rendering pipeline (including small-caps via font shorthand) and maps variants to OpenType features in Skia; includes tests.

  • Text rendering:
    • Add FontVariantCaps enum and plumb variant_caps through FFI (skia_c.hpp/.cpp), src/sk.rs, and src/ctx.rs draw/metrics paths.
    • Map font-variant-caps values to OpenType features (smcp, c2sc, pcap, c2pc, unic, titl).
    • Extend CanvasRenderingContext2D state with fontVariantCaps (default normal) plus getters/setters.
    • Parse CSS font shorthand to set fontVariantCaps for normal | small-caps.
  • Tests:
    • Add coverage for default, invalid value handling, all valid font-variant-caps values, rendering snapshots, shorthand vs property equivalence, and shorthand parsing.

Written by Cursor Bugbot for commit 5948330. This will update automatically on new commits. Configure here.

@yisibl yisibl force-pushed the add-fontVariantCaps branch from 248cf2d to 8dbf2b5 Compare December 6, 2025 14:52
1. Support `fontVariantCaps`, corresponding to `CanvasRenderingContext2D.fontVariantCaps` in the Canvas API
2. Support for `<font-variant-css2>= normal | small-caps` within the `font` shorthand.

e.g.

```js

// ✅ Valid
ctx.font = ‘small-caps 36px Roboto Mono’;
ctx.font = ‘normal 36px Roboto Mono’;

//  ❌ Other `font-variant-caps` values are invalid, consistent with the specification
ctx.font = ‘petite-caps 36px Roboto Mono’;
```
@yisibl yisibl force-pushed the add-fontVariantCaps branch from 8dbf2b5 to 5948330 Compare December 6, 2025 14:57
@yisibl yisibl requested a review from Brooooooklyn December 6, 2025 14:58
@yisibl yisibl marked this pull request as ready for review December 6, 2025 14:58
@Brooooooklyn Brooooooklyn requested a review from Copilot December 6, 2025 15:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for the fontVariantCaps property to the Canvas API, matching the standard CanvasRenderingContext2D.fontVariantCaps specification. The implementation enables developers to control how text is rendered with different capitalization variants (small-caps, all-small-caps, petite-caps, all-petite-caps, unicase, and titling-caps) both via the dedicated property and through the CSS font shorthand notation.

Key changes:

  • Added FontVariantCaps enum with all seven standard values (normal, small-caps, all-small-caps, petite-caps, all-petite-caps, unicase, titling-caps)
  • Integrated font-variant-css2 support (normal | small-caps) in the CSS font shorthand parser
  • Implemented OpenType font feature mapping in the C++ layer to properly render variant caps using tags like 'smcp', 'c2sc', 'pcap', 'c2pc', 'unic', and 'titl'

Reviewed changes

Copilot reviewed 6 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/state.rs Added font_variant_caps field to Context2dRenderingState with default value Normal
src/sk.rs Defined FontVariantCaps enum with 7 variants, implemented FromStr and as_str() methods, and added parameter to draw_text and get_line_metrics functions
src/ctx.rs Added getter/setter methods for fontVariantCaps property and integrated font shorthand parsing to automatically set fontVariantCaps based on CSS font-variant-css2
skia-c/skia_c.hpp Added variant_caps parameter to skiac_canvas_get_line_metrics_or_draw_text function signature
skia-c/skia_c.cpp Implemented OpenType font feature mapping logic using conditional branches to apply appropriate feature tags (smcp, c2sc, pcap, c2pc, unic, titl) based on variant_caps value
test/text.spec.ts Added comprehensive test coverage including default value tests, invalid value handling, all variant values, visual rendering tests, CSS shorthand integration tests, and equivalence tests
test/snapshots/font-variant-caps.png Snapshot file for visual regression testing of all fontVariantCaps values
test/snapshots/font-variant-caps-from-css-font-shorthand.png Snapshot file for visual regression testing of font shorthand integration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread __test__/text.spec.ts
@Brooooooklyn
Copy link
Copy Markdown
Owner

cursor review

@Brooooooklyn
Copy link
Copy Markdown
Owner

@codex review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


@Brooooooklyn Brooooooklyn merged commit d21c146 into main Dec 6, 2025
44 checks passed
@Brooooooklyn Brooooooklyn deleted the add-fontVariantCaps branch December 6, 2025 15:41
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.

feature request: support CanvasRenderingContext2D.fontVariantCaps

3 participants