Render legacy XeLaTeX composite Type 1 fonts
This commit is contained in:
1 parent
ff52a9f66b
commit
e7ab132520
11 files changed
+497
-116
No files matched your search
@@ -15,18 +15,18 @@ pub(crate) struct CompositeFont {
|
||||
pub(crate) registry: String,
|
||||
pub(crate) ordering: String,
|
||||
pub(crate) supplement: u32,
|
||||
slots: Vec<CompositeSlot>,
|
||||
pub(super) slots: Vec<CompositeSlot>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) vertical: Option<[i32; 2]>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
struct CompositeSlot {
|
||||
cid: u16,
|
||||
advance: u32,
|
||||
unicode: Option<String>,
|
||||
pub(super) struct CompositeSlot {
|
||||
pub(super) cid: u16,
|
||||
pub(super) advance: u32,
|
||||
pub(super) unicode: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
vertical: Option<[i32; 3]>,
|
||||
pub(super) vertical: Option<[i32; 3]>,
|
||||
}
|
||||
|
||||
impl CompositeFont {
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
//! Resolve XDV character codes through an actual Type 1 CharStrings dictionary.
|
||||
use super::{
|
||||
NativeFontCache, NativeFontPaint, NativeFontProgram, actual_resource,
|
||||
composite::{Codes, CompositeFont, CompositeSlot},
|
||||
map::{MapEntry, XdvEncoding},
|
||||
};
|
||||
use crate::{
|
||||
execution::SourceProvider, pdf::opentype_unicode::XdvGlyphNames, resources::ResourceKind,
|
||||
type1::Type1Font,
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CompositeType1Program {
|
||||
pub(crate) font: Arc<Type1Font>,
|
||||
pub(crate) mapping: CompositeFont,
|
||||
pub(crate) default_width: u32,
|
||||
}
|
||||
|
||||
pub(super) fn resolve(
|
||||
files: &mut impl SourceProvider,
|
||||
cache: &NativeFontCache,
|
||||
outline: &MapEntry,
|
||||
file: &str,
|
||||
slots: BTreeSet<u8>,
|
||||
encoding: &XdvEncoding,
|
||||
codes: Codes,
|
||||
) -> Result<NativeFontPaint, String> {
|
||||
let from = outline.from.to_string_lossy();
|
||||
let resource = actual_resource(files, ResourceKind::Type1, file, &from)?;
|
||||
// Slant and extension belong to the XDV text matrix for these fonts.
|
||||
let decoded = cache.type1(&resource, Default::default())?;
|
||||
let mut resources: Vec<_> = outline.version.iter().cloned().collect();
|
||||
let program_index = Some(resources.len());
|
||||
resources.push(resource.version);
|
||||
let (glyph_text, lists) = XdvGlyphNames::load_tracked(files, &from)?;
|
||||
resources.extend(lists);
|
||||
if let Codes::Custom(map) = &codes {
|
||||
resources.extend(map.resources.iter().cloned());
|
||||
}
|
||||
|
||||
let outlines = &decoded.outlines;
|
||||
let order = outlines.glyph_order();
|
||||
let mut by_scalar = BTreeMap::new();
|
||||
let text: Vec<_> = order.iter().map(|name| glyph_text.text(name)).collect();
|
||||
if matches!(codes, Codes::Unicode) {
|
||||
for (cid, name) in order.iter().enumerate().skip(1) {
|
||||
if !name.contains('.') {
|
||||
for ch in glyph_text.type1_codes(name) {
|
||||
by_scalar.insert(ch, cid as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let vertical = encoding.name == "Identity-V"
|
||||
|| (matches!(codes, Codes::Unicode) && encoding.vertical)
|
||||
|| matches!(&codes, Codes::Custom(map) if map.is_vertical());
|
||||
let mut names = Vec::with_capacity(256);
|
||||
let mut cells = Vec::with_capacity(256);
|
||||
let mut glyph_paths = BTreeMap::new();
|
||||
let mut measured = BTreeMap::new();
|
||||
let (_, default_width) = outlines.glyph_path_and_width(".notdef")?;
|
||||
let width = |value: i64| {
|
||||
value
|
||||
.saturating_add(5)
|
||||
.div_euclid(10)
|
||||
.clamp(0, i64::from(u32::MAX)) as u32
|
||||
};
|
||||
for slot in 0..=255u8 {
|
||||
let code = encoding.prefix | u32::from(slot);
|
||||
let cid = match &codes {
|
||||
Codes::Unicode => char::from_u32(code).and_then(|ch| by_scalar.get(&ch).copied()),
|
||||
Codes::Identity => u16::try_from(code).ok(),
|
||||
Codes::Custom(map) => map.cid(code),
|
||||
}
|
||||
.filter(|&cid| usize::from(cid) < order.len())
|
||||
.unwrap_or(0);
|
||||
let name = &order[usize::from(cid)];
|
||||
let mut cell = CompositeSlot {
|
||||
cid,
|
||||
unicode: text[usize::from(cid)].clone(),
|
||||
..Default::default()
|
||||
};
|
||||
if slots.contains(&slot) {
|
||||
let (path, advance) = match measured.entry(cid) {
|
||||
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
|
||||
std::collections::btree_map::Entry::Vacant(entry) => {
|
||||
entry.insert(outlines.glyph_path_and_width(name)?)
|
||||
}
|
||||
};
|
||||
glyph_paths.insert(slot, path.clone());
|
||||
cell.advance = width(*advance);
|
||||
}
|
||||
cells.push(cell);
|
||||
names.push(name.clone());
|
||||
}
|
||||
let mapping = CompositeFont {
|
||||
registry: "Adobe".into(),
|
||||
ordering: "Identity".into(),
|
||||
supplement: 0,
|
||||
slots: cells,
|
||||
vertical: vertical.then_some([1000, 880]),
|
||||
};
|
||||
Ok(NativeFontPaint {
|
||||
program: NativeFontProgram::CompositeType1(CompositeType1Program {
|
||||
font: Arc::clone(&decoded.program),
|
||||
mapping,
|
||||
default_width: width(default_width),
|
||||
}),
|
||||
encoding: names,
|
||||
glyph_paths,
|
||||
bbox: decoded.bbox,
|
||||
stem_v: decoded.stem_v,
|
||||
subset: true,
|
||||
descriptor_flags: None,
|
||||
xdv_descriptor: Some(
|
||||
decoded
|
||||
.xdv_descriptor
|
||||
.get_or_init(|| outlines.xdv_descriptor())
|
||||
.clone()?,
|
||||
),
|
||||
xdv_widths: BTreeMap::new(),
|
||||
xdv_matrix: outline.xdv_text_matrix(),
|
||||
xdv_unicode: None,
|
||||
resources,
|
||||
program_index,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user