Skip to content

Add variable font C API#185

Merged
mattleibow merged 7 commits into
mono:skiasharpfrom
ramezgerges:001-variable-fonts-support
Apr 26, 2026
Merged

Add variable font C API#185
mattleibow merged 7 commits into
mono:skiasharpfrom
ramezgerges:001-variable-fonts-support

Conversation

@ramezgerges

@ramezgerges ramezgerges commented Apr 20, 2026

Copy link
Copy Markdown

Description of Change

Add C API surface for SkTypeface variable font (OpenType Font Variations) functionality, exposing variation axis queries and typeface cloning with specific variation coordinates.

API Changes

New functions (sk_typeface.h / sk_typeface.cpp)

Function Wraps Description
sk_typeface_get_variation_design_position SkTypeface::getVariationDesignPosition Query current axis coordinate values
sk_typeface_get_variation_design_parameters SkTypeface::getVariationDesignParameters Query axis metadata (tag, min, default, max, hidden)
sk_typeface_clone_with_arguments SkTypeface::makeClone(SkFontArguments) Clone a typeface with specific variation coordinates

New types

Type Fields
sk_fontarguments_variation_position_coordinate_t axis (uint32 tag), value (float)
sk_fontarguments_variation_axis_t tag, min, def, max (floats), isHidden (bool)

Behavioral Changes

None.

Required SkiaSharp PR

Requires mono/SkiaSharp#3703

PR Checklist

  • Rebased on top of skiasharp at time of PR
  • Changes adhere to coding standard
  • Updated documentation

Expose SkTypeface variable font functionality through the C API:
- sk_typeface_get_variation_design_position: query current axis coordinates
- sk_typeface_get_variation_design_parameters: query axis metadata (tag, min, default, max, hidden)
- sk_typeface_clone_with_arguments: create typeface clone with specific variation coordinates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ramezgerges and others added 6 commits April 20, 2026 21:27
Replace reinterpret_cast between C and C++ structs with field-by-field
copy. The structs have identical layout so the cast worked in practice,
but accessing memory through an unrelated type is undefined behavior
per the C++ standard. Compilers optimize the copy loop to the same code.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add sk_fourbytetag_t typedef (uint32_t) matching SkFourByteTag
- Use sk_fourbytetag_t in variation axis/coordinate C structs
- Add AsVariationPositionCoordinate/ToVariationPositionCoordinate for
  layout-compatible Coordinate struct (verified by static_assert)
- Add ToVariationAxis for Axis conversion (handles isHidden() getter)
- Add AsSkFontArguments helper for clone_with_arguments
- Simplify sk_typeface.cpp: remove manual copy loops, use reinterpret_cast
  for coordinates, use helpers for parameters and font arguments

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Replace manual reinterpret_cast functions with DEF_MAP macro for
  VariationPosition::Coordinate (generates As*/To* automatically)
