Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow .glyphs to specify fsType #621

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct RawAxisUserToDesignMap(Vec<(OrderedFloat<f32>, OrderedFloat<f32>)>);
#[derive(Debug, PartialEq, Hash)]
pub struct Font {
pub units_per_em: u16,
pub fs_type: Option<u16>,
pub use_typo_metrics: Option<bool>,
pub has_wws_names: Option<bool>,
pub axes: Vec<Axis>,
Expand Down Expand Up @@ -342,6 +343,13 @@ impl CustomParameters {
None
})
}

fn fs_type(&self) -> Option<&Vec<i64>> {
let Some(CustomParameterValue::FsType(bits)) = self.get("fsType") else {
return None;
};
Some(bits)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -353,6 +361,7 @@ enum CustomParameterValue {
AxisLocations(Vec<AxisLocation>),
GlyphOrder(Vec<String>),
VirtualMaster(Vec<AxisLocation>),
FsType(Vec<i64>),
}

/// Hand-parse these because they take multiple shapes
Expand Down Expand Up @@ -442,6 +451,12 @@ impl FromPlist for CustomParameters {
value =
Some(CustomParameterValue::VirtualMaster(tokenizer.parse()?));
}
_ if name == Some(String::from("fsType")) => {
let Token::OpenParen = peek else {
return Err(Error::UnexpectedChar('('));
};
value = Some(CustomParameterValue::FsType(tokenizer.parse()?));
}
_ => tokenizer.skip_rec()?,
}
}
Expand Down Expand Up @@ -1840,6 +1855,11 @@ impl TryFrom<RawFont> for Font {
};
let units_per_em = units_per_em.try_into().map_err(Error::InvalidUpem)?;

let fs_type = from
.custom_parameters
.fs_type()
.map(|bits| bits.iter().map(|bit| 1 << bit).sum());

let mut names = BTreeMap::new();
for name in from.properties {
// TODO: we only support dflt, .glyphs l10n names are ignored
Expand Down Expand Up @@ -1908,6 +1928,7 @@ impl TryFrom<RawFont> for Font {

Ok(Font {
units_per_em,
fs_type,
use_typo_metrics,
has_wws_names,
axes,
Expand Down Expand Up @@ -2728,4 +2749,22 @@ mod tests {
.collect::<Vec<_>>()
);
}

#[test]
fn read_fstype_none() {
let font = Font::load(&glyphs3_dir().join("infinity.glyphs")).unwrap();
assert!(font.fs_type.is_none());
}

#[test]
fn read_fstype_zero() {
let font = Font::load(&glyphs3_dir().join("fstype_0x0000.glyphs")).unwrap();
assert_eq!(Some(0), font.fs_type);
}

#[test]
fn read_fstype_bits() {
let font = Font::load(&glyphs3_dir().join("fstype_0x0104.glyphs")).unwrap();
assert_eq!(Some(0x104), font.fs_type);
}
}
18 changes: 16 additions & 2 deletions glyphs2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl GlyphsIrSource {
// Explicitly field by field so if we add more compiler will force us to update here
let font = Font {
units_per_em: font.units_per_em,
fs_type: font.fs_type,
use_typo_metrics: font.use_typo_metrics,
has_wws_names: font.has_wws_names,
axes: font.axes.clone(),
Expand Down Expand Up @@ -115,6 +116,7 @@ impl GlyphsIrSource {
// Explicitly field by field so if we add more compiler will force us to update here
let font = Font {
units_per_em: font.units_per_em,
fs_type: None,
use_typo_metrics: font.use_typo_metrics,
has_wws_names: None,
axes: font.axes.clone(),
Expand Down Expand Up @@ -370,8 +372,8 @@ impl Work<Context, WorkId, WorkError> for StaticMetadataWork {
Tag::from_str(vendor_id).map_err(WorkError::InvalidTag)?;
}

// <https://github.com/googlefonts/glyphsLib/blob/cb8a4a914b0a33431f0a77f474bf57eec2f19bcc/Lib/glyphsLib/builder/custom_params.py#L1117-L1119>
static_metadata.misc.fs_type = Some(1 << 3);
// Default per <https://github.com/googlefonts/glyphsLib/blob/cb8a4a914b0a33431f0a77f474bf57eec2f19bcc/Lib/glyphsLib/builder/custom_params.py#L1117-L1119>
static_metadata.misc.fs_type = font.fs_type.or(Some(1 << 3));

// <https://github.com/googlefonts/glyphsLib/blob/main/Lib/glyphsLib/builder/custom_params.py#L1116-L1125>
static_metadata.misc.underline_thickness = 50.0.into();
Expand Down Expand Up @@ -1532,4 +1534,16 @@ mod tests {
]
);
}

#[test]
fn reads_fs_type_0x0000() {
let (_, context) = build_static_metadata(glyphs3_dir().join("fstype_0x0000.glyphs"));
assert_eq!(Some(0), context.static_metadata.get().misc.fs_type);
}

#[test]
fn reads_fs_type_0x0104() {
let (_, context) = build_static_metadata(glyphs3_dir().join("fstype_0x0104.glyphs"));
assert_eq!(Some(0x104), context.static_metadata.get().misc.fs_type);
}
}
40 changes: 40 additions & 0 deletions resources/testdata/glyphs3/fstype_0x0000.glyphs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
.appVersion = "3219";
.formatVersion = 3;
axes = (
{
name = Weight;
tag = wght;
}
);
customParameters = (
{
name = fsType;
value = (
);
}
);
date = "2023-09-20 08:55:41 +0000";
familyName = fstype;
fontMaster = (
{
axesValues = (
400
);
id = m01;
name = Regular;
}
);
glyphs = (
{
glyphname = fstype;
layers = (
{
layerId = m01;
width = 600;
}
);
}
);
unitsPerEm = 1000;
}
42 changes: 42 additions & 0 deletions resources/testdata/glyphs3/fstype_0x0104.glyphs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
.appVersion = "3219";
.formatVersion = 3;
axes = (
{
name = Weight;
tag = wght;
}
);
customParameters = (
{
name = fsType;
value = (
2,
8
);
}
);
date = "2023-09-20 08:55:41 +0000";
familyName = fstype;
fontMaster = (
{
axesValues = (
400
);
id = m01;
name = Regular;
}
);
glyphs = (
{
glyphname = fstype;
layers = (
{
layerId = m01;
width = 600;
}
);
}
);
unitsPerEm = 1000;
}