All checks were successful
Build ptprnt / build (push) Successful in 3m41s
109 lines
3.9 KiB
C++
109 lines
3.9 KiB
C++
/*
|
|
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 <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);
|
|
|
|
/**
|
|
* @brief Save bitmap to PNG file using Cairo
|
|
* @param bitmap The bitmap to save
|
|
* @param filename Output filename
|
|
* @return true if successful
|
|
*/
|
|
bool saveBitmapToPng(const graphics::Bitmap<graphics::ALPHA8>& bitmap, const std::string& filename) const;
|
|
|
|
/**
|
|
* @brief Generate timestamped filename for fake label output
|
|
* @return Filename like "fakelabel_20231011_123456.png"
|
|
*/
|
|
std::string generateTimestampedFilename() const;
|
|
|
|
std::unique_ptr<graphics::Bitmap<graphics::ALPHA8>> mLastPrint;
|
|
bool mHasAttachedDevice = false;
|
|
PrinterStatus mStatus{.tapeWidthMm = 12}; // Default to 12mm tape
|
|
};
|
|
|
|
} // namespace ptprnt::printer
|