pub(crate) use crate::document_core::helpers::*;
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use web_sys::HtmlCanvasElement;
use crate::document_core::helpers::parse_removed_para_meta;
use crate::document_core::{
DeferredPaginationJobState, DeferredPaginationStepResult, DocumentCore, DEFAULT_FALLBACK_FONT,
};
use crate::error::HwpError;
use crate::model::control::Control;
use crate::model::document::{Document, Section};
use crate::model::page::ColumnDef;
use crate::model::paragraph::Paragraph;
use crate::model::path::{path_from_flat, DocumentPath, PathSegment};
use crate::model::shape::ShapeObject;
use crate::renderer::canvas::CanvasRenderer;
use crate::renderer::composer::{
compose_paragraph, compose_section, reflow_line_segs, restamp_indentation, ComposedParagraph,
};
use crate::renderer::height_measurer::{HeightMeasurer, MeasuredSection, MeasuredTable};
use crate::renderer::html::HtmlRenderer;
use crate::renderer::layout::LayoutEngine;
use crate::renderer::page_layout::PageLayoutInfo;
use crate::renderer::pagination::{PaginationResult, Paginator};
use crate::renderer::render_tree::PageRenderTree;
use crate::renderer::scheduler::{RenderEvent, RenderObserver, RenderScheduler, Viewport};
use crate::renderer::style_resolver::{
resolve_font_substitution, resolve_styles, ResolvedStyleSet,
};
use crate::renderer::svg::SvgRenderer;
use crate::renderer::DEFAULT_DPI;
#[cfg(target_arch = "wasm32")]
mod bitmap_raster;
mod canvas_metrics;
mod footnote_shape;
mod form_action;
mod host_font_environment;
mod hyperlink;
mod layout_generation;
#[cfg(all(feature = "pdf-import", target_arch = "wasm32"))]
mod pdf_import;
#[cfg(all(feature = "pdf-import", target_arch = "wasm32"))]
mod pdf_request;
#[cfg(target_arch = "wasm32")]
mod print_image_sampling;
mod render_patch_boundary;
#[cfg(target_arch = "wasm32")]
mod static_svg_raster;
mod table_cell_bboxes;
mod template_automation;
#[cfg(target_arch = "wasm32")]
mod layer_export;
#[cfg(target_arch = "wasm32")]
type PdfImportInput = JsValue;
#[cfg(not(target_arch = "wasm32"))]
type PdfImportInput = [u8];
impl From<HwpError> for JsValue {
fn from(err: HwpError) -> Self {
JsValue::from_str(&err.to_string())
}
}
fn row_index_from_u32(v: u32) -> Result<u16, HwpError> {
u16::try_from(v)
.map_err(|_| HwpError::RenderError(format!("행 인덱스 {} 가 최대치(65535)를 넘습니다", v)))
}
fn deferred_pagination_result_json(result: DeferredPaginationStepResult) -> String {
let status = match result.state {
DeferredPaginationJobState::None => "none",
DeferredPaginationJobState::Pending => "pending",
DeferredPaginationJobState::Complete => "complete",
DeferredPaginationJobState::Fallback => "fallback",
DeferredPaginationJobState::Stale => "stale",
};
serde_json::json!({
"ok": true,
"status": status,
"revision": result.revision,
"fragmentsProcessed": result.fragments_processed,
"pageCount": result.page_count,
})
.to_string()
}
fn compose_cell(
sec: u32,
parent_para: u32,
path: &[(usize, usize, usize)],
) -> crate::document_core::ComposeHost<'_> {
crate::document_core::ComposeHost::Cell {
sec: sec as usize,
parent_para: parent_para as usize,
path,
}
}
fn compose_note(
sec: u32,
para: u32,
control: u32,
note_para: u32,
) -> crate::document_core::ComposeHost<'static> {
crate::document_core::ComposeHost::Note {
sec: sec as usize,
para: para as usize,
control: control as usize,
note_para: note_para as usize,
}
}
fn parse_cell_path_arg(cell_path_json: &str) -> Result<Vec<(usize, usize, usize)>, JsValue> {
if cell_path_json.is_empty() || cell_path_json == "[]" {
Ok(Vec::new())
} else {
DocumentCore::parse_cell_path(cell_path_json).map_err(JsValue::from)
}
}
#[cfg(any(target_arch = "wasm32", test))]
const MAX_CANVAS_DIMENSION: f64 = 16_384.0;
#[cfg(any(target_arch = "wasm32", test))]
fn normalize_canvas_scale(
page_width: f64,
page_height: f64,
requested_scale: f64,
) -> Result<f64, &'static str> {
if !page_width.is_finite()
|| !page_height.is_finite()
|| page_width <= 0.0
|| page_height <= 0.0
{
return Err("invalid page dimensions");
}
let scale = if requested_scale <= 0.0 || !requested_scale.is_finite() {
1.0
} else {
requested_scale.clamp(0.25, 12.0)
};
let scaled_width = page_width * scale;
let scaled_height = page_height * scale;
if !scaled_width.is_finite() || !scaled_height.is_finite() {
return Ok((MAX_CANVAS_DIMENSION / page_width)
.min(MAX_CANVAS_DIMENSION / page_height)
.min(scale));
}
if scaled_width > MAX_CANVAS_DIMENSION || scaled_height > MAX_CANVAS_DIMENSION {
Ok((MAX_CANVAS_DIMENSION / page_width)
.min(MAX_CANVAS_DIMENSION / page_height)
.min(scale))
} else {
Ok(scale)
}
}
#[cfg(any(target_arch = "wasm32", test))]
fn scaled_canvas_extent(page_extent: f64, scale: f64) -> u32 {
(page_extent * scale)
.ceil()
.clamp(1.0, MAX_CANVAS_DIMENSION) as u32
}
#[cfg(target_arch = "wasm32")]
fn canvas_layer_filter(
layer_kind: &str,
) -> Result<crate::renderer::web_canvas::LayerFilter, JsValue> {
use crate::model::shape::TextWrap;
use crate::renderer::web_canvas::LayerFilter;
match layer_kind {
"all" => Ok(LayerFilter::All),
"background" => Ok(LayerFilter::BackgroundOnly),
"flow" => Ok(LayerFilter::FlowOnly),
"flow-dynamic" => Ok(LayerFilter::FlowDynamic),
"flow-static" => Ok(LayerFilter::FlowStatic),
"behind" => Ok(LayerFilter::WrapOnly(TextWrap::BehindText)),
"front" => Ok(LayerFilter::WrapOnly(TextWrap::InFrontOfText)),
_ => Err(JsValue::from_str(
"invalid layer_kind: 'all' | 'background' | 'flow' | 'flow-dynamic' | 'flow-static' | 'behind' | 'front'",
)),
}
}
#[cfg(target_arch = "wasm32")]
fn render_page_to_canvas_filtered_with_profile_impl(
document: &HwpDocument,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
layer_kind: &str,
profile: &str,
) -> Result<(), JsValue> {
use crate::paint::RenderProfile;
use crate::renderer::layer_renderer::LayerRenderer;
use crate::renderer::web_canvas::WebCanvasRenderer;
let filter = canvas_layer_filter(layer_kind)?;
let profile = RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
crate::renderer::image_resolver::with_decoded_handoff(|| {
let tree = document
.build_canvas_page_layer_tree_with_profile(page_num, profile)
.map_err(JsValue::from)?;
let scale = normalize_canvas_scale(tree.page_width, tree.page_height, scale)
.map_err(JsValue::from_str)?;
canvas.set_width(scaled_canvas_extent(tree.page_width, scale));
canvas.set_height(scaled_canvas_extent(tree.page_height, scale));
let mut renderer = WebCanvasRenderer::new(canvas)?;
renderer.set_hft_outline_fonts(&document.hft_outline_fonts);
renderer.show_paragraph_marks = document.show_paragraph_marks;
renderer.show_control_codes = document.show_control_codes;
renderer.set_scale(scale);
renderer.set_layer_filter(filter);
renderer.render_page(&tree).map_err(JsValue::from)?;
Ok(())
})
}
#[cfg(target_arch = "wasm32")]
fn render_page_patch_to_canvas_filtered_with_profile_impl(
document: &HwpDocument,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
layer_kind: &str,
profile: &str,
patch: crate::renderer::render_tree::BoundingBox,
) -> Result<(), JsValue> {
use crate::paint::RenderProfile;
use crate::renderer::layer_renderer::LayerRenderer;
use crate::renderer::render_tree::BoundingBox;
use crate::renderer::web_canvas::WebCanvasRenderer;
if ![patch.x, patch.y, patch.width, patch.height]
.into_iter()
.all(f64::is_finite)
|| patch.width <= 0.0
|| patch.height <= 0.0
{
return Err(JsValue::from_str("invalid page patch rectangle"));
}
let filter = canvas_layer_filter(layer_kind)?;
let profile = RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
let tree = document
.build_canvas_page_layer_tree_with_profile(page_num, profile)
.map_err(JsValue::from)?;
let scale = normalize_canvas_scale(tree.page_width, tree.page_height, scale)
.map_err(JsValue::from_str)?;
let expected_width = scaled_canvas_extent(tree.page_width, scale);
let expected_height = scaled_canvas_extent(tree.page_height, scale);
if canvas.width() != expected_width || canvas.height() != expected_height {
return Err(JsValue::from_str(
"page patch canvas extent does not match the current page render",
));
}
let left = patch.x.max(0.0).min(tree.page_width);
let top = patch.y.max(0.0).min(tree.page_height);
let right = (patch.x + patch.width).max(left).min(tree.page_width);
let bottom = (patch.y + patch.height).max(top).min(tree.page_height);
if right <= left || bottom <= top {
return Err(JsValue::from_str(
"page patch rectangle does not intersect the page",
));
}
let mut renderer = WebCanvasRenderer::new(canvas)?;
renderer.set_hft_outline_fonts(&document.hft_outline_fonts);
renderer.show_paragraph_marks = document.show_paragraph_marks;
renderer.show_control_codes = document.show_control_codes;
renderer.set_scale(scale);
renderer.set_layer_filter(filter);
renderer.set_partial_clip(BoundingBox::new(left, top, right - left, bottom - top));
renderer.render_page(&tree).map_err(JsValue::from)?;
Ok(())
}
fn get_page_layer_tree_with_profile_impl(
document: &HwpDocument,
page_num: u32,
profile: &str,
omit_image_bytes: bool,
omit_font_bytes: bool,
) -> Result<String, JsValue> {
let profile = crate::paint::RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
document
.get_page_layer_tree_with_options_native(
page_num,
profile,
crate::paint::LayerJsonOptions {
omit_image_bytes,
omit_font_bytes,
},
)
.map_err(|error| error.into())
}
fn get_page_overlay_images_impl(document: &HwpDocument, page_num: u32) -> Result<String, JsValue> {
document
.get_page_overlay_images_native(page_num)
.map_err(|error| error.into())
}
fn get_page_flow_image_ops_impl(document: &HwpDocument, page_num: u32) -> Result<String, JsValue> {
document
.get_page_flow_image_ops_native(page_num)
.map_err(|error| error.into())
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ExternalImageReference {
key: String,
bin_data_id: u16,
original_path: String,
basename: String,
extension: String,
loaded: bool,
}
fn external_path_basename(path: &str) -> &str {
path.rsplit(|c| c == '/' || c == '\\')
.find(|part| !part.is_empty())
.unwrap_or(path)
}
fn external_path_extension(basename: &str) -> String {
std::path::Path::new(basename)
.extension()
.and_then(|ext| ext.to_str())
.unwrap_or("")
.to_string()
}
fn parse_external_image_key(key: &str) -> Option<u16> {
let bin_data_id = key.strip_prefix("binData:")?.parse::<u16>().ok()?;
(bin_data_id != 0).then_some(bin_data_id)
}
fn collect_external_image_references(document: &Document) -> Vec<ExternalImageReference> {
let mut references = std::collections::BTreeMap::new();
for section in &document.sections {
for para in §ion.paragraphs {
for ctrl in ¶.controls {
let pic = match ctrl {
Control::Picture(pic) => pic,
Control::Shape(shape) => match shape.as_ref() {
ShapeObject::Picture(pic) => pic,
_ => continue,
},
_ => continue,
};
let Some(original_path) = pic.image_attr.external_path.as_ref() else {
continue;
};
let bin_data_id = pic.image_attr.bin_data_id;
references.entry(bin_data_id).or_insert_with(|| {
let basename = external_path_basename(original_path).to_string();
ExternalImageReference {
key: format!("binData:{bin_data_id}"),
bin_data_id,
extension: external_path_extension(&basename),
basename,
original_path: original_path.clone(),
loaded: document.external_image_loaded(bin_data_id),
}
});
}
}
}
references.into_values().collect()
}
#[wasm_bindgen]
pub struct HwpDocument {
core: DocumentCore,
}
#[wasm_bindgen]
pub struct DocumentExport {
bytes: Option<Vec<u8>>,
content_loss_json: String,
}
impl From<crate::serializer::SerializedDocument> for DocumentExport {
fn from(serialized: crate::serializer::SerializedDocument) -> Self {
let (bytes, content_loss) = serialized.into_parts();
Self {
bytes: Some(bytes),
content_loss_json: content_loss.to_json(),
}
}
}
#[wasm_bindgen]
impl DocumentExport {
#[wasm_bindgen(js_name = contentLoss)]
pub fn content_loss(&self) -> String {
self.content_loss_json.clone()
}
#[wasm_bindgen(js_name = hasBytes)]
pub fn has_bytes(&self) -> bool {
self.bytes.is_some()
}
#[wasm_bindgen(js_name = takeBytes)]
pub fn take_bytes(&mut self) -> Result<Vec<u8>, JsValue> {
self.bytes
.take()
.ok_or_else(|| JsValue::from_str("이 내보내기 결과의 바이트를 이미 가져갔습니다"))
}
}
impl std::ops::Deref for HwpDocument {
type Target = DocumentCore;
fn deref(&self) -> &DocumentCore {
&self.core
}
}
impl std::ops::DerefMut for HwpDocument {
fn deref_mut(&mut self) -> &mut DocumentCore {
&mut self.core
}
}
impl From<DocumentCore> for HwpDocument {
fn from(core: DocumentCore) -> Self {
Self { core }
}
}
impl HwpDocument {
pub fn from_bytes(data: &[u8]) -> Result<HwpDocument, HwpError> {
DocumentCore::from_bytes(data).map(|core| HwpDocument { core })
}
pub fn from_bytes_with_password(data: &[u8], password: &[u8]) -> Result<HwpDocument, HwpError> {
DocumentCore::from_bytes_with_password(data, password).map(|core| HwpDocument { core })
}
pub fn load_bytes(&mut self, data: &[u8], password: Option<&[u8]>) -> Result<(), HwpError> {
self.core.load_bytes(data, password)
}
pub fn find_initial_column_def(paragraphs: &[Paragraph]) -> ColumnDef {
DocumentCore::find_initial_column_def(paragraphs)
}
pub fn find_column_def_for_paragraph(paragraphs: &[Paragraph], para_idx: usize) -> ColumnDef {
DocumentCore::find_column_def_for_paragraph(paragraphs, para_idx)
}
fn inject_external_image_by_bin_data_id(
&mut self,
bin_data_id: u16,
data: &[u8],
display_path: &str,
fallback_basename: Option<&str>,
) -> u32 {
let Some(reference) = collect_external_image_references(self.document())
.into_iter()
.find(|reference| reference.bin_data_id == bin_data_id)
else {
return 0;
};
if reference.loaded {
return 0;
}
if !self.document_mut().inject_external_image_data(
bin_data_id,
data.to_vec(),
reference.extension.clone(),
) {
return 0;
}
let basename = fallback_basename.unwrap_or(&reference.basename);
let resolved = if display_path.is_empty() {
format!("/samples/{basename}")
} else {
display_path.to_string()
};
self.document_mut()
.update_external_image_display_path(bin_data_id, &resolved);
1
}
}
fn hml_warning_code(code: crate::parser::hml::HmlWarningCode) -> &'static str {
use crate::parser::hml::HmlWarningCode;
match code {
HmlWarningCode::UnsupportedElement => "UnsupportedElement",
HmlWarningCode::UnsupportedAttribute => "UnsupportedAttribute",
HmlWarningCode::UnsupportedEquationSemantics => "UnsupportedEquationSemantics",
HmlWarningCode::MissingResource => "MissingResource",
HmlWarningCode::ExternalResourceBlocked => "ExternalResourceBlocked",
HmlWarningCode::InvalidReference => "InvalidReference",
HmlWarningCode::LossyConversion => "LossyConversion",
}
}
fn hml_warning_json(warning: &crate::parser::hml::HmlWarning) -> serde_json::Value {
serde_json::json!({
"code": hml_warning_code(warning.code),
"xmlPath": warning.xml_path,
"message": warning.message,
"preserved": warning.preserved,
})
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct HmlSaveState {
source_format: &'static str,
hml_savable: bool,
blockers: Vec<HmlSaveBlockerDto>,
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct HmlSaveBlockerDto {
code: String,
xml_path: String,
message: String,
preserved: bool,
}
fn hml_save_state(core: &DocumentCore) -> HmlSaveState {
let source_format = source_format_name(core.source_format);
match core.hml_export_preflight() {
Ok(()) => HmlSaveState {
source_format,
hml_savable: true,
blockers: Vec::new(),
},
Err(error) => {
let blockers = error
.blockers()
.iter()
.map(|blocker| HmlSaveBlockerDto {
code: blocker.code.to_string(),
xml_path: blocker.xml_path.clone(),
message: blocker.message.clone(),
preserved: false,
})
.collect();
HmlSaveState {
source_format,
hml_savable: false,
blockers,
}
}
}
}
fn source_format_name(format: crate::parser::FileFormat) -> &'static str {
match format {
crate::parser::FileFormat::Hwpx => "hwpx",
crate::parser::FileFormat::Hml => "hml",
crate::parser::FileFormat::Pdf => "pdf",
_ => "hwp",
}
}
fn format_hml_export_error(error: &crate::serializer::hml::HmlExportError) -> String {
use std::fmt::Write;
let mut message = error.to_string();
for blocker in error.blockers() {
let _ = write!(
message,
"\n[{}] {}: {}",
blocker.code, blocker.xml_path, blocker.message
);
}
message
}
#[wasm_bindgen]
impl HwpDocument {
#[wasm_bindgen(constructor)]
pub fn new(data: &[u8]) -> Result<HwpDocument, JsValue> {
DocumentCore::from_bytes(data)
.map(|core| HwpDocument { core })
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = supportsPdfImport)]
pub fn supports_pdf_import() -> bool {
cfg!(target_arch = "wasm32") && crate::pdf_support::pdf_import_support().enabled
}
#[wasm_bindgen(js_name = getPdfImportSupport)]
pub fn get_pdf_import_support() -> String {
serde_json::to_string(&crate::pdf_support::pdf_import_support())
.expect("compile-time PDF support is serializable")
}
#[wasm_bindgen(js_name = getPdfImportPolicy)]
pub fn get_pdf_import_policy() -> String {
#[cfg(feature = "pdf-import")]
{
serde_json::to_string(&crate::parser::pdf::PdfImportPolicy::default())
.expect("PDF policy serialization cannot fail")
}
#[cfg(not(feature = "pdf-import"))]
{
"null".to_owned()
}
}
#[wasm_bindgen(js_name = getPdfImportPolicyWithOptions)]
pub fn get_pdf_import_policy_with_options(options: JsValue) -> Result<String, JsValue> {
#[cfg(all(feature = "pdf-import", target_arch = "wasm32"))]
{
let (_, policy, _) = pdf_import::options(options)?;
Ok(serde_json::to_string(&policy).expect("PDF policy is serializable"))
}
#[cfg(not(all(feature = "pdf-import", target_arch = "wasm32")))]
{
let _ = options;
Err(JsValue::from_str(
"PDF_IMPORT_DISABLED: a PDF-enabled WASM runtime is required",
))
}
}
#[wasm_bindgen(js_name = getPdfImportRuntimeCapabilities)]
pub fn get_pdf_import_runtime_capabilities() -> String {
serde_json::json!({
"schemaVersion": 1,
"executionModel": "synchronous",
"preCopyPolicyValidation": cfg!(all(feature = "pdf-import", target_arch = "wasm32")),
"interruption": "hostWorkerTermination",
"requestTransportReservations": cfg!(all(feature = "pdf-import", target_arch = "wasm32")),
"editorReopenRemainingCredit": cfg!(all(feature = "pdf-import", target_arch = "wasm32")),
"editorReopenInterruption": "cooperativeChecksAndPublicationYield",
"mainThreadPreemption": false,
"processMemoryLimitEnforced": false,
"filesystemNetworkSandbox": false,
})
.to_string()
}
#[wasm_bindgen(js_name = importPdf)]
pub fn import_pdf(
#[wasm_bindgen(unchecked_param_type = "Uint8Array")] data: &PdfImportInput,
options: JsValue,
) -> Result<HwpDocument, JsValue> {
#[cfg(all(feature = "pdf-import", target_arch = "wasm32"))]
{
let (options, policy, _) = pdf_import::options(options)?;
let mut session = crate::PdfImportSession::new(policy)?;
let data = pdf_import::copy_input(data, &policy, &mut session)?;
session
.open(&data, &options)
.map(|core| HwpDocument { core })
.map_err(JsValue::from)
}
#[cfg(not(all(feature = "pdf-import", target_arch = "wasm32")))]
{
let _ = (data, options);
Err(JsValue::from_str(if cfg!(feature = "pdf-import") {
"PDF_IMPORT_OPTIONS: JS options require a WASM runtime; use DocumentCore::from_pdf_bytes in Rust"
} else {
"PDF_IMPORT_DISABLED: rebuild with --features pdf-import"
}))
}
}
#[wasm_bindgen(js_name = getPdfImportDiagnostics)]
pub fn get_pdf_import_diagnostics(&self) -> String {
#[cfg(feature = "pdf-import")]
{
serde_json::to_string(&self.core.pdf_metadata())
.expect("PDF import metadata serialization cannot fail")
}
#[cfg(not(feature = "pdf-import"))]
{
"null".to_string()
}
}
#[wasm_bindgen(js_name = openWithPassword)]
pub fn open_with_password(data: &[u8], password: &str) -> Result<HwpDocument, JsValue> {
let mut document = Self::from(DocumentCore::new_empty());
document.load_bytes(data, Some(password.as_bytes()))?;
Ok(document)
}
#[wasm_bindgen(js_name = createEmpty)]
pub fn create_empty() -> HwpDocument {
let mut core = DocumentCore::new_empty();
let mut section = Section::default();
section.section_def.page_def = crate::model::page::PageDef::a4_default();
section.paragraphs.push(Paragraph::new_empty());
let mut document = Document::default();
document.sections.push(section);
core.set_document(document);
HwpDocument { core }
}
#[wasm_bindgen(js_name = createBlankDocument)]
pub fn create_blank_document(&mut self) -> Result<String, JsValue> {
self.create_blank_document_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = registerExactFontSource)]
pub fn register_exact_font_source(
&mut self,
char_shape_id: u32,
language_index: u32,
font_bytes: &[u8],
face_index: u32,
) -> Result<String, JsValue> {
self.register_exact_font_source_native(
char_shape_id,
language_index as usize,
font_bytes,
face_index,
)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = registerHftOutlineFont)]
pub fn register_hft_outline_font(
&mut self,
family: &str,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_hft_outline_font_native(family, bytes, face_index)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = clearTextFontFamily)]
pub fn clear_text_font_family_wasm(&mut self, family: &str) {
self.clear_text_font_family_native(family);
}
#[wasm_bindgen(js_name = registerTextFontGlyphCoverage)]
pub fn register_text_font_glyph_coverage(
&mut self,
family: &str,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_text_font_glyph_coverage_native(family, bytes, face_index)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = ordinaryTextFontProgram)]
pub fn ordinary_text_font_program(&self, bytes: &[u8], face_index: u32) -> Option<Vec<u8>> {
crate::renderer::text_font_presentation::ordinary_text_font_program(bytes, face_index)
}
#[wasm_bindgen(js_name = registerMetafileFont)]
pub fn register_metafile_font(
&mut self,
family: &str,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_metafile_font_native(family, bytes, face_index)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = clearMetafileFonts)]
pub fn clear_metafile_fonts(&mut self) {
self.clear_metafile_fonts_native();
}
#[wasm_bindgen(js_name = getMetafileFontRequests)]
pub fn get_metafile_font_requests(&self) -> Result<String, JsValue> {
self.get_metafile_font_requests_native()
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = getPagePaintFontRequests)]
pub fn get_page_paint_font_requests(&self, page: u32) -> Result<String, JsValue> {
self.get_page_paint_font_requests_native(page)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = getTextShadowFontRequests)]
pub fn get_text_shadow_font_requests(&self) -> Result<String, JsValue> {
self.get_text_shadow_font_requests_native()
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = getNativePuaFontRequests)]
pub fn get_native_pua_font_requests(&self) -> Result<String, JsValue> {
serde_json::to_string(&self.native_pua_font_requests())
.map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen(js_name = getNativeOtfFontRequests)]
pub fn get_native_otf_font_requests_wasm(&self) -> Result<String, JsValue> {
serde_json::to_string(&self.native_otf_font_requests())
.map_err(|e| JsValue::from_str(&e.to_string()))
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = prepareNativeOtfFonts)]
pub fn prepare_native_otf_fonts_wasm(
&mut self,
provider: &js_sys::Function,
) -> Result<bool, JsValue> {
use wasm_bindgen::JsCast;
let mut prepared = std::collections::BTreeMap::new();
for request in self.native_otf_font_requests() {
let key = serde_json::to_string(&request)
.map_err(|error| JsValue::from_str(&error.to_string()))?;
let value = provider.call1(&JsValue::NULL, &JsValue::from_str(&key))?;
if value.is_null() || value.is_undefined() {
continue;
}
let values: Vec<JsValue> = if js_sys::Array::is_array(&value) {
let array = js_sys::Array::from(&value);
if array.length() > 256 {
return Err(JsValue::from_str(
"OTF provider program count exceeds limit",
));
}
array.iter().collect()
} else {
vec![value]
};
let mut programs = Vec::with_capacity(values.len());
let mut total_bytes = 0usize;
for value in values {
if !value.is_instance_of::<js_sys::Uint8Array>() {
return Err(JsValue::from_str(
"OTF provider must return Uint8Array, an array of Uint8Array, or null",
));
}
let array = js_sys::Uint8Array::new(&value);
let length = array.length() as usize;
total_bytes = total_bytes.saturating_add(length);
if length > 32 * 1024 * 1024 || total_bytes > 64 * 1024 * 1024 {
return Err(JsValue::from_str("OTF provider program exceeds limit"));
}
programs.push((array.to_vec(), 0));
}
prepared.insert(
(
request.family,
request.bold,
request.italic,
request.source_ttf,
),
programs,
);
}
self.prepare_native_otf_fonts_multiple_with(|request| {
prepared
.remove(&(
request.family.clone(),
request.bold,
request.italic,
request.source_ttf,
))
.unwrap_or_default()
})
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = registerNativePuaFont)]
pub fn register_native_pua_font_wasm(
&mut self,
family: &str,
bold: bool,
italic: bool,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_native_pua_font(family, bold, italic, bytes, face_index)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = registerNativePuaFontForSource)]
pub fn register_native_pua_font_for_source_wasm(
&mut self,
family: &str,
bold: bool,
italic: bool,
source_ttf: bool,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_native_pua_font_for_source(
family, bold, italic, source_ttf, bytes, face_index,
)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = registerLiteralFigureSpaceFont)]
pub fn register_literal_figure_space_font_wasm(
&mut self,
family: &str,
bold: bool,
italic: bool,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_literal_figure_space_font(family, bold, italic, bytes, face_index)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = registerLiteralFigureSpaceFontForSource)]
pub fn register_literal_figure_space_font_for_source_wasm(
&mut self,
family: &str,
bold: bool,
italic: bool,
source_ttf: bool,
bytes: &[u8],
face_index: u32,
) -> Result<bool, JsValue> {
self.register_literal_figure_space_font_for_source(
family, bold, italic, source_ttf, bytes, face_index,
)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = clearNativePuaFonts)]
pub fn clear_native_pua_fonts_wasm(&mut self) {
self.clear_native_pua_fonts();
}
#[wasm_bindgen(js_name = setExactFontInstance)]
pub fn set_exact_font_instance(&mut self, options_json: &str) -> Result<String, JsValue> {
self.set_exact_font_instance_native(options_json)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = clearExactFontInstance)]
pub fn clear_exact_font_instance(&mut self, options_json: &str) -> Result<String, JsValue> {
self.clear_exact_font_instance_native(options_json)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = setShowParagraphMarks)]
pub fn set_show_paragraph_marks(&mut self, enabled: bool) {
self.show_paragraph_marks = enabled;
self.invalidate_page_tree_cache();
}
#[wasm_bindgen(js_name = getShowParagraphMarks)]
pub fn get_show_paragraph_marks(&self) -> bool {
self.show_paragraph_marks
}
#[wasm_bindgen(js_name = setAnnotateMetricFont)]
pub fn set_annotate_metric_font(&mut self, enabled: bool) {
self.annotate_metric_font = enabled;
}
#[wasm_bindgen(js_name = getAnnotateMetricFont)]
pub fn get_annotate_metric_font(&self) -> bool {
self.annotate_metric_font
}
#[wasm_bindgen(js_name = getShowControlCodes)]
pub fn get_show_control_codes(&self) -> bool {
self.show_control_codes
}
#[wasm_bindgen(js_name = setShowControlCodes)]
pub fn set_show_control_codes(&mut self, enabled: bool) {
self.show_control_codes = enabled;
self.invalidate_page_tree_cache();
}
#[wasm_bindgen(js_name = getShowTransparentBorders)]
pub fn get_show_transparent_borders(&self) -> bool {
self.show_transparent_borders
}
#[wasm_bindgen(js_name = setShowTransparentBorders)]
pub fn set_show_transparent_borders(&mut self, enabled: bool) {
self.show_transparent_borders = enabled;
self.invalidate_page_tree_cache();
}
#[wasm_bindgen(js_name = setClipEnabled)]
pub fn set_clip_enabled(&mut self, enabled: bool) {
self.clip_enabled = enabled;
self.invalidate_page_tree_cache();
}
pub fn set_debug_overlay(&mut self, enabled: bool) {
self.debug_overlay = enabled;
}
pub fn set_respect_vpos_reset(&mut self, enabled: bool) {
if self.respect_vpos_reset != enabled {
self.respect_vpos_reset = enabled;
for d in self.core.dirty_sections.iter_mut() {
*d = true;
}
self.invalidate_page_tree_cache();
self.core.paginate();
}
}
#[wasm_bindgen(js_name = pageCount)]
pub fn page_count(&self) -> u32 {
self.core.page_count()
}
#[wasm_bindgen(js_name = renderPageSvg)]
pub fn render_page_svg(&self, page_num: u32) -> Result<String, JsValue> {
self.render_page_svg_native(page_num).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = renderPageSvgWithProfile)]
pub fn render_page_svg_with_profile(
&self,
page_num: u32,
profile: &str,
) -> Result<String, JsValue> {
let profile = crate::paint::RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
self.render_page_svg_layer_with_profile_native(page_num, profile)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = renderPageHtml)]
pub fn render_page_html(&self, page_num: u32) -> Result<String, JsValue> {
self.render_page_html_native(page_num).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = renderPageCanvas)]
pub fn render_page_canvas(&self, page_num: u32) -> Result<u32, JsValue> {
self.render_page_canvas_native(page_num)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = renderPageCanvasLegacy)]
pub fn render_page_canvas_legacy(&self, page_num: u32) -> Result<u32, JsValue> {
self.render_page_canvas_legacy_native(page_num)
.map_err(|e| e.into())
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderPageToCanvas)]
pub fn render_page_to_canvas(
&self,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
) -> Result<(), JsValue> {
use crate::renderer::layer_renderer::LayerRenderer;
use crate::renderer::web_canvas::WebCanvasRenderer;
crate::renderer::image_resolver::with_decoded_handoff(|| {
let tree = self
.build_canvas_page_layer_tree_with_profile(
page_num,
crate::paint::RenderProfile::Screen,
)
.map_err(JsValue::from)?;
let scale = normalize_canvas_scale(tree.page_width, tree.page_height, scale)
.map_err(JsValue::from_str)?;
canvas.set_width(scaled_canvas_extent(tree.page_width, scale));
canvas.set_height(scaled_canvas_extent(tree.page_height, scale));
let mut renderer = WebCanvasRenderer::new(canvas)?;
renderer.set_hft_outline_fonts(&self.hft_outline_fonts);
renderer.show_paragraph_marks = self.show_paragraph_marks;
renderer.show_control_codes = self.show_control_codes;
renderer.set_scale(scale);
renderer.render_page(&tree).map_err(JsValue::from)?;
Ok(())
})
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderHeaderFooterEditPreviewToCanvas)]
pub fn render_header_footer_edit_preview_to_canvas(
&self,
page_num: u32,
section_idx: u32,
is_header: bool,
apply_to: u8,
canvas: &HtmlCanvasElement,
scale: f64,
) -> Result<(), JsValue> {
use crate::renderer::web_canvas::WebCanvasRenderer;
let tree = self
.build_header_footer_edit_preview_tree(
page_num,
section_idx as usize,
is_header,
apply_to,
)
.map_err(JsValue::from)?;
let scale = normalize_canvas_scale(tree.root.bbox.width, tree.root.bbox.height, scale)
.map_err(JsValue::from_str)?;
canvas.set_width(scaled_canvas_extent(tree.root.bbox.width, scale));
canvas.set_height(scaled_canvas_extent(tree.root.bbox.height, scale));
let mut renderer = WebCanvasRenderer::new(canvas)?;
renderer.set_hft_outline_fonts(&self.hft_outline_fonts);
renderer.show_paragraph_marks = self.show_paragraph_marks;
renderer.show_control_codes = self.show_control_codes;
renderer.set_scale(scale);
renderer.render_tree(&tree);
Ok(())
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderPageToCanvasFiltered)]
pub fn render_page_to_canvas_filtered(
&self,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
layer_kind: &str,
) -> Result<(), JsValue> {
self.render_page_to_canvas_filtered_with_profile(
page_num, canvas, scale, layer_kind, "screen",
)
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderPageToCanvasFilteredWithProfile)]
pub fn render_page_to_canvas_filtered_with_profile(
&self,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
layer_kind: &str,
profile: &str,
) -> Result<(), JsValue> {
render_patch_boundary::render_page_to_canvas_filtered_with_profile(
self, page_num, canvas, scale, layer_kind, profile,
)
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderPagePatchToCanvasFilteredWithProfile)]
#[allow(clippy::too_many_arguments)]
pub fn render_page_patch_to_canvas_filtered_with_profile(
&self,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
layer_kind: &str,
profile: &str,
x: f64,
y: f64,
width: f64,
height: f64,
) -> Result<(), JsValue> {
use crate::renderer::render_tree::BoundingBox;
render_patch_boundary::render_page_patch_to_canvas_filtered_with_profile(
self,
page_num,
canvas,
scale,
layer_kind,
profile,
BoundingBox::new(x, y, width, height),
)
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = renderPageToCanvasLegacy)]
pub fn render_page_to_canvas_legacy(
&self,
page_num: u32,
canvas: &HtmlCanvasElement,
scale: f64,
) -> Result<(), JsValue> {
use crate::renderer::web_canvas::WebCanvasRenderer;
let tree = self
.build_page_tree_cached(page_num)
.map_err(|e| JsValue::from(e))?;
let scale = normalize_canvas_scale(tree.root.bbox.width, tree.root.bbox.height, scale)
.map_err(JsValue::from_str)?;
canvas.set_width(scaled_canvas_extent(tree.root.bbox.width, scale));
canvas.set_height(scaled_canvas_extent(tree.root.bbox.height, scale));
let mut renderer = WebCanvasRenderer::new(canvas)?;
renderer.set_hft_outline_fonts(&self.hft_outline_fonts);
renderer.show_paragraph_marks = self.show_paragraph_marks;
renderer.show_control_codes = self.show_control_codes;
renderer.set_scale(scale);
renderer.render_tree(&tree);
Ok(())
}
#[wasm_bindgen(js_name = getPageRenderTree)]
pub fn get_page_render_tree(&self, page_num: u32) -> Result<String, JsValue> {
let tree = self
.build_page_tree_cached(page_num)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(tree.root.to_json())
}
#[wasm_bindgen(js_name = getPageLayerTree)]
pub fn get_page_layer_tree(&self, page_num: u32) -> Result<String, JsValue> {
self.get_page_layer_tree_with_profile(page_num, "screen", Some(false), Some(false))
}
#[wasm_bindgen(js_name = getPageLayerTreeWithProfile)]
pub fn get_page_layer_tree_with_profile(
&self,
page_num: u32,
profile: &str,
omit_image_bytes: Option<bool>,
omit_font_bytes: Option<bool>,
) -> Result<String, JsValue> {
let omit_image_bytes = omit_image_bytes.unwrap_or(false);
let omit_font_bytes = omit_font_bytes.unwrap_or(false);
render_patch_boundary::get_page_layer_tree_with_profile(
self,
page_num,
profile,
omit_image_bytes,
omit_font_bytes,
)
}
#[cfg(all(feature = "subsecond-dev", target_arch = "wasm32"))]
#[wasm_bindgen(js_name = getRenderCodeRevision)]
pub fn get_render_code_revision(&self) -> String {
render_patch_boundary::patch_revision()
}
#[cfg(all(feature = "subsecond-dev", target_arch = "wasm32"))]
#[wasm_bindgen(js_name = rebuildDerivedState)]
pub fn rebuild_derived_state(&mut self) {
self.core.rebuild_derived_state();
}
#[wasm_bindgen(js_name = getDocumentFontSelectionPlan)]
pub fn get_document_font_selection_plan(&self) -> String {
self.core.get_document_font_selection_plan()
}
#[wasm_bindgen(js_name = buildFontEnvironmentFromAvailablePrograms)]
pub fn build_font_environment_from_available_programs(
&self,
json: &str,
) -> Result<String, JsValue> {
self.core
.build_font_environment_from_available_programs(json)
.map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen(js_name = setFontEnvironment)]
pub fn set_font_environment_json(&mut self, json: Option<String>) -> Result<bool, JsValue> {
let environment = json
.as_deref()
.map(crate::renderer::font_environment::FontEnvironment::from_json)
.transpose()
.map_err(|e| JsValue::from_str(&e.to_string()))?;
self.core
.set_font_environment(environment)
.map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen(js_name = getCanvasKitReplayPlan)]
pub fn get_canvaskit_replay_plan(&self, page_num: u32, mode: &str) -> Result<String, JsValue> {
self.get_canvaskit_replay_plan_native(page_num, mode)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFontDecisionTrace)]
pub fn get_font_decision_trace(
&self,
page_num: u32,
options_json: &str,
) -> Result<String, JsValue> {
self.get_font_decision_trace_native(page_num, options_json)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = getCanvasKitReplayPlanWithProfile)]
pub fn get_canvaskit_replay_plan_with_profile(
&self,
page_num: u32,
mode: &str,
profile: &str,
) -> Result<String, JsValue> {
let profile = crate::paint::RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
self.get_canvaskit_replay_plan_with_profile_native(page_num, mode, profile)
.map_err(|error| error.into())
}
#[wasm_bindgen(js_name = getCanvasKitDocumentPreflight)]
pub fn get_canvaskit_document_preflight(
&self,
mode: &str,
profile: &str,
) -> Result<String, JsValue> {
let profile = crate::paint::RenderProfile::parse(profile)
.ok_or_else(|| JsValue::from_str(&format!("unsupported render profile: {profile}")))?;
self.get_canvaskit_document_preflight_native(mode, profile)
.map_err(|error| error.into())
}
#[wasm_bindgen(js_name = getPageOverlayImages)]
pub fn get_page_overlay_images(&self, page_num: u32) -> Result<String, JsValue> {
render_patch_boundary::get_page_overlay_images(self, page_num)
}
#[wasm_bindgen(js_name = getPageSourceImageKeys)]
pub fn get_page_source_image_keys(&self, page_num: u32) -> Result<String, JsValue> {
self.get_page_source_image_keys_native(page_num)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageFlowImageOps)]
pub fn get_page_flow_image_ops(&self, page_num: u32) -> Result<String, JsValue> {
render_patch_boundary::get_page_flow_image_ops(self, page_num)
}
#[wasm_bindgen(js_name = getSourceImageBytes)]
pub fn get_source_image_bytes(&self, key: &str) -> Result<Vec<u8>, JsValue> {
match self.get_source_image_bytes_native(key) {
Some((_mime, bytes)) => Ok(bytes),
None => Err(JsValue::from_str(&format!(
"unresolvable source image key: {key}"
))),
}
}
#[wasm_bindgen(js_name = getSourceFontBytes)]
pub fn get_source_font_bytes(&self, key: &str) -> Result<Vec<u8>, JsValue> {
self.get_source_font_bytes_native(key)
.ok_or_else(|| JsValue::from_str(&format!("unresolvable source font key: {key}")))
}
#[wasm_bindgen(js_name = getPageInfo)]
pub fn get_page_info(&self, page_num: u32) -> Result<String, JsValue> {
self.get_page_info_native(page_num).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageDef)]
pub fn get_page_def(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_page_def_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPageDef)]
pub fn set_page_def(&mut self, section_idx: u32, json: &str) -> Result<String, JsValue> {
self.set_page_def_native(section_idx as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSectionDef)]
pub fn get_section_def(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_section_def_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setSectionDef)]
pub fn set_section_def(&mut self, section_idx: u32, json: &str) -> Result<String, JsValue> {
self.set_section_def_native(section_idx as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setSectionDefAll)]
pub fn set_section_def_all(&mut self, json: &str) -> Result<String, JsValue> {
self.set_section_def_all_native(json).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageBorderFill)]
pub fn get_page_border_fill(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_page_border_fill_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPageBorderFill)]
pub fn set_page_border_fill(
&mut self,
section_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.set_page_border_fill_native(section_idx as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getColumnDef)]
pub fn get_column_def(&self, section_idx: u32) -> Result<String, JsValue> {
let sec = self
.core
.document
.sections
.get(section_idx as usize)
.ok_or_else(|| JsValue::from_str("구역 인덱스 범위 초과"))?;
let col_def = HwpDocument::find_initial_column_def(&sec.paragraphs);
let col_type = match col_def.column_type {
crate::model::page::ColumnType::Normal => 0,
crate::model::page::ColumnType::Distribute => 1,
crate::model::page::ColumnType::Parallel => 2,
};
Ok(format!(
"{{\"columnCount\":{},\"columnType\":{},\"sameWidth\":{},\"spacing\":{}}}",
col_def.column_count, col_type, col_def.same_width, col_def.spacing,
))
}
#[wasm_bindgen(js_name = getDocumentInfo)]
pub fn get_document_info(&self) -> String {
self.core.get_document_info()
}
#[wasm_bindgen(js_name = getPageTextLayout)]
pub fn get_page_text_layout(&self, page_num: u32) -> Result<String, JsValue> {
self.get_page_text_layout_native(page_num)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageControlLayout)]
pub fn get_page_control_layout(&self, page_num: u32) -> Result<String, JsValue> {
self.get_page_control_layout_native(page_num)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setDpi)]
pub fn set_dpi(&mut self, dpi: f64) {
self.core.set_dpi(dpi);
}
#[wasm_bindgen(js_name = setFileName)]
pub fn set_file_name(&mut self, name: &str) {
if self.core.file_name != name {
self.core.file_name = name.to_string();
self.core.invalidate_page_tree_cache();
}
}
#[wasm_bindgen(js_name = getDpi)]
pub fn get_dpi(&self) -> f64 {
self.dpi
}
#[wasm_bindgen(js_name = setFallbackFont)]
pub fn set_fallback_font(&mut self, path: &str) {
self.fallback_font = path.to_string();
}
#[wasm_bindgen(js_name = getFallbackFont)]
pub fn get_fallback_font(&self) -> String {
self.fallback_font.clone()
}
#[wasm_bindgen(js_name = insertText)]
pub fn insert_text(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextLogical)]
pub fn insert_text_logical(
&mut self,
section_idx: u32,
para_idx: u32,
logical_offset: u32,
text: &str,
) -> Result<String, JsValue> {
let sec = section_idx as usize;
let pi = para_idx as usize;
if sec >= self.document.sections.len() || pi >= self.document.sections[sec].paragraphs.len()
{
return Err(JsValue::from_str("인덱스 범위 초과"));
}
let (text_offset, after_control) = crate::document_core::helpers::logical_to_text_offset(
&self.document.sections[sec].paragraphs[pi],
logical_offset as usize,
);
self.insert_text_at_caret_native(sec, pi, text_offset, after_control, text)?;
let new_text_offset = text_offset + text.chars().count();
let new_logical = crate::document_core::helpers::text_to_logical_offset(
&self.document.sections[sec].paragraphs[pi],
new_text_offset,
);
Ok(format!("{{\"ok\":true,\"logicalOffset\":{}}}", new_logical))
}
#[wasm_bindgen(js_name = getLogicalLength)]
pub fn get_logical_length(&self, section_idx: u32, para_idx: u32) -> Result<u32, JsValue> {
let sec = section_idx as usize;
let pi = para_idx as usize;
if sec >= self.document.sections.len() || pi >= self.document.sections[sec].paragraphs.len()
{
return Err(JsValue::from_str("인덱스 범위 초과"));
}
Ok(crate::document_core::helpers::logical_paragraph_length(
&self.document.sections[sec].paragraphs[pi],
) as u32)
}
#[wasm_bindgen(js_name = logicalToTextOffset)]
pub fn logical_to_text_offset(
&self,
section_idx: u32,
para_idx: u32,
logical_offset: u32,
) -> Result<u32, JsValue> {
let sec = section_idx as usize;
let pi = para_idx as usize;
if sec >= self.document.sections.len() || pi >= self.document.sections[sec].paragraphs.len()
{
return Err(JsValue::from_str("인덱스 범위 초과"));
}
let (text_offset, _) = crate::document_core::helpers::logical_to_text_offset(
&self.document.sections[sec].paragraphs[pi],
logical_offset as usize,
);
Ok(text_offset as u32)
}
#[wasm_bindgen(js_name = textToLogicalOffset)]
pub fn text_to_logical_offset(
&self,
section_idx: u32,
para_idx: u32,
text_offset: u32,
) -> Result<u32, JsValue> {
let sec = section_idx as usize;
let pi = para_idx as usize;
if sec >= self.document.sections.len() || pi >= self.document.sections[sec].paragraphs.len()
{
return Err(JsValue::from_str("인덱스 범위 초과"));
}
Ok(crate::document_core::helpers::text_to_logical_offset(
&self.document.sections[sec].paragraphs[pi],
text_offset as usize,
) as u32)
}
#[wasm_bindgen(js_name = deleteText)]
pub fn delete_text(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.delete_text_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceBodyTextLocal)]
pub fn replace_body_text_local(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
delete_count: u32,
text: &str,
) -> Result<String, JsValue> {
self.replace_body_text_local_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
delete_count as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextInCell)]
pub fn insert_text_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextInCellDeferredPagination)]
pub fn insert_text_in_cell_deferred_pagination(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_in_cell_native_deferred_pagination(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInCellDeferredPagination)]
pub fn delete_text_in_cell_deferred_pagination(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.delete_text_in_cell_native_deferred_pagination(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceTextInCellDeferredPagination)]
pub fn replace_text_in_cell_deferred_pagination(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
delete_count: u32,
text: &str,
) -> Result<String, JsValue> {
self.replace_text_in_cell_native_deferred_pagination(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
delete_count as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = beginDeferredPagination)]
pub fn begin_deferred_pagination(&mut self, fragment_budget: u32) -> Result<String, JsValue> {
Ok(deferred_pagination_result_json(
self.core
.begin_deferred_pagination((fragment_budget as usize).max(1)),
))
}
#[wasm_bindgen(js_name = stepDeferredPagination)]
pub fn step_deferred_pagination(&mut self, fragment_budget: u32) -> Result<String, JsValue> {
Ok(deferred_pagination_result_json(
self.core
.step_deferred_pagination((fragment_budget as usize).max(1)),
))
}
#[wasm_bindgen(js_name = cancelDeferredPagination)]
pub fn cancel_deferred_pagination(&mut self) -> bool {
self.core.cancel_deferred_pagination()
}
#[wasm_bindgen(js_name = flushDeferredPagination)]
pub fn flush_deferred_pagination(&mut self) -> Result<String, JsValue> {
Ok(deferred_pagination_result_json(
self.core.flush_deferred_pagination(),
))
}
#[wasm_bindgen(js_name = insertTextInCellEx)]
pub fn insert_text_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_str, json_u32};
self.insert_text_in_cell_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
&json_str(options_json, "text").unwrap_or_default(),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInCell)]
pub fn delete_text_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.delete_text_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInCellEx)]
pub fn delete_text_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.delete_text_in_cell_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_u32(options_json, "count").unwrap_or(0) as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitParagraphInCell)]
pub fn split_paragraph_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
removed_para_meta: Option<String>,
) -> Result<String, JsValue> {
self.split_paragraph_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
parse_removed_para_meta(removed_para_meta)?,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeParagraphInCell)]
pub fn merge_paragraph_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
) -> Result<String, JsValue> {
self.merge_paragraph_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextInCellByPath)]
pub fn insert_text_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.insert_text_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInCellByPath)]
pub fn delete_text_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.delete_text_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteRangeInCellByPath)]
#[allow(clippy::too_many_arguments)]
pub fn delete_range_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_para: u32,
start_offset: u32,
end_para: u32,
end_offset: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.delete_range_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
start_para as usize,
start_offset as usize,
end_para as usize,
end_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitParagraphInCellByPath)]
pub fn split_paragraph_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
removed_para_meta: Option<String>,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.split_paragraph_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
parse_removed_para_meta(removed_para_meta)?,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeParagraphInCellByPath)]
pub fn merge_paragraph_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.merge_paragraph_in_cell_by_path(section_idx as usize, parent_para_idx as usize, &path)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTextInCellByPath)]
pub fn get_text_in_cell_by_path_api(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.get_text_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooter)]
pub fn get_header_footer(
&self,
section_idx: u32,
is_header: bool,
apply_to: u8,
) -> Result<String, JsValue> {
self.get_header_footer_native(section_idx as usize, is_header, apply_to)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = createHeaderFooter)]
pub fn create_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
) -> Result<String, JsValue> {
self.create_header_footer_native(section_idx as usize, is_header, apply_to)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextInHeaderFooter)]
pub fn insert_text_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInHeaderFooter)]
pub fn delete_text_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.delete_text_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitParagraphInHeaderFooter)]
pub fn split_paragraph_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
char_offset: u32,
removed_para_meta: Option<String>,
) -> Result<String, JsValue> {
self.split_paragraph_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
char_offset as usize,
parse_removed_para_meta(removed_para_meta)?,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeParagraphInHeaderFooter)]
pub fn merge_paragraph_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
) -> Result<String, JsValue> {
self.merge_paragraph_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooterParaInfo)]
pub fn get_header_footer_para_info(
&self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
) -> Result<String, JsValue> {
self.get_header_footer_para_info_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceRangeInHeaderFooter)]
#[allow(clippy::too_many_arguments)]
pub fn replace_range_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
start_hf_para_idx: u32,
start_char_offset: u32,
end_hf_para_idx: u32,
end_char_offset: u32,
replacement_text: &str,
) -> Result<String, JsValue> {
self.replace_range_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
start_hf_para_idx as usize,
start_char_offset as usize,
end_hf_para_idx as usize,
end_char_offset as usize,
replacement_text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copySelectionInHeaderFooter)]
#[allow(clippy::too_many_arguments)]
pub fn copy_selection_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
start_hf_para_idx: u32,
start_char_offset: u32,
end_hf_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.copy_selection_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
start_hf_para_idx as usize,
start_char_offset as usize,
end_hf_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCharPropertiesInHeaderFooter)]
pub fn get_char_properties_in_header_footer(
&self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_char_properties_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCharFormatInHeaderFooter)]
#[allow(clippy::too_many_arguments)]
pub fn apply_char_format_in_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u8,
start_hf_para_idx: u32,
start_char_offset: u32,
end_hf_para_idx: u32,
end_char_offset: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_char_format_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
start_hf_para_idx as usize,
start_char_offset as usize,
end_hf_para_idx as usize,
end_char_offset as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTable)]
pub fn split_table(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
at_row: u32,
) -> Result<String, JsValue> {
let at_row = row_index_from_u32(at_row)?;
self.split_table_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
at_row,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeTableWithNext)]
pub fn merge_table_with_next(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.merge_table_with_next_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTableRow)]
pub fn insert_table_row(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
row_idx: u32,
below: bool,
) -> Result<String, JsValue> {
self.insert_table_row_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
row_idx as u16,
below,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTableColumn)]
pub fn insert_table_column(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
col_idx: u32,
right: bool,
) -> Result<String, JsValue> {
self.insert_table_column_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
col_idx as u16,
right,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTableRow)]
pub fn delete_table_row(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
row_idx: u32,
) -> Result<String, JsValue> {
self.delete_table_row_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
row_idx as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTableColumn)]
pub fn delete_table_column(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
col_idx: u32,
) -> Result<String, JsValue> {
self.delete_table_column_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
col_idx as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeTableCells)]
pub fn merge_table_cells(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u32,
start_col: u32,
end_row: u32,
end_col: u32,
) -> Result<String, JsValue> {
self.merge_table_cells_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
start_row as u16,
start_col as u16,
end_row as u16,
end_col as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeTableCellsEx)]
pub fn merge_table_cells_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.merge_table_cells_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "startRow").unwrap_or(0) as u16,
json_u32(options_json, "startCol").unwrap_or(0) as u16,
json_u32(options_json, "endRow").unwrap_or(0) as u16,
json_u32(options_json, "endCol").unwrap_or(0) as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTableCell)]
pub fn split_table_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
row: u32,
col: u32,
) -> Result<String, JsValue> {
self.split_table_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
row as u16,
col as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTableCellInto)]
pub fn split_table_cell_into(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
row: u32,
col: u32,
n_rows: u32,
m_cols: u32,
equal_row_height: bool,
merge_first: bool,
) -> Result<String, JsValue> {
self.split_table_cell_into_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
row as u16,
col as u16,
n_rows as u16,
m_cols as u16,
equal_row_height,
merge_first,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTableCellIntoEx)]
pub fn split_table_cell_into_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_u32};
self.split_table_cell_into_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "row").unwrap_or(0) as u16,
json_u32(options_json, "col").unwrap_or(0) as u16,
json_u32(options_json, "nRows").unwrap_or(1) as u16,
json_u32(options_json, "mCols").unwrap_or(1) as u16,
json_bool(options_json, "equalRowHeight").unwrap_or(false),
json_bool(options_json, "mergeFirst").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTableCellsInRange)]
pub fn split_table_cells_in_range(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u32,
start_col: u32,
end_row: u32,
end_col: u32,
n_rows: u32,
m_cols: u32,
equal_row_height: bool,
) -> Result<String, JsValue> {
self.split_table_cells_in_range_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
start_row as u16,
start_col as u16,
end_row as u16,
end_col as u16,
n_rows as u16,
m_cols as u16,
equal_row_height,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitTableCellsInRangeEx)]
pub fn split_table_cells_in_range_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_u32};
self.split_table_cells_in_range_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "startRow").unwrap_or(0) as u16,
json_u32(options_json, "startCol").unwrap_or(0) as u16,
json_u32(options_json, "endRow").unwrap_or(0) as u16,
json_u32(options_json, "endCol").unwrap_or(0) as u16,
json_u32(options_json, "nRows").unwrap_or(1) as u16,
json_u32(options_json, "mCols").unwrap_or(1) as u16,
json_bool(options_json, "equalRowHeight").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copyTableCellsTransposed)]
pub fn copy_table_cells_transposed(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u32,
start_col: u32,
end_row: u32,
end_col: u32,
) -> Result<String, JsValue> {
self.copy_table_cells_transposed_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
start_row as u16,
start_col as u16,
end_row as u16,
end_col as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteTableCellsTransposed)]
pub fn paste_table_cells_transposed(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u32,
start_col: u32,
) -> Result<String, JsValue> {
self.paste_table_cells_transposed_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
start_row as u16,
start_col as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = transposeTableCellsInPlace)]
pub fn transpose_table_cells_in_place(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.transpose_table_cells_in_place_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteTableCellsTransposedAsTable)]
pub fn paste_table_cells_transposed_as_table(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.paste_table_cells_transposed_as_new_table_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hasTableTransposeClipboard)]
pub fn has_table_transpose_clipboard(&self) -> bool {
self.has_table_transpose_clipboard_native()
}
#[wasm_bindgen(js_name = splitParagraph)]
pub fn split_paragraph(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
removed_para_meta: Option<String>,
) -> Result<String, JsValue> {
self.split_paragraph_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
parse_removed_para_meta(removed_para_meta)?,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertPageBreak)]
pub fn insert_page_break(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.insert_page_break_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertColumnBreak)]
pub fn insert_column_break(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.insert_column_break_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertNewNumber)]
pub fn insert_new_number(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
start_num: u32,
) -> Result<String, JsValue> {
if start_num == 0 || start_num > 65535 {
return Err(JsValue::from_str("start_num must be 1~65535"));
}
self.insert_new_number_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
start_num as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setColumnDef)]
pub fn set_column_def(
&mut self,
section_idx: u32,
column_count: u32,
column_type: u32,
same_width: u32,
spacing_hu: i32,
) -> Result<String, JsValue> {
self.set_column_def_native(
section_idx as usize,
column_count as u16,
column_type as u8,
same_width != 0,
spacing_hu as i16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeParagraph)]
pub fn merge_paragraph(&mut self, section_idx: u32, para_idx: u32) -> Result<String, JsValue> {
self.merge_paragraph_native(section_idx as usize, para_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteParagraph)]
pub fn delete_paragraph(&mut self, section_idx: u32, para_idx: u32) -> Result<String, JsValue> {
self.delete_paragraph_native(section_idx as usize, para_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertParagraph)]
pub fn insert_paragraph(&mut self, section_idx: u32, para_idx: u32) -> Result<String, JsValue> {
self.insert_paragraph_native(section_idx as usize, para_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitSection)]
pub fn split_section(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.split_section_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeSection)]
pub fn merge_section(&mut self, section_idx: u32) -> Result<String, JsValue> {
self.merge_section_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSectionCount)]
pub fn get_section_count(&self) -> u32 {
self.document.sections.len() as u32
}
#[wasm_bindgen(js_name = getParagraphCount)]
pub fn get_paragraph_count(&self, section_idx: u32) -> Result<u32, JsValue> {
self.get_paragraph_count_native(section_idx as usize)
.map(|v| v as u32)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getParagraphLength)]
pub fn get_paragraph_length(&self, section_idx: u32, para_idx: u32) -> Result<u32, JsValue> {
self.get_paragraph_length_native(section_idx as usize, para_idx as usize)
.map(|v| v as u32)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTextBoxControlIndex)]
pub fn get_textbox_control_index(&self, section_idx: u32, para_idx: u32) -> i32 {
self.get_textbox_control_index_native(section_idx as usize, para_idx as usize)
}
#[wasm_bindgen(js_name = findNextEditableControl)]
pub fn find_next_editable_control(
&self,
section_idx: u32,
para_idx: u32,
ctrl_idx: i32,
delta: i32,
) -> String {
self.find_next_editable_control_native(
section_idx as usize,
para_idx as usize,
ctrl_idx,
delta,
)
}
#[wasm_bindgen(js_name = findNearestControlBackward)]
pub fn find_nearest_control_backward(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> String {
self.find_nearest_control_backward_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
}
#[wasm_bindgen(js_name = findNearestControlForward)]
pub fn find_nearest_control_forward(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> String {
self.find_nearest_control_forward_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
}
#[wasm_bindgen(js_name = getControlTextPositions)]
pub fn get_control_text_positions(&self, section_idx: u32, para_idx: u32) -> String {
let sections = &self.document.sections;
if let Some(sec) = sections.get(section_idx as usize) {
if let Some(para) = sec.paragraphs.get(para_idx as usize) {
let positions = crate::document_core::find_control_text_positions(para);
return format!(
"[{}]",
positions
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(",")
);
}
}
"[]".to_string()
}
#[wasm_bindgen(js_name = navigateNextEditable)]
pub fn navigate_next_editable_wasm(
&self,
sec: u32,
para: u32,
char_offset: u32,
delta: i32,
context_json: &str,
) -> String {
let raw_context = DocumentCore::parse_nav_context(context_json);
let context = DocumentCore::fix_context_text_positions(
&self.core.document.sections,
sec as usize,
&raw_context,
);
let overflow_links = self.core.get_overflow_links(sec as usize);
let max_para = if !context.is_empty() {
let last = &context[context.len() - 1];
self.core.last_rendered_para_in_container(
sec as usize,
last.parent_para,
last.ctrl_idx,
last.cell_idx,
)
} else {
None
};
let result = self.core.navigate_next_editable(
sec as usize,
para as usize,
char_offset as usize,
delta,
&context,
max_para,
&overflow_links,
);
DocumentCore::nav_result_to_json(&result)
}
#[wasm_bindgen(js_name = getTextRange)]
pub fn get_text_range(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.get_text_range_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellParagraphCount)]
pub fn get_cell_paragraph_count(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<u32, JsValue> {
self.get_cell_paragraph_count_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)
.map(|v| v as u32)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellParagraphLength)]
pub fn get_cell_paragraph_length(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
) -> Result<u32, JsValue> {
self.get_cell_paragraph_length_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
)
.map(|v| v as u32)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellParagraphCountByPath)]
pub fn get_cell_paragraph_count_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<u32, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
let count = self
.resolve_container_para_count_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
)
.map_err(|e| -> JsValue { e.into() })?;
Ok(count as u32)
}
#[wasm_bindgen(js_name = getCellParagraphLengthByPath)]
pub fn get_cell_paragraph_length_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<u32, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
let para = self
.resolve_paragraph_by_path(section_idx as usize, parent_para_idx as usize, &path)
.map_err(|e| -> JsValue { e.into() })?;
Ok(para.text.chars().count() as u32)
}
#[wasm_bindgen(js_name = getCellTextDirection)]
pub fn get_cell_text_direction(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<u32, JsValue> {
let para = self
.document
.sections
.get(section_idx as usize)
.ok_or_else(|| JsValue::from_str("구역 인덱스 범위 초과"))?
.paragraphs
.get(parent_para_idx as usize)
.ok_or_else(|| JsValue::from_str("문단 인덱스 범위 초과"))?;
match para.controls.get(control_idx as usize) {
Some(Control::Table(table)) => {
let cell = table
.cells
.get(cell_idx as usize)
.ok_or_else(|| JsValue::from_str("셀 인덱스 범위 초과"))?;
Ok(cell.text_direction as u32)
}
_ => Ok(0), }
}
#[wasm_bindgen(js_name = getTextInCell)]
pub fn get_text_in_cell(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.get_text_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTextInCellEx)]
pub fn get_text_in_cell_ex(&self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.get_text_in_cell_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_u32(options_json, "count").unwrap_or(0) as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRect)]
pub fn get_cursor_rect(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectOnLine)]
pub fn get_cursor_rect_on_line(
&self,
section_idx: u32,
para_idx: u32,
line_index: u32,
at_end: bool,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
) -> Result<String, JsValue> {
let cell_ctx = if parent_para_idx == u32::MAX {
None
} else {
Some((
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
))
};
self.get_cursor_rect_on_line_native(
section_idx as usize,
para_idx as usize,
line_index as usize,
at_end,
cell_ctx,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTest)]
pub fn hit_test(&self, page_num: u32, x: f64, y: f64) -> Result<String, JsValue> {
self.hit_test_native(page_num, x, y).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectInHeaderFooter)]
pub fn get_cursor_rect_in_header_footer(
&self,
section_idx: u32,
is_header: bool,
apply_to: u8,
hf_para_idx: u32,
char_offset: u32,
preview_page_hint: i32,
) -> Result<String, JsValue> {
self.get_cursor_rect_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
hf_para_idx as usize,
char_offset as usize,
preview_page_hint,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInHeaderFooter)]
#[allow(clippy::too_many_arguments)]
pub fn get_selection_rects_in_header_footer(
&self,
section_idx: u32,
is_header: bool,
apply_to: u8,
page_num: u32,
start_hf_para_idx: u32,
start_char_offset: u32,
end_hf_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.get_selection_rects_in_header_footer_native(
section_idx as usize,
is_header,
apply_to,
page_num,
start_hf_para_idx as usize,
start_char_offset as usize,
end_hf_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTestHeaderFooter)]
pub fn hit_test_header_footer(&self, page_num: u32, x: f64, y: f64) -> Result<String, JsValue> {
self.hit_test_header_footer_native(page_num, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooterEditTarget)]
pub fn get_header_footer_edit_target(
&self,
page_num: u32,
is_header: bool,
) -> Result<String, JsValue> {
self.get_header_footer_edit_target_native(page_num, is_header)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooterPreviewPage)]
pub fn get_header_footer_preview_page(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_header_footer_preview_page_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTestInHeaderFooter)]
pub fn hit_test_in_header_footer(
&self,
page_num: u32,
is_header: bool,
x: f64,
y: f64,
) -> Result<String, JsValue> {
self.hit_test_in_header_footer_native(page_num, is_header, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTestInHeaderFooterTarget)]
pub fn hit_test_in_header_footer_target(
&self,
page_num: u32,
section_idx: u32,
is_header: bool,
apply_to: u8,
x: f64,
y: f64,
) -> Result<String, JsValue> {
self.hit_test_in_header_footer_target_native(
page_num,
section_idx as usize,
is_header,
apply_to,
x,
y,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getParaPropertiesInHf)]
pub fn get_para_properties_in_hf(
&self,
section_idx: usize,
is_header: bool,
apply_to: u8,
hf_para_idx: usize,
) -> Result<String, JsValue> {
self.get_para_properties_in_hf_native(section_idx, is_header, apply_to, hf_para_idx)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormatInHf)]
pub fn apply_para_format_in_hf(
&mut self,
section_idx: usize,
is_header: bool,
apply_to: u8,
hf_para_idx: usize,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_para_format_in_hf_native(
section_idx,
is_header,
apply_to,
hf_para_idx,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertFieldInHf)]
pub fn insert_field_in_hf(
&mut self,
section_idx: usize,
is_header: bool,
apply_to: u8,
hf_para_idx: usize,
char_offset: usize,
field_type: u8,
) -> Result<String, JsValue> {
self.insert_field_in_hf_native(
section_idx,
is_header,
apply_to,
hf_para_idx,
char_offset,
field_type,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyHfTemplate)]
pub fn apply_hf_template(
&mut self,
section_idx: usize,
is_header: bool,
apply_to: u8,
template_id: u8,
) -> Result<String, JsValue> {
self.apply_hf_template_native(section_idx, is_header, apply_to, template_id)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteHeaderFooter)]
pub fn delete_header_footer(
&mut self,
section_idx: u32,
is_header: bool,
apply_to: u32,
) -> Result<String, JsValue> {
self.delete_header_footer_native(section_idx as usize, is_header, apply_to as u8)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooterList)]
pub fn get_header_footer_list(
&self,
current_section_idx: u32,
current_is_header: bool,
current_apply_to: u32,
) -> Result<String, JsValue> {
self.get_header_footer_list_native(
current_section_idx as usize,
current_is_header,
current_apply_to as u8,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = navigateHeaderFooterByPage)]
pub fn navigate_header_footer_by_page(
&self,
current_page: u32,
is_header: bool,
direction: i32,
) -> Result<String, JsValue> {
self.navigate_header_footer_by_page_native(current_page, is_header, direction)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = toggleHideHeaderFooter)]
pub fn toggle_hide_header_footer(
&mut self,
page_index: u32,
is_header: bool,
) -> Result<String, JsValue> {
self.toggle_hide_header_footer_native(page_index, is_header)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectInCell)]
pub fn get_cursor_rect_in_cell(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getLineInfo)]
pub fn get_line_info(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_line_info_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getLineInfoInCell)]
pub fn get_line_info_in_cell(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_line_info_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCaretPosition)]
pub fn get_caret_position(&self) -> Result<String, JsValue> {
self.get_caret_position_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCaretPosition)]
pub fn set_caret_position(&mut self, section_idx: u32, para_idx: u32, char_offset: u32) {
self.set_caret_position_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
);
}
#[wasm_bindgen(js_name = getTableDimensions)]
pub fn get_table_dimensions(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_table_dimensions_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellInfo)]
pub fn get_cell_info(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<String, JsValue> {
self.get_cell_info_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellPropertiesByPath)]
pub fn get_cell_properties_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
cell_idx: u32,
) -> Result<String, JsValue> {
let path = parse_cell_path_arg(cell_path_json)?;
self.get_cell_properties_by_cell_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
cell_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellProperties)]
pub fn get_cell_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<String, JsValue> {
self.get_cell_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellOwnProperties)]
pub fn get_cell_own_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<String, JsValue> {
self.get_cell_own_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellProperties)]
pub fn set_cell_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.set_cell_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellZoneProperties)]
pub fn set_cell_zone_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u32,
start_col: u32,
end_row: u32,
end_col: u32,
json: &str,
) -> Result<String, JsValue> {
self.set_cell_zone_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
start_row as u16,
start_col as u16,
end_row as u16,
end_col as u16,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCellBorderFillIds)]
pub fn apply_cell_border_fill_ids(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.apply_cell_border_fill_ids_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = discardBorderPaintUndo)]
pub fn discard_border_paint_undo(&mut self, id: u32) {
self.discard_border_paint_undo_native(id);
}
#[wasm_bindgen(js_name = removeBorderFillTails)]
pub fn remove_border_fill_tails(&mut self, json: &str) -> Result<String, JsValue> {
self.remove_border_fill_tails_native(json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = resizeTableCells)]
pub fn resize_table_cells(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.resize_table_cells_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = equalizeTableCellSizes)]
#[allow(clippy::too_many_arguments)]
pub fn equalize_table_cell_sizes(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
start_row: u16,
start_col: u16,
end_row: u16,
end_col: u16,
axis: &str,
) -> Result<String, JsValue> {
let axis = match axis {
"width" => crate::model::table::CellSizeAxis::Width,
"height" => crate::model::table::CellSizeAxis::Height,
_ => return Err(JsValue::from_str("axis는 width 또는 height입니다")),
};
self.equalize_table_cell_sizes_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
(start_row, start_col, end_row, end_col),
axis,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = resizeTableCellsByPath)]
pub fn resize_table_cells_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
json: &str,
) -> Result<String, JsValue> {
let path = parse_cell_path_arg(cell_path_json)?;
self.resize_table_cells_by_cell_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveTableOffset)]
pub fn move_table_offset(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
delta_h: i32,
delta_v: i32,
) -> Result<String, JsValue> {
self.move_table_offset_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
delta_h,
delta_v,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableProperties)]
pub fn get_table_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_table_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setTableProperties)]
pub fn set_table_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.set_table_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableCellBboxes)]
pub fn get_table_cell_bboxes(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
page_hint: Option<u32>,
) -> Result<String, JsValue> {
self.get_table_cell_bboxes_from_page(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
page_hint.unwrap_or(0) as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableBBox)]
pub fn get_table_bbox(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_table_bbox_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableBBoxAtPage)]
pub fn get_table_bbox_at_page(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
page_idx: u32,
) -> Result<String, JsValue> {
self.get_table_bbox_at_page_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
page_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getShapeBBox)]
pub fn get_shape_bbox(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_shape_bbox_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTableControl)]
pub fn delete_table_control(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.delete_table_control_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = createTable)]
pub fn create_table(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
row_count: u32,
col_count: u32,
) -> Result<String, JsValue> {
self.create_table_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
row_count as u16,
col_count as u16,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = createTableEx)]
pub fn create_table_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_u32};
let section_idx = json_u32(options_json, "sectionIdx").unwrap_or(0) as usize;
let para_idx = json_u32(options_json, "paraIdx").unwrap_or(0) as usize;
let char_offset = json_u32(options_json, "charOffset").unwrap_or(0) as usize;
let row_count = json_u32(options_json, "rowCount").unwrap_or(2) as u16;
let col_count = json_u32(options_json, "colCount").unwrap_or(2) as u16;
let treat_as_char = json_bool(options_json, "treatAsChar").unwrap_or(false);
fn parse_u32_array(json: &str, key: &str) -> Option<Vec<u32>> {
if let Some(start) = json.find(&format!("\"{}\"", key)) {
let rest = &json[start..];
if let Some(arr_start) = rest.find('[') {
if let Some(arr_end) = rest[arr_start..].find(']') {
let arr_str = &rest[arr_start + 1..arr_start + arr_end];
let nums: Vec<u32> = arr_str
.split(',')
.filter_map(|s| s.trim().parse::<u32>().ok())
.collect();
if !nums.is_empty() {
Some(nums)
} else {
None
}
} else {
None
}
} else {
None
}
} else {
None
}
}
let col_widths = parse_u32_array(options_json, "colWidths");
let row_heights = parse_u32_array(options_json, "rowHeights");
self.create_table_ex_native(
section_idx,
para_idx,
char_offset,
row_count,
col_count,
treat_as_char,
col_widths.as_deref(),
row_heights.as_deref(),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = createTableInCellByPath)]
pub fn create_table_in_cell_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
char_offset: u32,
options_json: &str,
) -> Result<String, JsValue> {
let options: serde_json::Value = serde_json::from_str(options_json)
.map_err(|e| JsValue::from_str(&format!("options JSON 파싱 실패: {}", e)))?;
let treat_as_char = options
.get("treatAsChar")
.and_then(|v| v.as_bool())
.unwrap_or(true);
let count = |key: &str, default: u64| -> u16 {
options.get(key).and_then(|v| v.as_u64()).unwrap_or(default) as u16
};
let hu_list = |key: &str| -> Option<Vec<u32>> {
let list: Vec<u32> = options
.get(key)?
.as_array()?
.iter()
.filter_map(|v| v.as_u64().map(|n| n as u32))
.collect();
(!list.is_empty()).then_some(list)
};
let path = crate::document_core::DocumentCore::parse_cell_path(cell_path_json)
.map_err(|e| -> JsValue { e.into() })?;
let col_widths = hu_list("colWidths");
let row_heights = hu_list("rowHeights");
self.create_table_in_cell_by_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
count("rowCount", 2),
count("colCount", 2),
col_widths.as_deref(),
row_heights.as_deref(),
treat_as_char,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertPicture)]
#[allow(clippy::too_many_arguments)]
pub fn insert_picture(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
cell_path_json: &str,
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, JsValue> {
let cell_path: Vec<(usize, usize, usize)> =
if cell_path_json.is_empty() || cell_path_json == "[]" {
Vec::new()
} else {
DocumentCore::parse_cell_path(cell_path_json).map_err(JsValue::from)?
};
self.insert_picture_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
&cell_path,
image_data,
width,
height,
natural_width_px,
natural_height_px,
extension,
description,
paper_offset_x_hu,
paper_offset_y_hu,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertPictureInCell)]
#[allow(clippy::too_many_arguments)]
pub fn insert_picture_in_cell(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
cell_path_json: &str,
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, JsValue> {
let cell_path = DocumentCore::parse_cell_path(cell_path_json).map_err(JsValue::from)?;
self.insert_picture_in_cell_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
&cell_path,
image_data,
width,
height,
natural_width_px,
natural_height_px,
extension,
description,
offset_x_hu,
offset_y_hu,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = relocateFloatingPicture)]
#[allow(clippy::too_many_arguments)]
pub fn relocate_floating_picture(
&mut self,
src_section: u32,
src_para: u32,
src_cell_path_json: &str,
src_control: u32,
dst_section: u32,
dst_para: u32,
dst_char_offset: u32,
dst_cell_path_json: &str,
horz_offset: i32,
vert_offset: i32,
) -> Result<String, JsValue> {
let src_path = DocumentCore::parse_cell_path(src_cell_path_json).map_err(JsValue::from)?;
let dst_path = DocumentCore::parse_cell_path(dst_cell_path_json).map_err(JsValue::from)?;
self.relocate_floating_picture_native(
src_section as usize,
src_para as usize,
&src_path,
src_control as usize,
dst_section as usize,
dst_para as usize,
dst_char_offset as usize,
&dst_path,
horz_offset,
vert_offset,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertPictureEx)]
pub fn insert_picture_ex(
&mut self,
options_json: &str,
image_data: &[u8],
) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_i32, json_str, json_u32};
let section_idx = json_u32(options_json, "sectionIdx").unwrap_or(0);
let para_idx = json_u32(options_json, "paraIdx").unwrap_or(0);
let char_offset = json_u32(options_json, "charOffset").unwrap_or(0);
let cell_path_json = json_str(options_json, "cellPath").unwrap_or_default();
let width = json_u32(options_json, "width").unwrap_or(0);
let height = json_u32(options_json, "height").unwrap_or(0);
let natural_width_px = json_u32(options_json, "naturalWidthPx").unwrap_or(0);
let natural_height_px = json_u32(options_json, "naturalHeightPx").unwrap_or(0);
let extension = json_str(options_json, "extension").unwrap_or_default();
let description = json_str(options_json, "description").unwrap_or_default();
let paper_offset_x_hu = json_i32(options_json, "paperOffsetXHu");
let paper_offset_y_hu = json_i32(options_json, "paperOffsetYHu");
let cell_path: Vec<(usize, usize, usize)> =
if cell_path_json.is_empty() || cell_path_json == "[]" {
Vec::new()
} else {
DocumentCore::parse_cell_path(&cell_path_json).map_err(JsValue::from)?
};
self.insert_picture_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
&cell_path,
image_data,
width,
height,
natural_width_px,
natural_height_px,
&extension,
&description,
paper_offset_x_hu,
paper_offset_y_hu,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = assignPictureImage)]
#[allow(clippy::too_many_arguments)]
pub fn assign_picture_image(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
control_idx: u32,
image_data: &[u8],
natural_width_px: u32,
natural_height_px: u32,
extension: &str,
) -> Result<String, JsValue> {
let cell_path: Vec<(usize, usize, usize)> =
if cell_path_json.is_empty() || cell_path_json == "[]" {
Vec::new()
} else {
DocumentCore::parse_cell_path(cell_path_json).map_err(JsValue::from)?
};
self.assign_picture_image_native(
section_idx as usize,
parent_para_idx as usize,
&cell_path,
control_idx as usize,
image_data,
natural_width_px,
natural_height_px,
extension,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getExternalImageReferences)]
pub fn get_external_image_references(&self) -> String {
serde_json::to_string(&collect_external_image_references(self.document()))
.unwrap_or_else(|_| "[]".to_string())
}
#[wasm_bindgen(js_name = getExternalImageBasenames)]
pub fn get_external_image_basenames(&self) -> String {
use std::collections::BTreeSet;
let mut names: BTreeSet<String> = BTreeSet::new();
for reference in collect_external_image_references(self.document()) {
if !reference.loaded {
names.insert(reference.basename);
}
}
let arr: Vec<String> = names.into_iter().collect();
serde_json::to_string(&arr).unwrap_or_else(|_| "[]".to_string())
}
#[wasm_bindgen(js_name = injectExternalImage)]
pub fn inject_external_image(
&mut self,
basename: &str,
data: &[u8],
display_path: &str,
) -> u32 {
use crate::model::control::Control;
use crate::model::shape::ShapeObject;
use std::collections::BTreeSet;
let mut injected: u32 = 0;
let mut targets: BTreeSet<u16> = BTreeSet::new();
for section in &self.document().sections {
for para in §ion.paragraphs {
for ctrl in ¶.controls {
let pic = match ctrl {
Control::Picture(p) => p,
Control::Shape(s) => match s.as_ref() {
ShapeObject::Picture(p) => p,
_ => continue,
},
_ => continue,
};
if let Some(ref path) = pic.image_attr.external_path {
let path_basename = path
.rsplit(|c| c == '/' || c == '\\')
.next()
.unwrap_or(path);
if path_basename != basename {
continue;
}
let id = pic.image_attr.bin_data_id;
if self.document().external_image_loaded(id) {
continue;
}
targets.insert(id);
}
}
}
}
for id in targets {
injected +=
self.inject_external_image_by_bin_data_id(id, data, display_path, Some(basename));
}
if injected > 0 {
self.invalidate_page_tree_cache();
}
injected
}
#[wasm_bindgen(js_name = injectExternalImageByKey)]
pub fn inject_external_image_by_key(
&mut self,
key: &str,
data: &[u8],
display_path: &str,
) -> u32 {
let Some(bin_data_id) = parse_external_image_key(key) else {
return 0;
};
let injected =
self.inject_external_image_by_bin_data_id(bin_data_id, data, display_path, None);
if injected > 0 {
self.invalidate_page_tree_cache();
}
injected
}
#[wasm_bindgen(js_name = getPictureProperties)]
pub fn get_picture_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_picture_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getHeaderFooterPictureProperties)]
pub fn get_header_footer_picture_properties(
&self,
section_idx: u32,
outer_para_idx: u32,
outer_control_idx: u32,
inner_para_idx: u32,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.get_header_footer_picture_properties_native(
section_idx as usize,
outer_para_idx as usize,
outer_control_idx as usize,
inner_para_idx as usize,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPictureProperties)]
pub fn set_picture_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_picture_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setHeaderFooterPictureProperties)]
pub fn set_header_footer_picture_properties(
&mut self,
section_idx: u32,
outer_para_idx: u32,
outer_control_idx: u32,
inner_para_idx: u32,
inner_control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_header_footer_picture_properties_native(
section_idx as usize,
outer_para_idx as usize,
outer_control_idx as usize,
inner_para_idx as usize,
inner_control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deletePictureControl)]
pub fn delete_picture_control(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.delete_picture_control_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteCellPictureControlByPath)]
pub fn delete_cell_picture_control_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.delete_cell_picture_control_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteCellShapeControlByPath)]
pub fn delete_cell_shape_control_by_path(
&mut self,
section_idx: usize,
parent_para_idx: usize,
cell_path_json: &str,
inner_control_idx: usize,
) -> Result<String, JsValue> {
self.delete_cell_shape_control_by_path_native(
section_idx,
parent_para_idx,
cell_path_json,
inner_control_idx,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteCellTableControlByPath)]
pub fn delete_cell_table_control_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.delete_cell_table_control_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = listCharts)]
pub fn list_charts(&self) -> Result<String, JsValue> {
self.list_charts_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getChartData)]
pub fn get_chart_data(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_chart_data_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getChartDataByIndex)]
pub fn get_chart_data_by_index(&self, index: u32) -> Result<String, JsValue> {
self.get_chart_data_by_index_native(index as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setChartData)]
pub fn set_chart_data(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
edits_json: &str,
) -> Result<String, JsValue> {
self.set_chart_data_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
edits_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setChartDataByIndex)]
pub fn set_chart_data_by_index(
&mut self,
index: u32,
edits_json: &str,
) -> Result<String, JsValue> {
self.set_chart_data_by_index_native(index as usize, edits_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellShapePropertiesByPath)]
pub fn get_cell_shape_properties_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.get_cell_shape_properties_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellPicturePropertiesByPath)]
pub fn get_cell_picture_properties_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.get_cell_picture_properties_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellShapePropertiesByPath)]
pub fn set_cell_shape_properties_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_cell_shape_properties_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellPicturePropertiesByPath)]
pub fn set_cell_picture_properties_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
cell_path_json: &str,
inner_control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_cell_picture_properties_by_path_native(
section_idx as usize,
parent_para_idx as usize,
cell_path_json,
inner_control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteEquationControl)]
pub fn delete_equation_control(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.delete_equation_control_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getEquationProperties)]
pub fn get_equation_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: i32,
cell_para_idx: i32,
) -> Result<String, JsValue> {
let ci = if cell_idx >= 0 {
Some(cell_idx as usize)
} else {
None
};
let cpi = if cell_para_idx >= 0 {
Some(cell_para_idx as usize)
} else {
None
};
self.get_equation_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
ci,
cpi,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setEquationProperties)]
pub fn set_equation_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: i32,
cell_para_idx: i32,
props_json: &str,
) -> Result<String, JsValue> {
let ci = if cell_idx >= 0 {
Some(cell_idx as usize)
} else {
None
};
let cpi = if cell_para_idx >= 0 {
Some(cell_para_idx as usize)
} else {
None
};
self.set_equation_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
ci,
cpi,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getNoteEquationProperties)]
pub fn get_note_equation_properties(
&self,
kind: &str,
section_idx: u32,
parent_para_idx: u32,
note_control_idx: u32,
note_para_idx: u32,
inner_control_idx: u32,
) -> Result<String, JsValue> {
self.get_note_equation_properties_native(
kind,
section_idx as usize,
parent_para_idx as usize,
note_control_idx as usize,
note_para_idx as usize,
inner_control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setNoteEquationProperties)]
pub fn set_note_equation_properties(
&mut self,
kind: &str,
section_idx: u32,
parent_para_idx: u32,
note_control_idx: u32,
note_para_idx: u32,
inner_control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_note_equation_properties_native(
kind,
section_idx as usize,
parent_para_idx as usize,
note_control_idx as usize,
note_para_idx as usize,
inner_control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setNoteEquationPropertiesEx)]
pub fn set_note_equation_properties_ex(
&mut self,
options_json: &str,
) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_object, json_str, json_u32};
let props_json = json_object(options_json, "props").unwrap_or_else(|| "{}".to_string());
self.set_note_equation_properties_native(
&json_str(options_json, "kind").unwrap_or_default(),
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "noteControlIdx").unwrap_or(0) as usize,
json_u32(options_json, "noteParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "innerControlIdx").unwrap_or(0) as usize,
&props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = renderEquationPreview)]
pub fn render_equation_preview(
&self,
script: &str,
font_size_hwpunit: u32,
color: u32,
) -> Result<String, JsValue> {
self.render_equation_preview_native(script, font_size_hwpunit, color)
.map_err(|e| e.into())
}
fn parse_polygon_points(json: &str) -> Vec<crate::model::Point> {
let key = "\"polygonPoints\":[";
if let Some(start) = json.find(key) {
let rest = &json[start + key.len()..];
if let Some(end) = rest.find(']') {
let arr = &rest[..end];
return arr
.split("},")
.filter_map(|item| {
let item = item.trim().trim_start_matches('{').trim_end_matches('}');
let x =
crate::document_core::helpers::json_i32(&format!("{{{}}}", item), "x")?;
let y =
crate::document_core::helpers::json_i32(&format!("{{{}}}", item), "y")?;
Some(crate::model::Point { x, y })
})
.collect();
}
}
Vec::new()
}
#[wasm_bindgen(js_name = createShapeControl)]
pub fn create_shape_control(&mut self, json: &str) -> Result<String, JsValue> {
let sec = json_u32(json, "sectionIdx").unwrap_or(0) as usize;
let para = json_u32(json, "paraIdx").unwrap_or(0) as usize;
let offset = json_u32(json, "charOffset").unwrap_or(0) as usize;
let width = json_u32(json, "width").unwrap_or(8504);
let height = json_u32(json, "height").unwrap_or(8504);
let horz_offset = json_u32(json, "horzOffset").unwrap_or(0);
let vert_offset = json_u32(json, "vertOffset").unwrap_or(0);
let shape_type = json_str(json, "shapeType").unwrap_or_else(|| "rectangle".to_string());
let default_tac = shape_type == "textbox";
let treat_as_char = json_bool(json, "treatAsChar").unwrap_or(default_tac);
let text_wrap = json_str(json, "textWrap").unwrap_or_else(|| "Square".to_string());
let line_flip_x = json_bool(json, "lineFlipX").unwrap_or(false);
let line_flip_y = json_bool(json, "lineFlipY").unwrap_or(false);
let polygon_points: Vec<crate::model::Point> = if shape_type == "polygon" {
Self::parse_polygon_points(json)
} else {
Vec::new()
};
let result = self.create_shape_control_native(
sec,
para,
offset,
width,
height,
horz_offset,
vert_offset,
treat_as_char,
&text_wrap,
&shape_type,
line_flip_x,
line_flip_y,
&polygon_points,
)?;
if shape_type.starts_with("connector-") {
let ssid = json_u32(json, "startSubjectID").unwrap_or(0);
let ssidx = json_u32(json, "startSubjectIndex").unwrap_or(0);
let esid = json_u32(json, "endSubjectID").unwrap_or(0);
let esidx = json_u32(json, "endSubjectIndex").unwrap_or(0);
let pi = json_u32(&result, "paraIdx");
let ci = json_u32(&result, "controlIdx");
if let (Some(pi), Some(ci)) = (pi, ci) {
self.update_connector_subject_ids(
sec,
pi as usize,
ci as usize,
ssid,
ssidx,
esid,
esidx,
);
self.recalculate_connector_routing(sec, pi as usize, ci as usize, ssidx, esidx);
}
}
Ok(result)
}
#[wasm_bindgen(js_name = getShapeProperties)]
pub fn get_shape_properties(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_shape_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setShapeProperties)]
pub fn set_shape_properties(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.set_shape_properties_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteShapeControl)]
pub fn delete_shape_control(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.delete_shape_control_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = changeShapeZOrder)]
pub fn change_shape_z_order(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
operation: &str,
) -> Result<String, JsValue> {
self.change_shape_z_order_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
operation,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyShapeZOrderPairs)]
pub fn apply_shape_z_order_pairs(
&mut self,
section_idx: u32,
pairs_json: &str,
) -> Result<String, JsValue> {
self.apply_shape_z_order_pairs_native(section_idx as usize, pairs_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = groupShapes)]
pub fn group_shapes(&mut self, json: &str) -> Result<String, JsValue> {
let sec = json_u32(json, "sectionIdx").unwrap_or(0) as usize;
let targets: Vec<(usize, usize)> = {
let mut result = Vec::new();
if let Some(start) = json.find("\"targets\"") {
let rest = &json[start..];
if let Some(arr_start) = rest.find('[') {
if let Some(arr_end) = rest.find(']') {
let arr = &rest[arr_start + 1..arr_end];
let mut pos = 0;
while let Some(obj_start) = arr[pos..].find('{') {
let obj_start = pos + obj_start;
if let Some(obj_end) = arr[obj_start..].find('}') {
let obj = &arr[obj_start..obj_start + obj_end + 1];
let pi = json_u32(obj, "paraIdx").unwrap_or(0) as usize;
let ci = json_u32(obj, "controlIdx").unwrap_or(0) as usize;
result.push((pi, ci));
pos = obj_start + obj_end + 1;
} else {
break;
}
}
}
}
}
result
};
self.group_shapes_native(sec, &targets)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = ungroupShape)]
pub fn ungroup_shape(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.ungroup_shape_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveLineEndpoint)]
pub fn move_line_endpoint(
&mut self,
sec: u32,
para: u32,
ci: u32,
sx: i32,
sy: i32,
ex: i32,
ey: i32,
) -> Result<String, JsValue> {
self.move_line_endpoint_native(sec as usize, para as usize, ci as usize, sx, sy, ex, ey)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveLineEndpointEx)]
pub fn move_line_endpoint_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_i32, json_u32};
self.move_line_endpoint_native(
json_u32(options_json, "sec").unwrap_or(0) as usize,
json_u32(options_json, "para").unwrap_or(0) as usize,
json_u32(options_json, "ci").unwrap_or(0) as usize,
json_i32(options_json, "sx").unwrap_or(0),
json_i32(options_json, "sy").unwrap_or(0),
json_i32(options_json, "ex").unwrap_or(0),
json_i32(options_json, "ey").unwrap_or(0),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = updateConnectorsInSection)]
pub fn update_connectors_in_section_wasm(&mut self, section_idx: u32) {
self.update_connectors_in_section(section_idx as usize);
}
#[wasm_bindgen(js_name = insertFootnote)]
pub fn insert_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.insert_footnote_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertEndnote)]
pub fn insert_endnote(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.insert_endnote_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getEndnoteShape)]
pub fn get_endnote_shape(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_endnote_shape_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyEndnoteShape)]
pub fn apply_endnote_shape(
&mut self,
section_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_endnote_shape_native(section_idx as usize, props_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertEquation)]
pub fn insert_equation(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
script: &str,
font_size: u32,
color: u32,
) -> Result<String, JsValue> {
self.insert_equation_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
script,
font_size,
color,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = promoteOleEquation)]
pub fn promote_ole_equation(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.promote_ole_equation_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFootnoteInfo)]
pub fn get_footnote_info(
&self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_footnote_info_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFootnoteAtCursor)]
pub fn get_footnote_at_cursor(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
direction: &str,
) -> Result<String, JsValue> {
self.get_footnote_at_cursor_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
direction,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteFootnote)]
pub fn delete_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.delete_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextInFootnote)]
pub fn insert_text_in_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
char_offset: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
char_offset as usize,
text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteTextInFootnote)]
pub fn delete_text_in_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
char_offset: u32,
count: u32,
) -> Result<String, JsValue> {
self.delete_text_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
char_offset as usize,
count as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = splitParagraphInFootnote)]
pub fn split_paragraph_in_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
char_offset: u32,
removed_para_meta: Option<String>,
) -> Result<String, JsValue> {
self.split_paragraph_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
char_offset as usize,
parse_removed_para_meta(removed_para_meta)?,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = mergeParagraphInFootnote)]
pub fn merge_paragraph_in_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
) -> Result<String, JsValue> {
self.merge_paragraph_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pageHasFootnoteFootholds)]
pub fn page_has_footnote_footholds(&self, page_num: u32) -> bool {
self.page_has_footnote_footholds_native(page_num)
}
#[wasm_bindgen(js_name = hitTestFootnote)]
pub fn hit_test_footnote(&self, page_num: u32, x: f64, y: f64) -> Result<String, JsValue> {
self.hit_test_footnote_native(page_num, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTestInFootnote)]
pub fn hit_test_in_footnote(&self, page_num: u32, x: f64, y: f64) -> Result<String, JsValue> {
self.hit_test_in_footnote_native(page_num, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageFootnoteInfo)]
pub fn get_page_footnote_info(
&self,
page_num: u32,
footnote_index: u32,
) -> Result<String, JsValue> {
self.get_page_footnote_info_native(page_num, footnote_index as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectInFootnote)]
pub fn get_cursor_rect_in_footnote(
&self,
page_num: u32,
footnote_index: u32,
fn_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_in_footnote_native(
page_num,
footnote_index as usize,
fn_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getNoteEditInfo)]
pub fn get_note_edit_info(
&self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
) -> Result<String, JsValue> {
self.get_note_edit_info_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectInNote)]
pub fn get_cursor_rect_in_note(
&self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
note_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_in_note_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
note_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getParaPropertiesInFootnote)]
pub fn get_para_properties_in_footnote(
&self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
) -> Result<String, JsValue> {
self.get_para_properties_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormatInFootnote)]
pub fn apply_para_format_in_footnote(
&mut self,
section_idx: u32,
para_idx: u32,
control_idx: u32,
fn_para_idx: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_para_format_in_footnote_native(
section_idx as usize,
para_idx as usize,
control_idx as usize,
fn_para_idx as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hitTestBodyFootnoteMarker)]
pub fn hit_test_body_footnote_marker(
&self,
page_num: u32,
x: f64,
y: f64,
) -> Result<String, JsValue> {
self.hit_test_body_footnote_marker_native(page_num, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveVertical)]
pub fn move_vertical(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
delta: i32,
preferred_x: f64,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
) -> Result<String, JsValue> {
let cell_ctx = if parent_para_idx == u32::MAX {
None
} else {
Some((
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
))
};
self.move_vertical_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
delta,
preferred_x,
cell_ctx,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveVerticalEx)]
pub fn move_vertical_ex(&self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_f64, json_i32, json_u32};
let parent_para_idx = json_u32(options_json, "parentParaIdx").unwrap_or(u32::MAX);
let cell_ctx = if parent_para_idx == u32::MAX {
None
} else {
Some((
parent_para_idx as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
))
};
self.move_vertical_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "paraIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_i32(options_json, "delta").unwrap_or(0),
json_f64(options_json, "preferredX").unwrap_or(0.0),
cell_ctx,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFieldList)]
pub fn get_field_list(&self) -> String {
self.get_field_list_json()
}
#[wasm_bindgen(js_name = getFieldValue)]
pub fn get_field_value(&self, field_id: u32) -> Result<String, JsValue> {
self.get_field_value_by_id(field_id).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFieldValueByName)]
pub fn get_field_value_by_name_api(&self, name: &str) -> Result<String, JsValue> {
self.get_field_value_by_name(name).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setFieldValue)]
pub fn set_field_value(&mut self, field_id: u32, value: &str) -> Result<String, JsValue> {
self.set_field_value_by_id(field_id, value)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setFieldValueByName)]
pub fn set_field_value_by_name_api(
&mut self,
name: &str,
value: &str,
) -> Result<String, JsValue> {
self.set_field_value_by_name(name, value)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereField)]
pub fn insert_click_here_field_api(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
guide: &str,
memo: &str,
name: &str,
editable: bool,
) -> Result<String, JsValue> {
self.insert_click_here_field_at(
section_idx as usize,
para_idx as usize,
char_offset as usize,
guide,
memo,
name,
editable,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereFieldEx)]
pub fn insert_click_here_field_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_str, json_u32};
self.insert_click_here_field_at(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "paraIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
&json_str(options_json, "guide").unwrap_or_default(),
&json_str(options_json, "memo").unwrap_or_default(),
&json_str(options_json, "name").unwrap_or_default(),
json_bool(options_json, "editable").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereFieldInCell)]
pub fn insert_click_here_field_in_cell_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
is_textbox: bool,
guide: &str,
memo: &str,
name: &str,
editable: bool,
) -> Result<String, JsValue> {
self.insert_click_here_field_at_in_cell(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
is_textbox,
guide,
memo,
name,
editable,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereFieldInCellEx)]
pub fn insert_click_here_field_in_cell_ex(
&mut self,
options_json: &str,
) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_str, json_u32};
self.insert_click_here_field_at_in_cell(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_bool(options_json, "isTextbox").unwrap_or(false),
&json_str(options_json, "guide").unwrap_or_default(),
&json_str(options_json, "memo").unwrap_or_default(),
&json_str(options_json, "name").unwrap_or_default(),
json_bool(options_json, "editable").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereFieldByPath)]
pub fn insert_click_here_field_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
guide: &str,
memo: &str,
name: &str,
editable: bool,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.insert_click_here_field_at_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
guide,
memo,
name,
editable,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertClickHereFieldByPathEx)]
pub fn insert_click_here_field_by_path_ex(
&mut self,
options_json: &str,
) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_str, json_u32};
let path_json = json_str(options_json, "path").unwrap_or_default();
let path = DocumentCore::parse_cell_path(&path_json)?;
self.insert_click_here_field_at_by_path(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
&path,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
&json_str(options_json, "guide").unwrap_or_default(),
&json_str(options_json, "memo").unwrap_or_default(),
&json_str(options_json, "name").unwrap_or_default(),
json_bool(options_json, "editable").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getMemoList)]
pub fn get_memo_list(&self) -> String {
self.get_memo_list_json()
}
#[wasm_bindgen(js_name = insertMemo)]
pub fn insert_memo(
&mut self,
section_idx: u32,
para_idx: u32,
start_char_idx: u32,
end_char_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_memo_json_native(
section_idx as usize,
para_idx as usize,
start_char_idx as usize,
end_char_idx as usize,
json,
)
.map_err(|e| JsValue::from_str(&e.to_json()))
}
#[wasm_bindgen(js_name = insertMemoByPath)]
pub fn insert_memo_by_path(
&mut self,
section_idx: u32,
para_idx: u32,
path_json: &str,
start_char_idx: u32,
end_char_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_memo_by_path_json_native(
section_idx as usize,
para_idx as usize,
path_json,
start_char_idx as usize,
end_char_idx as usize,
json,
)
.map_err(|e| JsValue::from_str(&e.to_json()))
}
#[wasm_bindgen(js_name = setMemoText)]
pub fn set_memo_text(&mut self, id: u32, text: &str) -> Result<String, JsValue> {
self.core
.set_memo_text(id, text)
.map_err(|e| JsValue::from_str(&e.to_json()))
}
#[wasm_bindgen(js_name = removeMemo)]
pub fn remove_memo(&mut self, id: u32) -> Result<String, JsValue> {
self.core
.remove_memo(id)
.map_err(|e| JsValue::from_str(&e.to_json()))
}
#[wasm_bindgen(js_name = getFormObjectAt)]
pub fn get_form_object_at(&self, page_num: u32, x: f64, y: f64) -> Result<String, JsValue> {
self.core
.get_form_object_at_native(page_num, x, y)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFormValue)]
pub fn get_form_value(&self, sec: u32, para: u32, ci: u32) -> Result<String, JsValue> {
self.core
.get_form_value_native(sec as usize, para as usize, ci as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setFormValue)]
pub fn set_form_value(
&mut self,
sec: u32,
para: u32,
ci: u32,
value_json: &str,
) -> Result<String, JsValue> {
self.core
.set_form_value_native(sec as usize, para as usize, ci as usize, value_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setFormValueInCell)]
pub fn set_form_value_in_cell(
&mut self,
sec: u32,
table_para: u32,
table_ci: u32,
cell_idx: u32,
cell_para: u32,
form_ci: u32,
value_json: &str,
) -> Result<String, JsValue> {
self.core
.set_form_value_in_cell_native(
sec as usize,
table_para as usize,
table_ci as usize,
cell_idx as usize,
cell_para as usize,
form_ci as usize,
value_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setFormValueInCellEx)]
pub fn set_form_value_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_object, json_u32};
let value_json = json_object(options_json, "value").unwrap_or_else(|| "{}".to_string());
self.core
.set_form_value_in_cell_native(
json_u32(options_json, "sec").unwrap_or(0) as usize,
json_u32(options_json, "tablePara").unwrap_or(0) as usize,
json_u32(options_json, "tableCi").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellPara").unwrap_or(0) as usize,
json_u32(options_json, "formCi").unwrap_or(0) as usize,
&value_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFormObjectInfo)]
pub fn get_form_object_info(&self, sec: u32, para: u32, ci: u32) -> Result<String, JsValue> {
self.core
.get_form_object_info_native(sec as usize, para as usize, ci as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = searchText)]
pub fn search_text(
&self,
query: &str,
from_sec: u32,
from_para: u32,
from_char: u32,
forward: bool,
case_sensitive: bool,
include_cells: Option<bool>,
) -> Result<String, JsValue> {
self.core
.search_text_native(
query,
from_sec as usize,
from_para as usize,
from_char as usize,
forward,
case_sensitive,
include_cells.unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = searchAllText)]
pub fn search_all_text(
&self,
query: &str,
case_sensitive: bool,
include_cells: bool,
) -> Result<String, JsValue> {
self.core
.search_all_text_native(query, case_sensitive, include_cells)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceText)]
pub fn replace_text(
&mut self,
sec: u32,
para: u32,
char_offset: u32,
length: u32,
new_text: &str,
) -> Result<String, JsValue> {
self.core
.replace_text_native(
sec as usize,
para as usize,
char_offset as usize,
length as usize,
new_text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceOne)]
pub fn replace_one(
&mut self,
query: &str,
new_text: &str,
case_sensitive: bool,
) -> Result<String, JsValue> {
self.core
.replace_one_native(query, new_text, case_sensitive)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = replaceAll)]
pub fn replace_all(
&mut self,
query: &str,
new_text: &str,
case_sensitive: bool,
) -> Result<String, JsValue> {
self.core
.replace_all_native(query, new_text, case_sensitive)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPositionOfPage)]
pub fn get_position_of_page(&self, global_page: u32) -> Result<String, JsValue> {
self.core
.get_position_of_page_native(global_page as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageOfPosition)]
pub fn get_page_of_position(&self, section_idx: u32, para_idx: u32) -> Result<String, JsValue> {
self.core
.get_page_of_position_native(section_idx as usize, para_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getFieldInfoAt)]
pub fn get_field_info_at_api(
&self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> String {
self.get_field_info_at(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
}
#[wasm_bindgen(js_name = getFieldInfoAtInCell)]
pub fn get_field_info_at_in_cell_api(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
is_textbox: bool,
) -> String {
self.get_field_info_at_in_cell(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
is_textbox,
)
}
#[wasm_bindgen(js_name = getFieldInfoAtInCellEx)]
pub fn get_field_info_at_in_cell_ex(&self, options_json: &str) -> String {
use crate::document_core::helpers::{json_bool, json_u32};
self.get_field_info_at_in_cell(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_bool(options_json, "isTextbox").unwrap_or(false),
)
}
#[wasm_bindgen(js_name = removeFieldAt)]
pub fn remove_field_at_api(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> String {
match self.remove_field_at(
section_idx as usize,
para_idx as usize,
char_offset as usize,
) {
Ok(s) => s,
Err(e) => {
let escaped = e.to_string().replace('\\', "\\\\").replace('"', "\\\"");
format!("{{\"ok\":false,\"error\":\"{}\"}}", escaped)
}
}
}
#[wasm_bindgen(js_name = removeFieldAtInCell)]
pub fn remove_field_at_in_cell_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
is_textbox: bool,
) -> String {
match self.remove_field_at_in_cell(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
is_textbox,
) {
Ok(s) => s,
Err(e) => {
let escaped = e.to_string().replace('\\', "\\\\").replace('"', "\\\"");
format!("{{\"ok\":false,\"error\":\"{}\"}}", escaped)
}
}
}
#[wasm_bindgen(js_name = removeFieldAtInCellEx)]
pub fn remove_field_at_in_cell_ex(&mut self, options_json: &str) -> String {
use crate::document_core::helpers::{json_bool, json_u32};
match self.remove_field_at_in_cell(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_bool(options_json, "isTextbox").unwrap_or(false),
) {
Ok(s) => s,
Err(e) => {
let escaped = e.to_string().replace('\\', "\\\\").replace('"', "\\\"");
format!("{{\"ok\":false,\"error\":\"{}\"}}", escaped)
}
}
}
#[wasm_bindgen(js_name = setActiveField)]
pub fn set_active_field_api(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> bool {
self.set_active_field(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
}
#[wasm_bindgen(js_name = setActiveFieldInCell)]
pub fn set_active_field_in_cell_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
is_textbox: bool,
) -> bool {
self.set_active_field_in_cell(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
is_textbox,
)
}
#[wasm_bindgen(js_name = setActiveFieldInCellEx)]
pub fn set_active_field_in_cell_ex(&mut self, options_json: &str) -> bool {
use crate::document_core::helpers::{json_bool, json_u32};
self.set_active_field_in_cell(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
json_bool(options_json, "isTextbox").unwrap_or(false),
)
}
#[wasm_bindgen(js_name = getFieldInfoAtByPath)]
pub fn get_field_info_at_by_path_api(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
) -> String {
match DocumentCore::parse_cell_path(path_json) {
Ok(path) => self.get_field_info_at_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
),
Err(_) => r#"{"inField":false}"#.to_string(),
}
}
#[wasm_bindgen(js_name = setActiveFieldByPath)]
pub fn set_active_field_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
) -> bool {
match DocumentCore::parse_cell_path(path_json) {
Ok(path) => self.set_active_field_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
),
Err(_) => false,
}
}
#[wasm_bindgen(js_name = clearActiveField)]
pub fn clear_active_field_api(&mut self) {
self.clear_active_field();
}
#[wasm_bindgen(js_name = getClickHereProps)]
pub fn get_click_here_props(&self, field_id: u32) -> String {
use crate::model::control::{Control, FieldType};
for sec in &self.document.sections {
for para in &sec.paragraphs {
for ctrl in ¶.controls {
if let Control::Field(f) = ctrl {
if f.field_id == field_id && f.field_type == FieldType::ClickHere {
return self.format_click_here_props(f);
}
}
}
for ctrl in ¶.controls {
let paras: Vec<&crate::model::paragraph::Paragraph> = match ctrl {
Control::Table(t) => t.cells.iter().flat_map(|c| &c.paragraphs).collect(),
Control::Shape(s) => s
.drawing()
.and_then(|d| d.text_box.as_ref())
.map(|tb| tb.paragraphs.iter().collect())
.unwrap_or_default(),
_ => Vec::new(),
};
for p in paras {
for c in &p.controls {
if let Control::Field(f) = c {
if f.field_id == field_id && f.field_type == FieldType::ClickHere {
return self.format_click_here_props(f);
}
}
}
}
}
}
}
r#"{"ok":false}"#.to_string()
}
fn format_click_here_props(&self, f: &crate::model::control::Field) -> String {
let guide = f.guide_text().unwrap_or("");
let memo = f.memo_text().unwrap_or("");
let name = f
.ctrl_data_name
.as_deref()
.filter(|s| !s.is_empty())
.or_else(|| f.extract_wstring_value("Name:"))
.unwrap_or("");
let editable = f.is_editable_in_form();
format!(
"{{\"ok\":true,\"guide\":\"{}\",\"memo\":\"{}\",\"name\":\"{}\",\"editable\":{}}}",
json_escape(guide),
json_escape(memo),
json_escape(name),
editable,
)
}
#[wasm_bindgen(js_name = updateClickHereProps)]
pub fn update_click_here_props(
&mut self,
field_id: u32,
guide: &str,
memo: &str,
name: &str,
editable: bool,
) -> String {
use crate::model::control::{Control, Field, FieldType};
let new_props_bit = if editable { 1u32 } else { 0u32 };
fn update_field_in_para(
para: &mut crate::model::paragraph::Paragraph,
field_id: u32,
guide: &str,
memo: &str,
new_props_bit: u32,
new_name: &str,
) -> bool {
for (ci, ctrl) in para.controls.iter_mut().enumerate() {
if let Control::Field(f) = ctrl {
if f.field_id == field_id && f.field_type == FieldType::ClickHere {
let orig_guide = f.guide_text().unwrap_or("").to_string();
let orig_memo = f.memo_text().unwrap_or("").to_string();
if guide != orig_guide || memo != orig_memo {
let new_command = Field::build_clickhere_command(guide, memo);
f.command = new_command;
}
f.properties = (f.properties & !1) | new_props_bit;
f.ctrl_data_name = if new_name.is_empty() {
None
} else {
Some(new_name.to_string())
};
crate::document_core::queries::field_query::write_ctrl_data_name(
&mut para.ctrl_data_records,
ci,
new_name,
);
return true;
}
}
}
false
}
for sec in &mut self.document.sections {
sec.raw_stream = None;
for para in &mut sec.paragraphs {
if update_field_in_para(para, field_id, guide, memo, new_props_bit, name) {
self.invalidate_page_tree_cache();
return r#"{"ok":true}"#.to_string();
}
for ctrl in &mut para.controls {
let found = match ctrl {
Control::Table(t) => t.cells.iter_mut().any(|c| {
c.paragraphs.iter_mut().any(|p| {
update_field_in_para(p, field_id, guide, memo, new_props_bit, name)
})
}),
Control::Shape(s) => {
if let Some(tb) = s.drawing_mut().and_then(|d| d.text_box.as_mut()) {
tb.paragraphs.iter_mut().any(|p| {
update_field_in_para(
p,
field_id,
guide,
memo,
new_props_bit,
name,
)
})
} else {
false
}
}
_ => false,
};
if found {
self.invalidate_page_tree_cache();
return r#"{"ok":true}"#.to_string();
}
}
}
}
r#"{"ok":false}"#.to_string()
}
#[wasm_bindgen(js_name = applyCharFormatAtCursor)]
pub fn apply_char_format_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
start_pos: u32,
end_pos: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_char_format_at_cursor(
list_id,
para_in_list as usize,
start_pos as usize,
end_pos as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteAtCursor)]
pub fn delete_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
start_pos: u32,
end_pos: u32,
) -> Result<String, JsValue> {
self.delete_at_cursor(
list_id,
para_in_list as usize,
start_pos as usize,
end_pos as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = tableEditAtCursor)]
pub fn table_edit_at_cursor_api(&mut self, list_id: u32, op: &str) -> Result<String, JsValue> {
self.table_edit_at_cursor(list_id, op).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getControls)]
pub fn get_controls(&self) -> String {
self.controls_json()
}
#[wasm_bindgen(js_name = deleteControlAt)]
pub fn delete_control_at_api(
&mut self,
list_id: u32,
para_in_list: u32,
control_index: u32,
) -> Result<String, JsValue> {
self.delete_control_at(list_id, para_in_list as usize, control_index as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertAutoNumberAtCursor)]
pub fn insert_auto_number_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
pos: u32,
kind: &str,
) -> Result<String, JsValue> {
self.insert_auto_number_at_cursor(list_id, para_in_list as usize, pos as usize, kind)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTextFileText)]
pub fn get_text_file_text(&self) -> String {
self.text_file_json()
}
#[wasm_bindgen(js_name = getTextFileUnicode)]
pub fn get_text_file_unicode(&self) -> String {
self.text_file_unicode_json()
}
#[wasm_bindgen(js_name = getScanItems)]
pub fn get_scan_items(&self) -> String {
self.scan_items_json()
}
#[wasm_bindgen(js_name = getSectionStarts)]
pub fn get_section_starts(&self) -> String {
self.section_starts_json()
}
#[wasm_bindgen(js_name = breakAtCursor)]
pub fn break_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
pos: u32,
kind: &str,
) -> Result<String, JsValue> {
self.break_at_cursor(list_id, para_in_list as usize, pos as usize, kind)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveInlineControl)]
pub fn move_inline_control_api(
&mut self,
source_json: &str,
destination_json: &str,
) -> Result<String, JsValue> {
let source =
serde_json::from_str(source_json).map_err(|e| JsValue::from_str(&e.to_string()))?;
let destination = serde_json::from_str(destination_json)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
let result = self
.move_inline_control_native(&source, &destination)
.map_err(JsValue::from)?;
serde_json::to_string(&result).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen(js_name = moveControlAt)]
pub fn move_control_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
dx: i32,
dy: i32,
) -> Result<String, JsValue> {
self.move_control_at(para_in_list as usize, control_index as usize, dx, dy)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setControlZOrderAt)]
pub fn set_control_z_order_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
mode: &str,
) -> Result<String, JsValue> {
self.set_control_z_order_at(para_in_list as usize, control_index as usize, mode)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setControlFlipAt)]
pub fn set_control_flip_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
vertical: bool,
org_state: bool,
) -> Result<String, JsValue> {
self.set_control_flip_at(
para_in_list as usize,
control_index as usize,
vertical,
org_state,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageText)]
pub fn page_text_api(&self, page_index: u32) -> Result<String, JsValue> {
self.page_text(page_index as usize).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getObjectCycle)]
pub fn object_cycle_api(&self) -> Result<String, JsValue> {
self.object_cycle_json().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCharIndexAtStreamPos)]
pub fn char_index_at_api(
&self,
list_id: u32,
para_in_list: u32,
pos: u32,
) -> Result<String, JsValue> {
self.char_index_at(list_id, para_in_list as usize, pos as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageCaretStarts)]
pub fn page_caret_starts_api(&self) -> Result<String, JsValue> {
self.page_caret_starts().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setTextBoxAt)]
pub fn set_text_box_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
attach: bool,
) -> Result<String, JsValue> {
self.set_text_box_at(para_in_list as usize, control_index as usize, attach)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = attachCaptionAt)]
pub fn attach_caption_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
) -> Result<String, JsValue> {
self.attach_caption_at(para_in_list as usize, control_index as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = detachCaptionAt)]
pub fn detach_caption_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
) -> Result<String, JsValue> {
self.detach_caption_at(para_in_list as usize, control_index as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = resizeControlAt)]
pub fn resize_control_at_api(
&mut self,
para_in_list: u32,
control_index: u32,
d_width: i32,
d_height: i32,
) -> Result<String, JsValue> {
self.resize_control_at(
para_in_list as usize,
control_index as usize,
d_width,
d_height,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setControlLock)]
pub fn set_control_lock_api(
&mut self,
para_in_list: u32,
control_index: u32,
locked: bool,
) -> Result<String, JsValue> {
let some = |v: u32| (v != u32::MAX).then_some(v as usize);
self.set_control_lock(some(para_in_list), some(control_index), locked)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCurFieldState)]
pub fn get_cur_field_state(&self, list_id: u32, para_in_list: u32, pos: u32) -> u32 {
self.cur_field_state(list_id, para_in_list as usize, pos as usize)
}
#[wasm_bindgen(js_name = getCellShapeSet)]
pub fn get_cell_shape_set(&self, list_id: u32) -> String {
self.cell_shape_set_json(list_id)
}
#[wasm_bindgen(js_name = getObjects)]
pub fn get_objects(&self) -> String {
self.objects_json()
}
#[wasm_bindgen(js_name = splitParaAtCursor)]
pub fn split_para_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
pos: u32,
) -> Result<String, JsValue> {
self.split_para_at_cursor(list_id, para_in_list as usize, pos as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertTextAtCursor)]
pub fn insert_text_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
pos: u32,
text: &str,
) -> Result<String, JsValue> {
self.insert_text_at_cursor(list_id, para_in_list as usize, pos as usize, text)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = tableMergeAtCursor)]
pub fn table_merge_at_cursor_api(
&mut self,
list_id: u32,
end_row: u32,
end_col: u32,
) -> Result<String, JsValue> {
self.table_merge_at_cursor(list_id, end_row as u16, end_col as u16)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = clearTableCellsAtCursor)]
pub fn clear_table_cells_at_cursor_api(
&mut self,
list_id: u32,
end_row: u32,
end_col: u32,
) -> Result<String, JsValue> {
self.clear_table_cells_at_cursor(list_id, end_row as u16, end_col as u16)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormatAtCursor)]
pub fn apply_para_format_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_para_format_at_cursor(list_id, para_in_list as usize, props_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getWordEnd)]
pub fn get_word_end(&self, list_id: u32, para_in_list: u32, pos: u32) -> String {
self.word_end_json(list_id, para_in_list as usize, pos as usize)
}
#[wasm_bindgen(js_name = getWordStarts)]
pub fn get_word_starts(&self, list_id: u32, para_in_list: u32) -> String {
self.word_starts_json(list_id, para_in_list as usize)
}
#[wasm_bindgen(js_name = getLineStarts)]
pub fn get_line_starts(&self, list_id: u32, para_in_list: u32) -> String {
self.line_starts_json(list_id, para_in_list as usize)
}
#[wasm_bindgen(js_name = getCaretStops)]
pub fn get_caret_stops(&self, list_id: u32, para_in_list: u32) -> String {
self.caret_stops_json(list_id, para_in_list as usize)
}
#[wasm_bindgen(js_name = getParaBounds)]
pub fn get_para_bounds(&self, list_id: u32, para_in_list: u32) -> String {
self.para_bounds_json(list_id, para_in_list as usize)
}
#[wasm_bindgen(js_name = getCharShapeSet)]
pub fn get_char_shape_set(&self, list_id: u32, para_in_list: u32, pos: u32) -> String {
self.char_shape_set_json(list_id, para_in_list as usize, pos as usize)
}
#[wasm_bindgen(js_name = getParaShapeSet)]
pub fn get_para_shape_set(&self, list_id: u32, para_in_list: u32) -> String {
self.para_shape_set_json(list_id, para_in_list as usize)
}
#[wasm_bindgen(js_name = isEmptyDocument)]
pub fn is_empty_document_api(&self) -> bool {
self.is_empty_document()
}
#[wasm_bindgen(js_name = insertClickHereFieldAtCursor)]
pub fn insert_click_here_field_at_cursor_api(
&mut self,
list_id: u32,
para_in_list: u32,
pos: u32,
guide: &str,
memo: &str,
name: &str,
editable: bool,
) -> Result<String, JsValue> {
self.insert_click_here_field_at_cursor(
list_id,
para_in_list as usize,
pos as usize,
guide,
memo,
name,
editable,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorModel)]
pub fn get_cursor_model(&self) -> String {
self.get_cursor_model_json()
}
#[wasm_bindgen(js_name = getStoredCaret)]
pub fn get_stored_caret(&self) -> String {
let props = &self.document.doc_properties;
format!(
"{{\"list\":{},\"para\":{},\"pos\":{}}}",
props.caret_list_id, props.caret_para_id, props.caret_char_pos,
)
}
#[wasm_bindgen(js_name = renameField)]
pub fn rename_field_api(&mut self, oldname: &str, newname: &str) -> String {
match self.rename_field_by_name(oldname, newname) {
Ok(json) => json,
Err(_) => r#"{"ok":false,"renamed":0}"#.to_string(),
}
}
#[wasm_bindgen(js_name = getCursorRectByPath)]
pub fn get_cursor_rect_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_by_path_native(
section_idx as usize,
parent_para_idx as usize,
path_json,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCursorRectByPathNear)]
pub fn get_cursor_rect_by_path_near(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
hint_page: u32,
) -> Result<String, JsValue> {
self.get_cursor_rect_by_path_with_hint(
section_idx as usize,
parent_para_idx as usize,
path_json,
char_offset as usize,
Some(hint_page),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellInfoByPath)]
pub fn get_cell_info_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<String, JsValue> {
self.get_cell_info_by_path_native(section_idx as usize, parent_para_idx as usize, path_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableDimensionsByPath)]
pub fn get_table_dimensions_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<String, JsValue> {
self.get_table_dimensions_by_path_native(
section_idx as usize,
parent_para_idx as usize,
path_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getTableCellBboxesByPath)]
pub fn get_table_cell_bboxes_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<String, JsValue> {
self.get_table_cell_bboxes_by_path_native(
section_idx as usize,
parent_para_idx as usize,
path_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = moveVerticalByPath)]
pub fn move_vertical_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
delta: i32,
preferred_x: f64,
) -> Result<String, JsValue> {
self.move_vertical_by_path_native(
section_idx as usize,
parent_para_idx as usize,
path_json,
char_offset as usize,
delta,
preferred_x,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRects)]
pub fn get_selection_rects(
&self,
section_idx: u32,
start_para_idx: u32,
start_char_offset: u32,
end_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.get_selection_rects_native(
section_idx as usize,
start_para_idx as usize,
start_char_offset as usize,
end_para_idx as usize,
end_char_offset as usize,
None,
None,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInCell)]
pub fn get_selection_rects_in_cell(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.get_selection_rects_native(
section_idx as usize,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
Some((
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)),
None,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInCellEx)]
pub fn get_selection_rects_in_cell_ex(&self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.get_selection_rects_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "endCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "endCharOffset").unwrap_or(0) as usize,
Some((
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
)),
json_u32(options_json, "startPageHint").zip(json_u32(options_json, "endPageHint")),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInCellByPath)]
pub fn get_selection_rects_in_cell_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.get_selection_rects_in_cell_by_path_native(
section_idx as usize,
parent_para_idx as usize,
path_json,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
None,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInCellByPathEx)]
pub fn get_selection_rects_in_cell_by_path_ex(
&self,
options_json: &str,
) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_str, json_u32};
let path_json = json_str(options_json, "path").unwrap_or_default();
self.get_selection_rects_in_cell_by_path_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
&path_json,
json_u32(options_json, "startCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "endCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "endCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "startPageHint").zip(json_u32(options_json, "endPageHint")),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getSelectionRectsInFootnote)]
pub fn get_selection_rects_in_footnote(
&self,
page_num: u32,
footnote_index: u32,
start_fn_para_idx: u32,
start_char_offset: u32,
end_fn_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.get_selection_rects_in_footnote_native(
page_num,
footnote_index as usize,
start_fn_para_idx as usize,
start_char_offset as usize,
end_fn_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteRangeLogical)]
pub fn delete_range_logical(
&mut self,
section_idx: u32,
start_para_idx: u32,
start_logical: u32,
end_para_idx: u32,
end_logical: u32,
) -> Result<String, JsValue> {
self.delete_range_logical_native(
section_idx as usize,
start_para_idx as usize,
start_logical as usize,
end_para_idx as usize,
end_logical as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteRange)]
pub fn delete_range(
&mut self,
section_idx: u32,
start_para_idx: u32,
start_char_offset: u32,
end_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.delete_range_native(
section_idx as usize,
start_para_idx as usize,
start_char_offset as usize,
end_para_idx as usize,
end_char_offset as usize,
None,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteRangeInCell)]
pub fn delete_range_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.delete_range_native(
section_idx as usize,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
Some((
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteRangeInCellEx)]
pub fn delete_range_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.delete_range_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "endCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "endCharOffset").unwrap_or(0) as usize,
Some((
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
)),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportHwp)]
pub fn export_hwp(&self) -> Result<Vec<u8>, JsValue> {
self.export_hwp_with_adapter_snapshot()
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportHwpWithReport)]
pub fn export_hwp_with_report(&self) -> Result<DocumentExport, JsValue> {
self.export_hwp_with_adapter_snapshot_with_report()
.map(DocumentExport::from)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = exportHwpWithPassword)]
pub fn export_hwp_with_password_wasm(&self, password: &str) -> Result<Vec<u8>, JsValue> {
self.export_hwp_with_adapter_with_password(password.as_bytes())
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportHwpWithPasswordAndReport)]
pub fn export_hwp_with_password_and_report_wasm(
&self,
password: &str,
) -> Result<DocumentExport, JsValue> {
self.export_hwp_with_adapter_snapshot_with_password_and_report(password.as_bytes())
.map(DocumentExport::from)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = exportHwpx)]
pub fn export_hwpx(&self) -> Result<Vec<u8>, JsValue> {
self.export_hwpx_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportHwpxWithReport)]
pub fn export_hwpx_with_report(&self) -> Result<DocumentExport, JsValue> {
self.export_hwpx_native_with_report()
.map(DocumentExport::from)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = exportHwpxWithPassword)]
pub fn export_hwpx_with_password_wasm(&self, password: &str) -> Result<Vec<u8>, JsValue> {
self.export_hwpx_native_with_password(password.as_bytes())
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportHwpxWithPasswordAndReport)]
pub fn export_hwpx_with_password_and_report_wasm(
&self,
password: &str,
) -> Result<DocumentExport, JsValue> {
self.export_hwpx_native_with_password_and_report(password.as_bytes())
.map(DocumentExport::from)
.map_err(JsValue::from)
}
#[wasm_bindgen(js_name = exportHml)]
pub fn export_hml(&self) -> Result<Vec<u8>, JsValue> {
self.export_hml_native()
.map_err(|error| JsValue::from_str(&format_hml_export_error(&error)))
}
#[wasm_bindgen(js_name = exportHwpVerify)]
pub fn export_hwp_verify(&self) -> Result<String, JsValue> {
let v = self.serialize_hwp_with_verify().map_err(JsValue::from)?;
Ok(format!(
"{{\"bytesLen\":{},\"pageCountBefore\":{},\"pageCountAfter\":{},\"recovered\":{}}}",
v.bytes_len, v.page_count_before, v.page_count_after, v.recovered
))
}
#[wasm_bindgen(js_name = getSourceFormat)]
pub fn get_source_format(&self) -> String {
source_format_name(self.core.source_format).to_string()
}
#[wasm_bindgen(js_name = getHmlOpenMetadata)]
pub fn get_hml_open_metadata(&self) -> String {
let Some(metadata) = self.core.hml_metadata() else {
return "null".to_string();
};
let encoding = match metadata.encoding {
crate::parser::hml::HmlEncoding::Utf8 => "utf-8",
crate::parser::hml::HmlEncoding::Utf16Le => "utf-16le",
crate::parser::hml::HmlEncoding::Utf16Be => "utf-16be",
};
let warnings = metadata
.warnings
.iter()
.map(hml_warning_json)
.collect::<Vec<_>>();
let save_state = hml_save_state(&self.core);
serde_json::json!({
"format": "hml",
"hwpmlVersion": metadata.hwpml_version,
"encoding": encoding,
"resourceCount": metadata.resource_count,
"warnings": warnings,
"hmlSavable": save_state.hml_savable,
"saveBlockers": save_state.blockers,
})
.to_string()
}
#[wasm_bindgen(js_name = getHmlSaveState)]
pub fn get_hml_save_state(&self) -> String {
serde_json::to_string(&hml_save_state(&self.core))
.expect("HML save-state DTO serialization cannot fail")
}
#[wasm_bindgen(js_name = getValidationWarnings)]
pub fn get_validation_warnings(&self) -> String {
let report = self.core.validation_report();
let mut summary_parts: Vec<String> = Vec::new();
let mut entries: Vec<(String, usize)> = report.summary().into_iter().collect();
entries.sort_by(|a, b| a.0.cmp(&b.0));
for (k, v) in &entries {
let escaped = k.replace('\\', "\\\\").replace('"', "\\\"");
summary_parts.push(format!("\"{}\":{}", escaped, v));
}
let mut warning_parts: Vec<String> = Vec::new();
for w in &report.warnings {
let cell_part = match &w.cell_path {
Some(cp) => format!(
r#"{{"ctrl":{},"row":{},"col":{},"innerPara":{}}}"#,
cp.table_ctrl_idx, cp.row, cp.col, cp.inner_para_idx,
),
None => "null".to_string(),
};
let kind_name = match &w.kind {
crate::document_core::validation::WarningKind::LinesegArrayEmpty => {
"LinesegArrayEmpty"
}
crate::document_core::validation::WarningKind::LinesegUncomputed => {
"LinesegUncomputed"
}
crate::document_core::validation::WarningKind::LinesegTextRunReflow => {
"LinesegTextRunReflow"
}
};
warning_parts.push(format!(
r#"{{"section":{},"paragraph":{},"kind":"{}","cell":{}}}"#,
w.section_idx, w.paragraph_idx, kind_name, cell_part,
));
}
format!(
r#"{{"count":{},"summary":{{{}}},"warnings":[{}]}}"#,
report.len(),
summary_parts.join(","),
warning_parts.join(","),
)
}
#[wasm_bindgen(js_name = reflowLinesegs)]
pub fn reflow_linesegs(&mut self) -> usize {
self.core.reflow_linesegs_on_demand()
}
#[wasm_bindgen(js_name = convertToEditable)]
pub fn convert_to_editable(&mut self) -> Result<String, JsValue> {
self.convert_to_editable_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = beginBatch)]
pub fn begin_batch(&mut self) -> Result<String, JsValue> {
self.begin_batch_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = endBatch)]
pub fn end_batch(&mut self) -> Result<String, JsValue> {
self.end_batch_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getEventLog)]
pub fn get_event_log(&self) -> String {
self.serialize_event_log()
}
#[wasm_bindgen(js_name = saveSnapshot)]
pub fn save_snapshot(&mut self) -> u32 {
self.save_snapshot_native()
}
#[wasm_bindgen(js_name = restoreSnapshot)]
pub fn restore_snapshot(&mut self, id: u32) -> Result<String, JsValue> {
self.restore_snapshot_native(id).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = discardSnapshot)]
pub fn discard_snapshot(&mut self, id: u32) {
self.discard_snapshot_native(id)
}
#[wasm_bindgen(js_name = snapshotCapacity)]
pub fn snapshot_capacity(&self) -> u32 {
DocumentCore::MAX_SNAPSHOTS as u32
}
#[wasm_bindgen(js_name = captureDeleteRange)]
pub fn capture_delete_range(
&mut self,
section_idx: usize,
start_para: usize,
end_para: usize,
) -> Result<u32, JsValue> {
self.capture_delete_range_native(section_idx, start_para, end_para)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = restoreDeleteFragment)]
pub fn restore_delete_fragment(&mut self, id: u32) -> Result<String, JsValue> {
self.restore_delete_fragment_native(id)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = discardDeleteFragment)]
pub fn discard_delete_fragment(&mut self, id: u32) {
self.discard_delete_fragment_native(id)
}
#[wasm_bindgen(js_name = captureSectionRaw)]
pub fn capture_section_raw(&mut self, section_idx: usize) -> Result<u32, JsValue> {
self.capture_section_raw_native(section_idx)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = capturePictureTransform)]
pub fn capture_picture_transform(&mut self, target_json: &str) -> Result<u32, JsValue> {
self.capture_picture_transform_native(target_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = swapPictureTransform)]
pub fn swap_picture_transform(&mut self, id: u32) -> Result<(), JsValue> {
self.swap_picture_transform_native(id).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = discardPictureTransform)]
pub fn discard_picture_transform(&mut self, id: u32) {
self.discard_picture_transform_native(id);
}
#[wasm_bindgen(js_name = restoreSectionRaw)]
pub fn restore_section_raw(&mut self, id: u32) -> Result<String, JsValue> {
self.restore_section_raw_native(id).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = discardSectionRaw)]
pub fn discard_section_raw(&mut self, id: u32) {
self.discard_section_raw_native(id)
}
#[wasm_bindgen(js_name = getCharPropertiesAt)]
pub fn get_char_properties_at(
&self,
sec_idx: usize,
para_idx: usize,
char_offset: usize,
) -> Result<String, JsValue> {
self.get_char_properties_at_native(sec_idx, para_idx, char_offset)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellCharPropertiesAt)]
pub fn get_cell_char_properties_at(
&self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
char_offset: usize,
) -> Result<String, JsValue> {
self.get_cell_char_properties_at_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
char_offset,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getParaPropertiesAt)]
pub fn get_para_properties_at(
&self,
sec_idx: usize,
para_idx: usize,
) -> Result<String, JsValue> {
self.get_para_properties_at_native(sec_idx, para_idx)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellParaPropertiesAt)]
pub fn get_cell_para_properties_at(
&self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
) -> Result<String, JsValue> {
self.get_cell_para_properties_at_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getStyleList)]
pub fn get_style_list(&self) -> String {
let styles = &self.core.document.doc_info.styles;
let mut items = Vec::new();
for (i, s) in styles.iter().enumerate() {
items.push(format!(
"{{\"id\":{},\"name\":\"{}\",\"englishName\":\"{}\",\"type\":{},\"nextStyleId\":{},\"paraShapeId\":{},\"charShapeId\":{}}}",
i,
json_escape(&s.local_name),
json_escape(&s.english_name),
s.style_type,
s.next_style_id,
s.para_shape_id,
s.char_shape_id
));
}
format!("[{}]", items.join(","))
}
#[wasm_bindgen(js_name = getStyleDetail)]
pub fn get_style_detail(&self, style_id: u32) -> String {
let styles = &self.core.document.doc_info.styles;
let style = match styles.get(style_id as usize) {
Some(s) => s,
None => return "{}".to_string(),
};
let char_json = self
.core
.build_char_properties_json_by_id(style.char_shape_id);
let effective_psid =
self.find_effective_para_shape_for_style(style_id, style.para_shape_id);
let para_json = self.core.build_para_properties_json(effective_psid, 0);
format!(
"{{\"charProps\":{},\"paraProps\":{}}}",
char_json, para_json
)
}
fn find_effective_para_shape_for_style(&self, style_id: u32, base_psid: u16) -> u16 {
use crate::model::style::HeadType;
if let Some(ps) = self
.core
.document
.doc_info
.para_shapes
.get(base_psid as usize)
{
if ps.head_type != HeadType::None {
return base_psid;
}
}
let sid = style_id as u8;
for section in &self.core.document.sections {
for para in §ion.paragraphs {
if para.style_id == sid {
if let Some(ps) = self
.core
.document
.doc_info
.para_shapes
.get(para.para_shape_id as usize)
{
if ps.head_type != HeadType::None {
return para.para_shape_id;
}
}
}
}
}
base_psid
}
#[wasm_bindgen(js_name = updateStyle)]
pub fn update_style(&mut self, style_id: u32, json: &str) -> bool {
use crate::document_core::helpers::json_i32;
let styles = &mut self.core.document.doc_info.styles;
let style = match styles.get_mut(style_id as usize) {
Some(s) => s,
None => return false,
};
if let Some(name) = crate::document_core::helpers::json_str(json, "name") {
style.local_name = name;
}
if let Some(en) = crate::document_core::helpers::json_str(json, "englishName") {
style.english_name = en;
}
if let Some(v) = json_i32(json, "nextStyleId") {
style.next_style_id = v as u8;
}
style.raw_data = None;
self.core.document.doc_info.raw_stream_dirty = true;
true
}
#[wasm_bindgen(js_name = updateStyleShapes)]
pub fn update_style_shapes(
&mut self,
style_id: u32,
char_mods_json: &str,
para_mods_json: &str,
) -> bool {
use crate::document_core::helpers::{
build_tab_def_from_json, json_has_border_keys, json_has_tab_keys,
parse_char_shape_mods, parse_json_i16_array, parse_para_shape_mods,
};
let styles = &self.core.document.doc_info.styles;
let style = match styles.get(style_id as usize) {
Some(s) => s.clone(),
None => return false,
};
let old_csid = style.char_shape_id as u32;
let old_psid = style.para_shape_id;
let style_type = style.style_type;
if !char_mods_json.is_empty() && char_mods_json != "{}" {
let mut char_mods = parse_char_shape_mods(char_mods_json);
if json_has_border_keys(char_mods_json) {
char_mods.border_fill_id =
Some(self.core.create_border_fill_from_json(char_mods_json));
}
if let Some(cs) = self
.core
.document
.doc_info
.char_shapes
.get(style.char_shape_id as usize)
{
let new_cs = char_mods.apply_to(cs);
self.core.document.doc_info.char_shapes.push(new_cs);
let new_id = (self.core.document.doc_info.char_shapes.len() - 1) as u16;
self.core.document.doc_info.styles[style_id as usize].char_shape_id = new_id;
}
}
if !para_mods_json.is_empty() && para_mods_json != "{}" {
let mut para_mods = parse_para_shape_mods(para_mods_json);
if json_has_tab_keys(para_mods_json) {
let doc_info = &self.core.document.doc_info;
let base_tab_def_id = doc_info
.para_shapes
.get(old_psid as usize)
.map_or(0, |ps| ps.tab_def_id);
let tab_def =
build_tab_def_from_json(para_mods_json, base_tab_def_id, &doc_info.tab_defs);
para_mods.tab_def_id = Some(self.core.document.find_or_create_tab_def(tab_def));
}
if json_has_border_keys(para_mods_json) {
para_mods.border_fill_id =
Some(self.core.create_border_fill_from_json(para_mods_json));
}
if let Some(spacing) = parse_json_i16_array(para_mods_json, "borderSpacing", 4) {
para_mods.border_spacing = Some([spacing[0], spacing[1], spacing[2], spacing[3]]);
}
if let Some(ps) = self
.core
.document
.doc_info
.para_shapes
.get(style.para_shape_id as usize)
{
let new_ps = para_mods.apply_to(ps);
self.core.document.doc_info.para_shapes.push(new_ps);
let new_id = (self.core.document.doc_info.para_shapes.len() - 1) as u16;
self.core.document.doc_info.styles[style_id as usize].para_shape_id = new_id;
}
}
self.core.document.doc_info.styles[style_id as usize].raw_data = None;
self.core.document.doc_info.raw_stream_dirty = true;
let sid = style_id as u8;
let mut body_targets = Vec::new();
let mut cell_targets = Vec::new();
for (sec_idx, section) in self.core.document.sections.iter().enumerate() {
for (para_idx, para) in section.paragraphs.iter().enumerate() {
if para.style_id == sid {
body_targets.push((sec_idx, para_idx));
}
for (control_idx, ctrl) in para.controls.iter().enumerate() {
if let Control::Table(table) = ctrl {
for (cell_idx, cell) in table.cells.iter().enumerate() {
for (cell_para_idx, cpara) in cell.paragraphs.iter().enumerate() {
if cpara.style_id == sid {
cell_targets.push((
sec_idx,
para_idx,
control_idx,
cell_idx,
cell_para_idx,
));
}
}
}
}
}
}
}
let updated_style = self.core.document.doc_info.styles[style_id as usize].clone();
let new_csid = updated_style.char_shape_id as u32;
let new_psid = updated_style.para_shape_id;
let (old_indent, new_indent) = self.core.para_shape_indents(old_psid, new_psid);
for (sec_idx, para_idx) in body_targets {
if let Some(para) = self
.core
.document
.sections
.get_mut(sec_idx)
.and_then(|s| s.paragraphs.get_mut(para_idx))
{
if style_type == 0 && para.para_shape_id == old_psid {
para.para_shape_id = new_psid;
}
para.replace_style_char_shape_preserving_overrides(old_csid, new_csid);
}
self.core.reflow_body_paragraph(sec_idx, para_idx);
if let Some(section) = self.core.document.sections.get_mut(sec_idx) {
section.raw_stream = None;
}
}
for (sec_idx, para_idx, control_idx, cell_idx, cell_para_idx) in cell_targets {
if let Ok(cpara) = self.core.get_cell_paragraph_mut(
sec_idx,
para_idx,
control_idx,
cell_idx,
cell_para_idx,
) {
if style_type == 0 && cpara.para_shape_id == old_psid {
cpara.para_shape_id = new_psid;
restamp_indentation(&mut cpara.line_segs, old_indent, new_indent);
}
cpara.replace_style_char_shape_preserving_overrides(old_csid, new_csid);
}
self.core.reflow_cell_paragraph(
sec_idx,
para_idx,
control_idx,
cell_idx,
cell_para_idx,
);
self.core
.mark_cell_control_dirty(sec_idx, para_idx, control_idx);
if let Some(section) = self.core.document.sections.get_mut(sec_idx) {
section.raw_stream = None;
}
}
let num_sections = self.core.document.sections.len();
for sec_idx in 0..num_sections {
self.core.rebuild_section(sec_idx);
}
true
}
#[wasm_bindgen(js_name = createStyle)]
pub fn create_style(&mut self, json: &str) -> i32 {
use crate::document_core::helpers::{json_i32, json_str};
use crate::model::style::Style;
let count = self.core.document.doc_info.styles.len();
let index = match json_i32(json, "index") {
None => count,
Some(index) => match usize::try_from(index) {
Ok(index) if index <= count => index,
_ => return -1,
},
};
if index < count && count > usize::from(u8::MAX) {
return -1;
}
let name = json_str(json, "name").unwrap_or_default();
let english_name = json_str(json, "englishName").unwrap_or_default();
let style_type = json_i32(json, "type").unwrap_or(0) as u8;
let next_style_id = json_i32(json, "nextStyleId").unwrap_or(0) as u8;
let base_style = self.core.document.doc_info.styles.first();
let (fallback_char_shape_id, fallback_para_shape_id) = match base_style {
Some(s) => (s.char_shape_id, s.para_shape_id),
None => (0, 0),
};
let char_shape_id = json_i32(json, "baseCharShapeId")
.filter(|id| *id >= 0)
.map(|id| id as u16)
.filter(|id| (*id as usize) < self.core.document.doc_info.char_shapes.len())
.unwrap_or(fallback_char_shape_id);
let para_shape_id = json_i32(json, "baseParaShapeId")
.filter(|id| *id >= 0)
.map(|id| id as u16)
.filter(|id| (*id as usize) < self.core.document.doc_info.para_shapes.len())
.unwrap_or(fallback_para_shape_id);
let new_style = Style {
raw_data: None,
local_name: name,
english_name,
style_type,
next_style_id,
lang_id: 1042, para_shape_id,
char_shape_id,
lock_form: false,
};
if index < count {
self.remap_style_ids(|id| if id >= index as u16 { id + 1 } else { id });
}
self.core.document.doc_info.styles.insert(index, new_style);
self.core.document.doc_info.raw_stream_dirty = true;
self.core.rebuild_resolved_styles();
index as i32
}
#[wasm_bindgen(js_name = deleteStyle)]
pub fn delete_style(&mut self, style_id: u32) -> bool {
if style_id == 0 {
return false; }
let styles = &self.core.document.doc_info.styles;
if style_id as usize >= styles.len() {
return false;
}
let sid = style_id as u16;
self.remap_style_ids(|id| {
if id == sid {
0
} else if id > sid {
id - 1
} else {
id
}
});
self.core.document.doc_info.styles.remove(style_id as usize);
self.core.rebuild_resolved_styles();
true
}
fn remap_style_ids(&mut self, remap: impl Fn(u16) -> u16) {
use crate::model::identity::walk::{walk, Node};
let remap_records = |records: &mut [crate::model::document::RawRecord]| {
for record in records {
if record.tag_id == crate::parser::tags::HWPTAG_PARA_HEADER {
if let Some(id) = record.data.get_mut(10) {
*id = remap(u16::from(*id)) as u8;
}
}
}
};
let remap_paragraphs = |paragraphs: &mut [Paragraph]| {
walk(paragraphs, |node| {
match node {
Node::Paragraph(para) => para.style_id = remap(u16::from(para.style_id)) as u8,
Node::Control(Control::Ruby(ruby)) => {
ruby.style_id_ref = remap(ruby.style_id_ref);
}
Node::Control(Control::SectionDef(section_def)) => {
remap_records(&mut section_def.extra_child_records);
}
_ => {}
}
Ok(())
})
.expect("style reference traversal is infallible");
};
let tail_sealed =
crate::model::section_tail::validate_preservation(&self.core.document).is_ok();
for section in &mut self.core.document.sections {
remap_paragraphs(&mut section.paragraphs);
remap_records(&mut section.section_def.extra_child_records);
for master in &mut section.section_def.master_pages {
remap_paragraphs(&mut master.paragraphs);
}
if let Some(tail) = &mut section.trailing_lists {
remap_records(&mut tail.records);
}
section.raw_stream = None;
}
if tail_sealed {
self.core.document.seal_section_tail_provenance();
}
if let Some(clipboard) = &mut self.core.clipboard {
remap_paragraphs(&mut clipboard.paragraphs);
}
if let Some(transpose) = &mut self.core.table_transpose_clipboard {
for paragraphs in transpose.data.cells.iter_mut().flatten() {
remap_paragraphs(paragraphs);
}
}
for s in &mut self.core.document.doc_info.styles {
let next = remap(u16::from(s.next_style_id)) as u8;
if next != s.next_style_id {
s.next_style_id = next;
s.raw_data = None;
}
}
self.core.document.doc_info.raw_stream_dirty = true;
}
#[wasm_bindgen(js_name = getNumberingList)]
pub fn get_numbering_list(&self) -> String {
let numberings = &self.core.document.doc_info.numberings;
let mut items = Vec::new();
for (i, n) in numberings.iter().enumerate() {
let formats: Vec<String> = n
.level_formats
.iter()
.map(|f| format!("\"{}\"", json_escape(f)))
.collect();
items.push(format!(
"{{\"id\":{},\"levelFormats\":[{}],\"startNumber\":{}}}",
i + 1,
formats.join(","),
n.start_number
));
}
format!("[{}]", items.join(","))
}
#[wasm_bindgen(js_name = getBulletList)]
pub fn get_bullet_list(&self) -> String {
let bullets = &self.core.document.doc_info.bullets;
let mut items = Vec::new();
for (i, b) in bullets.iter().enumerate() {
let mapped = crate::renderer::layout::map_pua_bullet_char(b.bullet_char);
let raw_code = b.bullet_char as u32;
items.push(format!(
"{{\"id\":{},\"char\":\"{}\",\"rawCode\":{}}}",
i + 1,
mapped,
raw_code
));
}
format!("[{}]", items.join(","))
}
#[wasm_bindgen(js_name = ensureDefaultNumbering)]
pub fn ensure_default_numbering(&mut self) -> u16 {
let numberings = &self.core.document.doc_info.numberings;
if !numberings.is_empty() {
return 1; }
use crate::model::style::{Numbering, NumberingHead};
let mut n = Numbering::default();
let texts = ["^1.", "^2)", "^3)", "^4)", "^5)", "^6)", "^7)"];
for (format, text) in n.level_formats.iter_mut().zip(texts) {
*format = text.to_string();
}
n.start_number = 1;
n.level_start_numbers = [1; crate::model::style::NUMBERING_LEVELS];
n.heads[0] = NumberingHead {
number_format: 0,
attr: 1 << 3,
..Default::default()
}; n.heads[1] = NumberingHead {
number_format: 8,
attr: 1 << 3,
..Default::default()
}; n.heads[2] = NumberingHead {
number_format: 0,
attr: 1 << 3,
..Default::default()
}; n.heads[3] = NumberingHead {
number_format: 8,
attr: 1 << 3,
..Default::default()
}; n.heads[4] = NumberingHead {
number_format: 1,
attr: 1 << 3,
..Default::default()
}; n.heads[5] = NumberingHead {
number_format: 10,
attr: 1 << 3,
..Default::default()
}; n.heads[6] = NumberingHead {
number_format: 5,
attr: 1 << 3,
..Default::default()
}; self.core.document.doc_info.numberings.push(n);
self.core.document.doc_info.raw_stream_dirty = true;
1
}
#[wasm_bindgen(js_name = createNumbering)]
pub fn create_numbering(&mut self, json: &str) -> u16 {
use crate::document_core::helpers::json_i32;
use crate::model::style::{Numbering, NumberingHead};
let mut n = Numbering::default();
if let Some(arr_start) = json.find("\"levelFormats\"") {
let rest = &json[arr_start..];
if let Some(bracket_start) = rest.find('[') {
if let Some(bracket_end) = rest[bracket_start..].find(']') {
let arr_str = &rest[bracket_start + 1..bracket_start + bracket_end];
let mut level = 0;
for part in arr_str.split(',') {
if level >= crate::model::style::NUMBERING_LEVELS {
break;
}
let trimmed = part.trim().trim_matches('"');
if !trimmed.is_empty() {
n.level_formats[level] = trimmed.to_string();
level += 1;
}
}
}
}
}
if let Some(arr_start) = json.find("\"numberFormats\"") {
let rest = &json[arr_start..];
if let Some(bracket_start) = rest.find('[') {
if let Some(bracket_end) = rest[bracket_start..].find(']') {
let arr_str = &rest[bracket_start + 1..bracket_start + bracket_end];
let mut level = 0;
for part in arr_str.split(',') {
if level >= crate::model::style::NUMBERING_LEVELS {
break;
}
if let Ok(code) = part.trim().parse::<u8>() {
n.heads[level] = NumberingHead {
number_format: code,
attr: 1 << 3, ..Default::default()
};
level += 1;
}
}
}
}
}
n.start_number = json_i32(json, "startNumber").unwrap_or(1) as u16;
n.level_start_numbers = [n.start_number as u32; crate::model::style::NUMBERING_LEVELS];
self.core.document.doc_info.numberings.push(n);
self.core.document.doc_info.raw_stream_dirty = true;
self.core.document.doc_info.numberings.len() as u16
}
#[wasm_bindgen(js_name = setNumberingCharShape)]
pub fn set_numbering_char_shape(
&mut self,
numbering_id: u16,
level: u32,
json: Option<String>,
) -> Result<u32, JsValue> {
self.core
.set_numbering_char_shape_native(numbering_id, level as usize, json.as_deref())
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = createBullet)]
pub fn create_bullet(
&mut self,
json: &str,
image_data: Option<Vec<u8>>,
extension: Option<String>,
) -> Result<u16, JsValue> {
let image = match (&image_data, &extension) {
(Some(bytes), Some(extension)) => Some((bytes.as_slice(), extension.as_str())),
(None, None) => None,
_ => return Err(JsValue::from_str("그림과 확장자를 함께 주어야 함")),
};
self.core
.create_bullet_native(json, image)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = ensureDefaultBullet)]
pub fn ensure_default_bullet(&mut self, bullet_char_str: &str) -> u16 {
let bullet_ch = bullet_char_str.chars().next().unwrap_or('●');
let bullets = &self.core.document.doc_info.bullets;
for (i, b) in bullets.iter().enumerate() {
let mapped = crate::renderer::layout::map_pua_bullet_char(b.bullet_char);
if mapped == bullet_ch {
return (i + 1) as u16;
}
}
use crate::model::style::Bullet;
let b = Bullet {
bullet_char: bullet_ch,
attr: 1 << 3, text_distance: 50,
..Default::default()
};
self.core.document.doc_info.bullets.push(b);
self.core.document.doc_info.bullets.len() as u16
}
#[wasm_bindgen(js_name = getStyleAt)]
pub fn get_style_at(&self, sec_idx: u32, para_idx: u32) -> String {
let sec = sec_idx as usize;
let para = para_idx as usize;
let style_id = self
.core
.document
.sections
.get(sec)
.and_then(|s| s.paragraphs.get(para))
.map(|p| p.style_id as usize)
.unwrap_or(0);
let name = self
.core
.document
.doc_info
.styles
.get(style_id)
.map(|s| s.local_name.as_str())
.unwrap_or("");
format!("{{\"id\":{},\"name\":\"{}\"}}", style_id, json_escape(name))
}
#[wasm_bindgen(js_name = getCellStyleAt)]
pub fn get_cell_style_at(
&self,
sec_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
) -> String {
let style_id = self
.core
.get_cell_paragraph_ref(
sec_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
)
.map(|p| p.style_id as usize)
.unwrap_or(0);
let name = self
.core
.document
.doc_info
.styles
.get(style_id)
.map(|s| s.local_name.as_str())
.unwrap_or("");
format!("{{\"id\":{},\"name\":\"{}\"}}", style_id, json_escape(name))
}
#[wasm_bindgen(js_name = applyStyle)]
pub fn apply_style(
&mut self,
sec_idx: u32,
para_idx: u32,
style_id: u32,
) -> Result<String, JsValue> {
self.core
.apply_style_native(sec_idx as usize, para_idx as usize, style_id as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCellStyle)]
pub fn apply_cell_style(
&mut self,
sec_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
style_id: u32,
) -> Result<String, JsValue> {
self.core
.apply_cell_style_native(
sec_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
style_id as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = evaluateTableFormula)]
pub fn evaluate_table_formula(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
target_row: u32,
target_col: u32,
formula: &str,
write_result: bool,
) -> Result<String, JsValue> {
self.core
.evaluate_table_formula(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
target_row as usize,
target_col as usize,
formula,
write_result,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = evaluateTableFormulaEx)]
pub fn evaluate_table_formula_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_str, json_u32};
self.core
.evaluate_table_formula(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "targetRow").unwrap_or(0) as usize,
json_u32(options_json, "targetCol").unwrap_or(0) as usize,
&json_str(options_json, "formula").unwrap_or_default(),
json_bool(options_json, "writeResult").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setTableCellFormula)]
pub fn set_table_cell_formula(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
formula: &str,
result_format: &str,
) -> Result<String, JsValue> {
self.core
.set_table_cell_formula(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
formula,
result_format,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = removeTableCellFormulas)]
pub fn remove_table_cell_formulas(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
) -> Result<String, JsValue> {
self.core
.remove_table_cell_formulas(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = recalculateTableFormulas)]
pub fn recalculate_table_formulas(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
edited_cell: i32,
paginate: bool,
) -> Result<String, JsValue> {
self.core
.recalculate_table_formulas(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
usize::try_from(edited_cell).ok(),
paginate,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = findOrCreateFontId)]
pub fn find_or_create_font_id(&mut self, name: &str) -> i32 {
self.find_or_create_font_id_native(name)
}
#[wasm_bindgen(js_name = findOrCreateFontIdForLang)]
pub fn wasm_find_or_create_font_id_for_lang(&mut self, lang: u32, name: &str) -> i32 {
self.core
.find_or_create_font_id_for_lang(lang as usize, name)
}
#[wasm_bindgen(js_name = applyCharFormat)]
pub fn apply_char_format(
&mut self,
sec_idx: usize,
para_idx: usize,
start_offset: usize,
end_offset: usize,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_char_format_native(sec_idx, para_idx, start_offset, end_offset, props_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCharShapeRuns)]
pub fn get_char_shape_runs(
&self,
sec: usize,
para: usize,
start: usize,
end: usize,
) -> Result<String, JsValue> {
self.get_char_shape_runs_native(sec, para, start, end)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = setCharShapeRuns)]
pub fn set_char_shape_runs(
&mut self,
sec: usize,
para: usize,
start: usize,
end: usize,
runs_json: &str,
) -> Result<String, JsValue> {
self.set_char_shape_runs_native(sec, para, start, end, runs_json)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = getCharShapeRunsInCellByPath)]
pub fn get_char_shape_runs_in_cell_by_path(
&mut self,
sec: usize,
para: usize,
path_json: &str,
start: usize,
end: usize,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.get_char_shape_runs_in_cell_by_path_native(sec, para, &path, start, end)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = setCharShapeRunsInCellByPath)]
pub fn set_char_shape_runs_in_cell_by_path(
&mut self,
sec: usize,
para: usize,
path_json: &str,
start: usize,
end: usize,
runs_json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.set_char_shape_runs_in_cell_by_path_native(sec, para, &path, start, end, runs_json)
.map_err(Into::into)
}
#[wasm_bindgen(js_name = setCharShapeId)]
pub fn set_char_shape_id(
&mut self,
sec_idx: usize,
para_idx: usize,
start_offset: usize,
end_offset: usize,
char_shape_id: u32,
) -> Result<String, JsValue> {
self.set_char_shape_id_native(sec_idx, para_idx, start_offset, end_offset, char_shape_id)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCharFormatInCell)]
pub fn apply_char_format_in_cell(
&mut self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
start_offset: usize,
end_offset: usize,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_char_format_in_cell_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
start_offset,
end_offset,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCharFormatInCellEx)]
pub fn apply_char_format_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_object, json_u32};
let props_json = json_object(options_json, "props").unwrap_or_else(|| "{}".to_string());
self.apply_char_format_in_cell_native(
json_u32(options_json, "secIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startOffset").unwrap_or(0) as usize,
json_u32(options_json, "endOffset").unwrap_or(0) as usize,
&props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyCharFormatInCellByPath)]
#[allow(clippy::too_many_arguments)]
pub fn apply_char_format_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_offset: u32,
end_offset: u32,
props_json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.apply_char_format_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
start_offset as usize,
end_offset as usize,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellCharPropertiesAtByPath)]
pub fn get_cell_char_properties_at_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.get_cell_char_properties_at_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCharShapeIdInCellByPath)]
#[allow(clippy::too_many_arguments)]
pub fn set_char_shape_id_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_offset: u32,
end_offset: u32,
char_shape_id: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.set_char_shape_id_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
start_offset as usize,
end_offset as usize,
char_shape_id,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCharShapeIdInCell)]
pub fn set_char_shape_id_in_cell(
&mut self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
start_offset: usize,
end_offset: usize,
char_shape_id: u32,
) -> Result<String, JsValue> {
self.set_char_shape_id_in_cell_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
start_offset,
end_offset,
char_shape_id,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCharShapeIdInCellEx)]
pub fn set_char_shape_id_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.set_char_shape_id_in_cell_native(
json_u32(options_json, "secIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startOffset").unwrap_or(0) as usize,
json_u32(options_json, "endOffset").unwrap_or(0) as usize,
json_u32(options_json, "charShapeId").unwrap_or(0),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPageHide)]
pub fn set_page_hide(
&mut self,
sec: u32,
para: u32,
hide_header: bool,
hide_footer: bool,
hide_master: bool,
hide_border: bool,
hide_fill: bool,
hide_page_num: bool,
) -> Result<String, JsValue> {
self.set_page_hide_native(
sec as usize,
para as usize,
hide_header,
hide_footer,
hide_master,
hide_border,
hide_fill,
hide_page_num,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPageHideEx)]
pub fn set_page_hide_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_bool, json_u32};
self.set_page_hide_native(
json_u32(options_json, "sec").unwrap_or(0) as usize,
json_u32(options_json, "para").unwrap_or(0) as usize,
json_bool(options_json, "hideHeader").unwrap_or(false),
json_bool(options_json, "hideFooter").unwrap_or(false),
json_bool(options_json, "hideMaster").unwrap_or(false),
json_bool(options_json, "hideBorder").unwrap_or(false),
json_bool(options_json, "hideFill").unwrap_or(false),
json_bool(options_json, "hidePageNum").unwrap_or(false),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageHide)]
pub fn get_page_hide(&self, sec: u32, para: u32) -> Result<String, JsValue> {
self.get_page_hide_native(sec as usize, para as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setPageNumberPosition)]
pub fn set_page_number_position(
&mut self,
section_idx: u32,
position: u32,
format: u32,
dash: bool,
) -> Result<String, JsValue> {
self.set_page_number_position_native(
section_idx as usize,
u8::try_from(position).unwrap_or(u8::MAX),
u8::try_from(format).unwrap_or(u8::MAX),
dash,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getPageNumberPosition)]
pub fn get_page_number_position(&self, section_idx: u32) -> Result<String, JsValue> {
self.get_page_number_position_native(section_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setNumberingRestart)]
pub fn set_numbering_restart(
&mut self,
section_idx: u32,
para_idx: u32,
mode: u8,
start_num: u32,
) -> Result<String, JsValue> {
self.set_numbering_restart_native(section_idx as usize, para_idx as usize, mode, start_num)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormat)]
pub fn apply_para_format(
&mut self,
sec_idx: usize,
para_idx: usize,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_para_format_native(sec_idx, para_idx, props_json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setParaShapeId)]
pub fn set_para_shape_id(
&mut self,
sec_idx: usize,
para_idx: usize,
para_shape_id: u16,
) -> Result<String, JsValue> {
self.set_para_shape_id_native(sec_idx, para_idx, para_shape_id)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormatInCell)]
pub fn apply_para_format_in_cell(
&mut self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
props_json: &str,
) -> Result<String, JsValue> {
self.apply_para_format_in_cell_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getCellParaPropertiesAtByPath)]
pub fn get_cell_para_properties_at_by_path_api(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.get_cell_para_properties_at_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = applyParaFormatInCellByPath)]
pub fn apply_para_format_in_cell_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
props_json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.apply_para_format_in_cell_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
props_json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellParaShapeIdByPath)]
pub fn set_cell_para_shape_id_by_path_api(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
para_shape_id: u16,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.set_cell_para_shape_id_by_path(
section_idx as usize,
parent_para_idx as usize,
&path,
para_shape_id,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setCellParaShapeId)]
pub fn set_cell_para_shape_id(
&mut self,
sec_idx: usize,
parent_para_idx: usize,
control_idx: usize,
cell_idx: usize,
cell_para_idx: usize,
para_shape_id: u16,
) -> Result<String, JsValue> {
self.set_cell_para_shape_id_native(
sec_idx,
parent_para_idx,
control_idx,
cell_idx,
cell_para_idx,
para_shape_id,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = hasInternalClipboard)]
pub fn has_internal_clipboard(&self) -> bool {
self.has_internal_clipboard_native()
}
#[wasm_bindgen(js_name = clipboardIsSingleText)]
pub fn clipboard_is_single_text(&self) -> bool {
self.clipboard_is_single_text_native()
}
#[wasm_bindgen(js_name = getClipboardText)]
pub fn get_clipboard_text(&self) -> String {
self.get_clipboard_text_native()
}
#[wasm_bindgen(js_name = clearClipboard)]
pub fn clear_clipboard(&mut self) {
self.clear_clipboard_native()
}
#[wasm_bindgen(js_name = copySelection)]
pub fn copy_selection(
&mut self,
section_idx: u32,
start_para_idx: u32,
start_char_offset: u32,
end_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.copy_selection_native(
section_idx as usize,
start_para_idx as usize,
start_char_offset as usize,
end_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copySelectionLogical)]
pub fn copy_selection_logical(
&mut self,
section_idx: u32,
start_para_idx: u32,
start_logical_offset: u32,
end_para_idx: u32,
end_logical_offset: u32,
) -> Result<String, JsValue> {
self.copy_selection_logical_native(
section_idx as usize,
start_para_idx as usize,
start_logical_offset as usize,
end_para_idx as usize,
end_logical_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copySelectionInCell)]
pub fn copy_selection_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.copy_selection_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copySelectionInCellEx)]
pub fn copy_selection_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.copy_selection_in_cell_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "endCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "endCharOffset").unwrap_or(0) as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copySelectionInCellByPath)]
pub fn copy_selection_in_cell_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.copy_selection_in_cell_by_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = copyControl)]
pub fn copy_control(
&mut self,
section_idx: u32,
para_idx: u32,
cell_path_json: &str,
control_idx: u32,
) -> Result<String, JsValue> {
let cell_path = parse_cell_path_arg(cell_path_json)?;
self.copy_control_native(
section_idx as usize,
para_idx as usize,
&cell_path,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = clipboardHasControl)]
pub fn clipboard_has_control(&self) -> bool {
self.clipboard_has_control_native()
}
#[wasm_bindgen(js_name = pasteControl)]
pub fn paste_control(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.paste_control_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteInternal)]
pub fn paste_internal(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.paste_internal_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteInternalTextLogical)]
pub fn paste_internal_text_logical(
&mut self,
section_idx: u32,
para_idx: u32,
logical_offset: u32,
) -> Result<String, JsValue> {
self.paste_internal_text_logical_native(
section_idx as usize,
para_idx as usize,
logical_offset as usize,
)
.map_err(|error| error.into())
}
#[wasm_bindgen(js_name = pasteInternalInCell)]
pub fn paste_internal_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
) -> Result<String, JsValue> {
self.paste_internal_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteInternalInCellByPath)]
pub fn paste_internal_in_cell_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.paste_internal_in_cell_by_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportSelectionHtml)]
pub fn export_selection_html(
&self,
section_idx: u32,
start_para_idx: u32,
start_char_offset: u32,
end_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.export_selection_html_native(
section_idx as usize,
start_para_idx as usize,
start_char_offset as usize,
end_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportSelectionInCellHtml)]
pub fn export_selection_in_cell_html(
&self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
self.export_selection_in_cell_html_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportSelectionInCellHtmlEx)]
pub fn export_selection_in_cell_html_ex(&self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::json_u32;
self.export_selection_in_cell_html_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "startCharOffset").unwrap_or(0) as usize,
json_u32(options_json, "endCellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "endCharOffset").unwrap_or(0) as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportSelectionInCellHtmlByPath)]
pub fn export_selection_in_cell_html_by_path(
&self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
start_cell_para_idx: u32,
start_char_offset: u32,
end_cell_para_idx: u32,
end_char_offset: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.export_selection_in_cell_html_by_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
start_cell_para_idx as usize,
start_char_offset as usize,
end_cell_para_idx as usize,
end_char_offset as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = exportControlHtml)]
pub fn export_control_html(
&self,
section_idx: u32,
para_idx: u32,
cell_path_json: &str,
control_idx: u32,
) -> Result<String, JsValue> {
let cell_path = parse_cell_path_arg(cell_path_json)?;
self.export_control_html_native(
section_idx as usize,
para_idx as usize,
&cell_path,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getControlImageData)]
pub fn get_control_image_data(
&self,
section_idx: u32,
para_idx: u32,
cell_path_json: &str,
control_idx: u32,
) -> Result<Vec<u8>, JsValue> {
let cell_path = parse_cell_path_arg(cell_path_json)?;
self.get_control_image_data_native(
section_idx as usize,
para_idx as usize,
&cell_path,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getControlImageMime)]
pub fn get_control_image_mime(
&self,
section_idx: u32,
para_idx: u32,
cell_path_json: &str,
control_idx: u32,
) -> Result<String, JsValue> {
let cell_path = parse_cell_path_arg(cell_path_json)?;
self.get_control_image_mime_native(
section_idx as usize,
para_idx as usize,
&cell_path,
control_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteHwpJson)]
pub fn paste_hwp_json(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
self.paste_hwp_json_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteHtml)]
pub fn paste_html(
&mut self,
section_idx: u32,
para_idx: u32,
char_offset: u32,
html: &str,
) -> Result<String, JsValue> {
self.paste_html_native(
section_idx as usize,
para_idx as usize,
char_offset as usize,
html,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteHtmlTextLogical)]
pub fn paste_html_text_logical(
&mut self,
section_idx: u32,
para_idx: u32,
logical_offset: u32,
html: &str,
) -> Result<String, JsValue> {
self.paste_html_text_logical_native(
section_idx as usize,
para_idx as usize,
logical_offset as usize,
html,
)
.map_err(|error| error.into())
}
#[wasm_bindgen(js_name = pasteHtmlInCell)]
pub fn paste_html_in_cell(
&mut self,
section_idx: u32,
parent_para_idx: u32,
control_idx: u32,
cell_idx: u32,
cell_para_idx: u32,
char_offset: u32,
html: &str,
) -> Result<String, JsValue> {
self.paste_html_in_cell_native(
section_idx as usize,
parent_para_idx as usize,
control_idx as usize,
cell_idx as usize,
cell_para_idx as usize,
char_offset as usize,
html,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteHtmlInCellEx)]
pub fn paste_html_in_cell_ex(&mut self, options_json: &str) -> Result<String, JsValue> {
use crate::document_core::helpers::{json_str, json_u32};
self.paste_html_in_cell_native(
json_u32(options_json, "sectionIdx").unwrap_or(0) as usize,
json_u32(options_json, "parentParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "controlIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellIdx").unwrap_or(0) as usize,
json_u32(options_json, "cellParaIdx").unwrap_or(0) as usize,
json_u32(options_json, "charOffset").unwrap_or(0) as usize,
&json_str(options_json, "html").unwrap_or_default(),
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = pasteHtmlInCellByPath)]
pub fn paste_html_in_cell_by_path(
&mut self,
section_idx: u32,
parent_para_idx: u32,
path_json: &str,
char_offset: u32,
html: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.paste_html_in_cell_by_path_native(
section_idx as usize,
parent_para_idx as usize,
&path,
char_offset as usize,
html,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = measureWidthDiagnostic)]
pub fn measure_width_diagnostic(
&self,
section_idx: u32,
para_idx: u32,
) -> Result<String, JsValue> {
self.measure_width_diagnostic_native(section_idx as usize, para_idx as usize)
.map_err(|e| e.into())
}
}
pub(crate) mod event;
#[wasm_bindgen]
pub struct HwpViewer {
document: HwpDocument,
scheduler: RenderScheduler,
}
#[wasm_bindgen]
impl HwpViewer {
#[wasm_bindgen(constructor)]
pub fn new(document: HwpDocument) -> Self {
let page_count = document.page_count();
let scheduler = RenderScheduler::new(page_count);
Self {
document,
scheduler,
}
}
#[wasm_bindgen(js_name = updateViewport)]
pub fn update_viewport(&mut self, scroll_x: f64, scroll_y: f64, width: f64, height: f64) {
let event = RenderEvent::ViewportChanged(Viewport {
scroll_x,
scroll_y,
width,
height,
zoom: self.scheduler_zoom(),
});
self.scheduler.on_event(&event);
}
#[wasm_bindgen(js_name = setZoom)]
pub fn set_zoom(&mut self, zoom: f64) {
let event = RenderEvent::ZoomChanged(zoom);
self.scheduler.on_event(&event);
}
#[wasm_bindgen(js_name = visiblePages)]
pub fn visible_pages(&self) -> Vec<u32> {
self.scheduler.visible_pages()
}
#[wasm_bindgen(js_name = pendingTaskCount)]
pub fn pending_task_count(&self) -> u32 {
self.scheduler.pending_count() as u32
}
#[wasm_bindgen(js_name = pageCount)]
pub fn page_count(&self) -> u32 {
self.document.page_count()
}
#[wasm_bindgen(js_name = renderPageSvg)]
pub fn render_page_svg(&self, page_num: u32) -> Result<String, JsValue> {
self.document.render_page_svg(page_num)
}
#[wasm_bindgen(js_name = setAnnotateMetricFont)]
pub fn set_annotate_metric_font(&mut self, enabled: bool) {
self.document.set_annotate_metric_font(enabled);
}
#[wasm_bindgen(js_name = renderPageSvgWithProfile)]
pub fn render_page_svg_with_profile(
&self,
page_num: u32,
profile: &str,
) -> Result<String, JsValue> {
self.document
.render_page_svg_with_profile(page_num, profile)
}
#[wasm_bindgen(js_name = renderPageHtml)]
pub fn render_page_html(&self, page_num: u32) -> Result<String, JsValue> {
self.document.render_page_html(page_num)
}
}
impl HwpViewer {
fn scheduler_zoom(&self) -> f64 {
1.0
}
}
#[wasm_bindgen]
impl HwpDocument {
#[wasm_bindgen(js_name = getBookmarks)]
pub fn get_bookmarks(&self) -> Result<String, JsValue> {
self.core.get_bookmarks_native().map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getStructure)]
pub fn get_structure(&self, mode: &str) -> Result<String, JsValue> {
self.core.get_structure_native(mode).map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getOutlineNavigation)]
pub fn get_outline_navigation(&self) -> Result<String, JsValue> {
self.core
.get_outline_navigation_native()
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertRuby)]
pub fn insert_ruby(
&mut self,
sec: u32,
para: u32,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_ruby_native(sec as usize, para as usize, char_offset as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertCharOverlap)]
pub fn insert_char_overlap(
&mut self,
sec: u32,
para: u32,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_char_overlap_native(sec as usize, para as usize, char_offset as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getComposeControl)]
pub fn get_compose_control(
&self,
sec: u32,
para: u32,
ctrl_idx: u32,
) -> Result<String, JsValue> {
self.core
.get_compose_control_native(sec as usize, para as usize, ctrl_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setComposeControl)]
pub fn set_compose_control(
&mut self,
sec: u32,
para: u32,
ctrl_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.set_compose_control_native(sec as usize, para as usize, ctrl_idx as usize, json)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteComposeControl)]
pub fn delete_compose_control(
&mut self,
sec: u32,
para: u32,
ctrl_idx: u32,
keep_main_text: bool,
) -> Result<String, JsValue> {
self.core
.delete_compose_control_native(
sec as usize,
para as usize,
ctrl_idx as usize,
keep_main_text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertRubyByPath)]
pub fn insert_ruby_by_path(
&mut self,
sec: u32,
parent_para: u32,
path_json: &str,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.core
.insert_ruby_at(
compose_cell(sec, parent_para, &path),
char_offset as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertCharOverlapByPath)]
pub fn insert_char_overlap_by_path(
&mut self,
sec: u32,
parent_para: u32,
path_json: &str,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.core
.insert_char_overlap_at(
compose_cell(sec, parent_para, &path),
char_offset as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getComposeControlByPath)]
pub fn get_compose_control_by_path(
&self,
sec: u32,
parent_para: u32,
path_json: &str,
ctrl_idx: u32,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.core
.get_compose_control_at(compose_cell(sec, parent_para, &path), ctrl_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setComposeControlByPath)]
pub fn set_compose_control_by_path(
&mut self,
sec: u32,
parent_para: u32,
path_json: &str,
ctrl_idx: u32,
json: &str,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.core
.set_compose_control_at(
compose_cell(sec, parent_para, &path),
ctrl_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteComposeControlByPath)]
pub fn delete_compose_control_by_path(
&mut self,
sec: u32,
parent_para: u32,
path_json: &str,
ctrl_idx: u32,
keep_main_text: bool,
) -> Result<String, JsValue> {
let path = DocumentCore::parse_cell_path(path_json)?;
self.core
.delete_compose_control_at(
compose_cell(sec, parent_para, &path),
ctrl_idx as usize,
keep_main_text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertRubyInFootnote)]
pub fn insert_ruby_in_footnote(
&mut self,
sec: u32,
para: u32,
control_idx: u32,
fn_para: u32,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_ruby_at(
compose_note(sec, para, control_idx, fn_para),
char_offset as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = insertCharOverlapInFootnote)]
pub fn insert_char_overlap_in_footnote(
&mut self,
sec: u32,
para: u32,
control_idx: u32,
fn_para: u32,
char_offset: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.insert_char_overlap_at(
compose_note(sec, para, control_idx, fn_para),
char_offset as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = getComposeControlInFootnote)]
pub fn get_compose_control_in_footnote(
&self,
sec: u32,
para: u32,
control_idx: u32,
fn_para: u32,
ctrl_idx: u32,
) -> Result<String, JsValue> {
self.core
.get_compose_control_at(
compose_note(sec, para, control_idx, fn_para),
ctrl_idx as usize,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = setComposeControlInFootnote)]
pub fn set_compose_control_in_footnote(
&mut self,
sec: u32,
para: u32,
control_idx: u32,
fn_para: u32,
ctrl_idx: u32,
json: &str,
) -> Result<String, JsValue> {
self.core
.set_compose_control_at(
compose_note(sec, para, control_idx, fn_para),
ctrl_idx as usize,
json,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteComposeControlInFootnote)]
pub fn delete_compose_control_in_footnote(
&mut self,
sec: u32,
para: u32,
control_idx: u32,
fn_para: u32,
ctrl_idx: u32,
keep_main_text: bool,
) -> Result<String, JsValue> {
self.core
.delete_compose_control_at(
compose_note(sec, para, control_idx, fn_para),
ctrl_idx as usize,
keep_main_text,
)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = addBookmark)]
pub fn add_bookmark(
&mut self,
sec: u32,
para: u32,
char_offset: u32,
name: &str,
) -> Result<String, JsValue> {
self.core
.add_bookmark_native(sec as usize, para as usize, char_offset as usize, name)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = deleteBookmark)]
pub fn delete_bookmark(
&mut self,
sec: u32,
para: u32,
ctrl_idx: u32,
) -> Result<String, JsValue> {
self.core
.delete_bookmark_native(sec as usize, para as usize, ctrl_idx as usize)
.map_err(|e| e.into())
}
#[wasm_bindgen(js_name = renameBookmark)]
pub fn rename_bookmark(
&mut self,
sec: u32,
para: u32,
ctrl_idx: u32,
new_name: &str,
) -> Result<String, JsValue> {
self.core
.rename_bookmark_native(sec as usize, para as usize, ctrl_idx as usize, new_name)
.map_err(|e| e.into())
}
}
#[wasm_bindgen(js_name = extractThumbnail)]
pub fn extract_thumbnail(data: &[u8]) -> JsValue {
match crate::parser::extract_thumbnail_only(data) {
Some(result) => {
let base64 = base64_encode(&result.data);
let mime = match result.format.as_str() {
"png" => "image/png",
"bmp" => "image/bmp",
"gif" => "image/gif",
_ => "application/octet-stream",
};
let json = format!(
r#"{{"format":"{}","base64":"{}","dataUri":"data:{};base64,{}","width":{},"height":{}}}"#,
result.format, base64, mime, base64, result.width, result.height
);
JsValue::from_str(&json)
}
None => JsValue::NULL,
}
}
fn base64_encode(data: &[u8]) -> String {
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(data)
}
#[cfg(test)]
mod tests;