hwpers 0.5.0

A Rust library for parsing Korean Hangul Word Processor (HWP) files with full layout rendering support
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use crate::error::Result;
use crate::parser::body_text::BodyText;
use crate::parser::doc_info::DocInfo;
use crate::parser::header::FileHeader;
use crate::parser::record::Record;
use crate::preview::{PreviewImage, PreviewText, SummaryInfo};

#[derive(Debug)]
pub struct HwpDocument {
    pub header: FileHeader,
    pub doc_info: DocInfo,
    pub body_texts: Vec<BodyText>,
    pub preview_text: Option<PreviewText>,
    pub preview_image: Option<PreviewImage>,
    pub summary_info: Option<SummaryInfo>,
}

impl HwpDocument {
    pub fn sections(&self) -> impl Iterator<Item = &crate::model::Section> {
        self.body_texts.iter().flat_map(|bt| bt.sections.iter())
    }

    pub fn extract_text(&self) -> String {
        let mut result = String::new();

        for body_text in &self.body_texts {
            result.push_str(&body_text.extract_text());
        }

        result
    }

    /// Get a character shape by ID
    pub fn get_char_shape(&self, id: usize) -> Option<&crate::model::CharShape> {
        self.doc_info.char_shapes.get(id)
    }

    /// Get a paragraph shape by ID
    pub fn get_para_shape(&self, id: usize) -> Option<&crate::model::ParaShape> {
        self.doc_info.para_shapes.get(id)
    }

    /// Get a style by ID
    pub fn get_style(&self, id: usize) -> Option<&crate::model::style::Style> {
        self.doc_info.styles.get(id)
    }

    /// Get a border fill by ID
    pub fn get_border_fill(&self, id: usize) -> Option<&crate::model::border_fill::BorderFill> {
        self.doc_info.border_fills.get(id)
    }

    /// Get a tab definition by ID
    pub fn get_tab_def(&self, id: usize) -> Option<&crate::model::tab_def::TabDef> {
        self.doc_info.tab_defs.get(id)
    }

    /// Get a numbering definition by ID
    pub fn get_numbering(&self, id: usize) -> Option<&crate::model::numbering::Numbering> {
        self.doc_info.numberings.get(id)
    }

    /// Get a bullet definition by ID
    pub fn get_bullet(&self, id: usize) -> Option<&crate::model::numbering::Bullet> {
        self.doc_info.bullets.get(id)
    }

    /// Get binary data by ID
    pub fn get_bin_data(&self, id: u16) -> Option<&crate::model::bin_data::BinData> {
        self.doc_info.bin_data.iter().find(|bd| bd.bin_id == id)
    }

    /// Get a font face by ID
    pub fn get_face_name(&self, id: usize) -> Option<&crate::model::FaceName> {
        self.doc_info.face_names.get(id)
    }

    /// Get document properties
    pub fn get_properties(&self) -> Option<&crate::model::DocumentProperties> {
        self.doc_info.properties.as_ref()
    }

    /// Get bin data list for embedded objects
    pub fn get_bin_data_list(&self) -> Option<&Vec<crate::model::bin_data::BinData>> {
        if self.doc_info.bin_data.is_empty() {
            None
        } else {
            Some(&self.doc_info.bin_data)
        }
    }

    /// Extract text with formatting information
    pub fn extract_formatted_text(&self) -> Vec<FormattedText> {
        let mut result = Vec::new();

        for body_text in &self.body_texts {
            for section in &body_text.sections {
                for paragraph in &section.paragraphs {
                    if let Some(para_text) = &paragraph.text {
                        let formatted = FormattedText {
                            text: para_text.content.clone(),
                            char_shape_id: None, // Paragraph doesn't directly have char_shape_id
                            para_shape_id: Some(paragraph.para_shape_id),
                            style_id: Some(paragraph.style_id),
                        };
                        result.push(formatted);
                    }
                }
            }
        }

        result
    }

    /// Get all images in the document
    pub fn get_images(&self) -> Vec<&crate::model::bin_data::BinData> {
        self.doc_info
            .bin_data
            .iter()
            .filter(|bd| bd.is_image())
            .collect()
    }

    /// Get all OLE objects in the document
    pub fn get_ole_objects(&self) -> Vec<&crate::model::bin_data::BinData> {
        self.doc_info
            .bin_data
            .iter()
            .filter(|bd| bd.is_ole_object())
            .collect()
    }

