1 Commits

Author SHA1 Message Date
69810e1c5c Refactor cli setup and printer selection logic into separate classes
All checks were successful
Build ptprnt / build (push) Successful in 3m49s
2025-10-13 20:29:14 +02:00
32 changed files with 185 additions and 325 deletions

View File

@@ -88,7 +88,6 @@
},
"clangd.onConfigChanged": "restart",
"cSpell.words": [
"fakelabel",
"fontsize",
"halign",
"libusb",

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2024-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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2024-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
@@ -21,7 +21,7 @@
#include <string>
#include <vector>
#include "printers/interface/IPrinterDriver.hpp"
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt {

View File

@@ -23,23 +23,18 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include "PrinterDriverFactory.hpp"
#include "cli/CliParser.hpp"
#include "cli/interface/ICliParser.hpp"
#include "constants.hpp"
#include "core/PrinterDriverFactory.hpp"
#include "core/PrinterService.hpp"
#include "core/interface/IPrinterService.hpp"
#include "graphics/LabelBuilder.hpp"
namespace ptprnt {
PtouchPrint::PtouchPrint(const char* versionString)
: PtouchPrint(versionString, std::make_unique<cli::CliParser>(ptprnt::APP_DESC, versionString),
std::make_unique<core::PrinterService>()) {}
PtouchPrint::PtouchPrint(const char* versionString, std::unique_ptr<cli::ICliParser> cliParser,
std::unique_ptr<core::IPrinterService> printerService)
: mVersionString(versionString), mCliParser(std::move(cliParser)), mPrinterService(std::move(printerService)) {}
: mVersionString(versionString),
mCliParser(std::make_unique<cli::CliParser>(ptprnt::APP_DESC, versionString)),
mPrinterService(std::make_unique<core::PrinterService>()) {}
PtouchPrint::~PtouchPrint() = default;
@@ -102,7 +97,7 @@ void PtouchPrint::setupLogger() {
bool PtouchPrint::handleListDrivers() {
auto driverFactory = std::make_unique<PrinterDriverFactory>();
auto drivers = driverFactory->listAllDrivers();
auto drivers = driverFactory->listAllDrivers();
fmt::print("Available printer drivers:\n");
for (const auto& driver : drivers) {
@@ -138,7 +133,7 @@ bool PtouchPrint::handlePrinting() {
for (const auto& [cmdType, value] : options.commands) {
switch (cmdType) {
case cli::CommandType::Text:
labelBuilder.addText(value);
labelBuilder.addText(value + "\n");
break;
case cli::CommandType::Font:
spdlog::debug("Setting font to {}", value);

View File

@@ -23,11 +23,11 @@
#include <string>
namespace ptprnt::cli {
class ICliParser;
class CliParser;
}
namespace ptprnt::core {
class IPrinterService;
class PrinterService;
}
namespace ptprnt {
@@ -37,27 +37,14 @@ namespace ptprnt {
*
* Acts as a thin glue layer coordinating CLI parsing and core printer functionality.
* Separates CLI frontend concerns from the core library.
*
* Uses interfaces (ICliParser, IPrinterService) to enable dependency injection
* and facilitate unit testing with mocks.
*/
class PtouchPrint {
public:
/**
* @brief Construct the application with default implementations
* @brief Construct the application
* @param versionString Version string to display
*/
PtouchPrint(const char* versionString);
/**
* @brief Construct with custom implementations (for testing)
* @param versionString Version string to display
* @param cliParser Custom CLI parser implementation
* @param printerService Custom printer service implementation
*/
PtouchPrint(const char* versionString, std::unique_ptr<cli::ICliParser> cliParser,
std::unique_ptr<core::IPrinterService> printerService);
~PtouchPrint(); // Must be defined in .cpp where complete types are visible
// This is basically a singleton application class, no need to copy or move
@@ -86,8 +73,8 @@ class PtouchPrint {
bool handlePrinting();
std::string mVersionString;
std::unique_ptr<cli::ICliParser> mCliParser;
std::unique_ptr<core::IPrinterService> mPrinterService;
std::unique_ptr<cli::CliParser> mCliParser;
std::unique_ptr<core::PrinterService> mPrinterService;
};
} // namespace ptprnt

View File

@@ -23,8 +23,8 @@
namespace ptprnt::cli {
CliParser::CliParser(std::string appDescription, std::string versionString)
: mApp(std::move(appDescription)), mVersionString(std::move(versionString)) {
CliParser::CliParser(const std::string& appDescription, std::string versionString)
: mApp(appDescription), mVersionString(std::move(versionString)) {
setupParser();
}
@@ -67,9 +67,8 @@ void CliParser::setupParser() {
// Text printing options
// Note: CLI11 options are processed in order when using ->each() with callbacks
mApp.add_option(
"-t,--text",
"Text to print (can be used multiple times, use formatting options before to influence text layout)")
mApp.add_option("-t,--text",
"Text to print (can be used multiple times, use formatting options before to influence text layout)")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->each([this](const std::string& text) { mOptions.commands.emplace_back(CommandType::Text, text); });

View File

@@ -22,28 +22,47 @@
#include <CLI/CLI.hpp>
#include <string>
#include "interface/ICliParser.hpp"
#include <vector>
namespace ptprnt::cli {
/**
* @brief Types of CLI commands that can be issued
*/
enum class CommandType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 };
/**
* @brief A command with its type and value
*/
using Command = std::pair<CommandType, std::string>;
/**
* @brief Parsed CLI options and commands
*/
struct CliOptions {
bool verbose{false};
bool trace{false};
bool listDrivers{false};
std::string printerSelection{"auto"};
std::vector<Command> commands{};
};
/**
* @brief CLI argument parser for ptprnt
*
* Concrete implementation of ICliParser using CLI11.
* Handles all command-line argument parsing.
* Handles all command-line argument parsing using CLI11.
* Separates CLI concerns from core library functionality.
*/
class CliParser : public ICliParser {
class CliParser {
public:
/**
* @brief Construct a CLI parser
* @param appDescription Application description for help text
* @param versionString Version string to display
*/
CliParser(std::string appDescription, std::string versionString);
CliParser(const std::string& appDescription, std::string versionString);
~CliParser() override = default;
~CliParser() = default;
CliParser(const CliParser&) = delete;
CliParser& operator=(const CliParser&) = delete;
@@ -56,13 +75,13 @@ class CliParser : public ICliParser {
* @param argv Argument values
* @return 0 on success, positive value if should exit immediately (help/version), negative on error
*/
int parse(int argc, char** argv) override;
int parse(int argc, char** argv);
/**
* @brief Get the parsed options
* @return Reference to parsed options
*/
[[nodiscard]] const CliOptions& getOptions() const override { return mOptions; }
[[nodiscard]] const CliOptions& getOptions() const { return mOptions; }
private:
void setupParser();

View File

@@ -1,73 +0,0 @@
/*
ptrnt - print labels on linux
Copyright (C) 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/>.
*/
#pragma once
#include <string>
#include <vector>
namespace ptprnt::cli {
/**
* @brief Types of CLI commands that can be issued
*/
enum class CommandType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 };
/**
* @brief A command with its type and value
*/
using Command = std::pair<CommandType, std::string>;
/**
* @brief Parsed CLI options and commands
*/
struct CliOptions {
bool verbose{false};
bool trace{false};
bool listDrivers{false};
std::string printerSelection{"auto"};
std::vector<Command> commands{};
};
/**
* @brief Interface for CLI argument parsing
*
* This interface allows for mocking CLI parsing in unit tests
* and provides a clear contract for CLI parser implementations.
*/
class ICliParser {
public:
virtual ~ICliParser() = default;
/**
* @brief Parse command line arguments
* @param argc Argument count
* @param argv Argument values
* @return 0 on success, positive value if should exit immediately (help/version), negative on error
*/
virtual int parse(int argc, char** argv) = 0;
/**
* @brief Get the parsed options
* @return Reference to parsed options
*/
[[nodiscard]] virtual const CliOptions& getOptions() const = 0;
};
} // namespace ptprnt::cli

View File

@@ -21,7 +21,7 @@
#include <spdlog/spdlog.h>
#include "core/PrinterDriverFactory.hpp"
#include "PrinterDriverFactory.hpp"
namespace ptprnt::core {
@@ -38,7 +38,7 @@ bool PrinterService::initialize() {
std::vector<std::shared_ptr<IPrinterDriver>> PrinterService::detectPrinters() {
spdlog::debug("Detecting printers...");
auto usbDevs = mUsbDeviceFactory.findAllDevices();
auto usbDevs = mUsbDeviceFactory.findAllDevices();
auto driverFactory = std::make_unique<PrinterDriverFactory>();
mDetectedPrinters.clear();
@@ -54,20 +54,6 @@ std::vector<std::shared_ptr<IPrinterDriver>> PrinterService::detectPrinters() {
}
std::shared_ptr<IPrinterDriver> PrinterService::selectPrinter(const std::string& printerName) {
// If a specific printer is requested by name (not "auto"), try to create it directly
if (printerName != "auto") {
auto driverFactory = std::make_unique<PrinterDriverFactory>();
auto printer = driverFactory->createByName(printerName);
if (printer) {
mCurrentPrinter = printer;
spdlog::info("Using explicitly selected printer: {}", printerName);
return mCurrentPrinter;
}
spdlog::error("Printer driver '{}' not found", printerName);
return nullptr;
}
// Auto mode: detect USB printers
if (mDetectedPrinters.empty()) {
detectPrinters();
}
@@ -77,10 +63,24 @@ std::shared_ptr<IPrinterDriver> PrinterService::selectPrinter(const std::string&
return nullptr;
}
// Auto-select first detected printer
mCurrentPrinter = mDetectedPrinters.front();
spdlog::info("Auto-selected printer: {}", mCurrentPrinter->getName());
return mCurrentPrinter;
// Auto-select first printer
if (printerName == "auto") {
mCurrentPrinter = mDetectedPrinters.front();
spdlog::info("Auto-selected printer: {}", mCurrentPrinter->getName());
return mCurrentPrinter;
}
// Select printer by name
for (auto& printer : mDetectedPrinters) {
if (printer->getDriverName() == printerName) {
mCurrentPrinter = printer;
spdlog::info("Using explicitly selected printer: {}", printerName);
return mCurrentPrinter;
}
}
spdlog::error("Printer '{}' not found", printerName);
return nullptr;
}
bool PrinterService::printLabel(std::unique_ptr<graphics::ILabel> label) {

View File

@@ -23,25 +23,23 @@
#include <string>
#include <vector>
#include "interface/IPrinterService.hpp"
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
#include "printers/interface/IPrinterDriver.hpp"
namespace ptprnt::core {
/**
* @brief Core service for printer operations
*
* Concrete implementation of IPrinterService.
* Provides the core library functionality for:
* - Detecting printers
* - Selecting printers
* - Building and printing labels
*/
class PrinterService : public IPrinterService {
class PrinterService {
public:
PrinterService();
~PrinterService() override = default;
~PrinterService() = default;
PrinterService(const PrinterService&) = delete;
PrinterService& operator=(const PrinterService&) = delete;
@@ -52,33 +50,33 @@ class PrinterService : public IPrinterService {
* @brief Initialize USB device factory
* @return true on success, false on failure
*/
bool initialize() override;
bool initialize();
/**
* @brief Detect all compatible printers
* @return Vector of detected printers
*/
std::vector<std::shared_ptr<IPrinterDriver>> detectPrinters() override;
std::vector<std::shared_ptr<IPrinterDriver>> detectPrinters();
/**
* @brief Select a printer by name or auto-detect
* @param printerName Printer driver name, or "auto" for first detected
* @return Printer driver, or nullptr if not found
*/
std::shared_ptr<IPrinterDriver> selectPrinter(const std::string& printerName) override;
std::shared_ptr<IPrinterDriver> selectPrinter(const std::string& printerName);
/**
* @brief Get the currently selected printer
* @return Current printer, or nullptr if none selected
*/
[[nodiscard]] std::shared_ptr<IPrinterDriver> getCurrentPrinter() const override { return mCurrentPrinter; }
[[nodiscard]] std::shared_ptr<IPrinterDriver> getCurrentPrinter() const { return mCurrentPrinter; }
/**
* @brief Print a label
* @param label The label to print
* @return true on success, false on failure
*/
bool printLabel(std::unique_ptr<graphics::ILabel> label) override;
bool printLabel(std::unique_ptr<graphics::ILabel> label);
private:
libusbwrap::UsbDeviceFactory mUsbDeviceFactory;

View File

@@ -1,74 +0,0 @@
/*
ptrnt - print labels on linux
Copyright (C) 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/>.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "graphics/interface/ILabel.hpp"
#include "printers/interface/IPrinterDriver.hpp"
namespace ptprnt::core {
/**
* @brief Interface for core printer service operations
*
* This interface allows for mocking printer operations in unit tests
* and provides a clear contract for printer service implementations.
*/
class IPrinterService {
public:
virtual ~IPrinterService() = default;
/**
* @brief Initialize the printer service
* @return true on success, false on failure
*/
virtual bool initialize() = 0;
/**
* @brief Detect all compatible printers
* @return Vector of detected printers
*/
virtual std::vector<std::shared_ptr<IPrinterDriver>> detectPrinters() = 0;
/**
* @brief Select a printer by name or auto-detect
* @param printerName Printer driver name, or "auto" for first detected
* @return Printer driver, or nullptr if not found
*/
virtual std::shared_ptr<IPrinterDriver> selectPrinter(const std::string& printerName) = 0;
/**
* @brief Get the currently selected printer
* @return Current printer, or nullptr if none selected
*/
[[nodiscard]] virtual std::shared_ptr<IPrinterDriver> getCurrentPrinter() const = 0;
/**
* @brief Print a label
* @param label The label to print
* @return true on success, false on failure
*/
virtual bool printLabel(std::unique_ptr<graphics::ILabel> label) = 0;
};
} // namespace ptprnt::core

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2024-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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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
@@ -22,10 +22,10 @@
#include <memory>
#include <string_view>
#include "IPrinterTypes.hpp"
#include "graphics/Bitmap.hpp"
#include "graphics/Monochrome.hpp"
#include "graphics/interface/ILabel.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace ptprnt {

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2023-2024 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
@@ -20,6 +20,7 @@
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023-2024 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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023-2024 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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023-2024 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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023-2024 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
@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
@@ -43,9 +44,8 @@ enum class Speed {
class IUsbDevice {
public:
virtual ~IUsbDevice() = default;
virtual bool open() = 0;
virtual void close() = 0;
virtual bool open() = 0;
virtual void close() = 0;
// libusb wrappers
virtual bool detachKernelDriver(int interfaceNo) = 0;

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 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

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2022-2025 Moritz Martinius
Copyright (C) 2022-2023 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

View File

@@ -1,34 +1,34 @@
ptprnt_hpps = files(
'cli/CliParser.hpp',
'core/PrinterDriverFactory.hpp',
'core/PrinterService.hpp',
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',
'printers/P700Printer.hpp',
'printers/FakePrinter.hpp',
'PtouchPrint.hpp',
'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp',
'graphics/Label.hpp',
'graphics/LabelBuilder.hpp',
'graphics/Monochrome.hpp',
'libusbwrap/LibUsbTypes.hpp',
'libusbwrap/UsbDevice.hpp',
'libusbwrap/UsbDeviceFactory.hpp',
'libusbwrap/interface/IUsbDevice.hpp',
'libusbwrap/interface/IUsbDeviceFactory.hpp',
'printers/FakePrinter.hpp',
'printers/P700Printer.hpp',
'printers/interface/IPrinterDriver.hpp',
'printers/interface/IPrinterTypes.hpp',
'PtouchPrint.hpp',
'cli/CliParser.hpp',
'core/PrinterService.hpp'
)
ptprnt_srcs = files(
'cli/CliParser.cpp',
'core/PrinterDriverFactory.cpp',
'core/PrinterService.cpp',
'graphics/Bitmap.cpp',
ptprnt_srcs = files (
'PtouchPrint.cpp',
'PrinterDriverFactory.cpp',
'printers/P700Printer.cpp',
'printers/FakePrinter.cpp',
'graphics/Label.cpp',
'graphics/LabelBuilder.cpp',
'graphics/Bitmap.cpp',
'graphics/Monochrome.cpp',
'libusbwrap/UsbDevice.cpp',
'libusbwrap/UsbDeviceFactory.cpp',
'printers/FakePrinter.cpp',
'printers/P700Printer.cpp',
'PtouchPrint.cpp',
'libusbwrap/UsbDevice.cpp',
'cli/CliParser.cpp',
'core/PrinterService.cpp'
)

View File

@@ -19,25 +19,27 @@
#include "FakePrinter.hpp"
#include <cairo.h>
#include <spdlog/spdlog.h>
#include <cairo.h>
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <chrono>
#include <iomanip>
#include <sstream>
#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;
@@ -78,8 +80,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);
@@ -90,10 +92,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();
@@ -118,8 +120,7 @@ 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);
}
@@ -159,7 +160,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++) {
@@ -167,7 +168,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
}
}
@@ -176,8 +177,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;
}
@@ -200,8 +201,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
@@ -216,9 +217,14 @@ 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: {}",
@@ -242,12 +248,14 @@ 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();
}

View File

@@ -19,15 +19,15 @@
#pragma once
#include <cstdint>
#include <memory>
#include <vector>
#include <cstdint>
#include "graphics/Bitmap.hpp"
#include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#include "../interface/IPrinterDriver.hpp"
#include "../interface/IPrinterTypes.hpp"
#include "../libusbwrap/LibUsbTypes.hpp"
#include "../libusbwrap/interface/IUsbDevice.hpp"
#include "../graphics/Bitmap.hpp"
namespace ptprnt::printer {
@@ -40,7 +40,7 @@ namespace ptprnt::printer {
*/
class FakePrinter : public ::ptprnt::IPrinterDriver {
public:
FakePrinter() = default;
FakePrinter() = default;
~FakePrinter() override = default;
FakePrinter(const FakePrinter&) = delete;

View File

@@ -28,9 +28,9 @@
#include <thread>
#include <vector>
#include "graphics/Bitmap.hpp"
#include "graphics/Monochrome.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
#include "../graphics/Bitmap.hpp"
#include "../graphics/Monochrome.hpp"
#include "../libusbwrap/LibUsbTypes.hpp"
#include "spdlog/fmt/bin_to_hex.h"
namespace ptprnt::printer {

View File

@@ -17,8 +17,6 @@
*/
#pragma once
#include <spdlog/spdlog.h>
#include <sys/types.h>
@@ -30,13 +28,15 @@
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once
namespace ptprnt::printer {
namespace p700::commands {
const cmd_T INITIALIZE{0x1b, 0x40}; // ESC @ - Initialize
const cmd_T GET_STATUS{0x1b, 0x69, 0x53}; // ESC i S - Status query
const cmd_T PRINT_MODE{0x4d, 0x02}; // M 0x02 - Print mode
const cmd_T AUTO_STATUS{0x1b, 0x69, 0x61, 0x01}; // ESC i a - Auto status
const cmd_T MODE_SETTING{0x1b, 0x69, 0x4d, 0x40}; // ESC i M @ - Advanced mode
const cmd_T INITIALIZE{0x1b, 0x40}; // ESC @ - Initialize
const cmd_T GET_STATUS{0x1b, 0x69, 0x53}; // ESC i S - Status query
const cmd_T PRINT_MODE{0x4d, 0x02}; // M 0x02 - Print mode
const cmd_T AUTO_STATUS{0x1b, 0x69, 0x61, 0x01}; // ESC i a - Auto status
const cmd_T MODE_SETTING{0x1b, 0x69, 0x4d, 0x40}; // ESC i M @ - Advanced mode
const cmd_T RASTER_START{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const cmd_T INFO{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const cmd_T PACKBITSON{0x02};

0
src/ptprnt.log Normal file
View File

View File

@@ -1,10 +1,9 @@
[wrap-file]
directory = CLI11-2.5.0
source_url = https://github.com/CLIUtils/CLI11/archive/refs/tags/v2.5.0.tar.gz
source_filename = CLI11-2.5.0.tar.gz
source_hash = 17e02b4cddc2fa348e5dbdbb582c59a3486fa2b2433e70a0c3bacb871334fd55
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/cli11_2.5.0-2/CLI11-2.5.0.tar.gz
wrapdb_version = 2.5.0-2
directory = CLI11-2.3.2
source_url = https://github.com/CLIUtils/CLI11/archive/refs/tags/v2.3.2.tar.gz
source_filename = CLI11-2.3.2.tar.gz
source_hash = aac0ab42108131ac5d3344a9db0fdf25c4db652296641955720a4fbe52334e22
wrapdb_version = 2.3.2-1
[provide]
dependency_names = CLI11
cli11 = CLI11_dep

View File

@@ -1,13 +1,13 @@
[wrap-file]
directory = googletest-1.17.0
source_url = https://github.com/google/googletest/archive/refs/tags/v1.17.0.tar.gz
source_filename = googletest-1.17.0.tar.gz
source_hash = 65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
patch_filename = gtest_1.17.0-4_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.17.0-4/get_patch
patch_hash = 3abf7662d09db706453a5b064a1e914678c74b9d9b0b19382747ca561d0d8750
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.17.0-4/googletest-1.17.0.tar.gz
wrapdb_version = 1.17.0-4
directory = googletest-1.14.0
source_url = https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
source_filename = gtest-1.14.0.tar.gz
source_hash = 8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7
patch_filename = gtest_1.14.0-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.14.0-1/get_patch
patch_hash = 2e693c7d3f9370a7aa6dac802bada0874d3198ad4cfdf75647b818f691182b50
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.14.0-1/gtest-1.14.0.tar.gz
wrapdb_version = 1.14.0-1
[provide]
gtest = gtest_dep

View File

@@ -35,4 +35,6 @@ foreach test : tests
],
),
)
endforeach
endforeach