Bundle reserved-glyph font subsets for covered documents
This commit is contained in:
1 parent
ea14b3edbf
commit
ec45ce0694
15 files changed
+100
-3
No files matched your search
+96
-3
@@ -2096,7 +2096,13 @@ struct FontInputs {
|
||||
|
||||
impl FontInputs {
|
||||
fn load(model: &DocumentModel) -> Result<Self, String> {
|
||||
let latin_dir = latin_font_directory()?;
|
||||
let subset_dir = document_fits_subset_fonts(model)
|
||||
.then(subset_font_directory)
|
||||
.flatten();
|
||||
let latin_dir = match &subset_dir {
|
||||
Some(dir) => dir.clone(),
|
||||
None => latin_font_directory()?,
|
||||
};
|
||||
// Always embed the metric-compatible Liberation faces. Base-14 Times
|
||||
// depends on the viewer having the exact WinAnsi faces installed and
|
||||
// produced broken glyphs in some readers, so portability wins over the
|
||||
@@ -2116,7 +2122,12 @@ impl FontInputs {
|
||||
})
|
||||
.transpose()?;
|
||||
let monospace_dir = document_contains_monospace(model)
|
||||
.then(monospace_font_directory)
|
||||
.then(|| {
|
||||
subset_dir
|
||||
.clone()
|
||||
.map(Ok)
|
||||
.unwrap_or_else(monospace_font_directory)
|
||||
})
|
||||
.transpose()?;
|
||||
let monospace_fonts = if let Some(mono_dir) = &monospace_dir {
|
||||
Some(
|
||||
@@ -2127,7 +2138,12 @@ impl FontInputs {
|
||||
None
|
||||
};
|
||||
let sans_dir = document_contains_sans(model)
|
||||
.then(sans_font_directory)
|
||||
.then(|| {
|
||||
subset_dir
|
||||
.clone()
|
||||
.map(Ok)
|
||||
.unwrap_or_else(sans_font_directory)
|
||||
})
|
||||
.transpose()?;
|
||||
let sans_fonts = if let Some(sans_dir) = &sans_dir {
|
||||
Some(
|
||||
@@ -3107,6 +3123,83 @@ fn builtin_safe_char(ch: char) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
fn subset_safe_char(ch: char) -> bool {
|
||||
let code = ch as u32;
|
||||
matches!(code,
|
||||
0x09..=0x0A
|
||||
| 0x20..=0x7E
|
||||
| 0xA0..=0x24F
|
||||
| 0x2B0..=0x2FF
|
||||
| 0x370..=0x3FF
|
||||
| 0x400..=0x4FF
|
||||
| 0x1E00..=0x1EFF
|
||||
| 0x2000..=0x206F
|
||||
| 0x2070..=0x209F
|
||||
| 0x20A0..=0x20CF
|
||||
| 0x2100..=0x214F
|
||||
| 0x2190..=0x21FF
|
||||
| 0x2200..=0x22FF
|
||||
| 0x25A0..=0x25FF
|
||||
| 0xF0000..=0xFFFFF)
|
||||
}
|
||||
|
||||
fn document_fits_subset_fonts(model: &DocumentModel) -> bool {
|
||||
let text_fits = |text: &str| text.chars().all(subset_safe_char);
|
||||
let runs_fit = |runs: &[Run]| {
|
||||
runs.iter()
|
||||
.filter(|run| run.math.is_none())
|
||||
.all(|run| text_fits(&run.text))
|
||||
};
|
||||
if !(text_fits(&model.title)
|
||||
&& text_fits(&model.author)
|
||||
&& text_fits(&model.date)
|
||||
&& runs_fit(&model.title_runs))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if !model.footnotes.iter().all(|note| runs_fit(¬e.runs)) {
|
||||
return false;
|
||||
}
|
||||
model.blocks.iter().all(|block| match block {
|
||||
Block::Paragraph(runs) | Block::Heading { runs, .. } => runs_fit(runs),
|
||||
Block::List(list) => {
|
||||
let mut stack = vec![list];
|
||||
while let Some(current) = stack.pop() {
|
||||
if !current.items.iter().all(|item| runs_fit(&item.runs)) {
|
||||
return false;
|
||||
}
|
||||
stack.extend(current.items.iter().flat_map(|item| item.nested.iter()));
|
||||
}
|
||||
true
|
||||
}
|
||||
Block::Table { rows, caption, .. } => {
|
||||
rows.iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.all(|run| text_fits(&run.text))
|
||||
&& caption.as_ref().is_none_or(|runs| runs_fit(runs))
|
||||
}
|
||||
Block::Image { caption, .. } => caption.as_ref().is_none_or(|runs| runs_fit(runs)),
|
||||
Block::FramedBox { runs, .. } | Block::Centered { runs } => runs_fit(runs),
|
||||
Block::HorizontalRule { .. } => true,
|
||||
Block::Bibliography(items) => items
|
||||
.iter()
|
||||
.flat_map(|(_, runs)| runs)
|
||||
.all(|run| text_fits(&run.text)),
|
||||
Block::Math { .. } => true,
|
||||
Block::VerticalKern(_) | Block::VerticalGlue(_) | Block::Penalty(_) | Block::PageBreak => {
|
||||
true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn subset_font_directory() -> Option<PathBuf> {
|
||||
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/fonts-subset");
|
||||
dir.join("LiberationSerif-Regular.ttf")
|
||||
.is_file()
|
||||
.then_some(dir)
|
||||
}
|
||||
|
||||
fn document_needs_embedded_latin(model: &DocumentModel) -> bool {
|
||||
let text_needs_embedding = |text: &str| text.chars().any(|ch| !builtin_safe_char(ch));
|
||||
let runs_need_embedding = |runs: &[Run]| {
|
||||
|
||||
Reference in new issue
Block a user