Skip to content

Commit fdf6b0c

Browse files
committed
moss/boulder: Add support for font providers
Reads FAMILY_NAME from OpenType fonts in order to provide a font provider. e.g. `moss it 'font(Noto Sans Tamil Condensed Thin)'`
1 parent 1f16563 commit fdf6b0c

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

Cargo.lock

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ tokio-util = { version = "0.7.11", features = ["io"] }
5353
url = { version = "2.5.2", features = ["serde"] }
5454
xxhash-rust = { version = "0.8.11", features = ["xxh3"] }
5555
zstd = { version = "0.13.2", features = ["zstdmt"] }
56+
read-fonts = "0.20.0"
5657

5758
[profile.release]
5859
lto = "thin"

boulder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ strum.workspace = true
4343
thiserror.workspace = true
4444
tokio.workspace = true
4545
url.workspace = true
46+
read-fonts.workspace = true

boulder/src/package/analysis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl<'a> Chain<'a> {
3838
Box::new(handler::elf),
3939
Box::new(handler::pkg_config),
4040
Box::new(handler::cmake),
41+
Box::new(handler::font),
4142
// Catch-all if not excluded
4243
Box::new(handler::include_any),
4344
],

boulder/src/package/analysis/handler.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{path::PathBuf, process::Command};
22

33
use moss::{dependency, Dependency, Provider};
4+
use read_fonts::{types::NameId, FontRef, TableProvider};
45

56
use crate::package::collect::PathInfo;
67

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

137138
Ok(Decision::NextHandler.into())
138139
}
140+
141+
pub fn font(bucket: &mut BucketMut, info: &mut PathInfo) -> Result<Response, BoxError> {
142+
if !info.target_path.starts_with("/usr/share/font")
143+
&& !(info.file_name().ends_with(".ttf") || info.file_name().ends_with(".otf"))
144+
{
145+
return Ok(Decision::NextHandler.into());
146+
}
147+
148+
let bytes = std::fs::read(&info.path)?;
149+
let font = FontRef::new(&bytes)?;
150+
let font_name_table = font.name()?;
151+
if let Some(record) = font_name_table
152+
.name_record()
153+
.iter()
154+
.find(|record| record.name_id() == NameId::FAMILY_NAME)
155+
{
156+
let data = record.string(font_name_table.string_data())?;
157+
let family_name = data.chars().collect::<String>();
158+
159+
bucket.providers.insert(Provider {
160+
kind: dependency::Kind::Font,
161+
name: family_name,
162+
});
163+
}
164+
165+
Ok(Decision::NextHandler.into())
166+
}

crates/stone/src/payload/meta.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ pub enum Dependency {
5050

5151
/// An emul32-compatible pkgconfig .pc dependency (lib32/*.pc)
5252
PkgConfig32,
53+
54+
/// OpenType Font FAMILY_NAME
55+
Font,
5356
}
5457

5558
#[repr(u8)]
@@ -146,6 +149,7 @@ fn decode_dependency(i: u8) -> Result<Dependency, DecodeError> {
146149
6 => Dependency::Binary,
147150
7 => Dependency::SystemBinary,
148151
8 => Dependency::PkgConfig32,
152+
9 => Dependency::Font,
149153
_ => return Err(DecodeError::UnknownDependency(i)),
150154
};
151155
Ok(result)

moss/src/dependency.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub enum Kind {
5959

6060
/// Exported 32-bit pkgconfig provider
6161
PkgConfig32,
62+
63+
/// OpenType Font FAMILY_NAME
64+
Font,
6265
}
6366

6467
/// Convert payload dependency types to our internal representation
@@ -74,6 +77,7 @@ impl From<payload::meta::Dependency> for Kind {
7477
payload::meta::Dependency::Binary => Kind::Binary,
7578
payload::meta::Dependency::SystemBinary => Kind::SystemBinary,
7679
payload::meta::Dependency::PkgConfig32 => Kind::PkgConfig32,
80+
payload::meta::Dependency::Font => Kind::Font,
7781
}
7882
}
7983
}
@@ -91,6 +95,7 @@ impl From<Kind> for payload::meta::Dependency {
9195
Kind::Binary => Self::Binary,
9296
Kind::SystemBinary => Self::SystemBinary,
9397
Kind::PkgConfig32 => Self::PkgConfig32,
98+
Kind::Font => Self::Font,
9499
}
95100
}
96101
}

0 commit comments

Comments
 (0)