From 0baa73ae7d055940e85ee153d79bf9ab4f05f350 Mon Sep 17 00:00:00 2001 From: GenevensiS <66968533+G-e-n-e-v-e-n-s-i-S@users.noreply.github.com> Date: Fri, 24 Apr 2026 13:18:40 +0200 Subject: [PATCH] fix bugs - dimensions_of should now work in all contexts - import_image now works in style files, although should be avoided, since it reloads the file every time the code in run, it should be used in one shot scripts, like import scripts and bulk modification scripts - process_english_hints now correctly processes and tags --- src/gfx/generated_image.cpp | 38 ++++++++++++++++++------------ src/script/functions/english.cpp | 40 ++++++++++++++++++++++---------- src/script/functions/image.cpp | 18 +++++--------- src/util/io/package.cpp | 4 ++-- 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/gfx/generated_image.cpp b/src/gfx/generated_image.cpp index a821446e..fcc5b198 100644 --- a/src/gfx/generated_image.cpp +++ b/src/gfx/generated_image.cpp @@ -787,23 +787,27 @@ bool SetMetadataImage::operator == (const GeneratedImage& that) const { ImportedImage::ImportedImage(Set* set, const String& filepath) { - loadpath = filepath; - // has the set already been saved at least once? - if (set->needSaveAs()) throw ScriptError(_ERROR_1_("can't import image without set", loadpath)); + if (set->needSaveAs()) throw ScriptError(_ERROR_1_("can't import image without set", filepath)); + + // determine save name + loadpath = filepath; + savename = normalize_internal_filename(loadpath + _(".png")); + savename.Replace(":", "-"); + savename.Replace("/", "-"); // does the file pointed to by filepath exist? - if (!wxFileName(loadpath, wxPATH_UNIX).FileExists()) throw ScriptError(_ERROR_1_("import not found", loadpath)); - + if (!wxFileName(loadpath, wxPATH_UNIX).FileExists()) { + if (set->contains(savename)) return; + else throw ScriptError(_ERROR_1_("import not found", loadpath)); + } + // is the file an image? Image img; img.LoadFile(loadpath); if (!img.IsOk()) throw ScriptError(_ERROR_1_("import not image", loadpath)); // add the file to the set (or overwrite it if pre-existing), save set - savename = normalize_internal_filename(loadpath + _(".png")); - savename.Replace(":", "-"); - savename.Replace("/", "-"); auto outStream = set->openOut(savename); img.SaveFile(*outStream, wxBITMAP_TYPE_PNG); if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", loadpath)); @@ -829,14 +833,21 @@ bool ImportedImage::operator == (const GeneratedImage& that) const { DownloadedImage::DownloadedImage(Set* set, const String& url) { - loadpath = url; - // has the set already been saved at least once? - if (set->needSaveAs()) throw ScriptError(_ERROR_1_("can't download image without set", loadpath)); + if (set->needSaveAs()) throw ScriptError(_ERROR_1_("can't download image without set", url)); + + // determine save name + loadpath = url; + savename = normalize_internal_filename(loadpath + _(".png")); + savename.Replace(":", "-"); + savename.Replace("/", "-"); // can we download the data? WebRequestWindow wnd(loadpath); - if (wnd.ShowModal() != wxID_OK) throw ScriptError(_ERROR_1_("can't download image", loadpath)); + if (wnd.ShowModal() != wxID_OK) { + if (set->contains(savename)) return; + else throw ScriptError(_ERROR_1_("can't download image", loadpath)); + } // is the data an image? const String& content_type = wnd.out.GetContentType(); @@ -845,9 +856,6 @@ DownloadedImage::DownloadedImage(Set* set, const String& url) if (!img.IsOk()) throw ScriptError(_ERROR_("web request corrupted")); // add the file to the set (or overwrite it if pre-existing), save set - savename = normalize_internal_filename(loadpath + _(".png")); - savename.Replace(":", "-"); - savename.Replace("/", "-"); auto outStream = set->openOut(savename); img.SaveFile(*outStream, wxBITMAP_TYPE_PNG); if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", loadpath)); diff --git a/src/script/functions/english.cpp b/src/script/functions/english.cpp index c8f87450..88127a61 100644 --- a/src/script/functions/english.cpp +++ b/src/script/functions/english.cpp @@ -269,7 +269,9 @@ String process_english_hints(const String& str) { String ret; ret.reserve(str.size()); // have we seen a ? // 1 for singular, 2 for plural - int singplur = 0; + int singplur = 0; + int sing_found = 0; + int plur_found = 0; for (size_t i = 0 ; i < str.size() ; ) { Char c = str.GetChar(i); if (is_substr(str, i, _(" an, where the a originates from english_number_a(1) size_t after_end = skip_tag(str,after+1); if (after_end == String::npos) { - throw Error(_("Incomplete tag")); + throw ScriptError(_("Incomplete tag")); } if (after_end != String::npos && after_end + 1 < str.size() && isSpace(str.GetChar(after_end)) && is_vowel(str.GetChar(after_end+1))) { @@ -313,23 +315,31 @@ String process_english_hints(const String& str) { } ret.append(str,i,min(after,str.size())-i); i = after; - } else if (is_substr(str, i, _(""))) { + } else if (is_substr(str, i, _(""))) { + sing_found++; // singular -> keep, plural -> drop size_t start = skip_tag(str, i); - size_t end = match_close_tag(str, start); - if (singplur == 1 && end != String::npos) { - ret += str.substr(start, end - start); + size_t end = match_close_tag(str, i); + if (end == String::npos) { + throw ScriptError(_(" tag found without matching closing tag.")); + } + else if (singplur == 1) { + ret += str.substr(start, end - start); + singplur = 0; } - singplur = 0; i = skip_tag(str, end); - } else if (is_substr(str, i, _(""))) { + } else if (is_substr(str, i, _(""))) { + plur_found++; // singular -> drop, plural -> keep size_t start = skip_tag(str, i); - size_t end = match_close_tag(str, start); - if (singplur == 2 && end != String::npos) { - ret += str.substr(start, end - start); + size_t end = match_close_tag(str, i); + if (end == String::npos) { + throw ScriptError(_(" tag found without matching closing tag.")); + } + else if (singplur == 2) { + ret += str.substr(start, end - start); + singplur = 0; } - singplur = 0; i = skip_tag(str, end); } else if (c == _('(') && singplur) { // singular -> drop (...), plural -> keep it @@ -352,6 +362,12 @@ String process_english_hints(const String& str) { ret += c; ++i; } + } + if (sing_found > plur_found) { + throw ScriptError(_(" tag found without matching tag.")); + } + else if (sing_found < plur_found) { + throw ScriptError(_(" tag found without matching tag.")); } return ret; } diff --git a/src/script/functions/image.cpp b/src/script/functions/image.cpp index 3143c7ef..e25193cd 100644 --- a/src/script/functions/image.cpp +++ b/src/script/functions/image.cpp @@ -81,28 +81,22 @@ SCRIPT_FUNCTION(set_metadata) { SCRIPT_FUNCTION(width_of) { SCRIPT_PARAM(Set*, set); - SCRIPT_PARAM(CardP, card); - SCRIPT_PARAM(GeneratedImageP, input); - StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); - Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet)); + SCRIPT_PARAM(GeneratedImageP, input); + Image image = input->generate(GeneratedImage::Options(0, 0, set, set)); SCRIPT_RETURN(image.GetWidth()); } SCRIPT_FUNCTION(height_of) { SCRIPT_PARAM(Set*, set); - SCRIPT_PARAM(CardP, card); - SCRIPT_PARAM(GeneratedImageP, input); - StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); - Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet)); + SCRIPT_PARAM(GeneratedImageP, input); + Image image = input->generate(GeneratedImage::Options(0, 0, set, set)); SCRIPT_RETURN(image.GetHeight()); } SCRIPT_FUNCTION(dimensions_of) { SCRIPT_PARAM(Set*, set); - SCRIPT_PARAM(CardP, card); - SCRIPT_PARAM(GeneratedImageP, input); - StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); - Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet)); + SCRIPT_PARAM(GeneratedImageP, input); + Image image = input->generate(GeneratedImage::Options(0, 0, set, set)); ScriptCustomCollectionP ret(new ScriptCustomCollection()); ret->value.push_back(to_script(image.GetWidth())); ret->value.push_back(to_script(image.GetHeight())); diff --git a/src/util/io/package.cpp b/src/util/io/package.cpp index 97749971..5b38d001 100644 --- a/src/util/io/package.cpp +++ b/src/util/io/package.cpp @@ -684,7 +684,9 @@ void Packaged::validate(Version) { void Packaged::requireDependency(Packaged* package) { if (package == this) return; // dependency on self + if (this->relativeFilename().find("mse-set") != String::npos) return; // skip checks on sets String n = package->relativeFilename(); + if (n.find("mse-symbol-font") != String::npos) return; // skip checks for symbol-fonts FOR_EACH(dep, dependencies) { if (dep->package == n) { if (package->version < dep->version) { @@ -696,8 +698,6 @@ void Packaged::requireDependency(Packaged* package) { } } } - // skip dependency checks for symbol-fonts - if (package->relativeFilename().find("mse-symbol-font") != std::string::npos) return; // dependency not found queue_message(MESSAGE_WARNING,_ERROR_4_("dependency not given", name(), package->relativeFilename(), package->relativeFilename(), package->version.toString())); }