diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 7a0ac2d..fa43a2b 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -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& bitmap) return true; } +bool P700Printer::printLabel(std::unique_ptr label) { + // not quite sure if I should stack allocate Bitmap, but data is held on the heap anyway (std::vector). + auto bm = graphics::Bitmap(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); diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index 4b638dc..5c96c3b 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -71,6 +71,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver { bool attachUsbDevice(std::shared_ptr usbHndl) override; bool detachUsbDevice() override; bool printBitmap(const graphics::Bitmap& bitmap) override; + bool printLabel(const std::unique_ptr label) override; bool print() override; private: diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index abfda98..9a5e0d4 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -97,9 +97,9 @@ int PtouchPrint::run() { return 0; } - auto label = graphics::Label(); + auto label = std::make_unique(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); diff --git a/src/graphics/Label.cpp b/src/graphics/Label.cpp index a19d104..fefedbb 100644 --- a/src/graphics/Label.cpp +++ b/src/graphics/Label.cpp @@ -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 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 diff --git a/src/graphics/Label.hpp b/src/graphics/Label.hpp index 1ec934f..3b77c1b 100644 --- a/src/graphics/Label.hpp +++ b/src/graphics/Label.hpp @@ -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 getRaw() override; diff --git a/src/graphics/interface/ILabel.hpp b/src/graphics/interface/ILabel.hpp index 9987ef1..46acd05 100644 --- a/src/graphics/interface/ILabel.hpp +++ b/src/graphics/interface/ILabel.hpp @@ -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 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 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; -}; \ No newline at end of file +}; +} // namespace ptprnt::graphics \ No newline at end of file diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index 147b9ef..f199bc8 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -19,11 +19,11 @@ #pragma once -#include #include #include #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 usbHndl) = 0; virtual bool detachUsbDevice() = 0; virtual bool printBitmap(const graphics::Bitmap& bitmap) = 0; + virtual bool printLabel(const std::unique_ptr label) = 0; virtual bool print() = 0; }; diff --git a/tests/label_test/label_test.cpp b/tests/label_test/label_test.cpp index a4f1dfd..08e3ce7 100644 --- a/tests/label_test/label_test.cpp +++ b/tests/label_test/label_test.cpp @@ -22,5 +22,5 @@ #include TEST(basic_test, Label_smokeTest_succeeds) { - auto im = ptprnt::graphics::Label(); + auto im = ptprnt::graphics::Label(4711); }