Write PDF interword spaces with actual device fonts
This commit is contained in:
1 parent
b93d5f55da
commit
514a2d3344
24 files changed
+664
-65
No files matched your search
@@ -25,6 +25,13 @@ pub struct NativeFontPaint {
|
||||
}
|
||||
|
||||
impl NativeFontPaint {
|
||||
pub(crate) fn has_pdf_space(&self, font: &NativeFont) -> bool {
|
||||
// pdfTeX requires the space name in an actual map re-encoding.
|
||||
matches!(self.program, NativeFontProgram::Type1(_))
|
||||
&& self.resources.len() == 3
|
||||
&& self.encoding[32] == "space"
|
||||
&& font.metrics().width(' ') > 65_782
|
||||
}
|
||||
pub(crate) fn type1_resource(&self) -> Option<&crate::resources::ResourceVersion> {
|
||||
match &self.program {
|
||||
NativeFontProgram::Type1(_) => {
|
||||
@@ -216,6 +223,7 @@ fn verify_resource(
|
||||
Ok(resource)
|
||||
}
|
||||
|
||||
const SPACE_FONT_ID: u32 = u32::MAX;
|
||||
const MAX_TYPE1_PROGRAMS: usize = 16;
|
||||
const MAX_CACHED_PFB_BYTES: usize = 256 * 1024;
|
||||
|
||||
@@ -283,15 +291,61 @@ impl NativeFontCache {
|
||||
pages: &mut [crate::render::DisplayList],
|
||||
files: &mut impl crate::execution::SourceProvider,
|
||||
) -> Result<(), String> {
|
||||
if let Some(mut expanded) = virtual_paint::expand_pages(pages, files)? {
|
||||
resolve_physical_fonts(&mut expanded, files, self)?;
|
||||
for (original, resolved) in pages.iter_mut().zip(expanded) {
|
||||
*original = resolved;
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
resolve_physical_fonts(pages, files, self)
|
||||
let expanded = virtual_paint::expand_pages(pages, files)?;
|
||||
if expanded.is_none() && !pages.iter().any(|page| page.pdf_text_spaces.is_some()) {
|
||||
return resolve_physical_fonts(pages, files, self);
|
||||
}
|
||||
let mut resolved = expanded.unwrap_or_else(|| pages.to_vec());
|
||||
resolve_physical_fonts(&mut resolved, files, self)?;
|
||||
crate::pdf::prepare_text_spaces(&mut resolved, |font, device| {
|
||||
self.space_font(font, device, files)
|
||||
})?;
|
||||
for (original, resolved) in pages.iter_mut().zip(resolved) {
|
||||
*original = resolved;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn space_font(
|
||||
&self,
|
||||
request: &crate::pdf_device::text_spaces::SpaceFontRequest,
|
||||
device: Option<crate::pdf_device::PdfPageData>,
|
||||
files: &mut impl crate::execution::SourceProvider,
|
||||
) -> Result<crate::pdf_device::text_spaces::ResolvedSpaceFont, String> {
|
||||
use crate::execution::{CommandName, FontCatalog, FontIntegers};
|
||||
use crate::pdf_device::text_spaces::ResolvedSpaceFont;
|
||||
use crate::render::{DisplayItem, DisplayList, DisplayUnits};
|
||||
let mut catalog = FontCatalog::default();
|
||||
let id = catalog.load(
|
||||
files,
|
||||
&request.name,
|
||||
&request.from,
|
||||
crate::tfm::FontSize::Design,
|
||||
CommandName::Control(String::new()),
|
||||
FontIntegers { hyphen: 45, skew: -1 },
|
||||
)?;
|
||||
let mut font = (**catalog.glyph_font(id)?).clone();
|
||||
font.id = FontId::for_device(SPACE_FONT_ID);
|
||||
let font = Arc::new(font);
|
||||
let mut page = DisplayList {
|
||||
units: DisplayUnits::TeX,
|
||||
pdf: device,
|
||||
items: vec![DisplayItem::NativeGlyph {
|
||||
x: 0,
|
||||
y: 0,
|
||||
size: font.size_sp(),
|
||||
font: font.clone(),
|
||||
slot: b' ',
|
||||
character: ' ',
|
||||
paint: None,
|
||||
}],
|
||||
..DisplayList::default()
|
||||
};
|
||||
resolve_physical_fonts(std::slice::from_mut(&mut page), files, self)?;
|
||||
let DisplayItem::NativeGlyph { paint: Some(paint), .. } = page.items.pop().unwrap() else {
|
||||
unreachable!("space font slot must be resolved");
|
||||
};
|
||||
Ok(ResolvedSpaceFont { font, paint })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,7 +396,12 @@ fn resolve_physical_fonts(
|
||||
.map(|p| p.file.clone());
|
||||
let mut cache: BTreeMap<_, (NativeFontPaint, Arc<DecodedType1>)> = BTreeMap::new();
|
||||
for (id, (font, slots)) in used {
|
||||
let entry = map_entry(&map.data, &font.name)?;
|
||||
let entry = if id.index() == SPACE_FONT_ID && font.name == "pdftexspace" {
|
||||
// The pdfTeX device adds its own map entry on first use.
|
||||
Some(MapEntry { file: "pdftexspace.pfb".into(), encoding: None })
|
||||
} else {
|
||||
map_entry(&map.data, &font.name)?
|
||||
};
|
||||
let Some(entry) = entry else {
|
||||
let (resolution, digits) = if let Some(device) = &device {
|
||||
(device.pk_resolution, device.decimal_digits)
|
||||
|
||||
Reference in new issue
Block a user