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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Style parsing for HWPX documents.
//!
//! Parses character and paragraph styles from HWPML header.xml.
//! Supports both HWPML 2011 elements (charShape, paraShape) and
//! common abbreviations (charPr, paraPr).
use crate::error::Result;
use crate::model::{Alignment, ListStyle, ParagraphStyle, StyleRegistry, TextStyle};
use quick_xml::events::Event;
use quick_xml::Reader;
/// Parses styles from header.xml or styles definition.
///
/// Recognizes both full HWPML element names (charShape, paraShape) and
/// abbreviated forms (charPr, paraPr) for broader compatibility.
pub fn parse_styles(xml: &str, registry: &mut StyleRegistry) -> Result<()> {
let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(true);
let mut buf = Vec::new();
let mut current_char_style: Option<(u32, TextStyle)> = None;
let mut current_para_style: Option<(u32, ParagraphStyle)> = None;
let mut in_char_properties = false;
let mut in_para_properties = false;
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) | Ok(Event::Empty(e)) => {
let local_name = e.local_name();
let name = std::str::from_utf8(local_name.as_ref()).unwrap_or("");
match name {
// Character shape/properties - HWPML uses "charShape" or "charPr"
"charShape" | "charPr" | "charProperties" => {
let id = get_id_attr(&e);
current_char_style = Some((id, TextStyle::default()));
in_char_properties = true;
}
// Paragraph shape/properties - HWPML uses "paraShape" or "paraPr"
"paraShape" | "paraPr" | "paraProperties" => {
let id = get_id_attr(&e);
current_para_style = Some((id, ParagraphStyle::default()));
in_para_properties = true;
}
// Character formatting elements
"bold" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
style.bold = get_bool_attr(&e, "val").unwrap_or(true);
}
}
"italic" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
style.italic = get_bool_attr(&e, "val").unwrap_or(true);
}
}
"underline" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
// Check underline type - any non-none value means underlined
// Use case-insensitive comparison (HWPML uses "NONE", not "none")
let utype = get_string_attr(&e, "type").unwrap_or_default();
style.underline = !utype.eq_ignore_ascii_case("none") && utype != "0";
if utype.is_empty() {
style.underline = true; // Default if just <underline/>
}
}
}
"strikeout" | "strikethrough" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
// Check shape/type attribute - "NONE" or "none" means no strikethrough
let shape = get_string_attr(&e, "shape").unwrap_or_default();
let stype = get_string_attr(&e, "type").unwrap_or_default();
let is_none = shape.eq_ignore_ascii_case("none")
|| stype.eq_ignore_ascii_case("none")
|| shape == "0"
|| stype == "0";
if !is_none {
style.strikethrough = true;
}
}
}
// Superscript/subscript (HWPML uses supscript/subscript in charShape)
"supscript" | "superscript" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
style.superscript = true;
}
}
"subscript" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
style.subscript = true;
}
}
// Font face information
"fontRef" | "font" | "fontface" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
// Try multiple attribute names for font face
if let Some(face) = get_string_attr(&e, "face")
.or_else(|| get_string_attr(&e, "hangul"))
.or_else(|| get_string_attr(&e, "latin"))
{
style.font_name = Some(face);
}
}
}
// Font size - HWPML uses "height" in charShape (in hwpunit = 1/7200 inch)
"sz" | "size" | "height" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
if let Some(size) =
get_float_attr(&e, "val").or_else(|| get_float_attr(&e, "height"))
{
// HWPML height is in hwpunit (1/7200 inch)
// 1 point = 100 hwpunit, so divide by 100
style.font_size = Some(size / 100.0);
}
}
}
// Text color
"color" | "textColor" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
if let Some(color) = get_string_attr(&e, "val")
.or_else(|| get_string_attr(&e, "textColor"))
{
// Handle various color formats
let color_str = if color.starts_with('#') {
color
} else if color.len() == 6 || color.len() == 8 {
format!("#{}", color)
} else {
color
};
style.color = Some(color_str);
}
}
}
// Highlight/shading (background color)
"highlight" | "shd" | "shading" if in_char_properties => {
if let Some((_, ref mut style)) = current_char_style {
if let Some(color) = get_string_attr(&e, "val")
.or_else(|| get_string_attr(&e, "backColor"))
{
style.background_color = Some(format!("#{}", color));
}
}
}
// Paragraph alignment
"align" | "alignment" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
if let Some(align) = get_string_attr(&e, "val")
.or_else(|| get_string_attr(&e, "horizontal"))
{
style.alignment = parse_alignment(&align);
}
}
}
// Heading level / outline level
"outlineLevel" | "heading" | "level" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
if let Some(level) =
get_int_attr(&e, "val").or_else(|| get_int_attr(&e, "level"))
{
// Level 1-6 for headings, 0 means not a heading
if level > 0 {
style.heading_level = (level as u8).min(6);
}
}
}
}
// Indent (HWPML uses indent element with left/right/firstLine)
"indent" | "margin" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
if let Some(level) = get_int_attr(&e, "level")
.or_else(|| get_int_attr(&e, "left").map(|v| v / 850))
{
style.indent_level = level.max(0) as u8;
}
}
}
// Line spacing
"lineSpacing" | "spacing" | "lnSpc" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
if let Some(spacing) =
get_float_attr(&e, "val").or_else(|| get_float_attr(&e, "line"))
{
// HWPML line spacing is in percentage (e.g., 160 = 160%)
style.line_spacing = Some(spacing / 100.0);
}
}
}
// Numbered/bulleted lists
"numbering" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
style.list_style = Some(ListStyle::Ordered);
}
}
"bullet" if in_para_properties => {
if let Some((_, ref mut style)) = current_para_style {
if let Some(char_val) = get_string_attr(&e, "char") {
if let Some(ch) = char_val.chars().next() {
style.list_style = Some(ListStyle::CustomBullet(ch));
}
} else {
style.list_style = Some(ListStyle::Unordered);
}
}
}
_ => {}
}
}
Ok(Event::End(e)) => {
let local_name = e.local_name();
let name = std::str::from_utf8(local_name.as_ref()).unwrap_or("");
match name {
"charShape" | "charPr" | "charProperties" => {
if let Some((id, style)) = current_char_style.take() {
registry.register_char_style(id, style);
}
in_char_properties = false;
}
"paraShape" | "paraPr" | "paraProperties" => {
if let Some((id, style)) = current_para_style.take() {
registry.register_para_style(id, style);
}
in_para_properties = false;
}
_ => {}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(crate::error::Error::XmlParse(e.to_string())),
_ => {}
}
buf.clear();
}
Ok(())
}
/// Parses alignment string to Alignment enum.
fn parse_alignment(align: &str) -> Alignment {
match align.to_lowercase().as_str() {
"left" | "0" => Alignment::Left,
"center" | "1" => Alignment::Center,
"right" | "2" => Alignment::Right,
"justify" | "both" | "3" => Alignment::Justify,
_ => Alignment::Left,
}
}
/// Gets the 'id' attribute as u32.
fn get_id_attr(e: &quick_xml::events::BytesStart) -> u32 {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"id" {
if let Ok(val) = std::str::from_utf8(&attr.value) {
return val.parse().unwrap_or(0);
}
}
}
0
}
/// Gets a boolean attribute value.
fn get_bool_attr(e: &quick_xml::events::BytesStart, name: &str) -> Option<bool> {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == name.as_bytes() {
if let Ok(val) = std::str::from_utf8(&attr.value) {
return Some(val == "1" || val.to_lowercase() == "true");
}
}
}
None
}
/// Gets a string attribute value.
fn get_string_attr(e: &quick_xml::events::BytesStart, name: &str) -> Option<String> {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == name.as_bytes() {
if let Ok(val) = std::str::from_utf8(&attr.value) {
return Some(val.to_string());
}
}
}
None
}
/// Gets a float attribute value.
fn get_float_attr(e: &quick_xml::events::BytesStart, name: &str) -> Option<f32> {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == name.as_bytes() {
if let Ok(val) = std::str::from_utf8(&attr.value) {
return val.parse().ok();
}
}
}
None
}
/// Gets an integer attribute value.
fn get_int_attr(e: &quick_xml::events::BytesStart, name: &str) -> Option<i32> {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == name.as_bytes() {
if let Ok(val) = std::str::from_utf8(&attr.value) {
return val.parse().ok();
}
}
}
None
}