glyphweaveforge 0.1.3

Convert Markdown into PDF through an explicit Rust pipeline with minimal and Typst backends.
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
use typst::layout::PagedDocument;
use typst_as_lib::TypstEngine;
use typst_as_lib::typst_kit_options::TypstKitFontOptions;

use crate::adapters::render::plan::{ThemeProfile, page_spec, resolve_theme};
use crate::core::ports::{RenderBackend, RenderRequest, ResolvedAsset, ResourceStatus};
use crate::core::{Block, Document, ForgeError, Inline, Result, TableAlignment};

#[derive(Debug, Default)]
pub struct TypstPdfRenderer;

impl RenderBackend for TypstPdfRenderer {
    fn render(&self, document: &Document, request: &RenderRequest) -> Result<Vec<u8>> {
        let page_size = page_spec(request.page_size);
        let theme = resolve_theme(&request.theme);
        let mut assets = TypstAssetLibrary::default();
        let source = build_typst_source(
            document,
            page_size.width_mm,
            page_size.height_mm,
            &theme,
            &mut assets,
        )?;

        if let Ok(path) = std::env::var("GLYPHWEAVEFORGE_DEBUG_TYPST_PATH") {
            let _ = std::fs::write(path, &source);
        }

        let engine = TypstEngine::builder()
            .main_file(source)
            .search_fonts_with(
                TypstKitFontOptions::default()
                    .include_system_fonts(true)
                    .include_embedded_fonts(true),
            )
            .with_static_file_resolver(assets.files())
            .build();
        let warned = engine.compile::<PagedDocument>();
        let document = warned.output.map_err(|error| ForgeError::TypstCompile {
            message: error.to_string(),
        })?;

        typst_pdf::pdf(&document, &typst_pdf::PdfOptions::default()).map_err(|errors| {
            ForgeError::TypstExport {
                message: format!("{errors:?}"),
            }
        })
    }
}

#[derive(Debug, Default)]
struct TypstAssetLibrary {
    files: Vec<(String, Vec<u8>)>,
}

impl TypstAssetLibrary {
    fn register(&mut self, image: &ResolvedAsset) -> Result<String> {
        if image.status != ResourceStatus::Loaded {
            return Err(ForgeError::TypstAsset {
                target: image.original.clone(),
                message: image.message.clone(),
            });
        }

        let Some(bytes) = image.bytes.as_ref() else {
            return Err(ForgeError::TypstAsset {
                target: image.original.clone(),
                message: "resolved image bytes were empty".to_owned(),
            });
        };

        let extension = match image.format.unwrap_or("bin") {
            "jpeg" => "jpg",
            other => other,
        };
        let path = format!("assets/image-{}.{}", self.files.len(), extension);
        self.files.push((path.clone(), bytes.clone()));
        Ok(path)
    }

    fn files(&self) -> Vec<(&str, &[u8])> {
        self.files
            .iter()
            .map(|(path, bytes)| (path.as_str(), bytes.as_slice()))
            .collect()
    }
}

fn build_typst_source(
    document: &Document,
    page_width_mm: f32,
    page_height_mm: f32,
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let mut source = String::new();
    source.push_str(&format!(
        "#set page(width: {page_width_mm:.1}mm, height: {page_height_mm:.1}mm, margin: {margin:.1}mm)\n",
        margin = theme.margin_mm
    ));
    source.push_str(&format!(
        "#set text(font: \"{}\", size: {:.2}pt, fill: rgb(\"{}\"), lang: \"es\")\n",
        escape_typst_string(&theme.body_font),
        theme.body_font_size_pt,
        theme.body_color
    ));
    source.push_str("#set par(justify: true, leading: 0.68em)\n");
    source.push_str("#set raw(theme: auto)\n");
    source.push_str(&format!(
        "#show heading.where(level: 1): set text(font: \"{}\", size: {:.2}pt, weight: \"bold\", fill: rgb(\"{}\"))\n",
        escape_typst_string(&theme.heading_font),
        theme.body_font_size_pt * 2.15,
        theme.heading_color
    ));
    source.push_str("#show heading.where(level: 1): set block(above: 1.5em, below: 0.6em)\n");
    source.push_str(&format!(
        "#show heading.where(level: 2): set text(font: \"{}\", size: {:.2}pt, weight: \"bold\", fill: rgb(\"{}\"))\n",
        escape_typst_string(&theme.heading_font),
        theme.body_font_size_pt * 1.7,
        theme.heading_color
    ));
    source.push_str("#show heading.where(level: 2): set block(above: 1.1em, below: 0.6em)\n");
    source.push_str(&format!(
        "#show heading.where(level: 3): set text(font: \"{}\", size: {:.2}pt, weight: \"bold\", fill: rgb(\"{}\"))\n",
        escape_typst_string(&theme.heading_font),
        theme.body_font_size_pt * 1.4,
        theme.heading_color
    ));
    source.push_str("#show heading.where(level: 3): set block(above: 1.1em, below: 0.4em)\n");
    source.push_str(&format!(
        "#show strong: set text(weight: \"bold\", fill: rgb(\"{}\"))\n",
        theme.heading_color
    ));
    source.push_str("#show emph: set text(style: \"italic\")\n");
    source.push_str(&format!(
        "#show table.cell.where(y: 0): set text(weight: \"bold\", fill: rgb(\"{}\"))\n",
        theme.heading_color
    ));
    source.push_str(&format!(
        "#show link: set text(fill: rgb(\"{}\"))\n\n",
        theme.accent_color
    ));

    for block in &document.blocks {
        source.push_str(&render_block(block, theme, assets)?);
    }

    Ok(source)
}

