Commit 1da6fe2b authored by twanvl's avatar twanvl

Allow symbol_variation script function to load images from the stylesheet by filename

parent 9edf0ca5
...@@ -385,19 +385,20 @@ bool BuiltInImage::operator == (const GeneratedImage& that) const { ...@@ -385,19 +385,20 @@ bool BuiltInImage::operator == (const GeneratedImage& that) const {
// ----------------------------------------------------------------------------- : SymbolToImage // ----------------------------------------------------------------------------- : SymbolToImage
SymbolToImage::SymbolToImage(const String& filename, Age age, const SymbolVariationP& variation) SymbolToImage::SymbolToImage(bool is_local, const String& filename, Age age, const SymbolVariationP& variation)
: filename(filename), age(age), variation(variation) : is_local(is_local), filename(filename), age(age), variation(variation)
{} {}
SymbolToImage::~SymbolToImage() {} SymbolToImage::~SymbolToImage() {}
Image SymbolToImage::generate(const Options& opt) const { Image SymbolToImage::generate(const Options& opt) const {
// TODO : use opt.width and opt.height? // TODO : use opt.width and opt.height?
if (!opt.local_package) throw ScriptError(_("Can only load images in a context where an image is expected")); Package* package = is_local ? opt.local_package : opt.package;
if (!package) throw ScriptError(_("Can only load images in a context where an image is expected"));
SymbolP the_symbol; SymbolP the_symbol;
if (filename.empty()) { if (filename.empty()) {
the_symbol = default_symbol(); the_symbol = default_symbol();
} else { } else {
the_symbol = opt.local_package->readFile<SymbolP>(filename); the_symbol = package->readFile<SymbolP>(filename);
} }
int size = max(100, 3*max(opt.width,opt.height)); int size = max(100, 3*max(opt.width,opt.height));
if (opt.width <= 1 || opt.height <= 1) { if (opt.width <= 1 || opt.height <= 1) {
...@@ -410,7 +411,8 @@ Image SymbolToImage::generate(const Options& opt) const { ...@@ -410,7 +411,8 @@ Image SymbolToImage::generate(const Options& opt) const {
} }
bool SymbolToImage::operator == (const GeneratedImage& that) const { bool SymbolToImage::operator == (const GeneratedImage& that) const {
const SymbolToImage* that2 = dynamic_cast<const SymbolToImage*>(&that); const SymbolToImage* that2 = dynamic_cast<const SymbolToImage*>(&that);
return that2 && filename == that2->filename return that2 && is_local == that2->is_local
&& filename == that2->filename
&& age == that2->age && age == that2->age
&& (variation == that2->variation || && (variation == that2->variation ||
*variation == *that2->variation // custom variation *variation == *that2->variation // custom variation
......
...@@ -289,19 +289,20 @@ class BuiltInImage : public GeneratedImage { ...@@ -289,19 +289,20 @@ class BuiltInImage : public GeneratedImage {
/// Use a symbol as an image /// Use a symbol as an image
class SymbolToImage : public GeneratedImage { class SymbolToImage : public GeneratedImage {
public: public:
SymbolToImage(const String& filename, Age age, const SymbolVariationP& variation); SymbolToImage(bool is_local, const String& filename, Age age, const SymbolVariationP& variation);
~SymbolToImage(); ~SymbolToImage();
virtual Image generate(const Options& opt) const; virtual Image generate(const Options& opt) const;
virtual bool operator == (const GeneratedImage& that) const; virtual bool operator == (const GeneratedImage& that) const;
virtual bool local() const { return true; } virtual bool local() const { return is_local; }
#ifdef __WXGTK__ #ifdef __WXGTK__
virtual bool threadSafe() const { return false; } virtual bool threadSafe() const { return false; }
#endif #endif
private: private:
SymbolToImage(const SymbolToImage&); // copy ctor SymbolToImage(const SymbolToImage&); // copy ctor
bool is_local; ///< Use local package?
String filename; String filename;
Age age; ///< Age the symbol was last updated Age age; ///< Age the symbol was last updated
SymbolVariationP variation; SymbolVariationP variation;
}; };
......
...@@ -112,9 +112,20 @@ SCRIPT_FUNCTION(drop_shadow) { ...@@ -112,9 +112,20 @@ SCRIPT_FUNCTION(drop_shadow) {
SCRIPT_FUNCTION(symbol_variation) { SCRIPT_FUNCTION(symbol_variation) {
// find symbol // find symbol
SCRIPT_PARAM(ValueP, symbol); SCRIPT_PARAM(ScriptValueP, symbol); // TODO: change to input?
SymbolValueP value = dynamic_pointer_cast<SymbolValue>(symbol); ScriptObject<ValueP>* valueO = dynamic_cast<ScriptObject<ValueP>*>(symbol.get());
SCRIPT_OPTIONAL_PARAM(String, variation) { SymbolValue* value = valueO ? dynamic_cast<SymbolValue*>(valueO->getValue().get()) : nullptr;
String filename;
if (value) {
filename = value->filename;
} else if (valueO) {
throw ScriptError(_ERROR_2_("can't convert", valueO->typeName(), _TYPE_("symbol" )));
} else {
filename = from_script<String>(symbol);
}
// known variation?
SCRIPT_OPTIONAL_PARAM_(String, variation)
if (value && variation_) {
// find style // find style
SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(Set*, set);
SCRIPT_OPTIONAL_PARAM_(CardP, card); SCRIPT_OPTIONAL_PARAM_(CardP, card);
...@@ -124,7 +135,7 @@ SCRIPT_FUNCTION(symbol_variation) { ...@@ -124,7 +135,7 @@ SCRIPT_FUNCTION(symbol_variation) {
FOR_EACH(v, style->variations) { FOR_EACH(v, style->variations) {
if (v->name == variation) { if (v->name == variation) {
// found it // found it
return new_intrusive3<SymbolToImage>(value->filename, value->last_update, v); return new_intrusive4<SymbolToImage>(value, filename, value->last_update, v);
} }
} }
throw ScriptError(_("Variation of symbol not found ('") + variation + _("')")); throw ScriptError(_("Variation of symbol not found ('") + variation + _("')"));
...@@ -158,7 +169,7 @@ SCRIPT_FUNCTION(symbol_variation) { ...@@ -158,7 +169,7 @@ SCRIPT_FUNCTION(symbol_variation) {
} else { } else {
throw ScriptError(_("Unknown fill type for symbol_variation: ") + fill_type); throw ScriptError(_("Unknown fill type for symbol_variation: ") + fill_type);
} }
return new_intrusive3<SymbolToImage>(value->filename, value->last_update, var); return new_intrusive4<SymbolToImage>(value, filename, value ? value->last_update : Age(), var);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment