Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/function/to_image.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ DOC_MSE_VERSION: since 0.3.8

Convert any value to a [[type:image]].

A string is interpreted as a [[type:filename]].
* A string is interpreted as a [[type:filename]].
* A [[type:card]] will be converted into an image onto which image operations can be done.

--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
18 changes: 18 additions & 0 deletions src/gfx/generated_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,21 @@ bool ImageValueToImage::operator == (const GeneratedImage& that) const {
return that2 && filename == that2->filename
&& age == that2->age;
}

// ----------------------------------------------------------------------------- : PreGeneratedImage

PreGeneratedImage::PreGeneratedImage(const Image& image)
: image(image)
{}
PreGeneratedImage::~PreGeneratedImage() {}

Image PreGeneratedImage::generate(const Options& opt) const {
if (!image.Ok()) {
return Image(max(1, opt.width), max(1, opt.height));
}
return image;
}
bool PreGeneratedImage::operator == (const GeneratedImage& that) const {
const PreGeneratedImage* that2 = dynamic_cast<const PreGeneratedImage*>(&that);
return that2 && image.IsSameAs(that2->image);
}
14 changes: 14 additions & 0 deletions src/gfx/generated_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,17 @@ class ImageValueToImage : public GeneratedImage {
Age age; ///< Age the image was last updated
};

// ----------------------------------------------------------------------------- : PreGeneratedImage

/// Use an image from an ImageValue as an image
class PreGeneratedImage : public GeneratedImage {
public:
PreGeneratedImage(const Image& image);
~PreGeneratedImage();
Image generate(const Options& opt) const override;
bool operator == (const GeneratedImage& that) const override;
bool local() const override { return true; }
private:
PreGeneratedImage(const PreGeneratedImage&); // copy ctor
Image image;
};
13 changes: 11 additions & 2 deletions src/script/functions/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <data/stylesheet.hpp>
#include <data/symbol.hpp>
#include <data/field/symbol.hpp>
#include <data/format/formats.hpp>
#include <data/export_template.hpp>
#include <gfx/generated_image.hpp>
#include <render/symbol/filter.hpp>

Expand All @@ -24,8 +26,15 @@ void parse_enum(const String&, ImageCombine& out);
// ----------------------------------------------------------------------------- : Utility

SCRIPT_FUNCTION(to_image) {
SCRIPT_PARAM_C(GeneratedImageP, input);
return input;
SCRIPT_PARAM_C(ScriptValueP, input);
ScriptObject<CardP>* card = dynamic_cast<ScriptObject<CardP>*>(input.get());
if (card) {
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();
}

// ----------------------------------------------------------------------------- : Image functions
Expand Down
2 changes: 1 addition & 1 deletion src/script/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ wxDateTime ScriptValue::toDateTime() const {
throw ScriptErrorConversion(typeName(), _TYPE_("date"));
}
GeneratedImageP ScriptValue::toImage() const {
throw ScriptErrorConversion(typeName(), _TYPE_("image" ));
throw ScriptErrorConversion(typeName(), _TYPE_("image"));
}
String ScriptValue::toCode() const {
return toString();
Expand Down