fn render_block(
    block: &Block,
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    match block {
        Block::Heading { level, content } => render_heading(*level, content, theme, assets),
        Block::Paragraph { content } => {
            Ok(format!("{}\n\n", render_markup(content, theme, assets)?))
        }
        Block::List { ordered, items } => render_list(*ordered, items, theme, assets),
        Block::Table {
            alignments,
            headers,
            rows,
        } => render_table_block(alignments, headers, rows, theme, assets),
        Block::Quote { content } => Ok(format!(
            "#block(fill: rgb(\"{}\"), inset: 10pt, radius: 6pt, width: 100%)[{}]\n\n",
            theme.quote_background,
            render_markup(content, theme, assets)?
        )),
        Block::Code { language, code } => Ok(render_code_block(language.as_deref(), code, theme)),
        Block::Image { alt, asset } => render_image_block(alt, asset, assets),
        Block::MissingAsset {
            alt,
            target,
            message,
        } => Ok(render_notice_block(
            &format!("Missing image: {alt} ({target})"),
            message,
            theme,
        )),
        Block::Unsupported { kind, raw } => Ok(render_notice_block(
            &format!("Unsupported {kind}"),
            raw,
            theme,
        )),
        Block::ThematicBreak => Ok(format!(
            "#line(length: 100%, stroke: 0.6pt + rgb(\"{}\"))\n\n",
            theme.muted_color
        )),
    }
}

fn render_heading(
    level: u8,
    content: &[Inline],
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let level = level.clamp(1, 6) as usize;
    Ok(format!(
        "{} {}\n\n",
        "=".repeat(level),
        render_markup(content, theme, assets)?
    ))
}

fn render_list(
    ordered: bool,
    items: &[Vec<Inline>],
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let mut out = String::new();
    for item in items {
        let marker = if ordered { "+" } else { "-" };
        out.push_str(marker);
        out.push(' ');
        out.push_str(&render_markup(item, theme, assets)?);
        out.push('\n');
    }
    out.push('\n');
    Ok(out)
}

fn render_table_block(
    alignments: &[TableAlignment],
    headers: &[Vec<Inline>],
    rows: &[Vec<Vec<Inline>>],
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let column_count = headers
        .len()
        .max(rows.iter().map(|row| row.len()).max().unwrap_or(0));
    if column_count == 0 {
        return Ok(String::new());
    }

    let mut out = format!(
        "#table(columns: {column_count}, stroke: 0.5pt + rgb(\"{}\"), inset: 8pt, fill: (_, y) => if y == 0 {{ rgb(\"{}\") }} else if calc.rem(y, 2) == 1 {{ rgb(\"{}\") }} else {{ white }},\n",
        theme.muted_color, theme.quote_background, theme.code_background,
    );

    if !headers.is_empty() {
        out.push_str("  table.header");
        for (index, header) in headers.iter().enumerate() {
            out.push_str(&render_table_cell(
                header,
                alignments
                    .get(index)
                    .copied()
                    .unwrap_or(TableAlignment::None),
                theme,
                assets,
            )?);
        }
        out.push_str(",\n");
    }

    for row in rows {
        for (index, cell) in row.iter().enumerate() {
            out.push_str("  ");
            out.push_str(&render_table_cell(
                cell,
                alignments
                    .get(index)
                    .copied()
                    .unwrap_or(TableAlignment::None),
                theme,
                assets,
            )?);
            out.push_str(",\n");
        }

        for index in row.len()..column_count {
            out.push_str("  ");
            out.push_str(&render_table_cell(
                &[],
                alignments
                    .get(index)
                    .copied()
                    .unwrap_or(TableAlignment::None),
                theme,
                assets,
            )?);
            out.push_str(",\n");
        }
    }

    out.push_str(")\n\n");
    Ok(out)
}

