From 75e6498d9a8b600ab7f00b3d279f1054dd72feec Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 14 Mar 2023 12:41:46 -0600 Subject: [PATCH] Don't use M_PI Fixes https://github.com/harfbuzz/harfbuzz/issues/4166 Happy Pi Day! --- src/OT/Color/COLR/COLR.hh | 4 ++-- src/OT/glyf/VarCompositeGlyph.hh | 6 +++--- src/hb-cairo-utils.cc | 24 +++++++++++------------- src/hb-ft-colr.hh | 4 ++-- src/hb-paint.hh | 8 ++++---- src/hb-style.cc | 4 ++-- src/hb.hh | 6 ++++++ 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 31be6585d..3c774e8e6 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -746,8 +746,8 @@ struct PaintSweepGradient c->funcs->sweep_gradient (c->data, &cl, centerX + c->instancer (varIdxBase, 0), centerY + c->instancer (varIdxBase, 1), - (startAngle.to_float (c->instancer (varIdxBase, 2)) + 1) * (float) M_PI, - (endAngle.to_float (c->instancer (varIdxBase, 3)) + 1) * (float) M_PI); + (startAngle.to_float (c->instancer (varIdxBase, 2)) + 1) * HB_PI, + (endAngle.to_float (c->instancer (varIdxBase, 3)) + 1) * HB_PI); } HBUINT8 format; /* format = 8(noVar) or 9 (Var) */ diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index f1d48be2b..f543eff2b 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -145,7 +145,7 @@ struct VarCompositeGlyphRecord float rotation) { // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L240 - rotation = rotation * float (M_PI); + rotation = rotation * HB_PI; float c = cosf (rotation); float s = sinf (rotation); float other[6] = {c, s, -s, c, 0.f, 0.f}; @@ -156,8 +156,8 @@ struct VarCompositeGlyphRecord float skewX, float skewY) { // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L255 - skewX = skewX * float (M_PI); - skewY = skewY * float (M_PI); + skewX = skewX * HB_PI; + skewY = skewY * HB_PI; float other[6] = {1.f, tanf (skewY), tanf (skewX), 1.f, 0.f, 0.f}; transform (matrix, trans, other); } diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index d841994d2..0f94d8169 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -41,8 +41,6 @@ #define PREALLOCATED_COLOR_STOPS 16 -#define _2_M_PIf (2.f * float (M_PI)) - typedef struct { float r, g, b, a; } hb_cairo_color_t; @@ -525,7 +523,7 @@ _hb_cairo_add_patch (cairo_pattern_t *pattern, hb_cairo_point_t *center, hb_cair cairo_mesh_pattern_end_patch (pattern); } -#define MAX_ANGLE ((float) M_PI / 8.f) +#define MAX_ANGLE (HB_PI / 8.f) static void _hb_cairo_add_sweep_gradient_patches1 (float cx, float cy, float radius, @@ -608,7 +606,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, start_angle, &c, pattern); } - if (end_angle < _2_M_PIf) + if (end_angle < HB_2_PI) { c.r = hb_color_get_red (stops[n_stops - 1].color) / 255.; c.g = hb_color_get_green (stops[n_stops - 1].color) / 255.; @@ -616,7 +614,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, c.a = hb_color_get_alpha (stops[n_stops - 1].color) / 255.; _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, end_angle, &c, - _2_M_PIf, &c, + HB_2_PI, &c, pattern); } } @@ -680,7 +678,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, color0 = colors[n_stops-1]; _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, 0., &color0, - _2_M_PIf, &color0, + HB_2_PI, &color0, pattern); goto done; } @@ -692,7 +690,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, for (pos++; pos < n_stops; pos++) { - if (angles[pos] <= _2_M_PIf) + if (angles[pos] <= HB_2_PI) { _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, angles[pos - 1], &colors[pos-1], @@ -701,11 +699,11 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, } else { - float k = (_2_M_PIf - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + float k = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); _hb_cairo_interpolate_colors (&colors[pos - 1], &colors[pos], k, &color1); _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, angles[pos - 1], &colors[pos - 1], - _2_M_PIf, &color1, + HB_2_PI, &color1, pattern); break; } @@ -717,7 +715,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, color0 = colors[n_stops - 1]; _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, angles[n_stops - 1], &color0, - _2_M_PIf, &color0, + HB_2_PI, &color0, pattern); goto done; } @@ -801,14 +799,14 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, a1, c1, pattern); } - else if (a1 >= _2_M_PIf) + else if (a1 >= HB_2_PI) { hb_cairo_color_t color; - float f = (_2_M_PIf - a0)/(a1 - a0); + float f = (HB_2_PI - a0)/(a1 - a0); _hb_cairo_interpolate_colors (c0, c1, f, &color); _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, a0, c0, - _2_M_PIf, &color, + HB_2_PI, &color, pattern); goto done; } diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index b3457933c..fa5712f9b 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -301,8 +301,8 @@ _hb_ft_paint (hb_ft_paint_context_t *c, c->funcs->sweep_gradient (c->data, &cl, paint.u.sweep_gradient.center.x / 65536.f, paint.u.sweep_gradient.center.y / 65536.f, - (paint.u.sweep_gradient.start_angle / 65536.f + 1) * (float) M_PI, - (paint.u.sweep_gradient.end_angle / 65536.f + 1) * (float) M_PI); + (paint.u.sweep_gradient.start_angle / 65536.f + 1) * HB_PI, + (paint.u.sweep_gradient.end_angle / 65536.f + 1) * HB_PI); } break; case FT_COLR_PAINTFORMAT_GLYPH: diff --git a/src/hb-paint.hh b/src/hb-paint.hh index f7b71aa19..d291a4b97 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -203,8 +203,8 @@ struct hb_paint_funcs_t if (!a) return false; - float cc = cosf (a * (float) M_PI); - float ss = sinf (a * (float) M_PI); + float cc = cosf (a * HB_PI); + float ss = sinf (a * HB_PI); push_transform (paint_data, cc, ss, -ss, cc, 0.f, 0.f); return true; } @@ -216,8 +216,8 @@ struct hb_paint_funcs_t if (!sx && !sy) return false; - float x = tanf (-sx * (float) M_PI); - float y = tanf (+sy * (float) M_PI); + 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); return true; } diff --git a/src/hb-style.cc b/src/hb-style.cc index c7d7d713c..bd5cb5c6b 100644 --- a/src/hb-style.cc +++ b/src/hb-style.cc @@ -46,13 +46,13 @@ static inline float _hb_angle_to_ratio (float a) { - return tanf (a * float (-M_PI / 180.)); + return tanf (a * -HB_PI / 180.f); } static inline float _hb_ratio_to_angle (float r) { - return atanf (r) * float (-180. / M_PI); + return atanf (r) * -180.f / HB_PI; } /** diff --git a/src/hb.hh b/src/hb.hh index d09849e56..30b3de499 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -509,6 +509,12 @@ static_assert ((sizeof (hb_mask_t) == 4), ""); static_assert ((sizeof (hb_var_int_t) == 4), ""); +/* Pie time. */ +// https://github.com/harfbuzz/harfbuzz/issues/4166 +#define HB_PI 3.14159265358979f +#define HB_2_PI (2.f * HB_PI) + + /* Headers we include for everyone. Keep topologically sorted by dependency. * They express dependency amongst themselves, but no other file should include * them directly.*/