1mod hint;
4
5use super::{GlyphHMetrics, OutlinePen};
6use hint::{HintParams, HintState, HintingSink};
7use read_fonts::{
8 ps::{
9 cff::{blend::BlendState, dict, fd_select::FdSelect, index::Index},
10 cs::{self, CommandSink, NopFilterSink, TransformSink},
11 error::Error,
12 transform::{self, FontMatrix, ScaledFontMatrix},
13 },
14 tables::variations::ItemVariationStore,
15 types::{F2Dot14, Fixed, GlyphId},
16 FontData, FontRead, FontRef, ReadError, TableProvider,
17};
18use std::ops::Range;
19
20#[derive(Clone)]
38pub(crate) struct Outlines<'a> {
39 pub(crate) font: FontRef<'a>,
40 pub(crate) glyph_metrics: GlyphHMetrics<'a>,
41 offset_data: FontData<'a>,
42 global_subrs: Index<'a>,
43 top_dict: TopDict<'a>,
44 version: u16,
45 units_per_em: u16,
46}
47
48impl<'a> Outlines<'a> {
49 pub fn new(font: &FontRef<'a>) -> Option<Self> {
54 let units_per_em = font.head().ok()?.units_per_em();
55 Self::from_cff2(font, units_per_em).or_else(|| Self::from_cff(font, units_per_em))
56 }
57
58 pub fn from_cff(font: &FontRef<'a>, units_per_em: u16) -> Option<Self> {
59 let cff1 = font.cff().ok()?;
60 let glyph_metrics = GlyphHMetrics::new(font)?;
61 let top_dict_data = cff1.top_dicts().get(0).ok()?;
67 let top_dict = TopDict::new(cff1.offset_data().as_bytes(), top_dict_data, false).ok()?;
68 Some(Self {
69 font: font.clone(),
70 glyph_metrics,
71 offset_data: cff1.offset_data(),
72 global_subrs: cff1.global_subrs().into(),
73 top_dict,
74 version: 1,
75 units_per_em,
76 })
77 }
78
79 pub fn from_cff2(font: &FontRef<'a>, units_per_em: u16) -> Option<Self> {
80 let cff2 = font.cff2().ok()?;
81 let glyph_metrics = GlyphHMetrics::new(font)?;
82 let table_data = cff2.offset_data().as_bytes();
83 let top_dict = TopDict::new(table_data, cff2.top_dict_data(), true).ok()?;
84 Some(Self {
85 font: font.clone(),
86 glyph_metrics,
87 offset_data: cff2.offset_data(),
88 global_subrs: cff2.global_subrs().into(),
89 top_dict,
90 version: 2,
91 units_per_em,
92 })
93 }
94
95 pub fn is_cff2(&self) -> bool {
96 self.version == 2
97 }
98
99 pub fn units_per_em(&self) -> u16 {
100 self.units_per_em
101 }
102
103 pub fn glyph_count(&self) -> usize {
105 self.top_dict.charstrings.count() as usize
106 }
107
108 pub fn subfont_count(&self) -> u32 {
110 self.top_dict.font_dicts.count().max(1)
112 }
113
114 pub fn subfont_index(&self, glyph_id: GlyphId) -> u32 {
117 self.top_dict
128 .fd_select
129 .as_ref()
130 .and_then(|select| select.font_index(glyph_id))
131 .unwrap_or(0) as u32
132 }
133
134 pub fn subfont(
140 &self,
141 index: u32,
142 size: Option<f32>,
143 coords: &[F2Dot14],
144 ) -> Result<Subfont, Error> {
145 let font_dict = self.parse_font_dict(index)?;
146 let blend_state = self
147 .top_dict
148 .var_store
149 .clone()
150 .map(|store| BlendState::new(store, coords, 0))
151 .transpose()?;
152 let private_dict =
153 PrivateDict::new(self.offset_data, font_dict.private_dict_range, blend_state)?;
154 let upem = self.units_per_em as i32;
155 let mut scale = match size {
156 Some(ppem) if upem > 0 => {
157 Some(Fixed::from_bits((ppem * 64.) as i32) / Fixed::from_bits(upem))
160 }
161 _ => None,
162 };
163 let scale_requested = size.is_some();
164 let font_matrix = if let Some(top_matrix) = self.top_dict.font_matrix {
167 if let Some(sub_matrix) = font_dict.font_matrix {
169 let scaling = if top_matrix.scale > 1 && sub_matrix.scale > 1 {
170 top_matrix.scale.min(sub_matrix.scale)
171 } else {
172 1
173 };
174 let matrix =
176 transform::combine_scaled(&top_matrix.matrix, &sub_matrix.matrix, scaling);
177 let upem = Fixed::from_bits(sub_matrix.scale).mul_div(
178 Fixed::from_bits(top_matrix.scale),
179 Fixed::from_bits(scaling),
180 );
181 Some(
183 ScaledFontMatrix {
184 matrix,
185 scale: upem.to_bits(),
186 }
187 .normalize(),
188 )
189 } else {
190 Some(top_matrix)
192 }
193 } else {
194 font_dict.font_matrix.map(|matrix| matrix.normalize())
196 };
197 let mut font_matrix = if let Some(matrix) = font_matrix {
200 if matrix.scale != upem {
203 let original_scale = scale.unwrap_or(Fixed::from_i32(64));
207 scale = Some(
208 original_scale.mul_div(Fixed::from_bits(upem), Fixed::from_bits(matrix.scale)),
209 );
210 }
211 Some(matrix.matrix)
212 } else {
213 None
214 };
215 if font_matrix == Some(FontMatrix::IDENTITY) {
216 font_matrix = None;
219 }
220 let hint_scale = scale_for_hinting(scale);
221 let hint_state = HintState::new(&private_dict.hint_params, hint_scale);
222 Ok(Subfont {
223 is_cff2: self.is_cff2(),
224 scale,
225 scale_requested,
226 subrs_offset: private_dict.subrs_offset,
227 hint_state,
228 store_index: private_dict.store_index,
229 font_matrix,
230 })
231 }
232
233 pub fn draw(
245 &self,
246 subfont: &Subfont,
247 glyph_id: GlyphId,
248 coords: &[F2Dot14],
249 hint: bool,
250 pen: &mut impl OutlinePen,
251 ) -> Result<(), Error> {
252 let cff_data = self.offset_data.as_bytes();
253 let charstrings = self.top_dict.charstrings.clone();
254 let charstring_data = charstrings.get(glyph_id.to_u32() as usize)?;
255 let subrs = subfont.subrs(self)?;
256 let blend_state = subfont.blend_state(self, coords)?;
257 let cs_eval = CharstringEvaluator {
258 cff_data,
259 charstrings,
260 global_subrs: self.global_subrs.clone(),
261 subrs,
262 blend_state,
263 charstring_data,
264 };
265 let apply_hinting = hint && subfont.scale_requested;
267 let mut pen_sink = PenSink::new(pen);
268 let mut simplifying_adapter = NopFilterSink::new(&mut pen_sink);
269 if let Some(matrix) = subfont.font_matrix {
270 if apply_hinting {
271 let mut transform_sink =
272 HintedTransformingSink::new(&mut simplifying_adapter, matrix);
273 let mut hinting_adapter =
274 HintingSink::new(&subfont.hint_state, &mut transform_sink);
275 cs_eval.evaluate(&mut hinting_adapter)?;
276 } else {
277 let mut transform_sink = TransformSink::from_matrix_scale(
278 &mut simplifying_adapter,
279 matrix,
280 subfont.scale,
281 );
282 cs_eval.evaluate(&mut transform_sink)?;
283 }
284 } else if apply_hinting {
285 let mut hinting_adapter =
286 HintingSink::new(&subfont.hint_state, &mut simplifying_adapter);
287 cs_eval.evaluate(&mut hinting_adapter)?;
288 } else {
289 let mut scaling_adapter = TransformSink::from_matrix_scale(
290 &mut simplifying_adapter,
291 FontMatrix::IDENTITY,
292 subfont.scale,
293 );
294 cs_eval.evaluate(&mut scaling_adapter)?;
295 }
296 Ok(())
297 }
298
299 fn parse_font_dict(&self, subfont_index: u32) -> Result<FontDict, Error> {
300 if self.top_dict.font_dicts.count() != 0 {
301 let font_dict_data = self.top_dict.font_dicts.get(subfont_index as usize)?;
304 FontDict::new(font_dict_data)
305 } else {
306 let range = self.top_dict.private_dict_range.clone();
311 Ok(FontDict {
312 private_dict_range: range.start as usize..range.end as usize,
313 font_matrix: None,
314 })
315 }
316 }
317}
318
319fn scale_for_hinting(scale: Option<Fixed>) -> Fixed {
323 Fixed::from_bits((scale.unwrap_or(Fixed::ONE).to_bits().saturating_add(32)) / 64)
324}
325
326struct CharstringEvaluator<'a> {
327 cff_data: &'a [u8],
328 charstrings: Index<'a>,
329 global_subrs: Index<'a>,
330 subrs: Option<Index<'a>>,
331 blend_state: Option<BlendState<'a>>,
332 charstring_data: &'a [u8],
333}
334
335impl CharstringEvaluator<'_> {
336 fn evaluate(self, sink: &mut impl CommandSink) -> Result<Option<Fixed>, Error> {
337 let subrs = self.subrs.unwrap_or_default();
338 let ctx = (self.cff_data, &self.charstrings, &self.global_subrs, &subrs);
339 cs::evaluate(&ctx, self.blend_state, self.charstring_data, sink)
340 }
341}
342
343#[derive(Clone)]
351pub(crate) struct Subfont {
352 is_cff2: bool,
353 scale: Option<Fixed>,
354 scale_requested: bool,
358 subrs_offset: Option<usize>,
359 pub(crate) hint_state: HintState,
360 store_index: u16,
361 font_matrix: Option<FontMatrix>,
362}
363
364impl Subfont {
365 pub fn subrs<'a>(&self, scaler: &Outlines<'a>) -> Result<Option<Index<'a>>, Error> {
367 if let Some(subrs_offset) = self.subrs_offset {
368 let offset_data = scaler.offset_data.as_bytes();
369 let index_data = offset_data.get(subrs_offset..).unwrap_or_default();
370 Ok(Some(Index::new(index_data, self.is_cff2)?))
371 } else {
372 Ok(None)
373 }
374 }
375
376 pub fn blend_state<'a>(
379 &self,
380 scaler: &Outlines<'a>,
381 coords: &'a [F2Dot14],
382 ) -> Result<Option<BlendState<'a>>, Error> {
383 if let Some(var_store) = scaler.top_dict.var_store.clone() {
384 Ok(Some(BlendState::new(var_store, coords, self.store_index)?))
385 } else {
386 Ok(None)
387 }
388 }
389}
390
391#[derive(Default)]
394struct PrivateDict {
395 hint_params: HintParams,
396 subrs_offset: Option<usize>,
397 store_index: u16,
398}
399
400impl PrivateDict {
401 fn new(
402 data: FontData,
403 range: Range<usize>,
404 blend_state: Option<BlendState<'_>>,
405 ) -> Result<Self, Error> {
406 let private_dict_data = data.read_array(range.clone())?;
407 let mut dict = Self::default();
408 for entry in dict::entries(private_dict_data, blend_state) {
409 use dict::Entry::*;
410 match entry? {
411 BlueValues(values) => dict.hint_params.blues = values,
412 FamilyBlues(values) => dict.hint_params.family_blues = values,
413 OtherBlues(values) => dict.hint_params.other_blues = values,
414 FamilyOtherBlues(values) => dict.hint_params.family_other_blues = values,
415 BlueScale(value) => dict.hint_params.blue_scale = value,
416 BlueShift(value) => dict.hint_params.blue_shift = value,
417 BlueFuzz(value) => dict.hint_params.blue_fuzz = value,
418 LanguageGroup(group) => dict.hint_params.language_group = group,
419 SubrsOffset(offset) => {
421 dict.subrs_offset = Some(
422 range
423 .start
424 .checked_add(offset)
425 .ok_or(ReadError::OutOfBounds)?,
426 )
427 }
428 VariationStoreIndex(index) => dict.store_index = index,
429 _ => {}
430 }
431 }
432 Ok(dict)
433 }
434}
435
436#[derive(Clone, Default)]
438struct FontDict {
439 private_dict_range: Range<usize>,
440 font_matrix: Option<ScaledFontMatrix>,
441}
442
443impl FontDict {
444 fn new(font_dict_data: &[u8]) -> Result<Self, Error> {
445 let mut range = None;
446 let mut font_matrix = None;
447 for entry in dict::entries(font_dict_data, None) {
448 match entry? {
449 dict::Entry::PrivateDictRange(r) => {
450 range = Some(r);
451 }
452 dict::Entry::FontMatrix(matrix) => font_matrix = Some(matrix),
456 _ => {}
457 }
458 }
459 Ok(Self {
460 private_dict_range: range.ok_or(Error::MissingPrivateDict)?,
461 font_matrix,
462 })
463 }
464}
465
466#[derive(Clone, Default)]
469struct TopDict<'a> {
470 charstrings: Index<'a>,
471 font_dicts: Index<'a>,
472 fd_select: Option<FdSelect<'a>>,
473 private_dict_range: Range<u32>,
474 font_matrix: Option<ScaledFontMatrix>,
475 var_store: Option<ItemVariationStore<'a>>,
476}
477
478impl<'a> TopDict<'a> {
479 fn new(table_data: &'a [u8], top_dict_data: &'a [u8], is_cff2: bool) -> Result<Self, Error> {
480 let mut items = TopDict::default();
481 for entry in dict::entries(top_dict_data, None) {
482 match entry? {
483 dict::Entry::CharstringsOffset(offset) => {
484 items.charstrings =
485 Index::new(table_data.get(offset..).unwrap_or_default(), is_cff2)?;
486 }
487 dict::Entry::FdArrayOffset(offset) => {
488 items.font_dicts =
489 Index::new(table_data.get(offset..).unwrap_or_default(), is_cff2)?;
490 }
491 dict::Entry::FdSelectOffset(offset) => {
492 items.fd_select = Some(FdSelect::read(FontData::new(
493 table_data.get(offset..).unwrap_or_default(),
494 ))?);
495 }
496 dict::Entry::PrivateDictRange(range) => {
497 items.private_dict_range = range.start as u32..range.end as u32;
498 }
499 dict::Entry::FontMatrix(matrix) => {
500 items.font_matrix = Some(matrix.normalize());
502 }
503 dict::Entry::VariationStoreOffset(offset) if is_cff2 => {
504 let offset = offset.checked_add(2).ok_or(ReadError::OutOfBounds)?;
508 items.var_store = Some(ItemVariationStore::read(FontData::new(
509 table_data.get(offset..).unwrap_or_default(),
510 ))?);
511 }
512 _ => {}
513 }
514 }
515 Ok(items)
516 }
517}
518
519struct PenSink<'a, P>(&'a mut P);
522
523impl<'a, P> PenSink<'a, P> {
524 fn new(pen: &'a mut P) -> Self {
525 Self(pen)
526 }
527}
528
529impl<P> CommandSink for PenSink<'_, P>
530where
531 P: OutlinePen,
532{
533 fn move_to(&mut self, x: Fixed, y: Fixed) {
534 self.0.move_to(x.to_f32(), y.to_f32());
535 }
536
537 fn line_to(&mut self, x: Fixed, y: Fixed) {
538 self.0.line_to(x.to_f32(), y.to_f32());
539 }
540
541 fn curve_to(&mut self, cx0: Fixed, cy0: Fixed, cx1: Fixed, cy1: Fixed, x: Fixed, y: Fixed) {
542 self.0.curve_to(
543 cx0.to_f32(),
544 cy0.to_f32(),
545 cx1.to_f32(),
546 cy1.to_f32(),
547 x.to_f32(),
548 y.to_f32(),
549 );
550 }
551
552 fn close(&mut self) {
553 self.0.close();
554 }
555}
556
557struct HintedTransformingSink<'a, S> {
559 inner: &'a mut S,
560 matrix: FontMatrix,
561}
562
563impl<'a, S> HintedTransformingSink<'a, S> {
564 fn new(sink: &'a mut S, matrix: FontMatrix) -> Self {
565 Self {
566 inner: sink,
567 matrix,
568 }
569 }
570
571 fn transform(&self, x: Fixed, y: Fixed) -> (Fixed, Fixed) {
572 let (x, y) = self.matrix.transform(
575 Fixed::from_bits(x.to_bits() >> 10),
576 Fixed::from_bits(y.to_bits() >> 10),
577 );
578 (
579 Fixed::from_bits(x.to_bits() << 10),
580 Fixed::from_bits(y.to_bits() << 10),
581 )
582 }
583}
584
585impl<S: CommandSink> CommandSink for HintedTransformingSink<'_, S> {
586 fn hstem(&mut self, y: Fixed, dy: Fixed) {
587 self.inner.hstem(y, dy);
588 }
589
590 fn vstem(&mut self, x: Fixed, dx: Fixed) {
591 self.inner.vstem(x, dx);
592 }
593
594 fn hint_mask(&mut self, mask: &[u8]) {
595 self.inner.hint_mask(mask);
596 }
597
598 fn counter_mask(&mut self, mask: &[u8]) {
599 self.inner.counter_mask(mask);
600 }
601
602 fn clear_hints(&mut self) {
603 self.inner.clear_hints();
604 }
605
606 fn move_to(&mut self, x: Fixed, y: Fixed) {
607 let (x, y) = self.transform(x, y);
608 self.inner.move_to(x, y);
609 }
610
611 fn line_to(&mut self, x: Fixed, y: Fixed) {
612 let (x, y) = self.transform(x, y);
613 self.inner.line_to(x, y);
614 }
615
616 fn curve_to(&mut self, cx1: Fixed, cy1: Fixed, cx2: Fixed, cy2: Fixed, x: Fixed, y: Fixed) {
617 let (cx1, cy1) = self.transform(cx1, cy1);
618 let (cx2, cy2) = self.transform(cx2, cy2);
619 let (x, y) = self.transform(x, y);
620 self.inner.curve_to(cx1, cy1, cx2, cy2, x, y);
621 }
622
623 fn close(&mut self) {
624 self.inner.close();
625 }
626
627 fn finish(&mut self) {
628 self.inner.finish();
629 }
630}
631
632#[cfg(test)]
633mod tests {
634 use super::{super::pen::SvgPen, *};
635 use crate::{
636 outline::{HintingInstance, HintingOptions},
637 prelude::{LocationRef, Size},
638 MetadataProvider,
639 };
640 use font_test_data::bebuffer::BeBuffer;
641 use raw::tables::cff2::Cff2;
642 use read_fonts::ps::hinting::Blues;
643 use read_fonts::FontRef;
644
645 #[test]
646 fn read_cff_static() {
647 let font = FontRef::new(font_test_data::NOTO_SERIF_DISPLAY_TRIMMED).unwrap();
648 let cff = Outlines::new(&font).unwrap();
649 assert!(!cff.is_cff2());
650 assert!(cff.top_dict.var_store.is_none());
651 assert!(cff.top_dict.font_dicts.count() == 0);
652 assert!(!cff.top_dict.private_dict_range.is_empty());
653 assert!(cff.top_dict.fd_select.is_none());
654 assert_eq!(cff.subfont_count(), 1);
655 assert_eq!(cff.subfont_index(GlyphId::new(1)), 0);
656 assert_eq!(cff.global_subrs.count(), 17);
657 }
658
659 #[test]
660 fn read_cff2_static() {
661 let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
662 let cff = Outlines::new(&font).unwrap();
663 assert!(cff.is_cff2());
664 assert!(cff.top_dict.var_store.is_some());
665 assert!(cff.top_dict.font_dicts.count() != 0);
666 assert!(cff.top_dict.private_dict_range.is_empty());
667 assert!(cff.top_dict.fd_select.is_none());
668 assert_eq!(cff.subfont_count(), 1);
669 assert_eq!(cff.subfont_index(GlyphId::new(1)), 0);
670 assert_eq!(cff.global_subrs.count(), 0);
671 }
672
673 #[test]
674 fn read_example_cff2_table() {
675 let cff2 = Cff2::read(FontData::new(font_test_data::cff2::EXAMPLE)).unwrap();
676 let top_dict =
677 TopDict::new(cff2.offset_data().as_bytes(), cff2.top_dict_data(), true).unwrap();
678 assert!(top_dict.var_store.is_some());
679 assert!(top_dict.font_dicts.count() != 0);
680 assert!(top_dict.private_dict_range.is_empty());
681 assert!(top_dict.fd_select.is_none());
682 assert_eq!(cff2.global_subrs().count(), 0);
683 }
684
685 #[test]
686 fn cff2_variable_outlines_match_freetype() {
687 compare_glyphs(
688 font_test_data::CANTARELL_VF_TRIMMED,
689 font_test_data::CANTARELL_VF_TRIMMED_GLYPHS,
690 );
691 }
692
693 #[test]
694 fn cff_static_outlines_match_freetype() {
695 compare_glyphs(
696 font_test_data::NOTO_SERIF_DISPLAY_TRIMMED,
697 font_test_data::NOTO_SERIF_DISPLAY_TRIMMED_GLYPHS,
698 );
699 }
700
701 #[test]
702 fn unhinted_ends_with_close() {
703 let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
704 let glyph = font.outline_glyphs().get(GlyphId::new(1)).unwrap();
705 let mut svg = SvgPen::default();
706 glyph.draw(Size::unscaled(), &mut svg).unwrap();
707 assert!(svg.to_string().ends_with('Z'));
708 }
709
710 #[test]
711 fn hinted_ends_with_close() {
712 let font = FontRef::new(font_test_data::CANTARELL_VF_TRIMMED).unwrap();
713 let glyphs = font.outline_glyphs();
714 let hinter = HintingInstance::new(
715 &glyphs,
716 Size::unscaled(),
717 LocationRef::default(),
718 HintingOptions::default(),
719 )
720 .unwrap();
721 let glyph = glyphs.get(GlyphId::new(1)).unwrap();
722 let mut svg = SvgPen::default();
723 glyph.draw(&hinter, &mut svg).unwrap();
724 assert!(svg.to_string().ends_with('Z'));
725 }
726
727 #[test]
729 fn empty_private_dict() {
730 let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET).unwrap();
731 let outlines = super::Outlines::new(&font).unwrap();
732 assert!(outlines.top_dict.private_dict_range.is_empty());
733 assert!(outlines
734 .parse_font_dict(0)
735 .unwrap()
736 .private_dict_range
737 .is_empty());
738 }
739
740 #[test]
743 fn subrs_offset_overflow() {
744 let private_dict = BeBuffer::new()
746 .push(0u32) .push(29u8) .push(-1i32) .push(19u8) .to_vec();
751 assert!(
753 PrivateDict::new(FontData::new(&private_dict), 4..private_dict.len(), None).is_err()
754 );
755 }
756
757 #[test]
761 fn top_dict_ivs_offset_overflow() {
762 let top_dict = BeBuffer::new()
765 .push(29u8) .push(-1i32) .push(24u8) .to_vec();
769 assert!(TopDict::new(&[], &top_dict, true).is_err());
771 }
772
773 #[test]
780 fn proper_scaling_when_factor_equals_fixed_one() {
781 let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET).unwrap();
782 assert_eq!(font.head().unwrap().units_per_em(), 512);
783 let glyphs = font.outline_glyphs();
784 let glyph = glyphs.get(GlyphId::new(1)).unwrap();
785 let mut svg = SvgPen::with_precision(6);
786 glyph
787 .draw((Size::new(8.0), LocationRef::default()), &mut svg)
788 .unwrap();
789 assert!(svg.starts_with("M6.328125,7.000000 L1.671875,7.000000"));
791 }
792
793 fn compare_glyphs(font_data: &[u8], expected_outlines: &str) {
800 use super::super::testing;
801 let font = FontRef::new(font_data).unwrap();
802 let expected_outlines = testing::parse_glyph_outlines(expected_outlines);
803 let outlines = super::Outlines::new(&font).unwrap();
804 let mut path = testing::Path::default();
805 for expected_outline in &expected_outlines {
806 if expected_outline.size == 0.0 && !expected_outline.coords.is_empty() {
807 continue;
808 }
809 let size = (expected_outline.size != 0.0).then_some(expected_outline.size);
810 path.elements.clear();
811 let subfont = outlines
812 .subfont(
813 outlines.subfont_index(expected_outline.glyph_id),
814 size,
815 &expected_outline.coords,
816 )
817 .unwrap();
818 outlines
819 .draw(
820 &subfont,
821 expected_outline.glyph_id,
822 &expected_outline.coords,
823 false,
824 &mut path,
825 )
826 .unwrap();
827 if path.elements != expected_outline.path {
828 panic!(
829 "mismatch in glyph path for id {} (size: {}, coords: {:?}): path: {:?} expected_path: {:?}",
830 expected_outline.glyph_id,
831 expected_outline.size,
832 expected_outline.coords,
833 &path.elements,
834 &expected_outline.path
835 );
836 }
837 }
838 }
839
840 #[test]
842 fn capture_family_other_blues() {
843 let private_dict_data = &font_test_data::cff2::EXAMPLE[0x4f..=0xc0];
844 let store =
845 ItemVariationStore::read(FontData::new(&font_test_data::cff2::EXAMPLE[18..])).unwrap();
846 let coords = &[F2Dot14::from_f32(0.0)];
847 let blend_state = BlendState::new(store, coords, 0).unwrap();
848 let private_dict = PrivateDict::new(
849 FontData::new(private_dict_data),
850 0..private_dict_data.len(),
851 Some(blend_state),
852 )
853 .unwrap();
854 assert_eq!(
855 private_dict.hint_params.family_other_blues,
856 Blues::new([-249.0, -239.0].map(Fixed::from_f64).into_iter())
857 )
858 }
859
860 #[test]
861 fn implied_seac() {
862 let font = FontRef::new(font_test_data::CHARSTRING_PATH_OPS).unwrap();
863 let glyphs = font.outline_glyphs();
864 let gid = GlyphId::new(3);
865 assert_eq!(font.glyph_names().get(gid).unwrap(), "Scaron");
866 let glyph = glyphs.get(gid).unwrap();
867 let mut pen = SvgPen::new();
868 glyph
869 .draw((Size::unscaled(), LocationRef::default()), &mut pen)
870 .unwrap();
871 assert_eq!(pen.to_string().chars().filter(|ch| *ch == 'Z').count(), 2);
876 }
877
878 #[test]
879 fn implied_seac_clears_hints() {
880 let font = FontRef::new(font_test_data::CHARSTRING_PATH_OPS).unwrap();
881 let outlines = Outlines::from_cff(&font, 1000).unwrap();
882 let subfont = outlines.subfont(0, Some(16.0), &[]).unwrap();
883 let cff_data = outlines.offset_data.as_bytes();
884 let charstrings = outlines.top_dict.charstrings.clone();
885 let charstring_data = charstrings.get(3).unwrap();
886 let subrs = subfont.subrs(&outlines).unwrap();
887 let blend_state = None;
888 let cs_eval = CharstringEvaluator {
889 cff_data,
890 charstrings,
891 global_subrs: outlines.global_subrs.clone(),
892 subrs,
893 blend_state,
894 charstring_data,
895 };
896 struct ClearHintsCountingSink(u32);
897 impl CommandSink for ClearHintsCountingSink {
898 fn move_to(&mut self, _: Fixed, _: Fixed) {}
899 fn line_to(&mut self, _: Fixed, _: Fixed) {}
900 fn curve_to(&mut self, _: Fixed, _: Fixed, _: Fixed, _: Fixed, _: Fixed, _: Fixed) {}
901 fn close(&mut self) {}
902 fn clear_hints(&mut self) {
903 self.0 += 1;
904 }
905 }
906 let mut sink = ClearHintsCountingSink(0);
907 cs_eval.evaluate(&mut sink).unwrap();
908 assert_eq!(sink.0, 2);
911 }
912
913 const TRANSFORM: FontMatrix = FontMatrix::from_elements([
914 Fixed::ONE,
915 Fixed::ZERO,
916 Fixed::from_bits(10945),
918 Fixed::ONE,
919 Fixed::ZERO,
920 Fixed::ZERO,
921 ]);
922
923 #[test]
924 fn hinted_transform_sink() {
925 let input = [(383i32, 117i32), (450, 20), (555, -34), (683, -34)]
928 .map(|(x, y)| (Fixed::from_bits(x << 10), Fixed::from_bits(y << 10)));
929 let expected = [(403, 117i32), (453, 20), (549, -34), (677, -34)]
930 .map(|(x, y)| (Fixed::from_bits(x << 10), Fixed::from_bits(y << 10)));
931 let mut dummy = ();
932 let sink = HintedTransformingSink::new(&mut dummy, TRANSFORM);
933 let transformed = input.map(|(x, y)| sink.transform(x, y));
934 assert_eq!(transformed, expected);
935 }
936
937 #[test]
939 fn nested_font_matrices() {
940 let font = FontRef::new(font_test_data::MATERIAL_ICONS_SUBSET_MATRIX).unwrap();
942 let outlines = Outlines::from_cff(&font, 512).unwrap();
943 let top_matrix = outlines.top_dict.font_matrix.unwrap();
945 let expected_top_matrix = [65536, 0, 5604, 65536, 0, 0].map(Fixed::from_bits);
946 assert_eq!(top_matrix.matrix.elements(), expected_top_matrix);
947 assert_eq!(top_matrix.scale, 512);
948 let sub_matrix = outlines.parse_font_dict(0).unwrap().font_matrix.unwrap();
950 let expected_sub_matrix = [327680, 0, 0, 327680, 0, 0].map(Fixed::from_bits);
951 assert_eq!(sub_matrix.matrix.elements(), expected_sub_matrix);
952 assert_eq!(sub_matrix.scale, 10);
953 let subfont = outlines.subfont(0, Some(24.0), &[]).unwrap();
955 let expected_combined_matrix = [65536, 0, 5604, 65536, 0, 0].map(Fixed::from_bits);
956 assert_eq!(
957 subfont.font_matrix.unwrap().elements(),
958 expected_combined_matrix
959 );
960 assert_eq!(subfont.scale.unwrap().to_bits(), 98304);
962 }
963
964 #[test]
968 fn subfont_hint_scale_overflow() {
969 let _ = scale_for_hinting(Some(Fixed::from_bits(i32::MAX)));
971 }
972}