boko 0.4.0

Fast native ebook converter for EPUB, KFX, AZW3, and MOBI — the only KFX writer that needs no Kindle Previewer
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! ToCss implementation for ComputedStyle.
//!
//! A single canonical table ([`PROPERTIES`]) is the source of truth for how
//! each `ComputedStyle` property is compared against its default and
//! serialized to CSS. Both `ComputedStyle::to_css` (the CSS declaration blob)
//! and the KFX exporter's per-field extraction
//! (`crate::kfx::style_schema::extract_ir_field`) are driven by this table,
//! so a new `ComputedStyle` field normally needs exactly one entry here.

use std::fmt::Write;

use super::ToCss;
use super::types::ComputedStyle;

/// Serializer for one property: writes the CSS value into `out` and returns
/// `true` when the property differs from its default (`false` writes nothing).
type EmitFn = fn(&ComputedStyle, &ComputedStyle, &mut String) -> bool;

/// One CSS property of `ComputedStyle` in the canonical table.
struct CssProperty {
    /// Canonical CSS property name.
    name: &'static str,
    /// Whether `ComputedStyle::to_css` emits this property in the CSS blob.
    ///
    /// Historically `to_css` never emitted some properties that the KFX
    /// exporter extracts (min-width, max-height, clear, orphans, widows,
    /// word-break, border-collapse, border-spacing). That asymmetry is
    /// preserved: those entries are lookup-only via
    /// [`changed_property_value`].
    in_blob: bool,
    /// How to serialize the value when it differs from the default.
    emit: EmitFn,
}

/// Table entry: emit the field's CSS value when it differs from the default.
macro_rules! prop {
    ($name:expr, $field:ident) => {
        prop!($name, $field, in_blob: true)
    };
    ($name:expr, $field:ident, in_blob: $in_blob:expr) => {
        CssProperty {
            name: $name,
            in_blob: $in_blob,
            emit: |s, d, out| {
                if s.$field == d.$field {
                    false
                } else {
                    s.$field.to_css(out);
                    true
                }
            },
        }
    };
}

/// Table entry: emit an optional color field when it is `Some`.
macro_rules! color_prop {
    ($name:expr, $field:ident) => {
        CssProperty {
            name: $name,
            in_blob: true,
            emit: |s, _d, out| match s.$field {
                Some(color) => {
                    color.to_css(out);
                    true
                }
                None => false,
            },
        }
    };
}

/// Table entry (lookup-only): emit an integer count field when it differs
/// from the default.
macro_rules! count_prop {
    ($name:expr, $field:ident) => {
        CssProperty {
            name: $name,
            in_blob: false,
            emit: |s, d, out| {
                if s.$field == d.$field {
                    false
                } else {
                    write!(out, "{}", s.$field).unwrap();
                    true
                }
            },
        }
    };
}

