use crate::document_core::helpers::{json_i32, parse_char_shape_mods};
use crate::document_core::DocumentCore;
use crate::error::HwpError;
use crate::model::style::Bullet;
use serde_json::Value;
const USE_INST_WIDTH: u32 = 1 << 2;
const AUTO_INDENT: u32 = 1 << 3;
fn error(message: &str) -> HwpError {
HwpError::RenderError(message.to_string())
}
fn number(json: &Value, key: &str, default: i64, min: i64, max: i64) -> Result<i64, HwpError> {
match json.get(key) {
None => Ok(default),
Some(value) => value
.as_i64()
.filter(|n| (min..=max).contains(n))
.ok_or_else(|| HwpError::RenderError(format!("{key} 값은 {min}~{max} 사이여야 함"))),
}
}
fn flag(json: &Value, key: &str, default: bool) -> Result<bool, HwpError> {
match json.get(key) {
None => Ok(default),
Some(value) => value
.as_bool()
.ok_or_else(|| HwpError::RenderError(format!("{key} 값은 참·거짓이어야 함"))),
}
}
impl DocumentCore {
pub fn create_bullet_native(
&mut self,
json: &str,
image: Option<(&[u8], &str)>,
) -> Result<u16, HwpError> {
let value: Value =
serde_json::from_str(json).map_err(|e| HwpError::RenderError(e.to_string()))?;
let mut chars = value
.get("char")
.and_then(Value::as_str)
.unwrap_or("")
.chars();
let bullet_char = match (chars.next(), chars.next()) {
(Some(c), None)
if !c.is_control()
&& u32::from(c) <= 0xFFFF
&& c != '\u{FFFE}'
&& c != '\u{FFFF}' =>
{
c
}
_ => return Err(error("글머리표 문자는 제어 문자가 아닌 한 글자여야 함")),
};
let mut attr = number(&value, "align", 0, 0, 2)? as u32;
if flag(&value, "useInstWidth", false)? {
attr |= USE_INST_WIDTH;
}
if flag(&value, "autoIndent", true)? {
attr |= AUTO_INDENT;
}
let width_adjust =
number(&value, "widthAdjust", 0, i16::MIN.into(), i16::MAX.into())? as i16;
let text_distance = number(&value, "textOffset", 50, 0, 100)? as i16;
let char_shape = match value.get("charShape") {
None | Some(Value::Null) => None,
Some(shape @ Value::Object(_)) => Some(shape.to_string()),
Some(_) => return Err(error("글머리표 글자 모양은 객체여야 함")),
};
if let Some((bytes, extension)) = image {
if bytes.is_empty()
|| extension.is_empty()
|| !extension.chars().all(|c| c.is_ascii_alphanumeric())
{
return Err(error("그림 글머리표의 그림이 비었거나 확장자가 잘못됨"));
}
if self.document.bin_data_content.len() >= usize::from(u8::MAX) {
return Err(error("그림 글머리표는 문서의 255번째 그림까지 쓸 수 있음"));
}
}
if self.document.doc_info.bullets.len() >= usize::from(u16::MAX) {
return Err(error("글머리표 정의를 더 만들 수 없음"));
}
let char_shape_id = char_shape.map_or(u32::MAX, |json| self.head_char_shape_id(&json));
let image_bullet = image.map_or(0, |(bytes, extension)| {
self.register_embedded_bin_data(bytes, &extension.to_ascii_lowercase())
});
let info = &mut self.document.doc_info;
info.bullets.push(Bullet {
attr,
width_adjust,
text_distance,
char_shape_id,
bullet_char,
image_bullet: i32::from(image_bullet > 0),
image_data: [0, 0, 0, image_bullet as u8],
..Default::default()
});
info.raw_stream_dirty = true;
let id = info.bullets.len() as u16;
self.rebuild_resolved_styles();
Ok(id)
}
pub(super) fn head_char_shape_id(&mut self, json: &str) -> u32 {
let base = json_i32(json, "baseCharShapeId")
.and_then(|id| u32::try_from(id).ok())
.filter(|id| (*id as usize) < self.document.doc_info.char_shapes.len())
.unwrap_or(0);
let id = self
.document
.find_or_create_char_shape(base, &parse_char_shape_mods(json));
if id != 0 {
return id;
}
let shapes = &mut self.document.doc_info.char_shapes;
match shapes[1..].iter().position(|shape| *shape == shapes[0]) {
Some(index) => index as u32 + 1,
None => {
let mut shape = shapes[0].clone();
shape.raw_data = None;
shapes.push(shape);
(shapes.len() - 1) as u32
}
}
}
}