kakemono 0.23.0

Convert PDF, Word, Excel, PowerPoint and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! The text inside a figure as editable runs (`figure_lines` of the JSON):
//! a label, or the lines of one paragraph inside a box of the figure.
//!
//! A figure keeps the lines of text drawn inside it ([`Block::inner_lines`])
//! with their styled text ([`Block::inner_spans`]). They are grouped into
//! runs by geometry: lines one under the other, at a regular pitch, of the
//! same size and aligned on the left or on the centre. An edited run is
//! applied by drawing the figure again ([`figure_svg`]): the page region
//! without the glyphs of the edited runs, and their new text in their box,
//! broken into lines that fit its width. The result replaces the figure's
//! picture, as a `replace_image` does.

use super::text_of;
use crate::backend::PageRenderer;
use crate::model::{BOLD, Block, BlockKind, ITALIC, Line, MONO, RotatedRun, Span};
use crate::render::assets::origin_key;
use crate::render::escape::esc;
use std::collections::HashSet;

/// Largest distance between the baselines of two lines of one run, in
/// multiples of their font size (ems): a wider gap starts another run.
const RUN_PITCH_MAX_EM: f32 = 1.8;
/// Largest difference between the font sizes of two lines of one run, as a
/// share of the larger.
const RUN_SIZE_TOLERANCE_RATIO: f32 = 0.15;
/// Largest offset of the left edges (or of the centres) of two lines of one
/// run, in ems.
const RUN_ALIGN_TOLERANCE_EM: f32 = 1.0;
/// Smallest font size an edited run may be shrunk to, so that its text fits
/// the lines of its box, as a share of its original size; below it, the
/// text runs past the box.
const RUN_MIN_SCALE_RATIO: f32 = 0.75;
/// Step by which an edited run is shrunk while its text does not fit.
const RUN_SHRINK_STEP_RATIO: f32 = 0.05;
/// Distance between the baselines of the lines of a one-line run once
/// broken, in ems.
const SINGLE_LINE_PITCH_EM: f32 = 1.2;
/// How much wider than the original a one-line label may be written before
/// it is made smaller: a label is not broken, and its new text is often a
/// little longer (the box is the ink of the old text, with no margin).
const LABEL_MAX_GROWTH_RATIO: f32 = 1.6;

/// The runs of the text inside `block`: each the indexes of its lines in
/// [`Block::inner_lines`], top to bottom. Empty for a block whose text is
/// not that of a figure's picture (a rebuilt table).
pub(crate) fn runs(block: &Block) -> Vec<Vec<usize>> {
    if block.table.is_some() || block.inner_lines.len() != block.inner_spans.len() {
        return Vec::new();
    }
    // Lines come top to bottom across the whole figure, so the lines of a
    // run may be interleaved with those of a run beside it: a line joins
    // the run it continues, wherever that run is.
    let mut order: Vec<usize> = (0..block.inner_lines.len()).collect();
    order.sort_by(|&a, &b| block.inner_lines[a].baseline.total_cmp(&block.inner_lines[b].baseline));
    let mut runs: Vec<Vec<usize>> = Vec::new();
    for idx in order {
        let line = &block.inner_lines[idx];
        let continued = runs.iter_mut().rev().find(|run| {
            let prev = &block.inner_lines[*run.last().expect("a run has a line")];
            same_run(prev, line)
        });
        match continued {
            Some(run) => run.push(idx),
            None => runs.push(vec![idx]),
        }
    }
    // In reading order: by the top of their first line, then from the left.
    runs.sort_by(|a, b| {
        let (la, lb) = (&block.inner_lines[a[0]], &block.inner_lines[b[0]]);
        la.bbox
            .y0
            .total_cmp(&lb.bbox.y0)
            .then(la.bbox.x0.total_cmp(&lb.bbox.x0))
    });
    runs
}

/// Whether `line` continues the run whose last line is `prev`.
fn same_run(prev: &Line, line: &Line) -> bool {
    let size = prev.size.max(line.size).max(f32::EPSILON);
    let gap = line.baseline - prev.baseline;
    let left = (line.bbox.x0 - prev.bbox.x0).abs();
    let centre = ((line.bbox.x0 + line.bbox.x1) - (prev.bbox.x0 + prev.bbox.x1)).abs() / 2.0;
    // A tick label (a number) never joins a label of words next to it.
    let has_letters = |line: &Line| {
        line.glyphs
            .iter()
            .any(|glyph| glyph.text.chars().any(char::is_alphabetic))
    };
    prev.page == line.page
        && has_letters(prev) == has_letters(line)
        && gap > 0.0
        && gap <= RUN_PITCH_MAX_EM * size
        && (prev.size - line.size).abs() <= RUN_SIZE_TOLERANCE_RATIO * size
        && left.min(centre) <= RUN_ALIGN_TOLERANCE_EM * size
}

