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
package/core/src/model.rs
CHANGED
|
@@ -36,6 +36,8 @@ pub struct Document {
|
|
|
36
36
|
pub fixed_line_metrics: bool,
|
|
37
37
|
pub borders_outside_indent: bool,
|
|
38
38
|
pub tabs_relative_to_indent: bool,
|
|
39
|
+
pub table_at_border_center: bool,
|
|
40
|
+
pub writer_text_offset: bool,
|
|
39
41
|
pub footnote_separator_width: Option<f64>,
|
|
40
42
|
pub cell_metrics: bool,
|
|
41
43
|
}
|
|
@@ -66,6 +68,16 @@ pub struct Section {
|
|
|
66
68
|
pub page_format: PageNumberFormat,
|
|
67
69
|
pub content_scale: f64,
|
|
68
70
|
pub anchors: Vec<Anchor>,
|
|
71
|
+
pub notes: Vec<PageNote>,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#[derive(Debug, Clone, Default)]
|
|
75
|
+
pub struct PageNote {
|
|
76
|
+
pub x: f64,
|
|
77
|
+
pub y: f64,
|
|
78
|
+
pub scale: f64,
|
|
79
|
+
pub title: String,
|
|
80
|
+
pub text: String,
|
|
69
81
|
}
|
|
70
82
|
|
|
71
83
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
|
@@ -465,46 +477,6 @@ pub struct Cell {
|
|
|
465
477
|
pub overflow_shift: Option<f64>,
|
|
466
478
|
}
|
|
467
479
|
|
|
468
|
-
#[derive(Debug, Clone, Default)]
|
|
469
|
-
pub struct FixedDocument {
|
|
470
|
-
pub pages: Vec<FixedPage>,
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
#[derive(Debug, Clone, Default)]
|
|
474
|
-
pub struct FixedPage {
|
|
475
|
-
pub width: f64,
|
|
476
|
-
pub height: f64,
|
|
477
|
-
pub background: Option<RasterImage>,
|
|
478
|
-
pub lines: Vec<TextLine>,
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
#[derive(Debug, Clone)]
|
|
482
|
-
pub struct RasterImage {
|
|
483
|
-
pub png: Vec<u8>,
|
|
484
|
-
pub width_px: u32,
|
|
485
|
-
pub height_px: u32,
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
#[derive(Debug, Clone, Default)]
|
|
489
|
-
pub struct TextLine {
|
|
490
|
-
pub x: f64,
|
|
491
|
-
pub top: f64,
|
|
492
|
-
pub width: f64,
|
|
493
|
-
pub height: f64,
|
|
494
|
-
pub ascent: f64,
|
|
495
|
-
pub runs: Vec<TextRun>,
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
#[derive(Debug, Clone, PartialEq)]
|
|
499
|
-
pub struct TextRun {
|
|
500
|
-
pub text: String,
|
|
501
|
-
pub font: String,
|
|
502
|
-
pub size: f64,
|
|
503
|
-
pub bold: bool,
|
|
504
|
-
pub italic: bool,
|
|
505
|
-
pub color: Color,
|
|
506
|
-
}
|
|
507
|
-
|
|
508
480
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
509
481
|
pub enum ImageFormat {
|
|
510
482
|
Png,
|
|
@@ -627,6 +599,19 @@ pub enum ChartKind {
|
|
|
627
599
|
pub struct ChartSeries {
|
|
628
600
|
pub name: Option<String>,
|
|
629
601
|
pub values: Vec<Option<f64>>,
|
|
602
|
+
pub color: Option<Color>,
|
|
603
|
+
pub point_colors: Vec<Option<Color>>,
|
|
604
|
+
pub no_line: bool,
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
#[derive(Debug, Clone, Default)]
|
|
608
|
+
pub struct ChartStyle {
|
|
609
|
+
pub font: Option<String>,
|
|
610
|
+
pub border: Option<Color>,
|
|
611
|
+
pub plot_fill: Option<Color>,
|
|
612
|
+
pub plot_border: Option<Color>,
|
|
613
|
+
pub grid: Option<Color>,
|
|
614
|
+
pub no_grid: bool,
|
|
630
615
|
}
|
|
631
616
|
|
|
632
617
|
#[derive(Debug, Clone)]
|
|
@@ -638,6 +623,7 @@ pub struct Chart {
|
|
|
638
623
|
pub legend: Option<LegendPos>,
|
|
639
624
|
pub gap_width: f64,
|
|
640
625
|
pub markers: bool,
|
|
626
|
+
pub style: ChartStyle,
|
|
641
627
|
}
|
|
642
628
|
|
|
643
629
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
use crate::model::*;
|
|
2
|
+
use crate::xml::Element;
|
|
3
|
+
use std::collections::HashMap;
|
|
4
|
+
|
|
5
|
+
struct Grid {
|
|
6
|
+
rows: Vec<Vec<Option<(Option<f64>, String)>>>,
|
|
7
|
+
anchors: HashMap<String, (usize, usize)>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl Grid {
|
|
11
|
+
fn parse(table: &Element) -> Grid {
|
|
12
|
+
let mut rows = Vec::new();
|
|
13
|
+
let mut anchors = HashMap::new();
|
|
14
|
+
collect_rows(table, &mut rows, &mut anchors);
|
|
15
|
+
Grid { rows, anchors }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn cells(&self, range: &str) -> Vec<(Option<f64>, String)> {
|
|
19
|
+
let range = range.split_whitespace().next().unwrap_or(range);
|
|
20
|
+
let Some((r1, c1, r2, c2)) = parse_range(range) else { return Vec::new() };
|
|
21
|
+
let (start, down) = if range.starts_with("local-table") {
|
|
22
|
+
((r1, c1), true)
|
|
23
|
+
} else if let Some(&start) = self.anchors.get(range) {
|
|
24
|
+
(start, c1 == c2)
|
|
25
|
+
} else {
|
|
26
|
+
((r1, c1), true)
|
|
27
|
+
};
|
|
28
|
+
let count = (r2 - r1 + 1) * (c2 - c1 + 1);
|
|
29
|
+
(0..count)
|
|
30
|
+
.map(|k| {
|
|
31
|
+
let (r, c) = if down { (start.0 + k, start.1) } else { (start.0, start.1 + k) };
|
|
32
|
+
self.rows.get(r).and_then(|row| row.get(c)).cloned().flatten().unwrap_or((None, String::new()))
|
|
33
|
+
})
|
|
34
|
+
.collect()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn collect_rows(el: &Element, out: &mut Vec<Vec<Option<(Option<f64>, String)>>>, anchors: &mut HashMap<String, (usize, usize)>) {
|
|
39
|
+
for child in el.elements() {
|
|
40
|
+
match child.name.as_str() {
|
|
41
|
+
"table-row" => {
|
|
42
|
+
let mut row = Vec::new();
|
|
43
|
+
for cell in child.children("table-cell") {
|
|
44
|
+
if let Some(desc) = cell.child("g").and_then(|g| g.child("desc")) {
|
|
45
|
+
for range in desc.text().split_whitespace() {
|
|
46
|
+
anchors.insert(range.to_string(), (out.len(), row.len()));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let count = cell.attr("number-columns-repeated").and_then(|v| v.parse::<usize>().ok()).unwrap_or(1).min(256);
|
|
50
|
+
let value = cell.attr("value").and_then(|v| v.parse::<f64>().ok());
|
|
51
|
+
let text: Vec<String> = cell.children("p").map(|p| p.text()).collect();
|
|
52
|
+
for _ in 0..count {
|
|
53
|
+
row.push(Some((value, text.join(" "))));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
out.push(row);
|
|
57
|
+
}
|
|
58
|
+
"table-header-rows" | "table-rows" | "table-row-group" => collect_rows(child, out, anchors),
|
|
59
|
+
_ => {}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fn column_number(letters: &str) -> Option<usize> {
|
|
65
|
+
let mut n = 0usize;
|
|
66
|
+
for ch in letters.chars() {
|
|
67
|
+
if !ch.is_ascii_alphabetic() {
|
|
68
|
+
return None;
|
|
69
|
+
}
|
|
70
|
+
n = n * 26 + (ch.to_ascii_uppercase() as usize - 'A' as usize + 1);
|
|
71
|
+
}
|
|
72
|
+
(n > 0).then(|| n - 1)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn cell_address(text: &str) -> Option<(usize, usize)> {
|
|
76
|
+
let text = text.rsplit('.').next()?.replace('$', "");
|
|
77
|
+
let split = text.find(|c: char| c.is_ascii_digit())?;
|
|
78
|
+
let col = column_number(&text[..split])?;
|
|
79
|
+
let row: usize = text[split..].parse().ok()?;
|
|
80
|
+
Some((row.checked_sub(1)?, col))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn parse_range(range: &str) -> Option<(usize, usize, usize, usize)> {
|
|
84
|
+
let range = range.split_whitespace().next()?;
|
|
85
|
+
let (a, b) = range.split_once(':').unwrap_or((range, range));
|
|
86
|
+
let (r1, c1) = cell_address(a)?;
|
|
87
|
+
let (r2, c2) = cell_address(b)?;
|
|
88
|
+
Some((r1.min(r2), c1.min(c2), r1.max(r2), c1.max(c2)))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
struct ChartStyles {
|
|
92
|
+
styles: HashMap<String, Element>,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
impl ChartStyles {
|
|
96
|
+
fn graphic(&self, name: Option<&str>) -> Option<&Element> {
|
|
97
|
+
self.styles.get(name?)?.child("graphic-properties")
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fn fill(&self, name: Option<&str>) -> Option<Color> {
|
|
101
|
+
let g = self.graphic(name)?;
|
|
102
|
+
if g.attr("fill") == Some("none") {
|
|
103
|
+
return None;
|
|
104
|
+
}
|
|
105
|
+
g.attr("fill-color").and_then(Color::parse_hex)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn stroke(&self, name: Option<&str>) -> Option<Option<Color>> {
|
|
109
|
+
let g = self.graphic(name)?;
|
|
110
|
+
if g.attr("stroke") == Some("none") {
|
|
111
|
+
return Some(None);
|
|
112
|
+
}
|
|
113
|
+
g.attr("stroke-color").and_then(Color::parse_hex).map(Some)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn chart(&self, name: Option<&str>) -> Option<&Element> {
|
|
117
|
+
self.styles.get(name?)?.child("chart-properties")
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fn font(&self, name: Option<&str>) -> Option<String> {
|
|
121
|
+
let t = self.styles.get(name?)?.child("text-properties")?;
|
|
122
|
+
t.attr("font-family").or_else(|| t.attr("font-name")).map(|f| f.trim_matches('\'').to_string())
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub fn chart_content(root: &Element) -> DrawingContent {
|
|
127
|
+
match parse(root) {
|
|
128
|
+
Some(chart) => DrawingContent::Chart(chart),
|
|
129
|
+
None => DrawingContent::Placeholder(None),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn parse(root: &Element) -> Option<Chart> {
|
|
134
|
+
let chart = root.child("body")?.child("chart")?.child("chart")?;
|
|
135
|
+
let mut styles = HashMap::new();
|
|
136
|
+
if let Some(auto) = root.child("automatic-styles") {
|
|
137
|
+
for style in auto.children("style") {
|
|
138
|
+
if let Some(name) = style.attr("name") {
|
|
139
|
+
styles.insert(name.to_string(), style.clone());
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
let styles = ChartStyles { styles };
|
|
144
|
+
let plot = chart.child("plot-area")?;
|
|
145
|
+
let grid = chart.child("table").map(Grid::parse).unwrap_or(Grid { rows: Vec::new(), anchors: HashMap::new() });
|
|
146
|
+
let vertical = styles.chart(plot.attr("style-name")).and_then(|c| c.attr("vertical")) == Some("true");
|
|
147
|
+
let kind = match chart.attr("class").unwrap_or("").trim_start_matches("chart:") {
|
|
148
|
+
"bar" if vertical => ChartKind::Bar,
|
|
149
|
+
"bar" => ChartKind::Column,
|
|
150
|
+
"line" | "scatter" | "radar" | "filled-radar" | "stock" => ChartKind::Line,
|
|
151
|
+
"circle" | "ring" => ChartKind::Pie,
|
|
152
|
+
"area" => ChartKind::Area,
|
|
153
|
+
_ => return None,
|
|
154
|
+
};
|
|
155
|
+
let title = chart.child("title").map(|t| t.children("p").map(|p| p.text()).collect::<Vec<_>>().join(" ")).filter(|t| !t.trim().is_empty());
|
|
156
|
+
let legend = chart.child("legend").map(|l| match l.attr("legend-position") {
|
|
157
|
+
Some("bottom") => LegendPos::Bottom,
|
|
158
|
+
Some("top") => LegendPos::Top,
|
|
159
|
+
Some("start") => LegendPos::Left,
|
|
160
|
+
_ => LegendPos::Right,
|
|
161
|
+
});
|
|
162
|
+
let mut categories: Vec<String> = Vec::new();
|
|
163
|
+
let mut grid_color = None;
|
|
164
|
+
let mut grid_seen = false;
|
|
165
|
+
for axis in plot.children("axis") {
|
|
166
|
+
if axis.attr("dimension") == Some("x") {
|
|
167
|
+
if let Some(range) = axis.child("categories").and_then(|c| c.attr("cell-range-address")) {
|
|
168
|
+
categories = grid.cells(range).into_iter().map(|(v, t)| if t.is_empty() { v.map(crate::xlsx::format::general).unwrap_or_default() } else { t }).collect();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if axis.attr("dimension") == Some("y") {
|
|
172
|
+
for g in axis.children("grid") {
|
|
173
|
+
if g.attr("class").unwrap_or("major") == "major" {
|
|
174
|
+
grid_seen = true;
|
|
175
|
+
grid_color = styles.stroke(g.attr("style-name")).flatten().or(Some(Color(0xB3, 0xB3, 0xB3)));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
let mut series = Vec::new();
|
|
181
|
+
let mut markers = false;
|
|
182
|
+
for s in plot.children("series") {
|
|
183
|
+
let values: Vec<Option<f64>> = s.attr("values-cell-range-address").map(|r| grid.cells(r).into_iter().map(|(v, _)| v).collect()).unwrap_or_default();
|
|
184
|
+
let name = s
|
|
185
|
+
.attr("label-cell-address")
|
|
186
|
+
.and_then(|a| grid.cells(a).into_iter().next())
|
|
187
|
+
.map(|(_, t)| t)
|
|
188
|
+
.filter(|t| !t.is_empty());
|
|
189
|
+
let style = s.attr("style-name");
|
|
190
|
+
let color = if kind == ChartKind::Line {
|
|
191
|
+
styles.stroke(style).flatten().or_else(|| styles.fill(style))
|
|
192
|
+
} else {
|
|
193
|
+
styles.fill(style)
|
|
194
|
+
};
|
|
195
|
+
let no_line = kind == ChartKind::Line && matches!(styles.stroke(style), Some(None));
|
|
196
|
+
if kind == ChartKind::Line {
|
|
197
|
+
markers |= styles.chart(style).and_then(|c| c.attr("symbol-type")).is_some_and(|t| t != "none");
|
|
198
|
+
}
|
|
199
|
+
let mut point_colors = Vec::new();
|
|
200
|
+
for point in s.children("data-point") {
|
|
201
|
+
let count = point.attr("repeated").and_then(|v| v.parse::<usize>().ok()).unwrap_or(1).min(4096);
|
|
202
|
+
let fill = styles.fill(point.attr("style-name"));
|
|
203
|
+
for _ in 0..count {
|
|
204
|
+
point_colors.push(fill);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
series.push(ChartSeries { name, values, color, point_colors, no_line });
|
|
208
|
+
}
|
|
209
|
+
if series.is_empty() {
|
|
210
|
+
return None;
|
|
211
|
+
}
|
|
212
|
+
let longest = series.iter().map(|s| s.values.len()).max().unwrap_or(0);
|
|
213
|
+
while categories.len() < longest {
|
|
214
|
+
categories.push((categories.len() + 1).to_string());
|
|
215
|
+
}
|
|
216
|
+
let gap_width = styles.chart(series_style(plot)).and_then(|c| c.attr("gap-width")).and_then(|g| g.parse::<f64>().ok()).unwrap_or(100.0);
|
|
217
|
+
let wall = plot.child("wall").and_then(|w| w.attr("style-name"));
|
|
218
|
+
let style = ChartStyle {
|
|
219
|
+
font: chart
|
|
220
|
+
.child("title")
|
|
221
|
+
.and_then(|t| {
|
|
222
|
+
t.children("p")
|
|
223
|
+
.flat_map(|p| p.children("span"))
|
|
224
|
+
.find_map(|span| styles.font(span.attr("style-name")))
|
|
225
|
+
.or_else(|| styles.font(t.attr("style-name")))
|
|
226
|
+
})
|
|
227
|
+
.or_else(|| styles.font(chart.attr("style-name")))
|
|
228
|
+
.or_else(|| Some("Liberation Sans".into())),
|
|
229
|
+
border: styles.stroke(chart.attr("style-name")).flatten(),
|
|
230
|
+
plot_fill: styles.fill(wall),
|
|
231
|
+
plot_border: styles.stroke(wall).flatten(),
|
|
232
|
+
grid: grid_color,
|
|
233
|
+
no_grid: !grid_seen,
|
|
234
|
+
};
|
|
235
|
+
Some(Chart { title, kind, categories, series, legend, gap_width, markers, style })
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
fn series_style(plot: &Element) -> Option<&str> {
|
|
239
|
+
plot.child("series").and_then(|s| s.attr("style-name"))
|
|
240
|
+
}
|