1use super::{
2 FontFeature, GlyphBuffer, GlyphMask, LookupRequest, LookupStatus, ScriptProvider, ShapeError,
3 ShapeRequest, TextRange,
4};
5use crate::unicode::{graphemes, Script};
6
7const ALL: GlyphMask = GlyphMask::ALL;
8
9fn enabled(request: &ShapeRequest<'_>, tag: [u8; 4], default: bool) -> bool {
10 request
11 .features
12 .iter()
13 .filter(|feature| feature.tag == tag && overlaps(feature, request))
14 .fold(default, |_, feature| feature.value != 0)
15}
16
17fn overlaps(feature: &FontFeature, request: &ShapeRequest<'_>) -> bool {
18 feature.range.end > request.range.start as u32 && feature.range.start < request.range.end as u32
19}
20
21fn substitute(
22 request: &ShapeRequest<'_>,
23 font: &dyn super::ShapingData,
24 glyphs: &mut GlyphBuffer<'_>,
25 tag: [u8; 4],
26 mask: GlyphMask,
27 default: bool,
28) -> Result<LookupStatus, ShapeError> {
29 if !enabled(request, tag, default) {
30 return Ok(LookupStatus::NotFound);
31 }
32 font.substitute(LookupRequest::new(request, tag, mask), glyphs)
33}
34
35fn position(
36 request: &ShapeRequest<'_>,
37 font: &dyn super::ShapingData,
38 glyphs: &mut GlyphBuffer<'_>,
39 tag: [u8; 4],
40 default: bool,
41) -> Result<LookupStatus, ShapeError> {
42 if !enabled(request, tag, default) {
43 return Ok(LookupStatus::NotFound);
44 }
45 font.position(LookupRequest::new(request, tag, ALL), glyphs)
46}
47
48fn normalize_clusters(request: &ShapeRequest<'_>, glyphs: &mut GlyphBuffer<'_>) {
49 let text = &request.text[request.range.clone()];
50 for cluster in graphemes(text) {
51 let range = TextRange::new(
52 (request.range.start + cluster.range.start) as u32,
53 (request.range.start + cluster.range.end) as u32,
54 );
55 let mut matched = 0;
56 for glyph in glyphs.glyphs_mut() {
57 if glyph.cluster.start >= range.start && glyph.cluster.start < range.end {
58 glyph.cluster = range;
59 matched += 1;
60 }
61 }
62 if matched > 1 {
63 for glyph in glyphs.glyphs_mut() {
64 if glyph.cluster == range {
65 glyph.set_unsafe_to_break(true);
66 }
67 }
68 }
69 }
70}
71
72#[cfg(feature = "script-arabic")]
73pub const ARABIC: Arabic = Arabic;
75
76#[cfg(feature = "script-arabic")]
77pub struct Arabic;
79
80#[cfg(feature = "script-arabic")]
81const ISOL: GlyphMask = GlyphMask::new(1 << 0);
82#[cfg(feature = "script-arabic")]
83const INIT: GlyphMask = GlyphMask::new(1 << 1);
84#[cfg(feature = "script-arabic")]
85const MEDI: GlyphMask = GlyphMask::new(1 << 2);
86#[cfg(feature = "script-arabic")]
87const FINA: GlyphMask = GlyphMask::new(1 << 3);
88
89#[cfg(feature = "script-arabic")]
90#[derive(Clone, Copy, Eq, PartialEq)]
91enum Joining {
92 None,
93 Right,
94 Dual,
95 Transparent,
96}
97
98#[cfg(feature = "script-arabic")]
99impl Joining {
100 const fn joins_previous(self) -> bool {
101 matches!(self, Self::Right | Self::Dual)
102 }
103
104 const fn joins_next(self) -> bool {
105 matches!(self, Self::Dual)
106 }
107}
108
109#[cfg(feature = "script-arabic")]
110fn joining(character: char) -> Joining {
111 let scalar = character as u32;
112 if matches!(
113 scalar,
114 0x0610..=0x061A | 0x064B..=0x065F | 0x0670 | 0x06D6..=0x06ED | 0x08D3..=0x08FF
115 ) {
116 return Joining::Transparent;
117 }
118 if matches!(
119 scalar,
120 0x0622..=0x0625
121 | 0x0627
122 | 0x0629
123 | 0x062F..=0x0632
124 | 0x0648
125 | 0x0671..=0x0673
126 | 0x0675..=0x0677
127 | 0x0688..=0x0699
128 | 0x06C0
129 | 0x06C3..=0x06CB
130 | 0x06CD
131 | 0x06CF
132 | 0x06D2..=0x06D3
133 ) {
134 return Joining::Right;
135 }
136 if matches!(
137 scalar,
138 0x0626
139 | 0x0628
140 | 0x062A..=0x062E
141 | 0x0633..=0x063A
142 | 0x0641..=0x0647
143 | 0x0649..=0x064A
144 | 0x066E..=0x066F
145 | 0x0678..=0x0687
146 | 0x069A..=0x06BF
147 | 0x06CC
148 | 0x06CE
149 | 0x06D0..=0x06D1
150 | 0x06FA..=0x06FC
151 ) {
152 return Joining::Dual;
153 }
154 Joining::None
155}
156
157#[cfg(feature = "script-arabic")]
158fn character_at(
159 request: &ShapeRequest<'_>,
160 glyphs: &GlyphBuffer<'_>,
161 index: usize,
162) -> Option<char> {
163 let start = usize::try_from(glyphs.get(index)?.cluster.start).ok()?;
164 request.text.get(start..)?.chars().next()
165}
166
167#[cfg(feature = "script-arabic")]
168fn neighbor(
169 request: &ShapeRequest<'_>,
170 glyphs: &GlyphBuffer<'_>,
171 index: usize,
172 previous: bool,
173) -> Option<Joining> {
174 if previous {
175 for slot in (0..index).rev() {
176 let class = joining(character_at(request, glyphs, slot)?);
177 if class != Joining::Transparent {
178 return Some(class);
179 }
180 }
181 } else {
182 for slot in index + 1..glyphs.len() {
183 let class = joining(character_at(request, glyphs, slot)?);
184 if class != Joining::Transparent {
185 return Some(class);
186 }
187 }
188 }
189 None
190}
191
192#[cfg(feature = "script-arabic")]
193fn assign_joining_masks(
194 request: &ShapeRequest<'_>,
195 glyphs: &mut GlyphBuffer<'_>,
196) -> Result<bool, ShapeError> {
197 let mut joined = false;
198 for index in 0..glyphs.len() {
199 let current =
200 joining(character_at(request, glyphs, index).ok_or(ShapeError::InvalidGlyphRange)?);
201 if current == Joining::Transparent {
202 glyphs.set_mask(index, GlyphMask::NONE)?;
203 continue;
204 }
205 let previous = neighbor(request, glyphs, index, true)
206 .is_some_and(|class| class.joins_next() && current.joins_previous());
207 let next = neighbor(request, glyphs, index, false)
208 .is_some_and(|class| current.joins_next() && class.joins_previous());
209 let mask = match (previous, next) {
210 (false, false) => ISOL,
211 (false, true) => INIT,
212 (true, true) => MEDI,
213 (true, false) => FINA,
214 };
215 joined |= previous || next;
216 if previous || next {
217 glyphs
218 .get_mut(index)
219 .expect("valid joining slot")
220 .set_unsafe_to_break(true);
221 }
222 glyphs.set_mask(index, mask)?;
223 }
224 Ok(joined)
225}
226
227#[cfg(feature = "script-arabic")]
228impl ScriptProvider for Arabic {
229 fn script(&self) -> Script {
230 Script::Arabic
231 }
232
233 fn substitute(
234 &self,
235 request: &ShapeRequest<'_>,
236 font: &dyn super::ShapingData,
237 glyphs: &mut GlyphBuffer<'_>,
238 ) -> Result<(), ShapeError> {
239 let joined = assign_joining_masks(request, glyphs)?;
240 normalize_clusters(request, glyphs);
241 substitute(request, font, glyphs, *b"ccmp", ALL, true)?;
242 substitute(request, font, glyphs, *b"locl", ALL, true)?;
243 let mut forms = 0;
244 for (tag, mask) in [
245 (*b"isol", ISOL),
246 (*b"fina", FINA),
247 (*b"medi", MEDI),
248 (*b"init", INIT),
249 ] {
250 forms += usize::from(
251 substitute(request, font, glyphs, tag, mask, true)? == LookupStatus::Applied,
252 );
253 }
254 if joined && forms == 0 {
255 return Err(ShapeError::ShapingUnavailable);
256 }
257 for tag in [*b"rlig", *b"calt", *b"liga"] {
258 substitute(request, font, glyphs, tag, ALL, true)?;
259 }
260 Ok(())
261 }
262
263 fn position(
264 &self,
265 request: &ShapeRequest<'_>,
266 font: &dyn super::ShapingData,
267 glyphs: &mut GlyphBuffer<'_>,
268 ) -> Result<(), ShapeError> {
269 position(request, font, glyphs, *b"curs", true)?;
270 position(request, font, glyphs, *b"kern", true)?;
271 let has_marks = request.text[request.range.clone()]
272 .chars()
273 .any(|character| joining(character) == Joining::Transparent);
274 let mark = position(request, font, glyphs, *b"mark", true)?;
275 let mkmk = position(request, font, glyphs, *b"mkmk", true)?;
276 if has_marks && mark == LookupStatus::NotFound && mkmk == LookupStatus::NotFound {
277 return Err(ShapeError::ShapingUnavailable);
278 }
279 Ok(())
280 }
281}
282
283#[cfg(feature = "script-thai")]
284pub const THAI: Thai = Thai;
286
287#[cfg(feature = "script-thai")]
288pub struct Thai;
290
291#[cfg(feature = "script-thai")]
292fn thai_mark(character: char) -> bool {
293 matches!(character as u32, 0x0E31 | 0x0E34..=0x0E3A | 0x0E47..=0x0E4E)
294}
295
296#[cfg(feature = "script-thai")]
297impl ScriptProvider for Thai {
298 fn script(&self) -> Script {
299 Script::Thai
300 }
301
302 fn substitute(
303 &self,
304 request: &ShapeRequest<'_>,
305 font: &dyn super::ShapingData,
306 glyphs: &mut GlyphBuffer<'_>,
307 ) -> Result<(), ShapeError> {
308 normalize_clusters(request, glyphs);
309 for tag in [*b"ccmp", *b"locl", *b"liga"] {
310 substitute(request, font, glyphs, tag, ALL, true)?;
311 }
312 Ok(())
313 }
314
315 fn position(
316 &self,
317 request: &ShapeRequest<'_>,
318 font: &dyn super::ShapingData,
319 glyphs: &mut GlyphBuffer<'_>,
320 ) -> Result<(), ShapeError> {
321 position(request, font, glyphs, *b"kern", true)?;
322 let has_marks = request.text[request.range.clone()].chars().any(thai_mark);
323 let mark = position(request, font, glyphs, *b"mark", true)?;
324 let mkmk = position(request, font, glyphs, *b"mkmk", true)?;
325 if has_marks && mark == LookupStatus::NotFound && mkmk == LookupStatus::NotFound {
326 return Err(ShapeError::ShapingUnavailable);
327 }
328 Ok(())
329 }
330}
331
332#[cfg(feature = "script-devanagari")]
333pub const DEVANAGARI: Devanagari = Devanagari;
335
336#[cfg(feature = "script-devanagari")]
337pub struct Devanagari;
339
340#[cfg(feature = "script-devanagari")]
341fn devanagari_consonant(character: char) -> bool {
342 matches!(
343 character as u32,
344 0x0915..=0x0939 | 0x0958..=0x095F | 0x0978..=0x097F
345 )
346}
347
348#[cfg(feature = "script-devanagari")]
349fn devanagari_mark(character: char) -> bool {
350 matches!(
351 character as u32,
352 0x0900..=0x0903
353 | 0x093A..=0x094C
354 | 0x094E..=0x0957
355 | 0x0962..=0x0963
356 | 0xA8E0..=0xA8F1
357 )
358}
359
360#[cfg(feature = "script-devanagari")]
361fn glyph_at_text_offset(glyphs: &GlyphBuffer<'_>, offset: u32) -> Option<usize> {
362 glyphs
363 .glyphs()
364 .iter()
365 .position(|glyph| glyph.cluster.start <= offset && offset < glyph.cluster.end)
366}
367
368#[cfg(feature = "script-devanagari")]
369fn reorder_devanagari_prebase_matra(request: &ShapeRequest<'_>, glyphs: &mut GlyphBuffer<'_>) {
370 let mut base = None;
371 let mut previous = None;
372 for (relative, character) in request.text[request.range.clone()].char_indices() {
373 let offset = (request.range.start + relative) as u32;
374 if devanagari_consonant(character) {
375 if character != '\u{0930}' || previous != Some('\u{094D}') {
376 base = Some(offset);
377 }
378 previous = Some(character);
379 continue;
380 }
381 if character != '\u{093F}' {
382 previous = Some(character);
383 continue;
384 }
385 let (Some(base_index), Some(matra_index)) = (
386 base.and_then(|base| glyph_at_text_offset(glyphs, base)),
387 glyph_at_text_offset(glyphs, offset),
388 ) else {
389 continue;
390 };
391 if base_index < matra_index {
392 glyphs.glyphs_mut()[base_index..=matra_index].rotate_right(1);
393 }
394 previous = Some(character);
395 }
396}
397
398#[cfg(feature = "script-devanagari")]
399impl ScriptProvider for Devanagari {
400 fn script(&self) -> Script {
401 Script::Devanagari
402 }
403
404 fn substitute(
405 &self,
406 request: &ShapeRequest<'_>,
407 font: &dyn super::ShapingData,
408 glyphs: &mut GlyphBuffer<'_>,
409 ) -> Result<(), ShapeError> {
410 for tag in [*b"locl", *b"ccmp", *b"nukt"] {
411 substitute(request, font, glyphs, tag, ALL, true)?;
412 }
413 reorder_devanagari_prebase_matra(request, glyphs);
414
415 let mut conjunct_forms = 0;
416 for tag in [
417 *b"akhn", *b"rphf", *b"rkrf", *b"pref", *b"blwf", *b"half", *b"pstf", *b"vatu",
418 *b"abvf", *b"cjct",
419 ] {
420 conjunct_forms += usize::from(
421 substitute(request, font, glyphs, tag, ALL, true)? == LookupStatus::Applied,
422 );
423 }
424 if request.text[request.range.clone()].contains('\u{094D}') && conjunct_forms == 0 {
425 return Err(ShapeError::ShapingUnavailable);
426 }
427 for tag in [*b"init", *b"pres", *b"abvs", *b"blws", *b"psts", *b"haln"] {
428 substitute(request, font, glyphs, tag, ALL, true)?;
429 }
430 normalize_clusters(request, glyphs);
431 Ok(())
432 }
433
434 fn position(
435 &self,
436 request: &ShapeRequest<'_>,
437 font: &dyn super::ShapingData,
438 glyphs: &mut GlyphBuffer<'_>,
439 ) -> Result<(), ShapeError> {
440 position(request, font, glyphs, *b"kern", true)?;
441 position(request, font, glyphs, *b"dist", true)?;
442 let has_marks = request.text[request.range.clone()]
443 .chars()
444 .any(devanagari_mark);
445 let abvm = position(request, font, glyphs, *b"abvm", true)?;
446 let blwm = position(request, font, glyphs, *b"blwm", true)?;
447 let mark = position(request, font, glyphs, *b"mark", true)?;
448 let mkmk = position(request, font, glyphs, *b"mkmk", true)?;
449 if has_marks
450 && [abvm, blwm, mark, mkmk]
451 .iter()
452 .all(|status| *status == LookupStatus::NotFound)
453 {
454 return Err(ShapeError::ShapingUnavailable);
455 }
456 Ok(())
457 }
458}
459
460#[cfg(test)]
461mod tests {
462 use super::*;
463 use crate::bidi::Direction;
464 use crate::shaping::{
465 FlowPoint, FontAccessError, FontId, GlyphId, GlyphSource, ShapedGlyph, ShapingData,
466 };
467
468 struct Font;
469
470 impl GlyphSource for Font {
471 fn id(&self) -> FontId {
472 FontId::new(1)
473 }
474 fn metrics(&self) -> Result<super::super::FontMetrics, FontAccessError> {
475 Ok(super::super::FontMetrics::default())
476 }
477 fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError> {
478 Ok(Some(GlyphId::new(character as u16)))
479 }
480 fn glyph_advance(&self, _glyph: GlyphId) -> Result<FlowPoint, FontAccessError> {
481 Ok(FlowPoint { x: 10, y: 0 })
482 }
483 }
484
485 struct Data;
486 impl ShapingData for Data {
487 fn substitute(
488 &self,
489 request: LookupRequest<'_, '_>,
490 glyphs: &mut GlyphBuffer<'_>,
491 ) -> Result<LookupStatus, ShapeError> {
492 for index in 0..glyphs.len() {
493 if glyphs.mask(index).is_some_and(|mask| request.selects(mask)) {
494 glyphs.set_glyph(index, GlyphId::new(100 + request.feature()[0] as u16))?;
495 }
496 }
497 Ok(LookupStatus::Applied)
498 }
499 fn position(
500 &self,
501 _request: LookupRequest<'_, '_>,
502 _glyphs: &mut GlyphBuffer<'_>,
503 ) -> Result<LookupStatus, ShapeError> {
504 Ok(LookupStatus::Applied)
505 }
506 }
507
508 struct MissingData;
509
510 impl ShapingData for MissingData {
511 fn substitute(
512 &self,
513 _request: LookupRequest<'_, '_>,
514 _glyphs: &mut GlyphBuffer<'_>,
515 ) -> Result<LookupStatus, ShapeError> {
516 Ok(LookupStatus::NotFound)
517 }
518
519 fn position(
520 &self,
521 _request: LookupRequest<'_, '_>,
522 _glyphs: &mut GlyphBuffer<'_>,
523 ) -> Result<LookupStatus, ShapeError> {
524 Ok(LookupStatus::NotFound)
525 }
526 }
527
528 #[cfg(feature = "script-devanagari")]
529 struct DevanagariData;
530
531 #[cfg(feature = "script-devanagari")]
532 impl ShapingData for DevanagariData {
533 fn substitute(
534 &self,
535 _request: LookupRequest<'_, '_>,
536 _glyphs: &mut GlyphBuffer<'_>,
537 ) -> Result<LookupStatus, ShapeError> {
538 Ok(LookupStatus::Applied)
539 }
540
541 fn position(
542 &self,
543 _request: LookupRequest<'_, '_>,
544 _glyphs: &mut GlyphBuffer<'_>,
545 ) -> Result<LookupStatus, ShapeError> {
546 Ok(LookupStatus::Applied)
547 }
548 }
549
550 #[cfg(feature = "script-arabic")]
551 struct FormData;
552
553 #[cfg(feature = "script-arabic")]
554 impl ShapingData for FormData {
555 fn substitute(
556 &self,
557 request: LookupRequest<'_, '_>,
558 glyphs: &mut GlyphBuffer<'_>,
559 ) -> Result<LookupStatus, ShapeError> {
560 let Some(form) = [*b"isol", *b"init", *b"medi", *b"fina"]
561 .iter()
562 .position(|tag| *tag == request.feature())
563 else {
564 return Ok(LookupStatus::NotFound);
565 };
566 for index in 0..glyphs.len() {
567 if glyphs.mask(index).is_some_and(|mask| request.selects(mask)) {
568 glyphs.set_glyph(index, GlyphId::new(200 + form as u16))?;
569 }
570 }
571 Ok(LookupStatus::Applied)
572 }
573
574 fn position(
575 &self,
576 _request: LookupRequest<'_, '_>,
577 _glyphs: &mut GlyphBuffer<'_>,
578 ) -> Result<LookupStatus, ShapeError> {
579 Ok(LookupStatus::Applied)
580 }
581 }
582
583 #[cfg(feature = "script-arabic")]
584 #[test]
585 fn arabic_assigns_contextual_forms_without_allocating() {
586 let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
587 let face = super::super::ScriptTypeface::new(&Font, &Data).with_scripts(&providers);
588 let text = "مرحبا";
589 let request =
590 ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
591 let mut output = [ShapedGlyph::default(); 5];
592 let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
593 assert_eq!(count, 5);
594 assert!(output
595 .iter()
596 .all(|glyph| glyph.glyph_id() == GlyphId::new(208)));
597 assert!(output.iter().all(|glyph| glyph.unsafe_to_break()));
598 }
599
600 #[cfg(feature = "script-arabic")]
601 #[test]
602 fn arabic_joining_masks_skip_transparent_marks() {
603 let text = "بَبب";
604 let request =
605 ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
606 let mut output = [ShapedGlyph::default(); 4];
607 for (slot, (offset, character)) in output.iter_mut().zip(text.char_indices()) {
608 *slot = ShapedGlyph::new(
609 GlyphId::new(character as u16),
610 TextRange::new(offset as u32, (offset + character.len_utf8()) as u32),
611 );
612 }
613 let mut glyphs = GlyphBuffer::new(&mut output, 4).unwrap();
614
615 assert!(assign_joining_masks(&request, &mut glyphs).unwrap());
616 assert_eq!(glyphs.mask(0), Some(INIT));
617 assert_eq!(glyphs.mask(1), Some(GlyphMask::NONE));
618 assert_eq!(glyphs.mask(2), Some(MEDI));
619 assert_eq!(glyphs.mask(3), Some(FINA));
620 }
621
622 #[cfg(feature = "script-arabic")]
623 #[test]
624 fn arabic_marks_do_not_receive_joining_forms() {
625 let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
626 let face = super::super::ScriptTypeface::new(&Font, &FormData).with_scripts(&providers);
627 let text = "بَب";
628 let request =
629 ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
630 let mut output = [ShapedGlyph::default(); 3];
631
632 let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
633
634 assert_eq!(count, 3);
635 assert_eq!(output[1].glyph_id(), GlyphId::new('\u{64e}' as u16));
636 assert_ne!(output[0].cluster, output[1].cluster);
637 assert_eq!(output[1].cluster, output[2].cluster);
638 }
639
640 #[cfg(feature = "script-arabic")]
641 #[test]
642 fn arabic_marks_require_positioning_data() {
643 let providers: [&dyn ScriptProvider; 1] = [&ARABIC];
644 let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
645 let text = "بَ";
646 let request =
647 ShapeRequest::new(text, 0..text.len(), Direction::RightToLeft, Script::Arabic);
648 let mut output = [ShapedGlyph::default(); 2];
649
650 assert_eq!(
651 super::super::Typeface::shape_into(&face, &request, &mut output),
652 Err(ShapeError::ShapingUnavailable)
653 );
654 }
655
656 #[cfg(feature = "script-thai")]
657 #[test]
658 fn thai_keeps_base_and_mark_in_one_cluster() {
659 let providers: [&dyn ScriptProvider; 1] = [&THAI];
660 let face = super::super::ScriptTypeface::new(&Font, &Data).with_scripts(&providers);
661 let text = "กิ";
662 let request = ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Thai);
663 let mut output = [ShapedGlyph::default(); 2];
664 let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
665 assert_eq!(count, 2);
666 assert!(output
667 .iter()
668 .all(|glyph| glyph.glyph_id() == GlyphId::new(208)));
669 assert_eq!(output[0].cluster, output[1].cluster);
670 assert!(output.iter().all(|glyph| glyph.unsafe_to_break()));
671 }
672
673 #[cfg(feature = "script-thai")]
674 #[test]
675 fn thai_marks_require_positioning_data() {
676 let providers: [&dyn ScriptProvider; 1] = [&THAI];
677 let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
678 let text = "กิ";
679 let request = ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Thai);
680 let mut output = [ShapedGlyph::default(); 2];
681
682 assert_eq!(
683 super::super::Typeface::shape_into(&face, &request, &mut output),
684 Err(ShapeError::ShapingUnavailable)
685 );
686 }
687
688 #[cfg(feature = "script-devanagari")]
689 #[test]
690 fn devanagari_reorders_prebase_matra_without_allocating() {
691 let providers: [&dyn ScriptProvider; 1] = [&DEVANAGARI];
692 let face =
693 super::super::ScriptTypeface::new(&Font, &DevanagariData).with_scripts(&providers);
694 let text = "कि";
695 let request = ShapeRequest::new(
696 text,
697 0..text.len(),
698 Direction::LeftToRight,
699 Script::Devanagari,
700 );
701 let mut output = [ShapedGlyph::default(); 2];
702
703 let count = super::super::Typeface::shape_into(&face, &request, &mut output).unwrap();
704
705 assert_eq!(count, 2);
706 assert_eq!(output[0].glyph_id(), GlyphId::new('\u{093F}' as u16));
707 assert_eq!(output[1].glyph_id(), GlyphId::new('\u{0915}' as u16));
708 assert_eq!(output[0].cluster, output[1].cluster);
709 assert!(output.iter().all(|glyph| glyph.unsafe_to_break()));
710 }
711
712 #[cfg(feature = "script-devanagari")]
713 #[test]
714 fn devanagari_conjuncts_require_shaping_data() {
715 let providers: [&dyn ScriptProvider; 1] = [&DEVANAGARI];
716 let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
717 let text = "क्ष";
718 let request = ShapeRequest::new(
719 text,
720 0..text.len(),
721 Direction::LeftToRight,
722 Script::Devanagari,
723 );
724 let mut output = [ShapedGlyph::default(); 3];
725
726 assert_eq!(
727 super::super::Typeface::shape_into(&face, &request, &mut output),
728 Err(ShapeError::ShapingUnavailable)
729 );
730 }
731
732 #[cfg(feature = "script-devanagari")]
733 #[test]
734 fn devanagari_marks_require_positioning_data() {
735 let providers: [&dyn ScriptProvider; 1] = [&DEVANAGARI];
736 let face = super::super::ScriptTypeface::new(&Font, &MissingData).with_scripts(&providers);
737 let text = "कि";
738 let request = ShapeRequest::new(
739 text,
740 0..text.len(),
741 Direction::LeftToRight,
742 Script::Devanagari,
743 );
744 let mut output = [ShapedGlyph::default(); 2];
745
746 assert_eq!(
747 super::super::Typeface::shape_into(&face, &request, &mut output),
748 Err(ShapeError::ShapingUnavailable)
749 );
750 }
751}