1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
use std::collections::BTreeMap;
use std::ops::RangeInclusive;
use pathfinder_geometry::rect::RectF;
use pathfinder_geometry::transform2d::{Matrix2x2F, Transform2F};
use pathfinder_geometry::vector::{vec2f, Vector2F};
use crate::error::ParseError;
use crate::tables::glyf::{
calculate_phantom_points, BoundingBox, ComponentOffsets, CompositeGlyph,
CompositeGlyphArgument, CompositeGlyphComponent, CompositeGlyphFlag, EmptyGlyph, GlyfRecord,
GlyfTable, Glyph, PhantomPoints, Point, SimpleGlyph, SimpleGlyphFlag,
};
use crate::tables::os2::Os2;
use crate::tables::variable_fonts::gvar::{GvarTable, NumPoints};
use crate::tables::variable_fonts::OwnedTuple;
use crate::tables::{HheaTable, HmtxTable};
use crate::SafeFrom;
impl<'a> Glyph {
/// Apply glyph variation to the supplied glyph according to the variation
/// instance `user_instance`.
pub(crate) fn apply_variations(
&mut self,
glyph_index: u16,
instance: &OwnedTuple,
gvar: &GvarTable<'a>,
hmtx: &HmtxTable<'a>,
vmtx: Option<&HmtxTable<'a>>,
os2: Option<&Os2>,
hhea: &HheaTable,
) -> Result<(), ParseError> {
let Some(deltas) = glyph_deltas(self, glyph_index, instance, gvar)? else {
// The glyph has no deltas but we still need to populate the phantom points
let phantom_points =
calculate_phantom_points(glyph_index, self.bounding_box(), hmtx, vmtx, os2, hhea)?;
self.set_phantom_points(phantom_points);
return Ok(());
};
match self {
Glyph::Empty(empty) => {
let mut phantom_points =
calculate_phantom_points(glyph_index, None, hmtx, vmtx, os2, hhea)?;
apply_phantom_point_deltas(&mut phantom_points, &deltas);
empty.phantom_points = Some(phantom_points);
Ok(())
}
Glyph::Simple(simple_glyph) => {
// Calculate the phantom points before variations are applied
let mut phantom_points = calculate_phantom_points(
glyph_index,
Some(simple_glyph.bounding_box),
hmtx,
vmtx,
os2,
hhea,
)?;
// Apply the deltas to the coordinates of the glyph and calculate the updated
// bounding box as we go.
let mut bbox = BoundingBox::empty();
simple_glyph
.coordinates
.iter_mut()
.zip(deltas.iter().copied())
.enumerate()
.for_each(|(i, ((_flag, point), delta))| {
// NOTE(cast): Since Rust 1.45.0 floating point casts like these are
// saturating casts.
// https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#fixing-unsoundness-in-casts
point.0 = (point.0 as f32 + delta.x()).round() as i16;
point.1 = (point.1 as f32 + delta.y()).round() as i16;
if i == 0 {
bbox = BoundingBox {
x_min: point.0,
x_max: point.0,
y_min: point.1,
y_max: point.1,
}
} else {
bbox.add(*point)
}
});
simple_glyph.bounding_box = bbox;
// Apply deltas to the phantom points of the glyph
apply_phantom_point_deltas(
&mut phantom_points,
&deltas[simple_glyph.coordinates.len()..],
);
simple_glyph.phantom_points = Some(Box::new(phantom_points));
// TODO: Update flag 1 in head?
Ok(())
}
Glyph::Composite(composite) => {
// Calculate the phantom points before variations are applied
let mut phantom_points = calculate_phantom_points(
glyph_index,
Some(composite.bounding_box),
hmtx,
vmtx,
os2,
hhea,
)?;
// Use the deltas to reposition the sub-glyphs of the composite glyph
composite
.glyphs
.iter_mut()
.zip(deltas.iter().copied())
.for_each(|(composite_glyph, delta)| {
add_composite_glyph_delta(composite_glyph, delta)
});
// Apply deltas to phantom points
apply_phantom_point_deltas(&mut phantom_points, &deltas[composite.glyphs.len()..]);
composite.phantom_points = Some(Box::new(phantom_points));
Ok(())
}
}
}
/// Calculate the bounding box from the points of this glyph.
///
/// For simple glyphs this just returns the bounding box of the glyph. For
/// composite glyphs the sub-glyphs are traversed to calculate the
/// bounding box that contains them all.
pub(crate) fn calculate_bounding_box(&self, glyf: &GlyfTable<'a>) -> Result<RectF, ParseError> {
match self {
Glyph::Empty(glyph) => glyph.calculate_bounding_box(),
Glyph::Simple(glyph) => glyph.calculate_bounding_box(),
Glyph::Composite(glyph) => glyph.calculate_bounding_box(glyf),
}
}
fn set_phantom_points(&mut self, phantom_points: [Point; 4]) {
match self {
Glyph::Empty(empty) => empty.phantom_points = Some(phantom_points),
Glyph::Simple(simple) => simple.phantom_points = Some(Box::new(phantom_points)),
Glyph::Composite(composite) => {
composite.phantom_points = Some(Box::new(phantom_points))
}
}
}
}
fn apply_phantom_point_deltas(phantom_points: &mut PhantomPoints, deltas: &[Vector2F]) {
phantom_points
.iter_mut()
.zip(deltas.iter().copied())
.for_each(|(point, delta)| {
// NOTE(cast): saturating
point.0 = (point.0 as f32 + delta.x()).round() as i16;
point.1 = (point.1 as f32 + delta.y()).round() as i16;
});
}
impl EmptyGlyph {
pub(crate) fn calculate_bounding_box(&self) -> Result<RectF, ParseError> {
Ok(RectF::default())
}
}
impl SimpleGlyph {
pub(crate) fn calculate_bounding_box(&self) -> Result<RectF, ParseError> {
Ok(RectF::from_points(
vec2f(
self.bounding_box.x_min as f32,
self.bounding_box.y_min as f32,
),
vec2f(
self.bounding_box.x_max as f32,
self.bounding_box.y_max as f32,
),
))
}
}
impl CompositeGlyph {
pub(crate) fn calculate_bounding_box(&self, glyf: &GlyfTable<'_>) -> Result<RectF, ParseError> {
let mut bbox: Option<RectF> = None;
for child in &self.glyphs {
let record: &GlyfRecord<'_> = glyf
.records
.get(usize::from(child.glyph_index))
.ok_or(ParseError::BadIndex)?;
let GlyfRecord::Parsed(child_glyph) = record else {
panic!("glyph is not parsed");
};
let mut child_bbox = child_glyph.calculate_bounding_box(glyf)?;
// Scale the bbox
let offset = Vector2F::new(
i32::from(child.argument1) as f32,
i32::from(child.argument2) as f32,
);
match child.scale {
Some(scale) => {
let scale = Matrix2x2F::from(scale);
match child.flags.component_offsets() {
// translate, then scale
ComponentOffsets::Scaled => {
let transform = Transform2F {
matrix: scale,
vector: Vector2F::zero(),
};
child_bbox = transform * (child_bbox + offset);
}
// scale, then translate - this the default for Transform2F
ComponentOffsets::Unscaled => {
let transform = Transform2F {
matrix: scale,
vector: offset,
};
child_bbox = transform * child_bbox;
}
}
}
// just translate
None => child_bbox = child_bbox + offset,
}
// combine the scaled bbox with the overall bbox
match bbox.as_mut() {
Some(rect) => *rect = rect.union_rect(child_bbox),
None => bbox = Some(child_bbox),
}
}
Ok(bbox.unwrap_or_default())
}
}
fn add_composite_glyph_delta(composite_glyph: &mut CompositeGlyphComponent, delta: Vector2F) {
// > if ARGS_ARE_XY_VALUES (bit 1) is set, then X and Y offsets are used; if that bit is clear,
// > then point numbers are used. If the position of a component is represented using X and Y
// > offsets — the ARGS_ARE_XY_VALUES flag is set — then adjustment deltas can be applied to
// > those offsets. However, if the position of a component is represented using point numbers —
// > the ARGS_ARE_XY_VALUES flag is not set — then adjustment deltas have no effect on that
// > component and should not be specified.
//
// https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#point-numbers-and-processing-for-composite-glyphs
if composite_glyph.flags.args_are_xy_values() {
composite_glyph.argument1 = add_delta(composite_glyph.argument1, delta.x());
composite_glyph.argument2 = add_delta(composite_glyph.argument2, delta.y());
// add_delta always uses I16 so ensure the ARG_1_AND_2_ARE_WORDS flag is set
composite_glyph.flags |= CompositeGlyphFlag::ARG_1_AND_2_ARE_WORDS;
}
}
fn add_delta(arg: CompositeGlyphArgument, delta: f32) -> CompositeGlyphArgument {
// If ARGS_ARE_XY_VALUES is set we should only get I8 or I16 values in practice
// but handle them all nonetheless.
let adjusted = match arg {
CompositeGlyphArgument::U8(val) => val as f32 + delta,
CompositeGlyphArgument::I8(val) => val as f32 + delta,
CompositeGlyphArgument::U16(val) => val as f32 + delta,
CompositeGlyphArgument::I16(val) => val as f32 + delta,
};
let adjusted = adjusted.round();
// TODO: use smaller types when appropriate
CompositeGlyphArgument::I16(adjusted as i16)
}
/// Calculate the point deltas for the supplied glyph according to the variation
/// instance `instance`.
///
/// If deltas are present the resulting vector will include a delta for each
/// coordinate in the glyph, including the four phantom points.
///
/// If the glyph has no variation data then `Ok(None)` is returned.
fn glyph_deltas(
glyph: &Glyph,
glyph_index: u16,
instance: &OwnedTuple,
gvar: &GvarTable<'_>,
) -> Result<Option<Vec<Vector2F>>, ParseError> {
let num_points = NumPoints::new(glyph.number_of_points()?);
let Some(variations) = gvar.glyph_variation_data(glyph_index, num_points)? else {
return Ok(None);
};
let applicable = variations.determine_applicable(gvar, instance);
// Now the deltas need to be calculated for each point.
// The delta is multiplied by the scalar. The sum of deltas is applied to the
// default position
let mut final_deltas = vec![Vector2F::zero(); usize::safe_from(num_points.get())];
let mut region_deltas = vec![Vector2F::zero(); usize::safe_from(num_points.get())];
for (scale, region) in applicable {
let variation_data =
region.variation_data(num_points, variations.shared_point_numbers())?;
// This is the output for this region, by the end every point needs to have a delta assigned.
// Either explicitly or inferred. This buffer is reused between regions so we re-fill it
// with zeros for each new region.
region_deltas.fill(Vector2F::zero());
// This maps point numbers to deltas, in order. It allows direct lookup of deltas for a
// point as well as navigating between explicit points.
let explicit_deltas = variation_data.iter().collect::<BTreeMap<_, _>>();
// Fill in the explicit deltas
for (number, delta) in &explicit_deltas {
let region_delta = region_deltas
.get_mut(usize::safe_from(*number))
.ok_or(ParseError::BadIndex)?;
*region_delta = Vector2F::new(delta.0 as f32, delta.1 as f32);
}
// > Calculation of inferred deltas is done for a given glyph and a given region on a
// > contour-by-contour basis.
// >
// > For a given contour, if the point number list does not include any of the points in
// > that contour, then none of the points in the contour are affected and no inferred deltas
// > need to be computed.
// >
// > If the point number list includes some but not all of the points in a given contour,
// > then inferred deltas must be derived for the points that were not included in the point
// > number list, as follows.
// Only need to do this for simple glyphs
if let Glyph::Simple(simple_glyph) = glyph {
// Deltas need to be inferred if not all points were assigned explicit deltas
if explicit_deltas.len() != usize::safe_from(num_points.get()) {
infer_unreferenced_points(&mut region_deltas, &explicit_deltas, simple_glyph)?;
}
}
// Scale and accumulate the deltas from this variation region onto the final deltas
final_deltas
.iter_mut()
.zip(region_deltas.iter().copied())
.for_each(|(out, delta)| *out += delta * scale)
}
// Now all the deltas need to be applied to the glyph points
Ok(Some(final_deltas))
}
fn infer_unreferenced_points(
deltas: &mut [Vector2F],
raw_deltas: &BTreeMap<u32, (i16, i16)>,
simple_glyph: &SimpleGlyph,
) -> Result<(), ParseError> {
// Iterate over the contours of the glyph and ensure that all points of the
// contour have a delta
let mut begin = 0;
for end in simple_glyph.end_pts_of_contours.iter().copied() {
let start = begin;
let end = u32::from(end);
begin = end + 1;
let range = start..=end;
let range_len = usize::safe_from(end.saturating_sub(start)) + 1; // Plus 1 because range is inclusive
let explicit_count = raw_deltas.range(range.clone()).count();
match explicit_count {
0 => {
// No points in this contour were referenced; no inferred deltas need to
// be computed.
continue;
}
1 => {
// If exactly one point from the contour is referenced in the point number list,
// then every point in that contour uses the same X and Y delta values as that
// point. Find the one referenced point and use it to update the
// others NOTE(unwrap): Safe as we confirmed we have one delta
// to get into this block
let (_referenced_point_number, reference_delta) = raw_deltas
.range(range.clone())
.next()
.map(|(n, (x, y))| (*n, Vector2F::new(*x as f32, *y as f32)))
.unwrap();
// Get the delta for this point
let usize_range = usize::safe_from(*range.start())..=usize::safe_from(*range.end());
// Set all the deltas in this contour to `reference_delta`
deltas[usize_range].fill(reference_delta);
continue;
}
n if n == range_len => {
// All points in this contour were referenced; no inferred deltas need to
// be computed.
continue;
}
_ => {
// If the point number list includes some but not all of the points in a given
// contour, then inferred deltas must be derived for the points that were not
// included in the point number list.
infer_contour(&range, deltas, raw_deltas, simple_glyph)?;
}
}
}
Ok(())
}
fn infer_contour(
contour_range: &RangeInclusive<u32>,
deltas: &mut [Vector2F],
explicit_deltas: &BTreeMap<u32, (i16, i16)>,
simple_glyph: &SimpleGlyph,
) -> Result<(), ParseError> {
for target in contour_range.clone() {
if explicit_deltas.contains_key(&target) {
continue;
}
// This is an unreferenced point
//
// > First, for any un-referenced point, identify the nearest points before and after, in
// > point number order, that are referenced. Note that the same referenced points will be
// > used for calculating both X and Y inferred deltas. If there is no lower point number
// > from that contour that was referenced, then the highest, referenced point number from
// > that contour is used. Similarly, if no higher point number from that contour was
// > referenced, then the lowest, referenced point number is used.
// NOTE(unwrap): Due to checks above regarding the number of referenced points we should
// always find a next/prev point
let next = explicit_deltas
.range(target..=*contour_range.end())
.chain(explicit_deltas.range(*contour_range.start()..target))
.next()
.unwrap();
let prev = explicit_deltas
.range(target..=*contour_range.end())
.chain(explicit_deltas.range(*contour_range.start()..target))
.next_back()
.unwrap();
let target = usize::safe_from(target);
deltas[target] = infer_delta(target, prev, next, &simple_glyph.coordinates)?;
}
Ok(())
}
// > Once the adjacent, referenced points are identified, then inferred-delta
// > calculation is done
// > separately for X and Y directions.
fn infer_delta(
target: usize,
(prev_number, prev_delta): (&u32, &(i16, i16)),
(next_number, next_delta): (&u32, &(i16, i16)),
coordinates: &[(SimpleGlyphFlag, Point)],
) -> Result<Vector2F, ParseError> {
// https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#inferred-deltas-for-un-referenced-point-numbers
let prev_coord = coordinates
.get(usize::safe_from(*prev_number))
.ok_or(ParseError::BadIndex)?
.1;
let target_coord = coordinates.get(target).ok_or(ParseError::BadIndex)?.1;
let next_coord = coordinates
.get(usize::safe_from(*next_number))
.ok_or(ParseError::BadIndex)?
.1;
let delta_x = do_infer(
prev_coord.0,
target_coord.0,
next_coord.0,
prev_delta.0,
next_delta.0,
);
let delta_y = do_infer(
prev_coord.1,
target_coord.1,
next_coord.1,
prev_delta.1,
next_delta.1,
);
Ok(Vector2F::new(delta_x, delta_y))
}
// > The (X or Y) grid coordinate values of the adjacent, referenced points are compared. If
// > these coordinates are the same, then the delta values for the adjacent points are compared: if
// > the delta values are the same, then this value is used as the inferred delta for the target,
// > un-referenced point. If the delta values are different, then the inferred delta for the target
// > point is zero.
fn do_infer(
prev_coord: i16,
target_coord: i16,
next_coord: i16,
prev_delta: i16,
next_delta: i16,
) -> f32 {
if prev_coord == next_coord {
if prev_delta == next_delta {
prev_delta as f32
} else {
0.
}
} else {
// > But if the coordinate of the target point is not between the coordinates of the
// > adjacent points, then the inferred delta is the delta for whichever of the adjacent
// > points is closer in the given direction.
if target_coord <= prev_coord.min(next_coord) {
if prev_coord < next_coord {
prev_delta as f32
} else {
next_delta as f32
}
} else if target_coord >= prev_coord.max(next_coord) {
if prev_coord > next_coord {
prev_delta as f32
} else {
next_delta as f32
}
} else {
// > If the coordinate of the target point is between the coordinates of the adjacent
// > points, then a delta is interpolated
// > Note: The logical flow of the algorithm to this point implies that the coordinates
// > of the two adjacent points are different. This avoids a division by zero in the
// > following calculations that would otherwise occur.
let proportion =
(target_coord as f32 - prev_coord as f32) / (next_coord as f32 - prev_coord as f32);
(1. - proportion) * prev_delta as f32 + proportion * next_delta as f32
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::binary::read::ReadScope;
use crate::error::ReadWriteError;
use crate::font_data::FontData;
use crate::tables::glyf::GlyfTable;
use crate::tables::loca::LocaTable;
use crate::tables::variable_fonts::avar::AvarTable;
use crate::tables::variable_fonts::fvar::FvarTable;
use crate::tables::{FontTableProvider, HeadTable, MaxpTable, NameTable};
use crate::tests::read_fixture;
use crate::{assert_close, tag};
use pathfinder_geometry::vector::vec2i;
#[test]
fn apply_variations() -> Result<(), ReadWriteError> {
let buffer = read_fixture("tests/fonts/opentype/NotoSans-VF.abc.ttf");
let scope = ReadScope::new(&buffer);
let font_file = scope.read::<FontData<'_>>()?;
let provider = font_file.table_provider(0)?;
let head = ReadScope::new(&provider.read_table_data(tag::HEAD)?).read::<HeadTable>()?;
let maxp = ReadScope::new(&provider.read_table_data(tag::MAXP)?).read::<MaxpTable>()?;
let loca_data = provider.read_table_data(tag::LOCA)?;
let loca = ReadScope::new(&loca_data)
.read_dep::<LocaTable<'_>>((maxp.num_glyphs, head.index_to_loc_format))?;
let glyf_data = provider.read_table_data(tag::GLYF)?;
let mut glyf = ReadScope::new(&glyf_data).read_dep::<GlyfTable<'_>>(&loca)?;
let fvar_data = provider
.read_table_data(tag::FVAR)
.expect("unable to read fvar table data");
let fvar = ReadScope::new(&fvar_data).read::<FvarTable<'_>>().unwrap();
let avar_data = provider.table_data(tag::AVAR)?;
let avar = avar_data
.as_ref()
.map(|avar_data| ReadScope::new(avar_data).read::<AvarTable<'_>>())
.transpose()?;
let gvar_data = provider.read_table_data(tag::GVAR)?;
let gvar = ReadScope::new(&gvar_data).read::<GvarTable<'_>>().unwrap();
let name_table_data = provider
.read_table_data(tag::NAME)
.expect("unable to read name table data");
let name_table = ReadScope::new(&name_table_data)
.read::<NameTable<'_>>()
.unwrap();
// Pick a glyph. Glyph 2 is 'b'
let glyph_index = 2u16;
let glyph = glyf.get_parsed_glyph(glyph_index)?;
// Pick an instance
let mut instance = None;
for inst in fvar.instances() {
let inst = inst?;
let subfamily = name_table.string_for_id(inst.subfamily_name_id);
if subfamily.as_deref() == Some("Display Condensed Thin") {
// - wght = min: 100, max: 900, default: 400
// - wdth = min: 62.5, max: 100, default: 100
// - CTGR = min: 0, max: 100, default: 0
//
// Coordinates: [100.0, 76.24969, 100.0]
instance = Some(inst);
break;
}
}
let user_instance = instance.unwrap();
let instance = fvar
.normalize(user_instance.coordinates.iter(), avar.as_ref())
.unwrap();
let varied = glyph_deltas(glyph, glyph_index, &instance, &gvar)?
.expect("there should be glyph deltas");
// These values were obtained by feeding the same parameters into
// [skrifa](https://docs.rs/crate/skrifa/0.11.0).
let expected_deltas = &[
(-73.86737060546875, -80.800537109375),
(-73.86737060546875, -65.200439453125),
(-71.24325561523438, -51.0),
(-70.29388427734375, -50.599853515625),
(-73.1673583984375, -50.599853515625),
(-84.32525634765625, -30.09991455078125),
(-88.37908935546875, -7.4000244140625),
(-95.0008544921875, -7.4000244140625),
(-114.1365966796875, -7.4000244140625),
(-153.50152587890625, -5.29998779296875),
(-153.50152587890625, 0.10003662109375),
(-153.50152587890625, 4.1998291015625),
(-135.71871948242188, 3.89984130859375),
(-112.06466674804688, 0.0),
(-102.67691040039063, 0.0),
(-92.93865966796875, 0.0),
(-78.8035888671875, 15.49993896484375),
(-71.71092224121094, 31.09991455078125),
(-66.50018310546875, 31.09991455078125),
(-53.50018310546875, 0.0),
(-11.8001708984375, 0.0),
(-11.8001708984375, 0.0),
(-73.86737060546875, 0.0),
(-80.65133666992188, 40.5999755859375),
(-72.0006103515625, 40.5999755859375),
(-70.2852783203125, 27.50006103515625),
(-73.50018310546875, 14.30023193359375),
(-73.50018310546875, 13.70037841796875),
(-73.50018310546875, -23.8001708984375),
(-73.50018310546875, -41.50018310546875),
(-66.0003662109375, -47.29998779296875),
(-90.000732421875, -47.29998779296875),
(-93.10113525390625, -47.29998779296875),
(-90.10150146484375, -27.90008544921875),
(-90.10150146484375, -0.89996337890625),
(-90.10150146484375, 19.0),
(-84.10113525390625, 40.5999755859375),
(0.0, 0.0),
(0.0, 0.0),
(0.0, 0.0),
(0.0, 0.0),
];
assert_eq!(varied.len(), expected_deltas.len());
// Ignore phantom points at end
for (expected, actual) in expected_deltas[..expected_deltas.len() - 4]
.iter()
.copied()
.zip(varied.iter().copied())
{
assert_close!(actual.x(), expected.0, 0.005);
assert_close!(actual.y(), expected.1, 0.005);
}
Ok(())
}
#[test]
#[cfg(feature = "prince")]
fn apply_skia_variations_simple_glyph() -> Result<(), ReadWriteError> {
use crate::tables::Fixed;
let buffer = read_fixture("../../../tests/data/fonts/Skia.subset.ttf");
let scope = ReadScope::new(&buffer);
let font_file = scope.read::<FontData<'_>>()?;
let provider = font_file.table_provider(0)?;
let head = ReadScope::new(&provider.read_table_data(tag::HEAD)?).read::<HeadTable>()?;
let maxp = ReadScope::new(&provider.read_table_data(tag::MAXP)?).read::<MaxpTable>()?;
let loca_data = provider.read_table_data(tag::LOCA)?;
let loca = ReadScope::new(&loca_data)
.read_dep::<LocaTable<'_>>((maxp.num_glyphs, head.index_to_loc_format))?;
let glyf_data = provider.read_table_data(tag::GLYF)?;
let mut glyf = ReadScope::new(&glyf_data).read_dep::<GlyfTable<'_>>(&loca)?;
let fvar_data = provider
.read_table_data(tag::FVAR)
.expect("unable to read fvar table data");
let fvar = ReadScope::new(&fvar_data).read::<FvarTable<'_>>().unwrap();
let avar_data = provider.table_data(tag::AVAR)?;
let avar = avar_data
.as_ref()
.map(|avar_data| ReadScope::new(avar_data).read::<AvarTable<'_>>())
.transpose()?;
let gvar_data = provider.read_table_data(tag::GVAR)?;
let gvar = ReadScope::new(&gvar_data).read::<GvarTable<'_>>().unwrap();
// Pick a glyph. Glyph 45 is '-', this is chosen to replicate the example in the
// spec: https://learn.microsoft.com/en-us/typography/opentype/spec/otvaroverview#interpolation-example
let glyph_index = 45u16;
let glyph = glyf.get_parsed_glyph(glyph_index)?;
// (0.2, 0.7) — a slight weight increase and a large width increase. The example
// gives these are normalised values but we need to supply user values
let user_instance = &[Fixed::from(1.44), Fixed::from(1.21)];
let instance = fvar
.normalize(user_instance.iter().copied(), avar.as_ref())
.unwrap();
let varied = glyph_deltas(glyph, glyph_index, &instance, &gvar)?
.expect("there should be glyph deltas");
let expected_deltas = &[
(162.3, -28.4),
(8.8, -28.4),
(8.8, 36.4),
(162.3, 36.4),
(0., 0.),
(172.7, 0.),
];
for (expected, actual) in expected_deltas.iter().copied().zip(varied.iter().copied()) {
assert_close!(actual.x(), expected.0, 0.005);
assert_close!(actual.y(), expected.1, 0.005);
}
Ok(())
}
#[test]
#[cfg(feature = "prince")]
fn apply_skia_variations_composite_glyph() -> Result<(), ReadWriteError> {
use crate::tables::Fixed;
let buffer = read_fixture("../../../tests/data/fonts/Skia.subset.ttf");
let scope = ReadScope::new(&buffer);
let font_file = scope.read::<FontData<'_>>()?;
let provider = font_file.table_provider(0)?;
let head = ReadScope::new(&provider.read_table_data(tag::HEAD)?).read::<HeadTable>()?;
let maxp = ReadScope::new(&provider.read_table_data(tag::MAXP)?).read::<MaxpTable>()?;
let loca_data = provider.read_table_data(tag::LOCA)?;
let loca = ReadScope::new(&loca_data)
.read_dep::<LocaTable<'_>>((maxp.num_glyphs, head.index_to_loc_format))?;
let glyf_data = provider.read_table_data(tag::GLYF)?;
let mut glyf = ReadScope::new(&glyf_data).read_dep::<GlyfTable<'_>>(&loca)?;
let fvar_data = provider
.read_table_data(tag::FVAR)
.expect("unable to read fvar table data");
let fvar = ReadScope::new(&fvar_data).read::<FvarTable<'_>>().unwrap();
let avar_data = provider.table_data(tag::AVAR)?;
let avar = avar_data
.as_ref()
.map(|avar_data| ReadScope::new(avar_data).read::<AvarTable<'_>>())
.transpose()?;
let gvar_data = provider.read_table_data(tag::GVAR)?;
let gvar = ReadScope::new(&gvar_data).read::<GvarTable<'_>>().unwrap();
// Pick a glyph. Glyph 128 of the Skia font, which is the glyph for “Ä”. The
// glyph entry has two component entries, both with ARGS_ARE_XY_VALUES
// set. https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#point-numbers-and-processing-for-composite-glyphs
let glyph_index = 128u16;
let glyph = glyf.get_parsed_glyph(glyph_index)?;
// (0.2, 0.7) — a slight weight increase and a large width increase. The example
// gives these are normalised values but we need to supply user values
let user_instance = &[Fixed::from(1.44), Fixed::from(1.21)];
let instance = fvar
.normalize(user_instance.iter().copied(), avar.as_ref())
.unwrap();
let varied = glyph_deltas(glyph, glyph_index, &instance, &gvar)?
.expect("there should be glyph deltas");
// The example in the spec appears to be wrong, thus the final values don't
// match. R3 in the example is supposed to correspond to the region
// (weight, width) of (1, 1) however they seem to have used the values
// from the (-1, 1) region. To try to rule out the example
// using a different version of the font I confirmed this with versions of Skia
// from Mac OS 7.6.1 and macOS 11.7.1 (Big Sur) and they were the same.
//
// Tracked by: https://github.com/MicrosoftDocs/typography-issues/issues/1067
let r1_scale = 0.2;
let r2_scale = 0.7;
let r3_scale = 0.14;
let expected_deltas = &[
(0., 0.),
((r1_scale * 69.) + (r2_scale * 53.) + (r3_scale * -8.), 0.),
((r1_scale * 58.) + (r2_scale * 38.) + (r3_scale * -30.), 0.),
((r1_scale * 145.) + (r2_scale * 351.) + (r3_scale * 0.), 0.),
];
for (expected, actual) in expected_deltas.iter().copied().zip(varied.iter().copied()) {
assert_close!(actual.x(), expected.0, 0.01);
assert_close!(actual.y(), expected.1, 0.01);
}
Ok(())
}
#[test]
fn infer_unreferenced_points_test() {
// The data used in this test is extracted from the RobotoFlex font 'j' glyph.
// The inference was not working properly for point 7 of the first contour.
let mut deltas = vec![
vec2f(24.0, -1.0),
vec2f(19.0, -2.0),
vec2f(45.0, 0.0),
vec2f(39.0, 0.0),
vec2f(101.0, 0.0),
vec2f(193.0, 38.0),
vec2f(193.0, 48.0),
vec2f(0.0, 0.0), // This is the one that interpolation was not populating
vec2f(-30.0, 6.0),
vec2f(-30.0, 139.0),
vec2f(-30.0, 135.0),
vec2f(-1.0, 135.0),
vec2f(14.0, 135.0),
vec2f(13.0, 135.0),
vec2f(15.0, 135.0),
vec2f(22.0, 135.0),
vec2f(-36.0, -45.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
vec2f(81.0, -144.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
vec2f(198.0, -45.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
vec2f(82.0, 52.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
vec2f(135.0, 0.0),
vec2f(0.0, 0.0),
vec2f(0.0, 0.0),
];
let region_deltas = [
(0, (24, -1)),
(1, (19, -2)),
(2, (45, 0)),
(3, (39, 0)),
(4, (101, 0)),
(5, (193, 38)),
(6, (193, 48)),
(8, (-30, 6)),
(9, (-30, 139)),
(10, (-30, 135)),
(11, (-1, 135)),
(12, (14, 135)),
(13, (13, 135)),
(14, (15, 135)),
(15, (22, 135)),
(16, (-36, -45)),
(19, (81, -144)),
(22, (198, -45)),
(25, (82, 52)),
(29, (135, 0)),
];
let explicit_deltas = IntoIterator::into_iter(region_deltas).collect::<BTreeMap<_, _>>();
// let raw_deltas: &BTreeMap<u32, (i16, i16)> = ;
// let simple_glyph: &SimpleGlyph<'_> = ;
let glyph = SimpleGlyph {
bounding_box: BoundingBox {
x_min: -94,
x_max: 366,
y_min: -436,
y_max: 1481,
},
end_pts_of_contours: vec![15, 27],
instructions: Box::default(),
coordinates: vec![
(
SimpleGlyphFlag::ON_CURVE_POINT | SimpleGlyphFlag::X_SHORT_VECTOR,
Point(-94, -410),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(-69, -419),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(30, -436),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(70, -436),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(204, -436),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(343, -270),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(343, -90),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(343, 1052),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(157, 1052),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(157, -130),
),
(
SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(157, -210),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR | SimpleGlyphFlag::Y_SHORT_VECTOR,
Point(90, -280),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(30, -280),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(0, -280),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(-74, -266),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(-94, -260),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(134, 1370),
),
(
SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(134, 1323),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR,
Point(194, 1259),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(250, 1259),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(306, 1259),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(366, 1323),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(366, 1370),
),
(
SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(366, 1417),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(306, 1481),
),
(
SimpleGlyphFlag::ON_CURVE_POINT
| SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(250, 1481),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR
| SimpleGlyphFlag::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR,
Point(194, 1481),
),
(
SimpleGlyphFlag::X_SHORT_VECTOR | SimpleGlyphFlag::Y_SHORT_VECTOR,
Point(134, 1417),
),
],
phantom_points: None,
};
let expected = [
vec2i(24, -1),
vec2i(19, -2),
vec2i(45, 0),
vec2i(39, 0),
vec2i(101, 0),
vec2i(193, 38),
vec2i(193, 48),
vec2i(193, 6),
vec2i(-30, 6),
vec2i(-30, 139),
vec2i(-30, 135),
vec2i(-1, 135),
vec2i(14, 135),
vec2i(13, 135),
vec2i(15, 135),
vec2i(22, 135),
vec2i(-36, -45),
vec2i(-36, -87),
vec2i(25, -144),
vec2i(81, -144),
vec2i(137, -144),
vec2i(198, -87),
vec2i(198, -45),
vec2i(198, -4),
vec2i(138, 52),
vec2i(82, 52),
vec2i(25, 52),
vec2i(-36, -4),
vec2i(0, 0),
vec2i(135, 0),
vec2i(0, 0),
vec2i(0, 0),
];
infer_unreferenced_points(&mut deltas, &explicit_deltas, &glyph).unwrap();
// round actual values for comparison
let deltas = deltas
.into_iter()
.map(|delta| delta.round().to_i32())
.collect::<Vec<_>>();
assert_eq!(deltas, expected);
}
}