Skip to content

Commit 503f70c

Browse files
committed
Add support for woffv1 and change woff feature flag names to avoid
confusion between crate names and woff format versiosns.
1 parent 32b4c05 commit 503f70c

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

apps/wpt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55
license.workspace = true
66

77
[dependencies]
8-
blitz-dom = { path = "../../packages/blitz-dom", default-features = false, features = ["svg", "woff"] }
8+
blitz-dom = { path = "../../packages/blitz-dom", default-features = false, features = ["svg", "woff-c"] }
99
blitz-html = { path = "../../packages/blitz-html" }
1010
blitz-traits = { path = "../../packages/blitz-traits" }
1111
blitz-renderer-vello = { path = "../../packages/blitz-renderer-vello" }

packages/blitz-dom/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ license.workspace = true
55
edition = "2024"
66

77
[features]
8-
default = ["tracing", "svg", "woff", "clipboard", "accessibility"]
8+
default = ["tracing", "svg", "woff-c", "clipboard", "accessibility"]
99
tracing = ["dep:tracing"]
1010
svg = ["dep:usvg"]
11-
woff = ["dep:woff"]
12-
woff2 = ["dep:woff2"]
11+
# WOFF decoding using the "woff" crate which binds to C libraries
12+
# ("woff" for woff2) and "sfnt2woff" for woff1).
13+
# Both woff1 and woff2 are supported
14+
woff-c = ["dep:woff"]
15+
# WOFF decoding using the "woff2" crate which is pure Rust
16+
# Only woff2 is supported. Does not work correct with all woff2 fonts
17+
woff-rust = ["dep:woff2"]
1318
clipboard = ["dep:arboard"]
1419
accessibility = ["accesskit"]
1520

packages/blitz-dom/src/net.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,11 @@ impl NetHandler for FontFaceHandler {
159159
self.0 = match bytes.as_ref() {
160160
// WOFF (v1) files begin with 0x774F4646 ('wOFF' in ascii)
161161
// See: <https://w3c.github.io/woff/woff1/spec/Overview.html#WOFFHeader>
162-
#[cfg(any(feature = "woff", feature = "woff2"))]
163-
[b'w', b'O', b'F', b'F', ..] => FontFaceSourceFormatKeyword::Woff
162+
#[cfg(any(feature = "woff-c", feature = "woff-rust"))]
164163
[b'w', b'O', b'F', b'F', ..] => FontFaceSourceFormatKeyword::Woff,
165164
// WOFF2 files begin with 0x774F4632 ('wOF2' in ascii)
166165
// See: <https://w3c.github.io/woff/woff2/#woff20Header>
167-
#[cfg(any(feature = "woff", feature = "woff2"))]
166+
#[cfg(any(feature = "woff-c", feature = "woff-rust"))]
168167
[b'w', b'O', b'F', b'2', ..] => FontFaceSourceFormatKeyword::Woff2,
169168
// Opentype fonts with CFF data begin with 0x4F54544F ('OTTO' in ascii)
170169
// See: <https://learn.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font>
@@ -180,21 +179,36 @@ impl NetHandler for FontFaceHandler {
180179
}
181180

182181
// Satisfy rustc's mutability linting with woff feature both enabled/disabled
183-
#[cfg(any(feature = "woff", feature = "woff2"))]
182+
#[cfg(any(feature = "woff-c", feature = "woff-rust"))]
184183
let mut bytes = bytes;
185184

186185
match self.0 {
187-
#[cfg(any(feature = "woff", feature = "woff2"))]
186+
#[cfg(feature = "woff-c")]
187+
FontFaceSourceFormatKeyword::Woff => {
188+
#[cfg(feature = "tracing")]
189+
tracing::info!("Decompressing woff1 font");
190+
191+
// Use woff crate to decompress font
192+
let decompressed = woff::version1::decompress(&bytes);
193+
194+
if let Some(decompressed) = decompressed {
195+
bytes = Bytes::from(decompressed);
196+
} else {
197+
#[cfg(feature = "tracing")]
198+
tracing::warn!("Failed to decompress woff1 font");
199+
}
200+
}
201+
#[cfg(any(feature = "woff-c", feature = "woff-rust"))]
188202
FontFaceSourceFormatKeyword::Woff2 => {
189203
#[cfg(feature = "tracing")]
190204
tracing::info!("Decompressing woff2 font");
191205

192206
// Use woff crate to decompress font
193-
#[cfg(feature = "woff")]
207+
#[cfg(feature = "woff-c")]
194208
let decompressed = woff::version2::decompress(&bytes);
195209

196210
// Use woff2 crate to decompress font
197-
#[cfg(feature = "woff2")]
211+
#[cfg(feature = "woff-rust")]
198212
let decompressed = woff2::decode::convert_woff2_to_ttf(&mut bytes).ok();
199213

200214
if let Some(decompressed) = decompressed {

0 commit comments

Comments
 (0)