Files
harfbuzz/src/hb-subset.h
T
Behdad EsfahbodandClaude Sonnet 4.5 81ce4813c1 Lower CFF2 to CFF when subsetting to static instance (#5748)
* 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>
2026-02-17 13:42:08 -07:00

302 lines
11 KiB
C

/*
* Copyright © 2018 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Rod Sheeter
*/
#ifndef HB_SUBSET_H
#define HB_SUBSET_H
#include "hb.h"
#include "hb-ot.h"
HB_BEGIN_DECLS
/**
* hb_subset_input_t:
*
* Things that change based on the input. Characters to keep, etc.
*/
typedef struct hb_subset_input_t hb_subset_input_t;
/**
* hb_subset_plan_t:
*
* Contains information about how the subset operation will be executed.
* Such as mappings from the old glyph ids to the new ones in the subset.
*/
typedef struct hb_subset_plan_t hb_subset_plan_t;
/**
* hb_subset_flags_t:
* @HB_SUBSET_FLAGS_DEFAULT: all flags at their default value of false.
* @HB_SUBSET_FLAGS_NO_HINTING: If set hinting instructions will be dropped in
* the produced subset. Otherwise hinting instructions will be retained.
* @HB_SUBSET_FLAGS_RETAIN_GIDS: If set glyph indices will not be modified in
* the produced subset. If glyphs are dropped their indices will be retained
* as an empty glyph.
* @HB_SUBSET_FLAGS_DESUBROUTINIZE: If set and subsetting a CFF font the
* subsetter will attempt to remove subroutines from the CFF glyphs.
* @HB_SUBSET_FLAGS_NAME_LEGACY: If set non-unicode name records will be
* retained in the subset.
* @HB_SUBSET_FLAGS_SET_OVERLAPS_FLAG: If set the subsetter will set the
* OVERLAP_SIMPLE flag on each simple glyph.
* @HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED: If set the subsetter will not
* drop unrecognized tables and instead pass them through untouched.
* @HB_SUBSET_FLAGS_NOTDEF_OUTLINE: If set the notdef glyph outline will be
* retained in the final subset.
* @HB_SUBSET_FLAGS_GLYPH_NAMES: If set the PS glyph names will be retained
* in the final subset.
* @HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES: If set then the unicode ranges in
* OS/2 will not be recalculated.
* @HB_SUBSET_FLAGS_NO_LAYOUT_CLOSURE: If set do not perform glyph closure on layout
* substitution rules (GSUB). Since: 7.2.0.
* @HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS: If set perform IUP delta optimization on the
* remaining gvar table's deltas. Since: 8.5.0
* @HB_SUBSET_FLAGS_NO_BIDI_CLOSURE: If set do not pull mirrored versions of input
* codepoints into the subset. Since: 11.1.0
* @HB_SUBSET_FLAGS_IFTB_REQUIREMENTS: If set enforce requirements on the output subset
* to allow it to be used with incremental font transfer IFTB patches. Primarily,
* this forces all outline data to use long (32 bit) offsets. Since: EXPERIMENTAL
* @HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS: If this flag is set along side
* HB_SUBSET_FLAGS_RETAIN_GIDS then the number of glyphs in the font won't
* be reduced as a result of subsetting. If necessary empty glyphs will be
* included at the end of the font to keep the number of glyphs unchanged.
* @HB_SUBSET_FLAGS_DOWNGRADE_CFF2: If set and instantiating a variable font,
* convert the output CFF2 table to CFF1. This enables compatibility with older
* renderers that don't support CFF2. Since: REPLACEME
*
* List of boolean properties that can be configured on the subset input.
*
* Since: 2.9.0
**/
typedef enum { /*< flags >*/
HB_SUBSET_FLAGS_DEFAULT = 0x00000000u,
HB_SUBSET_FLAGS_NO_HINTING = 0x00000001u,
HB_SUBSET_FLAGS_RETAIN_GIDS = 0x00000002u,
HB_SUBSET_FLAGS_DESUBROUTINIZE = 0x00000004u,
HB_SUBSET_FLAGS_NAME_LEGACY = 0x00000008u,
HB_SUBSET_FLAGS_SET_OVERLAPS_FLAG = 0x00000010u,
HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED = 0x00000020u,
HB_SUBSET_FLAGS_NOTDEF_OUTLINE = 0x00000040u,
HB_SUBSET_FLAGS_GLYPH_NAMES = 0x00000080u,
HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES = 0x00000100u,
HB_SUBSET_FLAGS_NO_LAYOUT_CLOSURE = 0x00000200u,
HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS = 0x00000400u,
HB_SUBSET_FLAGS_NO_BIDI_CLOSURE = 0x00000800u,
#ifdef HB_EXPERIMENTAL_API
HB_SUBSET_FLAGS_IFTB_REQUIREMENTS = 0x00001000u,
HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS = 0x00002000u,
#endif
HB_SUBSET_FLAGS_DOWNGRADE_CFF2 = 0x00004000u,
} hb_subset_flags_t;
/**
* hb_subset_sets_t:
* @HB_SUBSET_SETS_GLYPH_INDEX: the set of glyph indexes to retain in the subset.
* @HB_SUBSET_SETS_UNICODE: the set of unicode codepoints to retain in the subset.
* @HB_SUBSET_SETS_NO_SUBSET_TABLE_TAG: the set of table tags which specifies tables that should not be
* subsetted.
* @HB_SUBSET_SETS_DROP_TABLE_TAG: the set of table tags which specifies tables which will be dropped
* in the subset.
* @HB_SUBSET_SETS_NAME_ID: the set of name ids that will be retained.
* @HB_SUBSET_SETS_NAME_LANG_ID: the set of name lang ids that will be retained.
* @HB_SUBSET_SETS_LAYOUT_FEATURE_TAG: the set of layout feature tags that will be retained
* in the subset.
* @HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG: the set of layout script tags that will be retained
* in the subset. Defaults to all tags. Since: 5.0.0
*
* List of sets that can be configured on the subset input.
*
* Since: 2.9.1
**/
typedef enum {
HB_SUBSET_SETS_GLYPH_INDEX = 0,
HB_SUBSET_SETS_UNICODE,
HB_SUBSET_SETS_NO_SUBSET_TABLE_TAG,
HB_SUBSET_SETS_DROP_TABLE_TAG,
HB_SUBSET_SETS_NAME_ID,
HB_SUBSET_SETS_NAME_LANG_ID,
HB_SUBSET_SETS_LAYOUT_FEATURE_TAG,
HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG,
} hb_subset_sets_t;
HB_EXTERN hb_subset_input_t *
hb_subset_input_create_or_fail (void);
HB_EXTERN hb_subset_input_t *
hb_subset_input_reference (hb_subset_input_t *input);
HB_EXTERN void
hb_subset_input_destroy (hb_subset_input_t *input);
HB_EXTERN hb_bool_t
hb_subset_input_set_user_data (hb_subset_input_t *input,
hb_user_data_key_t *key,
void * data,
hb_destroy_func_t destroy,
hb_bool_t replace);
HB_EXTERN void *
hb_subset_input_get_user_data (const hb_subset_input_t *input,
hb_user_data_key_t *key);
HB_EXTERN void
hb_subset_input_keep_everything (hb_subset_input_t *input);
HB_EXTERN hb_set_t *
hb_subset_input_unicode_set (hb_subset_input_t *input);
HB_EXTERN hb_set_t *
hb_subset_input_glyph_set (hb_subset_input_t *input);
HB_EXTERN hb_set_t *
hb_subset_input_set (hb_subset_input_t *input, hb_subset_sets_t set_type);
HB_EXTERN hb_map_t*
hb_subset_input_old_to_new_glyph_mapping (hb_subset_input_t *input);
HB_EXTERN hb_subset_flags_t
hb_subset_input_get_flags (hb_subset_input_t *input);
HB_EXTERN void
hb_subset_input_set_flags (hb_subset_input_t *input,
unsigned value);
HB_EXTERN hb_bool_t
hb_subset_input_pin_all_axes_to_default (hb_subset_input_t *input,
hb_face_t *face);
HB_EXTERN hb_bool_t
hb_subset_input_pin_axis_to_default (hb_subset_input_t *input,
hb_face_t *face,
hb_tag_t axis_tag);
HB_EXTERN hb_bool_t
hb_subset_input_pin_axis_location (hb_subset_input_t *input,
hb_face_t *face,
hb_tag_t axis_tag,
float axis_value);
HB_EXTERN hb_bool_t
hb_subset_input_get_axis_range (hb_subset_input_t *input,
hb_tag_t axis_tag,
float *axis_min_value,
float *axis_max_value,
float *axis_def_value);
HB_EXTERN hb_bool_t
hb_subset_input_set_axis_range (hb_subset_input_t *input,
hb_face_t *face,
hb_tag_t axis_tag,
float axis_min_value,
float axis_max_value,
float axis_def_value);
HB_EXTERN hb_bool_t
hb_subset_axis_range_from_string (const char *str, int len,
float *axis_min_value,
float *axis_max_value,
float *axis_def_value);
HB_EXTERN void
hb_subset_axis_range_to_string (hb_subset_input_t *input,
hb_tag_t axis_tag,
char *buf,
unsigned size);
#ifdef HB_EXPERIMENTAL_API
HB_EXTERN hb_bool_t
hb_subset_input_override_name_table (hb_subset_input_t *input,
hb_ot_name_id_t name_id,
unsigned platform_id,
unsigned encoding_id,
unsigned language_id,
const char *name_str,
int str_len);
/*
* Raw outline data access
*/
HB_EXTERN hb_blob_t*
hb_subset_cff_get_charstring_data (hb_face_t* face, hb_codepoint_t glyph_index);
HB_EXTERN hb_blob_t*
hb_subset_cff_get_charstrings_index (hb_face_t* face);
HB_EXTERN hb_blob_t*
hb_subset_cff2_get_charstring_data (hb_face_t* face, hb_codepoint_t glyph_index);
HB_EXTERN hb_blob_t*
hb_subset_cff2_get_charstrings_index (hb_face_t* face);
#endif
HB_EXTERN hb_face_t *
hb_subset_preprocess (hb_face_t *source);
HB_EXTERN hb_face_t *
hb_subset_or_fail (hb_face_t *source, const hb_subset_input_t *input);
HB_EXTERN hb_face_t *
hb_subset_plan_execute_or_fail (hb_subset_plan_t *plan);
HB_EXTERN hb_subset_plan_t *
hb_subset_plan_create_or_fail (hb_face_t *face,
const hb_subset_input_t *input);
HB_EXTERN void
hb_subset_plan_destroy (hb_subset_plan_t *plan);
HB_EXTERN hb_map_t *
hb_subset_plan_old_to_new_glyph_mapping (const hb_subset_plan_t *plan);
HB_EXTERN hb_map_t *
hb_subset_plan_new_to_old_glyph_mapping (const hb_subset_plan_t *plan);
HB_EXTERN hb_map_t *
hb_subset_plan_unicode_to_old_glyph_mapping (const hb_subset_plan_t *plan);
HB_EXTERN hb_subset_plan_t *
hb_subset_plan_reference (hb_subset_plan_t *plan);
HB_EXTERN hb_bool_t
hb_subset_plan_set_user_data (hb_subset_plan_t *plan,
hb_user_data_key_t *key,
void *data,
hb_destroy_func_t destroy,
hb_bool_t replace);
HB_EXTERN void *
hb_subset_plan_get_user_data (const hb_subset_plan_t *plan,
hb_user_data_key_t *key);
HB_END_DECLS
#endif /* HB_SUBSET_H */