Add a label builder (#14)
All checks were successful
Build ptprnt / build (push) Successful in 3m22s

Reviewed-on: moritz/ptouch-prnt#14
This commit was merged in pull request #14.
This commit is contained in:
2025-10-12 20:35:04 +00:00
parent 05cd9d244c
commit 78aab33fdb
5 changed files with 263 additions and 18 deletions

View File

@@ -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<graphics::Label>(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;