Commit Graph
15558 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.6 fea61c8ba1 hb-raster: use incremental fy in column-crossing loops
Precompute delta_fy = 64 * total_dy / total_dx once per edge per row,
then accumulate instead of dividing per column.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 06842d1a42 hb-raster: precompute edge slope to avoid per-row divisions
Replace dx/dy fields with a 16.16 fixed-point slope precomputed at
edge creation time.  The two integer divisions per edge per scanline
in edge_sweep_row become multiply+shift.  Same struct size.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 5bb6140c25 hb-raster: skip expired edges with advancing start index
Maintain a monotonically-advancing start index that skips past edges
whose yH has fallen below the current row.  This avoids re-scanning
the dead prefix of the sorted edge array on every scanline.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 68c30811c2 hb-raster: minor rasterizer optimizations
- Use hb_qsort() instead of qsort() to avoid function-pointer overhead
- Use >> 13 instead of / 8192 for alpha computation (unsigned shift)
- Track per-row x_min/x_max of touched cells; only sweep and clear
  that range instead of memset'ing the full width every row

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 d3c6a807d3 hb-raster: replace tile-based sampled rasterizer with analytic coverage
Replace the multi-sample tile rasterizer (NEON / SSE4.1 / scalar variants)
with an analytic-coverage approach that computes exact area and cover
contributions per pixel cell per edge.  This eliminates the tile bookkeeping
(tile_offsets, tile_edge_indices) and SIMD-specific code paths in favour of
a simpler, portable scanline sweep that yields higher quality output.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 a7377f7678 hb-cairo: render at device-pixel resolution when --raster is active
Instead of rendering the hb-raster image at subpixel resolution
(x_scale = font_size * 64 units/em, causing Cairo to resample 64×),
render at device-pixel resolution using the scale_matrix from the
scaled font.

The rasterizer transform is set to (px_x/x_scale, 0, 0, px_y/y_scale)
where px_x = scale_matrix.xx = font_size in device pixels per em.
The image is then placed with cairo_scale(1/px_x, -1/px_y) to map
raster pixels 1:1 to device pixels with Y-flip.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 1c23fc51b0 hb-cairo: add hb_cairo_font_face_set/get_raster(); wire up hb-view --raster
When enabled, hb_cairo_render_glyph() rasterizes via hb_raster and
blits the A8 coverage image using cairo_mask_surface(), leaving the
existing Cairo source undisturbed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandKhaled Hosny 99a84d7f02 Update src/hb-raster.h
Co-authored-by: Khaled Hosny <khaled@aliftype.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandKhaled Hosny 100bdbd244 Update src/hb-raster.h
Co-authored-by: Khaled Hosny <khaled@aliftype.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 c9d459b8e9 hb-raster: fix tile bounds clipping; move test to test/api/
Fix out-of-bounds pixel write in rasterize_tile: border tiles that
extend past the image edge were writing px-x_origin beyond the row
stride, wrapping into the start of the next row and overwriting
correctly-rasterized pixels with zero.  Pass px1/py1 (exclusive
bounds) to rasterize_tile and clip the pixel loops accordingly.

Also: drop unused ex0 in pass 2 of tile assignment; move test-raster
from src/ to test/api/ and convert it to the GLib g_assert/hb_test_add
framework.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 f622f99ee6 [hb-raster] Add harfbuzz-raster CPU A8 rasterizer library
New library harfbuzz-raster provides a fast CPU coverage rasterizer for
glyph outlines.  Three public types: hb_raster_image_t (ref-counted pixel
buffer), hb_raster_draw_t (outline rasterizer consuming hb_draw_funcs_t
callbacks), and a stub for hb_raster_paint_t (v2).

Key design points:
- Nonzero winding rule
- 26.6 fixed-point edge representation
- 8-sample supersampling per pixel (HB_RASTER_SAMPLES constant)
- 16×16 tile culling with CSR edge lists
- SIMD inner loops: NEON (aarch64) and SSE4.1 (x86); scalar fallback
- Bézier flattening (quadratic + cubic) via de Casteljau, max depth 16
- Auto-size from edge bounding box or caller-supplied extents

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 a1d9c296d8 hb_vector_t::extend: add exact parameter for tight allocation
Add bool exact=false parameter to all extend() overloads.  When true,
the allocation is exact (no growth headroom), useful for copy
constructors.  Pass exact=true from the Iterable copy constructor.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:26:46 -03:00
Behdad EsfahbodandClaude Opus 4.6 65270b19e8 Fix hb_vector_t::extend() buffer overflow for non-empty vectors
extend(Iterable) called alloc(hb_len(iter)) which only allocated
space for the new elements, ignoring existing length.  When the
vector already had content, push_has_room would write past the
allocated buffer.

