mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 10:55:47 +00:00
M3: Find matches words on a vertical line (view.find)
A glyph of a vertical-writing Type 0 font now spans its width w0 from /W or /DW across the column (ISO 32000-2 9.7.4.3), instead of its outline's width. Uneven outline widths made a narrow letter's line allowance smaller than the spacing of widely tracked text, so a column of Latin capitals with -40 Tc read as "A\nB\nCD\nE" and Find found nothing. hayro-interpret patch 102: OutlineGlyph::vertical_glyph_width. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9dbede8709
commit
d0da7938f2
@@ -295,9 +295,17 @@ impl<'a> Device<'a> for TextDevice {
|
||||
eprintln!("{kind} {text:?} transform={:?} glyph_transform={:?}", transform.as_coeffs(), glyph_transform.as_coeffs());
|
||||
}
|
||||
let advance = match glyph {
|
||||
Glyph::Outline(g) => {
|
||||
g.advance_width().filter(|a| *a > 0.0).map(f64::from).unwrap_or_else(|| g.outline().bounding_box().width().max(500.0))
|
||||
}
|
||||
// A vertical font's advance runs down the column; across it, the glyph is as wide as
|
||||
// its horizontal width w0, centred on the column by default (ISO 32000-2 §9.7.4.3). Taking
|
||||
// the outline's width instead gave a column of Latin letters boxes of uneven widths,
|
||||
// off its centre, and a narrow letter cut the line's gap allowance below the spacing
|
||||
// of widely tracked text, so the column fell apart into lines.
|
||||
Glyph::Outline(g) => g
|
||||
.vertical_glyph_width()
|
||||
.or_else(|| g.advance_width())
|
||||
.filter(|a| a.is_finite() && *a > 0.0)
|
||||
.map(f64::from)
|
||||
.unwrap_or_else(|| g.outline().bounding_box().width().max(500.0)),
|
||||
Glyph::Type3(g) => g.advance_width().filter(|a| a.is_finite() && *a > 0.0).map(f64::from).unwrap_or(600.0),
|
||||
};
|
||||
// The baseline direction in view space (y down).
|
||||
|
||||
@@ -569,6 +569,47 @@ fn vertical_ligatures_stay_in_their_column() {
|
||||
assert_eq!(text.line_rects(0..3).len(), 1, "one column");
|
||||
}
|
||||
|
||||
/// A column of Latin capitals in a vertical font, tracked out with negative character spacing
|
||||
/// (pdf.js issue 20930: `-40 Tc` puts 40 pt between 24 pt letters). Across the column each glyph
|
||||
/// is as wide as its `/W` width w0 and centred on the column (ISO 32000-2 §9.7.4.3), so the
|
||||
/// letters stay one line and Find matches the word. Boxes as wide as each outline made a narrow
|
||||
/// letter's line allowance smaller than the tracking, and the column broke into lines.
|
||||
#[test]
|
||||
fn tracked_vertical_latin_stays_one_line() {
|
||||
use lopdf::{Document, Object, Stream, dictionary};
|
||||
let mut doc = Document::with_version("1.7");
|
||||
let cmap = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def /CMapName /TestUnicode def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 5 beginbfchar <0024> <0041> <0025> <0042> <0026> <0043> <0027> <0044> <0028> <0045> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end";
|
||||
let unicode = doc.add_object(Stream::new(dictionary! {}, cmap.to_vec()));
|
||||
let widths: Vec<Object> = [(36, 667), (37, 667), (38, 722), (39, 722), (40, 667)]
|
||||
.into_iter()
|
||||
.flat_map(|(cid, w)| [Object::Integer(cid), Object::Array(vec![Object::Integer(w)])])
|
||||
.collect();
|
||||
let cid = doc.add_object(dictionary! {
|
||||
"Type" => "Font", "Subtype" => "CIDFontType2", "BaseFont" => "TestVerticalLatin",
|
||||
"CIDSystemInfo" => dictionary! { "Registry" => Object::string_literal("Adobe"), "Ordering" => Object::string_literal("Identity"), "Supplement" => 0 },
|
||||
"DW" => 1000, "W" => widths, "DW2" => vec![Object::Integer(880), Object::Integer(-1000)], "CIDToGIDMap" => "Identity"
|
||||
});
|
||||
let font = doc.add_object(dictionary! { "Type" => "Font", "Subtype" => "Type0", "BaseFont" => "TestVerticalLatin", "Encoding" => "Identity-V", "DescendantFonts" => vec![Object::Reference(cid)], "ToUnicode" => unicode });
|
||||
let content = doc.add_object(Stream::new(dictionary! {}, b"BT /F1 24 Tf -40 Tc 1 0 0 1 100 340 Tm <00240025002600270028> Tj ET".to_vec()));
|
||||
let pages = doc.new_object_id();
|
||||
let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages, "MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(400), Object::Integer(500)], "Resources" => dictionary! {"Font" => dictionary! {"F1" => font}}, "Contents" => content });
|
||||
doc.objects.insert(pages, dictionary! {"Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1}.into());
|
||||
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages});
|
||||
doc.trailer.set("Root", catalog);
|
||||
let mut bytes = Vec::new();
|
||||
doc.save_to(&mut bytes).unwrap();
|
||||
let pdf = Pdf::new(std::sync::Arc::new(bytes)).unwrap();
|
||||
let text = extract_page(&pdf, 0, &InterpreterSettings::default()).unwrap();
|
||||
assert_eq!(text.plain_text(), "ABCDE");
|
||||
assert_eq!(text.find("ABCDE"), vec![0..5]);
|
||||
// Each box spans w0 (here 24 pt × 0.667 or 0.722) centred on the column at x = 100.
|
||||
for (g, w0) in text.glyphs.iter().zip([667.0, 667.0, 722.0, 722.0, 667.0]) {
|
||||
let [x0, _, x1, _] = g.rect;
|
||||
assert!((x1 - x0 - 24.0 * w0 / 1000.0).abs() < 0.01, "{:?} is w0 wide: {:?}", g.text, g.rect);
|
||||
assert!(((x0 + x1) / 2.0 - 100.0).abs() < 0.01, "{:?} is centred on the column: {:?}", g.text, g.rect);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upright_mapping_round_trips() {
|
||||
let r = [10.0, 20.0, 30.0, 25.0];
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+7
@@ -415,6 +415,13 @@ impl Type0Font {
|
||||
}
|
||||
}
|
||||
|
||||
/// PdfCraft patch: the code's width w0 (its horizontal displacement, from `/W` or `/DW`)
|
||||
/// in either writing mode. In vertical writing it is the glyph's extent across the column
|
||||
/// (ISO 32000-2 §9.7.4.3).
|
||||
pub(crate) fn code_horizontal_width(&self, code: u32) -> f32 {
|
||||
self.horizontal_width(self.code_to_cid(code).unwrap_or(0))
|
||||
}
|
||||
|
||||
fn horizontal_width(&self, cid: u32) -> f32 {
|
||||
self.widths.get(&cid).copied().unwrap_or(self.dw)
|
||||
}
|
||||
|
||||
+13
@@ -179,6 +179,19 @@ impl OutlineGlyph {
|
||||
self.font.glyph_advance_width(self.char_code)
|
||||
}
|
||||
|
||||
/// PdfCraft patch: for a glyph of a vertical-writing Type0 font, its width w0 (the
|
||||
/// horizontal displacement from `/W` or `/DW`), which is how wide the glyph sits across the
|
||||
/// column (ISO 32000-2 §9.7.4.3). `None` for horizontal writing, where
|
||||
/// [`advance_width`](Self::advance_width) gives it.
|
||||
pub fn vertical_glyph_width(&self) -> Option<f32> {
|
||||
match &self.font {
|
||||
OutlineFont::Type0(font) if !font.is_horizontal() => {
|
||||
Some(font.code_horizontal_width(self.char_code))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the cache key for this glyph's font.
|
||||
///
|
||||
/// This identifies the font uniquely, even when `font_data()` returns `None`
|
||||
|
||||
Reference in New Issue
Block a user