Commit graph

8292 commits

Author SHA1 Message Date
Werner Lemberg
f68733d4a8 [autofit] Speed up construction of reverse character map. (1/4)
Build reverse cmap for characters of the current script style only.

* src/autofit/afadjust.c (in_range): New function.
  (af_reverse_character_map_new): Pass `AF_StyleMetrics` instead of
  `AF_FaceGlobals` so that we can access the current script style.
  Use `in_range` to limit tested code points.
* src/autofit/afadjust.h: Updated.
* src/autofit/aflatin.c (af_latin_metrics_init): Updated.
2025-05-25 04:50:46 +00:00
Werner Lemberg
4514f05158 [autofit] Avoid variable shadowing.
* src/autofit/afadjust.c (af_reverse_character_map_new): Do it.
2025-05-25 04:50:46 +00:00
Werner Lemberg
fd33c037ae [autofit] Minor speedup of reverse charmap creation.
* src/autofit/afglobal.h (AF_HAS_CMAP_ENTRY): New flag.
  (AF_STYLE_MASK): Update value.
* src/autofit/afglobal.c (af_face_globals_compute_style_coverage): Set
  `AF_HAS_CMAP_ENTRY`.

* src/autofit/afadjust.c (af_reverse_character_map_new): For the creation of
  the reverse map, change code to handle glyphs with cmap entries before
  glyphs without cmap entries.  Doing so avoids some code redundancy and
  reduces the number of hash lookups.
2025-05-25 04:50:46 +00:00
Werner Lemberg
541ad3d3cb [autofit] Whitespace. 2025-05-25 04:50:46 +00:00
Werner Lemberg
72e0c0711a [autofit] Use FT_Offset.
* src/autofit/afadjust.c (af_adjustment_database_lookup,
  af_reverse_character_map_new): Do it.
2025-05-25 04:50:46 +00:00
Werner Lemberg
47e35a5837 [autofit] Simplify return value of af_adjustment_database_lookup.
* src/autofit/adfadjust.h (af_adjustment_database_lookup): Return integer.
  (AF_AdjustmentDatabaseEntry): Move structure to...
* src/autofit/afadjust.c: ...this file.
  (af_adjustment_database_lookup, af_reverse_character_map_new): Updated.
* src/autofit/aflatin.c
  (af_glyph_hints_apply_vertical_separation_adjustments): Updated.
2025-05-25 04:50:46 +00:00
Werner Lemberg
94e93c7c75 [autofit] Remove no longer used functions and structures.
This is the clean-up from the previous commit.

* src/autofit/afadjust.c (af_reverse_character_map_entry_compare,
  af_reverse_character_map_lookup, af_reverse_character_map_expand):
  Removed.
* src/autofit/afadjust.h: Updated.
* src/autofit/aftypes.h (AF_ReverseMapEntry, AF_ReverseCharacterMap):
  Removed.
2025-05-25 04:50:46 +00:00
Werner Lemberg
2492b5477b [autofit] Use a hash for handling vertical accent adjustments.
This greatly simplifies the code.

* src/autofit/afadjust.c (af_reverse_character_map_new): Implement it.
  (af_reverse_character_map_done): Updated.
* src/autofit/afadjust.h: Updated.

* src/autofit/aflatin.c
  (af_glyph_hints_apply_vertical_separation_adjustments): Updated.

* src/autofit/aftypes.h: Include `fthash.h`.
  (AF_StyleMetrics): Change type of `reverse_charmap` to `FT_Hash`.
2025-05-25 04:50:46 +00:00
Werner Lemberg
701541662c * src/autofit/aflatin.c (af_latin_hints_apply): Improve code legibility. 2025-05-25 04:50:46 +00:00
Werner Lemberg
9dc66cbd0c [autofit] Use FT_LONG_MIN and FT_LONG_MAX for FT_Pos variables.
* src/autofit/aflatin.c (af_latin_metrics_init_blues,
  af_compute_vertical_extrema, af_find_highest_contour,
  af_find_second_highest_contour, af_find_lowest_contour,
  af_find_second_lowest_contour, af_check_contour_horizontal_overlap):
  Updated.
2025-05-25 04:50:46 +00:00
Werner Lemberg
d6e087d11b [autofit] Fix uninitialized variables.
* src/autofit/aflatin.c (af_latin_stretch_top_tilde,
  af_latin_stretch_bottom_tilde): Initialize `min_measurement`.
2025-05-25 04:50:46 +00:00
Behdad Esfahbod
5987a9f51d [ttmtx] Fix check for being non-default variation position.
If user explicitly sets variations to `NULL` after it being set to something
else, `face->blend` will still be non-`NULL`.  The intention here however is
correctly captured by `FT_IS_VARIATION`.

