1use std::fmt::Write as _;
17
18use crate::{
19 Color, Font, FontWeight, ListStyle, ParagraphAlign, ParagraphRun, ParagraphStyle, RunStyle,
20 StyledText, TextRun, Underline, coalesce_runs, paragraph_bounds,
21};
22
23#[derive(Clone, Copy, Debug, PartialEq)]
27pub struct DocStyle {
28 pub base: Font,
29 pub base_points: f64,
33}
34
35impl Default for DocStyle {
36 fn default() -> Self {
37 DocStyle {
38 base: Font::Body,
39 base_points: 16.0,
40 }
41 }
42}
43
44impl DocStyle {
45 #[must_use]
46 pub fn new(base: Font, base_points: f64) -> Self {
47 DocStyle { base, base_points }
48 }
49 fn plain(&self) -> RunStyle {
50 RunStyle::plain(self.base)
51 }
52}
53
54fn heading_scale(level: u8) -> f64 {
57 match level {
58 1 => 2.0,
59 2 => 1.5,
60 3 => 1.17,
61 4 => 1.0,
62 5 => 0.83,
63 _ => 0.67,
64 }
65}
66
67fn heading_level_for(scale: f64) -> Option<u8> {
74 if (scale - 1.0).abs() < 1e-6 {
75 return None;
76 }
77 (1u8..=6).find(|level| (heading_scale(*level) - scale).abs() < 1e-6)
78}
79
80#[must_use]
93pub fn markdown_to_styled(md: &str, style: DocStyle) -> StyledText {
94 let mut doc = StyledText::default();
95 let mut paragraphs: Vec<ParagraphRun> = Vec::new();
96 let mut ordinal = 1u32;
97 let mut prev_ordered = false;
98
99 for raw in md.lines() {
100 let start = doc.text.len();
101 let (body, para, heading) = split_block_markers(raw);
102
103 let para = if let ListStyle::Ordered(_) = para.list {
105 if !prev_ordered {
106 ordinal = 1;
107 }
108 prev_ordered = true;
109 let p = ParagraphStyle {
110 list: ListStyle::Ordered(ordinal),
111 ..para
112 };
113 ordinal += 1;
114 p
115 } else {
116 prev_ordered = false;
117 para
118 };
119
120 let (text, mut runs) = crate::markdown::parse(body, style.base);
121 if let Some(level) = heading {
123 let scale = heading_scale(level);
124 for r in &mut runs {
125 r.font.scale = scale;
126 if r.font.weight.is_none() {
127 r.font.weight = Some(FontWeight::Bold);
128 }
129 }
130 let mut head = RunStyle::plain(style.base);
131 head.font.scale = scale;
132 head.set_bold(true);
133 merge_base_run(&mut runs, text.len(), head);
134 }
135 for r in &mut runs {
136 r.range = (r.range.start + start)..(r.range.end + start);
137 }
138 doc.runs.extend(runs);
139 doc.text.push_str(&text);
140 doc.text.push('\n');
141 let end = doc.text.len();
142 if para != ParagraphStyle::default() {
143 paragraphs.push(ParagraphRun::new(start..end, para));
144 }
145 }
146 if !md.ends_with('\n') && doc.text.ends_with('\n') {
148 doc.text.pop();
149 doc.clamp();
150 }
151 doc.paragraphs = paragraphs;
152 doc.runs = coalesce_runs(std::mem::take(&mut doc.runs), style.base);
153 doc.clamp();
154 doc
155}
156
157fn split_block_markers(line: &str) -> (&str, ParagraphStyle, Option<u8>) {
160 let mut s = line;
161 let mut para = ParagraphStyle::default();
162 while let Some(rest) = s.strip_prefix('>') {
164 para.indent += 24.0;
165 s = rest.strip_prefix(' ').unwrap_or(rest);
166 }
167 let indented = s.len() - s.trim_start_matches(' ').len();
170 let trimmed = s.trim_start_matches(' ');
171 if let Some(rest) = trimmed
172 .strip_prefix("- ")
173 .or_else(|| trimmed.strip_prefix("* "))
174 {
175 para.list = ListStyle::Bullet;
176 para.list_level = (indented / 2).min(u8::MAX as usize) as u8;
177 return (rest, para, None);
178 }
179 if let Some((num, rest)) = split_ordered_marker(trimmed) {
180 para.list = ListStyle::Ordered(num);
181 para.list_level = (indented / 2).min(u8::MAX as usize) as u8;
182 return (rest, para, None);
183 }
184 let hashes = s.bytes().take_while(|b| *b == b'#').count();
186 if (1..=6).contains(&hashes)
187 && let Some(rest) = s[hashes..].strip_prefix(' ')
188 {
189 return (rest, para, Some(hashes as u8));
190 }
191 (s, para, None)
192}
193
194fn split_ordered_marker(s: &str) -> Option<(u32, &str)> {
196 let digits = s.bytes().take_while(u8::is_ascii_digit).count();
197 if digits == 0 || digits > 9 {
198 return None;
199 }
200 let rest = s[digits..].strip_prefix(". ")?;
201 s[..digits].parse().ok().map(|n| (n, rest))
202}
203
204fn merge_base_run(runs: &mut Vec<TextRun>, len: usize, style: RunStyle) {
206 let mut out = Vec::with_capacity(runs.len() * 2 + 1);
207 let mut at = 0usize;
208 for r in runs.iter() {
209 if r.range.start > at {
210 out.push(TextRun::styled(at..r.range.start, style.clone()));
211 }
212 out.push(r.clone());
213 at = r.range.end;
214 }
215 if at < len {
216 out.push(TextRun::styled(at..len, style));
217 }
218 *runs = out;
219}
220
221#[must_use]
226pub fn styled_to_markdown(doc: &StyledText, style: DocStyle) -> String {
227 let mut out = String::with_capacity(doc.text.len() + 32);
228 for (start, end) in paragraph_bounds(&doc.text) {
229 let line_end = end - usize::from(doc.text[start..end].ends_with('\n'));
230 let para = doc
231 .paragraphs
232 .iter()
233 .find(|p| p.range.start <= start && p.range.end >= line_end.max(start + 1))
234 .map_or_default(super::styled::ParagraphRun::style);
235 let heading = doc
237 .style_of(start..line_end.max(start), style.base)
238 .font
239 .scale;
240 let heading = if line_end > start {
241 heading_level_for(heading)
242 } else {
243 None
244 };
245 for _ in 0..(para.indent / 24.0).round().max(0.0) as usize {
246 out.push_str("> ");
247 }
248 for _ in 0..para.list_level {
249 out.push_str(" ");
250 }
251 match para.list {
252 ListStyle::Bullet => out.push_str("- "),
253 ListStyle::Ordered(n) => {
254 let _ = write!(out, "{n}. ");
255 }
256 ListStyle::None => {}
257 }
258 if let Some(level) = heading {
259 for _ in 0..level {
260 out.push('#');
261 }
262 out.push(' ');
263 }
264 write_markdown_inline(doc, start..line_end, style, heading.is_some(), &mut out);
265 if end < doc.text.len() || doc.text[start..end].ends_with('\n') {
266 out.push('\n');
267 }
268 }
269 out
270}
271
272fn write_markdown_inline(
273 doc: &StyledText,
274 range: std::ops::Range<usize>,
275 style: DocStyle,
276 in_heading: bool,
277 out: &mut String,
278) {
279 for (seg, s) in doc.segments(style.base) {
280 let lo = seg.start.max(range.start);
281 let hi = seg.end.min(range.end);
282 if lo >= hi {
283 continue;
284 }
285 let Some(text) = doc.text.get(lo..hi) else {
286 continue;
287 };
288 let bold = s.bold() && !in_heading;
290 let (open, close) = markdown_markers(&s, bold);
291 if let Some(url) = &s.link {
292 out.push('[');
293 out.push_str(&open);
294 out.push_str(text);
295 out.push_str(&close);
296 out.push_str("](");
297 out.push_str(url);
298 out.push(')');
299 continue;
300 }
301 out.push_str(&open);
302 out.push_str(&escape_markdown(text));
303 out.push_str(&close);
304 }
305}
306
307fn markdown_markers(s: &RunStyle, bold: bool) -> (String, String) {
308 let mut open = String::new();
309 let mut close = String::new();
310 if bold {
311 open.push_str("**");
312 close.insert_str(0, "**");
313 }
314 if s.font.italic {
315 open.push('*');
316 close.insert(0, '*');
317 }
318 if s.strikethrough {
319 open.push_str("~~");
320 close.insert_str(0, "~~");
321 }
322 if s.font.monospace {
323 open.push('`');
324 close.insert(0, '`');
325 }
326 (open, close)
327}
328
329fn escape_markdown(s: &str) -> String {
330 let mut out = String::with_capacity(s.len());
331 for c in s.chars() {
332 if matches!(c, '*' | '_' | '`' | '~' | '[' | ']' | '\\') {
333 out.push('\\');
334 }
335 out.push(c);
336 }
337 out
338}
339
340#[must_use]
347pub fn styled_to_html(doc: &StyledText, style: DocStyle) -> String {
348 let mut out = String::with_capacity(doc.text.len() * 2);
349 for (start, end) in paragraph_bounds(&doc.text) {
350 let line_end = end - usize::from(doc.text[start..end].ends_with('\n'));
351 let para = doc
352 .paragraphs
353 .iter()
354 .find(|p| p.range.start <= start && p.range.end >= line_end.max(start + 1))
355 .map_or_default(super::styled::ParagraphRun::style);
356 let (open_tag, close_tag) = match para.list {
357 ListStyle::None => ("p", "p"),
358 _ => ("li", "li"),
359 };
360 out.push('<');
361 out.push_str(open_tag);
362 let mut css = String::new();
363 match para.align {
364 ParagraphAlign::Natural => {}
365 ParagraphAlign::Center => css.push_str("text-align:center;"),
366 ParagraphAlign::Trailing => css.push_str("text-align:end;"),
367 ParagraphAlign::Justified => css.push_str("text-align:justify;"),
368 }
369 let indent = para.indent + f64::from(para.list_level) * 24.0;
370 if indent > 0.0 {
371 let _ = write!(css, "margin-inline-start:{indent}px;");
372 }
373 if para.space_before > 0.0 {
374 let _ = write!(css, "margin-block-start:{}px;", para.space_before);
375 }
376 if para.space_after > 0.0 {
377 let _ = write!(css, "margin-block-end:{}px;", para.space_after);
378 }
379 if !css.is_empty() {
380 let _ = write!(out, " style=\"{css}\"");
381 }
382 out.push('>');
383 for (seg, s) in doc.segments(style.base) {
384 let lo = seg.start.max(start);
385 let hi = seg.end.min(line_end);
386 if lo >= hi {
387 continue;
388 }
389 let Some(text) = doc.text.get(lo..hi) else {
390 continue;
391 };
392 write_html_run(&s, text, &mut out);
393 }
394 out.push_str("</");
395 out.push_str(close_tag);
396 out.push_str(">\n");
397 }
398 out
399}
400
401#[expect(
402 clippy::float_cmp,
403 reason = "a scale of exactly 1.0 is the unscaled default, compared as a sentinel"
404)]
405fn write_html_run(s: &RunStyle, text: &str, out: &mut String) {
406 let mut css = String::new();
407 if let Some(c) = s.color {
408 let _ = write!(css, "color:{};", css_hex(c));
409 }
410 if let Some(c) = s.background {
411 let _ = write!(css, "background-color:{};", css_hex(c));
412 }
413 if s.font.scale != 1.0 {
414 let _ = write!(css, "font-size:{}em;", s.font.scale);
415 }
416 if let Font::System(pt) | Font::Custom(_, pt) = s.font.style {
417 let _ = write!(css, "font-size:{pt}px;");
418 }
419 let mut tags: Vec<&str> = Vec::new();
420 if let Some(url) = &s.link {
421 out.push_str("<a href=\"");
422 escape_html(url, out);
423 out.push_str("\">");
424 }
425 if !css.is_empty() {
426 let _ = write!(out, "<span style=\"{css}\">");
427 }
428 if s.bold() {
429 tags.push("b");
430 }
431 if s.font.italic {
432 tags.push("i");
433 }
434 if s.underline.is_on() {
435 tags.push("u");
436 }
437 if s.strikethrough {
438 tags.push("s");
439 }
440 if s.font.monospace {
441 tags.push("code");
442 }
443 for t in &tags {
444 out.push('<');
445 out.push_str(t);
446 out.push('>');
447 }
448 escape_html(text, out);
449 for t in tags.iter().rev() {
450 out.push_str("</");
451 out.push_str(t);
452 out.push('>');
453 }
454 if !css.is_empty() {
455 out.push_str("</span>");
456 }
457 if s.link.is_some() {
458 out.push_str("</a>");
459 }
460}
461
462fn css_hex(c: Color) -> String {
463 let q = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
464 format!("#{:02x}{:02x}{:02x}", q(c.r), q(c.g), q(c.b))
465}
466
467fn escape_html(s: &str, out: &mut String) {
468 for c in s.chars() {
469 match c {
470 '&' => out.push_str("&"),
471 '<' => out.push_str("<"),
472 '>' => out.push_str(">"),
473 '"' => out.push_str("""),
474 _ => out.push(c),
475 }
476 }
477}
478
479#[must_use]
485pub fn html_to_styled(html: &str, style: DocStyle) -> StyledText {
486 let mut p = HtmlParser {
487 src: html.as_bytes(),
488 at: 0,
489 doc: StyledText::default(),
490 stack: vec![style.plain()],
491 para_stack: vec![ParagraphStyle::default()],
492 style,
493 para_start: 0,
494 list_ordinal: Vec::new(),
495 };
496 p.run();
497 p.finish()
498}
499
500struct HtmlParser<'a> {
501 src: &'a [u8],
502 at: usize,
503 doc: StyledText,
504 stack: Vec<RunStyle>,
505 para_stack: Vec<ParagraphStyle>,
506 style: DocStyle,
507 para_start: usize,
508 list_ordinal: Vec<u32>,
509}
510
511impl HtmlParser<'_> {
512 fn run(&mut self) {
513 let mut text_start = 0usize;
514 while self.at < self.src.len() {
515 if self.src[self.at] == b'<' {
516 self.flush(text_start);
517 self.tag();
518 text_start = self.doc.text.len();
519 } else {
520 let c = self.next_char();
521 self.push_char(c);
522 }
523 }
524 self.flush(text_start);
525 }
526
527 fn next_char(&mut self) -> char {
528 if self.src[self.at] == b'&'
530 && let Some(end) = self.src[self.at..].iter().take(12).position(|b| *b == b';')
531 {
532 let ent = std::str::from_utf8(&self.src[self.at + 1..self.at + end]).unwrap_or("");
533 let decoded = match ent {
534 "amp" => Some('&'),
535 "lt" => Some('<'),
536 "gt" => Some('>'),
537 "quot" => Some('"'),
538 "apos" | "#39" => Some('\''),
539 "nbsp" => Some('\u{a0}'),
540 e => e
541 .strip_prefix('#')
542 .and_then(|n| {
543 n.strip_prefix('x')
544 .and_then(|h| u32::from_str_radix(h, 16).ok())
545 .or_else(|| n.parse().ok())
546 })
547 .and_then(char::from_u32),
548 };
549 if let Some(c) = decoded {
550 self.at += end + 1;
551 return c;
552 }
553 }
554 let rest = &self.src[self.at..];
555 let s = std::str::from_utf8(rest).unwrap_or("\u{fffd}");
556 let c = s.chars().next().unwrap_or('\u{fffd}');
557 self.at += c.len_utf8().min(rest.len());
558 c
559 }
560
561 fn push_char(&mut self, c: char) {
562 if c.is_whitespace() {
565 if self.doc.text.ends_with(' ') || self.doc.text.ends_with('\n') {
566 return;
567 }
568 self.doc.text.push(' ');
569 return;
570 }
571 self.doc.text.push(c);
572 }
573
574 fn flush(&mut self, from: usize) {
576 let end = self.doc.text.len();
577 let style = self
578 .stack
579 .last()
580 .cloned()
581 .unwrap_or_else(|| self.style.plain());
582 if end > from && style != self.style.plain() {
583 self.doc.runs.push(TextRun::styled(from..end, style));
584 }
585 }
586
587 fn tag(&mut self) {
588 let Some(close) = self.src[self.at..].iter().position(|b| *b == b'>') else {
589 self.at = self.src.len();
590 return;
591 };
592 let raw = std::str::from_utf8(&self.src[self.at + 1..self.at + close]).unwrap_or("");
593 self.at += close + 1;
594 let closing = raw.starts_with('/');
595 let body = raw.trim_start_matches('/').trim_end_matches('/');
596 let name: String = body
597 .split([' ', '\t', '\n'])
598 .next()
599 .unwrap_or("")
600 .to_ascii_lowercase();
601 if !closing && matches!(name.as_str(), "script" | "style") {
603 let needle = format!("</{name}");
604 if let Some(end) = find_ci(&self.src[self.at..], needle.as_bytes()) {
605 self.at += end;
606 } else {
607 self.at = self.src.len();
608 }
609 return;
610 }
611 if name == "br" {
612 self.end_paragraph();
613 return;
614 }
615 if closing {
616 self.close(&name);
617 } else {
618 self.open(&name, body);
619 }
620 }
621
622 fn open(&mut self, name: &str, body: &str) {
623 if is_block(name) {
624 self.end_paragraph();
625 let mut para = ParagraphStyle::default();
626 if let Some(level) = heading_tag_level(name) {
627 let mut s = self.style.plain();
628 s.font.scale = heading_scale(level);
629 s.set_bold(true);
630 self.stack.push(s);
631 self.para_stack.push(para);
632 return;
633 }
634 if name == "li" {
635 let ordered = self.list_ordinal.last().copied();
636 para.list = match ordered {
637 Some(n) => ListStyle::Ordered(n),
638 None => ListStyle::Bullet,
639 };
640 para.list_level = self.list_ordinal.len().saturating_sub(1).min(255) as u8;
641 if let Some(n) = self.list_ordinal.last_mut() {
642 *n += 1;
643 }
644 }
645 if name == "ul" {
646 self.list_ordinal.push(0);
647 if let Some(last) = self.list_ordinal.last_mut() {
649 *last = 0;
650 }
651 self.para_stack.push(para);
652 self.stack.push(self.current_run());
653 return;
654 }
655 if name == "ol" {
656 self.list_ordinal.push(1);
657 self.para_stack.push(para);
658 self.stack.push(self.current_run());
659 return;
660 }
661 if name == "blockquote" {
662 para.indent += 24.0;
663 }
664 apply_block_css(&mut para, body);
665 self.para_stack.push(para);
666 self.stack.push(self.current_run());
667 return;
668 }
669 let mut s = self.current_run();
670 match name {
671 "b" | "strong" => s.set_bold(true),
672 "i" | "em" => s.set_italic(true),
673 "u" | "ins" => s.underline = Underline::Single,
674 "s" | "del" | "strike" => s.strikethrough = true,
675 "code" | "kbd" | "samp" | "tt" => s.font.monospace = true,
676 "a" => s.link = attr(body, "href").map(str::to_string),
677 _ => {}
678 }
679 apply_inline_css(&mut s, body, self.style);
680 self.stack.push(s);
681 }
682
683 fn close(&mut self, name: &str) {
684 if is_block(name) {
685 self.end_paragraph();
686 if self.para_stack.len() > 1 {
687 self.para_stack.pop();
688 }
689 if matches!(name, "ul" | "ol") {
690 self.list_ordinal.pop();
691 }
692 }
693 if self.stack.len() > 1 {
694 self.stack.pop();
695 }
696 }
697
698 fn current_run(&self) -> RunStyle {
699 self.stack
700 .last()
701 .cloned()
702 .unwrap_or_else(|| self.style.plain())
703 }
704
705 fn end_paragraph(&mut self) {
707 while self.doc.text.ends_with(' ') {
709 self.doc.text.pop();
710 }
711 if self.doc.text.len() <= self.para_start {
712 return;
713 }
714 self.doc.text.push('\n');
715 let style = self.para_stack.last().copied().unwrap_or_default();
716 if style != ParagraphStyle::default() {
717 self.doc.paragraphs.push(ParagraphRun::new(
718 self.para_start..self.doc.text.len(),
719 style,
720 ));
721 }
722 self.para_start = self.doc.text.len();
723 }
724
725 fn finish(mut self) -> StyledText {
726 self.end_paragraph();
727 if self.doc.text.ends_with('\n') {
729 self.doc.text.pop();
730 }
731 let base = self.style.base;
732 self.doc.runs = coalesce_runs(std::mem::take(&mut self.doc.runs), base);
733 self.doc.clamp();
734 self.doc
735 }
736}
737
738fn is_block(name: &str) -> bool {
739 matches!(
740 name,
741 "p" | "div"
742 | "li"
743 | "ul"
744 | "ol"
745 | "blockquote"
746 | "pre"
747 | "h1"
748 | "h2"
749 | "h3"
750 | "h4"
751 | "h5"
752 | "h6"
753 )
754}
755
756fn heading_tag_level(name: &str) -> Option<u8> {
757 let n = name.strip_prefix('h')?;
758 let level: u8 = n.parse().ok()?;
759 (1..=6).contains(&level).then_some(level)
760}
761
762fn attr<'a>(body: &'a str, name: &str) -> Option<&'a str> {
764 let lower = body.to_ascii_lowercase();
765 let at = lower.find(&format!("{name}="))? + name.len() + 1;
766 let rest = &body[at..];
767 let quote = rest.chars().next()?;
768 if quote == '"' || quote == '\'' {
769 rest[1..].find(quote).map(|e| &rest[1..=e])
770 } else {
771 Some(rest.split([' ', '\t']).next().unwrap_or(rest))
772 }
773}
774
775fn apply_inline_css(s: &mut RunStyle, body: &str, doc: DocStyle) {
776 let Some(css) = attr(body, "style") else {
777 return;
778 };
779 for decl in css.split(';') {
780 let Some((k, v)) = decl.split_once(':') else {
781 continue;
782 };
783 let (k, v) = (k.trim().to_ascii_lowercase(), v.trim());
784 match k.as_str() {
785 "color" => s.color = parse_css_color(v),
786 "background-color" | "background" => s.background = parse_css_color(v),
787 "font-weight" => {
788 let bold = v == "bold" || v.parse::<u32>().is_ok_and(|n| n >= 600);
789 s.set_bold(bold);
790 }
791 "font-style" => s.set_italic(v == "italic" || v == "oblique"),
792 "text-decoration" | "text-decoration-line" => {
793 if v.contains("underline") {
794 s.underline = Underline::Single;
795 }
796 if v.contains("line-through") {
797 s.strikethrough = true;
798 }
799 }
800 "font-family" => s.font.monospace = v.contains("monospace") || v.contains("Courier"),
801 "font-size" => {
802 if let Some(em) = v
803 .strip_suffix("em")
804 .and_then(|n| n.trim().parse::<f64>().ok())
805 {
806 s.font.scale = em;
807 } else if let Some(px) = v
808 .strip_suffix("px")
809 .and_then(|n| n.trim().parse::<f64>().ok())
810 {
811 s.font.style = Font::System(px);
814 s.font.scale = 1.0;
815 } else if let Some(pct) = v
816 .strip_suffix('%')
817 .and_then(|n| n.trim().parse::<f64>().ok())
818 {
819 s.font.scale = pct / 100.0;
820 } else if let Some(pt) = v
821 .strip_suffix("pt")
822 .and_then(|n| n.trim().parse::<f64>().ok())
823 {
824 s.font.style = Font::System(pt * 96.0 / 72.0);
825 s.font.scale = 1.0;
826 }
827 let _ = doc;
828 }
829 _ => {}
830 }
831 }
832}
833
834fn apply_block_css(para: &mut ParagraphStyle, body: &str) {
835 let Some(css) = attr(body, "style") else {
836 return;
837 };
838 for decl in css.split(';') {
839 let Some((k, v)) = decl.split_once(':') else {
840 continue;
841 };
842 let (k, v) = (k.trim().to_ascii_lowercase(), v.trim());
843 let px = |v: &str| v.trim_end_matches("px").trim().parse::<f64>().ok();
844 match k.as_str() {
845 "text-align" => {
846 para.align = match v {
847 "center" => ParagraphAlign::Center,
848 "right" | "end" => ParagraphAlign::Trailing,
849 "justify" => ParagraphAlign::Justified,
850 _ => ParagraphAlign::Natural,
851 }
852 }
853 "margin-inline-start" | "margin-left" | "padding-inline-start" => {
854 para.indent = px(v).unwrap_or(para.indent);
855 }
856 "margin-block-start" | "margin-top" => {
857 para.space_before = px(v).unwrap_or(para.space_before);
858 }
859 "margin-block-end" | "margin-bottom" => {
860 para.space_after = px(v).unwrap_or(para.space_after);
861 }
862 _ => {}
863 }
864 }
865}
866
867fn parse_css_color(v: &str) -> Option<Color> {
869 let v = v.trim();
870 if let Some(rest) = v.strip_prefix("rgb(").and_then(|r| r.strip_suffix(')')) {
871 let mut it = rest.split(',').map(|n| n.trim().parse::<f64>().ok());
872 return Some(Color::rgb(
873 it.next()?? / 255.0,
874 it.next()?? / 255.0,
875 it.next()?? / 255.0,
876 ));
877 }
878 Color::parse(v)
879}
880
881fn find_ci(hay: &[u8], needle: &[u8]) -> Option<usize> {
883 hay.windows(needle.len())
884 .position(|w| w.eq_ignore_ascii_case(needle))
885}
886
887#[must_use]
894pub fn styled_to_rtf(doc: &StyledText, style: DocStyle) -> String {
895 let mut colors: Vec<Color> = Vec::new();
898 let color_index = |c: Color, colors: &mut Vec<Color>| -> usize {
899 if let Some(i) = colors.iter().position(|x| *x == c) {
900 i + 1
901 } else {
902 colors.push(c);
903 colors.len()
904 }
905 };
906 let mut body = String::new();
907 for (start, end) in paragraph_bounds(&doc.text) {
908 let line_end = end - usize::from(doc.text[start..end].ends_with('\n'));
909 let para = doc
910 .paragraphs
911 .iter()
912 .find(|p| p.range.start <= start && p.range.end >= line_end.max(start + 1))
913 .map_or_default(super::styled::ParagraphRun::style);
914 body.push_str("\\pard");
915 match para.align {
916 ParagraphAlign::Natural => body.push_str("\\ql"),
917 ParagraphAlign::Center => body.push_str("\\qc"),
918 ParagraphAlign::Trailing => body.push_str("\\qr"),
919 ParagraphAlign::Justified => body.push_str("\\qj"),
920 }
921 let indent = para.indent + f64::from(para.list_level) * 24.0;
922 if indent > 0.0 {
923 let _ = write!(body, "\\li{}", (indent * 20.0).round() as i64);
925 }
926 if para.space_before > 0.0 {
927 let _ = write!(body, "\\sb{}", (para.space_before * 20.0).round() as i64);
928 }
929 if para.space_after > 0.0 {
930 let _ = write!(body, "\\sa{}", (para.space_after * 20.0).round() as i64);
931 }
932 match para.list {
933 ListStyle::Bullet => body.push_str("\\bullet\\tab "),
934 ListStyle::Ordered(n) => {
935 let _ = write!(body, " {n}.\\tab ");
936 }
937 ListStyle::None => body.push(' '),
940 }
941 for (seg, s) in doc.segments(style.base) {
942 let lo = seg.start.max(start);
943 let hi = seg.end.min(line_end);
944 if lo >= hi {
945 continue;
946 }
947 let Some(text) = doc.text.get(lo..hi) else {
948 continue;
949 };
950 body.push('{');
951 let attrs_at = body.len();
955 if s.bold() {
956 body.push_str("\\b");
957 }
958 if s.font.italic {
959 body.push_str("\\i");
960 }
961 match s.underline {
962 Underline::None => {}
963 Underline::Double => body.push_str("\\uldb"),
964 Underline::Dotted => body.push_str("\\uld"),
965 Underline::Wavy => body.push_str("\\ulwave"),
966 Underline::Single => body.push_str("\\ul"),
967 }
968 if s.strikethrough {
969 body.push_str("\\strike");
970 }
971 let pts = s.font.resolved_points(style.base_points);
973 if (pts - style.base_points).abs() > 0.01 {
974 let _ = write!(body, "\\fs{}", (pts * 2.0).round() as i64);
975 }
976 if let Some(c) = s.color {
977 let _ = write!(body, "\\cf{}", color_index(c, &mut colors));
978 }
979 if let Some(c) = s.background {
980 let _ = write!(body, "\\highlight{}", color_index(c, &mut colors));
981 }
982 if s.font.monospace {
983 body.push_str("\\f1");
984 }
985 if body.len() > attrs_at {
986 body.push(' ');
987 }
988 escape_rtf(text, &mut body);
989 body.push('}');
990 }
991 body.push_str("\\par\n");
992 }
993
994 let mut out = String::with_capacity(body.len() + 256);
995 out.push_str("{\\rtf1\\ansi\\deff0");
996 out.push_str("{\\fonttbl{\\f0 ");
997 out.push_str(rtf_family(style.base));
998 out.push_str(";}{\\f1 Courier New;}}");
999 out.push_str("{\\colortbl;");
1000 for c in &colors {
1001 let q = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
1002 let _ = write!(out, "\\red{}\\green{}\\blue{};", q(c.r), q(c.g), q(c.b));
1003 }
1004 out.push('}');
1005 let _ = writeln!(out, "\\fs{}", (style.base_points * 2.0).round() as i64);
1006 out.push_str(&body);
1007 out.push('}');
1008 out
1009}
1010
1011fn rtf_family(base: Font) -> &'static str {
1012 match base {
1013 Font::Custom(name, _) => {
1014 name
1018 }
1019 _ => "Helvetica",
1020 }
1021}
1022
1023fn escape_rtf(s: &str, out: &mut String) {
1024 for c in s.chars() {
1025 match c {
1026 '\\' | '{' | '}' => {
1027 out.push('\\');
1028 out.push(c);
1029 }
1030 '\n' => out.push_str("\\line "),
1031 c if (c as u32) < 128 => out.push(c),
1032 c => {
1036 let mut buf = [0u16; 2];
1037 for unit in c.encode_utf16(&mut buf) {
1038 let _ = write!(out, "\\u{}?", *unit as i16);
1039 }
1040 }
1041 }
1042 }
1043}
1044
1045#[must_use]
1054pub fn rtf_to_styled(rtf: &str, style: DocStyle) -> StyledText {
1055 let mut p = RtfParser {
1056 src: rtf.as_bytes(),
1057 at: 0,
1058 doc: StyledText::default(),
1059 stack: vec![RtfState::new(style)],
1060 colors: Vec::new(),
1061 mono_fonts: Vec::new(),
1062 style,
1063 run_start: 0,
1064 para_start: 0,
1065 para: ParagraphStyle::default(),
1066 pending_utf16: Vec::new(),
1067 };
1068 p.run();
1069 p.finish()
1070}
1071
1072#[derive(Clone)]
1073struct RtfState {
1074 run: RunStyle,
1075 skip: bool,
1077}
1078
1079impl RtfState {
1080 fn new(style: DocStyle) -> Self {
1081 RtfState {
1082 run: style.plain(),
1083 skip: false,
1084 }
1085 }
1086}
1087
1088struct RtfParser<'a> {
1089 src: &'a [u8],
1090 at: usize,
1091 doc: StyledText,
1092 stack: Vec<RtfState>,
1093 colors: Vec<Color>,
1094 mono_fonts: Vec<u32>,
1095 style: DocStyle,
1096 run_start: usize,
1097 para_start: usize,
1098 para: ParagraphStyle,
1099 pending_utf16: Vec<u16>,
1100}
1101
1102impl RtfParser<'_> {
1103 fn run(&mut self) {
1104 while self.at < self.src.len() {
1105 match self.src[self.at] {
1106 b'{' => {
1107 self.at += 1;
1108 let top = self
1109 .stack
1110 .last()
1111 .cloned()
1112 .unwrap_or(RtfState::new(self.style));
1113 self.stack.push(top);
1114 }
1115 b'}' => {
1116 self.at += 1;
1117 self.flush_run();
1118 if self.stack.len() > 1 {
1119 self.stack.pop();
1120 }
1121 }
1122 b'\\' => self.control(),
1123 b'\r' | b'\n' => self.at += 1, _ => {
1125 let b = self.src[self.at];
1126 self.at += 1;
1127 self.push_byte(b);
1128 }
1129 }
1130 }
1131 }
1132
1133 fn state(&self) -> RtfState {
1134 self.stack
1135 .last()
1136 .cloned()
1137 .unwrap_or_else(|| RtfState::new(self.style))
1138 }
1139
1140 fn skipping(&self) -> bool {
1141 self.stack.last().is_some_and(|s| s.skip)
1142 }
1143
1144 fn push_byte(&mut self, b: u8) {
1145 self.flush_utf16();
1146 if self.skipping() {
1147 return;
1148 }
1149 self.doc.text.push(b as char);
1150 }
1151
1152 fn push_char(&mut self, c: char) {
1153 if self.skipping() {
1154 return;
1155 }
1156 self.doc.text.push(c);
1157 }
1158
1159 fn push_utf16(&mut self, unit: u16) {
1161 if self.skipping() {
1162 return;
1163 }
1164 self.pending_utf16.push(unit);
1165 let decoded: String = String::from_utf16_lossy(&self.pending_utf16);
1166 if !decoded.contains('\u{fffd}') || self.pending_utf16.len() >= 2 {
1169 self.doc.text.push_str(&decoded);
1170 self.pending_utf16.clear();
1171 }
1172 }
1173
1174 fn flush_utf16(&mut self) {
1175 if !self.pending_utf16.is_empty() {
1176 let s = String::from_utf16_lossy(&self.pending_utf16);
1177 self.pending_utf16.clear();
1178 if !self.skipping() {
1179 self.doc.text.push_str(&s);
1180 }
1181 }
1182 }
1183
1184 fn flush_run(&mut self) {
1186 self.flush_utf16();
1187 let end = self.doc.text.len();
1188 let run = self.state().run;
1189 if end > self.run_start && run != self.style.plain() {
1190 self.doc
1191 .runs
1192 .push(TextRun::styled(self.run_start..end, run));
1193 }
1194 self.run_start = end;
1195 }
1196
1197 fn skip_group(&mut self) {
1200 let mut depth = 1i32;
1201 while self.at < self.src.len() && depth > 0 {
1202 match self.src[self.at] {
1203 b'{' => depth += 1,
1204 b'}' => depth -= 1,
1205 b'\\' => self.at += 1,
1207 _ => {}
1208 }
1209 self.at += 1;
1210 }
1211 if self.stack.len() > 1 {
1212 self.stack.pop();
1213 }
1214 self.run_start = self.doc.text.len();
1215 }
1216
1217 fn control(&mut self) {
1218 self.at += 1;
1219 if self.at >= self.src.len() {
1220 return;
1221 }
1222 let b = self.src[self.at];
1223 if b == b'\'' {
1226 let hex = self
1227 .src
1228 .get(self.at + 1..self.at + 3)
1229 .and_then(|h| std::str::from_utf8(h).ok())
1230 .and_then(|h| u8::from_str_radix(h, 16).ok());
1231 self.at += 3.min(self.src.len() - self.at);
1232 if let Some(v) = hex {
1233 self.flush_utf16();
1234 self.push_char(v as char);
1235 }
1236 return;
1237 }
1238 if b == b'*' {
1243 self.at += 1;
1244 if let Some(st) = self.stack.last_mut() {
1245 st.skip = true;
1246 }
1247 return;
1248 }
1249 if !b.is_ascii_alphabetic() {
1250 self.at += 1;
1252 self.flush_utf16();
1253 self.push_char(b as char);
1254 return;
1255 }
1256 let word_start = self.at;
1257 while self.at < self.src.len() && self.src[self.at].is_ascii_alphabetic() {
1258 self.at += 1;
1259 }
1260 let word = std::str::from_utf8(&self.src[word_start..self.at])
1261 .unwrap_or("")
1262 .to_string();
1263 let num_start = self.at;
1265 if self.at < self.src.len() && (self.src[self.at] == b'-') {
1266 self.at += 1;
1267 }
1268 while self.at < self.src.len() && self.src[self.at].is_ascii_digit() {
1269 self.at += 1;
1270 }
1271 let param: Option<i64> = std::str::from_utf8(&self.src[num_start..self.at])
1272 .ok()
1273 .and_then(|s| s.parse().ok());
1274 if self.at < self.src.len() && self.src[self.at] == b' ' {
1275 self.at += 1;
1276 }
1277 self.word(&word, param);
1278 }
1279
1280 fn word(&mut self, word: &str, param: Option<i64>) {
1281 let styling = matches!(
1283 word,
1284 "b" | "i"
1285 | "ul"
1286 | "ulnone"
1287 | "uldb"
1288 | "uld"
1289 | "ulwave"
1290 | "strike"
1291 | "striked"
1292 | "fs"
1293 | "cf"
1294 | "highlight"
1295 | "f"
1296 | "plain"
1297 );
1298 if styling {
1299 self.flush_run();
1300 }
1301 let on = param != Some(0);
1302 let st = self.stack.last_mut();
1303 let Some(st) = st else { return };
1304 match word {
1305 "fonttbl" | "colortbl" | "stylesheet" | "info" | "generator" | "pict" | "object"
1307 | "listtable" | "listoverridetable" => st.skip = true,
1308 "b" => st.run.set_bold(on),
1309 "i" => st.run.set_italic(on),
1310 "ul" => {
1311 st.run.underline = if on {
1312 Underline::Single
1313 } else {
1314 Underline::None
1315 }
1316 }
1317 "ulnone" => st.run.underline = Underline::None,
1318 "uldb" => st.run.underline = Underline::Double,
1319 "uld" => st.run.underline = Underline::Dotted,
1320 "ulwave" => st.run.underline = Underline::Wavy,
1321 "strike" | "striked" => st.run.strikethrough = on,
1322 "plain" => st.run = self.style.plain(),
1323 "fs" => {
1324 if let Some(half) = param {
1325 let pts = half as f64 / 2.0;
1326 if (pts - self.style.base_points).abs() < 0.01 {
1327 st.run.font.style = self.style.base;
1328 st.run.font.scale = 1.0;
1329 } else {
1330 st.run.font.style = Font::System(pts);
1331 st.run.font.scale = 1.0;
1332 }
1333 }
1334 }
1335 "cf" => st.run.color = color_at(&self.colors, param),
1336 "highlight" => st.run.background = color_at(&self.colors, param),
1337 "f" => {
1338 st.run.font.monospace =
1339 param.is_some_and(|n| self.mono_fonts.contains(&(n as u32)));
1340 }
1341 "par" => {
1342 self.flush_run();
1343 self.push_char('\n');
1344 self.run_start = self.doc.text.len();
1345 self.close_paragraph();
1346 }
1347 "line" => {
1348 self.flush_run();
1349 self.push_char('\n');
1350 self.run_start = self.doc.text.len();
1351 }
1352 "tab" => {
1353 self.push_char('\t');
1354 }
1355 "bullet" => self.push_char('\u{2022}'),
1356 "emdash" => self.push_char('—'),
1357 "endash" => self.push_char('–'),
1358 "lquote" => self.push_char('\u{2018}'),
1359 "rquote" => self.push_char('\u{2019}'),
1360 "ldblquote" => self.push_char('\u{201c}'),
1361 "rdblquote" => self.push_char('\u{201d}'),
1362 "u" => {
1363 if let Some(n) = param {
1364 self.push_utf16(n as i16 as u16);
1365 if self.at < self.src.len() && self.src[self.at] == b'?' {
1367 self.at += 1;
1368 }
1369 }
1370 }
1371 "pard" => self.para = ParagraphStyle::default(),
1372 "ql" => self.para.align = ParagraphAlign::Natural,
1373 "qc" => self.para.align = ParagraphAlign::Center,
1374 "qr" => self.para.align = ParagraphAlign::Trailing,
1375 "qj" => self.para.align = ParagraphAlign::Justified,
1376 "li" => self.para.indent = param.unwrap_or(0) as f64 / 20.0,
1377 "sb" => self.para.space_before = param.unwrap_or(0) as f64 / 20.0,
1378 "sa" => self.para.space_after = param.unwrap_or(0) as f64 / 20.0,
1379 _ => {}
1380 }
1381 if word == "colortbl" {
1385 self.read_color_table();
1386 self.skip_group();
1387 }
1388 if word == "fonttbl" {
1389 self.read_font_table();
1390 self.skip_group();
1391 }
1392 }
1393
1394 fn read_color_table(&mut self) {
1397 let mut rgb = [0u8; 3];
1398 let mut seen = false;
1399 while self.at < self.src.len() && self.src[self.at] != b'}' {
1400 if self.src[self.at] == b'\\' {
1401 let start = self.at + 1;
1402 let mut e = start;
1403 while e < self.src.len() && self.src[e].is_ascii_alphabetic() {
1404 e += 1;
1405 }
1406 let word = std::str::from_utf8(&self.src[start..e]).unwrap_or("");
1407 let ns = e;
1408 while e < self.src.len() && self.src[e].is_ascii_digit() {
1409 e += 1;
1410 }
1411 let n: u8 = std::str::from_utf8(&self.src[ns..e])
1412 .ok()
1413 .and_then(|s| s.parse().ok())
1414 .unwrap_or(0);
1415 match word {
1416 "red" => {
1417 rgb[0] = n;
1418 seen = true;
1419 }
1420 "green" => {
1421 rgb[1] = n;
1422 seen = true;
1423 }
1424 "blue" => {
1425 rgb[2] = n;
1426 seen = true;
1427 }
1428 _ => {}
1429 }
1430 self.at = e;
1431 continue;
1432 }
1433 if self.src[self.at] == b';' {
1434 self.colors.push(if seen {
1435 Color::rgb(
1436 f64::from(rgb[0]) / 255.0,
1437 f64::from(rgb[1]) / 255.0,
1438 f64::from(rgb[2]) / 255.0,
1439 )
1440 } else {
1441 Color::BLACK
1442 });
1443 rgb = [0u8; 3];
1444 seen = false;
1445 }
1446 self.at += 1;
1447 }
1448 }
1449
1450 fn read_font_table(&mut self) {
1454 let mut depth = 1i32;
1455 let mut current: Option<u32> = None;
1456 let mut name = String::new();
1457 let from = self.at;
1458 let mut at = self.at;
1459 while at < self.src.len() && depth > 0 {
1460 match self.src[at] {
1461 b'{' => {
1462 depth += 1;
1463 at += 1;
1464 }
1465 b'}' | b';' => {
1466 if self.src[at] == b'}' {
1467 depth -= 1;
1468 }
1469 if let Some(i) = current.take()
1470 && is_mono_family(&name)
1471 {
1472 self.mono_fonts.push(i);
1473 }
1474 name.clear();
1475 at += 1;
1476 }
1477 b'\\' => {
1478 let s = at + 1;
1479 let mut e = s;
1480 while e < self.src.len() && self.src[e].is_ascii_alphabetic() {
1481 e += 1;
1482 }
1483 let word = std::str::from_utf8(&self.src[s..e]).unwrap_or("");
1484 let ns = e;
1485 while e < self.src.len() && self.src[e].is_ascii_digit() {
1486 e += 1;
1487 }
1488 let n: Option<u32> = std::str::from_utf8(&self.src[ns..e])
1489 .ok()
1490 .and_then(|s| s.parse().ok());
1491 match word {
1492 "f" => current = n,
1493 "fmodern" => {
1496 if let Some(i) = current {
1497 self.mono_fonts.push(i);
1498 }
1499 }
1500 _ => {}
1501 }
1502 at = e;
1503 if at < self.src.len() && self.src[at] == b' ' {
1504 at += 1;
1505 }
1506 }
1507 b => {
1508 name.push(b as char);
1509 at += 1;
1510 }
1511 }
1512 }
1513 self.at = from;
1515 }
1516
1517 fn close_paragraph(&mut self) {
1518 if self.doc.text.len() <= self.para_start {
1519 self.para_start = self.doc.text.len();
1520 return;
1521 }
1522 if self.para != ParagraphStyle::default() {
1523 self.doc.paragraphs.push(ParagraphRun::new(
1524 self.para_start..self.doc.text.len(),
1525 self.para,
1526 ));
1527 }
1528 self.para_start = self.doc.text.len();
1529 }
1530
1531 fn finish(mut self) -> StyledText {
1532 self.flush_run();
1533 self.close_paragraph();
1534 if self.doc.text.ends_with('\n') {
1536 self.doc.text.pop();
1537 }
1538 let base = self.style.base;
1539 self.doc.runs = coalesce_runs(std::mem::take(&mut self.doc.runs), base);
1540 self.doc.clamp();
1541 self.doc
1542 }
1543}
1544
1545fn color_at(colors: &[Color], param: Option<i64>) -> Option<Color> {
1546 let i = param?;
1550 if i <= 0 {
1551 return None;
1552 }
1553 colors.get(i as usize).copied()
1554}
1555
1556fn is_mono_family(name: &str) -> bool {
1557 let n = name.to_ascii_lowercase();
1558 ["courier", "consolas", "mono", "menlo", "monaco"]
1559 .iter()
1560 .any(|m| n.contains(m))
1561}
1562
1563#[cfg(test)]
1564mod tests {
1565 use super::*;
1566
1567 fn style() -> DocStyle {
1568 DocStyle::new(Font::Body, 16.0)
1569 }
1570
1571 fn plain_doc(t: &str) -> StyledText {
1572 StyledText::plain(t)
1573 }
1574
1575 #[test]
1576 fn markdown_inline_round_trips() {
1577 let md = "Save **now** or *lose* it.";
1578 let doc = markdown_to_styled(md, style());
1579 assert_eq!(doc.text, "Save now or lose it.");
1580 assert!(doc.validate().is_ok());
1581 assert_eq!(styled_to_markdown(&doc, style()), md);
1582 }
1583
1584 #[test]
1585 fn markdown_headings_become_scaled_bold_runs() {
1586 let doc = markdown_to_styled("# Title\nbody", style());
1587 assert_eq!(doc.text, "Title\nbody");
1588 let s = doc.style_of(0..5, Font::Body);
1589 assert!(s.bold());
1590 assert_eq!(s.font.scale, 2.0);
1591 assert!(!doc.style_of(6..10, Font::Body).bold());
1592 assert_eq!(styled_to_markdown(&doc, style()), "# Title\nbody");
1593 }
1594
1595 #[test]
1596 fn markdown_lists_become_paragraph_runs() {
1597 let doc = markdown_to_styled("- one\n- two\n1. a\n2. b", style());
1598 assert_eq!(doc.text, "one\ntwo\na\nb");
1599 let kinds: Vec<_> = doc.paragraphs.iter().map(|p| p.list).collect();
1600 assert_eq!(
1601 kinds,
1602 vec![
1603 ListStyle::Bullet,
1604 ListStyle::Bullet,
1605 ListStyle::Ordered(1),
1606 ListStyle::Ordered(2)
1607 ]
1608 );
1609 assert_eq!(
1610 styled_to_markdown(&doc, style()),
1611 "- one\n- two\n1. a\n2. b"
1612 );
1613 }
1614
1615 #[test]
1616 fn markdown_quotes_indent() {
1617 let doc = markdown_to_styled("> quoted", style());
1618 assert_eq!(doc.text, "quoted");
1619 assert_eq!(doc.paragraphs[0].indent, 24.0);
1620 assert_eq!(styled_to_markdown(&doc, style()), "> quoted");
1621 }
1622
1623 #[test]
1624 fn markdown_escapes_what_would_reparse() {
1625 let doc = plain_doc("2 * 3 * 4 and _x_");
1626 let md = styled_to_markdown(&doc, style());
1627 assert_eq!(markdown_to_styled(&md, style()).text, doc.text);
1628 }
1629
1630 #[test]
1631 fn html_round_trips_the_attributes_it_can_write() {
1632 let mut doc = StyledText::plain("hello world");
1633 doc.apply(0..5, Font::Body, |s| {
1634 s.set_bold(true);
1635 s.underline = Underline::Single;
1636 s.color = Some(Color::hex(0x00ff_0000));
1637 });
1638 doc.apply(6..11, Font::Body, |s| {
1639 s.strikethrough = true;
1640 s.background = Some(Color::hex(0x0000_ff00));
1641 });
1642 let html = styled_to_html(&doc, style());
1643 let back = html_to_styled(&html, style());
1644 assert_eq!(back.text, "hello world");
1645 let a = back.style_of(0..5, Font::Body);
1646 assert!(a.bold() && a.underline.is_on());
1647 assert_eq!(a.color, Some(Color::hex(0x00ff_0000)));
1648 let b = back.style_of(6..11, Font::Body);
1649 assert!(b.strikethrough);
1650 assert_eq!(b.background, Some(Color::hex(0x0000_ff00)));
1651 }
1652
1653 #[test]
1654 fn html_paragraphs_and_headings() {
1655 let doc = html_to_styled(
1656 "<h2>Title</h2><p style=\"text-align:center\">mid</p>",
1657 style(),
1658 );
1659 assert_eq!(doc.text, "Title\nmid");
1660 assert_eq!(doc.style_of(0..5, Font::Body).font.scale, 1.5);
1661 assert_eq!(doc.paragraph_style_of(6..9).align, ParagraphAlign::Center);
1662 }
1663
1664 #[test]
1665 fn html_lists_number_themselves() {
1666 let doc = html_to_styled("<ol><li>a</li><li>b</li></ol>", style());
1667 assert_eq!(doc.text, "a\nb");
1668 assert_eq!(doc.paragraphs[0].list, ListStyle::Ordered(1));
1669 assert_eq!(doc.paragraphs[1].list, ListStyle::Ordered(2));
1670 }
1671
1672 #[test]
1673 fn html_ignores_scripts_and_unknown_tags() {
1674 let doc = html_to_styled(
1675 "<p>a<script>var x = '<b>';</script><weird>b</weird></p>",
1676 style(),
1677 );
1678 assert_eq!(doc.text, "ab", "script contents are not text");
1679 }
1680
1681 #[test]
1682 fn html_entities_and_multibyte_survive() {
1683 let doc = html_to_styled("<p>a & b é caf\u{e9} \u{1f600}</p>", style());
1684 assert_eq!(doc.text, "a & b é café 😀");
1685 assert!(doc.validate().is_ok());
1686 }
1687
1688 #[test]
1689 fn rtf_round_trips_every_attribute_day_writes() {
1690 let mut doc = StyledText::plain("bold italic under strike big red");
1691 doc.apply(0..4, Font::Body, |s| s.set_bold(true));
1692 doc.apply(5..11, Font::Body, |s| s.set_italic(true));
1693 doc.apply(12..17, Font::Body, |s| s.underline = Underline::Single);
1694 doc.apply(18..24, Font::Body, |s| s.strikethrough = true);
1695 doc.apply(25..28, Font::Body, |s| s.font.scale = 1.5);
1696 doc.apply(29..32, Font::Body, |s| {
1697 s.color = Some(Color::hex(0x00cc_0000));
1698 });
1699
1700 let rtf = styled_to_rtf(&doc, style());
1701 let back = rtf_to_styled(&rtf, style());
1702 assert_eq!(back.text, doc.text);
1703 assert!(back.style_of(0..4, Font::Body).bold());
1704 assert!(back.style_of(5..11, Font::Body).font.italic);
1705 assert!(back.style_of(12..17, Font::Body).underline.is_on());
1706 assert!(back.style_of(18..24, Font::Body).strikethrough);
1707 assert_eq!(
1708 back.style_of(29..32, Font::Body).color,
1709 Some(Color::hex(0x00cc_0000))
1710 );
1711 let big = back.style_of(25..28, Font::Body);
1713 assert_eq!(big.font.resolved_points(16.0), 24.0);
1714 }
1715
1716 #[test]
1717 fn rtf_paragraph_attributes_round_trip() {
1718 let mut doc = StyledText::plain("one\ntwo");
1719 doc.apply_paragraph(0..1, |p| p.align = ParagraphAlign::Center);
1720 doc.apply_paragraph(5..5, |p| p.indent = 36.0);
1721 let back = rtf_to_styled(&styled_to_rtf(&doc, style()), style());
1722 assert_eq!(back.text, "one\ntwo");
1723 assert_eq!(back.paragraph_style_of(0..1).align, ParagraphAlign::Center);
1724 assert_eq!(back.paragraph_style_of(5..5).indent, 36.0);
1725 }
1726
1727 #[test]
1728 fn rtf_escapes_and_unicode_survive() {
1729 let doc = plain_doc("a{b}c\\d é 😀");
1730 let back = rtf_to_styled(&styled_to_rtf(&doc, style()), style());
1731 assert_eq!(back.text, "a{b}c\\d é 😀");
1732 }
1733
1734 #[test]
1735 fn rtf_skips_destinations_and_tables() {
1736 let rtf = "{\\rtf1\\ansi{\\fonttbl{\\f0\\fswiss Helvetica;}}{\\*\\expandedcolortbl;;}\
1739 {\\*\\generator Riched20;}\\fs32 hello\\par}";
1740 let doc = rtf_to_styled(rtf, style());
1741 assert_eq!(doc.text, "hello", "got {:?}", doc.text);
1742 }
1743
1744 #[test]
1745 fn every_codec_survives_hostile_input_without_panicking() {
1746 for bad in [
1747 "",
1748 "{",
1749 "}",
1750 "\\",
1751 "{\\rtf1\\ansi\\u",
1752 "{\\rtf1{{{{{{",
1753 "{\\rtf1\\'zz}",
1754 "{\\colortbl",
1755 "<",
1756 "<p",
1757 "<p style=",
1758 "<a href=",
1759 "&#",
1760 "&#xZZ;",
1761 "<script>",
1762 "**unclosed",
1763 "# ",
1764 "- ",
1765 "1. ",
1766 ] {
1767 let _ = rtf_to_styled(bad, style());
1768 let _ = html_to_styled(bad, style());
1769 let d = markdown_to_styled(bad, style());
1770 assert!(d.validate().is_ok(), "markdown {bad:?} produced bad ranges");
1771 }
1772 }
1773}