mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-25 21:19:50 +00:00
Merge pull request #6162 from tsw2k/fix/docx-frame-shape
fix(render): frame shapes draw with a hole; frame presets open as frames
This commit is contained in:
@@ -201,6 +201,8 @@ pub struct ShapeExtra {
|
||||
pub end_style: ArrowStyle,
|
||||
/// A rounded rectangle's corner radius (`roundRect`'s `adj`, ECMA-376 §20.1.10.56): in
|
||||
/// 100000ths of the shorter side, at most [`MAX_CORNER`]; `None` draws [`DEFAULT_CORNER`].
|
||||
/// A [`ShapeKind::Frame`]'s border width (`frame`'s `adj1`), in the same units; `None`
|
||||
/// draws [`DEFAULT_FRAME`].
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub corner: Option<u32>,
|
||||
/// The outline dash pattern; absent in older saved documents means solid.
|
||||
@@ -276,6 +278,16 @@ pub const MAX_CORNER: u32 = 50_000;
|
||||
/// pt and a 60 × 150 pt one of 10 pt.
|
||||
pub const DEFAULT_CORNER: f32 = 16_667.0 / 100_000.0;
|
||||
|
||||
/// A frame's border width without its own: `frame`'s default `adj1`, 12.5% of the shorter side.
|
||||
pub const DEFAULT_FRAME: f32 = 0.125;
|
||||
|
||||
/// The border width, in points, of a `w` × `h` [`ShapeKind::Frame`] with `corner` (see
|
||||
/// [`ShapeExtra::corner`]): at most half the shorter side, which leaves no hole.
|
||||
pub fn frame_width(corner: Option<u32>, w: f32, h: f32) -> f32 {
|
||||
let side = wordcraft_geom::finite(w).min(wordcraft_geom::finite(h)).max(0.0);
|
||||
side * corner.map_or(DEFAULT_FRAME, |c| c.min(MAX_CORNER) as f32 / 100_000.0)
|
||||
}
|
||||
|
||||
/// The corner radius, in points, of a `w` × `h` rounded rectangle with `corner` (see
|
||||
/// [`ShapeExtra::corner`]).
|
||||
pub fn corner_radius(corner: Option<u32>, w: f32, h: f32) -> f32 {
|
||||
|
||||
@@ -24,6 +24,9 @@ pub enum ShapeKind {
|
||||
ArrowDown,
|
||||
Star,
|
||||
Heart,
|
||||
/// A picture frame (DrawingML's `frame`): a rectangle with a rectangular hole, its border as
|
||||
/// wide as [`crate::connector::frame_width`] says.
|
||||
Frame,
|
||||
/// A text box (rectangle with a text story).
|
||||
TextBox,
|
||||
/// A shape drawn point by point, such as ink (its geometry is the shape's `freeform`).
|
||||
|
||||
@@ -70,6 +70,7 @@ pub fn shape_outline(kind: ShapeKind, w: f32, h: f32, corner: Option<u32>) -> Op
|
||||
| ShapeKind::TextBox
|
||||
| ShapeKind::Line
|
||||
| ShapeKind::Freeform
|
||||
| ShapeKind::Frame
|
||||
| ShapeKind::StraightConnector
|
||||
| ShapeKind::ElbowConnector
|
||||
| ShapeKind::CurvedConnector => return None,
|
||||
|
||||
@@ -94,15 +94,6 @@ fn object_value_from_shared(
|
||||
};
|
||||
Ok(Target::Graphic { w, h, alt, float: convert(float)?, graphic })
|
||||
}
|
||||
// This model has no picture frame shape: DrawingML's `frame` preset is read as a
|
||||
// rectangle, without the frame's border width.
|
||||
mut shape @ Source::Shape { kind: craft_wordmodel::para::ShapeKind::Frame, .. } => {
|
||||
if let Source::Shape { kind, extra, .. } = &mut shape {
|
||||
*kind = craft_wordmodel::para::ShapeKind::Rectangle;
|
||||
extra.corner = None;
|
||||
}
|
||||
convert(shape)
|
||||
}
|
||||
Source::Group { w, h, float, ch_w, ch_h, children, canvas } => Ok(Target::Group {
|
||||
w,
|
||||
h,
|
||||
@@ -478,9 +469,9 @@ mod tests {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A picture frame shape opens as a rectangle instead of failing the whole document.
|
||||
/// A picture frame shape opens as a frame and keeps its own border width.
|
||||
#[test]
|
||||
fn a_frame_shape_reads_as_a_rectangle() {
|
||||
fn a_frame_shape_keeps_its_kind_and_border_width() {
|
||||
let shape = |kind| craft_wordmodel::para::InlineObject::Shape {
|
||||
kind,
|
||||
w: 40.0,
|
||||
@@ -497,7 +488,7 @@ mod tests {
|
||||
let doc = from_shared_model(doc_with(vec![shape(craft_wordmodel::para::ShapeKind::Frame)])).expect("from_shared_model");
|
||||
let found = objects(&doc);
|
||||
let [wordcraft_doc::para::InlineObject::Shape { kind, w, extra, .. }] = found.as_slice() else { panic!("{found:?}") };
|
||||
assert_eq!((*kind, *w, extra.corner), (wordcraft_doc::para::ShapeKind::Rectangle, 40.0, None));
|
||||
assert_eq!((*kind, *w, extra.corner), (wordcraft_doc::para::ShapeKind::Frame, 40.0, Some(2604)));
|
||||
}
|
||||
|
||||
/// A chart's series formatting and axis settings survive the trip through this model, so
|
||||
|
||||
@@ -157,3 +157,26 @@ fn own_picture_fill_keeps_image() {
|
||||
assert_eq!((float.wrap, float.x, float.y), (Wrap::InFrontOfText, 72.0, 100.0));
|
||||
assert_eq!(doc.media[media].as_slice(), BLUE_PNG);
|
||||
}
|
||||
/// A shape drawn with DrawingML's `frame` preset opens as a frame with its own border width
|
||||
/// (`adj1`), and saving writes it back as one.
|
||||
#[test]
|
||||
fn frame_preset_opens_and_saves_as_a_frame() {
|
||||
let xml = INLINE_XML
|
||||
.replacen(
|
||||
r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:solidFill>"#,
|
||||
r#"<a:prstGeom prst="frame"><a:avLst><a:gd name="adj1" fmla="val 10000"/></a:avLst></a:prstGeom><a:solidFill>"#,
|
||||
1,
|
||||
)
|
||||
.replacen(r#"<wps:cNvSpPr txBox="1"/>"#, "<wps:cNvSpPr/>", 1);
|
||||
let start = xml.find("<wps:txbx>").unwrap();
|
||||
let end = xml.find("</wps:txbx>").unwrap() + "</wps:txbx>".len();
|
||||
let xml = format!("{}{}", &xml[..start], &xml[end..]);
|
||||
let check = |doc: &Document| {
|
||||
let objects = &doc.body[0].as_para().unwrap().objects;
|
||||
let [InlineObject::Shape { kind, w, h, extra, .. }] = objects.as_slice() else { panic!("{objects:?}") };
|
||||
assert_eq!((*kind, *w, *h, extra.corner), (ShapeKind::Frame, 180.0, 80.0, Some(10_000)));
|
||||
};
|
||||
let doc = wordcraft_docx::read(&package(&xml)).unwrap();
|
||||
check(&doc);
|
||||
check(&wordcraft_docx::read(&wordcraft_docx::write(&doc).unwrap()).unwrap());
|
||||
}
|
||||
|
||||
@@ -217,6 +217,15 @@ pub(crate) fn shape_path(kind: ShapeKind, r: &Rect, corner: Option<u32>) -> Opti
|
||||
pb.cubic_to(x1, y0, cx, y0, cx, y0 + h * 0.3);
|
||||
pb.close();
|
||||
}
|
||||
// The outer rectangle and the hole wound the other way: a hole under either fill rule.
|
||||
ShapeKind::Frame => {
|
||||
poly(&mut pb, &[(x0, y0), (x1, y0), (x1, y1), (x0, y1)]);
|
||||
let t = wordcraft_doc::connector::frame_width(corner, r.w, r.h);
|
||||
let (i0, j0, i1, j1) = (x0 + t, y0 + t, x1 - t, y1 - t);
|
||||
if i1 > i0 && j1 > j0 {
|
||||
poly(&mut pb, &[(i0, j0), (i0, j1), (i1, j1), (i1, j0)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
pb.finish()
|
||||
}
|
||||
|
||||
@@ -1252,6 +1252,21 @@ pub fn shape_path(kind: ShapeKind, r: kurbo::Rect, corner: Option<u32>) -> BezPa
|
||||
p.close_path();
|
||||
p
|
||||
}
|
||||
// The outer rectangle clockwise and the hole counter-clockwise: a hole under either
|
||||
// fill rule.
|
||||
ShapeKind::Frame => {
|
||||
let mut p = r.to_path(0.1);
|
||||
let t = f64::from(wordcraft_doc::connector::frame_width(corner, r.width() as f32, r.height() as f32));
|
||||
let i = r.inset(-t);
|
||||
if i.width() > 0.0 && i.height() > 0.0 {
|
||||
p.move_to((i.x0, i.y0));
|
||||
p.line_to((i.x0, i.y1));
|
||||
p.line_to((i.x1, i.y1));
|
||||
p.line_to((i.x1, i.y0));
|
||||
p.close_path();
|
||||
}
|
||||
p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -671,6 +671,96 @@ fn picture_brightness_contrast_transparency() {
|
||||
assert!(!top.iter().any(|p| p[0] == 100));
|
||||
}
|
||||
|
||||
/// A letterhead's page frame (a `frame` shape in the header) is drawn as a border with a hole,
|
||||
/// not a filled box over the whole page.
|
||||
#[test]
|
||||
fn header_frame_shape_has_a_hole() {
|
||||
use wordcraft_doc::fill::Fill;
|
||||
use wordcraft_doc::para::{Float, InlineObject, Wrap};
|
||||
let mut d = Document::from_text("body");
|
||||
let mut hp = wordcraft_doc::Paragraph::new();
|
||||
let frame = InlineObject::Shape {
|
||||
kind: ShapeKind::Frame,
|
||||
w: 200.0,
|
||||
h: 100.0,
|
||||
fill: Some(Fill::solid(Rgb::BLACK)),
|
||||
stroke: None,
|
||||
stroke_width: 0.0,
|
||||
float: Float { wrap: Wrap::BehindText, ..Default::default() },
|
||||
story: None,
|
||||
freeform: None,
|
||||
effects: Default::default(),
|
||||
// A border a tenth of the shorter side: 10 pt.
|
||||
extra: wordcraft_doc::connector::ShapeExtra { corner: Some(10_000), ..Default::default() },
|
||||
};
|
||||
hp.insert_object(0, frame, &Default::default()).unwrap();
|
||||
let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]);
|
||||
d.last_section.headers.default = Some(id);
|
||||
let l = layout(&d, &mut LayoutCache::new(), &LayoutOptions::default());
|
||||
let page = &l.pages[0];
|
||||
let rect = page
|
||||
.header
|
||||
.iter()
|
||||
.find_map(|it| if let wordcraft_layout::Placed::Shape { rect, .. } = it { Some(*rect) } else { None })
|
||||
.expect("the frame is placed");
|
||||
// Undimmed, as while the header is edited, so the border's own colour is read.
|
||||
let mut opts = RenderOptions::default();
|
||||
opts.display.dim_header = false;
|
||||
let img = render_page(&d, page, 1.0, &opts);
|
||||
let px = |x: f32, y: f32| img.pixel(x as u32, y as u32)[0];
|
||||
let mid_y = rect.y + rect.h / 2.0;
|
||||
assert_eq!(px(rect.x + 5.0, mid_y), 0, "the border is drawn");
|
||||
assert_eq!(px(rect.x + rect.w - 5.0, mid_y), 0, "both sides of it");
|
||||
assert_eq!(px(rect.x + rect.w / 2.0, mid_y), 255, "the middle is a hole");
|
||||
assert_eq!(px(rect.x + rect.w / 2.0, rect.y + 15.0), 255, "the hole starts past the border");
|
||||
}
|
||||
|
||||
/// A header's pictures are drawn in their place among its text, not before it: a shape in front
|
||||
/// of a shaded header paragraph stays in front of the shading, whether the header is dimmed or
|
||||
/// not. Dimmed, the black square fades to a grey, with none of the red shading under it.
|
||||
#[test]
|
||||
fn header_shape_in_front_of_shaded_paragraph_stays_in_front() {
|
||||
use wordcraft_doc::fill::Fill;
|
||||
use wordcraft_doc::para::{Float, InlineObject, Wrap};
|
||||
let mut d = Document::from_text("body");
|
||||
let mut hp = wordcraft_doc::Paragraph::with_text("Letterhead", Default::default());
|
||||
hp.props.shading = Some(Rgb(0xFF, 0x00, 0x00));
|
||||
let square = InlineObject::Shape {
|
||||
kind: ShapeKind::Rectangle,
|
||||
w: 40.0,
|
||||
h: 40.0,
|
||||
fill: Some(Fill::solid(Rgb::BLACK)),
|
||||
stroke: None,
|
||||
stroke_width: 0.0,
|
||||
float: Float { wrap: Wrap::InFrontOfText, ..Default::default() },
|
||||
story: None,
|
||||
freeform: None,
|
||||
effects: Default::default(),
|
||||
extra: Default::default(),
|
||||
};
|
||||
hp.insert_object(0, square, &Default::default()).unwrap();
|
||||
let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]);
|
||||
d.last_section.headers.default = Some(id);
|
||||
let l = layout(&d, &mut LayoutCache::new(), &LayoutOptions::default());
|
||||
let page = &l.pages[0];
|
||||
let rect = page
|
||||
.header
|
||||
.iter()
|
||||
.find_map(|it| if let wordcraft_layout::Placed::Shape { rect, .. } = it { Some(*rect) } else { None })
|
||||
.expect("the square is placed");
|
||||
for dim_header in [false, true] {
|
||||
let mut opts = RenderOptions::default();
|
||||
opts.display.dim_header = dim_header;
|
||||
let img = render_page(&d, page, 1.0, &opts);
|
||||
// Just inside the square's top edge, where the paragraph's shading lies too.
|
||||
let p = img.pixel((rect.x + rect.w / 2.0) as u32, (rect.y + 3.0) as u32);
|
||||
assert!(p[0] == p[1] && p[1] == p[2], "the square covers the shading (dimmed header: {dim_header}): {p:?}");
|
||||
if !dim_header {
|
||||
assert_eq!(&p[..3], &[0, 0, 0], "undimmed, the square is black");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #2892: a rule is stroked with flat ends, as the PDF export and Word draw them. Round caps
|
||||
/// used to stick half the width out past every end — a blob over the neighbouring border at
|
||||
/// each corner of a table with thick borders, while the exported PDF had square corners.
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user