All checks were successful
Build ptprnt / build (push) Successful in 3m47s
Reviewed-on: moritz/ptouch-prnt#17
108 lines
3.7 KiB
C++
108 lines
3.7 KiB
C++
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#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<ICairoWrapper> wrapper;
|
|
void operator()(cairo_surface_t* surface) const;
|
|
};
|
|
|
|
struct CairoDeleter {
|
|
std::shared_ptr<ICairoWrapper> wrapper;
|
|
void operator()(cairo_t* cr) const;
|
|
};
|
|
|
|
struct GObjectDeleter {
|
|
std::shared_ptr<ICairoWrapper> 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<ICairoWrapper> 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<uint8_t> 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<ICairoWrapper> mCairoWrapper;
|
|
|
|
std::unique_ptr<cairo_surface_t, CairoSurfaceDeleter> mSurface{nullptr};
|
|
std::unique_ptr<cairo_t, CairoDeleter> mCairoCtx{nullptr};
|
|
std::unique_ptr<PangoContext, GObjectDeleter> mPangoCtx{nullptr};
|
|
std::unique_ptr<PangoLayout, GObjectDeleter> mPangoLyt{nullptr};
|
|
std::unique_ptr<PangoFontMap, GObjectDeleter> 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
|