Fix: alloc(length + hb_len(iter)).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:26:32 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 63fb2eec90 Monotone characters fixups (#5752)
* [buffer] Fix a typo and doc string

* [buffer] Add merge_grapheme_clusters / merge_out_grapheme_clusters

Cluster merges serve two distinct purposes that require different level
gates:

- Grapheme grouping: combining marks merged into their base cluster.
  Correct gate: IS_GRAPHEMES (levels 0 and 3).
  New functions: merge_grapheme_clusters / merge_out_grapheme_clusters.

- Monotonicity maintenance: clusters merged to prevent non-monotone
  output after ligature formation, reordering, etc.
  Correct gate: IS_MONOTONE (levels 0 and 1).
  Existing functions: merge_clusters / merge_out_clusters.

Previously both paths shared merge_clusters_impl's single IS_MONOTONE
gate, so GRAPHEMES (level 3) was incorrectly blocked from merging
grapheme clusters.  This also caused a regression in the Thai and
Hangul shapers for MONOTONE_CHARACTERS (level 1): when their explicit
== MONOTONE_GRAPHEMES call-site guards were removed in #5069, the
impls' IS_MONOTONE gate no longer protected them.

Gates are now in the inline wrappers in hb-buffer.hh; the _impl
functions are gate-free.  hb_form_clusters, CoreText grapheme
detection, and the Thai/Hangul shapers are updated to call the
grapheme-semantic variants.  Fixes #5750.

Fixes https://github.com/harfbuzz/harfbuzz/issues/5751
Fixes https://github.com/harfbuzz/harfbuzz/issues/5750

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* [thai] Adjust grapheme merging logic

* [hangul] Tweak grapheme merging

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 11:25:24 -07:00
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
Alexander Lobakin 7d67028e08 [hb-directwrite] Fix building with HB_DISABLE_DEPRECATED
Fix the following build error with HB_DISABLE_DEPRECATED:

In file included from harfbuzz\src\harfbuzz.cc:14:
harfbuzz\src\hb-directwrite.cc(468,1): error : no previous prototype
for function 'hb_directwrite_font_get_dw_font'
[-Werror,-Wmissing-prototypes]
    468 | hb_directwrite_font_get_dw_font (hb_font_t *font)
        | ^

hb_directwrite_font_get_dw_font() is guarded with
HB_DISABLE_DEPRECATED only in the header file, but not in the .cc.

Signed-off-by: Alexander Lobakin <alobakin@mailbox.org>
2026-02-16 02:28:45 +01:00
Khaled Hosny c763763662 [buffer-serialize] Fix calculating absolute offsets with partial range
Fixes https://github.com/harfbuzz/harfbuzz/issues/5743
2026-02-15 17:30:16 +02:00
Behdad Esfahbod 79ccd2112a Handle a couple of degenerate cases in solve_itp() 2026-02-15 10:19:27 -03:00
Behdad Esfahbod dc3303963c [update-unicode-tables.make] Add 'touch' target
To force regeneration.
2026-02-14 21:32:11 -03:00
Garret Rieger 07e48ef1f0 Fix two bugs in hb_set_is_subset().
1. If smaller set has empty pages the subset checking logic was incorrectly incrementing the larger page index after skipping the empty smaller page index.
2. The logic for is_subset() on mixed inverted/not inverted sets was wrong.
2026-02-13 15:06:01 -07:00
Behdad Esfahbod 6675656bff Update Unicode tables to packtab 1.2.0 2026-02-12 20:06:26 -05:00
Behdad Esfahbod 8f7733c7ff Regenerate Unicode tables with newer packtab 1.1.0 2026-02-12 19:44:29 -05:00
Behdad Esfahbod bac5128a11 Update Unicode tables to packtab 1.0.0 2026-02-12 15:24:50 -07:00
Behdad Esfahbod 2deabfbc90 [cmap/format4] Try to ignore wrong subtable length (#5740)
* WIP [cmap/format4] Try to ignore wrong subtable length

Fixes https://github.com/harfbuzz/harfbuzz/issues/5739

See comments.

* [cmap/format4] Followup fix for unreliable subtable length field

* [cmap/format4] Try to fix previous commit

Let's see..
Let's see..

* [cmap/format4] Another try at fixing recent changes

* [cmap/format4] Still try to fix

* [cmap/format4] Refactor duplicated code and disable unsafe path
2026-02-11 16:55:08 -07:00
Qunxin Liu 7ed158f930 Fix fuzzer issue (#5741)
Fixes: https://oss-fuzz.com/testcase-detail/6406793692774400
2026-02-11 13:12:30 -07:00
Behdad Esfahbod 1daff76c28 [VARC] TCenter really is parsed after Skew according to spec 2026-02-06 14:56:29 -07:00
Behdad Esfahbod 5f2df0a3b2 [VARC] Convert a division to mult 2026-02-06 13:12:57 -07:00
Behdad Esfahbod d6039e579b More accurate HB_PI
This doesn't seem to fix my error, but doesn't hurt to have.
2026-02-06 13:02:08 -07:00
Behdad Esfahbod 7c110fdfd6 [VARC] More speculation 2026-02-06 01:17:50 -07:00
Behdad Esfahbod ad62833d60 [VARC] Another shot 2026-02-06 00:48:33 -07:00
Behdad Esfahbod 7ee6cac08b [VARC] Another shot in the dark 2026-02-06 00:37:47 -07:00
Behdad Esfahbod f5c32f8704 Revert "[VARC] Another long shot"
This reverts commit 582af96b00.
2026-02-06 00:12:24 -07:00
Behdad Esfahbod 582af96b00 [VARC] Another long shot 2026-02-06 00:03:51 -07:00
Behdad Esfahbod 17c8a21907 [VARC] Try fixing double scaling 2026-02-05 23:38:55 -07:00
Behdad Esfahbod 8231730aaa [VARC] A couple more rounding issues 2026-02-05 22:33:27 -07:00
Behdad Esfahbod 63562615d4 [VARC] Process axes in forward order
In case there are duplicates...
2026-02-05 22:00:09 -07:00
Behdad Esfahbod 22b4e63743 [VARC] Don't round transform components for division 2026-02-05 21:58:50 -07:00
Behdad Esfahbod c1e94eb08e [gvar] Remove unused variable assignment 2026-02-05 15:33:27 -07:00
Behdad Esfahbod b863a8f462 Revert "[rust] Use HarfBuzz-style glyph outline"
This reverts commit 6ba28949f5.
2026-02-05 15:32:46 -07:00
Behdad Esfahbod 6ba28949f5 [rust] Use HarfBuzz-style glyph outline
Slightly slower, but matches our point order better.
2026-02-05 15:24:53 -07:00
Behdad Esfahbod ccaef949c4 [gvar] Speed up by using a fused delta application codepath (#5730)
* [gvar] Speed up by using a fused delta application codepath

6% speedup in hanzi VARC font rendering.

* [gvar] Improve fused delta application

Another 1%.
2026-02-05 14:20:19 -07:00
Behdad Esfahbod c42f4b5218 [VARC] Implement lazy skipping 2026-02-05 12:44:49 -07:00
Behdad Esfahbod 0334f2e38d [rust] Add a couple of compiler flags (#5729)
Makes binary 100kb smaller!
2026-02-04 05:07:31 -07:00
Behdad Esfahbod b1c71874a4 [rust] Use panic="abort" in debugoptimized builds as well 2026-02-03 23:43:00 -07:00
Behdad Esfahbod 8c8f76d690 [scalar-cache] Enlarge, and adjust scratch logic (#5728)
No need to clear on default construction, or set length even.
2026-02-03 23:40:55 -07:00
Khaled Hosny b42511e071 12.3.2 2026-01-24 14:09:52 +02:00
Qunxin Liu e601560b77 fix fuzzer issue: https://oss-fuzz.com/testcase-detail/6005602106277888
Do not use pointers that may point outside of the allocated memory region
2026-01-23 11:44:39 -07:00
Behdad Esfahbod e365cd0e2e [cmap] Another null check
https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-pgp8-9vq3-fq2r#advisory-comment-154898
2026-01-22 19:01:39 -07:00
Behdad Esfahbod dc73849f56 [gvar] Fix padding size calcs (#5718)
The previous state of is_long_offset() is irrelevant here.

Fixes https://oss-fuzz.com/testcase-detail/5920156651159552
2026-01-22 18:08:51 -07:00
Qunxin Liu b17fa8ef39 Fix fuzzer found heap-use-after-free crash (#5717)
Fixes https://oss-fuzz.com/testcase-detail/6005602106277888
2026-01-21 14:42:29 -07:00