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
//! docx の表
//!
//! 表は行の並び、行はセルの並び、セルは段落や表の並びである。セルの中に表を置けるので、入れ子になる。
use crate::document::Block;
use crate::error::Error;
use crate::paragraph::{self, Paragraph};
use crate::xml::{Event, Reader};
/// セル 1 つ
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Cell {
/// 横につながっているセルの数。1 ならつながっていない
pub span: u32,
/// 中身。セルには必ず段落が 1 つ以上入る
pub block: Vec<Block>,
}
impl Cell {
/// 文字 1 行だけのセル
pub fn new(text: &str) -> Cell {
Cell {span: 1, block: vec![Block::Paragraph(Paragraph::new(text))]}
}
/// 中の文字を、段落ごとに改行でつないで返す
pub fn text(&self) -> String {
let mut result = String::new();
for one in &self.block {
if !result.is_empty() {
result.push('\n');
}
result.push_str(&one.text());
}
result
}
}
/// 行 1 つ
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Row {
pub cell: Vec<Cell>,
}
impl Row {
/// 文字を並べた 1 行
pub fn new(text: &[&str]) -> Row {
Row {cell: text.iter().map(|one| Cell::new(one)).collect()}
}
}
/// 表
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Table {
/// 表の書式の名前 (`<w:tblStyle>`)。ないときは空
pub style: String,
/// 表そのものに枠線の指定 (`<w:tblBorders>`) があるか
///
/// 書式から来る枠線はここに出ない。Word の「表 (格子)」のような書式は
/// 枠線を書式のほうに持つので、読むと false になる。
pub border: bool,
pub row: Vec<Row>,
}
impl Table {
/// 枠線のある表
pub fn new(row: Vec<Row>) -> Table {
Table {style: String::new(), border: true, row}
}
/// いちばん多い行の列数
pub fn width(&self) -> usize {
self.row.iter().map(|one| one.cell.iter().map(|cell| cell.span.max(1) as usize).sum()).max().unwrap_or(0)
}
}
/// `<w:tbl>` を読んだ直後から `</w:tbl>` まで読む
pub fn read(source: &mut Reader) -> Result<Table, Error> {
let mut result = Table::default();
while let Some(event) = source.next()? {
if event == Event::Close {
if source.name() == "tbl" {
return Ok(result);
}
continue;
}
if event == Event::Text {
continue;
}
match source.name() {
"tblStyle" => result.style = source.attribute("val").unwrap_or("").to_string(),
"tblBorders" => result.border = true,
"tr" if event == Event::Open => result.row.push(read_row(source)?),
_ => {}
}
}
Ok(result)
}
/// `<w:tr>` を読んだ直後から `</w:tr>` まで読む
fn read_row(source: &mut Reader) -> Result<Row, Error> {
let mut result = Row::default();
while let Some(event) = source.next()? {
if event == Event::Close {
if source.name() == "tr" {
return Ok(result);
}
continue;
}
if event == Event::Open && source.name() == "tc" {
result.cell.push(read_cell(source)?);
}
}
Ok(result)
}
/// `<w:tc>` を読んだ直後から `</w:tc>` まで読む
fn read_cell(source: &mut Reader) -> Result<Cell, Error> {
let mut result = Cell {span: 1, block: Vec::new()};
while let Some(event) = source.next()? {
if event == Event::Close {
if source.name() == "tc" {
return Ok(result);
}
continue;
}
if event == Event::Text {
continue;
}
match source.name() {
"gridSpan" => {
let value = source.attribute("val").unwrap_or("1");
result.span = value.parse().unwrap_or(1);
}
"p" => {
if event == Event::Empty {
result.block.push(Block::Paragraph(Paragraph::default()));
} else {
result.block.push(Block::Paragraph(paragraph::read(source)?));
}
}
"tbl" if event == Event::Open => result.block.push(Block::Table(read(source)?)),
_ => {}
}
}
Ok(result)
}
/// 表を XML にして足す
pub fn write_into(result: &mut String, one: &Table) {
// docx は tblPr の中の順番が決まっている。tblStyle、tblW、tblBorders の順
result.push_str("<w:tbl><w:tblPr>");
if !one.style.is_empty() {
result.push_str("<w:tblStyle w:val=\"");
crate::xml::escape_into(&one.style, result);
result.push_str("\"/>");
}
result.push_str("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
if one.border {
result.push_str("<w:tblBorders>");
for side in ["top", "left", "bottom", "right", "insideH", "insideV"] {
result.push_str(&format!(
"<w:{side} w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\"/>"
));
}
result.push_str("</w:tblBorders>");
}
result.push_str("</w:tblPr><w:tblGrid>");
for _ in 0..one.width() {
result.push_str("<w:gridCol w:w=\"2000\"/>");
}
result.push_str("</w:tblGrid>");
for row in &one.row {
result.push_str("<w:tr>");
for cell in &row.cell {
result.push_str("<w:tc><w:tcPr><w:tcW w:w=\"0\" w:type=\"auto\"/>");
if cell.span > 1 {
result.push_str(&format!("<w:gridSpan w:val=\"{}\"/>", cell.span));
}
result.push_str("</w:tcPr>");
// セルには段落が 1 つ以上必要である
if cell.block.is_empty() {
result.push_str("<w:p/>");
}
for block in &cell.block {
crate::document::block_into(result, block);
}
result.push_str("</w:tc>");
}
result.push_str("</w:tr>");
}
result.push_str("</w:tbl>");
}