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
use crate::error::Error;
use crate::visual::Color;
use parley::fontique::GenericFamily;
use parley::style::{
    FontFamily, FontFamilyName, FontStyle as ParleyFontStyle, FontWeight, StyleProperty,
};
use parley::{Alignment, AlignmentOptions, FontContext, LayoutContext};
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use vello_cpu::kurbo::Rect;

// 重新导出 TextDecoration 供 renderer 使用
pub use crate::ast::TextDecoration;

/// 文本布局 - 包含排版后的行集合
///
/// 由 [`layout_text`] / [`layout_text_with_contexts`] 生成。
/// TextLayout 中的坐标全部相对 layout 原点(段落左上角),
/// 分页和绝对定位由 generator 模块负责。
#[derive(Clone, Debug)]
pub struct TextLayout {
    /// 排版后的所有行
    pub lines: Vec<TextLine>,
    /// 布局总宽度(pt)
    pub width: f64,
    /// 布局总高度(pt)
    pub height: f64,
}

/// 字形信息 - 与 parley 的 Glyph 一一对应
#[derive(Clone, Debug)]
pub struct Glyph {
    /// 字形 ID
    pub id: u32,
    /// X 坐标(相对所属 TextLine.bounds.origin 的偏移)
    pub x: f32,
    /// Y 坐标(相对所属 TextLine.bounds.origin 的偏移)
    pub y: f32,
    /// 前进宽度
    pub advance: f32,
}

/// 文本 Run - 与 parley 的 GlyphRun 一一对应
///
/// 一个 Run 是一段具有相同样式(字体、字号、颜色)的连续字形序列。
/// 它是文本渲染的最小单元。
#[derive(Clone, Debug)]
pub struct TextRun {
    /// 该 Run 的文本内容
    pub text: String,
    /// 该 Run 在段落中的文本范围
    pub text_range: std::ops::Range<usize>,
    /// 字体数据(指向 parley FontData 的引用)
    pub font_data: parley::FontData,
    /// 字体大小
    pub font_size: f32,
    /// 文本颜色
    pub color: Color,
    /// 总前进宽度
    pub advance: f32,
    /// 字形列表(坐标相对 TextLine.bounds.origin 偏移)
    pub glyphs: Vec<Glyph>,
    /// 是否从右到左
    pub is_rtl: bool,
    /// 第一个字符的基线 X 坐标(相对 layout 原点的绝对坐标)
    pub baseline_x: f32,
    /// 该行的基线 Y 坐标(相对行顶 row_top_rel 的偏移)
    /// 同一行内所有 run 共享此值
    pub baseline_y: f32,
    /// 超链接 URL(如果有)
    pub url: Option<String>,
    /// 文本修饰(none / underline / line-through)
    pub decoration: TextDecoration,
}

/// 文本行 - 包含一行中的所有 Run
///
/// 坐标系设计(相对 layout 原点):
/// - `bounds.origin`: 行在 layout 中的位置
/// - `runs[].glyphs[].x/y`: 相对 `bounds.origin` 的偏移量
///
/// 绝对定位时:glyph 页面坐标 = 页面偏移 + bounds.origin + glyph
#[derive(Clone, Debug)]
pub struct TextLine {
    /// 该行的所有 Run
    pub runs: Vec<TextRun>,
    /// 行的边界框(相对 layout 原点)
    pub bounds: Rect,
    /// 该行的高度(来自 LineMetrics.line_height)
    pub line_height: f32,
}

thread_local! {
    /// 全局字体上下文 - 线程本地存储
    pub static FONT_CONTEXT: RefCell<FontContext> = RefCell::new(FontContext::default());
    /// 全局布局上下文 - 线程本地存储
    pub static LAYOUT_CONTEXT: RefCell<LayoutContext<Color>> = RefCell::new(LayoutContext::default());
    /// 字体字节缓存 - 映射字体族名到原始字体字节
    /// 供 PdfRenderer 等需要直接访问字体数据的渲染后端使用
    pub static FONT_BYTES: RefCell<HashMap<String, Arc<Vec<u8>>>> = RefCell::new(HashMap::new());
}

