/* ptrnt - print labels on linux Copyright (C) 2023 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 { // 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 { public: Label(const uint16_t heightPixel); ~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() override; [[nodiscard]] int getHeight() override; [[nodiscard]] int getLayoutWidth() override; [[nodiscard]] int getLayoutHeight() override; [[nodiscard]] std::vector getRaw() 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; 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); 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