| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use std::path::PathBuf; |
| 17 |
|
| 18 |
pub mod assert; |
| 19 |
pub mod base; |
| 20 |
pub mod cells; |
| 21 |
pub mod compose; |
| 22 |
pub mod draw; |
| 23 |
pub mod manifest; |
| 24 |
pub mod pins; |
| 25 |
pub mod proof; |
| 26 |
pub mod vary; |
| 27 |
pub mod woff2; |
| 28 |
|
| 29 |
|
| 30 |
pub const HOUSE_SET: &str = include_str!("../glyphs/manifest.toml"); |
| 31 |
|
| 32 |
|
| 33 |
pub const PINS: &str = include_str!("../bases/pins.toml"); |
| 34 |
|
| 35 |
#[derive(Debug)] |
| 36 |
pub enum Error { |
| 37 |
Manifest(String), |
| 38 |
Pins(String), |
| 39 |
UnknownSlot { |
| 40 |
asked: String, |
| 41 |
known: Vec<String>, |
| 42 |
}, |
| 43 |
ReservedFontName { |
| 44 |
family: String, |
| 45 |
reserved: String, |
| 46 |
base: String, |
| 47 |
}, |
| 48 |
UnmeasurableBase { |
| 49 |
missing: char, |
| 50 |
what: String, |
| 51 |
}, |
| 52 |
DefaultInstance { |
| 53 |
declared: String, |
| 54 |
actual: String, |
| 55 |
tag: String, |
| 56 |
at: f32, |
| 57 |
}, |
| 58 |
AlreadyDrawn { |
| 59 |
codepoint: u32, |
| 60 |
base: String, |
| 61 |
gid: u32, |
| 62 |
}, |
| 63 |
ArchiveChecksum { |
| 64 |
path: PathBuf, |
| 65 |
url: String, |
| 66 |
expected: String, |
| 67 |
found: String, |
| 68 |
}, |
| 69 |
FaceChecksum { |
| 70 |
path: String, |
| 71 |
expected: String, |
| 72 |
found: String, |
| 73 |
}, |
| 74 |
Offline { |
| 75 |
wanted: PathBuf, |
| 76 |
url: String, |
| 77 |
}, |
| 78 |
Coverage(String), |
| 79 |
Archive(String), |
| 80 |
Fetch(String), |
| 81 |
Font(String), |
| 82 |
Draw(String), |
| 83 |
Woff2(String), |
| 84 |
Io(PathBuf, std::io::Error), |
| 85 |
} |
| 86 |
|
| 87 |
impl std::fmt::Display for Error { |
| 88 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 89 |
match self { |
| 90 |
Error::Manifest(m) => write!(f, "the house glyph set is malformed: {m}"), |
| 91 |
Error::Pins(m) => write!(f, "the pins are malformed: {m}"), |
| 92 |
Error::UnknownSlot { asked, known } => { |
| 93 |
write!(f, "no slot `{asked}`. Pinned slots: {}", known.join(", ")) |
| 94 |
} |
| 95 |
Error::ReservedFontName { |
| 96 |
family, |
| 97 |
reserved, |
| 98 |
base, |
| 99 |
} => write!( |
| 100 |
f, |
| 101 |
"`{family}` carries {base}'s Reserved Font Name \"{reserved}\", which OFL 1.1 \ |
| 102 |
clause 3 bars as a prefix and as a suffix alike. Name the slot instead." |
| 103 |
), |
| 104 |
Error::UnmeasurableBase { missing, what } => write!( |
| 105 |
f, |
| 106 |
"the base does not draw `{missing}`, so {what} cannot be measured. \ |
| 107 |
A base that cannot be measured cannot be refitted against, and guessing \ |
| 108 |
would give the marks a weight that does not match their neighbours." |
| 109 |
), |
| 110 |
Error::DefaultInstance { |
| 111 |
declared, |
| 112 |
actual, |
| 113 |
tag, |
| 114 |
at, |
| 115 |
} => write!( |
| 116 |
f, |
| 117 |
"the pin calls this face `{declared}` and the base's default instance is \ |
| 118 |
`{actual}` ({tag} {at}). A variable cut keeps the base's axis, so it also \ |
| 119 |
keeps the base's default, and a face labelled with a weight it does not \ |
| 120 |
draw at rest is one every naive `@font-face` and every `fc-match` will \ |
| 121 |
believe. Name the face `{actual}` in the pin, or instance the base first." |
| 122 |
), |
| 123 |
Error::AlreadyDrawn { |
| 124 |
codepoint, |
| 125 |
base, |
| 126 |
gid, |
| 127 |
} => write!( |
| 128 |
f, |
| 129 |
"{} is already drawn by {base} (glyph {gid}). The pipeline patches gaps and \ |
| 130 |
does not overwrite a base's own marks; drop it from the set for this slot.", |
| 131 |
manifest::format_codepoint(*codepoint) |
| 132 |
), |
| 133 |
Error::ArchiveChecksum { |
| 134 |
path, |
| 135 |
url, |
| 136 |
expected, |
| 137 |
found, |
| 138 |
} => write!( |
| 139 |
f, |
| 140 |
"{} does not match its pin.\n expected {expected}\n found {found}\n\ |
| 141 |
Upstream moved, or the cached copy is damaged. Re-read {url} before \ |
| 142 |
repinning: a base that changed under us redraws every mark that refits \ |
| 143 |
against it.", |
| 144 |
path.display() |
| 145 |
), |
| 146 |
Error::FaceChecksum { |
| 147 |
path, |
| 148 |
expected, |
| 149 |
found, |
| 150 |
} => write!( |
| 151 |
f, |
| 152 |
"`{path}` inside the archive does not match its pin.\n expected {expected}\n\ |
| 153 |
\x20 found {found}" |
| 154 |
), |
| 155 |
Error::Offline { wanted, url } => write!( |
| 156 |
f, |
| 157 |
"{} is not cached and --offline was given. Fetch it with:\n curl -L -o {} {url}", |
| 158 |
wanted.display(), |
| 159 |
wanted.display() |
| 160 |
), |
| 161 |
Error::Coverage(missing) => { |
| 162 |
write!(f, "the built face does not cover: {missing}") |
| 163 |
} |
| 164 |
Error::Archive(m) => write!(f, "{m}"), |
| 165 |
Error::Fetch(m) => write!(f, "{m}"), |
| 166 |
Error::Font(m) => write!(f, "{m}"), |
| 167 |
Error::Draw(m) => write!(f, "could not draw: {m}"), |
| 168 |
Error::Woff2(m) => write!(f, "woff2: {m}"), |
| 169 |
Error::Io(path, e) => write!(f, "{}: {e}", path.display()), |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
impl std::error::Error for Error {} |
| 175 |
|
| 176 |
|
| 177 |
pub struct Face { |
| 178 |
|
| 179 |
pub style: String, |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
pub stem: String, |
| 184 |
pub ttf: Vec<u8>, |
| 185 |
pub woff2: Vec<u8>, |
| 186 |
|
| 187 |
pub added: Vec<(u32, String)>, |
| 188 |
pub variation: Option<base::Variation>, |
| 189 |
|
| 190 |
|
| 191 |
pub verdict: String, |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
pub struct Cut { |
| 196 |
pub family: String, |
| 197 |
pub base_family: String, |
| 198 |
pub base_version: String, |
| 199 |
|
| 200 |
pub selected: usize, |
| 201 |
pub faces: Vec<Face>, |
| 202 |
|
| 203 |
pub license: Vec<u8>, |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
pub fn cut(slot_id: &str, cache: &std::path::Path, offline: bool) -> Result<Cut, Error> { |
| 218 |
let pins = pins::Pins::parse(PINS)?; |
| 219 |
let manifest = manifest::Manifest::parse(HOUSE_SET)?; |
| 220 |
let slot = pins.slot(slot_id)?; |
| 221 |
let base_pin = pins.base(&slot.base)?; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
base_pin.check_output_name(&slot.family)?; |
| 226 |
|
| 227 |
let selected: Vec<&manifest::GlyphSpec> = manifest |
| 228 |
.glyphs |
| 229 |
.iter() |
| 230 |
.filter(|g| slot.glyphs.includes(g)) |
| 231 |
.collect(); |
| 232 |
let house: Vec<u32> = selected.iter().map(|g| g.codepoint).collect(); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
let floor: &[u32] = if selected.iter().any(|g| g.shape.is_cell_furniture()) { |
| 238 |
&assert::ALLOY_SURFACE |
| 239 |
} else { |
| 240 |
&assert::BODY_SURFACE |
| 241 |
}; |
| 242 |
|
| 243 |
let version = format!("{}.{}", manifest.set.version, base_pin.version); |
| 244 |
let mut faces = Vec::new(); |
| 245 |
for face in base::load(base_pin, cache, offline)? { |
| 246 |
let id = compose::Identity { |
| 247 |
family: &slot.family, |
| 248 |
style: &face.style, |
| 249 |
version: &version, |
| 250 |
set_version: manifest.set.version, |
| 251 |
base: base_pin, |
| 252 |
}; |
| 253 |
let built = compose::build(&face.bytes, &selected, &id)?; |
| 254 |
let verdict = assert::describe(&assert::check( |
| 255 |
floor, |
| 256 |
&compose::coverage(&built.bytes)?, |
| 257 |
&house, |
| 258 |
))?; |
| 259 |
let stem = match &built.variation { |
| 260 |
Some(axis) => format!("{}[{}]", slot.family.replace(' ', ""), axis.tag), |
| 261 |
None => format!( |
| 262 |
"{}-{}", |
| 263 |
slot.family.replace(' ', ""), |
| 264 |
face.style.replace(' ', "") |
| 265 |
), |
| 266 |
}; |
| 267 |
faces.push(Face { |
| 268 |
style: face.style, |
| 269 |
stem, |
| 270 |
woff2: woff2::encode(&built.bytes)?, |
| 271 |
ttf: built.bytes, |
| 272 |
added: built.added, |
| 273 |
variation: built.variation, |
| 274 |
verdict, |
| 275 |
}); |
| 276 |
} |
| 277 |
|
| 278 |
Ok(Cut { |
| 279 |
family: slot.family.clone(), |
| 280 |
base_family: base_pin.family.clone(), |
| 281 |
base_version: base_pin.version.clone(), |
| 282 |
selected: selected.len(), |
| 283 |
license: base::license_text(base_pin, cache, offline)?, |
| 284 |
faces, |
| 285 |
}) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
pub fn cut_web( |
| 313 |
dir: &std::path::Path, |
| 314 |
cache: &std::path::Path, |
| 315 |
offline: bool, |
| 316 |
slots: &[(&str, &str)], |
| 317 |
) -> Result<(), Error> { |
| 318 |
std::fs::create_dir_all(dir).map_err(|e| Error::Io(dir.to_path_buf(), e))?; |
| 319 |
for (slot_id, filename) in slots { |
| 320 |
let cut = cut(slot_id, cache, offline)?; |
| 321 |
let face = cut |
| 322 |
.faces |
| 323 |
.first() |
| 324 |
.ok_or_else(|| Error::Pins(format!("slot `{slot_id}` cut no faces")))?; |
| 325 |
write_if_changed(&dir.join(filename), &face.woff2)?; |
| 326 |
write_if_changed( |
| 327 |
&dir.join(format!("OFL-{}.txt", cut.family.replace(' ', ""))), |
| 328 |
&cut.license, |
| 329 |
)?; |
| 330 |
} |
| 331 |
Ok(()) |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
pub struct NativeFace { |
| 342 |
|
| 343 |
pub slot: String, |
| 344 |
|
| 345 |
pub family: String, |
| 346 |
|
| 347 |
pub style: String, |
| 348 |
|
| 349 |
|
| 350 |
pub default_weight: f32, |
| 351 |
|
| 352 |
pub path: std::path::PathBuf, |
| 353 |
|
| 354 |
pub license_path: std::path::PathBuf, |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
pub fn cut_native( |
| 387 |
dir: &std::path::Path, |
| 388 |
cache: &std::path::Path, |
| 389 |
offline: bool, |
| 390 |
slots: &[(&str, &str)], |
| 391 |
) -> Result<Vec<NativeFace>, Error> { |
| 392 |
std::fs::create_dir_all(dir).map_err(|e| Error::Io(dir.to_path_buf(), e))?; |
| 393 |
let mut written = Vec::with_capacity(slots.len()); |
| 394 |
for (slot_id, filename) in slots { |
| 395 |
let cut = cut(slot_id, cache, offline)?; |
| 396 |
let face = cut |
| 397 |
.faces |
| 398 |
.first() |
| 399 |
.ok_or_else(|| Error::Pins(format!("slot `{slot_id}` cut no faces")))?; |
| 400 |
let path = dir.join(filename); |
| 401 |
let license_path = dir.join(format!("OFL-{}.txt", cut.family.replace(' ', ""))); |
| 402 |
write_if_changed(&path, &face.ttf)?; |
| 403 |
write_if_changed(&license_path, &cut.license)?; |
| 404 |
written.push(NativeFace { |
| 405 |
slot: (*slot_id).to_string(), |
| 406 |
family: cut.family.clone(), |
| 407 |
style: face.style.clone(), |
| 408 |
default_weight: face.variation.as_ref().map_or(400.0, |axis| axis.default), |
| 409 |
path, |
| 410 |
license_path, |
| 411 |
}); |
| 412 |
} |
| 413 |
Ok(written) |
| 414 |
} |
| 415 |
|
| 416 |
fn write_if_changed(path: &std::path::Path, bytes: &[u8]) -> Result<(), Error> { |
| 417 |
if std::fs::read(path).is_ok_and(|existing| existing == bytes) { |
| 418 |
return Ok(()); |
| 419 |
} |
| 420 |
std::fs::write(path, bytes).map_err(|e| Error::Io(path.to_path_buf(), e)) |
| 421 |
} |
| 422 |
|