Files
vectorcraft/crates/pdf/src/import/reshaped.rs
T
ruchirguitar f18fb4e92d PDF import: Indic type that would not draw as the file does keeps the file's glyphs (#5235)
Gujarati text from a PDF came in jumbled even with its fonts installed:

- A glyph with no advance (a vowel sign or anusvara set over its letter)
  was taken for vertical type and opened as an object of its own, which
  type then drew on a dotted circle. In a font the file writes
  horizontally it now stays on its line.
- A line read glyph by glyph is not always the text that was set: the
  file draws a vowel sign before the consonant it follows, and may not
  name a conjunct. A line with characters of U+0900 to U+0DFF is now set
  again and its glyphs compared with the ink the file drew; when they
  differ the line keeps the file's glyphs as outlines, with a note.

Tests: crates/pdf/src/tests/reshaped.rs (two fail without the change).
2026-11-12 20:56:16 +05:30

54 lines
2.5 KiB
Rust

//! Type in a script whose letters reorder and join as they are set (#5235). A PDF draws such text
//! as glyphs already shaped: a vowel sign before the consonant it follows in reading order, a
//! conjunct as one glyph that the file may not name, a mark placed over its base. Read glyph by
//! glyph, the characters don't always make the text that was set, and type made from them draws
//! other glyphs in other places. A line of such type is therefore set again and compared with what
//! the file drew; one that would look different keeps the file's glyphs as outlines.
use kurbo::{Rect, Shape};
use vectorcraft_doc::TextObject;
/// Does `text` have a character of a script whose letters reorder and join into conjuncts: the
/// Brahmic scripts of U+0900 to U+0DFF (Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil,
/// Telugu, Kannada, Malayalam, Sinhala)?
pub(super) fn reshapes(text: &str) -> bool {
text.chars().any(|c| ('\u{0900}'..='\u{0DFF}').contains(&c))
}
/// Would type `t` draw its glyphs where the file drew them (`ink`: each glyph's ink box in
/// document space)? As many glyphs with ink, each within 3 % of the type's size of one of the
/// file's.
pub(super) fn draws_as(t: &TextObject, ink: &[Rect]) -> bool {
let laid = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
let mut set: Vec<Rect> =
laid.glyphs.iter().filter(|g| !g.outline.elements().is_empty()).map(|g| (t.xf * g.outline.clone()).bounding_box()).collect();
if set.len() != ink.len() {
return false;
}
let size = t.runs.iter().map(|r| r.style.size).fold(0.0, f64::max);
let tol = size * 0.03;
if !tol.is_finite() || set.iter().chain(ink).any(|r| !(r.x0.is_finite() && r.y0.is_finite() && r.x1.is_finite() && r.y1.is_finite())) {
return false;
}
set.sort_by(|a, b| a.x0.total_cmp(&b.x0));
let mut used = vec![false; set.len()];
// Each of the file's glyphs takes one the type draws in its place: looked for among those
// whose left edge is as near.
ink.iter().all(|r| {
let from = set.partition_point(|s| s.x0 < r.x0 - tol);
let found = set
.iter()
.zip(used.iter_mut())
.skip(from)
.take_while(|(s, _)| s.x0 <= r.x0 + tol)
.find(|(s, used)| !**used && (s.y0 - r.y0).abs() <= tol && (s.x1 - r.x1).abs() <= tol && (s.y1 - r.y1).abs() <= tol);
match found {
Some((_, used)) => {
*used = true;
true
}
None => false,
}
})
}