use crate::error::HwpError;
use crate::model::bin_data::{BinData, BinDataContent, BinDataType};
use crate::model::control::Control;
use crate::model::document::Document;
use crate::model::event::DocumentEvent;
use crate::model::image::Picture;
use crate::model::paragraph::{CharShapeRef, Paragraph};
use crate::model::shape::ShapeObject;
use crate::model::style::HeadType;
use crate::renderer::composer::restamp_indentation;
use std::collections::HashMap;
impl crate::document_core::DocumentCore {
pub fn paste_foreign_document_native(
&mut self,
section_idx: usize,
para_idx: usize,
char_offset: usize,
foreign: Document,
) -> Result<String, HwpError> {
if section_idx >= self.document.sections.len() {
return Err(HwpError::RenderError(format!(
"구역 {} 범위 초과",
section_idx
)));
}
if para_idx >= self.document.sections[section_idx].paragraphs.len() {
return Err(HwpError::RenderError(format!(
"문단 {} 범위 초과",
para_idx
)));
}
let mut foreign_paras: Vec<Paragraph> = foreign
.sections
.iter()
.flat_map(|section| section.paragraphs.iter().cloned())
.collect();
if foreign_paras.is_empty() {
return Ok("{\"ok\":false,\"error\":\"외부 문서에 문단이 없다\"}".to_string());
}
let mut pool = BinPool::take(&mut self.document);
let id_map = merge_foreign_doc_info(&mut self.document, &foreign, &mut pool);
let target_is_empty = self.document.sections.len() == 1
&& self.document.sections[section_idx]
.paragraphs
.iter()
.all(|p| {
p.text.trim().is_empty()
&& p.controls
.iter()
.all(|c| matches!(c, Control::SectionDef(_) | Control::ColumnDef(_)))
});
if target_is_empty {
if let Some(foreign_section) = foreign.sections.first() {
self.document.sections[section_idx].section_def.page_def =
foreign_section.section_def.page_def.clone();
}
}
{
let mut ctx = RemapCtx {
map: &id_map,
foreign: &foreign,
pool: &mut pool,
next_field_id: next_free_field_id(&self.document),
};
ctx.paragraphs(&mut foreign_paras);
}
for para in &mut foreign_paras {
strip_section_scoped_controls(para);
}
crate::document_core::queries::memo_query::drop_memo_fields(&mut foreign_paras);
let imported_bin = pool.imported();
pool.restore(&mut self.document);
self.document.doc_info.raw_stream_dirty = true;
if imported_bin {
self.bump_bin_data_epoch();
}
self.rebuild_resolved_styles();
self.document.sections[section_idx].raw_stream = None;
{
let target = &self.document.sections[section_idx].paragraphs[para_idx];
let target_para_is_blank = target.text.trim().is_empty()
&& target
.controls
.iter()
.all(|c| matches!(c, Control::SectionDef(_) | Control::ColumnDef(_)));
if target_para_is_blank && char_offset == 0 {
let (shape_id, char_shapes) = {
let first = &foreign_paras[0];
(first.para_shape_id, first.char_shapes.clone())
};
let (old_indent, new_indent) =
self.para_shape_indents(target.para_shape_id, shape_id);
let target = &mut self.document.sections[section_idx].paragraphs[para_idx];
target.para_shape_id = shape_id;
restamp_indentation(&mut target.line_segs, old_indent, new_indent);
if !char_shapes.is_empty() {
target.char_shapes = char_shapes;
}
}
}
let foreign_count = foreign_paras.len();
if foreign_count == 1 && foreign_paras[0].controls.is_empty() {
let text = foreign_paras[0].text.clone();
let char_shapes = foreign_paras[0].char_shapes.clone();
let char_offsets = foreign_paras[0].char_offsets.clone();
let new_chars = text.chars().count();
self.document.sections[section_idx].paragraphs[para_idx]
.insert_text_from_paragraph(char_offset, &foreign_paras[0]);
self.apply_clipboard_char_shapes(
section_idx,
para_idx,
char_offset,
&char_shapes,
&char_offsets,
new_chars,
);
let stored_end_for_reset = crate::renderer::composer::paragraph_flow_end(
&self.document.sections[section_idx].paragraphs[para_idx],
);
self.reflow_paragraph(section_idx, para_idx);
let doc_hwp3_layout = self.document.layout_profile().hwp3_layout();
crate::renderer::composer::recalculate_section_vpos(
&mut self.document.sections[section_idx].paragraphs,
para_idx,
None,
stored_end_for_reset,
&self.styles,
self.dpi,
doc_hwp3_layout,
);
self.recompose_paragraph(section_idx, para_idx);
self.paginate_if_needed();
let new_offset = char_offset + new_chars;
self.event_log.push(DocumentEvent::ContentPasted {
section: section_idx,
para: para_idx,
});
return Ok(crate::document_core::helpers::json_ok_with(&format!(
"\"paraIdx\":{},\"charOffset\":{}",
para_idx, new_offset
)));
}
let right_half =
self.document.sections[section_idx].paragraphs[para_idx].split_at(char_offset);
self.document.sections[section_idx].paragraphs[para_idx].merge_from(&foreign_paras[0]);
let mut insert_idx = para_idx + 1;
for para in foreign_paras.iter().skip(1) {
self.document.sections[section_idx]
.paragraphs
.insert(insert_idx, para.clone());
insert_idx += 1;
}
let last_para_idx = insert_idx - 1;
let merge_point =
self.document.sections[section_idx].paragraphs[last_para_idx].merge_from(&right_half);
for i in para_idx..=last_para_idx {
self.reflow_paragraph(section_idx, i);
}
let doc_hwp3_layout = self.document.layout_profile().hwp3_layout();
crate::renderer::composer::recalculate_section_vpos(
&mut self.document.sections[section_idx].paragraphs,
para_idx,
Some(para_idx + 1..last_para_idx + 1),
None,
&self.styles,
self.dpi,
doc_hwp3_layout,
);
self.recompose_paragraph(section_idx, para_idx);
for i in para_idx + 1..=last_para_idx {
self.insert_composed_paragraph(section_idx, i);
}
self.paginate_if_needed();
self.event_log.push(DocumentEvent::ContentPasted {
section: section_idx,
para: para_idx,
});
Ok(crate::document_core::helpers::json_ok_with(&format!(
"\"paraIdx\":{},\"charOffset\":{}",
last_para_idx, merge_point
)))
}
}
fn table_key<T: serde::Serialize>(value: &T) -> Option<String> {
match serde_json::to_value(value) {
Ok(serde_json::Value::Object(mut obj)) => {
obj.remove("raw_data");
Some(serde_json::Value::Object(obj).to_string())
}
Ok(other) => Some(other.to_string()),
Err(_) => None,
}
}
fn build_table_index<T: serde::Serialize>(items: &[T]) -> HashMap<String, usize> {
let mut index = HashMap::new();
for (idx, item) in items.iter().enumerate() {
if let Some(key) = table_key(item) {
index.entry(key).or_insert(idx);
}
}
index
}
fn find_or_push<T: serde::Serialize>(
dst: &mut Vec<T>,
index: &mut HashMap<String, usize>,
candidate: T,
) -> usize {
if let Some(key) = table_key(&candidate) {
if let Some(&found) = index.get(&key) {
return found;
}
let new_idx = dst.len();
index.insert(key, new_idx);
dst.push(candidate);
return new_idx;
}
let new_idx = dst.len();
dst.push(candidate);
new_idx
}
#[derive(Default)]
struct ForeignIdMap {
fonts: Vec<Vec<u16>>,
char_shapes: Vec<u32>,
para_shapes: Vec<u16>,
border_fills: Vec<u16>,
tab_defs: Vec<u16>,
numberings: Vec<u16>,
bullets: Vec<u16>,
styles: Vec<u8>,
}
impl ForeignIdMap {
fn font(&self, slot: usize, old: u16) -> u16 {
self.fonts
.get(slot)
.and_then(|s| s.get(old as usize))
.copied()
.unwrap_or(old)
}
fn char_shape(&self, old: u32) -> u32 {
self.char_shapes.get(old as usize).copied().unwrap_or(old)
}
fn para_shape(&self, old: u16) -> u16 {
self.para_shapes.get(old as usize).copied().unwrap_or(old)
}
fn style(&self, old: u8) -> u8 {
self.styles.get(old as usize).copied().unwrap_or(old)
}
fn tab_def(&self, old: u16) -> u16 {
self.tab_defs.get(old as usize).copied().unwrap_or(old)
}
fn border_fill_ref(&self, old: u16) -> u16 {
if old == 0 {
return 0;
}
self.border_fills
.get((old - 1) as usize)
.and_then(|idx| idx.checked_add(1))
.unwrap_or(old)
}
fn numbering_ref(&self, old: u16, head_type: HeadType) -> u16 {
if old == 0 {
return 0;
}
let table = if head_type == HeadType::Bullet {
&self.bullets
} else {
&self.numberings
};
table
.get((old - 1) as usize)
.and_then(|idx| idx.checked_add(1))
.unwrap_or(old)
}
}
struct BinPool {
list: Vec<BinData>,
content: Vec<BinDataContent>,
map: HashMap<u16, u16>,
next_storage_id: u16,
imported: bool,
}
impl BinPool {
fn take(doc: &mut Document) -> Self {
let list = std::mem::take(&mut doc.doc_info.bin_data_list);
let content = std::mem::take(&mut doc.bin_data_content);
let next_storage_id = content
.iter()
.map(|c| c.id)
.chain(list.iter().map(|b| b.storage_id))
.max()
.unwrap_or(0)
.saturating_add(1);
BinPool {
list,
content,
map: HashMap::new(),
next_storage_id,
imported: false,
}
}
fn restore(self, doc: &mut Document) {
doc.doc_info.bin_data_list = self.list;
doc.bin_data_content = self.content;
}
fn imported(&self) -> bool {
self.imported
}
fn map_id(&mut self, foreign: &Document, old: u16) -> u16 {
if old == 0 {
return 0;
}
if let Some(&new_id) = self.map.get(&old) {
return new_id;
}
let Some(src) = find_foreign_bin_content(foreign, old) else {
self.map.insert(old, 0);
return 0;
};
let new_id = u16::try_from(self.content.len().max(self.list.len()) + 1).unwrap_or(0);
if new_id == 0 {
self.map.insert(old, 0);
return 0;
}
let mut record = foreign
.doc_info
.bin_data_list
.get((old - 1) as usize)
.cloned()
.unwrap_or(BinData {
data_type: BinDataType::Embedding,
extension: Some(src.extension.clone()),
..Default::default()
});
record.raw_data = None;
record.storage_id = self.next_storage_id;
self.next_storage_id = self.next_storage_id.saturating_add(1);
self.list.push(record);
self.content.push(BinDataContent {
id: new_id,
data: src.data.clone(),
extension: src.extension.clone(),
});
self.imported = true;
self.map.insert(old, new_id);
new_id
}
}
fn find_foreign_bin_content(foreign: &Document, id: u16) -> Option<&BinDataContent> {
if id == 0 {
return None;
}
if let Some(found) = foreign.bin_data_content.get((id - 1) as usize) {
return Some(found);
}
foreign.bin_data_content.iter().find(|c| c.id == id)
}
fn merge_foreign_doc_info(
current: &mut Document,
foreign: &Document,
pool: &mut BinPool,
) -> ForeignIdMap {
let mut map = ForeignIdMap::default();
while current.doc_info.font_faces.len() < foreign.doc_info.font_faces.len() {
current.doc_info.font_faces.push(Vec::new());
}
for (slot, foreign_slot) in foreign.doc_info.font_faces.iter().enumerate() {
let dst = &mut current.doc_info.font_faces[slot];
let mut index = build_table_index(dst);
let mut slot_map = Vec::with_capacity(foreign_slot.len());
for font in foreign_slot {
let mut candidate = font.clone();
candidate.raw_data = None;
if let Some(bin) = candidate.resolved_bin_data_id {
candidate.resolved_bin_data_id = Some(pool.map_id(foreign, bin));
}
let idx = find_or_push(dst, &mut index, candidate);
slot_map.push(u16::try_from(idx).unwrap_or(0));
}
map.fonts.push(slot_map);
}
{
let dst = &mut current.doc_info.border_fills;
let mut index = build_table_index(dst);
for border_fill in &foreign.doc_info.border_fills {
let mut candidate = border_fill.clone();
candidate.raw_data = None;
if let Some(image) = &mut candidate.fill.image {
image.bin_data_id = pool.map_id(foreign, image.bin_data_id);
}
let idx = find_or_push(dst, &mut index, candidate);
map.border_fills.push(u16::try_from(idx).unwrap_or(0));
}
}
{
let dst = &mut current.doc_info.tab_defs;
let mut index = build_table_index(dst);
for tab_def in &foreign.doc_info.tab_defs {
let mut candidate = tab_def.clone();
candidate.raw_data = None;
let idx = find_or_push(dst, &mut index, candidate);
map.tab_defs.push(u16::try_from(idx).unwrap_or(0));
}
}
{
let dst = &mut current.doc_info.char_shapes;
let mut index = build_table_index(dst);
for char_shape in &foreign.doc_info.char_shapes {
let mut candidate = char_shape.clone();
candidate.raw_data = None;
for (slot, font_id) in candidate.font_ids.iter_mut().enumerate() {
*font_id = map.font(slot, *font_id);
}
candidate.border_fill_id = map.border_fill_ref(candidate.border_fill_id);
let idx = find_or_push(dst, &mut index, candidate);
map.char_shapes.push(u32::try_from(idx).unwrap_or(0));
}
}
{
let dst = &mut current.doc_info.numberings;
let mut index = build_table_index(dst);
for numbering in &foreign.doc_info.numberings {
let mut candidate = numbering.clone();
candidate.raw_data = None;
for head in candidate.heads.iter_mut() {
head.char_shape_id = map.char_shape(head.char_shape_id);
}
let idx = find_or_push(dst, &mut index, candidate);
map.numberings.push(u16::try_from(idx).unwrap_or(0));
}
}
{
let dst = &mut current.doc_info.bullets;
let mut index = build_table_index(dst);
for bullet in &foreign.doc_info.bullets {
let mut candidate = bullet.clone();
candidate.raw_data = None;
candidate.char_shape_id = map.char_shape(candidate.char_shape_id);
let idx = find_or_push(dst, &mut index, candidate);
map.bullets.push(u16::try_from(idx).unwrap_or(0));
}
current.doc_info.bullet_count = current.doc_info.bullets.len() as u32;
}
{
let dst = &mut current.doc_info.para_shapes;
let mut index = build_table_index(dst);
for para_shape in &foreign.doc_info.para_shapes {
let mut candidate = para_shape.clone();
candidate.raw_data = None;
candidate.tab_def_id = map.tab_def(candidate.tab_def_id);
candidate.numbering_id = map.numbering_ref(candidate.numbering_id, candidate.head_type);
candidate.border_fill_id = map.border_fill_ref(candidate.border_fill_id);
let idx = find_or_push(dst, &mut index, candidate);
map.para_shapes.push(u16::try_from(idx).unwrap_or(0));
}
}
let styles_before = current.doc_info.styles.len();
{
let dst = &mut current.doc_info.styles;
let mut index = build_table_index(dst);
for style in &foreign.doc_info.styles {
let mut candidate = style.clone();
candidate.raw_data = None;
candidate.para_shape_id = map.para_shape(candidate.para_shape_id);
candidate.char_shape_id =
u16::try_from(map.char_shape(candidate.char_shape_id as u32)).unwrap_or(0);
let idx = find_or_push(dst, &mut index, candidate);
map.styles.push(u8::try_from(idx).unwrap_or(0));
}
}
for (foreign_idx, &new_idx) in map.styles.iter().enumerate() {
if (new_idx as usize) < styles_before {
continue; }
let next = foreign
.doc_info
.styles
.get(foreign_idx)
.map(|s| s.next_style_id)
.unwrap_or(0);
if let Some(style) = current.doc_info.styles.get_mut(new_idx as usize) {
style.next_style_id = map.style(next);
}
}
current.doc_info.memo_shape_count = current
.doc_info
.memo_shape_count
.max(foreign.doc_info.memo_shape_count);
if current.doc_info.memo_properties_xml.is_none() {
current
.doc_info
.memo_properties_xml
.clone_from(&foreign.doc_info.memo_properties_xml);
}
map
}
struct RemapCtx<'a> {
map: &'a ForeignIdMap,
foreign: &'a Document,
pool: &'a mut BinPool,
next_field_id: u32,
}
impl RemapCtx<'_> {
fn paragraphs(&mut self, paragraphs: &mut [Paragraph]) {
for para in paragraphs {
para.para_shape_id = self.map.para_shape(para.para_shape_id);
para.style_id = self.map.style(para.style_id);
for char_shape in &mut para.char_shapes {
char_shape.char_shape_id = self.map.char_shape(char_shape.char_shape_id);
}
for ctrl in &mut para.controls {
self.control(ctrl);
}
}
}
fn control(&mut self, ctrl: &mut Control) {
match ctrl {
Control::Table(table) => {
table.border_fill_id = self.map.border_fill_ref(table.border_fill_id);
for zone in &mut table.zones {
zone.border_fill_id = self.map.border_fill_ref(zone.border_fill_id);
}
for cell in &mut table.cells {
cell.border_fill_id = self.map.border_fill_ref(cell.border_fill_id);
self.paragraphs(&mut cell.paragraphs);
}
if let Some(caption) = &mut table.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
Control::Picture(picture) => self.picture(picture),
Control::Shape(shape) => self.shape(shape),
Control::Header(header) => self.paragraphs(&mut header.paragraphs),
Control::Footer(footer) => self.paragraphs(&mut footer.paragraphs),
Control::Footnote(footnote) => self.paragraphs(&mut footnote.paragraphs),
Control::Endnote(endnote) => self.paragraphs(&mut endnote.paragraphs),
Control::HiddenComment(comment) => self.paragraphs(&mut comment.paragraphs),
Control::Field(field) => {
field.field_id = self.next_field_id;
self.next_field_id = self.next_field_id.saturating_add(1).max(1);
if let Some(residue) = &mut field.guide_residue {
residue.char_shape_id = self.map.char_shape(residue.char_shape_id);
}
self.paragraphs(&mut field.memo_paragraphs);
}
Control::CharOverlap(overlap) => {
for id in &mut overlap.char_shape_ids {
*id = self.map.char_shape(*id);
}
}
Control::Ruby(ruby) => {
if let Ok(old) = u8::try_from(ruby.style_id_ref) {
ruby.style_id_ref = u16::from(self.map.style(old));
}
}
_ => {}
}
}
fn picture(&mut self, picture: &mut Picture) {
picture.image_attr.bin_data_id = self
.pool
.map_id(self.foreign, picture.image_attr.bin_data_id);
if let Some(caption) = &mut picture.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
fn shape(&mut self, shape: &mut ShapeObject) {
match shape {
ShapeObject::Picture(picture) => self.picture(picture),
ShapeObject::Ole(ole) => {
if let Ok(old) = u16::try_from(ole.bin_data_id) {
ole.bin_data_id = u32::from(self.pool.map_id(self.foreign, old));
}
if let Some(caption) = &mut ole.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
ShapeObject::Group(group) => {
for child in &mut group.children {
self.shape(child);
}
if let Some(caption) = &mut group.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
ShapeObject::Chart(chart) => {
if let Some(caption) = &mut chart.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
_ => {}
}
if let Some(drawing) = shape.drawing_mut() {
if let Some(image) = &mut drawing.fill.image {
image.bin_data_id = self.pool.map_id(self.foreign, image.bin_data_id);
}
if let Some(text_box) = &mut drawing.text_box {
self.paragraphs(&mut text_box.paragraphs);
}
if let Some(caption) = &mut drawing.caption {
self.paragraphs(&mut caption.paragraphs);
}
}
}
}
fn next_free_field_id(doc: &Document) -> u32 {
let mut max_id = 0u32;
for section in &doc.sections {
collect_max_field_id(§ion.paragraphs, &mut max_id);
}
max_id.saturating_add(1).max(1)
}
fn collect_max_field_id(paragraphs: &[Paragraph], max_id: &mut u32) {
for para in paragraphs {
for ctrl in ¶.controls {
collect_max_field_id_from_control(ctrl, max_id);
}
}
}
fn collect_max_field_id_from_control(ctrl: &Control, max_id: &mut u32) {
match ctrl {
Control::Field(field) => {
*max_id = (*max_id).max(field.field_id);
collect_max_field_id(&field.memo_paragraphs, max_id);
}
Control::Table(table) => {
for cell in &table.cells {
collect_max_field_id(&cell.paragraphs, max_id);
}
if let Some(caption) = &table.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
Control::Picture(picture) => {
if let Some(caption) = &picture.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
Control::Shape(shape) => collect_max_field_id_from_shape(shape, max_id),
Control::Header(header) => collect_max_field_id(&header.paragraphs, max_id),
Control::Footer(footer) => collect_max_field_id(&footer.paragraphs, max_id),
Control::Footnote(footnote) => collect_max_field_id(&footnote.paragraphs, max_id),
Control::Endnote(endnote) => collect_max_field_id(&endnote.paragraphs, max_id),
Control::HiddenComment(comment) => collect_max_field_id(&comment.paragraphs, max_id),
_ => {}
}
}
fn collect_max_field_id_from_shape(shape: &crate::model::shape::ShapeObject, max_id: &mut u32) {
match shape {
crate::model::shape::ShapeObject::Picture(picture) => {
if let Some(caption) = &picture.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
crate::model::shape::ShapeObject::Ole(ole) => {
if let Some(caption) = &ole.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
crate::model::shape::ShapeObject::Group(group) => {
for child in &group.children {
collect_max_field_id_from_shape(child, max_id);
}
if let Some(caption) = &group.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
crate::model::shape::ShapeObject::Chart(chart) => {
if let Some(caption) = &chart.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
_ => {}
}
if let Some(drawing) = shape.drawing() {
if let Some(text_box) = &drawing.text_box {
collect_max_field_id(&text_box.paragraphs, max_id);
}
if let Some(caption) = &drawing.caption {
collect_max_field_id(&caption.paragraphs, max_id);
}
}
}
fn strip_section_scoped_controls(para: &mut Paragraph) {
let leading = para
.controls
.iter()
.take_while(|c| matches!(c, Control::SectionDef(_) | Control::ColumnDef(_)))
.count();
let has_structural = para
.controls
.iter()
.any(|c| matches!(c, Control::SectionDef(_) | Control::ColumnDef(_)));
if !has_structural {
return;
}
super::clipboard::strip_structural_controls_for_text_clipboard(para);
drop_leading_control_slots(para, leading);
}
fn drop_leading_control_slots(para: &mut Paragraph, max_slots: usize) {
if max_slots == 0 {
return;
}
let chars: Vec<char> = para.text.chars().collect();
let slots = chars
.iter()
.take(max_slots)
.take_while(|&&c| c == '\u{0002}')
.count();
if slots == 0 || para.char_offsets.len() != chars.len() {
return;
}
if (0..slots).any(|i| para.char_offsets[i] != (i as u32) * 8) {
return;
}
let shift = (slots as u32) * 8;
para.delete_nb_space_origin_range(0, slots);
para.text = chars[slots..].iter().collect();
para.char_offsets = para.char_offsets[slots..]
.iter()
.map(|off| off.saturating_sub(shift))
.collect();
para.char_count = para.char_count.saturating_sub(shift);
let mut kept: Vec<CharShapeRef> = Vec::with_capacity(para.char_shapes.len());
let mut carried: Option<u32> = None;
for cs in ¶.char_shapes {
if cs.start_pos < shift {
carried = Some(cs.char_shape_id);
continue;
}
if cs.start_pos == shift {
carried = None;
}
kept.push(CharShapeRef {
start_pos: cs.start_pos - shift,
char_shape_id: cs.char_shape_id,
});
}
if let Some(char_shape_id) = carried {
kept.insert(
0,
CharShapeRef {
start_pos: 0,
char_shape_id,
},
);
}
para.char_shapes = kept;
for seg in &mut para.line_segs {
seg.text_start = seg.text_start.saturating_sub(shift);
}
for tag in &mut para.range_tags {
tag.start = tag.start.saturating_sub(shift);
tag.end = tag.end.saturating_sub(shift);
}
for range in &mut para.field_ranges {
range.start_char_idx = range.start_char_idx.saturating_sub(slots);
range.end_char_idx = range.end_char_idx.saturating_sub(slots);
}
for mark in &mut para.title_marks {
mark.char_idx = mark.char_idx.saturating_sub(slots);
}
}
impl crate::document_core::DocumentCore {
pub fn paste_hwp_json_native(
&mut self,
section_idx: usize,
para_idx: usize,
char_offset: usize,
json: &str,
) -> Result<String, HwpError> {
let parts = crate::document_core::hwpjson::hwpjson_to_hwpx_parts(json)?;
let foreign = parts_to_document(&parts)?;
let result =
self.paste_foreign_document_native(section_idx, para_idx, char_offset, foreign)?;
self.document.doc_info.raw_stream_dirty = true;
Ok(result)
}
}
fn parts_to_document(
parts: &crate::document_core::hwpjson::HwpxParts,
) -> Result<Document, HwpError> {
let (doc_info, doc_properties) =
crate::parser::hwpx::header::parse_hwpx_header(&parts.header_xml)?;
let section = crate::parser::hwpx::section::parse_hwpx_section(&parts.section_xml)?;
let mut doc = Document {
doc_info,
doc_properties,
sections: vec![section],
is_hwpx_variant: true,
..Default::default()
};
for (id, mime, bytes) in &parts.bins {
let extension = mime.rsplit('/').next().unwrap_or("png").to_string();
doc.bin_data_content
.push(crate::model::bin_data::BinDataContent {
id: *id,
data: crate::model::bin_data::BinDataBytes::from_shared(bytes.clone()),
extension,
});
}
Ok(doc)
}