Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Sep 14, 2023
1 parent ddeb0d7 commit 71110dd
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 13 deletions.
1 change: 1 addition & 0 deletions fontdrasil/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ impl Display for GlyphName {
}

pub type GroupName = GlyphName;
pub type AnchorName = GlyphName;
26 changes: 25 additions & 1 deletion fontir/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
use chrono::{DateTime, Utc};
use font_types::NameId;
use font_types::Tag;
use fontdrasil::types::{GlyphName, GroupName};
use fontdrasil::types::{GlyphName, GroupName, AnchorName};
use indexmap::IndexSet;
use kurbo::{Affine, BezPath, PathEl, Point};
use log::warn;
Expand Down Expand Up @@ -821,6 +821,20 @@ impl Features {
}
}

/// The complete set of anchor data
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Anchors {
}

/// A variable definition of an anchor.
///
/// Must have at least one definition, at the default location.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Anchor {
pub name: AnchorName,
pub positions: HashMap<NormalizedLocation, Point>,
}

/// A variable definition of a single glyph.
///
/// Guarrantees at least one definition. Currently that must be at
Expand Down Expand Up @@ -954,6 +968,16 @@ impl Persistable for Kerning {
}
}

impl Persistable for Anchors {
fn read(from: &mut dyn Read) -> Self {
serde_yaml::from_reader(from).unwrap()
}

fn write(&self, to: &mut dyn std::io::Write) {
serde_yaml::to_writer(to, self).unwrap();
}
}

/// A variable definition of a single glyph.
///
/// If defined in many locations, presumed to vary continuously
Expand Down
8 changes: 6 additions & 2 deletions fontir/src/orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ pub enum WorkId {
GlyphOrder,
Features,
Kerning,
Anchors,
}

impl Identifier for WorkId {}
Expand Down Expand Up @@ -370,6 +371,7 @@ pub struct Context {
pub glyphs: FeContextMap<ir::Glyph>,
pub features: FeContextItem<ir::Features>,
pub kerning: FeContextItem<ir::Kerning>,
pub anchors: FeContextItem<ir::Anchors>,
}

pub fn set_cached<T>(lock: &Arc<RwLock<Option<Arc<T>>>>, value: T) {
Expand All @@ -390,7 +392,8 @@ impl Context {
global_metrics: self.global_metrics.clone_with_acl(acl.clone()),
glyphs: self.glyphs.clone_with_acl(acl.clone()),
features: self.features.clone_with_acl(acl.clone()),
kerning: self.kerning.clone_with_acl(acl),
kerning: self.kerning.clone_with_acl(acl.clone()),
anchors: self.anchors.clone_with_acl(acl),
}
}

Expand Down Expand Up @@ -426,7 +429,8 @@ impl Context {
),
glyphs: ContextMap::new(acl.clone(), persistent_storage.clone()),
features: ContextItem::new(WorkId::Features, acl.clone(), persistent_storage.clone()),
kerning: ContextItem::new(WorkId::Kerning, acl, persistent_storage),
kerning: ContextItem::new(WorkId::Kerning, acl.clone(), persistent_storage.clone()),
anchors: ContextItem::new(WorkId::Anchors, acl, persistent_storage),
}
}

