| 1 |
/* |
| 2 |
* fontconfig/fc-fontations/attributes.rs |
| 3 |
* |
| 4 |
* Copyright 2025 Google LLC. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 7 |
* documentation for any purpose is hereby granted without fee, provided that |
| 8 |
* the above copyright notice appear in all copies and that both that |
| 9 |
* copyright notice and this permission notice appear in supporting |
| 10 |
* documentation, and that the name of the author(s) not be used in |
| 11 |
* advertising or publicity pertaining to distribution of the software without |
| 12 |
* specific, written prior permission. The authors make no |
| 13 |
* representations about the suitability of this software for any purpose. It |
| 14 |
* is provided "as is" without express or implied warranty. |
| 15 |
* |
| 16 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 22 |
* PERFORMANCE OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
use fontconfig_bindings::{ |
| 26 |
FcWeightFromOpenTypeDouble, FC_DUAL, FC_MONO, FC_SLANT_ITALIC, FC_SLANT_OBLIQUE, |
| 27 |
FC_SLANT_ROMAN, FC_WEIGHT_BLACK, FC_WEIGHT_BOLD, FC_WEIGHT_BOOK, FC_WEIGHT_DEMIBOLD, |
| 28 |
FC_WEIGHT_DEMILIGHT, FC_WEIGHT_EXTRABLACK, FC_WEIGHT_EXTRABOLD, FC_WEIGHT_EXTRALIGHT, |
| 29 |
FC_WEIGHT_HEAVY, FC_WEIGHT_LIGHT, FC_WEIGHT_MEDIUM, FC_WEIGHT_NORMAL, FC_WEIGHT_REGULAR, |
| 30 |
FC_WEIGHT_SEMIBOLD, FC_WEIGHT_SEMILIGHT, FC_WEIGHT_THIN, FC_WEIGHT_ULTRABLACK, |
| 31 |
FC_WEIGHT_ULTRABOLD, FC_WEIGHT_ULTRALIGHT, FC_WIDTH_CONDENSED, FC_WIDTH_EXPANDED, |
| 32 |
FC_WIDTH_EXTRACONDENSED, FC_WIDTH_EXTRAEXPANDED, FC_WIDTH_NORMAL, FC_WIDTH_SEMICONDENSED, |
| 33 |
FC_WIDTH_SEMIEXPANDED, FC_WIDTH_ULTRACONDENSED, FC_WIDTH_ULTRAEXPANDED, |
| 34 |
}; |
| 35 |
|
| 36 |
use fcint_bindings::{ |
| 37 |
FC_DECORATIVE_OBJECT, FC_INDEX_OBJECT, FC_NAMED_INSTANCE_OBJECT, FC_SIZE_OBJECT, |
| 38 |
FC_SLANT_OBJECT, FC_SPACING_OBJECT, FC_STYLE_OBJECT, FC_VARIABLE_OBJECT, FC_WEIGHT_OBJECT, |
| 39 |
FC_WIDTH_OBJECT, |
| 40 |
}; |
| 41 |
|
| 42 |
use crate::{ |
| 43 |
pattern_bindings::{ |
| 44 |
fc_wrapper::FcRangeWrapper, FcPatternBuilder, PatternElement, PatternValue, |
| 45 |
}, |
| 46 |
InstanceMode, |
| 47 |
}; |
| 48 |
use read_fonts::TableProvider; |
| 49 |
use skrifa::{ |
| 50 |
attribute::{Attributes, Stretch, Style, Weight}, |
| 51 |
instance::Location, |
| 52 |
metrics::GlyphMetrics, |
| 53 |
prelude::{LocationRef, Size}, |
| 54 |
AxisCollection, FontRef, MetadataProvider, NamedInstance, Tag, |
| 55 |
}; |
| 56 |
use std::ffi::CString; |
| 57 |
|
| 58 |
fn fc_weight(skrifa_weight: Weight) -> f64 { |
| 59 |
(match skrifa_weight { |
| 60 |
Weight::THIN => FC_WEIGHT_THIN, |
| 61 |
Weight::EXTRA_LIGHT => FC_WEIGHT_EXTRALIGHT, |
| 62 |
Weight::LIGHT => FC_WEIGHT_LIGHT, |
| 63 |
Weight::NORMAL => FC_WEIGHT_NORMAL, |
| 64 |
Weight::MEDIUM => FC_WEIGHT_MEDIUM, |
| 65 |
Weight::SEMI_BOLD => FC_WEIGHT_SEMIBOLD, |
| 66 |
Weight::BOLD => FC_WEIGHT_BOLD, |
| 67 |
Weight::EXTRA_BOLD => FC_WEIGHT_EXTRABOLD, |
| 68 |
Weight::BLACK => FC_WEIGHT_BLACK, |
| 69 |
// See fcfreetype.c: When weight is not available, set to medium. |
| 70 |
// This would mean a font did not have a parseable OS/2 table or |
| 71 |
// a weight value could not be retrieved from it. |
| 72 |
_ => FC_WEIGHT_MEDIUM, |
| 73 |
}) as f64 |
| 74 |
} |
| 75 |
|
| 76 |
fn fc_slant(skrifa_style: Style) -> u32 { |
| 77 |
match skrifa_style { |
| 78 |
Style::Italic => FC_SLANT_ITALIC, |
| 79 |
Style::Oblique(_) => FC_SLANT_OBLIQUE, |
| 80 |
_ => FC_SLANT_ROMAN, |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
fn fc_width(skrifa_stretch: Stretch) -> f64 { |
| 85 |
(match skrifa_stretch { |
| 86 |
Stretch::ULTRA_CONDENSED => FC_WIDTH_ULTRACONDENSED, |
| 87 |
Stretch::EXTRA_CONDENSED => FC_WIDTH_EXTRACONDENSED, |
| 88 |
Stretch::CONDENSED => FC_WIDTH_CONDENSED, |
| 89 |
Stretch::SEMI_CONDENSED => FC_WIDTH_SEMICONDENSED, |
| 90 |
Stretch::NORMAL => FC_WIDTH_NORMAL, |
| 91 |
Stretch::SEMI_EXPANDED => FC_WIDTH_SEMIEXPANDED, |
| 92 |
Stretch::EXPANDED => FC_WIDTH_EXPANDED, |
| 93 |
Stretch::EXTRA_EXPANDED => FC_WIDTH_EXTRAEXPANDED, |
| 94 |
Stretch::ULTRA_EXPANDED => FC_WIDTH_ULTRAEXPANDED, |
| 95 |
_ => FC_WIDTH_NORMAL, |
| 96 |
} as f64) |
| 97 |
} |
| 98 |
|
| 99 |
fn fc_weight_from_os2(font_ref: &FontRef) -> Option<f64> { |
| 100 |
let us_weight = font_ref.os2().ok()?.us_weight_class() as f64; |
| 101 |
unsafe { |
| 102 |
let result = FcWeightFromOpenTypeDouble(us_weight); |
| 103 |
if result == -1.0 { |
| 104 |
None |
| 105 |
} else { |
| 106 |
Some(result) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
fn fc_width_from_os2(font_ref: &FontRef) -> Option<f64> { |
| 112 |
let us_width = font_ref.os2().ok()?.us_width_class(); |
| 113 |
let converted = match us_width { |
| 114 |
1 => FC_WIDTH_ULTRACONDENSED, |
| 115 |
2 => FC_WIDTH_EXTRACONDENSED, |
| 116 |
3 => FC_WIDTH_CONDENSED, |
| 117 |
4 => FC_WIDTH_SEMICONDENSED, |
| 118 |
5 => FC_WIDTH_NORMAL, |
| 119 |
6 => FC_WIDTH_SEMIEXPANDED, |
| 120 |
7 => FC_WIDTH_EXPANDED, |
| 121 |
8 => FC_WIDTH_EXTRAEXPANDED, |
| 122 |
9 => FC_WIDTH_ULTRAEXPANDED, |
| 123 |
_ => FC_WIDTH_NORMAL, |
| 124 |
}; |
| 125 |
Some(converted as f64) |
| 126 |
} |
| 127 |
|
| 128 |
fn fc_size_from_os2(font_ref: &FontRef) -> Option<(f64, f64)> { |
| 129 |
font_ref.os2().ok().and_then(|os2| { |
| 130 |
Some(( |
| 131 |
os2.us_lower_optical_point_size()? as f64 / 20.0, |
| 132 |
os2.us_upper_optical_point_size()? as f64 / 20.0, |
| 133 |
)) |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
struct AttributesToPattern<'a> { |
| 138 |
weight_from_os2: Option<f64>, |
| 139 |
width_from_os2: Option<f64>, |
| 140 |
attributes: Attributes, |
| 141 |
axes: AxisCollection<'a>, |
| 142 |
named_instance: Option<NamedInstance<'a>>, |
| 143 |
font_ref: FontRef<'a>, |
| 144 |
} |
| 145 |
|
| 146 |
impl<'a> AttributesToPattern<'a> { |
| 147 |
fn new(font: &'a FontRef, instance_mode: &InstanceMode) -> Self { |
| 148 |
let named_instance = match instance_mode { |
| 149 |
InstanceMode::Named(index) => font.named_instances().get(*index as usize), |
| 150 |
_ => None, |
| 151 |
}; |
| 152 |
|
| 153 |
Self { |
| 154 |
weight_from_os2: fc_weight_from_os2(font), |
| 155 |
width_from_os2: fc_width_from_os2(font), |
| 156 |
attributes: Attributes::new(font), |
| 157 |
axes: font.axes(), |
| 158 |
named_instance, |
| 159 |
font_ref: font.clone(), |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
fn user_coord_for_tag(&self, tag: Tag) -> Option<f64> { |
| 164 |
let mut axis_coords = self |
| 165 |
.axes |
| 166 |
.iter() |
| 167 |
.map(|axis| axis.tag()) |
| 168 |
.zip(self.named_instance.clone()?.user_coords()); |
| 169 |
Some(axis_coords.find(|item| item.0 == tag)?.1 as f64) |
| 170 |
} |
| 171 |
|
| 172 |
fn flags_weight(&self) -> PatternElement { |
| 173 |
PatternElement::new( |
| 174 |
FC_WEIGHT_OBJECT as i32, |
| 175 |
fc_weight(self.attributes.weight).into(), |
| 176 |
) |
| 177 |
} |
| 178 |
|
| 179 |
fn os2_weight(&self) -> Option<PatternElement> { |
| 180 |
self.weight_from_os2 |
| 181 |
.map(|weight| PatternElement::new(FC_WEIGHT_OBJECT as i32, weight.into())) |
| 182 |
} |
| 183 |
|
| 184 |
fn flags_width(&self) -> PatternElement { |
| 185 |
PatternElement::new( |
| 186 |
FC_WIDTH_OBJECT as i32, |
| 187 |
fc_width(self.attributes.stretch).into(), |
| 188 |
) |
| 189 |
} |
| 190 |
|
| 191 |
fn os2_width(&self) -> Option<PatternElement> { |
| 192 |
self.width_from_os2 |
| 193 |
.map(|width| PatternElement::new(FC_WIDTH_OBJECT as i32, width.into())) |
| 194 |
} |
| 195 |
|
| 196 |
fn static_slant(&self) -> PatternElement { |
| 197 |
PatternElement::new( |
| 198 |
FC_SLANT_OBJECT as i32, |
| 199 |
(fc_slant(self.attributes.style) as i32).into(), |
| 200 |
) |
| 201 |
} |
| 202 |
|
| 203 |
fn instance_weight(&self) -> Option<PatternElement> { |
| 204 |
let named_instance_weight = self.user_coord_for_tag(Tag::new(b"wght"))?; |
| 205 |
unsafe { |
| 206 |
Some(PatternElement::new( |
| 207 |
FC_WEIGHT_OBJECT as i32, |
| 208 |
FcWeightFromOpenTypeDouble(named_instance_weight).into(), |
| 209 |
)) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
fn instance_width(&self) -> Option<PatternElement> { |
| 214 |
let named_instance_weight = self.user_coord_for_tag(Tag::new(b"wdth"))?; |
| 215 |
|
| 216 |
Some(PatternElement::new( |
| 217 |
FC_WIDTH_OBJECT as i32, |
| 218 |
named_instance_weight.into(), |
| 219 |
)) |
| 220 |
} |
| 221 |
|
| 222 |
fn instance_slant(&self) -> Option<PatternElement> { |
| 223 |
let named_instance_slant = self.user_coord_for_tag(Tag::new(b"slnt"))?; |
| 224 |
if named_instance_slant < 0.0 { |
| 225 |
Some(PatternElement::new( |
| 226 |
FC_SLANT_OBJECT as i32, |
| 227 |
(FC_SLANT_ITALIC as i32).into(), |
| 228 |
)) |
| 229 |
} else { |
| 230 |
Some(PatternElement::new( |
| 231 |
FC_SLANT_OBJECT as i32, |
| 232 |
(FC_SLANT_ROMAN as i32).into(), |
| 233 |
)) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
fn instance_size(&self) -> Option<PatternElement> { |
| 238 |
let named_instance_size = self.user_coord_for_tag(Tag::new(b"opsz"))?; |
| 239 |
|
| 240 |
Some(PatternElement::new( |
| 241 |
FC_SIZE_OBJECT as i32, |
| 242 |
named_instance_size.into(), |
| 243 |
)) |
| 244 |
} |
| 245 |
|
| 246 |
fn default_axis_size(&self) -> Option<PatternElement> { |
| 247 |
self.axes.get_by_tag(Tag::new(b"opsz")).map(|opsz_axis| { |
| 248 |
PatternElement::new( |
| 249 |
FC_SIZE_OBJECT as i32, |
| 250 |
(opsz_axis.default_value() as f64).into(), |
| 251 |
) |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
fn os2_size(&self) -> Option<PatternElement> { |
| 256 |
fc_size_from_os2(&self.font_ref).and_then(|(lower, higher)| { |
| 257 |
if lower != higher { |
| 258 |
let range = FcRangeWrapper::new(lower, higher)?; |
| 259 |
Some(PatternElement::new(FC_SIZE_OBJECT as i32, range.into())) |
| 260 |
} else { |
| 261 |
Some(PatternElement::new(FC_SIZE_OBJECT as i32, lower.into())) |
| 262 |
} |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
fn variable_weight(&self) -> Option<PatternElement> { |
| 267 |
let weight_axis = self.axes.get_by_tag(Tag::new(b"wght"))?; |
| 268 |
unsafe { |
| 269 |
Some(PatternElement::new( |
| 270 |
FC_WEIGHT_OBJECT as i32, |
| 271 |
FcRangeWrapper::new( |
| 272 |
FcWeightFromOpenTypeDouble(weight_axis.min_value() as f64), |
| 273 |
FcWeightFromOpenTypeDouble(weight_axis.max_value() as f64), |
| 274 |
)? |
| 275 |
.into(), |
| 276 |
)) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
fn variable_width(&self) -> Option<PatternElement> { |
| 281 |
let width_axis = self.axes.get_by_tag(Tag::new(b"wdth"))?; |
| 282 |
Some(PatternElement::new( |
| 283 |
FC_WIDTH_OBJECT as i32, |
| 284 |
FcRangeWrapper::new(width_axis.min_value() as f64, width_axis.max_value() as f64)? |
| 285 |
.into(), |
| 286 |
)) |
| 287 |
} |
| 288 |
|
| 289 |
fn variable_opsz(&self) -> Option<PatternElement> { |
| 290 |
let opsz_axis = self.axes.get_by_tag(Tag::new(b"opsz"))?; |
| 291 |
Some(PatternElement::new( |
| 292 |
FC_SIZE_OBJECT as i32, |
| 293 |
FcRangeWrapper::new(opsz_axis.min_value() as f64, opsz_axis.max_value() as f64)?.into(), |
| 294 |
)) |
| 295 |
} |
| 296 |
|
| 297 |
// Determine FontConfig spacing property. |
| 298 |
// In fcfreetype.c FcFreeTypeSpacing a heuristic checks whether the glyphs |
| 299 |
// for a bitmap font are "approximately equal". |
| 300 |
// For this purpose, fcfreetype.c selects the strike size closest to 16 px, |
| 301 |
// then iterates over available glyphs and checks whether the advance width |
| 302 |
// is within a relative tolerance of 1/33. |
| 303 |
// We'll do this based on skrifa advances here - if needed, this can be |
| 304 |
// moved to selecting a particular bitmap strike size and compare pixel |
| 305 |
// widths. |
| 306 |
fn spacing_from_advances(advances: impl Iterator<Item = f32>) -> Option<i32> { |
| 307 |
let mut encountered_advances = Vec::new(); |
| 308 |
|
| 309 |
let approximately_equal = |
| 310 |
|a: f32, b: f32| (a - b).abs() / a.abs().max(b.abs()) <= 1.0 / 33.0; |
| 311 |
|
| 312 |
'outer: for advance in advances { |
| 313 |
let mut may_push: Option<f32> = None; |
| 314 |
if encountered_advances.is_empty() { |
| 315 |
encountered_advances.push(advance); |
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
for encountered in encountered_advances.iter() { |
| 320 |
if encountered_advances.len() >= 3 { |
| 321 |
break 'outer; |
| 322 |
} |
| 323 |
if approximately_equal(advance, *encountered) { |
| 324 |
may_push = None; |
| 325 |
break; |
| 326 |
} |
| 327 |
may_push = Some(advance); |
| 328 |
} |
| 329 |
if let Some(push) = may_push { |
| 330 |
encountered_advances.push(push); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
match encountered_advances.len() { |
| 335 |
1 => Some(FC_MONO as i32), |
| 336 |
2 if approximately_equal( |
| 337 |
encountered_advances[0].min(encountered_advances[1]) * 2.0, |
| 338 |
encountered_advances[0].max(encountered_advances[1]), |
| 339 |
) => |
| 340 |
{ |
| 341 |
Some(FC_DUAL as i32) |
| 342 |
} |
| 343 |
|
| 344 |
_ => None, |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
fn spacing(&self) -> Option<PatternElement> { |
| 349 |
let mut location = Location::default(); |
| 350 |
if let Some(instance) = &self.named_instance { |
| 351 |
location = instance.location().clone(); |
| 352 |
}; |
| 353 |
|
| 354 |
let glyph_metrics = GlyphMetrics::new( |
| 355 |
&self.font_ref, |
| 356 |
Size::new(16.0), |
| 357 |
LocationRef::from(&location), |
| 358 |
); |
| 359 |
|
| 360 |
let advances = self |
| 361 |
.font_ref |
| 362 |
.charmap() |
| 363 |
.mappings() |
| 364 |
.filter_map(|(_codepoint, gid)| { |
| 365 |
glyph_metrics |
| 366 |
.advance_width(gid) |
| 367 |
.and_then(|adv| if adv > 0.0 { Some(adv) } else { None }) |
| 368 |
}); |
| 369 |
|
| 370 |
Self::spacing_from_advances(advances) |
| 371 |
.map(|spacing| PatternElement::new(FC_SPACING_OBJECT as i32, spacing.into())) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
#[derive(Default)] |
| 376 |
struct AttributesFromStyleString { |
| 377 |
weight: Option<PatternElement>, |
| 378 |
width: Option<PatternElement>, |
| 379 |
slant: Option<PatternElement>, |
| 380 |
decorative: Option<PatternElement>, |
| 381 |
} |
| 382 |
|
| 383 |
fn contains_weight(style_name: &CString) -> Option<PatternElement> { |
| 384 |
const WEIGHT_MAP: [(&str, f64); 23] = [ |
| 385 |
("thin", FC_WEIGHT_THIN as f64), |
| 386 |
("extralight", FC_WEIGHT_EXTRALIGHT as f64), |
| 387 |
("ultralight", FC_WEIGHT_ULTRALIGHT as f64), |
| 388 |
("demilight", FC_WEIGHT_DEMILIGHT as f64), |
| 389 |
("semilight", FC_WEIGHT_SEMILIGHT as f64), |
| 390 |
("light", FC_WEIGHT_LIGHT as f64), |
| 391 |
("book", FC_WEIGHT_BOOK as f64), |
| 392 |
("regular", FC_WEIGHT_REGULAR as f64), |
| 393 |
("normal", FC_WEIGHT_NORMAL as f64), |
| 394 |
("medium", FC_WEIGHT_MEDIUM as f64), |
| 395 |
("demibold", FC_WEIGHT_DEMIBOLD as f64), |
| 396 |
("demi", FC_WEIGHT_DEMIBOLD as f64), |
| 397 |
("semibold", FC_WEIGHT_SEMIBOLD as f64), |
| 398 |
("extrabold", FC_WEIGHT_EXTRABOLD as f64), |
| 399 |
("superbold", FC_WEIGHT_EXTRABOLD as f64), |
| 400 |
("ultrabold", FC_WEIGHT_ULTRABOLD as f64), |
| 401 |
("bold", FC_WEIGHT_BOLD as f64), |
| 402 |
("ultrablack", FC_WEIGHT_ULTRABLACK as f64), |
| 403 |
("superblack", FC_WEIGHT_EXTRABLACK as f64), |
| 404 |
("extrablack", FC_WEIGHT_EXTRABLACK as f64), |
| 405 |
("ultra", FC_WEIGHT_ULTRABOLD as f64), |
| 406 |
("black", FC_WEIGHT_BLACK as f64), |
| 407 |
("heavy", FC_WEIGHT_HEAVY as f64), |
| 408 |
]; |
| 409 |
|
| 410 |
for weight_mapping in WEIGHT_MAP { |
| 411 |
if style_name |
| 412 |
.to_string_lossy() |
| 413 |
.to_lowercase() |
| 414 |
.contains(weight_mapping.0) |
| 415 |
{ |
| 416 |
return Some(PatternElement::new( |
| 417 |
FC_WEIGHT_OBJECT as i32, |
| 418 |
weight_mapping.1.into(), |
| 419 |
)); |
| 420 |
} |
| 421 |
} |
| 422 |
None |
| 423 |
} |
| 424 |
|
| 425 |
fn contains_slant(style_name: &CString) -> Option<PatternElement> { |
| 426 |
const SLANT_MAP: [(&str, i32); 3] = [ |
| 427 |
("italic", FC_SLANT_ITALIC as i32), |
| 428 |
("kursiv", FC_SLANT_ITALIC as i32), |
| 429 |
("oblique", FC_SLANT_OBLIQUE as i32), |
| 430 |
]; |
| 431 |
|
| 432 |
for mapping in SLANT_MAP { |
| 433 |
if style_name |
| 434 |
.to_string_lossy() |
| 435 |
.to_lowercase() |
| 436 |
.contains(mapping.0) |
| 437 |
{ |
| 438 |
return Some(PatternElement::new( |
| 439 |
FC_SLANT_OBJECT as i32, |
| 440 |
mapping.1.into(), |
| 441 |
)); |
| 442 |
} |
| 443 |
} |
| 444 |
None |
| 445 |
} |
| 446 |
|
| 447 |
fn contains_width(style_name: &CString) -> Option<PatternElement> { |
| 448 |
const WIDTH_MAP: [(&str, f64); 10] = [ |
| 449 |
("ultracondensed", FC_WIDTH_ULTRACONDENSED as f64), |
| 450 |
("extracondensed", FC_WIDTH_EXTRACONDENSED as f64), |
| 451 |
("semicondensed", FC_WIDTH_SEMICONDENSED as f64), |
| 452 |
("condensed", FC_WIDTH_CONDENSED as f64), |
| 453 |
("normal", FC_WIDTH_NORMAL as f64), |
| 454 |
("semiexpanded", FC_WIDTH_SEMIEXPANDED as f64), |
| 455 |
("extraexpanded", FC_WIDTH_EXTRAEXPANDED as f64), |
| 456 |
("ultraexpanded", FC_WIDTH_ULTRAEXPANDED as f64), |
| 457 |
("expanded", FC_WIDTH_EXPANDED as f64), |
| 458 |
("extended", FC_WIDTH_EXPANDED as f64), |
| 459 |
]; |
| 460 |
for mapping in WIDTH_MAP { |
| 461 |
if style_name |
| 462 |
.to_string_lossy() |
| 463 |
.to_lowercase() |
| 464 |
.contains(mapping.0) |
| 465 |
{ |
| 466 |
return Some(PatternElement::new( |
| 467 |
FC_WIDTH_OBJECT as i32, |
| 468 |
mapping.1.into(), |
| 469 |
)); |
| 470 |
} |
| 471 |
} |
| 472 |
None |
| 473 |
} |
| 474 |
|
| 475 |
fn contains_decorative(style_name: &CString) -> Option<PatternElement> { |
| 476 |
let had_decorative = style_name |
| 477 |
.to_string_lossy() |
| 478 |
.to_lowercase() |
| 479 |
.contains("decorative"); |
| 480 |
|
| 481 |
Some(PatternElement::new( |
| 482 |
FC_DECORATIVE_OBJECT as i32, |
| 483 |
had_decorative.into(), |
| 484 |
)) |
| 485 |
} |
| 486 |
|
| 487 |
impl AttributesFromStyleString { |
| 488 |
fn new(pattern: &FcPatternBuilder) -> Self { |
| 489 |
let style_string = pattern |
| 490 |
.into_iter() |
| 491 |
.find(|element| element.object_id == FC_STYLE_OBJECT as i32) |
| 492 |
.and_then(|element| match &element.value { |
| 493 |
PatternValue::String(style) => Some(style), |
| 494 |
_ => None, |
| 495 |
}); |
| 496 |
|
| 497 |
if let Some(style) = style_string { |
| 498 |
Self { |
| 499 |
weight: contains_weight(style), |
| 500 |
width: contains_width(style), |
| 501 |
slant: contains_slant(style), |
| 502 |
decorative: contains_decorative(style), |
| 503 |
} |
| 504 |
} else { |
| 505 |
Self { |
| 506 |
weight: None, |
| 507 |
width: None, |
| 508 |
slant: None, |
| 509 |
decorative: Some(PatternElement::new( |
| 510 |
FC_DECORATIVE_OBJECT as i32, |
| 511 |
false.into(), |
| 512 |
)), |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
/// Appends style pattern elements such as weight, width, slant, decorative to the pattern. |
| 519 |
/// Requires a textual style element to be already added to the pattern, so it's good |
| 520 |
/// to run this after names have been added. This is because this method performs certain |
| 521 |
/// string matches on the font name to determine style attributes. |
| 522 |
pub fn append_style_elements( |
| 523 |
font: &FontRef, |
| 524 |
instance_mode: InstanceMode, |
| 525 |
ttc_index: Option<i32>, |
| 526 |
pattern: &mut FcPatternBuilder, |
| 527 |
) { |
| 528 |
// TODO: fcfreetype.c seems to prefer parsing information from the WWS name table entry, |
| 529 |
// but falls back to flags if those are not found. So far, I haven't identified test fonts |
| 530 |
// for which the WWS code path would trigger. |
| 531 |
|
| 532 |
let attributes_text = AttributesFromStyleString::new(pattern); |
| 533 |
|
| 534 |
let skrifa_attributes = AttributesToPattern::new(font, &instance_mode); |
| 535 |
|
| 536 |
if let Some(spacing) = skrifa_attributes.spacing() { |
| 537 |
pattern.append_element(spacing); |
| 538 |
} |
| 539 |
|
| 540 |
match instance_mode { |
| 541 |
InstanceMode::Default => { |
| 542 |
let pattern_weight = skrifa_attributes |
| 543 |
.os2_weight() |
| 544 |
.or(attributes_text.weight) |
| 545 |
.unwrap_or(skrifa_attributes.flags_weight()); |
| 546 |
pattern.append_element(pattern_weight); |
| 547 |
|
| 548 |
let width = skrifa_attributes |
| 549 |
.os2_width() |
| 550 |
.or(attributes_text.width) |
| 551 |
.unwrap_or(skrifa_attributes.flags_width()); |
| 552 |
pattern.append_element(width); |
| 553 |
|
| 554 |
pattern.append_element( |
| 555 |
attributes_text |
| 556 |
.slant |
| 557 |
.unwrap_or(skrifa_attributes.static_slant()), |
| 558 |
); |
| 559 |
|
| 560 |
if let Some(element) = attributes_text.decorative { |
| 561 |
pattern.append_element(element) |
| 562 |
} |
| 563 |
|
| 564 |
pattern.append_element(PatternElement::new(FC_VARIABLE_OBJECT as i32, false.into())); |
| 565 |
pattern.append_element(PatternElement::new( |
| 566 |
FC_INDEX_OBJECT as i32, |
| 567 |
ttc_index.unwrap_or_default().into(), |
| 568 |
)); |
| 569 |
|
| 570 |
if let Some(size) = skrifa_attributes |
| 571 |
.default_axis_size() |
| 572 |
.or(skrifa_attributes.os2_size()) |
| 573 |
{ |
| 574 |
pattern.append_element(size); |
| 575 |
} |
| 576 |
|
| 577 |
pattern.append_element(PatternElement::new( |
| 578 |
FC_NAMED_INSTANCE_OBJECT as i32, |
| 579 |
false.into(), |
| 580 |
)); |
| 581 |
} |
| 582 |
InstanceMode::Variable => { |
| 583 |
let weight = skrifa_attributes |
| 584 |
.variable_weight() |
| 585 |
.or(skrifa_attributes.os2_weight()) |
| 586 |
.or(attributes_text.weight) |
| 587 |
.unwrap_or(skrifa_attributes.flags_weight()); |
| 588 |
pattern.append_element(weight); |
| 589 |
|
| 590 |
let width = skrifa_attributes |
| 591 |
.variable_width() |
| 592 |
.or(skrifa_attributes.os2_width()) |
| 593 |
.or(attributes_text.width) |
| 594 |
.unwrap_or(skrifa_attributes.flags_width()); |
| 595 |
pattern.append_element(width); |
| 596 |
|
| 597 |
if let Some(element) = attributes_text.decorative { |
| 598 |
pattern.append_element(element) |
| 599 |
} |
| 600 |
|
| 601 |
if let Some(size) = skrifa_attributes.variable_opsz() { |
| 602 |
pattern.append_element(size); |
| 603 |
} |
| 604 |
|
| 605 |
pattern.append_element(PatternElement::new(FC_VARIABLE_OBJECT as i32, true.into())); |
| 606 |
|
| 607 |
// TODO: Check if this should have a zero ttc index if not part of a collection. |
| 608 |
pattern.append_element(PatternElement::new( |
| 609 |
FC_INDEX_OBJECT as i32, |
| 610 |
ttc_index.unwrap_or_default().into(), |
| 611 |
)); |
| 612 |
pattern.append_element(PatternElement::new( |
| 613 |
FC_NAMED_INSTANCE_OBJECT as i32, |
| 614 |
false.into(), |
| 615 |
)); |
| 616 |
pattern.append_element(skrifa_attributes.static_slant()); |
| 617 |
} |
| 618 |
InstanceMode::Named(index) => { |
| 619 |
let weight = skrifa_attributes |
| 620 |
.instance_weight() |
| 621 |
.or(attributes_text.weight) |
| 622 |
.unwrap_or(skrifa_attributes.flags_weight()); |
| 623 |
pattern.append_element(weight); |
| 624 |
|
| 625 |
let width = skrifa_attributes |
| 626 |
.instance_width() |
| 627 |
.or(attributes_text.width) |
| 628 |
.unwrap_or(skrifa_attributes.flags_width()); |
| 629 |
pattern.append_element(width); |
| 630 |
|
| 631 |
pattern.append_element( |
| 632 |
skrifa_attributes |
| 633 |
.instance_slant() |
| 634 |
.or(attributes_text.slant) |
| 635 |
.unwrap_or(skrifa_attributes.static_slant()), |
| 636 |
); |
| 637 |
|
| 638 |
if let Some(element) = attributes_text.decorative { |
| 639 |
pattern.append_element(element) |
| 640 |
} |
| 641 |
|
| 642 |
pattern.append_element(PatternElement::new(FC_VARIABLE_OBJECT as i32, false.into())); |
| 643 |
pattern.append_element(PatternElement::new( |
| 644 |
FC_INDEX_OBJECT as i32, |
| 645 |
(ttc_index.unwrap_or_default() + ((index + 1) << 16)).into(), |
| 646 |
)); |
| 647 |
if let Some(size_element) = skrifa_attributes |
| 648 |
.instance_size() |
| 649 |
.or(skrifa_attributes.default_axis_size()) |
| 650 |
{ |
| 651 |
pattern.append_element(size_element); |
| 652 |
}; |
| 653 |
|
| 654 |
pattern.append_element(PatternElement::new( |
| 655 |
FC_NAMED_INSTANCE_OBJECT as i32, |
| 656 |
true.into(), |
| 657 |
)); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
#[cfg(test)] |
| 663 |
mod test { |
| 664 |
use crate::attributes::AttributesToPattern; |
| 665 |
use fontconfig_bindings::{FC_DUAL, FC_MONO}; |
| 666 |
|
| 667 |
const THRESHOLD_FACTOR_DOWN: f32 = 1.0 - 1.0 / 33.0; |
| 668 |
const THRESHOLD_FACTOR_UP: f32 = 1.0 + 1.0 / 33.0; |
| 669 |
|
| 670 |
fn assert_spacing(advances: impl Iterator<Item = f32>, expectation: Option<i32>) { |
| 671 |
assert_eq!( |
| 672 |
AttributesToPattern::spacing_from_advances(advances), |
| 673 |
expectation |
| 674 |
); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn spacing_mono() { |
| 679 |
assert_spacing([10.0].iter().cloned(), Some(FC_MONO as i32)); |
| 680 |
|
| 681 |
assert_spacing( |
| 682 |
[ |
| 683 |
10.0, |
| 684 |
10.0, |
| 685 |
10.0, |
| 686 |
10.0 * THRESHOLD_FACTOR_UP, |
| 687 |
10.0 * THRESHOLD_FACTOR_DOWN, |
| 688 |
] |
| 689 |
.iter() |
| 690 |
.cloned(), |
| 691 |
Some(FC_MONO as i32), |
| 692 |
); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn spacing_proportional() { |
| 697 |
assert_spacing( |
| 698 |
[10.0, 10.0 * THRESHOLD_FACTOR_UP + 0.01].iter().cloned(), |
| 699 |
None, |
| 700 |
); |
| 701 |
|
| 702 |
assert_spacing( |
| 703 |
[10.0, 10.0 * THRESHOLD_FACTOR_DOWN - 0.01].iter().cloned(), |
| 704 |
None, |
| 705 |
); |
| 706 |
|
| 707 |
assert_spacing([10.0, 15.0].iter().cloned(), None); |
| 708 |
|
| 709 |
assert_spacing([10.0, 15.0, 20.0].iter().cloned(), None); |
| 710 |
} |
| 711 |
|
| 712 |
#[test] |
| 713 |
fn advances_dual() { |
| 714 |
assert_spacing([10.0, 20.0].iter().cloned(), Some(FC_DUAL as i32)); |
| 715 |
|
| 716 |
assert_spacing( |
| 717 |
[10.0, 20.0, 10.0, 20.0, 10.0, 20.0].iter().cloned(), |
| 718 |
Some(FC_DUAL as i32), |
| 719 |
); |
| 720 |
|
| 721 |
assert_spacing( |
| 722 |
[ |
| 723 |
10.0, |
| 724 |
20.0 * THRESHOLD_FACTOR_UP, |
| 725 |
10.0, |
| 726 |
20.0, |
| 727 |
10.0 * THRESHOLD_FACTOR_DOWN, |
| 728 |
20.0, |
| 729 |
] |
| 730 |
.iter() |
| 731 |
.cloned(), |
| 732 |
Some(FC_DUAL as i32), |
| 733 |
); |
| 734 |
} |
| 735 |
} |