Generate labels with pangocairo #8

Merged
moritz merged 30 commits from generate-text-part-one into master 2025-10-12 20:07:19 +00:00
11 changed files with 271 additions and 177 deletions
Showing only changes of commit 0b8ff28a60 - Show all commits

11
.vscode/launch.json vendored
View File

@@ -9,12 +9,15 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt", "program": "${workspaceFolder}/builddir/ptprnt",
"args": ["-t Hello"], "args": [
"-t i"
],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${fileDirname}", "cwd": "${workspaceFolder}",
"environment": [], "environment": [],
"externalConsole": false, "externalConsole": false,
"MIMode": "lldb", "MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [ "setupCommands": [
{ {
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren", "description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
@@ -24,4 +27,4 @@
] ]
} }
] ]
} }

View File

@@ -4,7 +4,7 @@
#include <memory> #include <memory>
#include "P700Printer.hpp" #include "printers/P700Printer.hpp"
#include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt { namespace ptprnt {

View File

@@ -88,7 +88,10 @@ int PtouchPrint::run() {
spdlog::warn("Found more than one device of the same printer on bus. Currently not supported"); spdlog::warn("Found more than one device of the same printer on bus. Currently not supported");
return -1; return -1;
} }
printer->attachUsbDevice(std::move(devices[0])); if (!printer->attachUsbDevice(std::move(devices[0]))) {
spdlog::error("Failed to attach USB device to printer");
return -1;
}
auto status = printer->getPrinterStatus(); auto status = printer->getPrinterStatus();
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm); spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
@@ -150,10 +153,10 @@ int PtouchPrint::run() {
} }
label->create(labelText); label->create(labelText);
label->writeToPng("./testlabel.png"); label->writeToPng("./testlabel.png");
/*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;
}*/ }
return 0; return 0;
} }

View File

