1use crate::bidi::Direction;
2use crate::unicode::{graphemes, line_breaks, LineBreak, LineBreaks, Script};
3use core::ops::Range;
4
5#[cfg(feature = "complex-shaping")]
6mod complex;
7#[cfg(feature = "complex-shaping")]
8pub use complex::{
9 GlyphBuffer, GlyphMask, LookupRequest, LookupStatus, ScriptProvider, ScriptTypeface,
10 ShapingData,
11};
12
13#[repr(transparent)]
14#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
15pub struct FontId(u64);
16
17impl FontId {
18 pub const fn new(value: u64) -> Self {
19 Self(value)
20 }
21
22 pub const fn value(self) -> u64 {
23 self.0
24 }
25}
26
27#[repr(transparent)]
28#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
29pub struct GlyphId(u16);
30
31impl GlyphId {
32 pub const fn new(value: u16) -> Self {
33 Self(value)
34 }
35
36 pub const fn value(self) -> u16 {
37 self.0
38 }
39}
40
41#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
42pub struct FlowPoint {
43 pub x: i32,
44 pub y: i32,
45}
46
47#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
48pub struct FontMetrics {
49 pub units_per_em: u16,
50 pub ascender: i32,
51 pub descender: i32,
52 pub line_gap: i32,
53}
54
55#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
56pub struct TextRange {
57 pub start: u32,
58 pub end: u32,
59}
60
61impl TextRange {
62 pub const fn new(start: u32, end: u32) -> Self {
63 Self { start, end }
64 }
65
66 pub const fn len(self) -> u32 {
67 self.end.saturating_sub(self.start)
68 }
69
70 pub const fn is_empty(self) -> bool {
71 self.start >= self.end
72 }
73}
74
75#[derive(Clone, Copy, Debug, Eq, PartialEq)]
76pub struct FontFeature {
77 pub tag: [u8; 4],
78 pub value: u32,
79 pub range: TextRange,
80}
81
82#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
83pub struct LineEdges(u8);
84
85impl LineEdges {
86 pub const NONE: Self = Self(0);
87 pub const START: Self = Self(1);
88 pub const END: Self = Self(2);
89 pub const BOTH: Self = Self(3);
90
91 pub const fn has_start(self) -> bool {
92 self.0 & Self::START.0 != 0
93 }
94
95 pub const fn has_end(self) -> bool {
96 self.0 & Self::END.0 != 0
97 }
98}
99
100impl FontFeature {
101 pub const fn new(tag: [u8; 4], value: u32) -> Self {
102 Self {
103 tag,
104 value,
105 range: TextRange::new(0, u32::MAX),
106 }
107 }
108
109 pub const fn with_range(mut self, range: TextRange) -> Self {
110 self.range = range;
111 self
112 }
113}
114
115pub struct ShapeRequest<'a> {
116 pub text: &'a str,
117 pub range: Range<usize>,
118 pub direction: Direction,
119 pub script: Script,
120 pub language: Option<&'a str>,
121 pub features: &'a [FontFeature],
122 pub line_edges: LineEdges,
123}
124
125impl<'a> ShapeRequest<'a> {
126 pub fn new(text: &'a str, range: Range<usize>, direction: Direction, script: Script) -> Self {
127 Self {
128 text,
129 range,
130 direction,
131 script,
132 language: None,
133 features: &[],
134 line_edges: LineEdges::NONE,
135 }
136 }
137
138 pub fn with_language(mut self, language: &'a str) -> Self {
139 self.language = Some(language);
140 self
141 }
142
143 pub fn with_features(mut self, features: &'a [FontFeature]) -> Self {
144 self.features = features;
145 self
146 }
147
148 pub fn with_line_edges(mut self, edges: LineEdges) -> Self {
149 self.line_edges = edges;
150 self
151 }
152}
153
154#[derive(Clone, Copy, Debug, Eq, PartialEq)]
155pub enum ShapeError {
156 InvalidTextRange,
157 InvalidGlyphRange,
158 TextTooLong,
159 InsufficientCapacity { required: usize },
160 UnsupportedScript { script: Script },
161 UnsupportedFeature { tag: [u8; 4] },
162 UnsupportedCluster { offset: usize },
163 MissingGlyph { offset: usize },
164 ShapingUnavailable,
165 Source(FontAccessError),
166}
167
168#[derive(Clone, Copy, Debug, Eq, PartialEq)]
169pub enum FontAccessError {
170 Unavailable,
171 Malformed,
172 Unsupported,
173}
174
175impl From<FontAccessError> for ShapeError {
176 fn from(error: FontAccessError) -> Self {
177 Self::Source(error)
178 }
179}
180
181#[repr(C)]
182#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
183pub struct ShapedGlyph {
184 glyph_id: GlyphId,
185 flags: u16,
186 pub cluster: TextRange,
187 pub advance: FlowPoint,
188 pub offset: FlowPoint,
189}
190
191impl ShapedGlyph {
192 const UNSAFE_TO_BREAK: u16 = 1;
193
194 pub const fn new(glyph_id: GlyphId, cluster: TextRange) -> Self {
195 Self {
196 glyph_id,
197 flags: 0,
198 cluster,
199 advance: FlowPoint { x: 0, y: 0 },
200 offset: FlowPoint { x: 0, y: 0 },
201 }
202 }
203
204 pub const fn glyph_id(self) -> GlyphId {
205 self.glyph_id
206 }
207
208 pub const fn unsafe_to_break(self) -> bool {
209 self.flags & Self::UNSAFE_TO_BREAK != 0
210 }
211
212 pub fn set_unsafe_to_break(&mut self, unsafe_to_break: bool) {
213 if unsafe_to_break {
214 self.flags |= Self::UNSAFE_TO_BREAK;
215 } else {
216 self.flags &= !Self::UNSAFE_TO_BREAK;
217 }
218 }
219}
220
221pub struct ShapedRun<'a> {
222 font_id: FontId,
223 text: TextRange,
224 bidi_level: u8,
225 glyphs: &'a [ShapedGlyph],
226}
227
228impl<'a> ShapedRun<'a> {
229 pub const fn new(
230 font_id: FontId,
231 text: TextRange,
232 bidi_level: u8,
233 glyphs: &'a [ShapedGlyph],
234 ) -> Self {
235 Self {
236 font_id,
237 text,
238 bidi_level,
239 glyphs,
240 }
241 }
242
243 pub const fn font_id(&self) -> FontId {
244 self.font_id
245 }
246
247 pub const fn text(&self) -> TextRange {
248 self.text
249 }
250
251 pub const fn direction(&self) -> Direction {
252 Direction::from_level(self.bidi_level)
253 }
254
255 pub const fn bidi_level(&self) -> u8 {
256 self.bidi_level
257 }
258
259 pub const fn glyphs(&self) -> &[ShapedGlyph] {
260 self.glyphs
261 }
262
263 pub fn is_safe_break(&self, offset: u32) -> bool {
264 if offset == self.text.start || offset == self.text.end {
265 return true;
266 }
267 if offset < self.text.start || offset > self.text.end {
268 return false;
269 }
270 !self.glyphs.iter().any(|glyph| {
271 glyph.cluster.start < offset && offset < glyph.cluster.end
272 || glyph.cluster.start == offset && glyph.unsafe_to_break()
273 })
274 }
275
276 pub fn line_breaks<'text, 'run>(
277 &'run self,
278 text: &'text str,
279 ) -> Result<SafeLineBreaks<'text, 'run, 'a>, ShapeError> {
280 let start = self.text.start as usize;
281 let end = self.text.end as usize;
282 if start > end
283 || end > text.len()
284 || !text.is_char_boundary(start)
285 || !text.is_char_boundary(end)
286 {
287 return Err(ShapeError::InvalidTextRange);
288 }
289 Ok(SafeLineBreaks {
290 inner: line_breaks(&text[start..end]),
291 run: self,
292 base: self.text.start,
293 })
294 }
295
296 pub fn position_into<'output>(
297 &self,
298 origin: FlowPoint,
299 output: &'output mut [PositionedGlyph],
300 ) -> Result<PositionedRun<'output>, PositionError> {
301 if output.len() < self.glyphs.len() {
302 return Err(PositionError::InsufficientCapacity {
303 required: self.glyphs.len(),
304 });
305 }
306 let mut pen = origin;
307 for (slot, glyph) in output.iter_mut().zip(self.glyphs) {
308 *slot = PositionedGlyph {
309 glyph_id: glyph.glyph_id(),
310 cluster: glyph.cluster,
311 origin: pen,
312 advance: glyph.advance,
313 offset: glyph.offset,
314 bidi_level: self.bidi_level,
315 };
316 pen.x = pen
317 .x
318 .checked_add(glyph.advance.x)
319 .ok_or(PositionError::CoordinateOverflow)?;
320 pen.y = pen
321 .y
322 .checked_add(glyph.advance.y)
323 .ok_or(PositionError::CoordinateOverflow)?;
324 }
325 Ok(PositionedRun {
326 font_id: self.font_id,
327 text: self.text,
328 direction: self.direction(),
329 bidi_level: self.bidi_level,
330 end: pen,
331 glyphs: &output[..self.glyphs.len()],
332 })
333 }
334}
335
336pub struct SafeLineBreaks<'text, 'run, 'glyph> {
337 inner: LineBreaks<'text>,
338 run: &'run ShapedRun<'glyph>,
339 base: u32,
340}
341
342impl Iterator for SafeLineBreaks<'_, '_, '_> {
343 type Item = LineBreak;
344
345 fn next(&mut self) -> Option<Self::Item> {
346 loop {
347 let mut next = self.inner.next()?;
348 next.offset += self.base as usize;
349 if self.run.is_safe_break(next.offset as u32) {
350 return Some(next);
351 }
352 }
353 }
354}
355
356#[derive(Clone, Copy, Debug, Eq, PartialEq)]
357pub enum PositionError {
358 InsufficientCapacity { required: usize },
359 CoordinateOverflow,
360}
361
362#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
363pub struct PositionedGlyph {
364 glyph_id: GlyphId,
365 pub cluster: TextRange,
366 pub origin: FlowPoint,
367 pub advance: FlowPoint,
368 pub offset: FlowPoint,
369 pub bidi_level: u8,
370}
371
372impl PositionedGlyph {
373 pub const fn new(glyph_id: GlyphId, origin: FlowPoint) -> Self {
374 Self {
375 glyph_id,
376 cluster: TextRange::new(0, 0),
377 origin,
378 advance: FlowPoint { x: 0, y: 0 },
379 offset: FlowPoint { x: 0, y: 0 },
380 bidi_level: 0,
381 }
382 }
383
384 pub const fn glyph_id(self) -> GlyphId {
385 self.glyph_id
386 }
387
388 pub const fn with_cluster(mut self, cluster: TextRange) -> Self {
389 self.cluster = cluster;
390 self
391 }
392
393 pub const fn with_advance(mut self, advance: FlowPoint) -> Self {
394 self.advance = advance;
395 self
396 }
397
398 pub const fn with_offset(mut self, offset: FlowPoint) -> Self {
399 self.offset = offset;
400 self
401 }
402
403 pub const fn with_bidi_level(mut self, bidi_level: u8) -> Self {
404 self.bidi_level = bidi_level;
405 self
406 }
407}
408
409pub struct PositionedRun<'a> {
410 font_id: FontId,
411 text: TextRange,
412 direction: Direction,
413 bidi_level: u8,
414 end: FlowPoint,
415 glyphs: &'a [PositionedGlyph],
416}
417
418impl PositionedRun<'_> {
419 pub const fn font_id(&self) -> FontId {
420 self.font_id
421 }
422
423 pub const fn text(&self) -> TextRange {
424 self.text
425 }
426
427 pub const fn direction(&self) -> Direction {
428 self.direction
429 }
430
431 pub const fn end(&self) -> FlowPoint {
432 self.end
433 }
434
435 pub const fn glyphs(&self) -> &[PositionedGlyph] {
436 self.glyphs
437 }
438
439 pub fn carets_into<'output>(
440 &self,
441 output: &'output mut [CaretStop],
442 ) -> Result<&'output [CaretStop], PositionError> {
443 let mut required = 0;
444 let mut previous = None;
445 for glyph in self.glyphs {
446 let text_offset = match self.direction {
447 Direction::LeftToRight => glyph.cluster.start,
448 Direction::RightToLeft => glyph.cluster.end,
449 };
450 if previous != Some(text_offset) {
451 required += 1;
452 previous = Some(text_offset);
453 }
454 }
455 let final_offset = match self.direction {
456 Direction::LeftToRight => self.text.end,
457 Direction::RightToLeft => self.text.start,
458 };
459 if previous != Some(final_offset) {
460 required += 1;
461 }
462 if output.len() < required {
463 return Err(PositionError::InsufficientCapacity { required });
464 }
465 let mut count = 0;
466 for glyph in self.glyphs {
467 let text_offset = match self.direction {
468 Direction::LeftToRight => glyph.cluster.start,
469 Direction::RightToLeft => glyph.cluster.end,
470 };
471 if count == 0 || output[count - 1].text_offset != text_offset {
472 output[count] = CaretStop {
473 text_offset,
474 position: glyph.origin,
475 bidi_level: self.bidi_level,
476 };
477 count += 1;
478 }
479 }
480 let text_offset = match self.direction {
481 Direction::LeftToRight => self.text.end,
482 Direction::RightToLeft => self.text.start,
483 };
484 if count == 0 || output[count - 1].text_offset != text_offset {
485 output[count] = CaretStop {
486 text_offset,
487 position: self.end,
488 bidi_level: self.bidi_level,
489 };
490 count += 1;
491 }
492 Ok(&output[..count])
493 }
494}
495
496#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
497pub struct CaretStop {
498 pub text_offset: u32,
499 pub position: FlowPoint,
500 pub bidi_level: u8,
501}
502
503pub trait Typeface {
504 fn id(&self) -> FontId;
505 fn metrics(&self) -> Result<FontMetrics, FontAccessError>;
506 fn covers(&self, grapheme: &str) -> Result<bool, FontAccessError>;
507 fn supports_complex_shaping(&self) -> bool {
508 true
509 }
510 fn shape_into(
511 &self,
512 request: &ShapeRequest<'_>,
513 output: &mut [ShapedGlyph],
514 ) -> Result<usize, ShapeError>;
515}
516
517pub trait GlyphSource {
518 fn id(&self) -> FontId;
519 fn metrics(&self) -> Result<FontMetrics, FontAccessError>;
520 fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError>;
521 fn glyph_advance(&self, glyph: GlyphId) -> Result<FlowPoint, FontAccessError>;
522
523 fn notdef_glyph(&self) -> Result<Option<GlyphId>, FontAccessError> {
524 Ok(Some(GlyphId::new(0)))
525 }
526
527 fn kerning(&self, _left: GlyphId, _right: GlyphId) -> Result<i32, FontAccessError> {
528 Ok(0)
529 }
530}
531
532pub struct SimpleTypeface<'a, T: ?Sized> {
533 source: &'a T,
534}
535
536impl<'a, T: ?Sized> SimpleTypeface<'a, T> {
537 pub const fn new(source: &'a T) -> Self {
538 Self { source }
539 }
540}
541
542impl<T: ?Sized> Typeface for SimpleTypeface<'_, T>
543where
544 T: GlyphSource,
545{
546 fn id(&self) -> FontId {
547 self.source.id()
548 }
549
550 fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
551 self.source.metrics()
552 }
553
554 fn covers(&self, grapheme: &str) -> Result<bool, FontAccessError> {
555 let mut characters = grapheme.chars();
556 let Some(character) = characters.next() else {
557 return Ok(false);
558 };
559 if characters.next().is_some() {
560 return Ok(false);
561 }
562 Ok(self.source.glyph_for(character)?.is_some())
563 }
564
565 fn supports_complex_shaping(&self) -> bool {
566 false
567 }
568
569 fn shape_into(
570 &self,
571 request: &ShapeRequest<'_>,
572 output: &mut [ShapedGlyph],
573 ) -> Result<usize, ShapeError> {
574 validate_request(request)?;
575 if matches!(request.script, Script::Arabic | Script::Devanagari) {
576 return Err(ShapeError::UnsupportedScript {
577 script: request.script,
578 });
579 }
580 let kern = kerning_enabled(request.features)?;
581 let text = &request.text[request.range.clone()];
582 let required = graphemes(text).count();
583 if output.len() < required {
584 return Err(ShapeError::InsufficientCapacity { required });
585 }
586
587 let mut previous: Option<(usize, GlyphId)> = None;
588 for (logical_index, cluster) in graphemes(text).enumerate() {
589 let mut characters = cluster.text.chars();
590 let character = characters.next().unwrap();
591 if characters.next().is_some() {
592 return Err(ShapeError::UnsupportedCluster {
593 offset: request.range.start + cluster.range.start,
594 });
595 }
596 let glyph_id = match self.source.glyph_for(character)? {
597 Some(glyph) => glyph,
598 None => self
599 .source
600 .notdef_glyph()?
601 .ok_or(ShapeError::MissingGlyph {
602 offset: request.range.start + cluster.range.start,
603 })?,
604 };
605 let output_index = match request.direction {
606 Direction::LeftToRight => logical_index,
607 Direction::RightToLeft => required - logical_index - 1,
608 };
609 let start = request.range.start + cluster.range.start;
610 let end = request.range.start + cluster.range.end;
611 output[output_index] = ShapedGlyph {
612 glyph_id,
613 flags: 0,
614 cluster: TextRange::new(start as u32, end as u32),
615 advance: self.source.glyph_advance(glyph_id)?,
616 offset: FlowPoint::default(),
617 };
618 if kern {
619 if let Some((previous_index, previous_glyph)) = previous {
620 output[previous_index].advance.x +=
621 self.source.kerning(previous_glyph, glyph_id)?;
622 }
623 previous = Some((output_index, glyph_id));
624 }
625 }
626 Ok(required)
627 }
628}
629
630fn validate_request(request: &ShapeRequest<'_>) -> Result<(), ShapeError> {
631 if request.range.start > request.range.end
632 || request.range.end > request.text.len()
633 || !request.text.is_char_boundary(request.range.start)
634 || !request.text.is_char_boundary(request.range.end)
635 {
636 return Err(ShapeError::InvalidTextRange);
637 }
638 if request.text.len() > u32::MAX as usize {
639 return Err(ShapeError::TextTooLong);
640 }
641 Ok(())
642}
643
644fn kerning_enabled(features: &[FontFeature]) -> Result<bool, ShapeError> {
645 let mut enabled = true;
646 for feature in features {
647 if feature.tag == *b"kern" {
648 enabled = feature.value != 0;
649 } else {
650 return Err(ShapeError::UnsupportedFeature { tag: feature.tag });
651 }
652 }
653 Ok(enabled)
654}
655
656#[cfg(test)]
657mod tests {
658 use super::*;
659 use std::prelude::v1::*;
660
661 #[test]
662 fn positioned_glyph_builder_preserves_static_placement() {
663 let glyph = PositionedGlyph::new(GlyphId::new(7), FlowPoint { x: 10, y: 20 })
664 .with_cluster(TextRange::new(2, 4))
665 .with_advance(FlowPoint { x: 8, y: 0 })
666 .with_offset(FlowPoint { x: 1, y: -2 })
667 .with_bidi_level(1);
668
669 assert_eq!(glyph.glyph_id(), GlyphId::new(7));
670 assert_eq!(glyph.cluster, TextRange::new(2, 4));
671 assert_eq!(glyph.origin, FlowPoint { x: 10, y: 20 });
672 assert_eq!(glyph.advance, FlowPoint { x: 8, y: 0 });
673 assert_eq!(glyph.offset, FlowPoint { x: 1, y: -2 });
674 assert_eq!(glyph.bidi_level, 1);
675 }
676
677 struct MockFont;
678
679 impl GlyphSource for MockFont {
680 fn id(&self) -> FontId {
681 FontId::new(4)
682 }
683
684 fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
685 Ok(FontMetrics {
686 units_per_em: 1000,
687 ascender: 800,
688 descender: -200,
689 line_gap: 0,
690 })
691 }
692
693 fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError> {
694 Ok(character
695 .is_ascii()
696 .then_some(GlyphId::new(character as u16)))
697 }
698
699 fn glyph_advance(&self, _glyph: GlyphId) -> Result<FlowPoint, FontAccessError> {
700 Ok(FlowPoint { x: 600, y: 0 })
701 }
702
703 fn kerning(&self, left: GlyphId, right: GlyphId) -> Result<i32, FontAccessError> {
704 Ok(
705 if left.value() == b'A' as u16 && right.value() == b'V' as u16 {
706 -80
707 } else {
708 0
709 },
710 )
711 }
712 }
713
714 #[test]
715 fn shapes_into_caller_storage_with_kerning() {
716 let face = SimpleTypeface::new(&MockFont);
717 assert!(!face.supports_complex_shaping());
718 let text = "AV";
719 let mut output = [ShapedGlyph::default(); 2];
720 let count = face
721 .shape_into(
722 &ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Latin),
723 &mut output,
724 )
725 .unwrap();
726
727 assert_eq!(count, 2);
728 assert_eq!(output[0].advance.x, 520);
729 assert_eq!(output[1].advance.x, 600);
730 }
731
732 #[test]
733 fn reverses_rtl_output_without_heap_storage() {
734 let face = SimpleTypeface::new(&MockFont);
735 let text = "abc";
736 let mut output = [ShapedGlyph::default(); 3];
737 face.shape_into(
738 &ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Hebrew),
739 &mut output,
740 )
741 .unwrap();
742
743 assert_eq!(output[0].glyph_id(), GlyphId::new(b'c' as u16));
744 assert_eq!(output[2].glyph_id(), GlyphId::new(b'a' as u16));
745 }
746
747 #[test]
748 fn reports_capacity_and_unsupported_clusters() {
749 let face = SimpleTypeface::new(&MockFont);
750 let mut output = [ShapedGlyph::default(); 1];
751 let text = "ab";
752 assert_eq!(
753 face.shape_into(
754 &ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Latin,),
755 &mut output,
756 ),
757 Err(ShapeError::InsufficientCapacity { required: 2 })
758 );
759 let combined = "a\u{301}";
760 assert_eq!(
761 face.shape_into(
762 &ShapeRequest::new(
763 combined,
764 0..combined.len(),
765 Direction::LeftToRight,
766 Script::Latin,
767 ),
768 &mut output,
769 ),
770 Err(ShapeError::UnsupportedCluster { offset: 0 })
771 );
772 }
773
774 #[test]
775 fn run_rejects_breaks_inside_or_before_unsafe_clusters() {
776 let mut glyphs = [
777 ShapedGlyph::new(GlyphId::new(1), TextRange::new(0, 3)),
778 ShapedGlyph::new(GlyphId::new(2), TextRange::new(3, 4)),
779 ];
780 glyphs[1].set_unsafe_to_break(true);
781 let run = ShapedRun::new(FontId::new(4), TextRange::new(0, 4), 0, &glyphs);
782
783 assert!(run.is_safe_break(0));
784 assert!(!run.is_safe_break(1));
785 assert!(!run.is_safe_break(3));
786 assert!(run.is_safe_break(4));
787 assert!(!run.is_safe_break(5));
788 }
789
790 #[test]
791 fn run_filters_unsafe_unicode_breaks() {
792 let text = "hello world";
793 let mut glyphs = [ShapedGlyph::default(); 11];
794 for (index, glyph) in glyphs.iter_mut().enumerate() {
795 *glyph = ShapedGlyph::new(
796 GlyphId::new(index as u16),
797 TextRange::new(index as u32, index as u32 + 1),
798 );
799 }
800 glyphs[6].set_unsafe_to_break(true);
801 let run = ShapedRun::new(
802 FontId::new(4),
803 TextRange::new(0, text.len() as u32),
804 0,
805 &glyphs,
806 );
807 let breaks = run.line_breaks(text).unwrap().collect::<Vec<_>>();
808
809 assert_eq!(breaks.len(), 1);
810 assert_eq!(breaks[0].offset, text.len());
811 }
812
813 #[test]
814 fn positions_glyphs_and_emits_visual_carets() {
815 let mut glyphs = [
816 ShapedGlyph::new(GlyphId::new(1), TextRange::new(0, 1)),
817 ShapedGlyph::new(GlyphId::new(2), TextRange::new(1, 2)),
818 ];
819 glyphs[0].advance.x = 3;
820 glyphs[1].advance.x = 4;
821 let run = ShapedRun::new(FontId::new(4), TextRange::new(0, 2), 0, &glyphs);
822 let mut positioned = [PositionedGlyph::default(); 2];
823 let positioned = run
824 .position_into(FlowPoint { x: 10, y: 20 }, &mut positioned)
825 .unwrap();
826
827 assert_eq!(positioned.glyphs()[0].origin, FlowPoint { x: 10, y: 20 });
828 assert_eq!(positioned.glyphs()[1].origin, FlowPoint { x: 13, y: 20 });
829 assert_eq!(positioned.end(), FlowPoint { x: 17, y: 20 });
830
831 let mut carets = [CaretStop::default(); 3];
832 let carets = positioned.carets_into(&mut carets).unwrap();
833 assert_eq!(carets[0].text_offset, 0);
834 assert_eq!(carets[1].text_offset, 1);
835 assert_eq!(carets[2].text_offset, 2);
836 assert_eq!(carets[2].position.x, 17);
837 }
838
839 #[test]
840 fn request_carries_explicit_line_edges() {
841 let request = ShapeRequest::new("a", 0..1, Direction::LeftToRight, Script::Latin)
842 .with_line_edges(LineEdges::BOTH);
843
844 assert!(request.line_edges.has_start());
845 assert!(request.line_edges.has_end());
846 }
847
848 #[test]
849 fn uses_notdef_for_an_uncovered_scalar() {
850 let face = SimpleTypeface::new(&MockFont);
851 let text = "δΈ–";
852 let mut output = [ShapedGlyph::default(); 1];
853 let count = face
854 .shape_into(
855 &ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Han),
856 &mut output,
857 )
858 .unwrap();
859
860 assert_eq!(count, 1);
861 assert_eq!(output[0].glyph_id(), GlyphId::new(0));
862 }
863}