Skip to content

Commit

Permalink
[codegen] Better handling of computed lengths
Browse files Browse the repository at this point in the history
This fixes an issue where we were incorrectly truncating the all
computed lengths to be u16s, even if the value would be stored as a u32.

We now keep computed values as usize and then generate a conversion for
the appropriate type only at the callsite, instead of blindly converting
to a u16 earlier on.
  • Loading branch information
cmyr committed Nov 19, 2024
1 parent 7f395a7 commit e717405
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 105 deletions.
2 changes: 1 addition & 1 deletion font-codegen/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ impl Field {
let typ = self.typ.cooked_type_tokens();
let expr = inline_expr.compile_expr();
if !inline_expr.referenced_fields.is_empty() {
quote!(#expr.unwrap() as #typ)
quote!( #typ :: try_from(#expr).unwrap() )
} else {
quote!(#expr as #typ)
}
Expand Down
4 changes: 2 additions & 2 deletions write-fonts/generated/generated_avar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FontWrite for Avar {
let version = self.compute_version() as MajorMinor;
version.write_into(writer);
(0 as u16).write_into(writer);
(array_len(&self.axis_segment_maps).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.axis_segment_maps)).unwrap()).write_into(writer);
self.axis_segment_maps.write_into(writer);
version
.compatible((2u16, 0u16))
Expand Down Expand Up @@ -113,7 +113,7 @@ impl SegmentMaps {
impl FontWrite for SegmentMaps {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.axis_value_maps).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.axis_value_maps)).unwrap()).write_into(writer);
self.axis_value_maps.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
10 changes: 5 additions & 5 deletions write-fonts/generated/generated_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl BaseTagList {
impl FontWrite for BaseTagList {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.baseline_tags).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.baseline_tags)).unwrap()).write_into(writer);
self.baseline_tags.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -222,7 +222,7 @@ impl BaseScriptList {
impl FontWrite for BaseScriptList {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.base_script_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.base_script_records)).unwrap()).write_into(writer);
self.base_script_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -346,7 +346,7 @@ impl FontWrite for BaseScript {
fn write_into(&self, writer: &mut TableWriter) {
self.base_values.write_into(writer);
self.default_min_max.write_into(writer);
(array_len(&self.base_lang_sys_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.base_lang_sys_records)).unwrap()).write_into(writer);
self.base_lang_sys_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -472,7 +472,7 @@ impl FontWrite for BaseValues {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
self.default_baseline_index.write_into(writer);
(array_len(&self.base_coords).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.base_coords)).unwrap()).write_into(writer);
self.base_coords.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -545,7 +545,7 @@ impl FontWrite for MinMax {
fn write_into(&self, writer: &mut TableWriter) {
self.min_coord.write_into(writer);
self.max_coord.write_into(writer);
(array_len(&self.feat_min_max_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.feat_min_max_records)).unwrap()).write_into(writer);
self.feat_min_max_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
6 changes: 3 additions & 3 deletions write-fonts/generated/generated_cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FontWrite for Cmap {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(0 as u16).write_into(writer);
(array_len(&self.encoding_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.encoding_records)).unwrap()).write_into(writer);
self.encoding_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -592,7 +592,7 @@ impl FontWrite for Cmap4 {
(4 as u16).write_into(writer);
(self.compute_length() as u16).write_into(writer);
self.language.write_into(writer);
(2 * array_len(&self.end_code).unwrap() as u16).write_into(writer);
(u16::try_from(2 * array_len(&self.end_code)).unwrap()).write_into(writer);
(self.compute_search_range() as u16).write_into(writer);
(self.compute_entry_selector() as u16).write_into(writer);
(self.compute_range_shift() as u16).write_into(writer);
Expand Down Expand Up @@ -963,7 +963,7 @@ impl FontWrite for Cmap12 {
(0 as u16).write_into(writer);
(self.compute_length() as u32).write_into(writer);
self.language.write_into(writer);
(array_len(&self.groups).unwrap() as u32).write_into(writer);
(u32::try_from(array_len(&self.groups)).unwrap()).write_into(writer);
self.groups.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
2 changes: 1 addition & 1 deletion write-fonts/generated/generated_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl FontWrite for TableDirectory {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
self.sfnt_version.write_into(writer);
(array_len(&self.table_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.table_records)).unwrap()).write_into(writer);
self.search_range.write_into(writer);
self.entry_selector.write_into(writer);
self.range_shift.write_into(writer);
Expand Down
10 changes: 5 additions & 5 deletions write-fonts/generated/generated_gdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl FontWrite for AttachList {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
self.coverage.write_into(writer);
(array_len(&self.attach_points).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.attach_points)).unwrap()).write_into(writer);
self.attach_points.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -213,7 +213,7 @@ impl AttachPoint {
impl FontWrite for AttachPoint {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.point_indices).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.point_indices)).unwrap()).write_into(writer);
self.point_indices.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -275,7 +275,7 @@ impl FontWrite for LigCaretList {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
self.coverage.write_into(writer);
(array_len(&self.lig_glyphs).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.lig_glyphs)).unwrap()).write_into(writer);
self.lig_glyphs.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -337,7 +337,7 @@ impl LigGlyph {
impl FontWrite for LigGlyph {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.caret_values).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.caret_values)).unwrap()).write_into(writer);
self.caret_values.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -650,7 +650,7 @@ impl FontWrite for MarkGlyphSets {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
(array_len(&self.coverages).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.coverages)).unwrap()).write_into(writer);
self.coverages.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
18 changes: 9 additions & 9 deletions write-fonts/generated/generated_gpos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl MarkArray {
impl FontWrite for MarkArray {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.mark_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.mark_records)).unwrap()).write_into(writer);
self.mark_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -837,7 +837,7 @@ impl FontWrite for SinglePosFormat2 {
(2 as u16).write_into(writer);
self.coverage.write_into(writer);
(self.compute_value_format() as ValueFormat).write_into(writer);
(array_len(&self.value_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.value_records)).unwrap()).write_into(writer);
self.value_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1002,7 +1002,7 @@ impl FontWrite for PairPosFormat1 {
self.coverage.write_into(writer);
(self.compute_value_format1() as ValueFormat).write_into(writer);
(self.compute_value_format2() as ValueFormat).write_into(writer);
(array_len(&self.pair_sets).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.pair_sets)).unwrap()).write_into(writer);
self.pair_sets.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1063,7 +1063,7 @@ impl PairSet {
impl FontWrite for PairSet {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.pair_value_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.pair_value_records)).unwrap()).write_into(writer);
self.pair_value_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1371,7 +1371,7 @@ impl FontWrite for CursivePosFormat1 {
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.entry_exit_record).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.entry_exit_record)).unwrap()).write_into(writer);
self.entry_exit_record.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1578,7 +1578,7 @@ impl BaseArray {
impl FontWrite for BaseArray {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.base_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.base_records)).unwrap()).write_into(writer);
self.base_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1774,7 +1774,7 @@ impl LigatureArray {
impl FontWrite for LigatureArray {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.ligature_attaches).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.ligature_attaches)).unwrap()).write_into(writer);
self.ligature_attaches.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1823,7 +1823,7 @@ impl LigatureAttach {
impl FontWrite for LigatureAttach {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.component_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.component_records)).unwrap()).write_into(writer);
self.component_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -2018,7 +2018,7 @@ impl Mark2Array {
impl FontWrite for Mark2Array {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.mark2_records).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.mark2_records)).unwrap()).write_into(writer);
self.mark2_records.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
22 changes: 11 additions & 11 deletions write-fonts/generated/generated_gsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl FontWrite for SingleSubstFormat2 {
fn write_into(&self, writer: &mut TableWriter) {
(2 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.substitute_glyph_ids).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.substitute_glyph_ids)).unwrap()).write_into(writer);
self.substitute_glyph_ids.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -477,7 +477,7 @@ impl FontWrite for MultipleSubstFormat1 {
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.sequences).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.sequences)).unwrap()).write_into(writer);
self.sequences.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -539,7 +539,7 @@ impl Sequence {
impl FontWrite for Sequence {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.substitute_glyph_ids).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.substitute_glyph_ids)).unwrap()).write_into(writer);
self.substitute_glyph_ids.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -603,7 +603,7 @@ impl FontWrite for AlternateSubstFormat1 {
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.alternate_sets).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.alternate_sets)).unwrap()).write_into(writer);
self.alternate_sets.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -671,7 +671,7 @@ impl AlternateSet {
impl FontWrite for AlternateSet {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.alternate_glyph_ids).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.alternate_glyph_ids)).unwrap()).write_into(writer);
self.alternate_glyph_ids.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -735,7 +735,7 @@ impl FontWrite for LigatureSubstFormat1 {
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.ligature_sets).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.ligature_sets)).unwrap()).write_into(writer);
self.ligature_sets.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -798,7 +798,7 @@ impl LigatureSet {
impl FontWrite for LigatureSet {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
(array_len(&self.ligatures).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.ligatures)).unwrap()).write_into(writer);
self.ligatures.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -860,7 +860,7 @@ impl FontWrite for Ligature {
#[allow(clippy::unnecessary_cast)]
fn write_into(&self, writer: &mut TableWriter) {
self.ligature_glyph.write_into(writer);
(plus_one(&self.component_glyph_ids.len()).unwrap() as u16).write_into(writer);
(u16::try_from(plus_one(&self.component_glyph_ids.len())).unwrap()).write_into(writer);
self.component_glyph_ids.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down Expand Up @@ -1120,11 +1120,11 @@ impl FontWrite for ReverseChainSingleSubstFormat1 {
fn write_into(&self, writer: &mut TableWriter) {
(1 as u16).write_into(writer);
self.coverage.write_into(writer);
(array_len(&self.backtrack_coverages).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.backtrack_coverages)).unwrap()).write_into(writer);
self.backtrack_coverages.write_into(writer);
(array_len(&self.lookahead_coverages).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.lookahead_coverages)).unwrap()).write_into(writer);
self.lookahead_coverages.write_into(writer);
(array_len(&self.substitute_glyph_ids).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.substitute_glyph_ids)).unwrap()).write_into(writer);
self.substitute_glyph_ids.write_into(writer);
}
fn table_type(&self) -> TableType {
Expand Down
2 changes: 1 addition & 1 deletion write-fonts/generated/generated_gvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl FontWrite for Gvar {
fn write_into(&self, writer: &mut TableWriter) {
(MajorMinor::VERSION_1_0 as MajorMinor).write_into(writer);
self.axis_count.write_into(writer);
(array_len(&self.shared_tuples).unwrap() as u16).write_into(writer);
(u16::try_from(array_len(&self.shared_tuples)).unwrap()).write_into(writer);
self.shared_tuples.write_into(writer);
(self.compute_glyph_count() as u16).write_into(writer);
(self.compute_flags() as GvarFlags).write_into(writer);
Expand Down
Loading

0 comments on commit e717405

Please sign in to comment.