liepress 0.1.0-beta.1

A Markdown to PDF/SVG/PNG converter with CSS styling support
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! 表格布局引擎
//!
//! 将表格 AST 节点转换为 VisualElement 列表。
//! 核心算法:启发式列宽分配 + 基于换行的行高计算。
//!
//! 设计分两阶段:
//! 1. compute_layout_info — 纯计算,返回列宽、行高等布局数据
//! 2. generate_rows — 按行区间生成视觉元素,支持跨页分割

use crate::ast::{Node, NodeKind, Style, TextAlign, computed_style_to_text_style};
use crate::generator::text::{annotate_runs_with_urls, collect_inline_segments};
use crate::text::{
    FONT_CONTEXT, LAYOUT_CONTEXT, TextAlign as TextAlign2, TextStyle, layout_text_with_contexts,
};
use crate::visual::{FillStrokeStyle, StrokeStyle, VisualElement};
use vello_cpu::kurbo::{Point, Rect};

// ─── 内部数据结构 ───

/// 单元格测量结果
struct CellMeasure {
    /// 不换行时的完整文本宽度
    ideal_width: f32,
    /// 最宽不可断片段的宽度(最小宽度)
    min_width: f32,
}

// ─── 公开数据结构 ───

/// 预计算的表格布局数据(不含视觉元素)
pub struct TableLayoutInfo {
    /// 行数
    pub num_rows: usize,
    /// 列数
    pub num_cols: usize,
    /// 每列宽度
    pub col_widths: Vec<f32>,
    /// 每行高度
    pub row_heights: Vec<f32>,
    /// 每列对齐方式
    pub row_align: Vec<TextAlign>,
}

// ─── 阶段 1:计算布局信息 ───

/// 计算表格布局信息(列宽 + 行高),不生成视觉元素
pub fn compute_layout_info(node: &Node, content_width: f32) -> TableLayoutInfo {
    // 1. 解析表格结构
    let rows = collect_rows(node);
    let num_cols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
    if num_cols == 0 || rows.is_empty() {
        return TableLayoutInfo {
            num_rows: 0,
            num_cols: 0,
            col_widths: vec![],
            row_heights: vec![],
            row_align: vec![],
        };
    }

    // 2. 获取列对齐信息
    let align = build_alignment(node, num_cols);

    // 3. 测量单元格
    let cell_measures = measure_cells(&rows, num_cols);

    // 4. 计算列宽
    let col_widths = calculate_column_widths(&cell_measures, num_cols, content_width);

    // 5. 计算行高
    let row_heights = calculate_row_heights(&rows, &col_widths, num_cols);

    TableLayoutInfo {
        num_rows: rows.len(),
        num_cols,
        col_widths,
        row_heights,
        row_align: align,
    }
}

// ─── 阶段 2:按行区间生成视觉元素 ───

