🐛 Omit board fill on WASM export when Show in exports is off (#13073)

Send the flag to render-wasm and drop the root's fills while it renders.

Closes #12930

AI-assisted-by: claude-opus-5.5
This commit is contained in:
Elena Torró 2026-11-17 07:58:10 +01:00 committed by GitHub
parent d97972c81d
commit a050a21fae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 131 additions and 52 deletions

View File

@ -27,6 +27,7 @@
(def ^:const BASE-PROPS-SIZE 104)
(def ^:const FLAG-CLIP-CONTENT 0x01)
(def ^:const FLAG-HIDDEN 0x02)
(def ^:const FLAG-HIDE-FILL-ON-EXPORT 0x04)
(def ^:const SECTION-CHILDREN 0x01)
(def ^:const SECTION-BLUR-LAYER 0x02)
@ -61,9 +62,11 @@
(not (get shape :show-content))
false)
hidden (get shape :hidden false)
hide-fill (get shape :hide-fill-on-export false)
flags (cond-> 0
clip-content (bit-or FLAG-CLIP-CONTENT)
hidden (bit-or FLAG-HIDDEN))
hidden (bit-or FLAG-HIDDEN)
hide-fill (bit-or FLAG-HIDE-FILL-ON-EXPORT))
blend-mode (sr/translate-blend-mode (get shape :blend-mode))
constraint-h (sr/translate-constraint-h (or (get shape :constraints-h) :none))
constraint-v (sr/translate-constraint-v (or (get shape :constraints-v) :none))

View File

