cli-parser-cleanup (#15)
All checks were successful
Build ptprnt / build (push) Successful in 3m50s

Reviewed-on: moritz/ptouch-prnt#15
This commit was merged in pull request #15.
This commit is contained in:
2025-10-13 19:23:27 +00:00
parent 78aab33fdb
commit 2d37f6fcfb
30 changed files with 760 additions and 367 deletions

View File

@@ -19,27 +19,25 @@
#include "FakePrinter.hpp"
#include <spdlog/spdlog.h>
#include <cairo.h>
#include <spdlog/spdlog.h>
#include <cstdint>
#include <stdexcept>
#include <vector>
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <vector>
#include "../graphics/Monochrome.hpp"
#include "graphics/Monochrome.hpp"
namespace ptprnt::printer {
const PrinterInfo FakePrinter::mInfo = {
.driverName = "FakePrinter",
.name = "Virtual Test Printer",
.version = "v1.0",
.usbId{0x0000, 0x0000}, // No USB ID - virtual printer created explicitly
.pixelLines = 128
};
const PrinterInfo FakePrinter::mInfo = {.driverName = "FakePrinter",
.name = "Virtual Test Printer",
.version = "v1.0",
.usbId{0x0000, 0x0000}, // No USB ID - virtual printer created explicitly
.pixelLines = 128};
const std::string_view FakePrinter::getDriverName() {
return mInfo.driverName;
@@ -80,8 +78,8 @@ bool FakePrinter::detachUsbDevice() {
bool FakePrinter::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
// Convert bitmap to MonochromeData and delegate
auto pixels = bitmap.getPixelsCpy();
auto mono = graphics::Monochrome(pixels, bitmap.getWidth(), bitmap.getHeight());
auto pixels = bitmap.getPixelsCpy();
auto mono = graphics::Monochrome(pixels, bitmap.getWidth(), bitmap.getHeight());
auto monoData = mono.get();
return printMonochromeData(monoData);
@@ -92,10 +90,10 @@ bool FakePrinter::printMonochromeData(const graphics::MonochromeData& data) {
// Simulate the printing process by reconstructing the bitmap
auto printed = simulatePrinting(data);
mLastPrint = std::make_unique<graphics::Bitmap<graphics::ALPHA8>>(std::move(printed));
mLastPrint = std::make_unique<graphics::Bitmap<graphics::ALPHA8>>(std::move(printed));
spdlog::info("FakePrinter: Successfully 'printed' label ({}x{} pixels)",
mLastPrint->getWidth(), mLastPrint->getHeight());
spdlog::info("FakePrinter: Successfully 'printed' label ({}x{} pixels)", mLastPrint->getWidth(),
mLastPrint->getHeight());
// Save to timestamped PNG file
std::string filename = generateTimestampedFilename();
@@ -120,7 +118,8 @@ bool FakePrinter::printLabel(const std::unique_ptr<graphics::ILabel> label) {
// Transform to portrait orientation for printing
monoData.transformTo(graphics::Orientation::PORTRAIT);
spdlog::debug("FakePrinter: Label surface is {}x{}, transformed to portrait", label->getWidth(), label->getHeight());
spdlog::debug("FakePrinter: Label surface is {}x{}, transformed to portrait", label->getWidth(),
label->getHeight());
return printMonochromeData(monoData);
}
@@ -160,7 +159,7 @@ graphics::Bitmap<graphics::ALPHA8> FakePrinter::simulatePrinting(const graphics:
// Now "print" this column by unpacking the bytes back to pixels
for (size_t byteIdx = 0; byteIdx < columnBytes.size(); byteIdx++) {
uint8_t byte = columnBytes[byteIdx];
uint8_t byte = columnBytes[byteIdx];
uint32_t baseRow = byteIdx * 8;
for (int bit = 0; bit < 8 && (baseRow + bit) < data.height; bit++) {
@@ -168,7 +167,7 @@ graphics::Bitmap<graphics::ALPHA8> FakePrinter::simulatePrinting(const graphics:
uint32_t row = baseRow + bit;
// Write to output bitmap
size_t pixelIdx = row * data.width + col;
size_t pixelIdx = row * data.width + col;
pixels[pixelIdx] = pixelOn ? 255 : 0; // 255 = black, 0 = white
}
}
@@ -177,8 +176,8 @@ graphics::Bitmap<graphics::ALPHA8> FakePrinter::simulatePrinting(const graphics:
// Set the pixels in the result bitmap
result.setPixels(pixels);
spdlog::debug("FakePrinter: Simulation complete, reconstructed {}x{} bitmap",
result.getWidth(), result.getHeight());
spdlog::debug("FakePrinter: Simulation complete, reconstructed {}x{} bitmap", result.getWidth(),
result.getHeight());
return result;
}
@@ -201,8 +200,8 @@ bool FakePrinter::saveLastPrintToPng(const std::string& filename) const {
bool FakePrinter::saveBitmapToPng(const graphics::Bitmap<graphics::ALPHA8>& bitmap, const std::string& filename) const {
// Create Cairo surface from bitmap data
auto pixels = bitmap.getPixelsCpy();
uint16_t width = bitmap.getWidth();
auto pixels = bitmap.getPixelsCpy();
uint16_t width = bitmap.getWidth();
uint16_t height = bitmap.getHeight();
// Cairo expects ARGB32 format, but we have ALPHA8
@@ -217,14 +216,9 @@ bool FakePrinter::saveBitmapToPng(const graphics::Bitmap<graphics::ALPHA8>& bitm
}
// Create Cairo surface
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
cairo_surface_t* surface = cairo_image_surface_create_for_data(
reinterpret_cast<unsigned char*>(argbPixels.data()),
CAIRO_FORMAT_ARGB32,
width,
height,
stride
);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
cairo_surface_t* surface = cairo_image_surface_create_for_data(reinterpret_cast<unsigned char*>(argbPixels.data()),
CAIRO_FORMAT_ARGB32, width, height, stride);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
spdlog::error("FakePrinter: Failed to create Cairo surface: {}",
@@ -248,14 +242,12 @@ bool FakePrinter::saveBitmapToPng(const graphics::Bitmap<graphics::ALPHA8>& bitm
std::string FakePrinter::generateTimestampedFilename() const {
// Get current time
auto now = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
// Format: fakelabel_YYYYMMDD_HHMMSS.png
std::stringstream ss;
ss << "fakelabel_"
<< std::put_time(std::localtime(&time), "%Y%m%d_%H%M%S")
<< ".png";
ss << "fakelabel_" << std::put_time(std::localtime(&time), "%Y%m%d_%H%M%S") << ".png";
return ss.str();
}