Skip to content

Commit

Permalink
Shuffle glyph type names
Browse files Browse the repository at this point in the history
NamedGlyph goes back to Glyph, and we import write_fonts Glyph as
RawGlyph.
  • Loading branch information
cmyr committed Aug 10, 2023
1 parent 2adc949 commit 9baed72
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
18 changes: 9 additions & 9 deletions fontbe/src/glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ use read_fonts::{
};
use write_fonts::{
tables::{
glyf::{Bbox, Component, ComponentFlags, CompositeGlyph, Glyph, SimpleGlyph},
glyf::{Bbox, Component, ComponentFlags, CompositeGlyph, Glyph as RawGlyph, SimpleGlyph},
variations::iup_delta_optimize,
},
OtRound,
};

use crate::{
error::{Error, GlyphProblem},
orchestration::{AnyWorkId, BeWork, Context, GvarFragment, LocaFormat, NamedGlyph, WorkId},
orchestration::{AnyWorkId, BeWork, Context, Glyph, GvarFragment, LocaFormat, WorkId},
};

#[derive(Debug)]
Expand Down Expand Up @@ -281,7 +281,7 @@ impl Work<Context, AnyWorkId, Error> for GlyphWork {
let composite = create_composite(context, ir_glyph, default_location, &components)?;
context
.glyphs
.set_unconditionally(NamedGlyph::new(name.clone(), composite));
.set_unconditionally(Glyph::new(name.clone(), composite));
let point_seqs = point_seqs_for_composite_glyph(ir_glyph);
(name, point_seqs, Vec::new())
}
Expand Down Expand Up @@ -309,7 +309,7 @@ impl Work<Context, AnyWorkId, Error> for GlyphWork {
};
context
.glyphs
.set_unconditionally(NamedGlyph::new(name.clone(), base_glyph.clone()));
.set_unconditionally(Glyph::new(name.clone(), base_glyph.clone()));

