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