Skip to content

Commit

Permalink
feat(hwp): GenShapeObject 파싱 준비 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnlee authored Nov 10, 2022
1 parent 0f8e244 commit cdb0421
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/hwp/src/hwp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod header;
pub mod paragraph;
pub mod section;
pub mod version;
pub mod unknown;

mod parameter_set;
mod record;
Expand Down
4 changes: 1 addition & 3 deletions crates/hwp/src/hwp/paragraph/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod header_footer;
pub mod hidden_comment;
pub mod index_mark;
pub mod number;
pub mod ole;
pub mod over_type;
pub mod page_definition;
pub mod page_hiding;
Expand Down Expand Up @@ -40,7 +39,6 @@ use self::{
index_mark::IndexMark,
number::AutoNumber,
number::NewNumber,
ole::OleControl,
over_type::OverType,
page_hiding::PageHiding,
page_number_control::PageNumberControl,
Expand All @@ -50,7 +48,7 @@ use self::{
arc::ShapeArcControl, container::ContainerControl, curve::ShapeCurveControl,
ellipse::ShapeEllipseControl, gen_shape_object::GenShapeObjectControl,
line::ShapeLineControl, picture::PictureControl, polygon::ShapePolygonControl,
rectangle::ShapeRectangleControl,
rectangle::ShapeRectangleControl, ole::OleControl,
},
sub_text::SubText,
table::TableControl,
Expand Down
46 changes: 45 additions & 1 deletion crates/hwp/src/hwp/paragraph/control/shape_object/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,68 @@ use crate::hwp::{
version::Version,
};

use super::content::{parse_content, GenShapeObjectContent};

/// 묶음 개체
#[derive(Debug, Clone)]
pub struct ContainerControl {
/// 개체 공통 속성
pub common_properties: CommonProperties,
/// 개체 요소 속성
pub element_properties: ElementProperties,
/// 컨텐츠
pub content: ContainerContent,
}

impl ContainerControl {
pub fn from_record(record: &mut Record, cursor: &mut RecordCursor, version: &Version) -> Self {
let common_properties = CommonProperties::from_record(record, cursor, version);
let element_properties = ElementProperties::from_record_cursor(cursor, false);
let content = ContainerContent::from_record_cursor(&element_properties, cursor);

// TODO: (@hahnlee) 남은 데이터 파싱하기
Self {
common_properties,
element_properties,
content,
}
}
}

#[derive(Debug, Clone)]
pub struct ContainerContent {
pub children: Vec<ContainerElement>,
}

impl ContainerContent {
pub fn from_record_cursor(properties: &ElementProperties, cursor: &mut RecordCursor) -> Self {
let children = properties
.children_ids
.as_ref()
.unwrap()
.into_iter()
.map(|_| ContainerElement::from_record_cursor(cursor))
.collect();

Self { children }
}
}

#[derive(Debug, Clone)]
pub struct ContainerElement {
/// 개체 요소 속성
pub element_properties: ElementProperties,
/// 컨텐츠
pub content: GenShapeObjectContent,
}

impl ContainerElement {
pub fn from_record_cursor(cursor: &mut RecordCursor) -> Self {
let element_properties = ElementProperties::from_record_cursor(cursor, false);
let content = parse_content(&element_properties, cursor);

Self {
element_properties,
content,
}
}
}
26 changes: 26 additions & 0 deletions crates/hwp/src/hwp/paragraph/control/shape_object/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use hwp_macro::make_4chid;

use crate::hwp::{
paragraph::control::element_properties::ElementProperties, record::RecordCursor,
unknown::UnknownRecord,
};

use super::container::ContainerContent;

#[derive(Debug, Clone)]
pub enum GenShapeObjectContent {
Container(ContainerContent),
Unknown(UnknownRecord),
}

pub fn parse_content(
properties: &ElementProperties,
cursor: &mut RecordCursor,
) -> GenShapeObjectContent {
match properties.ctrl_id {
make_4chid!('$', 'c', 'o', 'n') => GenShapeObjectContent::Container(
ContainerContent::from_record_cursor(properties, cursor),
),
_ => GenShapeObjectContent::Unknown(UnknownRecord::from_record_cursor(cursor)),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::hwp::{
version::Version,
};

use super::content::{parse_content, GenShapeObjectContent};

/// 그리기 객체
#[derive(Debug, Clone)]
pub struct GenShapeObjectControl {
Expand All @@ -16,6 +18,8 @@ pub struct GenShapeObjectControl {
pub element_properties: ElementProperties,
/// 글상자
pub draw_text: Option<DrawText>,
/// 컨텐츠
pub content: GenShapeObjectContent,
}

impl GenShapeObjectControl {
Expand All @@ -29,14 +33,13 @@ impl GenShapeObjectControl {
None
};

// TODO: (@hahnlee) children 파싱하기
let children = cursor.collect_children(record.level);
assert_ne!(children.len(), 0);
let content = parse_content(&element_properties, cursor);

Self {
common_properties,
element_properties,
draw_text,
content,
}
}
}
2 changes: 2 additions & 0 deletions crates/hwp/src/hwp/paragraph/control/shape_object/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod arc;
pub mod container;
pub mod content;
pub mod curve;
pub mod ellipse;
pub mod gen_shape_object;
pub mod line;
pub mod ole;
pub mod picture;
pub mod polygon;
pub mod rectangle;
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::hwp::{
paragraph::control::{
common_properties::CommonProperties, element_properties::ElementProperties,
},
record::{Record, RecordCursor},
version::Version,
};

use super::{common_properties::CommonProperties, element_properties::ElementProperties};

/// OLE
#[derive(Debug, Clone)]
pub struct OleControl {
Expand Down
4 changes: 1 addition & 3 deletions crates/hwp/src/hwp/paragraph/control/shape_object/picture.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::hwp::{
paragraph::control::{
common_properties::CommonProperties,
element_properties::ElementProperties,
common_properties::CommonProperties, element_properties::ElementProperties,
},
record::{Record, RecordCursor},
version::Version,
Expand All @@ -21,7 +20,6 @@ impl PictureControl {
let common_properties = CommonProperties::from_record(record, cursor, version);
let element_properties = ElementProperties::from_record_cursor(cursor, false);

// TODO: (@hahnlee) 남은 데이터 파싱하기
Self {
common_properties,
element_properties,
Expand Down
32 changes: 32 additions & 0 deletions crates/hwp/src/hwp/unknown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::io::Read;

use crate::hwp::record::{Record, RecordCursor};

#[derive(Debug, Clone)]
pub struct UnknownRecord {
/// 태그 ID
pub tag_id: u32,
/// 데이터
pub data: Vec<u8>,
/// 레코드
pub children: Vec<Record>,
}

impl UnknownRecord {
pub fn from_record_cursor(cursor: &mut RecordCursor) -> Self {
let record = cursor.current();
let mut reader = record.get_data_reader();

let tag_id = record.tag_id;
let mut data = Vec::new();
reader.read_to_end(&mut data).unwrap();

let children = cursor.collect_children(record.level);

Self {
tag_id,
data,
children,
}
}
}

0 comments on commit cdb0421

Please sign in to comment.