[font] Optimize font scaling further

This commit is contained in:
Behdad Esfahbod 2022-06-27 11:28:44 -06:00
parent e72506d085
commit d3308f4713
3 changed files with 24 additions and 24 deletions

View File

@ -1649,8 +1649,8 @@ DEFINE_NULL_INSTANCE (hb_font_t) =
1000, /* y_scale */ 1000, /* y_scale */
0., /* slant */ 0., /* slant */
0., /* slant_xy; */ 0., /* slant_xy; */
1000, /* upem */ 1.f, /* x_multf */
0.001f, /* upem_inv */ 1.f, /* y_multf */
1<<16, /* x_mult */ 1<<16, /* x_mult */
1<<16, /* y_mult */ 1<<16, /* y_mult */
@ -1683,9 +1683,8 @@ _hb_font_create (hb_face_t *face)
font->face = hb_face_reference (face); font->face = hb_face_reference (face);
font->klass = hb_font_funcs_get_empty (); font->klass = hb_font_funcs_get_empty ();
font->data.init0 (font); font->data.init0 (font);
font->x_scale = font->y_scale = hb_face_get_upem (face); font->x_scale = font->y_scale = face->get_upem ();
font->upem = face->get_upem (); font->x_multf = font->y_multf = 1.f;
font->upem_inv = 1.f / font->upem;
font->x_mult = font->y_mult = 1 << 16; font->x_mult = font->y_mult = 1 << 16;
return font; return font;

View File

@ -114,8 +114,8 @@ struct hb_font_t
int32_t y_scale; int32_t y_scale;
float slant; float slant;
float slant_xy; float slant_xy;
unsigned upem; float x_multf;
float upem_inv; float y_multf;
int64_t x_mult; int64_t x_mult;
int64_t y_mult; int64_t y_mult;
@ -141,12 +141,12 @@ struct hb_font_t
{ return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; } { return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; }
hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); } hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); }
hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); } hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); }
hb_position_t em_scalef_x (float v) { return em_scalef (v, x_scale); } hb_position_t em_scalef_x (float v) { return em_multf (v, x_multf); }
hb_position_t em_scalef_y (float v) { return em_scalef (v, y_scale); } hb_position_t em_scalef_y (float v) { return em_multf (v, y_multf); }
float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); } float em_fscale_x (int16_t v) { return em_fmult (v, x_multf); }
float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); } float em_fscale_y (int16_t v) { return em_fmult (v, y_multf); }
float em_fscalef_x (float v) { return em_fscalef (v, x_scale); } float em_fscalef_x (float v) { return em_fmultf (v, x_multf); }
float em_fscalef_y (float v) { return em_fscalef (v, y_scale); } float em_fscalef_y (float v) { return em_fmultf (v, y_multf); }
hb_position_t em_scale_dir (int16_t v, hb_direction_t direction) hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
{ return em_mult (v, dir_mult (direction)); } { return em_mult (v, dir_mult (direction)); }
@ -631,23 +631,24 @@ struct hb_font_t
void mults_changed () void mults_changed ()
{ {
upem = face->get_upem (); float upem = face->get_upem ();
upem_inv = 1.f / upem; x_multf = x_scale / upem;
y_multf = y_scale / upem;
bool x_neg = x_scale < 0; bool x_neg = x_scale < 0;
x_mult = (x_neg ? -((int64_t) -x_scale << 16) : ((int64_t) x_scale << 16)) * upem_inv; x_mult = (x_neg ? -((int64_t) -x_scale << 16) : ((int64_t) x_scale << 16)) / upem;
bool y_neg = y_scale < 0; bool y_neg = y_scale < 0;
y_mult = (y_neg ? -((int64_t) -y_scale << 16) : ((int64_t) y_scale << 16)) * upem_inv; y_mult = (y_neg ? -((int64_t) -y_scale << 16) : ((int64_t) y_scale << 16)) / upem;
slant_xy = y_scale ? slant * x_scale / y_scale : 0.f; slant_xy = y_scale ? slant * x_scale / y_scale : 0.f;
} }
hb_position_t em_mult (int16_t v, int64_t mult) hb_position_t em_mult (int16_t v, int64_t mult)
{ return (hb_position_t) ((v * mult + 32768) >> 16); } { return (hb_position_t) ((v * mult + 32768) >> 16); }
hb_position_t em_scalef (float v, int scale) hb_position_t em_multf (float v, float mult)
{ return (hb_position_t) roundf (em_fscalef (v, scale)); } { return (hb_position_t) roundf (em_fmultf (v, mult)); }
float em_fscalef (float v, int scale) float em_fmultf (float v, float mult)
{ return v * scale * upem_inv; } { return v * mult; }
float em_fscale (int16_t v, int scale) float em_fmult (int16_t v, float mult)
{ return (float) v * scale * upem_inv; } { return (float) v * mult; }
}; };
DECLARE_NULL_INSTANCE (hb_font_t); DECLARE_NULL_INSTANCE (hb_font_t);

View File

@ -479,7 +479,7 @@ test_hb_draw_font_kit_glyphs_tests (void)
char expected[] = "M581,274L443,274Q409,274 384,259Q359,243 348,219Q336,194 340,166" char expected[] = "M581,274L443,274Q409,274 384,259Q359,243 348,219Q336,194 340,166"
"Q343,138 365,111L468,-13Q470,-10 473,-6Q475,-3 477,0L253,0Q225,0 203,8" "Q343,138 365,111L468,-13Q470,-10 473,-6Q475,-3 477,0L253,0Q225,0 203,8"
"Q180,15 168,32Q155,48 155,73L155,269L50,269L50,73Q50,24 69,-10" "Q180,15 168,32Q155,48 155,73L155,269L50,269L50,73Q50,24 69,-10"
"Q88,-44 118,-65Q147,-85 181,-95Q214,-104 243,-104L473,-104" "Q88,-44 118,-64Q147,-85 181,-94Q214,-104 243,-104L473,-104"
"Q501,-104 525,-91Q549,-78 564,-56Q578,-34 578,-8Q578,18 557,43" "Q501,-104 525,-91Q549,-78 564,-56Q578,-34 578,-8Q578,18 557,43"
"L442,182Q439,179 437,176Q435,173 432,170L581,170L581,274ZM184,-194" "L442,182Q439,179 437,176Q435,173 432,170L581,170L581,274ZM184,-194"
"Q184,-216 199,-231Q214,-246 236,-246Q258,-246 273,-231Q288,-216 288,-194" "Q184,-216 199,-231Q214,-246 236,-246Q258,-246 273,-231Q288,-216 288,-194"