Skip to content

Commit

Permalink
Quote x_font property (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Feb 2, 2024
1 parent 7755979 commit bfe8104
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions gtk/src/hocr/HOCRDocument.cc
Original file line number Diff line number Diff line change
Expand Up @@ -885,15 +885,24 @@ std::map<Glib::ustring, Glib::ustring> HOCRItem::deserializeAttrGroup(const Glib
for(const Glib::ustring& attr : Utils::string_split(string, ';', false)) {
Glib::ustring trimmed = Utils::string_trim(attr);
int splitPos = trimmed.find_first_of(' ');
attrs.insert(std::make_pair(trimmed.substr(0, splitPos), splitPos > 0 ? Utils::string_trim(trimmed.substr(splitPos + 1)) : ""));
Glib::ustring key = trimmed.substr(0, splitPos);
Glib::ustring value = splitPos > 0 ? Utils::string_trim(trimmed.substr(splitPos + 1)) : "";
if (key == "x_font" && value.length() >= 2 && Utils::string_startswith(value, "'") && Utils::string_endswith(value, "'")) {
value = value.substr(1, value.length() - 2);
}
attrs.insert(std::make_pair(key, value));
}
return attrs;
}

Glib::ustring HOCRItem::serializeAttrGroup(const std::map<Glib::ustring, Glib::ustring>& attrs) {
std::vector<Glib::ustring> list;
for(auto it = attrs.begin(), itEnd = attrs.end(); it != itEnd; ++it) {
list.push_back(Glib::ustring::compose("%1 %2", it->first, it->second));
Glib::ustring value = it->second;
if (it->first == "x_font" && !value.empty()) {
value = Glib::ustring::compose("'%1'", value);
}
list.push_back(Glib::ustring::compose("%1 %2", it->first, value));
}
return Utils::string_join(list, "; ");
}
Expand Down
13 changes: 11 additions & 2 deletions qt/src/hocr/HOCRDocument.cc
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,24 @@ QMap<QString, QString> HOCRItem::deserializeAttrGroup(const QString& string) {
QMap<QString, QString> attrs;
for(const QString& attr : string.split(QRegularExpression("\\s*;\\s*"))) {
int splitPos = attr.indexOf(QRegularExpression("\\s+"));
attrs.insert(attr.left(splitPos), splitPos > 0 ? attr.mid(splitPos + 1) : "");
QString key = attr.left(splitPos);
QString value = splitPos > 0 ? attr.mid(splitPos + 1) : "";
if (key == "x_font" && value.length() >= 2 && value.startsWith('\'') && value.endsWith('\'')) {
value = value.mid(1, value.length() - 2);
}
attrs.insert(key, value);
}
return attrs;
}

QString HOCRItem::serializeAttrGroup(const QMap<QString, QString>& attrs) {
QStringList list;
for(auto it = attrs.begin(), itEnd = attrs.end(); it != itEnd; ++it) {
list.append(QString("%1 %2").arg(it.key(), it.value()));
QString value = it.value();
if (it.key() == "x_font" && !value.isEmpty()) {
value = QString("'%1'").arg(value);
}
list.append(QString("%1 %2").arg(it.key(), value));
}
return list.join("; ");
}
Expand Down

0 comments on commit bfe8104

Please sign in to comment.