From 9baed7284e7252065b76fd37aeb74bdcb4e58916 Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Thu, 10 Aug 2023 18:48:06 -0400 Subject: [PATCH] Shuffle glyph type names NamedGlyph goes back to Glyph, and we import write_fonts Glyph as RawGlyph. --- fontbe/src/glyphs.rs | 18 ++++++++--------- fontbe/src/metrics_and_limits.rs | 22 ++++++++++----------- fontbe/src/orchestration.rs | 34 +++++++++++++++++--------------- fontc/src/lib.rs | 14 ++++++------- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/fontbe/src/glyphs.rs b/fontbe/src/glyphs.rs index 48b6e4c6..18b00f82 100644 --- a/fontbe/src/glyphs.rs +++ b/fontbe/src/glyphs.rs @@ -22,7 +22,7 @@ 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, @@ -30,7 +30,7 @@ use write_fonts::{ use crate::{ error::{Error, GlyphProblem}, - orchestration::{AnyWorkId, BeWork, Context, GvarFragment, LocaFormat, NamedGlyph, WorkId}, + orchestration::{AnyWorkId, BeWork, Context, Glyph, GvarFragment, LocaFormat, WorkId}, }; #[derive(Debug)] @@ -281,7 +281,7 @@ impl Work 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()) } @@ -309,7 +309,7 @@ impl Work 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()); @@ -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!!"); }; @@ -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() { @@ -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 diff --git a/fontbe/src/metrics_and_limits.rs b/fontbe/src/metrics_and_limits.rs index 321d2772..4b0e2b39 100644 --- a/fontbe/src/metrics_and_limits.rs +++ b/fontbe/src/metrics_and_limits.rs @@ -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, @@ -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)] @@ -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 { @@ -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::() as u16; let num_contours = simple.contours().len() as u16; self.max_points = max(self.max_points, num_points); @@ -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()); @@ -262,7 +262,7 @@ impl Work 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(); @@ -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(), diff --git a/fontbe/src/orchestration.rs b/fontbe/src/orchestration.rs index 976c3180..070e8e8f 100644 --- a/fontbe/src/orchestration.rs +++ b/fontbe/src/orchestration.rs @@ -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, @@ -115,44 +115,46 @@ impl From for AnyWorkId { } /// A glyph and its associated name +/// +/// See #[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) -> Self { +impl Glyph { + pub(crate) fn new(name: GlyphName, glyph: impl Into) -> 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 { - dump_table(&self.glyph).unwrap() + dump_table(&self.data).unwrap() } } -impl IdAware for NamedGlyph { +impl IdAware 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) = 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(); } @@ -366,7 +368,7 @@ pub struct Context { // work results we've completed or restored from disk pub gvar_fragments: BeContextMap, - pub glyphs: BeContextMap, + pub glyphs: BeContextMap, pub avar: BeContextItem>, pub cmap: BeContextItem>, diff --git a/fontc/src/lib.rs b/fontc/src/lib.rs index 383189d9..5055c154 100644 --- a/fontc/src/lib.rs +++ b/fontc/src/lib.rs @@ -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::{ @@ -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::*; @@ -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] @@ -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(); @@ -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()); @@ -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"); };