/// 获取已注册字体的原始字节
pub fn get_font_bytes(family: &str) -> Option<Arc<Vec<u8>>> {
    FONT_BYTES.with(|cache| cache.borrow().get(family).cloned())
}

/// 访问字体上下文的便捷函数
pub fn with_font_context<R, F: FnOnce(&mut FontContext) -> R>(f: F) -> R {
    FONT_CONTEXT.with(|cx| f(&mut cx.borrow_mut()))
}

/// 访问布局上下文的便捷函数
pub fn with_layout_context<R, F: FnOnce(&mut LayoutContext<Color>) -> R>(f: F) -> R {
    LAYOUT_CONTEXT.with(|cx| f(&mut cx.borrow_mut()))
}

/// 同时访问两个上下文的便捷函数
pub fn with_text_contexts<R, F: FnOnce(&mut FontContext, &mut LayoutContext<Color>) -> R>(
    f: F,
) -> R {
    FONT_CONTEXT.with(|font_cx| {
        LAYOUT_CONTEXT.with(|layout_cx| f(&mut font_cx.borrow_mut(), &mut layout_cx.borrow_mut()))
    })
}

/// 文本对齐方式
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum TextAlign {
    #[default]
    Left,
    Center,
    Right,
}

#[derive(Debug, Clone)]
pub struct TextStyle {
    pub color: Color,
    /// 字体家族列表(优先级从高到低)
    pub font_family: Vec<String>,
    pub font_size: f64,
    pub font_weight: String,
    pub font_style: String,
    pub align: TextAlign,
    /// 超链接 URL(如果有)
    pub url: Option<String>,
    /// 文本修饰(none / underline / line-through)
    pub decoration: TextDecoration,
}

impl Default for TextStyle {
    fn default() -> Self {
        Self {
            color: Color::BLACK,
            font_family: vec!["sans-serif".to_string()],
            font_size: 10.5,
            font_weight: "normal".to_string(),
            font_style: "normal".to_string(),
            align: TextAlign::Left,
            url: None,
            decoration: TextDecoration::None,
        }
    }
}

/// 字体来源
#[derive(Debug, Clone)]
pub enum FontSource {
    /// 从文件路径加载
    Path(std::path::PathBuf),
    /// 从内存数据加载
    Memory(Vec<u8>),
}

/// 注册自定义字体到全局字体上下文。
pub fn register_font(
    source: FontSource,
    family_name_override: Option<&str>,
) -> crate::error::Result<()> {
    use parley::fontique::Blob;

    let (data, arc_bytes) = match source {
        FontSource::Path(path) => {
            let bytes = std::fs::read(&path)
                .map_err(|e| Error::FontLoadError(format!("读取字体文件失败: {e}")))?;
            let arc = Arc::new(bytes);
            (Blob::new(arc.clone()), arc)
        }
        FontSource::Memory(bytes) => {
            let arc = Arc::new(bytes);
            (Blob::new(arc.clone()), arc)
        }
    };

    let override_info = family_name_override.map(|name| parley::fontique::FontInfoOverride {
        family_name: Some(name),
        ..Default::default()
    });

    crate::text::with_font_context(|font_cx| {
        font_cx.collection.register_fonts(data, override_info);
    });

    if let Some(family) = family_name_override {
        FONT_BYTES.with(|cache| {
            cache.borrow_mut().insert(family.to_string(), arc_bytes);
        });
    }

    Ok(())
}

// ─── 内部辅助:从 parley Layout 提取行 ─────────────────────

/// 字形原始数据(相对 layout 原点的坐标),仅在提取过程中使用
struct GlyphRaw {
    id: u32,
    x: f32,
    y: f32,
    advance: f32,
}

