Speedup font loading with fontconfig and freetype.
- If possible use fontconfig to obtain character coverage instead of going through all glyphs using freetype. - `FT_Get_First_Char` typically returns ranges of continuous glyphs itself and it is far cheaper (I measured a speedup of about 7 times while catting a large file with lots of funny unicode) to add a range to the glyph coverage instead of adding each glyph individually. - Permit adding a range to a RangeSet without performing checks to speed up things even further.
This commit is contained in:
1 parent
10b64abec8
commit
466e5cab21
6 files changed
+118
-6
No files matched your search
+5
-1
@@ -6,7 +6,7 @@ use std::ops::Range;
|
||||
/// Track a set of integers, collapsing adjacent integers into ranges.
|
||||
/// Internally stores the set in an array of ranges.
|
||||
/// Allows adding and subtracting ranges.
|
||||
#[derive(Debug, Default, Clone, PartialEq)]
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct RangeSet<T: Integer + Copy> {
|
||||
ranges: Vec<Range<T>>,
|
||||
}
|
||||
@@ -241,6 +241,10 @@ impl<T: Integer + Copy + Debug + ToPrimitive> RangeSet<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_range_unchecked(&mut self, range: Range<T>) {
|
||||
self.ranges.push(range);
|
||||
}
|
||||
|
||||
/// Add a set of ranges to this set
|
||||
pub fn add_set(&mut self, set: &Self) {
|
||||
for r in set.iter() {
|
||||
|
||||
@@ -104,6 +104,49 @@ pub struct CharSet {
|
||||
cset: *mut FcCharSet,
|
||||
}
|
||||
|
||||
pub struct CharSetRef<'a> {
|
||||
cset: *mut FcCharSet,
|
||||
phantom: std::marker::PhantomData<&'a FcCharSet>,
|
||||
}
|
||||
|
||||
impl<'a> CharSetRef<'a> {
|
||||
pub fn to_range_set(&self) -> rangeset::RangeSet<u32> {
|
||||
let mut coverage = rangeset::RangeSet::new();
|
||||
let mut next_base_code_point = FcChar32::default();
|
||||
const FC_CHARSET_MAP_SIZE: usize = 256 / 32;
|
||||
const FC_CHARSET_DONE: FcChar32 = FcChar32::MAX;
|
||||
let mut map = [FcChar32::default(); FC_CHARSET_MAP_SIZE];
|
||||
let mut base_code_point =
|
||||
unsafe { FcCharSetFirstPage(self.cset, map.as_mut_ptr(), &mut next_base_code_point) };
|
||||
let mut range_start = FcChar32::MAX;
|
||||
let mut code_point = FcChar32::MAX;
|
||||
while base_code_point != FC_CHARSET_DONE {
|
||||
for (i, mask) in map.iter().enumerate() {
|
||||
for j in 0..32 {
|
||||
if mask & (1 << j) != 0 {
|
||||
let new_code_point = base_code_point + (j + i * 32) as u32;
|
||||
if new_code_point - 1 > code_point {
|
||||
coverage.add_range_unchecked(range_start..code_point + 1);
|
||||
range_start = new_code_point;
|
||||
}
|
||||
if range_start == FcChar32::MAX {
|
||||
range_start = new_code_point;
|
||||
}
|
||||
code_point = new_code_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
base_code_point = unsafe {
|
||||
FcCharSetNextPage(self.cset, map.as_mut_ptr(), &mut next_base_code_point)
|
||||
};
|
||||
}
|
||||
if range_start != FcChar32::MAX {
|
||||
coverage.add_range_unchecked(range_start..code_point + 1);
|
||||
}
|
||||
coverage
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for CharSet {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
@@ -112,6 +155,15 @@ impl Drop for CharSet {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a CharSet> for CharSetRef<'a> {
|
||||
fn from(c: &'a CharSet) -> Self {
|
||||
Self {
|
||||
cset: c.cset,
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CharSet {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
unsafe {
|
||||
@@ -145,6 +197,18 @@ impl Pattern {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_charset<'a>(&'a self) -> anyhow::Result<CharSetRef<'a>> {
|
||||
let mut c = ptr::null_mut();
|
||||
unsafe {
|
||||
FcPatternGetCharSet(self.pat, b"charset\0".as_ptr() as *const c_char, 0, &mut c);
|
||||
}
|
||||
ensure!(!c.is_null(), "pattern has no charset");
|
||||
Ok(CharSetRef {
|
||||
cset: c,
|
||||
phantom: std::marker::PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn add_charset(&mut self, charset: &CharSet) -> anyhow::Result<()> {
|
||||
unsafe {
|
||||
ensure!(
|
||||
|
||||
@@ -157,6 +157,7 @@ impl Face {
|
||||
index: self.source.index,
|
||||
variation: i,
|
||||
origin: self.source.origin,
|
||||
coverage: self.source.coverage.clone(),
|
||||
};
|
||||
res.push(ParsedFont::from_face(&self, source)?);
|
||||
}
|
||||
@@ -258,6 +259,9 @@ impl Face {
|
||||
}
|
||||
|
||||
pub fn compute_coverage(&self) -> RangeSet<u32> {
|
||||
if let Some(coverage) = self.source.coverage.as_ref() {
|
||||
return coverage.clone();
|
||||
}
|
||||
let mut coverage = RangeSet::new();
|
||||
|
||||
for encoding in &[
|
||||
@@ -270,10 +274,22 @@ impl Face {
|
||||
|
||||
let mut glyph = 0;
|
||||
let mut ucs4 = unsafe { FT_Get_First_Char(self.face, &mut glyph) };
|
||||
while glyph != 0 {
|
||||
coverage.add(ucs4 as u32);
|
||||
ucs4 = unsafe { FT_Get_Next_Char(self.face, ucs4, &mut glyph) };
|
||||
if glyph == 0 {
|
||||
break;
|
||||
}
|
||||
let mut ucs4_range_start = ucs4;
|
||||
loop {
|
||||
let ucs4_new = unsafe { FT_Get_Next_Char(self.face, ucs4, &mut glyph) };
|
||||
if glyph == 0 {
|
||||
break;
|
||||
}
|
||||
if ucs4_new - ucs4 != 1 {
|
||||
coverage.add_range_unchecked(ucs4_range_start as u32..(ucs4 + 1) as u32);
|
||||
ucs4_range_start = ucs4_new;
|
||||
}
|
||||
ucs4 = ucs4_new;
|
||||
}
|
||||
coverage.add_range_unchecked(ucs4_range_start as u32..(ucs4 + 1) as u32);
|
||||
|
||||
if *encoding == FT_Encoding::FT_ENCODING_MS_SYMBOL {
|
||||
// Fontconfig duplicates F000..F0FF to 0000..00FF
|
||||
@@ -284,7 +300,6 @@ impl Face {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
coverage
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@ impl FontLocator for FontConfigFontLocator {
|
||||
index,
|
||||
variation,
|
||||
origin: FontOrigin::FontConfig,
|
||||
coverage: Some(pat.get_charset()?.to_range_set())
|
||||
};
|
||||
|
||||
// fontconfig will give us a boatload of random fallbacks.
|
||||
@@ -157,6 +158,7 @@ impl FontLocator for FontConfigFontLocator {
|
||||
index: pat.get_integer("index")?.try_into()?,
|
||||
variation: 0,
|
||||
origin: FontOrigin::FontConfig,
|
||||
coverage: Some(pat.get_charset()?.to_range_set())
|
||||
};
|
||||
if let Ok(parsed) = crate::parser::ParsedFont::from_locator(&handle) {
|
||||
fonts.push(parsed);
|
||||
|
||||
@@ -108,12 +108,35 @@ impl std::fmt::Debug for FontDataSource {
|
||||
/// The `index` parameter is the index into a font
|
||||
/// collection if the data represents a collection of
|
||||
/// fonts.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct FontDataHandle {
|
||||
pub source: FontDataSource,
|
||||
pub index: u32,
|
||||
pub variation: u32,
|
||||
pub origin: FontOrigin,
|
||||
pub coverage: Option<rangeset::RangeSet<u32>>
|
||||
}
|
||||
|
||||
impl PartialOrd for FontDataHandle {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
(&self.source, self.index, self.variation, &self.origin).partial_cmp(&(
|
||||
&other.source,
|
||||
other.index,
|
||||
other.variation,
|
||||
&other.origin,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for FontDataHandle {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
(&self.source, self.index, self.variation, &self.origin).cmp(&(
|
||||
&other.source,
|
||||
other.index,
|
||||
other.variation,
|
||||
&other.origin,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl FontDataHandle {
|
||||
|
||||
@@ -448,6 +448,8 @@ pub(crate) fn load_built_in_fonts(font_info: &mut Vec<ParsedFont>) -> anyhow::Re
|
||||
index: 0,
|
||||
variation: 0,
|
||||
origin: FontOrigin::BuiltIn,
|
||||
// TODO: precompute this!
|
||||
coverage: None,
|
||||
};
|
||||
let face = lib.face_from_locator(&locator)?;
|
||||
let parsed = ParsedFont::from_face(&face, locator)?;
|
||||
@@ -487,6 +489,8 @@ pub(crate) fn parse_and_collect_font_info(
|
||||
index,
|
||||
variation: 0,
|
||||
origin,
|
||||
// TODO: ???
|
||||
coverage: None,
|
||||
};
|
||||
|
||||
let face = lib.face_from_locator(&locator)?;
|
||||
|
||||
Reference in new issue
Block a user