use super::MIN_SHAPE_SIZE;
use crate::document_core::helpers::{get_textbox_from_shape, get_textbox_from_shape_mut};
use crate::document_core::DocumentCore;
use crate::error::HwpError;
use crate::model::control::Control;
use crate::model::event::DocumentEvent;
use crate::model::paragraph::Paragraph;
use crate::model::shape::{common_obj_offsets, ShapeObject};
#[derive(PartialEq, Eq)]
pub(super) struct PictureBandGeometry {
horizontal_offset: i32,
vertical_offset: i32,
width: u32,
height: u32,
horz_rel_to: u8,
vert_rel_to: u8,
horz_align: u8,
vert_align: u8,
treat_as_char: bool,
text_wrap: u8,
margin: (i16, i16, i16, i16),
}
pub(super) fn picture_band_geometry(
common: &crate::model::shape::CommonObjAttr,
) -> PictureBandGeometry {
PictureBandGeometry {
horizontal_offset: common.horizontal_offset as i32,
vertical_offset: common.vertical_offset as i32,
width: common.width,
height: common.height,
horz_rel_to: common.horz_rel_to as u8,
vert_rel_to: common.vert_rel_to as u8,
horz_align: common.horz_align as u8,
vert_align: common.vert_align as u8,
treat_as_char: common.treat_as_char,
text_wrap: common.text_wrap as u8,
margin: (
common.margin.left,
common.margin.right,
common.margin.top,
common.margin.bottom,
),
}
}
impl DocumentCore {
fn resolve_picture_control_ref(
&self,
section_idx: usize,
parent_para_idx: usize,
control_idx: usize,
) -> Result<&crate::model::image::Picture, HwpError> {
let section = self.document.sections.get(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let body_len = section.paragraphs.len();
let para = if parent_para_idx < body_len {
section.paragraphs.get(parent_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
} else {
let mut virtual_idx = parent_para_idx - body_len;
let mut found = None;
'outer: for body_para in §ion.paragraphs {
for ctrl in &body_para.controls {
if let Control::Endnote(en) = ctrl {
if virtual_idx < en.paragraphs.len() {
found = en.paragraphs.get(virtual_idx);
break 'outer;
}
virtual_idx -= en.paragraphs.len();
}
}
}
found.ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
};
let ctrl = para.controls.get(control_idx).ok_or_else(|| {
HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
})?;
match ctrl {
Control::Picture(p) => Ok(p),
Control::Shape(shape) => match shape.as_ref() {
ShapeObject::Picture(p) => Ok(p),
_ => Err(HwpError::RenderError(
"지정된 Shape 컨트롤이 그림이 아닙니다".to_string(),
)),
},
_ => Err(HwpError::RenderError(
"지정된 컨트롤이 그림이 아닙니다".to_string(),
)),
}
}
pub(crate) fn resolve_picture_control_mut(
&mut self,
section_idx: usize,
parent_para_idx: usize,
control_idx: usize,
) -> Result<&mut crate::model::image::Picture, HwpError> {
let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let body_len = section.paragraphs.len();
let para = if parent_para_idx < body_len {
section.paragraphs.get_mut(parent_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
} else {
let mut virtual_idx = parent_para_idx - body_len;
let mut found = None;
'outer: for body_para in &mut section.paragraphs {
for ctrl in &mut body_para.controls {
if let Control::Endnote(en) = ctrl {
if virtual_idx < en.paragraphs.len() {
found = en.paragraphs.get_mut(virtual_idx);
break 'outer;
}
virtual_idx -= en.paragraphs.len();
}
}
}
found.ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
};
let ctrl = para.controls.get_mut(control_idx).ok_or_else(|| {
HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
})?;
match ctrl {
Control::Picture(p) => Ok(p),
Control::Shape(shape) => match shape.as_mut() {
ShapeObject::Picture(p) => Ok(p),
_ => Err(HwpError::RenderError(
"지정된 Shape 컨트롤이 그림이 아닙니다".to_string(),
)),
},
_ => Err(HwpError::RenderError(
"지정된 컨트롤이 그림이 아닙니다".to_string(),
)),
}
}
pub fn get_picture_properties_native(
&self,
section_idx: usize,
parent_para_idx: usize,
control_idx: usize,
) -> Result<String, HwpError> {
let pic = self.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)?;
Self::format_picture_properties_json(pic)
}
fn picture_crop_extent_hu(pic: &crate::model::image::Picture) -> (i32, i32) {
let width = if pic.shape_attr.original_width > 0 {
pic.shape_attr.original_width
} else {
pic.shape_attr.current_width
};
let height = if pic.shape_attr.original_height > 0 {
pic.shape_attr.original_height
} else {
pic.shape_attr.current_height
};
(
i32::try_from(width).unwrap_or(i32::MAX),
i32::try_from(height).unwrap_or(i32::MAX),
)
}
fn picture_crop_ui_amounts(pic: &crate::model::image::Picture) -> (i32, i32, i32, i32) {
let (extent_w, extent_h) = Self::picture_crop_extent_hu(pic);
let left = pic.crop.left.max(0);
let top = pic.crop.top.max(0);
let right = if extent_w > 0 && pic.crop.right > left {
(extent_w - pic.crop.right).max(0)
} else {
0
};
let bottom = if extent_h > 0 && pic.crop.bottom > top {
(extent_h - pic.crop.bottom).max(0)
} else {
0
};
(left, top, right, bottom)
}
fn set_picture_crop_from_ui_amounts(
pic: &mut crate::model::image::Picture,
left: i32,
top: i32,
right: i32,
bottom: i32,
) {
let (extent_w, extent_h) = Self::picture_crop_extent_hu(pic);
pic.crop.left = left.max(0);
pic.crop.top = top.max(0);
if extent_w > 0 {
pic.crop.right = (extent_w - right.max(0)).max(pic.crop.left);
} else {
pic.crop.right = right.max(0);
}
if extent_h > 0 {
pic.crop.bottom = (extent_h - bottom.max(0)).max(pic.crop.top);
} else {
pic.crop.bottom = bottom.max(0);
}
}
fn picture_transform_fingerprint(
pic: &crate::model::image::Picture,
) -> (u32, u32, u32, u32, u32, u32, i16, bool, bool) {
super::common::shape_transform_fingerprint(&pic.common, &pic.shape_attr)
}
pub(crate) fn picture_rotated_bounds(width: u32, height: u32, angle: i16) -> (u32, u32) {
if width == 0 || height == 0 || angle.rem_euclid(360) == 0 {
return (width, height);
}
let theta = (angle as f64).to_radians();
let cos = theta.cos().abs();
let sin = theta.sin().abs();
let rotated_width = width as f64 * cos + height as f64 * sin;
let rotated_height = width as f64 * sin + height as f64 * cos;
(
rotated_width.round().max(1.0) as u32,
rotated_height.round().max(1.0) as u32,
)
}
fn refresh_picture_rotation_layout_for_save(pic: &mut crate::model::image::Picture) {
let cur_w = if pic.shape_attr.current_width > 0 {
pic.shape_attr.current_width
} else {
pic.common.width
};
let cur_h = if pic.shape_attr.current_height > 0 {
pic.shape_attr.current_height
} else {
pic.common.height
};
if cur_w == 0 || cur_h == 0 {
return;
}
pic.shape_attr.current_width = cur_w;
pic.shape_attr.current_height = cur_h;
let old_center_x =
pic.common.horizontal_offset as i32 as i64 + (pic.common.width as i64 / 2);
let old_center_y =
pic.common.vertical_offset as i32 as i64 + (pic.common.height as i64 / 2);
let (bbox_w, bbox_h) =
Self::picture_rotated_bounds(cur_w, cur_h, pic.shape_attr.rotation_angle);
if pic.shape_attr.rotation_angle.rem_euclid(360) != 0 {
pic.common.width = bbox_w;
pic.common.height = bbox_h;
pic.common.horizontal_offset = (old_center_x - (bbox_w as i64 / 2)) as i32 as u32;
pic.common.vertical_offset = (old_center_y - (bbox_h as i64 / 2)) as i32 as u32;
} else {
pic.common.width = cur_w;
pic.common.height = cur_h;
pic.common.horizontal_offset = (old_center_x - (cur_w as i64 / 2)) as i32 as u32;
pic.common.vertical_offset = (old_center_y - (cur_h as i64 / 2)) as i32 as u32;
}
pic.shape_attr.rotation_center.x = (pic.common.width / 2) as i32;
pic.shape_attr.rotation_center.y = (pic.common.height / 2) as i32;
}
fn apply_picture_display_width(pic: &mut crate::model::image::Picture, width: u32) {
let old_common_width = pic.common.width;
let old_current_width = pic.shape_attr.current_width;
pic.common.width = width;
if pic.shape_attr.rotation_angle.rem_euclid(360) != 0
&& old_common_width > 0
&& old_current_width > 0
{
pic.shape_attr.current_width =
((old_current_width as f64 * width as f64 / old_common_width as f64).round())
.max(1.0) as u32;
} else {
pic.shape_attr.current_width = width;
}
}
fn apply_picture_display_height(pic: &mut crate::model::image::Picture, height: u32) {
let old_common_height = pic.common.height;
let old_current_height = pic.shape_attr.current_height;
pic.common.height = height;
if pic.shape_attr.rotation_angle.rem_euclid(360) != 0
&& old_common_height > 0
&& old_current_height > 0
{
pic.shape_attr.current_height =
((old_current_height as f64 * height as f64 / old_common_height as f64).round())
.max(1.0) as u32;
} else {
pic.shape_attr.current_height = height;
}
}
pub fn get_header_footer_picture_properties_native(
&self,
section_idx: usize,
outer_para_idx: usize,
outer_control_idx: usize,
inner_para_idx: usize,
inner_control_idx: usize,
) -> Result<String, HwpError> {
let section = self.document.sections.get(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let outer_para = section.paragraphs.get(outer_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("외부 문단 인덱스 {} 범위 초과", outer_para_idx))
})?;
let outer_ctrl = outer_para.controls.get(outer_control_idx).ok_or_else(|| {
HwpError::RenderError(format!(
"외부 컨트롤 인덱스 {} 범위 초과",
outer_control_idx
))
})?;
let inner_paras: &[crate::model::paragraph::Paragraph] = match outer_ctrl {
crate::model::control::Control::Header(h) => &h.paragraphs,
crate::model::control::Control::Footer(f) => &f.paragraphs,
_ => {
return Err(HwpError::RenderError(
"외부 컨트롤이 머리말/꼬리말이 아닙니다".to_string(),
))
}
};
let inner_para = inner_paras.get(inner_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("내부 문단 인덱스 {} 범위 초과", inner_para_idx))
})?;
let inner_ctrl = inner_para.controls.get(inner_control_idx).ok_or_else(|| {
HwpError::RenderError(format!(
"내부 컨트롤 인덱스 {} 범위 초과",
inner_control_idx
))
})?;
let pic = match inner_ctrl {
crate::model::control::Control::Picture(p) => p,
_ => {
return Err(HwpError::RenderError(
"지정된 내부 컨트롤이 그림이 아닙니다".to_string(),
))
}
};
Self::format_picture_properties_json(pic)
}
pub(crate) fn format_picture_properties_json(
pic: &crate::model::image::Picture,
) -> Result<String, HwpError> {
let c = &pic.common;
let vert_rel = match c.vert_rel_to {
crate::model::shape::VertRelTo::Paper => "Paper",
crate::model::shape::VertRelTo::Page => "Page",
crate::model::shape::VertRelTo::Para => "Para",
};
let vert_align = match c.vert_align {
crate::model::shape::VertAlign::Top => "Top",
crate::model::shape::VertAlign::Center => "Center",
crate::model::shape::VertAlign::Bottom => "Bottom",
crate::model::shape::VertAlign::Inside => "Inside",
crate::model::shape::VertAlign::Outside => "Outside",
};
let horz_rel = match c.horz_rel_to {
crate::model::shape::HorzRelTo::Paper => "Paper",
crate::model::shape::HorzRelTo::Page => "Page",
crate::model::shape::HorzRelTo::Column => "Column",
crate::model::shape::HorzRelTo::Para => "Para",
};
let horz_align = match c.horz_align {
crate::model::shape::HorzAlign::Left => "Left",
crate::model::shape::HorzAlign::Center => "Center",
crate::model::shape::HorzAlign::Right => "Right",
crate::model::shape::HorzAlign::Inside => "Inside",
crate::model::shape::HorzAlign::Outside => "Outside",
};
let text_wrap = match c.text_wrap {
crate::model::shape::TextWrap::Square => "Square",
crate::model::shape::TextWrap::Tight => "Tight",
crate::model::shape::TextWrap::Through => "Through",
crate::model::shape::TextWrap::TopAndBottom => "TopAndBottom",
crate::model::shape::TextWrap::BehindText => "BehindText",
crate::model::shape::TextWrap::InFrontOfText => "InFrontOfText",
};
let effect = match pic.image_attr.effect {
crate::model::image::ImageEffect::RealPic => "RealPic",
crate::model::image::ImageEffect::GrayScale => "GrayScale",
crate::model::image::ImageEffect::BlackWhite => "BlackWhite",
crate::model::image::ImageEffect::Pattern8x8 => "Pattern8x8",
};
let desc_escaped = crate::document_core::helpers::json_escape(&c.description);
let external_path_field = match &pic.image_attr.external_path {
Some(p) => format!(
",\"externalPath\":\"{}\"",
crate::document_core::helpers::json_escape(p)
),
None => String::new(),
};
let sa = &pic.shape_attr;
let (crop_left, crop_top, crop_right, crop_bottom) = Self::picture_crop_ui_amounts(pic);
Ok(format!(
concat!(
"{{\"width\":{},\"height\":{},\"treatAsChar\":{},",
"\"vertRelTo\":\"{}\",\"vertAlign\":\"{}\",",
"\"horzRelTo\":\"{}\",\"horzAlign\":\"{}\",",
"\"vertOffset\":{},\"horzOffset\":{},",
"\"textWrap\":\"{}\",\"restrictInPage\":{},\"allowOverlap\":{},\"sizeProtect\":{},",
"\"brightness\":{},\"contrast\":{},\"effect\":\"{}\",\"transparency\":{},",
"\"description\":\"{}\",",
"\"rotationAngle\":{},\"horzFlip\":{},\"vertFlip\":{},",
"\"originalWidth\":{},\"originalHeight\":{},",
"\"cropLeft\":{},\"cropTop\":{},\"cropRight\":{},\"cropBottom\":{},",
"\"paddingLeft\":{},\"paddingTop\":{},\"paddingRight\":{},\"paddingBottom\":{},",
"\"outerMarginLeft\":{},\"outerMarginTop\":{},\"outerMarginRight\":{},\"outerMarginBottom\":{},",
"\"borderColor\":{},\"borderWidth\":{},",
"\"hasCaption\":{},\"captionDirection\":\"{}\",\"captionVertAlign\":\"{}\",",
"\"captionWidth\":{},\"captionSpacing\":{},\"captionMaxWidth\":{},\"captionIncludeMargin\":{}{}}}"
),
c.width, c.height, c.treat_as_char,
vert_rel, vert_align,
horz_rel, horz_align,
c.vertical_offset as i32, c.horizontal_offset as i32,
text_wrap, c.flow_with_text, c.allow_overlap, c.size_protect,
pic.image_attr.brightness,
pic.image_attr.contrast,
effect,
pic.image_attr.clamped_transparency(),
desc_escaped,
sa.rotation_angle, sa.horz_flip, sa.vert_flip,
sa.original_width, sa.original_height,
crop_left, crop_top, crop_right, crop_bottom,
pic.padding.left, pic.padding.top, pic.padding.right, pic.padding.bottom,
c.margin.left, c.margin.top, c.margin.right, c.margin.bottom,
pic.border_color, pic.border_width,
pic.caption.is_some(),
pic.caption.as_ref().map_or("Bottom", |cap| match cap.direction {
crate::model::shape::CaptionDirection::Left => "Left",
crate::model::shape::CaptionDirection::Right => "Right",
crate::model::shape::CaptionDirection::Top => "Top",
crate::model::shape::CaptionDirection::Bottom => "Bottom",
}),
pic.caption.as_ref().map_or("Top", |cap| match cap.vert_align {
crate::model::shape::CaptionVertAlign::Top => "Top",
crate::model::shape::CaptionVertAlign::Center => "Center",
crate::model::shape::CaptionVertAlign::Bottom => "Bottom",
}),
pic.caption.as_ref().map_or(0u32, |cap| cap.width),
pic.caption.as_ref().map_or(0i16, |cap| cap.spacing),
pic.caption.as_ref().map_or(0u32, |cap| cap.max_width),
pic.caption.as_ref().map_or(false, |cap| cap.include_margin),
external_path_field,
))
}
pub fn set_picture_properties_native(
&mut self,
section_idx: usize,
parent_para_idx: usize,
control_idx: usize,
props_json: &str,
) -> Result<String, HwpError> {
let stored_end_before = self
.document
.sections
.get(section_idx)
.and_then(|section| section.paragraphs.get(parent_para_idx))
.and_then(crate::renderer::composer::paragraph_flow_end);
let band_geometry_before = self
.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)
.ok()
.map(|pic| picture_band_geometry(&pic.common));
let (
caption_created,
caption_removed,
should_migrate_to_inline,
should_migrate_to_floating,
) = {
let pic =
self.resolve_picture_control_mut(section_idx, parent_para_idx, control_idx)?;
let was_tac = pic.common.treat_as_char;
let had_caption = pic.caption.is_some();
let caption_created = Self::apply_picture_props_inner(pic, props_json);
let now_tac = pic.common.treat_as_char;
crate::serializer::control::sync_anchor_bits(&mut pic.common);
(
caption_created,
had_caption && pic.caption.is_none(),
!was_tac && now_tac,
was_tac && !now_tac,
)
};
let mut reflowed_body_end = None;
if should_migrate_to_inline || should_migrate_to_floating {
let styles = self.resolve_render_styles();
let width = self.picture_host_width(section_idx, Some(parent_para_idx))?;
let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let body_len = section.paragraphs.len();
let para = if parent_para_idx < body_len {
section.paragraphs.get_mut(parent_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
} else {
let mut virtual_idx = parent_para_idx - body_len;
let mut found = None;
'outer: for body_para in &mut section.paragraphs {
for ctrl in &mut body_para.controls {
if let Control::Endnote(en) = ctrl {
if virtual_idx < en.paragraphs.len() {
found = en.paragraphs.get_mut(virtual_idx);
break 'outer;
}
virtual_idx -= en.paragraphs.len();
}
}
}
found.ok_or_else(|| {
HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", parent_para_idx))
})?
};
let style = styles.para_styles.get(para.para_shape_id as usize);
let frame = if parent_para_idx < body_len {
crate::renderer::composer::ParagraphBox::body_for_style(width, style, self.dpi)
} else {
crate::renderer::composer::ParagraphBox::content_for_style(width, style, self.dpi)
};
let stored_end = crate::renderer::composer::paragraph_flow_end(para);
let has_text = !para.text.is_empty();
if should_migrate_to_inline {
Self::migrate_picture_floating_to_inline(
para,
control_idx,
frame,
&styles,
self.dpi,
);
} else if para.text.is_empty() && para.char_offsets.is_empty() {
Self::migrate_empty_picture_para_inline_to_floating(para);
} else if has_text {
super::picture_inline_migration::reflow_text_host(para, frame, &styles, self.dpi);
}
if has_text && parent_para_idx < body_len {
reflowed_body_end = Some(stored_end);
}
}
if let Some(stored_end) = reflowed_body_end {
self.finish_body_inline_object_reflow(section_idx, parent_para_idx, stored_end);
}
if caption_created || caption_removed {
crate::parser::assign_auto_numbers(&mut self.document);
}
let band_geometry_after = self
.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)
.ok()
.map(|pic| picture_band_geometry(&pic.common));
if band_geometry_before != band_geometry_after {
let _ = self.apply_body_edit_through_picture_band(section_idx, parent_para_idx, |_| {});
if parent_para_idx < self.document.sections[section_idx].paragraphs.len()
&& self
.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)
.is_ok_and(|picture| picture.common.treat_as_char)
{
self.reflow_paragraph(section_idx, parent_para_idx);
let hwp3_layout = self.document.layout_profile().hwp3_layout();
crate::renderer::composer::recalculate_section_vpos(
&mut self.document.sections[section_idx].paragraphs,
parent_para_idx,
None,
stored_end_before,
&self.styles,
self.dpi,
hwp3_layout,
);
}
}
let section = &mut self.document.sections[section_idx];
section.raw_stream = None;
self.recompose_section(section_idx);
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureResized {
section: section_idx,
para: parent_para_idx,
ctrl: control_idx,
});
if caption_created {
let char_offset = self
.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)?
.caption
.as_ref()
.map_or(0, |c| {
c.paragraphs.first().map_or(0, |p| p.text.chars().count())
});
Ok(format!(
"{{\"ok\":true,\"captionCharOffset\":{}}}",
char_offset
))
} else {
Ok("{\"ok\":true}".to_string())
}
}
pub fn set_header_footer_picture_properties_native(
&mut self,
section_idx: usize,
outer_para_idx: usize,
outer_control_idx: usize,
inner_para_idx: usize,
inner_control_idx: usize,
props_json: &str,
) -> Result<String, HwpError> {
let styles = self.resolve_render_styles();
let width = self.picture_host_width(section_idx, None)?;
let caption_created;
{
let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let outer_para = section.paragraphs.get_mut(outer_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("외부 문단 인덱스 {} 범위 초과", outer_para_idx))
})?;
let outer_ctrl = outer_para
.controls
.get_mut(outer_control_idx)
.ok_or_else(|| {
HwpError::RenderError(format!(
"외부 컨트롤 인덱스 {} 범위 초과",
outer_control_idx
))
})?;
let inner_paras: &mut Vec<crate::model::paragraph::Paragraph> = match outer_ctrl {
crate::model::control::Control::Header(h) => &mut h.paragraphs,
crate::model::control::Control::Footer(f) => &mut f.paragraphs,
_ => {
return Err(HwpError::RenderError(
"외부 컨트롤이 머리말/꼬리말이 아닙니다".to_string(),
))
}
};
let inner_para = inner_paras.get_mut(inner_para_idx).ok_or_else(|| {
HwpError::RenderError(format!("내부 문단 인덱스 {} 범위 초과", inner_para_idx))
})?;
let inner_ctrl = inner_para
.controls
.get_mut(inner_control_idx)
.ok_or_else(|| {
HwpError::RenderError(format!(
"내부 컨트롤 인덱스 {} 범위 초과",
inner_control_idx
))
})?;
let pic = match inner_ctrl {
crate::model::control::Control::Picture(p) => p,
_ => {
return Err(HwpError::RenderError(
"지정된 내부 컨트롤이 그림이 아닙니다".to_string(),
))
}
};
let was_tac = pic.common.treat_as_char;
caption_created = Self::apply_picture_props_inner(pic, props_json);
let now_tac = pic.common.treat_as_char;
crate::serializer::control::sync_anchor_bits(&mut pic.common);
let frame = crate::renderer::composer::ParagraphBox::content_for_style(
width,
styles.para_styles.get(inner_para.para_shape_id as usize),
self.dpi,
);
if !was_tac && now_tac {
Self::migrate_picture_floating_to_inline(
inner_para,
inner_control_idx,
frame,
&styles,
self.dpi,
);
} else if was_tac && !now_tac {
if inner_para.text.is_empty() {
Self::migrate_empty_picture_para_inline_to_floating(inner_para);
} else {
super::picture_inline_migration::reflow_text_host(
inner_para, frame, &styles, self.dpi,
);
}
}
}
if caption_created {
return Err(HwpError::RenderError(
"머리말/꼬리말 그림에 캡션 신규 생성은 본 버전에서 지원하지 않습니다".to_string(),
));
}
let section = &mut self.document.sections[section_idx];
section.raw_stream = None;
self.recompose_section(section_idx);
self.paginate_if_needed();
self.event_log.push(DocumentEvent::PictureResized {
section: section_idx,
para: outer_para_idx,
ctrl: outer_control_idx,
});
Ok("{\"ok\":true}".to_string())
}
pub(crate) fn migrate_empty_picture_para_inline_to_floating(
para: &mut crate::model::paragraph::Paragraph,
) {
if !para.text.is_empty() || !para.char_offsets.is_empty() {
return;
}
let old_seg = para.line_segs.first().cloned().unwrap_or_default();
let line_spacing = if old_seg.line_spacing > 0 {
old_seg.line_spacing
} else {
600
};
let reserved_hu = Self::topbottom_reserved_height_for_empty_picture_para(¶.controls);
let tac_heights = para
.controls
.iter()
.filter_map(Self::tac_control_height_for_empty_picture_para)
.collect::<Vec<_>>();
if tac_heights.is_empty() {
para.line_segs = vec![crate::model::paragraph::LineSeg {
text_start: 0,
vertical_pos: reserved_hu,
line_height: 1000,
text_height: 1000,
baseline_distance: 850,
line_spacing,
segment_width: old_seg.segment_width,
column_start: old_seg.column_start,
tag: old_seg.tag,
}];
return;
}
let mut vpos = reserved_hu;
let mut rebuilt = Vec::with_capacity(tac_heights.len());
for (idx, height) in tac_heights.into_iter().enumerate() {
let line_height = height.max(1);
rebuilt.push(crate::model::paragraph::LineSeg {
text_start: (idx as u32) * 8,
vertical_pos: vpos,
line_height,
text_height: line_height,
baseline_distance: (line_height as f64 * 0.85).round() as i32,
line_spacing,
segment_width: old_seg.segment_width,
column_start: old_seg.column_start,
tag: old_seg.tag,
});
vpos += line_height + line_spacing;
}
para.line_segs = rebuilt;
}
fn tac_control_height_for_empty_picture_para(ctrl: &Control) -> Option<i32> {
match ctrl {
Control::Picture(pic) if pic.common.treat_as_char => Some(pic.common.height as i32),
Control::Shape(shape) if shape.common().treat_as_char => Some(shape.flow_height_hu()),
Control::Table(table) if table.common.treat_as_char => Some(table.common.height as i32),
Control::Equation(eq) if eq.common.treat_as_char => Some(eq.common.height as i32),
_ => None,
}
}
fn topbottom_reserved_height_for_empty_picture_para(controls: &[Control]) -> i32 {
controls
.iter()
.map(|ctrl| match ctrl {
Control::Picture(pic)
if !pic.common.treat_as_char
&& matches!(
pic.common.text_wrap,
crate::model::shape::TextWrap::TopAndBottom
) =>
{
pic.common.height as i32
+ pic.common.margin.top as i32
+ pic.common.margin.bottom as i32
}
Control::Shape(shape)
if !shape.common().treat_as_char
&& matches!(
shape.common().text_wrap,
crate::model::shape::TextWrap::TopAndBottom
) =>
{
let common = shape.common();
common.height as i32 + common.margin.top as i32 + common.margin.bottom as i32
}
Control::Table(table)
if !table.common.treat_as_char
&& matches!(
table.common.text_wrap,
crate::model::shape::TextWrap::TopAndBottom
) =>
{
table.common.height as i32
+ table.outer_margin_top as i32
+ table.outer_margin_bottom as i32
}
_ => 0,
})
.sum()
}
pub(crate) fn take_place_picture_flow_offset(
pic: &crate::model::image::Picture,
) -> Option<i32> {
if pic.common.treat_as_char
|| !matches!(
pic.common.text_wrap,
crate::model::shape::TextWrap::TopAndBottom
)
|| !matches!(pic.common.vert_rel_to, crate::model::shape::VertRelTo::Para)
{
return None;
}
let visual_height = if pic.shape_attr.rotation_angle.rem_euclid(360) != 0
&& pic.shape_attr.current_width > 0
&& pic.shape_attr.current_height > 0
{
pic.common.height
} else {
let (_, height) = Self::picture_rotated_bounds(
pic.common.width,
pic.common.height,
pic.shape_attr.rotation_angle,
);
height
};
Some(
(pic.common.vertical_offset as i32)
.saturating_add(visual_height.min(i32::MAX as u32) as i32)
.max(0),
)
}
pub(crate) fn apply_picture_props_inner(
pic: &mut crate::model::image::Picture,
props_json: &str,
) -> bool {
use crate::document_core::helpers::{json_bool, json_i16, json_i32, json_str, json_u32};
let transform_before = Self::picture_transform_fingerprint(pic);
let mut rotation_changed = false;
if let Some(w) = json_u32(props_json, "width") {
if w != pic.common.width {
Self::apply_picture_display_width(pic, w);
}
}
if let Some(h) = json_u32(props_json, "height") {
if h != pic.common.height {
Self::apply_picture_display_height(pic, h);
}
}
if let Some(tac) = json_bool(props_json, "treatAsChar") {
pic.common.treat_as_char = tac;
if tac {
pic.common.attr |= 0x01;
} else {
pic.common.attr &= !0x01;
}
}
if let Some(v) = json_str(props_json, "vertRelTo") {
pic.common.vert_rel_to = match v.as_str() {
"Paper" => crate::model::shape::VertRelTo::Paper,
"Page" => crate::model::shape::VertRelTo::Page,
"Para" => crate::model::shape::VertRelTo::Para,
_ => pic.common.vert_rel_to,
};
}
if let Some(v) = json_str(props_json, "horzRelTo") {
pic.common.horz_rel_to = match v.as_str() {
"Paper" => crate::model::shape::HorzRelTo::Paper,
"Page" => crate::model::shape::HorzRelTo::Page,
"Column" => crate::model::shape::HorzRelTo::Column,
"Para" => crate::model::shape::HorzRelTo::Para,
_ => pic.common.horz_rel_to,
};
}
if let Some(v) = json_str(props_json, "vertAlign") {
pic.common.vert_align = match v.as_str() {
"Top" => crate::model::shape::VertAlign::Top,
"Center" => crate::model::shape::VertAlign::Center,
"Bottom" => crate::model::shape::VertAlign::Bottom,
_ => pic.common.vert_align,
};
}
if let Some(v) = json_str(props_json, "horzAlign") {
pic.common.horz_align = match v.as_str() {
"Left" => crate::model::shape::HorzAlign::Left,
"Center" => crate::model::shape::HorzAlign::Center,
"Right" => crate::model::shape::HorzAlign::Right,
_ => pic.common.horz_align,
};
}
if let Some(v) = json_str(props_json, "textWrap") {
pic.common.text_wrap = match v.as_str() {
"Square" => crate::model::shape::TextWrap::Square,
"Tight" => crate::model::shape::TextWrap::Tight,
"Through" => crate::model::shape::TextWrap::Through,
"TopAndBottom" => crate::model::shape::TextWrap::TopAndBottom,
"BehindText" => crate::model::shape::TextWrap::BehindText,
"InFrontOfText" => crate::model::shape::TextWrap::InFrontOfText,
_ => pic.common.text_wrap,
};
}
if let Some(v) = json_bool(props_json, "restrictInPage") {
pic.common.flow_with_text = v;
if v {
pic.common.attr |= 1 << 13;
pic.common.allow_overlap = false;
pic.common.attr &= !(1 << 14);
} else {
pic.common.attr &= !(1 << 13);
}
}
if let Some(v) = json_bool(props_json, "allowOverlap") {
pic.common.allow_overlap = v;
if v {
pic.common.attr |= 1 << 14;
} else {
pic.common.attr &= !(1 << 14);
}
}
if let Some(v) = json_bool(props_json, "sizeProtect") {
pic.common.size_protect = v;
if v {
pic.common.attr |= 1 << 20;
} else {
pic.common.attr &= !(1 << 20);
}
}
if let Some(v) = json_i32(props_json, "vertOffset") {
pic.common.vertical_offset = v as u32;
}
if let Some(v) = json_i32(props_json, "horzOffset") {
pic.common.horizontal_offset = v as u32;
}
Self::sync_common_obj_attr_known_bits(&mut pic.common);
if let Some(v) = json_i32(props_json, "brightness") {
pic.image_attr.brightness = v as i8;
}
if let Some(v) = json_i32(props_json, "contrast") {
pic.image_attr.contrast = v as i8;
}
if let Some(v) = json_i32(props_json, "transparency") {
pic.image_attr.transparency = v.clamp(0, 100) as u8;
}
if let Some(v) = json_str(props_json, "effect") {
pic.image_attr.effect = match v.as_str() {
"GrayScale" => crate::model::image::ImageEffect::GrayScale,
"BlackWhite" => crate::model::image::ImageEffect::BlackWhite,
"Pattern8x8" => crate::model::image::ImageEffect::Pattern8x8,
_ => crate::model::image::ImageEffect::RealPic,
};
}
if let Some(v) = json_i16(props_json, "rotationAngle") {
if v != pic.shape_attr.rotation_angle {
pic.shape_attr.rotation_angle = v;
rotation_changed = true;
}
}
if let Some(v) = json_bool(props_json, "horzFlip") {
pic.shape_attr.horz_flip = v;
if v {
pic.shape_attr.flip |= 0x01;
} else {
pic.shape_attr.flip &= !0x01;
}
}
if let Some(v) = json_bool(props_json, "vertFlip") {
pic.shape_attr.vert_flip = v;
if v {
pic.shape_attr.flip |= 0x02;
} else {
pic.shape_attr.flip &= !0x02;
}
}
if rotation_changed {
Self::refresh_picture_rotation_layout_for_save(pic);
}
if Self::picture_transform_fingerprint(pic) != transform_before {
pic.shape_attr.raw_rendering.clear();
let [sx, b, tx, c, sy, ty] = pic.shape_attr.local_reflection_matrix();
pic.shape_attr.render_tx = pic.shape_attr.offset_x as f64 + tx;
pic.shape_attr.render_ty = pic.shape_attr.offset_y as f64 + ty;
pic.shape_attr.render_sx = sx;
pic.shape_attr.render_sy = sy;
pic.shape_attr.render_b = b;
pic.shape_attr.render_c = c;
}
let crop_left = json_i32(props_json, "cropLeft");
let crop_top = json_i32(props_json, "cropTop");
let crop_right = json_i32(props_json, "cropRight");
let crop_bottom = json_i32(props_json, "cropBottom");
if crop_left.is_some()
|| crop_top.is_some()
|| crop_right.is_some()
|| crop_bottom.is_some()
{
let (mut left, mut top, mut right, mut bottom) = Self::picture_crop_ui_amounts(pic);
if let Some(v) = crop_left {
left = v;
}
if let Some(v) = crop_top {
top = v;
}
if let Some(v) = crop_right {
right = v;
}
if let Some(v) = crop_bottom {
bottom = v;
}
Self::set_picture_crop_from_ui_amounts(pic, left, top, right, bottom);
}
if let Some(v) = json_i16(props_json, "paddingLeft") {
pic.padding.left = v;
}
if let Some(v) = json_i16(props_json, "paddingTop") {
pic.padding.top = v;
}
if let Some(v) = json_i16(props_json, "paddingRight") {
pic.padding.right = v;
}
if let Some(v) = json_i16(props_json, "paddingBottom") {
pic.padding.bottom = v;
}
if let Some(v) = json_i16(props_json, "outerMarginLeft") {
pic.common.margin.left = v;
}
if let Some(v) = json_i16(props_json, "outerMarginTop") {
pic.common.margin.top = v;
}
if let Some(v) = json_i16(props_json, "outerMarginRight") {
pic.common.margin.right = v;
}
if let Some(v) = json_i16(props_json, "outerMarginBottom") {
pic.common.margin.bottom = v;
}
if let Some(v) = json_u32(props_json, "borderColor") {
pic.border_color = v;
}
if let Some(v) = json_i32(props_json, "borderWidth") {
pic.border_width = v;
}
if let Some(v) = json_str(props_json, "description") {
pic.common.description = v;
}
let mut caption_created = false;
if let Some(has_cap) = json_bool(props_json, "hasCaption") {
if has_cap {
if pic.caption.is_none() {
let mut cap = crate::model::shape::Caption::default();
let an = crate::model::control::AutoNumber {
number_type: crate::model::control::AutoNumberType::Picture,
..Default::default()
};
cap.paragraphs.push(
crate::model::paragraph::Paragraph::new_auto_number_caption("그림 ", an),
);
cap.max_width = pic.common.width;
pic.caption = Some(cap);
caption_created = true;
pic.common.attr |= 1 << 29;
}
let cap = pic.caption.as_mut().unwrap();
if let Some(v) = json_str(props_json, "captionDirection") {
cap.direction = match v.as_str() {
"Left" => crate::model::shape::CaptionDirection::Left,
"Right" => crate::model::shape::CaptionDirection::Right,
"Top" => crate::model::shape::CaptionDirection::Top,
_ => crate::model::shape::CaptionDirection::Bottom,
};
}
if let Some(v) = json_str(props_json, "captionVertAlign") {
cap.vert_align = match v.as_str() {
"Center" => crate::model::shape::CaptionVertAlign::Center,
"Bottom" => crate::model::shape::CaptionVertAlign::Bottom,
_ => crate::model::shape::CaptionVertAlign::Top,
};
}
if let Some(v) = json_u32(props_json, "captionWidth") {
cap.width = v;
}
if let Some(v) = json_i16(props_json, "captionSpacing") {
cap.spacing = v;
}
if let Some(v) = json_bool(props_json, "captionIncludeMargin") {
cap.include_margin = v;
}
} else {
pic.caption = None;
pic.common.attr &= !(1 << 29);
}
}
caption_created
}
#[allow(clippy::too_many_arguments)]
pub fn relocate_floating_picture_native(
&mut self,
src_section: usize,
src_para: usize,
src_cell_path: &[(usize, usize, usize)],
src_control: usize,
dst_section: usize,
dst_para: usize,
dst_char_offset: usize,
dst_cell_path: &[(usize, usize, usize)],
horz_offset: i32,
vert_offset: i32,
) -> Result<String, HwpError> {
if dst_section >= self.document.sections.len() {
return Err(HwpError::RenderError(format!(
"구역 인덱스 {} 범위 초과",
dst_section
)));
}
Self::resolve_cell_paragraph_mut(
&mut self.document.sections[dst_section],
dst_para,
dst_cell_path,
)?;
let into_table_cell = !dst_cell_path.is_empty()
&& !Self::cell_path_terminates_at_textbox(
&self.document.sections[dst_section],
dst_para,
dst_cell_path,
)?;
let picture = {
let section = self.document.sections.get_mut(src_section).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", src_section))
})?;
let para = Self::resolve_cell_paragraph_mut(section, src_para, src_cell_path)?;
match para.controls.get(src_control) {
Some(Control::Picture(pic)) if !pic.common.treat_as_char => {}
Some(Control::Picture(_)) => {
return Err(HwpError::RenderError(
"글자처럼 취급하는 그림은 옮겨 붙일 수 없습니다".to_string(),
))
}
_ => {
return Err(HwpError::RenderError(
"지정된 컨트롤이 그림이 아닙니다".to_string(),
))
}
}
let removed = para.remove_control(src_control);
Self::reflow_paragraph_line_segs_after_control_delete(para, &self.styles, self.dpi);
section.raw_stream = None;
removed
};
if src_cell_path.is_empty() {
if let Some(active) = self.active_field.as_mut() {
if active.section_idx == src_section
&& active.para_idx == src_para
&& active.cell_path.is_none()
&& active.control_idx > src_control
{
active.control_idx -= 1;
}
}
}
let dst_path = if dst_section == src_section && dst_para == src_para {
Self::path_after_control_removal(src_cell_path, src_control, dst_cell_path)
} else {
dst_cell_path.to_vec()
};
let mut picture = picture;
if let Control::Picture(pic) = &mut picture {
pic.common.horizontal_offset = horz_offset as u32;
pic.common.vertical_offset = vert_offset as u32;
if into_table_cell {
let common = &mut pic.common;
common.vert_rel_to = crate::model::shape::VertRelTo::Para;
common.horz_rel_to = crate::model::shape::HorzRelTo::Column;
common.flow_with_text = true;
common.attr =
(common.attr & !((0x3 << 3) | (0x3 << 8))) | (2 << 3) | (2 << 8) | (1 << 13);
}
}
let new_ctrl_idx = {
let section = &mut self.document.sections[dst_section];
section.raw_stream = None;
let target = Self::resolve_cell_paragraph_mut(section, dst_para, &dst_path)?;
let new_ctrl_idx = Self::control_insert_index(target, dst_char_offset);
target.align_ctrl_data_records();
target.controls.insert(new_ctrl_idx, picture);
target.ctrl_data_records.insert(new_ctrl_idx, None);
target.shift_for_inline_control_insert(new_ctrl_idx, dst_char_offset);
target.control_mask |= 0x00000800;
new_ctrl_idx
};
if let Some(&(table_idx, _, _)) = src_cell_path.first() {
self.mark_cell_control_dirty(src_section, src_para, table_idx);
}
if let Some(&(table_idx, _, _)) = dst_path.first() {
self.mark_cell_control_dirty(dst_section, dst_para, table_idx);
}
for section_idx in [src_section, dst_section] {
self.mark_section_dirty(section_idx);
self.recompose_section(section_idx);
}
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureDeleted {
section: src_section,
para: src_para,
ctrl: src_control,
});
self.event_log.push(DocumentEvent::PictureInserted {
section: dst_section,
para: dst_para,
});
let cell_path: Vec<_> = dst_path
.iter()
.map(|&(control, cell, para)| {
serde_json::json!({
"controlIndex": control, "cellIndex": cell, "cellParaIndex": para
})
})
.collect();
Ok(serde_json::json!({
"ok": true, "sec": dst_section, "paraIdx": dst_para,
"controlIdx": new_ctrl_idx, "cellPath": cell_path
})
.to_string())
}
fn path_after_control_removal(
src_path: &[(usize, usize, usize)],
removed: usize,
dst_path: &[(usize, usize, usize)],
) -> Vec<(usize, usize, usize)> {
let mut path = dst_path.to_vec();
let depth = src_path.len();
if path.len() > depth && path[..depth] == *src_path && path[depth].0 > removed {
path[depth].0 -= 1;
}
path
}
pub fn delete_picture_control_native(
&mut self,
section_idx: usize,
parent_para_idx: usize,
control_idx: usize,
) -> Result<String, HwpError> {
if section_idx >= self.document.sections.len() {
return Err(HwpError::RenderError(format!(
"구역 인덱스 {} 범위 초과",
section_idx
)));
}
let section = &mut self.document.sections[section_idx];
if parent_para_idx >= section.paragraphs.len() {
return Err(HwpError::RenderError(format!(
"부모 문단 인덱스 {} 범위 초과",
parent_para_idx
)));
}
let para = &mut section.paragraphs[parent_para_idx];
if control_idx >= para.controls.len() {
return Err(HwpError::RenderError(format!(
"컨트롤 인덱스 {} 범위 초과",
control_idx
)));
}
if !matches!(
¶.controls[control_idx],
crate::model::control::Control::Picture(_)
) {
return Err(HwpError::RenderError(
"지정된 컨트롤이 그림이 아닙니다".to_string(),
));
}
para.remove_control(control_idx);
if let Some(active) = self.active_field.as_mut() {
if active.section_idx == section_idx
&& active.para_idx == parent_para_idx
&& active.cell_path.is_none()
&& active.control_idx > control_idx
{
active.control_idx -= 1;
}
}
Self::reflow_paragraph_line_segs_after_control_delete(para, &self.styles, self.dpi);
section.raw_stream = None;
self.recompose_section(section_idx);
self.paginate_if_needed();
self.event_log.push(DocumentEvent::PictureDeleted {
section: section_idx,
para: parent_para_idx,
ctrl: control_idx,
});
Ok("{\"ok\":true}".to_string())
}
pub(crate) fn register_embedded_bin_data(&mut self, image_data: &[u8], extension: &str) -> u16 {
crate::model::bin_data::register_embedded_bin_data(
&mut self.document,
image_data,
extension,
)
}
pub fn assign_picture_image_native(
&mut self,
section_idx: usize,
parent_para_idx: usize,
cell_path: &[(usize, usize, usize)],
control_idx: usize,
image_data: &[u8],
natural_width_px: u32,
natural_height_px: u32,
extension: &str,
) -> Result<String, HwpError> {
if image_data.is_empty() {
return Err(HwpError::RenderError(
"이미지 데이터가 비어 있습니다".to_string(),
));
}
if cell_path.is_empty() {
self.resolve_picture_control_ref(section_idx, parent_para_idx, control_idx)?;
} else {
let para = self.resolve_paragraph_by_path(section_idx, parent_para_idx, cell_path)?;
let ctrl = para.controls.get(control_idx).ok_or_else(|| {
HwpError::RenderError(format!("셀 내 컨트롤 {} 범위 초과", control_idx))
})?;
if !matches!(ctrl, Control::Picture(_)) {
return Err(HwpError::RenderError(
"지정된 셀 내 컨트롤이 그림이 아닙니다".to_string(),
));
}
}
let position_id = self.register_embedded_bin_data(image_data, extension);
{
let pic = if cell_path.is_empty() {
self.resolve_picture_control_mut(section_idx, parent_para_idx, control_idx)?
} else {
let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
})?;
let para = Self::resolve_cell_paragraph_mut(section, parent_para_idx, cell_path)?;
let ctrl = para.controls.get_mut(control_idx).ok_or_else(|| {
HwpError::RenderError(format!("셀 내 컨트롤 {} 범위 초과", control_idx))
})?;
match ctrl {
Control::Picture(p) => p,
_ => {
return Err(HwpError::RenderError(
"지정된 셀 내 컨트롤이 그림이 아닙니다".to_string(),
))
}
}
};
pic.image_attr.bin_data_id = position_id;
pic.image_attr.external_path = None;
pic.crop = crate::model::image::CropInfo {
left: 0,
top: 0,
right: (natural_width_px * 75) as i32,
bottom: (natural_height_px * 75) as i32,
};
}
let section = &mut self.document.sections[section_idx];
section.raw_stream = None;
self.recompose_section(section_idx);
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureResized {
section: section_idx,
para: parent_para_idx,
ctrl: control_idx,
});
Ok(format!("{{\"ok\":true,\"binDataId\":{}}}", position_id))
}
#[allow(clippy::too_many_arguments)]
fn control_insert_index(
paragraph: &crate::model::paragraph::Paragraph,
char_offset: usize,
) -> usize {
let positions = crate::document_core::helpers::find_control_text_positions(paragraph);
positions
.iter()
.position(|&pos| pos > char_offset)
.unwrap_or(paragraph.controls.len())
}
fn leave_coordinate_trace(
paragraph: &mut crate::model::paragraph::Paragraph,
control_idx: usize,
char_offset: usize,
) {
paragraph.shift_for_inline_control_insert(control_idx, char_offset);
paragraph.char_count += 8;
}
pub fn insert_picture_native(
&mut self,
section_idx: usize,
para_idx: usize,
char_offset: usize,
cell_path: &[(usize, usize, usize)],
image_data: &[u8],
width: u32,
height: u32,
natural_width_px: u32,
natural_height_px: u32,
extension: &str,
description: &str,
paper_offset_x_hu: Option<i32>,
paper_offset_y_hu: Option<i32>,
) -> Result<String, HwpError> {
self.insert_picture_native_anchored(
section_idx,
para_idx,
char_offset,
cell_path,
image_data,
width,
height,
natural_width_px,
natural_height_px,
extension,
description,
paper_offset_x_hu,
paper_offset_y_hu,
false,
)
}
#[allow(clippy::too_many_arguments)]
pub fn insert_picture_in_cell_native(
&mut self,
section_idx: usize,
para_idx: usize,
char_offset: usize,
cell_path: &[(usize, usize, usize)],
image_data: &[u8],
width: u32,
height: u32,
natural_width_px: u32,
natural_height_px: u32,
extension: &str,
description: &str,
offset_x_hu: Option<i32>,
offset_y_hu: Option<i32>,
) -> Result<String, HwpError> {
if cell_path.is_empty() {
return Err(HwpError::RenderError(
"셀 안 그림 넣기에는 셀 경로가 필요합니다".to_string(),
));
}
self.insert_picture_native_anchored(
section_idx,
para_idx,
char_offset,
cell_path,
image_data,
width,
height,
natural_width_px,
natural_height_px,
extension,
description,
offset_x_hu,
offset_y_hu,
true,
)
}
#[allow(clippy::too_many_arguments)]
fn insert_picture_native_anchored(
&mut self,
section_idx: usize,
para_idx: usize,
char_offset: usize,
cell_path: &[(usize, usize, usize)],
image_data: &[u8],
width: u32,
height: u32,
natural_width_px: u32,
natural_height_px: u32,
extension: &str,
description: &str,
paper_offset_x_hu: Option<i32>,
paper_offset_y_hu: Option<i32>,
anchor_in_cell: bool,
) -> Result<String, HwpError> {
use crate::model::image::Picture;
use crate::model::paragraph::{CharShapeRef, LineSeg};
use crate::model::shape::{CommonObjAttr, HorzRelTo, VertRelTo};
if section_idx >= self.document.sections.len() {
return Err(HwpError::RenderError(format!(
"구역 인덱스 {} 범위 초과 (총 {}개)",
section_idx,
self.document.sections.len()
)));
}
if para_idx >= self.document.sections[section_idx].paragraphs.len() {
return Err(HwpError::RenderError(format!(
"문단 인덱스 {} 범위 초과",
para_idx
)));
}
if image_data.is_empty() {
return Err(HwpError::RenderError(
"이미지 데이터가 비어 있습니다".to_string(),
));
}
let cell_path_is_textbox = if !cell_path.is_empty() {
let section = &self.document.sections[section_idx];
let is_textbox = Self::cell_path_terminates_at_textbox(section, para_idx, cell_path)?;
if !is_textbox {
self.resolve_cell_by_path(section_idx, para_idx, cell_path)?;
}
is_textbox
} else {
false
};
if cell_path_is_textbox || anchor_in_cell {
self.resolve_paragraph_by_path(section_idx, para_idx, cell_path)?;
}
let position_id = self.register_embedded_bin_data(image_data, extension);
if !cell_path.is_empty() {
if cell_path_is_textbox || anchor_in_cell {
let offset_x_hu = paper_offset_x_hu.unwrap_or(0);
let offset_y_hu = paper_offset_y_hu.unwrap_or(0);
let (horz_rel_to, horz_rel_bits) = if cell_path_is_textbox {
(HorzRelTo::Para, 3u32)
} else {
(HorzRelTo::Column, 2u32)
};
let in_table_cell = !cell_path_is_textbox;
let common_attr: u32 = (2 << 3)
| (horz_rel_bits << 8)
| ((in_table_cell as u32) << 13)
| (4 << 15)
| (2 << 18)
| (1 << 26);
let common = CommonObjAttr {
ctrl_id: 0x67736F20,
attr: common_attr,
numbering_type: crate::model::shape::ObjectNumberingType::Picture,
treat_as_char: false,
vert_rel_to: VertRelTo::Para,
horz_rel_to,
text_wrap: crate::model::shape::TextWrap::Square,
flow_with_text: in_table_cell,
horizontal_offset: if cell_path_is_textbox {
offset_x_hu.max(0)
} else {
offset_x_hu
} as u32,
vertical_offset: if cell_path_is_textbox {
offset_y_hu.max(0)
} else {
offset_y_hu
} as u32,
width,
height,
z_order: 1,
description: description.to_string(),
..Default::default()
};
let pic = Picture::from_embedded_image(
common,
position_id,
natural_width_px,
natural_height_px,
);
let (new_ctrl_idx, logical_after) = {
let section = &mut self.document.sections[section_idx];
section.raw_stream = None;
let target_para =
Self::resolve_cell_paragraph_mut(section, para_idx, cell_path)?;
let new_ctrl_idx = Self::control_insert_index(target_para, char_offset);
target_para.align_ctrl_data_records();
target_para
.controls
.insert(new_ctrl_idx, Control::Picture(Box::new(pic)));
target_para.ctrl_data_records.insert(new_ctrl_idx, None);
target_para.shift_for_inline_control_insert(new_ctrl_idx, char_offset);
target_para.control_mask |= 0x00000800;
let logical_positions =
crate::document_core::helpers::find_logical_control_positions(target_para);
let logical_after = logical_positions
.get(new_ctrl_idx)
.copied()
.unwrap_or_else(|| target_para.text.chars().count())
+ 1;
(new_ctrl_idx, logical_after)
};
if !cell_path_is_textbox {
self.mark_cell_control_dirty(section_idx, para_idx, cell_path[0].0);
}
self.mark_section_dirty(section_idx);
self.recompose_section(section_idx);
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureInserted {
section: section_idx,
para: para_idx,
});
return Ok(crate::document_core::helpers::json_ok_with(&format!(
"\"paraIdx\":{},\"controlIdx\":{},\"logicalOffset\":{}",
para_idx, new_ctrl_idx, logical_after
)));
}
let (offset_x_hu, offset_y_hu) = match (paper_offset_x_hu, paper_offset_y_hu) {
(Some(x), Some(y)) => (x, y),
_ => self.compute_cell_page_offset(section_idx, para_idx, cell_path),
};
let common_attr: u32 = (4 << 15) | (2 << 18) | (1 << 26);
let common = CommonObjAttr {
ctrl_id: 0x67736F20,
attr: common_attr,
numbering_type: crate::model::shape::ObjectNumberingType::Picture,
treat_as_char: false,
vert_rel_to: VertRelTo::Paper,
horz_rel_to: HorzRelTo::Paper,
text_wrap: crate::model::shape::TextWrap::Square,
horizontal_offset: offset_x_hu.max(0) as u32,
vertical_offset: offset_y_hu.max(0) as u32,
width,
height,
z_order: 1,
description: description.to_string(),
..Default::default()
};
let pic = Picture::from_embedded_image(
common,
position_id,
natural_width_px,
natural_height_px,
);
self.document.sections[section_idx].raw_stream = None;
let parent = &mut self.document.sections[section_idx].paragraphs[para_idx];
let new_ctrl_idx = parent.controls.len();
parent.controls.push(Control::Picture(Box::new(pic)));
parent.ctrl_data_records.push(None);
let logical_positions =
crate::document_core::helpers::find_logical_control_positions(parent);
let logical_after = logical_positions
.get(new_ctrl_idx)
.copied()
.unwrap_or_else(|| parent.text.chars().count())
+ 1;
let outer_ctrl = cell_path[0].0;
self.mark_cell_control_dirty(section_idx, para_idx, outer_ctrl);
self.mark_section_dirty(section_idx);
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureInserted {
section: section_idx,
para: para_idx,
});
return Ok(crate::document_core::helpers::json_ok_with(&format!(
"\"paraIdx\":{},\"controlIdx\":{},\"logicalOffset\":{}",
para_idx, new_ctrl_idx, logical_after
)));
}
let (offset_x_hu, offset_y_hu) = match (paper_offset_x_hu, paper_offset_y_hu) {
(Some(x), Some(y)) => (x, y),
_ => (0, 0),
};
let common_attr: u32 = (4 << 15) | (2 << 18) | (1 << 26);
let common = CommonObjAttr {
ctrl_id: 0x67736F20, attr: common_attr,
numbering_type: crate::model::shape::ObjectNumberingType::Picture,
treat_as_char: false,
vert_rel_to: VertRelTo::Paper,
horz_rel_to: HorzRelTo::Paper,
text_wrap: crate::model::shape::TextWrap::Square,
horizontal_offset: offset_x_hu.max(0) as u32,
vertical_offset: offset_y_hu.max(0) as u32,
width,
height,
z_order: 1,
description: description.to_string(),
..Default::default()
};
let pic =
Picture::from_embedded_image(common, position_id, natural_width_px, natural_height_px);
self.document.sections[section_idx].raw_stream = None;
let parent = &mut self.document.sections[section_idx].paragraphs[para_idx];
let new_ctrl_idx = Self::control_insert_index(parent, char_offset);
parent.align_ctrl_data_records();
parent
.controls
.insert(new_ctrl_idx, Control::Picture(Box::new(pic)));
parent.ctrl_data_records.insert(new_ctrl_idx, None);
Self::leave_coordinate_trace(parent, new_ctrl_idx, char_offset);
let logical_positions =
crate::document_core::helpers::find_logical_control_positions(parent);
let logical_after = logical_positions
.get(new_ctrl_idx)
.copied()
.unwrap_or_else(|| parent.text.chars().count())
+ 1;
self.shift_active_field_for_control_insert(section_idx, para_idx, new_ctrl_idx);
self.mark_section_dirty(section_idx);
self.paginate_if_needed();
self.invalidate_page_tree_cache();
self.event_log.push(DocumentEvent::PictureInserted {
section: section_idx,
para: para_idx,
});
Ok(crate::document_core::helpers::json_ok_with(&format!(
"\"paraIdx\":{},\"controlIdx\":{},\"logicalOffset\":{}",
para_idx, new_ctrl_idx, logical_after
)))
}
}
#[cfg(test)]
mod issue_1151_cell_picture_insert_tests {
use super::*;
use crate::model::document::{Document, Section, SectionDef};
use crate::model::page::PageDef;
fn make_test_core() -> DocumentCore {
let mut doc = Document::default();
doc.sections.push(Section {
section_def: SectionDef {
page_def: PageDef {
width: 59528,
height: 84188,
margin_left: 8504,
margin_right: 8504,
margin_top: 5668,
margin_bottom: 4252,
margin_header: 4252,
margin_footer: 4252,
..Default::default()
},
..Default::default()
},
paragraphs: vec![Paragraph::default()],
trailing_lists: None,
raw_stream: None,
raw_provenance: None,
});
let mut core = DocumentCore::new_empty();
core.set_document(doc);
core
}
fn minimal_png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
]
}
fn collect_picture_transparencies(doc: &Document) -> Vec<u8> {
let mut values = Vec::new();
for section in &doc.sections {
collect_picture_transparencies_from_paragraphs(§ion.paragraphs, &mut values);
}
values
}
fn collect_picture_transparencies_from_paragraphs(
paragraphs: &[Paragraph],
values: &mut Vec<u8>,
) {
for para in paragraphs {
for control in ¶.controls {
collect_picture_transparencies_from_control(control, values);
}
}
}
fn collect_picture_transparencies_from_control(control: &Control, values: &mut Vec<u8>) {
match control {
Control::Picture(pic) => {
values.push(pic.image_attr.clamped_transparency());
if let Some(caption) = &pic.caption {
collect_picture_transparencies_from_paragraphs(&caption.paragraphs, values);
}
}
Control::Table(table) => {
for cell in &table.cells {
collect_picture_transparencies_from_paragraphs(&cell.paragraphs, values);
}
}
Control::Shape(shape) => collect_picture_transparencies_from_shape(shape, values),
Control::Header(header) => {
collect_picture_transparencies_from_paragraphs(&header.paragraphs, values);
}
Control::Footer(footer) => {
collect_picture_transparencies_from_paragraphs(&footer.paragraphs, values);
}
Control::Footnote(footnote) => {
collect_picture_transparencies_from_paragraphs(&footnote.paragraphs, values);
}
Control::Endnote(endnote) => {
collect_picture_transparencies_from_paragraphs(&endnote.paragraphs, values);
}
_ => {}
}
}
fn collect_picture_transparencies_from_shape(
shape: &crate::model::shape::ShapeObject,
values: &mut Vec<u8>,
) {
match shape {
crate::model::shape::ShapeObject::Picture(pic) => {
values.push(pic.image_attr.clamped_transparency());
if let Some(caption) = &pic.caption {
collect_picture_transparencies_from_paragraphs(&caption.paragraphs, values);
}
}
crate::model::shape::ShapeObject::Group(group) => {
for child in &group.children {
collect_picture_transparencies_from_shape(child, values);
}
if let Some(caption) = &group.caption {
collect_picture_transparencies_from_paragraphs(&caption.paragraphs, values);
}
}
_ => {
if let Some(drawing) = shape.drawing() {
if let Some(text_box) = &drawing.text_box {
collect_picture_transparencies_from_paragraphs(
&text_box.paragraphs,
values,
);
}
if let Some(caption) = &drawing.caption {
collect_picture_transparencies_from_paragraphs(&caption.paragraphs, values);
}
}
}
}
}
fn parse_idx(res: &str, key: &str) -> usize {
res.split(&format!("\"{}\":", key))
.nth(1)
.and_then(|s| s.split(|c: char| !c.is_ascii_digit()).next())
.and_then(|s| s.parse().ok())
.unwrap_or_else(|| panic!("missing {key} in {res}"))
}
#[test]
fn issue1151_insert_picture_into_table_cell_is_floating_sibling() {
let mut core = make_test_core();
let table_res = core
.create_table_native(0, 0, 0, 1, 1)
.expect("create 1x1 table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
5000,
5000,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert picture (floating)");
let table_ctrl =
&core.document.sections[0].paragraphs[table_para_idx].controls[table_ctrl_idx];
let table = match table_ctrl {
Control::Table(t) => t,
_ => panic!("expected Control::Table"),
};
let cell0_para0 = &table.cells[0].paragraphs[0];
assert!(
cell0_para0
.controls
.iter()
.all(|c| !matches!(c, Control::Picture(_))),
"cell 안에 picture 가 들어가면 안 된다 (floating 방식). got: {:?}",
cell0_para0.controls
);
let parent_para = &core.document.sections[0].paragraphs[table_para_idx];
let picture = parent_para
.controls
.iter()
.find_map(|c| match c {
Control::Picture(p) => Some(p.as_ref()),
_ => None,
})
.expect("expected sibling Picture in parent paragraph");
assert!(
!picture.common.treat_as_char,
"floating picture 는 treat_as_char=false 여야 한다"
);
assert!(
matches!(
picture.common.text_wrap,
crate::model::shape::TextWrap::Square
),
"floating picture wrap=Square (어울림) 이어야 한다. got: {:?}",
picture.common.text_wrap
);
}
#[test]
fn issue1151_v9_insert_picture_body_floating_default() {
let mut core = make_test_core();
let image = minimal_png();
core.insert_picture_native(
0,
0,
0,
&[], &image,
5000,
5000,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert picture body");
let body_para = &core.document.sections[0].paragraphs[0];
let pic_in_body = body_para.controls.iter().find_map(|c| match c {
Control::Picture(p) => Some(p.as_ref()),
_ => None,
});
let picture = pic_in_body.expect("expected Picture in body paragraph (sibling control)");
assert!(
!picture.common.treat_as_char,
"본문 picture default = tac=false (한컴 native 정합, v9 결함 E fix)"
);
assert!(
matches!(
picture.common.horz_rel_to,
crate::model::shape::HorzRelTo::Paper
),
"본문 picture horz_rel_to = Paper (셀 분기와 동일)"
);
assert!(
matches!(
picture.common.vert_rel_to,
crate::model::shape::VertRelTo::Paper
),
"본문 picture vert_rel_to = Paper"
);
assert!(matches!(
picture.common.text_wrap,
crate::model::shape::TextWrap::Square
));
assert_eq!(
core.document.sections[0].paragraphs.len(),
1,
"본문 picture 삽입 시 새 paragraph 생성 안 함 (sibling control)"
);
}
#[test]
fn issue1452_insert_picture_returns_logical_offset_after_picture() {
let mut core = make_test_core();
core.insert_text_native(0, 0, 0, "abc")
.expect("insert text");
let image = minimal_png();
let result = core
.insert_picture_native(
0,
0,
3,
&[],
&image,
5000,
5000,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert picture body");
assert_eq!(parse_idx(&result, "paraIdx"), 0);
assert_eq!(parse_idx(&result, "controlIdx"), 0);
assert_eq!(
parse_idx(&result, "logicalOffset"),
4,
"본문 텍스트 'abc' 뒤에 그림 1개를 넣으면 그림 뒤 커서 offset은 4여야 한다: {result}"
);
}
#[test]
fn issue1452_enter_after_dropped_inline_picture_keeps_next_para_below_picture() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image_bboxes(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for child in &node.children {
collect_image_bboxes(child, out);
}
}
fn collect_para_end_runs(
node: &RenderNode,
out: &mut Vec<(usize, Option<usize>, f64, f64, f64, f64)>,
) {
if let RenderNodeType::TextRun(run) = &node.node_type {
if run.is_para_end {
if let Some(para_idx) = run.para_index {
out.push((
para_idx,
run.char_start,
node.bbox.x,
node.bbox.y,
node.bbox.width,
node.bbox.height,
));
}
}
}
for child in &node.children {
collect_para_end_runs(child, out);
}
}
let mut core = make_test_core();
let image = minimal_png();
let pic_w = 30000u32;
let pic_h = 9000u32;
let result = core
.insert_picture_native(
0,
0,
0,
&[],
&image,
pic_w,
pic_h,
1,
1,
"png",
"drop",
None,
None,
)
.expect("insert dropped picture");
let ctrl_idx = parse_idx(&result, "controlIdx");
let logical_offset = parse_idx(&result, "logicalOffset");
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":true}"#)
.expect("dropped picture becomes treat-as-char");
core.split_paragraph_native(0, 0, logical_offset, None)
.expect("Enter after dropped picture");
assert_eq!(
core.document.sections[0].paragraphs.len(),
2,
"그림 뒤 Enter 는 새 빈 문단을 만들어야 한다"
);
assert_eq!(
core.document.sections[0].paragraphs[0].line_segs[0].line_height, pic_h as i32,
"TAC 그림만 남은 첫 문단은 그림 높이를 줄 높이로 유지해야 한다"
);
assert!(
core.document.sections[0].paragraphs[1].line_segs[0].line_height < pic_h as i32 / 2,
"새 빈 문단은 그림 높이를 물려받지 않고 기본 줄 높이로 시작해야 한다"
);
let tree = core.build_page_tree(0).expect("build page tree");
let mut images = Vec::new();
collect_image_bboxes(&tree.root, &mut images);
assert_eq!(images.len(), 1, "drop 그림 ImageNode 1개 필요");
let mut para_ends = Vec::new();
collect_para_end_runs(&tree.root, &mut para_ends);
let image = images[0];
let image_right = image.0 + image.2;
let image_bottom = image.1 + image.3;
let para0_end = para_ends
.iter()
.find(|(para_idx, _, _, _, _, _)| *para_idx == 0)
.expect("첫 문단 끝 표시");
let para1_end = para_ends
.iter()
.find(|(para_idx, _, _, _, _, _)| *para_idx == 1)
.expect("새 빈 문단 끝 표시");
assert_eq!(
para0_end.1,
Some(logical_offset),
"첫 문단 끝 표시는 그림 뒤 logical offset에 놓여야 한다"
);
assert!(
para0_end.2 >= image_right - 0.5,
"첫 문단부호 x는 그림 뒤에 있어야 한다: mark_x={}, image_right={}",
para0_end.2,
image_right
);
assert!(
para1_end.3 >= image_bottom - 0.5,
"새 빈 문단부호는 그림 아래 줄에 있어야 한다: mark_y={}, image_bottom={}",
para1_end.3,
image_bottom
);
}
#[test]
fn issue1452_picture_text_wrap_updates_hwp_attr_bits() {
let mut core = make_test_core();
let image = minimal_png();
core.insert_picture_native(
0,
0,
0,
&[],
&image,
5000,
5000,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert picture body");
{
let pic = match &mut core.document.sections[0].paragraphs[0].controls[0] {
Control::Picture(p) => p.as_mut(),
_ => panic!("expected picture"),
};
pic.common.attr |= 1 << 30;
}
let cases = [
(
"InFrontOfText",
crate::model::shape::TextWrap::InFrontOfText,
3u32,
),
(
"BehindText",
crate::model::shape::TextWrap::BehindText,
2u32,
),
(
"TopAndBottom",
crate::model::shape::TextWrap::TopAndBottom,
1u32,
),
("Square", crate::model::shape::TextWrap::Square, 0u32),
];
for (name, expected_wrap, expected_bits) in cases {
let json = format!(r#"{{"textWrap":"{}"}}"#, name);
core.set_picture_properties_native(0, 0, 0, &json)
.unwrap_or_else(|err| panic!("set textWrap={name} failed: {err}"));
let pic = match &core.document.sections[0].paragraphs[0].controls[0] {
Control::Picture(p) => p.as_ref(),
_ => panic!("expected picture"),
};
assert_eq!(pic.common.text_wrap, expected_wrap);
assert_eq!(
(pic.common.attr >> 21) & 0x07,
expected_bits,
"HWP 저장용 attr textWrap bit가 stale이면 안 된다: {name}"
);
assert_ne!(
pic.common.attr & (1 << 30),
0,
"알 수 없는 원본 attr 비트는 보존되어야 한다"
);
}
}
#[test]
fn issue1452_picture_transparency_props_roundtrip() {
let mut core = make_test_core();
let image = minimal_png();
core.insert_picture_native(
0,
0,
0,
&[],
&image,
5000,
5000,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert picture body");
core.set_picture_properties_native(0, 0, 0, r#"{"transparency":50}"#)
.expect("set transparency");
let props = core
.get_picture_properties_native(0, 0, 0)
.expect("get picture properties");
assert!(
props.contains(r#""transparency":50"#),
"그림 속성 JSON은 투명도 50%를 반환해야 한다: {props}"
);
let pic = match &core.document.sections[0].paragraphs[0].controls[0] {
Control::Picture(p) => p.as_ref(),
_ => panic!("expected picture"),
};
assert_eq!(pic.image_attr.clamped_transparency(), 50);
assert!((pic.image_attr.opacity() - 0.5).abs() < f64::EPSILON);
core.set_picture_properties_native(0, 0, 0, r#"{"transparency":200}"#)
.expect("set clamped transparency");
let pic = match &core.document.sections[0].paragraphs[0].controls[0] {
Control::Picture(p) => p.as_ref(),
_ => panic!("expected picture"),
};
assert_eq!(
pic.image_attr.clamped_transparency(),
100,
"속성 API로 들어온 범위 밖 투명도는 0~100으로 clamp되어야 한다"
);
}
#[test]
fn issue1452_picture_transparency_samples_parse_as_ui_percent() {
for path in ["samples/투명도0-50.hwp", "samples/투명도0-50.hwpx"] {
let data =
std::fs::read(path).unwrap_or_else(|err| panic!("fixture 읽기 실패 {path}: {err}"));
let core =
DocumentCore::from_bytes(&data).unwrap_or_else(|err| panic!("parse {path}: {err}"));
let transparencies = collect_picture_transparencies(&core.document);
assert!(
transparencies.len() >= 2,
"샘플에는 최소 두 개의 그림이 있어야 한다: {path}, got {transparencies:?}"
);
assert_eq!(
&transparencies[..2],
&[0, 50],
"샘플 첫 번째/두 번째 그림 투명도는 각각 0%, 50%여야 한다: {path}"
);
}
}
#[test]
fn issue1452_picture_transparency_samples_render_once_with_opacity() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_images(node: &RenderNode, out: &mut Vec<(Option<usize>, Option<usize>, f64)>) {
if let RenderNodeType::Image(img) = &node.node_type {
out.push((img.para_index, img.control_index, img.opacity));
}
for child in &node.children {
collect_images(child, out);
}
}
for path in ["samples/투명도0-50.hwp", "samples/투명도0-50.hwpx"] {
let data =
std::fs::read(path).unwrap_or_else(|err| panic!("fixture 읽기 실패 {path}: {err}"));
let core =
DocumentCore::from_bytes(&data).unwrap_or_else(|err| panic!("parse {path}: {err}"));
let tree = core
.build_page_tree(0)
.unwrap_or_else(|err| panic!("render tree {path}: {err}"));
let mut images = Vec::new();
collect_images(&tree.root, &mut images);
assert_eq!(
images.len(),
2,
"투명도 샘플의 그림은 두 번만 렌더되어야 한다: {path}, got {images:?}"
);
let mut identities = images
.iter()
.map(|(para, control, _)| (*para, *control))
.collect::<Vec<_>>();
identities.sort_unstable();
identities.dedup();
assert_eq!(
identities.len(),
2,
"같은 그림 control 이 중복 렌더되면 안 된다: {path}, got {images:?}"
);
let mut opacities = images
.iter()
.map(|(_, _, opacity)| (opacity * 100.0).round() as i32)
.collect::<Vec<_>>();
opacities.sort_unstable();
assert_eq!(
opacities,
vec![50, 100],
"렌더 트리 불투명도는 투명도 0/50%를 100/50%로 보존해야 한다: {path}"
);
}
}
#[test]
fn issue1452_enter_after_second_tac_picture_keeps_both_pictures() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_images(node: &RenderNode, out: &mut Vec<(Option<usize>, Option<usize>, f64)>) {
if let RenderNodeType::Image(img) = &node.node_type {
out.push((img.para_index, img.control_index, img.opacity));
}
for child in &node.children {
collect_images(child, out);
}
}
let data = std::fs::read("samples/투명도0-50.hwp")
.expect("fixture 읽기 실패 samples/투명도0-50.hwp");
let mut core = DocumentCore::from_bytes(&data).expect("parse samples/투명도0-50.hwp");
core.split_paragraph_native(0, 0, 2, None)
.expect("두 번째 TAC 그림 뒤 Enter");
assert_eq!(
core.document.sections[0].paragraphs.len(),
2,
"그림 뒤 Enter 는 새 빈 문단을 만들어야 한다"
);
assert!(
core.document.sections[0].paragraphs[0].line_segs.len() >= 2,
"원래 문단은 두 TAC 그림 줄을 유지해야 한다: {:?}",
core.document.sections[0].paragraphs[0].line_segs
);
let tree = core.build_page_tree(0).expect("build page tree");
let mut images = Vec::new();
collect_images(&tree.root, &mut images);
assert_eq!(
images.len(),
2,
"Enter 후에도 두 그림이 모두 렌더되어야 한다: {images:?}"
);
let mut identities = images
.iter()
.map(|(para, control, _)| (*para, *control))
.collect::<Vec<_>>();
identities.sort_unstable();
identities.dedup();
assert_eq!(
identities.len(),
2,
"두 그림 control 이 각각 렌더되어야 한다: {images:?}"
);
}
#[test]
fn issue1151_invalid_cell_path_returns_error() {
let mut core = make_test_core();
let _ = core
.create_table_native(0, 0, 0, 1, 1)
.expect("create table");
let bad_path: Vec<(usize, usize, usize)> = vec![(0, 5, 0)]; let image = minimal_png();
let res = core.insert_picture_native(
0, 0, 0, &bad_path, &image, 5000, 5000, 1, 1, "png", "test", None, None,
);
assert!(
res.is_err(),
"out-of-range cell path → Err 기대, got {res:?}"
);
}
}
#[cfg(test)]
mod issue_1151_v2_tac_toggle_tests {
use super::*;
use crate::model::document::{Document, Section, SectionDef};
use crate::model::image::{ImageAttr, Picture};
use crate::model::page::PageDef;
use crate::model::paragraph::LineSeg;
use crate::model::shape::{CommonObjAttr, HorzRelTo, ShapeComponentAttr, TextWrap, VertRelTo};
fn make_test_core() -> DocumentCore {
let mut doc = Document::default();
doc.sections.push(Section {
section_def: SectionDef {
page_def: PageDef {
width: 59528,
height: 84188,
margin_left: 8504,
margin_right: 8504,
margin_top: 5668,
margin_bottom: 4252,
margin_header: 4252,
margin_footer: 4252,
..Default::default()
},
..Default::default()
},
paragraphs: vec![Paragraph::default()],
trailing_lists: None,
raw_stream: None,
raw_provenance: None,
});
let mut core = DocumentCore::new_empty();
core.set_document(doc);
core
}
fn minimal_png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
]
}
fn parse_idx(res: &str, key: &str) -> usize {
res.split(&format!("\"{}\":", key))
.nth(1)
.and_then(|s| s.split(|c: char| !c.is_ascii_digit()).next())
.and_then(|s| s.parse().ok())
.unwrap_or_else(|| panic!("missing {key} in {res}"))
}
fn push_body_floating_picture(
para: &mut Paragraph,
width_hu: u32,
height_hu: u32,
offset_h: u32,
offset_v: u32,
bin_id: u16,
) -> usize {
let common_attr: u32 = (1 << 3) | (1 << 8) | (4 << 15) | (2 << 18);
let pic = Picture {
common: CommonObjAttr {
ctrl_id: 0x67736F20,
attr: common_attr,
treat_as_char: false,
vert_rel_to: VertRelTo::Paper,
horz_rel_to: HorzRelTo::Paper,
text_wrap: TextWrap::Square,
horizontal_offset: offset_h,
vertical_offset: offset_v,
width: width_hu,
height: height_hu,
z_order: 0,
..Default::default()
},
shape_attr: ShapeComponentAttr {
original_width: width_hu,
original_height: height_hu,
current_width: width_hu,
current_height: height_hu,
..Default::default()
},
border_x: [0i32, 0, width_hu as i32, 0],
border_y: [width_hu as i32, height_hu as i32, 0, height_hu as i32],
image_attr: ImageAttr {
bin_data_id: bin_id,
..Default::default()
},
..Default::default()
};
let idx = para.controls.len();
para.controls.push(Control::Picture(Box::new(pic)));
para.ctrl_data_records.push(None);
idx
}
fn expected_baseline(lh: i32) -> i32 {
(lh as f64 * 0.85).round() as i32
}
#[test]
fn tac_toggle_table_sibling_floating_to_inline() {
let mut core = make_test_core();
let table_res = core
.create_table_native(0, 0, 0, 1, 1)
.expect("create 1x1 table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
let pic_w = 5977u32;
let pic_h = 5331u32;
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
pic_w,
pic_h,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert floating picture in cell");
let pic_ctrl_idx = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
let before_paragraph_count = core.document.sections[0].paragraphs.len();
let before_controls_count = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len();
let res = core.set_picture_properties_native(
0,
table_para_idx,
pic_ctrl_idx,
r#"{"treatAsChar":true}"#,
);
assert!(res.is_ok(), "set_picture_properties_native failed: {res:?}");
let para = &core.document.sections[0].paragraphs[table_para_idx];
let pic = match ¶.controls[pic_ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!("picture not at expected ctrl_idx"),
};
assert_eq!(para.controls.len(), before_controls_count);
assert_eq!(
core.document.sections[0].paragraphs.len(),
before_paragraph_count
);
assert!(pic.common.treat_as_char, "treat_as_char true");
assert_eq!(pic.common.attr & 0x01, 0x01, "attr 비트 0 셋");
assert!(
matches!(pic.common.horz_rel_to, HorzRelTo::Para),
"horz_rel_to=Para, got {:?}",
pic.common.horz_rel_to
);
assert!(
matches!(pic.common.vert_rel_to, VertRelTo::Para),
"vert_rel_to=Para, got {:?}",
pic.common.vert_rel_to
);
assert_eq!(pic.common.horizontal_offset, 0, "h_offset=0");
assert_eq!(pic.common.vertical_offset, 0, "v_offset=0");
let seg = ¶.line_segs[0];
assert_eq!(
seg.line_height, pic_h as i32,
"line_segs[0].line_height = picture height"
);
assert_eq!(
seg.text_height, pic_h as i32,
"line_segs[0].text_height = picture height"
);
assert_eq!(
seg.baseline_distance,
expected_baseline(pic_h as i32),
"line_segs[0].baseline_distance = round(lh*0.85)"
);
assert_eq!(para.text, "");
assert_eq!(para.char_offsets.len(), 0);
}
#[test]
fn v8_cell_floating_picture_uses_paper_rel_to() {
let mut core = make_test_core();
let table_res = core
.create_table_native(0, 0, 0, 1, 1)
.expect("create 1x1 table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
5977,
5331,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert floating picture in cell");
let pic_ctrl_idx = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
let para = &core.document.sections[0].paragraphs[table_para_idx];
let pic = match ¶.controls[pic_ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!("not Picture"),
};
assert!(
matches!(pic.common.horz_rel_to, HorzRelTo::Paper),
"horz_rel_to = Paper (한컴 native default), got {:?}",
pic.common.horz_rel_to
);
assert!(
matches!(pic.common.vert_rel_to, VertRelTo::Paper),
"vert_rel_to = Paper, got {:?}",
pic.common.vert_rel_to
);
let bits_vert = (pic.common.attr >> 3) & 0b11;
let bits_horz = (pic.common.attr >> 8) & 0b111;
assert_eq!(bits_vert, 0, "attr bits 3-4 = Paper(0)");
assert_eq!(bits_horz, 0, "attr bits 8-10 = Paper(0)");
assert!(!pic.common.treat_as_char);
assert!(matches!(
pic.common.text_wrap,
crate::model::shape::TextWrap::Square
));
}
#[test]
fn v9_two_large_pictures_wrap_to_next_line() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image_bboxes(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for child in &node.children {
collect_image_bboxes(child, out);
}
}
let mut core = make_test_core();
let table_res = core.create_table_native(0, 0, 0, 1, 1).expect("table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
let pic_w = 22680u32;
let pic_h = 17010u32;
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
pic_w,
pic_h,
1,
1,
"png",
"p1",
None,
None,
)
.expect("insert pic1");
let pic1_ctrl = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
core.set_picture_properties_native(0, table_para_idx, pic1_ctrl, r#"{"treatAsChar":true}"#)
.expect("toggle pic1");
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
pic_w,
pic_h,
1,
1,
"png",
"p2",
None,
None,
)
.expect("insert pic2");
let pic2_ctrl = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
core.set_picture_properties_native(0, table_para_idx, pic2_ctrl, r#"{"treatAsChar":true}"#)
.expect("toggle pic2");
let tree = core.build_page_tree_cached(0).expect("build");
let mut images = vec![];
collect_image_bboxes(&tree.root, &mut images);
let pic_h_px = pic_h as f64 * 96.0 / 7200.0;
assert_eq!(images.len(), 2, "두 picture 모두 render 되어야 함");
let (x1, y1, _, _) = images[0];
let (x2, y2, _, _) = images[1];
let y_diff = y2 - y1;
assert!(
(y_diff - pic_h_px).abs() < 1.0,
"wrap: y_diff {:.2} ≈ pic_h {:.2} (한 picture height 만큼 진행) — got y1={}, y2={}",
y_diff,
pic_h_px,
y1,
y2
);
assert!(
(x1 - x2).abs() < 1.0,
"wrap: x 동일 (둘 다 새 line 의 좌측) — got x1={}, x2={}",
x1,
x2
);
}
#[test]
fn v9_two_tac_pictures_horizontal_distribute() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image_bboxes(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for child in &node.children {
collect_image_bboxes(child, out);
}
}
let mut core = make_test_core();
let table_res = core.create_table_native(0, 0, 0, 1, 1).expect("table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
5670,
5670,
1,
1,
"png",
"test1",
None,
None,
)
.expect("insert pic1");
let pic1_ctrl = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
core.set_picture_properties_native(0, table_para_idx, pic1_ctrl, r#"{"treatAsChar":true}"#)
.expect("toggle pic1");
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
5670,
5670,
1,
1,
"png",
"test2",
None,
None,
)
.expect("insert pic2");
let pic2_ctrl = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
core.set_picture_properties_native(0, table_para_idx, pic2_ctrl, r#"{"treatAsChar":true}"#)
.expect("toggle pic2");
let tree = core.build_page_tree_cached(0).expect("build page 0");
let mut images = vec![];
collect_image_bboxes(&tree.root, &mut images);
assert_eq!(images.len(), 2, "두 picture 모두 render 되어야 함");
let (x1, y1, w1, _h1) = images[0];
let (x2, y2, _w2, _h2) = images[1];
assert!(
(y1 - y2).abs() < 0.5,
"두 picture y 동일 (가로 분배) — got y1={}, y2={}",
y1,
y2
);
assert!(
x2 > x1 + 0.5,
"두 picture x 다름 (가로 누적) — got x1={}, x2={}",
x1,
x2
);
assert!(
(x2 - (x1 + w1)).abs() < 0.5,
"pic2 x ≈ pic1 x + pic1 width — got x1={}, x2={}, w1={}",
x1,
x2,
w1
);
}
#[test]
fn tac_toggle_body_floating_to_inline() {
let mut core = make_test_core();
let pic_h = 19019u32;
let pic_w = 20863u32;
let ctrl_idx = {
let para = &mut core.document.sections[0].paragraphs[0];
push_body_floating_picture(para, pic_w, pic_h, 13428, 13568, 1)
};
let res = core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":true}"#);
assert!(res.is_ok(), "set_picture_properties_native failed: {res:?}");
let para = &core.document.sections[0].paragraphs[0];
let pic = match ¶.controls[ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!("picture not at expected ctrl_idx"),
};
assert!(pic.common.treat_as_char);
assert!(matches!(pic.common.horz_rel_to, HorzRelTo::Para));
assert!(matches!(pic.common.vert_rel_to, VertRelTo::Para));
assert_eq!(pic.common.horizontal_offset, 0);
assert_eq!(pic.common.vertical_offset, 0);
let seg = ¶.line_segs[0];
assert_eq!(seg.line_height, pic_h as i32);
assert_eq!(seg.text_height, pic_h as i32);
assert_eq!(seg.baseline_distance, expected_baseline(pic_h as i32));
assert_eq!(para.text, "");
assert_eq!(para.char_offsets.len(), 0);
}
#[test]
fn tac_toggle_3x3_center_cell_floating_to_inline() {
let mut core = make_test_core();
let table_res = core
.create_table_native(0, 0, 0, 3, 3)
.expect("create 3x3 table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 1, 1)];
let image = minimal_png();
let pic_w = 5434u32;
let pic_h = 4847u32;
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
pic_w,
pic_h,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert floating picture in center cell");
let pic_ctrl_idx = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
let res = core.set_picture_properties_native(
0,
table_para_idx,
pic_ctrl_idx,
r#"{"treatAsChar":true}"#,
);
assert!(res.is_ok(), "set_picture_properties_native failed: {res:?}");
let para = &core.document.sections[0].paragraphs[table_para_idx];
let pic = match ¶.controls[pic_ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!("picture not at expected ctrl_idx"),
};
assert!(pic.common.treat_as_char);
assert_eq!(pic.common.horizontal_offset, 0);
assert_eq!(pic.common.vertical_offset, 0);
assert_eq!(para.line_segs[0].line_height, pic_h as i32);
assert_eq!(
para.line_segs[0].baseline_distance,
expected_baseline(pic_h as i32)
);
}
#[test]
fn v5_tac_toggle_invalidates_page_tree_and_emits_inline_picture_below_table() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image_bboxes(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for child in &node.children {
collect_image_bboxes(child, out);
}
}
fn collect_table_bboxes(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Table(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for child in &node.children {
collect_table_bboxes(child, out);
}
}
let mut core = make_test_core();
let table_res = core
.create_table_native(0, 0, 0, 1, 1)
.expect("create 1x1 table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
let pic_w = 5977u32;
let pic_h = 5331u32;
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
pic_w,
pic_h,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert floating picture in cell");
let pic_ctrl_idx = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
let tree_before = core
.build_page_tree_cached(0)
.expect("build_page_tree_cached pre-toggle");
let mut image_before: Vec<(f64, f64, f64, f64)> = vec![];
collect_image_bboxes(&tree_before.root, &mut image_before);
assert_eq!(image_before.len(), 1, "toggle 전 ImageNode 1 개 필요");
let (_x0, y_before, _w0, _h0) = image_before[0];
core.set_picture_properties_native(
0,
table_para_idx,
pic_ctrl_idx,
r#"{"treatAsChar":true}"#,
)
.expect("toggle");
let tree_after = core
.build_page_tree_cached(0)
.expect("build_page_tree_cached post-toggle");
let mut image_after: Vec<(f64, f64, f64, f64)> = vec![];
collect_image_bboxes(&tree_after.root, &mut image_after);
let mut table_after: Vec<(f64, f64, f64, f64)> = vec![];
collect_table_bboxes(&tree_after.root, &mut table_after);
assert_eq!(image_after.len(), 1, "toggle 후 ImageNode 1 개 필요");
assert_eq!(table_after.len(), 1, "toggle 후 Table 1 개 필요");
let (_x_a, y_after, _w_a, _h_a) = image_after[0];
let (_tx, ty, _tw, th) = table_after[0];
let table_bottom = ty + th;
assert!(
(y_before - y_after).abs() > 0.5,
"FAIL: page tree cache invalidate 누락 — toggle 후에도 picture y 동일 (before={}, after={})",
y_before,
y_after
);
assert!(
y_after > table_bottom,
"FAIL: picture 가 표 아래에 미배치 — picture y={}, table bottom={}",
y_after,
table_bottom
);
}
#[test]
fn v6_render_tree_scenario_a_after_baseline() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for c in &node.children {
collect_image(c, out);
}
}
fn collect_table(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Table(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for c in &node.children {
collect_table(c, out);
}
}
let bytes = std::fs::read("samples/tac-verify/scenario-a-after.hwp")
.expect("read scenario-a-after.hwp");
let doc = crate::parser::parse_hwp(&bytes).expect("parse scenario-a-after.hwp");
let mut core = DocumentCore::new_empty();
core.set_document(doc);
let tree = core.build_page_tree_cached(0).expect("build page 0");
let mut images = vec![];
let mut tables = vec![];
collect_image(&tree.root, &mut images);
collect_table(&tree.root, &mut tables);
assert_eq!(tables.len(), 1, "한컴 정합 표 1개");
assert_eq!(images.len(), 1, "한컴 정합 picture 1개");
let (_tx, ty, _tw, th) = tables[0];
let (_ix, iy, _iw, _ih) = images[0];
assert!(
iy > ty + th,
"한컴 baseline: picture 가 표 아래 (iy={}, table_bottom={})",
iy,
ty + th
);
}
#[test]
fn v6_resize_cell_then_tac_toggle_picture_below_table() {
use crate::renderer::render_tree::{RenderNode, RenderNodeType};
fn collect_image(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Image(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for c in &node.children {
collect_image(c, out);
}
}
fn collect_table(node: &RenderNode, out: &mut Vec<(f64, f64, f64, f64)>) {
if matches!(node.node_type, RenderNodeType::Table(_)) {
out.push((node.bbox.x, node.bbox.y, node.bbox.width, node.bbox.height));
}
for c in &node.children {
collect_table(c, out);
}
}
let mut core = make_test_core();
let table_res = core.create_table_native(0, 0, 0, 1, 1).expect("table");
let table_para_idx = parse_idx(&table_res, "paraIdx");
let table_ctrl_idx = parse_idx(&table_res, "controlIdx");
core.resize_table_cells_native(
0,
table_para_idx,
table_ctrl_idx,
r#"[{"cellIdx":0,"heightDelta":11216}]"#,
)
.expect("resize cell");
let table =
match &core.document.sections[0].paragraphs[table_para_idx].controls[table_ctrl_idx] {
Control::Table(t) => t,
_ => panic!(),
};
assert_eq!(
table.common.height, 11498,
"v6 fix: table.common.height 가 cell 조절 후 동기화 (raw_ctrl_data 뿐 아니라 self.common 도)"
);
assert_eq!(table.cells[0].height, 11498);
let cell_path: Vec<(usize, usize, usize)> = vec![(table_ctrl_idx, 0, 0)];
let image = minimal_png();
core.insert_picture_native(
0,
table_para_idx,
0,
&cell_path,
&image,
5977,
5331,
1,
1,
"png",
"test",
None,
None,
)
.expect("insert");
let pic_ctrl_idx = core.document.sections[0].paragraphs[table_para_idx]
.controls
.len()
- 1;
core.set_picture_properties_native(
0,
table_para_idx,
pic_ctrl_idx,
r#"{"treatAsChar":true}"#,
)
.expect("toggle");
let tree = core.build_page_tree_cached(0).expect("build page 0");
let mut images = vec![];
let mut tables = vec![];
collect_image(&tree.root, &mut images);
collect_table(&tree.root, &mut tables);
assert_eq!(tables.len(), 1);
assert_eq!(images.len(), 1);
let (_tx, ty, _tw, th) = tables[0];
let (_ix, iy, _iw, _ih) = images[0];
assert!(
iy > ty + th,
"v6 fix: picture 가 표 아래 (iy={}, table_bottom={}) — table.common.height 동기화 정합",
iy,
ty + th
);
}
#[test]
fn tac_toggle_when_already_tac_true_no_migration() {
let mut core = make_test_core();
let pic_h = 5000u32;
let ctrl_idx = {
let para = &mut core.document.sections[0].paragraphs[0];
push_body_floating_picture(para, 5000, pic_h, 1000, 1000, 1)
};
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":true}"#)
.expect("first migration");
let lh_after_first = core.document.sections[0].paragraphs[0].line_segs[0].line_height;
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"brightness":50}"#)
.expect("second call no-op for migration");
let para = &core.document.sections[0].paragraphs[0];
assert_eq!(para.line_segs[0].line_height, lh_after_first);
let pic = match ¶.controls[ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!(),
};
assert_eq!(pic.image_attr.brightness, 50);
}
#[test]
fn tac_toggle_true_to_false_restores_empty_picture_para_line_seg() {
let mut core = make_test_core();
let pic_h = 5000u32;
let ctrl_idx = {
let para = &mut core.document.sections[0].paragraphs[0];
push_body_floating_picture(para, 5000, pic_h, 1000, 1000, 1)
};
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":true}"#)
.expect("forward migration");
let lh_after_forward = core.document.sections[0].paragraphs[0].line_segs[0].line_height;
assert_eq!(lh_after_forward, pic_h as i32);
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":false}"#)
.expect("reverse toggle");
let para = &core.document.sections[0].paragraphs[0];
assert_eq!(para.line_segs.len(), 1);
assert_eq!(
para.line_segs[0].line_height, 1000,
"남은 TAC 개체가 없으면 기본 빈 줄 높이로 복원"
);
assert_eq!(
para.line_segs[0].baseline_distance, 850,
"기본 빈 줄 기준선으로 복원"
);
let pic = match ¶.controls[ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!(),
};
assert!(!pic.common.treat_as_char, "tac 비트는 false 로 토글");
}
#[test]
fn tac_toggle_with_empty_line_segs_creates_new_seg() {
let mut core = make_test_core();
let pic_h = 7000u32;
let ctrl_idx = {
let para = &mut core.document.sections[0].paragraphs[0];
para.line_segs.clear(); push_body_floating_picture(para, 7000, pic_h, 1000, 1000, 1)
};
core.set_picture_properties_native(0, 0, ctrl_idx, r#"{"treatAsChar":true}"#)
.expect("migration");
let para = &core.document.sections[0].paragraphs[0];
assert!(
!para.line_segs.is_empty(),
"빈 line_segs 였다면 신설되어야 한다"
);
let seg = ¶.line_segs[0];
assert_eq!(seg.line_height, pic_h as i32);
assert_eq!(seg.text_height, pic_h as i32);
assert_eq!(seg.baseline_distance, expected_baseline(pic_h as i32));
}
#[test]
#[allow(dead_code)]
fn _lineseg_default_for_test() {
let seg = LineSeg::default();
assert_eq!(seg.line_height, 0);
}
fn assert_toggle_matches_hancom(scenario: &str) {
let before_bytes =
std::fs::read(format!("samples/tac-verify/scenario-{scenario}-before.hwp"))
.expect("read before.hwp");
let after_bytes =
std::fs::read(format!("samples/tac-verify/scenario-{scenario}-after.hwp"))
.expect("read after.hwp");
let before_doc = crate::parser::parse_hwp(&before_bytes).expect("parse before");
let after_doc = crate::parser::parse_hwp(&after_bytes).expect("parse after");
let mut core = DocumentCore::new_empty();
core.set_document(before_doc);
let pic_ctrl_idx = core.document.sections[0].paragraphs[0]
.controls
.iter()
.position(|c| matches!(c, Control::Picture(_)))
.unwrap_or_else(|| panic!("scenario-{scenario}-before: no Picture control"));
core.set_picture_properties_native(0, 0, pic_ctrl_idx, r#"{"treatAsChar":true}"#)
.expect("toggle");
let toggled_para = &core.document.sections[0].paragraphs[0];
let toggled_pic = match &toggled_para.controls[pic_ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!("not Picture after toggle"),
};
let after_para = &after_doc.sections[0].paragraphs[0];
let after_pic_ctrl_idx = after_para
.controls
.iter()
.position(|c| matches!(c, Control::Picture(_)))
.unwrap_or_else(|| panic!("scenario-{scenario}-after: no Picture control"));
let after_pic = match &after_para.controls[after_pic_ctrl_idx] {
Control::Picture(p) => p.as_ref(),
_ => panic!(),
};
assert_eq!(
toggled_pic.common.treat_as_char, after_pic.common.treat_as_char,
"scenario-{scenario}: treat_as_char mismatch"
);
assert_eq!(
toggled_pic.common.horizontal_offset, after_pic.common.horizontal_offset,
"scenario-{scenario}: horizontal_offset mismatch"
);
assert_eq!(
toggled_pic.common.vertical_offset, after_pic.common.vertical_offset,
"scenario-{scenario}: vertical_offset mismatch"
);
assert_eq!(
toggled_pic.common.horz_rel_to as u8, after_pic.common.horz_rel_to as u8,
"scenario-{scenario}: horz_rel_to mismatch"
);
assert_eq!(
toggled_pic.common.vert_rel_to as u8, after_pic.common.vert_rel_to as u8,
"scenario-{scenario}: vert_rel_to mismatch"
);
let toggled_seg = &toggled_para.line_segs[0];
let after_seg = &after_para.line_segs[0];
assert_eq!(
toggled_seg.line_height, after_seg.line_height,
"scenario-{scenario}: line_height mismatch"
);
assert_eq!(
toggled_seg.text_height, after_seg.text_height,
"scenario-{scenario}: text_height mismatch"
);
assert_eq!(
toggled_seg.baseline_distance, after_seg.baseline_distance,
"scenario-{scenario}: baseline_distance mismatch (round(lh*0.85) 정합)"
);
assert_eq!(
core.document.sections[0].paragraphs.len(),
after_doc.sections[0].paragraphs.len(),
"scenario-{scenario}: paragraph count mismatch"
);
assert_eq!(
pic_ctrl_idx, after_pic_ctrl_idx,
"scenario-{scenario}: picture control_idx mismatch"
);
assert_eq!(
toggled_para.text, after_para.text,
"scenario-{scenario}: paragraph.text mismatch"
);
}
#[test]
fn integration_tac_toggle_matches_hancom_scenario_a() {
assert_toggle_matches_hancom("a");
}
#[test]
fn integration_tac_toggle_matches_hancom_scenario_b() {
assert_toggle_matches_hancom("b");
}
#[test]
fn integration_tac_toggle_matches_hancom_scenario_c() {
assert_toggle_matches_hancom("c");
}
#[test]
fn integration_tac_toggle_matches_hancom_scenario_d() {
assert_toggle_matches_hancom("d");
}
}
#[cfg(test)]
mod issue_1280_textbox_creation_tests {
use super::*;
use crate::model::document::{Document, Section, SectionDef};
use crate::model::page::PageDef;
fn make_test_core() -> DocumentCore {
let mut doc = Document::default();
doc.sections.push(Section {
section_def: SectionDef {
page_def: PageDef {
width: 59528,
height: 84188,
margin_left: 8504,
margin_right: 8504,
margin_top: 5668,
margin_bottom: 4252,
margin_header: 4252,
margin_footer: 4252,
..Default::default()
},
..Default::default()
},
paragraphs: vec![Paragraph::default()],
trailing_lists: None,
raw_stream: None,
raw_provenance: None,
});
let mut core = DocumentCore::new_empty();
core.set_document(doc);
core
}
fn parse_idx(res: &str, key: &str) -> usize {
res.split(&format!("\"{}\":", key))
.nth(1)
.and_then(|s| s.split(|c: char| !c.is_ascii_digit()).next())
.and_then(|s| s.parse().ok())
.unwrap_or_else(|| panic!("missing {key} in {res}"))
}
fn minimal_png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
]
}
fn create_shape(core: &mut DocumentCore, shape_type: &str) -> (usize, usize) {
let treat_as_char = shape_type == "textbox";
let res = core
.create_shape_control_native(
0,
0,
0,
21600,
7200,
0,
0,
treat_as_char,
"TopAndBottom",
shape_type,
false,
false,
&[],
)
.unwrap_or_else(|e| panic!("create {shape_type} failed: {e:?}"));
(parse_idx(&res, "paraIdx"), parse_idx(&res, "controlIdx"))
}
fn textbox_of<'a>(
core: &'a DocumentCore,
para_idx: usize,
ctrl_idx: usize,
) -> Option<&'a crate::model::shape::TextBox> {
match &core.document.sections[0].paragraphs[para_idx].controls[ctrl_idx] {
Control::Shape(s) => crate::document_core::helpers::get_textbox_from_shape(s.as_ref()),
other => panic!("expected Control::Shape, got {other:?}"),
}
}
fn common_of<'a>(
core: &'a DocumentCore,
para_idx: usize,
ctrl_idx: usize,
) -> &'a crate::model::shape::CommonObjAttr {
match &core.document.sections[0].paragraphs[para_idx].controls[ctrl_idx] {
Control::Shape(s) => s.common(),
other => panic!("expected Control::Shape, got {other:?}"),
}
}
fn create_textbox_with(
core: &mut DocumentCore,
treat_as_char: bool,
text_wrap: &str,
) -> (usize, usize) {
let res = core
.create_shape_control_native(
0,
0,
0,
21600,
7200,
1000,
2000,
treat_as_char,
text_wrap,
"textbox",
false,
false,
&[],
)
.unwrap_or_else(|e| panic!("create textbox failed: {e:?}"));
(parse_idx(&res, "paraIdx"), parse_idx(&res, "controlIdx"))
}
#[test]
fn create_floating_textbox_is_in_front_paper() {
use crate::model::shape::{HorzRelTo, TextWrap, VertRelTo};
let mut core = make_test_core();
let (para, ctrl) = create_textbox_with(&mut core, false, "InFrontOfText");
assert!(
textbox_of(&core, para, ctrl).is_some(),
"floating 글상자도 text_box 를 가져야 한다"
);
let c = common_of(&core, para, ctrl);
assert!(!c.treat_as_char, "floating: treat_as_char=false");
assert_eq!(
c.text_wrap,
TextWrap::InFrontOfText,
"글앞으로(InFrontOfText)"
);
assert_eq!(c.vert_rel_to, VertRelTo::Paper, "vert_rel_to=Paper");
assert_eq!(c.horz_rel_to, HorzRelTo::Paper, "horz_rel_to=Paper");
assert_eq!(c.attr & 0x01, 0, "attr bit0(treat_as_char)=0");
assert_eq!((c.attr >> 3) & 0x03, 0, "attr bit3-4(vert_rel_to)=Paper(0)");
assert_eq!((c.attr >> 8) & 0x03, 0, "attr bit8-9(horz_rel_to)=Paper(0)");
assert_eq!(
(c.attr >> 21) & 0x07,
3,
"attr bit21-23(text_wrap)=InFrontOfText(3)"
);
}
#[test]
fn create_inline_textbox_preserves_para_column() {
use crate::model::shape::{HorzRelTo, VertRelTo};
let mut core = make_test_core();
let (para, ctrl) = create_textbox_with(&mut core, true, "Square");
let c = common_of(&core, para, ctrl);
assert!(c.treat_as_char, "inline: treat_as_char=true");
assert_eq!(c.vert_rel_to, VertRelTo::Para, "inline vert_rel_to=Para");
assert_eq!(
c.horz_rel_to,
HorzRelTo::Column,
"inline horz_rel_to=Column"
);
}
#[test]
fn insert_text_into_floating_textbox() {
let mut core = make_test_core();
let (para, ctrl) = create_textbox_with(&mut core, false, "InFrontOfText");
core.insert_text_in_cell_native(0, para, ctrl, 0, 0, 0, "플로팅")
.expect("floating 글상자 텍스트 입력 성공");
let tb = textbox_of(&core, para, ctrl).expect("text_box 존재");
assert_eq!(
tb.paragraphs[0].text, "플로팅",
"floating 글상자 내부 텍스트 보존"
);
}
#[test]
fn insert_picture_into_textbox_uses_textbox_paragraph_control() {
use crate::model::shape::{HorzRelTo, TextWrap, VertRelTo};
let mut core = make_test_core();
let (para, ctrl) = create_textbox_with(&mut core, false, "InFrontOfText");
let body_control_count_before = core.document.sections[0].paragraphs[para].controls.len();
let cell_path = vec![(ctrl, 0, 0)];
let image = minimal_png();
core.insert_picture_native(
0,
para,
0,
&cell_path,
&image,
5000,
4000,
1,
1,
"png",
"textbox picture",
Some(750),
Some(1500),
)
.expect("글상자 내부 picture 삽입 성공");
let body = &core.document.sections[0].paragraphs[para];
assert_eq!(
body.controls.len(),
body_control_count_before,
"글상자 내부 삽입은 body sibling control 을 추가하면 안 된다"
);
let tb = textbox_of(&core, para, ctrl).expect("글상자 text_box 존재");
let picture = tb.paragraphs[0]
.controls
.iter()
.find_map(|c| match c {
Control::Picture(p) => Some(p.as_ref()),
_ => None,
})
.expect("글상자 내부 문단에 Picture control 이 있어야 한다");
assert!(!picture.common.treat_as_char);
assert_eq!(picture.common.horz_rel_to, HorzRelTo::Para);
assert_eq!(picture.common.vert_rel_to, VertRelTo::Para);
assert_eq!(picture.common.text_wrap, TextWrap::Square);
assert_eq!(picture.common.horizontal_offset, 750);
assert_eq!(picture.common.vertical_offset, 1500);
assert_eq!(picture.common.width, 5000);
assert_eq!(picture.common.height, 4000);
}
#[test]
fn create_textbox_has_textbox() {
let mut core = make_test_core();
let (para, ctrl) = create_shape(&mut core, "textbox");
assert!(
textbox_of(&core, para, ctrl).is_some(),
"글상자(shape_type=textbox)는 text_box를 가져야 한다 (#1280)"
);
}
#[test]
fn create_rectangle_has_no_textbox() {
let mut core = make_test_core();
let (para, ctrl) = create_shape(&mut core, "rectangle");
assert!(
textbox_of(&core, para, ctrl).is_none(),
"일반 사각형(shape_type=rectangle)은 text_box가 없어야 한다 (글상자/사각형 경로 분리)"
);
}
#[test]
fn insert_text_into_created_textbox() {
let mut core = make_test_core();
let (para, ctrl) = create_shape(&mut core, "textbox");
core.insert_text_in_cell_native(0, para, ctrl, 0, 0, 0, "테스트")
.expect("글상자에 텍스트 입력이 성공해야 한다 (#1280)");
let tb = textbox_of(&core, para, ctrl).expect("글상자 text_box 존재");
assert_eq!(
tb.paragraphs[0].text, "테스트",
"글상자 내부 첫 문단에 입력 텍스트가 보존되어야 한다"
);
}
#[test]
fn paste_text_into_textbox() {
let mut core = make_test_core();
core.insert_text_native(0, 0, 0, "복사원본")
.expect("본문 텍스트 입력");
core.copy_selection_native(0, 0, 0, 0, 4)
.expect("본문 텍스트 복사");
let (tb_para, tb_ctrl) = create_shape(&mut core, "textbox");
core.paste_internal_in_cell_native(0, tb_para, tb_ctrl, 0, 0, 0)
.expect("글상자에 붙여넣기가 성공해야 한다 (#1280; 수정 전엔 \"글상자 없음\")");
let tb = textbox_of(&core, tb_para, tb_ctrl).expect("글상자 text_box 존재");
assert!(
tb.paragraphs.iter().any(|p| p.text.contains("복사원본")),
"붙여넣기 후 글상자 내부 문단에 복사한 텍스트가 있어야 한다"
);
}
#[test]
fn paste_picture_into_textbox() {
let mut core = make_test_core();
let res = core
.insert_picture_native(
0,
0,
0,
&[],
&minimal_png(),
5000,
5000,
1,
1,
"png",
"",
None,
None,
)
.expect("본문 그림 삽입");
let pic_para = parse_idx(&res, "paraIdx");
let pic_ctrl = parse_idx(&res, "controlIdx");
core.copy_control_native(0, pic_para, &[], pic_ctrl)
.expect("그림 복사");
let (tb_para, tb_ctrl) = create_shape(&mut core, "textbox");
core.paste_internal_in_cell_native(0, tb_para, tb_ctrl, 0, 0, 0)
.expect("글상자에 그림 붙여넣기");
let tb = textbox_of(&core, tb_para, tb_ctrl).expect("글상자 text_box 존재");
let pic_count: usize = tb
.paragraphs
.iter()
.map(|p| {
p.controls
.iter()
.filter(|c| matches!(c, Control::Picture(_)))
.count()
})
.sum();
assert_eq!(
pic_count, 1,
"글상자 안에 붙여넣은 그림 컨트롤이 보존되어야 한다 (#1323)"
);
}
}
#[cfg(test)]
mod bindata_storage_id_collision_tests {
use super::*;
use crate::model::bin_data::{
BinData, BinDataCompression, BinDataContent, BinDataStatus, BinDataType,
};
use std::collections::HashSet;
const EXISTING_IMAGE: &[u8] = &[0xAA, 0xBB, 0xCC];
fn minimal_png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
]
}
fn make_core_with_storage_hole() -> DocumentCore {
let path =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("samples/basic/english.hwp");
let bytes = std::fs::read(&path).expect("read english.hwp");
let mut core = DocumentCore::from_bytes(&bytes).expect("load english.hwp");
assert!(
core.document.bin_data_content.is_empty(),
"전제: english.hwp 에는 BinData 가 없어야 함"
);
core.document.bin_data_content.push(BinDataContent {
id: 2,
data: EXISTING_IMAGE.to_vec().into(),
extension: "png".to_string(),
});
core.document.doc_info.bin_data_list.push(BinData {
raw_data: None,
attr: 0x0101,
data_type: BinDataType::Embedding,
compression: BinDataCompression::Default,
status: BinDataStatus::Success,
abs_path: None,
rel_path: None,
storage_id: 2,
extension: Some("png".to_string()),
});
core
}
#[test]
fn insert_picture_storage_id_skips_existing_ids() {
let mut core = make_core_with_storage_hole();
core.insert_picture_native(
0,
0,
0,
&[],
&minimal_png(),
9000,
6000,
1,
1,
"png",
"",
None,
None,
)
.expect("insert_picture_native");
let content_ids: Vec<u16> = core
.document
.bin_data_content
.iter()
.map(|c| c.id)
.collect();
let unique: HashSet<u16> = content_ids.iter().copied().collect();
assert_eq!(
unique.len(),
content_ids.len(),
"BinDataContent.id 가 중복되면 저장 시 스트림 이름이 충돌한다: {content_ids:?}"
);
let storage_ids: Vec<u16> = core
.document
.doc_info
.bin_data_list
.iter()
.map(|b| b.storage_id)
.collect();
let unique_storage: HashSet<u16> = storage_ids.iter().copied().collect();
assert_eq!(
unique_storage.len(),
storage_ids.len(),
"BinData.storage_id 중복: {storage_ids:?}"
);
let new_bin_id = core.document.sections[0]
.paragraphs
.iter()
.flat_map(|p| p.controls.iter())
.find_map(|c| match c {
Control::Picture(pic) => Some(pic.image_attr.bin_data_id),
_ => None,
})
.expect("삽입된 그림 컨트롤");
assert_eq!(
new_bin_id, 2,
"bin_data_id 는 content 배열 1-based 순번이어야 함"
);
let by_position = &core.document.bin_data_content[(new_bin_id - 1) as usize];
assert_eq!(
by_position.data.load(),
minimal_png(),
"위치 기반 조회로 신규 그림 데이터가 나와야 함"
);
assert_eq!(
core.document.bin_data_content[0].data.load(),
EXISTING_IMAGE
);
}
#[test]
fn insert_into_storage_hole_doc_survives_hwp_save_roundtrip() {
let mut core = make_core_with_storage_hole();
core.insert_picture_native(
0,
0,
0,
&[],
&minimal_png(),
9000,
6000,
1,
1,
"png",
"",
None,
None,
)
.expect("insert_picture_native");
let saved = core.export_hwp_with_adapter().expect("export_hwp");
let reloaded = DocumentCore::from_bytes(&saved).expect("재로드");
let datas: Vec<Vec<u8>> = reloaded
.document()
.bin_data_content
.iter()
.map(|c| c.data.load())
.collect();
assert!(
datas.iter().any(|d| &d[..] == EXISTING_IMAGE),
"저장 왕복 후 기존 이미지가 소실됨 (스트림 이름 충돌): {:?}",
reloaded
.document()
.bin_data_content
.iter()
.map(|c| (c.id, c.data.len()))
.collect::<Vec<_>>()
);
assert!(
datas.iter().any(|d| &d[..] == minimal_png().as_slice()),
"저장 왕복 후 신규 이미지가 소실됨"
);
}
}
#[cfg(test)]
mod issue_4347_insert_leaves_coordinate_trace {
use super::*;
use crate::model::document::{Document, Section};
use crate::model::paragraph::Paragraph;
fn core_with_two_leading_controls() -> DocumentCore {
let mut para = Paragraph {
text: "0123456789".to_string(),
char_offsets: (16..26).collect(),
char_count: 27,
..Default::default()
};
para.controls.push(Control::SectionDef(Default::default()));
para.controls.push(Control::ColumnDef(Default::default()));
para.ctrl_data_records.push(None);
para.ctrl_data_records.push(None);
let mut doc = Document::default();
doc.sections.push(Section {
paragraphs: vec![para],
..Default::default()
});
let mut core = DocumentCore::new_empty();
core.set_document(doc);
core
}
fn png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89,
]
}
#[test]
fn inserted_picture_in_a_textbox_keeps_its_place_too() {
let mut core = DocumentCore::new_empty();
let mut doc = Document::default();
doc.sections.push(Section {
paragraphs: vec![Paragraph::default()],
..Default::default()
});
core.set_document(doc);
let made = core
.create_shape_control_native(
0,
0,
0,
20000,
10000,
0,
0,
true,
"square",
"textbox",
false,
false,
&[],
)
.expect("글상자");
let ctrl_idx =
crate::document_core::helpers::json_u32(&made, "controlIdx").unwrap() as usize;
core.insert_text_in_cell_native(0, 0, ctrl_idx, 0, 0, 0, "가나다라마바사")
.expect("글상자에 글");
core.insert_picture_native(
0,
0,
3,
&[(ctrl_idx, 0, 0)],
&png(),
100,
100,
1,
1,
"png",
"",
None,
None,
)
.expect("글상자 안 그림");
let shape = match &core.document.sections[0].paragraphs[0].controls[ctrl_idx] {
Control::Shape(s) => s,
other => panic!("글상자가 아니다: {:?}", other),
};
let tb = shape
.drawing()
.and_then(|d| d.text_box.as_ref())
.expect("글상자 안");
let para = &tb.paragraphs[0];
assert_eq!(
para.char_offsets[2], 2,
"넣은 자리 앞 글자는 그대로여야 한다"
);
assert_eq!(
para.char_offsets[3], 11,
"넣은 자리 뒤 글자는 컨트롤 몫 8칸만큼 밀려야 한다"
);
assert_eq!(
para.control_text_positions().first().copied(),
Some(3),
"그림은 넣은 글자 자리에 있어야 한다"
);
}
#[test]
fn inserted_picture_keeps_its_place_not_the_paragraph_end() {
let mut core = core_with_two_leading_controls();
core.insert_picture_native(0, 0, 4, &[], &png(), 100, 100, 1, 1, "png", "", None, None)
.expect("그림 삽입");
let para = &core.document.sections[0].paragraphs[0];
assert_eq!(
para.char_offsets[3], 19,
"넣은 자리 앞 글자는 그대로여야 한다"
);
assert_eq!(
para.char_offsets[4], 28,
"넣은 자리 뒤 글자는 컨트롤 몫 8칸만큼 밀려야 한다"
);
assert_eq!(para.char_count, 35, "문단 길이도 8칸 늘어야 한다");
let positions = para.control_text_positions();
assert_eq!(
positions.len(),
3,
"구역 정의·단 정의·그림 셋의 자리가 나와야 한다"
);
assert_eq!(positions[2], 4, "그림은 넣은 글자 자리에 있어야 한다");
}
}