| 1 | //! `measureText` for the sandbox's canvas shim: a parley layout of the string |
| 2 | //! in a font parsed from the CSS `font` shorthand (size, weight, style, family). |
| 3 | |
| 4 | use blitz_dom::FontContext; |
| 5 | use parley::style::{FontFamily, FontStyle, FontWeight, StyleProperty}; |
| 6 | use parley::{Alignment, AlignmentOptions, LayoutContext}; |
| 7 | use serde_json::{json, Value}; |
| 8 | use std::borrow::Cow; |
| 9 | |
| 10 | struct Font { |
| 11 | size: f32, |
| 12 | weight: f32, |
| 13 | italic: bool, |
| 14 | family: String, |
| 15 | } |
| 16 | |
| 17 | fn parse_font(font: &str) -> Font { |
| 18 | let mut out = Font { size: 10.0, weight: 400.0, italic: false, family: "sans-serif".into() }; |
| 19 | let mut rest = font.trim(); |
| 20 | let mut tokens: Vec<&str> = Vec::new(); |
| 21 | // Everything up to the size token is style/variant/weight; the family is whatever follows the size. |
| 22 | for (i, tok) in rest.split_whitespace().enumerate() { |
| 23 | let lower = tok.to_ascii_lowercase(); |
| 24 | let size_tok = lower.split('/').next().unwrap_or(""); |
| 25 | if let Some(px) = size_tok.strip_suffix("px").and_then(|n| n.parse::<f32>().ok()) { |
| 26 | out.size = px; |
| 27 | let pos = rest.match_indices(tok).nth(0).map(|(p, _)| p + tok.len()).unwrap_or(rest.len()); |
| 28 | rest = rest[pos..].trim(); |
| 29 | let _ = i; |
| 30 | break; |
| 31 | } |
| 32 | tokens.push(tok); |
| 33 | } |
| 34 | for tok in tokens { |
| 35 | match tok.to_ascii_lowercase().as_str() { |
| 36 | "italic" | "oblique" => out.italic = true, |
| 37 | "bold" => out.weight = 700.0, |
| 38 | "bolder" => out.weight = 700.0, |
| 39 | "lighter" => out.weight = 300.0, |
| 40 | w => { |
| 41 | if let Ok(n) = w.parse::<f32>() { |
| 42 | out.weight = n; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | if !rest.is_empty() { |
| 48 | out.family = rest.replace('"', "").replace('\'', ""); |
| 49 | } |
| 50 | out |
| 51 | } |
| 52 | |
| 53 | pub fn measure(fcx: &mut FontContext, font: &str, text: &str) -> Value { |
| 54 | let f = parse_font(font); |
| 55 | let mut lcx: LayoutContext<[u8; 4]> = LayoutContext::new(); |
| 56 | let mut builder = lcx.ranged_builder(fcx, text, 1.0, false); |
| 57 | builder.push_default(StyleProperty::FontSize(f.size)); |
| 58 | builder.push_default(StyleProperty::FontWeight(FontWeight::new(f.weight))); |
| 59 | builder.push_default(StyleProperty::FontStyle(if f.italic { FontStyle::Italic } else { FontStyle::Normal })); |
| 60 | builder.push_default(StyleProperty::FontFamily(FontFamily::Source(Cow::Owned(f.family)))); |
| 61 | let mut layout = builder.build(text); |
| 62 | layout.break_all_lines(None); |
| 63 | layout.align(Alignment::Start, AlignmentOptions::default()); |
| 64 | let width = layout.full_width(); |
| 65 | let (ascent, descent) = layout |
| 66 | .lines() |
| 67 | .next() |
| 68 | .map(|l| { |
| 69 | let m = l.metrics(); |
| 70 | (m.baseline - m.offset, m.line_height - (m.baseline - m.offset)) |
| 71 | }) |
| 72 | .unwrap_or((f.size * 0.8, f.size * 0.2)); |
| 73 | json!({ |
| 74 | "width": width, |
| 75 | "actualBoundingBoxLeft": 0.0, "actualBoundingBoxRight": width, |
| 76 | "actualBoundingBoxAscent": ascent, "actualBoundingBoxDescent": descent, |
| 77 | "fontBoundingBoxAscent": ascent, "fontBoundingBoxDescent": descent, |
| 78 | }) |
| 79 | } |
| 80 | |