Compare commits
21 Commits
label-buil
...
59b3b34edc
| Author | SHA1 | Date | |
|---|---|---|---|
|
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": [
|
||||
|
||||
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) 2024-2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
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,26 +1,6 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2024-2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
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 <string>
|
||||
#include "interface/IPrinterDriver.hpp"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
|
||||
@@ -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
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
#include "CLI/Option.hpp"
|
||||
#include "PrinterDriverFactory.hpp"
|
||||
#include "graphics/LabelBuilder.hpp"
|
||||
#include "graphics/Label.hpp"
|
||||
#include "graphics/interface/ILabel.hpp"
|
||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||
|
||||
@@ -55,10 +55,7 @@ int PtouchPrint::init(int argc, char** argv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set log level based on flags
|
||||
if (mTraceFlag) {
|
||||
setupLogger(spdlog::level::trace);
|
||||
} else if (mVerboseFlag) {
|
||||
if (mVerboseFlag) {
|
||||
setupLogger(spdlog::level::debug);
|
||||
} else {
|
||||
setupLogger(spdlog::level::warn);
|
||||
@@ -73,6 +70,7 @@ int PtouchPrint::init(int argc, char** argv) {
|
||||
|
||||
int PtouchPrint::run() {
|
||||
spdlog::info("ptprnt version {}", mVersionString);
|
||||
SPDLOG_TRACE("testing trace");
|
||||
|
||||
// Handle --list-all-drivers flag
|
||||
if (mListDriversFlag) {
|
||||
@@ -162,21 +160,25 @@ int PtouchPrint::run() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Use LabelBuilder to construct the label
|
||||
graphics::LabelBuilder labelBuilder(printer->getPrinterInfo().pixelLines);
|
||||
|
||||
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:
|
||||
labelBuilder.addText(value);
|
||||
if (labelText.empty()) {
|
||||
labelText = value;
|
||||
} else {
|
||||
labelText = labelText + '\n' + value;
|
||||
}
|
||||
break;
|
||||
case CliCmdType::Font:
|
||||
spdlog::debug("Setting font to {}", value);
|
||||
labelBuilder.setFontFamily(value);
|
||||
label->setFontFamily(value);
|
||||
break;
|
||||
case CliCmdType::FontSize:
|
||||
spdlog::debug("Setting font size to {}", std::stod(value));
|
||||
labelBuilder.setFontSize(std::stod(value));
|
||||
label->setFontSize(std::stod(value));
|
||||
break;
|
||||
case CliCmdType::HAlign:
|
||||
spdlog::debug("Setting text horizontal alignment to {}", value);
|
||||
@@ -184,9 +186,9 @@ int PtouchPrint::run() {
|
||||
auto hPos = HALignPositionMap.find(value);
|
||||
if (hPos == HALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid horizontal alignment specified!");
|
||||
labelBuilder.setHAlign(HAlignPosition::UNKNOWN);
|
||||
label->setHAlign(HAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
labelBuilder.setHAlign(hPos->second);
|
||||
label->setHAlign(hPos->second);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -195,10 +197,10 @@ int PtouchPrint::run() {
|
||||
{
|
||||
auto vPos = VALignPositionMap.find(value);
|
||||
if (vPos == VALignPositionMap.end()) {
|
||||
spdlog::warn("Invalid vertical alignment specified!");
|
||||
labelBuilder.setVAlign(VAlignPosition::UNKNOWN);
|
||||
spdlog::warn("Invalid verical alignment specified!");
|
||||
label->setVAlign(VAlignPosition::UNKNOWN);
|
||||
} else {
|
||||
labelBuilder.setVAlign(vPos->second);
|
||||
label->setVAlign(vPos->second);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -209,8 +211,8 @@ int PtouchPrint::run() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
auto label = labelBuilder.build();
|
||||
label->create(labelText);
|
||||
label->writeToPng("./testlabel.png");
|
||||
if (!printer->printLabel(std::move(label))) {
|
||||
spdlog::error("An error occured while printing");
|
||||
return -1;
|
||||
@@ -262,7 +264,6 @@ void PtouchPrint::setupCliParser() {
|
||||
|
||||
// General options
|
||||
mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output");
|
||||
mApp.add_flag("--trace", mTraceFlag, "Enable trace output (shows USB communication)");
|
||||
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
|
||||
|
||||
// Printer selection
|
||||
|
||||
@@ -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
|
||||
@@ -60,7 +60,6 @@ class PtouchPrint {
|
||||
|
||||
// CLI flags and options
|
||||
bool mVerboseFlag = false;
|
||||
bool mTraceFlag = false;
|
||||
std::string mPrinterSelection = "auto";
|
||||
bool mListDriversFlag = false;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
@@ -90,6 +90,14 @@ int Label::getHeight() {
|
||||
return mPrinterHeight;
|
||||
}
|
||||
|
||||
int Label::getLayoutHeight() {
|
||||
return mLayoutHeight;
|
||||
}
|
||||
|
||||
int Label::getLayoutWidth() {
|
||||
return mLayoutWidth;
|
||||
}
|
||||
|
||||
void Label::configureLayout(PangoLayout* layout, const std::string& text, PangoFontDescription* fontDesc) {
|
||||
pango_layout_set_font_description(layout, fontDesc);
|
||||
pango_layout_set_text(layout, text.c_str(), static_cast<int>(text.length()));
|
||||
@@ -181,7 +189,7 @@ bool Label::create(const std::string& labelText) {
|
||||
break;
|
||||
case VAlignPosition::MIDDLE:
|
||||
cairo_move_to(mCairoCtx.get(), 0.0, (mPrinterHeight - mLayoutHeight) / 2);
|
||||
break;
|
||||
[[fallthrough]];
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -69,6 +69,8 @@ class Label : public ILabel {
|
||||
void writeToPng(const std::string& file);
|
||||
[[nodiscard]] int getWidth() override;
|
||||
[[nodiscard]] int getHeight() override;
|
||||
[[nodiscard]] int getLayoutWidth() override;
|
||||
[[nodiscard]] int getLayoutHeight() override;
|
||||
[[nodiscard]] std::vector<uint8_t> getRaw() override;
|
||||
void setFontSize(const double fontSize) override;
|
||||
void setFontFamily(const std::string& fontFamily) override;
|
||||
|
||||
@@ -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) 2024-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
|
||||
@@ -70,6 +70,8 @@ class ILabel {
|
||||
virtual std::vector<uint8_t> getRaw() = 0;
|
||||
virtual int getWidth() = 0;
|
||||
virtual int getHeight() = 0;
|
||||
virtual int getLayoutWidth() = 0;
|
||||
virtual int getLayoutHeight() = 0;
|
||||
|
||||
virtual void setText(const std::string& text) = 0;
|
||||
virtual void setFontSize(const double fontSize) = 0;
|
||||
|
||||
@@ -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) 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-2024 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-2024 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-2024 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-2024 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,22 +1,3 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2023-2024 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
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>
|
||||
|
||||
@@ -1,22 +1,3 @@
|
||||
/*
|
||||
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 <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-2023 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
|
||||
|
||||
@@ -12,7 +12,6 @@ ptprnt_hpps = files (
|
||||
'PrinterDriverFactory.hpp',
|
||||
'graphics/Bitmap.hpp',
|
||||
'graphics/Label.hpp',
|
||||
'graphics/LabelBuilder.hpp',
|
||||
'graphics/Monochrome.hpp'
|
||||
)
|
||||
|
||||
@@ -22,7 +21,6 @@ ptprnt_srcs = files (
|
||||
'printers/P700Printer.cpp',
|
||||
'printers/FakePrinter.cpp',
|
||||
'graphics/Label.cpp',
|
||||
'graphics/LabelBuilder.cpp',
|
||||
'graphics/Bitmap.cpp',
|
||||
'graphics/Monochrome.cpp',
|
||||
'libusbwrap/UsbDeviceFactory.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
|
||||
@@ -159,6 +159,7 @@ 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];
|
||||
uint32_t baseRow = byteIdx * 8;
|
||||
|
||||
@@ -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
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
@@ -33,6 +32,9 @@
|
||||
#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
|
||||
@@ -23,20 +23,16 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#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"
|
||||
|
||||
#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};
|
||||
|
||||
@@ -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