bare-x-to-pdf 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +9 -2
- package/LICENSE.md +198 -0
- package/NOTICE.md +13 -0
- package/README.md +46 -24
- package/binding.c +7 -11
- package/core/Cargo.lock +19 -234
- package/core/Cargo.toml +2 -2
- package/core/include/simple_converter.h +0 -1
- package/core/src/doc/mod.rs +2 -0
- package/core/src/docx/mod.rs +12 -1
- package/core/src/lib.rs +33 -11
- package/core/src/markdown/mod.rs +455 -0
- package/core/src/markdown/style.rs +191 -0
- package/core/src/model.rs +26 -40
- package/core/src/odf_chart.rs +240 -0
- package/core/src/odt/geometry.rs +483 -0
- package/core/src/odt/mod.rs +49 -11
- package/core/src/odt/odp.rs +440 -0
- package/core/src/odt/styles.rs +149 -28
- package/core/src/pptx/mod.rs +2 -2
- package/core/src/typst_backend/emit.rs +91 -15
- package/core/src/typst_backend/mod.rs +9 -1
- package/core/src/typst_backend/notes.rs +156 -0
- package/core/src/xlsx/biff.rs +999 -0
- package/core/src/xlsx/biff_art.rs +195 -0
- package/core/src/xlsx/biff_cf.rs +339 -0
- package/core/src/xlsx/biff_chart.rs +308 -0
- package/core/src/xlsx/mod.rs +234 -106
- package/core/src/xlsx/ods.rs +777 -0
- package/index.d.ts +24 -4
- package/index.js +34 -11
- package/package.json +33 -32
- package/cmake/pdfium.cmake +0 -75
- package/core/src/docx/writer.rs +0 -222
- package/core/src/pdf/mod.rs +0 -354
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
mod style;
|
|
2
|
+
|
|
3
|
+
use crate::error::Error;
|
|
4
|
+
use crate::model::*;
|
|
5
|
+
use pulldown_cmark::{Alignment, Event, Options, Parser, Tag, TagEnd};
|
|
6
|
+
use style::{Profile, TableCell};
|
|
7
|
+
|
|
8
|
+
pub fn read(bytes: &[u8]) -> Result<Document, Error> {
|
|
9
|
+
let text = String::from_utf8_lossy(bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes));
|
|
10
|
+
let profile = Profile::markdown();
|
|
11
|
+
let mut reader = Reader::new(&profile);
|
|
12
|
+
let options = Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TASKLISTS;
|
|
13
|
+
for event in Parser::new_ext(&text, options) {
|
|
14
|
+
reader.event(event);
|
|
15
|
+
}
|
|
16
|
+
reader.flush();
|
|
17
|
+
Ok(style::document(&profile, reader.blocks))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
struct ListState {
|
|
21
|
+
next: Option<u64>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
struct ItemState {
|
|
25
|
+
labelled: bool,
|
|
26
|
+
last_paragraph: Option<usize>,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
struct TableState {
|
|
30
|
+
alignments: Vec<Alignment>,
|
|
31
|
+
rows: Vec<Vec<TableCell>>,
|
|
32
|
+
in_head: bool,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#[derive(PartialEq)]
|
|
36
|
+
enum Pending {
|
|
37
|
+
None,
|
|
38
|
+
Paragraph,
|
|
39
|
+
Heading(usize),
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
struct Reader<'a> {
|
|
43
|
+
profile: &'a Profile,
|
|
44
|
+
blocks: Vec<Block>,
|
|
45
|
+
inlines: Vec<Inline>,
|
|
46
|
+
pending: Pending,
|
|
47
|
+
bold: usize,
|
|
48
|
+
italic: usize,
|
|
49
|
+
strike: usize,
|
|
50
|
+
link: usize,
|
|
51
|
+
quote: usize,
|
|
52
|
+
lists: Vec<ListState>,
|
|
53
|
+
items: Vec<ItemState>,
|
|
54
|
+
table: Option<TableState>,
|
|
55
|
+
cell: Option<Vec<Block>>,
|
|
56
|
+
code: Option<String>,
|
|
57
|
+
image: usize,
|
|
58
|
+
html: Option<String>,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fn align(a: Alignment) -> Option<Align> {
|
|
62
|
+
match a {
|
|
63
|
+
Alignment::Left | Alignment::None => None,
|
|
64
|
+
Alignment::Center => Some(Align::Center),
|
|
65
|
+
Alignment::Right => Some(Align::Right),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
impl<'a> Reader<'a> {
|
|
70
|
+
fn new(profile: &'a Profile) -> Self {
|
|
71
|
+
Reader {
|
|
72
|
+
profile,
|
|
73
|
+
blocks: Vec::new(),
|
|
74
|
+
inlines: Vec::new(),
|
|
75
|
+
pending: Pending::None,
|
|
76
|
+
bold: 0,
|
|
77
|
+
italic: 0,
|
|
78
|
+
strike: 0,
|
|
79
|
+
link: 0,
|
|
80
|
+
quote: 0,
|
|
81
|
+
lists: Vec::new(),
|
|
82
|
+
items: Vec::new(),
|
|
83
|
+
table: None,
|
|
84
|
+
cell: None,
|
|
85
|
+
code: None,
|
|
86
|
+
image: 0,
|
|
87
|
+
html: None,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn run(&self) -> RunProps {
|
|
92
|
+
let mut run = match &self.pending {
|
|
93
|
+
Pending::Heading(level) => self.profile.heading(*level).0,
|
|
94
|
+
_ => self.profile.body.clone(),
|
|
95
|
+
};
|
|
96
|
+
if self.bold > 0 || (self.table.as_ref().is_some_and(|t| t.in_head) && self.cell.is_some()) {
|
|
97
|
+
run.bold = Some(true);
|
|
98
|
+
}
|
|
99
|
+
if self.italic > 0 {
|
|
100
|
+
run.italic = Some(true);
|
|
101
|
+
}
|
|
102
|
+
if self.strike > 0 {
|
|
103
|
+
run.strike = Some(true);
|
|
104
|
+
}
|
|
105
|
+
if self.link > 0 {
|
|
106
|
+
run.color = Some(style::LINK);
|
|
107
|
+
run.underline = Some(true);
|
|
108
|
+
}
|
|
109
|
+
run
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fn quote_indent(&self) -> f64 {
|
|
113
|
+
if self.quote > 0 { 28.346 } else { 0.0 }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn text(&mut self, text: &str) {
|
|
117
|
+
if self.image > 0 {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if self.pending == Pending::None {
|
|
121
|
+
self.pending = Pending::Paragraph;
|
|
122
|
+
}
|
|
123
|
+
let props = self.run();
|
|
124
|
+
self.inlines.push(Inline::Text { text: text.to_string(), props });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
fn push(&mut self, block: Block) {
|
|
128
|
+
match self.cell.as_mut() {
|
|
129
|
+
Some(cell) => cell.push(block),
|
|
130
|
+
None => self.blocks.push(block),
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn flush(&mut self) {
|
|
135
|
+
let pending = std::mem::replace(&mut self.pending, Pending::None);
|
|
136
|
+
if pending == Pending::None && self.inlines.is_empty() {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
let inlines = std::mem::take(&mut self.inlines);
|
|
140
|
+
let mark = inlines
|
|
141
|
+
.iter()
|
|
142
|
+
.rev()
|
|
143
|
+
.find_map(|i| if let Inline::Text { props, .. } = i { Some(props.clone()) } else { None })
|
|
144
|
+
.unwrap_or_else(|| self.profile.body.clone());
|
|
145
|
+
if self.cell.is_some() {
|
|
146
|
+
let alignment = self.table.as_ref().and_then(|t| {
|
|
147
|
+
let column = self.cell_index();
|
|
148
|
+
t.alignments.get(column).copied().and_then(align)
|
|
149
|
+
});
|
|
150
|
+
self.push(style::paragraph(style::cell_props(alignment), mark, inlines, None));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if let Pending::Heading(level) = pending {
|
|
154
|
+
let (_, props) = self.profile.heading(level);
|
|
155
|
+
self.push(style::paragraph(props, mark, inlines, None));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
let mut props = self.profile.body_props();
|
|
159
|
+
let quote_indent = self.quote_indent();
|
|
160
|
+
if self.quote > 0 {
|
|
161
|
+
props.indent_left = Some(quote_indent);
|
|
162
|
+
props.indent_right = Some(quote_indent);
|
|
163
|
+
props.line_spacing = Some(LineSpacing::Multiple(self.profile.quote_line));
|
|
164
|
+
props.space_after = Some(self.profile.quote_after);
|
|
165
|
+
}
|
|
166
|
+
let mut list = None;
|
|
167
|
+
if let Some(item) = self.items.last_mut() {
|
|
168
|
+
let depth = self.lists.len();
|
|
169
|
+
let text = if item.labelled {
|
|
170
|
+
None
|
|
171
|
+
} else {
|
|
172
|
+
item.labelled = true;
|
|
173
|
+
Some(match self.lists.last_mut().and_then(|l| l.next.as_mut()) {
|
|
174
|
+
Some(n) => {
|
|
175
|
+
let label = format!("{n}.");
|
|
176
|
+
*n += 1;
|
|
177
|
+
label
|
|
178
|
+
}
|
|
179
|
+
None => "\u{2022}".to_string(),
|
|
180
|
+
})
|
|
181
|
+
};
|
|
182
|
+
let (_, text_at, hanging) = style::list_label("", &self.profile.body, depth);
|
|
183
|
+
props.indent_left = Some(text_at + quote_indent);
|
|
184
|
+
if self.quote == 0 {
|
|
185
|
+
props.space_after = Some(self.profile.list_item_after);
|
|
186
|
+
}
|
|
187
|
+
if let Some(label) = text {
|
|
188
|
+
props.indent_hanging = Some(hanging);
|
|
189
|
+
props.indent_first_line = Some(0.0);
|
|
190
|
+
list = Some(style::list_label(&label, &self.profile.body, depth).0);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
let index = self.blocks.len();
|
|
194
|
+
self.push(style::paragraph(props, mark, inlines, list));
|
|
195
|
+
if let Some(item) = self.items.last_mut() {
|
|
196
|
+
item.last_paragraph = Some(index);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fn cell_index(&self) -> usize {
|
|
201
|
+
self.table.as_ref().and_then(|t| t.rows.last()).map_or(0, Vec::len)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fn event(&mut self, event: Event<'_>) {
|
|
205
|
+
if let Some(html) = self.html.as_mut() {
|
|
206
|
+
match event {
|
|
207
|
+
Event::Html(t) => html.push_str(&t),
|
|
208
|
+
Event::End(TagEnd::HtmlBlock) => self.html_block(),
|
|
209
|
+
_ => {}
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if let Some(code) = self.code.as_mut() {
|
|
214
|
+
match event {
|
|
215
|
+
Event::Text(t) => code.push_str(&t),
|
|
216
|
+
Event::End(TagEnd::CodeBlock) => self.code_block(),
|
|
217
|
+
_ => {}
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
match event {
|
|
222
|
+
Event::Start(tag) => self.start(tag),
|
|
223
|
+
Event::End(tag) => self.end(tag),
|
|
224
|
+
Event::Text(t) => self.text(&t),
|
|
225
|
+
Event::Code(t) => {
|
|
226
|
+
if self.pending == Pending::None {
|
|
227
|
+
self.pending = Pending::Paragraph;
|
|
228
|
+
}
|
|
229
|
+
let mut props = self.run();
|
|
230
|
+
props.font = Some(style::MONO.into());
|
|
231
|
+
self.inlines.push(Inline::Text { text: t.to_string(), props });
|
|
232
|
+
}
|
|
233
|
+
Event::SoftBreak => self.text(" "),
|
|
234
|
+
Event::HardBreak => self.inlines.push(Inline::LineBreak),
|
|
235
|
+
Event::Rule => {
|
|
236
|
+
self.flush();
|
|
237
|
+
let rule = style::rule(self.profile);
|
|
238
|
+
self.push(rule);
|
|
239
|
+
}
|
|
240
|
+
Event::TaskListMarker(checked) => {
|
|
241
|
+
if self.pending == Pending::None {
|
|
242
|
+
self.pending = Pending::Paragraph;
|
|
243
|
+
}
|
|
244
|
+
let props = self.run();
|
|
245
|
+
self.inlines.push(Inline::Text { text: if checked { "\u{2612}".into() } else { "\u{25A1}".into() }, props });
|
|
246
|
+
self.text(" ");
|
|
247
|
+
}
|
|
248
|
+
_ => {}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
fn start(&mut self, tag: Tag<'_>) {
|
|
253
|
+
match tag {
|
|
254
|
+
Tag::Paragraph => {
|
|
255
|
+
self.flush();
|
|
256
|
+
self.pending = Pending::Paragraph;
|
|
257
|
+
}
|
|
258
|
+
Tag::Heading { level, .. } => {
|
|
259
|
+
self.flush();
|
|
260
|
+
self.pending = Pending::Heading(level as usize);
|
|
261
|
+
}
|
|
262
|
+
Tag::BlockQuote(_) => {
|
|
263
|
+
self.flush();
|
|
264
|
+
self.quote += 1;
|
|
265
|
+
}
|
|
266
|
+
Tag::CodeBlock(_) => {
|
|
267
|
+
self.flush();
|
|
268
|
+
self.code = Some(String::new());
|
|
269
|
+
}
|
|
270
|
+
Tag::List(start) => {
|
|
271
|
+
self.flush();
|
|
272
|
+
if let Some(item) = self.items.last() {
|
|
273
|
+
if let Some(index) = item.last_paragraph {
|
|
274
|
+
if let Some(Block::Paragraph(p)) = self.blocks.get_mut(index) {
|
|
275
|
+
p.props.space_after = Some(0.0);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
self.lists.push(ListState { next: start });
|
|
280
|
+
}
|
|
281
|
+
Tag::Item => {
|
|
282
|
+
self.flush();
|
|
283
|
+
self.items.push(ItemState { labelled: false, last_paragraph: None });
|
|
284
|
+
}
|
|
285
|
+
Tag::Table(alignments) => {
|
|
286
|
+
self.flush();
|
|
287
|
+
self.table = Some(TableState { alignments, rows: Vec::new(), in_head: false });
|
|
288
|
+
}
|
|
289
|
+
Tag::TableHead => {
|
|
290
|
+
if let Some(t) = self.table.as_mut() {
|
|
291
|
+
t.in_head = true;
|
|
292
|
+
t.rows.push(Vec::new());
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
Tag::TableRow => {
|
|
296
|
+
if let Some(t) = self.table.as_mut() {
|
|
297
|
+
t.rows.push(Vec::new());
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
Tag::TableCell => self.cell = Some(Vec::new()),
|
|
301
|
+
Tag::Emphasis => self.italic += 1,
|
|
302
|
+
Tag::Strong => self.bold += 1,
|
|
303
|
+
Tag::Strikethrough => self.strike += 1,
|
|
304
|
+
Tag::Link { .. } => self.link += 1,
|
|
305
|
+
Tag::Image { dest_url, .. } => {
|
|
306
|
+
self.image += 1;
|
|
307
|
+
if let Some(drawing) = data_image(&dest_url, self.profile.text_width()) {
|
|
308
|
+
if self.pending == Pending::None {
|
|
309
|
+
self.pending = Pending::Paragraph;
|
|
310
|
+
}
|
|
311
|
+
self.inlines.push(Inline::Drawing(drawing));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
Tag::HtmlBlock => {
|
|
315
|
+
self.flush();
|
|
316
|
+
self.html = Some(String::new());
|
|
317
|
+
}
|
|
318
|
+
_ => {}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
fn end(&mut self, tag: TagEnd) {
|
|
323
|
+
match tag {
|
|
324
|
+
TagEnd::Paragraph | TagEnd::Heading(_) => self.flush(),
|
|
325
|
+
TagEnd::BlockQuote(_) => {
|
|
326
|
+
self.flush();
|
|
327
|
+
self.quote = self.quote.saturating_sub(1);
|
|
328
|
+
}
|
|
329
|
+
TagEnd::List(_) => {
|
|
330
|
+
self.flush();
|
|
331
|
+
self.lists.pop();
|
|
332
|
+
}
|
|
333
|
+
TagEnd::Item => {
|
|
334
|
+
self.flush();
|
|
335
|
+
self.items.pop();
|
|
336
|
+
}
|
|
337
|
+
TagEnd::TableCell => {
|
|
338
|
+
self.flush();
|
|
339
|
+
let blocks = self.cell.take().unwrap_or_default();
|
|
340
|
+
let alignment = self.table.as_ref().and_then(|t| t.alignments.get(self.cell_index()).copied().and_then(align));
|
|
341
|
+
if let Some(row) = self.table.as_mut().and_then(|t| t.rows.last_mut()) {
|
|
342
|
+
row.push(TableCell { blocks, align: alignment });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
TagEnd::TableHead => {
|
|
346
|
+
if let Some(t) = self.table.as_mut() {
|
|
347
|
+
t.in_head = false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
TagEnd::Table => {
|
|
351
|
+
if let Some(t) = self.table.take() {
|
|
352
|
+
let rows: Vec<Vec<TableCell>> = t.rows.into_iter().filter(|r| !r.is_empty()).collect();
|
|
353
|
+
let table = style::table(self.profile, rows);
|
|
354
|
+
self.push(table);
|
|
355
|
+
let gap = style::empty_body(self.profile);
|
|
356
|
+
self.push(gap);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
TagEnd::Emphasis => self.italic = self.italic.saturating_sub(1),
|
|
360
|
+
TagEnd::Strong => self.bold = self.bold.saturating_sub(1),
|
|
361
|
+
TagEnd::Strikethrough => self.strike = self.strike.saturating_sub(1),
|
|
362
|
+
TagEnd::Link => self.link = self.link.saturating_sub(1),
|
|
363
|
+
TagEnd::Image => self.image = self.image.saturating_sub(1),
|
|
364
|
+
_ => {}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
fn code_block(&mut self) {
|
|
369
|
+
let code = self.code.take().unwrap_or_default();
|
|
370
|
+
let run = self.profile.code_run();
|
|
371
|
+
let mut inlines = Vec::new();
|
|
372
|
+
for (i, line) in code.trim_end_matches('\n').split('\n').enumerate() {
|
|
373
|
+
if i > 0 {
|
|
374
|
+
inlines.push(Inline::LineBreak);
|
|
375
|
+
}
|
|
376
|
+
let text = if line.is_empty() { " " } else { line };
|
|
377
|
+
inlines.push(Inline::Text { text: text.to_string(), props: run.clone() });
|
|
378
|
+
}
|
|
379
|
+
let list_indent = if self.items.is_empty() { 0.0 } else { style::list_indents(self.lists.len()).1 };
|
|
380
|
+
let indent = self.quote_indent() + list_indent;
|
|
381
|
+
let props = ParagraphProps {
|
|
382
|
+
space_before: Some(0.0),
|
|
383
|
+
space_after: Some(self.profile.code_after),
|
|
384
|
+
line_spacing: Some(LineSpacing::Multiple(1.0)),
|
|
385
|
+
shading: self.profile.code_shading,
|
|
386
|
+
indent_left: (indent > 0.0).then_some(indent),
|
|
387
|
+
..ParagraphProps::default()
|
|
388
|
+
};
|
|
389
|
+
self.push(style::paragraph(props, run, inlines, None));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
fn html_block(&mut self) {
|
|
393
|
+
let html = self.html.take().unwrap_or_default();
|
|
394
|
+
let mut text = String::new();
|
|
395
|
+
let mut in_tag = false;
|
|
396
|
+
for ch in html.chars() {
|
|
397
|
+
match ch {
|
|
398
|
+
'<' => in_tag = true,
|
|
399
|
+
'>' if in_tag => in_tag = false,
|
|
400
|
+
_ if !in_tag => text.push(ch),
|
|
401
|
+
_ => {}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
let text = text.split_whitespace().collect::<Vec<_>>().join(" ");
|
|
405
|
+
if text.is_empty() {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
let mut props = self.profile.body_props();
|
|
409
|
+
props.space_after = Some(0.0);
|
|
410
|
+
let run = self.profile.body.clone();
|
|
411
|
+
self.push(style::paragraph(props, run.clone(), vec![Inline::Text { text, props: run.clone() }], None));
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn data_image(url: &str, max_width: f64) -> Option<Drawing> {
|
|
416
|
+
let rest = url.strip_prefix("data:")?;
|
|
417
|
+
let (meta, payload) = rest.split_once(',')?;
|
|
418
|
+
if !meta.ends_with(";base64") {
|
|
419
|
+
return None;
|
|
420
|
+
}
|
|
421
|
+
let data = crate::odt::base64_decode(payload)?;
|
|
422
|
+
let format = ImageFormat::sniff(&data)?;
|
|
423
|
+
let (w, h) = pixel_size(&data)?;
|
|
424
|
+
let (mut width, mut height) = (w as f64 * 0.75, h as f64 * 0.75);
|
|
425
|
+
if width > max_width {
|
|
426
|
+
height *= max_width / width;
|
|
427
|
+
width = max_width;
|
|
428
|
+
}
|
|
429
|
+
Some(Drawing::new(width, height, DrawingContent::Image(ImageData { data, format })))
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn pixel_size(data: &[u8]) -> Option<(u32, u32)> {
|
|
433
|
+
if data.starts_with(&[0x89, b'P', b'N', b'G']) {
|
|
434
|
+
let w = u32::from_be_bytes(data.get(16..20)?.try_into().ok()?);
|
|
435
|
+
let h = u32::from_be_bytes(data.get(20..24)?.try_into().ok()?);
|
|
436
|
+
return Some((w, h));
|
|
437
|
+
}
|
|
438
|
+
if data.starts_with(&[0xFF, 0xD8]) {
|
|
439
|
+
let mut pos = 2;
|
|
440
|
+
while pos + 9 < data.len() {
|
|
441
|
+
if data[pos] != 0xFF {
|
|
442
|
+
return None;
|
|
443
|
+
}
|
|
444
|
+
let marker = data[pos + 1];
|
|
445
|
+
let len = u16::from_be_bytes([data[pos + 2], data[pos + 3]]) as usize;
|
|
446
|
+
if matches!(marker, 0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF) {
|
|
447
|
+
let h = u16::from_be_bytes([data[pos + 5], data[pos + 6]]) as u32;
|
|
448
|
+
let w = u16::from_be_bytes([data[pos + 7], data[pos + 8]]) as u32;
|
|
449
|
+
return Some((w, h));
|
|
450
|
+
}
|
|
451
|
+
pos += 2 + len;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
None
|
|
455
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
use crate::model::*;
|
|
2
|
+
|
|
3
|
+
const CM: f64 = 72.0 / 2.54;
|
|
4
|
+
|
|
5
|
+
pub struct Profile {
|
|
6
|
+
pub body: RunProps,
|
|
7
|
+
pub body_line: f64,
|
|
8
|
+
pub body_after: f64,
|
|
9
|
+
pub heading_font: &'static str,
|
|
10
|
+
pub heading_sizes: [f64; 6],
|
|
11
|
+
pub heading_italic: [bool; 6],
|
|
12
|
+
pub heading_before: [f64; 6],
|
|
13
|
+
pub heading_after: [f64; 6],
|
|
14
|
+
pub list_item_after: f64,
|
|
15
|
+
pub quote_after: f64,
|
|
16
|
+
pub quote_line: f64,
|
|
17
|
+
pub code_size: f64,
|
|
18
|
+
pub code_shading: Option<Color>,
|
|
19
|
+
pub code_after: f64,
|
|
20
|
+
pub table_cell_margin: f64,
|
|
21
|
+
pub margins: Margins,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub const MONO: &str = "Liberation Mono";
|
|
25
|
+
pub const LINK: Color = Color(0, 0, 0x80);
|
|
26
|
+
|
|
27
|
+
impl Profile {
|
|
28
|
+
pub fn markdown() -> Profile {
|
|
29
|
+
Profile {
|
|
30
|
+
body: RunProps { font: Some("Liberation Serif".into()), size: Some(12.0), color: Some(Color(0, 0, 0)), ..RunProps::default() },
|
|
31
|
+
body_line: 1.15,
|
|
32
|
+
body_after: 0.247 * CM,
|
|
33
|
+
heading_font: "Liberation Sans",
|
|
34
|
+
heading_sizes: [18.0, 16.0, 14.0, 13.0, 12.0, 12.0],
|
|
35
|
+
heading_italic: [false, false, false, true, false, true],
|
|
36
|
+
heading_before: [0.423 * CM, 0.353 * CM, 0.247 * CM, 0.212 * CM, 0.212 * CM, 0.106 * CM],
|
|
37
|
+
heading_after: [0.212 * CM, 0.212 * CM, 0.212 * CM, 0.212 * CM, 0.106 * CM, 0.106 * CM],
|
|
38
|
+
list_item_after: 0.247 * CM,
|
|
39
|
+
quote_after: 0.5 * CM,
|
|
40
|
+
quote_line: 1.0,
|
|
41
|
+
code_size: 10.0,
|
|
42
|
+
code_shading: Some(Color(0xE1, 0xE1, 0xE1)),
|
|
43
|
+
code_after: 0.5 * CM,
|
|
44
|
+
table_cell_margin: 0.1 * CM,
|
|
45
|
+
margins: Margins { top: 2.0 * CM, bottom: 2.0 * CM, left: 2.0 * CM, right: 2.0 * CM, header: 0.0, footer: 0.0 },
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
pub fn text_width(&self) -> f64 {
|
|
50
|
+
612.0 - self.margins.left - self.margins.right
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
pub fn heading(&self, level: usize) -> (RunProps, ParagraphProps) {
|
|
54
|
+
let i = level.clamp(1, 6) - 1;
|
|
55
|
+
let run = RunProps {
|
|
56
|
+
font: Some(self.heading_font.into()),
|
|
57
|
+
size: Some(self.heading_sizes[i]),
|
|
58
|
+
bold: Some(true),
|
|
59
|
+
italic: Some(self.heading_italic[i]),
|
|
60
|
+
color: Some(Color(0, 0, 0)),
|
|
61
|
+
..RunProps::default()
|
|
62
|
+
};
|
|
63
|
+
let props = ParagraphProps {
|
|
64
|
+
space_before: Some(self.heading_before[i]),
|
|
65
|
+
space_after: Some(self.heading_after[i]),
|
|
66
|
+
line_spacing: Some(LineSpacing::Multiple(1.0)),
|
|
67
|
+
keep_next: Some(true),
|
|
68
|
+
..ParagraphProps::default()
|
|
69
|
+
};
|
|
70
|
+
(run, props)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
pub fn body_props(&self) -> ParagraphProps {
|
|
74
|
+
ParagraphProps {
|
|
75
|
+
space_before: Some(0.0),
|
|
76
|
+
space_after: Some(self.body_after),
|
|
77
|
+
line_spacing: Some(LineSpacing::Multiple(self.body_line)),
|
|
78
|
+
..ParagraphProps::default()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
pub fn code_run(&self) -> RunProps {
|
|
83
|
+
RunProps { font: Some(MONO.into()), size: Some(self.code_size), ..self.body.clone() }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
pub fn page(&self) -> PageSetup {
|
|
87
|
+
PageSetup { width: 612.0, height: 792.0, margin: self.margins.clone() }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pub fn list_indents(depth: usize) -> (f64, f64) {
|
|
92
|
+
let label = 0.75 * CM + depth.saturating_sub(1) as f64 * 1.25 * CM;
|
|
93
|
+
(label, label + 0.5 * CM)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
pub fn list_label(text: &str, run: &RunProps, depth: usize) -> (ListLabel, f64, f64) {
|
|
97
|
+
let (label_at, text_at) = list_indents(depth);
|
|
98
|
+
let mut props = run.clone();
|
|
99
|
+
props.bold = None;
|
|
100
|
+
props.italic = None;
|
|
101
|
+
props.underline = None;
|
|
102
|
+
props.strike = None;
|
|
103
|
+
(ListLabel { text: text.into(), props, suffix: ListSuffix::Tab, tab_pos: Some(text_at) }, text_at, text_at - label_at)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
pub fn paragraph(props: ParagraphProps, mark: RunProps, inlines: Vec<Inline>, list: Option<ListLabel>) -> Block {
|
|
107
|
+
Block::Paragraph(Paragraph { props, mark, inlines, anchors: Vec::new(), list })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
pub fn rule(profile: &Profile) -> Block {
|
|
111
|
+
let mark = RunProps { size: Some(6.0), ..profile.body.clone() };
|
|
112
|
+
let props = ParagraphProps {
|
|
113
|
+
space_before: Some(0.0),
|
|
114
|
+
space_after: Some(0.5 * CM),
|
|
115
|
+
line_spacing: Some(LineSpacing::Multiple(1.0)),
|
|
116
|
+
borders: Borders {
|
|
117
|
+
bottom: BorderSide::Line { width: 0.5, color: Color(0x80, 0x80, 0x80), style: LineStyle::Solid },
|
|
118
|
+
..Borders::default()
|
|
119
|
+
},
|
|
120
|
+
..ParagraphProps::default()
|
|
121
|
+
};
|
|
122
|
+
paragraph(props, mark, Vec::new(), None)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
pub fn empty_body(profile: &Profile) -> Block {
|
|
126
|
+
paragraph(profile.body_props(), profile.body.clone(), Vec::new(), None)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
pub struct TableCell {
|
|
130
|
+
pub blocks: Vec<Block>,
|
|
131
|
+
pub align: Option<Align>,
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
pub fn table(profile: &Profile, rows: Vec<Vec<TableCell>>) -> Block {
|
|
135
|
+
let columns = rows.iter().map(Vec::len).max().unwrap_or(0).max(1);
|
|
136
|
+
let width = profile.text_width();
|
|
137
|
+
let margin = profile.table_cell_margin;
|
|
138
|
+
let widths = vec![width / columns as f64; columns];
|
|
139
|
+
let line = BorderSide::Line { width: 0.5, color: Color(0, 0, 0), style: LineStyle::Solid };
|
|
140
|
+
let borders = Borders { top: line, left: line, bottom: line, right: line, inside_h: line, inside_v: line };
|
|
141
|
+
let model_rows = rows
|
|
142
|
+
.into_iter()
|
|
143
|
+
.map(|cells| Row {
|
|
144
|
+
cells: cells
|
|
145
|
+
.into_iter()
|
|
146
|
+
.map(|cell| Cell {
|
|
147
|
+
blocks: if cell.blocks.is_empty() { vec![empty_cell(profile)] } else { cell.blocks },
|
|
148
|
+
span: 1,
|
|
149
|
+
borders,
|
|
150
|
+
halign: cell.align,
|
|
151
|
+
..Cell::default()
|
|
152
|
+
})
|
|
153
|
+
.collect(),
|
|
154
|
+
height: None,
|
|
155
|
+
exact_height: false,
|
|
156
|
+
})
|
|
157
|
+
.collect();
|
|
158
|
+
Block::Table(Table {
|
|
159
|
+
columns: widths,
|
|
160
|
+
rows: model_rows,
|
|
161
|
+
borders,
|
|
162
|
+
indent: 0.0,
|
|
163
|
+
cell_margins: CellMargins { top: Some(margin - 0.085), left: Some(margin), bottom: Some(margin - 0.085), right: Some(margin) },
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
fn empty_cell(profile: &Profile) -> Block {
|
|
168
|
+
paragraph(cell_props(None), profile.body.clone(), Vec::new(), None)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
pub fn cell_props(align: Option<Align>) -> ParagraphProps {
|
|
172
|
+
ParagraphProps {
|
|
173
|
+
align,
|
|
174
|
+
space_before: Some(0.0),
|
|
175
|
+
space_after: Some(0.0),
|
|
176
|
+
line_spacing: Some(LineSpacing::Multiple(1.0)),
|
|
177
|
+
..ParagraphProps::default()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
pub fn document(profile: &Profile, blocks: Vec<Block>) -> Document {
|
|
182
|
+
let blocks = if blocks.is_empty() { vec![empty_body(profile)] } else { blocks };
|
|
183
|
+
Document {
|
|
184
|
+
sections: vec![Section { page: profile.page(), blocks, columns: 1, content_scale: 1.0, ..Section::default() }],
|
|
185
|
+
default_tab: 1.25 * CM,
|
|
186
|
+
additive_spacing: true,
|
|
187
|
+
tabs_relative_to_indent: true,
|
|
188
|
+
writer_text_offset: true,
|
|
189
|
+
..Document::default()
|
|
190
|
+
}
|
|
191
|
+
}
|