Skip to content

Commit 89a2fac

Browse files
authored
Merge pull request #173 from varvedesign/feat/vectorcraft-pdf
fix(print): PDF gradients rendered blank (sampled-function range)
2 parents b3755a9 + a00f3c3 commit 89a2fac

5 files changed

Lines changed: 84 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ update, not for someone reading the commit log.
4646

4747
### Fixed
4848

49+
- **PDF gradients now render** — an RGB gradient fill exported to a PDF whose
50+
sampled function declared four 0-255 outputs for a three-component stream.
51+
Poppler rejected it with "function with wrong output size" and drew nothing,
52+
so gradient-filled shapes came out blank. The function now matches its colour
53+
space and PDF's 0-1 colour range. Gradient stops that fade to transparent are
54+
still flattened to opaque in PDF; that remains open.
4955
- **Autosave failures are visible** — when crash-recovery autosave cannot write
5056
a recovery copy, the status bar shows "Recovery paused" with the reason and
5157
the advice to save to a file, and clears itself on the next successful cycle.

‎crates/varve-print/src/lib.rs‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,15 +1373,20 @@ fn create_sampled_function(
13731373
}
13741374
}
13751375

1376+
// A Type 0 function's Range declares one [min max] pair per output
1377+
// component, and the number of pairs IS the output arity. It must match the
1378+
// shading's colour space (3 for DeviceRGB, 4 for DeviceCMYK), and PDF colour
1379+
// components are 0..1 — not 0..255. Declaring four 0..255 outputs made the
1380+
// stream's 3-byte RGB samples misalign and every component clamp to 1, so
1381+
// poppler warned "function with wrong output size" and drew a blank page.
1382+
let components: usize = if use_cmyk { 4 } else { 3 };
1383+
let range: Vec<Object> = (0..components)
1384+
.flat_map(|_| [Object::Real(0.0), Object::Real(1.0)])
1385+
.collect();
13761386
let func_dict = dictionary! {
13771387
"FunctionType" => 0i64,
13781388
"Domain" => vec![Object::Real(0.0), Object::Real(1.0)],
1379-
"Range" => vec![
1380-
Object::Real(0.0), Object::Real(255.0),
1381-
Object::Real(0.0), Object::Real(255.0),
1382-
Object::Real(0.0), Object::Real(255.0),
1383-
Object::Real(0.0), Object::Real(255.0),
1384-
],
1389+
"Range" => range,
13851390
"Size" => vec![Object::Integer(SAMPLES as i64)],
13861391
"BitsPerSample" => 8i64,
13871392
"Order" => 1i64,
@@ -4421,6 +4426,43 @@ mod tests {
44214426
);
44224427
}
44234428

