1 Commits

Author SHA1 Message Date
ed77d34f1c A somewhat working state, but still struggeling with pangocairo
Some checks failed
Build ptprnt / build (push) Failing after 39s
2023-11-12 12:26:19 +01:00
29 changed files with 211 additions and 399 deletions

View File

@@ -14,41 +14,14 @@ jobs:
- name: install meson - name: install meson
run: apt-get -yq install meson run: apt-get -yq install meson
- name: Install build dependencies - name: Install build dependencies
run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev gcovr run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr
- name: get build environment versions
run: |
echo "=== Start meson version ==="
gcc --version
echo "=== End meson version ==="
echo "=== Start supported c/cpp standards: ==="
gcc -v --help 2> /dev/null | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'
echo "=== End supported c/cpp standards ==="
echo "=== Start meson version ==="
meson --version
echo "=== End meson version ==="
echo "=== Start ninja version ==="
ninja --version
echo "=== End ninja version ==="
echo "=== Start dependency package version ==="
apt-cache policy libpango1.0-dev
apt-cache policy libcairo2-dev
apt-cache policy libfmt-dev
apt-cache policy libspdlog-dev
apt-cache policy libusb-1.0-0-dev
echo "=== End dependency package version ==="
- name: setup builddir - name: setup builddir
run: meson setup builddir -Db_coverage=true run: meson setup builddir -Db_coverage=true
- name: build and test dist package - name: build all targets
run: ninja -C builddir dist run: ninja -C builddir
- name: run unit tests - name: run unit tests
run: ninja -C builddir test run: ninja -C builddir test
- name: calculate coverage - name: calculate coverage
run: ninja -C builddir coverage-text run: ninja -C builddir coverage-text
- name: Coverage report - name: Coverage report
run: cat ./builddir/meson-logs/coverage.txt run: cat ./builddir/meson-logs/coverage.txt
- name: upload dist package
uses: actions/upload-artifact@v3
with:
name: ptprnt-dist
path: ./builddir/meson-dist/*
if-no-files-found: error

9
.gitignore vendored
View File

@@ -1,14 +1,9 @@
# Folder
builddir/ builddir/
ptouch-print/
subprojects/* subprojects/*
.cache/
coverageReport/
# Files
ptprnt.log
!subprojects/*.wrap !subprojects/*.wrap
.cache/
.vscode/* .vscode/*
!.vscode/c_cpp_properties.json !.vscode/c_cpp_properties.json
!.vscode/settings.json !.vscode/settings.json
!.vscode/launch.json !.vscode/launch.json
ptouch-print/

View File

@@ -85,6 +85,5 @@
}, },
"clang-tidy.buildPath": "builddir/", "clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart", "clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "/home/moritz/src/ptouch-prnt/builddir/compile_commands.json", "C_Cpp.default.compileCommands": "builddir/compile_commands.json"
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
} }

View File

@@ -19,7 +19,7 @@ pacman -S libusb spdlog pango cairo meson gcovr
Install dependencies on Debian/Ubuntu Install dependencies on Debian/Ubuntu
``` bash ``` bash
apt-get install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev meson gcovr apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr
``` ```
## Build ## Build

View File

@@ -1,21 +0,0 @@
#!/bin/bash
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
HTML_COV_PATH="coverageReport/html"
HTML_START_FILE="index.html"
echo "Generating Coverage report for ptouch-prnt"
ninja -C builddir
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}
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

View File

@@ -6,7 +6,6 @@ project('ptprnt', 'cpp',
usb_dep = dependency('libusb-1.0') usb_dep = dependency('libusb-1.0')
log_dep = dependency('spdlog') log_dep = dependency('spdlog')
fmt_dep = dependency('fmt')
pangocairo_dep = dependency('pangocairo') pangocairo_dep = dependency('pangocairo')
# CLI11 # CLI11
@@ -18,15 +17,45 @@ endif
incdir = include_directories('src') incdir = include_directories('src')
subdir('src') ptprnt_hpps = [
'src/libusbwrap/interface/IUsbDeviceFactory.hpp',
'src/libusbwrap/interface/IUsbDevice.hpp',
'src/libusbwrap/UsbDeviceFactory.hpp',
'src/libusbwrap/LibUsbTypes.hpp',
'src/libusbwrap/UsbDevice.hpp',
'src/interface/IPrinterDriver.hpp',
'src/interface/IPrinterTypes.hpp',
'src/P700Printer.hpp',
'src/PtouchPrint.hpp',
'src/graphics/Bitmap.hpp',
'src/graphics/Image.hpp',
'src/graphics/Monochrome.hpp'
]
ptprnt_srcs = [
'src/PtouchPrint.cpp',
'src/P700Printer.cpp',
'src/graphics/Image.cpp',
'src/graphics/Bitmap.cpp',
'src/graphics/Monochrome.cpp',
'src/libusbwrap/UsbDeviceFactory.cpp',
'src/libusbwrap/UsbDevice.cpp',
]
ptprnt_lib = library('ptprnt',
include_directories: incdir,
install: true,
dependencies: [usb_dep, log_dep, pangocairo_dep, cli11_dep],
sources: [ptprnt_hpps, ptprnt_srcs])
ptprnt_dep = declare_dependency(include_directories: incdir,
link_with: ptprnt_lib)
ptprnt_exe = executable( ptprnt_exe = executable(
'ptprnt', 'ptprnt',
'src/main.cpp', 'src/main.cpp',
install: true, install: true,
dependencies : [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep], dependencies : [usb_dep, log_dep, cli11_dep, ptprnt_dep],
include_directories: incdir,
sources: [ptprnt_srcs],
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'], cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],
) )

View File

@@ -41,11 +41,6 @@
namespace ptprnt::printer { namespace ptprnt::printer {
const PrinterInfo P700Printer::mInfo = {.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.usbId{0x04f9, 0x2061}};
P700Printer::~P700Printer() { P700Printer::~P700Printer() {
detachUsbDevice(); detachUsbDevice();
if (mUsbHndl) { if (mUsbHndl) {
@@ -85,8 +80,12 @@ const PrinterStatus P700Printer::getPrinterStatus() {
return PrinterStatus{.tapeWidthMm = recvBuf[10]}; return PrinterStatus{.tapeWidthMm = recvBuf[10]};
} }
const libusbwrap::usbId P700Printer::getUsbId() { const uint16_t P700Printer::getVid() {
return mInfo.usbId; return mInfo.vid;
}
const uint16_t P700Printer::getPid() {
return mInfo.pid;
} }
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) { bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
@@ -160,27 +159,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
return true; return true;
} }
bool P700Printer::setText(const std::string& text) { bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
return true;
};
bool P700Printer::setFont(const std::string& text) {
return true;
};
bool P700Printer::setFontSize(uint8_t fontSize) {
return true;
};
bool P700Printer::setHAlign(HAlignPosition hpos) {
return true;
};
bool P700Printer::setVAlign(VAlignPosition vpos) {
return true;
}
bool P700Printer::print() {
send(commands["lf"]); send(commands["lf"]);
send(commands["ff"]); send(commands["ff"]);
send(commands["eject"]); send(commands["eject"]);

View File

@@ -25,7 +25,6 @@
#include "interface/IPrinterDriver.hpp" #include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterTypes.hpp" #include "interface/IPrinterTypes.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once #pragma once
@@ -45,31 +44,30 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
P700Printer(P700Printer&&) = default; P700Printer(P700Printer&&) = default;
P700Printer& operator=(P700Printer&&) = default; P700Printer& operator=(P700Printer&&) = default;
// Printer info has to be static to be accessed without instantiation
static const PrinterInfo mInfo;
// IPrinterDriver // IPrinterDriver
[[nodiscard]] const std::string_view getDriverName() override; const std::string_view getDriverName() override;
[[nodiscard]] const std::string_view getName() override; const std::string_view getName() override;
[[nodiscard]] const libusbwrap::usbId getUsbId() override; const uint16_t getPid() override;
[[nodiscard]] const std::string_view getVersion() override; const uint16_t getVid() override;
[[nodiscard]] const PrinterInfo getPrinterInfo() override; const std::string_view getVersion() override;
[[nodiscard]] const PrinterStatus getPrinterStatus() override; const PrinterInfo getPrinterInfo() override;
const PrinterStatus getPrinterStatus() override;
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override; bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
bool detachUsbDevice() override; bool detachUsbDevice() override;
bool setText(const std::string& text) override;
bool setFont(const std::string& text) override;
bool setFontSize(uint8_t fontSize) override;
bool setHAlign(HAlignPosition hpos) override;
bool setVAlign(VAlignPosition vpos) override;
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override; bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
bool print() override; bool printText(const std::string& text, uint16_t fontSize) override;
private: private:
bool send(std::vector<uint8_t>& data); bool send(std::vector<uint8_t>& data);
bool init(); bool init();
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr}; std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
static constexpr PrinterInfo mInfo{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
std::map<std::string, std::vector<uint8_t>> commands{ std::map<std::string, std::vector<uint8_t>> commands{
{"rasterstart", {"rasterstart",
{0x1b, 0x69, 0x61, {0x1b, 0x69, 0x61,

View File

@@ -1,23 +0,0 @@
#include "PrinterDriverFactory.hpp"
#include <spdlog/spdlog.h>
#include <memory>
#include "P700Printer.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt {
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::create(libusbwrap::usbId id) {
if (printer::P700Printer::mInfo.usbId == id) {
spdlog::info("Found printer P700!");
return std::make_shared<printer::P700Printer>();
} else {
spdlog::trace("{:04x}:{:04x} is not a compatible printer", id.first, id.second);
}
return nullptr;
}
} // namespace ptprnt

View File

@@ -1,22 +0,0 @@
#include <memory>
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt {
class PrinterDriverFactory {
public:
PrinterDriverFactory() = default;
~PrinterDriverFactory() = default;
PrinterDriverFactory(const PrinterDriverFactory&) = delete;
PrinterDriverFactory& operator=(const PrinterDriverFactory&) = delete;
PrinterDriverFactory(PrinterDriverFactory&&) = delete;
PrinterDriverFactory& operator=(PrinterDriverFactory&&) = delete;
std::shared_ptr<IPrinterDriver> create(libusbwrap::usbId id);
private:
};
}

View File

@@ -16,29 +16,19 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <cstdint>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE #include "PtouchPrint.hpp"
#define SPDLOG_DEBUG_ON
#define SPDLOG_TRACE_ON
#include <CLI/App.hpp> #include <CLI/App.hpp>
#include <fmt/core.h>
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <functional> #include <functional>
#include <memory>
#include <vector>
#include "CLI/Option.hpp" #include "CLI/Option.hpp"
#include "PrinterDriverFactory.hpp" #include "P700Printer.hpp"
#include "PtouchPrint.hpp"
#include "graphics/Bitmap.hpp" #include "graphics/Bitmap.hpp"
#include "graphics/Image.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
namespace ptprnt { namespace ptprnt {
@@ -56,35 +46,32 @@ int PtouchPrint::init(int argc, char** argv) {
} }
if (mVerboseFlag) { if (mVerboseFlag) {
setupLogger(spdlog::level::debug); setupLogger(spdlog::level::trace);
} else { } else {
setupLogger(spdlog::level::err); setupLogger(spdlog::level::critical);
} }
if (!mUsbDeviceFactory.init()) { if (!mUsbDeviceFactory.init()) {
spdlog::error("Could not initialize libusb"); spdlog::error("Could not initialize libusb");
return -1; return -1;
} }
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
return 0; return 0;
} }
int PtouchPrint::run() { int PtouchPrint::run() {
spdlog::info("ptprnt version {}", mVersionString); auto numFoundPrinters = getCompatiblePrinters();
SPDLOG_TRACE("testing trace");
mDetectedPrinters = getCompatiblePrinters();
auto numFoundPrinters = mDetectedPrinters.size();
if (numFoundPrinters == 0) { if (numFoundPrinters == 0) {
spdlog::error( spdlog::error(
"No compatible printers found, please make sure that they are turned on and connected"); "No compatible printers found, please make sure if they are turned on and connected");
return -1; return -1;
} else if (numFoundPrinters > 1) { } else if (numFoundPrinters > 1) {
spdlog::warn("Found more than one compatible printer. Currently not supported."); spdlog::warn("Found more than one compatible printer. Currently not supported.");
return -1; return -1;
} }
auto printer = mDetectedPrinters[0]; auto printer = mCompatiblePrinters[0];
const auto printerUsbId = printer->getUsbId(); auto devices = mUsbDeviceFactory.findDevices(printer->getVid(), printer->getPid());
auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second);
if (devices.size() != 1) { if (devices.size() != 1) {
spdlog::warn( spdlog::warn(
"Found more than one device of the same printer on bus. Currently not supported"); "Found more than one device of the same printer on bus. Currently not supported");
@@ -98,73 +85,45 @@ int PtouchPrint::run() {
//printer->printText("wurst", 1); //printer->printText("wurst", 1);
for (auto& cmd : mCommands) { for (auto& cmd : mCommands) {
switch (cmd.first) { if (cmd.first == CliCmdType::Text) {
case CliCmdType::Text: auto label{graphics::Image()};
spdlog::debug("Setting text to {}", cmd.second); label.createLabel(cmd.second);
printer->setText(cmd.second);
break;
case CliCmdType::Font:;
spdlog::debug("Setting font to {}", cmd.second);
printer->setFont(cmd.second);
break;
case CliCmdType::FontSize:;
spdlog::debug("Setting font size to {}", cmd.second);
printer->setFontSize(static_cast<uint8_t>(std::atoi(cmd.second.c_str())));
break;
case CliCmdType::HAlign:;
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}",
cmd.second);
break;
case CliCmdType::VAlign:;
spdlog::debug("[Not implemented] Setting text vertical alignment to {}",
cmd.second);
break;
case CliCmdType::None:;
[[fallthrough]];
default:
spdlog::warn("This command is currently not supported.");
break;
} }
} }
if (!printer->print()) {
spdlog::error("An error occured while printing");
return -1;
}
return 0; return 0;
} }
std::vector<std::shared_ptr<IPrinterDriver>> PtouchPrint::getCompatiblePrinters() { unsigned int PtouchPrint::getCompatiblePrinters() {
auto usbDevs = mUsbDeviceFactory.findAllDevices(); auto usbDevs = mUsbDeviceFactory.findAllDevices();
auto driverFactory = std::make_unique<PrinterDriverFactory>();
std::vector<std::shared_ptr<IPrinterDriver>> foundPrinterDrivers{};
for (auto usbDev : usbDevs) { for (auto usbDev : usbDevs) {
auto driver = driverFactory->create(usbDev->getUsbId()); auto foundPrinterIt =
if (driver != nullptr) { std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(),
foundPrinterDrivers.push_back(driver); [usbDev](const std::shared_ptr<ptprnt::IPrinterDriver>& printer) {
return printer->getPid() == usbDev->getPid() &&
printer->getVid() == usbDev->getVid();
});
if (foundPrinterIt == mCompatiblePrinters.end()) {
continue;
} }
auto foundPrinter = *foundPrinterIt;
spdlog::info("Found Printer {}", foundPrinter->getName());
mDetectedPrinters.push_back(foundPrinter);
} }
return foundPrinterDrivers; // we can delete all instantiated printers that are not compatible
mCompatiblePrinters.clear();
return mDetectedPrinters.size();
} }
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) { void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); spdlog::set_level(lvl);
consoleSink->set_level(lvl); spdlog::debug("Starting ptprnt {}", mVersionString);
consoleSink->set_pattern("%^%L:%$ %v");
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("ptprnt.log", true);
fileSink->set_level(spdlog::level::trace);
fileSink->set_pattern("%Y-%m-%d %H:%m:%S:%e [pid:%P tid:%t] [%^%l%$] %v (%@)");
std::vector<spdlog::sink_ptr> sinks{consoleSink, fileSink};
auto logger = std::make_shared<spdlog::logger>("default_logger", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
spdlog::set_default_logger(logger);
} }
void PtouchPrint::setupCliParser() { void PtouchPrint::setupCliParser() {
auto printVersion = [this](std::size_t) { auto printVersion = [this](std::size_t) {
fmt::print("ptprnt version: {}\n", mVersionString); std::cout << "ptprnt version: " << mVersionString << std::endl;
}; };
// General options // General options

View File

@@ -49,12 +49,13 @@ class PtouchPrint {
// methods // methods
void setupLogger(spdlog::level::level_enum lvl); void setupLogger(spdlog::level::level_enum lvl);
void setupCliParser(); void setupCliParser();
std::vector<std::shared_ptr<IPrinterDriver>> getCompatiblePrinters(); unsigned int getCompatiblePrinters();
// member variables // member variables
CLI::App mApp{ptprnt::APP_DESC}; CLI::App mApp{ptprnt::APP_DESC};
libusbwrap::UsbDeviceFactory mUsbDeviceFactory{}; libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
std::vector<std::shared_ptr<IPrinterDriver>> mDetectedPrinters{}; std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters{};
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters{};
std::vector<CliCmd> mCommands{}; std::vector<CliCmd> mCommands{};
std::string mVersionString = ""; std::string mVersionString = "";

View File

@@ -52,11 +52,6 @@ bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
return true; return true;
} }
template <class T>
std::vector<T> Bitmap<T>::getPixelsCpy() {
return mPixels;
}
template <class T> template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) { std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
if (line >= mHeight) { if (line >= mHeight) {

View File

@@ -22,7 +22,6 @@
#include <stdint.h> #include <stdint.h>
#include <cstdint> #include <cstdint>
#include <memory>
#include <optional> #include <optional>
#include <vector> #include <vector>
@@ -42,7 +41,6 @@ class Bitmap {
uint16_t getWidth(); uint16_t getWidth();
uint16_t getHeight(); uint16_t getHeight();
bool setPixels(const std::vector<T>& pixels); 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>> getLine(uint16_t line);
std::optional<std::vector<T>> getCol(uint16_t col); std::optional<std::vector<T>> getCol(uint16_t col);

View File

@@ -19,13 +19,23 @@
#include "Image.hpp" #include "Image.hpp"
#include <algorithm>
#include <spdlog/spdlog.h>
#include <cstddef>
#include <iostream> // remove me
#include <string> #include <string>
#include "cairo.h"
#include "pango/pango-context.h"
#include "pango/pango-font.h" #include "pango/pango-font.h"
#include "pango/pango-layout.h"
#include "pango/pango-types.h"
#include "pango/pangocairo.h"
namespace ptprnt::graphics { namespace ptprnt::graphics {
Image::Image() { Image::Image() {
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128); /*mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
cairo_t* cr = cairo_create(mSurface); cairo_t* cr = cairo_create(mSurface);
mFontDescription = pango_font_description_new(); mFontDescription = pango_font_description_new();
pango_font_description_set_family(mFontDescription, "sans"); pango_font_description_set_family(mFontDescription, "sans");
@@ -42,7 +52,7 @@ Image::Image() {
cairo_move_to(cr, 0.0, 94.0); cairo_move_to(cr, 0.0, 94.0);
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0)); pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
cairo_surface_write_to_png(mSurface, "hello.png"); cairo_surface_write_to_png(mSurface, "hello.png");*/
} }
uint8_t* Image::getRaw() { uint8_t* Image::getRaw() {
@@ -50,8 +60,46 @@ uint8_t* Image::getRaw() {
return cairo_image_surface_get_data(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
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")};
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()));
// 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{};
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");
}
Image::~Image() { Image::~Image() {
g_object_unref(mLayout); spdlog::info("Image dtor...");
pango_font_description_free(mFontDescription);
} }
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -22,15 +22,30 @@
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <cstdint> #include <cstdint>
#include <string>
namespace ptprnt::graphics { namespace ptprnt::graphics {
constexpr const char* DEFAULT_FONT_FAMILY = "sans";
constexpr const uint8_t DEFAULT_FONT_SIZE = 32;
class Image { class Image {
public: public:
Image(); Image();
~Image(); ~Image();
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
Image(Image&&) = delete;
Image& operator=(Image&&) = delete;
uint8_t* getRaw(); uint8_t* getRaw();
void createLabel(const std::string& labelText);
private: private:
// methods
uint8_t getNumLines(std::string_view str);
// members
PangoLayout* mLayout; PangoLayout* mLayout;
PangoFontDescription* mFontDescription; PangoFontDescription* mFontDescription;
cairo_surface_t* mSurface; cairo_surface_t* mSurface;

View File

@@ -19,7 +19,6 @@
#pragma once #pragma once
#include <cstdint>
#include <memory> #include <memory>
#include <string_view> #include <string_view>
@@ -31,22 +30,17 @@ namespace ptprnt {
class IPrinterDriver { class IPrinterDriver {
public: public:
virtual ~IPrinterDriver() = default; virtual const std::string_view getDriverName() = 0;
[[nodiscard]] virtual const std::string_view getDriverName() = 0; virtual const std::string_view getName() = 0;
[[nodiscard]] virtual const std::string_view getName() = 0; virtual const std::string_view getVersion() = 0;
[[nodiscard]] virtual const std::string_view getVersion() = 0; virtual const uint16_t getVid() = 0;
[[nodiscard]] virtual const libusbwrap::usbId getUsbId() = 0; virtual const uint16_t getPid() = 0;
[[nodiscard]] virtual const PrinterInfo getPrinterInfo() = 0; virtual const PrinterInfo getPrinterInfo() = 0;
[[nodiscard]] virtual const PrinterStatus getPrinterStatus() = 0; virtual const PrinterStatus getPrinterStatus() = 0;
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0; virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
virtual bool detachUsbDevice() = 0; virtual bool detachUsbDevice() = 0;
virtual bool setText(const std::string& text) = 0;
virtual bool setFont(const std::string& text) = 0;
virtual bool setFontSize(uint8_t fontSize) = 0;
virtual bool setHAlign(HAlignPosition hpos) = 0;
virtual bool setVAlign(VAlignPosition vpos) = 0;
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0; virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
virtual bool print() = 0; virtual bool printText(const std::string& text, uint16_t fontSize) = 0;
}; };
} // namespace ptprnt } // namespace ptprnt

View File

@@ -20,18 +20,16 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <string>
#include <string_view> #include <string_view>
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt { namespace ptprnt {
struct PrinterInfo { struct PrinterInfo {
std::string_view driverName = ""; std::string_view driverName = "";
std::string_view name = ""; std::string_view name = "";
std::string_view version = ""; std::string_view version = "";
libusbwrap::usbId usbId{0x00, 0x00}; uint16_t vid = 0x00;
uint16_t pid = 0x00;
}; };
struct PrinterStatus { struct PrinterStatus {
@@ -39,27 +37,4 @@ struct PrinterStatus {
unsigned int tapeWidthMm = 0.0; unsigned int tapeWidthMm = 0.0;
}; };
enum class HAlignPosition {
UNKNOWN = 0,
LEFT = 1,
CENTER = 2,
RIGHT = 3,
JUSTIFY = 4,
};
enum class VAlignPosition {
UNKNOWN = 0,
TOP = 1,
MIDDLE = 2,
BOTTOM = 3,
};
struct PrintableText {
std::string text{""};
std::string font{"Noto"};
uint8_t fontSize{0};
HAlignPosition hAlign{HAlignPosition::LEFT};
VAlignPosition vAlign{VAlignPosition::MIDDLE};
};
} // namespace ptprnt } // namespace ptprnt

View File

@@ -19,7 +19,7 @@
#pragma once #pragma once
#include <utility> #include <memory>
#include "libusb.h" #include "libusb.h"
@@ -40,7 +40,4 @@ enum class Error {
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED, NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
OTHER = LIBUSB_ERROR_OTHER OTHER = LIBUSB_ERROR_OTHER
}; };
}
using usbId = std::pair<uint16_t, uint16_t>;
} // namespace libusbwrap

View File

@@ -104,8 +104,12 @@ bool UsbDevice::bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int*
return true; return true;
} }
const usbId UsbDevice::getUsbId() { const uint16_t UsbDevice::getVid() {
return {mLibusbDevDesc.idVendor, mLibusbDevDesc.idProduct}; return mLibusbDevDesc.idVendor;
}
const uint16_t UsbDevice::getPid() {
return mLibusbDevDesc.idProduct;
} }
const device::Speed UsbDevice::getSpeed() { const device::Speed UsbDevice::getSpeed() {

View File

@@ -29,8 +29,8 @@
namespace libusbwrap { namespace libusbwrap {
class UsbDevice : public IUsbDevice { class UsbDevice : public IUsbDevice {
public: public:
explicit UsbDevice(libusb_context* ctx, libusb_device* dev); UsbDevice(libusb_context* ctx, libusb_device* dev);
~UsbDevice() override; ~UsbDevice();
// delete copy ctor and assignment // delete copy ctor and assignment
UsbDevice(const UsbDevice&) = delete; UsbDevice(const UsbDevice&) = delete;
@@ -47,7 +47,8 @@ class UsbDevice : public IUsbDevice {
unsigned int timeout) override; unsigned int timeout) override;
// getters // getters
const usbId getUsbId() override; const uint16_t getVid() override;
const uint16_t getPid() override;
const device::Speed getSpeed() override; const device::Speed getSpeed() override;
const uint8_t getBusNumber() override; const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override; const uint8_t getPortNumber() override;

View File

@@ -24,7 +24,6 @@ enum class Speed {
class IUsbDevice { class IUsbDevice {
public: public:
virtual ~IUsbDevice() = default;
virtual bool open() = 0; virtual bool open() = 0;
virtual void close() = 0; virtual void close() = 0;
@@ -36,7 +35,8 @@ class IUsbDevice {
unsigned int timeout) = 0; unsigned int timeout) = 0;
// getters // getters
virtual const usbId getUsbId() = 0; virtual const uint16_t getVid() = 0;
virtual const uint16_t getPid() = 0;
virtual const device::Speed getSpeed() = 0; virtual const device::Speed getSpeed() = 0;
virtual const uint8_t getBusNumber() = 0; virtual const uint8_t getBusNumber() = 0;
virtual const uint8_t getPortNumber() = 0; virtual const uint8_t getPortNumber() = 0;

View File

@@ -9,7 +9,6 @@
namespace libusbwrap { namespace libusbwrap {
class IUsbDeviceFactory { class IUsbDeviceFactory {
public: public:
virtual ~IUsbDeviceFactory() = default;
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0; virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0; virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
}; };

View File

@@ -16,6 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include "PtouchPrint.hpp" #include "PtouchPrint.hpp"
int main(int argc, char** argv) { int main(int argc, char** argv) {

View File

@@ -1,26 +0,0 @@
ptprnt_hpps = files (
'libusbwrap/interface/IUsbDeviceFactory.hpp',
'libusbwrap/interface/IUsbDevice.hpp',
'libusbwrap/UsbDeviceFactory.hpp',
'libusbwrap/LibUsbTypes.hpp',
'libusbwrap/UsbDevice.hpp',
'interface/IPrinterDriver.hpp',
'interface/IPrinterTypes.hpp',
'P700Printer.hpp',
'PtouchPrint.hpp',
'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp',
'graphics/Image.hpp',
'graphics/Monochrome.hpp'
)
ptprnt_srcs = files (
'PtouchPrint.cpp',
'PrinterDriverFactory.cpp',
'P700Printer.cpp',
'graphics/Image.cpp',
'graphics/Bitmap.cpp',
'graphics/Monochrome.cpp',
'libusbwrap/UsbDeviceFactory.cpp',
'libusbwrap/UsbDevice.cpp',
)

View File

@@ -21,9 +21,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstdint>
#include <optional> #include <optional>
#include <vector>
TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) { TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8); auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
@@ -66,36 +64,3 @@ TEST(basic_test, Bitmap_getBitmapColInsideOfImage_yieldsValidColSize) {
auto colSize = col->size(); auto colSize = col->size();
ASSERT_EQ(8, colSize); ASSERT_EQ(8, colSize);
} }
TEST(basic_test, Bitmap_setPixels_setsGivenPixels) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
std::vector<uint8_t> pix(16 * 8);
bm.setPixels(pix);
auto pixCpy = bm.getPixelsCpy();
ASSERT_EQ(pix, pixCpy);
}
TEST(basic_test, Bitmap_setPixels_isSuccessfulWithValidPixels) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
std::vector<uint8_t> pix(16 * 8);
ASSERT_TRUE(bm.setPixels(pix));
}
TEST(basic_test, Bitmap_setPixels_yieldsSamePixelsBack) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
std::vector<uint8_t> pix(16 * 8);
bm.setPixels(pix);
auto pixCpy = bm.getPixelsCpy();
ASSERT_EQ(pix, pixCpy);
}
TEST(basic_test, Bitmap_setPixelsWithWrongSize_isFailing) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(5, 8);
std::vector<uint8_t> pix(16 * 8);
ASSERT_FALSE(bm.setPixels(pix));
}

View File

@@ -21,6 +21,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <optional>
TEST(basic_test, Image_smokeTest_succeeds) { TEST(basic_test, Image_smokeTest_succeeds) {
auto im = ptprnt::graphics::Image(); auto im = ptprnt::graphics::Image();
} }

View File

@@ -1,38 +1,15 @@
tests = [ tests = [['bitmap_test', 'bitmap_test_exe', ['bitmap_test/bitmap_test.cpp']],
[ ['image_test', 'image_test_exe', ['image_test/image_test.cpp']],
'bitmap_test', ['monochrome_test', 'monochrome_test_exe', ['monochrome_test/monochrome_test.cpp']]
'bitmap_test_exe', ]
['../src/graphics/Bitmap.cpp', 'bitmap_test/bitmap_test.cpp'],
],
[
'image_test',
'image_test_exe',
['../src/graphics/Image.cpp', 'image_test/image_test.cpp'],
],
[
'monochrome_test',
'monochrome_test_exe',
[
'../src/graphics/Monochrome.cpp',
'monochrome_test/monochrome_test.cpp',
],
],
]
foreach test : tests foreach test : tests
test( test(test.get(0),
test.get(0), executable(test.get(1),
executable(
test.get(1),
sources: test.get(2), sources: test.get(2),
include_directories: incdir, include_directories: incdir,
dependencies: [ link_with:[ptprnt_lib],
gtest_dep, dependencies: [gtest_dep, pangocairo_dep]
usb_dep, )
log_dep,
pangocairo_dep,
cli11_dep,
],
),
) )
endforeach endforeach

View File

@@ -57,9 +57,6 @@ TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespecti
} }
TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) { TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
// TODO: We need to find to access the vector without the possiblity of out-of-bounds access
// Ideas: constexpr? compile time check?
GTEST_SKIP() << "Skipping this test, as ASAN will halt as this is an out-of-bounds access";
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF}); 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});