Implement Label printing interface for PrinterDriver
Some checks failed
Build ptprnt / build (push) Failing after 32s
Some checks failed
Build ptprnt / build (push) Failing after 32s
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
#include "spdlog/fmt/bin_to_hex.h"
|
#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 ;)
|
// 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 {
|
namespace ptprnt::printer {
|
||||||
|
|
||||||
@@ -158,6 +159,15 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
|||||||
return true;
|
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() {
|
bool P700Printer::print() {
|
||||||
send(p700::commands::LF);
|
send(p700::commands::LF);
|
||||||
send(p700::commands::FF);
|
send(p700::commands::FF);
|
||||||
|
@@ -71,6 +71,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
||||||
bool detachUsbDevice() override;
|
bool detachUsbDevice() override;
|
||||||
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
||||||
|
bool printLabel(const std::unique_ptr<graphics::ILabel> label) override;
|
||||||
bool print() override;
|
bool print() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@@ -97,9 +97,9 @@ int PtouchPrint::run() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto label = graphics::Label();
|
auto label = std::make_unique<graphics::Label>(printer->getPrinterInfo().pixelLines);
|
||||||
std::string labelText{};
|
std::string labelText{};
|
||||||
|
// TODO: refactor
|
||||||
for (const auto& [cmd, value] : mCommands) {
|
for (const auto& [cmd, value] : mCommands) {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CliCmdType::Text:
|
case CliCmdType::Text:
|
||||||
@@ -111,11 +111,11 @@ int PtouchPrint::run() {
|
|||||||
break;
|
break;
|
||||||
case CliCmdType::Font:
|
case CliCmdType::Font:
|
||||||
spdlog::debug("Setting font to {}", value);
|
spdlog::debug("Setting font to {}", value);
|
||||||
label.setFontFamily(value);
|
label->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));
|
label->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);
|
||||||
@@ -123,9 +123,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);
|
label->setHAlign(HAlignPosition::UNKNOWN);
|
||||||
} else {
|
} else {
|
||||||
label.setHAlign(hPos->second);
|
label->setHAlign(hPos->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -135,9 +135,9 @@ 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 verical alignment specified!");
|
||||||
label.setVAlign(VAlignPosition::UNKNOWN);
|
label->setVAlign(VAlignPosition::UNKNOWN);
|
||||||
} else {
|
} else {
|
||||||
label.setVAlign(vPos->second);
|
label->setVAlign(vPos->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -148,13 +148,13 @@ int PtouchPrint::run() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!printer->print()) {
|
label->create(labelText);
|
||||||
|
label->writeToPng("./testlabel.png");
|
||||||
|
if (!printer->printLabel(std::move(label))) {
|
||||||
spdlog::error("An error occured while printing");
|
spdlog::error("An error occured while printing");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
label.create(labelText, 128);
|
|
||||||
label.writeToPng("./testlabel.png");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,6 +192,7 @@ void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
|||||||
spdlog::set_default_logger(logger);
|
spdlog::set_default_logger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: CLI parsing should be a seperate class/file
|
||||||
void PtouchPrint::setupCliParser() {
|
void PtouchPrint::setupCliParser() {
|
||||||
auto printVersion = [this](std::size_t) {
|
auto printVersion = [this](std::size_t) {
|
||||||
fmt::print("ptprnt version: {}\n", mVersionString);
|
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||||
|
@@ -36,11 +36,12 @@
|
|||||||
#include "pango/pangocairo.h"
|
#include "pango/pangocairo.h"
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
namespace ptprnt::graphics {
|
||||||
Label::Label()
|
Label::Label(const uint16_t heightPixel)
|
||||||
: mCairoCtx(cairo_create(mSurface)),
|
: mCairoCtx(cairo_create(mSurface)),
|
||||||
mPangoCtx(pango_cairo_create_context(mCairoCtx)),
|
mPangoCtx(pango_cairo_create_context(mCairoCtx)),
|
||||||
mPangoLyt(pango_layout_new(mPangoCtx)),
|
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() {
|
std::vector<uint8_t> Label::getRaw() {
|
||||||
assert(mSurface != nullptr);
|
assert(mSurface != nullptr);
|
||||||
@@ -55,6 +56,14 @@ uint8_t Label::getNumLines(std::string_view strv) {
|
|||||||
return std::count(strv.begin(), strv.end(), '\n');
|
return std::count(strv.begin(), strv.end(), '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Label::getWidth() {
|
||||||
|
return mPrinterHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Label::getHeight() {
|
||||||
|
return getLayoutWidth();
|
||||||
|
}
|
||||||
|
|
||||||
int Label::getLayoutHeight() {
|
int Label::getLayoutHeight() {
|
||||||
return mLayoutHeight;
|
return mLayoutHeight;
|
||||||
}
|
}
|
||||||
@@ -63,16 +72,14 @@ int Label::getLayoutWidth() {
|
|||||||
return mLayoutWidth;
|
return mLayoutWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Label::create(PrintableText printableText, const uint16_t heightPixel) {
|
bool Label::create(PrintableText printableText) {
|
||||||
setFontFamily(printableText.fontFamily);
|
setFontFamily(printableText.fontFamily);
|
||||||
setFontSize(printableText.fontSize);
|
setFontSize(printableText.fontSize);
|
||||||
|
|
||||||
return create(printableText.text, heightPixel);
|
return create(printableText.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Label::create(const std::string& labelText, const uint16_t heightPixel) {
|
bool Label::create(const std::string& labelText) {
|
||||||
mPrinterHeight = heightPixel;
|
|
||||||
|
|
||||||
// TODO: we need to create a custom fontconfig here so that Noto Emoji does not load the systems default
|
// 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
|
// 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
|
// see: https://docs.gtk.org/PangoFc/method.FontMap.set_config.html
|
||||||
|
@@ -33,17 +33,19 @@ namespace ptprnt::graphics {
|
|||||||
|
|
||||||
class Label : public ILabel {
|
class Label : public ILabel {
|
||||||
public:
|
public:
|
||||||
Label();
|
Label(const uint16_t heightPixel);
|
||||||
~Label();
|
~Label() override;
|
||||||
|
|
||||||
Label(const Label&) = delete;
|
Label(const Label&) = delete;
|
||||||
Label& operator=(const Label&) = delete;
|
Label& operator=(const Label&) = delete;
|
||||||
Label(Label&&) = delete;
|
Label(Label&&) = delete;
|
||||||
Label& operator=(Label&&) = delete;
|
Label& operator=(Label&&) = delete;
|
||||||
|
|
||||||
bool create(PrintableText printableText, const uint16_t heightPixel) override;
|
bool create(PrintableText printableText) override;
|
||||||
bool create(const std::string& labelText, const uint16_t heightPixel) override;
|
bool create(const std::string& labelText) override;
|
||||||
void writeToPng(const std::string& file);
|
void writeToPng(const std::string& file);
|
||||||
|
[[nodiscard]] int getWidth() override;
|
||||||
|
[[nodiscard]] int getHeight() override;
|
||||||
[[nodiscard]] int getLayoutWidth() override;
|
[[nodiscard]] int getLayoutWidth() override;
|
||||||
[[nodiscard]] int getLayoutHeight() override;
|
[[nodiscard]] int getLayoutHeight() override;
|
||||||
[[nodiscard]] std::vector<uint8_t> getRaw() override;
|
[[nodiscard]] std::vector<uint8_t> getRaw() override;
|
||||||
|
@@ -60,19 +60,23 @@ struct PrintableText {
|
|||||||
VAlignPosition vAlign{VAlignPosition::MIDDLE};
|
VAlignPosition vAlign{VAlignPosition::MIDDLE};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace ptprnt::graphics {
|
||||||
class ILabel {
|
class ILabel {
|
||||||
public:
|
public:
|
||||||
virtual ~ILabel() = default;
|
virtual ~ILabel() = default;
|
||||||
|
|
||||||
virtual bool create(PrintableText printableText, const uint16_t heightPixel) = 0;
|
virtual bool create(PrintableText printableText) = 0;
|
||||||
virtual bool create(const std::string& labelText, const uint16_t heightPixel) = 0;
|
virtual bool create(const std::string& labelText) = 0;
|
||||||
virtual std::vector<uint8_t> getRaw() = 0;
|
virtual std::vector<uint8_t> getRaw() = 0;
|
||||||
virtual int getLayoutWidth() = 0;
|
virtual int getWidth() = 0;
|
||||||
virtual int getLayoutHeight() = 0;
|
virtual int getHeight() = 0;
|
||||||
|
virtual int getLayoutWidth() = 0;
|
||||||
|
virtual int getLayoutHeight() = 0;
|
||||||
|
|
||||||
virtual void setText(const std::string& text) = 0;
|
virtual void setText(const std::string& text) = 0;
|
||||||
virtual void setFontSize(const double fontSize) = 0;
|
virtual void setFontSize(const double fontSize) = 0;
|
||||||
virtual void setFontFamily(const std::string& fontFamily) = 0;
|
virtual void setFontFamily(const std::string& fontFamily) = 0;
|
||||||
virtual void setHAlign(HAlignPosition hpos) = 0;
|
virtual void setHAlign(HAlignPosition hpos) = 0;
|
||||||
virtual void setVAlign(VAlignPosition vpos) = 0;
|
virtual void setVAlign(VAlignPosition vpos) = 0;
|
||||||
};
|
};
|
||||||
|
} // namespace ptprnt::graphics
|
@@ -19,11 +19,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
|
#include "graphics/interface/ILabel.hpp"
|
||||||
#include "interface/IPrinterTypes.hpp"
|
#include "interface/IPrinterTypes.hpp"
|
||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
@@ -40,6 +40,7 @@ class IPrinterDriver {
|
|||||||
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
||||||
virtual bool detachUsbDevice() = 0;
|
virtual bool detachUsbDevice() = 0;
|
||||||
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 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;
|
virtual bool print() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -22,5 +22,5 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(basic_test, Label_smokeTest_succeeds) {
|
TEST(basic_test, Label_smokeTest_succeeds) {
|
||||||
auto im = ptprnt::graphics::Label();
|
auto im = ptprnt::graphics::Label(4711);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user