| 1 |
use gf_metadata::{AxisProto, FallbackProto}; |
| 2 |
use std::{collections::HashSet, ops::Index}; |
| 3 |
|
| 4 |
#[cfg(feature = "fontations")] |
| 5 |
pub use fontations::read::FontRef; // Re-export our types for users |
| 6 |
#[cfg(feature = "fontations")] |
| 7 |
use fontations::skrifa::string::StringId; |
| 8 |
#[cfg(feature = "fontations")] |
| 9 |
use fontations::{ |
| 10 |
read::{ReadError, TableProvider}, |
| 11 |
write::FontBuilder, |
| 12 |
}; |
| 13 |
use indexmap::IndexMap; |
| 14 |
|
| 15 |
include!(concat!(env!("OUT_DIR"), "/data.rs")); |
| 16 |
|
| 17 |
const LINKED_VALUES: [(&str, (f32, f32)); 2] = [("wght", (400.0, 700.0)), ("ital", (0.0, 1.0))]; |
| 18 |
|
| 19 |
fn linked_value(axis: &str, value: f32) -> Option<f32> { |
| 20 |
LINKED_VALUES |
| 21 |
.iter() |
| 22 |
.find(|(linked_axis, (cur, _link))| *linked_axis == axis && value == *cur) |
| 23 |
.map(|(_, (_, link))| *link) |
| 24 |
} |
| 25 |
|
| 26 |
const GF_STATIC_STYLES: [(&str, u16); 18] = [ |
| 27 |
("Thin", 100), |
| 28 |
("ExtraLight", 200), |
| 29 |
("Light", 300), |
| 30 |
("Regular", 400), |
| 31 |
("Medium", 500), |
| 32 |
("SemiBold", 600), |
| 33 |
("Bold", 700), |
| 34 |
("ExtraBold", 800), |
| 35 |
("Black", 900), |
| 36 |
("Thin Italic", 100), |
| 37 |
("ExtraLight Italic", 200), |
| 38 |
("Light Italic", 300), |
| 39 |
("Italic", 400), |
| 40 |
("Medium Italic", 500), |
| 41 |
("SemiBold Italic", 600), |
| 42 |
("Bold Italic", 700), |
| 43 |
("ExtraBold Italic", 800), |
| 44 |
("Black Italic", 900), |
| 45 |
]; |
| 46 |
|
| 47 |
#[cfg(feature = "fontations")] |
| 48 |
const PROTECTED_IDS: [StringId; 9] = [ |
| 49 |
StringId::FAMILY_NAME, |
| 50 |
StringId::SUBFAMILY_NAME, |
| 51 |
StringId::UNIQUE_ID, |
| 52 |
StringId::FULL_NAME, |
| 53 |
StringId::VERSION_STRING, |
| 54 |
StringId::POSTSCRIPT_NAME, |
| 55 |
StringId::TYPOGRAPHIC_FAMILY_NAME, |
| 56 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 57 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 58 |
]; |
| 59 |
|
| 60 |
pub struct AxisRegistry { |
| 61 |
axes: BTreeMap<String, Box<AxisProto>>, |
| 62 |
} |
| 63 |
|
| 64 |
#[derive(Debug, Clone)] |
| 65 |
pub struct FontAxis { |
| 66 |
pub tag: String, |
| 67 |
pub min: f32, |
| 68 |
pub max: f32, |
| 69 |
pub default: f32, |
| 70 |
} |
| 71 |
|
| 72 |
#[derive(Debug, Clone)] |
| 73 |
pub struct NameParticle { |
| 74 |
pub name: Option<String>, |
| 75 |
pub value: f32, |
| 76 |
pub elided: bool, |
| 77 |
} |
| 78 |
|
| 79 |
impl AxisRegistry { |
| 80 |
pub fn new() -> Self { |
| 81 |
Self { |
| 82 |
axes: (*AXES).clone(), |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
pub fn get(&self, tag: &str) -> Option<&AxisProto> { |
| 87 |
self.axes.get(tag).map(|v| &**v) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn contains_key(&self, tag: &str) -> bool { |
| 91 |
self.axes.contains_key(tag) |
| 92 |
} |
| 93 |
|
| 94 |
pub fn iter(&self) -> impl Iterator<Item = (&String, &AxisProto)> { |
| 95 |
self.axes.iter().map(|(k, v)| (k, &**v)) |
| 96 |
} |
| 97 |
|
| 98 |
pub fn get_fallback<'a>(&'a self, name: &str) -> Option<(&'a str, &'a FallbackProto)> { |
| 99 |
self.axes |
| 100 |
.iter() |
| 101 |
.flat_map(|(tag, axis)| { |
| 102 |
let fallback = axis |
| 103 |
.fallback |
| 104 |
.iter() |
| 105 |
.find(|f| f.name.as_deref() == Some(name)); |
| 106 |
fallback.map(|f| (tag.as_str(), f)) |
| 107 |
}) |
| 108 |
.next() |
| 109 |
} |
| 110 |
|
| 111 |
// This is fallbacks_in_fvar, but without assuming any particular font representation |
| 112 |
pub fn fallbacks<'a>( |
| 113 |
&'a self, |
| 114 |
font_axes: &'a [FontAxis], |
| 115 |
) -> impl Iterator<Item = (String, Vec<FallbackProto>)> + 'a { |
| 116 |
font_axes.iter().filter_map(|axis| { |
| 117 |
self.get(&axis.tag).map(|registry_axis| { |
| 118 |
( |
| 119 |
registry_axis.tag.clone().unwrap_or_default(), |
| 120 |
registry_axis |
| 121 |
.fallback |
| 122 |
.iter() |
| 123 |
.filter(|f| f.value() >= axis.min && f.value() <= axis.max) |
| 124 |
.cloned() |
| 125 |
.collect(), |
| 126 |
) |
| 127 |
}) |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
// This is fallbacks_in_name_table, but without assuming any particular font representation |
| 132 |
pub fn name_table_fallbacks<'a>( |
| 133 |
&'a self, |
| 134 |
family_name: &'a str, |
| 135 |
subfamily_name: &'a str, |
| 136 |
font_axes: &'a [FontAxis], |
| 137 |
) -> impl Iterator<Item = (&'a str, &'a FallbackProto)> + 'a { |
| 138 |
let axis_names: HashSet<&str> = font_axes.iter().map(|axis| axis.tag.as_ref()).collect(); |
| 139 |
let tokens = family_name |
| 140 |
.split_whitespace() |
| 141 |
.skip(1) |
| 142 |
.chain(subfamily_name.split_whitespace()); |
| 143 |
tokens |
| 144 |
.flat_map(|token| self.get_fallback(token)) |
| 145 |
.filter(move |(tag, _)| !axis_names.contains(tag)) |
| 146 |
} |
| 147 |
|
| 148 |
pub fn fallback_for_value<'a>( |
| 149 |
&'a self, |
| 150 |
axis_tag: &str, |
| 151 |
value: f32, |
| 152 |
) -> Option<&'a FallbackProto> { |
| 153 |
self.get(axis_tag) |
| 154 |
.and_then(|axis| axis.fallback.iter().find(|f| f.value == Some(value))) |
| 155 |
} |
| 156 |
|
| 157 |
pub fn axis_order(&self) -> Vec<&str> { |
| 158 |
let mut axis_tags: Vec<&str> = self |
| 159 |
.axes |
| 160 |
.keys() |
| 161 |
.filter(|k| k.chars().all(|c| c.is_ascii_uppercase())) |
| 162 |
.map(|k| k.as_str()) |
| 163 |
.collect(); |
| 164 |
axis_tags.sort(); |
| 165 |
axis_tags.extend(vec!["opsz", "wdth", "wght", "ital", "slnt"]); |
| 166 |
axis_tags |
| 167 |
} |
| 168 |
|
| 169 |
// This is the old "_fvar_dflts" |
| 170 |
pub fn name_particles<'a>(&self, font_axes: &'a [FontAxis]) -> IndexMap<&'a str, NameParticle> { |
| 171 |
let mut particles = IndexMap::new(); |
| 172 |
for axis in font_axes { |
| 173 |
if axis.tag == "opsz" { |
| 174 |
particles.insert( |
| 175 |
"opsz", |
| 176 |
NameParticle { |
| 177 |
name: Some(format!("{}pt", axis.default)), |
| 178 |
value: axis.default, |
| 179 |
elided: true, |
| 180 |
}, |
| 181 |
); |
| 182 |
} else if let Some(fallback) = self.fallback_for_value(&axis.tag, axis.default) { |
| 183 |
particles.insert( |
| 184 |
&axis.tag, |
| 185 |
NameParticle { |
| 186 |
name: fallback.name.clone(), |
| 187 |
value: axis.default, |
| 188 |
elided: (fallback.value() == self.get(&axis.tag).unwrap().default_value()) |
| 189 |
&& !(["Regular", "Italic", "14pt"].contains(&fallback.name())), |
| 190 |
}, |
| 191 |
); |
| 192 |
} else { |
| 193 |
particles.insert( |
| 194 |
&axis.tag, |
| 195 |
NameParticle { |
| 196 |
name: None, |
| 197 |
value: axis.default, |
| 198 |
elided: true, |
| 199 |
}, |
| 200 |
); |
| 201 |
}; |
| 202 |
} |
| 203 |
particles |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
impl Default for AxisRegistry { |
| 208 |
fn default() -> Self { |
| 209 |
Self::new() |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
impl Index<&str> for AxisRegistry { |
| 214 |
type Output = AxisProto; |
| 215 |
|
| 216 |
fn index(&self, tag: &str) -> &Self::Output { |
| 217 |
self.get(tag).expect("No such axis") |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[cfg(feature = "fontations")] |
| 222 |
mod monkeypatching; |
| 223 |
#[cfg(feature = "fontations")] |
| 224 |
mod nametable; |
| 225 |
#[cfg(feature = "fontations")] |
| 226 |
mod stat; |
| 227 |
#[cfg(feature = "fontations")] |
| 228 |
mod fontations_impl { |
| 229 |
use super::*; |
| 230 |
use fontations::{ |
| 231 |
skrifa::{string::StringId, MetadataProvider, Tag}, |
| 232 |
write::{ |
| 233 |
from_obj::ToOwnedTable, |
| 234 |
tables::{ |
| 235 |
fvar::{Fvar, InstanceRecord}, |
| 236 |
name::{Name, NameRecord}, |
| 237 |
os2::Os2, |
| 238 |
stat::Stat, |
| 239 |
}, |
| 240 |
types::Fixed, |
| 241 |
}, |
| 242 |
}; |
| 243 |
use monkeypatching::{AxisValueNameId, SetAxisValueNameId}; |
| 244 |
use nametable::{ |
| 245 |
add_name, best_familyname, best_subfamilyname, find_or_add_name, rewrite_or_insert, |
| 246 |
}; |
| 247 |
use stat::{AxisLocation, AxisRecord, AxisValue, StatBuilder}; |
| 248 |
use std::{cmp::Reverse, collections::HashMap}; |
| 249 |
|
| 250 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 251 |
pub enum RenameAggressiveness { |
| 252 |
#[default] |
| 253 |
Aggressive, |
| 254 |
Conservative, |
| 255 |
} |
| 256 |
|
| 257 |
pub fn build_name_table( |
| 258 |
font: FontRef, |
| 259 |
family_name: Option<&str>, |
| 260 |
style_name: Option<&str>, |
| 261 |
siblings: &[FontRef], |
| 262 |
aggressive: Option<RenameAggressiveness>, |
| 263 |
) -> Result<Vec<u8>, Box<dyn std::error::Error>> { |
| 264 |
let mut new_font = FontBuilder::new(); |
| 265 |
let family_name = family_name |
| 266 |
.map(|x| x.to_string()) |
| 267 |
.unwrap_or_else(|| best_familyname(&font).unwrap_or("Unknown".to_string())); |
| 268 |
let style_name = style_name |
| 269 |
.map(|x| x.to_string()) |
| 270 |
.unwrap_or_else(|| best_subfamilyname(&font).unwrap_or("Regular".to_string())); |
| 271 |
|
| 272 |
let mut new_name = if font.table_data(Tag::new(b"fvar")).is_some() { |
| 273 |
build_vf_name_table(&mut new_font, &font, &family_name, siblings, aggressive)? |
| 274 |
} else { |
| 275 |
build_static_name_table_v1(&mut new_font, &font, &family_name, &style_name, aggressive)? |
| 276 |
}; |
| 277 |
|
| 278 |
let mut styles: Vec<_> = GF_STATIC_STYLES.iter().collect(); |
| 279 |
styles.sort_by_key(|(name, _weight)| Reverse(name.len())); |
| 280 |
for (name, weight) in styles.iter() { |
| 281 |
if style_name.contains(name) { |
| 282 |
let mut new_os2: Os2 = font.os2()?.to_owned_table(); |
| 283 |
new_os2.us_weight_class = *weight; |
| 284 |
new_font.add_table(&new_os2)?; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
// Set RIBBI bits |
| 289 |
new_name.name_record.sort(); |
| 290 |
new_font.add_table(&new_name)?; |
| 291 |
Ok(new_font.copy_missing_tables(font).build()) |
| 292 |
} |
| 293 |
|
| 294 |
fn fvar_instance_collisions(font: &FontRef, siblings: &[FontRef]) -> bool { |
| 295 |
let fonts = siblings.iter().chain(std::iter::once(font)); |
| 296 |
let is_italic = fonts |
| 297 |
.map(|f| { |
| 298 |
f.post() |
| 299 |
.map(|post| post.italic_angle().abs() != Fixed::from_f64(0.0)) |
| 300 |
.unwrap_or(false) |
| 301 |
}) |
| 302 |
.collect::<Vec<_>>(); |
| 303 |
is_italic.len() != is_italic.iter().collect::<HashSet<_>>().len() |
| 304 |
} |
| 305 |
|
| 306 |
fn build_vf_name_table( |
| 307 |
newfont: &mut FontBuilder, |
| 308 |
font: &FontRef, |
| 309 |
family_name: &str, |
| 310 |
siblings: &[FontRef], |
| 311 |
aggressive: Option<RenameAggressiveness>, |
| 312 |
) -> Result<Name, Box<dyn std::error::Error>> { |
| 313 |
let style_name = vf_style_name(font, family_name)?; |
| 314 |
let mut new_name: Name = (if fvar_instance_collisions(font, siblings) { |
| 315 |
build_static_name_table_v1(newfont, font, family_name, &style_name, aggressive) |
| 316 |
} else { |
| 317 |
build_static_name_table(newfont, font, family_name, style_name, aggressive) |
| 318 |
})?; |
| 319 |
// println!("Records: {:#?}", new_name.name_record); |
| 320 |
build_variations_ps_name(&mut new_name, font, Some(family_name)); |
| 321 |
|
| 322 |
// Ensure table records are sorted |
| 323 |
new_name.name_record.sort(); |
| 324 |
Ok(new_name) |
| 325 |
} |
| 326 |
|
| 327 |
pub fn build_variations_ps_name(newname: &mut Name, font: &FontRef, family_name: Option<&str>) { |
| 328 |
let fallback = best_familyname(font); |
| 329 |
let family_name = family_name.or(fallback.as_deref()).unwrap_or("New Font"); |
| 330 |
let subfamily_name = best_subfamilyname(font).unwrap_or("Regular".to_string()); |
| 331 |
let font_axes = font_axes(font).unwrap_or_default(); |
| 332 |
let registry = AxisRegistry::new(); |
| 333 |
let font_styles = registry.name_table_fallbacks(family_name, &subfamily_name, &font_axes); |
| 334 |
let mut var_ps = family_name.replace(" ", ""); |
| 335 |
for (_, fallback) in font_styles { |
| 336 |
let fallback_name = fallback.name(); |
| 337 |
if !var_ps.contains(fallback_name) { |
| 338 |
var_ps.push_str(fallback_name); |
| 339 |
} |
| 340 |
} |
| 341 |
rewrite_or_insert( |
| 342 |
&mut newname.name_record, |
| 343 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 344 |
&var_ps, |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
fn build_static_name_table_v1( |
| 349 |
newfont: &mut FontBuilder, |
| 350 |
font: &FontRef, |
| 351 |
family_name: &str, |
| 352 |
style_name: &str, |
| 353 |
aggressive: Option<RenameAggressiveness>, |
| 354 |
) -> Result<Name, Box<dyn std::error::Error>> { |
| 355 |
let (v1_tokens, non_weight) = style_name |
| 356 |
.split_whitespace() |
| 357 |
.partition::<Vec<_>, _>(|token| GF_STATIC_STYLES.iter().any(|(name, _)| name == token)); |
| 358 |
let family_tokens = family_name.split_whitespace(); |
| 359 |
let mut new_family_name = vec![]; |
| 360 |
for token in family_tokens { |
| 361 |
if non_weight.contains(&token) || new_family_name.contains(&token) { |
| 362 |
continue; |
| 363 |
} |
| 364 |
new_family_name.push(token); |
| 365 |
} |
| 366 |
new_family_name.extend(non_weight); |
| 367 |
let family_name = new_family_name.join(" "); |
| 368 |
let mut style_name = v1_tokens |
| 369 |
.join(" ") |
| 370 |
.replace("Regular Italic", "Italic") |
| 371 |
.trim() |
| 372 |
.to_string(); |
| 373 |
if style_name.is_empty() { |
| 374 |
style_name = "Regular".to_string(); |
| 375 |
} |
| 376 |
build_static_name_table(newfont, font, &family_name, style_name, aggressive) |
| 377 |
} |
| 378 |
|
| 379 |
fn build_static_name_table( |
| 380 |
newfont: &mut FontBuilder, |
| 381 |
font: &FontRef, |
| 382 |
family_name: &str, |
| 383 |
style_name: String, |
| 384 |
aggressive: Option<RenameAggressiveness>, |
| 385 |
) -> Result<Name, Box<dyn std::error::Error>> { |
| 386 |
let mut name: Name = font.name()?.to_owned_table(); |
| 387 |
let mut records = name.name_record.into_iter().collect::<Vec<NameRecord>>(); |
| 388 |
records.retain(|record| record.platform_id != 1); |
| 389 |
let existing_name = best_familyname(font).unwrap_or("New Font".to_string()); |
| 390 |
let mut removed_names: HashMap<StringId, String> = HashMap::new(); |
| 391 |
let full_name = family_name.to_string() + " " + &style_name; |
| 392 |
let ps_name = (family_name.to_string() + "-" + &style_name).replace(" ", ""); |
| 393 |
let removeable_name_ids = |
| 394 |
if ["Italic", "Bold Italic", "Bold", "Regular"].contains(&style_name.as_str()) { |
| 395 |
rewrite_or_insert(&mut records, StringId::FAMILY_NAME, family_name); |
| 396 |
rewrite_or_insert(&mut records, StringId::SUBFAMILY_NAME, &style_name); |
| 397 |
rewrite_or_insert(&mut records, StringId::FULL_NAME, &full_name); |
| 398 |
rewrite_or_insert(&mut records, StringId::POSTSCRIPT_NAME, &ps_name); |
| 399 |
vec![ |
| 400 |
StringId::TYPOGRAPHIC_FAMILY_NAME, |
| 401 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 402 |
StringId::new(21), |
| 403 |
StringId::new(22), |
| 404 |
] |
| 405 |
} else { |
| 406 |
let style_tokens = style_name.split_whitespace().collect::<Vec<_>>(); |
| 407 |
let mut new_family_name_tokens = family_name.split_whitespace().collect::<Vec<_>>(); |
| 408 |
let is_italic = style_tokens.contains(&"Italic"); |
| 409 |
let additional_tokens = (style_tokens.into_iter().filter(|token| { |
| 410 |
!(*token == "Regular" |
| 411 |
|| *token == "Italic" |
| 412 |
|| new_family_name_tokens.contains(token)) |
| 413 |
})) |
| 414 |
.collect::<Vec<_>>(); |
| 415 |
new_family_name_tokens.extend(additional_tokens); |
| 416 |
let new_family_name = new_family_name_tokens.join(" "); |
| 417 |
let new_style_name = if is_italic { "Italic" } else { "Regular" }; |
| 418 |
|
| 419 |
rewrite_or_insert(&mut records, StringId::FAMILY_NAME, &new_family_name); |
| 420 |
rewrite_or_insert(&mut records, StringId::SUBFAMILY_NAME, new_style_name); |
| 421 |
rewrite_or_insert(&mut records, StringId::FULL_NAME, &full_name); |
| 422 |
rewrite_or_insert(&mut records, StringId::POSTSCRIPT_NAME, &ps_name); |
| 423 |
rewrite_or_insert(&mut records, StringId::TYPOGRAPHIC_FAMILY_NAME, family_name); |
| 424 |
rewrite_or_insert( |
| 425 |
&mut records, |
| 426 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 427 |
&style_name, |
| 428 |
); |
| 429 |
|
| 430 |
vec![StringId::new(21), StringId::new(22)] |
| 431 |
}; |
| 432 |
|
| 433 |
let mut to_delete = vec![]; |
| 434 |
for name_id in removeable_name_ids.into_iter() { |
| 435 |
if let Some(existing) = records.iter_mut().position(|r| { |
| 436 |
r.name_id == name_id |
| 437 |
&& r.platform_id == 3 |
| 438 |
&& r.encoding_id == 1 |
| 439 |
&& r.language_id == 0x409 |
| 440 |
}) { |
| 441 |
removed_names.insert(name_id, records[existing].string.to_string()); |
| 442 |
to_delete.push(existing); |
| 443 |
} |
| 444 |
} |
| 445 |
for i in to_delete.into_iter().rev() { |
| 446 |
records.remove(i); |
| 447 |
} |
| 448 |
|
| 449 |
// If STAT table was using any removed names, add then back with a new ID |
| 450 |
if !removed_names.is_empty() && font.table_data(Tag::from_be_bytes(*b"STAT")).is_some() { |
| 451 |
let mut stat: Stat = font.stat()?.to_owned_table(); |
| 452 |
for axis in stat.design_axes.iter_mut() { |
| 453 |
let id = axis.axis_name_id; |
| 454 |
if let Some(old_name) = removed_names.get(&id) { |
| 455 |
axis.axis_name_id = find_or_add_name(&mut records, old_name); |
| 456 |
} |
| 457 |
} |
| 458 |
// Also do the axis value array |
| 459 |
if let Some(axis_values) = stat.offset_to_axis_values.as_deref_mut() { |
| 460 |
for axis_value in axis_values.iter_mut() { |
| 461 |
if let Some(name_id) = axis_value.value_name_id() { |
| 462 |
if let Some(old_name) = removed_names.get(&name_id) { |
| 463 |
axis_value.set_value_name_id(find_or_add_name(&mut records, old_name)); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
newfont.add_table(&stat)?; |
| 469 |
} |
| 470 |
|
| 471 |
if let Some(existing) = records.iter_mut().find(|r| { |
| 472 |
r.name_id == StringId::UNIQUE_ID |
| 473 |
&& r.platform_id == 3 |
| 474 |
&& r.encoding_id == 1 |
| 475 |
&& r.language_id == 0x409 |
| 476 |
}) { |
| 477 |
if let Some(new_unique) = new_unique_id(font, &full_name, &ps_name, &existing.string) { |
| 478 |
*existing.string = new_unique; |
| 479 |
} |
| 480 |
} |
| 481 |
if aggressive.unwrap_or_default() == RenameAggressiveness::Aggressive { |
| 482 |
for record in records.iter_mut() { |
| 483 |
if PROTECTED_IDS.contains(&record.name_id) |
| 484 |
|| !record.string.contains(&existing_name) |
| 485 |
{ |
| 486 |
continue; |
| 487 |
} |
| 488 |
if !record.string.contains(' ') { |
| 489 |
*record.string = record |
| 490 |
.string |
| 491 |
.replace(&existing_name, family_name) |
| 492 |
.replace(" ", ""); |
| 493 |
} else { |
| 494 |
*record.string = record.string.replace(&existing_name, family_name); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
name.name_record = records; |
| 499 |
name.name_record.sort(); |
| 500 |
Ok(name) |
| 501 |
} |
| 502 |
|
| 503 |
fn new_unique_id( |
| 504 |
font: &FontRef<'_>, |
| 505 |
full_name: &str, |
| 506 |
ps_name: &str, |
| 507 |
existing: &str, |
| 508 |
) -> Option<String> { |
| 509 |
let new = existing.to_string(); |
| 510 |
if let Some(existing_full_name) = font |
| 511 |
.localized_strings(StringId::FULL_NAME) |
| 512 |
.english_or_first() |
| 513 |
{ |
| 514 |
let existing_full_name = existing_full_name.chars().collect::<String>(); |
| 515 |
if new.contains(&existing_full_name) { |
| 516 |
return Some(new.replace(&existing_full_name, full_name)); |
| 517 |
} |
| 518 |
} |
| 519 |
if let Some(existing_ps_name) = font |
| 520 |
.localized_strings(StringId::POSTSCRIPT_NAME) |
| 521 |
.english_or_first() |
| 522 |
{ |
| 523 |
let existing_ps_name = existing_ps_name.chars().collect::<String>(); |
| 524 |
if new.contains(&existing_ps_name) { |
| 525 |
return Some(new.replace(&existing_ps_name, ps_name)); |
| 526 |
} |
| 527 |
} |
| 528 |
None |
| 529 |
} |
| 530 |
|
| 531 |
fn font_axes(font: &FontRef) -> Result<Vec<FontAxis>, ReadError> { |
| 532 |
let fvar = font.fvar().unwrap(); |
| 533 |
let mut axes = vec![]; |
| 534 |
for axis in fvar.axes()? { |
| 535 |
let tag = axis.axis_tag().to_string(); |
| 536 |
let min = axis.min_value().to_f32(); |
| 537 |
let max = axis.max_value().to_f32(); |
| 538 |
let default = axis.default_value().to_f32(); |
| 539 |
axes.push(FontAxis { |
| 540 |
tag, |
| 541 |
min, |
| 542 |
max, |
| 543 |
default, |
| 544 |
}); |
| 545 |
} |
| 546 |
Ok(axes) |
| 547 |
} |
| 548 |
|
| 549 |
fn vf_style_name(font: &FontRef, family_name: &str) -> Result<String, ReadError> { |
| 550 |
let axisregistry = AxisRegistry::new(); |
| 551 |
let axes: Vec<_> = font_axes(font)?; |
| 552 |
let fvar_dflts = axisregistry.name_particles(&axes); |
| 553 |
let mut relevant_particles: Vec<String> = axisregistry |
| 554 |
.axis_order() |
| 555 |
.iter() |
| 556 |
.flat_map(|tag| fvar_dflts.get(tag)) |
| 557 |
.filter(|particle| !particle.elided) |
| 558 |
.flat_map(|particle| particle.name.clone()) |
| 559 |
.collect::<Vec<_>>(); |
| 560 |
let family_name_tokens = family_name.split_whitespace().collect::<HashSet<_>>(); |
| 561 |
let subfamily_name = best_subfamilyname(font); |
| 562 |
let font_styles = axisregistry |
| 563 |
.name_table_fallbacks( |
| 564 |
family_name, |
| 565 |
subfamily_name.as_deref().unwrap_or("Regular"), |
| 566 |
&axes, |
| 567 |
) |
| 568 |
.map(|(_tag, proto)| proto.name()) |
| 569 |
.filter(|name| !family_name_tokens.contains(name)) |
| 570 |
.map(|name| name.to_string()) |
| 571 |
.filter(|name| !relevant_particles.contains(name)) |
| 572 |
.collect::<Vec<_>>(); |
| 573 |
relevant_particles.extend(font_styles); |
| 574 |
let name = relevant_particles |
| 575 |
.join(" ") |
| 576 |
.replace("Regular Italic", "Italic"); |
| 577 |
Ok(name) |
| 578 |
} |
| 579 |
|
| 580 |
/// Return a font with an fvar table which conforms to the Google Fonts instance spec: |
| 581 |
/// https://github.com/googlefonts/gf-docs/tree/main/Spec#fvar-instances |
| 582 |
pub fn build_fvar_instances( |
| 583 |
font: FontRef, |
| 584 |
axis_dflts: Option<HashMap<String, f32>>, |
| 585 |
) -> Result<Vec<u8>, Box<dyn std::error::Error>> { |
| 586 |
let axis_registry = AxisRegistry::new(); |
| 587 |
let mut new_font = FontBuilder::new(); |
| 588 |
let mut fvar: Fvar = font.fvar().unwrap().to_owned_table(); |
| 589 |
let mut name_table: Name = font.name().unwrap().to_owned_table(); |
| 590 |
let family_name = best_familyname(&font).unwrap_or("New Font".to_string()); |
| 591 |
let style_name = best_subfamilyname(&font).unwrap_or("Regular".to_string()); |
| 592 |
// Protect name IDs which are shared with the STAT table |
| 593 |
let mut stat_name_ids = HashSet::new(); |
| 594 |
if let Ok(stat) = font.stat() { |
| 595 |
for axis in stat.design_axes()?.iter() { |
| 596 |
stat_name_ids.insert(axis.axis_name_id()); |
| 597 |
} |
| 598 |
if let Some(axis_values_offset) = stat.offset_to_axis_values().transpose()? { |
| 599 |
for axis_value in axis_values_offset.axis_values().iter().flatten() { |
| 600 |
stat_name_ids.insert(axis_value.value_name_id()); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
// Remove old fvar subfamily and ps name records |
| 606 |
for instance in fvar.axis_instance_arrays.instances.iter() { |
| 607 |
if instance.subfamily_name_id != StringId::SUBFAMILY_NAME |
| 608 |
&& instance.subfamily_name_id != StringId::TYPOGRAPHIC_SUBFAMILY_NAME |
| 609 |
&& !stat_name_ids.contains(&instance.subfamily_name_id) |
| 610 |
{ |
| 611 |
name_table |
| 612 |
.name_record |
| 613 |
.retain(|record| record.name_id != instance.subfamily_name_id); |
| 614 |
} |
| 615 |
if let Some(psname) = instance.post_script_name_id { |
| 616 |
if psname != StringId::POSTSCRIPT_NAME { |
| 617 |
name_table |
| 618 |
.name_record |
| 619 |
.retain(|record| record.name_id != psname); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
let axes = font_axes(&font)?; |
| 625 |
let fvar_defaults = axis_registry.name_particles(&axes); |
| 626 |
|
| 627 |
let axis_dflts = axis_dflts.unwrap_or_else(|| { |
| 628 |
fvar_defaults |
| 629 |
.iter() |
| 630 |
.map(|(tag, particle)| (tag.to_string(), particle.value)) |
| 631 |
.collect() |
| 632 |
}); |
| 633 |
|
| 634 |
let is_italic = style_name.contains("Italic"); |
| 635 |
let is_roman_and_italic = |
| 636 |
fvar_defaults.contains_key("ital") || fvar_defaults.contains_key("slnt"); |
| 637 |
|
| 638 |
let mut fallbacks: HashMap<String, Vec<FallbackProto>> = |
| 639 |
axis_registry.fallbacks(&axes).collect(); |
| 640 |
if !fvar_defaults.contains_key("wght") { |
| 641 |
fallbacks.insert( |
| 642 |
"wght".to_string(), |
| 643 |
axis_registry |
| 644 |
.get("wght") |
| 645 |
.unwrap() |
| 646 |
.fallback |
| 647 |
.iter() |
| 648 |
.filter(|f| f.value == Some(400.0)) |
| 649 |
.take(1) |
| 650 |
.cloned() |
| 651 |
.collect(), |
| 652 |
); |
| 653 |
} |
| 654 |
let wght_fallbacks = fallbacks.get("wght").ok_or("No wght fallbacks")?; |
| 655 |
let min_ital: Option<f32> = axes |
| 656 |
.iter() |
| 657 |
.filter(|axis| axis.tag == "ital") |
| 658 |
.map(|axis| axis.min) |
| 659 |
.next(); |
| 660 |
let min_slnt: Option<f32> = axes |
| 661 |
.iter() |
| 662 |
.filter(|axis| axis.tag == "slnt") |
| 663 |
.map(|axis| axis.min) |
| 664 |
.next(); |
| 665 |
|
| 666 |
let mut instances = vec![]; |
| 667 |
let do_italic = if is_roman_and_italic { |
| 668 |
vec![false, true] |
| 669 |
} else if is_italic { |
| 670 |
vec![true] |
| 671 |
} else { |
| 672 |
vec![false] |
| 673 |
}; |
| 674 |
for italic in do_italic.into_iter() { |
| 675 |
for fallback in wght_fallbacks.iter() { |
| 676 |
let mut name = fallback.name.as_ref().unwrap().to_string(); |
| 677 |
if italic { |
| 678 |
name += " Italic"; |
| 679 |
} |
| 680 |
name = name.replace("Regular Italic", "Italic"); |
| 681 |
let mut coordinates = axis_dflts.clone(); |
| 682 |
if fvar_defaults.contains_key("wght") { |
| 683 |
coordinates.insert("wght".to_string(), fallback.value.unwrap()); |
| 684 |
} |
| 685 |
if italic { |
| 686 |
if let Some(min) = min_ital { |
| 687 |
coordinates.insert("ital".to_string(), min); |
| 688 |
} else if let Some(min) = min_slnt { |
| 689 |
coordinates.insert("slnt".to_string(), min); |
| 690 |
} |
| 691 |
} |
| 692 |
let subfamily_name_id = add_name(&mut name_table.name_record, &name); |
| 693 |
let post_script_name_id = add_name( |
| 694 |
&mut name_table.name_record, |
| 695 |
&format!("{}-{}", family_name, name).replace(" ", ""), |
| 696 |
); |
| 697 |
let coordinates = axes |
| 698 |
.iter() |
| 699 |
.map(|axis| coordinates.get(&axis.tag).cloned().unwrap_or(axis.default)) |
| 700 |
.map(|val| Fixed::from_f64(val as f64)) |
| 701 |
.collect(); |
| 702 |
instances.push(InstanceRecord { |
| 703 |
subfamily_name_id, |
| 704 |
flags: 0, |
| 705 |
coordinates, |
| 706 |
post_script_name_id: Some(post_script_name_id), |
| 707 |
}) |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
fvar.axis_instance_arrays.instances = instances; |
| 712 |
|
| 713 |
new_font.add_table(&fvar)?; |
| 714 |
name_table.name_record.sort(); |
| 715 |
new_font.add_table(&name_table)?; |
| 716 |
Ok(new_font.copy_missing_tables(font).build()) |
| 717 |
} |
| 718 |
|
| 719 |
// All right, let's do it |
| 720 |
pub fn build_stat( |
| 721 |
font: FontRef, |
| 722 |
siblings: &[FontRef], |
| 723 |
) -> Result<Vec<u8>, Box<dyn std::error::Error>> { |
| 724 |
let mut new_font = FontBuilder::new(); |
| 725 |
let axes = font_axes(&font)?; |
| 726 |
let axis_registry = AxisRegistry::new(); |
| 727 |
let fallbacks_in_fvar: IndexMap<String, Vec<FallbackProto>> = |
| 728 |
axis_registry.fallbacks(&axes).collect(); |
| 729 |
|
| 730 |
let mut fallbacks_in_siblings: Vec<(String, FallbackProto)> = vec![]; |
| 731 |
for fnt in siblings { |
| 732 |
let family_name = best_familyname(fnt).unwrap_or("New Font".to_string()); |
| 733 |
let subfamily_name = best_subfamilyname(fnt).unwrap_or("Regular".to_string()); |
| 734 |
let font_axes = font_axes(fnt).unwrap_or_default(); |
| 735 |
fallbacks_in_siblings.extend( |
| 736 |
axis_registry |
| 737 |
.name_table_fallbacks(&family_name, &subfamily_name, &font_axes) |
| 738 |
.map(|(tag, proto)| (tag.to_string(), proto.clone())), |
| 739 |
) |
| 740 |
} |
| 741 |
// And for this font |
| 742 |
let family_name = best_familyname(&font).unwrap_or("New Font".to_string()); |
| 743 |
let subfamily_name = best_subfamilyname(&font).unwrap_or("Regular".to_string()); |
| 744 |
let fallbacks_in_names = |
| 745 |
axis_registry.name_table_fallbacks(&family_name, &subfamily_name, &axes); |
| 746 |
|
| 747 |
let fvar: Fvar = font.fvar().unwrap().to_owned_table(); |
| 748 |
let mut name: Name = font.name().unwrap().to_owned_table(); |
| 749 |
let fvar_name_ids: HashSet<StringId> = fvar |
| 750 |
.axis_instance_arrays |
| 751 |
.instances |
| 752 |
.iter() |
| 753 |
.map(|x| x.subfamily_name_id) |
| 754 |
.chain( |
| 755 |
fvar.axis_instance_arrays |
| 756 |
.axes |
| 757 |
.iter() |
| 758 |
.map(|x| x.axis_name_id), |
| 759 |
) |
| 760 |
.collect(); |
| 761 |
let keep = |name_id: StringId| -> bool { |
| 762 |
name_id.to_u16() <= 25 || fvar_name_ids.contains(&name_id) |
| 763 |
}; |
| 764 |
let mut delete_ids = vec![]; |
| 765 |
if let Ok(stat) = font.stat() { |
| 766 |
for axis in stat.design_axes()?.iter() { |
| 767 |
let id = axis.axis_name_id.get(); |
| 768 |
if !keep(id) { |
| 769 |
delete_ids.push(id); |
| 770 |
} |
| 771 |
} |
| 772 |
if let Some(axis_values) = stat.offset_to_axis_values().transpose()? { |
| 773 |
for axis_value in axis_values.axis_values().iter().flatten() { |
| 774 |
let id = axis_value.value_name_id(); |
| 775 |
if !keep(id) { |
| 776 |
delete_ids.push(id); |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
name.name_record |
| 782 |
.retain(|record| !delete_ids.contains(&record.name_id)); |
| 783 |
let mut axis_records: Vec<AxisRecord> = vec![]; |
| 784 |
let mut values: Vec<AxisValue> = vec![]; |
| 785 |
let mut seen_axes = HashSet::new(); |
| 786 |
|
| 787 |
fn make_location(axis: Tag, value: f32, linked_value: Option<f32>) -> AxisLocation { |
| 788 |
if let Some(linked_value) = linked_value { |
| 789 |
AxisLocation::Three { |
| 790 |
tag: axis, |
| 791 |
value: Fixed::from_f64(value as f64), |
| 792 |
linked: Fixed::from_f64(linked_value as f64), |
| 793 |
} |
| 794 |
} else { |
| 795 |
AxisLocation::One { |
| 796 |
tag: axis, |
| 797 |
value: Fixed::from_f64(value as f64), |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
for (axis, fallbacks) in fallbacks_in_fvar.iter() { |
| 803 |
let tag = Tag::new_checked(&axis.as_bytes()[0..4])?; |
| 804 |
let ar_axis = axis_registry.get(axis).unwrap(); |
| 805 |
seen_axes.insert(tag); |
| 806 |
axis_records.push(AxisRecord { |
| 807 |
tag, |
| 808 |
name: ar_axis.display_name().to_string(), |
| 809 |
ordering: 0, |
| 810 |
}); |
| 811 |
let fallback_values = fallbacks.iter().map(|f| f.value()).collect::<Vec<f32>>(); |
| 812 |
for fallback in fallbacks.iter() { |
| 813 |
values.push(AxisValue { |
| 814 |
flags: if fallback.value() == ar_axis.default_value() { |
| 815 |
0x2 |
| 816 |
} else { |
| 817 |
0x0 |
| 818 |
}, |
| 819 |
name: fallback.name().to_string(), |
| 820 |
location: make_location( |
| 821 |
tag, |
| 822 |
fallback.value(), |
| 823 |
linked_value(axis, fallback.value()) |
| 824 |
.filter(|value| fallback_values.contains(value)), |
| 825 |
), |
| 826 |
}) |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
for (axis, fallback) in fallbacks_in_names { |
| 831 |
let tag = Tag::new_checked(&axis.as_bytes()[0..4])?; |
| 832 |
if seen_axes.contains(&tag) { |
| 833 |
continue; |
| 834 |
} |
| 835 |
// println!("Adding {} in names", axis); |
| 836 |
seen_axes.insert(tag); |
| 837 |
|
| 838 |
let ar_axis = axis_registry.get(axis).unwrap(); |
| 839 |
axis_records.push(AxisRecord { |
| 840 |
tag, |
| 841 |
name: ar_axis.display_name().to_string(), |
| 842 |
ordering: 0, |
| 843 |
}); |
| 844 |
values.push(AxisValue { |
| 845 |
flags: 0x0, |
| 846 |
name: fallback.name().to_string(), |
| 847 |
location: make_location( |
| 848 |
tag, |
| 849 |
fallback.value(), |
| 850 |
linked_value(axis, fallback.value()), |
| 851 |
), |
| 852 |
}); |
| 853 |
} |
| 854 |
|
| 855 |
for (axis, _fallback) in fallbacks_in_siblings { |
| 856 |
let tag = Tag::new_checked(&axis.as_bytes()[0..4])?; |
| 857 |
if seen_axes.contains(&tag) { |
| 858 |
continue; |
| 859 |
} |
| 860 |
seen_axes.insert(tag); |
| 861 |
|
| 862 |
// println!("Adding {} in siblings", axis); |
| 863 |
let ar_axis = axis_registry.get(&axis).unwrap(); |
| 864 |
let elided_value = ar_axis.default_value(); |
| 865 |
axis_records.push(AxisRecord { |
| 866 |
tag, |
| 867 |
name: ar_axis.display_name().to_string(), |
| 868 |
ordering: 0, |
| 869 |
}); |
| 870 |
if let Some(elided_fallback) = axis_registry.fallback_for_value(&axis, elided_value) { |
| 871 |
values.push(AxisValue { |
| 872 |
flags: 0x2, |
| 873 |
name: elided_fallback.name().to_string(), |
| 874 |
location: make_location(tag, elided_value, linked_value(&axis, elided_value)), |
| 875 |
}) |
| 876 |
} |
| 877 |
} |
| 878 |
axis_records.iter_mut().enumerate().for_each(|(i, record)| { |
| 879 |
record.ordering = i as u16; |
| 880 |
}); |
| 881 |
|
| 882 |
let stat_builder = StatBuilder { |
| 883 |
records: axis_records, |
| 884 |
values, |
| 885 |
}; |
| 886 |
let stat = stat_builder.build(&mut name.name_record); |
| 887 |
name.name_record.sort(); |
| 888 |
new_font.add_table(&name)?; |
| 889 |
new_font.add_table(&stat)?; |
| 890 |
Ok(new_font.copy_missing_tables(font).build()) |
| 891 |
} |
| 892 |
|
| 893 |
pub fn build_filename(font: FontRef, extension: &str) -> String { |
| 894 |
let family_name = best_familyname(&font) |
| 895 |
.unwrap_or("New Font".to_string()) |
| 896 |
.replace(" ", ""); |
| 897 |
let style_name = best_subfamilyname(&font).unwrap_or("Regular".to_string()); |
| 898 |
if font.table_data(Tag::new(b"fvar")).is_some() { |
| 899 |
let is_italic = style_name.contains("Italic"); |
| 900 |
let axes = font_axes(&font).unwrap_or_default(); |
| 901 |
// Sort uppercase axes first, then lowercase axes |
| 902 |
let mut axes = axes |
| 903 |
.iter() |
| 904 |
.map(|axis| axis.tag.to_string()) |
| 905 |
.collect::<Vec<_>>(); |
| 906 |
axes.sort(); |
| 907 |
let axes = axes.join(","); |
| 908 |
return format!( |
| 909 |
"{}{}[{}].{}", |
| 910 |
family_name, |
| 911 |
if is_italic { "-Italic" } else { "" }, |
| 912 |
axes, |
| 913 |
extension |
| 914 |
); |
| 915 |
} |
| 916 |
format!("{family_name}-{style_name}.{extension}").replace(" ", "") |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
#[cfg(feature = "fontations")] |
| 921 |
pub use fontations_impl::*; |
| 922 |
|
| 923 |
#[cfg(test)] |
| 924 |
mod tests { |
| 925 |
use fontations::{ |
| 926 |
skrifa::{string::StringId, MetadataProvider, Tag}, |
| 927 |
write::{ |
| 928 |
from_obj::ToOwnedTable, |
| 929 |
tables::{ |
| 930 |
name::Name, |
| 931 |
stat::{AxisValue, Stat}, |
| 932 |
}, |
| 933 |
}, |
| 934 |
}; |
| 935 |
use pretty_assertions::assert_eq; |
| 936 |
|
| 937 |
use super::*; |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn opsz() { |
| 941 |
let ar = AxisRegistry::new(); |
| 942 |
assert!(ar.contains_key("opsz")); |
| 943 |
assert_eq!(ar["opsz"].display_name.as_deref(), Some("Optical Size")); |
| 944 |
} |
| 945 |
const MAVEN_PRO: &[u8; 83576] = include_bytes!("../tests/data/MavenPro-Regular.ttf"); |
| 946 |
const OPEN_SANS: &[u8; 532636] = include_bytes!("../tests/data/OpenSans[wdth,wght].ttf"); |
| 947 |
const OPEN_SANS_ITALIC: &[u8; 584112] = |
| 948 |
include_bytes!("../tests/data/OpenSans-Italic[wdth,wght].ttf"); |
| 949 |
const OPEN_SANS_CONDENSED: &[u8; 973780] = |
| 950 |
include_bytes!("../tests/data/OpenSansCondensed[wght].ttf"); |
| 951 |
const OPEN_SANS_CONDENSED_ITALIC: &[u8; 1054652] = |
| 952 |
include_bytes!("../tests/data/OpenSansCondensed-Italic[wght].ttf"); |
| 953 |
const WONKY: &[u8; 532564] = include_bytes!("../tests/data/Wonky[wdth,wght].ttf"); |
| 954 |
const PLAYFAIR: &[u8; 1150824] = include_bytes!("../tests/data/Playfair[opsz,wdth,wght].ttf"); |
| 955 |
|
| 956 |
struct NameTableTestCase { |
| 957 |
binary: &'static [u8], |
| 958 |
family_name: &'static str, |
| 959 |
subfamily_name: Option<&'static str>, |
| 960 |
siblings: Vec<&'static [u8]>, |
| 961 |
expectations: Vec<(StringId, Option<&'static str>)>, |
| 962 |
} |
| 963 |
|
| 964 |
#[test] |
| 965 |
fn test_name_table() { |
| 966 |
let cases: Vec<NameTableTestCase> = vec![ |
| 967 |
NameTableTestCase { |
| 968 |
binary: MAVEN_PRO, |
| 969 |
family_name: "Maven Pro", |
| 970 |
subfamily_name: Some("Regular"), |
| 971 |
siblings: vec![], |
| 972 |
expectations: vec![ |
| 973 |
(StringId::FAMILY_NAME, Some("Maven Pro")), |
| 974 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 975 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-Regular")), |
| 976 |
(StringId::FULL_NAME, Some("Maven Pro Regular")), |
| 977 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-Regular")), |
| 978 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 979 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 980 |
], |
| 981 |
}, |
| 982 |
NameTableTestCase { |
| 983 |
binary: MAVEN_PRO, |
| 984 |
family_name: "Maven Pro", |
| 985 |
subfamily_name: Some("Italic"), |
| 986 |
siblings: vec![], |
| 987 |
expectations: vec![ |
| 988 |
(StringId::FAMILY_NAME, Some("Maven Pro")), |
| 989 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 990 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-Italic")), |
| 991 |
(StringId::FULL_NAME, Some("Maven Pro Italic")), |
| 992 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-Italic")), |
| 993 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 994 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 995 |
], |
| 996 |
}, |
| 997 |
NameTableTestCase { |
| 998 |
binary: MAVEN_PRO, |
| 999 |
family_name: "Maven Pro", |
| 1000 |
subfamily_name: Some("Bold"), |
| 1001 |
siblings: vec![], |
| 1002 |
expectations: vec![ |
| 1003 |
(StringId::FAMILY_NAME, Some("Maven Pro")), |
| 1004 |
(StringId::SUBFAMILY_NAME, Some("Bold")), |
| 1005 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-Bold")), |
| 1006 |
(StringId::FULL_NAME, Some("Maven Pro Bold")), |
| 1007 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-Bold")), |
| 1008 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1009 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1010 |
], |
| 1011 |
}, |
| 1012 |
NameTableTestCase { |
| 1013 |
binary: MAVEN_PRO, |
| 1014 |
family_name: "Maven Pro", |
| 1015 |
subfamily_name: Some("Bold Italic"), |
| 1016 |
siblings: vec![], |
| 1017 |
expectations: vec![ |
| 1018 |
(StringId::FAMILY_NAME, Some("Maven Pro")), |
| 1019 |
(StringId::SUBFAMILY_NAME, Some("Bold Italic")), |
| 1020 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-BoldItalic")), |
| 1021 |
(StringId::FULL_NAME, Some("Maven Pro Bold Italic")), |
| 1022 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-BoldItalic")), |
| 1023 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1024 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1025 |
], |
| 1026 |
}, |
| 1027 |
NameTableTestCase { |
| 1028 |
binary: MAVEN_PRO, |
| 1029 |
family_name: "Maven Pro", |
| 1030 |
subfamily_name: Some("Black"), |
| 1031 |
siblings: vec![], |
| 1032 |
expectations: vec![ |
| 1033 |
(StringId::FAMILY_NAME, Some("Maven Pro Black")), |
| 1034 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1035 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-Black")), |
| 1036 |
(StringId::FULL_NAME, Some("Maven Pro Black")), |
| 1037 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-Black")), |
| 1038 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, Some("Maven Pro")), |
| 1039 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, Some("Black")), |
| 1040 |
], |
| 1041 |
}, |
| 1042 |
NameTableTestCase { |
| 1043 |
binary: MAVEN_PRO, |
| 1044 |
family_name: "Maven Pro", |
| 1045 |
subfamily_name: Some("Black Italic"), |
| 1046 |
siblings: vec![], |
| 1047 |
expectations: vec![ |
| 1048 |
(StringId::FAMILY_NAME, Some("Maven Pro Black")), |
| 1049 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 1050 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenPro-BlackItalic")), |
| 1051 |
(StringId::FULL_NAME, Some("Maven Pro Black Italic")), |
| 1052 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-BlackItalic")), |
| 1053 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, Some("Maven Pro")), |
| 1054 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, Some("Black Italic")), |
| 1055 |
], |
| 1056 |
}, |
| 1057 |
NameTableTestCase { |
| 1058 |
binary: MAVEN_PRO, |
| 1059 |
family_name: "Maven Pro", |
| 1060 |
subfamily_name: Some("ExtraLight Italic"), |
| 1061 |
siblings: vec![], |
| 1062 |
expectations: vec![ |
| 1063 |
(StringId::FAMILY_NAME, Some("Maven Pro ExtraLight")), |
| 1064 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 1065 |
( |
| 1066 |
StringId::UNIQUE_ID, |
| 1067 |
Some("2.003;NONE;MavenPro-ExtraLightItalic"), |
| 1068 |
), |
| 1069 |
(StringId::FULL_NAME, Some("Maven Pro ExtraLight Italic")), |
| 1070 |
(StringId::POSTSCRIPT_NAME, Some("MavenPro-ExtraLightItalic")), |
| 1071 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, Some("Maven Pro")), |
| 1072 |
( |
| 1073 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 1074 |
Some("ExtraLight Italic"), |
| 1075 |
), |
| 1076 |
], |
| 1077 |
}, |
| 1078 |
NameTableTestCase { |
| 1079 |
binary: MAVEN_PRO, |
| 1080 |
family_name: "Maven Pro", |
| 1081 |
subfamily_name: Some("UltraExpanded Regular"), |
| 1082 |
siblings: vec![], |
| 1083 |
expectations: vec![ |
| 1084 |
(StringId::FAMILY_NAME, Some("Maven Pro UltraExpanded")), |
| 1085 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1086 |
( |
| 1087 |
StringId::UNIQUE_ID, |
| 1088 |
Some("2.003;NONE;MavenProUltraExpanded-Regular"), |
| 1089 |
), |
| 1090 |
(StringId::FULL_NAME, Some("Maven Pro UltraExpanded Regular")), |
| 1091 |
( |
| 1092 |
StringId::POSTSCRIPT_NAME, |
| 1093 |
Some("MavenProUltraExpanded-Regular"), |
| 1094 |
), |
| 1095 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1096 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1097 |
], |
| 1098 |
}, |
| 1099 |
NameTableTestCase { |
| 1100 |
binary: MAVEN_PRO, |
| 1101 |
family_name: "Maven Pro", |
| 1102 |
subfamily_name: Some("Condensed ExtraLight Italic"), |
| 1103 |
siblings: vec![], |
| 1104 |
expectations: vec![ |
| 1105 |
( |
| 1106 |
StringId::FAMILY_NAME, |
| 1107 |
Some("Maven Pro Condensed ExtraLight"), |
| 1108 |
), |
| 1109 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 1110 |
( |
| 1111 |
StringId::UNIQUE_ID, |
| 1112 |
Some("2.003;NONE;MavenProCondensed-ExtraLightItalic"), |
| 1113 |
), |
| 1114 |
( |
| 1115 |
StringId::FULL_NAME, |
| 1116 |
Some("Maven Pro Condensed ExtraLight Italic"), |
| 1117 |
), |
| 1118 |
( |
| 1119 |
StringId::POSTSCRIPT_NAME, |
| 1120 |
Some("MavenProCondensed-ExtraLightItalic"), |
| 1121 |
), |
| 1122 |
( |
| 1123 |
StringId::TYPOGRAPHIC_FAMILY_NAME, |
| 1124 |
Some("Maven Pro Condensed"), |
| 1125 |
), |
| 1126 |
( |
| 1127 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 1128 |
Some("ExtraLight Italic"), |
| 1129 |
), |
| 1130 |
], |
| 1131 |
}, |
| 1132 |
NameTableTestCase { |
| 1133 |
binary: OPEN_SANS, |
| 1134 |
family_name: "Open Sans", |
| 1135 |
subfamily_name: None, |
| 1136 |
siblings: vec![ |
| 1137 |
OPEN_SANS_ITALIC, |
| 1138 |
OPEN_SANS_CONDENSED, |
| 1139 |
OPEN_SANS_CONDENSED_ITALIC, |
| 1140 |
], |
| 1141 |
expectations: vec![ |
| 1142 |
(StringId::FAMILY_NAME, Some("Open Sans")), |
| 1143 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1144 |
(StringId::UNIQUE_ID, Some("3.000;GOOG;OpenSans-Regular")), |
| 1145 |
(StringId::FULL_NAME, Some("Open Sans Regular")), |
| 1146 |
(StringId::POSTSCRIPT_NAME, Some("OpenSans-Regular")), |
| 1147 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1148 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1149 |
( |
| 1150 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 1151 |
Some("OpenSans"), |
| 1152 |
), |
| 1153 |
], |
| 1154 |
}, |
| 1155 |
NameTableTestCase { |
| 1156 |
binary: OPEN_SANS_ITALIC, |
| 1157 |
family_name: "Open Sans", |
| 1158 |
subfamily_name: None, |
| 1159 |
siblings: vec![OPEN_SANS, OPEN_SANS_CONDENSED, OPEN_SANS_CONDENSED_ITALIC], |
| 1160 |
expectations: vec![ |
| 1161 |
(StringId::FAMILY_NAME, Some("Open Sans")), |
| 1162 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 1163 |
(StringId::UNIQUE_ID, Some("3.000;GOOG;OpenSans-Italic")), |
| 1164 |
(StringId::FULL_NAME, Some("Open Sans Italic")), |
| 1165 |
(StringId::POSTSCRIPT_NAME, Some("OpenSans-Italic")), |
| 1166 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1167 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1168 |
( |
| 1169 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 1170 |
Some("OpenSansItalic"), |
| 1171 |
), |
| 1172 |
], |
| 1173 |
}, |
| 1174 |
NameTableTestCase { |
| 1175 |
binary: OPEN_SANS_CONDENSED, |
| 1176 |
family_name: "Open Sans Condensed", |
| 1177 |
subfamily_name: None, |
| 1178 |
siblings: vec![OPEN_SANS, OPEN_SANS_ITALIC, OPEN_SANS_CONDENSED_ITALIC], |
| 1179 |
expectations: vec![ |
| 1180 |
(StringId::FAMILY_NAME, Some("Open Sans Condensed")), |
| 1181 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1182 |
( |
| 1183 |
StringId::UNIQUE_ID, |
| 1184 |
Some("3.000;GOOG;OpenSansCondensed-Regular"), |
| 1185 |
), |
| 1186 |
(StringId::FULL_NAME, Some("Open Sans Condensed Regular")), |
| 1187 |
(StringId::POSTSCRIPT_NAME, Some("OpenSansCondensed-Regular")), |
| 1188 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1189 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1190 |
( |
| 1191 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 1192 |
Some("OpenSansCondensed"), |
| 1193 |
), |
| 1194 |
], |
| 1195 |
}, |
| 1196 |
NameTableTestCase { |
| 1197 |
binary: OPEN_SANS_CONDENSED_ITALIC, |
| 1198 |
family_name: "Open Sans Condensed", |
| 1199 |
subfamily_name: None, |
| 1200 |
siblings: vec![OPEN_SANS, OPEN_SANS_ITALIC, OPEN_SANS_CONDENSED], |
| 1201 |
expectations: vec![ |
| 1202 |
(StringId::FAMILY_NAME, Some("Open Sans Condensed")), |
| 1203 |
(StringId::SUBFAMILY_NAME, Some("Italic")), |
| 1204 |
( |
| 1205 |
StringId::UNIQUE_ID, |
| 1206 |
Some("3.000;GOOG;OpenSansCondensed-Italic"), |
| 1207 |
), |
| 1208 |
(StringId::FULL_NAME, Some("Open Sans Condensed Italic")), |
| 1209 |
(StringId::POSTSCRIPT_NAME, Some("OpenSansCondensed-Italic")), |
| 1210 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1211 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1212 |
( |
| 1213 |
StringId::VARIATIONS_POSTSCRIPT_NAME_PREFIX, |
| 1214 |
Some("OpenSansCondensedItalic"), |
| 1215 |
), |
| 1216 |
], |
| 1217 |
}, |
| 1218 |
// Bad names |
| 1219 |
NameTableTestCase { |
| 1220 |
binary: MAVEN_PRO, |
| 1221 |
family_name: "Maven Pro", |
| 1222 |
subfamily_name: Some("Fat"), |
| 1223 |
siblings: vec![], |
| 1224 |
expectations: vec![ |
| 1225 |
(StringId::FAMILY_NAME, Some("Maven Pro Fat")), |
| 1226 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1227 |
(StringId::UNIQUE_ID, Some("2.003;NONE;MavenProFat-Regular")), |
| 1228 |
(StringId::FULL_NAME, Some("Maven Pro Fat Regular")), |
| 1229 |
(StringId::POSTSCRIPT_NAME, Some("MavenProFat-Regular")), |
| 1230 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1231 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1232 |
], |
| 1233 |
}, |
| 1234 |
NameTableTestCase { |
| 1235 |
binary: WONKY, |
| 1236 |
family_name: "Wonky", |
| 1237 |
subfamily_name: None, |
| 1238 |
siblings: vec![], |
| 1239 |
expectations: vec![ |
| 1240 |
(StringId::FAMILY_NAME, Some("Wonky")), |
| 1241 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1242 |
(StringId::UNIQUE_ID, Some("3.000;GOOG;Wonky-Regular")), |
| 1243 |
(StringId::FULL_NAME, Some("Wonky Regular")), |
| 1244 |
(StringId::POSTSCRIPT_NAME, Some("Wonky-Regular")), |
| 1245 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, None), |
| 1246 |
(StringId::TYPOGRAPHIC_SUBFAMILY_NAME, None), |
| 1247 |
], |
| 1248 |
}, |
| 1249 |
NameTableTestCase { |
| 1250 |
binary: PLAYFAIR, |
| 1251 |
family_name: "Playfair", |
| 1252 |
subfamily_name: None, |
| 1253 |
siblings: vec![], |
| 1254 |
expectations: vec![ |
| 1255 |
(StringId::FAMILY_NAME, Some("Playfair SemiExpanded Light")), |
| 1256 |
(StringId::SUBFAMILY_NAME, Some("Regular")), |
| 1257 |
( |
| 1258 |
StringId::UNIQUE_ID, |
| 1259 |
Some("2.000;FTH;Playfair-SemiExpandedLight"), |
| 1260 |
), |
| 1261 |
(StringId::FULL_NAME, Some("Playfair SemiExpanded Light")), |
| 1262 |
( |
| 1263 |
StringId::POSTSCRIPT_NAME, |
| 1264 |
Some("Playfair-SemiExpandedLight"), |
| 1265 |
), |
| 1266 |
(StringId::TYPOGRAPHIC_FAMILY_NAME, Some("Playfair")), |
| 1267 |
( |
| 1268 |
StringId::TYPOGRAPHIC_SUBFAMILY_NAME, |
| 1269 |
Some("SemiExpanded Light"), |
| 1270 |
), |
| 1271 |
], |
| 1272 |
}, |
| 1273 |
]; |
| 1274 |
|
| 1275 |
run_name_table_tests(&cases, None); |
| 1276 |
} |
| 1277 |
|
| 1278 |
fn run_name_table_tests(cases: &[NameTableTestCase], aggression: Option<RenameAggressiveness>) { |
| 1279 |
for (ix, case) in cases.iter().enumerate() { |
| 1280 |
let font = FontRef::new(case.binary).expect("Failed to read font"); |
| 1281 |
let siblings: Vec<FontRef> = case |
| 1282 |
.siblings |
| 1283 |
.iter() |
| 1284 |
.map(|b| FontRef::new(b).unwrap()) |
| 1285 |
.collect(); |
| 1286 |
let result = build_name_table( |
| 1287 |
font, |
| 1288 |
Some(case.family_name), |
| 1289 |
case.subfamily_name, |
| 1290 |
&siblings, |
| 1291 |
aggression, |
| 1292 |
) |
| 1293 |
.unwrap(); |
| 1294 |
let result_font = FontRef::new(&result).unwrap(); |
| 1295 |
for (id, expectation) in case.expectations.iter() { |
| 1296 |
let record = result_font.localized_strings(*id).english_or_first(); |
| 1297 |
if let Some(expectation) = expectation { |
| 1298 |
assert_eq!( |
| 1299 |
record.unwrap().chars().collect::<String>(), |
| 1300 |
*expectation, |
| 1301 |
"Case {}, {}", |
| 1302 |
ix + 1, |
| 1303 |
id |
| 1304 |
); |
| 1305 |
} else { |
| 1306 |
assert!(record.is_none()); |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
#[test] |
| 1313 |
fn test_name_table_aggression() { |
| 1314 |
let cases = [ |
| 1315 |
NameTableTestCase { |
| 1316 |
binary: MAVEN_PRO, |
| 1317 |
family_name: "Raven Am", |
| 1318 |
subfamily_name: Some("Regular"), |
| 1319 |
siblings: vec![], |
| 1320 |
expectations: vec![ |
| 1321 |
(StringId::COPYRIGHT_NOTICE, Some("Copyright 2011 The Raven Am Project Authors (http://www.vissol.co.uk/mavenpro/), with Reserved Font Name \"Raven Am\".")), |
| 1322 |
(StringId::FULL_NAME, Some("Raven Am Regular")), |
| 1323 |
] |
| 1324 |
} |
| 1325 |
]; |
| 1326 |
run_name_table_tests(&cases, Some(RenameAggressiveness::Aggressive)); |
| 1327 |
let cases = [ |
| 1328 |
NameTableTestCase { |
| 1329 |
binary: MAVEN_PRO, |
| 1330 |
family_name: "Raven Am", |
| 1331 |
subfamily_name: Some("Regular"), |
| 1332 |
siblings: vec![], |
| 1333 |
expectations: vec![ |
| 1334 |
(StringId::COPYRIGHT_NOTICE, Some("Copyright 2011 The Maven Pro Project Authors (http://www.vissol.co.uk/mavenpro/), with Reserved Font Name \"Maven Pro\".")), |
| 1335 |
(StringId::FULL_NAME, Some("Raven Am Regular")), |
| 1336 |
] |
| 1337 |
} |
| 1338 |
]; |
| 1339 |
run_name_table_tests(&cases, Some(RenameAggressiveness::Conservative)); |
| 1340 |
} |
| 1341 |
|
| 1342 |
#[derive(Debug, Clone, PartialEq)] |
| 1343 |
struct DumpStatValue<'a> { |
| 1344 |
axis: Tag, |
| 1345 |
name: &'a str, |
| 1346 |
value: f32, |
| 1347 |
linked: Option<f32>, |
| 1348 |
} |
| 1349 |
|
| 1350 |
fn dump_stat_values<'a>(stat: &Stat, name: &'a Name) -> Vec<DumpStatValue<'a>> { |
| 1351 |
let mut values = vec![]; |
| 1352 |
let tags = stat |
| 1353 |
.design_axes |
| 1354 |
.iter() |
| 1355 |
.map(|a| a.axis_tag) |
| 1356 |
.collect::<Vec<_>>(); |
| 1357 |
if let Some(axis_values) = stat.offset_to_axis_values.as_ref() { |
| 1358 |
for axis_value in axis_values.iter() { |
| 1359 |
match axis_value.as_ref() { |
| 1360 |
AxisValue::Format1(v) => values.push(DumpStatValue { |
| 1361 |
axis: tags[v.axis_index as usize], |
| 1362 |
name: name |
| 1363 |
.name_record |
| 1364 |
.iter() |
| 1365 |
.find(|record| record.name_id == v.value_name_id) |
| 1366 |
.map(|record| record.string.as_str()) |
| 1367 |
.unwrap_or(""), |
| 1368 |
value: v.value.to_f32(), |
| 1369 |
linked: None, |
| 1370 |
}), |
| 1371 |
AxisValue::Format2(_) => { |
| 1372 |
panic!("I didn't produce this") |
| 1373 |
} |
| 1374 |
AxisValue::Format3(v) => values.push(DumpStatValue { |
| 1375 |
axis: tags[v.axis_index as usize], |
| 1376 |
name: name |
| 1377 |
.name_record |
| 1378 |
.iter() |
| 1379 |
.find(|record| record.name_id == v.value_name_id) |
| 1380 |
.map(|record| record.string.as_str()) |
| 1381 |
.unwrap_or(""), |
| 1382 |
value: v.value.to_f32(), |
| 1383 |
linked: Some(v.linked_value.to_f32()), |
| 1384 |
}), |
| 1385 |
AxisValue::Format4(_) => { |
| 1386 |
panic!("I didn't produce this") |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 1391 |
values |
| 1392 |
} |
| 1393 |
|
| 1394 |
fn value<'a>(axis: &str, name: &'a str, value: f32, linked: Option<f32>) -> DumpStatValue<'a> { |
| 1395 |
DumpStatValue { |
| 1396 |
axis: Tag::new_checked(axis.as_bytes()).unwrap(), |
| 1397 |
name, |
| 1398 |
value, |
| 1399 |
linked, |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
#[test] |
| 1404 |
fn test_build_stat() { |
| 1405 |
let font_data = build_stat( |
| 1406 |
FontRef::new(OPEN_SANS).unwrap(), |
| 1407 |
&[ |
| 1408 |
FontRef::new(OPEN_SANS_ITALIC).unwrap(), |
| 1409 |
FontRef::new(OPEN_SANS_CONDENSED).unwrap(), |
| 1410 |
FontRef::new(OPEN_SANS_CONDENSED_ITALIC).unwrap(), |
| 1411 |
], |
| 1412 |
) |
| 1413 |
.unwrap(); |
| 1414 |
let new_font = FontRef::new(&font_data).unwrap(); |
| 1415 |
let new_stat: Stat = new_font.stat().unwrap().to_owned_table(); |
| 1416 |
let name: Name = new_font.name().unwrap().to_owned_table(); |
| 1417 |
// We expect three axes, wght, wdth, and ital |
| 1418 |
assert_eq!( |
| 1419 |
new_stat |
| 1420 |
.design_axes |
| 1421 |
.iter() |
| 1422 |
.map(|a| a.axis_tag) |
| 1423 |
.collect::<Vec<_>>(), |
| 1424 |
vec![Tag::new(b"wght"), Tag::new(b"wdth"), Tag::new(b"ital")] |
| 1425 |
); |
| 1426 |
assert_eq!( |
| 1427 |
dump_stat_values(&new_stat, &name), |
| 1428 |
vec![ |
| 1429 |
value("wght", "Light", 300.0, None), |
| 1430 |
value("wght", "Regular", 400.0, Some(700.0)), |
| 1431 |
value("wght", "Medium", 500.0, None), |
| 1432 |
value("wght", "SemiBold", 600.0, None), |
| 1433 |
value("wght", "Bold", 700.0, None), |
| 1434 |
value("wght", "ExtraBold", 800.0, None), |
| 1435 |
value("wdth", "Condensed", 75.0, None), |
| 1436 |
value("wdth", "SemiCondensed", 87.5, None), |
| 1437 |
value("wdth", "Normal", 100.0, None), |
| 1438 |
value("ital", "Roman", 0.0, Some(1.0)), |
| 1439 |
] |
| 1440 |
) |
| 1441 |
} |
| 1442 |
|
| 1443 |
#[test] |
| 1444 |
fn test_build_stat2() { |
| 1445 |
let font_data = build_stat( |
| 1446 |
FontRef::new(OPEN_SANS_ITALIC).unwrap(), |
| 1447 |
&[ |
| 1448 |
FontRef::new(OPEN_SANS).unwrap(), |
| 1449 |
FontRef::new(OPEN_SANS_CONDENSED).unwrap(), |
| 1450 |
FontRef::new(OPEN_SANS_CONDENSED_ITALIC).unwrap(), |
| 1451 |
], |
| 1452 |
) |
| 1453 |
.unwrap(); |
| 1454 |
let new_font = FontRef::new(&font_data).unwrap(); |
| 1455 |
let new_stat: Stat = new_font.stat().unwrap().to_owned_table(); |
| 1456 |
let name: Name = new_font.name().unwrap().to_owned_table(); |
| 1457 |
// We expect three axes, wght, wdth, and ital |
| 1458 |
assert_eq!( |
| 1459 |
new_stat |
| 1460 |
.design_axes |
| 1461 |
.iter() |
| 1462 |
.map(|a| a.axis_tag) |
| 1463 |
.collect::<Vec<_>>(), |
| 1464 |
vec![Tag::new(b"wght"), Tag::new(b"wdth"), Tag::new(b"ital")] |
| 1465 |
); |
| 1466 |
assert_eq!( |
| 1467 |
dump_stat_values(&new_stat, &name), |
| 1468 |
vec![ |
| 1469 |
value("wght", "Light", 300.0, None), |
| 1470 |
value("wght", "Regular", 400.0, Some(700.0)), |
| 1471 |
value("wght", "Medium", 500.0, None), |
| 1472 |
value("wght", "SemiBold", 600.0, None), |
| 1473 |
value("wght", "Bold", 700.0, None), |
| 1474 |
value("wght", "ExtraBold", 800.0, None), |
| 1475 |
value("wdth", "Condensed", 75.0, None), |
| 1476 |
value("wdth", "SemiCondensed", 87.5, None), |
| 1477 |
value("wdth", "Normal", 100.0, None), |
| 1478 |
value("ital", "Italic", 1.0, None), |
| 1479 |
] |
| 1480 |
) |
| 1481 |
} |
| 1482 |
|
| 1483 |
#[test] |
| 1484 |
fn test_build_filename() { |
| 1485 |
let maven_pro = FontRef::new(MAVEN_PRO).unwrap(); |
| 1486 |
assert_eq!(build_filename(maven_pro, "ttf"), "MavenPro-Regular.ttf"); |
| 1487 |
let open_sans = FontRef::new(OPEN_SANS).unwrap(); |
| 1488 |
assert_eq!(build_filename(open_sans, "ttf"), "OpenSans[wdth,wght].ttf"); |
| 1489 |
let open_sans_italic = FontRef::new(OPEN_SANS_ITALIC).unwrap(); |
| 1490 |
assert_eq!( |
| 1491 |
build_filename(open_sans_italic, "ttf"), |
| 1492 |
"OpenSans-Italic[wdth,wght].ttf" |
| 1493 |
); |
| 1494 |
let open_sans_condensed = FontRef::new(OPEN_SANS_CONDENSED).unwrap(); |
| 1495 |
assert_eq!( |
| 1496 |
build_filename(open_sans_condensed, "ttf"), |
| 1497 |
"OpenSansCondensed[wght].ttf" |
| 1498 |
); |
| 1499 |
let open_sans_condensed_italic = FontRef::new(OPEN_SANS_CONDENSED_ITALIC).unwrap(); |
| 1500 |
assert_eq!( |
| 1501 |
build_filename(open_sans_condensed_italic, "ttf"), |
| 1502 |
"OpenSansCondensed-Italic[wght].ttf" |
| 1503 |
); |
| 1504 |
let wonky = FontRef::new(WONKY).unwrap(); |
| 1505 |
assert_eq!(build_filename(wonky, "ttf"), "Wonky[wdth,wght].ttf"); |
| 1506 |
let playfair = FontRef::new(PLAYFAIR).unwrap(); |
| 1507 |
assert_eq!( |
| 1508 |
build_filename(playfair, "ttf"), |
| 1509 |
"Playfair[opsz,wdth,wght].ttf" |
| 1510 |
); |
| 1511 |
} |
| 1512 |
} |