diff --git a/src/core/control/xojfile/SaveHandler.cpp b/src/core/control/xojfile/SaveHandler.cpp index 5c38441160be..e243cb952d7a 100644 --- a/src/core/control/xojfile/SaveHandler.cpp +++ b/src/core/control/xojfile/SaveHandler.cpp @@ -16,6 +16,7 @@ #include "control/xml/XmlPointNode.h" // for XmlPointNode #include "control/xml/XmlTexNode.h" // for XmlTexNode #include "control/xml/XmlTextNode.h" // for XmlTextNode +#include "control/xojfile/XmlNames.h" // for XmlAttrs #include "model/AudioElement.h" // for AudioElement #include "model/BackgroundImage.h" // for BackgroundImage #include "model/Document.h" // for Document @@ -76,8 +77,8 @@ void SaveHandler::prepareSave(Document* doc) { } void SaveHandler::writeHeader() { - this->root->setAttrib("creator", PROJECT_STRING); - this->root->setAttrib("fileversion", FILE_FORMAT_VERSION); + this->root->setAttrib(XmlAttrs::CREATOR, PROJECT_STRING); + this->root->setAttrib(XmlAttrs::FILEVERSION, FILE_FORMAT_VERSION); this->root->addChild(new XmlTextNode("title", std::string{"Xournal++ document - see "} + PROJECT_HOMEPAGE_URL)); } @@ -91,8 +92,8 @@ auto SaveHandler::getColorStr(Color c, unsigned char alpha) -> std::string { void SaveHandler::writeTimestamp(AudioElement* audioElement, XmlAudioNode* xmlAudioNode) { if (!audioElement->getAudioFilename().empty()) { /** set stroke timestamp value to the XmlPointNode */ - xmlAudioNode->setAttrib("ts", audioElement->getTimestamp()); - xmlAudioNode->setAttrib("fn", audioElement->getAudioFilename().u8string()); + xmlAudioNode->setAttrib(XmlAttrs::TS, audioElement->getTimestamp()); + xmlAudioNode->setAttrib(XmlAttrs::FN, audioElement->getAudioFilename().u8string()); } } @@ -102,19 +103,19 @@ void SaveHandler::visitStroke(XmlPointNode* stroke, Stroke* s) { unsigned char alpha = 0xff; if (t == StrokeTool::PEN) { - stroke->setAttrib("tool", "pen"); + stroke->setAttrib(XmlAttrs::TOOL, "pen"); writeTimestamp(s, stroke); } else if (t == StrokeTool::ERASER) { - stroke->setAttrib("tool", "eraser"); + stroke->setAttrib(XmlAttrs::TOOL, "eraser"); } else if (t == StrokeTool::HIGHLIGHTER) { - stroke->setAttrib("tool", "highlighter"); + stroke->setAttrib(XmlAttrs::TOOL, "highlighter"); alpha = 0x7f; } else { g_warning("Unknown StrokeTool::Value"); - stroke->setAttrib("tool", "pen"); + stroke->setAttrib(XmlAttrs::TOOL, "pen"); } - stroke->setAttrib("color", getColorStr(s->getColor(), alpha).c_str()); + stroke->setAttrib(XmlAttrs::COLOR, getColorStr(s->getColor(), alpha).c_str()); const auto& pts = s->getPointVector(); @@ -125,9 +126,9 @@ void SaveHandler::visitStroke(XmlPointNode* stroke, Stroke* s) { values.reserve(pts.size() + 1); values.emplace_back(s->getWidth()); std::transform(pts.begin(), pts.end() - 1, std::back_inserter(values), [](const Point& p) { return p.z; }); - stroke->setAttrib("width", std::move(values)); + stroke->setAttrib(XmlAttrs::WIDTH, std::move(values)); } else { - stroke->setAttrib("width", s->getWidth()); + stroke->setAttrib(XmlAttrs::WIDTH, s->getWidth()); } visitStrokeExtended(stroke, s); @@ -138,23 +139,23 @@ void SaveHandler::visitStroke(XmlPointNode* stroke, Stroke* s) { */ void SaveHandler::visitStrokeExtended(XmlPointNode* stroke, Stroke* s) { if (s->getFill() != -1) { - stroke->setAttrib("fill", s->getFill()); + stroke->setAttrib(XmlAttrs::FILL, s->getFill()); } const StrokeCapStyle capStyle = s->getStrokeCapStyle(); if (capStyle == StrokeCapStyle::BUTT) { - stroke->setAttrib("capStyle", "butt"); + stroke->setAttrib(XmlAttrs::CAPSTYLE, "butt"); } else if (capStyle == StrokeCapStyle::ROUND) { - stroke->setAttrib("capStyle", "round"); + stroke->setAttrib(XmlAttrs::CAPSTYLE, "round"); } else if (capStyle == StrokeCapStyle::SQUARE) { - stroke->setAttrib("capStyle", "square"); + stroke->setAttrib(XmlAttrs::CAPSTYLE, "square"); } else { g_warning("Unknown stroke cap type: %i", capStyle); - stroke->setAttrib("capStyle", "round"); + stroke->setAttrib(XmlAttrs::CAPSTYLE, "round"); } if (s->getLineStyle().hasDashes()) { - stroke->setAttrib("style", StrokeStyle::formatStyle(s->getLineStyle())); + stroke->setAttrib(XmlAttrs::STYLE, StrokeStyle::formatStyle(s->getLineStyle())); } } @@ -162,7 +163,7 @@ void SaveHandler::visitLayer(XmlNode* page, Layer* l) { auto* layer = new XmlNode("layer"); page->addChild(layer); if (l->hasName()) { - layer->setAttrib("name", l->getName().c_str()); + layer->setAttrib(XmlAttrs::NAME, l->getName().c_str()); } for (auto&& e: l->getElements()) { @@ -178,11 +179,11 @@ void SaveHandler::visitLayer(XmlNode* page, Layer* l) { XojFont& f = t->getFont(); - text->setAttrib("font", f.getName().c_str()); - text->setAttrib("size", f.getSize()); - text->setAttrib("x", t->getX()); - text->setAttrib("y", t->getY()); - text->setAttrib("color", getColorStr(t->getColor()).c_str()); + text->setAttrib(XmlAttrs::FONT, f.getName().c_str()); + text->setAttrib(XmlAttrs::SIZE, f.getSize()); + text->setAttrib(XmlAttrs::X, t->getX()); + text->setAttrib(XmlAttrs::Y, t->getY()); + text->setAttrib(XmlAttrs::COLOR, getColorStr(t->getColor()).c_str()); writeTimestamp(t, text); } else if (e->getType() == ELEMENT_IMAGE) { @@ -192,20 +193,20 @@ void SaveHandler::visitLayer(XmlNode* page, Layer* l) { image->setImage(i->getImage()); - image->setAttrib("left", i->getX()); - image->setAttrib("top", i->getY()); - image->setAttrib("right", i->getX() + i->getElementWidth()); - image->setAttrib("bottom", i->getY() + i->getElementHeight()); + image->setAttrib(XmlAttrs::LEFT, i->getX()); + image->setAttrib(XmlAttrs::TOP, i->getY()); + image->setAttrib(XmlAttrs::RIGHT, i->getX() + i->getElementWidth()); + image->setAttrib(XmlAttrs::BOTTOM, i->getY() + i->getElementHeight()); } else if (e->getType() == ELEMENT_TEXIMAGE) { auto* i = dynamic_cast(e.get()); auto* image = new XmlTexNode("teximage", std::string(i->getBinaryData())); layer->addChild(image); - image->setAttrib("text", i->getText().c_str()); - image->setAttrib("left", i->getX()); - image->setAttrib("top", i->getY()); - image->setAttrib("right", i->getX() + i->getElementWidth()); - image->setAttrib("bottom", i->getY() + i->getElementHeight()); + image->setAttrib(XmlAttrs::TEXT, i->getText().c_str()); + image->setAttrib(XmlAttrs::LEFT, i->getX()); + image->setAttrib(XmlAttrs::TOP, i->getY()); + image->setAttrib(XmlAttrs::RIGHT, i->getX() + i->getElementWidth()); + image->setAttrib(XmlAttrs::BOTTOM, i->getY() + i->getElementHeight()); } } } @@ -213,8 +214,8 @@ void SaveHandler::visitLayer(XmlNode* page, Layer* l) { void SaveHandler::visitPage(XmlNode* root, PageRef p, Document* doc, int id) { auto* page = new XmlNode("page"); root->addChild(page); - page->setAttrib("width", p->getWidth()); - page->setAttrib("height", p->getHeight()); + page->setAttrib(XmlAttrs::WIDTH, p->getWidth()); + page->setAttrib(XmlAttrs::HEIGHT, p->getHeight()); auto* background = new XmlNode("background"); page->addChild(background); @@ -227,16 +228,16 @@ void SaveHandler::visitPage(XmlNode* root, PageRef p, Document* doc, int id) { * DO NOT CHANGE THE ORDER OF THE ATTRIBUTES! */ - background->setAttrib("type", "pdf"); + background->setAttrib(XmlAttrs::TYPE, "pdf"); if (!firstPdfPageVisited) { firstPdfPageVisited = true; if (doc->isAttachPdf()) { - background->setAttrib("domain", "attach"); + background->setAttrib(XmlAttrs::DOMAIN, "attach"); auto filepath = doc->getFilepath(); Util::clearExtensions(filepath); filepath += ".xopp.bg.pdf"; - background->setAttrib("filename", "bg.pdf"); + background->setAttrib(XmlAttrs::FILENAME, "bg.pdf"); GError* error = nullptr; if (!exists(filepath)) { @@ -253,24 +254,24 @@ void SaveHandler::visitPage(XmlNode* root, PageRef p, Document* doc, int id) { g_error_free(error); } } else { - background->setAttrib("domain", "absolute"); - background->setAttrib("filename", doc->getPdfFilepath().string()); + background->setAttrib(XmlAttrs::DOMAIN, "absolute"); + background->setAttrib(XmlAttrs::FILENAME, doc->getPdfFilepath().string()); } } - background->setAttrib("pageno", p->getPdfPageNr() + 1); + background->setAttrib(XmlAttrs::PAGENO, p->getPdfPageNr() + 1); } else if (p->getBackgroundType().isImagePage()) { - background->setAttrib("type", "pixmap"); + background->setAttrib(XmlAttrs::TYPE, "pixmap"); int cloneId = p->getBackgroundImage().getCloneId(); if (cloneId != -1) { - background->setAttrib("domain", "clone"); + background->setAttrib(XmlAttrs::DOMAIN, "clone"); char* filename = g_strdup_printf("%i", cloneId); - background->setAttrib("filename", filename); + background->setAttrib(XmlAttrs::FILENAME, filename); g_free(filename); } else if (p->getBackgroundImage().isAttached() && p->getBackgroundImage().getPixbuf()) { char* filename = g_strdup_printf("bg_%d.png", this->attachBgId++); - background->setAttrib("domain", "attach"); - background->setAttrib("filename", filename); + background->setAttrib(XmlAttrs::DOMAIN, "attach"); + background->setAttrib(XmlAttrs::FILENAME, filename); p->getBackgroundImage().setFilepath(filename); backgroundImages.emplace_back(p->getBackgroundImage()); @@ -278,8 +279,8 @@ void SaveHandler::visitPage(XmlNode* root, PageRef p, Document* doc, int id) { g_free(filename); p->getBackgroundImage().setCloneId(id); } else { - background->setAttrib("domain", "absolute"); - background->setAttrib("filename", p->getBackgroundImage().getFilepath().string()); + background->setAttrib(XmlAttrs::DOMAIN, "absolute"); + background->setAttrib(XmlAttrs::FILENAME, p->getBackgroundImage().getFilepath().string()); p->getBackgroundImage().setCloneId(id); } } else { @@ -298,20 +299,20 @@ void SaveHandler::visitPage(XmlNode* root, PageRef p, Document* doc, int id) { } void SaveHandler::writeSolidBackground(XmlNode* background, PageRef p) { - background->setAttrib("type", "solid"); - background->setAttrib("color", getColorStr(p->getBackgroundColor())); - background->setAttrib("style", PageTypeHandler::getStringForPageTypeFormat(p->getBackgroundType().format)); + background->setAttrib(XmlAttrs::TYPE, "solid"); + background->setAttrib(XmlAttrs::COLOR, getColorStr(p->getBackgroundColor())); + background->setAttrib(XmlAttrs::STYLE, PageTypeHandler::getStringForPageTypeFormat(p->getBackgroundType().format)); // Not compatible with Xournal, so the background needs // to be changed to a basic one! if (!p->getBackgroundType().config.empty()) { - background->setAttrib("config", p->getBackgroundType().config); + background->setAttrib(XmlAttrs::CONFIG, p->getBackgroundType().config); } } void SaveHandler::writeBackgroundName(XmlNode* background, PageRef p) { if (p->backgroundHasName()) { - background->setAttrib("name", p->getBackgroundName()); + background->setAttrib(XmlAttrs::NAME, p->getBackgroundName()); } } diff --git a/src/core/control/xojfile/XmlNames.h b/src/core/control/xojfile/XmlNames.h new file mode 100644 index 000000000000..041b72ae5264 --- /dev/null +++ b/src/core/control/xojfile/XmlNames.h @@ -0,0 +1,65 @@ +/* + * Xournal++ + * + * contexpr names of the attributes used in .xoj and .xopp files + * + * @author Xournal++ Team + * https://github.com/xournalpp/xournalpp + * + * @license GNU GPLv2 or later + */ + +#pragma once + + +// Names of attributes +namespace XmlAttrs { + +// xournal +constexpr auto CREATOR = "creator"; +constexpr auto VERSION = "version"; // also in MrWriter +constexpr auto FILEVERSION = "fileversion"; + +// page +constexpr auto WIDTH = "width"; // also in stroke +constexpr auto HEIGHT = "height"; + +// background +constexpr auto NAME = "name"; // also in layer +constexpr auto TYPE = "type"; +constexpr auto STYLE = "style"; // also in stroke +constexpr auto CONFIG = "config"; +constexpr auto COLOR = "color"; // also in stroke and text +constexpr auto DOMAIN = "domain"; +constexpr auto FILENAME = "filename"; +constexpr auto PAGENO = "pageno"; + +// timestamp +constexpr auto FN = "fn"; // also in stroke, text and audio +constexpr auto TS = "ts"; // also in stroke and text + +// stroke +constexpr auto TOOL = "tool"; +constexpr auto PRESSURES = "pressures"; +constexpr auto FILL = "fill"; +constexpr auto CAPSTYLE = "capStyle"; + +// text +constexpr auto FONT = "font"; +constexpr auto SIZE = "size"; +constexpr auto X = "x"; +constexpr auto Y = "y"; + +// image +constexpr auto LEFT = "left"; // also in teximage +constexpr auto TOP = "top"; // also in teximage +constexpr auto RIGHT = "right"; // also in teximage +constexpr auto BOTTOM = "bottom"; // also in teximage + +// teximage +constexpr auto TEXT = "text"; + +// attachment +constexpr auto PATH = "path"; + +} // namespace XmlAttrs diff --git a/src/core/control/xojfile/XmlParser.cpp b/src/core/control/xojfile/XmlParser.cpp index 0fb2c3adf7dd..10feca227259 100644 --- a/src/core/control/xojfile/XmlParser.cpp +++ b/src/core/control/xojfile/XmlParser.cpp @@ -16,6 +16,7 @@ #include "control/pagetype/PageTypeHandler.h" // for PageTypeHandler #include "control/xojfile/InputStream.h" // for InputStream #include "control/xojfile/LoadHandler.h" // for LoadHandler +#include "control/xojfile/XmlNames.h" // for XmlAttrs #include "control/xojfile/XmlParserHelper.h" // for getAttrib... #include "model/PageType.h" // for PageType #include "model/Point.h" // for Point @@ -376,12 +377,12 @@ void XmlParser::parseXournalTag() { const auto attributeMap = getAttributeMap(); std::string creator; - const auto optCreator = XmlParserHelper::getAttrib("creator", attributeMap); + const auto optCreator = XmlParserHelper::getAttrib(XmlAttrs::CREATOR, attributeMap); if (optCreator) { creator = *optCreator; } else { // Compatibility: the creator attribute exists since 7017b71. Before that, only a version string was written - const auto optVersion = XmlParserHelper::getAttrib("version", attributeMap); + const auto optVersion = XmlParserHelper::getAttrib(XmlAttrs::VERSION, attributeMap); if (optVersion) { creator = "Xournal " + *optVersion; } else { @@ -389,7 +390,7 @@ void XmlParser::parseXournalTag() { } } - const auto fileversion = XmlParserHelper::getAttribMandatory("fileversion", attributeMap, 1); + const auto fileversion = XmlParserHelper::getAttribMandatory(XmlAttrs::FILEVERSION, attributeMap, 1); this->handler->addXournal(creator, fileversion); } @@ -398,7 +399,7 @@ void XmlParser::parseMrWriterTag() { const auto attributeMap = getAttributeMap(); std::string creator; - auto optVersion = XmlParserHelper::getAttrib("verison", attributeMap); + auto optVersion = XmlParserHelper::getAttrib(XmlAttrs::VERSION, attributeMap); if (optVersion) { creator = "MrWriter " + *optVersion; } else { @@ -411,8 +412,8 @@ void XmlParser::parseMrWriterTag() { void XmlParser::parsePageTag() { const auto attributeMap = getAttributeMap(); - const auto width = XmlParserHelper::getAttribMandatory("width", attributeMap); - const auto height = XmlParserHelper::getAttribMandatory("height", attributeMap); + const auto width = XmlParserHelper::getAttribMandatory(XmlAttrs::WIDTH, attributeMap); + const auto height = XmlParserHelper::getAttribMandatory(XmlAttrs::HEIGHT, attributeMap); this->handler->addPage(width, height); } @@ -420,7 +421,7 @@ void XmlParser::parsePageTag() { void XmlParser::parseAudioTag() { const auto attributeMap = getAttributeMap(); - const auto filename = XmlParserHelper::getAttribMandatory("fn", attributeMap); + const auto filename = XmlParserHelper::getAttribMandatory(XmlAttrs::FN, attributeMap); this->handler->addAudioAttachment(filename); } @@ -428,8 +429,8 @@ void XmlParser::parseAudioTag() { void XmlParser::parseBackgroundTag() { const auto attributeMap = getAttributeMap(); - const auto name = XmlParserHelper::getAttrib("name", attributeMap); - const auto optType = XmlParserHelper::getAttrib("type", attributeMap); + const auto name = XmlParserHelper::getAttrib(XmlAttrs::NAME, attributeMap); + const auto optType = XmlParserHelper::getAttrib(XmlAttrs::TYPE, attributeMap); this->handler->addBackground(name); if (optType) { @@ -450,8 +451,8 @@ void XmlParser::parseBackgroundTag() { } void XmlParser::parseBgSolid(const XmlParserHelper::AttributeMap& attributeMap) { - const auto optStyle = XmlParserHelper::getAttrib("style", attributeMap); - const auto config = XmlParserHelper::getAttribMandatory("config", attributeMap, "", false); + const auto optStyle = XmlParserHelper::getAttrib(XmlAttrs::STYLE, attributeMap); + const auto config = XmlParserHelper::getAttribMandatory(XmlAttrs::CONFIG, attributeMap, "", false); PageType bg; if (optStyle) { bg.format = PageTypeHandler::getPageTypeFormatForString(*optStyle); @@ -464,30 +465,30 @@ void XmlParser::parseBgSolid(const XmlParserHelper::AttributeMap& attributeMap) } void XmlParser::parseBgPixmap(const XmlParserHelper::AttributeMap& attributeMap) { - const auto domain = XmlParserHelper::getAttribMandatory("domain", attributeMap, + const auto domain = XmlParserHelper::getAttribMandatory(XmlAttrs::DOMAIN, attributeMap, XmlParserHelper::Domain::ABOSLUTE); if (domain != XmlParserHelper::Domain::CLONE) { - const fs::path filename = XmlParserHelper::getAttribMandatory("filename", attributeMap); + const fs::path filename = XmlParserHelper::getAttribMandatory(XmlAttrs::FILENAME, attributeMap); this->handler->setBgPixmap(domain == XmlParserHelper::Domain::ATTACH, filename); } else { // In case of a cloned background image, filename contains the page // number from which the image is cloned. - const auto pageNr = XmlParserHelper::getAttribMandatory("filename", attributeMap); + const auto pageNr = XmlParserHelper::getAttribMandatory(XmlAttrs::FILENAME, attributeMap); this->handler->setBgPixmapCloned(pageNr); } } void XmlParser::parseBgPdf(const XmlParserHelper::AttributeMap& attributeMap) { if (!this->pdfFilenameParsed) { - auto domain = XmlParserHelper::getAttribMandatory("domain", attributeMap, + auto domain = XmlParserHelper::getAttribMandatory(XmlAttrs::DOMAIN, attributeMap, XmlParserHelper::Domain::ABOSLUTE); if (domain == XmlParserHelper::Domain::CLONE) { g_warning("XML parser: Domain \"clone\" is invalid for PDF backgrounds. Using \"absolute\" instead"); domain = XmlParserHelper::Domain::ABOSLUTE; } - const fs::path filename = XmlParserHelper::getAttribMandatory("filename", attributeMap); + const fs::path filename = XmlParserHelper::getAttribMandatory(XmlAttrs::FILENAME, attributeMap); if (!filename.empty()) { this->pdfFilenameParsed = true; @@ -497,7 +498,7 @@ void XmlParser::parseBgPdf(const XmlParserHelper::AttributeMap& attributeMap) { } } - const auto pageno = XmlParserHelper::getAttribMandatory("pageno", attributeMap, 1) - 1; + const auto pageno = XmlParserHelper::getAttribMandatory(XmlAttrs::PAGENO, attributeMap, 1) - 1; this->handler->setBgPdf(pageno); } @@ -505,7 +506,7 @@ void XmlParser::parseBgPdf(const XmlParserHelper::AttributeMap& attributeMap) { void XmlParser::parseLayerTag() { const auto attributeMap = getAttributeMap(); - const auto name = XmlParserHelper::getAttrib("name", attributeMap); + const auto name = XmlParserHelper::getAttrib(XmlAttrs::NAME, attributeMap); this->handler->addLayer(name); } @@ -520,27 +521,27 @@ void XmlParser::parseTimestampTag() { this->tempFilename.u8string().c_str()); } - this->tempFilename = XmlParserHelper::getAttribMandatory("fn", attributeMap); - this->tempTimestamp = XmlParserHelper::getAttribMandatory("ts", attributeMap); + this->tempFilename = XmlParserHelper::getAttribMandatory(XmlAttrs::FN, attributeMap); + this->tempTimestamp = XmlParserHelper::getAttribMandatory(XmlAttrs::TS, attributeMap); } void XmlParser::parseStrokeTag() { const auto attributeMap = getAttributeMap(); // tool - const auto tool = XmlParserHelper::getAttribMandatory("tool", attributeMap, StrokeTool::PEN); + const auto tool = XmlParserHelper::getAttribMandatory(XmlAttrs::TOOL, attributeMap, StrokeTool::PEN); // color const auto color = XmlParserHelper::getAttribColorMandatory(attributeMap, Colors::black); // width - auto widthStr = XmlParserHelper::getAttribMandatory("width", attributeMap, "1"); + auto widthStr = XmlParserHelper::getAttribMandatory(XmlAttrs::WIDTH, attributeMap, "1"); // Use g_ascii_strtod instead of streams beacuse it is about twice as fast const char* itPtr = widthStr.c_str(); char* endPtr = nullptr; const double width = g_ascii_strtod(itPtr, &endPtr); // pressures - auto pressureStr = XmlParserHelper::getAttrib("pressures", attributeMap); + auto pressureStr = XmlParserHelper::getAttrib(XmlAttrs::PRESSURES, attributeMap); if (pressureStr) { // MrWriter writes pressures in a separate field itPtr = pressureStr->c_str(); @@ -560,23 +561,23 @@ void XmlParser::parseStrokeTag() { } // fill - const auto fill = XmlParserHelper::getAttribMandatory("fill", attributeMap, -1, false); + const auto fill = XmlParserHelper::getAttribMandatory(XmlAttrs::FILL, attributeMap, -1, false); // cap stype - const auto capStyle = - XmlParserHelper::getAttribMandatory("capStyle", attributeMap, StrokeCapStyle::ROUND, false); + const auto capStyle = XmlParserHelper::getAttribMandatory(XmlAttrs::CAPSTYLE, attributeMap, + StrokeCapStyle::ROUND, false); // line style - const auto lineStyle = XmlParserHelper::getAttrib("style", attributeMap); + const auto lineStyle = XmlParserHelper::getAttrib(XmlAttrs::STYLE, attributeMap); // audio filename and timestamp - const auto optFilename = XmlParserHelper::getAttrib("fn", attributeMap); + const auto optFilename = XmlParserHelper::getAttrib(XmlAttrs::FN, attributeMap); if (optFilename && !optFilename->empty()) { if (!this->tempFilename.empty()) { g_warning("XML parser: Discarding audio timestamp element, because stroke tag contains \"fn\" attribute"); } this->tempFilename = *optFilename; - this->tempTimestamp = XmlParserHelper::getAttribMandatory("ts", attributeMap, 0UL); + this->tempTimestamp = XmlParserHelper::getAttribMandatory(XmlAttrs::TS, attributeMap, 0UL); } // forward data to handler @@ -615,20 +616,20 @@ void XmlParser::parseStrokeText() { void XmlParser::parseTextTag() { const auto attributeMap = getAttributeMap(); - const auto font = XmlParserHelper::getAttribMandatory("font", attributeMap, "Sans"); - const auto size = XmlParserHelper::getAttribMandatory("size", attributeMap, 12); - const auto x = XmlParserHelper::getAttribMandatory("x", attributeMap); - const auto y = XmlParserHelper::getAttribMandatory("y", attributeMap); + const auto font = XmlParserHelper::getAttribMandatory(XmlAttrs::FONT, attributeMap, "Sans"); + const auto size = XmlParserHelper::getAttribMandatory(XmlAttrs::SIZE, attributeMap, 12); + const auto x = XmlParserHelper::getAttribMandatory(XmlAttrs::X, attributeMap); + const auto y = XmlParserHelper::getAttribMandatory(XmlAttrs::Y, attributeMap); const auto color = XmlParserHelper::getAttribColorMandatory(attributeMap, Colors::black); // audio filename and timestamp - const auto optFilename = XmlParserHelper::getAttrib("fn", attributeMap); + const auto optFilename = XmlParserHelper::getAttrib(XmlAttrs::FN, attributeMap); if (optFilename && !optFilename->empty()) { if (!this->tempFilename.empty()) { g_warning("XML parser: Discarding audio timestamp element, because text tag contains \"fn\" attribute"); } this->tempFilename = *optFilename; - this->tempTimestamp = XmlParserHelper::getAttribMandatory("ts", attributeMap, 0UL); + this->tempTimestamp = XmlParserHelper::getAttribMandatory(XmlAttrs::TS, attributeMap, 0UL); } this->handler->addText(font, size, x, y, color, tempFilename, tempTimestamp); @@ -645,10 +646,10 @@ void XmlParser::parseTextText() { void XmlParser::parseImageTag() { const auto attributeMap = getAttributeMap(); - const auto left = XmlParserHelper::getAttribMandatory("left", attributeMap); - const auto top = XmlParserHelper::getAttribMandatory("top", attributeMap); - const auto right = XmlParserHelper::getAttribMandatory("right", attributeMap); - const auto bottom = XmlParserHelper::getAttribMandatory("bottom", attributeMap); + const auto left = XmlParserHelper::getAttribMandatory(XmlAttrs::LEFT, attributeMap); + const auto top = XmlParserHelper::getAttribMandatory(XmlAttrs::TOP, attributeMap); + const auto right = XmlParserHelper::getAttribMandatory(XmlAttrs::RIGHT, attributeMap); + const auto bottom = XmlParserHelper::getAttribMandatory(XmlAttrs::BOTTOM, attributeMap); this->handler->addImage(left, top, right, bottom); } @@ -662,12 +663,12 @@ void XmlParser::parseImageText() { void XmlParser::parseTexImageTag() { const auto attributeMap = getAttributeMap(); - const auto left = XmlParserHelper::getAttribMandatory("left", attributeMap); - const auto top = XmlParserHelper::getAttribMandatory("top", attributeMap); - const auto right = XmlParserHelper::getAttribMandatory("right", attributeMap); - const auto bottom = XmlParserHelper::getAttribMandatory("bottom", attributeMap); + const auto left = XmlParserHelper::getAttribMandatory(XmlAttrs::LEFT, attributeMap); + const auto top = XmlParserHelper::getAttribMandatory(XmlAttrs::TOP, attributeMap); + const auto right = XmlParserHelper::getAttribMandatory(XmlAttrs::RIGHT, attributeMap); + const auto bottom = XmlParserHelper::getAttribMandatory(XmlAttrs::BOTTOM, attributeMap); - auto text = XmlParserHelper::getAttribMandatory("text", attributeMap); + auto text = XmlParserHelper::getAttribMandatory(XmlAttrs::TEXT, attributeMap); // Attribute "texlength" found in eralier parsers was a workaround from 098a67b to bdd0ec2 @@ -683,7 +684,7 @@ void XmlParser::parseTexImageText() { void XmlParser::parseAttachment() { const auto attributeMap = getAttributeMap(); - const auto path = XmlParserHelper::getAttribMandatory("path", attributeMap); + const auto path = XmlParserHelper::getAttribMandatory(XmlAttrs::PATH, attributeMap); switch (this->hierarchy.top()) { case TagType::IMAGE: diff --git a/src/core/control/xojfile/XmlParserHelper.cpp b/src/core/control/xojfile/XmlParserHelper.cpp index d1b2aaf79356..19cb59ae8bae 100644 --- a/src/core/control/xojfile/XmlParserHelper.cpp +++ b/src/core/control/xojfile/XmlParserHelper.cpp @@ -11,6 +11,7 @@ #include +#include "control/xojfile/XmlNames.h" #include "model/LineStyle.h" #include "model/StrokeStyle.h" #include "util/Color.h" @@ -49,7 +50,7 @@ auto XmlParserHelper::getAttribMandatory(const std::string& name, c auto XmlParserHelper::getAttribColorMandatory(const AttributeMap& attributeMap, const Color& defaultValue, bool bg) -> Color { - const auto optColorStr = getAttrib("color", attributeMap); + const auto optColorStr = getAttrib(XmlAttrs::COLOR, attributeMap); if (optColorStr) { std::optional optColor; diff --git a/src/core/control/xojfile/XmlParserHelper.h b/src/core/control/xojfile/XmlParserHelper.h index 16078b44045e..d6a15d8defa3 100644 --- a/src/core/control/xojfile/XmlParserHelper.h +++ b/src/core/control/xojfile/XmlParserHelper.h @@ -1,4 +1,3 @@ - /* * Xournal++ * diff --git a/src/core/control/xojfile/XojExportHandler.cpp b/src/core/control/xojfile/XojExportHandler.cpp index 3a392299a4d1..c928e14e29ab 100644 --- a/src/core/control/xojfile/XojExportHandler.cpp +++ b/src/core/control/xojfile/XojExportHandler.cpp @@ -6,6 +6,7 @@ #include "control/pagetype/PageTypeHandler.h" // for PageTypeHandler #include "control/xml/XmlNode.h" // for XmlNode #include "control/xml/XmlTextNode.h" // for XmlTextNode +#include "control/xojfile/XmlNames.h" // for XmlAttrs #include "model/PageType.h" // for PageTypeFormat, PageType #include "model/XojPage.h" // for XojPage @@ -29,16 +30,16 @@ void XojExportHandler::visitStrokeExtended(XmlPointNode* stroke, Stroke* s) { } void XojExportHandler::writeHeader() { - this->root->setAttrib("creator", PROJECT_STRING); + this->root->setAttrib(XmlAttrs::CREATOR, PROJECT_STRING); // Keep this version on 2, as this is anyway not read by Xournal - this->root->setAttrib("fileversion", "2"); + this->root->setAttrib(XmlAttrs::FILEVERSION, "2"); this->root->addChild( new XmlTextNode("title", std::string{"Xournal document (Compatibility) - see "} + PROJECT_HOMEPAGE_URL)); } void XojExportHandler::writeSolidBackground(XmlNode* background, PageRef p) { - background->setAttrib("type", "solid"); - background->setAttrib("color", getColorStr(p->getBackgroundColor())); + background->setAttrib(XmlAttrs::TYPE, "solid"); + background->setAttrib(XmlAttrs::COLOR, getColorStr(p->getBackgroundColor())); PageTypeFormat bgFormat = p->getBackgroundType().format; std::string format; @@ -49,7 +50,7 @@ void XojExportHandler::writeSolidBackground(XmlNode* background, PageRef p) { format = "plain"; } - background->setAttrib("style", format); + background->setAttrib(XmlAttrs::STYLE, format); } void XojExportHandler::writeTimestamp(AudioElement* audioElement, XmlAudioNode* xmlAudioNode) {