bare-x-to-pdf 0.3.0 → 0.4.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/README.md +40 -5
- package/binding.c +236 -40
- package/cmake/rust.cmake +2 -2
- package/core/src/doc/mod.rs +29 -16
- package/core/src/docx/mod.rs +3 -5
- package/core/src/docx/numbering.rs +8 -4
- package/core/src/lib.rs +13 -9
- package/core/src/limits.rs +42 -0
- package/core/src/markdown/mod.rs +6 -0
- package/core/src/odt/geometry.rs +21 -0
- package/core/src/odt/mod.rs +13 -9
- package/core/src/odt/odp.rs +3 -5
- package/core/src/odt/styles.rs +2 -2
- package/core/src/pptx/mod.rs +23 -8
- package/core/src/pptx/text.rs +2 -2
- package/core/src/typst_backend/emit.rs +42 -19
- package/core/src/typst_backend/fonts.rs +1 -1
- package/core/src/typst_backend/images.rs +76 -0
- package/core/src/typst_backend/mod.rs +48 -6
- package/core/src/xlsx/biff.rs +15 -4
- package/core/src/xlsx/biff_art.rs +8 -2
- package/core/src/xlsx/biff_chart.rs +10 -1
- package/core/src/xlsx/formula.rs +30 -3
- package/core/src/xlsx/mod.rs +90 -24
- package/core/src/xlsx/ods.rs +15 -12
- package/core/src/xml.rs +6 -1
- package/index.d.ts +3 -1
- package/index.js +130 -38
- package/package.json +16 -13
package/core/src/lib.rs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
pub mod doc;
|
|
2
2
|
pub mod docx;
|
|
3
3
|
pub mod error;
|
|
4
|
+
pub mod limits;
|
|
4
5
|
pub mod markdown;
|
|
5
6
|
pub mod model;
|
|
6
7
|
pub mod odf_chart;
|
|
@@ -34,39 +35,39 @@ pub fn convert(input: &[u8], from: &str, to: &str, options: &Options) -> Result<
|
|
|
34
35
|
match (from, to) {
|
|
35
36
|
("docx" | "docm" | "dotx" | "dotm", "pdf") => {
|
|
36
37
|
let document = docx::read(input)?;
|
|
37
|
-
typst_backend::render_pdf(
|
|
38
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
38
39
|
}
|
|
39
40
|
("doc" | "dot", "pdf") => {
|
|
40
41
|
let document = doc::read(input)?;
|
|
41
|
-
typst_backend::render_pdf(
|
|
42
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
42
43
|
}
|
|
43
44
|
("odt" | "ott" | "fodt", "pdf") => {
|
|
44
45
|
let document = odt::read(input)?;
|
|
45
|
-
typst_backend::render_pdf(
|
|
46
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
46
47
|
}
|
|
47
48
|
("xlsx" | "xlsm" | "xltx" | "xltm", "pdf") => {
|
|
48
49
|
let document = xlsx::read(input)?;
|
|
49
|
-
typst_backend::render_pdf(
|
|
50
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
50
51
|
}
|
|
51
52
|
("odp" | "otp" | "fodp", "pdf") => {
|
|
52
53
|
let document = odt::read_odp(input)?;
|
|
53
|
-
typst_backend::render_pdf(
|
|
54
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
54
55
|
}
|
|
55
56
|
("ods" | "ots" | "fods", "pdf") => {
|
|
56
57
|
let document = xlsx::read_ods(input)?;
|
|
57
|
-
typst_backend::render_pdf(
|
|
58
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
58
59
|
}
|
|
59
60
|
("xls" | "xlt", "pdf") => {
|
|
60
61
|
let document = xlsx::read_xls(input)?;
|
|
61
|
-
typst_backend::render_pdf(
|
|
62
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
62
63
|
}
|
|
63
64
|
("md" | "markdown", "pdf") => {
|
|
64
65
|
let document = markdown::read(input)?;
|
|
65
|
-
typst_backend::render_pdf(
|
|
66
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
66
67
|
}
|
|
67
68
|
("pptx", "pdf") => {
|
|
68
69
|
let document = pptx::read(input)?;
|
|
69
|
-
typst_backend::render_pdf(
|
|
70
|
+
typst_backend::render_pdf(document, options.fonts_dir)
|
|
70
71
|
}
|
|
71
72
|
_ => Err(Error::new(format!("conversion from {from} to {to} is not supported"))),
|
|
72
73
|
}
|
|
@@ -131,6 +132,9 @@ pub unsafe extern "C" fn sc_convert(
|
|
|
131
132
|
*out = Buffer::empty();
|
|
132
133
|
*error = Buffer::empty();
|
|
133
134
|
|
|
135
|
+
static QUIET_PANICS: std::sync::Once = std::sync::Once::new();
|
|
136
|
+
QUIET_PANICS.call_once(|| panic::set_hook(Box::new(|_| {})));
|
|
137
|
+
|
|
134
138
|
let result = panic::catch_unwind(AssertUnwindSafe(|| {
|
|
135
139
|
let input: &[u8] = if input.is_null() {
|
|
136
140
|
&[]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
use crate::error::Error;
|
|
2
|
+
use std::io::Read;
|
|
3
|
+
|
|
4
|
+
pub const MAX_XML_DEPTH: usize = 512;
|
|
5
|
+
pub const MAX_FORMULA_DEPTH: usize = 256;
|
|
6
|
+
pub const MAX_ZIP_ENTRY_BYTES: u64 = 128 * 1024 * 1024;
|
|
7
|
+
pub const MAX_TABLE_SPAN: usize = 1000;
|
|
8
|
+
pub const MAX_TABLE_DEPTH: u32 = 12;
|
|
9
|
+
pub const MAX_TEXT_COLUMNS: usize = 64;
|
|
10
|
+
pub const MAX_REPEATED_SPACES: usize = 1000;
|
|
11
|
+
pub const MAX_CHART_POINTS: usize = 100_000;
|
|
12
|
+
pub const MAX_LIST_VALUE: i64 = 32_767;
|
|
13
|
+
pub const MAX_IMAGE_PIXELS: u64 = 50_000_000;
|
|
14
|
+
pub const MAX_IMAGE_BYTES: usize = 256 * 1024 * 1024;
|
|
15
|
+
pub const MAX_RANGE_CELLS: u64 = 1_000_000;
|
|
16
|
+
pub const MAX_SHEET_ROWS: u32 = 1_048_576;
|
|
17
|
+
pub const MAX_SHEET_COLUMNS: u32 = 16_384;
|
|
18
|
+
pub const MAX_SHEET_CELLS: u64 = 30_000;
|
|
19
|
+
pub const MIN_PAGE_SIDE: f64 = 72.0;
|
|
20
|
+
pub const MAX_PAGE_SIDE: f64 = 14_400.0;
|
|
21
|
+
pub const MAX_LENGTH: f64 = 1_000_000.0;
|
|
22
|
+
|
|
23
|
+
pub fn span(value: usize) -> usize {
|
|
24
|
+
value.clamp(1, MAX_TABLE_SPAN)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub fn finite(value: f64) -> f64 {
|
|
28
|
+
if value.is_finite() {
|
|
29
|
+
value.clamp(-MAX_LENGTH, MAX_LENGTH)
|
|
30
|
+
} else {
|
|
31
|
+
0.0
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
pub fn read_entry(reader: impl Read) -> Result<Vec<u8>, Error> {
|
|
36
|
+
let mut data = Vec::new();
|
|
37
|
+
reader.take(MAX_ZIP_ENTRY_BYTES + 1).read_to_end(&mut data)?;
|
|
38
|
+
if data.len() as u64 > MAX_ZIP_ENTRY_BYTES {
|
|
39
|
+
return Err(Error::new(format!("a part of the document is larger than {} MB uncompressed", MAX_ZIP_ENTRY_BYTES / (1024 * 1024))));
|
|
40
|
+
}
|
|
41
|
+
Ok(data)
|
|
42
|
+
}
|
package/core/src/markdown/mod.rs
CHANGED
|
@@ -121,6 +121,12 @@ impl<'a> Reader<'a> {
|
|
|
121
121
|
self.pending = Pending::Paragraph;
|
|
122
122
|
}
|
|
123
123
|
let props = self.run();
|
|
124
|
+
if let Some(Inline::Text { text: last, props: last_props }) = self.inlines.last_mut() {
|
|
125
|
+
if *last_props == props {
|
|
126
|
+
last.push_str(text);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
124
130
|
self.inlines.push(Inline::Text { text: text.to_string(), props });
|
|
125
131
|
}
|
|
126
132
|
|
package/core/src/odt/geometry.rs
CHANGED
|
@@ -8,6 +8,7 @@ struct Context<'a> {
|
|
|
8
8
|
vars: HashMap<&'static str, f64>,
|
|
9
9
|
cache: std::cell::RefCell<HashMap<String, f64>>,
|
|
10
10
|
depth: std::cell::Cell<usize>,
|
|
11
|
+
nesting: std::cell::Cell<usize>,
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
impl Context<'_> {
|
|
@@ -89,7 +90,22 @@ impl Parser<'_, '_> {
|
|
|
89
90
|
self.tokens.get(self.pos)
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
fn nested(&mut self, parse: impl FnOnce(&mut Self) -> f64) -> f64 {
|
|
94
|
+
let nesting = &self.ctx.nesting;
|
|
95
|
+
if nesting.get() >= crate::limits::MAX_FORMULA_DEPTH {
|
|
96
|
+
return 0.0;
|
|
97
|
+
}
|
|
98
|
+
nesting.set(nesting.get() + 1);
|
|
99
|
+
let value = parse(self);
|
|
100
|
+
self.ctx.nesting.set(self.ctx.nesting.get() - 1);
|
|
101
|
+
value
|
|
102
|
+
}
|
|
103
|
+
|
|
92
104
|
fn expr(&mut self) -> f64 {
|
|
105
|
+
self.nested(Self::expr_inner)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn expr_inner(&mut self) -> f64 {
|
|
93
109
|
let mut value = self.term();
|
|
94
110
|
while let Some(Token::Op(op @ ('+' | '-'))) = self.peek().cloned() {
|
|
95
111
|
self.pos += 1;
|
|
@@ -110,6 +126,10 @@ impl Parser<'_, '_> {
|
|
|
110
126
|
}
|
|
111
127
|
|
|
112
128
|
fn unary(&mut self) -> f64 {
|
|
129
|
+
self.nested(Self::unary_inner)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn unary_inner(&mut self) -> f64 {
|
|
113
133
|
if let Some(Token::Op('-')) = self.peek() {
|
|
114
134
|
self.pos += 1;
|
|
115
135
|
return -self.unary();
|
|
@@ -228,6 +248,7 @@ pub fn enhanced_path(geometry: &Element, width_pt: f64, height_pt: f64) -> Optio
|
|
|
228
248
|
vars,
|
|
229
249
|
cache: std::cell::RefCell::new(HashMap::new()),
|
|
230
250
|
depth: std::cell::Cell::new(0),
|
|
251
|
+
nesting: std::cell::Cell::new(0),
|
|
231
252
|
};
|
|
232
253
|
let norm = |x: f64, y: f64| ((x - origin.0) / space_w, (y - origin.1) / space_h);
|
|
233
254
|
let tokens: Vec<&str> = path.split_whitespace().collect();
|
package/core/src/odt/mod.rs
CHANGED
|
@@ -23,8 +23,7 @@ pub fn read(bytes: &[u8]) -> Result<Document, Error> {
|
|
|
23
23
|
let mut file = archive.by_index(i)?;
|
|
24
24
|
let name = file.name().to_string();
|
|
25
25
|
if name.starts_with("Pictures/") && !file.is_dir() {
|
|
26
|
-
let
|
|
27
|
-
file.read_to_end(&mut data)?;
|
|
26
|
+
let data = crate::limits::read_entry(&mut file)?;
|
|
28
27
|
if let Some(format) = ImageFormat::sniff(&data) {
|
|
29
28
|
media.insert(name, ImageData { data, format });
|
|
30
29
|
}
|
|
@@ -116,8 +115,7 @@ fn find_config_item<'a>(el: &'a Element, name: &str) -> Option<String> {
|
|
|
116
115
|
fn entry<R: Read + std::io::Seek>(archive: &mut zip::ZipArchive<R>, name: &str) -> Result<Option<Vec<u8>>, Error> {
|
|
117
116
|
match archive.by_name(name) {
|
|
118
117
|
Ok(mut file) => {
|
|
119
|
-
let
|
|
120
|
-
file.read_to_end(&mut data)?;
|
|
118
|
+
let data = crate::limits::read_entry(&mut file)?;
|
|
121
119
|
Ok(Some(data))
|
|
122
120
|
}
|
|
123
121
|
Err(zip::result::ZipError::FileNotFound) => Ok(None),
|
|
@@ -402,9 +400,9 @@ impl Reader<'_> {
|
|
|
402
400
|
}
|
|
403
401
|
}
|
|
404
402
|
if values[level] == 0 {
|
|
405
|
-
values[level] =
|
|
403
|
+
values[level] = start.saturating_sub(1);
|
|
406
404
|
}
|
|
407
|
-
values[level]
|
|
405
|
+
values[level] = values[level].saturating_add(1);
|
|
408
406
|
for deeper in level + 1..values.len() {
|
|
409
407
|
values[deeper] = 0;
|
|
410
408
|
}
|
|
@@ -447,7 +445,7 @@ impl Reader<'_> {
|
|
|
447
445
|
}
|
|
448
446
|
"a" => self.inlines(el, base, out, anchors),
|
|
449
447
|
"s" => {
|
|
450
|
-
let count = el.attr("c").and_then(|c| c.parse::<usize>().ok()).unwrap_or(1);
|
|
448
|
+
let count = el.attr("c").and_then(|c| c.parse::<usize>().ok()).unwrap_or(1).min(crate::limits::MAX_REPEATED_SPACES);
|
|
451
449
|
out.push(Inline::Text { text: " ".repeat(count), props: base.clone() });
|
|
452
450
|
}
|
|
453
451
|
"tab" => out.push(Inline::Tab),
|
|
@@ -603,7 +601,13 @@ impl Reader<'_> {
|
|
|
603
601
|
}
|
|
604
602
|
}
|
|
605
603
|
if table.columns.is_empty() {
|
|
606
|
-
let count = table
|
|
604
|
+
let count = table
|
|
605
|
+
.rows
|
|
606
|
+
.iter()
|
|
607
|
+
.map(|r| r.cells.iter().fold(0usize, |sum, c| sum.saturating_add(c.span)))
|
|
608
|
+
.max()
|
|
609
|
+
.unwrap_or(1)
|
|
610
|
+
.clamp(1, crate::limits::MAX_TABLE_SPAN);
|
|
607
611
|
table.columns = vec![468.0 / count as f64; count];
|
|
608
612
|
}
|
|
609
613
|
table
|
|
@@ -646,7 +650,7 @@ impl Reader<'_> {
|
|
|
646
650
|
for _ in 0..repeat {
|
|
647
651
|
let mut cell = Cell {
|
|
648
652
|
blocks: blocks.clone(),
|
|
649
|
-
span: tc.attr("number-columns-spanned").and_then(|v| v.parse().ok()).
|
|
653
|
+
span: tc.attr("number-columns-spanned").and_then(|v| v.parse().ok()).map_or(1, crate::limits::span),
|
|
650
654
|
..Cell::default()
|
|
651
655
|
};
|
|
652
656
|
if tc.attr("number-rows-spanned").and_then(|v| v.parse::<usize>().ok()).unwrap_or(1) > 1 {
|
package/core/src/odt/odp.rs
CHANGED
|
@@ -4,7 +4,7 @@ use crate::error::Error;
|
|
|
4
4
|
use crate::model::*;
|
|
5
5
|
use crate::xml::{self, Element};
|
|
6
6
|
use std::collections::HashMap;
|
|
7
|
-
use std::io::
|
|
7
|
+
use std::io::Cursor;
|
|
8
8
|
|
|
9
9
|
struct Package {
|
|
10
10
|
content: Element,
|
|
@@ -30,14 +30,12 @@ fn package(bytes: &[u8]) -> Result<Package, Error> {
|
|
|
30
30
|
continue;
|
|
31
31
|
}
|
|
32
32
|
if name.starts_with("Pictures/") || name.starts_with("media/") {
|
|
33
|
-
let
|
|
34
|
-
file.read_to_end(&mut data)?;
|
|
33
|
+
let data = crate::limits::read_entry(&mut file)?;
|
|
35
34
|
if let Some(format) = ImageFormat::sniff(&data) {
|
|
36
35
|
media.insert(name, ImageData { data, format });
|
|
37
36
|
}
|
|
38
37
|
} else if name.starts_with("Object ") && name.ends_with("/content.xml") {
|
|
39
|
-
let
|
|
40
|
-
file.read_to_end(&mut data)?;
|
|
38
|
+
let data = crate::limits::read_entry(&mut file)?;
|
|
41
39
|
if let Ok(root) = xml::parse(&data) {
|
|
42
40
|
objects.insert(name.trim_end_matches("/content.xml").to_string(), root);
|
|
43
41
|
}
|
package/core/src/odt/styles.rs
CHANGED
|
@@ -548,7 +548,7 @@ impl Styles {
|
|
|
548
548
|
let el = self.raw.get(&format!("section:{name}"))?;
|
|
549
549
|
let props = el.child("section-properties")?;
|
|
550
550
|
let columns = props.child("columns")?;
|
|
551
|
-
let count: usize = columns.attr("column-count").and_then(|v| v.parse().ok()).
|
|
551
|
+
let count: usize = columns.attr("column-count").and_then(|v| v.parse().ok()).map_or(1, |n: usize| n.clamp(1, crate::limits::MAX_TEXT_COLUMNS));
|
|
552
552
|
let gap = columns.attr("column-gap").and_then(length).unwrap_or(0.0);
|
|
553
553
|
let separator = columns.child("column-sep").map(|sep| ColumnSeparator {
|
|
554
554
|
width: sep.attr("width").and_then(length).unwrap_or(0.25).max(0.25),
|
|
@@ -945,7 +945,7 @@ fn parse_page_layout(el: &Element) -> PageLayout {
|
|
|
945
945
|
page.margin.right = v;
|
|
946
946
|
}
|
|
947
947
|
if let Some(cols) = p.child("columns") {
|
|
948
|
-
columns = cols.attr("column-count").and_then(|c| c.parse().ok()).
|
|
948
|
+
columns = cols.attr("column-count").and_then(|c| c.parse().ok()).map_or(1, |n: usize| n.clamp(1, crate::limits::MAX_TEXT_COLUMNS));
|
|
949
949
|
gap = cols.attr("column-gap").and_then(length).unwrap_or(0.0);
|
|
950
950
|
}
|
|
951
951
|
}
|
package/core/src/pptx/mod.rs
CHANGED
|
@@ -10,7 +10,7 @@ use crate::model::*;
|
|
|
10
10
|
use crate::xml::{self, Element};
|
|
11
11
|
use color::{parse_clr_map, resolve_child, ColorContext, Theme};
|
|
12
12
|
use std::collections::HashMap;
|
|
13
|
-
use std::io::
|
|
13
|
+
use std::io::Cursor;
|
|
14
14
|
use text::{emu, text_blocks, LevelStyles, TextContext};
|
|
15
15
|
|
|
16
16
|
const NO_STYLE_TABLE_GRID: &str = "{5940675A-B579-460E-94D1-54222C63F5DA}";
|
|
@@ -50,8 +50,12 @@ pub fn read(bytes: &[u8]) -> Result<Document, Error> {
|
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
let mut number = 0;
|
|
53
|
+
let mut seen_targets = std::collections::HashSet::new();
|
|
53
54
|
for id in slide_ids {
|
|
54
55
|
let Some((_, target)) = pres_rels.get(&id) else { continue };
|
|
56
|
+
if !seen_targets.insert(target.clone()) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
55
59
|
let Some(slide) = package.xml(target)? else { continue };
|
|
56
60
|
if slide.attr("show") == Some("0") {
|
|
57
61
|
continue;
|
|
@@ -81,8 +85,7 @@ impl<'a> Package<'a> {
|
|
|
81
85
|
fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>, Error> {
|
|
82
86
|
match self.archive.by_name(path) {
|
|
83
87
|
Ok(mut file) => {
|
|
84
|
-
let
|
|
85
|
-
file.read_to_end(&mut data)?;
|
|
88
|
+
let data = crate::limits::read_entry(&mut file)?;
|
|
86
89
|
Ok(Some(data))
|
|
87
90
|
}
|
|
88
91
|
Err(zip::result::ZipError::FileNotFound) => Ok(None),
|
|
@@ -170,16 +173,28 @@ pub(crate) fn parse_chart(root: &Element) -> Option<Chart> {
|
|
|
170
173
|
.or_else(|| el.child("strLit"))
|
|
171
174
|
.or_else(|| el.child("numLit"));
|
|
172
175
|
let Some(cache) = cache else { return Vec::new() };
|
|
173
|
-
let count = cache
|
|
176
|
+
let count = cache
|
|
177
|
+
.child("ptCount")
|
|
178
|
+
.and_then(|c| c.attr("val"))
|
|
179
|
+
.and_then(|v| v.parse::<usize>().ok())
|
|
180
|
+
.unwrap_or(0)
|
|
181
|
+
.min(crate::limits::MAX_CHART_POINTS);
|
|
174
182
|
let mut points = vec![None; count];
|
|
175
183
|
for pt in cache.children("pt") {
|
|
176
184
|
if let (Some(idx), Some(v)) = (pt.attr("idx").and_then(|i| i.parse::<usize>().ok()), pt.child("v")) {
|
|
185
|
+
if idx >= crate::limits::MAX_CHART_POINTS {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
177
188
|
if idx >= points.len() {
|
|
178
189
|
points.resize(idx + 1, None);
|
|
179
190
|
}
|
|
180
191
|
points[idx] = Some(v.text());
|
|
181
192
|
}
|
|
182
193
|
}
|
|
194
|
+
if points.len() > 4096 {
|
|
195
|
+
let used = points.iter().rposition(Option::is_some).map_or(0, |i| i + 1);
|
|
196
|
+
points.truncate(used);
|
|
197
|
+
}
|
|
183
198
|
points
|
|
184
199
|
};
|
|
185
200
|
let mut categories: Vec<String> = Vec::new();
|
|
@@ -1135,7 +1150,7 @@ fn parse_table(tbl: &Element, ctx: &SlideCtx) -> Table {
|
|
|
1135
1150
|
}
|
|
1136
1151
|
let tc_pr = tc.child("tcPr");
|
|
1137
1152
|
let mut cell = Cell {
|
|
1138
|
-
span: tc.attr("gridSpan").and_then(|v| v.parse().ok()).
|
|
1153
|
+
span: tc.attr("gridSpan").and_then(|v| v.parse().ok()).map_or(1, crate::limits::span),
|
|
1139
1154
|
..Cell::default()
|
|
1140
1155
|
};
|
|
1141
1156
|
if tc.attr("vMerge") == Some("1") {
|
|
@@ -1276,14 +1291,14 @@ fn custom_path(geom: &Element) -> Option<Vec<PathCommand>> {
|
|
|
1276
1291
|
current = *p;
|
|
1277
1292
|
}
|
|
1278
1293
|
("arcTo", _) => {
|
|
1279
|
-
let angle = |name: &str| cmd.attr(name).and_then(|v| v.parse::<f64>().ok()).unwrap_or(0.0) / 60000.0;
|
|
1294
|
+
let angle = |name: &str| cmd.attr(name).and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()).unwrap_or(0.0) / 60000.0;
|
|
1280
1295
|
let rw = cmd.attr("wR").and_then(|v| v.parse::<f64>().ok()).unwrap_or(0.0) / w;
|
|
1281
1296
|
let rh = cmd.attr("hR").and_then(|v| v.parse::<f64>().ok()).unwrap_or(0.0) / h;
|
|
1282
1297
|
let start = angle("stAng").to_radians();
|
|
1283
|
-
let sweep = angle("swAng").to_radians();
|
|
1298
|
+
let sweep = angle("swAng").to_radians().clamp(-std::f64::consts::TAU, std::f64::consts::TAU);
|
|
1284
1299
|
let cx = current.0 - rw * start.cos();
|
|
1285
1300
|
let cy = current.1 - rh * start.sin();
|
|
1286
|
-
let steps = ((sweep.abs() / (std::f64::consts::PI / 8.0)).ceil() as usize).
|
|
1301
|
+
let steps = ((sweep.abs() / (std::f64::consts::PI / 8.0)).ceil() as usize).clamp(1, 64);
|
|
1287
1302
|
for i in 1..=steps {
|
|
1288
1303
|
let a = start + sweep * i as f64 / steps as f64;
|
|
1289
1304
|
current = (cx + rw * a.cos(), cy + rh * a.sin());
|
package/core/src/pptx/text.rs
CHANGED
|
@@ -341,8 +341,8 @@ fn paragraph(p: &Element, ctx: &TextContext, counters: &mut HashMap<usize, i64>)
|
|
|
341
341
|
match &ppr.bullet {
|
|
342
342
|
Some(Bullet::Char(ch)) => Some(bullet_label(ch, &ppr, &last_props, size)),
|
|
343
343
|
Some(Bullet::AutoNum { scheme, start }) => {
|
|
344
|
-
let count = counters.entry(level).or_insert(start
|
|
345
|
-
*count
|
|
344
|
+
let count = counters.entry(level).or_insert(start.saturating_sub(1));
|
|
345
|
+
*count = count.saturating_add(1);
|
|
346
346
|
let value = *count;
|
|
347
347
|
counters.retain(|l, _| *l <= level);
|
|
348
348
|
let text = auto_number(scheme, value);
|
|
@@ -85,8 +85,9 @@ impl Emitter<'_> {
|
|
|
85
85
|
|
|
86
86
|
let header = self.margin_slot(section, true, index);
|
|
87
87
|
let footer = self.margin_slot(section, false, index);
|
|
88
|
-
let
|
|
89
|
-
|
|
88
|
+
let section_columns = section.columns.min(crate::limits::MAX_TEXT_COLUMNS);
|
|
89
|
+
let columns = if section_columns > 1 {
|
|
90
|
+
format!(", columns: {section_columns}")
|
|
90
91
|
} else {
|
|
91
92
|
String::new()
|
|
92
93
|
};
|
|
@@ -100,11 +101,11 @@ impl Emitter<'_> {
|
|
|
100
101
|
pt(page.margin.left + self.text_shift()),
|
|
101
102
|
pt(page.margin.right - self.text_shift())
|
|
102
103
|
);
|
|
103
|
-
if
|
|
104
|
+
if section_columns > 1 {
|
|
104
105
|
let _ = writeln!(self.out, "#set columns(gutter: {})", pt(section.column_gap));
|
|
105
106
|
}
|
|
106
107
|
if let Some(start) = section.page_start {
|
|
107
|
-
let _ = writeln!(self.out, "#counter(page).update({
|
|
108
|
+
let _ = writeln!(self.out, "#counter(page).update({})", start.clamp(0, i64::from(u32::MAX)));
|
|
108
109
|
}
|
|
109
110
|
let _ = writeln!(self.out, "#metadata(none) <section-{index}>");
|
|
110
111
|
for anchor in §ion.anchors {
|
|
@@ -250,7 +251,7 @@ impl Emitter<'_> {
|
|
|
250
251
|
|
|
251
252
|
fn columns_block(&self, c: &ColumnsBlock, out: &mut String) {
|
|
252
253
|
let body = self.blocks(&c.blocks, false);
|
|
253
|
-
let n = c.count.
|
|
254
|
+
let n = c.count.clamp(1, crate::limits::MAX_TEXT_COLUMNS);
|
|
254
255
|
let gap = pt(c.gap);
|
|
255
256
|
if !c.balanced {
|
|
256
257
|
let _ = writeln!(out, "#columns({n}, gutter: {gap})[{body}]");
|
|
@@ -270,7 +271,7 @@ impl Emitter<'_> {
|
|
|
270
271
|
let mut separators = String::new();
|
|
271
272
|
if let Some(sep) = c.separator {
|
|
272
273
|
let offset = match sep.valign {
|
|
273
|
-
VAlign::Top => "
|
|
274
|
+
VAlign::Top => "0pt".to_string(),
|
|
274
275
|
VAlign::Center => format!("h * {} / 2", trim_num(1.0 - sep.height)),
|
|
275
276
|
VAlign::Bottom => format!("h * {}", trim_num(1.0 - sep.height)),
|
|
276
277
|
};
|
|
@@ -1233,11 +1234,27 @@ impl Emitter<'_> {
|
|
|
1233
1234
|
|
|
1234
1235
|
fn table_expr(&self, table: &Table) -> String {
|
|
1235
1236
|
self.table_depth.set(self.table_depth.get() + 1);
|
|
1236
|
-
let expr = self.
|
|
1237
|
+
let expr = if self.table_depth.get() > crate::limits::MAX_TABLE_DEPTH {
|
|
1238
|
+
self.flattened_table(table)
|
|
1239
|
+
} else {
|
|
1240
|
+
self.table_expr_inner(table)
|
|
1241
|
+
};
|
|
1237
1242
|
self.table_depth.set(self.table_depth.get() - 1);
|
|
1238
1243
|
expr
|
|
1239
1244
|
}
|
|
1240
1245
|
|
|
1246
|
+
fn flattened_table(&self, table: &Table) -> String {
|
|
1247
|
+
let mut flat: Vec<Block> = Vec::new();
|
|
1248
|
+
let mut pending: Vec<&Block> = table.rows.iter().rev().flat_map(|row| row.cells.iter().rev()).flat_map(|cell| cell.blocks.iter().rev()).collect();
|
|
1249
|
+
while let Some(block) = pending.pop() {
|
|
1250
|
+
match block {
|
|
1251
|
+
Block::Table(inner) => pending.extend(inner.rows.iter().rev().flat_map(|row| row.cells.iter().rev()).flat_map(|cell| cell.blocks.iter().rev())),
|
|
1252
|
+
other => flat.push(other.clone()),
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
format!("block[{}]", self.blocks(&flat, false))
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1241
1258
|
fn table_expr_inner(&self, table: &Table) -> String {
|
|
1242
1259
|
if table.rows.is_empty() {
|
|
1243
1260
|
return "box()".to_string();
|
|
@@ -1245,10 +1262,10 @@ impl Emitter<'_> {
|
|
|
1245
1262
|
let grid = table
|
|
1246
1263
|
.rows
|
|
1247
1264
|
.iter()
|
|
1248
|
-
.map(|r| r.cells.iter().
|
|
1265
|
+
.map(|r| r.cells.iter().fold(0usize, |sum, c| sum.saturating_add(crate::limits::span(c.span))))
|
|
1249
1266
|
.max()
|
|
1250
1267
|
.unwrap_or(0)
|
|
1251
|
-
.
|
|
1268
|
+
.clamp(1, crate::limits::MAX_TABLE_SPAN);
|
|
1252
1269
|
let fixed_columns = table.columns.len() >= grid;
|
|
1253
1270
|
|
|
1254
1271
|
let mut defaults = CellMargins {
|
|
@@ -1271,7 +1288,7 @@ impl Emitter<'_> {
|
|
|
1271
1288
|
if let Some(&anchor) = open.get(&column) {
|
|
1272
1289
|
cells[anchor].rowspan += 1;
|
|
1273
1290
|
}
|
|
1274
|
-
column += cell.span;
|
|
1291
|
+
column += crate::limits::span(cell.span);
|
|
1275
1292
|
continue;
|
|
1276
1293
|
}
|
|
1277
1294
|
Some(VerticalMerge::Restart) => {
|
|
@@ -1292,8 +1309,13 @@ impl Emitter<'_> {
|
|
|
1292
1309
|
}
|
|
1293
1310
|
}
|
|
1294
1311
|
|
|
1295
|
-
let
|
|
1296
|
-
let mut body =
|
|
1312
|
+
let hidden = |b: &Block| matches!(b, Block::Paragraph(p) if paragraph_hidden(p));
|
|
1313
|
+
let mut body = if cell.blocks.iter().any(hidden) {
|
|
1314
|
+
let visible: Vec<Block> = cell.blocks.iter().filter(|b| !hidden(b)).cloned().collect();
|
|
1315
|
+
self.blocks(&visible, true)
|
|
1316
|
+
} else {
|
|
1317
|
+
self.blocks(&cell.blocks, true)
|
|
1318
|
+
};
|
|
1297
1319
|
if let (Some(shift), false) = (cell.overflow_shift, body.trim().is_empty()) {
|
|
1298
1320
|
let clip = cell.overflow_width.unwrap_or(1.0);
|
|
1299
1321
|
body = format!(
|
|
@@ -1328,7 +1350,7 @@ impl Emitter<'_> {
|
|
|
1328
1350
|
("top", pick(cell.borders.top, ri == 0, table.borders.top, table.borders.inside_h)),
|
|
1329
1351
|
("bottom", pick(cell.borders.bottom, ri == last_row, table.borders.bottom, table.borders.inside_h)),
|
|
1330
1352
|
("left", pick(cell.borders.left, column == 0, table.borders.left, table.borders.inside_v)),
|
|
1331
|
-
("right", pick(cell.borders.right, column + cell.span >= grid, table.borders.right, table.borders.inside_v)),
|
|
1353
|
+
("right", pick(cell.borders.right, column + crate::limits::span(cell.span) >= grid, table.borders.right, table.borders.inside_v)),
|
|
1332
1354
|
];
|
|
1333
1355
|
|
|
1334
1356
|
let border_top = match borders[0].1 {
|
|
@@ -1380,12 +1402,12 @@ impl Emitter<'_> {
|
|
|
1380
1402
|
}
|
|
1381
1403
|
|
|
1382
1404
|
cells.push(CellOut {
|
|
1383
|
-
colspan: cell.span,
|
|
1405
|
+
colspan: crate::limits::span(cell.span),
|
|
1384
1406
|
rowspan: 1,
|
|
1385
1407
|
args,
|
|
1386
1408
|
body,
|
|
1387
1409
|
});
|
|
1388
|
-
column += cell.span;
|
|
1410
|
+
column += crate::limits::span(cell.span);
|
|
1389
1411
|
}
|
|
1390
1412
|
}
|
|
1391
1413
|
|
|
@@ -1508,7 +1530,7 @@ fn split_tokens(text: &str) -> Vec<String> {
|
|
|
1508
1530
|
}
|
|
1509
1531
|
|
|
1510
1532
|
fn trim_num(value: f64) -> String {
|
|
1511
|
-
let rounded = (value * 1000.0).round() / 1000.0;
|
|
1533
|
+
let rounded = (crate::limits::finite(value) * 1000.0).round() / 1000.0;
|
|
1512
1534
|
format!("{rounded}")
|
|
1513
1535
|
}
|
|
1514
1536
|
|
|
@@ -1568,7 +1590,7 @@ fn tuple(items: &[String]) -> String {
|
|
|
1568
1590
|
}
|
|
1569
1591
|
|
|
1570
1592
|
pub fn pt(value: f64) -> String {
|
|
1571
|
-
let rounded = (value * 1000.0).round() / 1000.0;
|
|
1593
|
+
let rounded = (crate::limits::finite(value) * 1000.0).round() / 1000.0;
|
|
1572
1594
|
if rounded == 0.0 {
|
|
1573
1595
|
return "0pt".to_string();
|
|
1574
1596
|
}
|
|
@@ -1600,9 +1622,10 @@ pub fn typst_str(text: &str) -> String {
|
|
|
1600
1622
|
match ch {
|
|
1601
1623
|
'\\' => out.push_str("\\\\"),
|
|
1602
1624
|
'"' => out.push_str("\\\""),
|
|
1603
|
-
|
|
1604
|
-
let _ = write!(out, "\\u{{{:x}}}",
|
|
1625
|
+
'\t' | '\n' | '\r' => {
|
|
1626
|
+
let _ = write!(out, "\\u{{{:x}}}", ch as u32);
|
|
1605
1627
|
}
|
|
1628
|
+
c if (c as u32) < 0x20 || c == '\u{7f}' => {}
|
|
1606
1629
|
c => out.push(c),
|
|
1607
1630
|
}
|
|
1608
1631
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
use crate::limits::MAX_IMAGE_PIXELS;
|
|
2
|
+
use typst::foundations::Bytes;
|
|
3
|
+
use typst::visualize::{ExchangeFormat, RasterImage};
|
|
4
|
+
|
|
5
|
+
const PLACEHOLDER: [u8; 68] = [
|
|
6
|
+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
7
|
+
0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0b, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0x00,
|
|
8
|
+
0x02, 0x00, 0x00, 0x05, 0x00, 0x01, 0x7a, 0x5e, 0xab, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
pub fn sanitize(path: &str, data: Vec<u8>) -> Vec<u8> {
|
|
12
|
+
let format = match path.rsplit('.').next() {
|
|
13
|
+
Some("png") => ExchangeFormat::Png,
|
|
14
|
+
Some("jpg") => ExchangeFormat::Jpg,
|
|
15
|
+
Some("gif") => ExchangeFormat::Gif,
|
|
16
|
+
_ => return data,
|
|
17
|
+
};
|
|
18
|
+
let within_limit = dimensions(format, &data).is_some_and(|(w, h)| w > 0 && h > 0 && u64::from(w) * u64::from(h) <= MAX_IMAGE_PIXELS);
|
|
19
|
+
if !within_limit {
|
|
20
|
+
return PLACEHOLDER.to_vec();
|
|
21
|
+
}
|
|
22
|
+
let bytes = Bytes::new(data);
|
|
23
|
+
match RasterImage::plain(bytes.clone(), format) {
|
|
24
|
+
Ok(_) => bytes.to_vec(),
|
|
25
|
+
Err(_) => PLACEHOLDER.to_vec(),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fn dimensions(format: ExchangeFormat, data: &[u8]) -> Option<(u32, u32)> {
|
|
30
|
+
match format {
|
|
31
|
+
ExchangeFormat::Png => {
|
|
32
|
+
let ihdr = data.get(12..24)?;
|
|
33
|
+
if &ihdr[..4] != b"IHDR" {
|
|
34
|
+
return None;
|
|
35
|
+
}
|
|
36
|
+
Some((u32::from_be_bytes(ihdr[4..8].try_into().ok()?), u32::from_be_bytes(ihdr[8..12].try_into().ok()?)))
|
|
37
|
+
}
|
|
38
|
+
ExchangeFormat::Gif => {
|
|
39
|
+
let screen = data.get(6..10)?;
|
|
40
|
+
Some((u32::from(u16::from_le_bytes([screen[0], screen[1]])), u32::from(u16::from_le_bytes([screen[2], screen[3]]))))
|
|
41
|
+
}
|
|
42
|
+
ExchangeFormat::Jpg => jpeg_dimensions(data),
|
|
43
|
+
_ => None,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn jpeg_dimensions(data: &[u8]) -> Option<(u32, u32)> {
|
|
48
|
+
let mut pos = 2;
|
|
49
|
+
while pos + 4 <= data.len() {
|
|
50
|
+
if data[pos] != 0xff {
|
|
51
|
+
return None;
|
|
52
|
+
}
|
|
53
|
+
let marker = data[pos + 1];
|
|
54
|
+
if marker == 0xff {
|
|
55
|
+
pos += 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if marker == 0xd8 || marker == 0x01 || (0xd0..=0xd7).contains(&marker) {
|
|
59
|
+
pos += 2;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
let length = usize::from(u16::from_be_bytes([data[pos + 2], data[pos + 3]]));
|
|
63
|
+
let is_frame = (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc);
|
|
64
|
+
if is_frame {
|
|
65
|
+
let frame = data.get(pos + 5..pos + 9)?;
|
|
66
|
+
let height = u32::from(u16::from_be_bytes([frame[0], frame[1]]));
|
|
67
|
+
let width = u32::from(u16::from_be_bytes([frame[2], frame[3]]));
|
|
68
|
+
return Some((width, height));
|
|
69
|
+
}
|
|
70
|
+
if length < 2 {
|
|
71
|
+
return None;
|
|
72
|
+
}
|
|
73
|
+
pos += 2 + length;
|
|
74
|
+
}
|
|
75
|
+
None
|
|
76
|
+
}
|