    pub fn preview_text(&self) -> Option<&PreviewText> {
        self.preview_text.as_ref()
    }

    pub fn preview_image(&self) -> Option<&PreviewImage> {
        self.preview_image.as_ref()
    }

    pub fn summary_info(&self) -> Option<&SummaryInfo> {
        self.summary_info.as_ref()
    }

    pub fn title(&self) -> Option<&str> {
        self.summary_info.as_ref().and_then(|s| s.title.as_deref())
    }

    pub fn author(&self) -> Option<&str> {
        self.summary_info.as_ref().and_then(|s| s.author.as_deref())
    }

    pub fn subject(&self) -> Option<&str> {
        self.summary_info
            .as_ref()
            .and_then(|s| s.subject.as_deref())
    }

    pub fn keywords(&self) -> Option<&str> {
        self.summary_info
            .as_ref()
            .and_then(|s| s.keywords.as_deref())
    }

    pub fn is_distribution_document(&self) -> bool {
        self.header.is_distribute()
    }

    pub fn is_encrypted(&self) -> bool {
        self.header.is_encrypted()
    }
}

#[derive(Debug, Default, Clone)]
pub struct DocumentProperties {
    pub section_count: u16,
    pub page_start_number: u16,
    pub footnote_start_number: u16,
    pub endnote_start_number: u16,
    pub picture_start_number: u16,
    pub table_start_number: u16,
    pub equation_start_number: u16,
    pub total_character_count: u32,
    pub hangul_character_count: u32,
    pub english_character_count: u32,
    pub hanja_character_count: u32,
    pub japanese_character_count: u32,
    pub other_character_count: u32,
    pub symbol_character_count: u32,
    pub space_character_count: u32,
    pub total_page_count: u32,
    pub total_word_count: u32,
    pub line_count: u32,
    // Extended metadata fields
    pub document_title: Option<String>,
    pub document_subject: Option<String>,
    pub document_author: Option<String>,
    pub document_company: Option<String>,
    pub document_keywords: Option<String>,
    pub creation_date: Option<chrono::DateTime<chrono::Utc>>,
    pub last_modified_date: Option<chrono::DateTime<chrono::Utc>>,
    pub last_print_date: Option<chrono::DateTime<chrono::Utc>>,
    pub revision_number: u32,
    pub edit_time_minutes: u32,
    pub password_protected: bool,
    pub read_only: bool,
    pub compressed: bool,
}

impl DocumentProperties {
    /// Create new document properties with current timestamp
    pub fn new() -> Self {
        let now = chrono::Utc::now();
        Self {
            section_count: 1,
            page_start_number: 1,
            footnote_start_number: 1,
            endnote_start_number: 1,
            picture_start_number: 1,
            table_start_number: 1,
            equation_start_number: 1,
            total_character_count: 0,
            hangul_character_count: 0,
            english_character_count: 0,
            hanja_character_count: 0,
            japanese_character_count: 0,
            other_character_count: 0,
            symbol_character_count: 0,
            space_character_count: 0,
            total_page_count: 1,
            total_word_count: 0,
            line_count: 0,
            document_title: None,
            document_subject: None,
            document_author: None,
            document_company: None,
            document_keywords: None,
            creation_date: Some(now),
            last_modified_date: Some(now),
            last_print_date: None,
            revision_number: 1,
            edit_time_minutes: 0,
            password_protected: false,
            read_only: false,
            compressed: true, // Default to compressed for smaller files
        }
    }

    /// Set document title
    pub fn set_title(&mut self, title: String) -> &mut Self {
        self.document_title = Some(title);
        self.update_modified_date();
        self
    }

    /// Set document author
    pub fn set_author(&mut self, author: String) -> &mut Self {
        self.document_author = Some(author);
        self.update_modified_date();
        self
    }

    /// Set document subject
    pub fn set_subject(&mut self, subject: String) -> &mut Self {
        self.document_subject = Some(subject);
        self.update_modified_date();
        self
    }

    /// Set document company
    pub fn set_company(&mut self, company: String) -> &mut Self {
        self.document_company = Some(company);
        self.update_modified_date();
        self
    }

