Skip to content

Commit e674942

Browse files
ATARYS-LABclaude
andcommitted
PDF underlays: PDFATTACH and PDFADJUST, drawn on screen, saved in DXF and DWG
PDFATTACH puts pages of a PDF file under the drawing: the PDF Underlay dialog (pages, insertion point, scale, rotation, path type), the typed prompts, or the JSON form. Scale 1 is the page at its size on paper; a page with a measuring scale (/VP /Measure) is sized by it, as AutoCAD does. PDFADJUST sets fade, contrast, monochrome and the colour adjustment for a dark background. Pages are read and drawn with the craft apps' shared Hayro reader (the craft-libs revision craft-pdf uses since storytold#2029): drawn in the draw order, through layout viewports and inside blocks, again at the screen's resolution when zoomed in or out so thin lines stay, with their clip boundary and their PDF layers as stored. Fonts a PDF names without embedding them are found among the installed fonts. PDFUNDERLAY and PDFDEFINITION are read and written in DXF and DWG as AutoCAD writes them: placement, adjustments, clip boundary, display flags, PDF layer overrides and unloaded definitions. The page size is read from the PDF on open, as AutoCAD does. Images (convert) list PDF underlays as not drawn: plotting the page comes next, with PDFCLIP, PDFFRAME, PDFOSNAP, ULAYERS and the References palette. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XPjGXkLR3L7kJS9tr2pT85
1 parent e86d895 commit e674942

38 files changed

Lines changed: 3885 additions & 67 deletions

‎Cargo.lock‎

Lines changed: 154 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ image = { version = "0.25", default-features = false, features = ["png", "jpeg"]
5151
encoding_rs = "0.8.35"
5252
# XML for the SVG reader (streaming parser, no entity expansion beyond the five predefined ones)
5353
quick-xml = "0.42"
54+
# PDF page rendering for PDF underlays: the craft apps' shared, patched Hayro reader, at
55+
# craft-pdf's revision (pure Rust; no embedded fonts: the 14 standard PDF fonts are not
56+
# bundled, a PDF that relies on them without embedding them shows no text).
57+
hayro = { version = "=0.7.1", default-features = false }
5458

5559
# ui
5660
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae", features = ["serde"] }
@@ -65,6 +69,10 @@ arboard = { version = "3.6", default-features = false }
6569
krilla = { version = "=0.8.2", git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
6670
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
6771
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
72+
hayro = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
73+
hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
74+
hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
75+
hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "6e7f28518dd1cb57e1624bdc269c3c14316b13ae" }
6876

6977
[workspace.lints.rust]
7078
unsafe_code = "forbid"

‎apps/cadcraft-cli/src/main.rs‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ fn install_io() {
185185
groups: cadcraft_io::entity_groups,
186186
entity: cadcraft_io::entity_from_groups,
187187
});
188+
// PDF underlays (PDFATTACH) read their pages.
189+
cadcraft_engine::cmd::underlay::set_pdf(cadcraft_engine::cmd::underlay::PdfHooks {
190+
page: cadcraft_io::pdf_read::page_info,
191+
render: cadcraft_io::pdf_read::render_page,
192+
region: cadcraft_io::pdf_read::render_page_region,
193+
layers: cadcraft_io::pdf_layers::layer_list,
194+
with_layers: cadcraft_io::pdf_layers::apply_layers,
195+
});
188196
}
189197

