Got it almost working...
This commit is contained in:
@@ -22,9 +22,11 @@
|
||||
#include <algorithm>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <iostream> // remove me
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cairo.h"
|
||||
#include "pango/pango-context.h"
|
||||
@@ -35,67 +37,69 @@
|
||||
#include "pango/pangocairo.h"
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
Label::Label() {
|
||||
/*mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
|
||||
cairo_t* cr = cairo_create(mSurface);
|
||||
mFontDescription = pango_font_description_new();
|
||||
pango_font_description_set_family(mFontDescription, "sans");
|
||||
pango_font_description_set_weight(mFontDescription, PANGO_WEIGHT_SEMIBOLD);
|
||||
pango_font_description_set_size(mFontDescription, 60 * PANGO_SCALE);
|
||||
Label::Label()
|
||||
: mPangoCtx(pango_font_map_create_context(pango_cairo_font_map_get_default())),
|
||||
mPangoLyt(pango_layout_new(mPangoCtx)),
|
||||
mPangoFontDesc(pango_font_description_from_string("Noto sans 32")) {}
|
||||
|
||||
std::string printThis("Mist 💩");
|
||||
std::vector<uint8_t> Label::getRaw() {
|
||||
assert(mSurface != nullptr);
|
||||
size_t len = mPrinterHeight * mLayoutWidth;
|
||||
|
||||
mLayout = pango_cairo_create_layout(cr);
|
||||
pango_layout_set_font_description(mLayout, mFontDescription);
|
||||
pango_layout_set_text(mLayout, printThis.c_str(), -1);
|
||||
|
||||
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
||||
cairo_move_to(cr, 0.0, 94.0);
|
||||
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
|
||||
|
||||
cairo_surface_write_to_png(mSurface, "hello.png");*/
|
||||
}
|
||||
|
||||
uint8_t* Label::getRaw() {
|
||||
cairo_surface_flush(mSurface);
|
||||
return cairo_image_surface_get_data(mSurface);
|
||||
auto data = cairo_image_surface_get_data(mSurface);
|
||||
return {data, data + len};
|
||||
}
|
||||
|
||||
uint8_t Label::getNumLines(std::string_view strv) {
|
||||
return std::count(strv.begin(), strv.end(), '\n');
|
||||
}
|
||||
|
||||
void Label::createLabel(const std::string& str) {
|
||||
int Label::getLayoutHeight() {
|
||||
return mLayoutHeight;
|
||||
}
|
||||
|
||||
// create Pango layout first, to get the render dimensions
|
||||
auto pangoCtx{pango_font_map_create_context(pango_cairo_font_map_get_default())};
|
||||
auto pangoLyt{pango_layout_new(pangoCtx)};
|
||||
auto pangoFontDesc{pango_font_description_from_string("Noto sans 32")};
|
||||
int Label::getLayoutWidth() {
|
||||
return mLayoutWidth;
|
||||
}
|
||||
|
||||
pango_layout_set_single_paragraph_mode(pangoLyt, true);
|
||||
pango_layout_set_height(pangoLyt, getNumLines(str) * -1);
|
||||
pango_layout_set_alignment(pangoLyt, PANGO_ALIGN_CENTER);
|
||||
pango_layout_set_font_description(pangoLyt, pangoFontDesc);
|
||||
pango_context_load_font(pangoCtx, pangoFontDesc);
|
||||
pango_layout_set_text(pangoLyt, str.c_str(), static_cast<int>(str.length()));
|
||||
void Label::create(const std::string& labelText, const uint16_t heightPixel) {
|
||||
mPrinterHeight = heightPixel;
|
||||
pango_layout_set_single_paragraph_mode(mPangoLyt, true);
|
||||
pango_layout_set_height(mPangoLyt, getNumLines(labelText) * -1);
|
||||
pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_CENTER);
|
||||
pango_layout_set_font_description(mPangoLyt, mPangoFontDesc);
|
||||
pango_context_load_font(mPangoCtx, mPangoFontDesc);
|
||||
pango_layout_set_text(mPangoLyt, labelText.c_str(), static_cast<int>(labelText.length()));
|
||||
|
||||
int width = 0, height = 0;
|
||||
pango_layout_get_size(mPangoLyt, &mLayoutWidth, &mLayoutHeight);
|
||||
|
||||
pango_layout_get_size(pangoLyt, &width, &height);
|
||||
mLayoutWidth /= PANGO_SCALE;
|
||||
mLayoutHeight /= PANGO_SCALE;
|
||||
|
||||
SPDLOG_DEBUG("Layout width: {}, height: {}", width / PANGO_SCALE, height / PANGO_SCALE);
|
||||
SPDLOG_DEBUG("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight);
|
||||
|
||||
auto surf = cairo_image_surface_create(CAIRO_FORMAT_A8, width / PANGO_SCALE, 128);
|
||||
cairo_t* cr = cairo_create(surf);
|
||||
pango_cairo_show_layout(cr, pangoLyt);
|
||||
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, mLayoutWidth, mPrinterHeight);
|
||||
cairo_t* cr = cairo_create(mSurface);
|
||||
pango_cairo_show_layout(cr, mPangoLyt);
|
||||
|
||||
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
||||
cairo_surface_flush(mSurface);
|
||||
cairo_destroy(cr);
|
||||
}
|
||||
|
||||
// Debug only
|
||||
cairo_surface_write_to_png(surf, "hellow.png");
|
||||
void Label::writeToPng(const std::string& file) {
|
||||
if (mSurface) {
|
||||
cairo_surface_flush(mSurface);
|
||||
cairo_surface_write_to_png(mSurface, file.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
Label::~Label() {
|
||||
SPDLOG_DEBUG("Image dtor...");
|
||||
pango_font_description_free(mPangoFontDesc);
|
||||
g_object_unref(mPangoCtx);
|
||||
g_object_unref(mPangoLyt);
|
||||
cairo_surface_destroy(mSurface);
|
||||
}
|
||||
} // namespace ptprnt::graphics
|
Reference in New Issue
Block a user