Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / pdf / src / main / rust / src / draw_part3.rs
draw_part3.rs476 lines · 22.8 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod blind_reaudit_r5_text_tests { |
| 3 | use crate::outlines::GlyphProgram; |
| 4 | use crate::type1::Type1Font; |
| 5 | use crate::*; |
| 6 | use std::sync::Arc; |
| 7 | |
| 8 | /// A 1000x1000 box on a 1000-unit em, reachable as glyph name "A" at code 65. |
| 9 | fn embedded_box_font(wmode: u8) -> FontInfo { |
| 10 | let t1 = Type1Font { |
| 11 | glyphs: [( |
| 12 | "A".to_string(), |
| 13 | vec![vec![(0.0, 0.0), (1000.0, 0.0), (1000.0, 1000.0), (0.0, 1000.0), (0.0, 0.0)]], |
| 14 | )] |
| 15 | .into_iter() |
| 16 | .collect(), |
| 17 | encoding: [(65u32, "A".to_string())].into_iter().collect(), |
| 18 | font_matrix: [0.001, 0.0, 0.0, 0.001, 0.0, 0.0], |
| 19 | }; |
| 20 | FontInfo { |
| 21 | two_byte: false, |
| 22 | wmode, |
| 23 | vertical_metrics: Arc::default(), |
| 24 | default_vertical: (0.880, -1.0), |
| 25 | cid_to_gid: None, |
| 26 | to_unicode: None, |
| 27 | encoding: Arc::new([(65u32, 'A')].into_iter().collect()), |
| 28 | cmap_uni: Arc::default(), |
| 29 | cmap: None, |
| 30 | widths: Arc::new([(65u32, 0.5)].into_iter().collect()), |
| 31 | default_width: 0.5, |
| 32 | t3: None, |
| 33 | style: FontStyle::default(), |
| 34 | family: 0, |
| 35 | base_font: String::new(), |
| 36 | glyph_program: Some(Arc::new(GlyphProgram::Type1(t1))), |
| 37 | glyph_names: Arc::new([(65u32, "A".to_string())].into_iter().collect()), |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn show(fi: FontInfo, gs: GraphicsState) -> Vec<Prim> { |
| 42 | let doc = Document::with_version("1.7"); |
| 43 | let mut fonts = HashMap::new(); |
| 44 | fonts.insert(b"F1".to_vec(), fi); |
| 45 | let mut prims = Vec::new(); |
| 46 | show_string(&doc, &mut prims, &gs, &fonts, &IDENTITY, b"A", 0); |
| 47 | prims |
| 48 | } |
| 49 | |
| 50 | fn state(render_mode: i64) -> GraphicsState { |
| 51 | GraphicsState { |
| 52 | font_key: b"F1".to_vec(), |
| 53 | font_size: 100.0, |
| 54 | render_mode, |
| 55 | ..Default::default() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// §8.4.3.2: `w` is "a nonnegative number expressed in USER SPACE units", and |
| 60 | /// stroking paints the points whose perpendicular distance from the path IN USER |
| 61 | /// SPACE is at most half of it. So the pen is scaled by the CTM alone; the text |
| 62 | /// matrix and font size scale the GLYPH, not the pen, and §9.3.6 grants text no |
| 63 | /// exemption. pdf.js, pdfium and MuPDF all do exactly this. |
| 64 | /// |
| 65 | /// The pen used to be scaled by Trm = Tm·CTM, which on the shape cairo emits by |
| 66 | /// default — `0.5 w BT 12 0 0 12 100 700 Tm 2 Tr (Hg) Tj ET` — turned a 0.5pt pen |
| 67 | /// into 6.0pt. A 12pt glyph has ~1.2pt stems, so a 6pt pen adds 3pt each side, |
| 68 | /// every counter in H/g/e/o closes and the word paints as a solid black slab. |
| 69 | /// |
| 70 | /// Nothing else in the tree pins this value, so this test is the only thing |
| 71 | /// stopping it. Both consumers of the width — `Prim::Stroke.width` on the |
| 72 | /// embedded-outline path and `Prim::Text.stroke_width` on the substitute path — |
| 73 | /// read the one binding, so the outline path is asserted here as the strict case |
| 74 | /// (`Prim::Stroke.width` is additionally floored at 0.1, which 0.5 clears). |
| 75 | #[test] |
| 76 | fn a_text_pen_is_scaled_by_the_ctm_not_by_the_text_matrix() { |
| 77 | let stroke_width = |line_width: f64, ctm: Mat, tm: Mat| -> f32 { |
| 78 | let doc = Document::with_version("1.7"); |
| 79 | let mut fonts = HashMap::new(); |
| 80 | fonts.insert(b"F1".to_vec(), embedded_box_font(0)); |
| 81 | let gs = GraphicsState { |
| 82 | font_key: b"F1".to_vec(), |
| 83 | font_size: 12.0, |
| 84 | render_mode: 1, // stroke only, so the outline path emits Prim::Stroke |
| 85 | line_width, |
| 86 | ctm, |
| 87 | ..Default::default() |
| 88 | }; |
| 89 | let mut prims = Vec::new(); |
| 90 | show_string(&doc, &mut prims, &gs, &fonts, &tm, b"A", 0); |
| 91 | prims |
| 92 | .iter() |
| 93 | .find_map(|p| match p { |
| 94 | Prim::Stroke { width, .. } => Some(*width), |
| 95 | _ => None, |
| 96 | }) |
| 97 | .expect("mode 1 with an embedded program must emit a Prim::Stroke") |
| 98 | }; |
| 99 | |
| 100 | let identity: Mat = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; |
| 101 | let cairo_tm: Mat = [12.0, 0.0, 0.0, 12.0, 100.0, 700.0]; |
| 102 | |
| 103 | // The regression itself: a 12x text matrix must not touch the pen. |
| 104 | let w = stroke_width(0.5, identity, cairo_tm); |
| 105 | assert!( |
| 106 | (w - 0.5).abs() < 1e-5, |
| 107 | "Tm [12 0 0 12] with 0.5 w must give a 0.5 pen, got {w} \ |
| 108 | (the old Trm scaling gave 6.0 — 12x too heavy)" |
| 109 | ); |
| 110 | |
| 111 | // The second reported shape, 3x too heavy. |
| 112 | let w = stroke_width(1.0, identity, [3.0, 0.0, 0.0, 3.0, 0.0, 0.0]); |
| 113 | assert!((w - 1.0).abs() < 1e-5, "Tm [3 0 0 3] with 1 w must give 1.0, got {w}"); |
| 114 | |
| 115 | // The CTM, and only the CTM, still scales it — a zoomed/scaled page must |
| 116 | // not lose its stroke weight, or the fix would just be a different bug. |
| 117 | let w = stroke_width(0.5, [4.0, 0.0, 0.0, 4.0, 0.0, 0.0], cairo_tm); |
| 118 | assert!((w - 2.0).abs() < 1e-5, "0.5 w under a 4x CTM must give 2.0, got {w}"); |
| 119 | |
| 120 | // The case that was already correct and must not regress. |
| 121 | let w = stroke_width(2.0, identity, [1.0, 0.0, 0.0, 1.0, 100.0, 700.0]); |
| 122 | assert!((w - 2.0).abs() < 1e-5, "identity Tm and CTM must give line_width, got {w}"); |
| 123 | } |
| 124 | |
| 125 | /// A non-empty string can show ZERO glyphs, so "the string was non-empty" is not |
| 126 | /// a safe stand-in for "a glyph was shown". |
| 127 | /// |
| 128 | /// `fonts.rs`'s Identity-H/V branch is `while i + 1 < bytes.len()`, so a 2-byte |
| 129 | /// font consumes codes in pairs and an ODD-length string drops its final byte — |
| 130 | /// a 1-byte string yields no codes at all and emits nothing. |
| 131 | /// |
| 132 | /// This matters beyond this file. `interpret.rs::latches_text_clip` latches the |
| 133 | /// §9.4.3 text clip on `shown_mode >= 4 && !bytes.is_empty()`. On this input that |
| 134 | /// latches a clip for a run that produced no glyph outline, so `ET` emits the |
| 135 | /// marker, the consumer's accumulation is empty, and an empty accumulation clips |
| 136 | /// to NOTHING — blanking every following prim up to the matching pop. The |
| 137 | /// predicate needs "a glyph was actually iterated", which is knowable only here |
| 138 | /// and not from the byte string at the show site. |
| 139 | /// |
| 140 | /// The sibling branch differs: the non-Identity CMap path advances by |
| 141 | /// `min(bytes.len() - i, ..)` and so DOES yield a code for a 1-byte remainder. |
| 142 | /// The defect is specific to Identity-H/V, which is why reading the show sites |
| 143 | /// alone cannot reveal it. |
| 144 | #[test] |
| 145 | fn an_odd_length_identity_h_string_shows_no_glyph_despite_non_empty_bytes() { |
| 146 | let identity_h = || { |
| 147 | let mut fi = embedded_box_font(0); |
| 148 | fi.two_byte = true; |
| 149 | fi.cmap = None; // Identity-H: fixed 2-byte codes |
| 150 | fi.glyph_program = None; // substitute path, so `s` alone decides emission |
| 151 | fi |
| 152 | }; |
| 153 | let run = |bytes: &[u8]| -> usize { |
| 154 | let doc = Document::with_version("1.7"); |
| 155 | let mut fonts = HashMap::new(); |
| 156 | fonts.insert(b"F1".to_vec(), identity_h()); |
| 157 | // Mode 7: clip-only, which is where the latch mistake becomes destructive. |
| 158 | let gs = GraphicsState { |
| 159 | font_key: b"F1".to_vec(), |
| 160 | font_size: 12.0, |
| 161 | render_mode: 7, |
| 162 | ..Default::default() |
| 163 | }; |
| 164 | let mut prims = Vec::new(); |
| 165 | show_string(&doc, &mut prims, &gs, &fonts, &IDENTITY, bytes, 0); |
| 166 | prims.len() |
| 167 | }; |
| 168 | |
| 169 | // Precondition: a complete 2-byte code does show a glyph, so the assertion |
| 170 | // below measures the odd length rather than a broken fixture. |
| 171 | assert!(run(&[0x00, 0x41]) > 0, "precondition: a complete 2-byte code must show a glyph"); |
| 172 | |
| 173 | assert_eq!( |
| 174 | run(&[0x41]), |
| 175 | 0, |
| 176 | "a 1-byte string in an Identity-H font is non-empty yet shows no glyph; \ |
| 177 | latches_text_clip's `!bytes.is_empty()` therefore latches a clip with no \ |
| 178 | outline behind it, which clips the following content to nothing" |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /// C4: an embedded font in a clip mode must paint from its OWN outline. |
| 183 | /// |
| 184 | /// The outline was gated on modes 0..=2, so an embedded font in Tr 4/5/6 fell to |
| 185 | /// the substitute branch and was painted with a system face — wrong letterforms, |
| 186 | /// weight and intra-glyph widths — while its real outline sat unused. The same |
| 187 | /// font rendered correctly at Tr 0 and wrongly at Tr 4. |
| 188 | /// |
| 189 | /// This pins the two traps that make the obvious one-line version wrong: |
| 190 | /// - mode 7 must STAY on the substitute path. It is in neither `has_fill` nor |
| 191 | /// `has_stroke`, so the outline branch would emit no ink at all, and tag its |
| 192 | /// record `outline: true`, which the consumer drops before the clip |
| 193 | /// accumulator — no ink and no clip, i.e. total loss after every ET. |
| 194 | /// - the 4-6 record must be `outline: false` (so it reaches the accumulator) |
| 195 | /// AND `argb: 0` (so it does not paint over the contours). Those two flags |
| 196 | /// have to disagree with what modes 0-2 set. |
| 197 | #[test] |
| 198 | fn an_embedded_font_paints_its_own_outline_in_clip_modes() { |
| 199 | let ink = |ps: &[Prim]| ps.iter().filter(|p| matches!(p, Prim::Fill { .. } | Prim::Stroke { .. })).count(); |
| 200 | let text_rec = |ps: &[Prim]| -> Option<(u8, u32, bool)> { |
| 201 | ps.iter().find_map(|p| match p { |
| 202 | Prim::Text { render_mode, argb, outline, .. } => Some((*render_mode, *argb, *outline)), |
| 203 | _ => None, |
| 204 | }) |
| 205 | }; |
| 206 | |
| 207 | // Modes 0-2 keep their existing contract: real ink, record flagged as |
| 208 | // already-drawn so the consumer ignores it. |
| 209 | let prims = show(embedded_box_font(0), state(0)); |
| 210 | assert!(ink(&prims) > 0, "mode 0 must paint the embedded outline"); |
| 211 | assert_eq!( |
| 212 | text_rec(&prims).map(|t| t.2), |
| 213 | Some(true), |
| 214 | "mode 0's record must stay outline:true — it is not a clip contribution" |
| 215 | ); |
| 216 | |
| 217 | // Modes 4-6: real ink AND a record the accumulator can use. |
| 218 | for rm in [4i64, 5, 6] { |
| 219 | let prims = show(embedded_box_font(0), state(rm)); |
| 220 | assert!( |
| 221 | ink(&prims) > 0, |
| 222 | "mode {rm} must paint from the EMBEDDED outline, not a substitute face" |
| 223 | ); |
| 224 | let (got_rm, argb, outline) = |
| 225 | text_rec(&prims).expect("mode {rm} must still emit a clip record"); |
| 226 | assert_eq!(got_rm, rm as u8); |
| 227 | assert!( |
| 228 | !outline, |
| 229 | "mode {rm} record must be outline:false or the consumer drops it \ |
| 230 | before the clip accumulator — that is total content loss, not a \ |
| 231 | fidelity bug" |
| 232 | ); |
| 233 | assert_eq!( |
| 234 | argb, 0, |
| 235 | "mode {rm} record must be transparent so it does not paint a \ |
| 236 | substitute face on top of the real contours" |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | // Mode 7 stays on the substitute path: no ink, and a usable clip record. |
| 241 | let prims = show(embedded_box_font(0), state(7)); |
| 242 | assert_eq!(ink(&prims), 0, "mode 7 is clip-only and must lay down no ink"); |
| 243 | let (got_rm, argb, outline) = |
| 244 | text_rec(&prims).expect("mode 7 must still emit a clip record"); |
| 245 | assert_eq!((got_rm, argb, outline), (7u8, 0u32, false)); |
| 246 | } |
| 247 | |
| 248 | /// mode 7 is "Add to path for clipping" — neither marks the page. Mode 3 is how |
| 249 | /// every scanned document carries its OCR layer, so if it paints, the scan is |
| 250 | /// overprinted with a second copy of its own text. |
| 251 | /// |
| 252 | /// Asserted against an EMBEDDED font, because that is the path that can paint: |
| 253 | /// modes 0-2 emit real outlines as `Prim::Fill`/`Prim::Stroke`, which the Kotlin |
| 254 | /// side has no render-mode guard for (it skips `Prim::Text` for rm 3/7, and skips |
| 255 | /// `outline`-flagged Text entirely). Ink for mode 3 therefore has to be suppressed |
| 256 | /// HERE or not at all. |
| 257 | #[test] |
| 258 | fn render_mode_3_and_7_emit_no_ink_even_with_an_embedded_program() { |
| 259 | let ink = |p: &Prim| matches!(p, Prim::Fill { .. } | Prim::Stroke { .. }); |
| 260 | |
| 261 | // Precondition: this font really does paint in a painting mode. |
| 262 | assert!( |
| 263 | show(embedded_box_font(0), state(0)).iter().any(ink), |
| 264 | "precondition: mode 0 must emit outline ink, or the test proves nothing" |
| 265 | ); |
| 266 | |
| 267 | for rm in [3i64, 7] { |
| 268 | let prims = show(embedded_box_font(0), state(rm)); |
| 269 | assert!( |
| 270 | !prims.iter().any(ink), |
| 271 | "render mode {rm} must paint nothing, got {} ink prim(s)", |
| 272 | prims.iter().filter(|p| ink(p)).count() |
| 273 | ); |
| 274 | let texts: Vec<_> = prims |
| 275 | .iter() |
| 276 | .filter_map(|p| match p { |
| 277 | Prim::Text { argb, render_mode, text, .. } => Some((*argb, *render_mode, text)), |
| 278 | _ => None, |
| 279 | }) |
| 280 | .collect(); |
| 281 | assert_eq!(texts.len(), 1, "mode {rm} must still reach the text index"); |
| 282 | assert_eq!(texts[0].0, 0, "mode {rm} text prim must carry no colour"); |
| 283 | assert_eq!(texts[0].1, rm as u8); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /// The glyph-space -> text-space scale is the font program's units-per-em, and it |
| 288 | /// is applied exactly once: a 1000-unit box on a 1000-unit em at 100 Tf spans |
| 289 | /// 100 user-space units. Applying it twice (or not at all) is invisible in a |
| 290 | /// units-per-em-1000 font only if the second factor is 1, which is why this |
| 291 | /// asserts the absolute extent rather than a ratio. |
| 292 | #[test] |
| 293 | fn an_embedded_outline_is_scaled_by_units_per_em_exactly_once() { |
| 294 | let prims = show(embedded_box_font(0), state(0)); |
| 295 | let contours = prims |
| 296 | .iter() |
| 297 | .find_map(|p| match p { |
| 298 | Prim::Fill { contours, .. } => Some(contours.clone()), |
| 299 | _ => None, |
| 300 | }) |
| 301 | .expect("mode 0 with an embedded program must emit outline fills"); |
| 302 | let xs: Vec<f32> = contours.iter().flatten().map(|p| p.0).collect(); |
| 303 | let ys: Vec<f32> = contours.iter().flatten().map(|p| p.1).collect(); |
| 304 | let max_x = xs.iter().cloned().fold(f32::MIN, f32::max); |
| 305 | let max_y = ys.iter().cloned().fold(f32::MIN, f32::max); |
| 306 | assert!((max_x - 100.0).abs() < 1e-3, "em box should span 100 user units, got {max_x}"); |
| 307 | assert!((max_y - 100.0).abs() < 1e-3, "em box should span 100 user units, got {max_y}"); |
| 308 | } |
| 309 | |
| 310 | /// Horizontal scaling (Tz) scales the glyph and its advance horizontally only |
| 311 | /// (§9.4.4: Th multiplies the x column of the text-space parameter matrix). |
| 312 | #[test] |
| 313 | fn horizontal_scaling_widens_the_outline_without_stretching_it_vertically() { |
| 314 | let gs = GraphicsState { h_scale: 2.0, ..state(0) }; |
| 315 | let prims = show(embedded_box_font(0), gs); |
| 316 | let contours = prims |
| 317 | .iter() |
| 318 | .find_map(|p| match p { |
| 319 | Prim::Fill { contours, .. } => Some(contours.clone()), |
| 320 | _ => None, |
| 321 | }) |
| 322 | .expect("outline fill"); |
| 323 | let max_x = contours.iter().flatten().map(|p| p.0).fold(f32::MIN, f32::max); |
| 324 | let max_y = contours.iter().flatten().map(|p| p.1).fold(f32::MIN, f32::max); |
| 325 | assert!((max_x - 200.0).abs() < 1e-3, "Tz 200 must double the width, got {max_x}"); |
| 326 | assert!((max_y - 100.0).abs() < 1e-3, "Tz must not touch the height, got {max_y}"); |
| 327 | } |
| 328 | |
| 329 | /// §9.4.4: Trise is a row of the text-space parameter matrix, which does not |
| 330 | /// depend on the writing mode. The vertical branch built its placement point |
| 331 | /// from the position vector alone and dropped Trise, so a superscript in |
| 332 | /// vertical CJK sat on the baseline. |
| 333 | #[test] |
| 334 | fn text_rise_applies_in_vertical_writing_mode_too() { |
| 335 | let origin = |rise: f64| { |
| 336 | let gs = GraphicsState { rise, ..state(3) }; |
| 337 | show(embedded_box_font(1), gs) |
| 338 | .into_iter() |
| 339 | .find_map(|p| match p { |
| 340 | Prim::Text { y, .. } => Some(y), |
| 341 | _ => None, |
| 342 | }) |
| 343 | .expect("a text prim per glyph") |
| 344 | }; |
| 345 | assert!( |
| 346 | (origin(20.0) - origin(0.0) - 20.0).abs() < 1e-3, |
| 347 | "Trise must displace a vertical glyph by 20 user units" |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | /// Substitute glyphs are sized by Kotlin as `size` (the em, taken from the |
| 352 | /// matrix's Y scale) times `h_scale` (Tz). Under an ANISOTROPIC matrix those |
| 353 | /// two do not describe the glyph the outline path draws: the outline is scaled |
| 354 | /// by x_scale horizontally and y_scale vertically, so the substitute came out |
| 355 | /// narrower or wider by exactly that ratio — the two paths disagreeing about |
| 356 | /// the size of the same glyph. The ratio is the only part of the mismatch the |
| 357 | /// current wire can carry (a rotation still needs a field that does not exist), |
| 358 | /// and it must be exactly 1 for every isotropic matrix, which is nearly all of |
| 359 | /// them — hence the second half of this test. |
| 360 | #[test] |
| 361 | fn the_substitute_face_is_told_the_matrixs_horizontal_scale() { |
| 362 | let h_scale_for = |ctm: Mat, th: f64| { |
| 363 | let mut fi = embedded_box_font(0); |
| 364 | fi.glyph_program = None; // force the substitute path |
| 365 | let gs = GraphicsState { ctm, h_scale: th, ..state(0) }; |
| 366 | show(fi, gs) |
| 367 | .into_iter() |
| 368 | .find_map(|p| match p { |
| 369 | Prim::Text { h_scale, size, .. } => Some((h_scale, size)), |
| 370 | _ => None, |
| 371 | }) |
| 372 | .expect("substitute path emits a text prim") |
| 373 | }; |
| 374 | |
| 375 | // Isotropic: unchanged, whatever the zoom. This is the case that must not move. |
| 376 | for s in [1.0, 3.0, 0.25] { |
| 377 | let (hs, _) = h_scale_for([s, 0.0, 0.0, s, 0.0, 0.0], 1.0); |
| 378 | assert!((hs - 1.0).abs() < 1e-5, "isotropic scale {s} must leave Tz alone, got {hs}"); |
| 379 | } |
| 380 | // A pure rotation is isotropic too. |
| 381 | let (a, b) = (0.6_f64, 0.8_f64); // cos/sin of a 53-degree rotation |
| 382 | let (hs, _) = h_scale_for([a, b, -b, a, 0.0, 0.0], 1.0); |
| 383 | assert!((hs - 1.0).abs() < 1e-5, "a pure rotation must leave Tz alone, got {hs}"); |
| 384 | |
| 385 | // Anisotropic: x twice y. The em still comes from the Y scale, and the |
| 386 | // horizontal stretch rides on h_scale. |
| 387 | let (hs, size) = h_scale_for([2.0, 0.0, 0.0, 1.0, 0.0, 0.0], 1.0); |
| 388 | assert!((hs - 2.0).abs() < 1e-5, "x_scale/y_scale = 2 must reach the wire, got {hs}"); |
| 389 | assert!((size - 100.0).abs() < 1e-3, "the em still follows the Y scale, got {size}"); |
| 390 | |
| 391 | // …and it composes with a real Tz rather than replacing it. |
| 392 | let (hs, _) = h_scale_for([2.0, 0.0, 0.0, 1.0, 0.0, 0.0], 0.5); |
| 393 | assert!((hs - 1.0).abs() < 1e-5, "Tz 50% under a 2:1 matrix, got {hs}"); |
| 394 | |
| 395 | // A near-degenerate matrix must not put a non-finite or absurd scale on the |
| 396 | // wire. y_scale sits just above the divide-by-zero guard, so the raw ratio |
| 397 | // is ~5e8; the producer bounds it rather than relying on the consumer. |
| 398 | let (hs, _) = h_scale_for([1.0, 0.0, 0.0, 2e-9, 0.0, 0.0], 1.0); |
| 399 | assert!(hs.is_finite() && (0.01..=100.0).contains(&hs), "unbounded scale {hs}"); |
| 400 | // Fully degenerate (below the guard) falls back to plain Tz. |
| 401 | let (hs, _) = h_scale_for([1.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.75); |
| 402 | assert!((hs - 0.75).abs() < 1e-5, "degenerate matrix must yield plain Tz, got {hs}"); |
| 403 | |
| 404 | // A pathological Tz must not reach the wire either. NaN is the one that |
| 405 | // matters: Kotlin's `coerceIn` is two comparisons, both false against NaN, |
| 406 | // so it would sail through the consumer's clamp into `Paint.textScaleX` and |
| 407 | // the glyph would silently disappear rather than be the wrong width. |
| 408 | for bad_tz in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { |
| 409 | for ctm in [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0], [2.0, 0.0, 0.0, 1.0, 0.0, 0.0]] { |
| 410 | let (hs, _) = h_scale_for(ctm, bad_tz); |
| 411 | assert!(hs.is_finite(), "Tz {bad_tz} put {hs} on the wire"); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /// `Tf`'s operand and the CTM are file input and `size` is their product, so a |
| 417 | /// non-finite value is reachable even with both validated upstream: `f64 as f32` |
| 418 | /// saturates to infinity above ~3.4e38, and `inf * 0` from a zero-scale matrix |
| 419 | /// is NaN. It matters because NaN is not merely a wrong number here — Kotlin |
| 420 | /// floors the size with `coerceAtLeast`, a comparison that is false against NaN, |
| 421 | /// so it reaches `Paint.textSize` and the glyph vanishes. |
| 422 | #[test] |
| 423 | fn a_pathological_font_size_cannot_put_nan_on_the_wire() { |
| 424 | let sizes = |tfs: f64, ctm: Mat| { |
| 425 | let mut fi = embedded_box_font(0); |
| 426 | fi.glyph_program = None; |
| 427 | let gs = GraphicsState { ctm, font_size: tfs, ..state(0) }; |
| 428 | show(fi, gs) |
| 429 | .into_iter() |
| 430 | .filter_map(|p| match p { |
| 431 | Prim::Text { size, advance, .. } => Some((size, advance)), |
| 432 | _ => None, |
| 433 | }) |
| 434 | .collect::<Vec<_>>() |
| 435 | }; |
| 436 | let zero_scale: Mat = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; |
| 437 | let identity: Mat = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; |
| 438 | for (tfs, ctm) in [ |
| 439 | (f64::INFINITY, zero_scale), // inf * 0 = NaN, the reachable case |
| 440 | (f64::INFINITY, identity), |
| 441 | (f64::NAN, identity), |
| 442 | (1e308, identity), |
| 443 | ] { |
| 444 | for (size, advance) in sizes(tfs, ctm) { |
| 445 | assert!(size.is_finite(), "Tf {tfs} produced size {size}"); |
| 446 | assert!(advance.is_finite(), "Tf {tfs} produced advance {advance}"); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /// The no-metrics fallback returns a run advance of `len * 0.5 * Tfs * Th` but |
| 452 | /// used to put ONE glyph's `size` on the wire as the run's device advance. A |
| 453 | /// non-painted run (mode 3 — the OCR layer of a scan with an unresolvable font |
| 454 | /// resource) is aligned to that field by the selection layer, so every glyph's |
| 455 | /// selection rectangle piled up on the first character. |
| 456 | #[test] |
| 457 | fn a_run_with_no_font_metrics_reports_the_whole_runs_advance() { |
| 458 | let doc = Document::with_version("1.7"); |
| 459 | let fonts: HashMap<Vec<u8>, FontInfo> = HashMap::new(); |
| 460 | let gs = GraphicsState { font_key: b"F1".to_vec(), font_size: 10.0, ..Default::default() }; |
| 461 | let mut prims = Vec::new(); |
| 462 | let pen = show_string(&doc, &mut prims, &gs, &fonts, &IDENTITY, b"ABCD", 0); |
| 463 | let advance = prims |
| 464 | .iter() |
| 465 | .find_map(|p| match p { |
| 466 | Prim::Text { advance, .. } => Some(*advance), |
| 467 | _ => None, |
| 468 | }) |
| 469 | .expect("the run must still reach the text index"); |
| 470 | assert!((pen - 20.0).abs() < 1e-9, "4 codes at 0.5 em of 10 Tf"); |
| 471 | assert!( |
| 472 | (advance - pen as f32).abs() < 1e-3, |
| 473 | "the wire advance must span the whole run ({pen}), got {advance}" |
| 474 | ); |
| 475 | } |
| 476 | } |