Skip to content

Commit

Permalink
Sample of using sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Oct 24, 2023
1 parent 75d75e5 commit ade3346
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ members = [
]

[patch.crates-io]
fea-rs = { git = "https://github.com/cmyr/fea-rs.git", branch = "public-feature-builder-api" }
fea-rs = { git = "https://github.com/cmyr/fea-rs.git", branch = "sketchy" }
43 changes: 32 additions & 11 deletions fontbe/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use std::{

use fea_rs::{
compile::{
AxisLocation, Compilation, FeatureBuilder, FeatureProvider, PairPosBuilder, VariationInfo,
AxisLocation, Compilation, FeatureBuilder, FeatureProvider, Location as FeaLocation,
PairPosBuilder, VariationInfo,
},
parse::{SourceLoadError, SourceResolver},
Compiler, GlyphClass, GlyphMap, GlyphName as FeaRsGlyphName,
Expand All @@ -33,7 +34,7 @@ use fontdrasil::{
orchestration::{Access, Work},
types::GlyphName,
};
use write_fonts::{tables::gpos::{ValueRecord, AnchorTable, AnchorFormat1}, tables::layout::LookupFlag, OtRound};
use write_fonts::{tables::gpos::ValueRecord, tables::layout::LookupFlag, OtRound};

use crate::{
error::Error,
Expand Down Expand Up @@ -195,6 +196,19 @@ struct MarkGroup<'a> {
marks: Vec<(GlyphName, &'a Anchor)>,
}

/// Hopefully temporary; we shouldn't have multiple location types!
///
/// https://github.com/cmyr/fea-rs/issues/242
trait AsFea {
fn as_fea(&self) -> FeaLocation;
}

impl AsFea for NormalizedLocation {
fn as_fea(&self) -> FeaLocation {
todo!()
}
}

struct FeatureWriter<'a> {
kerning: &'a Kerning,
glyph_map: &'a GlyphOrder,
Expand All @@ -219,9 +233,9 @@ impl<'a> FeatureWriter<'a> {

fn glyph_id(&self, glyph_name: &GlyphName) -> Option<GlyphId> {
self.glyph_map
.glyph_id(glyph_name)
.map(|val| Some(GlyphId::new(val as u16)))
.unwrap_or_default()
.glyph_id(glyph_name)
.map(|val| Some(GlyphId::new(val as u16)))
.unwrap_or_default()
}

//TODO: at least for kerning, we should be able to generate the lookups
Expand All @@ -232,9 +246,7 @@ impl<'a> FeatureWriter<'a> {
}

// a little helper closure used below
let name_to_gid = |name| {
self.glyph_id(name).unwrap_or(GlyphId::NOTDEF)
};
let name_to_gid = |name| self.glyph_id(name).unwrap_or(GlyphId::NOTDEF);

// convert the groups stored in the Kerning object into the glyph classes
// expected by fea-rs:
Expand Down Expand Up @@ -385,10 +397,19 @@ impl<'a> FeatureWriter<'a> {
"markClass {} <anchor {} {}> @MC{}; # TODO variable anchor",
glyph_name, default_pos.x, default_pos.y, mark.name
));
// TODO is below equivalent to above?
// TODO error handling
let mark_gid = self.glyph_id(&mark.name).unwrap_or(GlyphId::NOTDEF);
let anchor_table = AnchorTable::Format1(AnchorFormat1::new(default_pos.x.ot_round(), default_pos.y.ot_round()));
builder.define_mark_class(glyph_name.as_str(), vec![(mark_gid.into(), Some(anchor_table))]); // TODO error handling
let variations = mark
.positions
.iter()
.map(|(loc, pos)| (loc.as_fea(), (pos.x.ot_round(), pos.y.ot_round())))
.collect();
builder.add_mark_class_for_glyph(
mark_gid,
(default_pos.x.ot_round(), default_pos.y.ot_round()),
format!("MC{}", mark.name),
variations,
);
}

// if we have bases *and* marks emit mark to base
Expand Down

0 comments on commit ade3346

Please sign in to comment.