Skip to content

Commit

Permalink
Merge pull request #503 from googlefonts/sharing
Browse files Browse the repository at this point in the history
Move coords to fontdrasil so fea-rs can agree with us on what NormalozedLocation looks like
  • Loading branch information
rsheeter authored Oct 24, 2023
2 parents 5c92294 + 00c1ff7 commit dcd0839
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 143 deletions.
12 changes: 6 additions & 6 deletions fontbe/src/avar.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Generates a [avar](https://learn.microsoft.com/en-us/typography/opentype/spec/avar) table.

use fontdrasil::orchestration::{Access, Work};
use fontir::{
use fontdrasil::{
coords::{CoordConverter, DesignCoord, NormalizedCoord},
ir::Axis,
orchestration::WorkId as FeWorkId,
orchestration::{Access, Work},
types::Axis,
};
use fontir::orchestration::WorkId as FeWorkId;
use log::debug;
use write_fonts::tables::avar::{Avar, AxisValueMap, SegmentMaps};

Expand Down Expand Up @@ -105,9 +105,9 @@ impl Work<Context, AnyWorkId, Error> for AvarWork {

#[cfg(test)]
mod tests {
use fontir::{
use fontdrasil::{
coords::{CoordConverter, DesignCoord, UserCoord},
ir::Axis,
types::Axis,
};
use read_fonts::types::Tag;
use std::{cmp, str::FromStr};
Expand Down
12 changes: 8 additions & 4 deletions fontbe/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ use fea_rs::{
Compiler, GlyphMap, GlyphName as FeaRsGlyphName,
};
use font_types::{F2Dot14, Tag};
use fontir::{
use fontdrasil::{
coords::{CoordConverter, DesignCoord, NormalizedCoord, NormalizedLocation, UserCoord},
ir::{Axis, Features, GlyphOrder, KernParticipant, Kerning, StaticMetadata},
types::Axis,
};
use fontir::{
ir::{Features, GlyphOrder, KernParticipant, Kerning, StaticMetadata},
orchestration::{Flags, WorkId as FeWorkId},
};
use log::{debug, error, trace, warn};
Expand Down Expand Up @@ -589,10 +592,11 @@ mod tests {

use fea_rs::compile::{AxisLocation, VariationInfo};
use font_types::Tag;
use fontir::{
use fontdrasil::{
coords::{CoordConverter, DesignCoord, NormalizedCoord, NormalizedLocation, UserCoord},
ir::{Axis, StaticMetadata},
types::Axis,
};
use fontir::ir::StaticMetadata;
use ordered_float::OrderedFloat;

use super::FeaVariationInfo;
Expand Down
3 changes: 2 additions & 1 deletion fontbe/src/fvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ impl Work<Context, AnyWorkId, Error> for FvarWork {

#[cfg(test)]
mod tests {
use fontir::ir::{Axis, StaticMetadata};
use fontdrasil::types::Axis;
use fontir::ir::StaticMetadata;

use super::generate_fvar;

Expand Down
2 changes: 1 addition & 1 deletion fontbe/src/glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::{
};

use fontdrasil::{
coords::{Location, NormalizedCoord, NormalizedLocation},
orchestration::{Access, Work},
types::GlyphName,
};
use fontir::{
coords::{Location, NormalizedCoord, NormalizedLocation},
ir,
orchestration::{Flags, WorkId as FeWorkId},
variations::{VariationModel, VariationRegion},
Expand Down
6 changes: 2 additions & 4 deletions fontbe/src/orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,8 @@ where
#[cfg(test)]
mod tests {
use font_types::Tag;
use fontir::{
coords::NormalizedCoord,
variations::{Tent, VariationRegion},
};
use fontdrasil::coords::NormalizedCoord;
use fontir::variations::{Tent, VariationRegion};

use super::*;

Expand Down
9 changes: 3 additions & 6 deletions fontbe/src/os2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,10 @@ impl Work<Context, AnyWorkId, Error> for Os2Work {

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use fontir::{
coords::NormalizedLocation,
ir::{GlobalMetric, GlobalMetrics},
};
use fontdrasil::coords::NormalizedLocation;
use fontir::ir::{GlobalMetric, GlobalMetrics};
use read_fonts::types::Tag;
use std::collections::HashSet;
use write_fonts::tables::os2::Os2;

use crate::os2::codepage_range_bits;
Expand Down
4 changes: 2 additions & 2 deletions fontbe/src/test_util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Only included in test

use font_types::Tag;
use fontir::{
use fontdrasil::{
coords::{CoordConverter, DesignCoord, UserCoord},
ir::Axis,
types::Axis,
};
use std::cmp;

Expand Down
3 changes: 2 additions & 1 deletion fontdrasil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ categories = ["text-processing", "parsing", "graphics"]
[features]

[dependencies]
ordered-float.workspace = true
smol_str.workspace = true

serde.workspace = true
font-types.workspace = true

[dev-dependencies]
diff.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion fontir/src/coords.rs → fontdrasil/src/coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt::{Debug, Write};
use std::ops::Sub;

use crate::serde::CoordConverterSerdeRepr;
use crate::{ir::Axis, piecewise_linear_map::PiecewiseLinearMap};
use crate::{piecewise_linear_map::PiecewiseLinearMap, types::Axis};
use font_types::{F2Dot14, Fixed, Tag};
use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
Expand Down
3 changes: 3 additions & 0 deletions fontdrasil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Helper library for code that is of value to FE and BE of font compilation.

pub mod coords;
pub mod orchestration;
pub mod paths;
pub mod piecewise_linear_map;
mod serde;
pub mod types;
File renamed without changes.
35 changes: 35 additions & 0 deletions fontdrasil/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::coords::{CoordConverter, DesignCoord, UserCoord};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct CoordConverterSerdeRepr {
default_idx: usize,
user_to_design: Vec<(f32, f32)>,
}

impl From<CoordConverterSerdeRepr> for CoordConverter {
fn from(from: CoordConverterSerdeRepr) -> Self {
let examples = from
.user_to_design
.into_iter()
.map(|(u, d)| (UserCoord::new(u), DesignCoord::new(d)))
.collect();
CoordConverter::new(examples, from.default_idx)
}
}

impl From<CoordConverter> for CoordConverterSerdeRepr {
fn from(from: CoordConverter) -> Self {
let user_to_design = from
.user_to_design
.from
.iter()
.zip(from.user_to_design.to)
.map(|(u, d)| (u.into_inner(), d.into_inner()))
.collect();
CoordConverterSerdeRepr {
default_idx: from.default_idx,
user_to_design,
}
}
}
21 changes: 21 additions & 0 deletions fontdrasil/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use std::fmt::{Debug, Display};
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use font_types::Tag;

use crate::coords::{CoordConverter, UserCoord};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct GlyphName(SmolStr);

Expand Down Expand Up @@ -49,3 +53,20 @@ impl Display for GlyphName {

pub type GroupName = GlyphName;
pub type AnchorName = GlyphName;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Axis {
pub name: String,
pub tag: Tag,
pub min: UserCoord,
pub default: UserCoord,
pub max: UserCoord,
pub hidden: bool,
pub converter: CoordConverter,
}

impl Axis {
pub fn is_point(&self) -> bool {
self.min == self.default && self.max == self.default
}
}
7 changes: 4 additions & 3 deletions fontir/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{error, io, path::PathBuf};

use font_types::{InvalidTag, Tag};
use fontdrasil::types::{AnchorName, GlyphName};
use fontdrasil::{
coords::{DesignCoord, NormalizedCoord, NormalizedLocation, UserCoord, UserLocation},
types::{AnchorName, GlyphName},
};
use kurbo::Point;
use thiserror::Error;

use crate::coords::{DesignCoord, NormalizedCoord, NormalizedLocation, UserCoord, UserLocation};

// TODO: eliminate dyn Error and collapse Error/WorkError

#[derive(Debug, Error)]
Expand Down
9 changes: 6 additions & 3 deletions fontir/src/glyph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
};

use fontdrasil::{
coords::NormalizedLocation,
orchestration::{Access, Work},
types::GlyphName,
};
Expand All @@ -17,7 +18,6 @@ use log::{debug, log_enabled, trace};
use ordered_float::OrderedFloat;

use crate::{
coords::NormalizedLocation,
error::WorkError,
ir::{Component, Glyph, GlyphBuilder, GlyphOrder},
orchestration::{Context, Flags, IrWork, WorkId},
Expand Down Expand Up @@ -435,11 +435,14 @@ mod tests {
use std::{collections::HashSet, path::Path};

use font_types::Tag;
use fontdrasil::{orchestration::Access, types::GlyphName};
use fontdrasil::{
coords::{NormalizedCoord, NormalizedLocation},
orchestration::Access,
types::GlyphName,
};
use kurbo::{Affine, BezPath};

use crate::{
coords::{NormalizedCoord, NormalizedLocation},
ir::{Component, Glyph, GlyphBuilder, GlyphInstance, GlyphOrder},
orchestration::{Context, Flags, WorkId},
paths::Paths,
Expand Down
51 changes: 16 additions & 35 deletions fontir/src/ir.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
//! Font IR types.

use crate::{
coords::{CoordConverter, NormalizedCoord, NormalizedLocation, UserCoord, UserLocation},
error::{PathConversionError, VariationModelError, WorkError},
orchestration::{IdAware, Persistable, WorkId},
serde::{
GlobalMetricsSerdeRepr, GlyphOrderSerdeRepr, GlyphSerdeRepr, KerningSerdeRepr,
MiscSerdeRepr, StaticMetadataSerdeRepr,
},
variations::VariationModel,
};
use chrono::{DateTime, Utc};
use font_types::NameId;
use font_types::Tag;
use fontdrasil::types::{AnchorName, GlyphName, GroupName};
use fontdrasil::{
coords::{NormalizedCoord, NormalizedLocation, UserLocation},
types::{AnchorName, Axis, GlyphName, GroupName},
};
use indexmap::IndexSet;
use kurbo::{Affine, BezPath, PathEl, Point};
use log::{log_enabled, trace, warn};
Expand All @@ -27,6 +19,16 @@ use std::{
};
use write_fonts::{tables::os2::SelectionFlags, OtRound};

use crate::{
error::{PathConversionError, VariationModelError, WorkError},
orchestration::{IdAware, Persistable, WorkId},
serde::{
GlobalMetricsSerdeRepr, GlyphOrderSerdeRepr, GlyphSerdeRepr, KerningSerdeRepr,
MiscSerdeRepr, StaticMetadataSerdeRepr,
},
variations::VariationModel,
};

pub const DEFAULT_VENDOR_ID: &str = "NONE";
const DEFAULT_VENDOR_ID_TAG: Tag = Tag::new(b"NONE");

Expand Down Expand Up @@ -760,23 +762,6 @@ impl NameKey {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Axis {
pub name: String,
pub tag: Tag,
pub min: UserCoord,
pub default: UserCoord,
pub max: UserCoord,
pub hidden: bool,
pub converter: CoordConverter,
}

impl Axis {
pub fn is_point(&self) -> bool {
self.min == self.default && self.max == self.default
}
}

/// IR for a named position in variation space
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct NamedInstance {
Expand Down Expand Up @@ -1487,14 +1472,10 @@ mod tests {
use serde::{Deserialize, Serialize};

use font_types::{NameId, Tag};
use fontdrasil::coords::{CoordConverter, NormalizedCoord, UserCoord};
use write_fonts::tables::os2::SelectionFlags;

use crate::{
coords::{CoordConverter, NormalizedCoord, UserCoord},
error::PathConversionError,
ir::Axis,
variations::VariationModel,
};
use crate::{error::PathConversionError, ir::Axis, variations::VariationModel};

use pretty_assertions::assert_eq;

Expand Down
2 changes: 0 additions & 2 deletions fontir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
pub mod coords;
pub mod error;
pub mod glyph;
pub mod ir;
pub mod orchestration;
pub mod paths;
pub mod piecewise_linear_map;
pub(crate) mod serde;
pub mod source;
pub mod stateset;
Expand Down
Loading

0 comments on commit dcd0839

Please sign in to comment.