- 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
+22 -14
View File
@@ -787,13 +787,20 @@ bool SetMetadataImage::operator == (const GeneratedImage& that) const {
ImportedImage::ImportedImage(Set* set, const String& filepath) ImportedImage::ImportedImage(Set* set, const String& filepath)
{ {
loadpath = filepath;
// has the set already been saved at least once? // 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? // 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? // is the file an image?
Image img; Image img;
@@ -801,9 +808,6 @@ ImportedImage::ImportedImage(Set* set, const String& filepath)
if (!img.IsOk()) throw ScriptError(_ERROR_1_("import not image", 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 // 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); auto outStream = set->openOut(savename);
img.SaveFile(*outStream, wxBITMAP_TYPE_PNG); img.SaveFile(*outStream, wxBITMAP_TYPE_PNG);
if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", loadpath)); 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) DownloadedImage::DownloadedImage(Set* set, const String& url)
{ {
loadpath = url;
// has the set already been saved at least once? // 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? // can we download the data?
WebRequestWindow wnd(loadpath); 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? // is the data an image?
const String& content_type = wnd.out.GetContentType(); 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")); if (!img.IsOk()) throw ScriptError(_ERROR_("web request corrupted"));
// add the file to the set (or overwrite it if pre-existing), save set // 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); auto outStream = set->openOut(savename);
img.SaveFile(*outStream, wxBITMAP_TYPE_PNG); img.SaveFile(*outStream, wxBITMAP_TYPE_PNG);
if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", loadpath)); if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", loadpath));
+23 -7
View File
@@ -270,6 +270,8 @@ String process_english_hints(const String& str) {
// have we seen a <hint-1/2>? // have we seen a <hint-1/2>?
// 1 for singular, 2 for plural // 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() ; ) { for (size_t i = 0 ; i < str.size() ; ) {
Char c = str.GetChar(i); Char c = str.GetChar(i);
if (is_substr(str, i, _("<hint-"))) { 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) // a -> an, where the a originates from english_number_a(1)
size_t after_end = skip_tag(str,after+1); size_t after_end = skip_tag(str,after+1);
if (after_end == String::npos) { 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() if (after_end != String::npos && after_end + 1 < str.size()
&& isSpace(str.GetChar(after_end)) && is_vowel(str.GetChar(after_end+1))) { && isSpace(str.GetChar(after_end)) && is_vowel(str.GetChar(after_end+1))) {
@@ -314,22 +316,30 @@ String process_english_hints(const String& str) {
ret.append(str,i,min(after,str.size())-i); ret.append(str,i,min(after,str.size())-i);
i = after; i = after;
} else if (is_substr(str, i, _("<singular>"))) { } else if (is_substr(str, i, _("<singular>"))) {
sing_found++;
// singular -> keep, plural -> drop // singular -> keep, plural -> drop
size_t start = skip_tag(str, i); size_t start = skip_tag(str, i);
size_t end = match_close_tag(str, start); size_t end = match_close_tag(str, i);
if (singplur == 1 && end != String::npos) { if (end == String::npos) {
ret += str.substr(start, end - start); 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); 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 // singular -> drop, plural -> keep
size_t start = skip_tag(str, i); size_t start = skip_tag(str, i);
size_t end = match_close_tag(str, start); size_t end = match_close_tag(str, i);
if (singplur == 2 && end != String::npos) { if (end == String::npos) {
ret += str.substr(start, end - start); 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); i = skip_tag(str, end);
} else if (c == _('(') && singplur) { } else if (c == _('(') && singplur) {
// singular -> drop (...), plural -> keep it // singular -> drop (...), plural -> keep it
@@ -353,6 +363,12 @@ String process_english_hints(const String& str) {
++i; ++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; return ret;
} }
+3 -9
View File
@@ -81,28 +81,22 @@ SCRIPT_FUNCTION(set_metadata) {
SCRIPT_FUNCTION(width_of) { SCRIPT_FUNCTION(width_of) {
SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(Set*, set);
SCRIPT_PARAM(CardP, card);
SCRIPT_PARAM(GeneratedImageP, input); SCRIPT_PARAM(GeneratedImageP, input);
StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); Image image = input->generate(GeneratedImage::Options(0, 0, set, set));
Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet));
SCRIPT_RETURN(image.GetWidth()); SCRIPT_RETURN(image.GetWidth());
} }
SCRIPT_FUNCTION(height_of) { SCRIPT_FUNCTION(height_of) {
SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(Set*, set);
SCRIPT_PARAM(CardP, card);
SCRIPT_PARAM(GeneratedImageP, input); SCRIPT_PARAM(GeneratedImageP, input);
StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); Image image = input->generate(GeneratedImage::Options(0, 0, set, set));
Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet));
SCRIPT_RETURN(image.GetHeight()); SCRIPT_RETURN(image.GetHeight());
} }
SCRIPT_FUNCTION(dimensions_of) { SCRIPT_FUNCTION(dimensions_of) {
SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(Set*, set);
SCRIPT_PARAM(CardP, card);
SCRIPT_PARAM(GeneratedImageP, input); SCRIPT_PARAM(GeneratedImageP, input);
StyleSheet* stylesheet = card && card->stylesheet ? card->stylesheet.get() : set->stylesheet.get(); Image image = input->generate(GeneratedImage::Options(0, 0, set, set));
Image image = input->generate(GeneratedImage::Options(0, 0, stylesheet));
ScriptCustomCollectionP ret(new ScriptCustomCollection()); ScriptCustomCollectionP ret(new ScriptCustomCollection());
ret->value.push_back(to_script(image.GetWidth())); ret->value.push_back(to_script(image.GetWidth()));
ret->value.push_back(to_script(image.GetHeight())); 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) { void Packaged::requireDependency(Packaged* package) {
if (package == this) return; // dependency on self if (package == this) return; // dependency on self
if (this->relativeFilename().find("mse-set") != String::npos) return; // skip checks on sets
String n = package->relativeFilename(); String n = package->relativeFilename();
if (n.find("mse-symbol-font") != String::npos) return; // skip checks for symbol-fonts
FOR_EACH(dep, dependencies) { FOR_EACH(dep, dependencies) {
if (dep->package == n) { if (dep->package == n) {
if (package->version < dep->version) { 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 // dependency not found
queue_message(MESSAGE_WARNING,_ERROR_4_("dependency not given", name(), package->relativeFilename(), package->relativeFilename(), package->version.toString())); queue_message(MESSAGE_WARNING,_ERROR_4_("dependency not given", name(), package->relativeFilename(), package->relativeFilename(), package->version.toString()));
} }