/// 从 parley Layout 提取行列表,返回相对 layout 原点的 TextLine 集合。
///
/// 每个 TextLine.bounds.origin 表示该行在 layout 中的位置:
/// - x = 该行最左字形相对于 layout 左侧的偏移
/// - y = 该行顶部相对于 layout 顶部的累积偏移
/// runs[].glyphs[].x/y = glyph 坐标 - 行原点,即相对行左上角的偏移
fn extract_lines_from_parley(
    layout: &parley::Layout<Color>,
    _full_text: &str,
    decoration_map: &[(std::ops::Range<usize>, TextDecoration)],
) -> Vec<TextLine> {
    let mut lines = Vec::new();
    let mut row_top_rel = 0.0_f32;
    let mut full_text_pos = 0_usize;

    for line in layout.lines() {
        let metrics = line.metrics();
        let line_height = metrics.line_height;
        let baseline_y = metrics.baseline - row_top_rel;

        let mut glyph_data: Vec<(GlyphRaw, usize)> = Vec::new();
        let mut run_infos: Vec<(
            Color,
            parley::FontData,
            f32,
            parley::layout::Run<'_, Color>,
            f32,
        )> = Vec::new();

        let mut next_run_idx = 0;
        for item in line.items() {
            if let parley::layout::PositionedLayoutItem::GlyphRun(glyph_run) = item {
                let run = glyph_run.run();
                let color = glyph_run.style().brush;
                let font_data = run.font().clone();
                let font_size = run.font_size();

                let first_glyph_x = glyph_run
                    .positioned_glyphs()
                    .next()
                    .map(|g| g.x)
                    .unwrap_or(0.0);

                run_infos.push((color, font_data, font_size, *run, first_glyph_x));

                for g in glyph_run.positioned_glyphs() {
                    glyph_data.push((
                        GlyphRaw {
                            id: g.id,
                            x: g.x,
                            y: g.y,
                            advance: g.advance,
                        },
                        next_run_idx,
                    ));
                }
                next_run_idx += 1;
            }
        }

        if glyph_data.is_empty() {
            row_top_rel += line_height;
            continue;
        }

        let min_x = glyph_data
            .iter()
            .map(|(g, _)| g.x)
            .fold(f32::INFINITY, f32::min);
        let max_x = glyph_data
            .iter()
            .map(|(g, _)| g.x)
            .fold(f32::NEG_INFINITY, f32::max);
        let width = max_x - min_x;

        let mut runs = Vec::new();

        let mut run_glyph_ranges: Vec<(usize, usize)> = Vec::new();
        let mut current_start = 0;
        let mut last_run_idx = glyph_data[0].1;

        for (i, (_, run_idx)) in glyph_data.iter().enumerate() {
            if *run_idx != last_run_idx {
                run_glyph_ranges.push((current_start, i));
                current_start = i;
                last_run_idx = *run_idx;
            }
        }
        run_glyph_ranges.push((current_start, glyph_data.len()));

        for (start_idx, end_idx) in run_glyph_ranges.iter() {
            let run_idx = glyph_data[*start_idx].1;
            let (color, font_data, font_size, _run, first_glyph_x) = &run_infos[run_idx];

            let glyph_count = end_idx - start_idx;
            let text_range = full_text_pos..full_text_pos + glyph_count;
            full_text_pos = text_range.end;

            let relative_glyphs: Vec<Glyph> = glyph_data[*start_idx..*end_idx]
                .iter()
                .map(|(g, _)| Glyph {
                    id: g.id,
                    x: g.x - min_x,
                    y: g.y - row_top_rel,
                    advance: g.advance,
                })
                .collect();

            let advance = relative_glyphs.iter().map(|g| g.advance).sum();
            let baseline_x = *first_glyph_x - min_x;

            runs.push(TextRun {
                text: String::new(),
                text_range: text_range.clone(),
                font_data: font_data.clone(),
                font_size: *font_size,
                color: *color,
                advance,
                glyphs: relative_glyphs,
                is_rtl: false,
                baseline_x,
                baseline_y,
                url: None,
                decoration: lookup_decoration(text_range.start, decoration_map),
            });
        }

        let bounds = Rect::new(
            min_x as f64,
            row_top_rel as f64,
            (min_x + width) as f64,
            (row_top_rel + line_height) as f64,
        );

        lines.push(TextLine {
            runs,
            bounds,
            line_height,
        });

        row_top_rel += line_height;
    }

    lines
}

