- 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 <singular> and <plural> tags
This commit is contained in:
GenevensiS
2026-04-24 13:18:40 +02:00
parent d782e4851c
commit 0baa73ae7d
4 changed files with 59 additions and 41 deletions
+23 -15
View File
@@ -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));
+28 -12
View File
@@ -269,7 +269,9 @@ String process_english_hints(const String& str) {
String ret; ret.reserve(str.size());
// have we seen a <hint-1/2>?
// 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, _("<hint-"))) {
@@ -288,7 +290,7 @@ String process_english_hints(const String& str) {
// a -> 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 </param> tag"));
throw ScriptError(_("Incomplete </param> 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, _("<singular>"))) {
} else if (is_substr(str, i, _("<singular>"))) {
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(_("<singular> 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, _("<plural>"))) {
} else if (is_substr(str, i, _("<plural>"))) {
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(_("<plural> 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(_("<singular> tag found without matching <plural> tag."));
}
else if (sing_found < plur_found) {
throw ScriptError(_("<plural> tag found without matching <singular> tag."));
}
return ret;
}
+6 -12
View File
@@ -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()));
+2 -2
View File
@@ -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()));
}