3 Commits

Author SHA1 Message Date
af2ba7de5b Reintroduce newlines via multiple text flags
All checks were successful
Build ptprnt / build (push) Successful in 3m31s
2025-10-12 22:29:26 +02:00
922ecdb57d Add a label builder
All checks were successful
Build ptprnt / build (push) Successful in 3m35s
2025-10-12 22:22:32 +02:00
05cd9d244c Generate labels with pangocairo (#8)
All checks were successful
Build ptprnt / build (push) Successful in 3m41s
Goal of this PR is to have some basic labels generated with pangocairo
- size of the canvas should be matching the input text and grow/shrink accordingly
- basic formatting options like fontsize and align should be working

Reviewed-on: moritz/ptouch-prnt#8
2025-10-12 20:07:18 +00:00
5 changed files with 263 additions and 18 deletions

View File

@@ -37,7 +37,7 @@
#include "CLI/Option.hpp" #include "CLI/Option.hpp"
#include "PrinterDriverFactory.hpp" #include "PrinterDriverFactory.hpp"
#include "graphics/Label.hpp" #include "graphics/LabelBuilder.hpp"
#include "graphics/interface/ILabel.hpp" #include "graphics/interface/ILabel.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
@@ -162,25 +162,21 @@ int PtouchPrint::run() {
return 0; return 0;
} }
auto label = std::make_unique<graphics::Label>(printer->getPrinterInfo().pixelLines); // Use LabelBuilder to construct the label
std::string labelText{}; graphics::LabelBuilder labelBuilder(printer->getPrinterInfo().pixelLines);
// TODO: refactor
for (const auto& [cmd, value] : mCommands) { for (const auto& [cmd, value] : mCommands) {
switch (cmd) { switch (cmd) {
case CliCmdType::Text: case CliCmdType::Text:
if (labelText.empty()) { labelBuilder.addText(value);
labelText = value;
} else {
labelText = labelText + '\n' + value;
}
break; break;
case CliCmdType::Font: case CliCmdType::Font:
spdlog::debug("Setting font to {}", value); spdlog::debug("Setting font to {}", value);
label->setFontFamily(value); labelBuilder.setFontFamily(value);
break; break;
case CliCmdType::FontSize: case CliCmdType::FontSize:
spdlog::debug("Setting font size to {}", std::stod(value)); spdlog::debug("Setting font size to {}", std::stod(value));
label->setFontSize(std::stod(value)); labelBuilder.setFontSize(std::stod(value));
break; break;
case CliCmdType::HAlign: case CliCmdType::HAlign:
spdlog::debug("Setting text horizontal alignment to {}", value); spdlog::debug("Setting text horizontal alignment to {}", value);
@@ -188,9 +184,9 @@ int PtouchPrint::run() {
auto hPos = HALignPositionMap.find(value); auto hPos = HALignPositionMap.find(value);
if (hPos == HALignPositionMap.end()) { if (hPos == HALignPositionMap.end()) {
spdlog::warn("Invalid horizontal alignment specified!"); spdlog::warn("Invalid horizontal alignment specified!");
label->setHAlign(HAlignPosition::UNKNOWN); labelBuilder.setHAlign(HAlignPosition::UNKNOWN);
} else { } else {
label->setHAlign(hPos->second); labelBuilder.setHAlign(hPos->second);
} }
} }
break; break;
@@ -199,10 +195,10 @@ int PtouchPrint::run() {
{ {
auto vPos = VALignPositionMap.find(value); auto vPos = VALignPositionMap.find(value);
if (vPos == VALignPositionMap.end()) { if (vPos == VALignPositionMap.end()) {
spdlog::warn("Invalid verical alignment specified!"); spdlog::warn("Invalid vertical alignment specified!");
label->setVAlign(VAlignPosition::UNKNOWN); labelBuilder.setVAlign(VAlignPosition::UNKNOWN);
} else { } else {
label->setVAlign(vPos->second); labelBuilder.setVAlign(vPos->second);
} }
} }
break; break;
@@ -213,8 +209,8 @@ int PtouchPrint::run() {
break; break;
} }
} }
label->create(labelText);
label->writeToPng("./testlabel.png"); auto label = labelBuilder.build();
if (!printer->printLabel(std::move(label))) { if (!printer->printLabel(std::move(label))) {
spdlog::error("An error occured while printing"); spdlog::error("An error occured while printing");
return -1; return -1;

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
#include "LabelBuilder.hpp"
#include <spdlog/spdlog.h>
#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<int>(hAlign));
return *this;
}
ILabelBuilder& LabelBuilder::setVAlign(VAlignPosition vAlign) {
mCurrentVAlign = vAlign;
spdlog::debug("LabelBuilder: Set vertical alignment to {}", static_cast<int>(vAlign));
return *this;
}
std::unique_ptr<ILabel> LabelBuilder::build() {
spdlog::debug("LabelBuilder: Building label with text: '{}'", mAccumulatedText);
auto label = std::make_unique<Label>(mPrinterHeight);
// Apply current formatting settings
label->setFontFamily(mCurrentFontFamily);
label->setFontSize(mCurrentFontSize);
label->setHAlign(mCurrentHAlign);
label->setVAlign(mCurrentVAlign);
// Create the label with accumulated text
label->create(mAccumulatedText);
return label;
}
ILabelBuilder& LabelBuilder::reset() {
mAccumulatedText.clear();
mCurrentFontFamily = DEFAULT_FONT_FAMILY;
mCurrentFontSize = DEFAULT_FONT_SIZE;
mCurrentHAlign = HAlignPosition::LEFT;
mCurrentVAlign = VAlignPosition::MIDDLE;
spdlog::debug("LabelBuilder: Reset to default state");
return *this;
}
} // namespace ptprnt::graphics

View File

@@ -0,0 +1,64 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "interface/ILabel.hpp"
#include "interface/ILabelBuilder.hpp"
namespace ptprnt::graphics {
/**
* @brief Concrete implementation of ILabelBuilder
*
* Builds labels by accumulating text segments with formatting options,
* then creates a Label instance with all the collected content.
*/
class LabelBuilder : public ILabelBuilder {
public:
/**
* @brief Construct a LabelBuilder for a specific printer height
* @param printerHeight Height of the printer in pixels (tape width)
*/
explicit LabelBuilder(int printerHeight);
~LabelBuilder() override = default;
ILabelBuilder& addText(const std::string& text) override;
ILabelBuilder& setFontFamily(const std::string& fontFamily) override;
ILabelBuilder& setFontSize(double fontSize) override;
ILabelBuilder& setHAlign(HAlignPosition hAlign) override;
ILabelBuilder& setVAlign(VAlignPosition vAlign) override;
std::unique_ptr<ILabel> build() override;
ILabelBuilder& reset() override;
private:
int mPrinterHeight;
std::string mAccumulatedText;
std::string mCurrentFontFamily{DEFAULT_FONT_FAMILY};
double mCurrentFontSize{DEFAULT_FONT_SIZE};
HAlignPosition mCurrentHAlign{HAlignPosition::LEFT};
VAlignPosition mCurrentVAlign{VAlignPosition::MIDDLE};
};
} // namespace ptprnt::graphics

View File

@@ -0,0 +1,88 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include "ILabel.hpp"
namespace ptprnt::graphics {
/**
* @brief Builder interface for creating labels with text and formatting options
*
* The LabelBuilder provides a fluent API for constructing labels with various
* text elements, fonts, sizes, and alignment options. It separates the construction
* logic from the label rendering logic, making it easier to test and maintain.
*/
class ILabelBuilder {
public:
virtual ~ILabelBuilder() = default;
/**
* @brief Add text to the label with current formatting settings
* @param text The text to add
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& addText(const std::string& text) = 0;
/**
* @brief Set the font family for subsequent text additions
* @param fontFamily Font family name (e.g., "sans", "serif", "monospace")
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setFontFamily(const std::string& fontFamily) = 0;
/**
* @brief Set the font size for subsequent text additions
* @param fontSize Font size in points
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setFontSize(double fontSize) = 0;
/**
* @brief Set horizontal alignment for subsequent text additions
* @param hAlign Horizontal alignment position
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setHAlign(HAlignPosition hAlign) = 0;
/**
* @brief Set vertical alignment for subsequent text additions
* @param vAlign Vertical alignment position
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setVAlign(VAlignPosition vAlign) = 0;
/**
* @brief Build and return the label with all added content
* @return Unique pointer to the constructed label
*/
virtual std::unique_ptr<ILabel> build() = 0;
/**
* @brief Reset the builder to initial state
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& reset() = 0;
};
} // namespace ptprnt::graphics

View File

@@ -12,6 +12,7 @@ ptprnt_hpps = files (
'PrinterDriverFactory.hpp', 'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp', 'graphics/Bitmap.hpp',
'graphics/Label.hpp', 'graphics/Label.hpp',
'graphics/LabelBuilder.hpp',
'graphics/Monochrome.hpp' 'graphics/Monochrome.hpp'
) )
@@ -21,6 +22,7 @@ ptprnt_srcs = files (
'printers/P700Printer.cpp', 'printers/P700Printer.cpp',
'printers/FakePrinter.cpp', 'printers/FakePrinter.cpp',
'graphics/Label.cpp', 'graphics/Label.cpp',
'graphics/LabelBuilder.cpp',
'graphics/Bitmap.cpp', 'graphics/Bitmap.cpp',
'graphics/Monochrome.cpp', 'graphics/Monochrome.cpp',
'libusbwrap/UsbDeviceFactory.cpp', 'libusbwrap/UsbDeviceFactory.cpp',