    /// Set document keywords
    pub fn set_keywords(&mut self, keywords: String) -> &mut Self {
        self.document_keywords = Some(keywords);
        self.update_modified_date();
        self
    }

    /// Update last modified date to current time
    pub fn update_modified_date(&mut self) {
        self.last_modified_date = Some(chrono::Utc::now());
        self.revision_number += 1;
    }

    /// Mark document as printed
    pub fn mark_printed(&mut self) {
        self.last_print_date = Some(chrono::Utc::now());
    }

    /// Set password protection
    pub fn set_password_protected(&mut self, protected: bool) -> &mut Self {
        self.password_protected = protected;
        self.update_modified_date();
        self
    }

    /// Set read-only status
    pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
        self.read_only = read_only;
        self.update_modified_date();
        self
    }

    /// Set compression
    pub fn set_compressed(&mut self, compressed: bool) -> &mut Self {
        self.compressed = compressed;
        self.update_modified_date();
        self
    }

    /// Calculate and update character counts from text content
    pub fn calculate_character_counts(&mut self, text: &str) {
        self.total_character_count = 0;
        self.hangul_character_count = 0;
        self.english_character_count = 0;
        self.hanja_character_count = 0;
        self.japanese_character_count = 0;
        self.other_character_count = 0;
        self.symbol_character_count = 0;
        self.space_character_count = 0;

        for ch in text.chars() {
            self.total_character_count += 1;

            if ch.is_whitespace() {
                self.space_character_count += 1;
            } else if is_hangul(ch) {
                self.hangul_character_count += 1;
            } else if ch.is_ascii_alphabetic() {
                self.english_character_count += 1;
            } else if is_hanja(ch) {
                self.hanja_character_count += 1;
            } else if is_japanese(ch) {
                self.japanese_character_count += 1;
            } else if ch.is_ascii_punctuation() || ch.is_ascii_digit() {
                self.symbol_character_count += 1;
            } else {
                self.other_character_count += 1;
            }
        }

        // Update word count (approximate)
        self.total_word_count = text.split_whitespace().count() as u32;

        // Update line count
        self.line_count = text.lines().count() as u32;
    }

    /// Add character counts from additional text
    pub fn add_character_counts(&mut self, text: &str) {
        let mut temp_props = DocumentProperties::default();
        temp_props.calculate_character_counts(text);

        self.total_character_count += temp_props.total_character_count;
        self.hangul_character_count += temp_props.hangul_character_count;
        self.english_character_count += temp_props.english_character_count;
        self.hanja_character_count += temp_props.hanja_character_count;
        self.japanese_character_count += temp_props.japanese_character_count;
        self.other_character_count += temp_props.other_character_count;
        self.symbol_character_count += temp_props.symbol_character_count;
        self.space_character_count += temp_props.space_character_count;
        self.total_word_count += temp_props.total_word_count;
        self.line_count += temp_props.line_count;
    }

    /// Convert to HWP format bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        use byteorder::{LittleEndian, WriteBytesExt};
        use std::io::Cursor;

        let mut data = Vec::new();
        let mut writer = Cursor::new(&mut data);

        // Write basic properties
        writer
            .write_u16::<LittleEndian>(self.section_count)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.page_start_number)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.footnote_start_number)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.endnote_start_number)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.picture_start_number)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.table_start_number)
            .unwrap();
        writer
            .write_u16::<LittleEndian>(self.equation_start_number)
            .unwrap();

        // Write character counts
        writer
            .write_u32::<LittleEndian>(self.total_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.hangul_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.english_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.hanja_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.japanese_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.other_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.symbol_character_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.space_character_count)
            .unwrap();

        // Write document metrics
        writer
            .write_u32::<LittleEndian>(self.total_page_count)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.total_word_count)
            .unwrap();
        writer.write_u32::<LittleEndian>(self.line_count).unwrap();

        // Write revision info
        writer
            .write_u32::<LittleEndian>(self.revision_number)
            .unwrap();
        writer
            .write_u32::<LittleEndian>(self.edit_time_minutes)
            .unwrap();

        // Write flags
        let mut flags = 0u32;
        if self.password_protected {
            flags |= 0x01;
        }
        if self.read_only {
            flags |= 0x02;
        }
        if self.compressed {
            flags |= 0x04;
        }
        writer.write_u32::<LittleEndian>(flags).unwrap();

        data
    }
}