4429+
/// Regression: an RGB gradient exported a Type 0 function whose Range
4430+
/// declared four 0..255 outputs. The output arity must equal the colour
4431+
/// space's component count, and PDF colour components are 0..1. With the
4432+
/// malformed dict, poppler reported "function with wrong output size" and
4433+
/// drew nothing, so every gradient-filled shape exported blank.
4434+
#[test]
4435+
fn gradient_sampled_function_range_matches_colour_space() {
4436+
let mut node = rect_node(1, 20.0, 20.0, 200.0, 120.0);
4437+
node.fills = Some(vec![gradient_fill(true)]);
4438+
let bytes = export_pdf(&[node], &PdfOptions::default()).expect("export");
4439+
let pdf = String::from_utf8_lossy(&bytes);
4440+
assert!(
4441+
pdf.contains("/Range[0 1 0 1 0 1]"),
4442+
"RGB shading must declare three 0..1 outputs"
4443+
);
4444+
assert!(
4445+
!pdf.contains("/Range[0 255"),
4446+
"colour components are 0..1, not 0..255"
4447+
);
4448+
}
4449+
4450+
#[test]
4451+
fn gradient_sampled_function_range_matches_cmyk_space() {
4452+
let mut node = rect_node(1, 20.0, 20.0, 200.0, 120.0);
4453+
node.fills = Some(vec![gradient_fill(true)]);
4454+
let opts = PdfOptions {
4455+
print_profile: Some(PrintProfile::Fogra39),
4456+
..PdfOptions::default()
4457+
};
4458+
let bytes = export_pdf(&[node], &opts).expect("export");
4459+
let pdf = String::from_utf8_lossy(&bytes);
4460+
assert!(
4461+
pdf.contains("/Range[0 1 0 1 0 1 0 1]"),
4462+
"DeviceCMYK shading must declare four 0..1 outputs"
4463+
);
4464+
}
4465+
44244466
#[test]
44254467
fn render_fills_gradient_preserves_explicit_affine_in_page_space() {
44264468
let mut node = rect_node(1, 10.0, 20.0, 100.0, 80.0);
7.86 KB
Loading
7.8 KB
Loading

‎docs/audits/vectorcraft/findings.md‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,39 @@ than relabelled.
190190
smallest honest change is to derive an overset count per area-text node and
191191
feed the existing preflight check, rather than inventing a new indicator.
192192

193-
## F6 — Gradient stop transparency is lost in PDF export (open)
193+
## F6 — PDF gradients: blank output (fixed) and stop transparency (open)
194194

195195
- **External evidence:** vectorcraft#1140 (canvas blends gradient stop opacity
196196
premultiplied, unlike PDF export, SVG viewers and `Gradient::sample`), #827
197197
(raster-effect blend modes dropped in PDF), #3234 (blend between tints of one
198198
spot colour written as process CMYK).
199-
- **Varve code:** `packages/engine/src/replayGradient.ts` interpolates stops in
200-
premultiplied alpha; `crates/varve-print/src/lib.rs` `ShadingRegistry` /
201-
`create_sampled_function` discards the alpha component when sampling.
202-
- **Status:** `applicable-requires-work`. Not implemented in this pass: a
203-
truthful fix needs per-stop alpha in the shading function plus an `/SMask`
204-
(or equivalent) and a rendered-PDF comparison, which is its own batch.
199+
- **Varve code:** `crates/varve-print/src/lib.rs` — `ShadingRegistry`,
200+
`create_sampled_function`, `sample_gradient`.
201+
- **Observed behavior (before), critical:** a Type 0 sampled function declared
202+
`Range` with **four 0..255 outputs** regardless of colour space, while an RGB
203+
stream carries **three** bytes per sample and PDF colour components are
204+
**0..1**. Poppler reported `GfxUnivariateShading: function with wrong output
205+
size` and drew **nothing**: every gradient-filled shape exported to PDF came
206+
out blank. This was not limited to transparency — any RGB gradient was
207+
affected.
208+
- **Intended outcome:** the function's output arity matches the shading colour
209+
space (3 for DeviceRGB, 4 for DeviceCMYK) and its range is 0..1.
210+
- **Implementation:** the Range is now built from the colour-space component
211+
count with `[0, 1]` pairs.
212+
- **Validation:** `gradient_sampled_function_range_matches_colour_space` and
213+
`..._cmyk_space` assert the emitted dictionaries; the crate suite is 201/201.
214+
Rendered with poppler 26.08: the exported red-to-blue gradient was a blank
215+
page before and correct after — `evidence/pdf-gradient-before.png` and
216+
`evidence/pdf-gradient-after.png`.
217+
- **Still open:** per-stop **alpha** is still discarded when the shading is
218+
sampled (`engine_color_rgba(&color)` drops the alpha component), so a
219+
gradient whose stops fade to transparent still exports opaque. A truthful fix
220+
needs a luminosity `/SMask` whose content is a DeviceGray shading of the alpha
221+
ramp, plus the PDF/X-1a flattening path reviewed for it. That is its own
222+
batch and is not half-implemented here.
223+
- **Note on the canvas side:** `packages/engine/src/replayGradient.ts`
224+
interpolates stops with premultiplied alpha, so canvas and SVG agree; only the
225+
PDF shading sampler diverges now.
205226

206227
## F7 — Whole-document hover/selection scans (checked, already handled)
207228

@@ -259,6 +280,8 @@ review:
259280
| `svg-gradient-whole-document.png` | `exportDocumentToSvgAdvanced` output rendered in Chromium: flat dark rectangle (before) versus the red-to-blue gradient (after). |
260281
| `offset-path-round.png` | Outward Offset Path result driven through Object → Path → Offset Path. The rounded corner arc is visible at the top-left; a miter join would show a sharp corner there. A menubar panel remains over the left edge of the capture. |
261282
| `offset-path-inward.png` | Inward Offset Path result from the same spec. |
283+
| `pdf-gradient-before.png` | Poppler render of a PDF exported from a gradient-filled rectangle **before** the sampled-function fix: the page is blank. |
284+
| `pdf-gradient-after.png` | The same export after the fix: the red-to-blue gradient renders. |
262285

263286
Screenshots are evidence of a specific run, not a claim about every platform:
264287
the SVG captures are Chromium on Linux at the recorded viewport, and the canvas

0 commit comments

Comments
 (0)