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
@@ -613,15 +613,24 @@ fn write_type0_font(
|
||||
struct PageContent {
|
||||
bytes: Vec<u8>,
|
||||
origin: crate::pdf_device::DeviceOrigin,
|
||||
measuring: bool,
|
||||
}
|
||||
|
||||
impl PageContent {
|
||||
fn measuring() -> Self {
|
||||
Self { measuring: true, ..Self::default() }
|
||||
}
|
||||
|
||||
fn push_str(&mut self, text: &str) {
|
||||
self.bytes.extend_from_slice(text.as_bytes());
|
||||
if !self.measuring {
|
||||
self.bytes.extend_from_slice(text.as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, byte: u8) {
|
||||
self.bytes.push(byte);
|
||||
if !self.measuring {
|
||||
self.bytes.push(byte);
|
||||
}
|
||||
}
|
||||
|
||||
fn move_origin(&mut self, point: [i64; 2], units: crate::render::DisplayUnits, digits: u8) {
|
||||
@@ -636,6 +645,38 @@ impl PageContent {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_text_spaces(
|
||||
pages: &mut [DisplayList],
|
||||
mut resolve: impl FnMut(
|
||||
&crate::pdf_device::text_spaces::SpaceFontRequest,
|
||||
Option<crate::pdf_device::PdfPageData>,
|
||||
) -> Result<crate::pdf_device::text_spaces::ResolvedSpaceFont, String>,
|
||||
) -> Result<(), String> {
|
||||
if !pages.iter().any(|page| page.pdf_text_spaces.is_some()) {
|
||||
return Ok(());
|
||||
}
|
||||
let mut fonts = native_text::FontHistory::default();
|
||||
let mut selected = None;
|
||||
for page in pages {
|
||||
let plan = native_text::plan_spaces(page, &mut fonts, || {
|
||||
if let Some(font) = &selected {
|
||||
return Ok(crate::pdf_device::text_spaces::ResolvedSpaceFont::clone(font));
|
||||
}
|
||||
let request = &page.pdf_text_spaces.as_ref().expect("space settings for shipped node").font;
|
||||
let font = resolve(request, page.pdf.clone())?;
|
||||
selected = Some(font.clone());
|
||||
Ok(font)
|
||||
})?;
|
||||
if page.pdf_text_spaces.is_some() {
|
||||
page.pdf_resources.needs_pdf_for_preview |= plan.spaces_in_previous_font
|
||||
|| plan.dedicated.as_ref()
|
||||
.is_some_and(|font| !font.paint.glyph_paths[&b' '].is_empty());
|
||||
page.pdf_space_plan = Some(plan);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn page_content(
|
||||
list: &DisplayList,
|
||||
height: i64,
|
||||
@@ -681,7 +722,20 @@ fn page_content(
|
||||
}
|
||||
let mut native_text: Option<NativeTextRun> = None;
|
||||
let mut tex_text: Option<native_text::NativeText> = None;
|
||||
for item in &list.items {
|
||||
for (index, item) in list.items.iter().enumerate() {
|
||||
if let DisplayItem::PdfTextSpace { x, y, operation } = item {
|
||||
if let Some(dedicated) = list.pdf_space_plan.as_ref().and_then(|plan| plan.dedicated.as_ref())
|
||||
&& *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit
|
||||
{
|
||||
if tex_text.is_none() {
|
||||
content.move_origin([0, 0], list.units, digits);
|
||||
}
|
||||
native_text::NativeText::explicit_space(
|
||||
&mut tex_text, &mut content, *x, *y, height, digits, dedicated,
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let DisplayItem::PdfGraphics {
|
||||
x,
|
||||
y,
|
||||
@@ -728,6 +782,8 @@ fn page_content(
|
||||
*slot,
|
||||
height,
|
||||
digits,
|
||||
list.pdf_space_plan.as_ref().and_then(|plan| plan.before_glyph.get(&index)).copied(),
|
||||
list.pdf_space_plan.as_ref().and_then(|plan| plan.dedicated.as_ref()),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
@@ -980,7 +1036,7 @@ fn page_content(
|
||||
pt(*height)
|
||||
));
|
||||
}
|
||||
DisplayItem::Link { .. } => {}
|
||||
DisplayItem::Link { .. } | DisplayItem::PdfTextSpace { .. } => {}
|
||||
DisplayItem::PdfImageObject(_) => {}
|
||||
DisplayItem::Fill([r, g, b]) => {
|
||||
content.push_str(&format!(
|
||||
@@ -1291,6 +1347,9 @@ fn native_pdf(
|
||||
if !(1..=32768).contains(&page.magnification) {
|
||||
return Err("invalid native page magnification".into());
|
||||
}
|
||||
if page.pdf_text_spaces.is_some() && page.pdf_space_plan.is_none() {
|
||||
return Err("native PDF text spaces require actual font resolution".into());
|
||||
}
|
||||
for item in &page.items {
|
||||
match item {
|
||||
DisplayItem::Text {..} | DisplayItem::MathGlyph {..} => return Err("native PDF requires actual native font snapshots, not legacy text or master glyphs".into()),
|
||||
@@ -1919,6 +1978,16 @@ fn write_pdf(
|
||||
}
|
||||
}
|
||||
}
|
||||
for plan in pages.iter().filter_map(|page| page.pdf_space_plan.as_ref()) {
|
||||
for id in &plan.in_font {
|
||||
native_used.get_mut(id).expect("painted space font").2.insert(b' ', ' ');
|
||||
}
|
||||
if let Some(font) = &plan.dedicated {
|
||||
native_used.entry(font.font.id).or_insert_with(|| (
|
||||
font.font.clone(), font.paint.clone(), BTreeMap::from([(b' ', ' ')]),
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut native_font_ids = BTreeMap::new();
|
||||
for (id, (face, paint, used)) in native_used {
|
||||
native_font_ids.insert(
|
||||
|
||||
Reference in new issue
Block a user