- Move static_assert to sk_structs.cpp following existing pattern
- Use AsVariationPositionCoordinate() in sk_typeface.cpp instead
  of inline reinterpret_cast

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mattleibow
mattleibow merged commit b6a88e5 into mono:skiasharp Apr 26, 2026
1 check passed
mattleibow added a commit to ramezgerges/SkiaSharp that referenced this pull request Apr 26, 2026
Update externals/skia to include the merged variable font C API
(mono/skia#185) at the tip of the skiasharp branch.

Changes: https://github.com/mono/skia/compare/0afa08b9ac..b6a88e589f

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mattleibow added a commit to mono/SkiaSharp that referenced this pull request Apr 26, 2026
Add variable font support to SkiaSharp and HarfBuzzSharp (#3703)

Changes: mono/skia#185

Add OpenType Font Variations support, enabling runtime axis querying,
typeface cloning at specific design coordinates, and full HarfBuzz
variation APIs. Variable fonts (e.g. Inter, Roboto Flex) expose
continuous design axes like weight, width, and optical size — this PR
lets consumers query and manipulate those axes from managed code.

~~ SKTypeface ~~

  * VariationDesignParameters / GetVariationDesignParameters(Span<>)
    — query axis metadata (tag, min, default, max, isHidden)
  * VariationDesignPosition / GetVariationDesignPosition(Span<>)
    — query the current axis coordinate values
  * Clone(ReadOnlySpan<SKFontVariationPositionCoordinate>)
    — create a typeface at a specific design position
  * Clone(SKFontArguments)
    — full control over position and collection index

~~ New types ~~

  * SKFourByteTag — four-byte tag struct with Parse/ToString, implicit
    uint conversion, and char constructor (matches HarfBuzzSharp.Tag)
  * SKFontVariationAxis — generated struct: Tag, Min, Default, Max,
    IsHidden
  * SKFontVariationPositionCoordinate — generated struct: Axis + Value
  * SKFontArguments — ref struct accepting spans, arrays, or stackalloc

~~ HarfBuzzSharp.Face ~~

  * HasVariationData, VariationAxisCount, VariationAxisInfos
  * TryFindVariationAxis(Tag, out info)
  * NamedInstanceCount, named instance design coords and name lookups

~~ HarfBuzzSharp.Font ~~

  * SetVariations, SetVariationCoordsDesign, SetVariationCoordsNormalized
  * VariationCoordsNormalized property and Span overload
  * SetVariationNamedInstance

Every property has a matching Span overload for allocation-free use via
stackalloc. Input validation (negative instance indices, null checks)
throws ArgumentOutOfRangeException or ArgumentNullException at the
managed boundary.

Includes 151 tests covering axis values, tag round-trips, span/property
equivalence, static font graceful handling, clone + render verification,
and HarfBuzz named instances. Gallery sample demonstrates Inter with
weight and optical size sliders.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ramezgerges added a commit to ramezgerges/skia that referenced this pull request Apr 26, 2026
Brings in the latest mono/skia 'skiasharp' branch updates on top of the
m147 work-in-progress:

  * Add variable font C API for typeface variation queries and cloning (mono#185)
  * Add sk_fontmgr_legacy_create_typeface C API (mono#198, mono#195) — replaces
    the old sk_default_fontmgr.{cpp,h} with a single C entry point that
    forwards to SkFontMgr::legacyMakeTypeface(). Fixes the Android
    default-typeface SIGSEGV.
  * Use int32_t instead of long for SkManagedStream move offset
    (mono#186) — fail-loud on overflow instead of silent truncation
    of platform-dependent C long.
  * Update libpng to 1.6.58
  * Update libexpat to 2.7.5

Conflict resolutions:
  - DEPS: keep m147's freetype roll, take origin/skiasharp's libexpat 2.7.5
    (and libpng 1.6.58 auto-merged the same way).
  - src/c/sk_default_fontmgr.cpp: accept origin/skiasharp's deletion;
    their legacyMakeTypeface refactor obviates our m147 carry of that file.
  - src/c/sk_typeface.cpp: re-apply our m139 SkFontScanner fix to the
    Android and FontConfig SkFontMgr_New_* call sites, which moved into
    this file as part of their refactor.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mattleibow added a commit that referenced this pull request Apr 26, 2026
Add color font palette support to C API (#197)

Requires: mono/SkiaSharp#3742

Extends the variable font C API (merged in #185) with color palette
support, enabling SkiaSharp to clone typefaces with specific palette
selections and per-color overrides for COLRv1/COLR fonts.

New C API surface:

  * sk_fontarguments_palette_override_t — struct mapping to
    SkFontArguments::Palette::Override (index + color)
  * sk_typeface_clone_with_arguments — extended from 4 to 7 parameters,
    adding paletteIndex, paletteOverrides, and paletteOverrideCount

Supporting changes:

  * DEF_MAP for Palette::Override (reinterpret_cast, layout-compatible)
  * static_assert in sk_structs.cpp for ABI verification
  * AsSkFontArguments helper updated to call setPalette()

Co-authored-by: Ramez Gerges <ramezragaa@proton.me>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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