Skip to content

Commit

Permalink
moss/boulder: Add support for font providers
Browse files Browse the repository at this point in the history
Reads FAMILY_NAME from OpenType fonts in order to provide a font
provider.

e.g. `moss it 'font(Noto Sans Tamil Condensed Thin)'`
  • Loading branch information
joebonrichie committed Aug 13, 2024
1 parent 1f16563 commit 4f7880e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ tokio-util = { version = "0.7.11", features = ["io"] }
url = { version = "2.5.2", features = ["serde"] }
xxhash-rust = { version = "0.8.11", features = ["xxh3"] }
zstd = { version = "0.13.2", features = ["zstdmt"] }
read-fonts = "0.20.0"

[profile.release]
lto = "thin"
Expand Down
1 change: 1 addition & 0 deletions boulder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ strum.workspace = true
thiserror.workspace = true
tokio.workspace = true
url.workspace = true
read-fonts.workspace = true
1 change: 1 addition & 0 deletions boulder/src/package/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<'a> Chain<'a> {
Box::new(handler::elf),
Box::new(handler::pkg_config),
Box::new(handler::cmake),
Box::new(handler::font),
// Catch-all if not excluded
Box::new(handler::include_any),
],
Expand Down
28 changes: 28 additions & 0 deletions boulder/src/package/analysis/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{path::PathBuf, process::Command};

use moss::{dependency, Dependency, Provider};
use read_fonts::{types::NameId, FontRef, TableProvider};

use crate::package::collect::PathInfo;

Expand Down Expand Up @@ -136,3 +137,30 @@ pub fn cmake(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, Bo

Ok(Decision::NextHandler.into())
}

pub fn font(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, BoxError> {
if !info.target_path.starts_with("/usr/share/font")

Check warning on line 142 in boulder/src/package/analysis/handler.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] boulder/src/package/analysis/handler.rs#L142

warning: this boolean expression can be simplified --> boulder/src/package/analysis/handler.rs:142:8 | 142 | if !info.target_path.starts_with("/usr/share/font") | ________^ 143 | | && !(info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf")) | |______________________________________________________________________________________^ help: try: `!(info.target_path.starts_with("/usr/share/font") || info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf"))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool = note: `#[warn(clippy::nonminimal_bool)]` on by default
Raw output
boulder/src/package/analysis/handler.rs:142:8:w:warning: this boolean expression can be simplified
   --> boulder/src/package/analysis/handler.rs:142:8
    |
142 |       if !info.target_path.starts_with("/usr/share/font")
    |  ________^
143 | |         && !(info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf"))
    | |______________________________________________________________________________________^ help: try: `!(info.target_path.starts_with("/usr/share/font") || info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf"))`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
    = note: `#[warn(clippy::nonminimal_bool)]` on by default


__END__
&& !(info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf"))
{
return Ok(Decision::NextHandler.into());
}

let bytes = std::fs::read(&info.path)?;
let font = FontRef::new(&bytes)?;
let font_name_table = font.name()?;
if let Some(record) = font_name_table
.name_record()
.into_iter()

Check warning on line 153 in boulder/src/package/analysis/handler.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] boulder/src/package/analysis/handler.rs#L153

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `slice` --> boulder/src/package/analysis/handler.rs:153:10 | 153 | .into_iter() | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref = note: `#[warn(clippy::into_iter_on_ref)]` on by default
Raw output
boulder/src/package/analysis/handler.rs:153:10:w:warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `slice`
   --> boulder/src/package/analysis/handler.rs:153:10
    |
153 |         .into_iter()
    |          ^^^^^^^^^ help: call directly: `iter`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
    = note: `#[warn(clippy::into_iter_on_ref)]` on by default


__END__
.find(|record| record.name_id() == NameId::FAMILY_NAME)
{
let data = record.string(font_name_table.string_data())?;
let family_name = data.chars().collect::<String>();

bucket.providers.insert(Provider {
kind: dependency::Kind::Font,
name: family_name,
});
}

Ok(Decision::NextHandler.into())
}
4 changes: 4 additions & 0 deletions crates/stone/src/payload/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub enum Dependency {

/// An emul32-compatible pkgconfig .pc dependency (lib32/*.pc)
PkgConfig32,

/// OpenType Font FAMILY_NAME
Font,
}

#[repr(u8)]
Expand Down Expand Up @@ -146,6 +149,7 @@ fn decode_dependency(i: u8) -> Result<Dependency, DecodeError> {
6 => Dependency::Binary,
7 => Dependency::SystemBinary,
8 => Dependency::PkgConfig32,
9 => Dependency::Font,
_ => return Err(DecodeError::UnknownDependency(i)),
};
Ok(result)
Expand Down
5 changes: 5 additions & 0 deletions moss/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub enum Kind {

/// Exported 32-bit pkgconfig provider
PkgConfig32,

/// OpenType Font FAMILY_NAME
Font,
}

/// Convert payload dependency types to our internal representation
Expand All @@ -74,6 +77,7 @@ impl From<payload::meta::Dependency> for Kind {
payload::meta::Dependency::Binary => Kind::Binary,
payload::meta::Dependency::SystemBinary => Kind::SystemBinary,
payload::meta::Dependency::PkgConfig32 => Kind::PkgConfig32,
payload::meta::Dependency::Font => Kind::Font,
}
}
}
Expand All @@ -91,6 +95,7 @@ impl From<Kind> for payload::meta::Dependency {
Kind::Binary => Self::Binary,
Kind::SystemBinary => Self::SystemBinary,
Kind::PkgConfig32 => Self::PkgConfig32,
Kind::Font => Self::Font,
}
}
}
Expand Down

0 comments on commit 4f7880e

Please sign in to comment.