Skip to content

Commit

Permalink
Fix issue where display item snapshot was not closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed Jun 12, 2024
1 parent 175bb63 commit 3b9a03f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 21 additions & 6 deletions nion/swift/DocumentController.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,12 +895,27 @@ def export_files(self, display_items: typing.Sequence[DisplayItem.DisplayItem])
self.export_file(display_items[0])

def export_svg_file(self, ui_settings: UISettings.UISettings, display_item: DisplayItem.DisplayItem, display_shape: Geometry.IntSize, path: pathlib.Path) -> None:
path = path.with_suffix(".svg")
drawing_context, shape = DisplayPanel.preview(ui_settings, display_item, display_shape.width, display_shape.height)
view_box = Geometry.IntRect(Geometry.IntPoint(), shape)
svg = drawing_context.to_svg(shape, view_box)
with Utility.AtomicFileWriter(path) as fp:
fp.write(svg)
# take a snapshot so to modify the display properties for the proper image zoom, position, and canvas mode.
# ensure that the snapshot is closed when finished.
display_item_snapshot = display_item.snapshot()
try:
# update the zoom, position, and canvas mode for the export. these are neutral values that ensure it will
# draw at 1:1 scale and centered in the canvas.
display_properties = display_item_snapshot.display_properties
display_properties["image_zoom"] = 1.0
display_properties["image_position"] = (0.5, 0.5)
display_properties["image_canvas_mode"] = "fit"
display_item_snapshot.display_properties = display_properties
# create the drawing context and shape for the preview
drawing_context, shape = DisplayPanel.preview(ui_settings, display_item_snapshot, display_shape.width, display_shape.height)
# set up the SVG
view_box = Geometry.IntRect(Geometry.IntPoint(), shape)
svg = drawing_context.to_svg(shape, view_box)
# write the file
with Utility.AtomicFileWriter(path.with_suffix(".svg")) as fp:
fp.write(svg)
finally:
display_item_snapshot.close()

def export_svg(self, display_item: DisplayItem.DisplayItem) -> None:
ExportDialog.ExportSVGDialog(self, display_item)
Expand Down
13 changes: 2 additions & 11 deletions nion/swift/ExportDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ def do_export(self, display_items: typing.Sequence[DisplayItem.DisplayItem]) ->


class ExportSVGHandler:
def __init__(self, display_item: DisplayItem.DisplayItem, display_size: Geometry.IntSize) -> None:
self.display_item = display_item

def __init__(self, display_size: Geometry.IntSize) -> None:
self.width_model = Model.PropertyModel(display_size.width)
self.height_model = Model.PropertyModel(display_size.height)

Expand All @@ -225,19 +223,12 @@ def __init__(self, document_controller: DocumentController.DocumentController, d

u = Declarative.DeclarativeUI()

display_item = display_item.snapshot()
display_properties = display_item.display_properties
display_properties["image_zoom"] = 1.0
display_properties["image_position"] = (0.5, 0.5)
display_properties["image_canvas_mode"] = "fit"
display_item.display_properties = display_properties

if display_item.display_data_shape and len(display_item.display_data_shape) == 2:
display_size = Geometry.IntSize(height=4, width=4)
else:
display_size = Geometry.IntSize(height=3, width=4)

handler = ExportSVGHandler(display_item, display_size)
handler = ExportSVGHandler(display_size)

def ok_clicked() -> bool:
dpi = 96
Expand Down

0 comments on commit 3b9a03f

Please sign in to comment.