/// Canonical property table, in `to_css` emission order.
///
/// Blob entries (`in_blob: true`) are emitted by `ComputedStyle::to_css` in
/// this exact order; lookup-only entries at the end exist solely for
/// [`changed_property_value`] (used by the KFX exporter).
const PROPERTIES: &[CssProperty] = &[
    // Font properties.
    CssProperty {
        name: "font-family",
        in_blob: true,
        // The blob quotes family names that need it; the KFX exporter
        // intentionally uses the raw family string instead and does NOT
        // consult this entry (see extract_ir_field).
        emit: |s, _d, out| match &s.font_family {
            Some(family) => {
                quote_font_family(out, family);
                true
            }
            None => false,
        },
    },
    prop!("font-size", font_size),
    prop!("font-weight", font_weight),
    prop!("font-style", font_style),
    // Colors.
    color_prop!("color", color),
    color_prop!("background-color", background_color),
    // Text properties.
    prop!("text-align", text_align),
    prop!("text-indent", text_indent),
    prop!("line-height", line_height),
    // Combined underline/line-through value. The KFX exporter needs the two
    // flags separately and handles them itself (see extract_ir_field).
    CssProperty {
        name: "text-decoration",
        in_blob: true,
        emit: |s, _d, out| {
            match (s.text_decoration_underline, s.text_decoration_line_through) {
                (true, true) => out.push_str("underline line-through"),
                (true, false) => out.push_str("underline"),
                (false, true) => out.push_str("line-through"),
                (false, false) => return false,
            }
            true
        },
    },
    // Display.
    prop!("display", display),
    // Margins (4-sided).
    prop!("margin-top", margin_top),
    prop!("margin-right", margin_right),
    prop!("margin-bottom", margin_bottom),
    prop!("margin-left", margin_left),
    // Padding (4-sided).
    prop!("padding-top", padding_top),
    prop!("padding-right", padding_right),
    prop!("padding-bottom", padding_bottom),
    prop!("padding-left", padding_left),
    // Vertical alignment.
    prop!("vertical-align", vertical_align),
    // List style. The blob emits it whenever non-default; the KFX exporter
    // additionally gates it on display: list-item (see extract_ir_field).
    prop!("list-style-type", list_style_type),
    // Font variant.
    prop!("font-variant", font_variant),
    // Text spacing.
    prop!("letter-spacing", letter_spacing),
    prop!("word-spacing", word_spacing),
    // Text transform.
    prop!("text-transform", text_transform),
    // Hyphenation.
    prop!("hyphens", hyphens),
    // White-space.
    prop!("white-space", white_space),
    // Underline style.
    prop!("text-decoration-style", underline_style),
    // Overline flag. The KFX exporter maps this flag to a decoration style
    // ("solid") rather than a line value, so it does not consult this entry
    // (see extract_ir_field).
    CssProperty {
        name: "text-decoration-line",
        in_blob: true,
        emit: |s, _d, out| {
            if s.overline {
                out.push_str("overline");
                true
            } else {
                false
            }
        },
    },
    // Underline color.
    color_prop!("text-decoration-color", underline_color),
    // Layout dimensions.
    prop!("width", width),
    prop!("height", height),
    prop!("max-width", max_width),
    prop!("min-height", min_height),
    // Float.
    prop!("float", float),
    // Page breaks.
    prop!("break-before", break_before),
    prop!("break-after", break_after),
    prop!("break-inside", break_inside),
    // Border styles (4-sided).
    prop!("border-style-top", border_style_top),
    prop!("border-style-right", border_style_right),
    prop!("border-style-bottom", border_style_bottom),
    prop!("border-style-left", border_style_left),
    // Border widths (4-sided).
    prop!("border-width-top", border_width_top),
    prop!("border-width-right", border_width_right),
    prop!("border-width-bottom", border_width_bottom),
    prop!("border-width-left", border_width_left),
    // Border colors (4-sided optional).
    color_prop!("border-top-color", border_color_top),
    color_prop!("border-right-color", border_color_right),
    color_prop!("border-bottom-color", border_color_bottom),
    color_prop!("border-left-color", border_color_left),
    // Border radius (4-corner).
    prop!("border-top-left-radius", border_radius_top_left),
    prop!("border-top-right-radius", border_radius_top_right),
    prop!("border-bottom-left-radius", border_radius_bottom_left),
    prop!("border-bottom-right-radius", border_radius_bottom_right),
    // List style position (same display gating note as list-style-type).
    prop!("list-style-position", list_style_position),
    // Visibility.
    prop!("visibility", visibility),
    // Note: language is stored but typically output via HTML lang attribute.
    //
    // Lookup-only entries below: never emitted in the CSS blob, used by the
    // KFX exporter via changed_property_value.
    prop!("min-width", min_width, in_blob: false),
    prop!("max-height", max_height, in_blob: false),
    prop!("clear", clear, in_blob: false),
    count_prop!("orphans", orphans),
    count_prop!("widows", widows),
    prop!("word-break", word_break, in_blob: false),
    prop!("border-collapse", border_collapse, in_blob: false),
    prop!("border-spacing", border_spacing, in_blob: false),
];

/// The default style every `emit` fn compares against. Built once: this is
/// consulted per property per style on the KFX export hot path, and
/// constructing the ~320-byte struct per lookup was measurable.
static DEFAULT_STYLE: std::sync::LazyLock<ComputedStyle> =
    std::sync::LazyLock::new(ComputedStyle::default);

/// Property-name → table-entry index, so per-name lookups don't linear-scan
/// the table's ~70 entries on the KFX export hot path.
static PROPERTY_INDEX: std::sync::LazyLock<rustc_hash::FxHashMap<&'static str, usize>> =
    std::sync::LazyLock::new(|| {
        PROPERTIES
            .iter()
            .enumerate()
            .map(|(i, p)| (p.name, i))
            .collect()
    });

/// Visit every property that `ComputedStyle::to_css` emits — i.e. every blob
/// property differing from its default — in emission order, as
/// (css-property-name, css-value) pairs.
pub fn for_each_changed_property(style: &ComputedStyle, f: &mut dyn FnMut(&str, &str)) {
    let default = &*DEFAULT_STYLE;
    let mut value = String::new();
    for prop in PROPERTIES {
        if !prop.in_blob {
            continue;
        }
        value.clear();
        if (prop.emit)(style, default, &mut value) {
            f(prop.name, &value);
        }
    }
}

/// Look up one property in the canonical table: `Some(css_value)` when the
/// property differs from its default, `None` otherwise.
///
/// # Panics
///
/// Panics if `name` is not in the canonical table. Callers pass compile-time
/// constant names, so an unknown name is a bug (drift between the KFX schema
/// and the table) that must not be silently ignored.
pub fn changed_property_value(style: &ComputedStyle, name: &str) -> Option<String> {
    let idx = *PROPERTY_INDEX
        .get(name)
        .unwrap_or_else(|| panic!("unknown CSS property name: {name}"));
    let prop = &PROPERTIES[idx];
    let mut value = String::new();
    (prop.emit)(style, &DEFAULT_STYLE, &mut value).then_some(value)
}

