/* 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 . */ #include "Image.hpp" #include #include #include #include // remove me #include #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" namespace ptprnt::graphics { Image::Image() { /*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); std::string printThis("Mist 💩"); 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* Image::getRaw() { cairo_surface_flush(mSurface); return cairo_image_surface_get_data(mSurface); } uint8_t Image::getNumLines(std::string_view strv) { return std::count(strv.begin(), strv.end(), '\n'); } void Image::createLabel(const std::string& str) { // 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")}; 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(str.length())); // Debug only GError* gerr{nullptr}; pango_layout_write_to_file(pangoLyt, PANGO_LAYOUT_SERIALIZE_DEFAULT, "hello.txt", &gerr); int width = 0, height = 0; 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); cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); // Debug only cairo_surface_write_to_png(surf, "hellow.png"); } Image::~Image() { spdlog::info("Image dtor..."); } } // namespace ptprnt::graphics