1#![cfg(feature = "font")]
13
14use std::collections::HashMap;
15use std::sync::{Mutex, OnceLock};
16
17use ttf_parser::{Face, GlyphId, OutlineBuilder, Rect};
18
19use crate::geom::Shape;
20use crate::metrics::{STAR_FRAC, SUBSCRIPT_SCALE, SUPERSCRIPT_SCALE};
21
22const BEZIER_STEPS: usize = 8;
23
24const FONT_REGULAR: &[u8] = include_bytes!("../fonts/LiberationSans-Regular.ttf");
25const FONT_BOLD: &[u8] = include_bytes!("../fonts/LiberationSans-Bold.ttf");
26const FONT_ITALIC: &[u8] = include_bytes!("../fonts/LiberationSans-Italic.ttf");
27const FONT_BOLD_ITALIC: &[u8] = include_bytes!("../fonts/LiberationSans-BoldItalic.ttf");
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
31pub enum FaceStyle {
32 #[default]
33 Regular,
34 Bold,
35 Italic,
36 BoldItalic,
37}
38
39impl FaceStyle {
40 pub fn from_flags(bold: bool, italic: bool) -> Self {
41 match (bold, italic) {
42 (true, true) => Self::BoldItalic,
43 (true, false) => Self::Bold,
44 (false, true) => Self::Italic,
45 (false, false) => Self::Regular,
46 }
47 }
48
49 fn bytes(self) -> &'static [u8] {
50 match self {
51 Self::Regular => FONT_REGULAR,
52 Self::Bold => FONT_BOLD,
53 Self::Italic => FONT_ITALIC,
54 Self::BoldItalic => FONT_BOLD_ITALIC,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy)]
61pub struct FaceMetrics {
62 pub upem: f64,
63 pub ascent: f64,
64 pub descent: f64,
65 pub line_gap: f64,
66 pub cap_height: f64,
67 pub x_height: f64,
68 pub stem_em: f64,
69}
70
71#[derive(Debug, Clone, Copy)]
73pub struct GlyphMetrics {
74 pub advance: f64,
75 pub ink_xmin: Option<f64>,
76 pub ink_ymin: Option<f64>,
77 pub ink_xmax: Option<f64>,
78 pub ink_ymax: Option<f64>,
79}
80
81impl GlyphMetrics {
82 pub fn has_ink(&self) -> bool {
83 self.ink_xmin.is_some()
84 }
85
86 pub fn ink_width(&self) -> f64 {
87 match (self.ink_xmin, self.ink_xmax) {
88 (Some(a), Some(b)) => b - a,
89 _ => 0.0,
90 }
91 }
92
93 pub fn ink_height(&self) -> f64 {
94 match (self.ink_ymin, self.ink_ymax) {
95 (Some(a), Some(b)) => b - a,
96 _ => 0.0,
97 }
98 }
99}
100
101fn face(style: FaceStyle) -> Face<'static> {
102 Face::parse(style.bytes(), 0).expect("bundled Liberation Sans TTF")
103}
104
105pub fn face_metrics(style: FaceStyle) -> FaceMetrics {
107 let f = face(style);
108 let upem = f64::from(f.units_per_em());
109 let mut cap = f.capital_height().map(f64::from).unwrap_or(0.0);
110 let mut xh = f.x_height().map(f64::from).unwrap_or(0.0);
111 if cap <= 0.0 {
112 if let Some(g) = glyph_metrics('H', style) {
113 cap = g.ink_ymax.unwrap_or(0.0);
114 }
115 }
116 if xh <= 0.0 {
117 if let Some(g) = glyph_metrics('x', style) {
118 xh = g.ink_ymax.unwrap_or(0.0);
119 }
120 }
121 FaceMetrics {
122 upem,
123 ascent: f64::from(f.ascender()),
124 descent: f64::from(f.descender()),
125 line_gap: f64::from(f.line_gap()),
126 cap_height: cap,
127 x_height: xh,
128 stem_em: measure_stem_em(style),
129 }
130}
131
132pub fn glyph_metrics(ch: char, style: FaceStyle) -> Option<GlyphMetrics> {
134 let f = face(style);
135 let upem = f64::from(f.units_per_em());
136 let Some(gid) = f.glyph_index(ch) else {
137 return Some(GlyphMetrics {
138 advance: 0.5 * upem,
139 ink_xmin: None,
140 ink_ymin: None,
141 ink_xmax: None,
142 ink_ymax: None,
143 });
144 };
145 let advance = f64::from(f.glyph_hor_advance(gid).unwrap_or(0));
146 let ink = f.glyph_bounding_box(gid).map(rect_to_ink);
147 Some(GlyphMetrics {
148 advance,
149 ink_xmin: ink.map(|r| r.0),
150 ink_ymin: ink.map(|r| r.1),
151 ink_xmax: ink.map(|r| r.2),
152 ink_ymax: ink.map(|r| r.3),
153 })
154}
155
156fn rect_to_ink(r: Rect) -> (f64, f64, f64, f64) {
157 (
158 f64::from(r.x_min),
159 f64::from(r.y_min),
160 f64::from(r.x_max),
161 f64::from(r.y_max),
162 )
163}
164
165struct ContourCollector {
166 contours: Vec<Vec<(f64, f64)>>,
167 cur: Vec<(f64, f64)>,
168}
169
170impl ContourCollector {
171 fn new() -> Self {
172 Self {
173 contours: Vec::new(),
174 cur: Vec::new(),
175 }
176 }
177
178 fn finish_contour(&mut self) {
179 if self.cur.len() >= 3 {
180 self.contours.push(std::mem::take(&mut self.cur));
181 } else {
182 self.cur.clear();
183 }
184 }
185}
186
187impl OutlineBuilder for ContourCollector {
188 fn move_to(&mut self, x: f32, y: f32) {
189 self.finish_contour();
190 self.cur = vec![(f64::from(x), f64::from(y))];
191 }
192
193 fn line_to(&mut self, x: f32, y: f32) {
194 self.cur.push((f64::from(x), f64::from(y)));
195 }
196
197 fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
198 let (x0, y0) = *self.cur.last().expect("quad_to without move_to");
199 let (x1, y1, x, y) = (f64::from(x1), f64::from(y1), f64::from(x), f64::from(y));
200 for i in 1..=BEZIER_STEPS {
201 let t = i as f64 / BEZIER_STEPS as f64;
202 let mt = 1.0 - t;
203 self.cur.push((
204 mt * mt * x0 + 2.0 * mt * t * x1 + t * t * x,
205 mt * mt * y0 + 2.0 * mt * t * y1 + t * t * y,
206 ));
207 }
208 }
209
210 fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
211 let (x0, y0) = *self.cur.last().expect("curve_to without move_to");
212 let (x1, y1, x2, y2, x, y) = (
213 f64::from(x1),
214 f64::from(y1),
215 f64::from(x2),
216 f64::from(y2),
217 f64::from(x),
218 f64::from(y),
219 );
220 for i in 1..=BEZIER_STEPS {
221 let t = i as f64 / BEZIER_STEPS as f64;
222 let mt = 1.0 - t;
223 self.cur.push((
224 mt * mt * mt * x0 + 3.0 * mt * mt * t * x1 + 3.0 * mt * t * t * x2 + t * t * t * x,
225 mt * mt * mt * y0 + 3.0 * mt * mt * t * y1 + 3.0 * mt * t * t * y2 + t * t * t * y,
226 ));
227 }
228 }
229
230 fn close(&mut self) {
231 self.finish_contour();
232 }
233}
234
235fn outline_glyph(face: &Face<'_>, gid: GlyphId) -> Vec<Vec<(f64, f64)>> {
236 let mut collector = ContourCollector::new();
237 let _ = face.outline_glyph(gid, &mut collector);
238 collector.finish_contour();
239 collector.contours
240}
241
242pub fn outline_run_em(text: &str, style: FaceStyle) -> (Option<Shape>, f64) {
244 if text.is_empty() {
245 return (None, 0.0);
246 }
247 let f = face(style);
248 let upem = f64::from(f.units_per_em());
249 let mut contours: Vec<Vec<(f64, f64)>> = Vec::new();
250 let mut pen_x = 0.0;
251 for ch in text.chars() {
252 let Some(gid) = f.glyph_index(ch) else {
253 pen_x += 0.5 * upem;
254 continue;
255 };
256 for mut contour in outline_glyph(&f, gid) {
257 for p in &mut contour {
258 p.0 += pen_x;
259 }
260 contours.push(contour);
261 }
262 pen_x += f64::from(f.glyph_hor_advance(gid).unwrap_or(0));
263 }
264 if contours.is_empty() {
265 return (None, pen_x);
266 }
267 let shape = Shape::from_contours_evenodd(&contours);
268 if shape.is_empty() {
269 (None, pen_x)
270 } else {
271 (Some(shape), pen_x)
272 }
273}
274
275#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
277pub enum ScriptRole {
278 Normal,
279 Subscript,
280 Superscript,
281}
282
283#[derive(Debug, Clone, Copy, PartialEq, Eq)]
285pub struct ChemGlyph {
286 pub ch: char,
287 pub role: ScriptRole,
288 pub face: FaceStyle,
289}
290
291fn script_cache() -> &'static Mutex<HashMap<(char, FaceStyle, ScriptRole), (Option<Shape>, f64)>> {
292 static CACHE: OnceLock<Mutex<HashMap<(char, FaceStyle, ScriptRole), (Option<Shape>, f64)>>> =
293 OnceLock::new();
294 CACHE.get_or_init(|| Mutex::new(HashMap::new()))
295}
296
297pub fn outline_glyph_em(ch: char, style: FaceStyle, role: ScriptRole) -> (Option<Shape>, f64) {
302 if let Ok(guard) = script_cache().lock() {
303 if let Some(hit) = guard.get(&(ch, style, role)) {
304 return hit.clone();
305 }
306 }
307 let built = outline_glyph_em_uncached(ch, style, role);
308 if let Ok(mut guard) = script_cache().lock() {
309 guard.insert((ch, style, role), built.clone());
310 }
311 built
312}
313
314fn script_scale_for(role: ScriptRole) -> f64 {
315 match role {
316 ScriptRole::Normal => 1.0,
317 ScriptRole::Subscript => SUBSCRIPT_SCALE,
318 ScriptRole::Superscript => SUPERSCRIPT_SCALE,
319 }
320}
321
322fn outline_glyph_em_uncached(ch: char, style: FaceStyle, role: ScriptRole) -> (Option<Shape>, f64) {
323 let f = face(style);
324 let upem = f64::from(f.units_per_em());
325 let Some(gid) = f.glyph_index(ch) else {
326 return (None, 0.5 * upem);
327 };
328 let advance = f64::from(f.glyph_hor_advance(gid).unwrap_or(0));
329 let contours = outline_glyph(&f, gid);
330 if contours.is_empty() {
331 return (None, advance);
332 }
333 let shape = Shape::from_contours_evenodd(&contours);
334 if shape.is_empty() {
335 return (None, advance);
336 }
337 match role {
338 ScriptRole::Normal => (Some(shape), advance),
339 ScriptRole::Subscript | ScriptRole::Superscript => {
340 let scale = script_scale_for(role);
341 let scaled = shape.scale(scale, scale, 0.0, 0.0);
343 let stem = face_metrics(style).stem_em;
345 let grow = 0.5 * (1.0 - scale) * stem * upem;
346 let thick = if grow > 0.05 {
347 scaled.buffer(grow)
348 } else {
349 scaled
350 };
351 (Some(thick), advance * scale)
352 }
353 }
354}
355
356pub fn outline_star_em(style: FaceStyle) -> (Shape, f64) {
361 let face_m = face_metrics(style);
362 let height = STAR_FRAC * face_m.cap_height;
363 let outer = 0.5 * height;
364 let inner = outer * 0.38;
365 let pad = 0.12 * face_m.cap_height;
366 let advance = height + pad;
367 let cx = 0.5 * advance;
368 let cy = 0.5 * face_m.cap_height;
369 let mut ring = Vec::with_capacity(10);
370 for i in 0..10 {
371 let a = -std::f64::consts::FRAC_PI_2 + i as f64 * std::f64::consts::PI / 5.0;
372 let r = if i % 2 == 0 { outer } else { inner };
373 ring.push((cx + r * a.cos(), cy + r * a.sin()));
374 }
375 let shape = Shape::from_ring(&ring);
376 (shape, advance)
377}
378
379pub fn outline_chem_run_em(glyphs: &[ChemGlyph], style: FaceStyle) -> (Option<Shape>, f64) {
387 if glyphs.is_empty() {
388 return (None, 0.0);
389 }
390 if glyphs.len() == 1 && glyphs[0].ch == '*' && glyphs[0].role == ScriptRole::Normal {
392 let (s, adv) = outline_star_em(glyphs[0].face);
393 return (if s.is_empty() { None } else { Some(s) }, adv);
394 }
395 let metrics_face = glyphs.first().map(|g| g.face).unwrap_or(style);
396 let face_m = face_metrics(metrics_face);
397 let sub_dy = -0.33 * face_m.cap_height;
398 let super_dy = 0.50 * face_m.cap_height;
399 let mut pen_x = 0.0;
400 let mut acc: Option<Shape> = None;
401 for g in glyphs {
402 let (piece, adv) = if g.ch == '*' && g.role == ScriptRole::Normal {
403 let (s, a) = outline_star_em(g.face);
404 (Some(s), a)
405 } else {
406 outline_glyph_em(g.ch, g.face, g.role)
407 };
408 let dy = match g.role {
409 ScriptRole::Normal => 0.0,
410 ScriptRole::Subscript => sub_dy,
411 ScriptRole::Superscript => super_dy,
412 };
413 if let Some(p) = piece.filter(|s| !s.is_empty()) {
414 let placed = p.translate(pen_x, dy);
415 acc = Some(match acc.take() {
416 Some(a) => a.union(&placed),
417 None => placed,
418 });
419 }
420 pen_x += adv;
421 }
422 (acc, pen_x)
423}
424
425pub fn compile_chem_shapes(
429 glyphs: &[ChemGlyph],
430 x: f64,
431 y: f64,
432 font_size: f64,
433 anchor: &str,
434 style: FaceStyle,
435) -> Option<Shape> {
436 if glyphs.is_empty() {
437 return None;
438 }
439 let face_m = face_metrics(style);
440 let scale = font_size / face_m.upem;
441 let (geom_em, advance_em) = outline_chem_run_em(glyphs, style);
442 let advance = advance_em * scale;
443 let origin_x = match anchor {
444 "end" => x - advance,
445 "start" | "left" => x,
446 _ => x - 0.5 * advance,
447 };
448 let geom_em = geom_em?;
449 let geom = geom_em
452 .scale(scale, -scale, 0.0, 0.0)
453 .translate(origin_x, y);
454 if geom.is_empty() {
455 None
456 } else {
457 Some(geom)
458 }
459}
460
461pub fn compile_text_shapes(
463 text: &str,
464 x: f64,
465 y: f64,
466 font_size: f64,
467 anchor: &str,
468 style: FaceStyle,
469) -> Option<Shape> {
470 if text.is_empty() {
471 return None;
472 }
473 let glyphs: Vec<ChemGlyph> = text
474 .chars()
475 .map(|ch| ChemGlyph {
476 ch,
477 role: ScriptRole::Normal,
478 face: style,
479 })
480 .collect();
481 compile_chem_shapes(&glyphs, x, y, font_size, anchor, style)
482}
483
484fn measure_stem_em(style: FaceStyle) -> f64 {
485 let f = face(style);
486 let upem = f64::from(f.units_per_em());
487 let Some(gid) = f.glyph_index('H') else {
488 return 0.0933;
489 };
490 let contours = outline_glyph(&f, gid);
491 if contours.is_empty() {
492 return 0.0933;
493 }
494 let geom = Shape::from_contours_evenodd(&contours);
495 let Some((_minx, miny, _maxx, maxy)) = geom.bounds() else {
496 return 0.0933;
497 };
498 let y = miny + 0.75 * (maxy - miny);
499 let span = geom.horizontal_span_at(y);
500 if span <= 0.0 {
501 0.0933
502 } else {
503 span / upem
504 }
505}
506
507#[cfg(test)]
508mod tests {
509 use super::*;
510 use crate::metrics::{FONT_PX, FONT_STEM_EM, STAR_FRAC, SUBSCRIPT_SCALE, SUPERSCRIPT_SCALE};
511
512 #[test]
513 fn liberation_face_metrics() {
514 let m = face_metrics(FaceStyle::Regular);
515 assert_eq!(m.upem, 2048.0);
516 assert!((m.ascent - 1854.0).abs() < 1e-6);
517 assert!((m.descent - -434.0).abs() < 1e-6);
518 assert!((m.stem_em - FONT_STEM_EM).abs() / FONT_STEM_EM < 0.05);
519 assert!(m.cap_height > 0.5 * m.upem);
520 }
521
522 #[test]
523 fn glyph_h_and_o() {
524 let h = glyph_metrics('H', FaceStyle::Regular).unwrap();
525 assert!((h.advance - 1479.0).abs() < 1e-6);
526 assert!(h.has_ink());
527 assert!(h.ink_height() > 1000.0);
528 let o = glyph_metrics('O', FaceStyle::Regular).unwrap();
529 assert!(o.ink_width() > o.advance * 0.5);
530 }
531
532 #[test]
533 fn outline_o_has_hole() {
534 let (shape, adv) = outline_run_em("O", FaceStyle::Regular);
535 let shape = shape.expect("O outline");
536 assert!(shape.has_holes());
537 assert!(adv > 0.0);
538 assert!(!shape.contains(shape.centroid().unwrap().0, shape.centroid().unwrap().1));
539 }
540
541 #[test]
542 fn compile_svg_space_label() {
543 let shape = compile_text_shapes("OH", 100.0, 50.0, FONT_PX, "middle", FaceStyle::Regular)
544 .expect("OH");
545 assert!(!shape.is_empty());
546 let d = shape.to_svg_d();
547 assert!(d.contains('M') && d.contains('Z'));
548 }
549
550 #[test]
551 fn script_glyph_is_smaller_but_thickened() {
552 let (full, adv_full) = outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Normal);
553 let (sub, adv_sub) = outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Subscript);
554 let full = full.expect("2");
555 let sub = sub.expect("2 sub");
556 assert!(adv_sub < adv_full * 0.75);
557 assert!(sub.area() > 0.0);
558 assert!(sub.area() > full.area() * SUBSCRIPT_SCALE * SUBSCRIPT_SCALE * 0.5);
560 }
561
562 #[test]
563 fn charge_superscript_larger_than_h_subscript() {
564 let (_full, adv_full) = outline_glyph_em('+', FaceStyle::Regular, ScriptRole::Normal);
565 let (_plus, adv_plus) = outline_glyph_em('+', FaceStyle::Regular, ScriptRole::Superscript);
566 let (_two, adv_sub) = outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Subscript);
567 assert!(
569 (adv_plus / adv_full - SUPERSCRIPT_SCALE).abs() < 0.05,
570 "superscript + advance should track SUPERSCRIPT_SCALE={SUPERSCRIPT_SCALE}, got {}",
571 adv_plus / adv_full
572 );
573 assert!(
574 adv_plus / adv_full > 0.80,
575 "charge + too small: ratio {}",
576 adv_plus / adv_full
577 );
578 assert!(
579 adv_plus / adv_full
580 > adv_sub / outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Normal).1 + 0.1,
581 "charge superscript should outsize H-count subscript"
582 );
583 let _ = SUBSCRIPT_SCALE;
584 }
585
586 #[test]
587 fn star_is_larger_than_asterisk_advance() {
588 let (_ast, adv_ast) = outline_run_em("*", FaceStyle::Regular);
589 let (star, adv_star) = outline_star_em(FaceStyle::Regular);
590 assert!(adv_star > adv_ast * 1.2);
591 let face = face_metrics(FaceStyle::Regular);
592 let (_x0, y0, _x1, y1) = star.bounds().expect("star ink");
593 let height = y1 - y0;
594 assert!(height > 1.7 * face.cap_height);
596 assert!(height < 2.3 * face.cap_height);
597 let _ = STAR_FRAC;
598 }
599
600 #[test]
601 fn face_style_flags_and_variants() {
602 assert_eq!(FaceStyle::from_flags(false, false), FaceStyle::Regular);
603 assert_eq!(FaceStyle::from_flags(true, false), FaceStyle::Bold);
604 assert_eq!(FaceStyle::from_flags(false, true), FaceStyle::Italic);
605 assert_eq!(FaceStyle::from_flags(true, true), FaceStyle::BoldItalic);
606 for style in [FaceStyle::Bold, FaceStyle::Italic, FaceStyle::BoldItalic] {
607 let m = face_metrics(style);
608 assert_eq!(m.upem, 2048.0);
609 assert!(m.stem_em > 0.05);
610 }
611 }
612
613 #[test]
614 fn glyph_metrics_missing_and_empty_ink() {
615 let missing = glyph_metrics('\u{10FFFF}', FaceStyle::Regular).unwrap();
616 assert!(!missing.has_ink());
617 assert_eq!(missing.ink_width(), 0.0);
618 assert_eq!(missing.ink_height(), 0.0);
619 assert!(missing.advance > 0.0);
620
621 let (none, adv) = outline_run_em("", FaceStyle::Regular);
622 assert!(none.is_none());
623 assert_eq!(adv, 0.0);
624
625 let (shape, adv2) = outline_run_em("\u{10FFFF}", FaceStyle::Regular);
627 assert!(shape.is_none());
628 assert!(adv2 > 0.0);
629 }
630
631 #[test]
632 fn chem_run_scripts_anchors_and_empty() {
633 assert!(outline_chem_run_em(&[], FaceStyle::Regular).0.is_none());
634 assert!(
635 compile_chem_shapes(&[], 0.0, 0.0, FONT_PX, "middle", FaceStyle::Regular).is_none()
636 );
637 assert!(compile_text_shapes("", 0.0, 0.0, FONT_PX, "middle", FaceStyle::Regular).is_none());
638
639 let glyphs = [
640 ChemGlyph {
641 ch: 'N',
642 role: ScriptRole::Normal,
643 face: FaceStyle::Regular,
644 },
645 ChemGlyph {
646 ch: '2',
647 role: ScriptRole::Subscript,
648 face: FaceStyle::Regular,
649 },
650 ChemGlyph {
651 ch: '+',
652 role: ScriptRole::Superscript,
653 face: FaceStyle::Regular,
654 },
655 ];
656 let (shape, adv) = outline_chem_run_em(&glyphs, FaceStyle::Regular);
657 assert!(shape.is_some());
658 assert!(adv > 0.0);
659
660 let start = compile_chem_shapes(&glyphs, 50.0, 20.0, FONT_PX, "start", FaceStyle::Regular)
661 .expect("start");
662 let end = compile_chem_shapes(&glyphs, 50.0, 20.0, FONT_PX, "end", FaceStyle::Regular)
663 .expect("end");
664 let mid = compile_chem_shapes(&glyphs, 50.0, 20.0, FONT_PX, "middle", FaceStyle::Regular)
665 .expect("mid");
666 assert!(!start.is_empty() && !end.is_empty() && !mid.is_empty());
667
668 let star_run = [ChemGlyph {
670 ch: '*',
671 role: ScriptRole::Normal,
672 face: FaceStyle::Regular,
673 }];
674 let (s, _) = outline_chem_run_em(&star_run, FaceStyle::Regular);
675 assert!(s.unwrap().area() > 0.0);
676 }
677
678 #[test]
679 fn script_cache_hit_and_missing_in_chem_run() {
680 let a = outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Subscript);
681 let b = outline_glyph_em('2', FaceStyle::Regular, ScriptRole::Subscript);
682 assert_eq!(a.1, b.1);
683 assert!(a.0.is_some());
684
685 let glyphs = [
687 ChemGlyph {
688 ch: '\u{10FFFF}',
689 role: ScriptRole::Normal,
690 face: FaceStyle::Regular,
691 },
692 ChemGlyph {
693 ch: '*',
694 role: ScriptRole::Normal,
695 face: FaceStyle::Regular,
696 },
697 ];
698 let (shape, adv) = outline_chem_run_em(&glyphs, FaceStyle::Regular);
699 assert!(shape.is_some());
700 assert!(adv > 0.0);
701
702 let miss = outline_glyph_em('\u{10FFFF}', FaceStyle::Bold, ScriptRole::Normal);
704 assert!(miss.0.is_none());
705 assert!(miss.1 > 0.0);
706 }
707
708 #[test]
709 fn contour_collector_cubic_curve_to() {
710 let mut c = ContourCollector::new();
712 c.move_to(0.0, 0.0);
713 c.curve_to(10.0, 0.0, 10.0, 10.0, 0.0, 10.0);
714 c.close();
715 assert_eq!(c.contours.len(), 1);
716 assert_eq!(
717 c.contours[0].len(),
718 1 + BEZIER_STEPS,
719 "move_to + one sample per Bezier step"
720 );
721 let t = 0.5;
723 let mt = 1.0 - t;
724 let expected = (
725 mt * mt * mt * 0.0
726 + 3.0 * mt * mt * t * 10.0
727 + 3.0 * mt * t * t * 10.0
728 + t * t * t * 0.0,
729 mt * mt * mt * 0.0
730 + 3.0 * mt * mt * t * 0.0
731 + 3.0 * mt * t * t * 10.0
732 + t * t * t * 10.0,
733 );
734 let mid_idx = BEZIER_STEPS / 2;
735 let (mx, my) = c.contours[0][mid_idx];
736 assert!(
737 (mx - expected.0).abs() < 1e-9 && (my - expected.1).abs() < 1e-9,
738 "midpoint ({mx}, {my}) vs cubic@0.5 ({}, {})",
739 expected.0,
740 expected.1
741 );
742 }
743}