mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-10 21:11:52 +00:00
Setting a field's rotation (Properties > Position, form_set_props) swapped the widget's width and height around its center, so a tall field placed along a page edge jumped into a short, wide one, and typing in it showed the text upright. ISO 32000-2 Table 192 /MK /R turns the widget annotation relative to the page; its /Rect stays the area it occupies in default user space. So a rotation now changes only /MK /R: the rectangle stays, and the regenerated appearance lays the text out along the turned direction (the box /BBox is as wide as the field is tall, and /Matrix turns it onto /Rect). On screen, the in-place editor and the just-committed preview paint the text turned by /MK /R less the page's /Rotate and the view's rotation, as the page will show it. The editor still takes the keys and pointer over the field, and a press or drag places the caret in the turned text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0175FWj4xFhMhSH3UpMU8DSC
63 lines
3.0 KiB
Rust
63 lines
3.0 KiB
Rust
//! Rendering checks for form fields. They live here, not in pdfcraft-forms, because forms and
|
|
//! render are both layer 3 and may not depend on each other, not even for tests.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, write_incremental};
|
|
use pdfcraft_forms::{FieldProps, FieldValue, NewField, add_field, set_props, set_value};
|
|
use pdfcraft_render::{PageRenderer, RenderConfig, RenderRequest, RequestKind};
|
|
|
|
fn one_page() -> Document {
|
|
let mut doc = Document::new_empty();
|
|
let pages = doc.get(doc.root().unwrap()).as_dict().unwrap().reference(b"Pages").unwrap();
|
|
let mut p = Dict::new();
|
|
p.set(b"Type".to_vec(), Object::name("Page"));
|
|
p.set(b"Parent".to_vec(), Object::Ref(pages));
|
|
p.set(b"MediaBox".to_vec(), Object::Array(vec![0.into(), 0.into(), 600.into(), 800.into()]));
|
|
let r = doc.add(p);
|
|
doc.update_dict(pages, |d| {
|
|
d.set(b"Kids".to_vec(), Object::Array(vec![Object::Ref(r)]));
|
|
d.set(b"Count".to_vec(), Object::Int(1));
|
|
})
|
|
.unwrap();
|
|
doc
|
|
}
|
|
|
|
/// Width and height of the dark pixels on page 1: the black text, not the field's grey (0.55)
|
|
/// border, which would give the field's own outline.
|
|
fn ink_span(doc: &Document) -> (u32, u32) {
|
|
let bytes = write_incremental(doc, &SaveOptions::default()).unwrap();
|
|
let mut r = PageRenderer::new(Arc::new(bytes), RenderConfig::default());
|
|
let p = r.render(RenderRequest { page: 0, kind: RequestKind::Pixels, tile: None, scale: 2.0, tag: 0, preview: false });
|
|
assert!(p.error.is_none(), "{:?}", p.error);
|
|
let (mut x0, mut y0, mut x1, mut y1) = (u32::MAX, u32::MAX, 0, 0);
|
|
let mut ink = false;
|
|
for y in 0..p.height {
|
|
for x in 0..p.width {
|
|
let i = ((y * p.width + x) * 4) as usize;
|
|
if p.rgba[i] < 100 {
|
|
ink = true;
|
|
(x0, y0, x1, y1) = (x0.min(x), y0.min(y), x1.max(x), y1.max(y));
|
|
}
|
|
}
|
|
}
|
|
assert!(ink, "the field drew no text");
|
|
(x1 - x0, y1 - y0)
|
|
}
|
|
|
|
/// A tall, narrow field turned 90 degrees stays where it is and its text runs up its long side
|
|
/// (#4296), rather than the field turning into a short, wide one that cuts the text off.
|
|
#[test]
|
|
fn a_rotated_field_draws_along_the_vertical_edge() {
|
|
let mut doc = one_page();
|
|
add_field(&mut doc, 0, [10.0, 40.0, 30.0, 120.0], &NewField::Text { multiline: false }, Some("tall")).unwrap();
|
|
set_value(&mut doc, "tall", &FieldValue::Text("MMMMMM".into())).unwrap();
|
|
set_props(&mut doc, "tall", &FieldProps { font_size: Some(12.0), ..FieldProps::default() }).unwrap();
|
|
let (wide, tall) = ink_span(&doc);
|
|
assert!(wide > tall, "upright text is a horizontal run: {wide}x{tall}");
|
|
set_props(&mut doc, "tall", &FieldProps { rotation: Some((0, 90)), ..FieldProps::default() }).unwrap();
|
|
let (wide, tall) = ink_span(&doc);
|
|
// Six 12 pt M's are about 60 pt, 120 px at scale 2; the field is 20 pt (40 px) wide.
|
|
assert!(tall >= 100 && wide < 40, "the text does not run up the field: {wide}x{tall} px");
|
|
}
|