Skip to content

Commit

Permalink
Merge pull request #716 from googlefonts/nullable-stat-axis-values
Browse files Browse the repository at this point in the history
[read/write-fonts] make STAT offset_to_axis_values nullable
  • Loading branch information
anthrotype authored Nov 22, 2023
2 parents dec4e1b + f99b59d commit 58ba78e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions read-fonts/generated/generated_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ impl<'a> Stat<'a> {
/// start of the design axes value offsets array. If axisValueCount
/// is zero, set to zero; if axisValueCount is greater than zero,
/// must be greater than zero.
pub fn offset_to_axis_value_offsets(&self) -> Offset32 {
pub fn offset_to_axis_value_offsets(&self) -> Nullable<Offset32> {
let range = self.shape.offset_to_axis_value_offsets_byte_range();
self.data.read_at(range.start).unwrap()
}

/// Attempt to resolve [`offset_to_axis_value_offsets`][Self::offset_to_axis_value_offsets].
pub fn offset_to_axis_values(&self) -> Result<AxisValueArray<'a>, ReadError> {
pub fn offset_to_axis_values(&self) -> Option<Result<AxisValueArray<'a>, ReadError>> {
let data = self.data;
let args = self.axis_value_count();
self.offset_to_axis_value_offsets()
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/src/tables/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests {
assert_eq!(axis_record.axis_tag(), Tag::new(b"wght"));
assert_eq!(axis_record.axis_name_id(), NameId::new(257));
assert_eq!(axis_record.axis_ordering(), 0);
let axis_values = table.offset_to_axis_values().unwrap();
let axis_values = table.offset_to_axis_values().unwrap().unwrap();
let axis_values = axis_values
.axis_values()
.iter()
Expand Down
3 changes: 2 additions & 1 deletion resources/codegen_inputs/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ table Stat {
/// start of the design axes value offsets array. If axisValueCount
/// is zero, set to zero; if axisValueCount is greater than zero,
/// must be greater than zero.
#[nullable]
#[read_offset_with($axis_value_count)]
#[compile_type(OffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32>)]
#[compile_type(NullableOffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32>)]
#[to_owned(convert_axis_value_offsets(obj.offset_to_axis_values()))]
offset_to_axis_value_offsets: Offset32<AxisValueArray>,
/// Name ID used as fallback when projection of names into a
Expand Down
2 changes: 1 addition & 1 deletion write-fonts/generated/generated_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Stat {
/// start of the design axes value offsets array. If axisValueCount
/// is zero, set to zero; if axisValueCount is greater than zero,
/// must be greater than zero.
pub offset_to_axis_values: OffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32>,
pub offset_to_axis_values: NullableOffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32>,
/// Name ID used as fallback when projection of names into a
/// particular font model produces a subfamily name containing only
/// elidable elements.
Expand Down
22 changes: 13 additions & 9 deletions write-fonts/src/tables/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ impl Stat {
) -> Self {
Stat {
design_axes: design_axes.into(),
offset_to_axis_values: OffsetMarker::new(
axis_values.into_iter().map(Into::into).collect(),
offset_to_axis_values: NullableOffsetMarker::new(
(!axis_values.is_empty())
.then(|| axis_values.into_iter().map(Into::into).collect()),
),
elided_fallback_name_id: Some(elided_fallback_name_id),
}
Expand All @@ -24,12 +25,15 @@ impl Stat {
// but in write-fonts we want to skip the shim table and just use a vec.
#[allow(clippy::unwrap_or_default)] // we need to be explicit to provide type info
fn convert_axis_value_offsets(
from: Result<read_fonts::tables::stat::AxisValueArray, ReadError>,
) -> OffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32> {
from.ok()
.map(|array| array.axis_values().to_owned_obj(array.offset_data()))
.unwrap_or_else(Vec::new)
.into()
from: Option<Result<read_fonts::tables::stat::AxisValueArray, ReadError>>,
) -> NullableOffsetMarker<Vec<OffsetMarker<AxisValue>>, WIDTH_32> {
from.map(|inner| {
inner
.ok()
.map(|array| array.axis_values().to_owned_obj(array.offset_data()))
.unwrap_or_else(Vec::new)
})
.into()
}

#[cfg(test)]
Expand Down Expand Up @@ -65,7 +69,7 @@ mod tests {

assert_eq!(read.design_axes().unwrap().len(), 1);
assert_eq!(read.axis_value_count(), 2);
let axis_values = read.offset_to_axis_values().unwrap();
let axis_values = read.offset_to_axis_values().unwrap().unwrap();
assert_eq!(axis_values.axis_value_offsets().len(), 2);
let value2 = axis_values.axis_values().get(1).unwrap();
let read_stat::AxisValue::Format1(value2) = value2 else {
Expand Down

0 comments on commit 58ba78e

Please sign in to comment.