Skip to content

Commit

Permalink
Add --glyph-name-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Mar 3, 2023
1 parent f6b3114 commit 5e6e08f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[patch.crates-io]
kurbo = { git="https://github.com/linebender/kurbo.git" }

[workspace]

members = [
Expand Down
2 changes: 2 additions & 0 deletions fontc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ thiserror = "1.0.37"
rayon = "1.6.0"
crossbeam-channel = "0.5.6"

regex = "1.7.1"

indexmap = "1.9.2"

[dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions fontc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub enum Error {
FileExpected(PathBuf),
#[error("At least one work item failed")]
TasksFailed(Vec<(AnyWorkId, String)>),
#[error("Invalid regex")]
BadRegex(#[from] regex::Error),
}
45 changes: 38 additions & 7 deletions fontc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use fontir::{
use glyphs2fontir::source::GlyphsIrSource;
use indexmap::IndexSet;
use log::{debug, error, info, log_enabled, trace, warn};
use regex::Regex;
use serde::{Deserialize, Serialize};
use ufo2fontir::source::DesignSpaceIrSource;

Expand Down Expand Up @@ -64,6 +65,11 @@ struct Args {
#[arg(short, long)]
#[clap(default_value = "build")]
build_dir: PathBuf,

/// Glyph names must match this regex to be processed
#[arg(short, long)]
#[clap(default_value = None)]
glyph_name_filter: Option<String>,
}

impl Args {
Expand Down Expand Up @@ -118,6 +124,7 @@ fn require_dir(dir: &Path) -> Result<PathBuf, io::Error> {
if !dir.exists() {
fs::create_dir(dir)?
}
debug!("require_dir {:?}", dir);
Ok(dir.to_path_buf())
}

Expand Down Expand Up @@ -195,12 +202,12 @@ impl ChangeDetector {
}

fn glyphs_changed(&self) -> IndexSet<GlyphName> {
let glyph_iter = self.current_inputs.glyphs.iter();

if self.static_metadata_ir_change() {
return self.current_inputs.glyphs.keys().cloned().collect();
return glyph_iter.map(|(name, _)| name).cloned().collect();
}
self.current_inputs
.glyphs
.iter()
glyph_iter
.filter_map(
|(glyph_name, curr_state)| match self.prev_inputs.glyphs.get(glyph_name) {
Some(prev_state) => {
Expand Down Expand Up @@ -315,7 +322,7 @@ fn add_feature_be_job(
change_detector: &mut ChangeDetector,
workload: &mut Workload,
) -> Result<(), Error> {
if change_detector.feature_be_change() {
if change_detector.feature_be_change() && change_detector.glyph_name_filter.is_none() {
let id: AnyWorkId = BeWorkIdentifier::Features.into();
let write_access = access_one(id.clone());
workload.insert(
Expand All @@ -330,6 +337,10 @@ fn add_feature_be_job(
},
);
} else {
// Features are extremely prone to not making sense when glyphs are filtered
if change_detector.glyph_name_filter.is_some() {
warn!("Not processing BE Features because a glyph name filter is active");
}
workload.mark_success(BeWorkIdentifier::Features);
}
Ok(())
Expand Down Expand Up @@ -379,6 +390,7 @@ fn add_glyph_ir_jobs(
}

struct ChangeDetector {
glyph_name_filter: Option<Regex>,
ir_paths: IrPaths,
ir_source: Box<dyn Source>,
prev_inputs: Input,
Expand All @@ -390,10 +402,28 @@ impl ChangeDetector {
fn new(config: Config, ir_paths: IrPaths, prev_inputs: Input) -> Result<ChangeDetector, Error> {
// What sources are we dealing with?
let mut ir_source = ir_source(&config.args.source)?;
let current_inputs = ir_source.inputs().map_err(Error::FontIrError)?;
let mut current_inputs = ir_source.inputs().map_err(Error::FontIrError)?;
let be_paths = BePaths::new(ir_paths.build_dir());

let glyph_name_filter = config
.args
.glyph_name_filter
.map(|raw_filter| Regex::new(&raw_filter))
.transpose()
.map_err(Error::BadRegex)?;

if let Some(regex) = &glyph_name_filter {
current_inputs.glyphs.retain(|glyph_name, _| {
let result = regex.is_match(glyph_name.as_str());
if !result {
trace!("'{glyph_name}' does not match --glyph_name_filter");
}
result
});
}

Ok(ChangeDetector {
glyph_name_filter,
ir_paths,
ir_source,
prev_inputs,
Expand Down Expand Up @@ -541,7 +571,7 @@ impl Workload {
inserted
}
Err(e) => {
error!("{:?} failed {}", completed_id, e);
error!("{:?} failed {:?}", completed_id, e);
self.error.push((completed_id.clone(), format!("{e}")));
true
}
Expand Down Expand Up @@ -677,6 +707,7 @@ mod tests {

fn test_args(build_dir: &Path, source: &str) -> Args {
Args {
glyph_name_filter: None,
source: testdata_dir().join(source),
emit_ir: true,
emit_debug: false,
Expand Down
15 changes: 13 additions & 2 deletions glyphs2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ impl Source for GlyphsIrSource {
fn create_static_metadata_work(&self, input: &Input) -> Result<Box<IrWork>, Error> {
self.check_static_metadata(&input.static_metadata)?;
let font_info = self.cache.as_ref().unwrap().font_info.clone();
Ok(Box::new(StaticMetadataWork { font_info }))
let glyph_names = Arc::new(input.glyphs.keys().cloned().collect());

Ok(Box::new(StaticMetadataWork {
font_info,
glyph_names,
}))
}

fn create_glyph_ir_work(
Expand Down Expand Up @@ -175,6 +180,7 @@ impl GlyphsIrSource {

struct StaticMetadataWork {
font_info: Arc<FontInfo>,
glyph_names: Arc<HashSet<GlyphName>>,
}

impl Work<Context, WorkError> for StaticMetadataWork {
Expand All @@ -184,7 +190,12 @@ impl Work<Context, WorkError> for StaticMetadataWork {
debug!("Static metadata for {}", font.family_name);
let axes = font_info.axes.clone();
let glyph_locations = font_info.master_locations.values().cloned().collect();
let glyph_order = font.glyph_order.iter().map(|s| s.into()).collect();
let glyph_order = font
.glyph_order
.iter()
.map(|s| s.into())
.filter(|gn| self.glyph_names.contains(gn))
.collect();
context.set_static_metadata(
StaticMetadata::new(axes, glyph_order, glyph_locations)
.map_err(WorkError::VariationModelError)?,
Expand Down
8 changes: 3 additions & 5 deletions ufo2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl DesignSpaceIrSource {
struct Cache {
static_metadata: StateSet,
locations: HashMap<PathBuf, Vec<DesignLocation>>,
glyph_names: Arc<HashSet<GlyphName>>,
designspace_file: PathBuf,
designspace: Arc<DesignSpaceDocument>,
fea_files: Arc<Vec<PathBuf>>,
Expand All @@ -52,15 +51,13 @@ struct Cache {
impl Cache {
fn new(
static_metadata: StateSet,
glyph_names: HashSet<GlyphName>,
locations: HashMap<PathBuf, Vec<DesignLocation>>,
designspace_file: PathBuf,
designspace: DesignSpaceDocument,
feature_files: Vec<PathBuf>,
) -> Cache {
Cache {
static_metadata,
glyph_names: Arc::from(glyph_names),
locations,
designspace_file,
designspace: Arc::from(designspace),
Expand Down Expand Up @@ -263,7 +260,6 @@ impl Source for DesignSpaceIrSource {

self.cache = Some(Cache::new(
static_metadata.clone(),
glyph_names,
glif_locations,
self.designspace_file.clone(),
designspace,
Expand All @@ -281,10 +277,12 @@ impl Source for DesignSpaceIrSource {
self.check_static_metadata(&input.static_metadata)?;
let cache = self.cache.as_ref().unwrap();

let glyph_names = Arc::new(input.glyphs.keys().cloned().collect());

Ok(Box::new(StaticMetadataWork {
designspace_file: cache.designspace_file.clone(),
designspace: cache.designspace.clone(),
glyph_names: cache.glyph_names.clone(),
glyph_names,
}))
}

Expand Down

0 comments on commit 5e6e08f

Please sign in to comment.