/// Check if character is Hangul (Korean)
fn is_hangul(ch: char) -> bool {
    matches!(ch as u32,
        0xAC00..=0xD7AF | // Hangul Syllables
        0x1100..=0x11FF | // Hangul Jamo
        0x3130..=0x318F | // Hangul Compatibility Jamo
        0xA960..=0xA97F | // Hangul Jamo Extended-A
        0xD7B0..=0xD7FF   // Hangul Jamo Extended-B
    )
}

/// Check if character is Hanja (Chinese characters used in Korean)
fn is_hanja(ch: char) -> bool {
    matches!(ch as u32,
        0x4E00..=0x9FFF | // CJK Unified Ideographs
        0x3400..=0x4DBF | // CJK Extension A
        0x20000..=0x2A6DF | // CJK Extension B
        0x2A700..=0x2B73F | // CJK Extension C
        0x2B740..=0x2B81F | // CJK Extension D
        0x2B820..=0x2CEAF | // CJK Extension E
        0x2CEB0..=0x2EBEF   // CJK Extension F
    )
}

/// Check if character is Japanese (Hiragana, Katakana)
fn is_japanese(ch: char) -> bool {
    matches!(ch as u32,
        0x3040..=0x309F | // Hiragana
        0x30A0..=0x30FF | // Katakana
        0x31F0..=0x31FF   // Katakana Phonetic Extensions
    )
}

#[derive(Debug, Clone)]
pub struct FormattedText {
    pub text: String,
    pub char_shape_id: Option<u16>,
    pub para_shape_id: Option<u16>,
    pub style_id: Option<u8>,
}

impl FormattedText {
    /// Get the character formatting for this text
    pub fn get_char_formatting<'a>(
        &self,
        document: &'a HwpDocument,
    ) -> Option<&'a crate::model::CharShape> {
        self.char_shape_id
            .and_then(|id| document.get_char_shape(id as usize))
    }

    /// Get the paragraph formatting for this text
    pub fn get_para_formatting<'a>(
        &self,
        document: &'a HwpDocument,
    ) -> Option<&'a crate::model::ParaShape> {
        self.para_shape_id
            .and_then(|id| document.get_para_shape(id as usize))
    }

    /// Get the style for this text
    pub fn get_style<'a>(
        &self,
        document: &'a HwpDocument,
    ) -> Option<&'a crate::model::style::Style> {
        self.style_id.and_then(|id| document.get_style(id as usize))
    }
}

impl DocumentProperties {
    pub fn from_record(record: &Record) -> Result<Self> {
        let mut reader = record.data_reader();
        let size = reader.remaining();

        // The record size varies. Read what's available
        let mut props = Self::default();

        if size >= 2 {
            props.section_count = reader.read_u16()?;
        }
        if size >= 4 {
            props.page_start_number = reader.read_u16()?;
        }
        if size >= 6 {
            props.footnote_start_number = reader.read_u16()?;
        }
        if size >= 8 {
            props.endnote_start_number = reader.read_u16()?;
        }
        if size >= 10 {
            props.picture_start_number = reader.read_u16()?;
        }
        if size >= 12 {
            props.table_start_number = reader.read_u16()?;
        }
        if size >= 14 {
            props.equation_start_number = reader.read_u16()?;
        }
        if size >= 18 {
            props.total_character_count = reader.read_u32()?;
        }
        if size >= 22 {
            props.hangul_character_count = reader.read_u32()?;
        }
        if size >= 26 {
            props.english_character_count = reader.read_u32()?;
        }
        if size >= 30 {
            props.hanja_character_count = reader.read_u32()?;
        }
        if size >= 34 {
            props.japanese_character_count = reader.read_u32()?;
        }
        if size >= 38 {
            props.other_character_count = reader.read_u32()?;
        }
        if size >= 42 {
            props.symbol_character_count = reader.read_u32()?;
        }
        if size >= 46 {
            props.space_character_count = reader.read_u32()?;
        }
        if size >= 50 {
            props.total_page_count = reader.read_u32()?;
        }
        if size >= 54 {
            props.total_word_count = reader.read_u32()?;
        }
        if size >= 58 {
            props.line_count = reader.read_u32()?;
        }

        Ok(props)
    }
}