Skip to content

Commit

Permalink
Merge pull request #707 from Brooooooklyn/fix-font-ext
Browse files Browse the repository at this point in the history
fix: file extensions in `loadFontsFromDir()` are no longer case-sensitive
  • Loading branch information
Brooooooklyn authored Aug 21, 2023
2 parents 56ed2f8 + 9342e33 commit efa95a4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion __test__/draw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ test('fillText-AA', async (t) => {

test('fillText-COLRv1', async (t) => {
const { ctx, canvas } = t.context
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'COLRv1.ttf'), 'Colrv1')
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'COLR-v1.ttf'), 'Colrv1')
ctx.font = '100px Colrv1'
ctx.fillText('abc', 50, 100)
await snapshotImage(t, { canvas, ctx }, 'png', 0.5)
Expand Down
File renamed without changes.
11 changes: 9 additions & 2 deletions src/global_fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub mod GlobalFonts {
Ok(font.register(font_data.as_ref(), maybe_name_alias))
}

// TODO: Do file extensions in font_path need to be converted to lowercase?
// Windows and macOS are case-insensitive, Linux is not.
// See: https://github.com/Brooooooklyn/canvas/actions/runs/5893418006/job/15985737723
#[napi]
pub fn register_from_path(font_path: String, name_alias: Option<String>) -> Result<bool> {
let maybe_name_alias = name_alias.and_then(|s| if s.is_empty() { None } else { Some(s) });
Expand Down Expand Up @@ -86,9 +89,13 @@ fn load_fonts_from_dir<P: AsRef<path::Path>>(dir: P) -> napi::Result<u32> {
load_fonts_from_dir(f.path())?;
} else {
let p = f.path();
let ext = p.extension().and_then(|s| s.to_str());
// The font file extensions are case-insensitive.
let ext = p
.extension()
.and_then(|s| s.to_str())
.map(|s| s.to_ascii_lowercase());

match ext {
match ext.as_deref() {
Some("ttf") | Some("ttc") | Some("otf") | Some("pfb") | Some("woff2")
| Some("woff") => {
if let Some(p) = p.into_os_string().to_str() {
Expand Down

0 comments on commit efa95a4

Please sign in to comment.