Implement Label printing interface for PrinterDriver
Some checks failed
Build ptprnt / build (push) Failing after 32s

This commit is contained in:
2024-04-28 20:02:07 +02:00
parent bb7ab6239d
commit 59ef4189c4
8 changed files with 57 additions and 31 deletions

View File

@@ -23,6 +23,7 @@
#include <cstdint>
#include <iterator>
#include <memory>
#include <thread>
#include <vector>
@@ -32,7 +33,7 @@
#include "spdlog/fmt/bin_to_hex.h"
// as long as DRYRUN is defined, no data is actually send to the printer, we need to save some tape ;)
#define DRYRUN
//#define DRYRUN
namespace ptprnt::printer {
@@ -158,6 +159,15 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
return true;
}
bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
// not quite sure if I should stack allocate Bitmap, but data is held on the heap anyway (std::vector).
auto bm = graphics::Bitmap<graphics::ALPHA8>(label->getWidth(), label->getHeight());
spdlog::debug("Label has {}x{}px size", label->getWidth(), label->getHeight());
bm.setPixels(label->getRaw());
printBitmap(bm);
return true;
}
bool P700Printer::print() {
send(p700::commands::LF);
send(p700::commands::FF);

View File

@@ -71,6 +71,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
bool detachUsbDevice() override;
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
bool printLabel(const std::unique_ptr<graphics::ILabel> label) override;
bool print() override;
private:

View File

@@ -97,9 +97,9 @@ int PtouchPrint::run() {
return 0;
}
auto label = graphics::Label();
auto label = std::make_unique<graphics::Label>(printer->getPrinterInfo().pixelLines);
std::string labelText{};
// TODO: refactor
for (const auto& [cmd, value] : mCommands) {
switch (cmd) {
case CliCmdType::Text:
@@ -111,11 +111,11 @@ int PtouchPrint::run() {
break;
case CliCmdType::Font:
spdlog::debug("Setting font to {}", value);
label.setFontFamily(value);
label->setFontFamily(value);
break;
case CliCmdType::FontSize:
spdlog::debug("Setting font size to {}", std::stod(value));
label.setFontSize(std::stod(value));
label->setFontSize(std::stod(value));
break;
case CliCmdType::HAlign:
spdlog::debug("Setting text horizontal alignment to {}", value);
@@ -123,9 +123,9 @@ int PtouchPrint::run() {
auto hPos = HALignPositionMap.find(value);
if (hPos == HALignPositionMap.end()) {
spdlog::warn("Invalid horizontal alignment specified!");
label.setHAlign(HAlignPosition::UNKNOWN);
label->setHAlign(HAlignPosition::UNKNOWN);
} else {
label.setHAlign(hPos->second);
label->setHAlign(hPos->second);
}
}
break;
@@ -135,9 +135,9 @@ int PtouchPrint::run() {
auto vPos = VALignPositionMap.find(value);
if (vPos == VALignPositionMap.end()) {
spdlog::warn("Invalid verical alignment specified!");
label.setVAlign(VAlignPosition::UNKNOWN);
label->setVAlign(VAlignPosition::UNKNOWN);
} else {
label.setVAlign(vPos->second);
label->setVAlign(vPos->second);
}
}
break;
@@ -148,13 +148,13 @@ int PtouchPrint::run() {
break;
}
}
if (!printer->print()) {
label->create(labelText);
label->writeToPng("./testlabel.png");
if (!printer->printLabel(std::move(label))) {
spdlog::error("An error occured while printing");
return -1;
}
label.create(labelText, 128);
label.writeToPng("./testlabel.png");
return 0;
}
@@ -192,6 +192,7 @@ void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
spdlog::set_default_logger(logger);
}
// TODO: CLI parsing should be a seperate class/file
void PtouchPrint::setupCliParser() {
auto printVersion = [this](std::size_t) {
fmt::print("ptprnt version: {}\n", mVersionString);

View File

@@ -36,11 +36,12 @@
#include "pango/pangocairo.h"
namespace ptprnt::graphics {
Label::Label()
Label::Label(const uint16_t heightPixel)
: mCairoCtx(cairo_create(mSurface)),
mPangoCtx(pango_cairo_create_context(mCairoCtx)),
mPangoLyt(pango_layout_new(mPangoCtx)),
mFontMap(pango_cairo_font_map_new()) {}
mFontMap(pango_cairo_font_map_new()),
mPrinterHeight(heightPixel) {}
std::vector<uint8_t> Label::getRaw() {
assert(mSurface != nullptr);
@@ -55,6 +56,14 @@ uint8_t Label::getNumLines(std::string_view strv) {
return std::count(strv.begin(), strv.end(), '\n');
}
int Label::getWidth() {
return mPrinterHeight;
}
int Label::getHeight() {
return getLayoutWidth();
}
int Label::getLayoutHeight() {
return mLayoutHeight;
}
@@ -63,16 +72,14 @@ int Label::getLayoutWidth() {
return mLayoutWidth;
}
bool Label::create(PrintableText printableText, const uint16_t heightPixel) {
bool Label::create(PrintableText printableText) {
setFontFamily(printableText.fontFamily);
setFontSize(printableText.fontSize);
return create(printableText.text, heightPixel);
return create(printableText.text);
}
bool Label::create(const std::string& labelText, const uint16_t heightPixel) {
mPrinterHeight = heightPixel;
bool Label::create(const std::string& labelText) {
// TODO: we need to create a custom fontconfig here so that Noto Emoji does not load the systems default
// fontconfig here. For this, we need to create a PangoFcFontMap and a custom FcConfig
// see: https://docs.gtk.org/PangoFc/method.FontMap.set_config.html

View File

@@ -33,17 +33,19 @@ namespace ptprnt::graphics {
class Label : public ILabel {
public:
Label();
~Label();
Label(const uint16_t heightPixel);
~Label() override;
Label(const Label&) = delete;
Label& operator=(const Label&) = delete;
Label(Label&&) = delete;
Label& operator=(Label&&) = delete;
bool create(PrintableText printableText, const uint16_t heightPixel) override;
bool create(const std::string& labelText, const uint16_t heightPixel) override;
bool create(PrintableText printableText) override;
bool create(const std::string& labelText) override;
void writeToPng(const std::string& file);
[[nodiscard]] int getWidth() override;
[[nodiscard]] int getHeight() override;
[[nodiscard]] int getLayoutWidth() override;
[[nodiscard]] int getLayoutHeight() override;
[[nodiscard]] std::vector<uint8_t> getRaw() override;

View File

@@ -60,19 +60,23 @@ struct PrintableText {
VAlignPosition vAlign{VAlignPosition::MIDDLE};
};
namespace ptprnt::graphics {
class ILabel {
public:
virtual ~ILabel() = default;
virtual bool create(PrintableText printableText, const uint16_t heightPixel) = 0;
virtual bool create(const std::string& labelText, const uint16_t heightPixel) = 0;
virtual std::vector<uint8_t> getRaw() = 0;
virtual int getLayoutWidth() = 0;
virtual int getLayoutHeight() = 0;
virtual bool create(PrintableText printableText) = 0;
virtual bool create(const std::string& labelText) = 0;
virtual std::vector<uint8_t> getRaw() = 0;
virtual int getWidth() = 0;
virtual int getHeight() = 0;
virtual int getLayoutWidth() = 0;
virtual int getLayoutHeight() = 0;
virtual void setText(const std::string& text) = 0;
virtual void setFontSize(const double fontSize) = 0;
virtual void setFontFamily(const std::string& fontFamily) = 0;
virtual void setHAlign(HAlignPosition hpos) = 0;
virtual void setVAlign(VAlignPosition vpos) = 0;
};
};
} // namespace ptprnt::graphics

View File

@@ -19,11 +19,11 @@
#pragma once
#include <cstdint>
#include <memory>
#include <string_view>
#include "graphics/Bitmap.hpp"
#include "graphics/interface/ILabel.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
@@ -40,6 +40,7 @@ class IPrinterDriver {
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
virtual bool detachUsbDevice() = 0;
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
virtual bool printLabel(const std::unique_ptr<graphics::ILabel> label) = 0;
virtual bool print() = 0;
};

View File

@@ -22,5 +22,5 @@
#include <gtest/gtest.h>
TEST(basic_test, Label_smokeTest_succeeds) {
auto im = ptprnt::graphics::Label();
auto im = ptprnt::graphics::Label(4711);
}