fn render_code_block(language: Option<&str>, code: &str, theme: &ThemeProfile) -> String {
    let label = escape_markup_text(language.unwrap_or("text"));
    format!(
        "#block(above: 0.5em, below: 0.8em, width: 100%)[#text(size: {:.2}pt, fill: rgb(\"{}\"))[{}]]\n#block(fill: rgb(\"{}\"), inset: 10pt, radius: 6pt, width: 100%)[```{}\n{}\n```]\n\n",
        (theme.code_font_size_pt - 0.5).max(7.5),
        theme.muted_color,
        label,
        theme.code_background,
        label,
        code,
    )
}

fn render_image_block(
    alt: &str,
    asset: &ResolvedAsset,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let path = assets.register(asset)?;
    Ok(format!(
        "#figure(image(\"{}\", width: 92%), caption: [{}])\n\n",
        escape_typst_string(&path),
        escape_markup_text(alt)
    ))
}

fn render_notice_block(title: &str, body: &str, theme: &ThemeProfile) -> String {
    format!(
        "#block(fill: rgb(\"{}\"), inset: 10pt, radius: 6pt, width: 100%)[*{}* {}]\n\n",
        theme.quote_background,
        escape_markup_text(title),
        escape_markup_text(body)
    )
}

fn render_table_cell(
    content: &[Inline],
    alignment: TableAlignment,
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let body = render_markup(content, theme, assets)?;
    let cell = if body.is_empty() {
        "[]".to_owned()
    } else {
        format!("[{body}]")
    };

    Ok(match alignment {
        TableAlignment::None | TableAlignment::Left => cell,
        TableAlignment::Center => format!("[#align(center){cell}]"),
        TableAlignment::Right => format!("[#align(right){cell}]"),
    })
}

fn render_markup(
    inlines: &[Inline],
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    let mut out = String::new();
    for inline in inlines {
        out.push_str(&render_inline(inline, theme, assets)?);
    }
    Ok(out)
}

fn render_inline(
    inline: &Inline,
    theme: &ThemeProfile,
    assets: &mut TypstAssetLibrary,
) -> Result<String> {
    match inline {
        Inline::Text(text) => Ok(escape_markup_text(text)),
        Inline::Code(text) => Ok(format!(
            "#box(fill: rgb(\"{}\"), inset: (x: 0.22em, y: 0.08em), radius: 2pt)[`{}`]",
            theme.code_background,
            escape_code_markup(text)
        )),
        Inline::Emphasis(children) => Ok(format!("_{}_", render_markup(children, theme, assets)?)),
        Inline::Strong(children) => Ok(format!("*{}*", render_markup(children, theme, assets)?)),
        Inline::Link { label, target } => Ok(format!(
            "#link(\"{}\")[{}]",
            escape_typst_string(target),
            render_markup(label, theme, assets)?
        )),
        Inline::Image { alt, .. } => Ok(format!("[image: {}]", escape_markup_text(alt))),
        Inline::ResolvedImage { alt, asset } => {
            if asset.status == ResourceStatus::Loaded {
                let path = assets.register(asset)?;
                Ok(format!(
                    "#image(\"{}\", height: 1.2em)",
                    escape_typst_string(&path)
                ))
            } else {
                Ok(escape_markup_text(alt))
            }
        }
        Inline::SoftBreak => Ok(" ".to_owned()),
        Inline::HardBreak => Ok(" \\\n".to_owned()),
    }
}

fn escape_markup_text(text: &str) -> String {
    let mut escaped = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '\\' => escaped.push_str("\\\\"),
            '#' | '[' | ']' | '*' | '_' | '`' | '$' => {
                escaped.push('\\');
                escaped.push(ch);
            }
            '\n' | '\r' => escaped.push(' '),
            _ => escaped.push(ch),
        }
    }
    escaped
}

fn escape_code_markup(text: &str) -> String {
    text.replace('`', "\\`")
}

fn escape_typst_string(text: &str) -> String {
    let mut escaped = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '\\' => escaped.push_str("\\\\"),
            '"' => escaped.push_str("\\\""),
            '\n' => escaped.push_str("\\n"),
            '\r' => {}
            '\t' => escaped.push_str("\\t"),
            _ => escaped.push(ch),
        }
    }
    escaped
}