diff --git a/crates/core/src/pdf/write/afm_data.rs b/crates/core/src/pdf/write/afm_data.rs index 754aa514..542bb158 100644 --- a/crates/core/src/pdf/write/afm_data.rs +++ b/crates/core/src/pdf/write/afm_data.rs @@ -1,3 +1,952 @@ -//! R4.4a: the AFM metrics of Helvetica, Helvetica-Bold and Times-Roman over WinAnsi, and -//! the Unicode tables the text rules need, generated from the O4 and O5 oracles by -//! `scripts/r4-afm-data.mjs`. Written by the R4.4a track. +//! Generated by `scripts/r4-afm-data.mjs`: do not edit, rerun the generator. +//! +//! The AFM metrics of Helvetica, Helvetica-Bold and Times-Roman over WinAnsi as pdf-lib +//! 1.17.1 measures them (@pdf-lib/standard-fonts 1.0.0), and the Unicode tables of the +//! text rules as V8 13.6.233.17-node.53 had them (Node v24.21.0, ICU 78.3, +//! Unicode 17.0). Sources, the frozen oracles: +//! - O5 `crates/core/tests/oracles/r4/afm.json` (commit 516342b) +//! sha256 79f282775a052810e7565e754e0076dfe693f0fa027846ca4337f7f44a788733 +//! - O4 `crates/core/tests/oracles/r4/text.json` (commit 516342b) +//! sha256 fec2cd3954508f3513d2d5af4bce0c258987bb4cded6ba74083f98cd9dec3905 + +/// The metrics one standard font is measured with, by WinAnsi byte. +pub struct FontMetrics { + /// AFM `WX` of the glyph each byte prints; 0 where the byte prints no glyph (the + /// width lookup then falls back to 250, pdf-lib's `|| 250`). + pub widths: [u16; 256], + /// AFM `KPX` (left byte, right byte, amount) between WinAnsi glyphs, sorted by pair. + pub kerning: &'static [(u8, u8, i16)], +} + +/// Helvetica: 1295 kerning pairs between WinAnsi glyphs. +#[rustfmt::skip] +pub static HELVETICA: FontMetrics = FontMetrics { + widths: [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, + 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, + 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, + 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 333, 556, 556, 500, 556, 556, 278, 556, + 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, + 500, 334, 260, 334, 584, 0, 556, 0, 222, 556, 333, 1000, 556, 556, 333, 1000, 667, 333, + 1000, 0, 611, 0, 0, 222, 222, 333, 333, 350, 556, 1000, 333, 1000, 500, 333, 944, 0, 500, + 500, 278, 333, 556, 556, 556, 556, 260, 556, 333, 737, 370, 556, 584, 333, 737, 333, 400, + 584, 333, 333, 333, 556, 537, 278, 333, 333, 365, 556, 834, 834, 834, 611, 667, 667, 667, + 667, 667, 667, 1000, 722, 667, 667, 667, 667, 278, 278, 278, 278, 722, 722, 778, 778, 778, + 778, 778, 584, 778, 722, 722, 722, 722, 667, 667, 611, 556, 556, 556, 556, 556, 556, 889, + 500, 556, 556, 556, 556, 278, 278, 278, 278, 556, 556, 556, 556, 556, 556, 556, 584, 611, + 556, 556, 556, 556, 500, 556, 500, + ], + kerning: &[ + (32, 84, -50), (32, 86, -50), (32, 87, -40), (32, 89, -90), (32, 145, -60), (32, 147, -30), + (32, 221, -90), (44, 146, -100), (44, 148, -100), (46, 32, -60), (46, 146, -100), + (46, 148, -100), (46, 160, -60), (58, 32, -50), (58, 160, -50), (59, 32, -50), + (59, 160, -50), (65, 67, -30), (65, 71, -30), (65, 79, -30), (65, 81, -30), (65, 84, -120), + (65, 85, -50), (65, 86, -70), (65, 87, -50), (65, 89, -100), (65, 117, -30), (65, 118, -40), + (65, 119, -40), (65, 121, -40), (65, 159, -40), (65, 199, -30), (65, 210, -30), + (65, 211, -30), (65, 212, -30), (65, 213, -30), (65, 214, -30), (65, 216, -30), + (65, 217, -50), (65, 218, -50), (65, 219, -50), (65, 220, -50), (65, 221, -100), + (65, 249, -30), (65, 250, -30), (65, 251, -30), (65, 252, -30), (65, 253, -40), + (65, 255, -40), (66, 44, -20), (66, 46, -20), (66, 85, -10), (66, 217, -10), (66, 218, -10), + (66, 219, -10), (66, 220, -10), (67, 44, -30), (67, 46, -30), (68, 44, -70), (68, 46, -70), + (68, 65, -40), (68, 86, -70), (68, 87, -40), (68, 89, -90), (68, 192, -40), (68, 193, -40), + (68, 194, -40), (68, 195, -40), (68, 196, -40), (68, 197, -40), (68, 221, -90), + (70, 44, -150), (70, 46, -150), (70, 65, -80), (70, 97, -50), (70, 101, -30), + (70, 111, -30), (70, 114, -45), (70, 192, -80), (70, 193, -80), (70, 194, -80), + (70, 195, -80), (70, 196, -80), (70, 197, -80), (70, 224, -50), (70, 225, -50), + (70, 226, -50), (70, 227, -50), (70, 228, -50), (70, 229, -50), (70, 232, -30), + (70, 233, -30), (70, 234, -30), (70, 235, -30), (70, 242, -30), (70, 243, -30), + (70, 244, -30), (70, 245, -30), (70, 246, -30), (70, 248, -30), (74, 44, -30), + (74, 46, -30), (74, 65, -20), (74, 97, -20), (74, 117, -20), (74, 192, -20), (74, 193, -20), + (74, 194, -20), (74, 195, -20), (74, 196, -20), (74, 197, -20), (74, 224, -20), + (74, 225, -20), (74, 226, -20), (74, 227, -20), (74, 228, -20), (74, 229, -20), + (74, 249, -20), (74, 250, -20), (74, 251, -20), (74, 252, -20), (75, 79, -50), + (75, 101, -40), (75, 111, -40), (75, 117, -30), (75, 121, -50), (75, 159, -50), + (75, 210, -50), (75, 211, -50), (75, 212, -50), (75, 213, -50), (75, 214, -50), + (75, 216, -50), (75, 232, -40), (75, 233, -40), (75, 234, -40), (75, 235, -40), + (75, 242, -40), (75, 243, -40), (75, 244, -40), (75, 245, -40), (75, 246, -40), + (75, 248, -40), (75, 249, -30), (75, 250, -30), (75, 251, -30), (75, 252, -30), + (75, 253, -50), (75, 255, -50), (76, 84, -110), (76, 86, -110), (76, 87, -70), + (76, 89, -140), (76, 121, -30), (76, 146, -160), (76, 148, -140), (76, 159, -30), + (76, 221, -140), (76, 253, -30), (76, 255, -30), (79, 44, -40), (79, 46, -40), + (79, 65, -20), (79, 84, -40), (79, 86, -50), (79, 87, -30), (79, 88, -60), (79, 89, -70), + (79, 192, -20), (79, 193, -20), (79, 194, -20), (79, 195, -20), (79, 196, -20), + (79, 197, -20), (79, 221, -70), (80, 44, -180), (80, 46, -180), (80, 65, -120), + (80, 97, -40), (80, 101, -50), (80, 111, -50), (80, 192, -120), (80, 193, -120), + (80, 194, -120), (80, 195, -120), (80, 196, -120), (80, 197, -120), (80, 224, -40), + (80, 225, -40), (80, 226, -40), (80, 227, -40), (80, 228, -40), (80, 229, -40), + (80, 232, -50), (80, 233, -50), (80, 234, -50), (80, 235, -50), (80, 242, -50), + (80, 243, -50), (80, 244, -50), (80, 245, -50), (80, 246, -50), (80, 248, -50), + (81, 85, -10), (81, 217, -10), (81, 218, -10), (81, 219, -10), (81, 220, -10), + (82, 79, -20), (82, 84, -30), (82, 85, -40), (82, 86, -50), (82, 87, -30), (82, 89, -50), + (82, 210, -20), (82, 211, -20), (82, 212, -20), (82, 213, -20), (82, 214, -20), + (82, 216, -20), (82, 217, -40), (82, 218, -40), (82, 219, -40), (82, 220, -40), + (82, 221, -50), (83, 44, -20), (83, 46, -20), (84, 44, -120), (84, 45, -140), + (84, 46, -120), (84, 58, -20), (84, 59, -20), (84, 65, -120), (84, 79, -40), (84, 97, -120), + (84, 101, -120), (84, 111, -120), (84, 114, -120), (84, 117, -120), (84, 119, -120), + (84, 121, -120), (84, 159, -60), (84, 173, -140), (84, 192, -120), (84, 193, -120), + (84, 194, -120), (84, 195, -120), (84, 196, -120), (84, 197, -120), (84, 210, -40), + (84, 211, -40), (84, 212, -40), (84, 213, -40), (84, 214, -40), (84, 216, -40), + (84, 224, -120), (84, 225, -120), (84, 226, -120), (84, 227, -60), (84, 228, -120), + (84, 229, -120), (84, 232, -60), (84, 233, -120), (84, 234, -120), (84, 235, -120), + (84, 242, -120), (84, 243, -120), (84, 244, -120), (84, 245, -60), (84, 246, -120), + (84, 248, -120), (84, 249, -120), (84, 250, -120), (84, 251, -120), (84, 252, -120), + (84, 253, -120), (84, 255, -60), (85, 44, -40), (85, 46, -40), (85, 65, -40), + (85, 192, -40), (85, 193, -40), (85, 194, -40), (85, 195, -40), (85, 196, -40), + (85, 197, -40), (86, 44, -125), (86, 45, -80), (86, 46, -125), (86, 58, -40), (86, 59, -40), + (86, 65, -80), (86, 71, -40), (86, 79, -40), (86, 97, -70), (86, 101, -80), (86, 111, -80), + (86, 117, -70), (86, 173, -80), (86, 192, -80), (86, 193, -80), (86, 194, -80), + (86, 195, -80), (86, 196, -80), (86, 197, -80), (86, 210, -40), (86, 211, -40), + (86, 212, -40), (86, 213, -40), (86, 214, -40), (86, 216, -40), (86, 224, -70), + (86, 225, -70), (86, 226, -70), (86, 227, -70), (86, 228, -70), (86, 229, -70), + (86, 232, -80), (86, 233, -80), (86, 234, -80), (86, 235, -80), (86, 242, -80), + (86, 243, -80), (86, 244, -80), (86, 245, -80), (86, 246, -80), (86, 248, -80), + (86, 249, -70), (86, 250, -70), (86, 251, -70), (86, 252, -70), (87, 44, -80), + (87, 45, -40), (87, 46, -80), (87, 65, -50), (87, 79, -20), (87, 97, -40), (87, 101, -30), + (87, 111, -30), (87, 117, -30), (87, 121, -20), (87, 159, -20), (87, 173, -40), + (87, 192, -50), (87, 193, -50), (87, 194, -50), (87, 195, -50), (87, 196, -50), + (87, 197, -50), (87, 210, -20), (87, 211, -20), (87, 212, -20), (87, 213, -20), + (87, 214, -20), (87, 216, -20), (87, 224, -40), (87, 225, -40), (87, 226, -40), + (87, 227, -40), (87, 228, -40), (87, 229, -40), (87, 232, -30), (87, 233, -30), + (87, 234, -30), (87, 235, -30), (87, 242, -30), (87, 243, -30), (87, 244, -30), + (87, 245, -30), (87, 246, -30), (87, 248, -30), (87, 249, -30), (87, 250, -30), + (87, 251, -30), (87, 252, -30), (87, 253, -20), (87, 255, -20), (89, 44, -140), + (89, 45, -140), (89, 46, -140), (89, 58, -60), (89, 59, -60), (89, 65, -110), (89, 79, -85), + (89, 97, -140), (89, 101, -140), (89, 105, -20), (89, 111, -140), (89, 117, -110), + (89, 173, -140), (89, 192, -110), (89, 193, -110), (89, 194, -110), (89, 195, -110), + (89, 196, -110), (89, 197, -110), (89, 210, -85), (89, 211, -85), (89, 212, -85), + (89, 213, -85), (89, 214, -85), (89, 216, -85), (89, 224, -140), (89, 225, -140), + (89, 226, -140), (89, 227, -140), (89, 228, -140), (89, 229, -140), (89, 232, -140), + (89, 233, -140), (89, 234, -140), (89, 235, -140), (89, 237, -20), (89, 242, -140), + (89, 243, -140), (89, 244, -140), (89, 245, -140), (89, 246, -140), (89, 248, -140), + (89, 249, -110), (89, 250, -110), (89, 251, -110), (89, 252, -110), (97, 118, -20), + (97, 119, -20), (97, 121, -30), (97, 159, -30), (97, 253, -30), (97, 255, -30), + (98, 44, -40), (98, 46, -40), (98, 98, -10), (98, 108, -20), (98, 117, -20), (98, 118, -20), + (98, 121, -20), (98, 159, -20), (98, 249, -20), (98, 250, -20), (98, 251, -20), + (98, 252, -20), (98, 253, -20), (98, 255, -20), (99, 44, -15), (99, 107, -20), + (101, 44, -15), (101, 46, -15), (101, 118, -30), (101, 119, -20), (101, 120, -30), + (101, 121, -20), (101, 159, -20), (101, 253, -20), (101, 255, -20), (102, 44, -30), + (102, 46, -30), (102, 97, -30), (102, 101, -30), (102, 111, -30), (102, 146, 50), + (102, 148, 60), (102, 224, -30), (102, 225, -30), (102, 226, -30), (102, 227, -30), + (102, 228, -30), (102, 229, -30), (102, 232, -30), (102, 233, -30), (102, 234, -30), + (102, 235, -30), (102, 242, -30), (102, 243, -30), (102, 244, -30), (102, 245, -30), + (102, 246, -30), (102, 248, -30), (103, 114, -10), (104, 121, -30), (104, 159, -30), + (104, 253, -30), (104, 255, -30), (107, 101, -20), (107, 111, -20), (107, 232, -20), + (107, 233, -20), (107, 234, -20), (107, 235, -20), (107, 242, -20), (107, 243, -20), + (107, 244, -20), (107, 245, -20), (107, 246, -20), (107, 248, -20), (109, 117, -10), + (109, 121, -15), (109, 159, -15), (109, 249, -10), (109, 250, -10), (109, 251, -10), + (109, 252, -10), (109, 253, -15), (109, 255, -15), (110, 117, -10), (110, 118, -20), + (110, 121, -15), (110, 159, -15), (110, 249, -10), (110, 250, -10), (110, 251, -10), + (110, 252, -10), (110, 253, -15), (110, 255, -15), (111, 44, -40), (111, 46, -40), + (111, 118, -15), (111, 119, -15), (111, 120, -30), (111, 121, -30), (111, 159, -30), + (111, 253, -30), (111, 255, -30), (112, 44, -35), (112, 46, -35), (112, 121, -30), + (112, 159, -30), (112, 253, -30), (112, 255, -30), (114, 44, -50), (114, 46, -50), + (114, 58, 30), (114, 59, 30), (114, 97, -10), (114, 105, 15), (114, 107, 15), + (114, 108, 15), (114, 109, 25), (114, 110, 25), (114, 112, 30), (114, 116, 40), + (114, 117, 15), (114, 118, 30), (114, 121, 30), (114, 159, 30), (114, 224, -10), + (114, 225, -10), (114, 226, -10), (114, 227, -10), (114, 228, -10), (114, 229, -10), + (114, 236, 15), (114, 237, 15), (114, 238, 15), (114, 239, 15), (114, 241, 25), + (114, 249, 15), (114, 250, 15), (114, 251, 15), (114, 252, 15), (114, 253, 30), + (114, 255, 30), (115, 44, -15), (115, 46, -15), (115, 119, -30), (118, 44, -80), + (118, 46, -80), (118, 97, -25), (118, 101, -25), (118, 111, -25), (118, 224, -25), + (118, 225, -25), (118, 226, -25), (118, 227, -25), (118, 228, -25), (118, 229, -25), + (118, 232, -25), (118, 233, -25), (118, 234, -25), (118, 235, -25), (118, 242, -25), + (118, 243, -25), (118, 244, -25), (118, 245, -25), (118, 246, -25), (118, 248, -25), + (119, 44, -60), (119, 46, -60), (119, 97, -15), (119, 101, -10), (119, 111, -10), + (119, 224, -15), (119, 225, -15), (119, 226, -15), (119, 227, -15), (119, 228, -15), + (119, 229, -15), (119, 232, -10), (119, 233, -10), (119, 234, -10), (119, 235, -10), + (119, 242, -10), (119, 243, -10), (119, 244, -10), (119, 245, -10), (119, 246, -10), + (119, 248, -10), (120, 101, -30), (120, 232, -30), (120, 233, -30), (120, 234, -30), + (120, 235, -30), (121, 44, -100), (121, 46, -100), (121, 97, -20), (121, 101, -20), + (121, 111, -20), (121, 224, -20), (121, 225, -20), (121, 226, -20), (121, 227, -20), + (121, 228, -20), (121, 229, -20), (121, 232, -20), (121, 233, -20), (121, 234, -20), + (121, 235, -20), (121, 242, -20), (121, 243, -20), (121, 244, -20), (121, 245, -20), + (121, 246, -20), (121, 248, -20), (122, 101, -15), (122, 111, -15), (122, 232, -15), + (122, 233, -15), (122, 234, -15), (122, 235, -15), (122, 242, -15), (122, 243, -15), + (122, 244, -15), (122, 245, -15), (122, 246, -15), (122, 248, -15), (138, 44, -20), + (138, 46, -20), (145, 145, -57), (146, 32, -70), (146, 100, -50), (146, 114, -50), + (146, 115, -50), (146, 146, -57), (146, 154, -50), (146, 160, -70), (148, 32, -40), + (148, 160, -40), (154, 44, -15), (154, 46, -15), (154, 119, -30), (158, 101, -15), + (158, 111, -15), (158, 232, -15), (158, 233, -15), (158, 234, -15), (158, 235, -15), + (158, 242, -15), (158, 243, -15), (158, 244, -15), (158, 245, -15), (158, 246, -15), + (158, 248, -15), (159, 44, -100), (159, 46, -100), (159, 97, -20), (159, 101, -20), + (159, 111, -20), (159, 224, -20), (159, 225, -20), (159, 226, -20), (159, 227, -20), + (159, 228, -20), (159, 229, -20), (159, 232, -20), (159, 233, -20), (159, 234, -20), + (159, 235, -20), (159, 242, -20), (159, 243, -20), (159, 244, -20), (159, 245, -20), + (159, 246, -20), (159, 248, -20), (160, 84, -50), (160, 86, -50), (160, 87, -40), + (160, 89, -90), (160, 145, -60), (160, 147, -30), (160, 221, -90), (192, 67, -30), + (192, 71, -30), (192, 79, -30), (192, 81, -30), (192, 84, -120), (192, 85, -50), + (192, 86, -70), (192, 87, -50), (192, 89, -100), (192, 117, -30), (192, 118, -40), + (192, 119, -40), (192, 121, -40), (192, 159, -40), (192, 199, -30), (192, 210, -30), + (192, 211, -30), (192, 212, -30), (192, 213, -30), (192, 214, -30), (192, 216, -30), + (192, 217, -50), (192, 218, -50), (192, 219, -50), (192, 220, -50), (192, 221, -100), + (192, 249, -30), (192, 250, -30), (192, 251, -30), (192, 252, -30), (192, 253, -40), + (192, 255, -40), (193, 67, -30), (193, 71, -30), (193, 79, -30), (193, 81, -30), + (193, 84, -120), (193, 85, -50), (193, 86, -70), (193, 87, -50), (193, 89, -100), + (193, 117, -30), (193, 118, -40), (193, 119, -40), (193, 121, -40), (193, 159, -40), + (193, 199, -30), (193, 210, -30), (193, 211, -30), (193, 212, -30), (193, 213, -30), + (193, 214, -30), (193, 216, -30), (193, 217, -50), (193, 218, -50), (193, 219, -50), + (193, 220, -50), (193, 221, -100), (193, 249, -30), (193, 250, -30), (193, 251, -30), + (193, 252, -30), (193, 253, -40), (193, 255, -40), (194, 67, -30), (194, 71, -30), + (194, 79, -30), (194, 81, -30), (194, 84, -120), (194, 85, -50), (194, 86, -70), + (194, 87, -50), (194, 89, -100), (194, 117, -30), (194, 118, -40), (194, 119, -40), + (194, 121, -40), (194, 159, -40), (194, 199, -30), (194, 210, -30), (194, 211, -30), + (194, 212, -30), (194, 213, -30), (194, 214, -30), (194, 216, -30), (194, 217, -50), + (194, 218, -50), (194, 219, -50), (194, 220, -50), (194, 221, -100), (194, 249, -30), + (194, 250, -30), (194, 251, -30), (194, 252, -30), (194, 253, -40), (194, 255, -40), + (195, 67, -30), (195, 71, -30), (195, 79, -30), (195, 81, -30), (195, 84, -120), + (195, 85, -50), (195, 86, -70), (195, 87, -50), (195, 89, -100), (195, 117, -30), + (195, 118, -40), (195, 119, -40), (195, 121, -40), (195, 159, -40), (195, 199, -30), + (195, 210, -30), (195, 211, -30), (195, 212, -30), (195, 213, -30), (195, 214, -30), + (195, 216, -30), (195, 217, -50), (195, 218, -50), (195, 219, -50), (195, 220, -50), + (195, 221, -100), (195, 249, -30), (195, 250, -30), (195, 251, -30), (195, 252, -30), + (195, 253, -40), (195, 255, -40), (196, 67, -30), (196, 71, -30), (196, 79, -30), + (196, 81, -30), (196, 84, -120), (196, 85, -50), (196, 86, -70), (196, 87, -50), + (196, 89, -100), (196, 117, -30), (196, 118, -40), (196, 119, -40), (196, 121, -40), + (196, 159, -40), (196, 199, -30), (196, 210, -30), (196, 211, -30), (196, 212, -30), + (196, 213, -30), (196, 214, -30), (196, 216, -30), (196, 217, -50), (196, 218, -50), + (196, 219, -50), (196, 220, -50), (196, 221, -100), (196, 249, -30), (196, 250, -30), + (196, 251, -30), (196, 252, -30), (196, 253, -40), (196, 255, -40), (197, 67, -30), + (197, 71, -30), (197, 79, -30), (197, 81, -30), (197, 84, -120), (197, 85, -50), + (197, 86, -70), (197, 87, -50), (197, 89, -100), (197, 117, -30), (197, 118, -40), + (197, 119, -40), (197, 121, -40), (197, 159, -40), (197, 199, -30), (197, 210, -30), + (197, 211, -30), (197, 212, -30), (197, 213, -30), (197, 214, -30), (197, 216, -30), + (197, 217, -50), (197, 218, -50), (197, 219, -50), (197, 220, -50), (197, 221, -100), + (197, 249, -30), (197, 250, -30), (197, 251, -30), (197, 252, -30), (197, 253, -40), + (197, 255, -40), (199, 44, -30), (199, 46, -30), (210, 44, -40), (210, 46, -40), + (210, 65, -20), (210, 84, -40), (210, 86, -50), (210, 87, -30), (210, 88, -60), + (210, 89, -70), (210, 192, -20), (210, 193, -20), (210, 194, -20), (210, 195, -20), + (210, 196, -20), (210, 197, -20), (210, 221, -70), (211, 44, -40), (211, 46, -40), + (211, 65, -20), (211, 84, -40), (211, 86, -50), (211, 87, -30), (211, 88, -60), + (211, 89, -70), (211, 192, -20), (211, 193, -20), (211, 194, -20), (211, 195, -20), + (211, 196, -20), (211, 197, -20), (211, 221, -70), (212, 44, -40), (212, 46, -40), + (212, 65, -20), (212, 84, -40), (212, 86, -50), (212, 87, -30), (212, 88, -60), + (212, 89, -70), (212, 192, -20), (212, 193, -20), (212, 194, -20), (212, 195, -20), + (212, 196, -20), (212, 197, -20), (212, 221, -70), (213, 44, -40), (213, 46, -40), + (213, 65, -20), (213, 84, -40), (213, 86, -50), (213, 87, -30), (213, 88, -60), + (213, 89, -70), (213, 192, -20), (213, 193, -20), (213, 194, -20), (213, 195, -20), + (213, 196, -20), (213, 197, -20), (213, 221, -70), (214, 44, -40), (214, 46, -40), + (214, 65, -20), (214, 84, -40), (214, 86, -50), (214, 87, -30), (214, 88, -60), + (214, 89, -70), (214, 192, -20), (214, 193, -20), (214, 194, -20), (214, 195, -20), + (214, 196, -20), (214, 197, -20), (214, 221, -70), (216, 44, -40), (216, 46, -40), + (216, 65, -20), (216, 84, -40), (216, 86, -50), (216, 87, -30), (216, 88, -60), + (216, 89, -70), (216, 192, -20), (216, 193, -20), (216, 194, -20), (216, 195, -20), + (216, 196, -20), (216, 197, -20), (216, 221, -70), (217, 44, -40), (217, 46, -40), + (217, 65, -40), (217, 192, -40), (217, 193, -40), (217, 194, -40), (217, 195, -40), + (217, 196, -40), (217, 197, -40), (218, 44, -40), (218, 46, -40), (218, 65, -40), + (218, 192, -40), (218, 193, -40), (218, 194, -40), (218, 195, -40), (218, 196, -40), + (218, 197, -40), (219, 44, -40), (219, 46, -40), (219, 65, -40), (219, 192, -40), + (219, 193, -40), (219, 194, -40), (219, 195, -40), (219, 196, -40), (219, 197, -40), + (220, 44, -40), (220, 46, -40), (220, 65, -40), (220, 192, -40), (220, 193, -40), + (220, 194, -40), (220, 195, -40), (220, 196, -40), (220, 197, -40), (221, 44, -140), + (221, 45, -140), (221, 46, -140), (221, 58, -60), (221, 59, -60), (221, 65, -110), + (221, 79, -85), (221, 97, -140), (221, 101, -140), (221, 105, -20), (221, 111, -140), + (221, 117, -110), (221, 173, -140), (221, 192, -110), (221, 193, -110), (221, 194, -110), + (221, 195, -110), (221, 196, -110), (221, 197, -110), (221, 210, -85), (221, 211, -85), + (221, 212, -85), (221, 213, -85), (221, 214, -85), (221, 216, -85), (221, 224, -140), + (221, 225, -140), (221, 226, -140), (221, 227, -70), (221, 228, -140), (221, 229, -140), + (221, 232, -140), (221, 233, -140), (221, 234, -140), (221, 235, -140), (221, 237, -20), + (221, 242, -140), (221, 243, -140), (221, 244, -140), (221, 245, -140), (221, 246, -140), + (221, 248, -140), (221, 249, -110), (221, 250, -110), (221, 251, -110), (221, 252, -110), + (224, 118, -20), (224, 119, -20), (224, 121, -30), (224, 159, -30), (224, 253, -30), + (224, 255, -30), (225, 118, -20), (225, 119, -20), (225, 121, -30), (225, 159, -30), + (225, 253, -30), (225, 255, -30), (226, 118, -20), (226, 119, -20), (226, 121, -30), + (226, 159, -30), (226, 253, -30), (226, 255, -30), (227, 118, -20), (227, 119, -20), + (227, 121, -30), (227, 159, -30), (227, 253, -30), (227, 255, -30), (228, 118, -20), + (228, 119, -20), (228, 121, -30), (228, 159, -30), (228, 253, -30), (228, 255, -30), + (229, 118, -20), (229, 119, -20), (229, 121, -30), (229, 159, -30), (229, 253, -30), + (229, 255, -30), (231, 44, -15), (231, 107, -20), (232, 44, -15), (232, 46, -15), + (232, 118, -30), (232, 119, -20), (232, 120, -30), (232, 121, -20), (232, 159, -20), + (232, 253, -20), (232, 255, -20), (233, 44, -15), (233, 46, -15), (233, 118, -30), + (233, 119, -20), (233, 120, -30), (233, 121, -20), (233, 159, -20), (233, 253, -20), + (233, 255, -20), (234, 44, -15), (234, 46, -15), (234, 118, -30), (234, 119, -20), + (234, 120, -30), (234, 121, -20), (234, 159, -20), (234, 253, -20), (234, 255, -20), + (235, 44, -15), (235, 46, -15), (235, 118, -30), (235, 119, -20), (235, 120, -30), + (235, 121, -20), (235, 159, -20), (235, 253, -20), (235, 255, -20), (241, 117, -10), + (241, 118, -20), (241, 121, -15), (241, 159, -15), (241, 249, -10), (241, 250, -10), + (241, 251, -10), (241, 252, -10), (241, 253, -15), (241, 255, -15), (242, 44, -40), + (242, 46, -40), (242, 118, -15), (242, 119, -15), (242, 120, -30), (242, 121, -30), + (242, 159, -30), (242, 253, -30), (242, 255, -30), (243, 44, -40), (243, 46, -40), + (243, 118, -15), (243, 119, -15), (243, 120, -30), (243, 121, -30), (243, 159, -30), + (243, 253, -30), (243, 255, -30), (244, 44, -40), (244, 46, -40), (244, 118, -15), + (244, 119, -15), (244, 120, -30), (244, 121, -30), (244, 159, -30), (244, 253, -30), + (244, 255, -30), (245, 44, -40), (245, 46, -40), (245, 118, -15), (245, 119, -15), + (245, 120, -30), (245, 121, -30), (245, 159, -30), (245, 253, -30), (245, 255, -30), + (246, 44, -40), (246, 46, -40), (246, 118, -15), (246, 119, -15), (246, 120, -30), + (246, 121, -30), (246, 159, -30), (246, 253, -30), (246, 255, -30), (248, 44, -95), + (248, 46, -95), (248, 97, -55), (248, 98, -55), (248, 99, -55), (248, 100, -55), + (248, 101, -55), (248, 102, -55), (248, 103, -55), (248, 104, -55), (248, 105, -55), + (248, 106, -55), (248, 107, -55), (248, 108, -55), (248, 109, -55), (248, 110, -55), + (248, 111, -55), (248, 112, -55), (248, 113, -55), (248, 114, -55), (248, 115, -55), + (248, 116, -55), (248, 117, -55), (248, 118, -70), (248, 119, -70), (248, 120, -85), + (248, 121, -70), (248, 122, -55), (248, 154, -55), (248, 158, -55), (248, 159, -70), + (248, 224, -55), (248, 225, -55), (248, 226, -55), (248, 227, -55), (248, 228, -55), + (248, 229, -55), (248, 231, -55), (248, 232, -55), (248, 233, -55), (248, 234, -55), + (248, 235, -55), (248, 236, -55), (248, 237, -55), (248, 238, -55), (248, 239, -55), + (248, 241, -55), (248, 242, -55), (248, 243, -55), (248, 244, -55), (248, 245, -55), + (248, 246, -55), (248, 248, -55), (248, 249, -55), (248, 250, -55), (248, 251, -55), + (248, 252, -55), (248, 253, -70), (248, 255, -70), (253, 44, -100), (253, 46, -100), + (253, 97, -20), (253, 101, -20), (253, 111, -20), (253, 224, -20), (253, 225, -20), + (253, 226, -20), (253, 227, -20), (253, 228, -20), (253, 229, -20), (253, 232, -20), + (253, 233, -20), (253, 234, -20), (253, 235, -20), (253, 242, -20), (253, 243, -20), + (253, 244, -20), (253, 245, -20), (253, 246, -20), (253, 248, -20), (255, 44, -100), + (255, 46, -100), (255, 97, -20), (255, 101, -20), (255, 111, -20), (255, 224, -20), + (255, 225, -20), (255, 226, -20), (255, 227, -20), (255, 228, -20), (255, 229, -20), + (255, 232, -20), (255, 233, -20), (255, 234, -20), (255, 235, -20), (255, 242, -20), + (255, 243, -20), (255, 244, -20), (255, 245, -20), (255, 246, -20), (255, 248, -20), + ], +}; + +/// Helvetica-Bold: 1181 kerning pairs between WinAnsi glyphs. +#[rustfmt::skip] +pub static HELVETICA_BOLD: FontMetrics = FontMetrics { + widths: [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 278, 333, 474, 556, 556, 889, 722, 238, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, + 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611, 975, 722, 722, 722, + 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, + 667, 944, 667, 667, 611, 333, 278, 333, 584, 556, 333, 556, 611, 556, 611, 556, 333, 611, + 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, + 500, 389, 280, 389, 584, 0, 556, 0, 278, 556, 500, 1000, 556, 556, 333, 1000, 667, 333, + 1000, 0, 611, 0, 0, 278, 278, 500, 500, 350, 556, 1000, 333, 1000, 556, 333, 944, 0, 500, + 556, 278, 333, 556, 556, 556, 556, 280, 556, 333, 737, 370, 556, 584, 333, 737, 333, 400, + 584, 333, 333, 333, 611, 556, 278, 333, 333, 365, 556, 834, 834, 834, 611, 722, 722, 722, + 722, 722, 722, 1000, 722, 667, 667, 667, 667, 278, 278, 278, 278, 722, 722, 778, 778, 778, + 778, 778, 584, 778, 722, 722, 722, 722, 667, 667, 611, 556, 556, 556, 556, 556, 556, 889, + 556, 556, 556, 556, 556, 278, 278, 278, 278, 611, 611, 611, 611, 611, 611, 611, 584, 611, + 611, 611, 611, 611, 556, 611, 556, + ], + kerning: &[ + (32, 84, -100), (32, 86, -80), (32, 87, -80), (32, 89, -120), (32, 145, -60), + (32, 147, -80), (32, 221, -120), (44, 32, -40), (44, 146, -120), (44, 148, -120), + (44, 160, -40), (46, 32, -40), (46, 146, -120), (46, 148, -120), (46, 160, -40), + (58, 32, -40), (58, 160, -40), (59, 32, -40), (59, 160, -40), (65, 67, -40), (65, 71, -50), + (65, 79, -40), (65, 81, -40), (65, 84, -90), (65, 85, -50), (65, 86, -80), (65, 87, -60), + (65, 89, -110), (65, 117, -30), (65, 118, -40), (65, 119, -30), (65, 121, -30), + (65, 159, -30), (65, 199, -40), (65, 210, -40), (65, 211, -40), (65, 212, -40), + (65, 213, -40), (65, 214, -40), (65, 216, -40), (65, 217, -50), (65, 218, -50), + (65, 219, -50), (65, 220, -50), (65, 221, -110), (65, 249, -30), (65, 250, -30), + (65, 251, -30), (65, 252, -30), (65, 253, -30), (65, 255, -30), (66, 65, -30), + (66, 85, -10), (66, 192, -30), (66, 193, -30), (66, 194, -30), (66, 195, -30), + (66, 196, -30), (66, 197, -30), (66, 217, -10), (66, 218, -10), (66, 219, -10), + (66, 220, -10), (68, 44, -30), (68, 46, -30), (68, 65, -40), (68, 86, -40), (68, 87, -40), + (68, 89, -70), (68, 192, -40), (68, 193, -40), (68, 194, -40), (68, 195, -40), + (68, 196, -40), (68, 197, -40), (68, 221, -70), (70, 44, -100), (70, 46, -100), + (70, 65, -80), (70, 97, -20), (70, 192, -80), (70, 193, -80), (70, 194, -80), + (70, 195, -80), (70, 196, -80), (70, 197, -80), (70, 224, -20), (70, 225, -20), + (70, 226, -20), (70, 227, -20), (70, 228, -20), (70, 229, -20), (74, 44, -20), + (74, 46, -20), (74, 65, -20), (74, 117, -20), (74, 192, -20), (74, 193, -20), + (74, 194, -20), (74, 195, -20), (74, 196, -20), (74, 197, -20), (74, 249, -20), + (74, 250, -20), (74, 251, -20), (74, 252, -20), (75, 79, -30), (75, 101, -15), + (75, 111, -35), (75, 117, -30), (75, 121, -40), (75, 159, -40), (75, 210, -30), + (75, 211, -30), (75, 212, -30), (75, 213, -30), (75, 214, -30), (75, 216, -30), + (75, 232, -15), (75, 233, -15), (75, 234, -15), (75, 235, -15), (75, 242, -35), + (75, 243, -35), (75, 244, -35), (75, 245, -35), (75, 246, -35), (75, 248, -35), + (75, 249, -30), (75, 250, -30), (75, 251, -30), (75, 252, -30), (75, 253, -40), + (75, 255, -40), (76, 84, -90), (76, 86, -110), (76, 87, -80), (76, 89, -120), + (76, 121, -30), (76, 146, -140), (76, 148, -140), (76, 159, -30), (76, 221, -120), + (76, 253, -30), (76, 255, -30), (79, 44, -40), (79, 46, -40), (79, 65, -50), (79, 84, -40), + (79, 86, -50), (79, 87, -50), (79, 88, -50), (79, 89, -70), (79, 192, -50), (79, 193, -50), + (79, 194, -50), (79, 195, -50), (79, 196, -50), (79, 197, -50), (79, 221, -70), + (80, 44, -120), (80, 46, -120), (80, 65, -100), (80, 97, -30), (80, 101, -30), + (80, 111, -40), (80, 192, -100), (80, 193, -100), (80, 194, -100), (80, 195, -100), + (80, 196, -100), (80, 197, -100), (80, 224, -30), (80, 225, -30), (80, 226, -30), + (80, 227, -30), (80, 228, -30), (80, 229, -30), (80, 232, -30), (80, 233, -30), + (80, 234, -30), (80, 235, -30), (80, 242, -40), (80, 243, -40), (80, 244, -40), + (80, 245, -40), (80, 246, -40), (80, 248, -40), (81, 44, 20), (81, 46, 20), (81, 85, -10), + (81, 217, -10), (81, 218, -10), (81, 219, -10), (81, 220, -10), (82, 79, -20), + (82, 84, -20), (82, 85, -20), (82, 86, -50), (82, 87, -40), (82, 89, -50), (82, 210, -20), + (82, 211, -20), (82, 212, -20), (82, 213, -20), (82, 214, -20), (82, 216, -20), + (82, 217, -20), (82, 218, -20), (82, 219, -20), (82, 220, -20), (82, 221, -50), + (84, 44, -80), (84, 45, -120), (84, 46, -80), (84, 58, -40), (84, 59, -40), (84, 65, -90), + (84, 79, -40), (84, 97, -80), (84, 101, -60), (84, 111, -80), (84, 114, -80), + (84, 117, -90), (84, 119, -60), (84, 121, -60), (84, 159, -60), (84, 173, -120), + (84, 192, -90), (84, 193, -90), (84, 194, -90), (84, 195, -90), (84, 196, -90), + (84, 197, -90), (84, 210, -40), (84, 211, -40), (84, 212, -40), (84, 213, -40), + (84, 214, -40), (84, 216, -40), (84, 224, -80), (84, 225, -80), (84, 226, -80), + (84, 227, -80), (84, 228, -80), (84, 229, -80), (84, 232, -60), (84, 233, -60), + (84, 234, -60), (84, 235, -60), (84, 242, -80), (84, 243, -80), (84, 244, -80), + (84, 245, -80), (84, 246, -80), (84, 248, -80), (84, 249, -90), (84, 250, -90), + (84, 251, -90), (84, 252, -90), (84, 253, -60), (84, 255, -60), (85, 44, -30), + (85, 46, -30), (85, 65, -50), (85, 192, -50), (85, 193, -50), (85, 194, -50), + (85, 195, -50), (85, 196, -50), (85, 197, -50), (86, 44, -120), (86, 45, -80), + (86, 46, -120), (86, 58, -40), (86, 59, -40), (86, 65, -80), (86, 71, -50), (86, 79, -50), + (86, 97, -60), (86, 101, -50), (86, 111, -90), (86, 117, -60), (86, 173, -80), + (86, 192, -80), (86, 193, -80), (86, 194, -80), (86, 195, -80), (86, 196, -80), + (86, 197, -80), (86, 210, -50), (86, 211, -50), (86, 212, -50), (86, 213, -50), + (86, 214, -50), (86, 216, -50), (86, 224, -60), (86, 225, -60), (86, 226, -60), + (86, 227, -60), (86, 228, -60), (86, 229, -60), (86, 232, -50), (86, 233, -50), + (86, 234, -50), (86, 235, -50), (86, 242, -90), (86, 243, -90), (86, 244, -90), + (86, 245, -90), (86, 246, -90), (86, 248, -90), (86, 249, -60), (86, 250, -60), + (86, 251, -60), (86, 252, -60), (87, 44, -80), (87, 45, -40), (87, 46, -80), (87, 58, -10), + (87, 59, -10), (87, 65, -60), (87, 79, -20), (87, 97, -40), (87, 101, -35), (87, 111, -60), + (87, 117, -45), (87, 121, -20), (87, 159, -20), (87, 173, -40), (87, 192, -60), + (87, 193, -60), (87, 194, -60), (87, 195, -60), (87, 196, -60), (87, 197, -60), + (87, 210, -20), (87, 211, -20), (87, 212, -20), (87, 213, -20), (87, 214, -20), + (87, 216, -20), (87, 224, -40), (87, 225, -40), (87, 226, -40), (87, 227, -40), + (87, 228, -40), (87, 229, -40), (87, 232, -35), (87, 233, -35), (87, 234, -35), + (87, 235, -35), (87, 242, -60), (87, 243, -60), (87, 244, -60), (87, 245, -60), + (87, 246, -60), (87, 248, -60), (87, 249, -45), (87, 250, -45), (87, 251, -45), + (87, 252, -45), (87, 253, -20), (87, 255, -20), (89, 44, -100), (89, 46, -100), + (89, 58, -50), (89, 59, -50), (89, 65, -110), (89, 79, -70), (89, 97, -90), (89, 101, -80), + (89, 111, -100), (89, 117, -100), (89, 192, -110), (89, 193, -110), (89, 194, -110), + (89, 195, -110), (89, 196, -110), (89, 197, -110), (89, 210, -70), (89, 211, -70), + (89, 212, -70), (89, 213, -70), (89, 214, -70), (89, 216, -70), (89, 224, -90), + (89, 225, -90), (89, 226, -90), (89, 227, -90), (89, 228, -90), (89, 229, -90), + (89, 232, -80), (89, 233, -80), (89, 234, -80), (89, 235, -80), (89, 242, -100), + (89, 243, -100), (89, 244, -100), (89, 245, -100), (89, 246, -100), (89, 248, -100), + (89, 249, -100), (89, 250, -100), (89, 251, -100), (89, 252, -100), (97, 103, -10), + (97, 118, -15), (97, 119, -15), (97, 121, -20), (97, 159, -20), (97, 253, -20), + (97, 255, -20), (98, 108, -10), (98, 117, -20), (98, 118, -20), (98, 121, -20), + (98, 159, -20), (98, 249, -20), (98, 250, -20), (98, 251, -20), (98, 252, -20), + (98, 253, -20), (98, 255, -20), (99, 104, -10), (99, 107, -20), (99, 108, -20), + (99, 121, -10), (99, 159, -10), (99, 253, -10), (99, 255, -10), (100, 100, -10), + (100, 118, -15), (100, 119, -15), (100, 121, -15), (100, 159, -15), (100, 253, -15), + (100, 255, -15), (101, 44, 10), (101, 46, 20), (101, 118, -15), (101, 119, -15), + (101, 120, -15), (101, 121, -15), (101, 159, -15), (101, 253, -15), (101, 255, -15), + (102, 44, -10), (102, 46, -10), (102, 101, -10), (102, 111, -20), (102, 146, 30), + (102, 148, 30), (102, 232, -10), (102, 233, -10), (102, 234, -10), (102, 235, -10), + (102, 242, -20), (102, 243, -20), (102, 244, -20), (102, 245, -20), (102, 246, -20), + (102, 248, -20), (103, 101, 10), (103, 103, -10), (103, 232, 10), (103, 233, 10), + (103, 234, 10), (103, 235, 10), (104, 121, -20), (104, 159, -20), (104, 253, -20), + (104, 255, -20), (107, 111, -15), (107, 242, -15), (107, 243, -15), (107, 244, -15), + (107, 245, -15), (107, 246, -15), (107, 248, -15), (108, 119, -15), (108, 121, -15), + (108, 159, -15), (108, 253, -15), (108, 255, -15), (109, 117, -20), (109, 121, -30), + (109, 159, -30), (109, 249, -20), (109, 250, -20), (109, 251, -20), (109, 252, -20), + (109, 253, -30), (109, 255, -30), (110, 117, -10), (110, 118, -40), (110, 121, -20), + (110, 159, -20), (110, 249, -10), (110, 250, -10), (110, 251, -10), (110, 252, -10), + (110, 253, -20), (110, 255, -20), (111, 118, -20), (111, 119, -15), (111, 120, -30), + (111, 121, -20), (111, 159, -20), (111, 253, -20), (111, 255, -20), (112, 121, -15), + (112, 159, -15), (112, 253, -15), (112, 255, -15), (114, 44, -60), (114, 45, -20), + (114, 46, -60), (114, 99, -20), (114, 100, -20), (114, 103, -15), (114, 111, -20), + (114, 113, -20), (114, 115, -15), (114, 116, 20), (114, 118, 10), (114, 121, 10), + (114, 154, -15), (114, 159, 10), (114, 173, -20), (114, 231, -20), (114, 242, -20), + (114, 243, -20), (114, 244, -20), (114, 245, -20), (114, 246, -20), (114, 248, -20), + (114, 253, 10), (114, 255, 10), (115, 119, -15), (118, 44, -80), (118, 46, -80), + (118, 97, -20), (118, 111, -30), (118, 224, -20), (118, 225, -20), (118, 226, -20), + (118, 227, -20), (118, 228, -20), (118, 229, -20), (118, 242, -30), (118, 243, -30), + (118, 244, -30), (118, 245, -30), (118, 246, -30), (118, 248, -30), (119, 44, -40), + (119, 46, -40), (119, 111, -20), (119, 242, -20), (119, 243, -20), (119, 244, -20), + (119, 245, -20), (119, 246, -20), (119, 248, -20), (120, 101, -10), (120, 232, -10), + (120, 233, -10), (120, 234, -10), (120, 235, -10), (121, 44, -80), (121, 46, -80), + (121, 97, -30), (121, 101, -10), (121, 111, -25), (121, 224, -30), (121, 225, -30), + (121, 226, -30), (121, 227, -30), (121, 228, -30), (121, 229, -30), (121, 232, -10), + (121, 233, -10), (121, 234, -10), (121, 235, -10), (121, 242, -25), (121, 243, -25), + (121, 244, -25), (121, 245, -25), (121, 246, -25), (121, 248, -25), (122, 101, 10), + (122, 232, 10), (122, 233, 10), (122, 234, 10), (122, 235, 10), (145, 145, -46), + (146, 32, -80), (146, 100, -80), (146, 108, -20), (146, 114, -40), (146, 115, -60), + (146, 118, -20), (146, 146, -46), (146, 154, -60), (146, 160, -80), (148, 32, -80), + (148, 160, -80), (154, 119, -15), (158, 101, 10), (158, 232, 10), (158, 233, 10), + (158, 234, 10), (158, 235, 10), (159, 44, -80), (159, 46, -80), (159, 97, -30), + (159, 101, -10), (159, 111, -25), (159, 224, -30), (159, 225, -30), (159, 226, -30), + (159, 227, -30), (159, 228, -30), (159, 229, -30), (159, 232, -10), (159, 233, -10), + (159, 234, -10), (159, 235, -10), (159, 242, -25), (159, 243, -25), (159, 244, -25), + (159, 245, -25), (159, 246, -25), (159, 248, -25), (160, 84, -100), (160, 86, -80), + (160, 87, -80), (160, 89, -120), (160, 145, -60), (160, 147, -80), (160, 221, -120), + (192, 67, -40), (192, 71, -50), (192, 79, -40), (192, 81, -40), (192, 84, -90), + (192, 85, -50), (192, 86, -80), (192, 87, -60), (192, 89, -110), (192, 117, -30), + (192, 118, -40), (192, 119, -30), (192, 121, -30), (192, 159, -30), (192, 199, -40), + (192, 210, -40), (192, 211, -40), (192, 212, -40), (192, 213, -40), (192, 214, -40), + (192, 216, -40), (192, 217, -50), (192, 218, -50), (192, 219, -50), (192, 220, -50), + (192, 221, -110), (192, 249, -30), (192, 250, -30), (192, 251, -30), (192, 252, -30), + (192, 253, -30), (192, 255, -30), (193, 67, -40), (193, 71, -50), (193, 79, -40), + (193, 81, -40), (193, 84, -90), (193, 85, -50), (193, 86, -80), (193, 87, -60), + (193, 89, -110), (193, 117, -30), (193, 118, -40), (193, 119, -30), (193, 121, -30), + (193, 159, -30), (193, 199, -40), (193, 210, -40), (193, 211, -40), (193, 212, -40), + (193, 213, -40), (193, 214, -40), (193, 216, -40), (193, 217, -50), (193, 218, -50), + (193, 219, -50), (193, 220, -50), (193, 221, -110), (193, 249, -30), (193, 250, -30), + (193, 251, -30), (193, 252, -30), (193, 253, -30), (193, 255, -30), (194, 67, -40), + (194, 71, -50), (194, 79, -40), (194, 81, -40), (194, 84, -90), (194, 85, -50), + (194, 86, -80), (194, 87, -60), (194, 89, -110), (194, 117, -30), (194, 118, -40), + (194, 119, -30), (194, 121, -30), (194, 159, -30), (194, 199, -40), (194, 210, -40), + (194, 211, -40), (194, 212, -40), (194, 213, -40), (194, 214, -40), (194, 216, -40), + (194, 217, -50), (194, 218, -50), (194, 219, -50), (194, 220, -50), (194, 221, -110), + (194, 249, -30), (194, 250, -30), (194, 251, -30), (194, 252, -30), (194, 253, -30), + (194, 255, -30), (195, 67, -40), (195, 71, -50), (195, 79, -40), (195, 81, -40), + (195, 84, -90), (195, 85, -50), (195, 86, -80), (195, 87, -60), (195, 89, -110), + (195, 117, -30), (195, 118, -40), (195, 119, -30), (195, 121, -30), (195, 159, -30), + (195, 199, -40), (195, 210, -40), (195, 211, -40), (195, 212, -40), (195, 213, -40), + (195, 214, -40), (195, 216, -40), (195, 217, -50), (195, 218, -50), (195, 219, -50), + (195, 220, -50), (195, 221, -110), (195, 249, -30), (195, 250, -30), (195, 251, -30), + (195, 252, -30), (195, 253, -30), (195, 255, -30), (196, 67, -40), (196, 71, -50), + (196, 79, -40), (196, 81, -40), (196, 84, -90), (196, 85, -50), (196, 86, -80), + (196, 87, -60), (196, 89, -110), (196, 117, -30), (196, 118, -40), (196, 119, -30), + (196, 121, -30), (196, 159, -30), (196, 199, -40), (196, 210, -40), (196, 211, -40), + (196, 212, -40), (196, 213, -40), (196, 214, -40), (196, 216, -40), (196, 217, -50), + (196, 218, -50), (196, 219, -50), (196, 220, -50), (196, 221, -110), (196, 249, -30), + (196, 250, -30), (196, 251, -30), (196, 252, -30), (196, 253, -30), (196, 255, -30), + (197, 67, -40), (197, 71, -50), (197, 79, -40), (197, 81, -40), (197, 84, -90), + (197, 85, -50), (197, 86, -80), (197, 87, -60), (197, 89, -110), (197, 117, -30), + (197, 118, -40), (197, 119, -30), (197, 121, -30), (197, 159, -30), (197, 199, -40), + (197, 210, -40), (197, 211, -40), (197, 212, -40), (197, 213, -40), (197, 214, -40), + (197, 216, -40), (197, 217, -50), (197, 218, -50), (197, 219, -50), (197, 220, -50), + (197, 221, -110), (197, 249, -30), (197, 250, -30), (197, 251, -30), (197, 252, -30), + (197, 253, -30), (197, 255, -30), (210, 44, -40), (210, 46, -40), (210, 65, -50), + (210, 84, -40), (210, 86, -50), (210, 87, -50), (210, 88, -50), (210, 89, -70), + (210, 192, -50), (210, 193, -50), (210, 194, -50), (210, 195, -50), (210, 196, -50), + (210, 197, -50), (210, 221, -70), (211, 44, -40), (211, 46, -40), (211, 65, -50), + (211, 84, -40), (211, 86, -50), (211, 87, -50), (211, 88, -50), (211, 89, -70), + (211, 192, -50), (211, 193, -50), (211, 194, -50), (211, 195, -50), (211, 196, -50), + (211, 197, -50), (211, 221, -70), (212, 44, -40), (212, 46, -40), (212, 65, -50), + (212, 84, -40), (212, 86, -50), (212, 87, -50), (212, 88, -50), (212, 89, -70), + (212, 192, -50), (212, 193, -50), (212, 194, -50), (212, 195, -50), (212, 196, -50), + (212, 197, -50), (212, 221, -70), (213, 44, -40), (213, 46, -40), (213, 65, -50), + (213, 84, -40), (213, 86, -50), (213, 87, -50), (213, 88, -50), (213, 89, -70), + (213, 192, -50), (213, 193, -50), (213, 194, -50), (213, 195, -50), (213, 196, -50), + (213, 197, -50), (213, 221, -70), (214, 44, -40), (214, 46, -40), (214, 65, -50), + (214, 84, -40), (214, 86, -50), (214, 87, -50), (214, 88, -50), (214, 89, -70), + (214, 192, -50), (214, 193, -50), (214, 194, -50), (214, 195, -50), (214, 196, -50), + (214, 197, -50), (214, 221, -70), (216, 44, -40), (216, 46, -40), (216, 65, -50), + (216, 84, -40), (216, 86, -50), (216, 87, -50), (216, 88, -50), (216, 89, -70), + (216, 192, -50), (216, 193, -50), (216, 194, -50), (216, 195, -50), (216, 196, -50), + (216, 197, -50), (216, 221, -70), (217, 44, -30), (217, 46, -30), (217, 65, -50), + (217, 192, -50), (217, 193, -50), (217, 194, -50), (217, 195, -50), (217, 196, -50), + (217, 197, -50), (218, 44, -30), (218, 46, -30), (218, 65, -50), (218, 192, -50), + (218, 193, -50), (218, 194, -50), (218, 195, -50), (218, 196, -50), (218, 197, -50), + (219, 44, -30), (219, 46, -30), (219, 65, -50), (219, 192, -50), (219, 193, -50), + (219, 194, -50), (219, 195, -50), (219, 196, -50), (219, 197, -50), (220, 44, -30), + (220, 46, -30), (220, 65, -50), (220, 192, -50), (220, 193, -50), (220, 194, -50), + (220, 195, -50), (220, 196, -50), (220, 197, -50), (221, 44, -100), (221, 46, -100), + (221, 58, -50), (221, 59, -50), (221, 65, -110), (221, 79, -70), (221, 97, -90), + (221, 101, -80), (221, 111, -100), (221, 117, -100), (221, 192, -110), (221, 193, -110), + (221, 194, -110), (221, 195, -110), (221, 196, -110), (221, 197, -110), (221, 210, -70), + (221, 211, -70), (221, 212, -70), (221, 213, -70), (221, 214, -70), (221, 216, -70), + (221, 224, -90), (221, 225, -90), (221, 226, -90), (221, 227, -90), (221, 228, -90), + (221, 229, -90), (221, 232, -80), (221, 233, -80), (221, 234, -80), (221, 235, -80), + (221, 242, -100), (221, 243, -100), (221, 244, -100), (221, 245, -100), (221, 246, -100), + (221, 248, -100), (221, 249, -100), (221, 250, -100), (221, 251, -100), (221, 252, -100), + (224, 103, -10), (224, 118, -15), (224, 119, -15), (224, 121, -20), (224, 159, -20), + (224, 253, -20), (224, 255, -20), (225, 103, -10), (225, 118, -15), (225, 119, -15), + (225, 121, -20), (225, 159, -20), (225, 253, -20), (225, 255, -20), (226, 103, -10), + (226, 118, -15), (226, 119, -15), (226, 121, -20), (226, 159, -20), (226, 253, -20), + (226, 255, -20), (227, 103, -10), (227, 118, -15), (227, 119, -15), (227, 121, -20), + (227, 159, -20), (227, 253, -20), (227, 255, -20), (228, 103, -10), (228, 118, -15), + (228, 119, -15), (228, 121, -20), (228, 159, -20), (228, 253, -20), (228, 255, -20), + (229, 103, -10), (229, 118, -15), (229, 119, -15), (229, 121, -20), (229, 159, -20), + (229, 253, -20), (229, 255, -20), (231, 104, -10), (231, 107, -20), (231, 108, -20), + (231, 121, -10), (231, 159, -10), (231, 253, -10), (231, 255, -10), (232, 44, 10), + (232, 46, 20), (232, 118, -15), (232, 119, -15), (232, 120, -15), (232, 121, -15), + (232, 159, -15), (232, 253, -15), (232, 255, -15), (233, 44, 10), (233, 46, 20), + (233, 118, -15), (233, 119, -15), (233, 120, -15), (233, 121, -15), (233, 159, -15), + (233, 253, -15), (233, 255, -15), (234, 44, 10), (234, 46, 20), (234, 118, -15), + (234, 119, -15), (234, 120, -15), (234, 121, -15), (234, 159, -15), (234, 253, -15), + (234, 255, -15), (235, 44, 10), (235, 46, 20), (235, 118, -15), (235, 119, -15), + (235, 120, -15), (235, 121, -15), (235, 159, -15), (235, 253, -15), (235, 255, -15), + (241, 117, -10), (241, 118, -40), (241, 121, -20), (241, 159, -20), (241, 249, -10), + (241, 250, -10), (241, 251, -10), (241, 252, -10), (241, 253, -20), (241, 255, -20), + (242, 118, -20), (242, 119, -15), (242, 120, -30), (242, 121, -20), (242, 159, -20), + (242, 253, -20), (242, 255, -20), (243, 118, -20), (243, 119, -15), (243, 120, -30), + (243, 121, -20), (243, 159, -20), (243, 253, -20), (243, 255, -20), (244, 118, -20), + (244, 119, -15), (244, 120, -30), (244, 121, -20), (244, 159, -20), (244, 253, -20), + (244, 255, -20), (245, 118, -20), (245, 119, -15), (245, 120, -30), (245, 121, -20), + (245, 159, -20), (245, 253, -20), (245, 255, -20), (246, 118, -20), (246, 119, -15), + (246, 120, -30), (246, 121, -20), (246, 159, -20), (246, 253, -20), (246, 255, -20), + (248, 118, -20), (248, 119, -15), (248, 120, -30), (248, 121, -20), (248, 159, -20), + (248, 253, -20), (248, 255, -20), (253, 44, -80), (253, 46, -80), (253, 97, -30), + (253, 101, -10), (253, 111, -25), (253, 224, -30), (253, 225, -30), (253, 226, -30), + (253, 227, -30), (253, 228, -30), (253, 229, -30), (253, 232, -10), (253, 233, -10), + (253, 234, -10), (253, 235, -10), (253, 242, -25), (253, 243, -25), (253, 244, -25), + (253, 245, -25), (253, 246, -25), (253, 248, -25), (255, 44, -80), (255, 46, -80), + (255, 97, -30), (255, 101, -10), (255, 111, -25), (255, 224, -30), (255, 225, -30), + (255, 226, -30), (255, 227, -30), (255, 228, -30), (255, 229, -30), (255, 232, -10), + (255, 233, -10), (255, 234, -10), (255, 235, -10), (255, 242, -25), (255, 243, -25), + (255, 244, -25), (255, 245, -25), (255, 246, -25), (255, 248, -25), + ], +}; + +/// Times-Roman: 986 kerning pairs between WinAnsi glyphs. +#[rustfmt::skip] +pub static TIMES_ROMAN: FontMetrics = FontMetrics { + widths: [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 250, 333, 408, 500, 500, 833, 778, 180, 333, 333, 500, 564, 250, 333, 250, 278, 500, 500, + 500, 500, 500, 500, 500, 500, 500, 500, 278, 278, 564, 564, 564, 444, 921, 722, 667, 667, + 722, 611, 556, 722, 722, 333, 389, 722, 611, 889, 722, 722, 556, 722, 667, 556, 611, 722, + 722, 944, 722, 722, 611, 333, 278, 333, 469, 500, 333, 444, 500, 444, 500, 444, 333, 500, + 500, 278, 278, 500, 278, 778, 500, 500, 500, 500, 333, 389, 278, 500, 500, 722, 500, 500, + 444, 480, 200, 480, 541, 0, 500, 0, 333, 500, 444, 1000, 500, 500, 333, 1000, 556, 333, 889, + 0, 611, 0, 0, 333, 333, 444, 444, 350, 500, 1000, 333, 980, 389, 333, 722, 0, 444, 500, 250, + 333, 500, 500, 500, 500, 200, 500, 333, 760, 276, 500, 564, 333, 760, 333, 400, 564, 300, + 300, 333, 500, 453, 250, 333, 300, 310, 500, 750, 750, 750, 444, 722, 722, 722, 722, 722, + 722, 889, 667, 611, 611, 611, 611, 333, 333, 333, 333, 722, 722, 722, 722, 722, 722, 722, + 564, 722, 722, 722, 722, 722, 722, 556, 500, 444, 444, 444, 444, 444, 444, 667, 444, 444, + 444, 444, 444, 278, 278, 278, 278, 500, 500, 500, 500, 500, 500, 500, 564, 500, 500, 500, + 500, 500, 500, 500, 500, + ], + kerning: &[ + (32, 65, -55), (32, 84, -18), (32, 86, -50), (32, 87, -30), (32, 89, -90), (32, 192, -55), + (32, 193, -55), (32, 194, -55), (32, 195, -55), (32, 196, -55), (32, 197, -55), + (32, 221, -90), (44, 146, -70), (44, 148, -70), (46, 146, -70), (46, 148, -70), + (65, 67, -40), (65, 71, -40), (65, 79, -55), (65, 81, -55), (65, 84, -111), (65, 85, -55), + (65, 86, -135), (65, 87, -90), (65, 89, -105), (65, 118, -74), (65, 119, -92), + (65, 121, -92), (65, 146, -111), (65, 159, -92), (65, 199, -40), (65, 210, -55), + (65, 211, -55), (65, 212, -55), (65, 213, -55), (65, 214, -55), (65, 216, -55), + (65, 217, -55), (65, 218, -55), (65, 219, -55), (65, 220, -55), (65, 221, -105), + (65, 253, -92), (65, 255, -92), (66, 65, -35), (66, 85, -10), (66, 192, -35), + (66, 193, -35), (66, 194, -35), (66, 195, -35), (66, 196, -35), (66, 197, -35), + (66, 217, -10), (66, 218, -10), (66, 219, -10), (66, 220, -10), (68, 65, -40), + (68, 86, -40), (68, 87, -30), (68, 89, -55), (68, 192, -40), (68, 193, -40), (68, 194, -40), + (68, 195, -40), (68, 196, -40), (68, 197, -40), (68, 221, -55), (70, 44, -80), + (70, 46, -80), (70, 65, -74), (70, 97, -15), (70, 111, -15), (70, 192, -74), (70, 193, -74), + (70, 194, -74), (70, 195, -74), (70, 196, -74), (70, 197, -74), (70, 224, -15), + (70, 225, -15), (70, 226, -15), (70, 227, -15), (70, 228, -15), (70, 229, -15), + (70, 242, -15), (70, 243, -15), (70, 244, -15), (70, 245, -15), (70, 246, -15), + (70, 248, -15), (74, 65, -60), (74, 192, -60), (74, 193, -60), (74, 194, -60), + (74, 195, -60), (74, 196, -60), (74, 197, -60), (75, 79, -30), (75, 101, -25), + (75, 111, -35), (75, 117, -15), (75, 121, -25), (75, 159, -25), (75, 210, -30), + (75, 211, -30), (75, 212, -30), (75, 213, -30), (75, 214, -30), (75, 216, -30), + (75, 232, -25), (75, 233, -25), (75, 234, -25), (75, 235, -25), (75, 242, -35), + (75, 243, -35), (75, 244, -35), (75, 245, -35), (75, 246, -35), (75, 248, -35), + (75, 249, -15), (75, 250, -15), (75, 251, -15), (75, 252, -15), (75, 253, -25), + (75, 255, -25), (76, 84, -92), (76, 86, -100), (76, 87, -74), (76, 89, -100), + (76, 121, -55), (76, 146, -92), (76, 159, -55), (76, 221, -100), (76, 253, -55), + (76, 255, -55), (78, 65, -35), (78, 192, -35), (78, 193, -35), (78, 194, -35), + (78, 195, -35), (78, 196, -35), (78, 197, -35), (79, 65, -35), (79, 84, -40), (79, 86, -50), + (79, 87, -35), (79, 88, -40), (79, 89, -50), (79, 192, -35), (79, 193, -35), (79, 194, -35), + (79, 195, -35), (79, 196, -35), (79, 197, -35), (79, 221, -50), (80, 44, -111), + (80, 46, -111), (80, 65, -92), (80, 97, -15), (80, 192, -92), (80, 193, -92), + (80, 194, -92), (80, 195, -92), (80, 196, -92), (80, 197, -92), (80, 224, -15), + (80, 225, -15), (80, 226, -15), (80, 227, -15), (80, 228, -15), (80, 229, -15), + (81, 85, -10), (81, 217, -10), (81, 218, -10), (81, 219, -10), (81, 220, -10), + (82, 79, -40), (82, 84, -60), (82, 85, -40), (82, 86, -80), (82, 87, -55), (82, 89, -65), + (82, 210, -40), (82, 211, -40), (82, 212, -40), (82, 213, -40), (82, 214, -40), + (82, 216, -40), (82, 217, -40), (82, 218, -40), (82, 219, -40), (82, 220, -40), + (82, 221, -65), (84, 44, -74), (84, 45, -92), (84, 46, -74), (84, 58, -50), (84, 59, -55), + (84, 65, -93), (84, 79, -18), (84, 97, -80), (84, 101, -70), (84, 105, -35), (84, 111, -80), + (84, 114, -35), (84, 117, -45), (84, 119, -80), (84, 121, -80), (84, 159, -80), + (84, 173, -92), (84, 192, -93), (84, 193, -93), (84, 194, -93), (84, 195, -93), + (84, 196, -93), (84, 197, -93), (84, 210, -18), (84, 211, -18), (84, 212, -18), + (84, 213, -18), (84, 214, -18), (84, 216, -18), (84, 224, -40), (84, 225, -80), + (84, 226, -80), (84, 227, -40), (84, 228, -40), (84, 229, -80), (84, 232, -70), + (84, 233, -70), (84, 234, -70), (84, 235, -30), (84, 237, -35), (84, 242, -80), + (84, 243, -80), (84, 244, -80), (84, 245, -80), (84, 246, -80), (84, 248, -80), + (84, 249, -45), (84, 250, -45), (84, 251, -45), (84, 252, -45), (84, 253, -80), + (84, 255, -80), (85, 65, -40), (85, 192, -40), (85, 193, -40), (85, 194, -40), + (85, 195, -40), (85, 196, -40), (85, 197, -40), (86, 44, -129), (86, 45, -100), + (86, 46, -129), (86, 58, -74), (86, 59, -74), (86, 65, -135), (86, 71, -15), (86, 79, -40), + (86, 97, -111), (86, 101, -111), (86, 105, -60), (86, 111, -129), (86, 117, -75), + (86, 173, -100), (86, 192, -135), (86, 193, -135), (86, 194, -135), (86, 195, -135), + (86, 196, -135), (86, 197, -135), (86, 210, -40), (86, 211, -40), (86, 212, -40), + (86, 213, -40), (86, 214, -40), (86, 216, -40), (86, 224, -71), (86, 225, -111), + (86, 226, -71), (86, 227, -71), (86, 228, -71), (86, 229, -111), (86, 232, -71), + (86, 233, -111), (86, 234, -71), (86, 235, -71), (86, 236, -20), (86, 237, -60), + (86, 238, -20), (86, 239, -20), (86, 242, -89), (86, 243, -129), (86, 244, -129), + (86, 245, -89), (86, 246, -89), (86, 248, -129), (86, 249, -75), (86, 250, -75), + (86, 251, -75), (86, 252, -75), (87, 44, -92), (87, 45, -65), (87, 46, -92), (87, 58, -37), + (87, 59, -37), (87, 65, -120), (87, 79, -10), (87, 97, -80), (87, 101, -80), (87, 105, -40), + (87, 111, -80), (87, 117, -50), (87, 121, -73), (87, 159, -73), (87, 173, -65), + (87, 192, -120), (87, 193, -120), (87, 194, -120), (87, 195, -120), (87, 196, -120), + (87, 197, -120), (87, 210, -10), (87, 211, -10), (87, 212, -10), (87, 213, -10), + (87, 214, -10), (87, 216, -10), (87, 224, -80), (87, 225, -80), (87, 226, -80), + (87, 227, -80), (87, 228, -80), (87, 229, -80), (87, 232, -40), (87, 233, -80), + (87, 234, -80), (87, 235, -40), (87, 237, -40), (87, 242, -80), (87, 243, -80), + (87, 244, -80), (87, 245, -80), (87, 246, -80), (87, 248, -80), (87, 249, -50), + (87, 250, -50), (87, 251, -50), (87, 252, -50), (87, 253, -73), (87, 255, -73), + (89, 44, -129), (89, 45, -111), (89, 46, -129), (89, 58, -92), (89, 59, -92), + (89, 65, -120), (89, 79, -30), (89, 97, -100), (89, 101, -100), (89, 105, -55), + (89, 111, -110), (89, 117, -111), (89, 173, -111), (89, 192, -120), (89, 193, -120), + (89, 194, -120), (89, 195, -120), (89, 196, -120), (89, 197, -120), (89, 210, -30), + (89, 211, -30), (89, 212, -30), (89, 213, -30), (89, 214, -30), (89, 216, -30), + (89, 224, -60), (89, 225, -100), (89, 226, -100), (89, 227, -60), (89, 228, -60), + (89, 229, -100), (89, 232, -60), (89, 233, -100), (89, 234, -100), (89, 235, -60), + (89, 237, -55), (89, 242, -70), (89, 243, -110), (89, 244, -110), (89, 245, -70), + (89, 246, -70), (89, 248, -110), (89, 249, -71), (89, 250, -111), (89, 251, -111), + (89, 252, -71), (97, 118, -20), (97, 119, -15), (98, 46, -40), (98, 117, -20), + (98, 118, -15), (98, 249, -20), (98, 250, -20), (98, 251, -20), (98, 252, -20), + (99, 121, -15), (99, 159, -15), (99, 253, -15), (99, 255, -15), (101, 103, -15), + (101, 118, -25), (101, 119, -25), (101, 120, -15), (101, 121, -15), (101, 159, -15), + (101, 253, -15), (101, 255, -15), (102, 97, -10), (102, 102, -25), (102, 105, -20), + (102, 146, 55), (102, 224, -10), (102, 225, -10), (102, 226, -10), (102, 227, -10), + (102, 228, -10), (102, 229, -10), (102, 237, -20), (103, 97, -5), (103, 224, -5), + (103, 225, -5), (103, 226, -5), (103, 227, -5), (103, 228, -5), (103, 229, -5), + (104, 121, -5), (104, 159, -5), (104, 253, -5), (104, 255, -5), (105, 118, -25), + (107, 101, -10), (107, 111, -10), (107, 121, -15), (107, 159, -15), (107, 232, -10), + (107, 233, -10), (107, 234, -10), (107, 235, -10), (107, 242, -10), (107, 243, -10), + (107, 244, -10), (107, 245, -10), (107, 246, -10), (107, 248, -10), (107, 253, -15), + (107, 255, -15), (108, 119, -10), (110, 118, -40), (110, 121, -15), (110, 159, -15), + (110, 253, -15), (110, 255, -15), (111, 118, -15), (111, 119, -25), (111, 121, -10), + (111, 159, -10), (111, 253, -10), (111, 255, -10), (112, 121, -10), (112, 159, -10), + (112, 253, -10), (112, 255, -10), (114, 44, -40), (114, 45, -20), (114, 46, -55), + (114, 103, -18), (114, 173, -20), (118, 44, -65), (118, 46, -65), (118, 97, -25), + (118, 101, -15), (118, 111, -20), (118, 224, -25), (118, 225, -25), (118, 226, -25), + (118, 227, -25), (118, 228, -25), (118, 229, -25), (118, 232, -15), (118, 233, -15), + (118, 234, -15), (118, 235, -15), (118, 242, -20), (118, 243, -20), (118, 244, -20), + (118, 245, -20), (118, 246, -20), (118, 248, -20), (119, 44, -65), (119, 46, -65), + (119, 97, -10), (119, 111, -10), (119, 224, -10), (119, 225, -10), (119, 226, -10), + (119, 227, -10), (119, 228, -10), (119, 229, -10), (119, 242, -10), (119, 243, -10), + (119, 244, -10), (119, 245, -10), (119, 246, -10), (119, 248, -10), (120, 101, -15), + (120, 232, -15), (120, 233, -15), (120, 234, -15), (120, 235, -15), (121, 44, -65), + (121, 46, -65), (145, 65, -80), (145, 145, -74), (145, 192, -80), (145, 193, -80), + (145, 194, -80), (145, 195, -80), (145, 196, -80), (145, 197, -80), (146, 32, -74), + (146, 100, -50), (146, 108, -10), (146, 114, -50), (146, 115, -55), (146, 116, -18), + (146, 118, -50), (146, 146, -74), (146, 154, -55), (146, 160, -74), (147, 65, -80), + (147, 192, -80), (147, 193, -80), (147, 194, -80), (147, 195, -80), (147, 196, -80), + (147, 197, -80), (159, 44, -65), (159, 46, -65), (160, 65, -55), (160, 84, -18), + (160, 86, -50), (160, 87, -30), (160, 89, -90), (160, 192, -55), (160, 193, -55), + (160, 194, -55), (160, 195, -55), (160, 196, -55), (160, 197, -55), (160, 221, -90), + (192, 67, -40), (192, 71, -40), (192, 79, -55), (192, 81, -55), (192, 84, -111), + (192, 85, -55), (192, 86, -135), (192, 87, -90), (192, 89, -105), (192, 118, -74), + (192, 119, -92), (192, 121, -92), (192, 146, -111), (192, 159, -92), (192, 199, -40), + (192, 210, -55), (192, 211, -55), (192, 212, -55), (192, 213, -55), (192, 214, -55), + (192, 216, -55), (192, 217, -55), (192, 218, -55), (192, 219, -55), (192, 220, -55), + (192, 221, -105), (192, 253, -92), (192, 255, -92), (193, 67, -40), (193, 71, -40), + (193, 79, -55), (193, 81, -55), (193, 84, -111), (193, 85, -55), (193, 86, -135), + (193, 87, -90), (193, 89, -105), (193, 118, -74), (193, 119, -92), (193, 121, -92), + (193, 146, -111), (193, 159, -92), (193, 199, -40), (193, 210, -55), (193, 211, -55), + (193, 212, -55), (193, 213, -55), (193, 214, -55), (193, 216, -55), (193, 217, -55), + (193, 218, -55), (193, 219, -55), (193, 220, -55), (193, 221, -105), (193, 253, -92), + (193, 255, -92), (194, 67, -40), (194, 71, -40), (194, 79, -55), (194, 81, -55), + (194, 84, -111), (194, 85, -55), (194, 86, -135), (194, 87, -90), (194, 89, -105), + (194, 118, -74), (194, 119, -92), (194, 121, -92), (194, 146, -111), (194, 159, -92), + (194, 199, -40), (194, 210, -55), (194, 211, -55), (194, 212, -55), (194, 213, -55), + (194, 214, -55), (194, 216, -55), (194, 217, -55), (194, 218, -55), (194, 219, -55), + (194, 220, -55), (194, 221, -105), (194, 253, -92), (194, 255, -92), (195, 67, -40), + (195, 71, -40), (195, 79, -55), (195, 81, -55), (195, 84, -111), (195, 85, -55), + (195, 86, -135), (195, 87, -90), (195, 89, -105), (195, 118, -74), (195, 119, -92), + (195, 121, -92), (195, 146, -111), (195, 159, -92), (195, 199, -40), (195, 210, -55), + (195, 211, -55), (195, 212, -55), (195, 213, -55), (195, 214, -55), (195, 216, -55), + (195, 217, -55), (195, 218, -55), (195, 219, -55), (195, 220, -55), (195, 221, -105), + (195, 253, -92), (195, 255, -92), (196, 67, -40), (196, 71, -40), (196, 79, -55), + (196, 81, -55), (196, 84, -111), (196, 85, -55), (196, 86, -135), (196, 87, -90), + (196, 89, -105), (196, 118, -74), (196, 119, -92), (196, 121, -92), (196, 146, -111), + (196, 159, -92), (196, 199, -40), (196, 210, -55), (196, 211, -55), (196, 212, -55), + (196, 213, -55), (196, 214, -55), (196, 216, -55), (196, 217, -55), (196, 218, -55), + (196, 219, -55), (196, 220, -55), (196, 221, -105), (196, 253, -92), (196, 255, -92), + (197, 67, -40), (197, 71, -40), (197, 79, -55), (197, 81, -55), (197, 84, -111), + (197, 85, -55), (197, 86, -135), (197, 87, -90), (197, 89, -105), (197, 118, -74), + (197, 119, -92), (197, 121, -92), (197, 146, -111), (197, 159, -92), (197, 199, -40), + (197, 210, -55), (197, 211, -55), (197, 212, -55), (197, 213, -55), (197, 214, -55), + (197, 216, -55), (197, 217, -55), (197, 218, -55), (197, 219, -55), (197, 220, -55), + (197, 221, -105), (197, 253, -92), (197, 255, -92), (209, 65, -35), (209, 192, -35), + (209, 193, -35), (209, 194, -35), (209, 195, -35), (209, 196, -35), (209, 197, -35), + (210, 65, -35), (210, 84, -40), (210, 86, -50), (210, 87, -35), (210, 88, -40), + (210, 89, -50), (210, 192, -35), (210, 193, -35), (210, 194, -35), (210, 195, -35), + (210, 196, -35), (210, 197, -35), (210, 221, -50), (211, 65, -35), (211, 84, -40), + (211, 86, -50), (211, 87, -35), (211, 88, -40), (211, 89, -50), (211, 192, -35), + (211, 193, -35), (211, 194, -35), (211, 195, -35), (211, 196, -35), (211, 197, -35), + (211, 221, -50), (212, 65, -35), (212, 84, -40), (212, 86, -50), (212, 87, -35), + (212, 88, -40), (212, 89, -50), (212, 192, -35), (212, 193, -35), (212, 194, -35), + (212, 195, -35), (212, 196, -35), (212, 197, -35), (212, 221, -50), (213, 65, -35), + (213, 84, -40), (213, 86, -50), (213, 87, -35), (213, 88, -40), (213, 89, -50), + (213, 192, -35), (213, 193, -35), (213, 194, -35), (213, 195, -35), (213, 196, -35), + (213, 197, -35), (213, 221, -50), (214, 65, -35), (214, 84, -40), (214, 86, -50), + (214, 87, -35), (214, 88, -40), (214, 89, -50), (214, 192, -35), (214, 193, -35), + (214, 194, -35), (214, 195, -35), (214, 196, -35), (214, 197, -35), (214, 221, -50), + (216, 65, -35), (216, 84, -40), (216, 86, -50), (216, 87, -35), (216, 88, -40), + (216, 89, -50), (216, 192, -35), (216, 193, -35), (216, 194, -35), (216, 195, -35), + (216, 196, -35), (216, 197, -35), (216, 221, -50), (217, 65, -40), (217, 192, -40), + (217, 193, -40), (217, 194, -40), (217, 195, -40), (217, 196, -40), (217, 197, -40), + (218, 65, -40), (218, 192, -40), (218, 193, -40), (218, 194, -40), (218, 195, -40), + (218, 196, -40), (218, 197, -40), (219, 65, -40), (219, 192, -40), (219, 193, -40), + (219, 194, -40), (219, 195, -40), (219, 196, -40), (219, 197, -40), (220, 65, -40), + (220, 192, -40), (220, 193, -40), (220, 194, -40), (220, 195, -40), (220, 196, -40), + (220, 197, -40), (221, 44, -129), (221, 45, -111), (221, 46, -129), (221, 58, -92), + (221, 59, -92), (221, 65, -120), (221, 79, -30), (221, 97, -100), (221, 101, -100), + (221, 105, -55), (221, 111, -110), (221, 117, -111), (221, 173, -111), (221, 192, -120), + (221, 193, -120), (221, 194, -120), (221, 195, -120), (221, 196, -120), (221, 197, -120), + (221, 210, -30), (221, 211, -30), (221, 212, -30), (221, 213, -30), (221, 214, -30), + (221, 216, -30), (221, 224, -60), (221, 225, -100), (221, 226, -100), (221, 227, -60), + (221, 228, -60), (221, 229, -100), (221, 232, -60), (221, 233, -100), (221, 234, -100), + (221, 235, -60), (221, 237, -55), (221, 242, -70), (221, 243, -110), (221, 244, -110), + (221, 245, -70), (221, 246, -70), (221, 248, -110), (221, 249, -71), (221, 250, -111), + (221, 251, -111), (221, 252, -71), (224, 118, -20), (224, 119, -15), (225, 118, -20), + (225, 119, -15), (226, 118, -20), (226, 119, -15), (227, 118, -20), (227, 119, -15), + (228, 118, -20), (228, 119, -15), (229, 118, -20), (229, 119, -15), (231, 121, -15), + (231, 159, -15), (231, 253, -15), (231, 255, -15), (232, 103, -15), (232, 118, -25), + (232, 119, -25), (232, 120, -15), (232, 121, -15), (232, 159, -15), (232, 253, -15), + (232, 255, -15), (233, 103, -15), (233, 118, -25), (233, 119, -25), (233, 120, -15), + (233, 121, -15), (233, 159, -15), (233, 253, -15), (233, 255, -15), (234, 103, -15), + (234, 118, -25), (234, 119, -25), (234, 120, -15), (234, 121, -15), (234, 159, -15), + (234, 253, -15), (234, 255, -15), (235, 103, -15), (235, 118, -25), (235, 119, -25), + (235, 120, -15), (235, 121, -15), (235, 159, -15), (235, 253, -15), (235, 255, -15), + (236, 118, -25), (237, 118, -25), (238, 118, -25), (239, 118, -25), (241, 118, -40), + (241, 121, -15), (241, 159, -15), (241, 253, -15), (241, 255, -15), (242, 118, -15), + (242, 119, -25), (242, 121, -10), (242, 159, -10), (242, 253, -10), (242, 255, -10), + (243, 118, -15), (243, 119, -25), (243, 121, -10), (243, 159, -10), (243, 253, -10), + (243, 255, -10), (244, 118, -15), (244, 119, -25), (244, 121, -10), (244, 159, -10), + (244, 253, -10), (244, 255, -10), (245, 118, -15), (245, 119, -25), (245, 121, -10), + (245, 159, -10), (245, 253, -10), (245, 255, -10), (246, 118, -15), (246, 119, -25), + (246, 121, -10), (246, 159, -10), (246, 253, -10), (246, 255, -10), (248, 118, -15), + (248, 119, -25), (248, 121, -10), (248, 159, -10), (248, 253, -10), (248, 255, -10), + (253, 44, -65), (253, 46, -65), (255, 44, -65), (255, 46, -65), + ], +}; + +/// `Encodings.WinAnsi`: (code point, byte) for every code point it encodes, sorted. +#[rustfmt::skip] +pub static WINANSI: &[(u32, u8)] = &[ + (0x0020, 32), (0x0021, 33), (0x0022, 34), (0x0023, 35), (0x0024, 36), (0x0025, 37), + (0x0026, 38), (0x0027, 39), (0x0028, 40), (0x0029, 41), (0x002A, 42), (0x002B, 43), + (0x002C, 44), (0x002D, 45), (0x002E, 46), (0x002F, 47), (0x0030, 48), (0x0031, 49), + (0x0032, 50), (0x0033, 51), (0x0034, 52), (0x0035, 53), (0x0036, 54), (0x0037, 55), + (0x0038, 56), (0x0039, 57), (0x003A, 58), (0x003B, 59), (0x003C, 60), (0x003D, 61), + (0x003E, 62), (0x003F, 63), (0x0040, 64), (0x0041, 65), (0x0042, 66), (0x0043, 67), + (0x0044, 68), (0x0045, 69), (0x0046, 70), (0x0047, 71), (0x0048, 72), (0x0049, 73), + (0x004A, 74), (0x004B, 75), (0x004C, 76), (0x004D, 77), (0x004E, 78), (0x004F, 79), + (0x0050, 80), (0x0051, 81), (0x0052, 82), (0x0053, 83), (0x0054, 84), (0x0055, 85), + (0x0056, 86), (0x0057, 87), (0x0058, 88), (0x0059, 89), (0x005A, 90), (0x005B, 91), + (0x005C, 92), (0x005D, 93), (0x005E, 94), (0x005F, 95), (0x0060, 96), (0x0061, 97), + (0x0062, 98), (0x0063, 99), (0x0064, 100), (0x0065, 101), (0x0066, 102), (0x0067, 103), + (0x0068, 104), (0x0069, 105), (0x006A, 106), (0x006B, 107), (0x006C, 108), (0x006D, 109), + (0x006E, 110), (0x006F, 111), (0x0070, 112), (0x0071, 113), (0x0072, 114), (0x0073, 115), + (0x0074, 116), (0x0075, 117), (0x0076, 118), (0x0077, 119), (0x0078, 120), (0x0079, 121), + (0x007A, 122), (0x007B, 123), (0x007C, 124), (0x007D, 125), (0x007E, 126), (0x00A0, 160), + (0x00A1, 161), (0x00A2, 162), (0x00A3, 163), (0x00A4, 164), (0x00A5, 165), (0x00A6, 166), + (0x00A7, 167), (0x00A8, 168), (0x00A9, 169), (0x00AA, 170), (0x00AB, 171), (0x00AC, 172), + (0x00AD, 173), (0x00AE, 174), (0x00AF, 175), (0x00B0, 176), (0x00B1, 177), (0x00B2, 178), + (0x00B3, 179), (0x00B4, 180), (0x00B5, 181), (0x00B6, 182), (0x00B7, 183), (0x00B8, 184), + (0x00B9, 185), (0x00BA, 186), (0x00BB, 187), (0x00BC, 188), (0x00BD, 189), (0x00BE, 190), + (0x00BF, 191), (0x00C0, 192), (0x00C1, 193), (0x00C2, 194), (0x00C3, 195), (0x00C4, 196), + (0x00C5, 197), (0x00C6, 198), (0x00C7, 199), (0x00C8, 200), (0x00C9, 201), (0x00CA, 202), + (0x00CB, 203), (0x00CC, 204), (0x00CD, 205), (0x00CE, 206), (0x00CF, 207), (0x00D0, 208), + (0x00D1, 209), (0x00D2, 210), (0x00D3, 211), (0x00D4, 212), (0x00D5, 213), (0x00D6, 214), + (0x00D7, 215), (0x00D8, 216), (0x00D9, 217), (0x00DA, 218), (0x00DB, 219), (0x00DC, 220), + (0x00DD, 221), (0x00DE, 222), (0x00DF, 223), (0x00E0, 224), (0x00E1, 225), (0x00E2, 226), + (0x00E3, 227), (0x00E4, 228), (0x00E5, 229), (0x00E6, 230), (0x00E7, 231), (0x00E8, 232), + (0x00E9, 233), (0x00EA, 234), (0x00EB, 235), (0x00EC, 236), (0x00ED, 237), (0x00EE, 238), + (0x00EF, 239), (0x00F0, 240), (0x00F1, 241), (0x00F2, 242), (0x00F3, 243), (0x00F4, 244), + (0x00F5, 245), (0x00F6, 246), (0x00F7, 247), (0x00F8, 248), (0x00F9, 249), (0x00FA, 250), + (0x00FB, 251), (0x00FC, 252), (0x00FD, 253), (0x00FE, 254), (0x00FF, 255), (0x0152, 140), + (0x0153, 156), (0x0160, 138), (0x0161, 154), (0x0178, 159), (0x017D, 142), (0x017E, 158), + (0x0192, 131), (0x02C6, 136), (0x02DC, 152), (0x2013, 150), (0x2014, 151), (0x2018, 145), + (0x2019, 146), (0x201A, 130), (0x201C, 147), (0x201D, 148), (0x201E, 132), (0x2020, 134), + (0x2021, 135), (0x2022, 149), (0x2026, 133), (0x2030, 137), (0x2039, 139), (0x203A, 155), + (0x20AC, 128), (0x2122, 153), +]; + +/// safeText `[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]`: the dropped code point ranges, inclusive. +#[rustfmt::skip] +pub static SAFE_TEXT_DROPPED: &[(u32, u32)] = &[ + (0x0000, 0x001F), (0x007F, 0x009F), (0x00AD, 0x00AD), (0x0600, 0x0605), (0x061C, 0x061C), + (0x06DD, 0x06DD), (0x070F, 0x070F), (0x0890, 0x0891), (0x08E2, 0x08E2), (0x180E, 0x180E), + (0x200B, 0x200F), (0x2028, 0x202E), (0x2060, 0x2064), (0x2066, 0x206F), (0xFEFF, 0xFEFF), + (0xFFF9, 0xFFFB), (0x110BD, 0x110BD), (0x110CD, 0x110CD), (0x13430, 0x1343F), + (0x1BCA0, 0x1BCA3), (0x1D173, 0x1D17A), (0xE0001, 0xE0001), (0xE0020, 0xE007F), +]; + +/// `toUpperCase` one-to-one: (start, end, step, delta), start + k * step <= end maps to +/// itself + delta. Sorted by start, disjoint. +#[rustfmt::skip] +pub static UPPER_RUNS: &[(u32, u32, u32, i32)] = &[ + (0x0061, 0x007A, 1, -32), (0x00B5, 0x00B5, 1, 743), (0x00E0, 0x00F6, 1, -32), + (0x00F8, 0x00FE, 1, -32), (0x00FF, 0x00FF, 1, 121), (0x0101, 0x012F, 2, -1), + (0x0131, 0x0131, 1, -232), (0x0133, 0x0137, 2, -1), (0x013A, 0x0148, 2, -1), + (0x014B, 0x0177, 2, -1), (0x017A, 0x017E, 2, -1), (0x017F, 0x017F, 1, -300), + (0x0180, 0x0180, 1, 195), (0x0183, 0x0185, 2, -1), (0x0188, 0x0188, 1, -1), + (0x018C, 0x018C, 1, -1), (0x0192, 0x0192, 1, -1), (0x0195, 0x0195, 1, 97), + (0x0199, 0x0199, 1, -1), (0x019A, 0x019A, 1, 163), (0x019B, 0x019B, 1, 42561), + (0x019E, 0x019E, 1, 130), (0x01A1, 0x01A5, 2, -1), (0x01A8, 0x01A8, 1, -1), + (0x01AD, 0x01AD, 1, -1), (0x01B0, 0x01B0, 1, -1), (0x01B4, 0x01B6, 2, -1), + (0x01B9, 0x01B9, 1, -1), (0x01BD, 0x01BD, 1, -1), (0x01BF, 0x01BF, 1, 56), + (0x01C5, 0x01C5, 1, -1), (0x01C6, 0x01C6, 1, -2), (0x01C8, 0x01C8, 1, -1), + (0x01C9, 0x01C9, 1, -2), (0x01CB, 0x01CB, 1, -1), (0x01CC, 0x01CC, 1, -2), + (0x01CE, 0x01DC, 2, -1), (0x01DD, 0x01DD, 1, -79), (0x01DF, 0x01EF, 2, -1), + (0x01F2, 0x01F2, 1, -1), (0x01F3, 0x01F3, 1, -2), (0x01F5, 0x01F5, 1, -1), + (0x01F9, 0x021F, 2, -1), (0x0223, 0x0233, 2, -1), (0x023C, 0x023C, 1, -1), + (0x023F, 0x0240, 1, 10815), (0x0242, 0x0242, 1, -1), (0x0247, 0x024F, 2, -1), + (0x0250, 0x0250, 1, 10783), (0x0251, 0x0251, 1, 10780), (0x0252, 0x0252, 1, 10782), + (0x0253, 0x0253, 1, -210), (0x0254, 0x0254, 1, -206), (0x0256, 0x0257, 1, -205), + (0x0259, 0x0259, 1, -202), (0x025B, 0x025B, 1, -203), (0x025C, 0x025C, 1, 42319), + (0x0260, 0x0260, 1, -205), (0x0261, 0x0261, 1, 42315), (0x0263, 0x0263, 1, -207), + (0x0264, 0x0264, 1, 42343), (0x0265, 0x0265, 1, 42280), (0x0266, 0x0266, 1, 42308), + (0x0268, 0x0268, 1, -209), (0x0269, 0x0269, 1, -211), (0x026A, 0x026A, 1, 42308), + (0x026B, 0x026B, 1, 10743), (0x026C, 0x026C, 1, 42305), (0x026F, 0x026F, 1, -211), + (0x0271, 0x0271, 1, 10749), (0x0272, 0x0272, 1, -213), (0x0275, 0x0275, 1, -214), + (0x027D, 0x027D, 1, 10727), (0x0280, 0x0280, 1, -218), (0x0282, 0x0282, 1, 42307), + (0x0283, 0x0283, 1, -218), (0x0287, 0x0287, 1, 42282), (0x0288, 0x0288, 1, -218), + (0x0289, 0x0289, 1, -69), (0x028A, 0x028B, 1, -217), (0x028C, 0x028C, 1, -71), + (0x0292, 0x0292, 1, -219), (0x029D, 0x029D, 1, 42261), (0x029E, 0x029E, 1, 42258), + (0x0345, 0x0345, 1, 84), (0x0371, 0x0373, 2, -1), (0x0377, 0x0377, 1, -1), + (0x037B, 0x037D, 1, 130), (0x03AC, 0x03AC, 1, -38), (0x03AD, 0x03AF, 1, -37), + (0x03B1, 0x03C1, 1, -32), (0x03C2, 0x03C2, 1, -31), (0x03C3, 0x03CB, 1, -32), + (0x03CC, 0x03CC, 1, -64), (0x03CD, 0x03CE, 1, -63), (0x03D0, 0x03D0, 1, -62), + (0x03D1, 0x03D1, 1, -57), (0x03D5, 0x03D5, 1, -47), (0x03D6, 0x03D6, 1, -54), + (0x03D7, 0x03D7, 1, -8), (0x03D9, 0x03EF, 2, -1), (0x03F0, 0x03F0, 1, -86), + (0x03F1, 0x03F1, 1, -80), (0x03F2, 0x03F2, 1, 7), (0x03F3, 0x03F3, 1, -116), + (0x03F5, 0x03F5, 1, -96), (0x03F8, 0x03F8, 1, -1), (0x03FB, 0x03FB, 1, -1), + (0x0430, 0x044F, 1, -32), (0x0450, 0x045F, 1, -80), (0x0461, 0x0481, 2, -1), + (0x048B, 0x04BF, 2, -1), (0x04C2, 0x04CE, 2, -1), (0x04CF, 0x04CF, 1, -15), + (0x04D1, 0x052F, 2, -1), (0x0561, 0x0586, 1, -48), (0x10D0, 0x10FA, 1, 3008), + (0x10FD, 0x10FF, 1, 3008), (0x13F8, 0x13FD, 1, -8), (0x1C80, 0x1C80, 1, -6254), + (0x1C81, 0x1C81, 1, -6253), (0x1C82, 0x1C82, 1, -6244), (0x1C83, 0x1C84, 1, -6242), + (0x1C85, 0x1C85, 1, -6243), (0x1C86, 0x1C86, 1, -6236), (0x1C87, 0x1C87, 1, -6181), + (0x1C88, 0x1C88, 1, 35266), (0x1C8A, 0x1C8A, 1, -1), (0x1D79, 0x1D79, 1, 35332), + (0x1D7D, 0x1D7D, 1, 3814), (0x1D8E, 0x1D8E, 1, 35384), (0x1E01, 0x1E95, 2, -1), + (0x1E9B, 0x1E9B, 1, -59), (0x1EA1, 0x1EFF, 2, -1), (0x1F00, 0x1F07, 1, 8), + (0x1F10, 0x1F15, 1, 8), (0x1F20, 0x1F27, 1, 8), (0x1F30, 0x1F37, 1, 8), (0x1F40, 0x1F45, 1, 8), + (0x1F51, 0x1F57, 2, 8), (0x1F60, 0x1F67, 1, 8), (0x1F70, 0x1F71, 1, 74), + (0x1F72, 0x1F75, 1, 86), (0x1F76, 0x1F77, 1, 100), (0x1F78, 0x1F79, 1, 128), + (0x1F7A, 0x1F7B, 1, 112), (0x1F7C, 0x1F7D, 1, 126), (0x1FB0, 0x1FB1, 1, 8), + (0x1FBE, 0x1FBE, 1, -7205), (0x1FD0, 0x1FD1, 1, 8), (0x1FE0, 0x1FE1, 1, 8), + (0x1FE5, 0x1FE5, 1, 7), (0x214E, 0x214E, 1, -28), (0x2170, 0x217F, 1, -16), + (0x2184, 0x2184, 1, -1), (0x24D0, 0x24E9, 1, -26), (0x2C30, 0x2C5F, 1, -48), + (0x2C61, 0x2C61, 1, -1), (0x2C65, 0x2C65, 1, -10795), (0x2C66, 0x2C66, 1, -10792), + (0x2C68, 0x2C6C, 2, -1), (0x2C73, 0x2C73, 1, -1), (0x2C76, 0x2C76, 1, -1), + (0x2C81, 0x2CE3, 2, -1), (0x2CEC, 0x2CEE, 2, -1), (0x2CF3, 0x2CF3, 1, -1), + (0x2D00, 0x2D25, 1, -7264), (0x2D27, 0x2D27, 1, -7264), (0x2D2D, 0x2D2D, 1, -7264), + (0xA641, 0xA66D, 2, -1), (0xA681, 0xA69B, 2, -1), (0xA723, 0xA72F, 2, -1), + (0xA733, 0xA76F, 2, -1), (0xA77A, 0xA77C, 2, -1), (0xA77F, 0xA787, 2, -1), + (0xA78C, 0xA78C, 1, -1), (0xA791, 0xA793, 2, -1), (0xA794, 0xA794, 1, 48), + (0xA797, 0xA7A9, 2, -1), (0xA7B5, 0xA7C3, 2, -1), (0xA7C8, 0xA7CA, 2, -1), + (0xA7CD, 0xA7DB, 2, -1), (0xA7F6, 0xA7F6, 1, -1), (0xAB53, 0xAB53, 1, -928), + (0xAB70, 0xABBF, 1, -38864), (0xFF41, 0xFF5A, 1, -32), (0x10428, 0x1044F, 1, -40), + (0x104D8, 0x104FB, 1, -40), (0x10597, 0x105A1, 1, -39), (0x105A3, 0x105B1, 1, -39), + (0x105B3, 0x105B9, 1, -39), (0x105BB, 0x105BC, 1, -39), (0x10CC0, 0x10CF2, 1, -64), + (0x10D70, 0x10D85, 1, -32), (0x118C0, 0x118DF, 1, -32), (0x16E60, 0x16E7F, 1, -32), + (0x16EBB, 0x16ED3, 1, -27), (0x1E922, 0x1E943, 1, -34), +]; + +/// `toUpperCase` one-to-many: (code point, result), sorted. +#[rustfmt::skip] +pub static UPPER_SPECIAL: &[(u32, &str)] = &[ + (0x00DF, "\u{53}\u{53}"), (0x0149, "\u{2BC}\u{4E}"), (0x01F0, "\u{4A}\u{30C}"), + (0x0390, "\u{399}\u{308}\u{301}"), (0x03B0, "\u{3A5}\u{308}\u{301}"), + (0x0587, "\u{535}\u{552}"), (0x1E96, "\u{48}\u{331}"), (0x1E97, "\u{54}\u{308}"), + (0x1E98, "\u{57}\u{30A}"), (0x1E99, "\u{59}\u{30A}"), (0x1E9A, "\u{41}\u{2BE}"), + (0x1F50, "\u{3A5}\u{313}"), (0x1F52, "\u{3A5}\u{313}\u{300}"), + (0x1F54, "\u{3A5}\u{313}\u{301}"), (0x1F56, "\u{3A5}\u{313}\u{342}"), + (0x1F80, "\u{1F08}\u{399}"), (0x1F81, "\u{1F09}\u{399}"), (0x1F82, "\u{1F0A}\u{399}"), + (0x1F83, "\u{1F0B}\u{399}"), (0x1F84, "\u{1F0C}\u{399}"), (0x1F85, "\u{1F0D}\u{399}"), + (0x1F86, "\u{1F0E}\u{399}"), (0x1F87, "\u{1F0F}\u{399}"), (0x1F88, "\u{1F08}\u{399}"), + (0x1F89, "\u{1F09}\u{399}"), (0x1F8A, "\u{1F0A}\u{399}"), (0x1F8B, "\u{1F0B}\u{399}"), + (0x1F8C, "\u{1F0C}\u{399}"), (0x1F8D, "\u{1F0D}\u{399}"), (0x1F8E, "\u{1F0E}\u{399}"), + (0x1F8F, "\u{1F0F}\u{399}"), (0x1F90, "\u{1F28}\u{399}"), (0x1F91, "\u{1F29}\u{399}"), + (0x1F92, "\u{1F2A}\u{399}"), (0x1F93, "\u{1F2B}\u{399}"), (0x1F94, "\u{1F2C}\u{399}"), + (0x1F95, "\u{1F2D}\u{399}"), (0x1F96, "\u{1F2E}\u{399}"), (0x1F97, "\u{1F2F}\u{399}"), + (0x1F98, "\u{1F28}\u{399}"), (0x1F99, "\u{1F29}\u{399}"), (0x1F9A, "\u{1F2A}\u{399}"), + (0x1F9B, "\u{1F2B}\u{399}"), (0x1F9C, "\u{1F2C}\u{399}"), (0x1F9D, "\u{1F2D}\u{399}"), + (0x1F9E, "\u{1F2E}\u{399}"), (0x1F9F, "\u{1F2F}\u{399}"), (0x1FA0, "\u{1F68}\u{399}"), + (0x1FA1, "\u{1F69}\u{399}"), (0x1FA2, "\u{1F6A}\u{399}"), (0x1FA3, "\u{1F6B}\u{399}"), + (0x1FA4, "\u{1F6C}\u{399}"), (0x1FA5, "\u{1F6D}\u{399}"), (0x1FA6, "\u{1F6E}\u{399}"), + (0x1FA7, "\u{1F6F}\u{399}"), (0x1FA8, "\u{1F68}\u{399}"), (0x1FA9, "\u{1F69}\u{399}"), + (0x1FAA, "\u{1F6A}\u{399}"), (0x1FAB, "\u{1F6B}\u{399}"), (0x1FAC, "\u{1F6C}\u{399}"), + (0x1FAD, "\u{1F6D}\u{399}"), (0x1FAE, "\u{1F6E}\u{399}"), (0x1FAF, "\u{1F6F}\u{399}"), + (0x1FB2, "\u{1FBA}\u{399}"), (0x1FB3, "\u{391}\u{399}"), (0x1FB4, "\u{386}\u{399}"), + (0x1FB6, "\u{391}\u{342}"), (0x1FB7, "\u{391}\u{342}\u{399}"), (0x1FBC, "\u{391}\u{399}"), + (0x1FC2, "\u{1FCA}\u{399}"), (0x1FC3, "\u{397}\u{399}"), (0x1FC4, "\u{389}\u{399}"), + (0x1FC6, "\u{397}\u{342}"), (0x1FC7, "\u{397}\u{342}\u{399}"), (0x1FCC, "\u{397}\u{399}"), + (0x1FD2, "\u{399}\u{308}\u{300}"), (0x1FD3, "\u{399}\u{308}\u{301}"), + (0x1FD6, "\u{399}\u{342}"), (0x1FD7, "\u{399}\u{308}\u{342}"), + (0x1FE2, "\u{3A5}\u{308}\u{300}"), (0x1FE3, "\u{3A5}\u{308}\u{301}"), + (0x1FE4, "\u{3A1}\u{313}"), (0x1FE6, "\u{3A5}\u{342}"), (0x1FE7, "\u{3A5}\u{308}\u{342}"), + (0x1FF2, "\u{1FFA}\u{399}"), (0x1FF3, "\u{3A9}\u{399}"), (0x1FF4, "\u{38F}\u{399}"), + (0x1FF6, "\u{3A9}\u{342}"), (0x1FF7, "\u{3A9}\u{342}\u{399}"), (0x1FFC, "\u{3A9}\u{399}"), + (0xFB00, "\u{46}\u{46}"), (0xFB01, "\u{46}\u{49}"), (0xFB02, "\u{46}\u{4C}"), + (0xFB03, "\u{46}\u{46}\u{49}"), (0xFB04, "\u{46}\u{46}\u{4C}"), (0xFB05, "\u{53}\u{54}"), + (0xFB06, "\u{53}\u{54}"), (0xFB13, "\u{544}\u{546}"), (0xFB14, "\u{544}\u{535}"), + (0xFB15, "\u{544}\u{53B}"), (0xFB16, "\u{54E}\u{546}"), (0xFB17, "\u{544}\u{53D}"), +]; + +/// JS `\s`: the matched code point ranges, inclusive. +#[rustfmt::skip] +pub static JS_WHITESPACE: &[(u32, u32)] = &[ + (0x0009, 0x000D), (0x0020, 0x0020), (0x00A0, 0x00A0), (0x1680, 0x1680), (0x2000, 0x200A), + (0x2028, 0x2029), (0x202F, 0x202F), (0x205F, 0x205F), (0x3000, 0x3000), (0xFEFF, 0xFEFF), +]; + +/// `String.prototype.trim`: the stripped code point ranges, inclusive. +#[rustfmt::skip] +pub static JS_TRIM: &[(u32, u32)] = &[ + (0x0009, 0x000D), (0x0020, 0x0020), (0x00A0, 0x00A0), (0x1680, 0x1680), (0x2000, 0x200A), + (0x2028, 0x2029), (0x202F, 0x202F), (0x205F, 0x205F), (0x3000, 0x3000), (0xFEFF, 0xFEFF), +]; diff --git a/crates/core/src/pdf/write/dates.rs b/crates/core/src/pdf/write/dates.rs index 8a1e687e..95bad7e9 100644 --- a/crates/core/src/pdf/write/dates.rs +++ b/crates/core/src/pdf/write/dates.rs @@ -1,8 +1,105 @@ -//! R4.4a: `formatDate` of `render/dates.ts`. Written by the R4.4a track. +//! R4.4a: `formatDate` of `packages/pdf/src/render/dates.ts`, the long dates of the PDF +//! (visit date, next appointment). The TypeScript reads the value through `Date.UTC` and +//! prints it with `Intl.DateTimeFormat` (`fr-FR` / `en-US`, day numeric, month long, year +//! numeric, UTC); the rules below are what that gave, pinned by the frozen O6 oracle. use crate::domain::archive::Locale; -/// Long date in the locale for a `YYYY-MM-DD` value; anything else printed unchanged. -pub fn format_date(_iso_date: &str, _locale: Locale) -> String { - unimplemented!("R4.4a") +/// `fr-FR` long month names (ICU), January first. +const MONTHS_FR: [&str; 12] = [ + "janvier", + "février", + "mars", + "avril", + "mai", + "juin", + "juillet", + "août", + "septembre", + "octobre", + "novembre", + "décembre", +]; + +/// `en-US` long month names (ICU), January first. +const MONTHS_EN: [&str; 12] = [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", +]; + +/// A calendar date `Date.UTC` accepted without rolling over. +struct CalendarDate { + year: u32, + /// 0 for January, as `getUTCMonth`. + month_index: usize, + day: u32, +} + +/// Long date in the locale for a `YYYY-MM-DD` value; anything else printed unchanged (a +/// report is user data and never fails the export). Years 0000 to 0099 print as 1900 to +/// 1999 (`Date.UTC` maps them so); a month or day that rolls over is refused. +pub fn format_date(iso_date: &str, locale: Locale) -> String { + let Some(date) = parse_date(iso_date) else { + return iso_date.to_owned(); + }; + let (months, day, year) = (month_names(locale), date.day, date.year); + let month = months.get(date.month_index).copied().unwrap_or_default(); + match locale { + Locale::Fr => format!("{day} {month} {year}"), + Locale::En => format!("{month} {day}, {year}"), + } +} + +fn month_names(locale: Locale) -> &'static [&'static str; 12] { + match locale { + Locale::Fr => &MONTHS_FR, + Locale::En => &MONTHS_EN, + } +} + +/// `/^(\d{4})-(\d{2})-(\d{2})$/` (ASCII digits, `$` at the very end) then +/// `Date.UTC(y, mo - 1, d)`: a year below 100 is 1900 + year, and the date is kept only when +/// the month and the day did not roll over (proleptic Gregorian, as ECMAScript dates are). +fn parse_date(text: &str) -> Option { + let [y0, y1, y2, y3, b'-', m0, m1, b'-', d0, d1] = *text.as_bytes() else { + return None; + }; + let year = digits(&[y0, y1, y2, y3])?; + let (month, day) = (digits(&[m0, m1])?, digits(&[d0, d1])?); + let year = if year <= 99 { 1900 + year } else { year }; + let in_range = (1..=12).contains(&month) && (1..=days_in_month(year, month)).contains(&day); + let month_index = usize::try_from(month.checked_sub(1)?).ok()?; + in_range.then_some(CalendarDate { + year, + month_index, + day, + }) +} + +/// The value of ASCII decimal digits, `None` on any other byte (JavaScript `\d`). +fn digits(bytes: &[u8]) -> Option { + bytes.iter().try_fold(0, |value: u32, &byte| { + byte.is_ascii_digit() + .then(|| value * 10 + u32::from(byte - b'0')) + }) +} + +fn days_in_month(year: u32, month: u32) -> u32 { + let leap = year.is_multiple_of(4) && (!year.is_multiple_of(100) || year.is_multiple_of(400)); + match month { + 2 if leap => 29, + 2 => 28, + 4 | 6 | 9 | 11 => 30, + _ => 31, + } } diff --git a/crates/core/src/pdf/write/draw_log.rs b/crates/core/src/pdf/write/draw_log.rs index 8b0c7dad..542bddec 100644 --- a/crates/core/src/pdf/write/draw_log.rs +++ b/crates/core/src/pdf/write/draw_log.rs @@ -1 +1,199 @@ -//! R4.4b: the draw log. Written by the layout track. +//! The draw log (R4.4): every drawing call the TypeScript engine made through pdf-lib, +//! with every argument at full precision, in call order per page; the images by embed +//! order; the links as the saved annotations carried them. The O1 oracle +//! (`apps/app/tests/fixtures/goldens/r4/draw.*.json`) is this shape; R4.5 turns it into +//! content streams. Nothing here rounds or reorders. + +use serde::Serialize; + +use super::text::Font; + +/// `rgb(r, g, b)` of pdf-lib, components in 0..=1. +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct Rgb { + pub red: f64, + pub green: f64, + pub blue: f64, +} + +pub const fn rgb(red: f64, green: f64, blue: f64) -> Rgb { + Rgb { red, green, blue } +} + +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct Point { + pub x: f64, + pub y: f64, +} + +/// `page.drawText(text, { x, y, size, font, color })`. +#[derive(Debug, Clone, PartialEq, Serialize)] +pub struct TextOp { + pub text: String, + pub x: f64, + pub y: f64, + pub size: f64, + #[serde(serialize_with = "font_name")] + pub font: Font, + pub color: Rgb, +} + +fn font_name(font: &Font, serializer: S) -> Result { + serializer.serialize_str(font.as_str()) +} + +/// An axis-aligned box: an image's placement, a link's area. +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct Rect { + pub x: f64, + pub y: f64, + pub width: f64, + pub height: f64, +} + +/// `page.drawImage(image, { x, y, width, height })`; the image by its embed index. +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct ImageOp { + pub embed: usize, + pub x: f64, + pub y: f64, + pub width: f64, + pub height: f64, +} + +/// `page.drawLine({ start, end, thickness, color })`. +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct LineOp { + pub start: Point, + pub end: Point, + pub thickness: f64, + pub color: Rgb, +} + +/// `page.drawRectangle({ x, y, width, height, color })`. +#[derive(Debug, Clone, Copy, PartialEq, Serialize)] +pub struct RectangleOp { + pub x: f64, + pub y: f64, + pub width: f64, + pub height: f64, + pub color: Rgb, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +#[serde(tag = "op")] +pub enum DrawOp { + #[serde(rename = "drawText")] + Text(TextOp), + #[serde(rename = "drawImage")] + Image(ImageOp), + #[serde(rename = "drawLine")] + Line(LineOp), + #[serde(rename = "drawRectangle")] + Rectangle(RectangleOp), +} + +/// A `/Link` annotation: `[x1, y1, x2, y2]` and its URI, on one page. +#[derive(Debug, Clone, PartialEq, Serialize)] +pub struct LinkOp { + pub page: usize, + pub rect: [f64; 4], + pub uri: String, +} + +/// One A4 page's calls, in order. The footer calls come last on every page; the engine +/// made them after every page's content, so the global call order is every page's +/// `ops[..footer_start]` in page order, then every page's footer. +#[derive(Debug, Clone, Default, PartialEq, Serialize)] +pub struct PageLog { + pub ops: Vec, + /// Index of the first footer call; `ops.len()` until the footer is drawn. + pub footer_start: usize, +} + +/// An embedded image: the PNG bytes as given, in embed order (branding slots first, then +/// each observation's views in view order), with its pixel size from the header. +#[derive(Debug, Clone, PartialEq)] +pub struct Embed { + pub png: Vec, + pub width: u32, + pub height: u32, +} + +/// The whole document's drawing, before serialisation. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct DrawLog { + pub pages: Vec, + pub embeds: Vec, + pub links: Vec, +} + +/// A4 portrait, `PageSizes.A4` of pdf-lib. +pub const PAGE_WIDTH: f64 = 595.28; +pub const PAGE_HEIGHT: f64 = 841.89; + +impl DrawLog { + pub fn add_page(&mut self) -> usize { + self.pages.push(PageLog::default()); + self.pages.len() - 1 + } + + /// Marks the end of `page`'s own content: the calls that follow are its footer. + pub fn begin_footer(&mut self, page: usize) { + if let Some(log) = self.pages.get_mut(page) { + log.footer_start = log.ops.len(); + } + } + + /// Every call in the engine's global order: page contents, then the footers. + pub fn calls_in_order(&self) -> impl Iterator { + let contents = self + .pages + .iter() + .enumerate() + .flat_map(|(p, page)| page.ops[..page.footer_start].iter().map(move |op| (p, op))); + let footers = self + .pages + .iter() + .enumerate() + .flat_map(|(p, page)| page.ops[page.footer_start..].iter().map(move |op| (p, op))); + contents.chain(footers) + } + + /// Registers an image; its index is what the draw calls reference. + pub fn embed(&mut self, png: Vec, width: u32, height: u32) -> usize { + self.embeds.push(Embed { png, width, height }); + self.embeds.len() - 1 + } + + pub fn text(&mut self, page: usize, op: TextOp) { + self.push(page, DrawOp::Text(op)); + } + + pub fn image(&mut self, page: usize, op: ImageOp) { + self.push(page, DrawOp::Image(op)); + } + + pub fn line(&mut self, page: usize, op: LineOp) { + self.push(page, DrawOp::Line(op)); + } + + pub fn rectangle(&mut self, page: usize, op: RectangleOp) { + self.push(page, DrawOp::Rectangle(op)); + } + + /// `addLinkAnnotation`: `[x, y, x + width, y + height]`. + pub fn link(&mut self, page: usize, rect: Rect, uri: &str) { + self.links.push(LinkOp { + page, + rect: [rect.x, rect.y, rect.x + rect.width, rect.y + rect.height], + uri: uri.to_owned(), + }); + } + + fn push(&mut self, page: usize, op: DrawOp) { + if let Some(log) = self.pages.get_mut(page) { + log.ops.push(op); + } + } +} diff --git a/crates/core/src/pdf/write/input.rs b/crates/core/src/pdf/write/input.rs index 2a925199..e31fa074 100644 --- a/crates/core/src/pdf/write/input.rs +++ b/crates/core/src/pdf/write/input.rs @@ -1 +1,213 @@ -//! R4.4b: the export input and its checks. Written by the layout track. +//! The export input and its checks (`packages/pdf/src/export.ts` steps 0 to 3): the +//! branding and the labels through their schemas again, the snapshot gate with every asset +//! accepted by the SVG reader, the report's sheet and the page caps. Today's error kinds. + +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; +#[cfg(feature = "ts")] +use ts_rs::TS; + +use crate::anatomy::svg::read_svg; +use crate::domain::archive::{ + Branding, Locale, PdfLabelSet, PdfLabels, SheetSnapshot, SnapshotRejection, assert_snapshot, +}; +use crate::domain::report::Report; +use crate::domain::sheet::{NoMainView, SheetIndex}; + +/// What the application hands the export (plan §B.1.3), as JSON: the report, the snapshot +/// of the sheet it was written on, the branding, the labels, the locale and the export time +/// (an ISO string the caller took from its clock; the core reads no clock). +#[derive(Debug, Clone, Deserialize, Serialize)] +#[cfg_attr(feature = "ts", derive(TS), ts(export))] +#[serde(rename_all = "camelCase")] +pub struct ExportInput { + pub report: Report, + #[cfg_attr(feature = "ts", ts(type = "unknown"))] + pub sheet_snapshot: Value, + #[cfg_attr(feature = "ts", ts(type = "unknown"))] + pub branding: Value, + #[cfg_attr(feature = "ts", ts(type = "unknown"))] + pub labels: Value, + pub locale: Locale, + pub exported_at: String, +} + +/// Today's `ArchiveExportErrorKind`, with its detail. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExportError { + pub kind: ExportErrorKind, + pub detail: String, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[cfg_attr(feature = "ts", derive(TS), ts(export))] +#[serde(rename_all = "snake_case")] +pub enum ExportErrorKind { + SnapshotInvalid, + SnapshotTooLarge, + SheetMismatch, + TooManyPages, + /// The branding or the label set fails its schema (URLs included). + BrandingInvalid, +} + +impl ExportErrorKind { + pub fn as_str(self) -> &'static str { + match self { + Self::SnapshotInvalid => "snapshot_invalid", + Self::SnapshotTooLarge => "snapshot_too_large", + Self::SheetMismatch => "sheet_mismatch", + Self::TooManyPages => "too_many_pages", + Self::BrandingInvalid => "branding_invalid", + } + } +} + +fn error(kind: ExportErrorKind, detail: &str) -> ExportError { + ExportError { + kind, + detail: detail.to_owned(), + } +} + +/// The export's warnings, as today (`ExportWarning`). +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[cfg_attr(feature = "ts", derive(TS), ts(export))] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum ExportWarning { + #[serde(rename_all = "camelCase")] + ViewRasterFailed { + observation_id: String, + view_id: String, + }, + LogoFailed { + slot: LogoSlot, + }, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[cfg_attr(feature = "ts", derive(TS), ts(export))] +#[serde(rename_all = "camelCase")] +pub enum LogoSlot { + HeaderLogo, + FooterLogo, + PoweredBy, + Partner, +} + +/// `PAGE_CAPS` of export.ts. +pub const MAX_OBSERVATIONS: usize = 500; +pub const MAX_VIEWS_PER_OBSERVATION: usize = 8; + +/// The input once every check passed: typed branding and labels, the parsed snapshot and +/// its index, the labels of the locale. +pub struct CheckedInput { + pub report: Report, + pub snapshot: SheetSnapshot, + pub index: SheetIndex, + pub branding: Branding, + pub labels: PdfLabelSet, + pub locale: Locale, + pub exported_at: String, +} + +impl CheckedInput { + pub fn labels(&self) -> &PdfLabels { + match self.locale { + Locale::Fr => &self.labels.fr, + Locale::En => &self.labels.en, + } + } +} + +/// Step 0: the branding and the labels pass their schemas again (they reach the engine +/// from an archive or the application and end up in link URIs and drawn text). +fn typed_branding(input: &ExportInput) -> Result<(Branding, PdfLabelSet), ExportError> { + let branding = Branding::deserialize(&input.branding) + .map_err(|_| error(ExportErrorKind::BrandingInvalid, "branding"))?; + let labels = PdfLabelSet::deserialize(&input.labels) + .map_err(|_| error(ExportErrorKind::BrandingInvalid, "labels"))?; + Ok((branding, labels)) +} + +/// Step 1: the snapshot gate, with every asset accepted by the SVG reader (R3's reader +/// since R4: a drawing it refuses is `snapshot_invalid`, declared RUST.md §9.4). +fn checked_snapshot(value: &Value) -> Result { + let snapshot = assert_snapshot(value).map_err(|rejection| match rejection { + SnapshotRejection::Caps => error(ExportErrorKind::SnapshotTooLarge, "caps"), + other => error(ExportErrorKind::SnapshotInvalid, &reason_of(other)), + })?; + for asset in &snapshot.assets { + if let Err(rejection) = read_svg(&asset.svg) { + let detail = format!("asset {}", rejection.reason()); + return Err(error(ExportErrorKind::SnapshotInvalid, &detail)); + } + } + Ok(snapshot) +} + +/// The rejection's serde name (`caps`, `schema`, …), today's `SnapshotError.reason`. +fn reason_of(rejection: SnapshotRejection) -> String { + serde_json::to_value(rejection) + .ok() + .and_then(|v| v.as_str().map(str::to_owned)) + .unwrap_or_default() +} + +/// Steps 2 and 3: the report belongs to this sheet and fits the page budget. +fn assert_exportable(report: &Report, snapshot: &SheetSnapshot) -> Result<(), ExportError> { + let reference = &snapshot.reference; + if report.sheet.sheet_id != reference.sheet_id + || report.sheet.document_sha256 != reference.document_sha256 + { + return Err(error(ExportErrorKind::SheetMismatch, "")); + } + if report.observations.len() > MAX_OBSERVATIONS { + return Err(error(ExportErrorKind::TooManyPages, "observations")); + } + let too_many_views = report.observations.iter().any(|observation| { + let views: std::collections::HashSet<&str> = observation + .zones + .iter() + .map(|z| z.view_id.as_str()) + .collect(); + views.len() > MAX_VIEWS_PER_OBSERVATION + }); + if too_many_views { + return Err(error(ExportErrorKind::TooManyPages, "views")); + } + Ok(()) +} + +/// Every check of export.ts before a page is drawn, in its order. +pub fn check_input(input: ExportInput) -> Result { + let (branding, labels) = typed_branding(&input)?; + let snapshot = checked_snapshot(&input.sheet_snapshot)?; + assert_exportable(&input.report, &snapshot)?; + let index = SheetIndex::build(&snapshot.document) + .map_err(|NoMainView| error(ExportErrorKind::SnapshotInvalid, "no main view"))?; + Ok(CheckedInput { + report: input.report, + snapshot, + index, + branding, + labels, + locale: input.locale, + exported_at: input.exported_at, + }) +} + +/// The rasters the layout embeds (R4.4: given by the caller; R4.6: requested through the +/// session). A slot or view absent from the map, or whose bytes are not a PNG the header +/// of which can be read, draws nothing and leaves a warning, as `embedPng` throwing did. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Rasters { + pub header_logo: Option>, + pub footer_logo: Option>, + pub powered_by: Option>, + pub partner: Option>, + /// By `(observation id, view id)`. + pub views: HashMap<(String, String), Vec>, +} diff --git a/crates/core/src/pdf/write/layout/conclusion.rs b/crates/core/src/pdf/write/layout/conclusion.rs new file mode 100644 index 00000000..7c3a15f1 --- /dev/null +++ b/crates/core/src/pdf/write/layout/conclusion.rs @@ -0,0 +1,148 @@ +//! `render/conclusionPage.ts`: the synthesis page (conclusion, recommendations, next +//! appointment). + +use super::header::{HeaderAssets, draw_header}; +use super::{ + BODY_BOLD, BODY_REGULAR, COLOR_ACCENT, COLOR_INK, COLOR_MUTED, EYEBROW_Y_OFFSET, + HEADING_REGULAR, Layout, MARGIN_X, SECTION_LABEL_GAP, TITLE_RULE_OFFSET, TITLE_RULE_WIDTH, + TITLE_Y_OFFSET, +}; +use crate::domain::report::NextAppointment; +use crate::pdf::write::dates::format_date; +use crate::pdf::write::draw_log::{LineOp, PAGE_HEIGHT, PAGE_WIDTH, Point, Rgb, TextOp}; +use crate::pdf::write::text::{safe_text, wrap_lines}; + +const SECTION_TOP_GAP: f64 = 28.0; +const SECTION_BLOCK_GAP: f64 = 18.0; +const TEXT_LINE_HEIGHT: f64 = 14.0; + +struct Section { + label: String, + body: String, + color: Rgb, +} + +/// A JavaScript `a || b` on strings: the empty one gives way. +fn or_empty(value: &str, empty: &str) -> String { + if value.is_empty() { empty } else { value }.to_owned() +} + +fn next_appointment_text(layout: &Layout<'_>, next: &NextAppointment) -> String { + match next { + NextAppointment::Date(date) => format_date(&date.date, layout.input.locale), + NextAppointment::Period(period) => period.text.clone(), + } +} + +fn sections(layout: &Layout<'_>) -> Vec
{ + let report = &layout.input.report; + let labels = layout.labels(); + let mut sections = vec![ + Section { + label: labels.pdf_section_conclusion.clone(), + body: or_empty(&report.conclusion, &labels.pdf_empty), + color: COLOR_INK, + }, + Section { + label: labels.pdf_section_recommendations.clone(), + body: or_empty(&report.recommendations, &labels.pdf_empty), + color: COLOR_INK, + }, + ]; + if let Some(next) = &report.next_appointment { + sections.push(Section { + label: labels.pdf_section_next_appointment.clone(), + body: or_empty(&next_appointment_text(layout, next), &labels.pdf_empty), + color: COLOR_MUTED, + }); + } + sections +} + +fn draw_title(layout: &mut Layout<'_>, page: usize) -> f64 { + let labels = layout.labels(); + let eyebrow = labels.pdf_eyebrow_synthesis.clone(); + let title = labels.pdf_title_conclusion.clone(); + let eyebrow_y = PAGE_HEIGHT - EYEBROW_Y_OFFSET; + let title_y = PAGE_HEIGHT - TITLE_Y_OFFSET; + layout.log.text( + page, + TextOp { + text: safe_text(&eyebrow), + x: MARGIN_X, + y: eyebrow_y, + size: 9.0, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); + layout.log.text( + page, + TextOp { + text: safe_text(&title), + x: MARGIN_X, + y: title_y, + size: 22.0, + font: HEADING_REGULAR, + color: COLOR_INK, + }, + ); + layout.log.line( + page, + LineOp { + start: Point { + x: MARGIN_X, + y: title_y - TITLE_RULE_OFFSET, + }, + end: Point { + x: MARGIN_X + TITLE_RULE_WIDTH, + y: title_y - TITLE_RULE_OFFSET, + }, + thickness: 2.0, + color: COLOR_ACCENT, + }, + ); + title_y +} + +fn draw_section(layout: &mut Layout<'_>, page: usize, section: &Section, mut y: f64) -> f64 { + let max = PAGE_WIDTH - MARGIN_X * 2.0; + layout.log.text( + page, + TextOp { + text: safe_text(§ion.label), + x: MARGIN_X, + y, + size: 9.0, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); + y -= SECTION_LABEL_GAP + 10.0; + for line in wrap_lines(§ion.body, BODY_REGULAR, 11.0, max) { + layout.log.text( + page, + TextOp { + text: safe_text(&line), + x: MARGIN_X, + y, + size: 11.0, + font: BODY_REGULAR, + color: section.color, + }, + ); + y -= TEXT_LINE_HEIGHT; + } + y - SECTION_BLOCK_GAP +} + +/// `addConclusionPage`. +pub fn add_conclusion_page(layout: &mut Layout<'_>, header: &HeaderAssets<'_>) { + let page = layout.log.add_page(); + draw_header(layout, page, header); + let title_y = draw_title(layout, page); + let mut y = title_y - SECTION_TOP_GAP; + for section in sections(layout) { + y = draw_section(layout, page, §ion, y); + } +} diff --git a/crates/core/src/pdf/write/layout/footer.rs b/crates/core/src/pdf/write/layout/footer.rs new file mode 100644 index 00000000..a8f5af09 --- /dev/null +++ b/crates/core/src/pdf/write/layout/footer.rs @@ -0,0 +1,189 @@ +//! `render/footer.ts`: rule, site logo, powered-by credit, partner credit, page number. + +use super::{BODY_REGULAR, BrandingImages, COLOR_MUTED, CreditImage, Image, Layout, MARGIN_X}; +use crate::pdf::write::draw_log::{ImageOp, LineOp, PAGE_WIDTH, Point, Rect, Rgb, TextOp, rgb}; +use crate::pdf::write::text::{measure, safe_text}; + +const FOOTER_BASE_Y: f64 = 20.0; +const FONT_SIZE: f64 = 8.0; +const LOGO_W: f64 = 80.0; +const LOGO_H: f64 = 30.0; +const ROW_Y: f64 = FOOTER_BASE_Y + 14.0; +const TEXT_BASE_Y: f64 = ROW_Y + 6.0; +const CREDIT_LOGO_H: f64 = 16.0; +const POWERED_LOGO_W: f64 = 70.0; +const COLOR_RULE: Rgb = rgb(0.85, 0.83, 0.8); +const COLOR_CREDIT: Rgb = rgb(0.28, 0.56, 0.42); + +fn draw_rule(layout: &mut Layout<'_>, page: usize) { + let separator_y = ROW_Y + LOGO_H + 6.0; + layout.log.line( + page, + LineOp { + start: Point { + x: MARGIN_X, + y: separator_y, + }, + end: Point { + x: PAGE_WIDTH - MARGIN_X, + y: separator_y, + }, + thickness: 0.5, + color: COLOR_RULE, + }, + ); +} + +fn draw_linked_image(layout: &mut Layout<'_>, page: usize, image: Image, rect: Rect, url: &str) { + layout.log.image( + page, + ImageOp { + embed: image.embed, + x: rect.x, + y: rect.y, + width: rect.width, + height: rect.height, + }, + ); + layout.log.link(page, rect, url); +} + +fn draw_site_logo(layout: &mut Layout<'_>, page: usize, assets: &BrandingImages) { + let Some(footer) = assets.footer else { return }; + let rect = Rect { + x: MARGIN_X, + y: ROW_Y, + width: LOGO_W, + height: LOGO_H, + }; + draw_linked_image(layout, page, footer, rect, &assets.site_url); +} + +fn draw_muted_label(layout: &mut Layout<'_>, page: usize, text: &str, x: f64) { + layout.log.text( + page, + TextOp { + text: safe_text(text), + x, + y: TEXT_BASE_Y, + size: FONT_SIZE, + font: BODY_REGULAR, + color: COLOR_MUTED, + }, + ); +} + +/// The credit's fallback text in the accent colour, linked. +fn draw_credit_text(layout: &mut Layout<'_>, page: usize, credit: &CreditImage, x: f64) { + layout.log.text( + page, + TextOp { + text: safe_text(&credit.fallback_text), + x, + y: TEXT_BASE_Y, + size: FONT_SIZE, + font: BODY_REGULAR, + color: COLOR_CREDIT, + }, + ); + let rect = Rect { + x, + y: TEXT_BASE_Y - 2.0, + width: measure(BODY_REGULAR, &credit.fallback_text, FONT_SIZE), + height: FONT_SIZE + 4.0, + }; + layout.log.link(page, rect, &credit.url); +} + +fn draw_credit_logo( + layout: &mut Layout<'_>, + page: usize, + credit: &CreditImage, + x: f64, + width: f64, +) { + let Some(image) = credit.image else { return }; + let rect = Rect { + x, + y: ROW_Y + 2.0, + width, + height: CREDIT_LOGO_H, + }; + draw_linked_image(layout, page, image, rect, &credit.url); +} + +/// Label then the logo (or the fallback text) at `x`. +fn draw_credit( + layout: &mut Layout<'_>, + page: usize, + credit: &CreditImage, + placed: (f64, f64, f64), +) { + let (x, label_w, credit_w) = placed; + if credit.image.is_some() { + draw_credit_logo(layout, page, credit, x + label_w, credit_w); + } else { + draw_credit_text(layout, page, credit, x + label_w); + } +} + +/// Centred block: label then the fixed-width logo or the fallback text. +fn draw_powered_by(layout: &mut Layout<'_>, page: usize, credit: &CreditImage) { + let label = format!("{} ", layout.labels().footer_powered_by); + let label_w = measure(BODY_REGULAR, &label, FONT_SIZE); + let credit_w = if credit.image.is_some() { + POWERED_LOGO_W + } else { + measure(BODY_REGULAR, &credit.fallback_text, FONT_SIZE) + }; + let x = (PAGE_WIDTH - label_w - credit_w) / 2.0; + draw_muted_label(layout, page, &label, x); + draw_credit(layout, page, credit, (x, label_w, credit_w)); +} + +/// Right-aligned block: label then the aspect-sized logo or the handle. +fn draw_partner(layout: &mut Layout<'_>, page: usize, credit: &CreditImage) { + let label = format!("{} ", layout.labels().footer_in_partnership); + let label_w = measure(BODY_REGULAR, &label, FONT_SIZE); + let credit_w = match credit.image { + Some(image) => CREDIT_LOGO_H * (f64::from(image.width) / f64::from(image.height)), + None => measure(BODY_REGULAR, &credit.fallback_text, FONT_SIZE), + }; + let x = PAGE_WIDTH - MARGIN_X - label_w - credit_w; + draw_muted_label(layout, page, &label, x); + draw_credit(layout, page, credit, (x, label_w, credit_w)); +} + +fn draw_page_number(layout: &mut Layout<'_>, page: usize, number: usize, total: usize) { + let text = format!("{number} / {total}"); + layout.log.text( + page, + TextOp { + x: (PAGE_WIDTH - measure(BODY_REGULAR, &text, FONT_SIZE)) / 2.0, + y: FOOTER_BASE_Y + 2.0, + size: FONT_SIZE, + font: BODY_REGULAR, + color: COLOR_MUTED, + text: safe_text(&text), + }, + ); +} + +/// `drawFooter` of page `page` (0-based) numbered `number` of `total`. +pub fn draw_footer( + layout: &mut Layout<'_>, + page: usize, + number: usize, + total: usize, + assets: &BrandingImages, +) { + draw_rule(layout, page); + draw_site_logo(layout, page, assets); + if let Some(credit) = &assets.powered_by { + draw_powered_by(layout, page, credit); + } + if let Some(credit) = &assets.partner { + draw_partner(layout, page, credit); + } + draw_page_number(layout, page, number, total); +} diff --git a/crates/core/src/pdf/write/layout/header.rs b/crates/core/src/pdf/write/layout/header.rs new file mode 100644 index 00000000..6495d345 --- /dev/null +++ b/crates/core/src/pdf/write/layout/header.rs @@ -0,0 +1,145 @@ +//! `render/header.ts`: practitioner block left, logo centred, animal block right, a rule. + +use super::{BODY_BOLD, BODY_REGULAR, COLOR_INK, COLOR_MUTED, Image, Layout, MARGIN_X}; +use crate::pdf::write::dates::format_date; +use crate::pdf::write::draw_log::{ImageOp, LineOp, PAGE_WIDTH, Point, Rect, TextOp}; +use crate::pdf::write::text::{measure, safe_text}; + +const TOP_Y: f64 = 800.0; +const LOGO_WIDTH: f64 = 100.0; +const LOGO_HEIGHT: f64 = 38.0; + +pub struct HeaderAssets<'a> { + pub logo: Option, + pub site_url: &'a str, +} + +/// A JavaScript truthy string: present and not empty. +fn truthy(text: Option<&String>) -> Option<&str> { + text.map(String::as_str).filter(|t| !t.is_empty()) +} + +fn practitioner_lines(layout: &Layout<'_>) -> Vec { + let practitioner = &layout.input.report.practitioner; + let siret = truthy(practitioner.siret.as_ref()) + .map(|siret| format!("{}{siret}", layout.labels().pdf_siret_prefix)); + [ + truthy(Some(&practitioner.name)).map(str::to_owned), + truthy(practitioner.cabinet.as_ref()).map(str::to_owned), + truthy(practitioner.contact.as_ref()).map(str::to_owned), + siret, + ] + .into_iter() + .flatten() + .collect() +} + +fn animal_lines(layout: &Layout<'_>) -> Vec { + let report = &layout.input.report; + let animal = &report.animal; + let parts = [ + Some(animal.name.clone()), + truthy(animal.breed.as_ref()).map(|b| format!("({b})")), + truthy(animal.age.as_ref()).map(|a| format!("· {a}")), + ]; + let animal_line = parts + .into_iter() + .flatten() + .filter(|part| !part.is_empty()) + .collect::>() + .join(" "); + vec![ + animal_line, + format!("{}{}", layout.labels().pdf_owner_prefix, report.owner.name), + format_date(&report.date, layout.input.locale), + ] +} + +/// The first line is the title (11 pt bold ink), the others 9 pt regular muted. +fn line_style(index: usize) -> (f64, super::Font, super::Rgb) { + if index == 0 { + (11.0, BODY_BOLD, COLOR_INK) + } else { + (9.0, BODY_REGULAR, COLOR_MUTED) + } +} + +fn draw_left_column(layout: &mut Layout<'_>, page: usize, lines: &[String]) { + for (i, line) in lines.iter().enumerate() { + let (size, font, color) = line_style(i); + layout.log.text( + page, + TextOp { + text: safe_text(line), + x: MARGIN_X, + y: TOP_Y - i as f64 * 12.0, + size, + font, + color, + }, + ); + } +} + +fn draw_right_column(layout: &mut Layout<'_>, page: usize, lines: &[String]) { + for (i, line) in lines.iter().enumerate() { + let (size, font, color) = line_style(i); + layout.log.text( + page, + TextOp { + text: safe_text(line), + x: PAGE_WIDTH - MARGIN_X - measure(font, line, size), + y: TOP_Y - i as f64 * 12.0, + size, + font, + color, + }, + ); + } +} + +fn draw_logo(layout: &mut Layout<'_>, page: usize, assets: &HeaderAssets<'_>) { + let Some(logo) = assets.logo else { return }; + let rect = Rect { + x: (PAGE_WIDTH - LOGO_WIDTH) / 2.0, + y: TOP_Y - LOGO_HEIGHT + 10.0, + width: LOGO_WIDTH, + height: LOGO_HEIGHT, + }; + layout.log.image( + page, + ImageOp { + embed: logo.embed, + x: rect.x, + y: rect.y, + width: rect.width, + height: rect.height, + }, + ); + layout.log.link(page, rect, assets.site_url); +} + +/// `drawHeader`. +pub fn draw_header(layout: &mut Layout<'_>, page: usize, assets: &HeaderAssets<'_>) { + let left = practitioner_lines(layout); + draw_left_column(layout, page, &left); + draw_logo(layout, page, assets); + let right = animal_lines(layout); + draw_right_column(layout, page, &right); + let separator_y = TOP_Y - (left.len() as f64 * 12.0).max(LOGO_HEIGHT) - 8.0; + layout.log.line( + page, + LineOp { + start: Point { + x: MARGIN_X, + y: separator_y, + }, + end: Point { + x: PAGE_WIDTH - MARGIN_X, + y: separator_y, + }, + thickness: 0.5, + color: COLOR_MUTED, + }, + ); +} diff --git a/crates/core/src/pdf/write/layout/mod.rs b/crates/core/src/pdf/write/layout/mod.rs index f25dc9ac..348433b9 100644 --- a/crates/core/src/pdf/write/layout/mod.rs +++ b/crates/core/src/pdf/write/layout/mod.rs @@ -1 +1,199 @@ -//! R4.4b: the page layouts. Written by the layout track. +//! The page layouts (`packages/pdf/src/render/*` and the page loop of `export.ts`): one +//! observation page per observation, the conclusion page, then the footer of every page, +//! into a [`DrawLog`]. Every number is computed in the TypeScript's order on f64, so the +//! log matches the O1 oracle bit for bit. + +mod conclusion; +mod footer; +mod header; +mod observation; +mod png; + +use super::draw_log::{DrawLog, Rgb, rgb}; +use super::input::{CheckedInput, ExportError, ExportErrorKind, ExportWarning, LogoSlot, Rasters}; +use super::text::Font; +use crate::domain::archive::{Credit, ImageAsset, PdfLabels}; + +/// `render/constants.ts`: values identical across callers. +pub const MARGIN_X: f64 = 36.0; +pub const EYEBROW_Y_OFFSET: f64 = 95.0; +pub const TITLE_Y_OFFSET: f64 = 122.0; +pub const TITLE_RULE_OFFSET: f64 = 12.0; +pub const TITLE_RULE_WIDTH: f64 = 48.0; +pub const SECTION_LABEL_GAP: f64 = 4.0; +pub const COLOR_INK: Rgb = rgb(0.12, 0.11, 0.09); +pub const COLOR_MUTED: Rgb = rgb(0.42, 0.39, 0.35); +pub const COLOR_ACCENT: Rgb = rgb(0.72, 0.35, 0.25); + +/// `render/fonts.ts`: Helvetica body, Times heading. +pub const BODY_REGULAR: Font = Font::Helvetica; +pub const BODY_BOLD: Font = Font::HelveticaBold; +pub const HEADING_REGULAR: Font = Font::TimesRoman; + +/// An image embedded in the document, by index, with its pixel size. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Image { + pub embed: usize, + pub width: u32, + pub height: u32, +} + +/// `CreditAsset`: the credit's logo when it embedded, its URL and fallback text. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CreditImage { + pub image: Option, + pub url: String, + pub fallback_text: String, +} + +/// `BrandingImages`: the four slots once embedded, in a fixed order. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BrandingImages { + pub site_url: String, + pub header: Option, + pub footer: Option, + pub powered_by: Option, + pub partner: Option, +} + +/// The whole export's drawing state. +pub struct Layout<'a> { + pub input: &'a CheckedInput, + pub log: DrawLog, + pub warnings: Vec, +} + +impl<'a> Layout<'a> { + pub fn labels(&self) -> &'a PdfLabels { + self.input.labels() + } + + /// Embeds `png` and returns its handle; `None` (a warning for the caller) when the + /// bytes are not a PNG whose header can be read, as `embedPng` throwing was. + fn embed(&mut self, png: &[u8]) -> Option { + let (width, height) = png::dimensions(png)?; + let embed = self.log.embed(png.to_vec(), width, height); + Some(Image { + embed, + width, + height, + }) + } + + /// `embedSlot`: a PNG asset decodes its own base64, an SVG asset takes the raster the + /// caller made of it; any failure is one `logo_failed` warning and no image. + fn embed_slot( + &mut self, + slot: LogoSlot, + asset: Option<&ImageAsset>, + raster: Option<&[u8]>, + ) -> Option { + let asset = asset?; + let image = match asset { + ImageAsset::Png(png) => png::base64_bytes(&png.base64).and_then(|b| self.embed(&b)), + ImageAsset::Svg(_) => raster.and_then(|bytes| self.embed(bytes)), + }; + if image.is_none() { + self.warnings.push(ExportWarning::LogoFailed { slot }); + } + image + } + + fn embed_credit( + &mut self, + slot: LogoSlot, + credit: Option<&Credit>, + raster: Option<&[u8]>, + ) -> Option { + let credit = credit?; + Some(CreditImage { + image: self.embed_slot(slot, credit.logo.as_ref(), raster), + url: credit.url.clone(), + fallback_text: credit.fallback_text.clone(), + }) + } + + /// `embedBranding`: header, footer, powered-by, partner, in that order. + fn embed_branding(&mut self, rasters: &Rasters) -> BrandingImages { + let branding = &self.input.branding; + BrandingImages { + site_url: branding.site_url.clone(), + header: self.embed_slot( + LogoSlot::HeaderLogo, + branding.header_logo.as_ref(), + rasters.header_logo.as_deref(), + ), + footer: self.embed_slot( + LogoSlot::FooterLogo, + branding.footer_logo.as_ref(), + rasters.footer_logo.as_deref(), + ), + powered_by: self.embed_credit( + LogoSlot::PoweredBy, + branding.powered_by.as_ref(), + rasters.powered_by.as_deref(), + ), + partner: self.embed_credit( + LogoSlot::Partner, + branding.partner.as_ref(), + rasters.partner.as_deref(), + ), + } + } +} + +/// Printable ASCII without the space: what the link sink writes as hex bytes +/// (`linkAnnotation.ts`); anything else means the branding bypassed validation. +fn assert_link_urls(branding: &BrandingImages) -> Result<(), ExportError> { + let urls = [ + Some(&branding.site_url), + branding.powered_by.as_ref().map(|c| &c.url), + branding.partner.as_ref().map(|c| &c.url), + ]; + let printable = |url: &String| url.chars().all(|c| ('\u{21}'..='\u{7e}').contains(&c)); + if urls.into_iter().flatten().all(printable) { + return Ok(()); + } + Err(ExportError { + kind: ExportErrorKind::BrandingInvalid, + detail: "url".to_owned(), + }) +} + +/// The drawing of the whole report (`drawPages` of export.ts) and its warnings. +pub struct Drawn { + pub log: DrawLog, + pub warnings: Vec, +} + +/// Lays the report out: branding embedded first, one page per observation, the +/// conclusion page, then every page's footer with its pagination. +pub fn layout(input: &CheckedInput, rasters: &Rasters) -> Result { + let mut layout = Layout { + input, + log: DrawLog::default(), + warnings: Vec::new(), + }; + let branding = layout.embed_branding(rasters); + assert_link_urls(&branding)?; + let header = header::HeaderAssets { + logo: branding.header, + site_url: &branding.site_url, + }; + for (position, observation) in input.report.observations.iter().enumerate() { + observation::add_observation_page(&mut layout, &header, position, observation, rasters); + } + conclusion::add_conclusion_page(&mut layout, &header); + let total = layout.log.pages.len(); + for page in 0..total { + layout.log.begin_footer(page); + footer::draw_footer(&mut layout, page, page + 1, total, &branding); + } + // A page's annotations are its own array: the header link, then the footer's, in + // page order, whatever the order the calls were made in. + layout.log.links.sort_by_key(|link| link.page); + Ok(Drawn { + log: layout.log, + warnings: layout.warnings, + }) +} diff --git a/crates/core/src/pdf/write/layout/observation.rs b/crates/core/src/pdf/write/layout/observation.rs new file mode 100644 index 00000000..94016f06 --- /dev/null +++ b/crates/core/src/pdf/write/layout/observation.rs @@ -0,0 +1,673 @@ +//! `render/observationPage.ts`: one page per observation: header, title block with the +//! severity pill, the image grid of the observation's views, then the sections (zones, +//! findings by zone and side, free findings, severity, comment). + +use std::collections::HashMap; + +use super::header::{HeaderAssets, draw_header}; +use super::{ + BODY_BOLD, BODY_REGULAR, COLOR_ACCENT, COLOR_INK, EYEBROW_Y_OFFSET, HEADING_REGULAR, Image, + Layout, MARGIN_X, SECTION_LABEL_GAP, TITLE_RULE_OFFSET, TITLE_RULE_WIDTH, TITLE_Y_OFFSET, +}; +use crate::domain::archive::{Locale, PdfLabels}; +use crate::domain::report::{Finding, LateralSide, Observation, ReportZone, Severity}; +use crate::domain::sheet::IndexedDocument; +use crate::domain::sheet::schema::{LocalizedText, SheetView}; +use crate::json::js_number; +use crate::pdf::write::draw_log::{ + ImageOp, LineOp, PAGE_HEIGHT, PAGE_WIDTH, Point, RectangleOp, Rgb, TextOp, rgb, +}; +use crate::pdf::write::input::{ExportWarning, Rasters}; +use crate::pdf::write::text::{js_trim, js_upper, measure, safe_text, wrap_lines}; + +const MARGIN_BOTTOM: f64 = 48.0; +const IMAGE_TOP_GAP: f64 = 32.0; +const IMAGE_LABEL_GAP: f64 = 14.0; +const IMAGE_BOTTOM_GAP: f64 = 14.0; +const IMAGE_COL_GAP: f64 = 16.0; +const IMAGE_ROW_GAP: f64 = 12.0; +const IMAGE_MIN_CELL_H: f64 = 60.0; +const TEXT_TOP_GAP: f64 = 10.0; +const TEXT_LINE_HEIGHT: f64 = 13.0; +const SECTION_BLOCK_GAP: f64 = 10.0; +const COLOR_WHITE: Rgb = rgb(1.0, 1.0, 1.0); +const SECTION_TEXT_SIZE: f64 = 10.0; +const SEPARATOR_GLYPH: &str = "•"; +const SEPARATOR_SIZE: f64 = 14.0; +const SEPARATOR_SIDE_GAP: f64 = 5.0; +/// Items of a section joined by this print as a segmented line. +const ITEM_JOIN: &str = " · "; + +fn severity_bg(severity: Severity) -> Rgb { + match severity { + Severity::Mild => rgb(0.55, 0.65, 0.45), + Severity::Moderate => rgb(0.85, 0.55, 0.18), + Severity::Severe => rgb(0.78, 0.2, 0.2), + } +} + +fn severity_label(labels: &PdfLabels, severity: Severity) -> &str { + match severity { + Severity::Mild => &labels.severity_mild, + Severity::Moderate => &labels.severity_moderate, + Severity::Severe => &labels.severity_severe, + } +} + +fn side_label(labels: &PdfLabels, side: LateralSide) -> &str { + match side { + LateralSide::Left => &labels.lateral_left, + LateralSide::Right => &labels.lateral_right, + } +} + +/// `pickLabel(text, locale)`. +fn pick(text: &LocalizedText, locale: Locale) -> &str { + match locale { + Locale::Fr => &text.fr, + Locale::En => &text.en, + } +} + +// --------------------------------------------------------------------------- +// Segmented text + +/// Width of the visible separator block including the empty space on each side. +fn separator_width() -> f64 { + measure(BODY_BOLD, SEPARATOR_GLYPH, SEPARATOR_SIZE) + SEPARATOR_SIDE_GAP * 2.0 +} + +/// Counts the line breaks a list of items would consume when laid out on a `max_width` line. +fn count_segmented_lines(items: &[&str], max_width: f64) -> usize { + if items.is_empty() { + return 1; + } + let sep_w = separator_width(); + let (mut lines, mut x) = (1, 0.0); + for (i, item) in items.iter().enumerate() { + let item_w = measure(BODY_REGULAR, item, SECTION_TEXT_SIZE); + if x + item_w > max_width && x > 0.0 { + lines += 1; + x = 0.0; + } + x += item_w; + if i < items.len() - 1 { + if x + sep_w > max_width { + lines += 1; + x = 0.0; + } else { + x += sep_w; + } + } + } + lines +} + +fn draw_separator(layout: &mut Layout<'_>, page: usize, x: f64, y: f64) { + let baseline_offset = (SEPARATOR_SIZE - SECTION_TEXT_SIZE) * 0.3; + layout.log.text( + page, + TextOp { + text: safe_text(SEPARATOR_GLYPH), + x: x + SEPARATOR_SIDE_GAP, + y: y - baseline_offset, + size: SEPARATOR_SIZE, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); +} + +fn draw_body_text(layout: &mut Layout<'_>, page: usize, text: &str, x: f64, y: f64) { + layout.log.text( + page, + TextOp { + text: safe_text(text), + x, + y, + size: SECTION_TEXT_SIZE, + font: BODY_REGULAR, + color: COLOR_INK, + }, + ); +} + +/// Items separated by a visibly larger accent bullet; returns the y below the last line. +fn draw_segmented_text( + layout: &mut Layout<'_>, + page: usize, + items: &[&str], + start: (f64, f64), + max_width: f64, +) -> f64 { + let (start_x, start_y) = start; + if items.is_empty() { + return start_y - TEXT_LINE_HEIGHT; + } + let sep_w = separator_width(); + let (mut x, mut y) = (start_x, start_y); + for (i, item) in items.iter().enumerate() { + let item_w = measure(BODY_REGULAR, item, SECTION_TEXT_SIZE); + if x + item_w > start_x + max_width && x > start_x { + x = start_x; + y -= TEXT_LINE_HEIGHT; + } + draw_body_text(layout, page, item, x, y); + x += item_w; + if i == items.len() - 1 { + continue; + } + if x + sep_w > start_x + max_width { + x = start_x; + y -= TEXT_LINE_HEIGHT; + } else { + draw_separator(layout, page, x, y); + x += sep_w; + } + } + y - TEXT_LINE_HEIGHT +} + +// --------------------------------------------------------------------------- +// Title block + +fn draw_severity_pill( + layout: &mut Layout<'_>, + page: usize, + severity: Severity, + anchor: (f64, f64), +) { + let (num_w, title_y) = anchor; + let sev_text = js_upper(severity_label(layout.labels(), severity)); + let (sev_size, pad_x, pad_y) = (9.0, 8.0, 4.0); + let pill_w = measure(BODY_BOLD, &sev_text, sev_size) + pad_x * 2.0; + let pill_h = sev_size + pad_y * 2.0; + let pill_x = MARGIN_X + num_w + 14.0; + let pill_y = title_y + 4.0; + layout.log.rectangle( + page, + RectangleOp { + x: pill_x, + y: pill_y, + width: pill_w, + height: pill_h, + color: severity_bg(severity), + }, + ); + layout.log.text( + page, + TextOp { + text: safe_text(&sev_text), + x: pill_x + pad_x, + y: pill_y + pad_y, + size: sev_size, + font: BODY_BOLD, + color: COLOR_WHITE, + }, + ); +} + +/// Eyebrow, the two-digit number, the severity pill, the accent rule; returns `titleY`. +fn draw_title_block( + layout: &mut Layout<'_>, + page: usize, + position: usize, + severity: Option, +) -> f64 { + let eyebrow_y = PAGE_HEIGHT - EYEBROW_Y_OFFSET; + let title_y = PAGE_HEIGHT - TITLE_Y_OFFSET; + let obs_label = format!("{:02}", position + 1); + let eyebrow = layout.labels().pdf_eyebrow_observation.clone(); + layout.log.text( + page, + TextOp { + text: safe_text(&eyebrow), + x: MARGIN_X, + y: eyebrow_y, + size: 9.0, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); + layout.log.text( + page, + TextOp { + text: safe_text(&obs_label), + x: MARGIN_X, + y: title_y, + size: 22.0, + font: HEADING_REGULAR, + color: COLOR_INK, + }, + ); + if let Some(severity) = severity { + let num_w = measure(HEADING_REGULAR, &obs_label, 22.0); + draw_severity_pill(layout, page, severity, (num_w, title_y)); + } + layout.log.line( + page, + LineOp { + start: Point { + x: MARGIN_X, + y: title_y - TITLE_RULE_OFFSET, + }, + end: Point { + x: MARGIN_X + TITLE_RULE_WIDTH, + y: title_y - TITLE_RULE_OFFSET, + }, + thickness: 2.0, + color: COLOR_ACCENT, + }, + ); + title_y +} + +// --------------------------------------------------------------------------- +// Images + +/// Unique views the observation touches and the snapshot holds, main first (stable). +pub fn observation_view_ids<'a>( + sheet: &IndexedDocument<'_>, + observation: &'a Observation, +) -> Vec<&'a str> { + let mut seen = Vec::new(); + for zone in &observation.zones { + let id = zone.view_id.as_str(); + if !seen.contains(&id) && sheet.view(id).is_some() { + seen.push(id); + } + } + let main = sheet.main_view().id(); + // `sort` by `Number(b is main) - Number(a is main)`: the main view first, the rest kept. + let (mains, others): (Vec<_>, Vec<_>) = seen.into_iter().partition(|id| *id == main); + mains.into_iter().chain(others).collect() +} + +fn view_label(layout: &Layout<'_>, view: &SheetView) -> String { + let labels = layout.labels(); + match view { + SheetView::Main(_) => labels.pdf_view_general.clone(), + SheetView::Detail(_) => { + let name = js_upper(pick(view.name(), layout.input.locale)); + format!("{}{name}", labels.pdf_view_detail_prefix) + } + } +} + +struct Rendered { + label: String, + image: Image, + aspect: f64, +} + +/// The observation's views embedded in view order; a missing or unreadable raster is a +/// `view_raster_failed` warning. +fn build_images( + layout: &mut Layout<'_>, + observation: &Observation, + rasters: &Rasters, +) -> Vec { + let sheet = layout.input.index.over(&layout.input.snapshot.document); + let view_ids: Vec = observation_view_ids(&sheet, observation) + .into_iter() + .map(str::to_owned) + .collect(); + let mut rendered = Vec::new(); + for view_id in view_ids { + let key = (observation.id.clone(), view_id.clone()); + let image = rasters.views.get(&key).and_then(|png| layout.embed(png)); + let Some(image) = image else { + layout.warnings.push(ExportWarning::ViewRasterFailed { + observation_id: observation.id.clone(), + view_id, + }); + continue; + }; + let sheet = layout.input.index.over(&layout.input.snapshot.document); + let view = sheet.view(&view_id); + let label = view.map_or_else(String::new, |v| view_label(layout, v)); + let aspect = if image.width > 0 { + f64::from(image.height) / f64::from(image.width) + } else { + 0.6 + }; + rendered.push(Rendered { + label, + image, + aspect, + }); + } + rendered +} + +struct Grid { + cols: usize, + rows: usize, + cell_w: f64, + offset_x: f64, + row_budget: f64, +} + +fn grid_of(count: usize, avail_h: f64) -> Grid { + let is_single = count == 1; + let cols = if is_single { 1 } else { 2 }; + let rows = count.div_ceil(cols); + let full_cell_w = + (PAGE_WIDTH - MARGIN_X * 2.0 - IMAGE_COL_GAP * (cols - 1) as f64) / cols as f64; + let cell_w = if is_single { + full_cell_w / 2.0 + } else { + full_cell_w + }; + let rows_f = rows as f64; + Grid { + cols, + rows, + cell_w, + offset_x: if is_single { + (full_cell_w - cell_w) / 2.0 + } else { + 0.0 + }, + row_budget: IMAGE_MIN_CELL_H + .max((avail_h - rows_f * IMAGE_LABEL_GAP - (rows_f - 1.0) * IMAGE_ROW_GAP) / rows_f), + } +} + +/// One row of the grid: labels above, then the images; returns the row height. +fn draw_image_row( + layout: &mut Layout<'_>, + page: usize, + row: &[Rendered], + grid: &Grid, + top_y: f64, +) -> f64 { + let cell_hs: Vec = row + .iter() + .map(|it| (grid.cell_w * it.aspect).min(grid.row_budget)) + .collect(); + let cell_x = |c: usize| MARGIN_X + grid.offset_x + c as f64 * (grid.cell_w + IMAGE_COL_GAP); + for (c, it) in row.iter().enumerate() { + layout.log.text( + page, + TextOp { + text: safe_text(&it.label), + x: cell_x(c), + y: top_y, + size: 8.0, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); + } + let images_y = top_y - IMAGE_LABEL_GAP; + for (c, it) in row.iter().enumerate() { + layout.log.image( + page, + ImageOp { + embed: it.image.embed, + x: cell_x(c), + y: images_y - cell_hs[c], + width: grid.cell_w, + height: cell_hs[c], + }, + ); + } + cell_hs.iter().fold(0.0, |max, h| f64::max(max, *h)) +} + +/// The (up to 2-column) image grid; returns the cursor y below it. +fn draw_image_grid( + layout: &mut Layout<'_>, + page: usize, + rendered: &[Rendered], + start_y: f64, + reserved_bottom: f64, +) -> f64 { + if rendered.is_empty() { + return start_y; + } + let avail_h = IMAGE_MIN_CELL_H.max(start_y - reserved_bottom); + let grid = grid_of(rendered.len(), avail_h); + let mut cursor_y = start_y; + for r in 0..grid.rows { + let row = &rendered[r * grid.cols..(r * grid.cols + grid.cols).min(rendered.len())]; + let row_h = draw_image_row(layout, page, row, &grid, cursor_y); + cursor_y -= IMAGE_LABEL_GAP + row_h; + if r < grid.rows - 1 { + cursor_y -= IMAGE_ROW_GAP; + } + } + cursor_y - IMAGE_BOTTOM_GAP +} + +// --------------------------------------------------------------------------- +// Sections + +struct Section { + label: String, + value: String, +} + +/// Zone name: snapshot annotation, else the report's label snapshot, else the id. +fn zone_text(zone: &ReportZone, sheet: &IndexedDocument<'_>, locale: Locale) -> String { + if let Some(annotation) = sheet.annotation(&zone.annotation_id) { + return pick(&sheet.name(annotation), locale).to_owned(); + } + let (first, second) = match locale { + Locale::En => (&zone.label_en_snapshot, &zone.label_fr_snapshot), + Locale::Fr => (&zone.label_fr_snapshot, &zone.label_en_snapshot), + }; + let truthy = |s: &Option| s.as_deref().filter(|t| !t.is_empty()).map(str::to_owned); + truthy(first) + .or_else(|| truthy(second)) + .unwrap_or_else(|| zone.annotation_id.clone()) +} + +fn describe_zones(layout: &Layout<'_>, zones: &[ReportZone]) -> String { + let sheet = layout.input.index.over(&layout.input.snapshot.document); + let labels = layout.labels(); + zones + .iter() + .map(|z| { + let vertebrae = match &z.vertebrae { + Some(list) if !list.is_empty() => { + let items: Vec = list + .iter() + .map(|v| format!("{}{}", v.region, js_number(v.index.get()))) + .collect(); + format!(" ({})", items.join(", ")) + } + _ => String::new(), + }; + let side = z + .side + .map_or_else(String::new, |s| format!(" ({})", side_label(labels, s))); + format!( + "{}{vertebrae}{side}", + zone_text(z, &sheet, layout.input.locale) + ) + }) + .collect::>() + .join(ITEM_JOIN) +} + +fn finding_text(finding: &Finding, locale: Locale) -> String { + let label = match locale { + Locale::En => &finding.label_en_snapshot, + Locale::Fr => &finding.label_fr_snapshot, + }; + if finding.requires_vet_referral == Some(true) { + format!("(!) {label}") + } else { + label.clone() + } +} + +/// Groups findings by zone and side (first appearance order), one section per group. +fn findings_sections(layout: &Layout<'_>, findings: &[Finding]) -> Vec
{ + let sheet = layout.input.index.over(&layout.input.snapshot.document); + let labels = layout.labels(); + let locale = layout.input.locale; + let mut order: Vec = Vec::new(); + let mut groups: HashMap> = HashMap::new(); + for f in findings { + let key = match f.side { + Some(LateralSide::Left) => format!("{}:left", f.finding_zone_id), + Some(LateralSide::Right) => format!("{}:right", f.finding_zone_id), + None => f.finding_zone_id.clone(), + }; + if !groups.contains_key(&key) { + order.push(key.clone()); + } + groups.entry(key).or_default().push(f); + } + order + .iter() + .map(|key| { + let group = &groups[key]; + let zone_id = key.split(':').next().unwrap_or(key); + let mut zone_name = sheet + .finding_zone(zone_id) + .map_or_else(|| zone_id.to_owned(), |z| pick(&z.name, locale).to_owned()); + if let Some(side) = group.first().and_then(|f| f.side) { + zone_name = format!("{zone_name} ({})", side_label(labels, side)); + } + Section { + label: format!( + "{}{}", + labels.pdf_section_findings_prefix, + js_upper(&zone_name) + ), + value: group + .iter() + .map(|f| finding_text(f, locale)) + .collect::>() + .join(ITEM_JOIN), + } + }) + .collect() +} + +fn sections(layout: &Layout<'_>, observation: &Observation) -> Vec
{ + let labels = layout.labels(); + let zones_text = describe_zones(layout, &observation.zones); + let mut sections = vec![Section { + label: labels.pdf_section_zones.clone(), + value: if zones_text.is_empty() { + labels.pdf_empty.clone() + } else { + zones_text + }, + }]; + sections.extend(findings_sections(layout, &observation.findings)); + if !observation.free_text_findings.is_empty() { + sections.push(Section { + label: labels.pdf_section_free_findings.clone(), + value: observation.free_text_findings.join(ITEM_JOIN), + }); + } + if let Some(severity) = observation.severity { + sections.push(Section { + label: labels.pdf_section_severity.clone(), + value: severity_label(labels, severity).to_owned(), + }); + } + let comment = js_trim(&observation.comment); + if !comment.is_empty() { + sections.push(Section { + label: labels.pdf_section_comment.clone(), + value: comment.to_owned(), + }); + } + sections +} + +fn estimate_sections_height(sections: &[Section], max_text_w: f64) -> f64 { + let mut h = TEXT_TOP_GAP; + for section in sections { + let line_count = if section.value.contains(ITEM_JOIN) { + count_segmented_lines( + §ion.value.split(ITEM_JOIN).collect::>(), + max_text_w, + ) + } else { + wrap_lines(§ion.value, BODY_REGULAR, SECTION_TEXT_SIZE, max_text_w).len() + }; + h += SECTION_LABEL_GAP + 9.0 + line_count as f64 * TEXT_LINE_HEIGHT + SECTION_BLOCK_GAP; + } + h +} + +fn draw_section_value( + layout: &mut Layout<'_>, + page: usize, + value: &str, + start_y: f64, + max_text_w: f64, +) -> f64 { + if value.contains(ITEM_JOIN) { + let items: Vec<&str> = value.split(ITEM_JOIN).collect(); + return draw_segmented_text(layout, page, &items, (MARGIN_X, start_y), max_text_w); + } + let mut cursor_y = start_y; + for line in wrap_lines(value, BODY_REGULAR, SECTION_TEXT_SIZE, max_text_w) { + draw_body_text(layout, page, &line, MARGIN_X, cursor_y); + cursor_y -= TEXT_LINE_HEIGHT; + } + cursor_y +} + +fn draw_sections( + layout: &mut Layout<'_>, + page: usize, + sections: &[Section], + start_y: f64, + max_text_w: f64, +) -> f64 { + let mut cursor_y = start_y; + for section in sections { + layout.log.text( + page, + TextOp { + text: safe_text(§ion.label), + x: MARGIN_X, + y: cursor_y, + size: 8.0, + font: BODY_BOLD, + color: COLOR_ACCENT, + }, + ); + cursor_y -= SECTION_LABEL_GAP + 9.0; + cursor_y = draw_section_value(layout, page, §ion.value, cursor_y, max_text_w); + cursor_y -= SECTION_BLOCK_GAP; + } + cursor_y +} + +/// `addObservationPage`. +pub fn add_observation_page( + layout: &mut Layout<'_>, + header: &HeaderAssets<'_>, + position: usize, + observation: &Observation, + rasters: &Rasters, +) { + let page = layout.log.add_page(); + draw_header(layout, page, header); + let title_y = draw_title_block(layout, page, position, observation.severity); + let rendered = build_images(layout, observation, rasters); + let max_text_w = PAGE_WIDTH - MARGIN_X * 2.0; + let sections = sections(layout, observation); + let reserved_bottom = + MARGIN_BOTTOM + estimate_sections_height(§ions, max_text_w) + IMAGE_BOTTOM_GAP; + let mut cursor_y = draw_image_grid( + layout, + page, + &rendered, + title_y - IMAGE_TOP_GAP, + reserved_bottom, + ); + cursor_y -= TEXT_TOP_GAP; + draw_sections(layout, page, §ions, cursor_y, max_text_w); +} diff --git a/crates/core/src/pdf/write/layout/png.rs b/crates/core/src/pdf/write/layout/png.rs new file mode 100644 index 00000000..14bf7459 --- /dev/null +++ b/crates/core/src/pdf/write/layout/png.rs @@ -0,0 +1,55 @@ +//! What the layout needs from a PNG before R4.5 decodes it: its pixel size, read from the +//! IHDR as pdf-lib's `embedPng` exposed `width` and `height`; and the base64 of a branding +//! PNG asset as `atob` decoded it. + +const SIGNATURE: [u8; 8] = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; + +/// `(width, height)` of a PNG from its signature and IHDR; `None` for anything else. +pub fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + if bytes.len() < 24 || bytes[..8] != SIGNATURE || &bytes[12..16] != b"IHDR" { + return None; + } + let field = |at: usize| { + bytes + .get(at..at + 4) + .and_then(|b| <[u8; 4]>::try_from(b).ok()) + .map(u32::from_be_bytes) + }; + Some((field(16)?, field(20)?)) +} + +/// `atob`: the standard alphabet, `=` padding, ASCII whitespace skipped; `None` on any other +/// character or a dangling group. +pub fn base64_bytes(text: &str) -> Option> { + let mut out = Vec::with_capacity(text.len() / 4 * 3); + let (mut acc, mut bits) = (0u32, 0u32); + let mut padding = 0; + for c in text.chars().filter(|c| !c.is_ascii_whitespace()) { + if c == '=' { + padding += 1; + continue; + } + if padding > 0 { + return None; + } + acc = (acc << 6) | u32::from(sextet(c)?); + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + (padding <= 2 && bits < 6).then_some(out) +} + +fn sextet(c: char) -> Option { + match c { + 'A'..='Z' => Some(c as u8 - b'A'), + 'a'..='z' => Some(c as u8 - b'a' + 26), + '0'..='9' => Some(c as u8 - b'0' + 52), + '+' => Some(62), + '/' => Some(63), + _ => None, + } +} diff --git a/crates/core/src/pdf/write/text.rs b/crates/core/src/pdf/write/text.rs index 3876a41a..768521c7 100644 --- a/crates/core/src/pdf/write/text.rs +++ b/crates/core/src/pdf/write/text.rs @@ -1,5 +1,10 @@ -//! R4.4a: the text rules of the engine (`packages/pdf/src/text.ts`, `render/text.ts`). -//! Written by the R4.4a track; the signatures below are the contract the layout uses. +//! R4.4a: the text rules of the engine, `safeText` (`packages/pdf/src/text.ts`), `measure` and +//! `wrapLines` (`packages/pdf/src/render/text.ts`), with the JavaScript string rules they and +//! the layout lean on (`toUpperCase`, `trim`, `\s`). Every table is the one V8 and pdf-lib +//! 1.17.1 used, as the frozen O4 and O5 oracles recorded it (`afm_data`), never Rust's own +//! Unicode tables: the layout measures and wraps exactly as the TypeScript did. + +use super::afm_data::{self, FontMetrics}; /// The three standard fonts the engine embeds (`render/fonts.ts`), by their PDF base name. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -18,29 +23,195 @@ impl Font { Self::TimesRoman => "Times-Roman", } } + + fn metrics(self) -> &'static FontMetrics { + match self { + Self::Helvetica => &afm_data::HELVETICA, + Self::HelveticaBold => &afm_data::HELVETICA_BOLD, + Self::TimesRoman => &afm_data::TIMES_ROMAN, + } + } } -/// `safeText`: Cc, Cf, Zl and Zp dropped, other non-WinAnsi code points `?`. -pub fn safe_text(_text: &str) -> String { - unimplemented!("R4.4a") +/// pdf-lib's `widthOfGlyph` for a glyph the font gives no width: `getWidthOfGlyph(g) || 250`. +const MISSING_GLYPH_WIDTH: f64 = 250.0; + +/// The WinAnsi byte of `?`, the glyph every code point outside the encoding prints. +const QUESTION_BYTE: u8 = b'?'; + +/// Whether `code_point` lies in one of the sorted, disjoint, inclusive `ranges`. +fn in_ranges(ranges: &[(u32, u32)], code_point: u32) -> bool { + let index = ranges.partition_point(|&(_, end)| end < code_point); + ranges + .get(index) + .is_some_and(|&(start, _)| start <= code_point) } -/// JavaScript `String.prototype.toUpperCase()`. -pub fn js_upper(_text: &str) -> String { - unimplemented!("R4.4a") +/// `Encodings.WinAnsi.encodeUnicodeCodePoint(c).code`, `None` where pdf-lib cannot encode. +fn winansi_byte(c: char) -> Option { + let table = afm_data::WINANSI; + let index = table + .binary_search_by_key(&u32::from(c), |&(cp, _)| cp) + .ok()?; + table.get(index).map(|&(_, byte)| byte) } -/// JavaScript `String.prototype.trim()`. -pub fn js_trim(_text: &str) -> &str { - unimplemented!("R4.4a") +/// `[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]` as V8 matched it (`text.ts` `DROPPED`). +fn is_dropped(c: char) -> bool { + in_ranges(afm_data::SAFE_TEXT_DROPPED, u32::from(c)) } -/// `measure(font, text, size) = widthOfTextAtSize(safeText(text), size)`. -pub fn measure(_font: Font, _text: &str, _size: f64) -> f64 { - unimplemented!("R4.4a") +/// `safeText` (`text.ts`): the Cc, Cf, Zl and Zp code points removed, every other code point +/// WinAnsi cannot encode replaced by `?`. U+00AD (Cf) is dropped although WinAnsi has it. +pub fn safe_text(text: &str) -> String { + text.chars() + .filter(|&c| !is_dropped(c)) + .map(|c| if winansi_byte(c).is_some() { c } else { '?' }) + .collect() } -/// `wrapLines(text, font, size, max)`: split on JS `\s+`, each word made safe, greedy join. -pub fn wrap_lines(_text: &str, _font: Font, _size: f64, _max: f64) -> Vec { - unimplemented!("R4.4a") +/// The upper case of one code point through the one-to-one runs, itself where none applies. +fn upper_single(c: char) -> char { + let code_point = u32::from(c); + let runs = afm_data::UPPER_RUNS; + let after = runs.partition_point(|&(start, ..)| start <= code_point); + let Some(&(start, end, step, delta)) = after.checked_sub(1).and_then(|i| runs.get(i)) else { + return c; + }; + let on_step = (code_point - start).checked_rem(step) == Some(0); + if code_point > end || !on_step { + return c; + } + code_point + .checked_add_signed(delta) + .and_then(char::from_u32) + .unwrap_or(c) +} + +/// The one-to-many upper case of `c` (`ß` → `SS`), when it has one. +fn upper_special(c: char) -> Option<&'static str> { + let table = afm_data::UPPER_SPECIAL; + let index = table + .binary_search_by_key(&u32::from(c), |&(cp, _)| cp) + .ok()?; + table.get(index).map(|&(_, upper)| upper) +} + +/// JavaScript `String.prototype.toUpperCase()` (V8, root locale: context-free, so code point +/// by code point) from the O4 tables. +pub fn js_upper(text: &str) -> String { + let mut upper = String::with_capacity(text.len()); + for c in text.chars() { + match upper_special(c) { + Some(many) => upper.push_str(many), + None => upper.push(upper_single(c)), + } + } + upper +} + +/// Whether JavaScript `/\s/` matches `c` (`wrapLines` splits on `\s+`). +pub fn is_js_whitespace(c: char) -> bool { + in_ranges(afm_data::JS_WHITESPACE, u32::from(c)) +} + +/// JavaScript `String.prototype.trim()`. Its set is not Rust's `trim`: U+FEFF is stripped, +/// U+0085 is kept. +pub fn js_trim(text: &str) -> &str { + text.trim_matches(|c: char| in_ranges(afm_data::JS_TRIM, u32::from(c))) +} + +/// The WinAnsi byte a code point prints with; one outside the encoding (pdf-lib would throw, +/// a safe text has none) counts as `?`, what `safeText` makes of it. +fn glyph_byte(c: char) -> u8 { + winansi_byte(c).unwrap_or(QUESTION_BYTE) +} + +/// `widthOfGlyph`: the AFM `WX`, or 250 where the font has none. +fn glyph_width(metrics: &FontMetrics, byte: u8) -> f64 { + match metrics.widths[usize::from(byte)] { + 0 => MISSING_GLYPH_WIDTH, + width => f64::from(width), + } +} + +/// `getXAxisKerningForPair(left, right) || 0`. +fn kerning(metrics: &FontMetrics, left: u8, right: u8) -> f64 { + let pairs = metrics.kerning; + let found = pairs.binary_search_by_key(&(left, right), |&(l, r, _)| (l, r)); + found + .ok() + .and_then(|index| pairs.get(index)) + .map_or(0.0, |&(_, _, amount)| f64::from(amount)) +} + +/// `widthOfTextAtSize(safe, 1000)` of pdf-lib (`StandardFontEmbedder`): the raw sum, over the +/// glyphs of `safe`, of the glyph width plus the kerning with the next glyph, in that order. +pub fn width_units(font: Font, safe: &str) -> f64 { + let metrics = font.metrics(); + let mut bytes = safe.chars().map(glyph_byte).peekable(); + let mut total = 0.0; + while let Some(left) = bytes.next() { + let kern = bytes + .peek() + .map_or(0.0, |&right| kerning(metrics, left, right)); + total += glyph_width(metrics, left) + kern; + } + total +} + +/// `measure(font, text, size)` (`render/text.ts`) = `widthOfTextAtSize(safeText(text), size)`: +/// `units * (size / 1000)`, evaluated in that order as pdf-lib does. +pub fn measure(font: Font, text: &str, size: f64) -> f64 { + width_units(font, &safe_text(text)) * (size / 1000.0) +} + +/// `text.split(/\s+/)`: the pieces between the runs of JS whitespace, with the empty first +/// and last pieces JavaScript keeps (`" a"` → `["", "a"]`, `""` → `[""]`). +fn split_js_whitespace(text: &str) -> Vec<&str> { + let mut words = Vec::new(); + let mut start = 0; + let mut in_run = false; + for (index, c) in text.char_indices() { + if is_js_whitespace(c) { + if !in_run { + words.push(text.get(start..index).unwrap_or_default()); + } + in_run = true; + } else if in_run { + start = index; + in_run = false; + } + } + words.push(if in_run { + "" + } else { + text.get(start..).unwrap_or_default() + }); + words +} + +/// `wrapLines(text, font, size, max)` (`render/text.ts`): split on JS `\s+`, each word made +/// safe, joined greedily with one space while the line measures at most `max`; a word wider +/// than `max` stands alone on its line. Empty words take part as JavaScript has them. +pub fn wrap_lines(text: &str, font: Font, size: f64, max: f64) -> Vec { + let mut lines = Vec::new(); + let mut line = String::new(); + for word in split_js_whitespace(text).into_iter().map(safe_text) { + let test = if line.is_empty() { + word.clone() + } else { + format!("{line} {word}") + }; + if measure(font, &test, size) > max && !line.is_empty() { + lines.push(line); + line = word; + } else { + line = test; + } + } + if !line.is_empty() { + lines.push(line); + } + lines } diff --git a/crates/core/tests/pdf_layout.rs b/crates/core/tests/pdf_layout.rs index f8ff1a5e..872e276b 100644 --- a/crates/core/tests/pdf_layout.rs +++ b/crates/core/tests/pdf_layout.rs @@ -1 +1,345 @@ -//! R4.4b placeholder. +//! R4.4b: the layout against the O1 draw logs (`apps/app/tests/fixtures/goldens/r4/ +//! draw....json`, `apps/app/tests/helpers/r4-draw.ts`): every call +//! of the TypeScript engine, at full precision, in call order, for the 12 Step 6 runs. The +//! export input of each run is the envelope its own R3 PDF carries (`pdf/r3/*.pdf`, read by +//! the R4 reader), the rasters the stubs the goldens used (1×1 PNG without logos, 2×1 PNG +//! with them: the SVG partner logo and every view; the PNG logos decode their own base64). +//! Then the Step 6 text and image goldens (`goldens/step6/{text,images}.*.json`, two +//! decimals) as the derived check the plan lists. +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use common::r4::{repo_root, sha256_hex}; +use faunosteo_core::domain::archive::Locale; +use faunosteo_core::domain::js::parse_json; +use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document}; +use faunosteo_core::pdf::write::draw_log::{DrawLog, DrawOp}; +use faunosteo_core::pdf::write::input::{ExportInput, Rasters, check_input}; +use faunosteo_core::pdf::write::layout::layout; +use serde_json::{Value, json}; + +const REPORTS: [&str; 3] = ["demo", "v2full", "v2unresolved"]; +const LOCALES: [&str; 2] = ["fr", "en"]; +const MODES: [&str; 2] = ["nologos", "logos"]; + +const ONE_PX_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="; +const TWO_BY_ONE_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC"; + +fn base64(text: &str) -> Vec { + let table: Vec = (b'A'..=b'Z') + .chain(b'a'..=b'z') + .chain(b'0'..=b'9') + .chain(*b"+/") + .collect(); + let mut out = Vec::new(); + let (mut acc, mut bits) = (0u32, 0u32); + for c in text.bytes().filter(|c| *c != b'=') { + acc = (acc << 6) | table.iter().position(|t| *t == c).unwrap() as u32; + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + out +} + +fn fixture(path: &str) -> Vec { + std::fs::read(repo_root().join("apps/app/tests/fixtures").join(path)).unwrap() +} + +/// The envelope the R3 PDF of a run carries, as the export input of that run. +fn export_input(report: &str, locale: &str, mode: &str) -> ExportInput { + let pdf = fixture(&format!("pdf/r3/{report}.{locale}.{mode}.pdf")); + let located = read_attached_document(&pdf, ProbeLimits::UNBOUNDED).unwrap(); + let envelope = parse_json(&located.text).unwrap(); + let input = json!({ + "report": envelope["report"], + "sheetSnapshot": envelope["sheetSnapshot"], + "branding": envelope["branding"], + "labels": envelope["labels"], + "locale": envelope["locale"], + "exportedAt": envelope["exportedAt"], + }); + serde_json::from_value(input).unwrap() +} + +/// The stub rasters of the goldens: one PNG for every view and every SVG logo slot. +fn stub_rasters(input: &ExportInput, mode: &str) -> Rasters { + let png = base64(if mode == "logos" { + TWO_BY_ONE_PNG + } else { + ONE_PX_PNG + }); + let mut rasters = Rasters { + header_logo: Some(png.clone()), + footer_logo: Some(png.clone()), + powered_by: Some(png.clone()), + partner: Some(png.clone()), + views: Default::default(), + }; + for observation in &input.report.observations { + for zone in &observation.zones { + rasters + .views + .entry((observation.id.clone(), zone.view_id.clone())) + .or_insert_with(|| png.clone()); + } + } + rasters +} + +fn color(c: &faunosteo_core::pdf::write::draw_log::Rgb) -> Value { + json!({ "type": "RGB", "red": c.red, "green": c.green, "blue": c.blue }) +} + +/// One op in the oracle's shape (without `seq`). +fn op_json(op: &DrawOp, log: &DrawLog) -> Value { + match op { + DrawOp::Text(t) => json!({ "op": "drawText", "text": t.text, "options": { + "x": t.x, "y": t.y, "size": t.size, "font": t.font.as_str(), "color": color(&t.color) } }), + DrawOp::Image(i) => json!({ "op": "drawImage", + "image": { "embedIndex": i.embed, "pngSha256": sha256_hex(&log.embeds[i.embed].png) }, + "options": { "x": i.x, "y": i.y, "width": i.width, "height": i.height } }), + DrawOp::Line(l) => json!({ "op": "drawLine", "options": { + "start": { "x": l.start.x, "y": l.start.y }, "end": { "x": l.end.x, "y": l.end.y }, + "thickness": l.thickness, "color": color(&l.color) } }), + DrawOp::Rectangle(r) => json!({ "op": "drawRectangle", "options": { + "x": r.x, "y": r.y, "width": r.width, "height": r.height, "color": color(&r.color) } }), + } +} + +fn without_seq(op: &Value) -> Value { + let mut copy = op.clone(); + copy.as_object_mut().unwrap().remove("seq"); + copy +} + +/// Objects with their keys sorted, recursively: the oracle's key order is the recorder's. +fn canon(value: &Value) -> Value { + match value { + Value::Object(map) => { + let mut keys: Vec<&String> = map.keys().collect(); + keys.sort(); + Value::Object( + keys.into_iter() + .map(|k| (k.clone(), canon(&map[k]))) + .collect(), + ) + } + Value::Array(items) => Value::Array(items.iter().map(canon).collect()), + other => other.clone(), + } +} + +/// Compares two JSON values in their JavaScript spelling (`4` and `4.0` are one number), +/// key order aside. +fn same(a: &Value, b: &Value) -> bool { + let js = |v: &Value| faunosteo_core::json::to_js_compact(&canon(v)).unwrap(); + js(a) == js(b) +} + +fn compare_run(report: &str, locale: &str, mode: &str, mismatches: &mut Vec) -> usize { + let name = format!("{report}.{locale}.{mode}"); + let oracle: Value = serde_json::from_str( + &std::fs::read_to_string(repo_root().join(format!( + "apps/app/tests/fixtures/goldens/r4/draw.{name}.json" + ))) + .unwrap(), + ) + .unwrap(); + let input = export_input(report, locale, mode); + let rasters = stub_rasters(&input, mode); + let checked = check_input(input).unwrap(); + let drawn = layout(&checked, &rasters).unwrap(); + assert!(drawn.warnings.is_empty(), "{name}: {:?}", drawn.warnings); + let pages = oracle["pages"].as_array().unwrap(); + if pages.len() != drawn.log.pages.len() { + mismatches.push(format!( + "{name}: {} pages, expected {}", + drawn.log.pages.len(), + pages.len() + )); + return 0; + } + let mut ops = 0; + for (p, page) in pages.iter().enumerate() { + let expected = page["ops"].as_array().unwrap(); + let actual = &drawn.log.pages[p].ops; + if expected.len() != actual.len() { + mismatches.push(format!( + "{name} page {p}: {} ops, expected {}", + actual.len(), + expected.len() + )); + } + for (i, (e, a)) in expected.iter().zip(actual).enumerate() { + ops += 1; + let a = op_json(a, &drawn.log); + if !same(&without_seq(e), &a) { + mismatches.push(format!( + "{name} page {p} op {i}\n expected {}\n actual {a}", + without_seq(e) + )); + } + } + } + let embeds: Vec = drawn + .log + .embeds + .iter() + .enumerate() + .map(|(i, e)| json!({ "embedIndex": i, "pngSha256": sha256_hex(&e.png) })) + .collect(); + if !same(&oracle["embeds"], &Value::Array(embeds.clone())) { + mismatches.push(format!( + "{name} embeds\n expected {}\n actual {}", + oracle["embeds"], + Value::Array(embeds) + )); + } + let links: Vec = drawn + .log + .links + .iter() + .map(|l| json!({ "page": l.page, "rect": l.rect, "uri": l.uri })) + .collect(); + if !same(&oracle["links"], &Value::Array(links.clone())) { + mismatches.push(format!( + "{name} links\n expected {}\n actual {}", + oracle["links"], + Value::Array(links) + )); + } + ops +} + +#[test] +fn o1_draw_logs_match_the_typescript_at_full_precision() { + let mut mismatches = Vec::new(); + let mut ops = 0; + for report in REPORTS { + for locale in LOCALES { + for mode in MODES { + ops += compare_run(report, locale, mode, &mut mismatches); + } + } + } + assert!( + mismatches.is_empty(), + "{} mismatches:\n{}", + mismatches.len(), + mismatches + .iter() + .take(12) + .cloned() + .collect::>() + .join("\n") + ); + println!("O1: 12 runs, {ops} ops equal"); +} + +/// Two decimals as the Step 6 recorder rounded (`Math.round(n * 100) / 100`). +fn round2(n: f64) -> f64 { + (n * 100.0).round() / 100.0 +} + +#[test] +fn step6_text_and_image_goldens_derive_from_the_log() { + let goldens = repo_root().join("apps/app/tests/fixtures/goldens/step6"); + let mut checked = 0; + for report in REPORTS { + for locale in LOCALES { + for mode in MODES { + let name = format!("{report}.{locale}.{mode}"); + let input = export_input(report, locale, mode); + let rasters = stub_rasters(&input, mode); + let drawn = layout(&check_input(input).unwrap(), &rasters).unwrap(); + let text: Value = serde_json::from_str( + &std::fs::read_to_string(goldens.join(format!("text.{name}.json"))).unwrap(), + ) + .unwrap(); + let images: Value = serde_json::from_str( + &std::fs::read_to_string(goldens.join(format!("images.{name}.json"))).unwrap(), + ) + .unwrap(); + let (mut texts, mut imgs) = (Vec::new(), Vec::new()); + for (p, op) in drawn.log.calls_in_order() { + { + match op { + DrawOp::Text(t) => texts.push(json!({ "page": p, "text": t.text, "x": round2(t.x), "y": round2(t.y), "size": t.size, "font": t.font.as_str() })), + DrawOp::Image(i) => imgs.push(json!({ "page": p, "x": round2(i.x), "y": round2(i.y), "width": round2(i.width), "height": round2(i.height) })), + _ => {} + } + } + } + assert!(same(&text, &Value::Array(texts)), "{name}: text golden"); + assert!(same(&images, &Value::Array(imgs)), "{name}: images golden"); + checked += 2; + } + } + } + assert_eq!(checked, 24); +} + +#[test] +fn the_checks_refuse_what_export_ts_refused() { + let mut input = export_input("demo", "fr", "nologos"); + input.report.sheet.document_sha256 = "0".repeat(64); + let err = check_input(input).err().unwrap(); + assert_eq!(err.kind.as_str(), "sheet_mismatch"); + let mut input = export_input("demo", "fr", "nologos"); + input.branding = json!({ "siteUrl": "not a url" }); + assert_eq!(check_input(input).err().unwrap().detail, "branding"); + let mut input = export_input("demo", "fr", "nologos"); + input.labels = json!({ "fr": {} }); + assert_eq!(check_input(input).err().unwrap().detail, "labels"); + let mut input = export_input("demo", "fr", "nologos"); + input.sheet_snapshot = json!({ "status": "draft" }); + assert_eq!( + check_input(input).err().unwrap().kind.as_str(), + "snapshot_invalid" + ); + let input = export_input("demo", "fr", "nologos"); + let _ = Locale::Fr; + assert!(check_input(input).is_ok()); +} + +#[test] +fn page_caps_and_missing_rasters_behave_as_export_ts() { + use faunosteo_core::pdf::write::input::{ExportWarning, LogoSlot, MAX_OBSERVATIONS}; + // 501 observations: too_many_pages / observations. + let mut input = export_input("demo", "fr", "nologos"); + let first = input.report.observations[0].clone(); + while input.report.observations.len() <= MAX_OBSERVATIONS { + input.report.observations.push(first.clone()); + } + let err = check_input(input).err().unwrap(); + assert_eq!( + (err.kind.as_str(), err.detail.as_str()), + ("too_many_pages", "observations") + ); + // A missing view raster and an unreadable logo: one warning each, the page still drawn. + let input = export_input("demo", "fr", "logos"); + let mut rasters = stub_rasters(&input, "logos"); + rasters.partner = Some(b"not a png".to_vec()); + let first_key = rasters.views.keys().next().cloned().unwrap(); + rasters.views.remove(&first_key); + let checked = check_input(input).unwrap(); + let drawn = layout(&checked, &rasters).unwrap(); + assert_eq!( + drawn.warnings, + vec![ + ExportWarning::LogoFailed { + slot: LogoSlot::Partner + }, + ExportWarning::ViewRasterFailed { + observation_id: first_key.0, + view_id: first_key.1, + }, + ] + ); + assert_eq!(drawn.log.pages.len(), checked.report.observations.len() + 1); +} diff --git a/crates/core/tests/pdf_text.rs b/crates/core/tests/pdf_text.rs index 980258d6..2095bda5 100644 --- a/crates/core/tests/pdf_text.rs +++ b/crates/core/tests/pdf_text.rs @@ -1 +1,407 @@ -//! R4.4a placeholder. +//! R4.4a: the text and date rules of the PDF writer (`pdf::write::{text, dates}`) against the +//! frozen oracles: O4 `oracles/r4/text.json` (safeText, toUpperCase, JS `\s` and trim, +//! measure, wrapLines), O5 `afm.json` (the generated `afm_data` tables) and O6 `dates.json` +//! (formatDate). Widths compare bit for bit, never with a tolerance. +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use std::collections::HashMap; + +use common::r4::r4_oracle; +use faunosteo_core::domain::archive::Locale; +use faunosteo_core::pdf::write::afm_data::{self, FontMetrics}; +use faunosteo_core::pdf::write::dates::format_date; +use faunosteo_core::pdf::write::text::{ + Font, is_js_whitespace, js_trim, js_upper, measure, safe_text, width_units, wrap_lines, +}; +use serde_json::Value; + +const SURROGATES: std::ops::RangeInclusive = 0xD800..=0xDFFF; +const FONTS: [Font; 3] = [Font::Helvetica, Font::HelveticaBold, Font::TimesRoman]; + +fn text_oracle() -> Value { + r4_oracle("text.json") +} + +fn u32_of(value: &Value) -> u32 { + u32::try_from(value.as_u64().unwrap()).unwrap() +} + +fn ranges_of(section: &Value) -> Vec<(u32, u32)> { + section["ranges"] + .as_array() + .unwrap() + .iter() + .map(|r| (u32_of(&r[0]), u32_of(&r[1]))) + .collect() +} + +fn in_ranges(ranges: &[(u32, u32)], cp: u32) -> bool { + ranges + .iter() + .any(|&(start, end)| (start..=end).contains(&cp)) +} + +/// Every Rust `char`: the scalar values, 0..=0x10FFFF without the surrogates. +fn all_chars() -> impl Iterator { + (0..=0x10_FFFF).filter_map(char::from_u32) +} + +#[test] +fn safe_text_reproduces_every_oracle_range() { + let ranges = text_oracle()["safeText"]["ranges"] + .as_array() + .unwrap() + .clone(); + let (mut checked, mut skipped, mut next) = (0u32, Vec::new(), 0u32); + for row in &ranges { + let (start, end, class) = (u32_of(&row[0]), u32_of(&row[1]), row[2].as_str().unwrap()); + assert_eq!(start, next, "ranges are contiguous"); + next = end + 1; + for cp in start..=end { + let Some(c) = char::from_u32(cp) else { + // A Rust char is never a surrogate: JavaScript saw a lone unit, made it `?`. + assert!(SURROGATES.contains(&cp) && class == "question", "{cp:#X}"); + skipped.push(cp); + continue; + }; + let expected = match class { + "kept" => c.to_string(), + "dropped" => String::new(), + "question" => "?".to_owned(), + other => panic!("class {other}"), + }; + assert_eq!(safe_text(&c.to_string()), expected, "{cp:#X} ({class})"); + checked += 1; + } + } + assert_eq!(next, 0x11_0000, "the ranges cover every code point"); + assert_eq!( + skipped, + SURROGATES.collect::>(), + "only the surrogates are skipped" + ); + println!( + "safeText: {} ranges, {checked} code points checked, {} surrogates skipped", + ranges.len(), + skipped.len() + ); +} + +#[test] +fn safe_text_works_on_whole_strings() { + assert_eq!(safe_text("a\u{AD}b\u{2028}c\u{1F600}\u{0}é€"), "abc?é€"); + assert_eq!(safe_text("\u{A0}\u{178}\u{FEFF}"), "\u{A0}\u{178}"); + assert_eq!(safe_text(""), ""); +} + +/// O4 `upper`: the expected upper case of every code point that changes. +fn upper_table(upper: &Value) -> HashMap { + let mut table = HashMap::new(); + for run in upper["runs"].as_array().unwrap() { + let (start, end, step) = (u32_of(&run[0]), u32_of(&run[1]), u32_of(&run[2])); + let delta = run[3].as_i64().unwrap(); + for cp in (start..=end).step_by(step as usize) { + let mapped = char::from_u32(u32::try_from(i64::from(cp) + delta).unwrap()).unwrap(); + table.insert(cp, mapped.to_string()); + } + } + for special in upper["special"].as_array().unwrap() { + let result = special[1].as_array().unwrap().iter(); + let text = result + .map(|cp| char::from_u32(u32_of(cp)).unwrap()) + .collect(); + assert!(table.insert(u32_of(&special[0]), text).is_none()); + } + table +} + +#[test] +fn js_upper_is_v8_to_upper_case_for_every_code_point() { + let oracle = text_oracle(); + let table = upper_table(&oracle["upper"]); + let single = oracle["upper"]["singleCount"].as_u64().unwrap() as usize; + let special = oracle["upper"]["special"].as_array().unwrap().len(); + assert_eq!(table.len(), single + special); + let (mut changed, mut diverging) = (0, Vec::new()); + for c in all_chars() { + let expected = table + .get(&u32::from(c)) + .cloned() + .unwrap_or_else(|| c.to_string()); + changed += usize::from(expected != c.to_string()); + let upper = js_upper(&c.to_string()); + assert_eq!(upper, expected, "{:#X}", u32::from(c)); + let rust: String = c.to_uppercase().collect(); + if rust != upper { + diverging.push(format!("U+{:04X} V8 {upper:?} Rust {rust:?}", u32::from(c))); + } + } + assert_eq!(changed, table.len()); + assert_eq!(js_upper("Straße fin ʼn"), "STRASSE FIN ʼN"); + println!( + "upper: {changed} code points change ({single} one-to-one, {special} one-to-many); \ + Rust {:?} to_uppercase differs on {}: {:?}", + char::UNICODE_VERSION, + diverging.len(), + diverging.iter().take(8).collect::>() + ); +} + +#[test] +fn js_whitespace_and_trim_follow_the_oracle_sets() { + let oracle = text_oracle(); + let whitespace = ranges_of(&oracle["jsWhitespace"]); + let trim = ranges_of(&oracle["jsTrim"]); + let (mut spaces, mut trimmed) = (0, 0); + for c in all_chars() { + let cp = u32::from(c); + assert_eq!( + is_js_whitespace(c), + in_ranges(&whitespace, cp), + "\\s {cp:#X}" + ); + spaces += usize::from(in_ranges(&whitespace, cp)); + let padded = format!("{c}{c}x{c}y{c}"); + let expected = if in_ranges(&trim, cp) { + trimmed += 1; + format!("x{c}y") + } else { + padded.clone() + }; + assert_eq!(js_trim(&padded), expected, "trim {cp:#X}"); + } + assert_eq!(js_trim(" \t\u{FEFF}\u{3000}"), ""); + assert_eq!( + js_trim("\u{85}a\u{85}"), + "\u{85}a\u{85}", + "U+0085 is not JS whitespace" + ); + println!("jsWhitespace: {spaces} code points; jsTrim: {trimmed} code points"); +} + +/// The rows of every O4 `measure` group, in file order (the `widthSample` indexing). +fn measure_rows(measure: &Value) -> Vec { + let groups = [ + "sheetNames", + "pdfLabels", + "reports", + "randomWinAnsi", + "randomUnicode", + ]; + let rows: Vec = groups + .iter() + .flat_map(|group| measure["groups"][group].as_array().unwrap().clone()) + .collect(); + for group in groups { + let count = measure["counts"][group].as_u64().unwrap() as usize; + assert_eq!( + measure["groups"][group].as_array().unwrap().len(), + count, + "{group}" + ); + } + rows +} + +fn check_measure_row(row: &Value, sizes: &[f64]) -> usize { + let (text, safe) = (row["text"].as_str().unwrap(), row["safe"].as_str().unwrap()); + assert_eq!(safe_text(text), safe, "safe of {text:?}"); + let mut exact_widths = 0; + for (f, font) in FONTS.into_iter().enumerate() { + let units = row["units"][f].as_f64().unwrap(); + assert_eq!( + width_units(font, safe), + units, + "{} units of {text:?}", + font.as_str() + ); + for (s, &size) in sizes.iter().enumerate() { + let width = measure(font, text, size); + assert_eq!( + width.to_bits(), + (units * (size / 1000.0)).to_bits(), + "{text:?} {size}" + ); + if let Some(recorded) = row["widths"][f][s].as_f64() { + assert_eq!(width.to_bits(), recorded.to_bits(), "{text:?} {f} {size}"); + exact_widths += 1; + } + } + } + exact_widths +} + +#[test] +fn measure_matches_pdf_lib_bit_for_bit() { + let oracle = text_oracle(); + let measure_oracle = &oracle["measure"]; + let fonts: Vec<&str> = FONTS.iter().map(|f| f.as_str()).collect(); + assert_eq!(measure_oracle["fonts"], serde_json::json!(fonts)); + assert_eq!(measure_oracle["widthFormula"], "units * (size / 1000)"); + let sizes: Vec = measure_oracle["sizes"] + .as_array() + .unwrap() + .iter() + .map(|s| s.as_f64().unwrap()) + .collect(); + let rows = measure_rows(measure_oracle); + assert_eq!( + rows.len() as u64, + measure_oracle["widthSample"]["total"].as_u64().unwrap() + ); + let sampled: Vec = measure_oracle["widthSample"]["indices"] + .as_array() + .unwrap() + .iter() + .map(|i| i.as_u64().unwrap() as usize) + .collect(); + let with_widths: Vec = (0..rows.len()) + .filter(|&i| rows[i]["widths"].is_array()) + .collect(); + assert_eq!( + with_widths, sampled, + "widths are kept on the sample rows only" + ); + let exact: usize = rows.iter().map(|row| check_measure_row(row, &sizes)).sum(); + assert_eq!(exact, sampled.len() * FONTS.len() * sizes.len()); + println!( + "measure: {} rows x {} fonts x {} sizes = {} widths, {exact} bit-exact against the \ + recorded widths ({} sample rows)", + rows.len(), + FONTS.len(), + sizes.len(), + rows.len() * FONTS.len() * sizes.len(), + sampled.len() + ); +} + +/// One font of `afm_data` against O5: the WX of every byte's glyph (0 where the byte has +/// none) and every KPX pair between WinAnsi glyphs, by byte pair in sorted order. +fn check_font_table(oracle: &Value, metrics: &FontMetrics, byte_glyph: &HashMap) { + let mut pairs = Vec::new(); + for left in 0..=255u8 { + let glyph = byte_glyph.get(&left); + let wx = glyph.map_or(0, |g| oracle["glyphs"][g][0].as_u64().unwrap()); + assert_eq!( + u64::from(metrics.widths[usize::from(left)]), + wx, + "WX of {left}" + ); + for right in 0..=255u8 { + let amount = glyph + .zip(byte_glyph.get(&right)) + .and_then(|(l, r)| oracle["kerning"][l][r].as_i64()); + if let Some(amount) = amount { + pairs.push((left, right, i16::try_from(amount).unwrap())); + } + } + } + assert_eq!(metrics.kerning, pairs.as_slice()); +} + +#[test] +fn afm_tables_hold_every_glyph_and_pair_of_o5() { + let afm = r4_oracle("afm.json"); + let rows = afm["winAnsi"].as_array().unwrap(); + let byte_of = |row: &Value| u8::try_from(row[1].as_u64().unwrap()).unwrap(); + let mut winansi: Vec<(u32, u8)> = rows.iter().map(|r| (u32_of(&r[0]), byte_of(r))).collect(); + winansi.sort_unstable(); + assert_eq!(afm_data::WINANSI, winansi.as_slice()); + let byte_glyph: HashMap = rows + .iter() + .map(|r| (byte_of(r), r[2].as_str().unwrap())) + .collect(); + let tables = [ + &afm_data::HELVETICA, + &afm_data::HELVETICA_BOLD, + &afm_data::TIMES_ROMAN, + ]; + for (font, metrics) in FONTS.into_iter().zip(tables) { + let oracle = &afm["fonts"][font.as_str()]; + assert_eq!(oracle["winAnsiGlyphsWithoutWidth"], serde_json::json!([])); + check_font_table(oracle, metrics, &byte_glyph); + println!( + "afm {}: {} kerning pairs over WinAnsi", + font.as_str(), + metrics.kerning.len() + ); + } +} + +#[test] +fn wrap_lines_reproduces_every_case() { + let oracle = text_oracle(); + let wrap = &oracle["wrapLines"]; + assert_eq!(wrap["font"], "Helvetica"); + let numbers = |key: &str| -> Vec { + wrap[key] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_f64().unwrap()) + .collect() + }; + let (sizes, max_widths) = (numbers("sizes"), numbers("maxWidths")); + assert_eq!(max_widths[0], 595.28 - 36.0 * 2.0); + let cases = wrap["cases"].as_array().unwrap(); + for case in cases { + let text = case["text"].as_str().unwrap(); + for (s, &size) in sizes.iter().enumerate() { + for (w, &max) in max_widths.iter().enumerate() { + let expected: Vec = + serde_json::from_value(case["lines"][s][w].clone()).unwrap(); + let lines = wrap_lines(text, Font::Helvetica, size, max); + assert_eq!(lines, expected, "{text:?} at {size} pt within {max}"); + } + } + } + assert_eq!( + wrap_lines("SIRET ", Font::Helvetica, 10.0, 1000.0), + ["SIRET "] + ); + println!( + "wrapLines: {} cases x {} sizes x {} widths = {} wraps", + cases.len(), + sizes.len(), + max_widths.len(), + cases.len() * sizes.len() * max_widths.len() + ); +} + +fn check_date(input: &str, fr: &str, en: &str) { + assert_eq!(format_date(input, Locale::Fr), fr, "fr {input:?}"); + assert_eq!(format_date(input, Locale::En), en, "en {input:?}"); +} + +#[test] +fn format_date_reproduces_the_intl_grid_and_the_others() { + let oracle = r4_oracle("dates.json"); + let grid = &oracle["grid"]; + let list = |key: &str| -> Vec { serde_json::from_value(grid[key].clone()).unwrap() }; + let (years, months, days) = (list("years"), list("months"), list("days")); + let (mut cells, mut refused) = (0, 0); + for year in &years { + for month in &months { + let results = &grid["results"][format!("{year}-{month}")]; + for (d, day) in days.iter().enumerate() { + let input = format!("{year}-{month}-{day}"); + let fr = results["fr"][d].as_str().unwrap(); + check_date(&input, fr, results["en"][d].as_str().unwrap()); + cells += 1; + refused += usize::from(fr == input); + } + } + } + let others = oracle["others"].as_array().unwrap(); + for row in others { + let [input, fr, en] = [0, 1, 2].map(|i| row[i].as_str().unwrap()); + check_date(input, fr, en); + } + check_date("2026-01-05", "5 janvier 2026", "January 5, 2026"); + println!( + "dates: {cells} grid cells ({refused} refused) x 2 locales, {} others", + others.len() + ); +} diff --git a/packages/core-wasm/types/ExportErrorKind.ts b/packages/core-wasm/types/ExportErrorKind.ts new file mode 100644 index 00000000..9ac6d592 --- /dev/null +++ b/packages/core-wasm/types/ExportErrorKind.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type ExportErrorKind = "snapshot_invalid" | "snapshot_too_large" | "sheet_mismatch" | "too_many_pages" | "branding_invalid"; diff --git a/packages/core-wasm/types/ExportInput.ts b/packages/core-wasm/types/ExportInput.ts new file mode 100644 index 00000000..f611a79e --- /dev/null +++ b/packages/core-wasm/types/ExportInput.ts @@ -0,0 +1,10 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Locale } from "./Locale"; +import type { Report } from "./Report"; + +/** + * What the application hands the export (plan §B.1.3), as JSON: the report, the snapshot + * of the sheet it was written on, the branding, the labels, the locale and the export time + * (an ISO string the caller took from its clock; the core reads no clock). + */ +export type ExportInput = { report: Report, sheetSnapshot: unknown, branding: unknown, labels: unknown, locale: Locale, exportedAt: string, }; diff --git a/packages/core-wasm/types/ExportWarning.ts b/packages/core-wasm/types/ExportWarning.ts new file mode 100644 index 00000000..e902ae19 --- /dev/null +++ b/packages/core-wasm/types/ExportWarning.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { LogoSlot } from "./LogoSlot"; + +/** + * The export's warnings, as today (`ExportWarning`). + */ +export type ExportWarning = { "kind": "view_raster_failed", observationId: string, viewId: string, } | { "kind": "logo_failed", slot: LogoSlot, }; diff --git a/packages/core-wasm/types/LogoSlot.ts b/packages/core-wasm/types/LogoSlot.ts new file mode 100644 index 00000000..5f63ed7d --- /dev/null +++ b/packages/core-wasm/types/LogoSlot.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type LogoSlot = "headerLogo" | "footerLogo" | "poweredBy" | "partner"; diff --git a/packages/core-wasm/types/index.ts b/packages/core-wasm/types/index.ts index 9930e651..91b84196 100644 --- a/packages/core-wasm/types/index.ts +++ b/packages/core-wasm/types/index.ts @@ -20,6 +20,9 @@ export type { DetailTriggerAnnotation } from './DetailTriggerAnnotation' export type { DetailView } from './DetailView' export type { DraftRestore } from './DraftRestore' export type { DraftRestoreInput } from './DraftRestoreInput' +export type { ExportErrorKind } from './ExportErrorKind' +export type { ExportInput } from './ExportInput' +export type { ExportWarning } from './ExportWarning' export type { Finding } from './Finding' export type { FindingCategory } from './FindingCategory' export type { FindingCategoryKind } from './FindingCategoryKind' @@ -34,6 +37,7 @@ export type { Laterality } from './Laterality' export type { Locale } from './Locale' export type { LocalizedOptionalText } from './LocalizedOptionalText' export type { LocalizedText } from './LocalizedText' +export type { LogoSlot } from './LogoSlot' export type { MainView } from './MainView' export type { MigrationOutcome } from './MigrationOutcome' export type { MoveDirection } from './MoveDirection' diff --git a/scripts/r4-afm-data.mjs b/scripts/r4-afm-data.mjs new file mode 100644 index 00000000..3e949211 --- /dev/null +++ b/scripts/r4-afm-data.mjs @@ -0,0 +1,253 @@ +// R4.4a generator of crates/core/src/pdf/write/afm_data.rs (plan 2026-10-17 +// §B.1.3 "Text rules"): the metrics pdf-lib 1.17.1 measures the three standard +// fonts with, over WinAnsi, from the frozen O5 oracle (afm.json), and the +// Unicode tables of the text rules as V8 had them, from the frozen O4 oracle +// (text.json): the safeText dropped classes, toUpperCase, JS \s and trim. +// Every table is sorted for a binary search. Deterministic: rerunning on the +// same oracles writes the same file (the header names their sha256 and the +// commit that last touched each). The Rust suite crates/core/tests/pdf_text.rs +// checks the tables back against the oracles. +// Usage: node scripts/r4-afm-data.mjs +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { readFileSync, writeFileSync } from 'node:fs' +import { join, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' + +const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url))) +const ORACLES = 'crates/core/tests/oracles/r4' +const OUT = join(ROOT, 'crates/core/src/pdf/write/afm_data.rs') +const FONTS = [ + ['Helvetica', 'HELVETICA'], + ['Helvetica-Bold', 'HELVETICA_BOLD'], + ['Times-Roman', 'TIMES_ROMAN'], +] +const MAX_LINE = 100 +const INDENT = ' ' + +function readOracle(name) { + const path = `${ORACLES}/${name}` + const text = readFileSync(join(ROOT, path), 'utf8') + const sha256 = createHash('sha256').update(text).digest('hex') + const commit = execFileSync('git', ['log', '-1', '--format=%h', '--', path], { + cwd: ROOT, + encoding: 'utf8', + }).trim() + return { path, data: JSON.parse(text), sha256, commit: commit || 'none' } +} + +function fail(message) { + throw new Error(`r4-afm-data: ${message}`) +} + +function hex(codePoint) { + return `0x${codePoint.toString(16).toUpperCase().padStart(4, '0')}` +} + +/** Items joined by ", " and packed greedily into lines of at most MAX_LINE. */ +function pack(items, indent) { + const lines = [] + let line = '' + for (const item of items) { + const next = line ? `${line} ${item},` : `${indent}${item},` + if (next.length > MAX_LINE && line) { + lines.push(line) + line = `${indent}${item},` + } else { + line = next + } + } + if (line) lines.push(line) + return lines.join('\n') +} + +function byteGlyphs(winAnsi) { + const glyphs = new Map() + for (const [, byte, glyph] of winAnsi) { + const known = glyphs.get(byte) + if (known !== undefined && known !== glyph) { + fail(`byte ${byte} prints ${known} and ${glyph}`) + } + glyphs.set(byte, glyph) + } + return glyphs +} + +function widthsOf(font, glyphs) { + const widths = new Array(256).fill(0) + for (const [byte, glyph] of glyphs) { + const metric = font.glyphs[glyph] + if (metric !== undefined) widths[byte] = metric[0] + } + return widths +} + +function kerningOf(font, glyphs) { + const pairs = [] + for (const [left, leftGlyph] of glyphs) { + const row = font.kerning[leftGlyph] ?? {} + for (const [right, rightGlyph] of glyphs) { + const amount = row[rightGlyph] + if (amount !== undefined) pairs.push([left, right, amount]) + } + } + return pairs.sort((a, b) => a[0] - b[0] || a[1] - b[1]) +} + +function fontStatic(name, constName, font, glyphs) { + const widths = widthsOf(font, glyphs) + const kerning = kerningOf(font, glyphs) + const tuples = kerning.map(([l, r, k]) => `(${l}, ${r}, ${k})`) + return [ + `/// ${name}: ${kerning.length} kerning pairs between WinAnsi glyphs.`, + '#[rustfmt::skip]', + `pub static ${constName}: FontMetrics = FontMetrics {`, + `${INDENT}widths: [`, + pack(widths.map(String), INDENT.repeat(2)), + `${INDENT}],`, + `${INDENT}kerning: &[`, + pack(tuples, INDENT.repeat(2)), + `${INDENT}],`, + '};', + ].join('\n') +} + +function rangesOf(rows, keep) { + return rows.filter(keep).map(([start, end]) => `(${hex(start)}, ${hex(end)})`) +} + +function sliceStatic(doc, name, type, items) { + return [ + ...doc.map((line) => `/// ${line}`), + '#[rustfmt::skip]', + `pub static ${name}: &[${type}] = &[`, + pack(items, INDENT), + '];', + ].join('\n') +} + +function checkSafeText(ranges, winAnsi) { + const encodable = new Set(winAnsi.map(([codePoint]) => codePoint)) + for (const [start, end, kind] of ranges) { + for (let cp = start; cp <= end; cp += 1) { + const surrogate = cp >= 0xd800 && cp <= 0xdfff + const expected = encodable.has(cp) && !surrogate ? 'kept' : 'question' + if (kind !== 'dropped' && kind !== expected) { + fail(`safeText ${hex(cp)} is ${kind}, WinAnsi says ${expected}`) + } + } + } +} + +function upperSpecial([codePoint, result]) { + const escaped = result + .map((cp) => `\\u{${cp.toString(16).toUpperCase()}}`) + .join('') + return `(${hex(codePoint)}, "${escaped}")` +} + +function header(afm, text) { + const env = text.data.environment + return [ + `//! Generated by \`scripts/r4-afm-data.mjs\`: do not edit, rerun the generator.`, + '//!', + '//! The AFM metrics of Helvetica, Helvetica-Bold and Times-Roman over WinAnsi as pdf-lib', + '//! 1.17.1 measures them (@pdf-lib/standard-fonts 1.0.0), and the Unicode tables of the', + `//! text rules as V8 ${env.v8} had them (Node ${env.node}, ICU ${env.icu},`, + `//! Unicode ${env.unicode}). Sources, the frozen oracles:`, + `//! - O5 \`${afm.path}\` (commit ${afm.commit})`, + `//! sha256 ${afm.sha256}`, + `//! - O4 \`${text.path}\` (commit ${text.commit})`, + `//! sha256 ${text.sha256}`, + ].join('\n') +} + +const FONT_METRICS = `/// The metrics one standard font is measured with, by WinAnsi byte. +pub struct FontMetrics { + /// AFM \`WX\` of the glyph each byte prints; 0 where the byte prints no glyph (the + /// width lookup then falls back to 250, pdf-lib's \`|| 250\`). + pub widths: [u16; 256], + /// AFM \`KPX\` (left byte, right byte, amount) between WinAnsi glyphs, sorted by pair. + pub kerning: &'static [(u8, u8, i16)], +}` + +function checkRuns(runs) { + runs.forEach(([start, end], index) => { + const previous = runs[index - 1] + if (end < start || (previous && previous[1] >= start)) { + fail(`upper run ${hex(start)} unsorted or overlapping`) + } + }) +} + +function unicodeTables(text, winAnsi) { + const { safeText, upper, jsWhitespace, jsTrim } = text + checkSafeText(safeText.ranges, winAnsi) + checkRuns(upper.runs) + const sortedWinAnsi = [...winAnsi].sort((a, b) => a[0] - b[0]) + return [ + sliceStatic( + [ + '`Encodings.WinAnsi`: (code point, byte) for every code point it encodes, sorted.', + ], + 'WINANSI', + '(u32, u8)', + sortedWinAnsi.map(([cp, byte]) => `(${hex(cp)}, ${byte})`), + ), + sliceStatic( + [ + 'safeText `[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}]`: the dropped code point ranges, inclusive.', + ], + 'SAFE_TEXT_DROPPED', + '(u32, u32)', + rangesOf(safeText.ranges, ([, , kind]) => kind === 'dropped'), + ), + sliceStatic( + [ + '`toUpperCase` one-to-one: (start, end, step, delta), start + k * step <= end maps to', + 'itself + delta. Sorted by start, disjoint.', + ], + 'UPPER_RUNS', + '(u32, u32, u32, i32)', + upper.runs.map( + ([s, e, step, d]) => `(${hex(s)}, ${hex(e)}, ${step}, ${d})`, + ), + ), + sliceStatic( + ['`toUpperCase` one-to-many: (code point, result), sorted.'], + 'UPPER_SPECIAL', + '(u32, &str)', + [...upper.special].sort((a, b) => a[0] - b[0]).map(upperSpecial), + ), + sliceStatic( + ['JS `\\s`: the matched code point ranges, inclusive.'], + 'JS_WHITESPACE', + '(u32, u32)', + rangesOf(jsWhitespace.ranges, () => true), + ), + sliceStatic( + ['`String.prototype.trim`: the stripped code point ranges, inclusive.'], + 'JS_TRIM', + '(u32, u32)', + rangesOf(jsTrim.ranges, () => true), + ), + ] +} + +function generate() { + const afm = readOracle('afm.json') + const text = readOracle('text.json') + const { winAnsi, fonts } = afm.data + const glyphs = byteGlyphs(winAnsi) + const parts = [ + header(afm, text), + FONT_METRICS, + ...FONTS.map(([name, constName]) => + fontStatic(name, constName, fonts[name], glyphs), + ), + ...unicodeTables(text.data, winAnsi), + ] + writeFileSync(OUT, `${parts.join('\n\n')}\n`) +} + +generate()