summaryrefslogtreecommitdiffstats
path: root/tiny_skia/src/text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tiny_skia/src/text.rs')
-rw-r--r--tiny_skia/src/text.rs152
1 files changed, 85 insertions, 67 deletions
diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs
index a34c7317..8f494650 100644
--- a/tiny_skia/src/text.rs
+++ b/tiny_skia/src/text.rs
@@ -14,8 +14,7 @@ use std::sync::Arc;
pub struct Pipeline {
font_system: RefCell<cosmic_text::FontSystem>,
glyph_cache: GlyphCache,
- measurement_cache: RefCell<Cache>,
- render_cache: Cache,
+ cache: RefCell<Cache>,
}
impl Pipeline {
@@ -23,14 +22,12 @@ impl Pipeline {
Pipeline {
font_system: RefCell::new(cosmic_text::FontSystem::new_with_fonts(
[cosmic_text::fontdb::Source::Binary(Arc::new(
- include_bytes!("../../wgpu/fonts/Iced-Icons.ttf")
- .as_slice(),
+ include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
))]
.into_iter(),
)),
glyph_cache: GlyphCache::new(),
- measurement_cache: RefCell::new(Cache::new()),
- render_cache: Cache::new(),
+ cache: RefCell::new(Cache::new()),
}
}
@@ -38,6 +35,8 @@ impl Pipeline {
self.font_system.get_mut().db_mut().load_font_source(
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
);
+
+ self.cache = RefCell::new(Cache::new());
}
pub fn draw(
@@ -55,20 +54,11 @@ impl Pipeline {
pixels: &mut tiny_skia::PixmapMut<'_>,
clip_mask: Option<&tiny_skia::Mask>,
) {
- let line_height =
- f32::from(line_height.to_absolute(Pixels(size))) * scale_factor;
-
- let bounds = bounds * scale_factor;
- let size = size * scale_factor;
+ let line_height = f32::from(line_height.to_absolute(Pixels(size)));
let font_system = self.font_system.get_mut();
let key = Key {
- bounds: {
- let size = bounds.size();
-
- // TODO: Reuse buffers from layouting
- Size::new(size.width.ceil(), size.height.ceil())
- },
+ bounds: bounds.size(),
content,
font,
size,
@@ -76,16 +66,12 @@ impl Pipeline {
shaping,
};
- let (_, buffer) = self.render_cache.allocate(font_system, key);
+ let (_, entry) = self.cache.get_mut().allocate(font_system, key);
- let (total_lines, max_width) = buffer
- .layout_runs()
- .enumerate()
- .fold((0, 0.0), |(_, max), (i, buffer)| {
- (i + 1, buffer.line_w.max(max))
- });
+ let max_width = entry.bounds.width * scale_factor;
+ let total_height = entry.bounds.height * scale_factor;
- let total_height = total_lines as f32 * line_height;
+ let bounds = bounds * scale_factor;
let x = match horizontal_alignment {
alignment::Horizontal::Left => bounds.x,
@@ -99,16 +85,14 @@ impl Pipeline {
alignment::Vertical::Bottom => bounds.y - total_height,
};
- // TODO: Subpixel glyph positioning
- let x = x.round() as i32;
- let y = y.round() as i32;
-
let mut swash = cosmic_text::SwashCache::new();
- for run in buffer.layout_runs() {
+ for run in entry.buffer.layout_runs() {
for glyph in run.glyphs {
+ let physical_glyph = glyph.physical((x, y), scale_factor);
+
if let Some((buffer, placement)) = self.glyph_cache.allocate(
- glyph.cache_key,
+ physical_glyph.cache_key,
color,
font_system,
&mut swash,
@@ -121,8 +105,9 @@ impl Pipeline {
.expect("Create glyph pixel map");
pixels.draw_pixmap(
- x + glyph.x_int + placement.left,
- y - glyph.y_int - placement.top + run.line_y as i32,
+ physical_glyph.x + placement.left,
+ physical_glyph.y - placement.top
+ + (run.line_y * scale_factor).round() as i32,
pixmap,
&tiny_skia::PixmapPaint::default(),
tiny_skia::Transform::identity(),
@@ -134,7 +119,7 @@ impl Pipeline {
}
pub fn trim_cache(&mut self) {
- self.render_cache.trim();
+ self.cache.get_mut().trim();
self.glyph_cache.trim();
}
@@ -146,12 +131,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,
@@ -163,14 +148,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(
@@ -184,11 +162,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,
@@ -200,14 +178,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: &cosmic_text::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) -> cosmic_text::Family<'static> {
@@ -366,12 +350,18 @@ impl GlyphCache {
}
struct Cache {
- entries: FxHashMap<KeyHash, cosmic_text::Buffer>,
+ entries: FxHashMap<KeyHash, Entry>,
+ measurements: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder,
trim_count: usize,
}
+struct Entry {
+ buffer: cosmic_text::Buffer,
+ bounds: Size,
+}
+
#[cfg(not(target_arch = "wasm32"))]
type HashBuilder = twox_hash::RandomXxHashBuilder64;
@@ -384,6 +374,7 @@ impl Cache {
fn new() -> Self {
Self {
entries: FxHashMap::default(),
+ measurements: FxHashMap::default(),
recently_used: FxHashSet::default(),
hasher: HashBuilder::default(),
trim_count: 0,
@@ -394,20 +385,14 @@ impl Cache {
&mut self,
font_system: &mut cosmic_text::FontSystem,
key: Key<'_>,
- ) -> (KeyHash, &mut cosmic_text::Buffer) {
- let hash = {
- let mut hasher = 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);
-
- hasher.finish()
- };
+ ) -> (KeyHash, &mut Entry) {
+ let hash = key.hash(self.hasher.build_hasher());
+
+ if let Some(hash) = self.measurements.get(&hash) {
+ let _ = self.recently_used.insert(*hash);
+
+ return (*hash, self.entries.get_mut(hash).unwrap());
+ }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
let metrics = cosmic_text::Metrics::new(key.size, key.size * 1.2);
@@ -428,7 +413,24 @@ 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);
@@ -440,6 +442,8 @@ impl Cache {
if self.trim_count > Self::TRIM_INTERVAL {
self.entries
.retain(|key, _| self.recently_used.contains(key));
+ self.measurements
+ .retain(|_, value| self.recently_used.contains(value));
self.recently_used.clear();
@@ -460,4 +464,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;