Compare commits
22 Commits
dependency
...
6a593f2a40
| Author | SHA1 | Date | |
|---|---|---|---|
|
6a593f2a40
|
|||
|
59b3b34edc
|
|||
|
6e3a5bd12f
|
|||
|
0b8ff28a60
|
|||
|
3dc5da6fc8
|
|||
|
5132eab6fa
|
|||
|
77c6b7bc7b
|
|||
|
f702ec5473
|
|||
|
59ef4189c4
|
|||
|
bb7ab6239d
|
|||
|
37ee7c10f1
|
|||
|
d98399949c
|
|||
|
a47a3189d3
|
|||
|
6857de7b1f
|
|||
|
5a38600e2a
|
|||
|
1163ae5745
|
|||
|
09a2e621d6
|
|||
|
28308dccad
|
|||
|
5f5c0f0f97
|
|||
|
4a59b50839
|
|||
|
79477baecd
|
|||
|
cf8492a714
|
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -6,7 +6,7 @@
|
||||
"configurations": [
|
||||
{
|
||||
"name": "ptprnt_debug",
|
||||
"type": "lldb",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/builddir/ptprnt",
|
||||
"args": [
|
||||
|
||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -88,10 +88,6 @@
|
||||
},
|
||||
"clangd.onConfigChanged": "restart",
|
||||
"cSpell.words": [
|
||||
"fakelabel",
|
||||
"fontsize",
|
||||
"halign",
|
||||
"libusb",
|
||||
"ptrnt"
|
||||
],
|
||||
}
|
||||
11
meson.build
11
meson.build
@@ -35,15 +35,6 @@ incdir = include_directories('src')
|
||||
|
||||
subdir('src')
|
||||
|
||||
# Build arguments
|
||||
cpp_args = ['-DPROJ_VERSION="' + meson.project_version() + '"']
|
||||
|
||||
# USB trace mode option (for debugging without sending to hardware)
|
||||
if get_option('usb_trace_only')
|
||||
cpp_args += ['-DUSB_TRACE_ONLY']
|
||||
message('USB_TRACE_ONLY enabled: USB data will be logged but not sent to device')
|
||||
endif
|
||||
|
||||
ptprnt_exe = executable(
|
||||
'ptprnt',
|
||||
'src/main.cpp',
|
||||
@@ -51,7 +42,7 @@ ptprnt_exe = executable(
|
||||
dependencies: [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep],
|
||||
include_directories: incdir,
|
||||
sources: [ptprnt_srcs],
|
||||
cpp_args: cpp_args,
|
||||
cpp_args: ['-DPROJ_VERSION="' + meson.project_version() + '"'],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
option('usb_trace_only',
|
||||
type: 'boolean',
|
||||
value: false,
|
||||
description: 'Enable USB trace mode: log USB data without sending to device (saves label tape during debugging)')
|
||||
@@ -1,32 +1,12 @@
|
||||
/*
|
||||
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/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "PrinterDriverFactory.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
#include "printers/FakePrinter.hpp"
|
||||
#include "printers/P700Printer.hpp"
|
||||
#include "printers/FakePrinter.hpp"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
|
||||
namespace ptprnt {
|
||||
|
||||
@@ -49,7 +29,8 @@ std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createFakePrinter() {
|
||||
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createByName(const std::string& driverName) {
|
||||
// Convert to lowercase for case-insensitive comparison
|
||||
std::string nameLower = driverName;
|
||||
std::ranges::transform(nameLower, nameLower.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||
std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
|
||||
if (nameLower == "p700" || nameLower == "p700printer") {
|
||||
spdlog::info("Creating P700 printer driver by name");
|
||||
@@ -63,7 +44,10 @@ std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createByName(const std::st
|
||||
}
|
||||
|
||||
std::vector<std::string> PrinterDriverFactory::listAllDrivers() const {
|
||||
return {std::string(printer::P700Printer::mInfo.driverName), std::string(printer::FakePrinter::mInfo.driverName)};
|
||||
return {
|
||||
std::string(printer::P700Printer::mInfo.driverName),
|
||||
std::string(printer::FakePrinter::mInfo.driverName)
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace ptprnt
|
||||
@@ -1,27 +1,7 @@
|
||||
/*
|
||||
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/>.
|
||||
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "printers/interface/IPrinterDriver.hpp"
|
||||
#include <string>
|
||||
#include "interface/IPrinterDriver.hpp"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
|
||||
namespace ptprnt {
|
||||
@@ -65,4 +45,4 @@ class PrinterDriverFactory {
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace ptprnt
|
||||
}
|
||||
@@ -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
|
||||
@@ -18,174 +18,307 @@
|
||||
*/
|
||||
#include "PtouchPrint.hpp"
|
||||
|
||||
#include <CLI/App.hpp>
|
||||
#include <algorithm>
|
||||
#include <fmt/core.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 "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"
|
||||
#include <cctype>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "CLI/Option.hpp"
|
||||
#include "PrinterDriverFactory.hpp"
|
||||
#include "graphics/Label.hpp"
|
||||
#include "graphics/interface/ILabel.hpp"
|
||||
#include "libusbwrap/UsbDeviceFactory.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)) {}
|
||||
|
||||
PtouchPrint::~PtouchPrint() = default;
|
||||
PtouchPrint::PtouchPrint(const char* versionString) : mVersionString{versionString} {}
|
||||
|
||||
int PtouchPrint::init(int argc, char** argv) {
|
||||
// Parse CLI arguments
|
||||
int parseResult = mCliParser->parse(argc, argv);
|
||||
if (parseResult != 0) {
|
||||
// Pass through: positive = clean exit (help/version), negative = error
|
||||
return parseResult;
|
||||
}
|
||||
setupCliParser();
|
||||
|
||||
// Setup logging based on CLI flags
|
||||
setupLogger();
|
||||
|
||||
// Initialize printer service
|
||||
if (!mPrinterService->initialize()) {
|
||||
try {
|
||||
mApp.parse(argc, argv);
|
||||
} catch (const CLI::ParseError& e) {
|
||||
mApp.exit(e);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mVerboseFlag) {
|
||||
setupLogger(spdlog::level::debug);
|
||||
} else {
|
||||
setupLogger(spdlog::level::warn);
|
||||
}
|
||||
|
||||
if (!mUsbDeviceFactory.init()) {
|
||||
spdlog::error("Could not initialize libusb");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PtouchPrint::run() {
|
||||
spdlog::info("ptprnt version {}", mVersionString);
|
||||
SPDLOG_TRACE("testing trace");
|
||||
|
||||
const auto& options = mCliParser->getOptions();
|
||||
// Handle --list-all-drivers flag
|
||||
if (mListDriversFlag) {
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
auto drivers = driverFactory->listAllDrivers();
|
||||
|
||||
// Handle --list-all-drivers
|
||||
if (options.listDrivers) {
|
||||
return handleListDrivers() ? 0 : -1;
|
||||
fmt::print("Available printer drivers:\n");
|
||||
for (const auto& driver : drivers) {
|
||||
fmt::print(" - {}\n", driver);
|
||||
}
|
||||
fmt::print("\nUse with: -p <driver_name> or --printer <driver_name>\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Handle printing
|
||||
return handlePrinting() ? 0 : -1;
|
||||
}
|
||||
// Determine which printer to use
|
||||
std::shared_ptr<IPrinterDriver> printer = nullptr;
|
||||
|
||||
void PtouchPrint::setupLogger() {
|
||||
const auto& options = mCliParser->getOptions();
|
||||
if (mPrinterSelection != "auto") {
|
||||
// Explicit printer selection by name
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
printer = driverFactory->createByName(mPrinterSelection);
|
||||
|
||||
spdlog::level::level_enum level = spdlog::level::warn;
|
||||
if (options.trace) {
|
||||
level = spdlog::level::trace;
|
||||
} else if (options.verbose) {
|
||||
level = spdlog::level::debug;
|
||||
if (!printer) {
|
||||
spdlog::error("Failed to create printer driver '{}'", mPrinterSelection);
|
||||
spdlog::info("Use --list-all-drivers to see available drivers");
|
||||
return -1;
|
||||
}
|
||||
|
||||
spdlog::info("Using explicitly selected printer: {}", mPrinterSelection);
|
||||
|
||||
// FakePrinter doesn't need USB device attachment
|
||||
if (mPrinterSelection == "FakePrinter" || mPrinterSelection == "fake") {
|
||||
printer->attachUsbDevice(nullptr);
|
||||
} else {
|
||||
// Real printer needs USB device
|
||||
const auto printerUsbId = printer->getUsbId();
|
||||
auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second);
|
||||
|
||||
if (devices.empty()) {
|
||||
spdlog::error("No USB device found for printer {}. Is it connected and powered on?", mPrinterSelection);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (devices.size() > 1) {
|
||||
spdlog::warn("Found more than one device of the same printer on bus. Using first one.");
|
||||
}
|
||||
|
||||
if (!printer->attachUsbDevice(std::move(devices[0]))) {
|
||||
spdlog::error("Failed to attach USB device to printer");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Auto-detect printer from USB devices
|
||||
mDetectedPrinters = getCompatiblePrinters();
|
||||
auto numFoundPrinters = mDetectedPrinters.size();
|
||||
|
||||
if (numFoundPrinters == 0) {
|
||||
spdlog::error("No compatible printers found, please make sure that they are turned on and connected");
|
||||
spdlog::info("Tip: Use -p FakePrinter for testing without hardware");
|
||||
return -1;
|
||||
} else if (numFoundPrinters > 1) {
|
||||
spdlog::warn("Found more than one compatible printer. Use -p to select explicitly.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printer = mDetectedPrinters[0];
|
||||
const auto printerUsbId = printer->getUsbId();
|
||||
auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second);
|
||||
|
||||
if (devices.size() != 1) {
|
||||
spdlog::warn("Found more than one device of the same printer on bus. Currently not supported");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!printer->attachUsbDevice(std::move(devices[0]))) {
|
||||
spdlog::error("Failed to attach USB device to printer");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
consoleSink->set_level(level);
|
||||
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);
|
||||
}
|
||||
|
||||
bool PtouchPrint::handleListDrivers() {
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
auto drivers = driverFactory->listAllDrivers();
|
||||
|
||||
fmt::print("Available printer drivers:\n");
|
||||
for (const auto& driver : drivers) {
|
||||
fmt::print(" - {}\n", driver);
|
||||
}
|
||||
fmt::print("\nUse with: -p <driver_name> or --printer <driver_name>\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PtouchPrint::handlePrinting() {
|
||||
const auto& options = mCliParser->getOptions();
|
||||
|
||||
// Select printer
|
||||
auto printer = mPrinterService->selectPrinter(options.printerSelection);
|
||||
if (!printer) {
|
||||
spdlog::error("Failed to select printer");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get printer status
|
||||
auto status = printer->getPrinterStatus();
|
||||
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
||||
|
||||
// Check if there are any commands
|
||||
if (options.commands.empty()) {
|
||||
if (0 == mCommands.size()) {
|
||||
spdlog::warn("No command specified, nothing to do...");
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Build label using LabelBuilder
|
||||
graphics::LabelBuilder labelBuilder(printer->getPrinterInfo().pixelLines);
|
||||
|
||||
for (const auto& [cmdType, value] : options.commands) {
|
||||
switch (cmdType) {
|
||||
case cli::CommandType::Text:
|
||||
labelBuilder.addText(value);
|
||||
auto label = std::make_unique<graphics::Label>(printer->getPrinterInfo().pixelLines);
|
||||
std::string labelText{};
|
||||
// TODO: refactor
|
||||
for (const auto& [cmd, value] : mCommands) {
|
||||
switch (cmd) {
|
||||
case CliCmdType::Text:
|
||||
if (labelText.empty()) {
|
||||
labelText = value;
|
||||
} else {
|
||||
labelText = labelText + '\n' + value;
|
||||
}
|
||||
break;
|
||||
case cli::CommandType::Font:
|
||||
case CliCmdType::Font:
|
||||
spdlog::debug("Setting font to {}", value);
|
||||
labelBuilder.setFontFamily(value);
|
||||
label->setFontFamily(value);
|
||||
break;
|
||||
case cli::CommandType::FontSize:
|
||||
case CliCmdType::FontSize:
|
||||
spdlog::debug("Setting font size to {}", std::stod(value));
|
||||
labelBuilder.setFontSize(std::stod(value));
|
||||
label->setFontSize(std::stod(value));
|
||||
break;
|
||||
case cli::CommandType::HAlign: {
|
||||
case CliCmdType::HAlign:
|
||||
spdlog::debug("Setting text horizontal alignment to {}", value);
|
||||
auto hPos = HALignPositionMap.find(value);
|
||||
if (hPos == HALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid horizontal alignment specified!");
|
||||
labelBuilder.setHAlign(HAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
labelBuilder.setHAlign(hPos->second);
|
||||
{
|
||||
auto hPos = HALignPositionMap.find(value);
|
||||
if (hPos == HALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid horizontal alignment specified!");
|
||||
label->setHAlign(HAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
label->setHAlign(hPos->second);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case cli::CommandType::VAlign: {
|
||||
case CliCmdType::VAlign:
|
||||
spdlog::debug("Setting text vertical alignment to {}", value);
|
||||
auto vPos = VALignPositionMap.find(value);
|
||||
if (vPos == VALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid vertical alignment specified!");
|
||||
labelBuilder.setVAlign(VAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
labelBuilder.setVAlign(vPos->second);
|
||||
{
|
||||
auto vPos = VALignPositionMap.find(value);
|
||||
if (vPos == VALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid verical alignment specified!");
|
||||
label->setVAlign(VAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
label->setVAlign(vPos->second);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case cli::CommandType::None:
|
||||
case CliCmdType::None:
|
||||
[[fallthrough]];
|
||||
default:
|
||||
spdlog::warn("This command is currently not supported.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Build and print the label
|
||||
auto label = labelBuilder.build();
|
||||
if (!mPrinterService->printLabel(std::move(label))) {
|
||||
spdlog::error("An error occurred while printing");
|
||||
return false;
|
||||
label->create(labelText);
|
||||
label->writeToPng("./testlabel.png");
|
||||
if (!printer->printLabel(std::move(label))) {
|
||||
spdlog::error("An error occured while printing");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace ptprnt
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> PtouchPrint::getCompatiblePrinters() {
|
||||
|
||||
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> foundPrinterDrivers{};
|
||||
|
||||
for (auto& usbDev : usbDevs) {
|
||||
auto driver = driverFactory->create(usbDev->getUsbId());
|
||||
if (driver != nullptr) {
|
||||
foundPrinterDrivers.push_back(driver);
|
||||
}
|
||||
}
|
||||
return foundPrinterDrivers;
|
||||
}
|
||||
|
||||
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
||||
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
consoleSink->set_level(lvl);
|
||||
if (spdlog::level::level_enum::debug == lvl || spdlog::level::level_enum::trace == lvl) {
|
||||
// This will enable file and line number for debug and trace macros
|
||||
// TODO: line number and functions only work with macros
|
||||
consoleSink->set_pattern("%^%L:%$ %v");
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
// TODO: CLI parsing should be a seperate class/file
|
||||
void PtouchPrint::setupCliParser() {
|
||||
auto printVersion = [this](std::size_t) {
|
||||
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||
};
|
||||
|
||||
// General options
|
||||
mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output");
|
||||
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
|
||||
|
||||
// Printer selection
|
||||
mApp.add_option("-p,--printer", mPrinterSelection,
|
||||
"Select printer driver (default: auto). Use --list-all-drivers to see available options")
|
||||
->default_val("auto");
|
||||
|
||||
mApp.add_flag("--list-all-drivers", mListDriversFlag, "List all available printer drivers and exit");
|
||||
|
||||
// Text printing options
|
||||
mApp.add_option("-t,--text",
|
||||
"Text to print (can be used multple times, use formatting options before to "
|
||||
"influence text layout)")
|
||||
->group("Printing")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->trigger_on_parse()
|
||||
->each([this](std::string text) { mCommands.emplace_back(CliCmdType::Text, text); });
|
||||
mApp.add_option("-f,--font", "Font used for the following text occurences")
|
||||
->group("Text printing ")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)
|
||||
->trigger_on_parse()
|
||||
->each([this](std::string font) { mCommands.emplace_back(CliCmdType::Font, font); });
|
||||
mApp.add_option("-s,--fontsize", "Font size of the following text occurences")
|
||||
->group("Text printing ")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)
|
||||
->trigger_on_parse()
|
||||
->each([this](std::string size) { mCommands.emplace_back(CliCmdType::FontSize, size); });
|
||||
mApp.add_option("--valign", "Vertical alignment of the following text occurences")
|
||||
->group("Text printing ")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)
|
||||
->trigger_on_parse()
|
||||
->transform([](std::string in) -> std::string {
|
||||
std::unordered_set<std::string> validValignOptions{"top", "middle", "bottom"};
|
||||
std::ranges::transform(in, in.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||
if (validValignOptions.find(in) == validValignOptions.end()) {
|
||||
return {""};
|
||||
}
|
||||
return in;
|
||||
})
|
||||
->each([this](std::string valign) { mCommands.emplace_back(CliCmdType::VAlign, valign); });
|
||||
mApp.add_option("--halign", "Vertical alignment of the following text occurences")
|
||||
->group("Text printing ")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeFirst)
|
||||
->trigger_on_parse()
|
||||
->transform([](std::string in) -> std::string {
|
||||
std::unordered_set<std::string> validValignOptions{"left", "center", "right", "justify"};
|
||||
std::transform(in.begin(), in.end(), in.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||
if (validValignOptions.find(in) == validValignOptions.end()) {
|
||||
return {""};
|
||||
}
|
||||
return in;
|
||||
})
|
||||
->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); });
|
||||
|
||||
// Image options
|
||||
mApp.add_option("-i,--image", "Image to print. Excludes all text printing ")->group("Image printing");
|
||||
}
|
||||
} // namespace ptprnt
|
||||
@@ -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
|
||||
@@ -19,75 +19,48 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <CLI/CLI.hpp>
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace ptprnt::cli {
|
||||
class ICliParser;
|
||||
}
|
||||
|
||||
namespace ptprnt::core {
|
||||
class IPrinterService;
|
||||
}
|
||||
#include "constants.hpp"
|
||||
#include "interface/IPrinterDriver.hpp"
|
||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||
|
||||
namespace ptprnt {
|
||||
enum class CliCmdType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 };
|
||||
using CliCmd = std::pair<CliCmdType, std::string>;
|
||||
|
||||
/**
|
||||
* @brief Main application class for 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
|
||||
* @param versionString Version string to display
|
||||
*/
|
||||
PtouchPrint(const char* versionString);
|
||||
~PtouchPrint() = default;
|
||||
|
||||
/**
|
||||
* @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
|
||||
// This is basically a singelton application class, no need to copy or move
|
||||
PtouchPrint(const PtouchPrint&) = delete;
|
||||
PtouchPrint& operator=(const PtouchPrint&) = delete;
|
||||
PtouchPrint(PtouchPrint&&) = delete;
|
||||
PtouchPrint& operator=(PtouchPrint&&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Initialize the application
|
||||
* @param argc Argument count
|
||||
* @param argv Argument values
|
||||
* @return 0 on success, non-zero on error
|
||||
*/
|
||||
int init(int argc, char** argv);
|
||||
|
||||
/**
|
||||
* @brief Run the application
|
||||
* @return 0 on success, non-zero on error
|
||||
*/
|
||||
int run();
|
||||
|
||||
private:
|
||||
void setupLogger();
|
||||
bool handleListDrivers();
|
||||
bool handlePrinting();
|
||||
// methods
|
||||
void setupLogger(spdlog::level::level_enum lvl);
|
||||
void setupCliParser();
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> getCompatiblePrinters();
|
||||
|
||||
std::string mVersionString;
|
||||
std::unique_ptr<cli::ICliParser> mCliParser;
|
||||
std::unique_ptr<core::IPrinterService> mPrinterService;
|
||||
// member variables
|
||||
CLI::App mApp{ptprnt::APP_DESC};
|
||||
libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> mDetectedPrinters{};
|
||||
std::vector<CliCmd> mCommands{};
|
||||
std::string mVersionString = "";
|
||||
|
||||
// CLI flags and options
|
||||
bool mVerboseFlag = false;
|
||||
std::string mPrinterSelection = "auto";
|
||||
bool mListDriversFlag = false;
|
||||
};
|
||||
|
||||
} // namespace ptprnt
|
||||
} // namespace ptprnt
|
||||
@@ -1,94 +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/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CliParser.hpp"
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
CliParser::CliParser(std::string appDescription, std::string versionString)
|
||||
: mApp(std::move(appDescription)), mVersionString(std::move(versionString)) {
|
||||
setupParser();
|
||||
}
|
||||
|
||||
int CliParser::parse(int argc, char** argv) {
|
||||
try {
|
||||
mApp.parse(argc, argv);
|
||||
} catch (const CLI::CallForHelp& e) {
|
||||
// User requested help - display it and signal clean exit
|
||||
mApp.exit(e);
|
||||
return 1; // Signal: exit cleanly
|
||||
} catch (const CLI::CallForVersion&) {
|
||||
// User requested version - already displayed by callback
|
||||
return 1; // Signal: exit cleanly
|
||||
} catch (const CLI::ParseError& e) {
|
||||
// Parse error - display error message
|
||||
mApp.exit(e);
|
||||
return -1; // Signal: error
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CliParser::setupParser() {
|
||||
// Version callback
|
||||
auto printVersion = [this](std::size_t) {
|
||||
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||
throw CLI::CallForVersion();
|
||||
};
|
||||
|
||||
// General options
|
||||
mApp.add_flag("-v,--verbose", mOptions.verbose, "Enable verbose output");
|
||||
mApp.add_flag("--trace", mOptions.trace, "Enable trace output (shows USB communication)");
|
||||
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
|
||||
|
||||
// Printer selection
|
||||
mApp.add_option("-p,--printer", mOptions.printerSelection,
|
||||
"Select printer driver (default: auto). Use --list-all-drivers to see available options")
|
||||
->default_val("auto");
|
||||
|
||||
mApp.add_flag("--list-all-drivers", mOptions.listDrivers, "List all available printer drivers and exit");
|
||||
|
||||
// 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)")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& text) { mOptions.commands.emplace_back(CommandType::Text, text); });
|
||||
|
||||
// Text formatting options
|
||||
mApp.add_option("-f,--font", "Font used for the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& font) { mOptions.commands.emplace_back(CommandType::Font, font); });
|
||||
|
||||
mApp.add_option("-s,--fontsize", "Font size of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& size) { mOptions.commands.emplace_back(CommandType::FontSize, size); });
|
||||
|
||||
mApp.add_option("--valign", "Vertical alignment of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& align) { mOptions.commands.emplace_back(CommandType::VAlign, align); });
|
||||
|
||||
mApp.add_option("--halign", "Horizontal alignment of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& align) { mOptions.commands.emplace_back(CommandType::HAlign, align); });
|
||||
}
|
||||
|
||||
} // namespace ptprnt::cli
|
||||
@@ -1,75 +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 <CLI/CLI.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "interface/ICliParser.hpp"
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
/**
|
||||
* @brief CLI argument parser for ptprnt
|
||||
*
|
||||
* Concrete implementation of ICliParser using CLI11.
|
||||
* Handles all command-line argument parsing.
|
||||
* Separates CLI concerns from core library functionality.
|
||||
*/
|
||||
class CliParser : public ICliParser {
|
||||
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() override = default;
|
||||
|
||||
CliParser(const CliParser&) = delete;
|
||||
CliParser& operator=(const CliParser&) = delete;
|
||||
CliParser(CliParser&&) = delete;
|
||||
CliParser& operator=(CliParser&&) = delete;
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
int parse(int argc, char** argv) override;
|
||||
|
||||
/**
|
||||
* @brief Get the parsed options
|
||||
* @return Reference to parsed options
|
||||
*/
|
||||
[[nodiscard]] const CliOptions& getOptions() const override { return mOptions; }
|
||||
|
||||
private:
|
||||
void setupParser();
|
||||
|
||||
CLI::App mApp;
|
||||
std::string mVersionString;
|
||||
CliOptions mOptions;
|
||||
};
|
||||
|
||||
} // namespace ptprnt::cli
|
||||
@@ -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
|
||||
@@ -1,95 +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/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "PrinterService.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include "core/PrinterDriverFactory.hpp"
|
||||
|
||||
namespace ptprnt::core {
|
||||
|
||||
PrinterService::PrinterService() = default;
|
||||
|
||||
bool PrinterService::initialize() {
|
||||
if (!mUsbDeviceFactory.init()) {
|
||||
spdlog::error("Could not initialize libusb");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> PrinterService::detectPrinters() {
|
||||
spdlog::debug("Detecting printers...");
|
||||
|
||||
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
mDetectedPrinters.clear();
|
||||
|
||||
for (auto& usbDev : usbDevs) {
|
||||
auto driver = driverFactory->create(usbDev->getUsbId());
|
||||
if (driver != nullptr) {
|
||||
mDetectedPrinters.push_back(driver);
|
||||
}
|
||||
}
|
||||
|
||||
spdlog::debug("Found {} compatible printer(s)", mDetectedPrinters.size());
|
||||
return mDetectedPrinters;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (mDetectedPrinters.empty()) {
|
||||
spdlog::error("No compatible printers detected");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Auto-select first detected printer
|
||||
mCurrentPrinter = mDetectedPrinters.front();
|
||||
spdlog::info("Auto-selected printer: {}", mCurrentPrinter->getName());
|
||||
return mCurrentPrinter;
|
||||
}
|
||||
|
||||
bool PrinterService::printLabel(std::unique_ptr<graphics::ILabel> label) {
|
||||
if (!mCurrentPrinter) {
|
||||
spdlog::error("No printer selected");
|
||||
return false;
|
||||
}
|
||||
|
||||
return mCurrentPrinter->printLabel(std::move(label));
|
||||
}
|
||||
|
||||
} // namespace ptprnt::core
|
||||
@@ -1,89 +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 "interface/IPrinterService.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 {
|
||||
public:
|
||||
PrinterService();
|
||||
~PrinterService() override = default;
|
||||
|
||||
PrinterService(const PrinterService&) = delete;
|
||||
PrinterService& operator=(const PrinterService&) = delete;
|
||||
PrinterService(PrinterService&&) = delete;
|
||||
PrinterService& operator=(PrinterService&&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Initialize USB device factory
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool initialize() override;
|
||||
|
||||
/**
|
||||
* @brief Detect all compatible printers
|
||||
* @return Vector of detected printers
|
||||
*/
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> detectPrinters() override;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* @brief Get the currently selected printer
|
||||
* @return Current printer, or nullptr if none selected
|
||||
*/
|
||||
[[nodiscard]] std::shared_ptr<IPrinterDriver> getCurrentPrinter() const override { 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;
|
||||
|
||||
private:
|
||||
libusbwrap::UsbDeviceFactory mUsbDeviceFactory;
|
||||
std::vector<std::shared_ptr<IPrinterDriver>> mDetectedPrinters;
|
||||
std::shared_ptr<IPrinterDriver> mCurrentPrinter;
|
||||
};
|
||||
|
||||
} // namespace ptprnt::core
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
|
||||
@@ -1,95 +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/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "LabelBuilder.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include "Label.hpp"
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
|
||||
LabelBuilder::LabelBuilder(int printerHeight) : mPrinterHeight(printerHeight) {
|
||||
reset();
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::addText(const std::string& text) {
|
||||
if (!text.empty()) {
|
||||
if (!mAccumulatedText.empty()) {
|
||||
// Add a newline if the label already has some text accumulated
|
||||
mAccumulatedText += '\n';
|
||||
}
|
||||
mAccumulatedText += text;
|
||||
spdlog::debug("LabelBuilder: Added text '{}', total length: {}", text, mAccumulatedText.length());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::setFontFamily(const std::string& fontFamily) {
|
||||
mCurrentFontFamily = fontFamily;
|
||||
spdlog::debug("LabelBuilder: Set font family to '{}'", fontFamily);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::setFontSize(double fontSize) {
|
||||
mCurrentFontSize = fontSize;
|
||||
spdlog::debug("LabelBuilder: Set font size to {}", fontSize);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::setHAlign(HAlignPosition hAlign) {
|
||||
mCurrentHAlign = hAlign;
|
||||
spdlog::debug("LabelBuilder: Set horizontal alignment to {}", static_cast<int>(hAlign));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::setVAlign(VAlignPosition vAlign) {
|
||||
mCurrentVAlign = vAlign;
|
||||
spdlog::debug("LabelBuilder: Set vertical alignment to {}", static_cast<int>(vAlign));
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::unique_ptr<ILabel> LabelBuilder::build() {
|
||||
spdlog::debug("LabelBuilder: Building label with text: '{}'", mAccumulatedText);
|
||||
|
||||
auto label = std::make_unique<Label>(mPrinterHeight);
|
||||
|
||||
// Apply current formatting settings
|
||||
label->setFontFamily(mCurrentFontFamily);
|
||||
label->setFontSize(mCurrentFontSize);
|
||||
label->setHAlign(mCurrentHAlign);
|
||||
label->setVAlign(mCurrentVAlign);
|
||||
|
||||
// Create the label with accumulated text
|
||||
label->create(mAccumulatedText);
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
ILabelBuilder& LabelBuilder::reset() {
|
||||
mAccumulatedText.clear();
|
||||
mCurrentFontFamily = DEFAULT_FONT_FAMILY;
|
||||
mCurrentFontSize = DEFAULT_FONT_SIZE;
|
||||
mCurrentHAlign = HAlignPosition::LEFT;
|
||||
mCurrentVAlign = VAlignPosition::MIDDLE;
|
||||
spdlog::debug("LabelBuilder: Reset to default state");
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace ptprnt::graphics
|
||||
@@ -1,64 +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 "interface/ILabel.hpp"
|
||||
#include "interface/ILabelBuilder.hpp"
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
|
||||
/**
|
||||
* @brief Concrete implementation of ILabelBuilder
|
||||
*
|
||||
* Builds labels by accumulating text segments with formatting options,
|
||||
* then creates a Label instance with all the collected content.
|
||||
*/
|
||||
class LabelBuilder : public ILabelBuilder {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a LabelBuilder for a specific printer height
|
||||
* @param printerHeight Height of the printer in pixels (tape width)
|
||||
*/
|
||||
explicit LabelBuilder(int printerHeight);
|
||||
|
||||
~LabelBuilder() override = default;
|
||||
|
||||
ILabelBuilder& addText(const std::string& text) override;
|
||||
ILabelBuilder& setFontFamily(const std::string& fontFamily) override;
|
||||
ILabelBuilder& setFontSize(double fontSize) override;
|
||||
ILabelBuilder& setHAlign(HAlignPosition hAlign) override;
|
||||
ILabelBuilder& setVAlign(VAlignPosition vAlign) override;
|
||||
std::unique_ptr<ILabel> build() override;
|
||||
ILabelBuilder& reset() override;
|
||||
|
||||
private:
|
||||
int mPrinterHeight;
|
||||
std::string mAccumulatedText;
|
||||
std::string mCurrentFontFamily{DEFAULT_FONT_FAMILY};
|
||||
double mCurrentFontSize{DEFAULT_FONT_SIZE};
|
||||
HAlignPosition mCurrentHAlign{HAlignPosition::LEFT};
|
||||
VAlignPosition mCurrentVAlign{VAlignPosition::MIDDLE};
|
||||
};
|
||||
|
||||
} // namespace ptprnt::graphics
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
|
||||
@@ -1,88 +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 "ILabel.hpp"
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
|
||||
/**
|
||||
* @brief Builder interface for creating labels with text and formatting options
|
||||
*
|
||||
* The LabelBuilder provides a fluent API for constructing labels with various
|
||||
* text elements, fonts, sizes, and alignment options. It separates the construction
|
||||
* logic from the label rendering logic, making it easier to test and maintain.
|
||||
*/
|
||||
class ILabelBuilder {
|
||||
public:
|
||||
virtual ~ILabelBuilder() = default;
|
||||
|
||||
/**
|
||||
* @brief Add text to the label with current formatting settings
|
||||
* @param text The text to add
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& addText(const std::string& text) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set the font family for subsequent text additions
|
||||
* @param fontFamily Font family name (e.g., "sans", "serif", "monospace")
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& setFontFamily(const std::string& fontFamily) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set the font size for subsequent text additions
|
||||
* @param fontSize Font size in points
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& setFontSize(double fontSize) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set horizontal alignment for subsequent text additions
|
||||
* @param hAlign Horizontal alignment position
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& setHAlign(HAlignPosition hAlign) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set vertical alignment for subsequent text additions
|
||||
* @param vAlign Vertical alignment position
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& setVAlign(VAlignPosition vAlign) = 0;
|
||||
|
||||
/**
|
||||
* @brief Build and return the label with all added content
|
||||
* @return Unique pointer to the constructed label
|
||||
*/
|
||||
virtual std::unique_ptr<ILabel> build() = 0;
|
||||
|
||||
/**
|
||||
* @brief Reset the builder to initial state
|
||||
* @return Reference to this builder for method chaining
|
||||
*/
|
||||
virtual ILabelBuilder& reset() = 0;
|
||||
};
|
||||
|
||||
} // namespace ptprnt::graphics
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -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 {
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -20,6 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -67,19 +67,21 @@ ssize_t UsbDeviceFactory::refreshDeviceList() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask, uint16_t pidMask,
|
||||
uint16_t vid, uint16_t pid) {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||
uint16_t pidMask,
|
||||
uint16_t vid,
|
||||
uint16_t pid) {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> matchedDevices;
|
||||
// see libusb/examples/listdevs.c
|
||||
for (auto& currDev : mLibusbDeviceList) {
|
||||
struct libusb_device_descriptor currDevDesc{};
|
||||
struct libusb_device_descriptor currDevDesc {};
|
||||
|
||||
int ret = libusb_get_device_descriptor(currDev.get(), &currDevDesc);
|
||||
spdlog::trace("Detected Device {:04x}:{:04x} ", currDevDesc.idVendor, currDevDesc.idProduct);
|
||||
if (ret < 0) {
|
||||
continue;
|
||||
}
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) && ((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
matchedDevices.push_back(std::make_unique<UsbDevice>(mLibusbCtx, std::move(currDev)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,27 +1,9 @@
|
||||
/*
|
||||
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/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -43,9 +25,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;
|
||||
|
||||
@@ -1,22 +1,3 @@
|
||||
/*
|
||||
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/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
@@ -28,8 +9,8 @@
|
||||
namespace libusbwrap {
|
||||
class IUsbDeviceFactory {
|
||||
public:
|
||||
virtual ~IUsbDeviceFactory() = default;
|
||||
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||
virtual ~IUsbDeviceFactory() = default;
|
||||
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||
virtual std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
||||
};
|
||||
} // namespace libusbwrap
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2022-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
|
||||
@@ -22,10 +22,7 @@ int main(int argc, char** argv) {
|
||||
ptprnt::PtouchPrint ptouchprnt(PROJ_VERSION);
|
||||
int ret = ptouchprnt.init(argc, argv);
|
||||
if (ret != 0) {
|
||||
// Non-zero from init means don't continue
|
||||
// Positive values = clean exit (help/version)
|
||||
// Negative values = error
|
||||
return ret > 0 ? 0 : ret;
|
||||
return ret;
|
||||
}
|
||||
return ptouchprnt.run();
|
||||
}
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
ptprnt_hpps = files(
|
||||
'cli/CliParser.hpp',
|
||||
'core/PrinterDriverFactory.hpp',
|
||||
'core/PrinterService.hpp',
|
||||
'graphics/Bitmap.hpp',
|
||||
'graphics/Label.hpp',
|
||||
'graphics/LabelBuilder.hpp',
|
||||
'graphics/Monochrome.hpp',
|
||||
ptprnt_hpps = files (
|
||||
'libusbwrap/interface/IUsbDeviceFactory.hpp',
|
||||
'libusbwrap/interface/IUsbDevice.hpp',
|
||||
'libusbwrap/UsbDeviceFactory.hpp',
|
||||
'libusbwrap/LibUsbTypes.hpp',
|
||||
'libusbwrap/UsbDevice.hpp',
|
||||
'libusbwrap/UsbDeviceFactory.hpp',
|
||||
'libusbwrap/interface/IUsbDevice.hpp',
|
||||
'libusbwrap/interface/IUsbDeviceFactory.hpp',
|
||||
'printers/FakePrinter.hpp',
|
||||
'interface/IPrinterDriver.hpp',
|
||||
'interface/IPrinterTypes.hpp',
|
||||
'printers/P700Printer.hpp',
|
||||
'printers/interface/IPrinterDriver.hpp',
|
||||
'printers/interface/IPrinterTypes.hpp',
|
||||
'printers/FakePrinter.hpp',
|
||||
'PtouchPrint.hpp',
|
||||
'PrinterDriverFactory.hpp',
|
||||
'graphics/Bitmap.hpp',
|
||||
'graphics/Label.hpp',
|
||||
'graphics/Monochrome.hpp'
|
||||
)
|
||||
|
||||
ptprnt_srcs = files(
|
||||
'cli/CliParser.cpp',
|
||||
'core/PrinterDriverFactory.cpp',
|
||||
'core/PrinterService.cpp',
|
||||
'graphics/Bitmap.cpp',
|
||||
'graphics/Label.cpp',
|
||||
'graphics/LabelBuilder.cpp',
|
||||
'graphics/Monochrome.cpp',
|
||||
'libusbwrap/UsbDevice.cpp',
|
||||
'libusbwrap/UsbDeviceFactory.cpp',
|
||||
'printers/FakePrinter.cpp',
|
||||
'printers/P700Printer.cpp',
|
||||
ptprnt_srcs = files (
|
||||
'PtouchPrint.cpp',
|
||||
'PrinterDriverFactory.cpp',
|
||||
'printers/P700Printer.cpp',
|
||||
'printers/FakePrinter.cpp',
|
||||
'graphics/Label.cpp',
|
||||
'graphics/Bitmap.cpp',
|
||||
'graphics/Monochrome.cpp',
|
||||
'libusbwrap/UsbDeviceFactory.cpp',
|
||||
'libusbwrap/UsbDevice.cpp',
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -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);
|
||||
}
|
||||
@@ -158,8 +159,9 @@ graphics::Bitmap<graphics::ALPHA8> FakePrinter::simulatePrinting(const graphics:
|
||||
}
|
||||
|
||||
// Now "print" this column by unpacking the bytes back to pixels
|
||||
// This simulates the printer head physically printing this column
|
||||
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 +169,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 +178,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 +202,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 +218,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 +249,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();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -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;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -21,18 +21,20 @@
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#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"
|
||||
|
||||
// as long as DRYRUN is defined, no data is actually send to the printer, we need to save some tape ;)
|
||||
#define DRYRUN
|
||||
|
||||
namespace ptprnt::printer {
|
||||
|
||||
const PrinterInfo P700Printer::mInfo = {.driverName = "P700",
|
||||
@@ -127,51 +129,45 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
||||
}
|
||||
|
||||
bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
|
||||
// Send initialization sequence
|
||||
// The INITIALIZE command needs to be sent as a 128-byte packet with ESC @ at the end
|
||||
std::vector<uint8_t> initCmd(128, 0x00);
|
||||
initCmd[126] = p700::commands::INITIALIZE[0]; // ESC
|
||||
initCmd[127] = p700::commands::INITIALIZE[1]; // @
|
||||
send(initCmd);
|
||||
#ifdef DRYRUN
|
||||
spdlog::debug("DRYRUN enabled, printing nothing");
|
||||
#endif
|
||||
|
||||
// Status is already queried in getPrinterStatus()
|
||||
send(p700::commands::PRINT_MODE);
|
||||
send(p700::commands::AUTO_STATUS);
|
||||
send(p700::commands::MODE_SETTING);
|
||||
send(p700::commands::RASTER_START);
|
||||
std::vector<uint8_t> rastercmd(4);
|
||||
rastercmd[0] = 0x47;
|
||||
rastercmd[1] = 0x00; // size +1
|
||||
rastercmd[2] = 0x00;
|
||||
rastercmd[3] = 0x00; // size -1
|
||||
|
||||
// Send raster data row by row in reverse order (bottom to top)
|
||||
// The printer feeds tape as it prints, so first row sent appears at the end
|
||||
for (int row = data.height - 1; row >= 0; row--) {
|
||||
std::vector<uint8_t> rowData;
|
||||
// Process data column by column for the printer
|
||||
for (uint32_t col = 0; col < data.width; col++) {
|
||||
std::vector<uint8_t> columnData;
|
||||
|
||||
// Extract row data byte by byte
|
||||
for (uint32_t col = 0; col < data.width; col += 8) {
|
||||
// Extract column data bit by bit
|
||||
for (uint32_t row = 0; row < data.height; row += 8) {
|
||||
uint8_t byte = 0;
|
||||
for (int bit = 0; bit < 8 && (col + bit) < data.width; bit++) {
|
||||
if (data.getBit(col + bit, row)) {
|
||||
for (int bit = 0; bit < 8 && (row + bit) < data.height; bit++) {
|
||||
if (data.getBit(col, row + bit)) {
|
||||
byte |= (1 << (7 - bit));
|
||||
}
|
||||
}
|
||||
rowData.push_back(byte);
|
||||
columnData.push_back(byte);
|
||||
}
|
||||
|
||||
// Build raster line command: G + length_high + 0x00 + length_low + data
|
||||
std::vector<uint8_t> buf;
|
||||
buf.push_back(0x47); // 'G' command
|
||||
buf.push_back((rowData.size() + 1) & 0xFF); // length + 1 (low byte)
|
||||
buf.push_back(0x00); // high byte (always 0 for our data size)
|
||||
buf.push_back((rowData.size() - 1) & 0xFF); // length - 1
|
||||
buf.insert(buf.end(), rowData.begin(), rowData.end());
|
||||
buf.insert(buf.begin(), rastercmd.begin(), rastercmd.end());
|
||||
buf.insert(std::next(buf.begin(), 4), columnData.begin(), columnData.end());
|
||||
buf[1] = columnData.size() + 1;
|
||||
buf[3] = columnData.size() - 1;
|
||||
|
||||
if (!send(buf)) {
|
||||
spdlog::error("Error sending raster line {} to printer", row);
|
||||
return false;
|
||||
spdlog::error("Error sending buffer to printer");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Send print finalization commands
|
||||
send(p700::commands::EJECT);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -187,8 +183,8 @@ bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
|
||||
// Transform to portrait orientation for printing
|
||||
monoData.transformTo(graphics::Orientation::PORTRAIT);
|
||||
|
||||
spdlog::debug("Label surface was {}x{}, after transform to portrait: {}x{}", label->getWidth(), label->getHeight(),
|
||||
monoData.width, monoData.height);
|
||||
spdlog::debug("Label surface is {}x{}, transformed to portrait", label->getWidth(), label->getHeight());
|
||||
monoData.visualize();
|
||||
|
||||
return printMonochromeData(monoData);
|
||||
}
|
||||
@@ -207,26 +203,24 @@ bool P700Printer::send(const std::vector<uint8_t>& data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
spdlog::trace("USB Tx → 0x02 {:03d} bytes: {:Xn}", data.size(), spdlog::to_hex(data));
|
||||
#ifdef USB_TRACE_ONLY
|
||||
// Trace mode: Log the data that would be sent without actually sending it
|
||||
return true;
|
||||
#else
|
||||
int tx = 0;
|
||||
size_t tx = 0;
|
||||
|
||||
#ifndef DRYRUN
|
||||
if (!mUsbHndl->bulkTransfer(0x02, data, &tx, 0)) {
|
||||
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
tx = data.size();
|
||||
spdlog::trace("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
||||
#endif
|
||||
|
||||
assert(tx > 0);
|
||||
if (static_cast<std::uint32_t>(tx) != data.size()) {
|
||||
if (tx != data.size()) {
|
||||
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes", tx, data.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool P700Printer::init() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 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
|
||||
@@ -17,8 +17,6 @@
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -30,13 +28,11 @@
|
||||
#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 GET_STATUS{0x1b, 0x69, 0x53};
|
||||
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
0
src/ptprnt.log
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -35,4 +35,6 @@ foreach test : tests
|
||||
],
|
||||
),
|
||||
)
|
||||
endforeach
|
||||
endforeach
|
||||
|
||||
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# update_copyright.sh
|
||||
# Updates copyright year ranges in a source file based on git history
|
||||
#
|
||||
# Usage: ./update_copyright.sh [--dry-run] <file>
|
||||
#
|
||||
# Examples:
|
||||
# # Update a single file
|
||||
# ./update_copyright.sh src/main.cpp
|
||||
#
|
||||
# # Dry-run on a single file
|
||||
# ./update_copyright.sh --dry-run src/main.cpp
|
||||
#
|
||||
# # Update all C++ files using find -exec
|
||||
# find src \( -name "*.cpp" -o -name "*.hpp" \) -exec ./update_copyright.sh {} \;
|
||||
#
|
||||
# # Dry-run on all C++ files
|
||||
# find src \( -name "*.cpp" -o -name "*.hpp" \) -exec ./update_copyright.sh --dry-run {} \;
|
||||
|
||||
set -e
|
||||
|
||||
# Check for dry-run flag
|
||||
DRY_RUN=false
|
||||
FILE=""
|
||||
|
||||
if [ "$1" = "--dry-run" ]; then
|
||||
DRY_RUN=true
|
||||
FILE="$2"
|
||||
elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
grep "^#" "$0" | sed 's/^# \?//'
|
||||
exit 0
|
||||
else
|
||||
FILE="$1"
|
||||
fi
|
||||
|
||||
# Check if file argument provided
|
||||
if [ -z "$FILE" ]; then
|
||||
echo "Error: No file specified"
|
||||
echo "Usage: $0 [--dry-run] <file>"
|
||||
echo "Run '$0 --help' for more information"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if file exists
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "Error: File not found: $FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get the repository root
|
||||
REPO_ROOT=$(git rev-parse --show-toplevel)
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
# Copyright holder name
|
||||
COPYRIGHT_HOLDER="Moritz Martinius"
|
||||
|
||||
# Function to get first and last commit years for a file
|
||||
get_years_for_file() {
|
||||
local file="$1"
|
||||
|
||||
# Get the year of the first commit that touched this file
|
||||
first_year=$(git log --follow --format=%ad --date=format:%Y --reverse "$file" 2>/dev/null | head -1)
|
||||
|
||||
# Get the year of the last commit that touched this file
|
||||
last_year=$(git log --follow --format=%ad --date=format:%Y -1 "$file" 2>/dev/null)
|
||||
|
||||
# If file is not in git, use current year
|
||||
if [ -z "$first_year" ]; then
|
||||
first_year=$(date +%Y)
|
||||
last_year=$(date +%Y)
|
||||
fi
|
||||
|
||||
echo "$first_year $last_year"
|
||||
}
|
||||
|
||||
# Get years from git history
|
||||
read -r first_year last_year <<< "$(get_years_for_file "$FILE")"
|
||||
|
||||
# Determine the copyright year string
|
||||
if [ "$first_year" = "$last_year" ]; then
|
||||
year_string="$first_year"
|
||||
else
|
||||
year_string="$first_year-$last_year"
|
||||
fi
|
||||
|
||||
# Check if file has a copyright notice
|
||||
if ! grep -q "Copyright (C)" "$FILE"; then
|
||||
echo "No copyright notice found in $FILE, skipping"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
# Just show what would be changed
|
||||
current_year=$(grep "Copyright (C)" "$FILE" | sed -n 's/.*Copyright (C) \([0-9]\{4\}\(-[0-9]\{4\}\)\?\).*/\1/p' | head -1)
|
||||
if [ "$current_year" != "$year_string" ]; then
|
||||
echo "Would update $FILE: $current_year → $year_string"
|
||||
else
|
||||
echo "No change needed for $FILE (already $year_string)"
|
||||
fi
|
||||
else
|
||||
# Update the copyright line
|
||||
# Matches patterns like "Copyright (C) 2023 Moritz Martinius" or "Copyright (C) 2023-2024 Moritz Martinius"
|
||||
# Handle variable whitespace between year and name
|
||||
|
||||
# Get current year from file for comparison
|
||||
current_year=$(grep "Copyright (C)" "$FILE" | sed -n 's/.*Copyright (C) \([0-9]\{4\}\(-[0-9]\{4\}\)\?\).*/\1/p' | head -1)
|
||||
|
||||
if [ "$current_year" = "$year_string" ]; then
|
||||
echo "No changes needed for $FILE (already $year_string)"
|
||||
else
|
||||
sed -i "s/Copyright (C) [0-9]\{4\}\(-[0-9]\{4\}\)\? \+$COPYRIGHT_HOLDER/Copyright (C) $year_string $COPYRIGHT_HOLDER/" "$FILE"
|
||||
echo "✓ Updated $FILE: $current_year → $year_string"
|
||||
fi
|
||||
fi
|
||||
Reference in New Issue
Block a user