/// 为 [row_start..row_end) 范围内的行生成视觉元素
///
/// 所有元素的坐标相对于表格内容区左上角(即左缩进 + content_x 后的偏移由调用方处理)。
/// 此函数负责单元格文本、背景填充以及该区间内的边框线。
pub fn generate_rows(
    node: &Node,
    layout: &TableLayoutInfo,
    row_start: usize,
    row_end: usize,
    style: &Style,
) -> Vec<VisualElement> {
    if row_start >= row_end || row_start >= layout.num_rows {
        return vec![];
    }
    let end = row_end.min(layout.num_rows);

    let rows = collect_rows(node);

    // 计算列起始 X 坐标
    let col_starts = compute_column_starts(&layout.col_widths);

    // 计算该行区间内的起始 Y 坐标(相对值,从 0 开始)
    let row_starts = compute_row_starts_range(&layout.row_heights, row_start, end);

    let mut elements = Vec::new();

    // 确定是否包含表头行(第一行为表头)
    let has_header = !rows.is_empty();

    // 背景填充(表头 + 交替行)
    let chunk_table_width: f32 = col_starts.last().copied().unwrap_or(0.0)
        + layout.col_widths.last().copied().unwrap_or(0.0);

    for (chunk_idx, abs_row_idx) in (row_start..end).enumerate() {
        let row_y = row_starts[chunk_idx];
        let row_h = layout.row_heights[abs_row_idx];

        // 表头背景
        if has_header && abs_row_idx == 0 {
            if let Some(bg) = style.table_header_bg {
                elements.push(VisualElement::Rect {
                    rect: Rect::new(
                        0.0,
                        row_y as f64,
                        chunk_table_width as f64,
                        (row_y + row_h) as f64,
                    ),
                    style: FillStrokeStyle {
                        fill: Some(bg),
                        stroke: None,
                    },
                });
            }
        } else if abs_row_idx > 0 && abs_row_idx % 2 == 0 {
            // 偶数行(非表头)使用交替行背景
            if let Some(bg) = style.table_alt_row_bg {
                elements.push(VisualElement::Rect {
                    rect: Rect::new(
                        0.0,
                        row_y as f64,
                        chunk_table_width as f64,
                        (row_y + row_h) as f64,
                    ),
                    style: FillStrokeStyle {
                        fill: Some(bg),
                        stroke: None,
                    },
                });
            }
        }
    }

    // 生成单元格文本
    layout_cell_texts(
        &rows,
        &col_starts,
        &layout.col_widths,
        &row_starts,
        &layout.row_heights,
        &layout.row_align,
        layout.num_cols,
        row_start,
        end,
        style,
        &mut elements,
    );

    // 绘制该行区间内的边框
    draw_table_borders_range(
        &col_starts,
        &layout.col_widths,
        &row_starts,
        &layout.row_heights,
        row_start,
        end,
        style,
        &mut elements,
    );

    elements
}

// ─── 解析结构 ───

/// 从表格节点中提取二维单元格节点数组
fn collect_rows(node: &Node) -> Vec<Vec<&Node>> {
    let mut rows = Vec::new();
    if let NodeKind::Table { children, .. } = &node.kind {
        for child in children {
            if let NodeKind::TableRow { children: cells } = &child.kind {
                let row: Vec<&Node> = cells.iter().collect();
                rows.push(row);
            }
        }
    }
    rows
}

/// 构建列对齐数组
fn build_alignment(node: &Node, num_cols: usize) -> Vec<TextAlign> {
    let mut a = match &node.kind {
        NodeKind::Table { align, .. } => align.clone(),
        _ => vec![],
    };
    while a.len() < num_cols {
        a.push(TextAlign::Left);
    }
    a.truncate(num_cols);
    a
}

// ─── 测量单元格 ───

/// 测量所有单元格,返回每行每列的测量结果
fn measure_cells(rows: &[Vec<&Node>], num_cols: usize) -> Vec<Vec<CellMeasure>> {
    let padding_h = 4.0; // 用固定值而非样式,测量阶段尚不知样式
    let mut measures = Vec::with_capacity(rows.len());
    for row in rows {
        let mut row_meas = Vec::with_capacity(num_cols);
        for cell in row.iter().take(num_cols) {
            row_meas.push(measure_cell(cell, padding_h));
        }
        while row_meas.len() < num_cols {
            row_meas.push(CellMeasure {
                ideal_width: padding_h * 2.0,
                min_width: padding_h * 2.0,
            });
        }
        measures.push(row_meas);
    }
    measures
}