let mut contour_end = 0;
let mut contour_ends = Vec::with_capacity(base_glyph.contours().len());
Expand Down Expand Up @@ -671,7 +671,7 @@ fn compute_composite_bboxes(context: &Context) -> Result<(), Error> {
// Hopefully we can figure out some of those bboxes!
for composite in composites.iter() {
let glyph_name = &composite.name;
let Glyph::Composite(composite) = &composite.glyph else {
let RawGlyph::Composite(composite) = &composite.data else {
panic!("Only composites should be in our vector of composites!!");
};

Expand All @@ -688,9 +688,9 @@ fn compute_composite_bboxes(context: &Context) -> Result<(), Error> {
glyphs
.get(ref_glyph_name)
.map(|g| g.as_ref().clone())
.and_then(|g| match &g.glyph {
Glyph::Composite(..) => None,
Glyph::Simple(simple_glyph) => Some(bbox2rect(simple_glyph.bbox)),
.and_then(|g| match &g.data {
RawGlyph::Composite(..) => None,
RawGlyph::Simple(simple_glyph) => Some(bbox2rect(simple_glyph.bbox)),
})
});
if bbox.is_none() {
Expand Down Expand Up @@ -729,7 +729,7 @@ fn compute_composite_bboxes(context: &Context) -> Result<(), Error> {
.glyphs
.get(&WorkId::GlyfFragment(glyph_name.clone()).into()))
.clone();
let Glyph::Composite(composite) = &mut glyph.glyph else {
let RawGlyph::Composite(composite) = &mut glyph.data else {
panic!("{glyph_name} is not a composite; we shouldn't be trying to update it");
};
composite.bbox = bbox.into(); // delay conversion to Bbox to avoid accumulating rounding error
Expand Down
22 changes: 11 additions & 11 deletions fontbe/src/metrics_and_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use read_fonts::types::FWord;
use write_fonts::{
dump_table,
tables::{
glyf::{Bbox, Contour, Glyph},
glyf::{Bbox, Contour, Glyph as RawGlyph},
hhea::Hhea,
hmtx::Hmtx,
maxp::Maxp,
Expand All @@ -25,7 +25,7 @@ use write_fonts::{

use crate::{
error::Error,
orchestration::{AnyWorkId, BeWork, Context, NamedGlyph, WorkId},
orchestration::{AnyWorkId, BeWork, Context, Glyph, WorkId},
};

#[derive(Debug)]
Expand Down Expand Up @@ -81,11 +81,11 @@ impl GlyphLimits {
}

impl FontLimits {
fn update(&mut self, id: GlyphId, advance: u16, glyph: &NamedGlyph) {
fn update(&mut self, id: GlyphId, advance: u16, glyph: &Glyph) {
// min side bearings are only for non-empty glyphs
// we will presume only simple glyphs with no contours are empty
if !glyph.glyph.is_empty() {
let bbox = glyph.glyph.bbox();
if !glyph.data.is_empty() {
let bbox = glyph.data.bbox();
let left_side_bearing = bbox.x_min;
// aw - (lsb + xMax - xMin) ... but if lsb == xMin then just advance - xMax?
let right_side_bearing: i16 = match advance as i32 - bbox.x_max as i32 {
Expand All @@ -108,11 +108,11 @@ impl FontLimits {
self.advance_width_max = max(self.advance_width_max, advance);
}

let bbox = glyph.glyph.bbox();
let bbox = glyph.data.bbox();
self.bbox = self.bbox.map(|b| b.union(bbox)).or(Some(bbox));

match &glyph.glyph {
Glyph::Simple(simple) => {
match &glyph.data {
RawGlyph::Simple(simple) => {
let num_points = simple.contours().iter().map(Contour::len).sum::<usize>() as u16;
let num_contours = simple.contours().len() as u16;
self.max_points = max(self.max_points, num_points);
Expand All @@ -129,7 +129,7 @@ impl FontLimits {
},
)
}
Glyph::Composite(composite) => {
RawGlyph::Composite(composite) => {
let num_components = composite.components().len() as u16;
self.max_component_elements = max(self.max_component_elements, num_components);
let components = Some(composite.components().iter().map(|c| c.glyph).collect());
Expand Down Expand Up @@ -262,7 +262,7 @@ impl Work<Context, AnyWorkId, Error> for MetricAndLimitWork {
glyph_limits.update(gid, advance, &glyph);
LongMetric {
advance,
side_bearing: glyph.glyph.bbox().x_min,
side_bearing: glyph.data.bbox().x_min,
}
})
.collect();
Expand Down Expand Up @@ -379,7 +379,7 @@ mod tests {
glyph_limits.update(
GlyphId::new(0),
0,
&crate::orchestration::NamedGlyph::new(
&crate::orchestration::Glyph::new(
"don't care".into(),
SimpleGlyph::from_bezpath(
&BezPath::from_svg("M-437,611 L-334,715 L-334,611 Z").unwrap(),
Expand Down
34 changes: 18 additions & 16 deletions fontbe/src/orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use serde::{Deserialize, Serialize};
use write_fonts::{
dump_table,
tables::{
avar::Avar, cmap::Cmap, fvar::Fvar, glyf::Glyph, gpos::Gpos, gsub::Gsub, gvar::GlyphDeltas,
head::Head, hhea::Hhea, maxp::Maxp, name::Name, os2::Os2, post::Post, stat::Stat,
variations::Tuple,
avar::Avar, cmap::Cmap, fvar::Fvar, glyf::Glyph as RawGlyph, gpos::Gpos, gsub::Gsub,
gvar::GlyphDeltas, head::Head, hhea::Hhea, maxp::Maxp, name::Name, os2::Os2, post::Post,
stat::Stat, variations::Tuple,
},
validate::Validate,
FontWrite, OtRound,
Expand Down Expand Up @@ -115,44 +115,46 @@ impl From<WorkId> for AnyWorkId {
}

/// A glyph and its associated name
///
/// See <https://learn.microsoft.com/en-us/typography/opentype/spec/glyf>
#[derive(Debug, Clone)]
pub struct NamedGlyph {
pub struct Glyph {
pub name: GlyphName,
pub glyph: Glyph,
pub data: RawGlyph,
}

impl NamedGlyph {
pub(crate) fn new(name: GlyphName, glyph: impl Into<Glyph>) -> Self {
impl Glyph {
pub(crate) fn new(name: GlyphName, glyph: impl Into<RawGlyph>) -> Self {
Self {
name,
glyph: glyph.into(),
data: glyph.into(),
}
}

pub fn is_simple(&self) -> bool {
matches!(&self.glyph, Glyph::Simple(_))
matches!(&self.data, RawGlyph::Simple(_))
}

pub fn to_bytes(&self) -> Vec<u8> {
dump_table(&self.glyph).unwrap()
dump_table(&self.data).unwrap()
}
}

impl IdAware<AnyWorkId> for NamedGlyph {
impl IdAware<AnyWorkId> for Glyph {
fn id(&self) -> AnyWorkId {
AnyWorkId::Be(WorkId::GlyfFragment(self.name.clone()))
}
}

impl Persistable for NamedGlyph {
impl Persistable for Glyph {
fn read(from: &mut dyn Read) -> Self {
let (name, bytes): (GlyphName, Vec<u8>) = bincode::deserialize_from(from).unwrap();
let glyph = Glyph::read(bytes.as_slice().into()).unwrap();
NamedGlyph { name, glyph }
let glyph = RawGlyph::read(bytes.as_slice().into()).unwrap();
Glyph { name, data: glyph }
}

fn write(&self, to: &mut dyn Write) {
let glyph_bytes = dump_table(&self.glyph).unwrap();
let glyph_bytes = dump_table(&self.data).unwrap();
let to_write = (&self.name, glyph_bytes);
bincode::serialize_into(to, &to_write).unwrap();
}
Expand Down Expand Up @@ -366,7 +368,7 @@ pub struct Context {

// work results we've completed or restored from disk
pub gvar_fragments: BeContextMap<GvarFragment>,
pub glyphs: BeContextMap<NamedGlyph>,
pub glyphs: BeContextMap<Glyph>,

pub avar: BeContextItem<BeValue<Avar>>,
pub cmap: BeContextItem<BeValue<Cmap>>,
Expand Down
14 changes: 7 additions & 7 deletions fontc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ mod tests {

use chrono::{Duration, TimeZone, Utc};
use fontbe::orchestration::{
AnyWorkId, Context as BeContext, LocaFormat, NamedGlyph, WorkId as BeWorkIdentifier,
AnyWorkId, Context as BeContext, Glyph, LocaFormat, WorkId as BeWorkIdentifier,
};
use fontdrasil::types::GlyphName;
use fontir::{
Expand Down Expand Up @@ -344,7 +344,7 @@ mod tests {
use tempfile::{tempdir, TempDir};
use write_fonts::{
dump_table,
tables::glyf::{Bbox, Glyph},
tables::glyf::{Bbox, Glyph as RawGlyph},
};

use super::*;
Expand Down Expand Up @@ -754,10 +754,10 @@ mod tests {
buf
}

fn read_be_glyph(build_dir: &Path, name: &str) -> Glyph {
fn read_be_glyph(build_dir: &Path, name: &str) -> RawGlyph {
let raw_glyph = read_file(&build_dir.join(format!("glyphs/{name}.glyf")));
let read: &mut dyn Read = &mut raw_glyph.as_slice();
NamedGlyph::read(read).glyph
Glyph::read(read).data
}

#[test]
Expand All @@ -767,7 +767,7 @@ mod tests {
assert!(glyph.default_instance().contours.is_empty(), "{glyph:?}");
assert_eq!(2, glyph.default_instance().components.len(), "{glyph:?}");

let Glyph::Composite(glyph) = read_be_glyph(temp_dir.path(), glyph.name.as_str()) else {
let RawGlyph::Composite(glyph) = read_be_glyph(temp_dir.path(), glyph.name.as_str()) else {
panic!("Expected a simple glyph");
};
let raw_glyph = dump_table(&glyph).unwrap();
Expand All @@ -783,7 +783,7 @@ mod tests {
assert!(glyph.default_instance().components.is_empty(), "{glyph:?}");
assert_eq!(2, glyph.default_instance().contours.len(), "{glyph:?}");

let Glyph::Simple(glyph) = read_be_glyph(temp_dir.path(), glyph.name.as_str()) else {
let RawGlyph::Simple(glyph) = read_be_glyph(temp_dir.path(), glyph.name.as_str()) else {
panic!("Expected a simple glyph");
};
assert_eq!(2, glyph.contours().len());
Expand All @@ -795,7 +795,7 @@ mod tests {
let build_dir = temp_dir.path();
compile(Args::for_test(build_dir, "static.designspace"));

let Glyph::Simple( glyph) = read_be_glyph(build_dir, "bar") else {
let RawGlyph::Simple( glyph) = read_be_glyph(build_dir, "bar") else {
panic!("Expected a simple glyph");
};

Expand Down

0 comments on commit 9baed72

Please sign in to comment.