Skip to content

Commit

Permalink
Merge pull request #486 from googlefonts/case
Browse files Browse the repository at this point in the history
Add an underscore after capitals in glyph names
  • Loading branch information
rsheeter authored Oct 9, 2023
2 parents 1ccc237 + 310e620 commit ca5862c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
6 changes: 3 additions & 3 deletions fontc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ mod tests {
use fontbe::orchestration::{
AnyWorkId, Context as BeContext, Glyph, LocaFormatWrapper, WorkId as BeWorkIdentifier,
};
use fontdrasil::types::GlyphName;
use fontdrasil::{paths::glyph_file, types::GlyphName};
use fontir::{
ir::{self, KernParticipant},
orchestration::{Context as FeContext, Persistable, WorkId as FeWorkIdentifier},
Expand Down Expand Up @@ -818,12 +818,12 @@ mod tests {
}

fn read_ir_glyph(build_dir: &Path, name: &str) -> ir::Glyph {
let raw_glyph = read_file(&build_dir.join(format!("glyph_ir/{name}.yml")));
let raw_glyph = read_file(&build_dir.join("glyph_ir").join(glyph_file(name, ".yml")));
ir::Glyph::read(&mut raw_glyph.as_slice())
}

fn read_be_glyph(build_dir: &Path, name: &str) -> RawGlyph {
let raw_glyph = read_file(&build_dir.join(format!("glyphs/{name}.glyf")));
let raw_glyph = read_file(&build_dir.join("glyphs").join(glyph_file(name, ".glyf")));
let read: &mut dyn Read = &mut raw_glyph.as_slice();
Glyph::read(read).data
}
Expand Down
42 changes: 41 additions & 1 deletion fontdrasil/src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
pub fn glyph_file(glyph_name: &str, suffix: &str) -> String {
// TODO handle names that are invalid for the filesystem
// Ref https://github.com/unified-font-object/ufo-spec/issues/164
glyph_name.to_owned() + suffix
let mut filename = Vec::new();
for ch in glyph_name.chars() {
filename.push(ch);
if ch == '_' || ch.is_uppercase() {
filename.push('_');
}
}
filename.extend(suffix.chars());
filename.into_iter().collect()
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use super::glyph_file;

/// <https://github.com/googlefonts/fontc/issues/41>
fn assert_unique_for_caseinsensitive_fs(names: &[&str]) {
let filenames: HashSet<_> = names
.iter()
.map(|n| glyph_file(n, ""))
.map(|n| n.to_lowercase())
.collect();
assert_eq!(
names.len(),
filenames.len(),
"{names:?} became {filenames:?}"
);
}

#[test]
fn lower_and_upper_a() {
assert_unique_for_caseinsensitive_fs(&["a", "A"]);
}

#[test]
fn adding_underscore_avoids_collisions() {
// if we don't add _ to _ the resulting names are identical
assert_unique_for_caseinsensitive_fs(&["Aa", "a_a"]);
}
}

0 comments on commit ca5862c

Please sign in to comment.