impl ToCss for ComputedStyle {
    fn to_css(&self, buf: &mut String) {
        for_each_changed_property(self, &mut |name, value| {
            buf.push_str(name);
            buf.push_str(": ");
            buf.push_str(value);
            buf.push_str("; ");
        });
    }
}

/// CSS generic font families that must NOT be quoted.
const GENERIC_FAMILIES: &[&str] = &[
    "serif",
    "sans-serif",
    "monospace",
    "cursive",
    "fantasy",
    "system-ui",
    "ui-serif",
    "ui-sans-serif",
    "ui-monospace",
    "ui-rounded",
    "math",
    "emoji",
    "fangsong",
];

/// Quote font-family names that need quoting in CSS.
///
/// A comma-separated font stack like `din next lt pro,sans-serif` becomes
/// `"din next lt pro",sans-serif` — generic families are left unquoted,
/// custom names with spaces or leading digits are quoted.
fn quote_font_family(buf: &mut String, family: &str) {
    for (i, part) in family.split(',').enumerate() {
        if i > 0 {
            buf.push(',');
        }
        let trimmed = part.trim();
        let is_generic = GENERIC_FAMILIES
            .iter()
            .any(|g| g.eq_ignore_ascii_case(trimmed));
        let needs_quoting = !is_generic
            && (trimmed.contains(' ')
                || trimmed.starts_with(|c: char| c.is_ascii_digit())
                || trimmed.contains('"')
                || trimmed.is_empty());
        if needs_quoting {
            buf.push('"');
            buf.push_str(trimmed);
            buf.push('"');
        } else {
            buf.push_str(trimmed);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn quoted(input: &str) -> String {
        let mut buf = String::new();
        quote_font_family(&mut buf, input);
        buf
    }

    #[test]
    fn test_font_family_quoting_spaces_and_digit_prefix() {
        // Real-world case from B003ZK58TA: unquoted name with spaces + leading digit
        assert_eq!(
            quoted("001_cvi_cover-din next lt pro,sans-serif"),
            r#""001_cvi_cover-din next lt pro",sans-serif"#
        );
    }

    #[test]
    fn test_font_family_quoting_spaces() {
        assert_eq!(
            quoted("DIN Next LT Pro,sans-serif"),
            r#""DIN Next LT Pro",sans-serif"#
        );
    }

    #[test]
    fn test_font_family_no_quoting_single_word() {
        assert_eq!(quoted("Helvetica"), "Helvetica");
    }

    #[test]
    fn test_font_family_generic_not_quoted() {
        assert_eq!(quoted("serif"), "serif");
        assert_eq!(quoted("sans-serif"), "sans-serif");
        assert_eq!(quoted("monospace"), "monospace");
    }

    #[test]
    fn test_font_family_generic_case_insensitive() {
        assert_eq!(quoted("Sans-Serif"), "Sans-Serif");
    }

    #[test]
    fn test_font_family_full_stack() {
        assert_eq!(
            quoted("031_next-reads-shift light,palatino,palatino linotype,georgia,serif"),
            r#""031_next-reads-shift light",palatino,"palatino linotype",georgia,serif"#
        );
    }

    #[test]
    fn test_font_family_leading_digit() {
        assert_eq!(quoted("123font"), r#""123font""#);
    }

    #[test]
    fn test_computed_style_font_family_quoted() {
        let style = ComputedStyle {
            font_family: Some("001_cvi_cover-din next lt pro,sans-serif".to_string()),
            ..Default::default()
        };
        let mut css = String::new();
        style.to_css(&mut css);
        assert!(
            css.contains(r#"font-family: "001_cvi_cover-din next lt pro",sans-serif;"#),
            "Expected quoted font-family in CSS output, got: {}",
            css
        );
    }

    #[test]
    fn test_property_names_unique() {
        for (i, a) in PROPERTIES.iter().enumerate() {
            for b in &PROPERTIES[i + 1..] {
                assert_ne!(a.name, b.name, "duplicate property name in table");
            }
        }
    }

    #[test]
    fn test_changed_property_value_matches_blob() {
        let style = ComputedStyle {
            font_weight: crate::style::FontWeight::BOLD,
            ..Default::default()
        };
        assert_eq!(
            changed_property_value(&style, "font-weight"),
            Some("bold".to_string())
        );
        assert_eq!(changed_property_value(&style, "font-style"), None);
    }

    #[test]
    #[should_panic(expected = "unknown CSS property name")]
    fn test_changed_property_value_unknown_name_panics() {
        changed_property_value(&ComputedStyle::default(), "not-a-property");
    }
}