/// 测量单个单元格的理想宽度和最小宽度
fn measure_cell(cell: &Node, padding_h: f32) -> CellMeasure {
    let segments = collect_inline_segments_from_cell(cell);
    if segments.is_empty() {
        return CellMeasure {
            ideal_width: padding_h * 2.0,
            min_width: padding_h * 2.0,
        };
    }

    let texts: Vec<(&str, &TextStyle)> = segments.iter().map(|(t, s)| (t.as_str(), s)).collect();

    let ideal_width = FONT_CONTEXT.with(|font_cx| {
        LAYOUT_CONTEXT.with(|layout_cx| {
            let mut fcx = font_cx.borrow_mut();
            let mut lcx = layout_cx.borrow_mut();
            let layout =
                layout_text_with_contexts(&texts, None, TextAlign2::Left, &mut fcx, &mut lcx);
            layout.width as f32
        })
    });

    let min_width = segments
        .iter()
        .flat_map(|(text, style)| {
            let words = split_words(text);
            words.into_iter().map(move |w| (w, style))
        })
        .filter(|(w, _)| !w.is_empty())
        .map(|(word, style)| {
            FONT_CONTEXT.with(|font_cx| {
                LAYOUT_CONTEXT.with(|layout_cx| {
                    let mut fcx = font_cx.borrow_mut();
                    let mut lcx = layout_cx.borrow_mut();
                    let layout = layout_text_with_contexts(
                        &[(word, style)],
                        None,
                        TextAlign2::Left,
                        &mut fcx,
                        &mut lcx,
                    );
                    layout.width as f32
                })
            })
        })
        .fold(0.0_f32, f32::max);

    CellMeasure {
        ideal_width: ideal_width + padding_h * 2.0,
        min_width: (min_width + padding_h * 2.0).max(padding_h * 2.0),
    }
}

/// 从单元格节点收集内联段
fn collect_inline_segments_from_cell(cell: &Node) -> Vec<(String, TextStyle)> {
    match &cell.kind {
        NodeKind::Paragraph { children } => collect_inline_segments(children),
        _ => {
            let text = cell.text_content();
            if text.is_empty() {
                vec![]
            } else {
                vec![(text, computed_style_to_text_style(&cell.style))]
            }
        }
    }
}

// ─── 计算列宽 ───

fn calculate_column_widths(
    cell_measures: &[Vec<CellMeasure>],
    num_cols: usize,
    available_width: f32,
) -> Vec<f32> {
    let mut ideal_w = vec![0.0_f32; num_cols];
    let mut min_w = vec![0.0_f32; num_cols];

    for row in cell_measures {
        for (c, m) in row.iter().enumerate().take(num_cols) {
            if m.ideal_width > ideal_w[c] {
                ideal_w[c] = m.ideal_width;
            }
            if m.min_width > min_w[c] {
                min_w[c] = m.min_width;
            }
        }
    }

    let total_ideal: f32 = ideal_w.iter().sum();
    if total_ideal <= available_width {
        return ideal_w;
    }

    let total_min: f32 = min_w.iter().sum();
    if total_min >= available_width {
        return min_w;
    }

    let extra = available_width - total_min;
    let ideal_extra: f32 = ideal_w.iter().zip(min_w.iter()).map(|(i, m)| i - m).sum();

    let mut final_w = Vec::with_capacity(num_cols);
    for c in 0..num_cols {
        let ratio = if ideal_extra > 0.0 {
            (ideal_w[c] - min_w[c]) / ideal_extra
        } else {
            1.0 / num_cols as f32
        };
        final_w.push(min_w[c] + extra * ratio);
    }

    final_w
}

// ─── 计算行高 ───