* src/sfnt/ttmtx.c (tt_face_get_metrics): Use `FT_IS_VARIATION`.
2025-05-23 08:23:51 +02:00
Behdad Esfahbod
75b84313cf [gxvar] Optimize loading of 'gvar' table.
* src/truetype/ttgxvar.c (tt_var_load_item_variation_store,
  tt_var_load_delta_set_index_mapping, ft_var_load_gvar): Use frames to
  reduce bounds checking.
  (TT_Vary_Apply_Glyph_Deltas): Better macro usage.
2025-05-23 08:06:02 +02:00
Behdad Esfahbod
42a649be49 [mm] Short-circuit setting face to default instance if already is so.
I found that in hb-ft I need to sometime reset the face to the default
instance.  However, calling `FT_Set_Var_Design_Coordinates` with no coords
was still slowing down at least the glyph-advance code by processing
variations.

Perhaps there's a better fix, but this handles the easy cases.

* src/base/ftmm.c (FT_Set_Var_Design_Coordinates): Implement it.
2025-05-22 05:12:07 +02:00
Behdad Esfahbod
702e4a1d32 * src/truetype/ttgxvar.c (TT_Vary_Apply_Glyph_Deltas): Fix allocation.
Bug was introduced in commit 582de1a4b6.
2025-05-22 04:57:17 +02:00
Werner Lemberg
013adc82b9 * src/truetype/ttgxvar.c (ft_var_apply_tuple): Remove redundant test.
This was erroneously added in commit e6e9110a8.
2025-05-21 17:28:52 +02:00
ColdPaleLight
11fa9da720 [sdf] Skip neighbor comparison for pixels near edges.
If the distance of the current pixel to the edge is less than or equal
to 0.5, skip further neighbor comparisons to avoid unnecessary
calculations. When the `USE_SQUARED_DISTANCES` macro is defined,
compare to 0.25 instead.

* src/sdf/ftbsdf.c (compare_neighbor): Add early return for pixels
  close to edges to avoid unnecessary neighbor checks.
2025-05-21 12:51:45 +00:00
Behdad Esfahbod (بهداد اسفهبد)
d7b6e947a3 [gxvar] Cache shared-tuple scalar values.
GoogleSansFlex HB benchmark-font draw results:

  Before:  2.13ms
  After:   1.65ms
  Speedup: 22%

* src/truetype/ttgxvar.h (GX_BlendRec): Add `tuplescalars` field.

* src/truetype/ttgxvar.c (ft_var_load_gvar): Allocate `tuplescalars` array.
  (tt_set_mm_blend): Set its values.
  (TT_Vary_Apply_Glyph_Deltas): Use it.
  (tt_done_blend): Deallocate it.
2025-05-21 14:39:22 +02:00
Behdad Esfahbod (بهداد اسفهبد)
582de1a4b6 [gxvar] Reduce number of mallocs.
8% speed up in drawing GoogleSansFlex variable in HB benchmark-font.

* src/truetype/ttgxvar.c (TT_Vary_Apply_Glyph_Deltas): Use an allocation
  pool instead of individual allocations.
2025-05-21 14:39:22 +02:00
Behdad Esfahbod (بهداد اسفهبد)
fd92af79d8 [gxvar] Another optimization in computing scalar.
Another ~6% speedup in GoogleSansFlex var drawing.

* src/truetype/ttgxvar.c (ft_var_apply_tuple): Test `ncv` for being zero.
2025-05-21 14:39:22 +02:00
Behdad Esfahbod (بهداد اسفهبد)
e7cc14ba16 [gxvar] Remove stale comment.
The code *does* check against start/end!
2025-05-21 14:39:20 +02:00
Behdad Esfahbod (بهداد اسفهبد)
e6e9110a8f * src/truetype/ttgxvar.c (ft_var_apply_tuple): Micro-optimize. 2025-05-21 14:16:11 +02:00
Behdad Esfahbod (بهداد اسفهبد)
14399c73c4 [gxvar] Inline two USHORT reads.
Speeds up GoogleSansFlex variable glyph drawing by ~13%.

  Before: 2.95ms
  After:  2.57ms

* src/truetype/ttgxvar.c (TT_Vary_Apply_Glyph_Deltas): Do it.
2025-05-21 14:14:02 +02:00
Behdad Esfahbod (بهداد اسفهبد)
0a574d50e8 [gxvar] Micro-optimize loop end condition.
* src/truetype/ttgxvar.c (TT_Vary_Apply_Glyph_Deltas): Do it.
2025-05-21 14:01:12 +02:00
Behdad Esfahbod
dc8efeff2a [truetype] Don't process mvar metrics if default instance.
GoogleSansFlex default-instance HarfBuzz benchmark-font draw:

  Before:  0.610ms
  After:   0.435ms
  Speedup: 28%

