mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-24 10:08:00 -06:00
fix(render): draw comma-below letters in non-embedded standard fonts (refs #1666)
The stand-ins for the standard fonts (PDFium's Foxit fonts) have neither the comma-below letters nor a commaaccent glyph, and the composite-letter patch left comma accents out. A non-embedded font naming /scommaaccent therefore drew nothing, which is the blank "si" left in Romanian XFA forms, and /tcommaaccent drew a cedilla through the glyph list's alias to U+0163. Romanian s/S/t/T and Latvian k/K/l/L/n/N/r/R/G with a comma below are now built from the letter and the font's comma, scaled to three quarters and set just under the baseline, moved back along the slant in italics. The uni021A and uni021B names of T and t with a comma below, which the glyph list does not name, resolve by code point. Lowercase g with its turned comma above is still not built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ec0a965074
commit
88735a7003
@@ -5755,6 +5755,74 @@ trailer << /Root 1 0 R >>
|
||||
}
|
||||
}
|
||||
|
||||
/// Comma-below letters in non-embedded standard fonts (vendored hayro-interpret patch to
|
||||
/// `StandardKind`). The stand-ins have neither these letters nor a `commaaccent` to build
|
||||
/// them from, so Romanian ș Ș (a form laid out from its XFA template names `/scommaaccent`)
|
||||
/// and Latvian ķ ļ ņ ŗ Ģ drew nothing, and ț Ț drew a cedilla. Each is now its letter with a
|
||||
/// comma under it, apart from the letter and inside the descender.
|
||||
#[test]
|
||||
fn comma_below_letters_in_standard_fonts_draw_a_comma_under_the_letter() {
|
||||
let pairs = [
|
||||
("s", "scommaaccent"),
|
||||
("S", "Scommaaccent"),
|
||||
("t", "tcommaaccent"),
|
||||
("T", "Tcommaaccent"),
|
||||
("k", "kcommaaccent"),
|
||||
("K", "Kcommaaccent"),
|
||||
("l", "lcommaaccent"),
|
||||
("L", "Lcommaaccent"),
|
||||
("n", "ncommaaccent"),
|
||||
("N", "Ncommaaccent"),
|
||||
("r", "rcommaaccent"),
|
||||
("R", "Rcommaaccent"),
|
||||
("G", "Gcommaaccent"),
|
||||
];
|
||||
let letters: Vec<&str> = pairs.iter().flat_map(|(base, accented)| [*base, *accented]).collect();
|
||||
let (size, baseline) = (40u32, 60u32);
|
||||
for font in ["Helvetica", "Times-Roman", "Courier", "Helvetica-BoldOblique"] {
|
||||
let mut content = String::new();
|
||||
for i in 0..letters.len() {
|
||||
content.push_str(&format!("BT /F1 {size} Tf {} {} Td <{:02X}> Tj ET\n", 10 + 40 * i, 80 - baseline, 1 + i));
|
||||
}
|
||||
let differences = letters.iter().map(|n| format!("/{n}")).collect::<Vec<_>>().join(" ");
|
||||
let pdf = format!(
|
||||
"%PDF-1.4
|
||||
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
|
||||
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
|
||||
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 {} 80] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >> endobj
|
||||
4 0 obj << /Length {} >> stream
|
||||
{content}endstream endobj
|
||||
5 0 obj << /Type /Font /Subtype /Type1 /BaseFont /{font}
|
||||
/Encoding << /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [1 {differences}] >> >> endobj
|
||||
trailer << /Root 1 0 R >>
|
||||
%%EOF",
|
||||
20 + 40 * letters.len(),
|
||||
content.len()
|
||||
);
|
||||
let mut r = PageRenderer::new(Arc::new(pdf.into_bytes()), RenderConfig::default());
|
||||
let p = r.render(RenderRequest { page: 0, kind: RequestKind::Pixels, tile: None, scale: 1.0, tag: 0, ..Default::default() });
|
||||
assert!(p.error.is_none(), "{font}: {:?}", p.error);
|
||||
// The rows (y down) of the i-th letter that have dark pixels.
|
||||
let ink_rows = |i: u32| -> Vec<u32> {
|
||||
(0..p.height)
|
||||
.filter(|y| ((8 + 40 * i)..(48 + 40 * i).min(p.width)).any(|x| p.rgba[((y * p.width + x) * 4) as usize] < 128))
|
||||
.collect()
|
||||
};
|
||||
for (pair, (_, name)) in pairs.iter().enumerate() {
|
||||
let (base, accented) = (ink_rows(2 * pair as u32), ink_rows(2 * pair as u32 + 1));
|
||||
let (Some(&base_bottom), Some(&bottom)) = (base.last(), accented.last()) else {
|
||||
panic!("{font} {name}: draws nothing ({} rows of its base letter)", base.len());
|
||||
};
|
||||
assert!(bottom > base_bottom + 2, "{font} {name}: a mark hangs below the letter ({bottom} vs {base_bottom})");
|
||||
assert!(
|
||||
(base_bottom + 1..bottom).any(|y| !accented.contains(&y)),
|
||||
"{font} {name}: the mark is apart from the letter, a comma and not a cedilla"
|
||||
);
|
||||
assert!(bottom < baseline + size * 3 / 10, "{font} {name}: the comma stays within the descender ({bottom})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Contributor-original classic-xref vector fixtures; no embedded or external assets.
|
||||
fn raw_crop_unit_pdf(unit: f64, rotation: i32, crop: [f64; 4], marker: Option<[f64; 4]>) -> Vec<u8> {
|
||||
let bounds = crop.map(|n| n.to_string()).join(" ");
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+54
-5
@@ -445,7 +445,8 @@ impl StandardKind {
|
||||
/// PdfCraft patch: the outline of a glyph from [`Self::composite_glyph`] — the base letter,
|
||||
/// then the accent centred on it. An accent above a letter taller than `x` is raised by the
|
||||
/// difference (the stand-in's accents sit on lowercase letters) and, in an italic font,
|
||||
/// moved along the slant; an ogonek hangs from the letter's right edge.
|
||||
/// moved along the slant; an ogonek hangs from the letter's right edge; a comma below is
|
||||
/// the font's comma, smaller, under the baseline.
|
||||
fn composite_outline(&self, glyph: GlyphId) -> Option<BezPath> {
|
||||
let code = u8::try_from(glyph.to_u32().checked_sub(COMPOSITE_GLYPH_BASE)?).ok()?;
|
||||
let (base, accent, place) = accented_parts(self.code_to_ps_name(code)?)?;
|
||||
@@ -455,13 +456,27 @@ impl StandardKind {
|
||||
.map(|g| self.base_font_blob.outline_glyph(g))
|
||||
};
|
||||
let mut path = outline(base)?;
|
||||
let accent = outline(accent)?;
|
||||
let mut accent = outline(accent)?;
|
||||
if path.elements().is_empty() || accent.elements().is_empty() {
|
||||
return Some(path);
|
||||
}
|
||||
if place == Place::CommaBelow {
|
||||
accent = Affine::scale(COMMA_BELOW_SCALE) * accent;
|
||||
}
|
||||
let (b, a) = (path.bounding_box(), accent.bounding_box());
|
||||
let (dx, dy) = match place {
|
||||
Place::Below => (b.center().x - a.center().x, 0.0),
|
||||
Place::CommaBelow => {
|
||||
// Its top a little below the baseline (or the letter's overshoot below it), and
|
||||
// in an italic font moved back along the slant by as much as it is lowered.
|
||||
let dy = b.y0.min(0.0) - COMMA_BELOW_GAP * a.height() - a.y1;
|
||||
let slant = if self.base_font.is_italic() {
|
||||
dy * ITALIC_ACCENT_SLANT
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
(b.center().x - a.center().x + slant, dy)
|
||||
}
|
||||
Place::Ogonek => (b.x1 - a.x1, 0.0),
|
||||
Place::Above => {
|
||||
let x_top = outline("x")
|
||||
@@ -582,12 +597,21 @@ const COMPOSITE_GLYPH_BASE: u32 = 0x0100_0000;
|
||||
/// an italic stand-in (tan 15°; the standard italics lean 12° to 16°).
|
||||
const ITALIC_ACCENT_SLANT: f64 = 0.27;
|
||||
|
||||
/// PdfCraft patch: the size of a comma accent against the font's comma. A full-size comma
|
||||
/// moved under the baseline reaches past the descenders and into the line below.
|
||||
const COMMA_BELOW_SCALE: f64 = 0.75;
|
||||
|
||||
/// PdfCraft patch: the gap between the baseline and a comma accent, as a share of its height.
|
||||
const COMMA_BELOW_GAP: f64 = 0.15;
|
||||
|
||||
/// Where an accent goes on its base letter.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Place {
|
||||
Above,
|
||||
Below,
|
||||
Ogonek,
|
||||
/// The font's comma, smaller, under the letter (the stand-ins have no `commaaccent`).
|
||||
CommaBelow,
|
||||
}
|
||||
|
||||
/// PdfCraft patch: the stand-ins for the standard fonts (PDFium's Foxit fonts) carry only the
|
||||
@@ -596,7 +620,11 @@ enum Place {
|
||||
/// iText 2 writes Turkish text with Times New Roman left unembedded) drew nothing for them. Each of
|
||||
/// these Latin Extended-A letters is a base letter the stand-ins have plus one of their accents,
|
||||
/// as Type 1 `seac` composites and the composites of the standard-14 AFM files are built.
|
||||
/// Letters whose accent isn't a plain one (ď ľ ť and the comma accents) aren't here.
|
||||
/// The comma-below letters of Romanian (ș ț) and Latvian (ķ ļ ņ ŗ Ģ) are the base letter and the
|
||||
/// font's comma, since the stand-ins have no `commaaccent` either; without them a form laid out
|
||||
/// from its XFA template drew Romanian ș as a blank and ț with a cedilla. Letters whose accent
|
||||
/// isn't a plain one (ď ľ ť, whose caron is an apostrophe beside the letter, and ģ, whose comma
|
||||
/// is turned and above it) aren't here.
|
||||
const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
("Amacron", "A", "macron", Place::Above),
|
||||
("amacron", "a", "macron", Place::Above),
|
||||
@@ -628,6 +656,7 @@ const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
("Gbreve", "G", "breve", Place::Above),
|
||||
("gbreve", "g", "breve", Place::Above),
|
||||
("Gdotaccent", "G", "dotaccent", Place::Above),
|
||||
("Gcommaaccent", "G", "comma", Place::CommaBelow),
|
||||
("gdotaccent", "g", "dotaccent", Place::Above),
|
||||
("Hcircumflex", "H", "circumflex", Place::Above),
|
||||
("hcircumflex", "h", "circumflex", Place::Above),
|
||||
@@ -641,10 +670,16 @@ const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
("iogonek", "i", "ogonek", Place::Ogonek),
|
||||
("Idotaccent", "I", "dotaccent", Place::Above),
|
||||
("Jcircumflex", "J", "circumflex", Place::Above),
|
||||
("Kcommaaccent", "K", "comma", Place::CommaBelow),
|
||||
("kcommaaccent", "k", "comma", Place::CommaBelow),
|
||||
("Lacute", "L", "acute", Place::Above),
|
||||
("lacute", "l", "acute", Place::Above),
|
||||
("Lcommaaccent", "L", "comma", Place::CommaBelow),
|
||||
("lcommaaccent", "l", "comma", Place::CommaBelow),
|
||||
("Nacute", "N", "acute", Place::Above),
|
||||
("nacute", "n", "acute", Place::Above),
|
||||
("Ncommaaccent", "N", "comma", Place::CommaBelow),
|
||||
("ncommaaccent", "n", "comma", Place::CommaBelow),
|
||||
("Ncaron", "N", "caron", Place::Above),
|
||||
("ncaron", "n", "caron", Place::Above),
|
||||
("Omacron", "O", "macron", Place::Above),
|
||||
@@ -655,6 +690,8 @@ const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
("ohungarumlaut", "o", "hungarumlaut", Place::Above),
|
||||
("Racute", "R", "acute", Place::Above),
|
||||
("racute", "r", "acute", Place::Above),
|
||||
("Rcommaaccent", "R", "comma", Place::CommaBelow),
|
||||
("rcommaaccent", "r", "comma", Place::CommaBelow),
|
||||
("Rcaron", "R", "caron", Place::Above),
|
||||
("rcaron", "r", "caron", Place::Above),
|
||||
("Sacute", "S", "acute", Place::Above),
|
||||
@@ -663,8 +700,12 @@ const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
("scircumflex", "s", "circumflex", Place::Above),
|
||||
("Scedilla", "S", "cedilla", Place::Below),
|
||||
("scedilla", "s", "cedilla", Place::Below),
|
||||
("Scommaaccent", "S", "comma", Place::CommaBelow),
|
||||
("scommaaccent", "s", "comma", Place::CommaBelow),
|
||||
("Tcedilla", "T", "cedilla", Place::Below),
|
||||
("tcedilla", "t", "cedilla", Place::Below),
|
||||
("Tcommaaccent", "T", "comma", Place::CommaBelow),
|
||||
("tcommaaccent", "t", "comma", Place::CommaBelow),
|
||||
("Tcaron", "T", "caron", Place::Above),
|
||||
("Utilde", "U", "tilde", Place::Above),
|
||||
("utilde", "u", "tilde", Place::Above),
|
||||
@@ -689,7 +730,8 @@ const ACCENTED_LETTERS: &[(&str, &str, &str, Place)] = &[
|
||||
];
|
||||
|
||||
/// PdfCraft patch: the base letter, accent and placement of an accented letter in
|
||||
/// [`ACCENTED_LETTERS`], by glyph name (`uni011E` and `u011E` forms too).
|
||||
/// [`ACCENTED_LETTERS`], by glyph name (`uni011E` and `u011E` forms too). Ș ș Ț ț by code point:
|
||||
/// the glyph list names only the first two, and gives the names of the second two to Ţ ţ.
|
||||
fn accented_parts(name: &str) -> Option<(&'static str, &'static str, Place)> {
|
||||
let name = normalized_glyph_name(name);
|
||||
let by_name = |n: &str| {
|
||||
@@ -698,5 +740,12 @@ fn accented_parts(name: &str) -> Option<(&'static str, &'static str, Place)> {
|
||||
.find(|(letter, ..)| *letter == n)
|
||||
.map(|&(_, base, accent, place)| (base, accent, place))
|
||||
};
|
||||
by_name(name).or_else(|| by_name(glyph_names::get_reverse(glyph_name_to_unicode(name)?)?))
|
||||
by_name(name).or_else(|| {
|
||||
let c = glyph_name_to_unicode(name)?;
|
||||
match c {
|
||||
'\u{021A}' => by_name("Tcommaaccent"),
|
||||
'\u{021B}' => by_name("tcommaaccent"),
|
||||
_ => by_name(glyph_names::get_reverse(c)?),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user