/// The rotated runs of text (an axis title written upwards) inside the
/// picture of the figure `block`, from the rotated text of the pages.
pub(crate) fn rotated_in<'a>(rotated: &'a [Vec<RotatedRun>], block: &Block) -> Vec<&'a RotatedRun> {
    let BlockKind::Figure { image: Some(spec), .. } = &block.kind else {
        return Vec::new();
    };
    if block.table.is_some() || spec.embedded.is_some() {
        return Vec::new();
    }
    rotated
        .get(spec.page)
        .map(|runs| {
            runs.iter()
                .filter(|run| crate::render::hard95::run_in_region(run, &spec.rect))
                .collect()
        })
        .unwrap_or_default()
}

/// The styled text of a rotated run.
pub(crate) fn rotated_spans(run: &RotatedRun) -> Vec<Span> {
    vec![Span {
        text: run.text.clone(),
        style: run.style,
        href: None,
        color: run.color,
        math: None,
        note: None,
    }]
}

/// The styled text of a run: its lines joined by spaces.
pub(crate) fn run_spans(block: &Block, run: &[usize]) -> Vec<Span> {
    let mut spans: Vec<Span> = Vec::new();
    for (pos, &idx) in run.iter().enumerate() {
        if pos > 0 {
            spans.push(Span::plain(" "));
        }
        spans.extend(block.inner_spans[idx].iter().cloned());
    }
    // Lines of the same style make one span, not one per line.
    let mut merged: Vec<Span> = Vec::with_capacity(spans.len());
    for span in spans {
        match merged.last_mut() {
            Some(last)
                if last.style == span.style
                    && last.href == span.href
                    && last.color == span.color
                    && last.math.is_none()
                    && span.math.is_none()
                    && last.note.is_none()
                    && span.note.is_none() =>
            {
                last.text.push_str(&span.text);
            }
            _ => merged.push(span),
        }
    }
    merged
}

/// One run as the JSON writes it.
#[derive(serde::Serialize)]
pub(crate) struct RunOut {
    /// Its text with markup (editable).
    text: String,
    /// Its box on the page, in points: left, top, right, bottom.
    #[serde(rename = "box")]
    bbox: [f32; 4],
    /// Its font size in points.
    size: f32,
    /// The direction of its baseline in degrees, for rotated text.
    #[serde(skip_serializing_if = "Option::is_none")]
    rotation: Option<f32>,
}

/// The runs of `block` as the JSON writes them, its rotated runs
/// (`rotated_in`) after the others; `None` when it has none.
pub(crate) fn runs_out(block: &Block, rotated: &[&RotatedRun]) -> Option<Vec<RunOut>> {
    let runs = runs(block);
    let round = |value: f32| (value * 100.0).round() / 100.0;
    let mut out: Vec<RunOut> = runs
        .iter()
        .map(|run| {
            let lines = run.iter().map(|&idx| &block.inner_lines[idx]);
            let round = |value: f32| (value * 100.0).round() / 100.0;
            let (x0, y0, x1, y1) = lines.fold((f32::MAX, f32::MAX, f32::MIN, f32::MIN), |acc, line| {
                (
                    acc.0.min(line.bbox.x0),
                    acc.1.min(line.bbox.y0),
                    acc.2.max(line.bbox.x1),
                    acc.3.max(line.bbox.y1),
                )
            });
            RunOut {
                text: text_of(&run_spans(block, run)),
                bbox: [round(x0), round(y0), round(x1), round(y1)],
                size: round(block.inner_lines[run[0]].size),
                rotation: None,
            }
        })
        .collect();
    out.extend(rotated.iter().map(|run| {
        let (sin, cos) = run.angle.to_radians().sin_cos();
        let (x1, y1) = (run.x + cos * run.length, run.y + sin * run.length);
        RunOut {
            text: text_of(&rotated_spans(run)),
            bbox: [
                round(run.x.min(x1)),
                round(run.y.min(y1)),
                round(run.x.max(x1)),
                round(run.y.max(y1)),
            ],
            size: round(run.size),
            rotation: Some(round(run.angle)),
        }
    }));
    (!out.is_empty()).then_some(out)
}

/// The edited runs of one figure: the lines of each and its new spans.
pub(crate) struct FigureEdit {
    /// Index of the figure in [`crate::model::Document::blocks`].
    pub(crate) block_idx: usize,
    /// Each edited run: its line indexes and its new text.
    pub(crate) runs: Vec<(Vec<usize>, Vec<Span>)>,
    /// Each edited rotated run and its new text.
    pub(crate) turned: Vec<(RotatedRun, Vec<Span>)>,
}

