summaryrefslogtreecommitdiffstats
path: root/wgpu/src/text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'wgpu/src/text.rs')
-rw-r--r--wgpu/src/text.rs143
1 files changed, 82 insertions, 61 deletions
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index 0d88865c..1b94edf6 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -18,8 +18,7 @@ pub struct Pipeline {
renderers: Vec<glyphon::TextRenderer>,
atlas: glyphon::TextAtlas,
prepare_layer: usize,
- measurement_cache: RefCell<Cache>,
- render_cache: Cache,
+ cache: RefCell<Cache>,
}
impl Pipeline {
@@ -47,15 +46,16 @@ impl Pipeline {
},
),
prepare_layer: 0,
- measurement_cache: RefCell::new(Cache::new()),
- render_cache: Cache::new(),
+ cache: RefCell::new(Cache::new()),
}
}
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
- self.font_system.get_mut().db_mut().load_font_source(
+ let _ = self.font_system.get_mut().db_mut().load_font_source(
glyphon::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
);
+
+ self.cache = RefCell::new(Cache::new());
}
pub fn prepare(
@@ -78,25 +78,25 @@ impl Pipeline {
let font_system = self.font_system.get_mut();
let renderer = &mut self.renderers[self.prepare_layer];
+ let cache = self.cache.get_mut();
let keys: Vec<_> = sections
.iter()
.map(|section| {
- let (key, _) = self.render_cache.allocate(
+ let (key, _) = cache.allocate(
font_system,
Key {
content: section.content,
- size: section.size * scale_factor,
+ size: section.size,
line_height: f32::from(
section
.line_height
.to_absolute(Pixels(section.size)),
- ) * scale_factor,
+ ),
font: section.font,
bounds: Size {
- width: (section.bounds.width * scale_factor).ceil(),
- height: (section.bounds.height * scale_factor)
- .ceil(),
+ width: section.bounds.width,
+ height: section.bounds.height,
},
shaping: section.shaping,
},
@@ -113,22 +113,14 @@ impl Pipeline {
.iter()
.zip(keys.iter())
.filter_map(|(section, key)| {
- let buffer =
- self.render_cache.get(key).expect("Get cached buffer");
-
- let (total_lines, max_width) = buffer
- .layout_runs()
- .enumerate()
- .fold((0, 0.0), |(_, max), (i, buffer)| {
- (i + 1, buffer.line_w.max(max))
- });
-
- let total_height =
- total_lines as f32 * buffer.metrics().line_height;
+ let entry = cache.get(key).expect("Get cached buffer");
let x = section.bounds.x * scale_factor;
let y = section.bounds.y * scale_factor;
+ let max_width = entry.bounds.width * scale_factor;
+ let total_height = entry.bounds.height * scale_factor;
+
let left = match section.horizontal_alignment {
alignment::Horizontal::Left => x,
alignment::Horizontal::Center => x - max_width / 2.0,
@@ -150,14 +142,11 @@ impl Pipeline {
let clip_bounds = bounds.intersection(&section_bounds)?;
- // TODO: Subpixel glyph positioning
- let left = left.round() as i32;
- let top = top.round() as i32;
-
Some(glyphon::TextArea {
- buffer,
+ buffer: &entry.buffer,
left,
top,
+ scale: scale_factor,
bounds: glyphon::TextBounds {
left: clip_bounds.x as i32,
top: clip_bounds.y as i32,
@@ -235,7 +224,7 @@ impl Pipeline {
pub fn end_frame(&mut self) {
self.atlas.trim();
- self.render_cache.trim();
+ self.cache.get_mut().trim();
self.prepare_layer = 0;
}
@@ -248,12 +237,12 @@ impl Pipeline {
font: Font,
bounds: Size,
shaping: Shaping,
- ) -> (f32, f32) {
- let mut measurement_cache = self.measurement_cache.borrow_mut();
+ ) -> Size {
+ let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size)));
- let (_, paragraph) = measurement_cache.allocate(
+ let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
@@ -265,14 +254,7 @@ impl Pipeline {
},
);
- let (total_lines, max_width) = paragraph
- .layout_runs()
- .enumerate()
- .fold((0, 0.0), |(_, max), (i, buffer)| {
- (i + 1, buffer.line_w.max(max))
- });
-
- (max_width, line_height * total_lines as f32)
+ entry.bounds
}
pub fn hit_test(
@@ -286,11 +268,11 @@ impl Pipeline {
point: Point,
_nearest_only: bool,
) -> Option<Hit> {
- let mut measurement_cache = self.measurement_cache.borrow_mut();
+ let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size)));
- let (_, paragraph) = measurement_cache.allocate(
+ let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
@@ -302,14 +284,20 @@ impl Pipeline {
},
);
- let cursor = paragraph.hit(point.x, point.y)?;
+ let cursor = entry.buffer.hit(point.x, point.y)?;
Some(Hit::CharOffset(cursor.index))
}
+}
- pub fn trim_measurement_cache(&mut self) {
- self.measurement_cache.borrow_mut().trim();
- }
+fn measure(buffer: &glyphon::Buffer) -> Size {
+ let (width, total_lines) = buffer
+ .layout_runs()
+ .fold((0.0, 0usize), |(width, total_lines), run| {
+ (run.line_w.max(width), total_lines + 1)
+ });
+
+ Size::new(width, total_lines as f32 * buffer.metrics().line_height)
}
fn to_family(family: font::Family) -> glyphon::Family<'static> {
@@ -359,11 +347,17 @@ fn to_shaping(shaping: Shaping) -> glyphon::Shaping {
}
struct Cache {
- entries: FxHashMap<KeyHash, glyphon::Buffer>,
+ entries: FxHashMap<KeyHash, Entry>,
+ measurements: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder,
}
+struct Entry {
+ buffer: glyphon::Buffer,
+ bounds: Size,
+}
+
#[cfg(not(target_arch = "wasm32"))]
type HashBuilder = twox_hash::RandomXxHashBuilder64;
@@ -374,12 +368,13 @@ impl Cache {
fn new() -> Self {
Self {
entries: FxHashMap::default(),
+ measurements: FxHashMap::default(),
recently_used: FxHashSet::default(),
hasher: HashBuilder::default(),
}
}
- fn get(&self, key: &KeyHash) -> Option<&glyphon::Buffer> {
+ fn get(&self, key: &KeyHash) -> Option<&Entry> {
self.entries.get(key)
}
@@ -387,20 +382,14 @@ impl Cache {
&mut self,
font_system: &mut glyphon::FontSystem,
key: Key<'_>,
- ) -> (KeyHash, &mut glyphon::Buffer) {
- let hash = {
- let mut hasher = self.hasher.build_hasher();
+ ) -> (KeyHash, &mut Entry) {
+ let hash = key.hash(self.hasher.build_hasher());
- key.content.hash(&mut hasher);
- key.size.to_bits().hash(&mut hasher);
- key.line_height.to_bits().hash(&mut hasher);
- key.font.hash(&mut hasher);
- key.bounds.width.to_bits().hash(&mut hasher);
- key.bounds.height.to_bits().hash(&mut hasher);
- key.shaping.hash(&mut hasher);
+ if let Some(hash) = self.measurements.get(&hash) {
+ let _ = self.recently_used.insert(*hash);
- hasher.finish()
- };
+ return (*hash, self.entries.get_mut(hash).unwrap());
+ }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
let metrics = glyphon::Metrics::new(key.size, key.line_height);
@@ -421,7 +410,23 @@ impl Cache {
to_shaping(key.shaping),
);
- let _ = entry.insert(buffer);
+ let bounds = measure(&buffer);
+ let _ = entry.insert(Entry { buffer, bounds });
+
+ for bounds in [
+ bounds,
+ Size {
+ width: key.bounds.width,
+ ..bounds
+ },
+ ] {
+ if key.bounds != bounds {
+ let _ = self.measurements.insert(
+ Key { bounds, ..key }.hash(self.hasher.build_hasher()),
+ hash,
+ );
+ }
+ }
}
let _ = self.recently_used.insert(hash);
@@ -432,6 +437,8 @@ impl Cache {
fn trim(&mut self) {
self.entries
.retain(|key, _| self.recently_used.contains(key));
+ self.measurements
+ .retain(|_, value| self.recently_used.contains(value));
self.recently_used.clear();
}
@@ -447,4 +454,18 @@ struct Key<'a> {
shaping: Shaping,
}
+impl Key<'_> {
+ fn hash<H: Hasher>(self, mut hasher: H) -> KeyHash {
+ self.content.hash(&mut hasher);
+ self.size.to_bits().hash(&mut hasher);
+ self.line_height.to_bits().hash(&mut hasher);
+ self.font.hash(&mut hasher);
+ self.bounds.width.to_bits().hash(&mut hasher);
+ self.bounds.height.to_bits().hash(&mut hasher);
+ self.shaping.hash(&mut hasher);
+
+ hasher.finish()
+ }
+}
+
type KeyHash = u64;