Multilabel support & Label refactoring (#17)
All checks were successful
Build ptprnt / build (push) Successful in 3m47s
All checks were successful
Build ptprnt / build (push) Successful in 3m47s
Reviewed-on: moritz/ptouch-prnt#17
This commit was merged in pull request #17.
This commit is contained in:
@@ -25,37 +25,63 @@
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cairo.h"
|
||||
#include "graphics/CairoWrapper.hpp"
|
||||
#include "graphics/interface/ICairoWrapper.hpp"
|
||||
#include "graphics/interface/ILabel.hpp"
|
||||
#include "pango/pango-font.h"
|
||||
#include "pango/pango-layout.h"
|
||||
#include "pango/pango-types.h"
|
||||
#include "pango/pangocairo.h"
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
Label::Label(const uint16_t heightPixel)
|
||||
: mPrinterHeight(heightPixel) {
|
||||
// Initialize resources in correct order with RAII
|
||||
mFontMap.reset(pango_cairo_font_map_new());
|
||||
|
||||
// Deleter implementations
|
||||
void CairoSurfaceDeleter::operator()(cairo_surface_t* surface) const {
|
||||
if (surface && wrapper)
|
||||
wrapper->cairo_surface_destroy(surface);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Label::getRaw() {
|
||||
void CairoDeleter::operator()(cairo_t* cr) const {
|
||||
if (cr && wrapper)
|
||||
wrapper->cairo_destroy(cr);
|
||||
}
|
||||
|
||||
void GObjectDeleter::operator()(gpointer obj) const {
|
||||
if (obj && wrapper)
|
||||
wrapper->g_object_unref(obj);
|
||||
}
|
||||
|
||||
// Default constructor - creates real Cairo/Pango wrapper
|
||||
Label::Label(const uint16_t heightPixel) : Label(heightPixel, std::make_shared<CairoWrapper>()) {}
|
||||
|
||||
// Constructor with dependency injection
|
||||
Label::Label(const uint16_t heightPixel, std::shared_ptr<ICairoWrapper> cairoWrapper)
|
||||
: mCairoWrapper(std::move(cairoWrapper)), mPrinterHeight(heightPixel) {
|
||||
// Initialize resources in correct order with RAII
|
||||
// Pass wrapper to deleter so cleanup uses the wrapper
|
||||
GObjectDeleter deleter;
|
||||
deleter.wrapper = mCairoWrapper;
|
||||
mFontMap = std::unique_ptr<PangoFontMap, GObjectDeleter>(mCairoWrapper->pango_cairo_font_map_new(), deleter);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Label::getRaw() const {
|
||||
assert(mSurface != nullptr);
|
||||
auto* surface = mSurface.get();
|
||||
|
||||
cairo_surface_flush(surface);
|
||||
assert(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_A8);
|
||||
mCairoWrapper->cairo_surface_flush(surface);
|
||||
assert(mCairoWrapper->cairo_image_surface_get_format(surface) == CAIRO_FORMAT_A8);
|
||||
|
||||
int width = cairo_image_surface_get_width(surface);
|
||||
int height = cairo_image_surface_get_height(surface);
|
||||
int stride = cairo_image_surface_get_stride(surface);
|
||||
int width = mCairoWrapper->cairo_image_surface_get_width(surface);
|
||||
int height = mCairoWrapper->cairo_image_surface_get_height(surface);
|
||||
int stride = mCairoWrapper->cairo_image_surface_get_stride(surface);
|
||||
|
||||
spdlog::debug("Cairo Surface data: W: {}; H: {}; S:{}", width, height, stride);
|
||||
|
||||
auto data = cairo_image_surface_get_data(surface);
|
||||
auto data = mCairoWrapper->cairo_image_surface_get_data(surface);
|
||||
|
||||
// If stride equals width, we can return data directly
|
||||
if (stride == width) {
|
||||
@@ -80,41 +106,41 @@ uint8_t Label::getNumLines(std::string_view strv) {
|
||||
return std::count(strv.begin(), strv.end(), '\n');
|
||||
}
|
||||
|
||||
int Label::getWidth() {
|
||||
int Label::getWidth() const {
|
||||
// Return the actual Cairo surface width (which is the layout width)
|
||||
return mLayoutWidth;
|
||||
}
|
||||
|
||||
int Label::getHeight() {
|
||||
int Label::getHeight() const {
|
||||
// Return the actual Cairo surface height (which is the printer height)
|
||||
return mPrinterHeight;
|
||||
}
|
||||
|
||||
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);
|
||||
mCairoWrapper->pango_layout_set_font_description(layout, fontDesc);
|
||||
mCairoWrapper->pango_layout_set_text(layout, text.c_str(), static_cast<int>(text.length()));
|
||||
mCairoWrapper->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);
|
||||
mCairoWrapper->pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
|
||||
break;
|
||||
case HAlignPosition::RIGHT:
|
||||
pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
|
||||
mCairoWrapper->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);
|
||||
mCairoWrapper->pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
|
||||
mCairoWrapper->pango_layout_set_justify(layout, true);
|
||||
#if PANGO_VERSION_MAJOR >= 1 && PANGO_VERSION_MINOR >= 50
|
||||
pango_layout_set_justify_last_line(layout, true);
|
||||
mCairoWrapper->pango_layout_set_justify_last_line(layout, true);
|
||||
#endif
|
||||
break;
|
||||
case HAlignPosition::CENTER:
|
||||
[[fallthrough]];
|
||||
default:
|
||||
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
|
||||
mCairoWrapper->pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -133,40 +159,49 @@ bool Label::create(const std::string& labelText) {
|
||||
// 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);
|
||||
auto* tempSurface = mCairoWrapper->cairo_image_surface_create(CAIRO_FORMAT_A8, 1, 1);
|
||||
auto* tempCr = mCairoWrapper->cairo_create(tempSurface);
|
||||
auto* tempPangoCtx = mCairoWrapper->pango_cairo_create_context(tempCr);
|
||||
auto* tempPangoLyt = mCairoWrapper->pango_layout_new(tempPangoCtx);
|
||||
|
||||
PangoFontDescription* regularFont = pango_font_description_new();
|
||||
pango_font_description_set_size(regularFont, static_cast<int>(mFontSize * PANGO_SCALE));
|
||||
pango_font_description_set_family(regularFont, mFontFamily.c_str());
|
||||
PangoFontDescription* regularFont = mCairoWrapper->pango_font_description_new();
|
||||
mCairoWrapper->pango_font_description_set_size(regularFont, static_cast<int>(mFontSize * PANGO_SCALE));
|
||||
mCairoWrapper->pango_font_description_set_family(regularFont, mFontFamily.c_str());
|
||||
|
||||
// Configure temporary layout for size calculation
|
||||
configureLayout(tempPangoLyt, labelText, regularFont);
|
||||
applyHorizontalAlignment(tempPangoLyt);
|
||||
|
||||
// Calculate label size from temporary layout
|
||||
pango_layout_get_size(tempPangoLyt, &mLayoutWidth, &mLayoutHeight);
|
||||
mCairoWrapper->pango_layout_get_size(tempPangoLyt, &mLayoutWidth, &mLayoutHeight);
|
||||
mLayoutWidth /= PANGO_SCALE;
|
||||
mLayoutHeight /= PANGO_SCALE;
|
||||
|
||||
spdlog::debug("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight);
|
||||
//auto alignedWidth = mLayoutWidth + (8 - (mLayoutWidth % 8));
|
||||
//spdlog::debug("Aligned Layout width: {}, height: {}", alignedWidth, mLayoutHeight);
|
||||
|
||||
// Clean up temporary resources
|
||||
g_object_unref(tempPangoLyt);
|
||||
g_object_unref(tempPangoCtx);
|
||||
cairo_destroy(tempCr);
|
||||
cairo_surface_destroy(tempSurface);
|
||||
mCairoWrapper->g_object_unref(tempPangoLyt);
|
||||
mCairoWrapper->g_object_unref(tempPangoCtx);
|
||||
mCairoWrapper->cairo_destroy(tempCr);
|
||||
mCairoWrapper->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()));
|
||||
// Create deleters with wrapper reference
|
||||
CairoSurfaceDeleter surfaceDeleter;
|
||||
surfaceDeleter.wrapper = mCairoWrapper;
|
||||
CairoDeleter cairoDeleter;
|
||||
cairoDeleter.wrapper = mCairoWrapper;
|
||||
GObjectDeleter gobjectDeleter;
|
||||
gobjectDeleter.wrapper = mCairoWrapper;
|
||||
|
||||
mSurface = std::unique_ptr<cairo_surface_t, CairoSurfaceDeleter>(
|
||||
mCairoWrapper->cairo_image_surface_create(CAIRO_FORMAT_A8, mLayoutWidth, mPrinterHeight), surfaceDeleter);
|
||||
cairo_t* cr = mCairoWrapper->cairo_create(mSurface.get());
|
||||
mCairoCtx = std::unique_ptr<cairo_t, CairoDeleter>(cr, cairoDeleter);
|
||||
mPangoCtx =
|
||||
std::unique_ptr<PangoContext, GObjectDeleter>(mCairoWrapper->pango_cairo_create_context(cr), gobjectDeleter);
|
||||
mPangoLyt =
|
||||
std::unique_ptr<PangoLayout, GObjectDeleter>(mCairoWrapper->pango_layout_new(mPangoCtx.get()), gobjectDeleter);
|
||||
|
||||
// Configure final layout with same settings
|
||||
configureLayout(mPangoLyt.get(), labelText, regularFont);
|
||||
@@ -177,31 +212,104 @@ bool Label::create(const std::string& labelText) {
|
||||
case VAlignPosition::TOP:
|
||||
break;
|
||||
case VAlignPosition::BOTTOM:
|
||||
cairo_move_to(mCairoCtx.get(), 0.0, mPrinterHeight - mLayoutHeight);
|
||||
mCairoWrapper->cairo_move_to(mCairoCtx.get(), 0.0, mPrinterHeight - mLayoutHeight);
|
||||
break;
|
||||
case VAlignPosition::MIDDLE:
|
||||
cairo_move_to(mCairoCtx.get(), 0.0, (mPrinterHeight - mLayoutHeight) / 2);
|
||||
mCairoWrapper->cairo_move_to(mCairoCtx.get(), 0.0, (mPrinterHeight - mLayoutHeight) / 2);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Finally show the layout on the Cairo surface
|
||||
pango_cairo_show_layout(mCairoCtx.get(), mPangoLyt.get());
|
||||
mCairoWrapper->pango_cairo_show_layout(mCairoCtx.get(), mPangoLyt.get());
|
||||
|
||||
cairo_set_source_rgb(mCairoCtx.get(), 0.0, 0.0, 0.0);
|
||||
cairo_surface_flush(mSurface.get());
|
||||
mCairoWrapper->cairo_set_source_rgb(mCairoCtx.get(), 0.0, 0.0, 0.0);
|
||||
mCairoWrapper->cairo_surface_flush(mSurface.get());
|
||||
// mCairoCtx smart pointer will handle cleanup
|
||||
return true;
|
||||
}
|
||||
|
||||
void Label::writeToPng(const std::string& file) {
|
||||
if (mSurface) {
|
||||
cairo_surface_flush(mSurface.get());
|
||||
cairo_surface_write_to_png(mSurface.get(), file.c_str());
|
||||
mCairoWrapper->cairo_surface_flush(mSurface.get());
|
||||
mCairoWrapper->cairo_surface_write_to_png(mSurface.get(), file.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool Label::append(const ILabel& other, uint32_t spacingPx) {
|
||||
// Check that heights match
|
||||
if (getHeight() != other.getHeight()) {
|
||||
spdlog::error("Cannot append labels with different heights: {} vs {}", getHeight(), other.getHeight());
|
||||
return false;
|
||||
}
|
||||
|
||||
int currentWidth = getWidth();
|
||||
int otherWidth = other.getWidth();
|
||||
int height = getHeight();
|
||||
int spacing = static_cast<int>(spacingPx);
|
||||
int newWidth = currentWidth + spacing + otherWidth;
|
||||
|
||||
spdlog::debug("Appending label: current={}x{}, other={}x{}, spacing={}, new={}x{}", currentWidth, height,
|
||||
otherWidth, height, spacing, newWidth, height);
|
||||
|
||||
// Get current and other label data
|
||||
auto currentData = getRaw();
|
||||
auto otherData = other.getRaw();
|
||||
|
||||
// Create new surface with extended width
|
||||
CairoSurfaceDeleter surfaceDeleter;
|
||||
surfaceDeleter.wrapper = mCairoWrapper;
|
||||
auto newSurface = std::unique_ptr<cairo_surface_t, CairoSurfaceDeleter>(
|
||||
mCairoWrapper->cairo_image_surface_create(CAIRO_FORMAT_A8, newWidth, height), surfaceDeleter);
|
||||
|
||||
if (mCairoWrapper->cairo_surface_status(newSurface.get()) != CAIRO_STATUS_SUCCESS) {
|
||||
spdlog::error("Failed to create new surface for appended label");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get data pointer and stride
|
||||
mCairoWrapper->cairo_surface_flush(newSurface.get());
|
||||
unsigned char* newData = mCairoWrapper->cairo_image_surface_get_data(newSurface.get());
|
||||
int newStride = mCairoWrapper->cairo_image_surface_get_stride(newSurface.get());
|
||||
|
||||
// Clear the new surface (set to transparent/white)
|
||||
std::memset(newData, 0x00, newStride * height);
|
||||
|
||||
// Copy current label data
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < currentWidth; ++x) {
|
||||
size_t srcIdx = y * currentWidth + x;
|
||||
size_t dstIdx = y * newStride + x;
|
||||
if (srcIdx < currentData.size()) {
|
||||
newData[dstIdx] = currentData[srcIdx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy other label data (with spacing offset)
|
||||
int xOffset = currentWidth + spacing;
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < otherWidth; ++x) {
|
||||
size_t srcIdx = y * otherWidth + x;
|
||||
size_t dstIdx = y * newStride + (xOffset + x);
|
||||
if (srcIdx < otherData.size()) {
|
||||
newData[dstIdx] = otherData[srcIdx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mCairoWrapper->cairo_surface_mark_dirty(newSurface.get());
|
||||
|
||||
// Replace current surface with new one
|
||||
mSurface = std::move(newSurface);
|
||||
|
||||
// Update layout dimensions
|
||||
mLayoutWidth = newWidth;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Label::setFontSize(const double fontSize) {
|
||||
mFontSize = fontSize;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user