fn calculate_row_heights(rows: &[Vec<&Node>], col_widths: &[f32], num_cols: usize) -> Vec<f32> {
    let padding_h = 4.0;
    let padding_v = 2.0;
    let mut heights = Vec::with_capacity(rows.len());

    for row in rows {
        let mut max_height = 0.0_f32;

        for (c, cell) in row.iter().enumerate().take(num_cols) {
            let cell_width = col_widths[c] - padding_h * 2.0;
            if cell_width <= 0.0 {
                continue;
            }

            let segments = collect_inline_segments_from_cell(cell);
            if segments.is_empty() {
                max_height = max_height.max(padding_v * 2.0);
                continue;
            }

            let texts: Vec<(&str, &TextStyle)> =
                segments.iter().map(|(t, s)| (t.as_str(), s)).collect();

            let height = FONT_CONTEXT.with(|font_cx| {
                LAYOUT_CONTEXT.with(|layout_cx| {
                    let mut fcx = font_cx.borrow_mut();
                    let mut lcx = layout_cx.borrow_mut();
                    let layout = layout_text_with_contexts(
                        &texts,
                        Some(cell_width as f64),
                        TextAlign2::Left,
                        &mut fcx,
                        &mut lcx,
                    );

                    let mut h = 0.0_f32;
                    for line in &layout.lines {
                        h += line.line_height;
                    }
                    h
                })
            });

            max_height = max_height.max(height + padding_v * 2.0);
        }

        heights.push(max_height.max(padding_v * 2.0));
    }

    heights
}

// ─── 累积坐标 ───

/// 计算每列起始 X 坐标
fn compute_column_starts(col_widths: &[f32]) -> Vec<f32> {
    let mut starts = Vec::with_capacity(col_widths.len());
    let mut x = 0.0_f32;
    for w in col_widths {
        starts.push(x);
        x += w;
    }
    starts
}

/// 计算 [row_start..row_end) 范围内的行起始 Y 坐标(相对值)
fn compute_row_starts_range(row_heights: &[f32], row_start: usize, row_end: usize) -> Vec<f32> {
    let mut starts = Vec::with_capacity(row_end - row_start);
    let mut y = 0.0_f32;
    for i in row_start..row_end {
        starts.push(y);
        y += row_heights[i];
    }
    starts
}

// ─── 生成单元格文本 ───

fn layout_cell_texts(
    rows: &[Vec<&Node>],
    col_starts: &[f32],
    col_widths: &[f32],
    row_starts: &[f32],
    _row_heights: &[f32],
    align: &[TextAlign],
    num_cols: usize,
    row_start: usize,
    row_end: usize,
    style: &Style,
    elements: &mut Vec<VisualElement>,
) {
    let padding_h = style.table_cell_padding_h_pt;
    let padding_v = style.table_cell_padding_v_pt;

    for (chunk_idx, abs_row_idx) in (row_start..row_end).enumerate() {
        if abs_row_idx >= rows.len() {
            break;
        }
        let row = &rows[abs_row_idx];
        let cell_y = row_starts[chunk_idx];

        for (c, cell) in row.iter().enumerate().take(num_cols) {
            let cell_x = col_starts[c];
            let cell_w = col_widths[c];

            let segments = collect_inline_segments_from_cell(cell);
            if segments.is_empty() {
                continue;
            }

            let text_align = map_text_align(align.get(c).copied().unwrap_or(TextAlign::Left));
            let cell_text_width = cell_w - padding_h * 2.0;
            if cell_text_width <= 0.0 {
                continue;
            }

            let texts: Vec<(&str, &TextStyle)> =
                segments.iter().map(|(t, s)| (t.as_str(), s)).collect();

            FONT_CONTEXT.with(|font_cx| {
                LAYOUT_CONTEXT.with(|layout_cx| {
                    let mut fcx = font_cx.borrow_mut();
                    let mut lcx = layout_cx.borrow_mut();

                    let total_text: String = segments.iter().map(|(t, _)| t.as_str()).collect();
                    let layout = layout_text_with_contexts(
                        &texts,
                        Some(cell_text_width as f64),
                        text_align,
                        &mut fcx,
                        &mut lcx,
                    );

                    let mut lines = layout.lines;
                    annotate_runs_with_urls(&mut lines, &total_text, &segments);
                    for line in &lines {
                        let line_abs_x = cell_x + padding_h + line.bounds.x0 as f32;
                        let line_abs_y = cell_y + padding_v + line.bounds.y0 as f32;
                        let line_width = line.bounds.width() as f32;
                        let bounds = Rect::new(
                            line_abs_x as f64,
                            line_abs_y as f64,
                            (line_abs_x + line_width) as f64,
                            (line_abs_y + line.line_height) as f64,
                        );

                        elements.push(VisualElement::TextLine {
                            runs: line.runs.clone(),
                            bounds,
                            line_height: line.line_height,
                        });
                    }
                })
            });
        }
    }
}

