* Add CFF2 to CFF1 converter for font instantiation
When instantiating a CFF2 variable font to a static instance, automatically
convert the output to CFF1 format instead of keeping it as CFF2. This is
appropriate since CFF2's variation features are no longer needed in static
fonts, and CFF1 is more widely supported.
The converter integrates with the CFF2 subsetter and triggers automatically
when pinned=true (i.e., when instantiating with normalized coordinates).
Converts table structure:
- CFF2 inline TopDict → CFF1 TopDict INDEX
- Adds Name INDEX and String INDEX
- Creates CID-keyed font with ROS operator (Adobe-Identity-0)
- Generates identity CID charset
- Removes variation operators (vstore, vsindex, blend)
- Reuses desubroutinized charstrings and subroutines
Focus is on structural conversion; charstring encoding details like
endchar/return operators and width encoding can be added incrementally.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix CFF2 to CFF1 converter table tag and serialization
Updates the CFF2 to CFF1 converter to properly output CFF format:
- Use plan->add_table() to output 'CFF ' tag instead of 'CFF2'
- Return false from subset() to signal CFF2 table not needed
- Use passed serializer instead of creating new one
- Change String INDEX to use push/copy/pop_pack pattern
Known issue: Output has 5 extra bytes before CFF header that need
investigation of serializer head/tail allocation patterns.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix CFF2 to CFF1 serialization order and header version
Fixes the INDEX serialization order and version field initialization:
- Reorder serialization to match CFF1 pattern: CFF header, then Name INDEX,
then Top DICT INDEX. This eliminates extra bytes before the header.
- Fix version field by using cff->min_size instead of OT::cff1::min_size
for nameIndex offset. This resolves the issue where major version was
written as 0x00 instead of 0x01.
- Add String INDEX with "Adobe" and "Identity" strings (SIDs 391, 392)
required by the ROS operator in CID-keyed fonts.
The CFF header now correctly starts at offset 0 with version 1.0.
Remaining issue: FDArray offset parsing error needs investigation.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Improve FDArray serialization in CFF2 to CFF1 converter
Change FDArray serialization to use inferred template types and avoid
preprocessor issues with the likely() macro when using template syntax.
Progress:
- CFF header now at offset 0 with correct version 1.0 ✓
- String INDEX with ROS strings (Adobe, Identity) ✓
- ROS operator with correct SIDs ✓
- Serialization order matches CFF1 pattern ✓
Remaining issue:
- FDArray INDEX structure needs investigation - fontTools reports
"unpack requires a buffer of 4 bytes" when reading FDArray offsets.
May be related to INDEX offSize or data alignment.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* WIP: Add CFF2 to CFF1 converter for font instantiation
When a CFF2 variable font is fully instantiated (all axes pinned),
convert it to CFF1 format. This is work in progress.
Key changes:
- Add serialize_cff2_to_cff1() function to convert instantiated CFF2 to CFF1
- Use cff2_private_dict_op_serializer_t to instantiate Private DICT blends
- Add CFF1 table structures (Name INDEX, String INDEX for ROS, etc.)
- Create CID-keyed fonts with ROS operator (Adobe-Identity-0)
- Use subsetter's serializer with end_serialize() to resolve links
Current status:
- Private DICT instantiation works (blends evaluated correctly)
- CharStrings are properly flattened (verified: no blend operators remain)
- CFF1 table structure created but CharStrings INDEX validation fails in OTS
- Need to investigate CharStrings serialization format issue
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Add FDSelect and FDArray to Top DICT for CID-keyed fonts
CFF1 CID-keyed fonts require FDSelect and FDArray operators in the
Top DICT, even when there's only one Font DICT. CFF2 makes FDSelect
optional when there's a single FD, but CFF1 always requires it.
This commit:
- Always creates an FDSelect for CID-keyed CFF1 fonts (even with 1 FD)
- Explicitly serializes FDSelect and FDArray operators in Top DICT
- Avoids duplicating these operators when processing CFF2 Top DICT
Fixes font rendering - glyphs now have proper paths.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Use FDSelect3 (range-based) instead of FDSelect0
When creating a default FDSelect for single-FD CID fonts, use the
compact range-based FDSelect3 format instead of FDSelect0.
For a font with 254 glyphs mapping to FD 0:
- FDSelect0: 254 bytes (1 byte per glyph)
- FDSelect3: 8 bytes (format + nRanges + range + sentinel)
Saves 246 bytes (~1KB in this test font).
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Update documentation with implementation status
Documents:
- What's implemented and working
- Known limitations (FontBBox, width, stack depth)
- Key conversion steps
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Copy FontBBox from head table
Instead of calculating from scratch, copy the font bounding box
directly from the head table (xMin, yMin, xMax, yMax).
Before: FontBBox value="0 0 0 0"
After: FontBBox value="-175 -250 1178 899"
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [CFF2->CFF1] Add width optimization and encoding
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [CFF2->CFF1] Port O(n) width optimization algorithm
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [cff2-to-cff1] Add command capture infrastructure for specialization
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [cff2-to-cff1] Implement CharString specialization with stack depth control
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [cff2-to-cff1] Add generalization phase for stack safety
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [cff2-to-cff1] Update documentation to reflect completed features
Removed outdated limitations that have been implemented:
✓ FontBBox (now copied from head table)
✓ Width handling (O(n) optimization + CharString encoding)
✓ Stack depth control (generalize→specialize with maxstack=48)
Added new features to implementation status:
- CharString specialization
- Width optimization and encoding
- Stack depth control
Remaining limitations are now accurate (only CID-keyed, no curve
specialization/peephole optimizations).
* [cff2-to-cff1] Remove CID-keyed 'limitation' from docs
CID-keyed output is by design, not a limitation:
- Variable fonts (CFF2) are instantiated to CID-keyed CFF1
- We explicitly add ROS operator to make output CID-keyed
- This is the correct behavior for instantiated fonts
Changed section from 'KNOWN LIMITATIONS' to 'FUTURE ENHANCEMENTS'
to better reflect that curve/peephole optimizations are optional
improvements, not missing functionality.
* [CFF2->CFF1] Add HB_SUBSET_FLAGS_DOWNGRADE_CFF2 flag
Make the CFF2→CFF1 converter opt-in via the HB_SUBSET_FLAGS_DOWNGRADE_CFF2
flag (disabled by default). This gives users control over whether variable
font instantiation produces CFF2 (default) or CFF1 (for compatibility).
Changes:
- Add HB_SUBSET_FLAGS_DOWNGRADE_CFF2 flag to hb-subset.h
- Gate CFF2→CFF1 conversion on flag check in hb-subset-cff2.cc
- Add --downgrade-cff2 command-line option to hb-subset tool
The flag enables converting instantiated variable fonts from CFF2 to CFF1,
providing compatibility with older renderers that don't support CFF2.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [CFF2->CFF1] Deduplicate CFF1Font string constant
Define CFF1_DEFAULT_FONT_NAME constant once in hb-subset-cff2-to-cff1.hh
and use it in both the plan creation and serialization code.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* [CFF2->CFF1] Fix swapped horizontal/vertical logic in specializer
The specialization logic for rmoveto/rlineto to h/v variants was backwards:
- When dx=0 and dy!=0, movement is VERTICAL (vmoveto/vlineto), not horizontal
- When dy=0 and dx!=0, movement is HORIZONTAL (hmoveto/hlineto), not vertical
This was causing completely garbled outlines in converted fonts. The fix
corrects the operator selection to match the actual movement direction.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Fixed three bugs found during code review:
1. Division by zero in normalize_pt(): Added check to return {0, 0}
when normalizing a zero-length vector, preventing NaN values in
sweep gradient rendering.
2. Memory leak in get_color_stops(): Now properly frees old memory
before reallocating and checks for malloc failure to prevent
null pointer dereference.
3. Integer division in helper_cairo_create_context(): Fixed missing
decimal point causing integer division (br / 255) instead of
floating-point division, which resulted in incorrect alpha
channel calculations for color blending.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Position PNG images at bottom-left of glyph box for correct rendering.
Uses y_bearing + height to account for negative height values in glyph extents.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
PNG color-fonts (like Apple Color Emoji's sbix table) were not being
rendered by hb-draw. There were two issues:
1. probe_paint_funcs had no callbacks registered, so HarfBuzz couldn't
detect PNG glyphs as color glyphs. Fixed by adding a simple
probe_paint_image callback that returns true.
2. PNG images were using negative height directly in SVG <image>
elements, which doesn't work properly. Fixed by wrapping PNG images
in a <g> element with a transform that handles coordinate system
flip, similar to how SVG embedded images are handled.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Removes the --quiet/-q option that output raw path data only.
This simplifies the codebase by eliminating the write_raw_paths()
function and associated logic.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This increases the precision of scale and transform values in SVG output
for better accuracy in rendering.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Renames --ned option to --flat for better clarity. The option flattens
geometry and disables reuse of glyph definitions in SVG output.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Uses std::unordered_map to cache hb_glyph_extents_t by glyph ID, avoiding
redundant hb_font_get_glyph_extents() calls when the same glyph appears
multiple times (e.g., 'e' in "speed").
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
When both --logical and --ink are specified, the bounding box is computed as
the union of logical and ink extents, providing maximum coverage. This matches
the behavior of specifying both flags in hb-view.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds --logical (default) and --ink options to control bounding box calculation.
--logical uses font logical extents (ascender/descender), --ink uses actual
glyph ink extents for a tighter bounding box. Default behavior is --logical.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds --margin option similar to hb-view but with default of 0 instead of 16.
Accepts 1-4 space-separated numbers (top right bottom left, with CSS-style
fallback logic). Expands SVG viewBox and dimensions accordingly.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Previously foreground only worked for color fonts. Now it works for all fonts
by wrapping content in a compact <g fill="..."> element that applies to all
child elements. Supports RGB and RGBA colors with transparency.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds --background command-line option to hb-draw, similar to hb-view/hb-cairo.
Supports RGB and RGBA color formats (rrggbb[aa]). Background is rendered as
an SVG rect element covering the viewBox when specified.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Color glyphs are now defined once in <defs> and reused via <use> elements,
similar to non-color glyphs. Previously each color glyph instance rendered
the full paint operations inline, causing duplication.
Tested with repeating emoji: reduces SVG size by ~44% for files with
repeated color glyphs (e.g., 3 emoji instances: 5.8KB vs 11KB without reuse).
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
In reuse mode, the outer scale(font_scale_x, -font_scale_y) wrapper
already flips Y. The color glyph wrapper was incorrectly also applying
scale(1,-1), causing a double Y-flip that made the output appear
off-bounds at any font size other than upem.
Now only apply scale(1,-1) in upem_scale mode (no outer wrapper).
Also restore precision 9 for 1/upem scale values.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Reduce MAX_ANGLE from pi/8 to pi/16, producing ~32 slices per full
circle instead of ~16. This reduces visible color banding in sweep
gradient approximation.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Coordinate data uses the user's --precision (default 2), but scale and
transform matrix values need more decimal places to avoid compounding
rounding errors. Add scale_precision() helper that returns max(precision, 5).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Creating upem_font as a sub-font of the main font (which has scale =
upem * 64 due to subpixel bits) caused the paint API to emit spurious
parent→child scale transforms. With precision=2 rounding, 1/64 rounds
to 0.02 and 0.02*64 = 1.28, destroying the mathematical cancellation
and making color glyphs ~1.6x too large.
Fix by creating upem_font directly from the face, eliminating the
parent-child relationship. Update expected test outputs accordingly.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Render COLRv0/v1, CBDT/sbix bitmaps, and SVG table glyphs as rich SVG
with colors, gradients, compositing, and inlined bitmap images using
hb_font_paint_glyph(). Falls back to monochrome outlines when no color
data is available.
New options: --no-color, --palette N, --foreground rrggbb[aa]
Glyph outline paths are stored as shared <defs> and reused by both
display <use> elements and <clipPath> references, avoiding duplicate
path data.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Used in conjunction with retain gids, when set the num glyphs from the input font will be preserved. Empty glyphs will be inserted as necessary to maintain the num glyphs value. Put under experimental for now.
* [util] Respect HB_NO_FEATURES_H macro
For builds using harfbuzz.cc and no features file.
* Kill PACKAGE_NAME and PACKAGE_VERSION
Remnants of autotools.
* [BUILD.md] Update
If a shape call was failing, perhaps because of --verify failing,
hb-shape was producing multiple lines possibly, making the test
runner get out of sync and fail all following tests.
https://github.com/harfbuzz/harfruzz/issues/60
Fixes#5281. Does the closure by default, but I've introduced a new flag and option to disable this behaviour since some users may want to get the minimal set if they know they don't need the mirrored variants.