190198
fn open(s: &mut Session, path: &str) -> Result<(), String> {
@@ -293,12 +301,19 @@ fn convert(args: &[String]) -> Result<(), String> {
293301
}
294302

295303
/// The warning for the model-space entities of `path` an image can't show, by type (those kept
296-
/// as unknown objects, `Proxy` in `info`), or `None` when there are none.
304+
/// as unknown objects, `Proxy` in `info`, and PDF underlays, whose page isn't plotted), or `None`
305+
/// when there are none.
297306
fn undrawn_message(d: &cadcraft_engine::doc::Drawing, path: &str) -> Option<String> {
298307
let mut types: std::collections::BTreeMap<&str, usize> = std::collections::BTreeMap::new();
299308
for e in d.model.iter() {
300-
if let cadcraft_engine::doc::EntityKind::Unknown(u) = &e.kind {
301-
let n = types.entry(u.dxf_type.as_str()).or_insert(0);
309+
// PDF underlays' pages aren't plotted yet: only their frame is.
310+
let undrawn = match &e.kind {
311+
cadcraft_engine::doc::EntityKind::Unknown(u) => Some(u.dxf_type.as_str()),
312+
cadcraft_engine::doc::EntityKind::PdfUnderlay(_) => Some("PDFUNDERLAY"),
313+
_ => None,
314+
};
315+
if let Some(t) = undrawn {
316+
let n = types.entry(t).or_insert(0);
302317
*n = n.saturating_add(1);
303318
}
304319
}

‎apps/cadcraft-cli/tests/it/undrawn_entities.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ fn cli(args: &[&str]) -> Output {
77
Command::new(env!("CARGO_BIN_EXE_cadcraft-cli")).args(args).output().unwrap()
88
}
99

10-
/// A line, two foreign REGIONs and a PDF underlay: the last three are kept as unknown entities.
10+
/// A line, two foreign REGIONs, kept as unknown entities, and a PDF underlay, whose page an image
11+
/// doesn't draw yet.
1112
const PROXIES: &str = "0\nSECTION\n2\nENTITIES\n\
1213
0\nLINE\n8\n0\n10\n0\n20\n0\n11\n5\n21\n5\n\
1314
0\nREGION\n8\n0\n70\n1\n1\nabc\n\
@@ -25,7 +26,8 @@ fn convert_to_an_image_warns_about_entities_it_cannot_draw() {
2526

2627
let out = cli(&["info", input]);
2728
let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
28-
assert_eq!(v["counts"]["Proxy"], 3, "{v}");
29+
assert_eq!(v["counts"]["Proxy"], 2, "{v}");
30+
assert_eq!(v["counts"]["PDF Underlay"], 1, "{v}");
2931

3032
for ext in ["svg", "png", "pdf"] {
3133
let image = dir.join(format!("out.{ext}"));

‎apps/cadcraft-web/src/main.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ mod web {
112112
groups: cadcraft_io::entity_groups,
113113
entity: cadcraft_io::entity_from_groups,
114114
});
115+
// PDF underlays (PDFATTACH) read their pages.
116+
cadcraft_engine::cmd::underlay::set_pdf(cadcraft_engine::cmd::underlay::PdfHooks {
117+
page: cadcraft_io::pdf_read::page_info,
118+
render: cadcraft_io::pdf_read::render_page,
119+
region: cadcraft_io::pdf_read::render_page_region,
120+
layers: cadcraft_io::pdf_layers::layer_list,
121+
with_layers: cadcraft_io::pdf_layers::apply_layers,
122+
});
115123
// `?host=parent`: a same-origin page framing CADCraft (such as a Nextcloud app) opens
116124
// drawings in it and stores what it saves. Otherwise there is no file system here: saved
117125
// files reach the user as browser downloads.

‎apps/cadcraft/src/main.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ pub fn install_io() {
124124
groups: cadcraft_io::entity_groups,
125125
entity: cadcraft_io::entity_from_groups,
126126
});
127+
// PDF underlays (PDFATTACH) read their pages.
128+
cadcraft_engine::cmd::underlay::set_pdf(cadcraft_engine::cmd::underlay::PdfHooks {
129+
page: cadcraft_io::pdf_read::page_info,
130+
render: cadcraft_io::pdf_read::render_page,
131+
region: cadcraft_io::pdf_read::render_page_region,
132+
layers: cadcraft_io::pdf_layers::layer_list,
133+
with_layers: cadcraft_io::pdf_layers::apply_layers,
134+
});
127135
}
128136

129137
fn services() -> Services {
@@ -150,6 +158,13 @@ fn services() -> Services {
150158
dialog.pick_file().map(|p| p.to_string_lossy().to_string())
151159
})),
152160
no_file_system: false,
161+
pick_pdf: Some(Box::new(|| {
162+
rfd::FileDialog::new()
163+
.add_filter(i18n::t("PDF files"), &["pdf"])
164+
.add_filter(i18n::t("All files"), &["*"])
165+
.pick_file()
166+
.map(|p| p.to_string_lossy().to_string())
167+
})),
153168
}
154169
}
155170

0 commit comments

Comments
 (0)