Skip to content

Commit

Permalink
Parse .glyphs grayscale colors
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Mar 2, 2025
1 parent 3403986 commit 9d9eab9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 12 deletions.
65 changes: 53 additions & 12 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,31 @@ impl FromPlist for Color {
let stop_offset = tokenizer.parse::<f64>()?;
tokenizer.eat(b')')?;

if colors.len() != 4 {
return Err(crate::plist::Error::UnexpectedNumberOfValues {
// See <https://github.com/googlefonts/glyphsLib/blob/c4db6b981d577f456d64ebe9993818770e170454/Lib/glyphsLib/builder/common.py#L41-L50>
match colors.len() {
// Grayscale
2 => Ok(Color {
r: colors[0],
g: colors[0],
b: colors[0],
a: colors[1],
stop_offset: stop_offset.into(),
}),
// RGB
4 => Ok(Color {
r: colors[0],
g: colors[1],
b: colors[2],
a: colors[3],
stop_offset: stop_offset.into(),
}),
// 5 is CMYK, match python by not supporting that
v => Err(crate::plist::Error::UnexpectedNumberOfValues {
value_type: "rgba values",
expected: 4,
actual: colors.len(),
});
actual: v,
}),
}

Ok(Color {
r: colors[0],
g: colors[1],
b: colors[2],
a: colors[3],
stop_offset: stop_offset.into(),
})
}
}

Expand Down Expand Up @@ -3912,4 +3922,35 @@ mod tests {
.collect::<HashSet<_>>()
);
}

#[test]
fn parse_grayscale_colors() {
let font = Font::load(&glyphs3_dir().join("COLRv1-grayscale.glyphs")).unwrap();
assert_eq!(
vec![
Color {
r: 64,
g: 64,
b: 64,
a: 255,
stop_offset: 0.into(),
},
Color {
r: 0,
g: 0,
b: 0,
a: 255,
stop_offset: 1.into(),
},
],
font.glyphs
.values()
.flat_map(|g| g
.layers
.iter()
.flat_map(|l| l.shapes.iter())
.flat_map(|s| (s.attributes().gradient.colors.iter().cloned())))
.collect::<Vec<_>>()
);
}
}
56 changes: 56 additions & 0 deletions resources/testdata/glyphs3/COLRv1-grayscale.glyphs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
.appVersion = "3343";
.formatVersion = 3;
familyName = "New Font";
fontMaster = (
{
id = m01;
name = Regular;
}
);
glyphs = (
{
glyphname = A;
layers = (
{
attr = {
color = 1;
};
layerId = m01;
shapes = (
{
attr = {
gradient = {
colors = (
(
(64,255),
0
),
(
(0,255),
1
)
);
end = (0.9,0.9);
start = (0.1,0.1);
};
};
closed = 1;
nodes = (
(63,0,l),
(542,0,l),
(542,500,l),
(63,500,l)
);
}
);
width = 600;
}
);
unicode = 65;
}
);
unitsPerEm = 1000;
versionMajor = 1;
versionMinor = 0;
}

0 comments on commit 9d9eab9

Please sign in to comment.