Skip to content

Commit

Permalink
[fea-rs] Use Opts more
Browse files Browse the repository at this point in the history
Opts are now available during compilation.

This also adds (currently unused) options to specify if you want gpos &
gsub to be compiled.
  • Loading branch information
cmyr committed Feb 6, 2024
1 parent 2214fa0 commit fd868b0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion fea-rs/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ pub fn compile<V: VariationInfo, T: FeatureProvider>(
glyph_map: &GlyphMap,
var_info: Option<&V>,
extra_features: Option<&T>,
opts: Opts,
) -> Result<(Compilation, DiagnosticSet), DiagnosticSet> {
let mut ctx = CompilationCtx::new(glyph_map, tree.source_map(), var_info, extra_features);
let mut ctx = CompilationCtx::new(glyph_map, tree.source_map(), var_info, extra_features, opts);
ctx.compile(&tree.typed_root());
match ctx.build() {
Ok((compilation, warnings)) => {
Expand Down
6 changes: 5 additions & 1 deletion fea-rs/src/compile/compile_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
Token,
},
typed::ContextualRuleNode,
Diagnostic, GlyphIdent, GlyphMap, Kind, NodeOrToken,
Diagnostic, GlyphIdent, GlyphMap, Kind, NodeOrToken, Opts,
};

use super::{
Expand Down Expand Up @@ -68,6 +68,7 @@ pub struct CompilationCtx<'a, F: FeatureProvider, V: VariationInfo> {
source_map: &'a SourceMap,
variation_info: Option<&'a V>,
feature_writer: Option<&'a F>,
opts: Opts,
/// Any errors or warnings generated during compilation.
pub errors: Vec<Diagnostic>,
/// Stores any [specified table values][tables] in the input FEA.
Expand Down Expand Up @@ -99,6 +100,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> {
source_map: &'a SourceMap,
variation_info: Option<&'a V>,
feature_writer: Option<&'a F>,
opts: Opts,
) -> Self {
CompilationCtx {
glyph_map,
Expand All @@ -122,6 +124,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> {
script: Default::default(),
mark_attach_class_id: Default::default(),
mark_filter_sets: Default::default(),
opts,
}
}

Expand Down Expand Up @@ -249,6 +252,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> {
stat,
gsub,
gpos,
opts: self.opts.clone(),
},
self.errors.clone(),
))
Expand Down
4 changes: 2 additions & 2 deletions fea-rs/src/compile/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> Compiler<'a, F, V> {
tree.source_map(),
self.var_info,
self.feature_writer,
self.opts,
);
ctx.compile(&tree.typed_root());

Expand All @@ -157,9 +158,8 @@ impl<'a, F: FeatureProvider, V: VariationInfo> Compiler<'a, F, V> {

/// Compile to a binary font.
pub fn compile_binary(self) -> Result<Vec<u8>, CompilerError> {
let opts = self.opts.clone();
let glyph_map = self.glyph_map;
Ok(self.compile()?.to_binary(glyph_map, opts)?)
Ok(self.compile()?.to_binary(glyph_map)?)
}
}

Expand Down
16 changes: 16 additions & 0 deletions fea-rs/src/compile/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const DEFAULT_N_MESSAGES_TO_PRINT: usize = 100;
pub struct Opts {
pub(crate) make_post_table: bool,
pub(crate) max_n_errors: usize,
pub(crate) compile_gsub: bool,
pub(crate) compile_gpos: bool,
}

impl Opts {
Expand All @@ -36,13 +38,27 @@ impl Opts {
self.max_n_errors = max_n_errors;
self
}

/// Specify whether or not we should compile the GPOS table. Default is `true`.
pub fn compile_gpos(mut self, flag: bool) -> Self {
self.compile_gpos = flag;
self
}

/// Specify whether or not we should compile the GSUB table. Default is `true`.
pub fn compile_gsub(mut self, flag: bool) -> Self {
self.compile_gsub = flag;
self
}
}

impl Default for Opts {
fn default() -> Self {
Self {
make_post_table: false,
max_n_errors: DEFAULT_N_MESSAGES_TO_PRINT,
compile_gsub: true,
compile_gpos: true,
}
}
}
6 changes: 4 additions & 2 deletions fea-rs/src/compile/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::GlyphMap;
///
/// [`to_binary`]: Compilation::to_binary
pub struct Compilation {
/// The options passed in for this compilation.
pub(crate) opts: Opts,
/// The `head` table, if one was generated
pub head: Option<wtables::head::Head>,
/// The `hhea` table, if one was generated
Expand Down Expand Up @@ -77,13 +79,13 @@ impl Compilation {
/// This is a convenience method used for things like testing; if you are
/// building a font compiler you will probably prefer to manipulate the
/// generated tables directly.
pub fn to_binary(&self, glyph_map: &GlyphMap, opts: Opts) -> Result<Vec<u8>, BuilderError> {
pub fn to_binary(&self, glyph_map: &GlyphMap) -> Result<Vec<u8>, BuilderError> {
// because we often inspect our output with ttx, and ttx fails if maxp is
// missing, we create a maxp table.
let mut builder = self.to_font_builder()?;
let maxp = Maxp::new(glyph_map.len().try_into().unwrap());
builder.add_table(&maxp)?;
if opts.make_post_table {
if self.opts.make_post_table {
let post = glyph_map.make_post_table();
builder.add_table(&post)?;
}
Expand Down

0 comments on commit fd868b0

Please sign in to comment.