From 05855b812a73a5ec2020f364e47ef4cc96146e67 Mon Sep 17 00:00:00 2001 From: Brendan Hagan Date: Fri, 12 Aug 2022 22:49:27 -0400 Subject: [PATCH] feat: add ability to control zoom/angle on generated card image --- src/data/format/formats.hpp | 6 ++-- src/data/format/image.cpp | 63 +++++++++++++++++++++++++++------- src/script/functions/image.cpp | 11 +++++- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/data/format/formats.hpp b/src/data/format/formats.hpp index 4e317ca0..fd7d9665 100644 --- a/src/data/format/formats.hpp +++ b/src/data/format/formats.hpp @@ -99,11 +99,11 @@ void export_images(const SetP& set, const vector& cards, void export_image(const SetP& set, const CardP& card, const String& filename); /// Generate a bitmap image of a card -Bitmap export_bitmap(const SetP& set, const CardP& card); +Bitmap export_bitmap(const SetP& set, const CardP& card); +Bitmap export_bitmap(const SetP& set, const CardP& card, const double zoom, const Radians angle_radians); /// Export a set to Magic Workstation format void export_mws(Window* parent, const SetP& set); /// Export a set to Apprentice -void export_apprentice(Window* parent, const SetP& set); - +void export_apprentice(Window* parent, const SetP& set); diff --git a/src/data/format/image.cpp b/src/data/format/image.cpp index 91a4997e..0083a03c 100644 --- a/src/data/format/image.cpp +++ b/src/data/format/image.cpp @@ -25,15 +25,35 @@ void export_image(const SetP& set, const CardP& card, const String& filename) { } class UnzoomedDataViewer : public DataViewer { -public: +public: + UnzoomedDataViewer(); + UnzoomedDataViewer(double zoom, double angle); virtual ~UnzoomedDataViewer() {}; Rotation getRotation() const override; -private: - double angle = 0.0; -}; +private: + double zoom; + double angle; + bool declared_values; +}; + +UnzoomedDataViewer::UnzoomedDataViewer() + : zoom(1.0) + , angle(0.0) + , declared_values(false) +{} + +UnzoomedDataViewer::UnzoomedDataViewer(const double zoom, const Radians angle = 0.0) + : zoom(zoom) + , angle(angle) + , declared_values(true) +{} Rotation UnzoomedDataViewer::getRotation() const { - if (!stylesheet) stylesheet = set->stylesheet; + if (!stylesheet) stylesheet = set->stylesheet; + if (declared_values) { + return Rotation(angle, stylesheet->getCardRect(), zoom, 1.0, ROTATION_ATTACH_TOP_LEFT); + } + double export_zoom = settings.stylesheetSettingsFor(set->stylesheetFor(card)).export_zoom(); bool use_viewer_rotation = !settings.stylesheetSettingsFor(set->stylesheetFor(card)).card_normal_export(); @@ -42,26 +62,43 @@ Rotation UnzoomedDataViewer::getRotation() const { } else { return Rotation(angle, stylesheet->getCardRect(), export_zoom, 1.0, ROTATION_ATTACH_TOP_LEFT); } -} - -Bitmap export_bitmap(const SetP& set, const CardP& card) { - if (!set) throw Error(_("no set")); - // create viewer +} + +Bitmap export_bitmap(const SetP& set, const CardP& card) { + if (!set) throw Error(_("no set")); UnzoomedDataViewer viewer = UnzoomedDataViewer(); viewer.setSet(set); viewer.setCard(card); // size of cards RealSize size = viewer.getRotation().getExternalSize(); // create bitmap & dc - Bitmap bitmap((int) size.width, (int) size.height); + Bitmap bitmap((int)size.width, (int)size.height); if (!bitmap.Ok()) throw InternalError(_("Unable to create bitmap")); wxMemoryDC dc; dc.SelectObject(bitmap); // draw viewer.draw(dc); dc.SelectObject(wxNullBitmap); - return bitmap; -} + return bitmap; +} + +Bitmap export_bitmap(const SetP& set, const CardP& card, const double zoom, const Radians angle = 0.0) { + if (!set) throw Error(_("no set")); + UnzoomedDataViewer viewer = UnzoomedDataViewer(zoom, angle); + viewer.setSet(set); + viewer.setCard(card); + // size of cards + RealSize size = viewer.getRotation().getExternalSize(); + // create bitmap & dc + Bitmap bitmap((int)size.width, (int)size.height); + if (!bitmap.Ok()) throw InternalError(_("Unable to create bitmap")); + wxMemoryDC dc; + dc.SelectObject(bitmap); + // draw + viewer.draw(dc); + dc.SelectObject(wxNullBitmap); + return bitmap; +} // ----------------------------------------------------------------------------- : Multiple card export diff --git a/src/script/functions/image.cpp b/src/script/functions/image.cpp index 23fb8094..55ab22b1 100644 --- a/src/script/functions/image.cpp +++ b/src/script/functions/image.cpp @@ -32,7 +32,16 @@ SCRIPT_FUNCTION(to_image) { SCRIPT_FUNCTION(to_card_image) { SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(CardP, input); - return make_intrusive(export_bitmap(set, input).ConvertToImage()); + SCRIPT_PARAM_DEFAULT(double, zoom, 100); + SCRIPT_PARAM_DEFAULT(Degrees, angle, 0); + SCRIPT_PARAM_DEFAULT(bool, use_user_settings, false); + if (use_user_settings) { + // Use the User's Preferences for Export Zoom and Angle settings. + return make_intrusive(export_bitmap(set, input).ConvertToImage()); + } else { + // Use the provided (or defaulted) Zoom and Angle. + return make_intrusive(export_bitmap(set, input, (zoom / 100), deg_to_rad(angle)).ConvertToImage()); + } } // ----------------------------------------------------------------------------- : Image functions