/// The figure `block` drawn again with its edited runs, as an SVG of its
/// picture's box: the page region without their glyphs, and their new text.
/// Warnings (a text that does not fit) go to `warnings`. `None` when the
/// page cannot be drawn.
pub(crate) fn figure_svg(
    renderer: &PageRenderer<'_>,
    block: &Block,
    edit: &FigureEdit,
    warnings: &mut Vec<String>,
) -> Option<String> {
    let crate::model::BlockKind::Figure { image: Some(spec), .. } = &block.kind else {
        return None;
    };
    let mut removed: HashSet<(i32, i32)> = HashSet::new();
    for (run, _) in &edit.turned {
        for &(x, y) in &run.origins {
            removed.insert(origin_key(x, y));
        }
    }
    for (run, _) in &edit.runs {
        for &idx in run {
            for glyph in &block.inner_lines[idx].glyphs {
                removed.insert(origin_key(glyph.ox, glyph.oy));
                for &(x, y) in &glyph.extra {
                    removed.insert(origin_key(x, y));
                }
            }
        }
    }
    let remove = |x: f32, y: f32| {
        let (qx, qy) = origin_key(x, y);
        (-1..=1).any(|dx| (-1..=1).any(|dy| removed.contains(&(qx + dx, qy + dy))))
    };
    let page = renderer.page_svg_without_all_text(spec.page, &remove)?;
    let rect = spec.rect;
    // The page in points, seen through the figure's box.
    let open_end = page.find('>')?;
    let root = &page[..open_end];
    let body = &page[open_end + 1..page.rfind("</svg>")?];
    let xlink = if root.contains("xmlns:xlink") {
        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
    } else {
        ""
    };
    let mut out = format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\"{xlink} width=\"{w}\" height=\"{h}\" viewBox=\"{x} {y} {w} {h}\">{body}",
        x = rect.x0,
        y = rect.y0,
        w = rect.width(),
        h = rect.height(),
    );
    for (run, spans) in &edit.runs {
        out.push_str(&run_text(block, run, spans, block.id, warnings));
    }
    for (run, spans) in &edit.turned {
        out.push_str(&turned_text(run, spans));
    }
    out.push_str("</svg>");
    Some(out)
}

/// The SVG `<text>` of an edited run: its words broken into lines that fit
/// the width of its box, at its size (shrunk down to
/// [`RUN_MIN_SCALE_RATIO`] when they need more lines than it had), on its
/// lines' baselines and pitch, aligned as its lines were.
fn run_text(block: &Block, run: &[usize], spans: &[Span], id: usize, warnings: &mut Vec<String>) -> String {
    let lines: Vec<&Line> = run.iter().map(|&idx| &block.inner_lines[idx]).collect();
    let first = lines[0];
    let x0 = lines.iter().map(|line| line.bbox.x0).fold(f32::MAX, f32::min);
    let x1 = lines.iter().map(|line| line.bbox.x1).fold(f32::MIN, f32::max);
    let width = x1 - x0;
    let pitch = if lines.len() > 1 {
        (lines[lines.len() - 1].baseline - first.baseline) / (lines.len() - 1) as f32
    } else {
        first.size * SINGLE_LINE_PITCH_EM
    };
    let centred = lines.len() == 1 || {
        let spread = |key: &dyn Fn(&Line) -> f32| {
            let values: Vec<f32> = lines.iter().map(|line| key(line)).collect();
            values.iter().copied().fold(f32::MIN, f32::max) - values.iter().copied().fold(f32::MAX, f32::min)
        };
        spread(&|line| line.bbox.x0) > spread(&|line| (line.bbox.x0 + line.bbox.x1) / 2.0)
    };
    let style = spans.first().map_or(0, |span| span.style);
    let font = TextFont::of(style);
    let text: String = spans.iter().map(|span| span.text.as_str()).collect();
    let words: Vec<&str> = text.split_whitespace().collect();
    // One line may be a little wider than the original: the box is the ink
    // of the old text, and a short label's new text often runs past it.
    let max_lines = lines.len().max(1);
    let mut size = first.size;
    let fits = |broken: &[String], size: f32| {
        if max_lines == 1 {
            text_width(&broken.join(" "), size, &font) <= width * LABEL_MAX_GROWTH_RATIO
        } else {
            broken.len() <= max_lines
        }
    };
    let layout = |size: f32| {
        if max_lines == 1 {
            vec![words.join(" ")]
        } else {
            break_words(&words, width, size, &font)
        }
    };
    let mut broken = layout(size);
    while !fits(&broken, size) && size > first.size * RUN_MIN_SCALE_RATIO {
        size -= first.size * RUN_SHRINK_STEP_RATIO;
        broken = layout(size);
    }
    if !fits(&broken, size) {
        warnings.push(format!(
            "block {id}: the edited text inside the figure (\"{}\") does not fit its box",
            text.chars().take(40).collect::<String>()
        ));
    }
    let pitch = pitch * size / first.size;
    let (anchor, x) = if centred {
        ("middle", (x0 + x1) / 2.0)
    } else {
        ("start", x0)
    };
    let mut out = format!(
        "<text font-family=\"{}\" font-size=\"{size}\" font-weight=\"{}\" font-style=\"{}\" text-anchor=\"{anchor}\" fill=\"#000\">",
        font.family, font.weight, font.style
    );
    for (pos, line) in broken.iter().enumerate() {
        out.push_str(&format!(
            "<tspan x=\"{x}\" y=\"{}\">{}</tspan>",
            first.baseline + pos as f32 * pitch,
            esc(line)
        ));
    }
    out.push_str("</text>");
    out
}

