From 8d078ff3450d51cf386c3f107cc836fe96b94aba Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 30 May 2019 12:38:32 -0700 Subject: [PATCH] Add an initial implementation of canvas `measureText` --- canvas/src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/canvas/src/lib.rs b/canvas/src/lib.rs index 35c1b37e..01674d2d 100644 --- a/canvas/src/lib.rs +++ b/canvas/src/lib.rs @@ -61,6 +61,8 @@ impl CanvasRenderingContext2D { self.scene } + // Drawing rectangles + #[inline] pub fn fill_rect(&mut self, rect: RectF) { let mut path = Path2D::new(); @@ -75,6 +77,8 @@ impl CanvasRenderingContext2D { self.stroke_path(path); } + // Drawing text + pub fn fill_text(&mut self, string: &str, position: Point2DF) { // TODO(pcwalton): Report errors. let paint_id = self.scene.push_paint(&self.current_state.fill_paint); @@ -103,6 +107,23 @@ impl CanvasRenderingContext2D { paint_id)); } + pub fn measure_text(&self, string: &str) -> TextMetrics { + let layout = skribo::layout(&TextStyle { size: self.current_state.font_size }, + &self.current_state.font_collection, + string); + let width = match layout.glyphs.last() { + None => 0.0, + Some(last_glyph) => { + let glyph_id = last_glyph.glyph_id; + let font_metrics = last_glyph.font.font.metrics(); + let glyph_rect = last_glyph.font.font.typographic_bounds(glyph_id).unwrap(); + let scale_factor = layout.size / font_metrics.units_per_em as f32; + last_glyph.offset.x + glyph_rect.max_x() * scale_factor + } + }; + TextMetrics { width } + } + // Line styles #[inline] @@ -319,6 +340,12 @@ impl FillStyle { } } +// TODO(pcwalton): Support other fields. +#[derive(Clone, Copy, Debug)] +pub struct TextMetrics { + pub width: f32, +} + #[derive(Clone)] pub struct CanvasFontContext { #[allow(dead_code)]