Got it almost working...

This commit is contained in:
2023-12-03 22:08:59 +01:00
parent 1163ae5745
commit 5a38600e2a
14 changed files with 128 additions and 74 deletions

View File

@@ -22,6 +22,7 @@
#include <algorithm>
#include <spdlog/spdlog.h>
#include <iostream>
#include <iterator>
#include <optional>
#include <vector>
@@ -32,19 +33,20 @@ Bitmap<T>::Bitmap(uint16_t width, uint16_t height)
: mWidth{width}, mHeight{height}, mPixels(width * height) {}
template <class T>
uint16_t Bitmap<T>::getWidth() {
[[nodiscard]] uint16_t Bitmap<T>::getWidth() const {
return mWidth;
}
template <class T>
uint16_t Bitmap<T>::getHeight() {
[[nodiscard]] uint16_t Bitmap<T>::getHeight() const {
return mHeight;
}
template <class T>
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
if (pixels.size() != mPixels.size()) {
spdlog::error("Invalid pixel buffer size.");
spdlog::error("Invalid pixel buffer size (got {} vs. {} bitmap size).", pixels.size(),
mPixels.size());
return false;
}
@@ -53,12 +55,12 @@ bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
}
template <class T>
std::vector<T> Bitmap<T>::getPixelsCpy() {
[[nodiscard]] std::vector<T> Bitmap<T>::getPixelsCpy() const {
return mPixels;
}
template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) const {
if (line >= mHeight) {
// out of bound
return std::nullopt;
@@ -70,7 +72,7 @@ std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
}
template <class T>
std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) {
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) const {
if (col >= mWidth) {
// out of bound
return std::nullopt;

View File

@@ -39,12 +39,18 @@ class Bitmap {
Bitmap(uint16_t width, uint16_t height);
~Bitmap() = default;
uint16_t getWidth();
uint16_t getHeight();
Bitmap(const Bitmap&) = default;
Bitmap& operator=(const Bitmap&) = default;
Bitmap(Bitmap&&) = default;
Bitmap& operator=(Bitmap&&) = default;
[[nodiscard]] uint16_t getWidth() const;
[[nodiscard]] uint16_t getHeight() const;
bool setPixels(const std::vector<T>& pixels);
std::vector<T> getPixelsCpy();
std::optional<std::vector<T>> getLine(uint16_t line);
std::optional<std::vector<T>> getCol(uint16_t col);
[[nodiscard]] std::vector<T> getPixelsCpy() const;
[[nodiscard]] std::optional<std::vector<T>> getLine(uint16_t line) const;
[[nodiscard]] std::optional<std::vector<T>> getCol(uint16_t col) const;
void visualize() const;
private:
uint16_t mWidth;

View File

@@ -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

View File

@@ -23,6 +23,11 @@
#include <cstdint>
#include <string>
#include <vector>
#include "cairo.h"
#include "pango/pango-font.h"
#include "pango/pango-types.h"
namespace ptprnt::graphics {
@@ -39,15 +44,21 @@ class Label {
Label(Label&&) = delete;
Label& operator=(Label&&) = delete;
uint8_t* getRaw();
void createLabel(const std::string& labelText);
std::vector<uint8_t> getRaw();
void create(const std::string& labelText, const uint16_t heightPixel);
void writeToPng(const std::string& file);
int getLayoutWidth();
int getLayoutHeight();
private:
// methods
uint8_t getNumLines(std::string_view str);
// members
PangoLayout* mLayout;
PangoFontDescription* mFontDescription;
cairo_surface_t* mSurface;
PangoContext* mPangoCtx{nullptr};
PangoLayout* mPangoLyt{nullptr};
PangoFontDescription* mPangoFontDesc{nullptr};
cairo_surface_t* mSurface{nullptr};
int mLayoutWidth = 0, mLayoutHeight = 0;
int mPrinterHeight = 0;
};
} // namespace ptprnt::graphics

View File

@@ -21,7 +21,8 @@
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
@@ -57,4 +58,20 @@ std::vector<uint8_t> Monochrome::get() {
}
return outPixels;
}
void Monochrome::visualize() {
auto mono = get();
for (unsigned char pix : mono) {
std::cout << ((pix & (1 << 7)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 6)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 5)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 4)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 3)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 2)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 1)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 0)) == 0 ? "." : "x");
}
std::cout << std::endl;
}
} // namespace ptprnt::graphics

View File

@@ -31,11 +31,12 @@ class Monochrome {
void setThreshold(uint8_t);
void invert(bool shouldInvert);
void visualize();
std::vector<uint8_t> get();
private:
const std::vector<uint8_t>& mPixels;
uint8_t mThreshhold = 127;
uint8_t mThreshhold = UINT8_MAX / 2;
bool mShouldInvert = false;
};
} // namespace ptprnt::graphics