/// 查找 byte position 对应的文本修饰
fn lookup_decoration(
    pos: usize,
    map: &[(std::ops::Range<usize>, TextDecoration)],
) -> TextDecoration {
    for (range, dec) in map {
        if range.contains(&pos) {
            return *dec;
        }
    }
    TextDecoration::None
}

// ─── 公开布局函数 ────────────────────────────────────────

/// 创建文本布局
///
/// 使用 parley 排版文本,返回包含行集合的 TextLayout。
pub fn create_text_layout(
    text: &str,
    font_config: &TextStyle,
    max_width: Option<f64>,
) -> TextLayout {
    with_text_contexts(|font_cx, layout_cx| {
        create_text_layout_with_contexts(text, font_config, max_width, font_cx, layout_cx)
    })
}

/// 使用指定的上下文创建文本布局
pub fn create_text_layout_with_contexts(
    text: &str,
    style: &TextStyle,
    max_width: Option<f64>,
    font_cx: &mut FontContext,
    layout_cx: &mut LayoutContext<Color>,
) -> TextLayout {
    let mut builder = layout_cx.ranged_builder(font_cx, text, 1.0, true);

    let font_stack = to_parley_font_family(&style.font_family);
    builder.push_default(StyleProperty::FontFamily(font_stack));
    builder.push_default(StyleProperty::FontSize(style.font_size as f32));
    builder.push_default(StyleProperty::Brush(style.color));
    builder.push_default(StyleProperty::FontStyle(to_parley_font_style(
        &style.font_style,
    )));
    builder.push_default(StyleProperty::FontWeight(to_parley_font_weight(
        &style.font_weight,
    )));

    let mut layout = builder.build(text);
    layout.break_all_lines(max_width.map(|w| w as f32));
    layout.align(Alignment::Start, AlignmentOptions::default());

    let width = layout.width() as f64;
    let height = layout.height() as f64;
    let decoration_map = [(0..text.len(), style.decoration)];
    let lines = extract_lines_from_parley(&layout, text, &decoration_map);

    TextLayout {
        lines,
        width,
        height,
    }
}

/// 将多段不同样式的文本合并在一个 TextLayout 中。
pub fn layout_text(
    texts: &[(&str, &TextStyle)],
    max_width: Option<f64>,
    align: TextAlign,
) -> TextLayout {
    with_text_contexts(|font_cx, layout_cx| {
        layout_text_with_contexts(texts, max_width, align, font_cx, layout_cx)
    })
}