@@ -37,18 +37,24 @@
namespace ptprnt::graphics { namespace ptprnt::graphics {
Label::Label(const uint16_t heightPixel) Label::Label(const uint16_t heightPixel)
: mCairoCtx(cairo_create(mSurface)), : mPrinterHeight(heightPixel) {
mPangoCtx(pango_cairo_create_context(mCairoCtx)), // Initialize resources in correct order with RAII
mPangoLyt(pango_layout_new(mPangoCtx)), mFontMap.reset(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);
size_t len = mPrinterHeight * mLayoutWidth; auto* surface = mSurface.get();
size_t len = cairo_image_surface_get_height(surface) * cairo_image_surface_get_stride(surface);
cairo_surface_flush(mSurface); cairo_surface_flush(surface);
auto data = cairo_image_surface_get_data(mSurface); assert(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_A8);
spdlog::debug("Cairo Surface data: W: {}; H: {}; S:{}", cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface), cairo_image_surface_get_stride(surface));
auto data = cairo_image_surface_get_data(surface);
FILE* write_ptr;
write_ptr = fopen("test.bin", "wb"); // w for write, b for binary
fwrite(data, len, 1, write_ptr); //
return {data, data + len}; return {data, data + len};
} }
@@ -72,6 +78,35 @@ int Label::getLayoutWidth() {
return mLayoutWidth; return mLayoutWidth;
} }
void Label::configureLayout(PangoLayout* layout, const std::string& text, PangoFontDescription* fontDesc) {
pango_layout_set_font_description(layout, fontDesc);
pango_layout_set_text(layout, text.c_str(), static_cast<int>(text.length()));
pango_layout_set_height(layout, getNumLines(text) * -1);
}
void Label::applyHorizontalAlignment(PangoLayout* layout) {
switch (mHAlign) {
case HAlignPosition::LEFT:
pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
break;
case HAlignPosition::RIGHT:
pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
break;
case HAlignPosition::JUSTIFY:
pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
pango_layout_set_justify(layout, true);
#if PANGO_VERSION_MAJOR >= 1 && PANGO_VERSION_MINOR >= 50
pango_layout_set_justify_last_line(layout, true);
#endif
break;
case HAlignPosition::CENTER:
[[fallthrough]];
default:
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
break;
}
}
bool Label::create(PrintableText printableText) { bool Label::create(PrintableText printableText) {
setFontFamily(printableText.fontFamily); setFontFamily(printableText.fontFamily);
setFontSize(printableText.fontSize); setFontSize(printableText.fontSize);
@@ -80,79 +115,78 @@ bool Label::create(PrintableText printableText) {
} }
bool Label::create(const std::string& labelText) { 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 // TODO: we need to create a custom font config 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 // font config 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
// see: https://gist.github.com/CallumDev/7c66b3f9cf7a876ef75f // see: https://gist.github.com/CallumDev/7c66b3f9cf7a876ef75f
// Create a temporary surface for layout size calculations
auto* tempSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 1, 1);
auto* tempCr = cairo_create(tempSurface);
auto* tempPangoCtx = pango_cairo_create_context(tempCr);
auto* tempPangoLyt = pango_layout_new(tempPangoCtx);
PangoFontDescription* regularFont = pango_font_description_new(); PangoFontDescription* regularFont = pango_font_description_new();
pango_font_description_set_size(regularFont, static_cast<int>(mFontSize * PANGO_SCALE)); pango_font_description_set_size(regularFont, static_cast<int>(mFontSize * PANGO_SCALE));
pango_font_description_set_family(regularFont, mFontFamily.c_str()); pango_font_description_set_family(regularFont, mFontFamily.c_str());
//pango_layout_set_single_paragraph_mode(mPangoLyt, true); // this will force a single line in the label width width -1
pango_layout_set_height(mPangoLyt, getNumLines(labelText) * -1);
pango_layout_set_font_description(mPangoLyt, regularFont);
pango_layout_set_text(mPangoLyt, labelText.c_str(), static_cast<int>(labelText.length()));
// Set horizontal alignment // Configure temporary layout for size calculation
switch (mHAlign) { configureLayout(tempPangoLyt, labelText, regularFont);
case HAlignPosition::LEFT: applyHorizontalAlignment(tempPangoLyt);
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_LEFT);
break;
case HAlignPosition::RIGHT:
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_RIGHT);
break;
case HAlignPosition::JUSTIFY:
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_LEFT); // not sure if needed
pango_layout_set_justify(mPangoLyt, true);
// only enabled in pango 1.50 and greater
#if PANGO_VERSION_MAJOR >= 1 && PANGO_VERSION_MINOR >= 50
pango_layout_set_justify_last_line(mPangoLyt, true);
#endif
break;
case HAlignPosition::CENTER:
[[fallthrough]];
default:
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_CENTER);
break;
}
// calculate label size for Cairo surface creation // Calculate label size from temporary layout
pango_layout_get_size(mPangoLyt, &mLayoutWidth, &mLayoutHeight); pango_layout_get_size(tempPangoLyt, &mLayoutWidth, &mLayoutHeight);
mLayoutWidth /= PANGO_SCALE; mLayoutWidth /= PANGO_SCALE;
mLayoutHeight /= PANGO_SCALE; mLayoutHeight /= PANGO_SCALE;
spdlog::debug("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight); spdlog::debug("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight);
auto alignedWidth = mLayoutWidth + (8 - (mLayoutWidth % 8)); //auto alignedWidth = mLayoutWidth + (8 - (mLayoutWidth % 8));
//spdlog::debug("Aligned Layout width: {}, height: {}", alignedWidth, mLayoutHeight);
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, alignedWidth, mPrinterHeight); // Clean up temporary resources
cairo_t* cr = cairo_create(mSurface); g_object_unref(tempPangoLyt);
g_object_unref(tempPangoCtx);
cairo_destroy(tempCr);
cairo_surface_destroy(tempSurface);
// Now create the final surface and Pango context for actual rendering
mSurface.reset(cairo_image_surface_create(CAIRO_FORMAT_A8, mLayoutWidth, mPrinterHeight));
cairo_t* cr = cairo_create(mSurface.get());
mCairoCtx.reset(cr);
mPangoCtx.reset(pango_cairo_create_context(cr));
mPangoLyt.reset(pango_layout_new(mPangoCtx.get()));
// Configure final layout with same settings
configureLayout(mPangoLyt.get(), labelText, regularFont);
applyHorizontalAlignment(mPangoLyt.get());
// Adjust Cairo cursor position to respect the vertical alignment // Adjust Cairo cursor position to respect the vertical alignment
switch (mVAlign) { switch (mVAlign) {
case VAlignPosition::TOP: case VAlignPosition::TOP:
break; break;
case VAlignPosition::BOTTOM: case VAlignPosition::BOTTOM:
cairo_move_to(cr, 0.0, mPrinterHeight - mLayoutHeight); cairo_move_to(mCairoCtx.get(), 0.0, mPrinterHeight - mLayoutHeight);
break; break;
case VAlignPosition::MIDDLE: case VAlignPosition::MIDDLE:
cairo_move_to(cr, 0.0, (mPrinterHeight - mLayoutHeight) / 2); cairo_move_to(mCairoCtx.get(), 0.0, (mPrinterHeight - mLayoutHeight) / 2);
[[fallthrough]]; [[fallthrough]];
default: default:
break; break;
} }
// Finally show the layout on the Cairo surface // Finally show the layout on the Cairo surface
pango_cairo_show_layout(cr, mPangoLyt); pango_cairo_show_layout(mCairoCtx.get(), mPangoLyt.get());
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); cairo_set_source_rgb(mCairoCtx.get(), 0.0, 0.0, 0.0);
cairo_surface_flush(mSurface); cairo_surface_flush(mSurface.get());
cairo_destroy(cr); // mCairoCtx smart pointer will handle cleanup
return true; return true;
} }
void Label::writeToPng(const std::string& file) { void Label::writeToPng(const std::string& file) {
if (mSurface) { if (mSurface) {
cairo_surface_flush(mSurface); cairo_surface_flush(mSurface.get());
cairo_surface_write_to_png(mSurface, file.c_str()); cairo_surface_write_to_png(mSurface.get(), file.c_str());
} }
} }
@@ -178,9 +212,6 @@ void Label::setText(const std::string& text) {
Label::~Label() { Label::~Label() {
spdlog::debug("Image dtor..."); spdlog::debug("Image dtor...");
g_object_unref(mPangoCtx); // RAII smart pointers handle cleanup automatically
g_object_unref(mPangoLyt);
g_object_unref(mFontMap);
cairo_surface_destroy(mSurface);
} }
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -22,6 +22,7 @@
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <cstdint> #include <cstdint>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -31,6 +32,28 @@
namespace ptprnt::graphics { namespace ptprnt::graphics {
// Custom deleters for Cairo/Pango resources
struct CairoSurfaceDeleter {
void operator()(cairo_surface_t* surface) const {
if (surface)
cairo_surface_destroy(surface);
}
};
struct CairoDeleter {
void operator()(cairo_t* cr) const {
if (cr)
cairo_destroy(cr);
}
};
struct GObjectDeleter {
void operator()(gpointer obj) const {
if (obj)
g_object_unref(obj);
}
};
class Label : public ILabel { class Label : public ILabel {
public: public:
Label(const uint16_t heightPixel); Label(const uint16_t heightPixel);
@@ -60,13 +83,14 @@ class Label : public ILabel {
// methods // methods
[[nodiscard]] uint8_t getNumLines(std::string_view str); [[nodiscard]] uint8_t getNumLines(std::string_view str);
[[nodiscard]] PangoFontMap* createCustomFontMap(); [[nodiscard]] PangoFontMap* createCustomFontMap();
// members void configureLayout(PangoLayout* layout, const std::string& text, PangoFontDescription* fontDesc);
// TODO: convert raw pointers here into std::unique_ptr with custom deleters, calling g_object_unref() void applyHorizontalAlignment(PangoLayout* layout);
cairo_surface_t* mSurface{nullptr};
cairo_t* mCairoCtx{nullptr}; std::unique_ptr<cairo_surface_t, CairoSurfaceDeleter> mSurface{nullptr};
PangoContext* mPangoCtx{nullptr}; std::unique_ptr<cairo_t, CairoDeleter> mCairoCtx{nullptr};
PangoLayout* mPangoLyt{nullptr}; std::unique_ptr<PangoContext, GObjectDeleter> mPangoCtx{nullptr};
PangoFontMap* mFontMap{nullptr}; std::unique_ptr<PangoLayout, GObjectDeleter> mPangoLyt{nullptr};
std::unique_ptr<PangoFontMap, GObjectDeleter> mFontMap{nullptr};
double mFontSize{DEFAULT_FONT_SIZE}; double mFontSize{DEFAULT_FONT_SIZE};
std::string mFontFamily{DEFAULT_FONT_FAMILY}; std::string mFontFamily{DEFAULT_FONT_FAMILY};
HAlignPosition mHAlign = HAlignPosition::LEFT; HAlignPosition mHAlign = HAlignPosition::LEFT;

View File

@@ -25,71 +25,85 @@
#include <vector> #include <vector>
namespace ptprnt::graphics { namespace ptprnt::graphics {
Monochrome::Monochrome(const std::vector<uint8_t>& grayscale, uint32_t width, uint32_t height) // Constructor from grayscale data
: mPixels(grayscale), mWidth(width), mHeight(height) {} MonochromeData::MonochromeData(const std::vector<uint8_t>& grayscale, uint32_t width, uint32_t height,
Orientation orient)
: stride(0),
orientation(orient),
width(width),
height(height),
mPixels(grayscale),
mIsProcessed(false) {}
Monochrome::Monochrome(const std::span<uint8_t> grayscale, uint32_t width, uint32_t height) MonochromeData::MonochromeData(const std::span<uint8_t> grayscale, uint32_t width, uint32_t height,
: mPixels(grayscale.begin(), grayscale.end()), mWidth(width), mHeight(height) {} Orientation orient)
: stride(0),
orientation(orient),
width(width),
height(height),
mPixels(grayscale.begin(), grayscale.end()),
mIsProcessed(false) {}
void Monochrome::setThreshold(uint8_t threshhold) { void MonochromeData::setThreshold(uint8_t threshold) {
mThreshhold = threshhold; mThreshold = threshold;
mIsProcessed = false; // Mark as needing reprocessing
} }
void Monochrome::invert(bool shouldInvert) { void MonochromeData::invert(bool shouldInvert) {
mShouldInvert = shouldInvert; mShouldInvert = shouldInvert;
mIsProcessed = false; // Mark as needing reprocessing
} }
std::vector<uint8_t> Monochrome::get() { MonochromeData MonochromeData::get() {
// Calculate output size for packed format: (width + 7) / 8 bytes per row if (!mIsProcessed) {
uint32_t stride = (mWidth + 7) / 8; processGrayscaleToMonochrome();
size_t outputSize = stride * mHeight; mIsProcessed = true;
std::vector<uint8_t> outPixels(outputSize, 0); }
// Pack pixels row by row for correct 2D layout // Return a copy of the processed data
for (uint32_t y = 0; y < mHeight; ++y) { MonochromeData result;
for (uint32_t x = 0; x < mWidth; ++x) { result.bytes = bytes;
size_t pixelIndex = y * mWidth + x; // Row-major index in input result.stride = stride;
size_t byteIndex = y * stride + x / 8; // Byte index in packed output result.orientation = orientation;
size_t bitIndex = 7 - (x % 8); // MSB first result.width = width;
result.height = height;
result.mIsProcessed = true;
return result;
}
// Convert grayscale pixel to bit based on threshold void MonochromeData::processGrayscaleToMonochrome() {
bool pixelOn = mPixels[pixelIndex] > mThreshhold; // Calculate stride based on packed monochrome data (1 bit per pixel, 8 pixels per byte)
if (mShouldInvert) { stride = static_cast<uint32_t>((width + 7) / 8);
pixelOn = !pixelOn;
}
if (pixelOn) { // Create the monochrome byte array
outPixels[byteIndex] |= (1 << bitIndex); bytes.clear();
bytes.resize(stride * height, 0);
// Convert grayscale to monochrome
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
uint32_t pixelIndex = y * width + x;
if (pixelIndex < mPixels.size()) {
uint8_t pixelValue = mPixels[pixelIndex];
// Apply threshold
bool isSet = pixelValue >= mThreshold;
// Apply inversion if needed
if (mShouldInvert) {
isSet = !isSet;
}
// Set the bit in the monochrome data
if (isSet) {
setBit(x, y, true);
}
} }
} }
} }
return outPixels;
} }
void Monochrome::visualize() { // Transformation methods implementation
auto mono = get();
for (unsigned char pix : mono) {
std::cout << ((pix & (1 << 7)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 6)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 5)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 4)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 3)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 2)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 1)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 0)) == 0 ? "." : "x");
}
std::cout << std::endl;
}
MonochromeData Monochrome::getMonochromeData() {
auto processedBytes = get();
// Calculate stride based on packed monochrome data (1 bit per pixel, 8 pixels per byte)
auto stride = static_cast<uint32_t>((mWidth + 7) / 8);
return {std::move(processedBytes), stride, Orientation::LANDSCAPE, mWidth, mHeight};
}
// MonochromeData transformation methods implementation
void MonochromeData::transformTo(Orientation targetOrientation) { void MonochromeData::transformTo(Orientation targetOrientation) {
if (orientation == targetOrientation) { if (orientation == targetOrientation) {
return; // No transformation needed return; // No transformation needed
@@ -151,7 +165,7 @@ void MonochromeData::setBit(uint32_t x, uint32_t y, bool value) {
} }
std::vector<uint8_t> MonochromeData::createRotatedData(Orientation targetOrientation) const { std::vector<uint8_t> MonochromeData::createRotatedData(Orientation targetOrientation) const {
uint32_t newWidth, newHeight; uint32_t newWidth = 0, newHeight = 0;
// Determine new dimensions // Determine new dimensions
switch (targetOrientation) { switch (targetOrientation) {
@@ -182,8 +196,8 @@ std::vector<uint8_t> MonochromeData::createRotatedData(Orientation targetOrienta
// Copy pixels with appropriate transformation // Copy pixels with appropriate transformation
for (uint32_t y = 0; y < height; ++y) { for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) { for (uint32_t x = 0; x < width; ++x) {
bool pixel = getBit(x, y); bool pixel = getBit(x, y);
uint32_t newX, newY; uint32_t newX = 0, newY = 0;
switch (targetOrientation) { switch (targetOrientation) {
case Orientation::LANDSCAPE: case Orientation::LANDSCAPE:

View File

@@ -34,19 +34,38 @@ enum class Orientation {
PORTRAIT_FLIPPED = 3 // 270 degrees clockwise (90 counter-clockwise) PORTRAIT_FLIPPED = 3 // 270 degrees clockwise (90 counter-clockwise)
}; };
struct MonochromeData { class MonochromeData {
std::vector<uint8_t> bytes; public:
uint32_t stride; // Constructors
Orientation orientation;
uint32_t width; // Width in pixels
uint32_t height; // Height in pixels
MonochromeData() : stride(0), orientation(Orientation::LANDSCAPE), width(0), height(0) {} MonochromeData() : stride(0), orientation(Orientation::LANDSCAPE), width(0), height(0) {}
MonochromeData(std::vector<uint8_t> data, uint32_t stride_bytes, Orientation orient = Orientation::LANDSCAPE, MonochromeData(std::vector<uint8_t> data, uint32_t stride_bytes, Orientation orient = Orientation::LANDSCAPE,
uint32_t w = 0, uint32_t h = 0) uint32_t w = 0, uint32_t h = 0)
: bytes(std::move(data)), stride(stride_bytes), orientation(orient), width(w), height(h) {} : bytes(std::move(data)), stride(stride_bytes), orientation(orient), width(w), height(h) {}
// Constructor from grayscale data (replaces old Monochrome class)
MonochromeData(const std::vector<uint8_t>& grayscale, uint32_t width, uint32_t height,
Orientation orient = Orientation::LANDSCAPE);
MonochromeData(const std::span<uint8_t> grayscale, uint32_t width, uint32_t height,
Orientation orient = Orientation::LANDSCAPE);
~MonochromeData() = default;
// Copy constructor and assignment
MonochromeData(const MonochromeData&) = default;
MonochromeData& operator=(const MonochromeData&) = default;
// Move constructor and assignment
MonochromeData(MonochromeData&&) = default;
MonochromeData& operator=(MonochromeData&&) = default;
// Configuration methods
void setThreshold(uint8_t threshold);
void invert(bool shouldInvert);
// Get processed monochrome data
MonochromeData get();
// Transform the image data to the target orientation // Transform the image data to the target orientation
void transformTo(Orientation targetOrientation); void transformTo(Orientation targetOrientation);
@@ -57,25 +76,25 @@ struct MonochromeData {
[[nodiscard]] bool getBit(uint32_t x, uint32_t y) const; [[nodiscard]] bool getBit(uint32_t x, uint32_t y) const;
void setBit(uint32_t x, uint32_t y, bool value); void setBit(uint32_t x, uint32_t y, bool value);
[[nodiscard]] std::vector<uint8_t> createRotatedData(Orientation targetOrientation) const; [[nodiscard]] std::vector<uint8_t> createRotatedData(Orientation targetOrientation) const;
};
class Monochrome { // Public member access for backward compatibility
public: std::vector<uint8_t> bytes;
Monochrome(const std::vector<uint8_t>& grayscale, uint32_t width, uint32_t height); uint32_t stride;
Monochrome(const std::span<uint8_t> grayscale, uint32_t width, uint32_t height); Orientation orientation;
~Monochrome() = default; uint32_t width; // Width in pixels
uint32_t height; // Height in pixels
void setThreshold(uint8_t);
void invert(bool shouldInvert);
void visualize();
std::vector<uint8_t> get();
MonochromeData getMonochromeData();
private: private:
std::vector<uint8_t> mPixels; // Processing parameters (for old Monochrome class compatibility)
uint32_t mWidth; std::vector<uint8_t> mPixels; // Original grayscale pixels
uint32_t mHeight; uint8_t mThreshold = UINT8_MAX / 2;
uint8_t mThreshhold = UINT8_MAX / 2; bool mShouldInvert = false;
bool mShouldInvert = false; bool mIsProcessed = false; // Flag to indicate if conversion has been done
// Helper method to convert grayscale to monochrome
void processGrayscaleToMonochrome();
}; };
// For backward compatibility, create a type alias
using Monochrome = MonochromeData;
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -6,7 +6,7 @@ ptprnt_hpps = files (
'libusbwrap/UsbDevice.hpp', 'libusbwrap/UsbDevice.hpp',
'interface/IPrinterDriver.hpp', 'interface/IPrinterDriver.hpp',
'interface/IPrinterTypes.hpp', 'interface/IPrinterTypes.hpp',
'P700Printer.hpp', 'printers/P700Printer.hpp',
'PtouchPrint.hpp', 'PtouchPrint.hpp',
'PrinterDriverFactory.hpp', 'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp', 'graphics/Bitmap.hpp',
@@ -17,7 +17,7 @@ ptprnt_hpps = files (
ptprnt_srcs = files ( ptprnt_srcs = files (
'PtouchPrint.cpp', 'PtouchPrint.cpp',
'PrinterDriverFactory.cpp', 'PrinterDriverFactory.cpp',
'P700Printer.cpp', 'printers/P700Printer.cpp',
'graphics/Label.cpp', 'graphics/Label.cpp',
'graphics/Bitmap.cpp', 'graphics/Bitmap.cpp',
'graphics/Monochrome.cpp', 'graphics/Monochrome.cpp',

View File

@@ -27,9 +27,9 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include "graphics/Bitmap.hpp" #include "../graphics/Bitmap.hpp"
#include "graphics/Monochrome.hpp" #include "../graphics/Monochrome.hpp"
#include "libusbwrap/LibUsbTypes.hpp" #include "../libusbwrap/LibUsbTypes.hpp"
#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 ;)
@@ -121,30 +121,29 @@ bool P700Printer::detachUsbDevice() {
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) { bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
// Convert bitmap to MonochromeData and delegate to printMonochromeData // Convert bitmap to MonochromeData and delegate to printMonochromeData
auto pixels = bitmap.getPixelsCpy(); auto pixels = bitmap.getPixelsCpy();
auto mono = graphics::Monochrome(pixels, bitmap.getWidth(), bitmap.getHeight()); auto mono = graphics::Monochrome(pixels, bitmap.getWidth(), bitmap.getHeight());
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
return printMonochromeData(monoData); return printMonochromeData(monoData);
} }
bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) { bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
#ifdef DRYRUN #ifdef DRYRUN
spdlog::debug("DRYRUN enabled"); spdlog::debug("DRYRUN enabled, printing nothing");
data.visualize();
#endif #endif
send(p700::commands::RASTER_START); send(p700::commands::RASTER_START);
std::vector<uint8_t> rastercmd(4); std::vector<uint8_t> rastercmd(4);
rastercmd[0] = 0x47; rastercmd[0] = 0x47;
rastercmd[1] = 0x00; // size +1 rastercmd[1] = 0x00; // size +1
rastercmd[2] = 0x00; rastercmd[2] = 0x00;
rastercmd[3] = 0x00; // size -1 rastercmd[3] = 0x00; // size -1
// Process data column by column for the printer // Process data column by column for the printer
for (uint32_t col = 0; col < data.width; col++) { for (uint32_t col = 0; col < data.width; col++) {
std::vector<uint8_t> columnData; std::vector<uint8_t> columnData;
// Extract column data bit by bit // Extract column data bit by bit
for (uint32_t row = 0; row < data.height; row += 8) { for (uint32_t row = 0; row < data.height; row += 8) {
uint8_t byte = 0; uint8_t byte = 0;
@@ -155,19 +154,19 @@ bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
} }
columnData.push_back(byte); columnData.push_back(byte);
} }
std::vector<uint8_t> buf; std::vector<uint8_t> buf;
buf.insert(buf.begin(), rastercmd.begin(), rastercmd.end()); buf.insert(buf.begin(), rastercmd.begin(), rastercmd.end());
buf.insert(std::next(buf.begin(), 4), columnData.begin(), columnData.end()); buf.insert(std::next(buf.begin(), 4), columnData.begin(), columnData.end());
buf[1] = columnData.size() + 1; buf[1] = columnData.size() + 1;
buf[3] = columnData.size() - 1; buf[3] = columnData.size() - 1;
if (!send(buf)) { if (!send(buf)) {
spdlog::error("Error sending buffer to printer"); spdlog::error("Error sending buffer to printer");
break; break;
} }
} }
send(p700::commands::EJECT); send(p700::commands::EJECT);
return true; return true;
} }
@@ -175,9 +174,10 @@ bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) { bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
// Convert label directly to MonochromeData // Convert label directly to MonochromeData
auto pixels = label->getRaw(); auto pixels = label->getRaw();
auto mono = graphics::Monochrome(pixels, label->getWidth(), label->getHeight()); auto mono = graphics::Monochrome(pixels, label->getWidth(), label->getHeight(), graphics::Orientation::PORTRAIT);
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
monoData.visualize();
spdlog::debug("Label has {}x{}px size", label->getWidth(), label->getHeight()); spdlog::debug("Label has {}x{}px size", label->getWidth(), label->getHeight());
return printMonochromeData(monoData); return printMonochromeData(monoData);
} }

View File

@@ -23,10 +23,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "interface/IPrinterDriver.hpp" #include "../interface/IPrinterDriver.hpp"
#include "interface/IPrinterTypes.hpp" #include "../interface/IPrinterTypes.hpp"
#include "libusbwrap/LibUsbTypes.hpp" #include "../libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp" #include "../libusbwrap/interface/IUsbDevice.hpp"
#pragma once #pragma once

View File

@@ -29,7 +29,7 @@ TEST(basic_test, Monochrome_convertGrayscale_yieldsMonochrome) {
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1); auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
auto out = mono.get(); auto out = mono.get();
EXPECT_EQ(out, expected); EXPECT_EQ(out.bytes, expected);
} }
TEST(basic_test, Monochrome_convertInvertedGrayscale_yieldsInvertedMonochrome) { TEST(basic_test, Monochrome_convertInvertedGrayscale_yieldsInvertedMonochrome) {
@@ -41,7 +41,7 @@ TEST(basic_test, Monochrome_convertInvertedGrayscale_yieldsInvertedMonochrome) {
mono.invert(true); mono.invert(true);
auto out = mono.get(); auto out = mono.get();
EXPECT_EQ(out, expected); EXPECT_EQ(out.bytes, expected);
} }
TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespectingThreshhold) { TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespectingThreshhold) {
@@ -53,7 +53,7 @@ TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespecti
mono.setThreshold(16); mono.setThreshold(16);
auto out = mono.get(); auto out = mono.get();
EXPECT_EQ(out, expected); EXPECT_EQ(out.bytes, expected);
} }
TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) { TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
@@ -67,14 +67,14 @@ TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
auto mono = ptprnt::graphics::Monochrome(pixels, 17, 1); auto mono = ptprnt::graphics::Monochrome(pixels, 17, 1);
auto out = mono.get(); auto out = mono.get();
EXPECT_EQ(out, expected); EXPECT_EQ(out.bytes, expected);
} }
TEST(MonochromeData_test, MonochromeData_getMonochromeData_returnsStructWithCorrectData) { TEST(MonochromeData_test, MonochromeData_getMonochromeData_returnsStructWithCorrectData) {
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00}); const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
auto mono = ptprnt::graphics::Monochrome(pixels, 8, 1); auto mono = ptprnt::graphics::Monochrome(pixels, 8, 1);
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
EXPECT_EQ(monoData.bytes.size(), 1); EXPECT_EQ(monoData.bytes.size(), 1);
EXPECT_EQ(monoData.bytes[0], 0b10101010); EXPECT_EQ(monoData.bytes[0], 0b10101010);
@@ -90,7 +90,7 @@ TEST(MonochromeData_test, MonochromeData2x2_transformToPortrait_rotatesCorrectly
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF}); const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 2, 2); auto mono = ptprnt::graphics::Monochrome(pixels, 2, 2);
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT); monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT);
@@ -114,7 +114,7 @@ TEST(MonochromeData_test, MonochromeData3x2_transformToPortrait_rotatesCorrectly
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF}); const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2); auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2);
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT); monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT);
@@ -141,7 +141,7 @@ TEST(MonochromeData_test, MonochromeData3x2_transformToPortrait_rotatesCorrectly
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF}); const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2); auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2);
auto monoData = mono.getMonochromeData(); auto monoData = mono.get();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT_FLIPPED); monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT_FLIPPED);