* src/truetype/ttobjs.c (tt_face_init) [TT_CONFIG_OPTION_GX_VAR_SUPPORT]:
  Check `instance_index`.
2025-05-21 09:09:01 +02:00
Alexei Podtelezhnikov
22511643da * src/truetype/ttinterp.c (Ins_MIRP,DELTAP,DELTAC): Minor. 2025-05-20 22:18:19 -04:00
Alexei Podtelezhnikov
8b4dcd935e [truetype] Update bytecode error handling.
* src/truetype/ttinterp.c (Ins_SHP,SHPIX,ALIGNRP): Return appropriate
error in case of stack underflow.
(Ins_IP): Ditto and remove redundant check.
2025-05-20 17:27:43 -04:00
Werner Lemberg
b87f48a73e * src/aflatin.c (af_latin_hint_edges): Refine fix for serif handling.
Commit 11432424 (from 2025-May-12) was too aggressive; it is now restricted
to edges that are not too far away.
2025-05-19 15:48:24 +02:00
Werner Lemberg
46edeb321c * src/afadjust.c (adjustment_database): Make it complete up to U+FFFF.
Also fix two entries.

This is for Unicode 17.0.
2025-05-19 05:14:25 +00:00
Werner Lemberg
d3fbbed91f [autofit] Fix vertical adjustment of (mainly) double diacritics.
The series of commits that introduced this adjustment support had some
flaws.

- If there were two diacritics on top of a base glyph, and the upper
  diacritic was a tilde, the vertical centering correction was incorrectly
  applied to the lower, non-tilde glyph instead of the tilde.

- The maximum value allowed to shift a glyph was too strict (and also not
  handling rounding issues), causing some diacritics and combinations of
  diacritics to be not shifted at all.

* src/autofit/aflatin.c
  (af_glyph_hints_apply_vertical_separation_adjustments): Correctly handle
  vertical centering correction.
2025-05-19 05:14:25 +00:00
Werner Lemberg
4c9f14f422 [autofit] Code hygiene.
* src/autofit/aflatin.c
  (af_glyph_hints_apply_vertical_separation_adjustments): Use more flag
  variables and improve trace messages.
2025-05-19 05:14:25 +00:00
Werner Lemberg
11432424cf * src/autofit/aflatin.c (af_latin_hint_edges): Fix handling of serifs.
This has been discovered while inspecting the auto-hinting results of
character 'Ễ' in font `arial.ttf` (version 7.00) at 13ppem.
2025-05-19 05:14:25 +00:00
Werner Lemberg
87f0b694af [autofit] Minor.
* src/autofit/aflatin.c (af_latin_hint_edges): Rename `has_serifs` to
  `has_non_stem_edges` and make it of type `FT_Bool`.
2025-05-19 05:14:25 +00:00
Werner Lemberg
fd66a29d10 [autofit] New algorithm for preventing hinting of tilde glyphs.
The old algorithm removed segments from edges to make the auto-hinter ignore
a tilde.  However, the implementation had two flaws.

- Edge array elements were moved around without reordering them afterwards.
- The linking between edges and segments wasn't correctly updated for moved
  edges, which could cause endless loops.

Correcting both problems are non-trivial; additionally, a fix would make
the auto-hinter slower.

For these reasons, a new, simpler approach is taken: A new flag allows
points to be tagged as being ignored, and if such a point is enountered, it
doesn't get added to a segment.

Fixes issue #1333.

* src/autofit/afhints.h (AF_FLAG_IGNORE): New macro.

* src/autofit/aflatin.c (af_latin_hints_compute_segments, af_touch_contour):
  Use it.
  (af_remove_segments_containing_point, af_remove_top_points_from_edges,
  af_remove_bottom_points_from_edges): Removed.
  (af_latin_stretch_top_tilde): Call `af_touch_top_contours` and
  `af_touch_bottom_contours` unconditionally.
  (af_latin_hints_apply): Updated.
2025-05-19 05:14:25 +00:00
Alexei Podtelezhnikov
89e3e98e47 * src/truetype/ttinterp.c (TT_DotFix14): Silence UBSAN. 2025-05-16 13:19:26 -04:00
Alexei Podtelezhnikov
8082aba5f2 * src/truetype/ttinterp.c (Ins_DELTAP): Flip if-else. 2025-05-15 22:27:52 -04:00
Alexei Podtelezhnikov
7172bd11ba [truetype] Optimize DELTAP and DELTAC.
* src/truetype/ttinterp.c (Ins_DELTAP,DELTAC): Perform PPEM range-
checking and manipulations outside the loop.
2025-05-14 23:17:23 -04:00
Alexei Podtelezhnikov
8a152c824a * src/truetype/ttinterp.c (Compute_Funcs): Replace shifts. 2025-05-14 14:56:21 -04:00
Alexei Podtelezhnikov
828916527c [truetype] Tighten code overflow checks.
Fixes #1336.

