Got it almost working...
All checks were successful
Build ptprnt / build (push) Successful in 2m29s

This commit is contained in:
2023-12-03 22:08:59 +01:00
parent 3e9c3267e2
commit b42e866a49
14 changed files with 143 additions and 78 deletions

View File

@@ -85,5 +85,6 @@
},
"clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "./builddir/compile_commands.json"
"C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json",
"gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"]
}

View File

@@ -2,6 +2,7 @@
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
HTML_COV_PATH="coverageReport/html"
XML_COV_PATH="coverageReport/xml"
HTML_START_FILE="index.html"
echo "Generating Coverage report for ptouch-prnt"
@@ -12,10 +13,15 @@ ninja -C builddir test
mkdir -p ${HTML_COV_PATH}
gcovr --html --html-details --html-syntax-highlighting --filter src --output ${HTML_COV_PATH}/${HTML_START_FILE}
mkdir -p ${XML_COV_PATH}
gcovr --xml-pretty --filter src --output ${XML_COV_PATH}/cov.xml
if [ $? ]
then
echo "Coverage report successful generated!"
echo "Open: file://${SCRIPT_PATH}/${HTML_COV_PATH}/${HTML_START_FILE}"
else
echo "Error generating coverage report!"
fi
fi
rm *.gcov

View File

@@ -1,7 +1,7 @@
project('ptprnt', 'cpp',
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
license: 'GPLv3',
default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=address,undefined', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
)
usb_dep = dependency('libusb-1.0')

View File

@@ -122,21 +122,23 @@ bool P700Printer::detachUsbDevice() {
}
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
{
auto img = graphics::Label();
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
#ifdef DRYRUN
SPDLOG_DEBUG("DRYRUN enabled");
for (unsigned int lineNo = 0; lineNo < bitmap.getHeight(); lineNo++) {
auto line = bitmap.getLine(lineNo);
auto monoLine = graphics::Monochrome(*line);
monoLine.visualize();
}
#endif
send(commands["rasterstart"]);
std::vector<uint8_t> rastercmd(4);
rastercmd[0] = 0x47;
rastercmd[1] = 0x00; // size +1
rastercmd[2] = 0x00;
rastercmd[3] = 0x00; // size -1
for (unsigned int i = 0; i < bm.getWidth(); i++) {
auto bmcol = bm.getCol(i);
for (unsigned int i = 0; i < bitmap.getWidth(); i++) {
auto bmcol = bitmap.getCol(i);
if (!bmcol) {
spdlog::error("Out of bounds bitmap access");
break;
@@ -151,6 +153,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
buf[1] = col.size() + 1;
buf[3] = col.size() - 1;
if (!send(buf)) {
spdlog::error("Error sending buffer to printer");
break;
};
}
@@ -182,11 +185,11 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
}
#else
tx = data.size();
SPDLOG_DEBUG("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
spdlog::info("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
#endif
if (tx != static_cast<int>(data.size())) {
spdlog::error("Could not transfer all data via USB bulk transfer. Only send {} of {} bytes",
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes",
tx, data.size());
return false;
}

View File

@@ -17,6 +17,7 @@
*/
#include <spdlog/spdlog.h>
#include <sys/types.h>
#include <map>
@@ -67,7 +68,8 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
.pid = 0x2061,
.pixelLines = 128};
std::map<std::string, std::vector<uint8_t>> commands{
{"rasterstart",
{0x1b, 0x69, 0x61,

View File

@@ -16,12 +16,13 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include "graphics/Monochrome.hpp"
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#define SPDLOG_DEBUG_ON
#define SPDLOG_TRACE_ON
#include "PtouchPrint.hpp"
#include <CLI/App.hpp>
#include <fmt/core.h>
#include <spdlog/common.h>
@@ -38,6 +39,7 @@
#include "CLI/Option.hpp"
#include "P700Printer.hpp"
#include "PtouchPrint.hpp"
#include "graphics/Bitmap.hpp"
#include "graphics/Label.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
@@ -93,9 +95,6 @@ int PtouchPrint::run() {
printer->attachUsbDevice(std::move(devices[0]));
auto status = printer->getPrinterStatus();
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
//printer->printBitmap(bm);
//printer->printText("wurst", 1);
if (0 == mCommands.size()) {
spdlog::warn("No command specified, nothing to do...");
@@ -106,7 +105,15 @@ int PtouchPrint::run() {
SPDLOG_DEBUG("Command: {}", cmd.second);
if (cmd.first == CliCmdType::Text) {
auto label{graphics::Label()};
label.createLabel(cmd.second);
label.create(cmd.second, printer->getPrinterInfo().pixelLines);
auto bm = graphics::Bitmap<graphics::ALPHA8>(label.getLayoutWidth(),
printer->getPrinterInfo().pixelLines);
if (!bm.setPixels(label.getRaw())) {
spdlog::error("Non-matching bitmap size");
return -1;
}
label.writeToPng("salat.png");
printer->printBitmap(bm);
}
}

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

View File

@@ -30,6 +30,7 @@ struct PrinterInfo {
std::string_view version = "";
uint16_t vid = 0x00;
uint16_t pid = 0x00;
uint16_t pixelLines = 0;
};
struct PrinterStatus {

View File

@@ -32,7 +32,11 @@
namespace libusbwrap {
UsbDeviceFactory::~UsbDeviceFactory() = default;
UsbDeviceFactory::~UsbDeviceFactory() {
if (mLibusbCtx) {
libusb_exit(mLibusbCtx);
}
};
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
refreshDeviceList();