From fd868b0ec811d8472c647a4fce9b2e330e5f7a2f Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Thu, 1 Feb 2024 16:15:37 -0500 Subject: [PATCH] [fea-rs] Use Opts more Opts are now available during compilation. This also adds (currently unused) options to specify if you want gpos & gsub to be compiled. --- fea-rs/src/compile.rs | 3 ++- fea-rs/src/compile/compile_ctx.rs | 6 +++++- fea-rs/src/compile/compiler.rs | 4 ++-- fea-rs/src/compile/opts.rs | 16 ++++++++++++++++ fea-rs/src/compile/output.rs | 6 ++++-- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/fea-rs/src/compile.rs b/fea-rs/src/compile.rs index 7a95fcad5..e2d0099db 100644 --- a/fea-rs/src/compile.rs +++ b/fea-rs/src/compile.rs @@ -62,8 +62,9 @@ pub fn compile( 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)) => { diff --git a/fea-rs/src/compile/compile_ctx.rs b/fea-rs/src/compile/compile_ctx.rs index 74b4eea2b..55ad7e34e 100644 --- a/fea-rs/src/compile/compile_ctx.rs +++ b/fea-rs/src/compile/compile_ctx.rs @@ -29,7 +29,7 @@ use crate::{ Token, }, typed::ContextualRuleNode, - Diagnostic, GlyphIdent, GlyphMap, Kind, NodeOrToken, + Diagnostic, GlyphIdent, GlyphMap, Kind, NodeOrToken, Opts, }; use super::{ @@ -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, /// Stores any [specified table values][tables] in the input FEA. @@ -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, @@ -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, } } @@ -249,6 +252,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> { stat, gsub, gpos, + opts: self.opts.clone(), }, self.errors.clone(), )) diff --git a/fea-rs/src/compile/compiler.rs b/fea-rs/src/compile/compiler.rs index 581938a01..4450a3eab 100644 --- a/fea-rs/src/compile/compiler.rs +++ b/fea-rs/src/compile/compiler.rs @@ -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()); @@ -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, 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)?) } } diff --git a/fea-rs/src/compile/opts.rs b/fea-rs/src/compile/opts.rs index 8ed2db588..967a24475 100644 --- a/fea-rs/src/compile/opts.rs +++ b/fea-rs/src/compile/opts.rs @@ -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 { @@ -36,6 +38,18 @@ 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 { @@ -43,6 +57,8 @@ impl Default for Opts { Self { make_post_table: false, max_n_errors: DEFAULT_N_MESSAGES_TO_PRINT, + compile_gsub: true, + compile_gpos: true, } } } diff --git a/fea-rs/src/compile/output.rs b/fea-rs/src/compile/output.rs index e77220293..8e54d8c5f 100644 --- a/fea-rs/src/compile/output.rs +++ b/fea-rs/src/compile/output.rs @@ -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, /// The `hhea` table, if one was generated @@ -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, BuilderError> { + pub fn to_binary(&self, glyph_map: &GlyphMap) -> Result, 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)?; }