Skip to content

Commit

Permalink
to_image(card) can now take a zoom value
Browse files Browse the repository at this point in the history
  • Loading branch information
djackddonovan committed Oct 15, 2022
1 parent c3a5a9b commit 0fb6ea5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/function/to_image.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Convert any value to a [[type:image]].
--Parameters--
! Parameter Type Description
| @input@ ''any type'' Value to convert to an image
| @zoom@ [[type:double]] (optional) A zoom value for the image. Only used when converting cards. This allows you to keep the sharpness of the text at higher resolution.

--Examples--
>>> to_image("image1.png") == <img src="image1.png" alt='"image1.png"' style="border:1px solid black;vertical-align:middle;margin:1px;" />
2 changes: 1 addition & 1 deletion src/data/format/formats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void export_images(const SetP& set, const vector<CardP>& 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, double zoom = 1.0);

/// Export a set to Magic Workstation format
void export_mws(Window* parent, const SetP& set);
Expand Down
22 changes: 13 additions & 9 deletions src/data/format/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@ void export_image(const SetP& set, const CardP& card, const String& filename) {
// but image.saveFile determines it automagicly
}

class UnzoomedDataViewer : public DataViewer {
class ZoomOverrideDataViewer : public DataViewer {
public:
UnzoomedDataViewer(bool use_zoom_settings)
: use_zoom_settings(use_zoom_settings)
ZoomOverrideDataViewer(bool use_zoom_settings, double zoom = 1.0)
: use_zoom_settings(use_zoom_settings), zoom(zoom)
{}
Rotation getRotation() const override;
private:
bool use_zoom_settings;
double zoom = 1.0;
double angle = 0.0;
};
Rotation UnzoomedDataViewer::getRotation() const {
Rotation ZoomOverrideDataViewer::getRotation() const {
Rotation rot;
if (use_zoom_settings) {
return DataViewer::getRotation();
} else {
rot = DataViewer::getRotation();
rot.setZoom(rot.getZoom() * zoom);
}
else {
if (!stylesheet) stylesheet = set->stylesheet;
return Rotation(angle, stylesheet->getCardRect(), zoom, 1.0, ROTATION_ATTACH_TOP_LEFT);
rot = Rotation(angle, stylesheet->getCardRect(), zoom, 1.0, ROTATION_ATTACH_TOP_LEFT);
}
return rot;
}

Bitmap export_bitmap(const SetP& set, const CardP& card) {
Bitmap export_bitmap(const SetP& set, const CardP& card, double zoom /*= 1.0*/) {
if (!set) throw Error(_("no set"));
// create viewer
UnzoomedDataViewer viewer(!settings.stylesheetSettingsFor(set->stylesheetFor(card)).card_normal_export());
ZoomOverrideDataViewer viewer(!settings.stylesheetSettingsFor(set->stylesheetFor(card)).card_normal_export(), zoom);
viewer.setSet(set);
viewer.setCard(card);
// size of cards
Expand Down
4 changes: 3 additions & 1 deletion src/script/functions/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ SCRIPT_FUNCTION(to_image) {
SCRIPT_PARAM_C(ScriptValueP, input);
ScriptObject<CardP>* card = dynamic_cast<ScriptObject<CardP>*>(input.get());
if (card) {
Image image = export_bitmap(export_info()->set, card->getValue()).ConvertToImage();
SCRIPT_OPTIONAL_PARAM_(double, zoom);
if (zoom <= 0) zoom = 1; // default
Image image = export_bitmap(export_info()->set, card->getValue(), zoom).ConvertToImage();
return make_intrusive<PreGeneratedImage>(image);
}
return input->toImage();
Expand Down

0 comments on commit 0fb6ea5

Please sign in to comment.