Skip to content

Commit 709ba45

Browse files
committed
fixes xmp encoding of multiple items with same lang for langAlt type properties
1 parent 32be814 commit 709ba45

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/xmp.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,13 +839,36 @@ int XmpParser::encode(std::string& xmpPacket, const XmpData& xmpData, uint16_t f
839839
if (!la)
840840
throw Error(ErrorCode::kerEncodeLangAltPropertyFailed, xmp.key());
841841

842-
int idx = 1;
843842
for (const auto& [lang, specs] : la->value_) {
844843
if (!specs.empty()) { // remove lang specs with no value
845844
printNode(ns, xmp.tagName(), specs, 0);
846-
meta.AppendArrayItem(ns.c_str(), xmp.tagName().c_str(), kXMP_PropArrayIsAlternate, specs.c_str());
847-
const std::string item = xmp.tagName() + "[" + toString(idx++) + "]";
848-
meta.SetQualifier(ns.c_str(), item.c_str(), kXMP_NS_XML, "lang", lang.c_str());
845+
846+
// check if there is an item in the array with given lang
847+
// I am sure this can be rewritten in a more optimal fashion
848+
std::size_t item_cnt = meta.CountArrayItems(ns.c_str(), xmp.tagName().c_str());
849+
std::size_t existing_item_idx = 0; // 0 means it does not exist
850+
for (std::size_t i = 1; i <= item_cnt; ++i) {
851+
std::string qualifier_value;
852+
XMP_OptionBits qualifier_options;
853+
854+
const std::string item = xmp.tagName() + "[" + toString(i) + "]";
855+
856+
auto found = meta.GetQualifier(ns.c_str(), item.c_str(), kXMP_NS_XML, "lang", &qualifier_value,
857+
&qualifier_options);
858+
if (found and qualifier_value == lang) {
859+
existing_item_idx = i;
860+
break;
861+
}
862+
}
863+
864+
if (existing_item_idx) {
865+
meta.SetArrayItem(ns.c_str(), xmp.tagName().c_str(), existing_item_idx, specs.c_str());
866+
} else {
867+
meta.AppendArrayItem(ns.c_str(), xmp.tagName().c_str(), kXMP_PropArrayIsAlternate, specs.c_str());
868+
auto index = meta.CountArrayItems(ns.c_str(), xmp.tagName().c_str());
869+
const std::string item = xmp.tagName() + "[" + toString(index) + "]";
870+
meta.SetQualifier(ns.c_str(), item.c_str(), kXMP_NS_XML, "lang", lang.c_str());
871+
}
849872
}
850873
}
851874
continue;

0 commit comments

Comments
 (0)