@ -55,7 +55,7 @@
:pdf (wasm.api/render-shape-pdf object-id)
:svg (wasm.api/render-shape-svg object-id scale)
(if (= :client (wasm.api/export-target object-id scale type))
(wasm.api/render-shape-pixels object-id scale type)
(wasm.api/render-shape-pixels object-id scale type true)
(wasm.api/render-shape-raster object-id scale type)))))
(defn export-image-uri

View File

@ -70,7 +70,7 @@
scale (mth/max 1 (/ target-size max-size))
;; Thumbnails are always PNG: they need the alpha
;; channel and are consumed as a `image/png` data uri.
png-bytes (wasm.api/render-shape-pixels frame-id scale :png)]
png-bytes (wasm.api/render-shape-pixels frame-id scale :png false)]
(if (or (nil? png-bytes) (zero? (.-length png-bytes)))
(do
(l/error :hint "render-shape-pixels returned empty" :frame-id (str frame-id))

View File

@ -1130,6 +1130,10 @@
[hidden]
(h/call wasm/internal-module "_set_shape_hidden" hidden))
(defn set-shape-hide-fill-on-export
[hide]
(h/call wasm/internal-module "_set_shape_hide_fill_on_export" hide))
(defn clear-shape-fills!
"Clear the fills of the currently-selected shape (call `use-shape` first).
Equivalent to `set-shape-fills` with an empty collection."
@ -2934,8 +2938,9 @@
(defn render-shape-pixels
"Renders a shape subtree to encoded image bytes on the GPU export surface.
`format` is :png, :jpeg or :webp; jpeg is flattened onto white on the Rust
side, since it has no alpha channel."
[shape-id scale format]
side, since it has no alpha channel. With `export?` the root drops its
fills when it hides them on export."
[shape-id scale format export?]
(when (initialized?)
(let [buffer (uuid/get-u32 shape-id)
offset (h/call wasm/internal-module "_render_shape_pixels"
@ -2944,7 +2949,8 @@
(aget buffer 2)
(aget buffer 3)
scale
(sr/translate-raster-format format))]
(sr/translate-raster-format format)
export?)]
(read-raster-result offset))))
(defn render-shape-raster

View File

@ -191,6 +191,9 @@
:hidden
(api/set-shape-hidden v)
:hide-fill-on-export
(api/set-shape-hide-fill-on-export (boolean v))
:shapes
(api/set-shape-children v)

View File

@ -588,6 +588,15 @@ pub extern "C" fn set_shape_hidden(hidden: bool) -> Result<()> {
Ok(())
}
#[no_mangle]
#[wasm_error]
pub extern "C" fn set_shape_hide_fill_on_export(hide: bool) -> Result<()> {
with_current_shape_mut!(state, |shape: &mut Shape| {
shape.set_hide_fill_on_export(hide);
});
Ok(())
}
#[no_mangle]
#[wasm_error]
pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) -> Result<()> {
@ -775,7 +784,8 @@ pub extern "C" fn get_shape_extrect(a: u32, b: u32, c: u32, d: u32) -> Result<*m
/// Raster image via the GPU surface. Returns `[len][width][height][bytes]`
/// (LE). `format` selects the encoder: 0 = PNG, 1 = JPEG, 2 = WEBP (see
/// `RasterFormat`).
/// `RasterFormat`). `export` drops the root's fills when it hides them on
/// export; thumbnails keep them.
#[no_mangle]
#[wasm_error]
pub extern "C" fn render_shape_pixels(
@ -785,6 +795,7 @@ pub extern "C" fn render_shape_pixels(
d: u32,
scale: f32,
format: u32,
export: bool,
) -> Result<*mut u8> {
let id = uuid_from_u32_quartet(a, b, c, d);
@ -796,7 +807,7 @@ pub extern "C" fn render_shape_pixels(
with_state!(state, {
let (data, width, height) =
state.render_shape_pixels(&id, scale, performance::get_time(), format)?;
state.render_shape_pixels(&id, scale, performance::get_time(), format, export)?;
let len = data.len() as u32;
let mut buf = Vec::with_capacity(4 + data.len());

View File

@ -419,11 +419,7 @@ fn beziers_to_segments(beziers: &[(BezierSource, Bezier)]) -> Vec<Segment> {
let mut last_end = (first_bezier.end.x as f32, first_bezier.end.y as f32);
let mut cur_end = first_bezier.end;
loop {
let Some((next_src, next_bezier)) = find_next_in_pool(&mut pool, cur_end, cur_src)
else {
break;
};
while let Some((next_src, next_bezier)) = find_next_in_pool(&mut pool, cur_end, cur_src) {
push_bezier(&mut result, &next_bezier);
last_end = (next_bezier.end.x as f32, next_bezier.end.y as f32);
cur_end = next_bezier.end;

View File

@ -3,7 +3,7 @@ use std::mem::ManuallyDrop;
use skia_safe as skia;
use crate::error::{Error, Result};
use crate::state::ShapesPoolRef;
use crate::state::{ShapesPoolMutRef, ShapesPoolRef};
use crate::uuid::Uuid;
use super::vector;
@ -143,7 +143,7 @@ pub fn export_abort(slot: &mut Option<PdfExportSession>) {
pub fn render_to_pdf(
shared: &mut RenderResources,
id: &Uuid,
tree: ShapesPoolRef,
tree: ShapesPoolMutRef,
) -> Result<Vec<u8>> {
render_to_pdf_multi(shared, &[*id], tree)
}
@ -154,7 +154,7 @@ pub fn render_to_pdf(
pub fn render_to_pdf_multi(
shared: &mut RenderResources,
ids: &[Uuid],
tree: ShapesPoolRef,
tree: ShapesPoolMutRef,
) -> Result<Vec<u8>> {
if ids.is_empty() {
return Err(Error::CriticalError(
@ -164,7 +164,9 @@ pub fn render_to_pdf_multi(
let mut session = None;
export_begin(&mut session)?;
for id in ids {
if let Err(err) = export_add_page(&mut session, shared, id, tree) {
let page =
tree.with_export_root(id, |tree| export_add_page(&mut session, shared, id, tree));
if let Err(err) = page {
export_abort(&mut session);
return Err(err);
}
@ -234,7 +236,7 @@ mod tests {
let mut resources = RenderResources::try_new_headless().expect("headless resources");
let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources);
let pdf = render_to_pdf_multi(&mut resources, &[a, b], &pool).expect("pdf");
let pdf = render_to_pdf_multi(&mut resources, &[a, b], &mut pool).expect("pdf");
assert!(pdf.starts_with(b"%PDF"), "missing PDF header");
assert_eq!(page_count(&pdf), 2, "expected two pages");
}
@ -270,11 +272,11 @@ mod tests {
#[test]
fn empty_id_list_is_an_error() {
let pool = ShapesPool::new();
let mut pool = ShapesPool::new();
let mut resources = RenderResources::try_new_headless().expect("headless resources");
let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources);
let err = render_to_pdf_multi(&mut resources, &[], &pool).expect_err("empty");
let err = render_to_pdf_multi(&mut resources, &[], &mut pool).expect_err("empty");
assert!(
matches!(err, Error::CriticalError(_)),
"empty ids must fail critically: {err:?}"
@ -295,7 +297,7 @@ mod tests {
let mut resources = RenderResources::try_new_headless().expect("headless resources");
let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources);
let pdf = render_to_pdf(&mut resources, &id, &pool).expect("pdf");
let pdf = render_to_pdf(&mut resources, &id, &mut pool).expect("pdf");
assert!(pdf.starts_with(b"%PDF"));
assert_eq!(page_count(&pdf), 1);
}

View File

@ -781,7 +781,7 @@ mod tests {
.add(family, LIGATURE_FONT, false, false)
.expect("ligature font");
let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources);
let pdf = super::super::render_to_pdf(&mut resources, &id, &pool).expect("pdf");
let pdf = super::super::render_to_pdf(&mut resources, &id, &mut pool).expect("pdf");
let trailer = parse_xref(&pdf).expect("xref").trailer;
assert!(

View File

@ -191,6 +191,7 @@ pub struct Shape {
pub background_blur: Option<Blur>,
pub opacity: f32,
pub hidden: bool,
pub hide_fill_on_export: bool,
pub svg: Option<skia::svg::Dom>,
pub svg_attrs: Option<SvgAttrs>,
pub shadows: Vec<Shadow>,
@ -295,6 +296,7 @@ impl Shape {
vertical_align: VerticalAlign::Top,
opacity: 1.,
hidden: false,
hide_fill_on_export: false,
blur: None,
background_blur: None,
svg: None,
@ -466,6 +468,10 @@ impl Shape {
self.hidden = value;
}
pub fn set_hide_fill_on_export(&mut self, value: bool) {
self.hide_fill_on_export = value;
}
pub fn svg_transform(&self) -> Option<Matrix> {
self.svg_transform
}

View File

@ -98,16 +98,24 @@ impl State {
scale: f32,
timestamp: i32,
format: RasterFormat,
export: bool,
) -> Result<(Vec<u8>, i32, i32)> {
get_render_state().render_shape_pixels(id, &self.shapes, scale, timestamp, format)
let render = |tree: &ShapesPool| {
get_render_state().render_shape_pixels(id, tree, scale, timestamp, format)
};
if export {
self.shapes.with_export_root(id, render)
} else {
render(&self.shapes)
}
}
pub fn render_shape_pdf(&mut self, id: &Uuid) -> Result<Vec<u8>> {
crate::render::pdf::render_to_pdf(get_resources(), id, &self.shapes)
crate::render::pdf::render_to_pdf(get_resources(), id, &mut self.shapes)
}
pub fn render_shapes_pdf(&mut self, ids: &[Uuid]) -> Result<Vec<u8>> {
crate::render::pdf::render_to_pdf_multi(get_resources(), ids, &self.shapes)
crate::render::pdf::render_to_pdf_multi(get_resources(), ids, &mut self.shapes)
}
pub fn pdf_export_begin(&mut self) -> Result<()> {
@ -115,7 +123,10 @@ impl State {
}
pub fn pdf_export_add_page(&mut self, id: &Uuid) -> Result<()> {
crate::render::pdf::export_add_page(&mut self.pdf_export, get_resources(), id, &self.shapes)
let session = &mut self.pdf_export;
self.shapes.with_export_root(id, |tree| {
crate::render::pdf::export_add_page(session, get_resources(), id, tree)
})
}
pub fn pdf_export_finish(&mut self) -> Result<Vec<u8>> {
@ -127,7 +138,9 @@ impl State {
}
pub fn render_shape_svg(&mut self, id: &Uuid, scale: f32) -> Result<Vec<u8>> {
crate::render::svg::render_to_svg(get_resources(), id, &self.shapes, scale)
self.shapes.with_export_root(id, |tree| {
crate::render::svg::render_to_svg(get_resources(), id, tree, scale)
})
}
pub fn render_shapes_svg(&mut self, ids: &[Uuid], scale: f32) -> Result<Vec<u8>> {
@ -142,7 +155,9 @@ impl State {
scale: f32,
format: RasterFormat,
) -> Result<(Vec<u8>, i32, i32)> {
crate::render::raster::render_to_raster(get_resources(), id, &self.shapes, scale, format)
self.shapes.with_export_root(id, |tree| {
crate::render::raster::render_to_raster(get_resources(), id, tree, scale, format)
})
}
/// Distinct font families used by the (visible) subtree rooted at `id`, in

View File

@ -142,6 +142,24 @@ impl ShapesPoolImpl {
self.uuid_to_idx.contains_key(id)
}
/// Runs `render` with the fills of `id` removed when it hides them on
/// export. Only the exported board applies the flag; nested boards keep
/// their fills.
pub fn with_export_root<T>(&mut self, id: &Uuid, render: impl FnOnce(&Self) -> T) -> T {
let hide = self.get(id).is_some_and(|shape| shape.hide_fill_on_export);
let fills = if hide {
self.get_mut(id)
.map(|shape| std::mem::take(&mut shape.fills))
} else {
None
};
let result = render(self);
if let (Some(fills), Some(shape)) = (fills, self.get_mut(id)) {
shape.fills = fills;
}
result
}
pub fn get_mut(&mut self, id: &Uuid) -> Option<&mut Shape> {
let idx = *self.uuid_to_idx.get(id)?;
// The caller may change the shape, so its modified copy goes stale.
@ -611,6 +629,24 @@ mod tests {
(pool, parent, child)
}
#[test]
fn with_export_root_hides_only_the_root_fills() {
let (mut pool, parent, child) = pool_with_parent_and_child();
let fills = vec![shapes::Fill::Solid(shapes::SolidColor(skia::Color::RED))];
for id in [parent, child] {
let shape = pool.get_mut(&id).expect("shape");
shape.set_fills(fills.clone());
shape.set_hide_fill_on_export(true);
}
pool.with_export_root(&parent, |tree| {
assert!(tree.get(&parent).expect("parent").fills.is_empty());
assert_eq!(tree.get(&child).expect("child").fills, fills);
});
assert_eq!(pool.get(&parent).expect("parent").fills, fills);
}
#[test]
fn set_modifiers_passes_a_move_down_to_children() {
let (mut pool, parent, child) = pool_with_parent_and_child();

View File

@ -444,7 +444,8 @@ impl PendingTiles {
// Farthest first, since we use pop() to process the tiles
// in order of priority (closest first)
self.tile_order.sort_unstable_by(|a, b| b.0.cmp(&a.0));
self.tile_order
.sort_unstable_by_key(|a| std::cmp::Reverse(a.0));
for (_, tile) in self.tile_order.iter() {
let tile = *tile;

View File

@ -87,10 +87,12 @@ impl TryFrom<&[u8]> for RawFillData {
// FIXME: return Result
pub fn read_fills_from_bytes(buffer: &[u8], num_fills: usize) -> Vec<shapes::Fill> {
buffer
.chunks_exact(RAW_FILL_DATA_SIZE)
.as_chunks::<RAW_FILL_DATA_SIZE>()
.0
.iter()
.take(num_fills)
.map(|bytes| {
RawFillData::try_from(bytes)
RawFillData::try_from(bytes.as_slice())
.expect("Invalid fill data")
.into()
})

View File

@ -11,6 +11,7 @@ use super::RawShapeType;
const FLAG_CLIP_CONTENT: u8 = 0b0000_0001;
const FLAG_HIDDEN: u8 = 0b0000_0010;
const FLAG_HIDE_FILL_ON_EXPORT: u8 = 0b0000_0100;
pub(crate) const RAW_BASE_PROPS_SIZE: usize = std::mem::size_of::<RawBasePropsData>();
@ -78,6 +79,10 @@ impl RawBasePropsData {
(self.flags & FLAG_HIDDEN) != 0
}
fn hide_fill_on_export(&self) -> bool {
(self.flags & FLAG_HIDE_FILL_ON_EXPORT) != 0
}
fn blend_mode(&self) -> BlendMode {
RawBlendMode::from(self.blend_mode).into()
}
@ -112,6 +117,7 @@ pub(crate) fn apply_base_props(raw: &RawBasePropsData) -> Result<()> {
shape.set_shape_type(shape_type.into());
shape.set_clip(raw.clip_content());
shape.set_hidden(raw.hidden());
shape.set_hide_fill_on_export(raw.hide_fill_on_export());
shape.set_blend_mode(raw.blend_mode());
shape.set_opacity(raw.opacity);
shape.set_constraint_h(raw.constraint_h());

View File

@ -297,12 +297,9 @@ pub fn replace_text_with_newlines(
let mut current_cursor = *cursor;
if let Some(new_offset) = replace_text_at_cursor(text_content, &current_cursor, lines[0]) {
current_cursor =
TextPositionWithAffinity::new_downstream_affinity(current_cursor.paragraph, new_offset);
} else {
return None;
}
let new_offset = replace_text_at_cursor(text_content, &current_cursor, lines[0])?;
current_cursor =
TextPositionWithAffinity::new_downstream_affinity(current_cursor.paragraph, new_offset);
for line in lines.iter().skip(1) {
if !split_paragraph_at_cursor(text_content, &current_cursor) {
@ -336,12 +333,9 @@ pub fn insert_text_with_newlines(
let mut current_cursor = *cursor;
if let Some(new_offset) = insert_text_at_cursor(text_content, &current_cursor, lines[0]) {
current_cursor =
TextPositionWithAffinity::new_downstream_affinity(current_cursor.paragraph, new_offset);
} else {
return None;
}
let new_offset = insert_text_at_cursor(text_content, &current_cursor, lines[0])?;
current_cursor =
TextPositionWithAffinity::new_downstream_affinity(current_cursor.paragraph, new_offset);
for line in lines.iter().skip(1) {
if !split_paragraph_at_cursor(text_content, &current_cursor) {
@ -477,8 +471,7 @@ pub fn delete_selection_range(text_content: &mut TextContent, selection: &TextSe
delete_range_in_paragraph(&mut paragraphs[end_paragraph], 0, end.offset);
let end_para_children: Vec<_> =
paragraphs[end_paragraph].children_mut().drain(..).collect();
let end_para_children = std::mem::take(paragraphs[end_paragraph].children_mut());
paragraphs[start.paragraph]
.children_mut()
.extend(end_para_children);
@ -593,10 +586,7 @@ pub fn delete_char_before(
let paragraphs = text_content.paragraphs_mut();
let prev_para_len = paragraph_char_count(&paragraphs[prev_para_idx]);
let current_children: Vec<_> = paragraphs[cursor.paragraph]
.children_mut()
.drain(..)
.collect();
let current_children = std::mem::take(paragraphs[cursor.paragraph].children_mut());
paragraphs[prev_para_idx]
.children_mut()
.extend(current_children);
@ -770,7 +760,7 @@ pub fn delete_char_after(text_content: &mut TextContent, cursor: &TextPositionWi
delete_range_in_paragraph(para, cursor.offset, end);
} else if cursor.paragraph < paragraphs.len() - 1 {
let next_para_idx = cursor.paragraph + 1;
let next_children: Vec<_> = paragraphs[next_para_idx].children_mut().drain(..).collect();
let next_children = std::mem::take(paragraphs[next_para_idx].children_mut());
paragraphs[cursor.paragraph]
.children_mut()
.extend(next_children);

View File

@ -76,11 +76,13 @@ impl TryFrom<&[u8]> for RawGuide {
fn read_guides_from_bytes(buffer: &[u8], count: usize) -> Result<Vec<Guide>> {
buffer
.chunks_exact(RAW_GUIDE_SIZE)
.as_chunks::<RAW_GUIDE_SIZE>()
.0
.iter()
.take(count)
.enumerate()
.map(|(i, bytes)| {
RawGuide::try_from(bytes).map(|raw_guide| {
RawGuide::try_from(bytes.as_slice()).map(|raw_guide| {
let mut guide: Guide = raw_guide.into();
guide.index = i;
guide