/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#pragma once
#include
#include
#include
#include
#include
#include "cairo.h"
#include "graphics/interface/ILabel.hpp"
#include "pango/pango-types.h"
namespace ptprnt::graphics {
// Forward declaration
class ICairoWrapper;
// Custom deleters for Cairo/Pango resources that use the wrapper
// Implementation in Label.cpp to avoid incomplete type issues
struct CairoSurfaceDeleter {
std::shared_ptr wrapper;
void operator()(cairo_surface_t* surface) const;
};
struct CairoDeleter {
std::shared_ptr wrapper;
void operator()(cairo_t* cr) const;
};
struct GObjectDeleter {
std::shared_ptr wrapper;
void operator()(gpointer obj) const;
};
class Label : public ILabel {
public:
// Default constructor using real Cairo/Pango implementation
explicit Label(uint16_t heightPixel);
// Constructor for dependency injection (testing)
Label(uint16_t heightPixel, std::shared_ptr cairoWrapper);
~Label() override;
Label(const Label&) = delete;
Label& operator=(const Label&) = delete;
Label(Label&&) = delete;
Label& operator=(Label&&) = delete;
bool create(PrintableText printableText) override;
bool create(const std::string& labelText) override;
void writeToPng(const std::string& file);
[[nodiscard]] int getWidth() const override;
[[nodiscard]] int getHeight() const override;
[[nodiscard]] std::vector getRaw() const override;
void setFontSize(const double fontSize) override;
void setFontFamily(const std::string& fontFamily) override;
void setText(const std::string& text) override;
void setHAlign(HAlignPosition hpos) override;
void setVAlign(VAlignPosition vpos) override;
bool append(const ILabel& other, uint32_t spacingPx = 60) override;
private:
// methods
[[nodiscard]] uint8_t getNumLines(std::string_view str);
[[nodiscard]] PangoFontMap* createCustomFontMap();
void configureLayout(PangoLayout* layout, const std::string& text, PangoFontDescription* fontDesc);
void applyHorizontalAlignment(PangoLayout* layout);
// Cairo/Pango wrapper for dependency injection
std::shared_ptr mCairoWrapper;
std::unique_ptr mSurface{nullptr};
std::unique_ptr mCairoCtx{nullptr};
std::unique_ptr mPangoCtx{nullptr};
std::unique_ptr mPangoLyt{nullptr};
std::unique_ptr mFontMap{nullptr};
double mFontSize{DEFAULT_FONT_SIZE};
std::string mFontFamily{DEFAULT_FONT_FAMILY};
HAlignPosition mHAlign = HAlignPosition::LEFT;
VAlignPosition mVAlign = VAlignPosition::MIDDLE;
std::string mText{""};
int mLayoutWidth = 0, mLayoutHeight = 0;
int mPrinterHeight = 0;
};
} // namespace ptprnt::graphics