mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 22:37:12 +00:00
Merge branch 'ppt95-text' into ppt95-shapes
# Conflicts: # crates/ppt-binary/src/ppt95/tests.rs
This commit is contained in:
@@ -572,7 +572,7 @@ pub fn convert(document: &Rec, app: &dyn Defaults) -> Presentation {
|
||||
if let Some(kind) = o.ph.as_ref().map(|ph| ph.kind).filter(|k| !placed.iter().any(|d| same_family(*d, *k))) {
|
||||
placed.push(kind);
|
||||
match kind {
|
||||
PhType::Title => title_ruler = o.ruler,
|
||||
PhType::Title | PhType::CtrTitle => title_ruler = o.ruler,
|
||||
PhType::Body => body_ruler = o.ruler,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -799,6 +799,25 @@ fn the_masters_text_styles_are_read() {
|
||||
assert_eq!(defaults(&cut), defaults(&plain_master));
|
||||
}
|
||||
|
||||
/// A master whose title placeholder is a centred title (PowerPoint 95 type 0x0F) gives the title
|
||||
/// style its ruler's margins, as a plain title does.
|
||||
#[test]
|
||||
fn a_masters_centred_title_gives_the_title_style_its_ruler() {
|
||||
let title = placeholder_95(
|
||||
0x0F,
|
||||
0,
|
||||
0,
|
||||
[-2448, -1776, 2448, -1056],
|
||||
Some(text(b"T", &[plain(2)], &[para_run(2, false, 0, 1, 0)], 3, [0; 4], MASTER_BODY)),
|
||||
);
|
||||
let atoms = [style_atom([LEVEL; 5], 0x13)];
|
||||
let stream =
|
||||
stream_with(document_atom(5760, 4320, 0), &[], &[master_with(MASTER, DEFAULT, [0; 4], &[title])], environment_with(&[styles(&atoms)]));
|
||||
let t = import(&stream).unwrap().masters[0].title_style.level(0).cloned().unwrap();
|
||||
// The wide ruler (slot 3): level 1 at 576 units, its first line at 288.
|
||||
assert_eq!((t.para.margin_left, t.para.indent), (Some(72.0), Some(-36.0)));
|
||||
}
|
||||
|
||||
/// A shape with text and no format of its own is a text box with no fill or line; one without text
|
||||
/// draws nothing and is left out; a placeholder of a type PowerPoint 95 isn't known to write keeps
|
||||
/// its text as a text box.
|
||||
@@ -876,6 +895,22 @@ fn text_is_windows_1252() {
|
||||
|
||||
/// Damaged text never panics and stays bounded: runs past the text, huge run lengths and counts,
|
||||
/// truncated entries, control characters, empty text, and a character atom past the cap.
|
||||
/// Paragraphs and runs are about 4 and 2 KB each, so the document's text makes at most
|
||||
/// [`super::text::MAX_TEXT_ITEMS`] of them together, across all its shapes; text past that is
|
||||
/// left out.
|
||||
#[test]
|
||||
fn text_makes_a_bounded_number_of_paragraphs_and_runs() {
|
||||
let bx = [0, 0, 1000, 1000];
|
||||
// 40,000 one-letter paragraphs, a paragraph and a run each: 80,000 items in one shape.
|
||||
let many = [b'a', 13].repeat(40_000);
|
||||
let p = deck_of(vec![shape_95(bx, Some(simple_text(&many, bx, BODY))), shape_95(bx, Some(simple_text(b"after", bx, BODY)))], &[]);
|
||||
let items: usize = p.slides[0].shapes.iter().filter_map(|s| s.text.as_ref()).flat_map(|t| &t.paragraphs).map(|para| 1 + para.runs.len()).sum();
|
||||
assert_eq!(items, super::text::MAX_TEXT_ITEMS);
|
||||
let first = body_of(&p.slides[0].shapes[0]);
|
||||
assert_eq!((first.paragraphs.len(), first.paragraphs[0].runs[0].text.as_str()), (super::text::MAX_TEXT_ITEMS / 2, "a"));
|
||||
assert!(!texts(&p).iter().any(|t| t == "after"), "the budget is the document's, not each shape's");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_text_never_panics() {
|
||||
let bx = [0, 0, 1000, 1000];
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
//! character formatting (24 bytes from +16) and five of paragraph formatting (52 bytes from
|
||||
//! +160), laid out as in the runs. Slide text carries its formatting in full, as copies of these.
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
use craft_pptx::text::{Align, Anchor, BodyProps, Bullet, LevelStyle, ListStyle, ParaProps, RunKind, Spacing};
|
||||
use craft_pptx::{ColorRef, Fill, Paragraph, Rgba, Run, RunProps, TextBody};
|
||||
|
||||
@@ -71,6 +73,10 @@ pub(super) const OTHER: usize = 4;
|
||||
/// Characters read from one text at most: far beyond what PowerPoint 95 puts in a shape, so a
|
||||
/// huge character atom can't make a huge text body.
|
||||
pub(super) const MAX_CHARS: usize = 1 << 20;
|
||||
/// Paragraphs and runs made from the whole document's text at most. Each is about 2 to 4 KB, so
|
||||
/// a text of nothing but paragraph or line breaks would otherwise make gigabytes from a megabyte
|
||||
/// of file. That is far more than any PowerPoint 95 file probed holds.
|
||||
pub(super) const MAX_TEXT_ITEMS: usize = 1 << 16;
|
||||
/// Font and ruler slots read at most.
|
||||
const MAX_SLOTS: usize = 4096;
|
||||
/// Indent levels: PowerPoint 95 has five.
|
||||
@@ -86,9 +92,22 @@ pub(super) struct Env {
|
||||
rulers: Vec<Option<Ruler>>,
|
||||
/// The master's text styles by text type: each level's character and paragraph formatting.
|
||||
styles: Vec<Option<[(CharRun, ParaRun); LEVELS]>>,
|
||||
/// Paragraphs and runs made so far, against [`MAX_TEXT_ITEMS`].
|
||||
items: Cell<usize>,
|
||||
}
|
||||
|
||||
impl Env {
|
||||
/// Counts one more paragraph or run; `false` once the document's text has made
|
||||
/// [`MAX_TEXT_ITEMS`], when the rest of its text is left out.
|
||||
fn take_item(&self) -> bool {
|
||||
let n = self.items.get();
|
||||
if n == MAX_TEXT_ITEMS {
|
||||
log::warn!("ppt: PowerPoint 95 text passes {MAX_TEXT_ITEMS} paragraphs and runs; the rest is left out");
|
||||
}
|
||||
self.items.set(n.saturating_add(1));
|
||||
n < MAX_TEXT_ITEMS
|
||||
}
|
||||
|
||||
pub(super) fn new(document: &Rec) -> Env {
|
||||
let Some(environment) = document.child(rt::ENVIRONMENT) else { return Env::default() };
|
||||
// There may be more than one font list; the first that holds fonts is the table.
|
||||
@@ -104,7 +123,7 @@ impl Env {
|
||||
.child(rt::STYLES)
|
||||
.map(|list| list.children().filter(|r| r.kind == rt::STYLE).take(STYLE_TYPES).map(|r| style(&r)).collect())
|
||||
.unwrap_or_default();
|
||||
Env { fonts, rulers, styles }
|
||||
Env { fonts, rulers, styles, items: Cell::new(0) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,12 +348,18 @@ fn paragraphs(chars: &[char], char_runs: &[CharRun], para_runs: &[ParaRun], rule
|
||||
let mut out = vec![];
|
||||
let mut start = 0usize;
|
||||
loop {
|
||||
if !env.take_item() {
|
||||
break;
|
||||
}
|
||||
let end = chars.get(start..).and_then(|rest| rest.iter().position(|&c| c == '\r')).map_or(total, |e| start + e);
|
||||
let pr = run_at(¶_starts, start).and_then(|i| para_runs.get(i));
|
||||
let level = pr.map_or(0, |r| r.level.min(LEVELS as u8 - 1));
|
||||
let mut para = Paragraph { level, props: para_props(pr, ruler.and_then(|r| r.get(usize::from(level)))), ..Default::default() };
|
||||
let mut i = start;
|
||||
while i < end {
|
||||
if !env.take_item() {
|
||||
break;
|
||||
}
|
||||
let props = props_at(i);
|
||||
if chars.get(i) == Some(&'\u{b}') {
|
||||
para.runs.push(Run { text: String::new(), props, kind: RunKind::Break });
|
||||
|
||||
Reference in New Issue
Block a user