Resolve imported PDF CFF device fonts from real OpenType faces
This commit is contained in:
1 parent
84d6f3ae51
commit
82e54894c4
14 files changed
+326
-38
No files matched your search
@@ -285,6 +285,15 @@ pub trait SourceProvider {
|
||||
) -> Result<Option<crate::resources::NamedFont>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// Resolve an exact PDF device name using the requested CFF charset kind.
|
||||
fn pdf_cff(
|
||||
&mut self,
|
||||
_name: &str,
|
||||
_from: &str,
|
||||
_cid_keyed: bool,
|
||||
) -> Result<Option<crate::resources::NamedFont>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// An exact lookup that can report a missing font file before trying another format.
|
||||
fn optional_font_file(
|
||||
&mut self, kind: crate::resources::ResourceKind, name: &str, from: &str,
|
||||
|
||||
@@ -324,6 +324,16 @@ impl NativeFontCache {
|
||||
truetype::device_program(files, name, from)
|
||||
}
|
||||
|
||||
/// Resolve the actual CFF table for a PDF device font.
|
||||
pub fn pdf_device_cff(
|
||||
files: &mut impl crate::execution::SourceProvider,
|
||||
name: &str,
|
||||
from: &str,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
cff::device_program(files, name, from, cid_keyed)
|
||||
}
|
||||
|
||||
// The caller reads the actual resource and verifies its bytes on this
|
||||
// revision before asking for the decoded representation.
|
||||
fn type1(&self, resource: &LoadedResource, transform: Type1Transform) -> Result<Arc<DecodedType1>, String> {
|
||||
|
||||
@@ -21,6 +21,34 @@ impl OpenTypeCffProgram {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn device_program(
|
||||
files: &mut impl SourceProvider,
|
||||
name: &str,
|
||||
from: &str,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
let Some(selected) = files.pdf_cff(name, from, cid_keyed)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let resource = super::verify_resource(selected.resource, ResourceKind::OpenType, name)?;
|
||||
use crate::resources::{PdfSfntKind, cff_program, exact_pdf_sfnt_name};
|
||||
if exact_pdf_sfnt_name(
|
||||
&resource.data,
|
||||
selected.index,
|
||||
name,
|
||||
PdfSfntKind::Cff { cid_keyed },
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
return Err(format!(
|
||||
"PDF device font {name} does not match the selected CFF face"
|
||||
));
|
||||
}
|
||||
let bytes = cff_program(&resource.data, selected.index, cid_keyed)
|
||||
.ok_or_else(|| format!("PDF device font {name} has an invalid CFF program"))?;
|
||||
Ok(Some(bytes.into()))
|
||||
}
|
||||
|
||||
pub(super) fn resolve(
|
||||
files: &mut impl SourceProvider,
|
||||
outlines: &mut OutlineFiles,
|
||||
|
||||
@@ -87,7 +87,9 @@ pub(super) fn device_program(
|
||||
) -> Result<Option<Arc<[u8]>>, String> {
|
||||
let Some(selected) = files.pdf_truetype(name, from)? else { return Ok(None) };
|
||||
let resource = super::verify_resource(selected.resource, ResourceKind::TrueType, name)?;
|
||||
if crate::resources::exact_truetype_name(&resource.data, selected.index, name).is_none() {
|
||||
if crate::resources::exact_pdf_sfnt_name(
|
||||
&resource.data, selected.index, name, crate::resources::PdfSfntKind::TrueType,
|
||||
).is_none() {
|
||||
return Err(format!("PDF device font {name} does not match the selected TrueType face"));
|
||||
}
|
||||
let data = if resource.data.starts_with(b"ttcf") {
|
||||
|
||||
@@ -61,6 +61,8 @@ pub struct PdfImage {
|
||||
device_type1_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
device_truetype_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
device_cid_cff_fonts: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pdftex_source: Option<imports::SourceGraph>,
|
||||
}
|
||||
@@ -426,6 +428,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
}
|
||||
}
|
||||
@@ -567,6 +570,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
}
|
||||
}
|
||||
@@ -618,6 +622,7 @@ impl PdfImage {
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
})
|
||||
}
|
||||
@@ -896,8 +901,7 @@ impl PdfImage {
|
||||
Vec::new()
|
||||
};
|
||||
let pdftex_source = pdftex.then(|| imports::SourceGraph::capture(&mut graph)).transpose()?;
|
||||
let (device_type1_fonts, device_truetype_fonts) =
|
||||
device_fonts::capture(&doc, inherited(b"Resources"));
|
||||
let fonts = device_fonts::capture(&doc, inherited(b"Resources"));
|
||||
Ok((
|
||||
Self {
|
||||
version: resource.version,
|
||||
@@ -909,8 +913,9 @@ impl PdfImage {
|
||||
objects: graph.bodies,
|
||||
color_space: None,
|
||||
imported_fonts,
|
||||
device_type1_fonts,
|
||||
device_truetype_fonts,
|
||||
device_type1_fonts: fonts.type1,
|
||||
device_truetype_fonts: fonts.truetype,
|
||||
device_cid_cff_fonts: fonts.cid_cff,
|
||||
pdftex_source,
|
||||
},
|
||||
[left, bottom],
|
||||
@@ -927,6 +932,11 @@ impl PdfImage {
|
||||
&self.device_truetype_fonts
|
||||
}
|
||||
|
||||
/// Unembedded CIDFontType0 names recorded from this selected PDF page.
|
||||
pub fn device_cid_cff_fonts(&self) -> &[String] {
|
||||
&self.device_cid_cff_fonts
|
||||
}
|
||||
|
||||
pub(crate) fn extent(&self) -> [f64; 2] {
|
||||
self.extent.map(f64::from_bits)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,19 @@ use std::collections::{BTreeSet, HashSet};
|
||||
struct Names {
|
||||
type1: BTreeSet<String>,
|
||||
truetype: BTreeSet<String>,
|
||||
cid_cff: BTreeSet<String>,
|
||||
}
|
||||
|
||||
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> (Vec<String>, Vec<String>) {
|
||||
#[derive(Default)]
|
||||
pub(super) struct DeviceFontNames {
|
||||
pub(super) type1: Vec<String>,
|
||||
pub(super) truetype: Vec<String>,
|
||||
pub(super) cid_cff: Vec<String>,
|
||||
}
|
||||
|
||||
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> DeviceFontNames {
|
||||
let Some(resources) = resources.and_then(|value| resolved(doc, value).ok()) else {
|
||||
return (Vec::new(), Vec::new());
|
||||
return DeviceFontNames::default();
|
||||
};
|
||||
let mut pending = vec![resources];
|
||||
let mut seen = HashSet::new();
|
||||
@@ -70,10 +78,11 @@ pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> (Vec<String
|
||||
}
|
||||
}
|
||||
}
|
||||
(
|
||||
names.type1.into_iter().collect(),
|
||||
names.truetype.into_iter().collect(),
|
||||
)
|
||||
DeviceFontNames {
|
||||
type1: names.type1.into_iter().collect(),
|
||||
truetype: names.truetype.into_iter().collect(),
|
||||
cid_cff: names.cid_cff.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Object> {
|
||||
@@ -106,10 +115,12 @@ fn capture_font<'a>(
|
||||
.first()
|
||||
.and_then(|value| resolved(doc, value).ok())
|
||||
.and_then(|value| value.as_dict().ok())
|
||||
&& get(doc, descendant, b"Subtype").and_then(|value| value.as_name().ok())
|
||||
== Some(b"CIDFontType2")
|
||||
{
|
||||
capture_name(doc, descendant, &mut names.truetype);
|
||||
match get(doc, descendant, b"Subtype").and_then(|value| value.as_name().ok()) {
|
||||
Some(b"CIDFontType2") => capture_name(doc, descendant, &mut names.truetype),
|
||||
Some(b"CIDFontType0") => capture_name(doc, descendant, &mut names.cid_cff),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(b"Type3") => {
|
||||
|
||||
@@ -149,6 +149,7 @@ pub(super) fn read(
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
device_truetype_fonts: Vec::new(),
|
||||
device_cid_cff_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
restored_pdf_origin: None,
|
||||
},
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
mod deferred;
|
||||
mod named_fonts;
|
||||
pub use named_fonts::{FontStyle, NamedFont};
|
||||
pub(crate) use named_fonts::exact_truetype_name;
|
||||
pub(crate) use named_fonts::{PdfSfntKind, cff_program, exact_pdf_sfnt_name};
|
||||
mod project;
|
||||
pub use deferred::{
|
||||
DeferredProbe, DirectoryAbsence, GeneratedLookup, GeneratedLookupOrigin, GeneratedLookupRole,
|
||||
|
||||
@@ -85,9 +85,43 @@ impl NamedFace {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exact_truetype_name(data: &[u8], index: u32, requested: &str) -> Option<String> {
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum PdfSfntKind {
|
||||
TrueType,
|
||||
Cff { cid_keyed: bool },
|
||||
}
|
||||
|
||||
impl PdfSfntKind {
|
||||
fn resource_kind(self) -> ResourceKind {
|
||||
match self {
|
||||
Self::TrueType => ResourceKind::TrueType,
|
||||
Self::Cff { .. } => ResourceKind::OpenType,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn cff_program(data: &[u8], index: u32, cid_keyed: bool) -> Option<&[u8]> {
|
||||
let face = ttf_parser::RawFace::parse(data, index).ok()?;
|
||||
let bytes = face.table(ttf_parser::Tag::from_bytes(b"CFF "))?;
|
||||
let cff = read_fonts::ps::cff::CffFontRef::new_cff(bytes, 0, None).ok()?;
|
||||
(cff.is_cid() == cid_keyed).then_some(bytes)
|
||||
}
|
||||
|
||||
pub(crate) fn exact_pdf_sfnt_name(
|
||||
data: &[u8],
|
||||
index: u32,
|
||||
requested: &str,
|
||||
kind: PdfSfntKind,
|
||||
) -> Option<String> {
|
||||
let face = ttf_parser::Face::parse(data, index).ok()?;
|
||||
face.tables().glyf.as_ref()?;
|
||||
match kind {
|
||||
PdfSfntKind::TrueType => {
|
||||
face.tables().glyf.as_ref()?;
|
||||
}
|
||||
PdfSfntKind::Cff { cid_keyed } => {
|
||||
cff_program(data, index, cid_keyed)?;
|
||||
}
|
||||
}
|
||||
let mut names = face.names().into_iter();
|
||||
names
|
||||
.any(|name| {
|
||||
@@ -220,18 +254,37 @@ impl FileWorld {
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
self.read_pdf_sfnt(name, from, PdfSfntKind::TrueType)
|
||||
}
|
||||
|
||||
/// Resolve a versioned OpenType face with the requested CFF charset kind.
|
||||
pub fn read_pdf_cff(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
cid_keyed: bool,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
self.read_pdf_sfnt(name, from, PdfSfntKind::Cff { cid_keyed })
|
||||
}
|
||||
|
||||
fn read_pdf_sfnt(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
kind: PdfSfntKind,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
let mut candidates = Vec::new();
|
||||
let result = self.select_pdf_truetype(name, from, &mut candidates);
|
||||
let result = self.select_pdf_sfnt(name, from, kind, &mut candidates);
|
||||
self.trace.lookups.push(ResourceLookup {
|
||||
kind: ResourceKind::TrueType,
|
||||
kind: kind.resource_kind(),
|
||||
name: name.into(),
|
||||
from: from.into(),
|
||||
candidates,
|
||||
resolved: result
|
||||
.as_ref()
|
||||
.ok()
|
||||
.and_then(|font| font.as_ref())
|
||||
.and_then(|result| result.as_ref())
|
||||
.map(|font| font.resource.version.path.clone()),
|
||||
lookup_path: None,
|
||||
error: result.as_ref().err().cloned(),
|
||||
@@ -239,16 +292,23 @@ impl FileWorld {
|
||||
result
|
||||
}
|
||||
|
||||
fn select_pdf_truetype(
|
||||
fn select_pdf_sfnt(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &Path,
|
||||
kind: PdfSfntKind,
|
||||
candidates: &mut Vec<PathBuf>,
|
||||
) -> Result<Option<NamedFont>, String> {
|
||||
let mut scoped_paths = self.paths(ResourceKind::TrueType, from);
|
||||
scoped_paths.extend(self.paths(ResourceKind::OpenType, from));
|
||||
let search_order = match kind {
|
||||
PdfSfntKind::TrueType => [ResourceKind::TrueType, ResourceKind::OpenType],
|
||||
PdfSfntKind::Cff { .. } => [ResourceKind::OpenType, ResourceKind::TrueType],
|
||||
};
|
||||
let mut seen = BTreeSet::new();
|
||||
for (entry, _) in scoped_paths {
|
||||
for (entry, _) in search_order
|
||||
.into_iter()
|
||||
.flat_map(|kind| self.paths(kind, from))
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
let SearchPath::Directory {
|
||||
path,
|
||||
recursive,
|
||||
@@ -298,7 +358,7 @@ impl FileWorld {
|
||||
continue;
|
||||
}
|
||||
for index in 0..count as u32 {
|
||||
if let Some(full_name) = exact_truetype_name(&data.data, index, name) {
|
||||
if let Some(full_name) = exact_pdf_sfnt_name(&data.data, index, name, kind) {
|
||||
let resource = self.read_path(path)?;
|
||||
return Ok(Some(NamedFont {
|
||||
resource,
|
||||
@@ -333,7 +393,7 @@ impl FileWorld {
|
||||
continue;
|
||||
};
|
||||
let data = self.input_file(path.clone())?;
|
||||
if let Some(full_name) = exact_truetype_name(&data.data, index, name) {
|
||||
if let Some(full_name) = exact_pdf_sfnt_name(&data.data, index, name, kind) {
|
||||
let resource = self.read_path(path)?;
|
||||
return Ok(Some(NamedFont {
|
||||
resource,
|
||||
|
||||
@@ -819,6 +819,38 @@ letterspace-fresh.log 和 device-pdftex-spaced-moved.log。
|
||||
[原版字距虚拟字体构造](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L16400-L16492)
|
||||
[原版格式所写字体字段](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L30627-L30696)
|
||||
|
||||
### 导入 PDF 的 OpenType CFF 设备字体
|
||||
|
||||
复用实际 SFNT 名称查找及版本化资源。未嵌入的 Type1 字典先
|
||||
查找同名 PFB/PFA,随后查找按名称编码的 CFF。CIDFontType0
|
||||
查找实际 CID-keyed CFF。预览追加真实 FontFile3 程序,分别使用
|
||||
Type1C 和 CIDFontType0C。原 PDF 的显式编码、CID 及字形位置
|
||||
进入现有渲染器。导出遵循原版设备字体引用。
|
||||
[PDF 字体程序的组织方式](https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.6.pdf)
|
||||
|
||||
三项定向回归通过,包括已有 Type 1 和 TrueType 预览路径。八组
|
||||
真实字形对照覆盖 Latin Modern 内建编码、外部重编码、OTC 非首
|
||||
字面、XeTeX,以及 Harano Aji Mincho 的 Adobe-Japan1 字符。
|
||||
常规、重编码、CID 三组的设备字体和整嵌对照,另加 OTC 与独立
|
||||
OTF 的对照,轮廓和位置均完全相同。日文字形「日」「本」「語」
|
||||
实际使用 CID 3284、3722、1952。
|
||||
[Harano Aji Fonts 20250811](https://github.com/trueroad/HaranoAjiFonts/tree/20250811)
|
||||
|
||||
四页会话更换真实字形命令时导出 PDF 字节相同。更换字形、
|
||||
移动插图和恢复资源都只更新第 2 页。后两次各复用 109 步。
|
||||
成功修订的 PDF、测量页面、SVG、来源映射和 aux 与独立全量
|
||||
一致。缺字体时编辑器保留上一版预览和 aux。
|
||||
|
||||
回执在 target/p3-imported-cff。receipt.json 的 SHA-256 为
|
||||
3cbabd540d9d923327995db60a9a305dcc70fa32a0d52d80d6fb57a257aecff3,
|
||||
revision.log 为 f3872ba1c987dc93c71314c1d35c84fd2320c096540fe7e70420115869fdb6c7。
|
||||
下一入口为 XeTeX 直接排版 CID CFF 时的导出语义。原样 Harano
|
||||
Aji 小样的两个词框和 144 DPI 灰度像素一致,xdvipdfmx 保留
|
||||
Adobe-Japan1-7,原生重编号并写成 Adobe-Identity-0。另有子集名
|
||||
和 ToUnicode 差异。证据在 next-xetex-cid-cff。Source Serif
|
||||
4.005 CFF2 的真实探针亦保留于 next-xetex-cff2,XeTeX 出 XDV,
|
||||
两个 PDF 导出均报告不支持。
|
||||
|
||||
### 导入 PDF 的 TrueType 设备字体
|
||||
|
||||
选页资源中的 TrueType 和 CIDFontType2 按 SFNT 内部的 PostScript
|
||||
|
||||
@@ -166,6 +166,16 @@ impl SourceProvider for RecordingHost<'_> {
|
||||
})).map_err(Clone::clone)
|
||||
})
|
||||
}
|
||||
fn pdf_cff(
|
||||
&mut self, name: &str, from: &str, cid_keyed: bool,
|
||||
) -> Result<Option<texrra_tex_core::resources::NamedFont>, String> {
|
||||
let (name, from) = (name.to_owned(), from.to_owned());
|
||||
call!(self, pdf_cff(&name, &from, cid_keyed); |result| {
|
||||
result.as_ref().map(|result| result.as_ref().map(|font| {
|
||||
(font.resource.version.clone(), font.index, font.full_name.clone())
|
||||
})).map_err(Clone::clone)
|
||||
})
|
||||
}
|
||||
fn optional_font_file(&mut self, kind: ResourceKind, name: &str, from: &str) -> Result<Option<LoadedResource>, String> {
|
||||
let (name, from) = (name.to_owned(), from.to_owned());
|
||||
call!(self, optional_font_file(kind, &name, &from); |result| {
|
||||
|
||||
@@ -1685,6 +1685,15 @@ impl SourceProvider for Files {
|
||||
}
|
||||
Ok(selected)
|
||||
}
|
||||
fn pdf_cff(
|
||||
&mut self, name: &str, from: &str, cid_keyed: bool,
|
||||
) -> Result<Option<texrra_tex_core::resources::NamedFont>, String> {
|
||||
let selected = self.world.read_pdf_cff(name, Path::new(from), cid_keyed)?;
|
||||
if let Some(font) = &selected {
|
||||
self.observe_resource(ResourceKind::OpenType, &font.resource);
|
||||
}
|
||||
Ok(selected)
|
||||
}
|
||||
fn optional_font_file(&mut self, kind: ResourceKind, name: &str, from: &str) -> Result<Option<LoadedResource>, String> {
|
||||
let resource = self.world.read_optional(kind, name, Path::new(from))?;
|
||||
if let Some(resource) = &resource {
|
||||
|
||||
@@ -17,6 +17,8 @@ mod overlay;
|
||||
pub(crate) struct PreviewFonts {
|
||||
type1: BTreeMap<String, Arc<Type1Font>>,
|
||||
truetype: BTreeMap<String, Arc<[u8]>>,
|
||||
cff: BTreeMap<String, Arc<[u8]>>,
|
||||
cid_cff: BTreeMap<String, Arc<[u8]>>,
|
||||
missing: bool,
|
||||
conflict: Option<String>,
|
||||
}
|
||||
@@ -40,6 +42,7 @@ impl PreviewFonts {
|
||||
let mut visited = BTreeSet::new();
|
||||
let mut required_type1: BTreeSet<(&Path, &str)> = BTreeSet::new();
|
||||
let mut required_truetype = BTreeSet::new();
|
||||
let mut required_cid_cff = BTreeSet::new();
|
||||
while let Some(list) = pending.pop() {
|
||||
for item in &list.items {
|
||||
match item {
|
||||
@@ -70,6 +73,12 @@ impl PreviewFonts {
|
||||
.iter()
|
||||
.map(|name| (source.version.path.as_path(), name.as_str())),
|
||||
);
|
||||
required_cid_cff.extend(
|
||||
source
|
||||
.device_cid_cff_fonts()
|
||||
.iter()
|
||||
.map(|name| (source.version.path.as_path(), name.as_str())),
|
||||
);
|
||||
}
|
||||
DisplayItem::PdfForm { form, .. } => {
|
||||
if visited.insert(form.number)
|
||||
@@ -83,14 +92,26 @@ impl PreviewFonts {
|
||||
}
|
||||
}
|
||||
for (from, name) in required_type1 {
|
||||
let program = cache.pdf_device_type1(files, name, &from.to_string_lossy())?;
|
||||
record(
|
||||
&mut fonts.type1,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
let from = from.to_string_lossy();
|
||||
let program = cache.pdf_device_type1(files, name, &from)?;
|
||||
if let Some(program) = &program {
|
||||
record(
|
||||
&mut fonts.type1,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
Some(program),
|
||||
);
|
||||
} else {
|
||||
let program = NativeFontCache::pdf_device_cff(files, name, &from, false)?;
|
||||
record(
|
||||
&mut fonts.cff,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
}
|
||||
for (from, name) in required_truetype {
|
||||
let program =
|
||||
@@ -103,11 +124,36 @@ impl PreviewFonts {
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
for (from, name) in required_cid_cff {
|
||||
let program =
|
||||
NativeFontCache::pdf_device_cff(files, name, &from.to_string_lossy(), true)?;
|
||||
record(
|
||||
&mut fonts.cid_cff,
|
||||
&mut fonts.missing,
|
||||
&mut fonts.conflict,
|
||||
name,
|
||||
program.as_ref(),
|
||||
);
|
||||
}
|
||||
if let Some(name) = fonts
|
||||
.type1
|
||||
.keys()
|
||||
.find(|name| fonts.cff.contains_key(*name))
|
||||
{
|
||||
fonts.conflict = Some(name.clone());
|
||||
}
|
||||
Ok(fonts)
|
||||
}
|
||||
|
||||
pub(super) fn includes_device_fonts(&self) -> bool {
|
||||
self.missing || !self.type1.is_empty() || !self.truetype.is_empty()
|
||||
self.missing || !self.programs_empty()
|
||||
}
|
||||
|
||||
fn programs_empty(&self) -> bool {
|
||||
self.type1.is_empty()
|
||||
&& self.truetype.is_empty()
|
||||
&& self.cff.is_empty()
|
||||
&& self.cid_cff.is_empty()
|
||||
}
|
||||
|
||||
/// Attach the captured programs to a private copy for PDF-backed previews.
|
||||
@@ -117,7 +163,7 @@ impl PreviewFonts {
|
||||
"conflicting actual fonts for PDF preview device name {name}"
|
||||
));
|
||||
}
|
||||
if self.type1.is_empty() && self.truetype.is_empty() {
|
||||
if self.programs_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut document = Document::load_mem(pdf)
|
||||
@@ -162,11 +208,15 @@ impl PreviewFonts {
|
||||
(name.as_bytes(), stream)
|
||||
})
|
||||
.collect();
|
||||
let cff = cff_streams(&mut document, &self.cff, b"Type1C");
|
||||
let cid_cff = cff_streams(&mut document, &self.cid_cff, b"CIDFontType0C");
|
||||
let attachment = Attachment {
|
||||
names,
|
||||
descriptors,
|
||||
type1,
|
||||
truetype,
|
||||
cff,
|
||||
cid_cff,
|
||||
};
|
||||
let mut attached = false;
|
||||
let changes: Vec<_> = document
|
||||
@@ -190,6 +240,22 @@ impl PreviewFonts {
|
||||
}
|
||||
}
|
||||
|
||||
fn cff_streams<'a>(
|
||||
document: &mut Document,
|
||||
programs: &'a BTreeMap<String, Arc<[u8]>>,
|
||||
subtype: &[u8],
|
||||
) -> BTreeMap<&'a [u8], ObjectId> {
|
||||
programs
|
||||
.iter()
|
||||
.map(|(name, bytes)| {
|
||||
let mut dictionary = Dictionary::new();
|
||||
dictionary.set("Subtype", Object::Name(subtype.to_vec()));
|
||||
let stream = document.add_object(Stream::new(dictionary, bytes.to_vec()));
|
||||
(name.as_bytes(), stream)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn record<T: PartialEq + ?Sized>(
|
||||
programs: &mut BTreeMap<String, Arc<T>>,
|
||||
missing: &mut bool,
|
||||
@@ -215,6 +281,8 @@ struct Attachment<'a> {
|
||||
descriptors: BTreeMap<ObjectId, Dictionary>,
|
||||
type1: BTreeMap<&'a [u8], (&'a Arc<Type1Font>, ObjectId)>,
|
||||
truetype: BTreeMap<&'a [u8], ObjectId>,
|
||||
cff: BTreeMap<&'a [u8], ObjectId>,
|
||||
cid_cff: BTreeMap<&'a [u8], ObjectId>,
|
||||
}
|
||||
|
||||
impl Attachment<'_> {
|
||||
@@ -251,10 +319,19 @@ impl Attachment<'_> {
|
||||
};
|
||||
let (file_key, stream, bbox) = match name(b"Subtype") {
|
||||
Some(b"Type1") => {
|
||||
let Some((program, stream)) = self.type1.get(base) else {
|
||||
if let Some((program, stream)) = self.type1.get(base) {
|
||||
("FontFile", stream, Some(program.bbox))
|
||||
} else if let Some(stream) = self.cff.get(base) {
|
||||
("FontFile3", stream, None)
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Some(b"CIDFontType0") => {
|
||||
let Some(stream) = self.cid_cff.get(base) else {
|
||||
return false;
|
||||
};
|
||||
("FontFile", stream, Some(program.bbox))
|
||||
("FontFile3", stream, None)
|
||||
}
|
||||
Some(b"TrueType" | b"CIDFontType2") => {
|
||||
let Some(stream) = self.truetype.get(base) else {
|
||||
|
||||
@@ -2192,6 +2192,35 @@ fn mapped_opentype_cff_paints_reencoded_glyphs_and_embeds_the_original_table() {
|
||||
assert_eq!(embedded.content[0], 1);
|
||||
assert!(source.windows(embedded.content.len()).any(|bytes| bytes == embedded.content));
|
||||
|
||||
let mut included = pdf;
|
||||
for object in included.objects.values_mut() {
|
||||
if let Ok(dict) = object.as_dict_mut() {
|
||||
dict.remove(b"FontFile3");
|
||||
}
|
||||
}
|
||||
included.prune_objects();
|
||||
included.save(root.join("external.pdf")).unwrap();
|
||||
fs::write(root.join("import.tex"), br"\catcode123=1 \catcode125=2
|
||||
\pdfoutput=1 \pdfcompresslevel=0
|
||||
\pdfximage width180pt {external.pdf}
|
||||
\shipout\hbox{\pdfrefximage\pdflastximage}
|
||||
\end
|
||||
").unwrap();
|
||||
let mut imported = job.clone();
|
||||
imported.input = root.join("import.tex");
|
||||
imported.output = PathBuf::from("import.pdf");
|
||||
imported.world.paths.insert(ResourceKind::Type1, vec![SearchPath::directory(&root, false)]);
|
||||
let result = NativeCompiler::default().compile(imported).unwrap();
|
||||
let preview = result.preview_since(None).unwrap();
|
||||
assert!(preview.pages[0].svg.matches("<use").count() >= 2);
|
||||
assert!(result.resources.trace.reads.contains(
|
||||
&file_version(&root.join("lmroman10-regular.otf")).unwrap().unwrap(),
|
||||
));
|
||||
let published = lopdf::Document::load_mem(result.pdf.as_deref().unwrap()).unwrap();
|
||||
assert!(!published.objects.values().any(
|
||||
|object| object.as_dict().is_ok_and(|dict| dict.has(b"FontFile3"))
|
||||
));
|
||||
|
||||
fs::remove_file(root.join("lm-ec.enc")).unwrap();
|
||||
let error = compiler.compile_detailed(job).err().unwrap().error.to_string();
|
||||
assert!(error.contains("lm-ec.enc"), "{error}");
|
||||
|
||||
Reference in new issue
Block a user