diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index f2137f2..36d0079 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -37,7 +37,7 @@ #include "CLI/Option.hpp" #include "PrinterDriverFactory.hpp" -#include "graphics/Label.hpp" +#include "graphics/LabelBuilder.hpp" #include "graphics/interface/ILabel.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" @@ -162,25 +162,21 @@ int PtouchPrint::run() { return 0; } - auto label = std::make_unique(printer->getPrinterInfo().pixelLines); - std::string labelText{}; - // TODO: refactor + // Use LabelBuilder to construct the label + graphics::LabelBuilder labelBuilder(printer->getPrinterInfo().pixelLines); + for (const auto& [cmd, value] : mCommands) { switch (cmd) { case CliCmdType::Text: - if (labelText.empty()) { - labelText = value; - } else { - labelText = labelText + '\n' + value; - } + labelBuilder.addText(value); break; case CliCmdType::Font: spdlog::debug("Setting font to {}", value); - label->setFontFamily(value); + labelBuilder.setFontFamily(value); break; case CliCmdType::FontSize: spdlog::debug("Setting font size to {}", std::stod(value)); - label->setFontSize(std::stod(value)); + labelBuilder.setFontSize(std::stod(value)); break; case CliCmdType::HAlign: spdlog::debug("Setting text horizontal alignment to {}", value); @@ -188,9 +184,9 @@ int PtouchPrint::run() { auto hPos = HALignPositionMap.find(value); if (hPos == HALignPositionMap.end()) { spdlog::warn("Invalid horizontal alignment specified!"); - label->setHAlign(HAlignPosition::UNKNOWN); + labelBuilder.setHAlign(HAlignPosition::UNKNOWN); } else { - label->setHAlign(hPos->second); + labelBuilder.setHAlign(hPos->second); } } break; @@ -199,10 +195,10 @@ int PtouchPrint::run() { { auto vPos = VALignPositionMap.find(value); if (vPos == VALignPositionMap.end()) { - spdlog::warn("Invalid verical alignment specified!"); - label->setVAlign(VAlignPosition::UNKNOWN); + spdlog::warn("Invalid vertical alignment specified!"); + labelBuilder.setVAlign(VAlignPosition::UNKNOWN); } else { - label->setVAlign(vPos->second); + labelBuilder.setVAlign(vPos->second); } } break; @@ -213,8 +209,8 @@ int PtouchPrint::run() { break; } } - label->create(labelText); - label->writeToPng("./testlabel.png"); + + auto label = labelBuilder.build(); if (!printer->printLabel(std::move(label))) { spdlog::error("An error occured while printing"); return -1; diff --git a/src/graphics/LabelBuilder.cpp b/src/graphics/LabelBuilder.cpp new file mode 100644 index 0000000..6b9ec4d --- /dev/null +++ b/src/graphics/LabelBuilder.cpp @@ -0,0 +1,95 @@ +/* + ptrnt - print labels on linux + Copyright (C) 2025 Moritz Martinius + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + */ + +#include "LabelBuilder.hpp" + +#include + +#include "Label.hpp" + +namespace ptprnt::graphics { + +LabelBuilder::LabelBuilder(int printerHeight) : mPrinterHeight(printerHeight) { + reset(); +} + +ILabelBuilder& LabelBuilder::addText(const std::string& text) { + if (!text.empty()) { + if (!mAccumulatedText.empty()) { + // Add a newline if the label already has some text accumulated + mAccumulatedText += '\n'; + } + mAccumulatedText += text; + spdlog::debug("LabelBuilder: Added text '{}', total length: {}", text, mAccumulatedText.length()); + } + return *this; +} + +ILabelBuilder& LabelBuilder::setFontFamily(const std::string& fontFamily) { + mCurrentFontFamily = fontFamily; + spdlog::debug("LabelBuilder: Set font family to '{}'", fontFamily); + return *this; +} + +ILabelBuilder& LabelBuilder::setFontSize(double fontSize) { + mCurrentFontSize = fontSize; + spdlog::debug("LabelBuilder: Set font size to {}", fontSize); + return *this; +} + +ILabelBuilder& LabelBuilder::setHAlign(HAlignPosition hAlign) { + mCurrentHAlign = hAlign; + spdlog::debug("LabelBuilder: Set horizontal alignment to {}", static_cast(hAlign)); + return *this; +} + +ILabelBuilder& LabelBuilder::setVAlign(VAlignPosition vAlign) { + mCurrentVAlign = vAlign; + spdlog::debug("LabelBuilder: Set vertical alignment to {}", static_cast(vAlign)); + return *this; +} + +std::unique_ptr LabelBuilder::build() { + spdlog::debug("LabelBuilder: Building label with text: '{}'", mAccumulatedText); + + auto label = std::make_unique