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
//! 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 {
/// 中の文字を、段落ごとに改行でつないで返す
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>,
}
/// 表
#[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 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)
}