1use crate::{
7 BlockFormat, BlockInfo, DocumentStats, FindMatch, FindOptions, FrameFormat, ListFormat,
8 TextFormat,
9};
10
11pub fn to_i64(v: usize) -> i64 {
14 debug_assert!(v <= i64::MAX as usize, "position overflow: {v}");
15 v as i64
16}
17
18pub fn to_usize(v: i64) -> usize {
19 assert!(v >= 0, "negative position: {v}");
20 v as usize
21}
22
23impl From<&frontend::document_inspection::DocumentStatsDto> for DocumentStats {
26 fn from(dto: &frontend::document_inspection::DocumentStatsDto) -> Self {
27 Self {
28 character_count: to_usize(dto.character_count),
29 word_count: to_usize(dto.word_count),
30 block_count: to_usize(dto.block_count),
31 frame_count: to_usize(dto.frame_count),
32 image_count: to_usize(dto.image_count),
33 list_count: to_usize(dto.list_count),
34 table_count: to_usize(dto.table_count),
35 }
36 }
37}
38
39impl From<&frontend::document_inspection::BlockInfoDto> for BlockInfo {
42 fn from(dto: &frontend::document_inspection::BlockInfoDto) -> Self {
43 Self {
44 block_id: to_usize(dto.block_id),
45 block_number: to_usize(dto.block_number),
46 start: to_usize(dto.block_start),
47 length: to_usize(dto.block_length),
48 }
49 }
50}
51
52impl FindOptions {
55 pub(crate) fn to_find_text_dto(
56 &self,
57 query: &str,
58 start_position: usize,
59 ) -> frontend::document_search::FindTextDto {
60 frontend::document_search::FindTextDto {
61 query: query.into(),
62 case_sensitive: self.case_sensitive,
63 whole_word: self.whole_word,
64 diacritic_sensitive: self.diacritic_sensitive,
65 language: self.language.clone(),
66 use_regex: self.use_regex,
67 search_backward: self.search_backward,
68 start_position: to_i64(start_position),
69 }
70 }
71
72 pub(crate) fn to_find_all_dto(&self, query: &str) -> frontend::document_search::FindAllDto {
73 frontend::document_search::FindAllDto {
74 query: query.into(),
75 case_sensitive: self.case_sensitive,
76 whole_word: self.whole_word,
77 diacritic_sensitive: self.diacritic_sensitive,
78 language: self.language.clone(),
79 use_regex: self.use_regex,
80 }
81 }
82}
83
84impl crate::ReplaceOptions {
85 pub(crate) fn to_replace_ranges_dto(
89 &self,
90 ranges: &[crate::ReplaceRange],
91 ) -> frontend::document_search::ReplaceRangesDto {
92 let mut positions = Vec::with_capacity(ranges.len());
93 let mut lengths = Vec::with_capacity(ranges.len());
94 let mut replacements = Vec::with_capacity(ranges.len());
95 for range in ranges {
96 positions.push(to_i64(range.position));
97 lengths.push(to_i64(range.length));
98 replacements.push(range.replacement.clone());
99 }
100 frontend::document_search::ReplaceRangesDto {
101 positions,
102 lengths,
103 replacements,
104 format_policy: self.format_policy,
105 }
106 }
107
108 pub(crate) fn to_replace_dto(
109 &self,
110 query: &str,
111 replacement: &str,
112 replace_all: bool,
113 ) -> frontend::document_search::ReplaceTextDto {
114 frontend::document_search::ReplaceTextDto {
115 query: query.into(),
116 replacement: replacement.into(),
117 case_sensitive: self.find.case_sensitive,
118 whole_word: self.find.whole_word,
119 diacritic_sensitive: self.find.diacritic_sensitive,
120 language: self.find.language.clone(),
121 use_regex: self.find.use_regex,
122 replace_all,
123 format_policy: self.format_policy,
124 }
125 }
126}
127
128pub fn find_result_to_match(dto: &frontend::document_search::FindResultDto) -> Option<FindMatch> {
129 if dto.found {
130 Some(FindMatch {
131 position: to_usize(dto.position),
132 length: to_usize(dto.length),
133 matched_text: dto.matched_text.clone(),
134 })
135 } else {
136 None
137 }
138}
139
140pub fn find_all_to_matches(dto: &frontend::document_search::FindAllResultDto) -> Vec<FindMatch> {
144 debug_assert_eq!(dto.positions.len(), dto.lengths.len());
145 debug_assert_eq!(dto.positions.len(), dto.matched_texts.len());
146 dto.positions
147 .iter()
148 .zip(dto.lengths.iter())
149 .zip(dto.matched_texts.iter())
150 .map(|((&pos, &len), text)| FindMatch {
151 position: to_usize(pos),
152 length: to_usize(len),
153 matched_text: text.clone(),
154 })
155 .collect()
156}
157
158use frontend::document_formatting::dtos as fmt_dto;
167
168fn underline_style_to_dto(v: &crate::UnderlineStyle) -> fmt_dto::UnderlineStyle {
169 match v {
170 crate::UnderlineStyle::NoUnderline => fmt_dto::UnderlineStyle::NoUnderline,
171 crate::UnderlineStyle::SingleUnderline => fmt_dto::UnderlineStyle::SingleUnderline,
172 crate::UnderlineStyle::DashUnderline => fmt_dto::UnderlineStyle::DashUnderline,
173 crate::UnderlineStyle::DotLine => fmt_dto::UnderlineStyle::DotLine,
174 crate::UnderlineStyle::DashDotLine => fmt_dto::UnderlineStyle::DashDotLine,
175 crate::UnderlineStyle::DashDotDotLine => fmt_dto::UnderlineStyle::DashDotDotLine,
176 crate::UnderlineStyle::WaveUnderline => fmt_dto::UnderlineStyle::WaveUnderline,
177 crate::UnderlineStyle::SpellCheckUnderline => fmt_dto::UnderlineStyle::SpellCheckUnderline,
178 }
179}
180
181fn vertical_alignment_to_dto(v: &crate::CharVerticalAlignment) -> fmt_dto::CharVerticalAlignment {
182 match v {
183 crate::CharVerticalAlignment::Normal => fmt_dto::CharVerticalAlignment::Normal,
184 crate::CharVerticalAlignment::SuperScript => fmt_dto::CharVerticalAlignment::SuperScript,
185 crate::CharVerticalAlignment::SubScript => fmt_dto::CharVerticalAlignment::SubScript,
186 crate::CharVerticalAlignment::Middle => fmt_dto::CharVerticalAlignment::Middle,
187 crate::CharVerticalAlignment::Bottom => fmt_dto::CharVerticalAlignment::Bottom,
188 crate::CharVerticalAlignment::Top => fmt_dto::CharVerticalAlignment::Top,
189 crate::CharVerticalAlignment::Baseline => fmt_dto::CharVerticalAlignment::Baseline,
190 }
191}
192
193fn alignment_to_dto(v: &crate::Alignment) -> fmt_dto::Alignment {
194 match v {
195 crate::Alignment::Left => fmt_dto::Alignment::Left,
196 crate::Alignment::Right => fmt_dto::Alignment::Right,
197 crate::Alignment::Center => fmt_dto::Alignment::Center,
198 crate::Alignment::Justify => fmt_dto::Alignment::Justify,
199 }
200}
201
202fn direction_to_dto(v: &crate::TextDirection) -> fmt_dto::TextDirection {
203 match v {
204 crate::TextDirection::LeftToRight => fmt_dto::TextDirection::LeftToRight,
205 crate::TextDirection::RightToLeft => fmt_dto::TextDirection::RightToLeft,
206 }
207}
208
209fn list_style_to_dto(v: &crate::ListStyle) -> fmt_dto::ListStyle {
210 match v {
211 crate::ListStyle::Disc => fmt_dto::ListStyle::Disc,
212 crate::ListStyle::Circle => fmt_dto::ListStyle::Circle,
213 crate::ListStyle::Square => fmt_dto::ListStyle::Square,
214 crate::ListStyle::Decimal => fmt_dto::ListStyle::Decimal,
215 crate::ListStyle::LowerAlpha => fmt_dto::ListStyle::LowerAlpha,
216 crate::ListStyle::UpperAlpha => fmt_dto::ListStyle::UpperAlpha,
217 crate::ListStyle::LowerRoman => fmt_dto::ListStyle::LowerRoman,
218 crate::ListStyle::UpperRoman => fmt_dto::ListStyle::UpperRoman,
219 }
220}
221
222fn marker_to_dto(v: &crate::MarkerType) -> fmt_dto::MarkerType {
223 match v {
224 crate::MarkerType::NoMarker => fmt_dto::MarkerType::NoMarker,
225 crate::MarkerType::Unchecked => fmt_dto::MarkerType::Unchecked,
226 crate::MarkerType::Checked => fmt_dto::MarkerType::Checked,
227 }
228}
229
230impl TextFormat {
236 pub(crate) fn to_set_dto(
237 &self,
238 position: usize,
239 anchor: usize,
240 ) -> frontend::document_formatting::SetTextFormatDto {
241 frontend::document_formatting::SetTextFormatDto {
242 position: to_i64(position),
243 anchor: to_i64(anchor),
244 font_family: self.font_family.clone(),
245 font_point_size: self.font_point_size.map(|v| v as i64),
246 font_weight: self.font_weight.map(|v| v as i64),
247 font_bold: self.font_bold,
248 font_italic: self.font_italic,
249 font_underline: self.font_underline,
250 font_overline: self.font_overline,
251 font_strikeout: self.font_strikeout,
252 letter_spacing: self.letter_spacing.map(|v| v as i64),
253 word_spacing: self.word_spacing.map(|v| v as i64),
254 underline_style: self.underline_style.as_ref().map(underline_style_to_dto),
255 vertical_alignment: self
256 .vertical_alignment
257 .as_ref()
258 .map(vertical_alignment_to_dto),
259 }
260 }
261
262 pub(crate) fn to_merge_dto(
263 &self,
264 position: usize,
265 anchor: usize,
266 ) -> frontend::document_formatting::MergeTextFormatDto {
267 frontend::document_formatting::MergeTextFormatDto {
268 position: to_i64(position),
269 anchor: to_i64(anchor),
270 font_family: self.font_family.clone(),
271 font_bold: self.font_bold,
272 font_italic: self.font_italic,
273 font_underline: self.font_underline,
274 font_strikeout: self.font_strikeout,
275 vertical_alignment: self
276 .vertical_alignment
277 .as_ref()
278 .map(vertical_alignment_to_dto),
279 }
280 }
281}
282
283impl From<&frontend::common::format_runs::CharacterFormat> for TextFormat {
286 fn from(fmt: &frontend::common::format_runs::CharacterFormat) -> Self {
287 Self {
288 font_family: fmt.font_family.clone(),
289 font_point_size: fmt.font_point_size.map(|v| v as u32),
290 font_weight: fmt.font_weight.map(|v| v as u32),
291 font_bold: fmt.font_bold,
292 font_italic: fmt.font_italic,
293 font_underline: fmt.font_underline,
294 font_overline: fmt.font_overline,
295 font_strikeout: fmt.font_strikeout,
296 letter_spacing: fmt.letter_spacing.map(|v| v as i32),
297 word_spacing: fmt.word_spacing.map(|v| v as i32),
298 underline_style: fmt.underline_style.clone(),
299 vertical_alignment: fmt.vertical_alignment.clone(),
300 anchor_href: fmt.anchor_href.clone(),
301 anchor_names: fmt.anchor_names.clone(),
302 is_anchor: fmt.is_anchor,
303 tooltip: fmt.tooltip.clone(),
304 foreground_color: None,
305 background_color: None,
306 underline_color: None,
307 }
308 }
309}
310
311impl BlockFormat {
314 pub(crate) fn to_set_dto(
315 &self,
316 position: usize,
317 anchor: usize,
318 ) -> frontend::document_formatting::SetBlockFormatDto {
319 frontend::document_formatting::SetBlockFormatDto {
320 position: to_i64(position),
321 anchor: to_i64(anchor),
322 alignment: self.alignment.as_ref().map(alignment_to_dto),
323 heading_level: self.heading_level.map(|v| v as i64),
324 indent: self.indent.map(|v| v as i64),
325 marker: self.marker.as_ref().map(marker_to_dto),
326 line_height: self.line_height.map(|v| (v * 1000.0) as i64),
327 non_breakable_lines: self.non_breakable_lines,
328 page_break_before: self.page_break_before,
329 direction: self.direction.as_ref().map(direction_to_dto),
330 clear_direction: self.clear_direction,
331 background_color: self.background_color.clone(),
332 is_code_block: self.is_code_block,
333 code_language: self.code_language.clone(),
334 hyphenate: self.hyphenate,
335 language: self.language.clone(),
336 top_margin: self.top_margin.map(|v| v as i64),
337 bottom_margin: self.bottom_margin.map(|v| v as i64),
338 left_margin: self.left_margin.map(|v| v as i64),
339 right_margin: self.right_margin.map(|v| v as i64),
340 text_indent: self.text_indent.map(|v| v as i64),
341 }
342 }
343}
344
345impl From<&frontend::block::dtos::BlockDto> for BlockFormat {
346 fn from(b: &frontend::block::dtos::BlockDto) -> Self {
347 Self {
348 alignment: b.fmt_alignment.clone(),
349 top_margin: b.fmt_top_margin.map(|v| v as i32),
350 bottom_margin: b.fmt_bottom_margin.map(|v| v as i32),
351 left_margin: b.fmt_left_margin.map(|v| v as i32),
352 right_margin: b.fmt_right_margin.map(|v| v as i32),
353 heading_level: b.fmt_heading_level.map(|v| v as u8),
354 indent: b.fmt_indent.map(|v| v as u8),
355 text_indent: b.fmt_text_indent.map(|v| v as i32),
356 marker: b.fmt_marker.clone(),
357 tab_positions: b.fmt_tab_positions.iter().map(|&v| v as i32).collect(),
358 line_height: b.fmt_line_height.map(|v| v as f32 / 1000.0),
359 non_breakable_lines: b.fmt_non_breakable_lines,
360 page_break_before: b.fmt_page_break_before,
361 direction: b.fmt_direction.clone(),
362 clear_direction: false,
364 background_color: b.fmt_background_color.clone(),
365 is_code_block: b.fmt_is_code_block,
366 code_language: b.fmt_code_language.clone(),
367 hyphenate: b.fmt_hyphenate,
368 language: b.fmt_language.clone(),
369 }
370 }
371}
372
373impl FrameFormat {
376 pub(crate) fn to_set_dto(
377 &self,
378 position: usize,
379 anchor: usize,
380 frame_id: usize,
381 ) -> frontend::document_formatting::SetFrameFormatDto {
382 frontend::document_formatting::SetFrameFormatDto {
383 position: to_i64(position),
384 anchor: to_i64(anchor),
385 frame_id: to_i64(frame_id),
386 height: self.height.map(|v| v as i64),
387 width: self.width.map(|v| v as i64),
388 top_margin: self.top_margin.map(|v| v as i64),
389 bottom_margin: self.bottom_margin.map(|v| v as i64),
390 left_margin: self.left_margin.map(|v| v as i64),
391 right_margin: self.right_margin.map(|v| v as i64),
392 padding: self.padding.map(|v| v as i64),
393 border: self.border.map(|v| v as i64),
394 is_blockquote: self.is_blockquote,
395 }
396 }
397}
398
399impl ListFormat {
402 pub(crate) fn to_set_dto(
403 &self,
404 list_id: usize,
405 ) -> frontend::document_formatting::SetListFormatDto {
406 frontend::document_formatting::SetListFormatDto {
407 list_id: to_i64(list_id),
408 style: self.style.as_ref().map(list_style_to_dto),
409 indent: self.indent.map(|v| v as i64),
410 prefix: self.prefix.clone(),
411 suffix: self.suffix.clone(),
412 }
413 }
414}
415
416impl crate::flow::TableFormat {
419 pub(crate) fn to_set_dto(
420 &self,
421 table_id: usize,
422 ) -> frontend::document_formatting::SetTableFormatDto {
423 frontend::document_formatting::SetTableFormatDto {
424 table_id: to_i64(table_id),
425 border: self.border.map(|v| v as i64),
426 cell_spacing: self.cell_spacing.map(|v| v as i64),
427 cell_padding: self.cell_padding.map(|v| v as i64),
428 width: self.width.map(|v| v as i64),
429 alignment: self.alignment.as_ref().map(alignment_to_dto),
430 }
431 }
432}
433
434fn cell_vertical_alignment_to_dto(
437 v: &crate::flow::CellVerticalAlignment,
438) -> fmt_dto::CellVerticalAlignment {
439 match v {
440 crate::flow::CellVerticalAlignment::Top => fmt_dto::CellVerticalAlignment::Top,
441 crate::flow::CellVerticalAlignment::Middle => fmt_dto::CellVerticalAlignment::Middle,
442 crate::flow::CellVerticalAlignment::Bottom => fmt_dto::CellVerticalAlignment::Bottom,
443 }
444}
445
446impl crate::flow::CellFormat {
447 pub(crate) fn to_set_dto(
448 &self,
449 cell_id: usize,
450 ) -> frontend::document_formatting::SetTableCellFormatDto {
451 frontend::document_formatting::SetTableCellFormatDto {
452 cell_id: to_i64(cell_id),
453 padding: self.padding.map(|v| v as i64),
454 border: self.border.map(|v| v as i64),
455 vertical_alignment: self
456 .vertical_alignment
457 .as_ref()
458 .map(cell_vertical_alignment_to_dto),
459 background_color: self.background_color.clone(),
460 }
461 }
462}