1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! # Boxworks text preprocessor
//!
//! This crate implements the logic that converts text (words and spaces)
//! into horizontal list elements.
//! It is implemented in the Chief Executive chapter in Knuth's
//! TeX (starting in TeX.2021.1029).

use std::rc::Rc;

use boxworks::ds;
use tfm::ligkern;

#[derive(Debug)]
pub struct Font {
    pub default_space: core::Glue,
    pub lig_kern_program: tfm::ligkern::CompiledProgram,
}

#[derive(Default)]
pub struct TextPreprocessorImpl {
    fonts: Vec<Font>,
    // TODO: should be initialized to the null font
    // TODO: should current_font be some kind of specific font identifier type.
    current_font: u32,
}

impl boxworks::TextPreprocessor for TextPreprocessorImpl {
    fn add_text(&mut self, text: &str, list: &mut Vec<ds::Horizontal>) {
        let font = &self.fonts[self.current_font as usize];

        struct Emitter<'a>(&'a mut Vec<ds::Horizontal>, u32);
        impl<'a> ligkern::Emitter for Emitter<'a> {
            fn emit_character(&mut self, c: char) {
                self.0.push(ds::Horizontal::Char(ds::Char {
                    char: c,
                    font: self.1,
                }));
            }
            fn emit_kern(&mut self, kern: core::Scaled) {
                self.0.push(ds::Horizontal::Kern(ds::Kern {
                    width: kern,
                    kind: ds::KernKind::Normal,
                }));
            }
            fn emit_ligature(&mut self, c: char, original: Rc<str>) {
                self.0.push(ds::Horizontal::Ligature(ds::Ligature {
                    included_left_boundary: false,
                    included_right_boundary: false,
                    char: c,
                    font: self.1,
                    original_chars: original,
                }));
            }
        }

        let mut e = Emitter(list, self.current_font);
        font.lig_kern_program.run(text, &mut e);
    }

    fn add_space(&mut self, list: &mut Vec<ds::Horizontal>) {
        // TeX.2021.1041
        // TODO: space factor, etc.
        list.push(ds::Horizontal::Glue(
            self.fonts[self.current_font as usize].default_space.into(),
        ));
    }
}

impl TextPreprocessorImpl {
    pub fn register_font(&mut self, id: u32, font: Font) {
        assert_eq!(id as usize, self.fonts.len());
        self.fonts.push(font);
    }
    pub fn activate_font(&mut self, id: u32) {
        self.current_font = id;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use boxworks::TextPreprocessor;
    use boxworks_lang as bwl;

    macro_rules! preprocessor_tests {
        ( $( ( $name: ident, $input: expr, $want: expr, ), )+ ) => {
            $(
                #[test]
                fn $name() {
                    let input = $input;
                    let want = $want;
                    run_preprocessor_test(input, want)
                }
            )+
        };
    }

    preprocessor_tests!(
        (
            cmr10_basic,
            "second",
            r#"
                text("second", font=0)
            "#,
        ),
        (
            cmr10_basic_with_space,
            "sec ond",
            r#"
                text("sec", font=0)
                glue(3.33333pt, 1.66666pt, 1.11111pt)
                text("ond", font=0)
            "#,
        ),
        (
            cmr10_kern_ao,
            "AO",
            r#"
                text("A", font=0)
                kern(-0.27779pt)
                text("O", font=0)
            "#,
        ),
        (
            cmr10_kern_av,
            "AV",
            r#"
                text("A", font=0)
                kern(-1.11113pt)
                text("V", font=0)
            "#,
        ),
        (
            cmr10_ligature_1,
            "ff",
            r#"
                lig("\u{b}", "ff", font=0)
            "#,
        ),
        (
            cmr10_ligature_2,
            "ffi",
            r#"
                lig("\u{e}", "ffi", font=0)
            "#,
        ),
    );

    fn run_preprocessor_test(input: &str, want: &str) {
        let mut tfm_file = {
            let raw = include_bytes!("../../tfm/corpus/computer-modern/cmr10.tfm");
            tfm::File::deserialize(raw).0.unwrap()
        };

        let want = bwl::parse_horizontal_list(want).unwrap();

        let mut tp: TextPreprocessorImpl = Default::default();
        tp.register_font(
            0,
            Font {
                default_space: core::Glue {
                    // TODO: read these from the tfm file params
                    // We need to convert FixWord to Scaled
                    // and then adjust by the design size...
                    // So the following line is not good enough - it's off
                    // by the design size of ~10
                    // width: tfm_file.params[1].to_scaled(),
                    width: core::Scaled::ONE * 10 / 3,
                    stretch: core::Scaled::ONE * 5 / 3,
                    stretch_order: core::GlueOrder::Normal,
                    shrink: core::Scaled::ONE * 10 / 9 + core::Scaled(1),
                    shrink_order: core::GlueOrder::Normal,
                },
                lig_kern_program: tfm::ligkern::CompiledProgram::compile_from_tfm_file(
                    &mut tfm_file,
                )
                .0,
            },
        );
        tp.activate_font(0);
        let mut got = vec![];
        for word in input.split_inclusive(' ') {
            tp.add_text(word.trim_matches(' '), &mut got);
            if word.ends_with(" ") {
                tp.add_space(&mut got);
            }
        }

        assert_eq!(got, want);
    }
}