From a5b083b758658ef5e9f52272cdd4ba91c2d341ed Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 22 Feb 2025 16:32:20 -0700 Subject: [PATCH] [paint] Use transforms from hb-geometry --- src/hb-geometry.hh | 22 +++++++++++++--------- src/hb-paint.hh | 25 ++++++++++++++----------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/hb-geometry.hh b/src/hb-geometry.hh index e775b66b6..4a37fec13 100644 --- a/src/hb-geometry.hh +++ b/src/hb-geometry.hh @@ -175,6 +175,10 @@ struct hb_transform_t void transform (const hb_transform_t &o, bool before=false) { multiply (o, before); } + static hb_transform_t translation (Float x, Float y) + { + return {1, 0, 0, 1, x, y}; + } void translate (Float x, Float y, bool before=false) { if (before) @@ -192,6 +196,10 @@ struct hb_transform_t } } + static hb_transform_t scaling (Float scaleX, Float scaleY) + { + return {scaleX, 0, 0, scaleY, 0, 0}; + } void scale (Float scaleX, Float scaleY) { if (scaleX == 1 && scaleY == 1) @@ -211,7 +219,6 @@ struct hb_transform_t hb_sincos (radians, s, c); return {c, s, -s, c, 0, 0}; } - void rotate (Float radians, bool before=false) { if (radians == 0) @@ -230,7 +237,6 @@ struct hb_transform_t -s * center_x + (1 - c) * center_y }; } - void rotate_around_center (Float radians, Float center_x, Float center_y, bool before=false) { if (radians == 0) @@ -239,18 +245,16 @@ struct hb_transform_t transform (rotation_around_center (radians, center_x, center_y), before); } + static hb_transform_t skewing (Float skewX, Float skewY) + { + return {1, skewY ? tanf (skewY) : 0, skewX ? tanf (skewX) : 0, 1, 0, 0}; + } void skew (Float skewX, Float skewY) { if (skewX == 0 && skewY == 0) return; - // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L255 - auto other = hb_transform_t{1, - skewY ? tanf (skewY) : 0, - skewX ? tanf (skewX) : 0, - 1, - 0, 0}; - transform (other); + transform (skewing (skewX, skewY)); } Float xx = 1; diff --git a/src/hb-paint.hh b/src/hb-paint.hh index c98b2560e..bb9da492f 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -28,6 +28,7 @@ #include "hb.hh" #include "hb-face.hh" #include "hb-font.hh" +#include "hb-geometry.hh" #define HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS \ HB_PAINT_FUNC_IMPLEMENT (push_transform) \ @@ -180,6 +181,11 @@ struct hb_paint_funcs_t upem/xscale, 0, -slant * upem/xscale, upem/yscale, 0, 0); } + void push_transform (void *paint_data, hb_transform_t t) + { + push_transform (paint_data, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); + } + HB_NODISCARD bool push_translate (void *paint_data, float dx, float dy) @@ -188,7 +194,7 @@ struct hb_paint_funcs_t return false; push_transform (paint_data, - 1.f, 0.f, 0.f, 1.f, dx, dy); + hb_transform_t::translation (dx, dy)); return true; } @@ -200,7 +206,7 @@ struct hb_paint_funcs_t return false; push_transform (paint_data, - sx, 0.f, 0.f, sy, 0.f, 0.f); + hb_transform_t::scaling (sx, sy)); return true; } @@ -211,9 +217,8 @@ struct hb_paint_funcs_t if (!a) return false; - float c, s; - hb_sincos (a * HB_PI, s, c); - push_transform (paint_data, c, s, -s, c, 0.f, 0.f); + push_transform (paint_data, + hb_transform_t::rotation (a * HB_PI)); return true; } @@ -224,9 +229,8 @@ struct hb_paint_funcs_t if (!a) return false; - float c, s; - hb_sincos (a * HB_PI, s, c); - push_transform (paint_data, c, s, -s, c, cx * (1 - c) + cy * s, cy * (1 - c) - cx * s); + push_transform (paint_data, + hb_transform_t::rotation_around_center (a * HB_PI, cx, cy)); return true; } @@ -237,9 +241,8 @@ struct hb_paint_funcs_t if (!sx && !sy) return false; - float x = tanf (-sx * HB_PI); - float y = tanf (+sy * HB_PI); - push_transform (paint_data, 1.f, y, x, 1.f, 0.f, 0.f); + push_transform (paint_data, + hb_transform_t::skewing (-sx * HB_PI, sy * HB_PI)); return true; } };