Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/shift-backends/src/designspace/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ufo::UfoReader;
use norad::designspace::DesignSpaceDocument;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use shift_font::{Axis, Font, Layer, LayerId, Location, Source, SourceId};
use shift_font::{Axis, Font, LayerId, Location, Source, SourceId};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -156,7 +156,6 @@ impl DesignspaceReader {
};

let name = source_name(ds_source, idx);
font.add_layer(Layer::new(name.clone()));
let location = location_from_dimensions(&ds_source.location, &doc);
let source_id = font.add_source(Source::with_filename(
name,
Expand Down Expand Up @@ -281,7 +280,6 @@ fn load_axisless_designspace(
};

let name = axisless_source_name(ds_source, idx);
font.add_layer(Layer::new(name.clone()));
let source_id = font.add_source(Source::with_filename(
name,
Location::new(),
Expand Down
6 changes: 1 addition & 5 deletions crates/shift-backends/src/glyphs/reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use glyphs_reader::{FeatureSnippet, Font as GlyphsFont, NodeType, Shape};
use shift_font::{
Anchor, Axis, Component, Contour, FeatureData, Font, Glyph, GlyphLayer, KerningData,
KerningPair, KerningSide, Layer, LayerId, Location, Source, Transform,
KerningPair, KerningSide, LayerId, Location, Source, Transform,
};
use std::collections::HashMap;
use std::path::Path;
Expand Down Expand Up @@ -183,10 +183,6 @@ impl FontReader for GlyphsReader {

let mut source_by_master_id = HashMap::new();
for (master_idx, master) in glyphs_font.masters.iter().enumerate() {
if master_idx != glyphs_font.default_master_idx {
font.add_layer(Layer::new(master.name.clone()));
}

let mut location = Location::new();
for (axis_idx, axis) in glyphs_font.axes.iter().enumerate() {
if let Some(value) = master.axes_values.get(axis_idx) {
Expand Down
15 changes: 1 addition & 14 deletions crates/shift-backends/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::errors::FormatBackendResult;
use shift_font::{
Axis, FeatureData, Font, FontMetadata, FontMetrics, Glyph, GlyphName, Guideline, KerningData,
Layer, LayerId, LibData, Source, SourceId,
LibData, Source, SourceId,
};

pub trait FontView {
Expand All @@ -10,14 +10,12 @@ pub trait FontView {
fn axes(&self) -> &[Axis];
fn sources(&self) -> &[Source];
fn default_source_id(&self) -> Option<SourceId>;
fn layers(&self) -> Vec<(LayerId, &Layer)>;
fn glyphs(&self) -> Vec<&Glyph>;
fn glyph(&self, name: &str) -> Option<&Glyph>;
fn kerning(&self) -> &KerningData;
fn features(&self) -> &FeatureData;
fn guidelines(&self) -> &[Guideline];
fn lib(&self) -> &LibData;
fn default_layer_id(&self) -> LayerId;
}

impl FontView for Font {
Expand All @@ -41,13 +39,6 @@ impl FontView for Font {
self.default_source_id()
}

fn layers(&self) -> Vec<(LayerId, &Layer)> {
self.layers()
.iter()
.map(|(layer_id, layer)| (*layer_id, layer))
.collect()
}

fn glyphs(&self) -> Vec<&Glyph> {
self.glyphs().values().map(|glyph| glyph.as_ref()).collect()
}
Expand All @@ -71,10 +62,6 @@ impl FontView for Font {
fn lib(&self) -> &LibData {
self.lib()
}

fn default_layer_id(&self) -> LayerId {
self.default_layer_id()
}
}

pub trait FontReader: Send + Sync {
Expand Down
6 changes: 2 additions & 4 deletions crates/shift-backends/src/ufo/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::traits::FontReader;
use norad::{Font as NoradFont, Line};
use shift_font::{
Anchor, Component, Contour, FeatureData, Font, Glyph, GlyphLayer, Guideline, KerningData,
KerningPair, KerningSide, Layer, LayerId, LibData, LibValue, Location, PointType, Source,
SourceId, Transform,
KerningPair, KerningSide, LayerId, LibData, LibValue, Location, PointType, Source, SourceId,
Transform,
};
use std::collections::HashMap;
use std::path::Path;
Expand Down Expand Up @@ -250,8 +250,6 @@ impl FontReader for UfoReader {
let source_id = if layer.name() == &norad_default_layer_name {
default_source_id
} else {
let new_layer = Layer::new(layer.name().to_string());
font.add_layer(new_layer);
font.add_source(Source::new(layer.name().to_string(), Location::new()))
};

Expand Down
14 changes: 11 additions & 3 deletions crates/shift-backends/tests/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ fn loads_ufo_components_anchors_layers_and_kerning() {
.collect();
assert!(anchor_names.contains(&"top"));

let layer_names: Vec<_> = font.layers().values().map(|layer| layer.name()).collect();
assert!(layer_names.contains(&"public.default"));
assert!(font.layers().len() >= 2);
let source_names: Vec<_> = font.sources().iter().map(|source| source.name()).collect();
assert!(source_names.contains(&"Regular"));
assert!(font.sources().len() >= 2);
assert!(font
.glyphs()
.values()
.flat_map(|glyph| glyph.layers().values())
.all(|layer| font
.sources()
.iter()
.any(|source| source.id() == layer.source_id())));

assert_eq!(font.kerning().get_kerning("T", "A"), Some(-75.0));
assert_eq!(font.kerning().get_kerning("V", "A"), Some(-100.0));
Expand Down
22 changes: 11 additions & 11 deletions crates/shift-backends/tests/round_trip/ufo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ fn preserves_components_anchors_layers_and_kerning() {
&& (anchor.y() - 456.0).abs() < 0.001
}));

let original_layer_names: Vec<_> = original
.layers()
.values()
.map(|layer| layer.name())
let original_source_names: Vec<_> = original
.sources()
.iter()
.map(|source| source.name())
.collect();
let reloaded_layer_names: Vec<_> = reloaded
.layers()
.values()
.map(|layer| layer.name())
let reloaded_source_names: Vec<_> = reloaded
.sources()
.iter()
.map(|source| source.name())
.collect();
assert_eq!(reloaded_layer_names.len(), original_layer_names.len());
for name in original_layer_names {
assert!(reloaded_layer_names.contains(&name));
assert_eq!(reloaded_source_names.len(), original_source_names.len());
for name in original_source_names {
assert!(reloaded_source_names.contains(&name));
}

assert_eq!(reloaded.kerning().get_kerning("T", "A"), Some(-75.0));
Expand Down
6 changes: 3 additions & 3 deletions crates/shift-bridge/__test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Bridge", () => {
function defaultLayerRef(name = "A", unicode = 65) {
return {
glyphHandle: { name, unicode },
layerId: bridge.getSources()[0].layerId,
sourceId: defaultSourceId(),
};
}

Expand Down Expand Up @@ -158,8 +158,8 @@ describe("Bridge", () => {

it("surfaces typed bridge errors at the NAPI boundary", () => {
expect(() =>
bridge.addContour({ glyphHandle: { name: "A", unicode: 65 }, layerId: "not-a-layer" }),
).toThrow(/layer ID/i);
bridge.addContour({ glyphHandle: { name: "A", unicode: 65 }, sourceId: "not-a-source" }),
).toThrow(/source ID/i);

const glyphRef = defaultLayerRef();
expect(() =>
Expand Down
13 changes: 0 additions & 13 deletions crates/shift-bridge/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ impl FontView for FontSaveSnapshot {
self.font.default_source_id()
}

fn layers(&self) -> Vec<(LayerId, &shift_font::Layer)> {
self
.font
.layers()
.iter()
.map(|(layer_id, layer)| (*layer_id, layer))
.collect()
}

fn glyphs(&self) -> Vec<&Glyph> {
let override_name = self
.active_glyph_override
Expand Down Expand Up @@ -253,10 +244,6 @@ impl FontView for FontSaveSnapshot {
fn lib(&self) -> &shift_font::LibData {
self.font.lib()
}

fn default_layer_id(&self) -> LayerId {
self.font.default_layer_id()
}
}

pub struct ExportFontTask {
Expand Down
45 changes: 2 additions & 43 deletions crates/shift-font/src/ir/font.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::axis::{Axis, Location};
use crate::entity::{LayerId, SourceId};
use crate::entity::SourceId;
use crate::features::FeatureData;
use crate::glyph::Glyph;
use crate::guideline::Guideline;
use crate::kerning::KerningData;
use crate::layer::Layer;
use crate::lib_data::LibData;
use crate::metrics::FontMetrics;
use crate::source::Source;
Expand Down Expand Up @@ -89,20 +88,15 @@ struct FontData {
sources: Vec<Source>,
#[serde(default)]
default_source_id: Option<SourceId>,
layers: HashMap<LayerId, Layer>,
glyphs: HashMap<GlyphName, Arc<Glyph>>,
kerning: KerningData,
features: FeatureData,
guidelines: Vec<Guideline>,
lib: LibData,
default_layer_id: LayerId,
}

impl Default for Font {
fn default() -> Self {
let default_layer_id = LayerId::new();
let mut layers = HashMap::new();
layers.insert(default_layer_id, Layer::default_layer());
let default_source = Source::new("Regular".to_string(), Location::new());
let default_source_id = default_source.id();

Expand All @@ -113,13 +107,11 @@ impl Default for Font {
axes: Vec::new(),
sources: vec![default_source],
default_source_id: Some(default_source_id),
layers,
glyphs: HashMap::new(),
kerning: KerningData::new(),
features: FeatureData::new(),
guidelines: Vec::new(),
lib: LibData::new(),
default_layer_id,
}),
}
}
Expand All @@ -131,24 +123,18 @@ impl Font {
}

pub fn empty() -> Self {
let default_layer_id = LayerId::new();
let mut layers = HashMap::new();
layers.insert(default_layer_id, Layer::default_layer());

Self {
inner: Arc::new(FontData {
metadata: FontMetadata::default(),
metrics: FontMetrics::default(),
axes: Vec::new(),
sources: Vec::new(),
default_source_id: None,
layers,
glyphs: HashMap::new(),
kerning: KerningData::new(),
features: FeatureData::new(),
guidelines: Vec::new(),
lib: LibData::new(),
default_layer_id,
}),
}
}
Expand Down Expand Up @@ -225,32 +211,6 @@ impl Font {
!self.data().axes.is_empty()
}

pub fn layers(&self) -> &HashMap<LayerId, Layer> {
&self.data().layers
}

pub fn layer(&self, id: LayerId) -> Option<&Layer> {
self.data().layers.get(&id)
}

pub fn layer_mut(&mut self, id: LayerId) -> Option<&mut Layer> {
self.data_mut().layers.get_mut(&id)
}

pub fn default_layer_id(&self) -> LayerId {
self.data().default_layer_id
}

pub fn default_layer(&self) -> Option<&Layer> {
self.data().layers.get(&self.data().default_layer_id)
}

pub fn add_layer(&mut self, layer: Layer) -> LayerId {
let id = layer.id();
self.data_mut().layers.insert(id, layer);
id
}

pub fn glyphs(&self) -> &HashMap<GlyphName, Arc<Glyph>> {
&self.data().glyphs
}
Expand Down Expand Up @@ -345,7 +305,7 @@ impl Font {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Contour, GlyphLayer, PointType};
use crate::{Contour, GlyphLayer, LayerId, PointType};
use std::sync::Arc;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -406,7 +366,6 @@ mod tests {
fn font_creation() {
let font = Font::new();
assert_eq!(font.glyph_count(), 0);
assert!(font.default_layer().is_some());
assert_eq!(font.sources().len(), 1);
assert_eq!(font.default_source().map(Source::name), Some("Regular"));
}
Expand Down
Loading
Loading