All checks were successful
Build ptprnt / build (push) Successful in 3m44s
100 lines
3.2 KiB
C++
100 lines
3.2 KiB
C++
/*
|
|
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 <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 {
|
|
|
|
// 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]] std::vector<uint8_t> 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<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
|