Skip to content

Commit 4ee67ca

Browse files
committed
desktop: Update fontconfig to 0.11.0
1 parent e41992a commit 4ee67ca

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

Cargo.lock

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

desktop/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ rand = "0.9.1"
5151
thiserror.workspace = true
5252
async-channel.workspace = true
5353
unicode-bidi = "0.3.18"
54-
fontconfig = { version = "0.10.2", optional = true, features = ["dlopen"]}
54+
fontconfig = { version = "0.11.0-rc.1", optional = true, features = ["dlopen"]}
5555
memmap2.workspace = true
5656
walkdir.workspace = true
5757
async-task = "4.7.1"

desktop/src/backends/ui.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ impl UiBackend for DesktopUiBackend {
336336
register: &mut dyn FnMut(FontDefinition),
337337
) -> Vec<FontQuery> {
338338
#[cfg(feature = "fontconfig")]
339-
return fontconfig_sort_device_fonts(query, register);
339+
return fontconfig_sort_device_fonts(query, register)
340+
.inspect_err(|err| tracing::error!("Cannot sort device fonts: {err}"))
341+
.unwrap_or_default();
340342

341343
#[cfg(not(feature = "fontconfig"))]
342344
return Vec::new();
@@ -440,55 +442,72 @@ fn load_fontdb_font(name: String, face: &FaceInfo) -> Result<FontDefinition<'sta
440442
}
441443
}
442444

445+
#[cfg(feature = "fontconfig")]
446+
#[derive(Debug, thiserror::Error)]
447+
enum FontconfigError {
448+
#[error("Malformed font family")]
449+
MalformedFontFamily,
450+
#[error("Internal fontconfig error: {0}")]
451+
Internal(fontconfig::FontconfigError),
452+
}
453+
454+
#[cfg(feature = "fontconfig")]
455+
impl From<fontconfig::FontconfigError> for FontconfigError {
456+
fn from(value: fontconfig::FontconfigError) -> Self {
457+
FontconfigError::Internal(value)
458+
}
459+
}
460+
443461
#[cfg(feature = "fontconfig")]
444462
fn fontconfig_sort_device_fonts(
445463
query: &FontQuery,
446464
register: &mut dyn FnMut(FontDefinition),
447-
) -> Vec<FontQuery> {
465+
) -> Result<Vec<FontQuery>, FontconfigError> {
448466
use fontconfig::{FontFormat, Pattern};
449467
use std::sync::LazyLock;
450468

451469
static FONTCONFIG: LazyLock<Option<fontconfig::Fontconfig>> =
452470
LazyLock::new(fontconfig::Fontconfig::new);
453471

454472
let Some(fc) = FONTCONFIG.as_ref() else {
455-
return Vec::new();
473+
return Ok(Vec::new());
456474
};
457475

458476
let Ok(family) = std::ffi::CString::new(query.name.as_str()) else {
459-
tracing::error!("Cannot sort device fonts, null in font family");
460-
return Vec::new();
477+
return Err(FontconfigError::MalformedFontFamily);
461478
};
462479

463-
let mut pattern: Pattern<'static> = Pattern::new(fc);
464-
pattern.add_string(fontconfig::FC_FAMILY, family.as_c_str());
480+
let mut pattern: Pattern<'static> = Pattern::new(fc)?;
481+
482+
pattern.add_string(fontconfig::FC_FAMILY, family.as_c_str())?;
465483

466484
if query.is_bold {
467-
pattern.add_integer(fontconfig::FC_WEIGHT, fontconfig::FC_WEIGHT_BOLD);
485+
pattern.add_integer(fontconfig::FC_WEIGHT, fontconfig::FC_WEIGHT_BOLD)?;
468486
}
469487
if query.is_italic {
470-
pattern.add_integer(fontconfig::FC_SLANT, fontconfig::FC_SLANT_ITALIC);
488+
pattern.add_integer(fontconfig::FC_SLANT, fontconfig::FC_SLANT_ITALIC)?;
471489
}
472490

473-
let font_set = pattern.sort_fonts(true);
491+
let font_set = pattern.sort_fonts(fontconfig::UnicodeCoverage::Trim)?;
492+
474493
let mut font_queries = Vec::new();
475494
for font in font_set.iter() {
476495
let is_ttf = font
477496
.format()
478497
.is_ok_and(|f| matches!(f, FontFormat::TrueType));
479498
if !is_ttf {
480-
if let Some(name) = font.name() {
481-
tracing::info!("Skipping font '{name}' because it's not a TTF");
499+
if let Ok(name) = font.name() {
500+
tracing::info!("Skipping font '{name}' because it's not a TTF (doesn't have a name)");
482501
}
483502
continue;
484503
}
485504

486505
let (
487-
Some(name), //
488-
Some(filename),
489-
Some(index),
490-
Some(weight),
491-
Some(slant),
506+
Ok(name), //
507+
Ok(filename),
508+
Ok(index),
509+
Ok(weight),
510+
Ok(slant),
492511
) = (
493512
font.name(),
494513
font.filename(),
@@ -524,5 +543,6 @@ fn fontconfig_sort_device_fonts(
524543
let query = FontQuery::new(query.font_type, name.to_string(), is_bold, is_italic);
525544
font_queries.push(query);
526545
}
527-
font_queries
546+
547+
Ok(font_queries)
528548
}

0 commit comments

Comments
 (0)