From 1219e26d17687670b72ca352419795e72eaad718 Mon Sep 17 00:00:00 2001 From: 0HyperCube <78500760+0HyperCube@users.noreply.github.com> Date: Sat, 22 Oct 2022 22:48:38 +0100 Subject: [PATCH] Fix image export bounds calculation for selected layers (#807) * Fix Export Bounds * Fix the scale of the transform --- .../document/document_message_handler.rs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index 72d241e9..376bbe57 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -315,6 +315,8 @@ impl MessageHandler { + let old_transforms = self.remove_document_transform(); + // Calculate the bounding box of the region to be exported let bounds = match bounds { ExportBounds::AllArtwork => self.all_layer_bounds(&persistent_data.font_cache), @@ -328,9 +330,10 @@ impl MessageHandler String { - // Remove the artwork and artboard pan/tilt/zoom to render it without the user's viewport navigation, and save it to be restored at the end - info!("Transform {transform}"); - + /// Remove the artwork and artboard pan/tilt/zoom to render it without the user's viewport navigation, and save it to be restored at the end + fn remove_document_transform(&mut self) -> [DAffine2; 2] { let old_artwork_transform = self.graphene_document.root.transform; self.graphene_document.root.transform = DAffine2::IDENTITY; GrapheneDocument::mark_children_as_dirty(&mut self.graphene_document.root); @@ -965,6 +968,19 @@ impl DocumentMessageHandler { self.artboard_message_handler.artboards_graphene_document.root.transform = DAffine2::IDENTITY; GrapheneDocument::mark_children_as_dirty(&mut self.artboard_message_handler.artboards_graphene_document.root); + [old_artwork_transform, old_artboard_transform] + } + + /// Transform the artwork and artboard back to their original scales + fn restore_document_transform(&mut self, [old_artwork_transform, old_artboard_transform]: [DAffine2; 2]) { + self.graphene_document.root.transform = old_artwork_transform; + GrapheneDocument::mark_children_as_dirty(&mut self.graphene_document.root); + + self.artboard_message_handler.artboards_graphene_document.root.transform = old_artboard_transform; + GrapheneDocument::mark_children_as_dirty(&mut self.artboard_message_handler.artboards_graphene_document.root); + } + + pub fn render_document(&mut self, size: DVec2, transform: DAffine2, persistent_data: &PersistentData, render_mode: DocumentRenderMode) -> String { // Render the document SVG code let render_data = RenderData::new(ViewMode::Normal, &persistent_data.font_cache, None); @@ -986,14 +1002,6 @@ impl DocumentMessageHandler { size.x, size.y, "\n", outside_artboards, matrix, artboards, artwork ); - // Transform the artwork and artboard back to their original scales - - self.graphene_document.root.transform = old_artwork_transform; - GrapheneDocument::mark_children_as_dirty(&mut self.graphene_document.root); - - self.artboard_message_handler.artboards_graphene_document.root.transform = old_artboard_transform; - GrapheneDocument::mark_children_as_dirty(&mut self.artboard_message_handler.artboards_graphene_document.root); - svg }