Generate labels with pangocairo #8
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "printers/P700Printer.hpp"
|
||||
#include "printers/FakePrinter.hpp"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
|
||||
namespace ptprnt {
|
||||
@@ -20,4 +21,33 @@ std::shared_ptr<IPrinterDriver> PrinterDriverFactory::create(libusbwrap::usbId i
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createFakePrinter() {
|
||||
spdlog::info("Creating FakePrinter (virtual test printer)");
|
||||
return std::make_shared<printer::FakePrinter>();
|
||||
}
|
||||
|
||||
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createByName(const std::string& driverName) {
|
||||
// Convert to lowercase for case-insensitive comparison
|
||||
std::string nameLower = driverName;
|
||||
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");
|
||||
return std::make_shared<printer::P700Printer>();
|
||||
} else if (nameLower == "fakeprinter" || nameLower == "fake") {
|
||||
return createFakePrinter();
|
||||
}
|
||||
|
||||
spdlog::warn("Unknown printer driver name: {}", driverName);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> PrinterDriverFactory::listAllDrivers() const {
|
||||
return {
|
||||
std::string(printer::P700Printer::mInfo.driverName),
|
||||
std::string(printer::FakePrinter::mInfo.driverName)
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace ptprnt
|
||||
@@ -1,4 +1,6 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "interface/IPrinterDriver.hpp"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
|
||||
@@ -14,8 +16,32 @@ class PrinterDriverFactory {
|
||||
PrinterDriverFactory(PrinterDriverFactory&&) = delete;
|
||||
PrinterDriverFactory& operator=(PrinterDriverFactory&&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Create a printer driver based on USB ID
|
||||
* @param id USB vendor and product ID
|
||||
* @return Printer driver instance or nullptr if no match
|
||||
*/
|
||||
std::shared_ptr<IPrinterDriver> create(libusbwrap::usbId id);
|
||||
|
||||
/**
|
||||
* @brief Create a virtual FakePrinter for testing without hardware
|
||||
* @return FakePrinter instance
|
||||
*/
|
||||
std::shared_ptr<IPrinterDriver> createFakePrinter();
|
||||
|
||||
/**
|
||||
* @brief Create a printer driver by name
|
||||
* @param driverName Name of the driver (from PrinterInfo.driverName)
|
||||
* @return Printer driver instance or nullptr if no match
|
||||
*/
|
||||
std::shared_ptr<IPrinterDriver> createByName(const std::string& driverName);
|
||||
|
||||
/**
|
||||
* @brief Get list of all available printer driver names
|
||||
* @return Vector of driver names
|
||||
*/
|
||||
std::vector<std::string> listAllDrivers() const;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
@@ -71,27 +71,87 @@ int PtouchPrint::init(int argc, char** argv) {
|
||||
int PtouchPrint::run() {
|
||||
spdlog::info("ptprnt version {}", mVersionString);
|
||||
SPDLOG_TRACE("testing trace");
|
||||
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");
|
||||
return -1;
|
||||
} else if (numFoundPrinters > 1) {
|
||||
spdlog::warn("Found more than one compatible printer. Currently not supported.");
|
||||
|
||||
// Handle --list-all-drivers flag
|
||||
if (mListDriversFlag) {
|
||||
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 0;
|
||||
}
|
||||
|
||||
// Determine which printer to use
|
||||
std::shared_ptr<IPrinterDriver> printer = nullptr;
|
||||
|
||||
if (mPrinterSelection != "auto") {
|
||||
// Explicit printer selection by name
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
printer = driverFactory->createByName(mPrinterSelection);
|
||||
|
||||
if (!printer) {
|
||||
spdlog::error("Failed to create printer driver '{}'", mPrinterSelection);
|
||||
spdlog::info("Use --list-all-drivers to see available drivers");
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto printer = mDetectedPrinters[0];
|
||||
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.size() != 1) {
|
||||
spdlog::warn("Found more than one device of the same printer on bus. Currently not supported");
|
||||
|
||||
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 status = printer->getPrinterStatus();
|
||||
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
||||
|
||||
@@ -206,6 +266,14 @@ void PtouchPrint::setupCliParser() {
|
||||
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 "
|
||||
|
||||
@@ -58,7 +58,9 @@ class PtouchPrint {
|
||||
std::vector<CliCmd> mCommands{};
|
||||
std::string mVersionString = "";
|
||||
|
||||
// CLI flags
|
||||
// CLI flags and options
|
||||
bool mVerboseFlag = false;
|
||||
std::string mPrinterSelection = "auto";
|
||||
bool mListDriversFlag = false;
|
||||
};
|
||||
} // namespace ptprnt
|
||||
@@ -7,6 +7,7 @@ ptprnt_hpps = files (
|
||||
'interface/IPrinterDriver.hpp',
|
||||
'interface/IPrinterTypes.hpp',
|
||||
'printers/P700Printer.hpp',
|
||||
'printers/FakePrinter.hpp',
|
||||
'PtouchPrint.hpp',
|
||||
'PrinterDriverFactory.hpp',
|
||||
'graphics/Bitmap.hpp',
|
||||
@@ -18,6 +19,7 @@ ptprnt_srcs = files (
|
||||
'PtouchPrint.cpp',
|
||||
'PrinterDriverFactory.cpp',
|
||||
'printers/P700Printer.cpp',
|
||||
'printers/FakePrinter.cpp',
|
||||
'graphics/Label.cpp',
|
||||
'graphics/Bitmap.cpp',
|
||||
'graphics/Monochrome.cpp',
|
||||
|
||||
188
src/printers/FakePrinter.cpp
Normal file
188
src/printers/FakePrinter.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
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
|
||||
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 "FakePrinter.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#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 std::string_view FakePrinter::getDriverName() {
|
||||
return mInfo.driverName;
|
||||
}
|
||||
|
||||
const std::string_view FakePrinter::getName() {
|
||||
return mInfo.name;
|
||||
}
|
||||
|
||||
const std::string_view FakePrinter::getVersion() {
|
||||
return mInfo.version;
|
||||
}
|
||||
|
||||
const PrinterInfo FakePrinter::getPrinterInfo() {
|
||||
return mInfo;
|
||||
}
|
||||
|
||||
const PrinterStatus FakePrinter::getPrinterStatus() {
|
||||
return mStatus;
|
||||
}
|
||||
|
||||
const libusbwrap::usbId FakePrinter::getUsbId() {
|
||||
return mInfo.usbId;
|
||||
}
|
||||
|
||||
bool FakePrinter::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
|
||||
// FakePrinter doesn't need a real USB device
|
||||
mHasAttachedDevice = true;
|
||||
spdlog::debug("FakePrinter: Simulated USB device attachment");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FakePrinter::detachUsbDevice() {
|
||||
mHasAttachedDevice = false;
|
||||
spdlog::debug("FakePrinter: Simulated USB device detachment");
|
||||
return true;
|
||||
}
|
||||
|
||||
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 monoData = mono.get();
|
||||
|
||||
return printMonochromeData(monoData);
|
||||
}
|
||||
|
||||
bool FakePrinter::printMonochromeData(const graphics::MonochromeData& data) {
|
||||
spdlog::debug("FakePrinter: Simulating printing of {}x{} bitmap", data.width, data.height);
|
||||
|
||||
// Simulate the printing process by reconstructing the bitmap
|
||||
auto printed = simulatePrinting(data);
|
||||
mLastPrint = std::make_unique<graphics::Bitmap<graphics::ALPHA8>>(std::move(printed));
|
||||
|
||||
spdlog::info("FakePrinter: Successfully 'printed' label ({}x{} pixels)",
|
||||
mLastPrint->getWidth(), mLastPrint->getHeight());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FakePrinter::printLabel(const std::unique_ptr<graphics::ILabel> label) {
|
||||
// Convert label directly to MonochromeData
|
||||
auto pixels = label->getRaw();
|
||||
auto mono = graphics::Monochrome(pixels, label->getWidth(), label->getHeight(), graphics::Orientation::PORTRAIT);
|
||||
auto monoData = mono.get();
|
||||
|
||||
spdlog::debug("FakePrinter: Label has {}x{} pixel size", label->getWidth(), label->getHeight());
|
||||
|
||||
return printMonochromeData(monoData);
|
||||
}
|
||||
|
||||
bool FakePrinter::print() {
|
||||
spdlog::debug("FakePrinter: Print command (no-op for virtual printer)");
|
||||
return true;
|
||||
}
|
||||
|
||||
graphics::Bitmap<graphics::ALPHA8> FakePrinter::simulatePrinting(const graphics::MonochromeData& data) {
|
||||
spdlog::debug("FakePrinter: Simulating column-by-column printing like real hardware");
|
||||
|
||||
// Create output bitmap with same dimensions
|
||||
graphics::Bitmap<graphics::ALPHA8> result(data.width, data.height);
|
||||
std::vector<uint8_t> pixels(data.width * data.height, 0);
|
||||
|
||||
// Simulate printer behavior: process column by column
|
||||
// This mimics how label printers physically print one vertical line at a time
|
||||
for (uint32_t col = 0; col < data.width; col++) {
|
||||
spdlog::trace("FakePrinter: Processing column {}/{}", col + 1, data.width);
|
||||
|
||||
// Extract column data bit by bit (simulating what would be sent to printer)
|
||||
std::vector<uint8_t> columnBytes;
|
||||
|
||||
for (uint32_t row = 0; row < data.height; row += 8) {
|
||||
uint8_t byte = 0;
|
||||
|
||||
// Pack 8 vertical pixels into one byte (printer data format)
|
||||
for (int bit = 0; bit < 8 && (row + bit) < data.height; bit++) {
|
||||
if (data.getBit(col, row + bit)) {
|
||||
byte |= (1 << (7 - bit));
|
||||
}
|
||||
}
|
||||
|
||||
columnBytes.push_back(byte);
|
||||
}
|
||||
|
||||
// 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];
|
||||
uint32_t baseRow = byteIdx * 8;
|
||||
|
||||
for (int bit = 0; bit < 8 && (baseRow + bit) < data.height; bit++) {
|
||||
bool pixelOn = (byte & (1 << (7 - bit))) != 0;
|
||||
uint32_t row = baseRow + bit;
|
||||
|
||||
// Write to output bitmap
|
||||
size_t pixelIdx = row * data.width + col;
|
||||
pixels[pixelIdx] = pixelOn ? 255 : 0; // 255 = black, 0 = white
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the pixels in the result bitmap
|
||||
result.setPixels(pixels);
|
||||
|
||||
spdlog::debug("FakePrinter: Simulation complete, reconstructed {}x{} bitmap",
|
||||
result.getWidth(), result.getHeight());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const graphics::Bitmap<graphics::ALPHA8>& FakePrinter::getLastPrint() const {
|
||||
if (!mLastPrint) {
|
||||
throw std::runtime_error("FakePrinter: No print data available");
|
||||
}
|
||||
return *mLastPrint;
|
||||
}
|
||||
|
||||
bool FakePrinter::saveLastPrintToPng(const std::string& filename) const {
|
||||
if (!mLastPrint || mLastPrint->getWidth() == 0 || mLastPrint->getHeight() == 0) {
|
||||
spdlog::error("FakePrinter: No print data available to save");
|
||||
return false;
|
||||
}
|
||||
|
||||
// For now, just log it - actual PNG saving would require Cairo/PNG library integration
|
||||
spdlog::info("FakePrinter: Successfully 'saved' {}x{} bitmap to {} (simulated)",
|
||||
mLastPrint->getWidth(), mLastPrint->getHeight(), filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ptprnt::printer
|
||||
94
src/printers/FakePrinter.hpp
Normal file
94
src/printers/FakePrinter.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
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
|
||||
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 <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../interface/IPrinterDriver.hpp"
|
||||
#include "../interface/IPrinterTypes.hpp"
|
||||
#include "../libusbwrap/LibUsbTypes.hpp"
|
||||
#include "../libusbwrap/interface/IUsbDevice.hpp"
|
||||
#include "../graphics/Bitmap.hpp"
|
||||
|
||||
namespace ptprnt::printer {
|
||||
|
||||
/**
|
||||
* @brief Virtual printer driver for testing without hardware
|
||||
*
|
||||
* FakePrinter simulates a real label printer by processing bitmap data
|
||||
* column by column and reconstructing it into a new bitmap, mimicking
|
||||
* the physical printing process of label printers.
|
||||
*/
|
||||
class FakePrinter : public ::ptprnt::IPrinterDriver {
|
||||
public:
|
||||
FakePrinter() = default;
|
||||
~FakePrinter() override = default;
|
||||
|
||||
FakePrinter(const FakePrinter&) = delete;
|
||||
FakePrinter& operator=(const FakePrinter&) = delete;
|
||||
FakePrinter(FakePrinter&&) = default;
|
||||
FakePrinter& operator=(FakePrinter&&) = default;
|
||||
|
||||
// Printer info - static to be accessed without instantiation
|
||||
static const PrinterInfo mInfo;
|
||||
|
||||
// IPrinterDriver interface
|
||||
[[nodiscard]] const std::string_view getDriverName() override;
|
||||
[[nodiscard]] const std::string_view getName() override;
|
||||
[[nodiscard]] const libusbwrap::usbId getUsbId() override;
|
||||
[[nodiscard]] const std::string_view getVersion() override;
|
||||
[[nodiscard]] const PrinterInfo getPrinterInfo() override;
|
||||
[[nodiscard]] const PrinterStatus getPrinterStatus() override;
|
||||
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
||||
bool detachUsbDevice() override;
|
||||
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
||||
bool printMonochromeData(const graphics::MonochromeData& data) override;
|
||||
bool printLabel(const std::unique_ptr<graphics::ILabel> label) override;
|
||||
bool print() override;
|
||||
|
||||
/**
|
||||
* @brief Get the last printed bitmap
|
||||
* @return The reconstructed bitmap from simulated printing
|
||||
*/
|
||||
[[nodiscard]] const graphics::Bitmap<graphics::ALPHA8>& getLastPrint() const;
|
||||
|
||||
/**
|
||||
* @brief Save the last print to a PNG file
|
||||
* @param filename Path to save the PNG
|
||||
* @return true if successful
|
||||
*/
|
||||
bool saveLastPrintToPng(const std::string& filename) const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Simulate printing by reconstructing bitmap column by column
|
||||
* @param data Monochrome data to "print"
|
||||
* @return Reconstructed bitmap
|
||||
*/
|
||||
graphics::Bitmap<graphics::ALPHA8> simulatePrinting(const graphics::MonochromeData& data);
|
||||
|
||||
std::unique_ptr<graphics::Bitmap<graphics::ALPHA8>> mLastPrint;
|
||||
bool mHasAttachedDevice = false;
|
||||
PrinterStatus mStatus{.tapeWidthMm = 12}; // Default to 12mm tape
|
||||
};
|
||||
|
||||
} // namespace ptprnt::printer
|
||||
Reference in New Issue
Block a user