Skip to content

Commit

Permalink
Add and use constexpr attribute names
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoerschell committed Mar 11, 2024
1 parent 9ac958b commit a374154
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 104 deletions.
105 changes: 53 additions & 52 deletions src/core/control/xojfile/SaveHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

Expand All @@ -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());
}
}

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -138,31 +139,31 @@ 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()));
}
}

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()) {
Expand All @@ -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) {
Expand All @@ -192,29 +193,29 @@ 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<TexImage*>(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());
}
}
}

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);
Expand All @@ -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)) {
Expand All @@ -253,33 +254,33 @@ 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());

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 {
Expand All @@ -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());
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/core/control/xojfile/XmlNames.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a374154

Please sign in to comment.