Skip to content

Commit

Permalink
Add kerning to fea include test to trigger breakage; fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Aug 23, 2023
1 parent 927d330 commit eb56511
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
2 changes: 2 additions & 0 deletions fontbe/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Error {
InvalidTableBytes(Tag),
#[error("Missing directory:{0}")]
MissingDirectory(PathBuf),
#[error("Missing file:{0}")]
FileExpected(PathBuf),
}

#[derive(Debug)]
Expand Down
47 changes: 34 additions & 13 deletions fontbe/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
ffi::{OsStr, OsString},
fmt::Display,
fs,
path::PathBuf,
sync::Arc,
};

Expand Down Expand Up @@ -40,38 +41,57 @@ pub struct FeatureWork {}
struct InMemoryResolver {
content_path: OsString,
content: Arc<str>,
// Our fea might be generated in memory, such as to inject generated kerning,
// while compiling a disk-based source with a well defined include path
include_dir: Option<PathBuf>,
}

impl SourceResolver for InMemoryResolver {
fn get_contents(&self, path: &OsStr) -> Result<Arc<str>, SourceLoadError> {
if path == &*self.content_path {
fn get_contents(&self, rel_path: &OsStr) -> Result<Arc<str>, SourceLoadError> {
if rel_path == &*self.content_path {
return Ok(self.content.clone());
}
Err(SourceLoadError::new(
path.to_os_string(),
NotSupportedError::new(),
))
let Some(include_dir) = &self.include_dir else {
return Err(SourceLoadError::new(
rel_path.to_os_string(),
NoIncludePathError::new(),
));
};
let path = include_dir
.join(rel_path)
.canonicalize()
.map_err(|e| SourceLoadError::new(rel_path.to_os_string(), e))?;
if !path.is_file() {
return Err(SourceLoadError::new(
rel_path.to_os_string(),
Error::FileExpected(path),
));
}
trace!("Resolved {rel_path:?} to {path:?}");
let contents = fs::read_to_string(path)
.map_err(|e| SourceLoadError::new(rel_path.to_os_string(), e))?;
Ok(Arc::from(contents.as_str()))
}
}

#[derive(Debug)]
struct NotSupportedError {}
struct NoIncludePathError {}

impl NotSupportedError {
fn new() -> NotSupportedError {
NotSupportedError {}
impl NoIncludePathError {
fn new() -> NoIncludePathError {
NoIncludePathError {}
}
}

impl std::error::Error for NotSupportedError {
impl std::error::Error for NoIncludePathError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

impl Display for NotSupportedError {
impl Display for NoIncludePathError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Not supported")?;
f.write_str("No include path available")?;
Ok(())
}
}
Expand Down Expand Up @@ -260,6 +280,7 @@ impl FeatureWork {
Compiler::new(root.clone(), &glyph_order).with_resolver(InMemoryResolver {
content_path: root,
content: Arc::from(fea_content.as_str()),
include_dir: include_dir.clone(),
});
if let Some(include_dir) = include_dir {
compiler = compiler.with_project_root(include_dir)
Expand Down
11 changes: 11 additions & 0 deletions resources/testdata/fea_include_ufo/FeaInc-Bold.ufo/kerning.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>bar</key>
<dict>
<key>plus</key>
<integer>-200</integer>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>bar</key>
<dict>
<key>plus</key>
<integer>-100</integer>
</dict>
</dict>
</plist>

0 comments on commit eb56511

Please sign in to comment.