// ─── 绘制区间边框 ───

/// 绘制 [row_start..row_end) 行区间内的边框
fn draw_table_borders_range(
    col_starts: &[f32],
    col_widths: &[f32],
    row_starts: &[f32],
    row_heights: &[f32],
    row_start: usize,
    row_end: usize,
    style: &Style,
    elements: &mut Vec<VisualElement>,
) {
    let border = StrokeStyle {
        color: style.table_border_color,
        width: style.table_border_width_pt as f64,
    };

    // 该区间的总宽度
    let table_width: f32 =
        col_starts.last().copied().unwrap_or(0.0) + col_widths.last().copied().unwrap_or(0.0);

    // 该区间的高度范围
    let chunk_first_y = row_starts.first().copied().unwrap_or(0.0);
    let chunk_last_y = row_starts.last().copied().unwrap_or(0.0)
        + if let (Some(&last_h), Some(_last_start)) =
            (row_heights.get(row_end - 1), row_starts.last())
        {
            last_h
        } else {
            0.0
        };

    // 顶部边框(仅在 row_start == 0 时或该区间第一行的顶部)
    elements.push(VisualElement::Line {
        start: Point::new(0.0, chunk_first_y as f64),
        end: Point::new(table_width as f64, chunk_first_y as f64),
        style: border.clone(),
    });

    // 每行底部边框
    for (chunk_idx, abs_row_idx) in (row_start..row_end).enumerate() {
        let y = row_starts[chunk_idx] + row_heights[abs_row_idx];
        elements.push(VisualElement::Line {
            start: Point::new(0.0, y as f64),
            end: Point::new(table_width as f64, y as f64),
            style: border.clone(),
        });
    }

    // 垂直边框(每列左右)
    for c in 0..=col_widths.len() {
        let x = if c == 0 {
            0.0
        } else if c < col_starts.len() {
            col_starts[c]
        } else {
            col_starts.last().copied().unwrap_or(0.0) + col_widths.last().copied().unwrap_or(0.0)
        };

        elements.push(VisualElement::Line {
            start: Point::new(x as f64, chunk_first_y as f64),
            end: Point::new(x as f64, chunk_last_y as f64),
            style: border.clone(),
        });
    }
}

// ─── 辅助函数 ───

fn map_text_align(a: TextAlign) -> TextAlign2 {
    match a {
        TextAlign::Left => TextAlign2::Left,
        TextAlign::Center => TextAlign2::Center,
        TextAlign::Right => TextAlign2::Right,
        TextAlign::Justify => TextAlign2::Left,
    }
}

fn split_words(text: &str) -> Vec<&str> {
    let mut words = Vec::new();
    let mut start = 0;
    let mut in_word = false;

    for (i, c) in text.char_indices() {
        if c.is_whitespace() {
            if in_word {
                words.push(&text[start..i]);
                in_word = false;
            }
        } else if is_cjk(c) {
            if in_word {
                words.push(&text[start..i]);
                in_word = false;
            }
            words.push(&text[i..i + c.len_utf8()]);
        } else {
            if !in_word {
                start = i;
                in_word = true;
            }
        }
    }
    if in_word {
        words.push(&text[start..]);
    }
    words
}

fn is_cjk(c: char) -> bool {
    matches!(c,
        '\u{4E00}'..='\u{9FFF}'
        | '\u{3400}'..='\u{4DBF}'
        | '\u{F900}'..='\u{FAFF}'
        | '\u{2F800}'..='\u{2FA1F}'
    )
}