Add an initial implementation of canvas `measureText`
@@ -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)]
