Files
ptprnt/src/graphics/Bitmap.cpp
Moritz Martinius 05cd9d244c
All checks were successful
Build ptprnt / build (push) Successful in 3m41s
Generate labels with pangocairo (#8)
Goal of this PR is to have some basic labels generated with pangocairo
- size of the canvas should be matching the input text and grow/shrink accordingly
- basic formatting options like fontsize and align should be working

Reviewed-on: moritz/ptouch-prnt#8
2025-10-12 20:07:18 +00:00

84 lines
2.4 KiB
C++

/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 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/>.
*/
#include "Bitmap.hpp"
#include <spdlog/spdlog.h>
#include <ranges>
#include <stdexcept>
#include <vector>
namespace ptprnt::graphics {
template <class T>
Bitmap<T>::Bitmap(uint16_t width, uint16_t height) : mWidth{width}, mHeight{height}, mPixels(width * height) {}
template <class T>
[[nodiscard]] uint16_t Bitmap<T>::getWidth() const {
return mWidth;
}
template <class T>
[[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 (got {} vs. {} bitmap size).", pixels.size(), mPixels.size());
return false;
}
mPixels = pixels;
return true;
}
template <class T>
[[nodiscard]] std::vector<T> Bitmap<T>::getPixelsCpy() const {
return mPixels;
}
template <class T>
[[nodiscard]] std::vector<T> Bitmap<T>::getLine(const uint16_t lineNo) const {
if (lineNo >= mHeight) {
throw(std::out_of_range("Line is out of range!"));
}
auto lineStart = mPixels.begin() + (lineNo * mWidth);
auto lineEnd = mPixels.begin() + ((lineNo + 1) * mWidth);
return std::vector<T>(lineStart, lineEnd);
}
// TODO: I guess this is borked
template <class T>
[[nodiscard]] std::vector<T> Bitmap<T>::getCol(const uint16_t colNo) const {
if (colNo >= mWidth) {
throw(std::out_of_range("Col is out of range!"));
}
std::vector<T> col{};
col.reserve(mHeight);
for (size_t i{0}; i <= mPixels.size(); i++) {
if (i % mWidth == colNo) {
col.push_back(mPixels[i]);
}
}
return col;
}
} // namespace ptprnt::graphics