1use crate::contract::{OutlineCommand, ShapedGlyph, ShapingInput};
4use crate::text_shaping::TextShapingFace;
5use crate::{
6 FontOutlineError, FontOutlineErrorKind, FontOutlineFace, FontOutlineFaceRequest,
7 HintedFontOutlineFace, TextBezierError, TextBezierErrorKind, TextPoint, TextShapingError,
8 TextShapingErrorKind, TextShapingOutput, flatten_cubic_bezier, flatten_quadratic_bezier,
9 shape_text_a0,
10};
11use std::fmt;
12
13pub const OUTLINE_FACE_SCALER: f64 = 1433.0;
19pub const TEXT_BEZIER_ERROR: f64 = 2.0;
21pub const SUBSCRIPT_SUPERSCRIPT_SIZE_RATIO: f64 = 0.64;
23pub const SUBSCRIPT_FACE_SCALER: f64 = 917.0;
25pub const SUBSCRIPT_VERTICAL_OFFSET_RATIO: f64 = -0.25;
27pub const SUPERSCRIPT_VERTICAL_OFFSET_RATIO: f64 = 0.45;
29
30#[derive(Clone, Copy, Debug)]
32pub struct TextContourRequest<'a> {
33 pub shaping: &'a ShapingInput,
34 pub em_x: f64,
36 pub em_y: f64,
38 pub origin_x: f64,
39 pub origin_y: f64,
40 pub max_error: f64,
41 pub fake_bold: bool,
42 pub fake_italic: bool,
43}
44
45#[derive(Clone, Debug, PartialEq)]
47pub struct TextContour {
48 pub points: Vec<TextPoint>,
49}
50
51#[derive(Clone, Debug)]
53pub struct TextContourOutput {
54 pub contours: Vec<TextContour>,
55 pub contour_group_sizes: Vec<usize>,
59 pub contour_group_origins: Vec<TextPoint>,
63 pub advance_x: f64,
64 pub advance_y: f64,
65 pub units_per_em: u16,
66 pub glyphs: usize,
67 pub missing_glyphs: usize,
70 pub outline_less_glyphs: usize,
74 pub outline_commands: usize,
75 pub bezier_work_items: usize,
76 pub peak_temporary_bezier_points: usize,
77}
78
79#[doc(hidden)]
81pub struct HintedTextContourSession<'a> {
82 shaping: TextShapingFace<'a>,
83 outline: HintedFontOutlineFace<'a>,
84 subscript_outline: Option<HintedFontOutlineFace<'a>>,
85}
86
87#[derive(Clone, Copy, Debug, Eq, PartialEq)]
89#[doc(hidden)]
90pub enum TextRunStyle {
91 Normal,
92 Subscript,
93 Superscript,
94}
95
96impl TextRunStyle {
97 fn face_scaler(self) -> f64 {
99 match self {
100 Self::Normal => OUTLINE_FACE_SCALER,
101 Self::Subscript | Self::Superscript => SUBSCRIPT_FACE_SCALER,
102 }
103 }
104
105 fn vertical_offset_ratio(self) -> f64 {
107 match self {
108 Self::Normal => 0.0,
109 Self::Subscript => SUBSCRIPT_VERTICAL_OFFSET_RATIO,
110 Self::Superscript => SUPERSCRIPT_VERTICAL_OFFSET_RATIO,
111 }
112 }
113}
114
115#[derive(Clone, Copy, Debug, Eq, PartialEq)]
117pub enum TextContourErrorKind {
118 ResourceLimit,
119 InvalidInput,
120 Shaping,
121 Outline,
122}
123
124#[derive(Clone, Debug, Eq, PartialEq)]
126pub struct TextContourError {
127 pub kind: TextContourErrorKind,
128 pub path: String,
129 pub message: &'static str,
130}
131
132impl fmt::Display for TextContourError {
133 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
134 write!(formatter, "{} at {}", self.message, self.path)
135 }
136}
137
138impl std::error::Error for TextContourError {}
139
140pub fn shape_text_contours_a0(
142 font_bytes: &[u8],
143 request: TextContourRequest<'_>,
144) -> Result<TextContourOutput, TextContourError> {
145 shape_text_contours_with_outline_mode(font_bytes, request, OutlineMode::Unhinted)
146}
147
148pub fn shape_text_contours_hinted_a0(
150 font_bytes: &[u8],
151 request: TextContourRequest<'_>,
152) -> Result<TextContourOutput, TextContourError> {
153 shape_text_contours_with_outline_mode(font_bytes, request, OutlineMode::Hinted)
154}
155
156pub struct TextContourSession<'a> {
161 shaping: TextShapingFace<'a>,
162 outline: FontOutlineFace<'a>,
163}
164
165impl<'a> TextContourSession<'a> {
166 pub fn new(font_bytes: &'a [u8], shaping: &ShapingInput) -> Result<Self, TextContourError> {
169 let shaping_face = TextShapingFace::new(font_bytes, shaping).map_err(map_shaping_error)?;
170 let outline = FontOutlineFace::new(
171 font_bytes,
172 FontOutlineFaceRequest {
173 font_id: shaping.font_id.as_str(),
174 font_sha256: &shaping.font_sha256.0,
175 face_index: shaping.face_index,
176 variations: &shaping.variations,
177 },
178 )
179 .map_err(map_outline_error)?;
180 Ok(Self {
181 shaping: shaping_face,
182 outline,
183 })
184 }
185
186 pub fn shape(
188 &self,
189 request: TextContourRequest<'_>,
190 ) -> Result<TextContourOutput, TextContourError> {
191 validate_request(request)?;
192 let shaped = self
193 .shaping
194 .shape_input(request.shaping)
195 .map_err(map_shaping_error)?;
196 compose_shaped_contours(&self.outline, &shaped, request, TextRunStyle::Normal)
197 }
198}
199
200impl<'a> HintedTextContourSession<'a> {
201 #[doc(hidden)]
202 pub fn new(font_bytes: &'a [u8], shaping: &ShapingInput) -> Result<Self, TextContourError> {
203 let shaping_face = TextShapingFace::new(font_bytes, shaping).map_err(map_shaping_error)?;
204 let face_request = FontOutlineFaceRequest {
205 font_id: shaping.font_id.as_str(),
206 font_sha256: &shaping.font_sha256.0,
207 face_index: shaping.face_index,
208 variations: &shaping.variations,
209 };
210 let outline =
211 HintedFontOutlineFace::new(font_bytes, face_request).map_err(map_outline_error)?;
212 let subscript_outline = if shaping.text.contains("_{") || shaping.text.contains("^{") {
215 Some(
216 HintedFontOutlineFace::new_subscript(font_bytes, face_request)
217 .map_err(map_outline_error)?,
218 )
219 } else {
220 None
221 };
222 Ok(Self {
223 shaping: shaping_face,
224 outline,
225 subscript_outline,
226 })
227 }
228
229 #[doc(hidden)]
230 pub fn shape_input_styled(
231 &self,
232 request: TextContourRequest<'_>,
233 style: TextRunStyle,
234 ) -> Result<TextContourOutput, TextContourError> {
235 validate_request(request)?;
236 let shaped = self
237 .shaping
238 .shape_input(request.shaping)
239 .map_err(map_shaping_error)?;
240 let face = match style {
241 TextRunStyle::Normal => &self.outline,
242 TextRunStyle::Subscript | TextRunStyle::Superscript => {
243 self.subscript_outline.as_ref().ok_or_else(|| {
244 contour_error(
245 TextContourErrorKind::InvalidInput,
246 "$.markup",
247 "styled run requested without a styled outline face",
248 )
249 })?
250 }
251 };
252 compose_shaped_contours(face, &shaped, request, style)
253 }
254}
255
256#[derive(Clone, Copy)]
257enum OutlineMode {
258 Unhinted,
259 Hinted,
260}
261
262trait ContourOutlineFace {
263 fn units_per_em(&self) -> u16;
264
265 fn outline_to_internal(&self) -> f64;
266
267 fn quantizes_cursor_to_internal_units(&self) -> bool;
268
269 fn extract_glyph(&self, glyph_id: u32) -> Result<crate::FontOutlineOutput, FontOutlineError>;
270}
271
272impl ContourOutlineFace for FontOutlineFace<'_> {
273 fn units_per_em(&self) -> u16 {
274 FontOutlineFace::units_per_em(self)
275 }
276
277 fn outline_to_internal(&self) -> f64 {
278 OUTLINE_FACE_SCALER / f64::from(self.units_per_em())
279 }
280
281 fn quantizes_cursor_to_internal_units(&self) -> bool {
282 false
283 }
284
285 fn extract_glyph(&self, glyph_id: u32) -> Result<crate::FontOutlineOutput, FontOutlineError> {
286 FontOutlineFace::extract_glyph(self, glyph_id)
287 }
288}
289
290impl ContourOutlineFace for HintedFontOutlineFace<'_> {
291 fn units_per_em(&self) -> u16 {
292 HintedFontOutlineFace::units_per_em(self)
293 }
294
295 fn outline_to_internal(&self) -> f64 {
296 1.0
297 }
298
299 fn quantizes_cursor_to_internal_units(&self) -> bool {
300 true
301 }
302
303 fn extract_glyph(&self, glyph_id: u32) -> Result<crate::FontOutlineOutput, FontOutlineError> {
304 HintedFontOutlineFace::extract_glyph(self, glyph_id)
305 }
306}
307
308fn shape_text_contours_with_outline_mode(
309 font_bytes: &[u8],
310 request: TextContourRequest<'_>,
311 outline_mode: OutlineMode,
312) -> Result<TextContourOutput, TextContourError> {
313 validate_request(request)?;
314 let shaped = shape_text_a0(font_bytes, request.shaping).map_err(map_shaping_error)?;
315 let face_request = FontOutlineFaceRequest {
316 font_id: request.shaping.font_id.as_str(),
317 font_sha256: &request.shaping.font_sha256.0,
318 face_index: request.shaping.face_index,
319 variations: &request.shaping.variations,
320 };
321 match outline_mode {
322 OutlineMode::Unhinted => {
323 let face = FontOutlineFace::new(font_bytes, face_request).map_err(map_outline_error)?;
324 compose_shaped_contours(&face, &shaped, request, TextRunStyle::Normal)
325 }
326 OutlineMode::Hinted => {
327 let face =
328 HintedFontOutlineFace::new(font_bytes, face_request).map_err(map_outline_error)?;
329 compose_shaped_contours(&face, &shaped, request, TextRunStyle::Normal)
330 }
331 }
332}
333
334fn compose_shaped_contours(
335 face: &impl ContourOutlineFace,
336 shaped: &TextShapingOutput,
337 request: TextContourRequest<'_>,
338 style: TextRunStyle,
339) -> Result<TextContourOutput, TextContourError> {
340 if face.units_per_em() != shaped.units_per_em {
341 return Err(contour_error(
342 TextContourErrorKind::InvalidInput,
343 "$.face_index",
344 "shaping and outline backends disagree on units per em",
345 ));
346 }
347
348 let transform = PlacementTransform::of_run(face, request, style);
349 let PlacementTransform {
350 position_x_to_internal,
351 position_y_to_internal,
352 internal_to_x,
353 internal_to_y,
354 ..
355 } = transform;
356 let style_offset_y = style.vertical_offset_ratio() * f64::from(request.shaping.scale_y);
359
360 let mut output = ContourBuilder::new(request.max_error, transform);
361 let mut cursor_x = 0.0;
362 let mut cursor_y = 0.0;
363 let mut outline_less_glyphs = 0usize;
364 for glyph in &shaped.glyphs {
365 let position_x = cursor_x + glyph.x_offset.get() as f64;
366 let position_y = cursor_y + glyph.y_offset.get() as f64 + style_offset_y;
367 match extract_styled_outline(face, glyph.glyph_id, request)? {
368 Some(outline) => {
369 output.charge_commands(outline.commands.len())?;
370 output.append_outline(&outline.commands, position_x, position_y)?;
371 output.glyph_origin =
372 output.placed(TextPoint { x: 0.0, y: 0.0 }, position_x, position_y);
373 }
374 None if glyph.glyph_id != 0
376 && !cluster_source(request.shaping, glyph)?.is_whitespace() =>
377 {
378 outline_less_glyphs = outline_less_glyphs.checked_add(1).ok_or_else(|| {
379 resource_error("$.glyphs", "outline-less glyph count overflowed")
380 })?;
381 }
382 None => {}
383 }
384 output.close_group();
385 cursor_x = advance_cursor(
386 cursor_x,
387 glyph.x_advance.get() as f64,
388 position_x_to_internal,
389 face.quantizes_cursor_to_internal_units(),
390 );
391 cursor_y = advance_cursor(
392 cursor_y,
393 glyph.y_advance.get() as f64,
394 position_y_to_internal,
395 face.quantizes_cursor_to_internal_units(),
396 );
397 if !cursor_x.is_finite() || !cursor_y.is_finite() {
398 return Err(contour_error(
399 TextContourErrorKind::InvalidInput,
400 "$.glyphs",
401 "shaped cursor accumulation is not finite",
402 ));
403 }
404 }
405 let advance_x = cursor_x * position_x_to_internal * internal_to_x;
406 let advance_y = -cursor_y * position_y_to_internal * internal_to_y;
407 if !advance_x.is_finite() || !advance_y.is_finite() {
408 return Err(contour_error(
409 TextContourErrorKind::InvalidInput,
410 "$.advance",
411 "text run advance is not finite",
412 ));
413 }
414 Ok(output.finish(
415 advance_x,
416 advance_y,
417 shaped.units_per_em,
418 shaped.glyphs.len(),
419 shaped
420 .glyphs
421 .iter()
422 .filter(|glyph| glyph.glyph_id == 0)
423 .count(),
424 outline_less_glyphs,
425 ))
426}
427
428fn cluster_source(shaping: &ShapingInput, glyph: &ShapedGlyph) -> Result<char, TextContourError> {
430 let cluster = usize::try_from(glyph.cluster).map_err(|_| {
431 contour_error(
432 TextContourErrorKind::InvalidInput,
433 "$.glyphs.cluster",
434 "shaped glyph cluster does not fit the host index type",
435 )
436 })?;
437 shaping
438 .text
439 .get(cluster..)
440 .and_then(|suffix| suffix.chars().next())
441 .ok_or_else(|| {
442 contour_error(
443 TextContourErrorKind::InvalidInput,
444 "$.glyphs.cluster",
445 "shaped glyph cluster is outside the source text",
446 )
447 })
448}
449
450fn extract_styled_outline(
456 face: &impl ContourOutlineFace,
457 glyph_id: u32,
458 request: TextContourRequest<'_>,
459) -> Result<Option<crate::FontOutlineOutput>, TextContourError> {
460 match face.extract_glyph(glyph_id) {
461 Ok(mut outline) => {
462 if request.fake_bold || request.fake_italic {
463 if !face.quantizes_cursor_to_internal_units() {
464 return Err(contour_error(
465 TextContourErrorKind::InvalidInput,
466 "$.fake_style",
467 "fake bold/italic synthesis requires the hinted outline path",
468 ));
469 }
470 crate::fake_style::apply_fake_style(
471 &mut outline.commands,
472 request.fake_bold,
473 request.fake_italic,
474 )?;
475 }
476 Ok(Some(outline))
477 }
478 Err(error) if error.kind == FontOutlineErrorKind::MissingOutline => Ok(None),
479 Err(error) => Err(map_outline_error(error)),
480 }
481}
482
483fn advance_cursor(current: f64, advance: f64, to_internal: f64, quantize: bool) -> f64 {
484 let next = current + advance;
485 if quantize {
486 (next * to_internal).trunc() / to_internal
487 } else {
488 next
489 }
490}
491
492#[derive(Clone, Copy)]
493struct PlacementTransform {
494 outline_to_internal: f64,
495 position_x_to_internal: f64,
496 position_y_to_internal: f64,
497 internal_to_x: f64,
498 internal_to_y: f64,
499 origin_x: f64,
500 origin_y: f64,
501}
502
503impl PlacementTransform {
504 fn of_run(
506 face: &impl ContourOutlineFace,
507 request: TextContourRequest<'_>,
508 style: TextRunStyle,
509 ) -> Self {
510 Self {
514 outline_to_internal: face.outline_to_internal(),
515 position_x_to_internal: style.face_scaler() / f64::from(request.shaping.scale_x),
516 position_y_to_internal: style.face_scaler() / f64::from(request.shaping.scale_y),
517 internal_to_x: request.em_x / OUTLINE_FACE_SCALER,
518 internal_to_y: request.em_y / OUTLINE_FACE_SCALER,
519 origin_x: request.origin_x,
520 origin_y: request.origin_y,
521 }
522 }
523}
524
525struct ContourBuilder {
526 contours: Vec<TextContour>,
527 group_sizes: Vec<usize>,
528 group_origins: Vec<TextPoint>,
529 glyph_origin: TextPoint,
531 grouped_contours: usize,
532 current: Vec<TextPoint>,
533 last_outline_point: TextPoint,
534 max_error: f64,
535 transform: PlacementTransform,
536 retained_points: usize,
537 outline_commands: usize,
538 bezier_work_items: usize,
539 peak_temporary_bezier_points: usize,
540}
541
542impl ContourBuilder {
543 fn new(max_error: f64, transform: PlacementTransform) -> Self {
544 Self {
545 contours: Vec::with_capacity(256),
546 group_sizes: Vec::new(),
547 group_origins: Vec::new(),
548 glyph_origin: TextPoint { x: 0.0, y: 0.0 },
549 grouped_contours: 0,
550 current: Vec::new(),
551 last_outline_point: TextPoint { x: 0.0, y: 0.0 },
552 max_error,
553 transform,
554 retained_points: 0,
555 outline_commands: 0,
556 bezier_work_items: 0,
557 peak_temporary_bezier_points: 0,
558 }
559 }
560
561 fn close_group(&mut self) {
563 let size = self.contours.len().saturating_sub(self.grouped_contours);
564 if size > 0 {
565 self.group_sizes.push(size);
566 self.group_origins.push(self.glyph_origin);
567 self.grouped_contours = self.contours.len();
568 }
569 }
570
571 fn charge_commands(&mut self, count: usize) -> Result<(), TextContourError> {
572 self.outline_commands = self.outline_commands.checked_add(count).ok_or_else(|| {
573 resource_error("$.outline_commands", "outline command limit exceeded")
574 })?;
575 Ok(())
576 }
577
578 fn append_outline(
579 &mut self,
580 commands: &[OutlineCommand],
581 position_x: f64,
582 position_y: f64,
583 ) -> Result<(), TextContourError> {
584 for command in commands {
585 match command {
586 OutlineCommand::MoveTo(command) => {
587 self.flush()?;
588 self.last_outline_point = self.outline_point(command.x.get(), command.y.get());
589 self.push_transformed(self.last_outline_point, position_x, position_y)?;
590 }
591 OutlineCommand::LineTo(command) => {
592 self.last_outline_point = self.outline_point(command.x.get(), command.y.get());
593 self.push_transformed(self.last_outline_point, position_x, position_y)?;
594 }
595 OutlineCommand::QuadTo(command) => {
596 let end = self.outline_point(command.x.get(), command.y.get());
597 let control =
598 self.outline_point(command.control_x.get(), command.control_y.get());
599 let flattened = flatten_quadratic_bezier(
600 [self.last_outline_point, control, end],
601 self.max_error,
602 )
603 .map_err(map_bezier_error)?;
604 self.bezier_work_items += flattened.work_items;
605 self.peak_temporary_bezier_points = self
606 .peak_temporary_bezier_points
607 .max(flattened.points.len());
608 for point in flattened.points {
609 self.push_transformed(point, position_x, position_y)?;
610 }
611 self.last_outline_point = end;
612 }
613 OutlineCommand::CurveTo(command) => {
614 let end = self.outline_point(command.x.get(), command.y.get());
615 let control1 =
616 self.outline_point(command.control1_x.get(), command.control1_y.get());
617 let control2 =
618 self.outline_point(command.control2_x.get(), command.control2_y.get());
619 let flattened = flatten_cubic_bezier(
620 [self.last_outline_point, control1, control2, end],
621 self.max_error,
622 )
623 .map_err(map_bezier_error)?;
624 self.bezier_work_items += flattened.work_items;
625 self.peak_temporary_bezier_points = self
626 .peak_temporary_bezier_points
627 .max(flattened.points.len());
628 for point in flattened.points {
629 self.push_transformed(point, position_x, position_y)?;
630 }
631 self.last_outline_point = end;
632 }
633 OutlineCommand::Close(_) => self.flush()?,
634 }
635 }
636 self.flush()
637 }
638
639 fn outline_point(&self, x: f64, y: f64) -> TextPoint {
640 TextPoint {
641 x: x * self.transform.outline_to_internal,
642 y: y * self.transform.outline_to_internal,
643 }
644 }
645
646 fn placed(&self, point: TextPoint, position_x: f64, position_y: f64) -> TextPoint {
649 TextPoint {
650 x: self.transform.origin_x
651 + (point.x + position_x * self.transform.position_x_to_internal)
652 * self.transform.internal_to_x,
653 y: self.transform.origin_y
654 - (point.y + position_y * self.transform.position_y_to_internal)
655 * self.transform.internal_to_y,
656 }
657 }
658
659 fn push_transformed(
660 &mut self,
661 point: TextPoint,
662 position_x: f64,
663 position_y: f64,
664 ) -> Result<(), TextContourError> {
665 let transformed = self.placed(point, position_x, position_y);
666 if !transformed.x.is_finite() || !transformed.y.is_finite() {
667 return Err(contour_error(
668 TextContourErrorKind::InvalidInput,
669 "$.contours",
670 "positioned contour coordinate is not finite",
671 ));
672 }
673 if self.current.last() == Some(&transformed) {
674 return Ok(());
675 }
676 self.current.push(transformed);
677 self.retained_points += 1;
678 Ok(())
679 }
680
681 fn flush(&mut self) -> Result<(), TextContourError> {
682 if self.current.is_empty() {
683 return Ok(());
684 }
685 self.contours.push(TextContour {
686 points: std::mem::take(&mut self.current),
687 });
688 Ok(())
689 }
690
691 fn finish(
692 self,
693 advance_x: f64,
694 advance_y: f64,
695 units_per_em: u16,
696 glyphs: usize,
697 missing_glyphs: usize,
698 outline_less_glyphs: usize,
699 ) -> TextContourOutput {
700 TextContourOutput {
701 contours: self.contours,
702 contour_group_sizes: self.group_sizes,
703 contour_group_origins: self.group_origins,
704 advance_x,
705 advance_y,
706 units_per_em,
707 glyphs,
708 missing_glyphs,
709 outline_less_glyphs,
710 outline_commands: self.outline_commands,
711 bezier_work_items: self.bezier_work_items,
712 peak_temporary_bezier_points: self.peak_temporary_bezier_points,
713 }
714 }
715}
716
717fn validate_request(request: TextContourRequest<'_>) -> Result<(), TextContourError> {
718 if !request.em_x.is_finite()
719 || !request.em_y.is_finite()
720 || !request.origin_x.is_finite()
721 || !request.origin_y.is_finite()
722 || !request.max_error.is_finite()
723 || request.em_x <= 0.0
724 || request.em_y <= 0.0
725 || request.shaping.scale_x <= 0
726 || request.shaping.scale_y <= 0
727 {
728 return Err(contour_error(
729 TextContourErrorKind::InvalidInput,
730 "$",
731 "text contour sizes and scales must be positive; origins and tolerance must be finite",
732 ));
733 }
734 Ok(())
735}
736
737fn map_shaping_error(error: TextShapingError) -> TextContourError {
738 TextContourError {
739 kind: if error.kind == TextShapingErrorKind::ResourceLimit {
740 TextContourErrorKind::ResourceLimit
741 } else {
742 TextContourErrorKind::Shaping
743 },
744 path: error.path,
745 message: error.message,
746 }
747}
748
749fn map_outline_error(error: FontOutlineError) -> TextContourError {
750 TextContourError {
751 kind: if error.kind == FontOutlineErrorKind::ResourceLimit {
752 TextContourErrorKind::ResourceLimit
753 } else {
754 TextContourErrorKind::Outline
755 },
756 path: error.path,
757 message: error.message,
758 }
759}
760
761fn map_bezier_error(error: TextBezierError) -> TextContourError {
762 TextContourError {
763 kind: if error.kind == TextBezierErrorKind::ResourceLimit {
764 TextContourErrorKind::ResourceLimit
765 } else {
766 TextContourErrorKind::InvalidInput
767 },
768 path: "$.contours".to_owned(),
769 message: error.message,
770 }
771}
772
773fn resource_error(path: &'static str, message: &'static str) -> TextContourError {
774 contour_error(TextContourErrorKind::ResourceLimit, path, message)
775}
776
777fn contour_error(
778 kind: TextContourErrorKind,
779 path: impl Into<String>,
780 message: &'static str,
781) -> TextContourError {
782 TextContourError {
783 kind,
784 path: path.into(),
785 message,
786 }
787}
788
789#[cfg(test)]
790mod tests {
791 use super::advance_cursor;
792
793 #[test]
794 fn hinted_cursor_quantizes_each_advance_to_internal_units() {
795 let to_internal = 1433.0 / 2048.0;
796 let first = advance_cursor(0.0, 1366.0, to_internal, true);
797 let second = advance_cursor(first, 1366.0, to_internal, true);
798
799 let expected_first = (1366.0_f64 * to_internal).trunc() / to_internal;
800 let expected_second = ((expected_first + 1366.0) * to_internal).trunc() / to_internal;
801 assert_eq!(first, expected_first);
802 assert_eq!(second, expected_second);
803 assert_ne!(second, advance_cursor(0.0, 2732.0, to_internal, true));
804 }
805
806 #[test]
807 fn hinted_cursor_truncates_negative_internal_positions_toward_zero() {
808 let to_internal = 1433.0 / 2048.0;
809 let position = advance_cursor(0.0, -1366.0, to_internal, true);
810
811 assert_eq!(position, (-1366.0_f64 * to_internal).trunc() / to_internal);
812 assert!(position > -1366.0);
813 }
814
815 #[test]
816 fn unhinted_cursor_preserves_the_shaping_position() {
817 assert_eq!(advance_cursor(12.5, 3.25, 0.75, false), 15.75);
818 }
819}