/// 使用指定的 FontContext 和 LayoutContext 创建多段样式文本布局。
pub fn layout_text_with_contexts(
    texts: &[(&str, &TextStyle)],
    max_width: Option<f64>,
    align: TextAlign,
    font_cx: &mut FontContext,
    layout_cx: &mut LayoutContext<Color>,
) -> TextLayout {
    if texts.is_empty() {
        return layout_text_with_contexts(
            &[("", &TextStyle::default())],
            max_width,
            align,
            font_cx,
            layout_cx,
        );
    }

    let mut combined = String::new();
    let mut ranges: Vec<(usize, usize)> = Vec::with_capacity(texts.len());
    for (text, _) in texts {
        let start = combined.len();
        combined.push_str(text);
        let end = combined.len();
        ranges.push((start, end));
    }

    let mut builder = layout_cx.ranged_builder(font_cx, &combined, 1.0, true);

    let first_style = &texts[0].1;
    let default_font_stack = to_parley_font_family(&first_style.font_family);
    builder.push_default(StyleProperty::FontFamily(default_font_stack));
    builder.push_default(StyleProperty::FontSize(first_style.font_size as f32));
    builder.push_default(StyleProperty::Brush(first_style.color));
    builder.push_default(StyleProperty::FontStyle(to_parley_font_style(
        &first_style.font_style,
    )));
    builder.push_default(StyleProperty::FontWeight(to_parley_font_weight(
        &first_style.font_weight,
    )));

    for (i, (_, style)) in texts.iter().enumerate().skip(1) {
        let (start, end) = ranges[i];
        if start >= end {
            continue;
        }
        builder.push(
            StyleProperty::FontFamily(to_parley_font_family(&style.font_family)),
            start..end,
        );
        builder.push(StyleProperty::FontSize(style.font_size as f32), start..end);
        builder.push(StyleProperty::Brush(style.color), start..end);
        builder.push(
            StyleProperty::FontStyle(to_parley_font_style(&style.font_style)),
            start..end,
        );
        builder.push(
            StyleProperty::FontWeight(to_parley_font_weight(&style.font_weight)),
            start..end,
        );
    }

    let mut layout = builder.build(&combined);
    layout.break_all_lines(max_width.map(|w| w as f32));

    let paragraph_align = match align {
        TextAlign::Left => Alignment::Start,
        TextAlign::Center => Alignment::Center,
        TextAlign::Right => Alignment::End,
    };
    layout.align(paragraph_align, AlignmentOptions::default());

    let width = layout.width() as f64;
    let height = layout.height() as f64;

    // 构建 decoration map
    let mut decoration_map: Vec<(std::ops::Range<usize>, TextDecoration)> = Vec::new();
    for (i, (_, style)) in texts.iter().enumerate() {
        let (start, end) = ranges[i];
        decoration_map.push((start..end, style.decoration));
    }
    let lines = extract_lines_from_parley(&layout, &combined, &decoration_map);

    TextLayout {
        lines,
        width,
        height,
    }
}

/// 将字体家族列表(Vec<String>)转换为 parley 的 FontFamily::List。
fn to_parley_font_family(families: &[String]) -> FontFamily<'static> {
    let names: Vec<FontFamilyName<'static>> = families
        .iter()
        .map(|f| match GenericFamily::parse(f) {
            Some(generic) => FontFamilyName::Generic(generic),
            None => FontFamilyName::Named(Cow::Owned(f.clone())),
        })
        .collect();
    FontFamily::List(Cow::Owned(names))
}

/// 将 font_style 字符串转换为 parley 的 FontStyle
fn to_parley_font_style(style: &str) -> ParleyFontStyle {
    match style.to_lowercase().as_str() {
        "italic" => ParleyFontStyle::Italic,
        "oblique" => ParleyFontStyle::Oblique(None),
        _ => ParleyFontStyle::Normal,
    }
}

/// 将 font_weight 字符串或数值转换为 parley 的 FontWeight
fn to_parley_font_weight(weight: &str) -> FontWeight {
    match weight.to_lowercase().as_str() {
        "normal" => FontWeight::NORMAL,
        "bold" => FontWeight::BOLD,
        "thin" | "100" => FontWeight::THIN,
        "extra_light" | "200" => FontWeight::EXTRA_LIGHT,
        "light" | "300" => FontWeight::LIGHT,
        "semi_light" | "350" => FontWeight::SEMI_LIGHT,
        "medium" | "500" => FontWeight::MEDIUM,
        "semi_bold" | "600" => FontWeight::SEMI_BOLD,
        "extra_bold" | "800" => FontWeight::EXTRA_BOLD,
        "black" | "900" => FontWeight::BLACK,
        _ => {
            if let Ok(val) = weight.parse::<f32>() {
                FontWeight::new(val)
            } else {
                FontWeight::NORMAL
            }
        }
    }
}