* src/truetype/ttinterp.c (Ins_NPUSHB,NPUSHW,PUSHB, PUSHW):
Tighten conditions.
2025-05-14 12:45:53 -04:00
Chris Liddell
0fcd10ee7e [type1] Fix memory leak on CharString error.
In the event that a CharString retrieved via the incremental interface
caused an error, the memory for that CharString was not being freed.

* src/type1/t1gload.c (T1_Parse_Glyph_And_Get_Char_String): Free charstring.
2025-05-14 11:09:17 +02:00
Werner Lemberg
15b7e8c3a9 Whitespace, formatting, minor spelling fix. 2025-05-14 10:20:04 +02:00
Werner Lemberg
8d18acba8c Adminstrativa. 2025-05-14 09:16:57 +02:00
Alexei Podtelezhnikov
408957dca0 [truetype] Finalize bytecode changes.
* src/truetype/ttinterp.c: Formatting, minor.
(TT_Goto_CodeRange): Initialize 'iniRange' here...
(TT_RunIns): ... instead of here.
* docs/CHANGES: Updated.
2025-05-13 17:07:31 -04:00
Alexei Podtelezhnikov
2f2dfad597 [truetype] Streamline backward compatibility.
The 'backward_compatibility' is only set in v40: no need to check
the interpreter version again and again. In addition, we now track
IUP in its lower bits.  After both IUPx and IUPy have been called,
'backward_compatibility' becomes equal to 0x7.

* src/truetype/ttinterp.h (TT_ExecContextRec): Wrap 'iup?_called'
into 'backward_compatibility'.
* src/truetype/ttinterp.c (Ins_INSTCTRL): Sync 'backward_compatibility'.
(Direct_Move, Direct_Move_X/Y, Move_Zp2_Point,
Ins_FLIPPT,FLIPRGON,FLIPRGOFF,SHPIX,DELTAP): Updated.
(TT_RunIns): Do not mess with IUP flags.
* src/truetype/ttgload.c (tt_loader_init): Set 'backward_compatibility'.
(TT_Hint_Glyph): Updated.
2025-05-13 09:15:06 -04:00
Alexei Podtelezhnikov
4792cff5c3 [truetype] Optimize bytecode movements.
Replacing FT_MulDiv with FT_MulFix gives large performance gains.

* src/truetype/ttinterp.h (TT_ExecContextRec): Store "projected"
freedom vector.
* src/truetype/ttinterp.c (Compute_Funcs): Compute "projected"
freedom vector.
(Direct_Move, Direct_Move_Orig, Compute_Point_displacement): Use it.
2025-05-10 14:10:26 -04:00
Alexei Podtelezhnikov
55e60b9f53 [truetype] Simplify stack access.
* src/truetype/ttinterp.c (Ins_MINDEX,CINDEX): Use local stack pointers.
(Ins_DELTAP,DELTAC): Ditto; limit the number of pairs before looping.
2025-05-10 14:10:26 -04:00
Alexei Podtelezhnikov
c71b9dad3f [truetype] Tweak bytecode looping.
* src/truetype/ttinterp.c (Ins_FLIPPT,SHP,SHPIX,IP,ALIGNRP): Handle
counters and stack locally.
(TT_RunIns): Updated.
2025-05-10 14:10:26 -04:00
Alexei Podtelezhnikov
8c04ecd890 [truetype] Tweak bytecode initiation.
* src/truetype/ttinterp.c (TT_Load_Context): Init stack tops.
* src/truetype/ttinterp.h (TT_ExecContextRec): Document it and clean.
* src/truetype/ttobjs.c (tt_size_run_prep,fpgm): Updated.
* src/truetype/ttgload.c (tt_loader_init): Minor.
2025-05-10 14:10:26 -04:00
Alexei Podtelezhnikov
c3a5513508 [truetype] Modify stack handling.
* src/truetype/ttinterp.c (Ins_GETVARIATION): Increment stack.
(Ins_NPUSHB,NPUSHW,PUSHB,PUSHW): Handle the code stack here, ...
(TT_RunIns): ... instead of here. Consolidate error handling.
(SkipCode): Remove preemptive check.
2025-05-10 14:10:26 -04:00
Alexei Podtelezhnikov
b0febf5d91 [truetype] Modify bytecode steps.
* src/truetype/ttinterp.h (TT_Exec_ContextRec): Remove 'step_ins'.
* src/truetype/ttinterp.c (TT_RunIns, Ins_Goto_CodeRange,
Ins_JMPR,ENDF,PUSHW,NPUSHW): Set and use zero increment 'length' instead.
(Ins_CALL,LOOPCALL,UNKNOWN): Updated.
2025-05-10 14:10:25 -04:00