/// The SVG `<text>` of an edited rotated run: on its baseline, centred on
/// the middle of the old text, at its size (smaller, down to
/// [`RUN_MIN_SCALE_RATIO`], when much longer than the old text).
fn turned_text(run: &RotatedRun, spans: &[Span]) -> String {
    let font = TextFont::of(spans.first().map_or(run.style, |span| span.style));
    let text: String = spans.iter().map(|span| span.text.as_str()).collect();
    let width = text_width(&text, run.size, &font);
    let room = run.length * LABEL_MAX_GROWTH_RATIO;
    let size = if width > room {
        (run.size * room / width).max(run.size * RUN_MIN_SCALE_RATIO)
    } else {
        run.size
    };
    let (sin, cos) = run.angle.to_radians().sin_cos();
    let (mid_x, mid_y) = (run.x + cos * run.length / 2.0, run.y + sin * run.length / 2.0);
    format!(
        "<text transform=\"translate({mid_x} {mid_y}) rotate({})\" font-family=\"{}\" font-size=\"{size}\" font-weight=\"{}\" font-style=\"{}\" text-anchor=\"middle\" fill=\"#000\">{}</text>",
        run.angle,
        font.family,
        font.weight,
        font.style,
        esc(&text)
    )
}

/// The font an edited run is written in, from the style of its first span.
struct TextFont {
    /// CSS font family.
    family: &'static str,
    /// CSS font weight.
    weight: &'static str,
    /// CSS font style.
    style: &'static str,
}

impl TextFont {
    /// The font for the style bits `style`.
    fn of(style: u8) -> Self {
        Self {
            // Most figures are labelled in a sans-serif font (and Type 3
            // fonts, as matplotlib writes, are not marked as such).
            family: if style & MONO != 0 { "monospace" } else { "sans-serif" },
            weight: if style & BOLD != 0 { "bold" } else { "normal" },
            style: if style & ITALIC != 0 { "italic" } else { "normal" },
        }
    }
}

/// `words` broken into lines no wider than `width` at `size` (a word wider
/// than the width has a line of its own).
fn break_words(words: &[&str], width: f32, size: f32, font: &TextFont) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    let mut current = String::new();
    for word in words {
        let candidate = if current.is_empty() {
            (*word).to_string()
        } else {
            format!("{current} {word}")
        };
        if !current.is_empty() && text_width(&candidate, size, font) > width {
            lines.push(std::mem::replace(&mut current, (*word).to_string()));
        } else {
            current = candidate;
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines
}

/// The width of `text` at `size` in `font`, in points, measured with the
/// fonts SVG text is drawn with; a rough estimate when it cannot be.
fn text_width(text: &str, size: f32, font: &TextFont) -> f32 {
    let svg = format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"10000\" height=\"100\"><text x=\"0\" y=\"50\" font-family=\"{}\" font-size=\"{size}\" font-weight=\"{}\" font-style=\"{}\">{}</text></svg>",
        font.family,
        font.weight,
        font.style,
        esc(text)
    );
    let options = resvg::usvg::Options {
        fontdb: crate::backend::render::svg_fonts(),
        ..Default::default()
    };
    resvg::usvg::Tree::from_str(&svg, &options)
        .ok()
        .map(|tree| tree.root().abs_bounding_box().width())
        .filter(|width| *width > 0.0)
        .unwrap_or_else(|| text.chars().count() as f32 * size * 0.5)
}