Expand Down
1 change: 1 addition & 0 deletions fontir/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Paths {

pub fn target_file(&self, id: &WorkId) -> PathBuf {
match id {
WorkId::Anchors => self.build_dir.join("anchors.yml"),
WorkId::StaticMetadata => self.build_dir.join("static_metadata.yml"),
WorkId::PreliminaryGlyphOrder => self.build_dir.join("glyph_order.preliminary.yml"),
WorkId::GlyphOrder => self.build_dir.join("glyph_order.yml"),
Expand Down
33 changes: 23 additions & 10 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub enum Shape {

// The font you get directly from a plist, minimally modified
// Types chosen specifically to accomodate plist translation.
#[derive(Debug, FromPlist, PartialEq, Eq)]
#[derive(Debug, FromPlist, PartialEq)]
#[allow(non_snake_case)]
struct RawFont {
pub units_per_em: Option<i64>,
Expand Down Expand Up @@ -166,7 +166,7 @@ pub struct Axis {
pub hidden: Option<bool>,
}

#[derive(Clone, Debug, FromPlist, PartialEq, Eq)]
#[derive(Clone, Debug, FromPlist, PartialEq)]
pub struct RawGlyph {
pub layers: Vec<RawLayer>,
pub glyphname: String,
Expand All @@ -176,15 +176,15 @@ pub struct RawGlyph {
pub other_stuff: BTreeMap<String, Plist>,
}

#[derive(Clone, Debug, FromPlist, PartialEq, Eq)]
#[derive(Clone, Debug, FromPlist, PartialEq)]
pub struct RawLayer {
pub layer_id: String,
pub associated_master_id: Option<String>,
pub width: OrderedFloat<f64>,
shapes: Option<Vec<RawShape>>,
paths: Option<Vec<Path>>,
components: Option<Vec<Component>>,
//pub anchors: Option<Vec<Anchor>>,
pub anchors: Option<Vec<Anchor>>,
#[fromplist(rest)]
pub other_stuff: BTreeMap<String, Plist>,
}
Expand Down Expand Up @@ -275,8 +275,8 @@ pub enum NodeType {

#[derive(Clone, Debug, FromPlist, PartialEq)]
pub struct Anchor {
pub name: String,
pub position: Point,
pub name: Option<String>,
pub pos: Option<Point>,
}

#[derive(Clone, Debug, PartialEq, Hash)]
Expand Down Expand Up @@ -541,10 +541,18 @@ impl FromPlist for Affine {
impl FromPlist for Point {
fn from_plist(plist: Plist) -> Self {
let raw = plist.as_str().unwrap();
let raw = &raw[1..raw.len() - 1];
let coords: Vec<f64> = raw.split(", ").map(|c| c.parse().unwrap()).collect();
Point::new(coords[0], coords[1])
eprintln!("parse point from {plist:#?}");
match plist {
Plist::Array(values) if values.len() == 2 => {
Point::new(values[0].as_f64().unwrap(), values[1].as_f64().unwrap())
},
Plist::String(value) => {
let raw = &value[1..value.len() - 1];
let coords: Vec<f64> = raw.split(", ").map(|c| c.parse().unwrap()).collect();
Point::new(coords[0], coords[1])
},
_ => panic!("Cannot parse point from {plist:?}"),
}
}
}

Expand Down Expand Up @@ -1863,6 +1871,11 @@ mod tests {
assert_load_v2_matches_load_v3("WghtVar_OS2.glyphs");
}

#[test]
fn read_wght_var_anchors_2_and_3() {
assert_load_v2_matches_load_v3("WghtVar_Anchors.glyphs");
}

fn only_shape_in_only_layer<'a>(font: &'a Font, glyph_name: &str) -> &'a Shape {
let glyph = font.glyphs.get(glyph_name).unwrap();
assert_eq!(1, glyph.layers.len());
Expand Down
7 changes: 7 additions & 0 deletions glyphs2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,4 +1426,11 @@ mod tests {
.collect();
assert!(bad_kerns.is_empty(), "{bad_kerns:#?}");
}

#[test]
fn load_anchors() {
// v2 == v3 test above confirms also works with glyphs 2
let font = Font::load(&glyphs3_dir().join("WghtVar_Anchors.glyphs")).unwrap();

}
}
179 changes: 179 additions & 0 deletions resources/testdata/glyphs2/WghtVar_Anchors.glyphs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
.appVersion = "3218";
DisplayStrings = (
A
);
customParameters = (
{
name = Axes;
value = (
{
Name = Weight;
Tag = wght;
}
);
}
);
date = "2022-12-01 04:52:20 +0000";
familyName = WghtVar;
fontMaster = (
{
alignmentZones = (
"{800, 16}",
"{0, -16}",
"{-200, -16}"
);
ascender = 800;
capHeight = 0;
descender = -200;
id = m01;
weightValue = 400;
xHeight = 0;
},
{
ascender = 800;
capHeight = 700;
descender = -200;
id = "E09E0C54-128D-4FEA-B209-1B70BEFE300B";
weight = Bold;
weightValue = 700;
xHeight = 500;
}
);
glyphs = (
{
glyphname = A;
lastChange = "2023-09-14 14:42:56 +0000";
layers = (
{
anchors = (
{
name = top;
position = "{300, 700}";
}
);
layerId = m01;
paths = (
{
closed = 1;
nodes = (
"354 183 LINE",
"414 585 LINE",
"178 585 LINE",
"238 182 LINE"
);
},
{
closed = 1;
nodes = (
"354 0 LINE",
"354 107 LINE",
"238 107 LINE",
"238 0 LINE"
);
}
);
width = 600;
},
{
anchors = (
{
name = top;
position = "{325, 725}";
}
);
layerId = "E09E0C54-128D-4FEA-B209-1B70BEFE300B";
paths = (
{
closed = 1;
nodes = (
"354 183 LINE",
"414 585 LINE",
"178 585 LINE",
"238 182 LINE"
);
},
{
closed = 1;
nodes = (
"354 0 LINE",
"354 107 LINE",
"238 107 LINE",
"238 0 LINE"
);
}
);
width = 600;
}
);
unicode = 0041;
},
{
glyphname = space;
lastChange = "2022-12-01 04:58:12 +0000";
layers = (
{
layerId = m01;
width = 200;
},
{
layerId = "E09E0C54-128D-4FEA-B209-1B70BEFE300B";
width = 600;
}
);
unicode = 0020;
},
{
glyphname = macroncomb;
lastChange = "2023-09-14 14:41:26 +0000";
layers = (
{
anchors = (
{
name = _top;
position = "{300, 600}";
}
);
layerId = m01;
paths = (
{
closed = 1;
nodes = (
"140 661 LINE",
"454 661 LINE",
"454 724 LINE",
"140 724 LINE"
);
}
);
width = 600;
},
{
anchors = (
{
name = _top;
position = "{310, 610}";
}
);
layerId = "E09E0C54-128D-4FEA-B209-1B70BEFE300B";
paths = (
{
closed = 1;
nodes = (
"132 658 LINE",
"462 658 LINE",
"462 728 LINE",
"132 728 LINE"
);
}
);
width = 600;
}
);
unicode = 0304;
}
);
unitsPerEm = 1000;
versionMajor = 1;
versionMinor = 0;
}
Loading

0 comments on commit 71110dd

Please sign in to comment.