mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 14:31:10 +01:00
* 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>
208 lines
5.5 KiB
C++
208 lines
5.5 KiB
C++
/*
|
|
* CFF Width Optimizer
|
|
*
|
|
* Determines optimal defaultWidthX and nominalWidthX values
|
|
* to minimize CharString byte cost.
|
|
*
|
|
* Based on fontTools.cffLib.width
|
|
*/
|
|
|
|
#ifndef HB_CFF_WIDTH_OPTIMIZER_HH
|
|
#define HB_CFF_WIDTH_OPTIMIZER_HH
|
|
|
|
#include "hb.hh"
|
|
|
|
namespace CFF {
|
|
|
|
/* Calculate byte cost for encoding a width delta */
|
|
static inline unsigned
|
|
width_delta_cost (int delta)
|
|
{
|
|
delta = abs (delta);
|
|
if (delta <= 107) return 1;
|
|
if (delta <= 1131) return 2;
|
|
return 5;
|
|
}
|
|
|
|
/* Cumulative sum forward */
|
|
static void
|
|
cumsum_forward (const hb_hashmap_t<unsigned, unsigned> &freq,
|
|
unsigned min_w, unsigned max_w,
|
|
hb_vector_t<unsigned> &cumsum)
|
|
{
|
|
cumsum.resize (max_w - min_w + 1);
|
|
unsigned v = 0;
|
|
for (unsigned x = min_w; x <= max_w; x++)
|
|
{
|
|
v += freq.get (x);
|
|
cumsum[x - min_w] = v;
|
|
}
|
|
}
|
|
|
|
/* Cumulative max forward */
|
|
static void
|
|
cummax_forward (const hb_hashmap_t<unsigned, unsigned> &freq,
|
|
unsigned min_w, unsigned max_w,
|
|
hb_vector_t<unsigned> &cummax)
|
|
{
|
|
cummax.resize (max_w - min_w + 1);
|
|
unsigned v = 0;
|
|
for (unsigned x = min_w; x <= max_w; x++)
|
|
{
|
|
v = hb_max (v, freq.get (x));
|
|
cummax[x - min_w] = v;
|
|
}
|
|
}
|
|
|
|
/* Cumulative sum backward */
|
|
static void
|
|
cumsum_backward (const hb_hashmap_t<unsigned, unsigned> &freq,
|
|
unsigned min_w, unsigned max_w,
|
|
hb_vector_t<unsigned> &cumsum)
|
|
{
|
|
cumsum.resize (max_w - min_w + 1);
|
|
unsigned v = 0;
|
|
for (int x = (int) max_w; x >= (int) min_w; x--)
|
|
{
|
|
v += freq.get ((unsigned) x);
|
|
cumsum[x - min_w] = v;
|
|
}
|
|
}
|
|
|
|
/* Cumulative max backward */
|
|
static void
|
|
cummax_backward (const hb_hashmap_t<unsigned, unsigned> &freq,
|
|
unsigned min_w, unsigned max_w,
|
|
hb_vector_t<unsigned> &cummax)
|
|
{
|
|
cummax.resize (max_w - min_w + 1);
|
|
unsigned v = 0;
|
|
for (int x = (int) max_w; x >= (int) min_w; x--)
|
|
{
|
|
v = hb_max (v, freq.get ((unsigned) x));
|
|
cummax[x - min_w] = v;
|
|
}
|
|
}
|
|
|
|
/* Helper to safely get cumulative value with bounds checking */
|
|
static inline unsigned
|
|
safe_get (const hb_vector_t<unsigned> &vec, int x, unsigned min_w, unsigned max_w)
|
|
{
|
|
if (x < (int) min_w || x > (int) max_w) return 0;
|
|
return vec[x - min_w];
|
|
}
|
|
|
|
/* Optimize defaultWidthX and nominalWidthX from a list of widths
|
|
* O(UPEM+numGlyphs) algorithm from fontTools.cffLib.width */
|
|
static void
|
|
optimize_widths (const hb_vector_t<unsigned> &width_list,
|
|
unsigned &default_width,
|
|
unsigned &nominal_width)
|
|
{
|
|
if (width_list.length == 0)
|
|
{
|
|
default_width = nominal_width = 0;
|
|
return;
|
|
}
|
|
|
|
/* Build frequency map */
|
|
hb_hashmap_t<unsigned, unsigned> widths;
|
|
unsigned min_w = width_list[0];
|
|
unsigned max_w = width_list[0];
|
|
|
|
for (unsigned w : width_list)
|
|
{
|
|
widths.set (w, widths.get (w) + 1);
|
|
min_w = hb_min (min_w, w);
|
|
max_w = hb_max (max_w, w);
|
|
}
|
|
|
|
/* Cumulative sum/max forward/backward */
|
|
hb_vector_t<unsigned> cumFrqU, cumMaxU, cumFrqD, cumMaxD;
|
|
cumsum_forward (widths, min_w, max_w, cumFrqU);
|
|
cummax_forward (widths, min_w, max_w, cumMaxU);
|
|
cumsum_backward (widths, min_w, max_w, cumFrqD);
|
|
cummax_backward (widths, min_w, max_w, cumMaxD);
|
|
|
|
/* Cost per nominal choice, without default consideration */
|
|
auto nomnCost = [&] (unsigned x) -> unsigned {
|
|
return safe_get (cumFrqU, x, min_w, max_w) +
|
|
safe_get (cumFrqU, x - 108, min_w, max_w) +
|
|
safe_get (cumFrqU, x - 1132, min_w, max_w) * 3 +
|
|
safe_get (cumFrqD, x, min_w, max_w) +
|
|
safe_get (cumFrqD, x + 108, min_w, max_w) +
|
|
safe_get (cumFrqD, x + 1132, min_w, max_w) * 3 -
|
|
widths.get (x);
|
|
};
|
|
|
|
/* Cost-saving per nominal choice, by best default choice */
|
|
auto dfltCost = [&] (unsigned x) -> unsigned {
|
|
unsigned u = hb_max (hb_max (safe_get (cumMaxU, x, min_w, max_w),
|
|
safe_get (cumMaxU, x - 108, min_w, max_w) * 2),
|
|
safe_get (cumMaxU, x - 1132, min_w, max_w) * 5);
|
|
unsigned d = hb_max (hb_max (safe_get (cumMaxD, x, min_w, max_w),
|
|
safe_get (cumMaxD, x + 108, min_w, max_w) * 2),
|
|
safe_get (cumMaxD, x + 1132, min_w, max_w) * 5);
|
|
return hb_max (u, d);
|
|
};
|
|
|
|
/* Find best nominal */
|
|
unsigned best_nominal = min_w;
|
|
unsigned best_cost = nomnCost (min_w) - dfltCost (min_w);
|
|
|
|
for (unsigned x = min_w + 1; x <= max_w; x++)
|
|
{
|
|
unsigned cost = nomnCost (x) - dfltCost (x);
|
|
if (cost < best_cost)
|
|
{
|
|
best_cost = cost;
|
|
best_nominal = x;
|
|
}
|
|
}
|
|
|
|
/* Work back the best default */
|
|
unsigned best_default = best_nominal;
|
|
unsigned best_default_cost = (unsigned) -1;
|
|
|
|
/* Check candidates around best_nominal */
|
|
int candidates[] = {
|
|
(int) best_nominal,
|
|
(int) best_nominal - 108,
|
|
(int) best_nominal - 1132,
|
|
(int) best_nominal + 108,
|
|
(int) best_nominal + 1132
|
|
};
|
|
|
|
for (int candidate : candidates)
|
|
{
|
|
if (candidate < (int) min_w || candidate > (int) max_w)
|
|
continue;
|
|
|
|
/* Compute actual cost with this default */
|
|
unsigned cost = 0;
|
|
for (auto kv : widths.iter ())
|
|
{
|
|
unsigned w = kv.first;
|
|
unsigned freq = kv.second;
|
|
|
|
if (w == (unsigned) candidate)
|
|
continue;
|
|
|
|
cost += freq * width_delta_cost ((int) w - (int) best_nominal);
|
|
}
|
|
|
|
if (cost < best_default_cost)
|
|
{
|
|
best_default_cost = cost;
|
|
best_default = (unsigned) candidate;
|
|
}
|
|
}
|
|
|
|
default_width = best_default;
|
|
nominal_width = best_nominal;
|
|
}
|
|
|
|
} /* namespace CFF */
|
|
|
|
#endif /* HB_CFF_WIDTH_OPTIMIZER_HH */
|