Width is no automatically chosen based on image width

This commit is contained in:
2023-11-21 20:44:20 +01:00
parent 3d5b1ae7b6
commit f32079fe46

View File

@@ -29,6 +29,7 @@
#include "cairo.h"
#include "pango/pango-context.h"
#include "pango/pango-font.h"
#include "pango/pango-fontmap.h"
#include "pango/pango-layout.h"
#include "pango/pango-types.h"
#include "pango/pangocairo.h"
@@ -65,38 +66,38 @@ uint8_t Image::getNumLines(std::string_view strv) {
}
void Image::createLabel(const std::string& str) {
// create Pango layout first, to get the render dimensions
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
cairo_t* cr = cairo_create(mSurface);
//auto pangoCtx{pango_context_new()};
auto pangoLyt{pango_cairo_create_layout(cr)};
auto pangoFontDesc{pango_font_description_from_string("FreeSans 32")};
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")};
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_layout_set_text(pangoLyt, str.c_str(), static_cast<int>(str.length()));
pango_context_load_font(pangoCtx, pangoFontDesc);
pango_layout_set_height(pangoLyt, 128 * PANGO_SCALE)
pango_layout_set_text(pangoLyt, str.c_str(), static_cast<int>(str.length()));
// Debug only
GError* gerr{nullptr};
pango_layout_write_to_file(pangoLyt, PANGO_LAYOUT_SERIALIZE_DEFAULT, "hello.txt", &gerr);
// create Cairo surface from p ango layout
int* width{};
int* height{}; // TODO: change me, fixed for current printer (128px)
PangoRectangle* rect{};
int width, height;
pango_layout_get_size(pangoLyt, &width, &height);
spdlog::debug("Layout width: {}, height: {}", width / PANGO_SCALE, height / PANGO_SCALE);
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);
pango_layout_get_extents(pangoLyt, rect, rect);
std::cerr << "Width is: " << rect->width << " height is:" << rect->height << std::endl;
//auto cairoSfc{cairo_image_surface_create(CAIRO_FORMAT_A8, *width, *height)};
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
// Debug only
cairo_surface_write_to_png(mSurface, "hellow.png");
cairo_surface_write_to_png(surf, "hellow.png");
}
Image::~Image() {