Merge pull request #5189 from harfbuzz/font-changed

[font] Simplify changed mechanism
This commit is contained in:
Behdad Esfahbod 2025-03-22 14:23:07 -06:00 committed by GitHub
commit c3bc5e4c68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 49 deletions

View file

@ -7,7 +7,7 @@ use std::collections::HashMap;
use std::mem::transmute;
use std::os::raw::c_void;
use std::ptr::null_mut;
use std::sync::atomic::{AtomicPtr, Ordering};
use std::sync::atomic::{AtomicPtr, AtomicU32, Ordering};
use std::sync::{Mutex, OnceLock};
use read_fonts::tables::cpal::ColorRecord;
@ -66,7 +66,7 @@ struct FontationsData<'a> {
// Mutex for the below
mutex: Mutex<()>,
serial: u32,
serial: AtomicU32,
x_size: Size,
y_size: Size,
location: Location,
@ -102,7 +102,7 @@ impl FontationsData<'_> {
glyph_names,
glyph_from_names: OnceLock::new(),
mutex: Mutex::new(()),
serial: u32::MAX,
serial: AtomicU32::new(u32::MAX),
x_size: Size::new(0.0),
y_size: Size::new(0.0),
location: Location::default(),
@ -117,7 +117,8 @@ impl FontationsData<'_> {
unsafe fn _check_for_updates(&mut self) {
let font_serial = hb_font_get_serial(self.font);
if self.serial == font_serial {
let serial = self.serial.load(Ordering::Relaxed);
if serial == font_serial {
return;
}
@ -158,7 +159,7 @@ impl FontationsData<'_> {
let location = transmute::<&Location, &Location>(&self.location);
self.y_glyph_metrics = Some(self.font_ref.glyph_metrics(self.y_size, location));
self.serial = font_serial;
self.serial.store(font_serial, Ordering::Release);
}
fn check_for_updates(&mut self) {
unsafe { self._check_for_updates() }

View file

@ -1893,7 +1893,7 @@ _hb_font_adopt_var_coords (hb_font_t *font,
font->design_coords = design_coords;
font->num_coords = coords_length;
font->mults_changed (); // Easiest to call this to drop cached data
font->changed ();
}
/**
@ -1948,7 +1948,8 @@ hb_font_create_sub_font (hb_font_t *parent)
}
}
font->mults_changed ();
font->changed ();
font->serial_coords = font->serial;
return font;
}
@ -2036,7 +2037,7 @@ hb_font_set_user_data (hb_font_t *font,
hb_bool_t replace)
{
if (!hb_object_is_immutable (font))
font->serial++;
font->changed ();
return hb_object_set_user_data (font, key, data, destroy, replace);
}
@ -2130,9 +2131,7 @@ hb_font_changed (hb_font_t *font)
if (hb_object_is_immutable (font))
return;
font->serial++;
font->mults_changed ();
font->changed ();
}
/**
@ -2154,8 +2153,6 @@ hb_font_set_parent (hb_font_t *font,
if (parent == font->parent)
return;
font->serial++;
if (!parent)
parent = hb_font_get_empty ();
@ -2164,6 +2161,8 @@ hb_font_set_parent (hb_font_t *font,
font->parent = hb_font_reference (parent);
hb_font_destroy (old);
font->changed ();
}
/**
@ -2201,8 +2200,6 @@ hb_font_set_face (hb_font_t *font,
if (face == font->face)
return;
font->serial++;
if (unlikely (!face))
face = hb_face_get_empty ();
@ -2210,9 +2207,11 @@ hb_font_set_face (hb_font_t *font,
hb_face_make_immutable (face);
font->face = hb_face_reference (face);
font->mults_changed ();
font->changed ();
hb_face_destroy (old);
font->changed ();
}
/**
@ -2257,8 +2256,6 @@ hb_font_set_funcs (hb_font_t *font,
return;
}
font->serial++;
if (font->destroy)
font->destroy (font->user_data);
@ -2270,6 +2267,8 @@ hb_font_set_funcs (hb_font_t *font,
font->klass = klass;
font->user_data = font_data;
font->destroy = destroy;
font->changed ();
}
/**
@ -2296,13 +2295,13 @@ hb_font_set_funcs_data (hb_font_t *font,
return;
}
font->serial++;
if (font->destroy)
font->destroy (font->user_data);
font->user_data = font_data;
font->destroy = destroy;
font->changed ();
}
static struct supported_font_funcs_t {
@ -2488,11 +2487,10 @@ hb_font_set_scale (hb_font_t *font,
if (font->x_scale == x_scale && font->y_scale == y_scale)
return;
font->serial++;
font->x_scale = x_scale;
font->y_scale = y_scale;
font->mults_changed ();
font->changed ();
}
/**
@ -2539,10 +2537,10 @@ hb_font_set_ppem (hb_font_t *font,
if (font->x_ppem == x_ppem && font->y_ppem == y_ppem)
return;
font->serial++;
font->x_ppem = x_ppem;
font->y_ppem = y_ppem;
font->changed ();
}
/**
@ -2586,9 +2584,9 @@ hb_font_set_ptem (hb_font_t *font,
if (font->ptem == ptem)
return;
font->serial++;
font->ptem = ptem;
font->changed ();
}
/**
@ -2648,12 +2646,11 @@ hb_font_set_synthetic_bold (hb_font_t *font,
font->embolden_in_place == (bool) in_place)
return;
font->serial++;
font->x_embolden = x_embolden;
font->y_embolden = y_embolden;
font->embolden_in_place = in_place;
font->mults_changed ();
font->changed ();
}
/**
@ -2707,10 +2704,9 @@ hb_font_set_synthetic_slant (hb_font_t *font, float slant)
if (font->slant == slant)
return;
font->serial++;
font->slant = slant;
font->mults_changed ();
font->changed ();
}
/**
@ -2756,9 +2752,6 @@ hb_font_set_variations (hb_font_t *font,
if (hb_object_is_immutable (font))
return;
font->serial++;
font->serial_coords = font->serial;
if (!variations_length && font->instance_index == HB_FONT_NO_VAR_NAMED_INSTANCE)
{
hb_font_set_var_coords_normalized (font, nullptr, 0);
@ -2827,9 +2820,6 @@ hb_font_set_variation (hb_font_t *font,
if (hb_object_is_immutable (font))
return;
font->serial++;
font->serial_coords = font->serial;
// TODO Share some of this code with set_variations()
const OT::fvar &fvar = *font->face->table.fvar;
@ -2900,9 +2890,6 @@ hb_font_set_var_coords_design (hb_font_t *font,
if (hb_object_is_immutable (font))
return;
font->serial++;
font->serial_coords = font->serial;
int *normalized = coords_length ? (int *) hb_calloc (coords_length, sizeof (int)) : nullptr;
float *design_coords = coords_length ? (float *) hb_calloc (coords_length, sizeof (float)) : nullptr;
@ -2939,9 +2926,6 @@ hb_font_set_var_named_instance (hb_font_t *font,
if (font->instance_index == instance_index)
return;
font->serial++;
font->serial_coords = font->serial;
font->instance_index = instance_index;
hb_font_set_variations (font, nullptr, 0);
}
@ -2987,9 +2971,6 @@ hb_font_set_var_coords_normalized (hb_font_t *font,
if (hb_object_is_immutable (font))
return;
font->serial++;
font->serial_coords = font->serial;
int *copy = coords_length ? (int *) hb_calloc (coords_length, sizeof (coords[0])) : nullptr;
int *unmapped = coords_length ? (int *) hb_calloc (coords_length, sizeof (coords[0])) : nullptr;
float *design_coords = coords_length ? (float *) hb_calloc (coords_length, sizeof (design_coords[0])) : nullptr;

View file

@ -702,7 +702,7 @@ struct hb_font_t
return false;
}
void mults_changed ()
void changed ()
{
float upem = face->get_upem ();
@ -719,6 +719,8 @@ struct hb_font_t
slant_xy = y_scale ? slant * x_scale / y_scale : 0.f;
data.fini ();
serial++;
}
hb_position_t em_mult (int16_t v, int64_t mult)