[font/fvar/avar] Increase axis coordinate normalization precision

Spec requires 16.16; we were doing 2.14. Fix up.
Now we do math in float, but do the 16.16 and final 2.14 rounding
as required by the spec.
This commit is contained in:
Behdad Esfahbod committed 2025-03-02 17:00:17 -07:00
1 parent bfcbb439cb
commit d7b15b0e21
5 files changed
+47 -47

No files matched your search

+18 -25
View File
@@ -143,10 +143,10 @@ struct AxisValueMap
struct SegmentMaps : Array16Of<AxisValueMap>
{
int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const
float map_float (float value, unsigned int from_offset = 0, unsigned int to_offset = 1) const
{
#define fromCoord coords[from_offset].to_int ()
#define toCoord coords[to_offset].to_int ()
#define fromCoord coords[from_offset].to_float ()
#define toCoord coords[to_offset].to_float ()
/* The following special-cases are not part of OpenType, which requires
* that at least -1, 0, and +1 must be mapped. But we include these as
@@ -199,31 +199,24 @@ struct SegmentMaps : Array16Of<AxisValueMap>
if (i == count)
return value - arrayZ[count - 1].fromCoord + arrayZ[count - 1].toCoord;
int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord;
return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) *
(value - arrayZ[i-1].fromCoord)) / denom);
auto &before = arrayZ[i-1];
auto &after = arrayZ[i];
float denom = after.fromCoord - before.fromCoord; // Can't be zero by now.
// Lerp
return before.toCoord + ((after.toCoord - before.toCoord) * (value - before.fromCoord)) / denom;
#undef toCoord
#undef fromCoord
}
int unmap (int value) const { return map (value, 1, 0); }
float unmap_float (float value) const { return map_float (value, 1, 0); }
// TODO Kill this.
Triple unmap_axis_range (const Triple& axis_range) const
{
F2DOT14 val, unmapped_val;
val.set_float (axis_range.minimum);
unmapped_val.set_int (unmap (val.to_int ()));
float unmapped_min = unmapped_val.to_float ();
val.set_float (axis_range.middle);
unmapped_val.set_int (unmap (val.to_int ()));
float unmapped_middle = unmapped_val.to_float ();
val.set_float (axis_range.maximum);
unmapped_val.set_int (unmap (val.to_int ()));
float unmapped_max = unmapped_val.to_float ();
float unmapped_min = unmap_float (axis_range.minimum);
float unmapped_middle = unmap_float (axis_range.middle);
float unmapped_max = unmap_float (axis_range.maximum);
return Triple{(double) unmapped_min, (double) unmapped_middle, (double) unmapped_max};
}
@@ -337,14 +330,14 @@ struct avar
return_trace (true);
}
void map_coords (int *coords, unsigned int coords_length) const
void map_coords_16_16 (int *coords, unsigned int coords_length) const
{
unsigned int count = hb_min (coords_length, axisCount);
const SegmentMaps *map = &firstAxisSegmentMaps;
for (unsigned int i = 0; i < count; i++)
{
coords[i] = map->map (coords[i]);
coords[i] = roundf (map->map_float (coords[i] / 65536.f) * 65536.f);
map = &StructAfter<SegmentMaps> (*map);
}
@@ -369,8 +362,8 @@ struct avar
int v = coords[i];
uint32_t varidx = varidx_map.map (i);
float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache);
v += roundf (delta);
v = hb_clamp (v, -(1<<14), +(1<<14));
v += roundf (delta * 4); // 2.14 -> 16.16
v = hb_clamp (v, -(1<<16), +(1<<16));
out.push (v);
}
for (unsigned i = 0; i < coords_length; i++)
@@ -386,7 +379,7 @@ struct avar
const SegmentMaps *map = &firstAxisSegmentMaps;
for (unsigned int i = 0; i < axisCount; i++)
{
if (map->map (0) != 0)
if (map->map_float (0.) != 0)
return true;
map = &StructAfter<SegmentMaps> (*map);
}
+4 -5
View File
@@ -179,7 +179,7 @@ struct AxisRecord
hb_tag_t get_axis_tag () const { return axisTag; }
int normalize_axis_value (float v) const
float normalize_axis_value (float v) const
{
float min_value, default_value, max_value;
get_coordinates (min_value, default_value, max_value);
@@ -189,10 +189,9 @@ struct AxisRecord
if (v == default_value)
return 0;
else if (v < default_value)
v = (v - default_value) / (default_value - min_value);
return (v - default_value) / (default_value - min_value);
else
v = (v - default_value) / (max_value - default_value);
return roundf (v * 16384.f);
return (v - default_value) / (max_value - default_value);
}
hb_ot_name_id_t get_name_id () const { return axisNameID; }
@@ -328,7 +327,7 @@ struct fvar
return axes.lfind (tag, &i) && ((void) axes[i].get_axis_info (i, info), true);
}
int normalize_axis_value (unsigned int axis_index, float v) const
float normalize_axis_value (unsigned int axis_index, float v) const
{ return get_axes ()[axis_index].normalize_axis_value (v); }
unsigned int get_instance_count () const { return instanceCount; }
+12 -4
View File
@@ -286,10 +286,14 @@ hb_ot_var_normalize_variations (hb_face_t *face,
hb_ot_var_axis_info_t info;
if (hb_ot_var_find_axis_info (face, variations[i].tag, &info) &&
info.axis_index < coords_length)
coords[info.axis_index] = fvar.normalize_axis_value (info.axis_index, variations[i].value);
coords[info.axis_index] = roundf (fvar.normalize_axis_value (info.axis_index, variations[i].value) * 65536.0f);
}
face->table.avar->map_coords (coords, coords_length);
face->table.avar->map_coords_16_16 (coords, coords_length);
// Round to 2.14
for (unsigned i = 0; i < coords_length; i++)
coords[i] = (coords[i] + 2) >> 2;
}
/**
@@ -323,9 +327,13 @@ hb_ot_var_normalize_coords (hb_face_t *face,
{
const OT::fvar &fvar = *face->table.fvar;
for (unsigned int i = 0; i < coords_length; i++)
normalized_coords[i] = fvar.normalize_axis_value (i, design_coords[i]);
normalized_coords[i] = roundf (fvar.normalize_axis_value (i, design_coords[i]) * 65536.0f);
face->table.avar->map_coords (normalized_coords, coords_length);
face->table.avar->map_coords_16_16 (normalized_coords, coords_length);
// Round to 2.14
for (unsigned i = 0; i < coords_length; i++)
normalized_coords[i] = (normalized_coords[i] + 2) >> 2;
}
+12 -12
View File
@@ -678,7 +678,7 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes,
hb_subset_plan_t *plan)
{
OT::cmap::accelerator_t cmap (plan->source);
unsigned size_threshold = plan->source->get_num_glyphs ();
unsigned size_threshold = plan->source->get_num_glyphs ();
if (glyphs->is_empty () && unicodes->get_population () < size_threshold)
{
@@ -806,7 +806,7 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes,
// so record those first.
plan->os2_info.min_cmap_codepoint = plan->unicodes.get_min();
plan->os2_info.max_cmap_codepoint = plan->unicodes.get_max();
hb_set_t variation_selectors_to_retain;
cmap.collect_variation_selectors(&variation_selectors_to_retain);
+ variation_selectors_to_retain.iter()
@@ -1117,24 +1117,24 @@ _normalize_axes_location (hb_face_t *face, hb_subset_plan_t *plan)
{
plan->axes_triple_distances.set (axis_tag, axis.get_triple_distances ());
int normalized_min = axis.normalize_axis_value (axis_range->minimum);
int normalized_default = axis.normalize_axis_value (axis_range->middle);
int normalized_max = axis.normalize_axis_value (axis_range->maximum);
float normalized_min = axis.normalize_axis_value (axis_range->minimum);
float normalized_default = axis.normalize_axis_value (axis_range->middle);
float normalized_max = axis.normalize_axis_value (axis_range->maximum);
if (has_avar && old_axis_idx < avar_axis_count)
{
normalized_min = seg_maps->map (normalized_min);
normalized_default = seg_maps->map (normalized_default);
normalized_max = seg_maps->map (normalized_max);
normalized_min = seg_maps->map_float (normalized_min);
normalized_default = seg_maps->map_float (normalized_default);
normalized_max = seg_maps->map_float (normalized_max);
}
plan->axes_location.set (axis_tag, Triple (static_cast<double> (normalized_min / 16384.0),
static_cast<double> (normalized_default / 16384.0),
static_cast<double> (normalized_max / 16384.0)));
plan->axes_location.set (axis_tag, Triple ((double) normalized_min,
(double) normalized_default,
(double) normalized_max));
if (normalized_default != 0)
plan->pinned_at_default = false;
plan->normalized_coords[old_axis_idx] = normalized_default;
plan->normalized_coords[old_axis_idx] = roundf (normalized_default * 16384.f);
}
old_axis_idx++;
+1 -1
View File
@@ -709,7 +709,7 @@ test_hb_draw_font_kit_variations_tests (void)
"C436,229 405,295 271,349L247,359C160,393 119,439 119,506"
"C119,592 178,637 262,637C311,637 346,626 390,585L348,629L373,535"
"C380,510 394,503 408,503C424,503 434,510 437,526C418,614 348,665 259,665"
"C161,665 78,606 78,500C78,414 128,361 224,321L261,305C367,259 395,217 395,152"
"C161,665 78,606 78,500C78,414 128,361 224,320L261,305C367,259 395,217 395,152"
"C395,65 334,15 246,15ZM267,331L267,759L240,759L240,331L267,331ZM240,-115"
"L267,-115L267,331L240,331L240,-115Z";
g_assert_cmpmem (str, draw_data.consumed, expected, sizeof (expected) - 1);