From 62bcecccbe36196e11f23a9f58cb4a25fe7fd78e Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sun, 29 Jan 2023 13:47:43 +0100 Subject: [PATCH] Add basic label printer support --- harbour-seaprint.pro | 2 +- qml/components/ChoiceSetting.qml | 2 +- qml/pages/utils.js | 15 +++++- src/ippmsg.cpp | 1 + src/printerworker.cpp | 74 +++++++++++++++++++++++++- src/printerworker.h | 1 + translations/harbour-seaprint-de.ts | 4 ++ translations/harbour-seaprint-es.ts | 4 ++ translations/harbour-seaprint-fr.ts | 4 ++ translations/harbour-seaprint-nl.ts | 4 ++ translations/harbour-seaprint-pl.ts | 4 ++ translations/harbour-seaprint-zh_CN.ts | 4 ++ translations/harbour-seaprint.ts | 4 ++ 13 files changed, 117 insertions(+), 6 deletions(-) diff --git a/harbour-seaprint.pro b/harbour-seaprint.pro index 7cc55eb..a2e4c58 100644 --- a/harbour-seaprint.pro +++ b/harbour-seaprint.pro @@ -53,7 +53,7 @@ SOURCES += src/harbour-seaprint.cpp \ DISTFILES += qml/harbour-seaprint.qml \ qml/cover/CoverPage.qml \ - qml/components/*.qml \ + qml/components/*qml \ qml/pages/*.qml \ qml/pages/*.js \ qml/pages/*svg \ diff --git a/qml/components/ChoiceSetting.qml b/qml/components/ChoiceSetting.qml index 1d8e28f..fbe3b5d 100644 --- a/qml/components/ChoiceSetting.qml +++ b/qml/components/ChoiceSetting.qml @@ -7,7 +7,7 @@ Setting { property var preferred_choices: [] property string preferred_choice_suffix: "" - property var actual_choices: Utils.fixupChoices(name, choices, parent.selectedFileType) + property var actual_choices: Utils.fixupChoices(name, choices, parent.selectedFileType, parent.printer) property int num_large_choices: 8 diff --git a/qml/pages/utils.js b/qml/pages/utils.js index fc6d9a6..f72d16d 100644 --- a/qml/pages/utils.js +++ b/qml/pages/utils.js @@ -445,7 +445,7 @@ function knownPaperSize(mediaName) } -function fixupChoices(name, choices, mimeType) +function fixupChoices(name, choices, mimeType, printer) { switch(name) { case "document-format": @@ -497,7 +497,18 @@ function fixupChoices(name, choices, mimeType) return choices; } case "media": - return choices.filter(knownPaperSize); + if(printer.attrs.hasOwnProperty("media-ready")) + { + for(var m in printer.attrs["media-ready"].value) + { + var value = printer.attrs["media-ready"].value[m]; + if(!has(choices, value)) + { + choices.push(value); + } + } + } + return choices; default: return choices; } diff --git a/src/ippmsg.cpp b/src/ippmsg.cpp index 8ff6bfb..db57832 100644 --- a/src/ippmsg.cpp +++ b/src/ippmsg.cpp @@ -288,6 +288,7 @@ QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data) || name.startsWith("printer-firmware") || name.endsWith("-supported") || name == "printer-icons" + || name == "media-ready" || name.endsWith("-reasons"))); if(!unnamed.empty() || forceArray) diff --git a/src/printerworker.cpp b/src/printerworker.cpp index 4930ef2..93b9fc3 100644 --- a/src/printerworker.cpp +++ b/src/printerworker.cpp @@ -150,7 +150,14 @@ void PrinterWorker::print(QString filename, QString mimeType, QString targetForm } else if(mimeType == Mimer::Plaintext) { - convertPlaintext(filename, contents, Params); + if(Params.paperSizeH == 0 && Params.getPaperSizeWInMillimeters() < 26) + { + convertPlaintextLabel(filename, contents, Params); + } + else + { + convertPlaintext(filename, contents, Params); + } } else if(Mimer::isImage(mimeType)) { @@ -649,7 +656,6 @@ void PrinterWorker::convertPlaintext(QString filename, Bytestream header, PrintP doc.setDefaultFont(font); (void)doc.documentLayout(); // wat - // Needs to be before painter pdfWriter.setMargins({mmMargin, mmMargin, mmMargin, mmMargin}); @@ -732,6 +738,70 @@ void PrinterWorker::convertPlaintext(QString filename, Bytestream header, PrintP qDebug() << "posted"; } +void PrinterWorker::convertPlaintextLabel(QString filename, Bytestream header, PrintParameters Params) +{ + QFile inFile(filename); + if(!inFile.open(QIODevice::ReadOnly)) + { + throw ConvertFailedException(tr("Failed to open file")); + } + + QString allText = inFile.readAll(); + while(allText.endsWith('\n')) + { + allText.chop(1); + } + + if(allText.contains('\n') || allText.contains('\f')) + { + throw ConvertFailedException(tr("Multiline label not supported")); + } + + QTemporaryFile tmpPdfFile; + tmpPdfFile.open(); + + // NB: running with rotated format - pdf2printable will sort that out + int pixelHeight = Params.getPaperSizeWInPixels(); + QFont font = QFont("Arial"); + font.setPixelSize(pixelHeight); + + QFontMetrics fm(font); + QString bigText = "AXgjqÈÅÄÖþ"; + QRect rect = fm.boundingRect(bigText); + + while(rect.height() > pixelHeight*(14.0/16)) + { + font.setPixelSize(font.pixelSize()-1); + fm = QFontMetrics(font); + rect = fm.boundingRect(bigText); + } + + rect = fm.boundingRect(allText); + + int pixelWidth = rect.width() + pixelHeight/2; + float ratio = pixelWidth*1.0/pixelHeight; + Params.paperSizeH = Params.paperSizeW * ratio; + + QPdfWriter pdfWriter(tmpPdfFile.fileName()); + pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION); + QPageSize pageSize({Params.getPaperSizeHInPoints(), Params.getPaperSizeWInPoints()}, QPageSize::Point); + pdfWriter.setPageSize(pageSize); + pdfWriter.setMargins({0,0,0,0}); + pdfWriter.setResolution(Params.hwResH); + + QPainter painter(&pdfWriter); + int xOffset = -rect.x() + pixelHeight/4; + int yOffset = -rect.y() + pixelHeight/16; + painter.setFont(font); + painter.drawText(xOffset, yOffset, allText); + painter.end(); + + convertPdf(tmpPdfFile.fileName(), header, Params); + + qDebug() << "Finished"; + qDebug() << "posted"; +} + void PrinterWorker::awaitResult(CurlRequester& cr, QString callback) { Bytestream resMsg; diff --git a/src/printerworker.h b/src/printerworker.h index f8f57d2..e6aaccf 100644 --- a/src/printerworker.h +++ b/src/printerworker.h @@ -60,6 +60,7 @@ private slots: void convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins); void convertOfficeDocument(QString filename, Bytestream header, PrintParameters Params); void convertPlaintext(QString filename, Bytestream header, PrintParameters Params); + void convertPlaintextLabel(QString filename, Bytestream header, PrintParameters Params); void awaitResult(CurlRequester& cr, QString callback); diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index d0207ec..0fb8218 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -524,6 +524,10 @@ auf diesem Drucker Inconsistent duplex setting Uneinheitliche Duplex-Einstellung + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint-es.ts b/translations/harbour-seaprint-es.ts index 168d52c..cad8b28 100644 --- a/translations/harbour-seaprint-es.ts +++ b/translations/harbour-seaprint-es.ts @@ -523,6 +523,10 @@ Inconsistent duplex setting + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint-fr.ts b/translations/harbour-seaprint-fr.ts index 2d8b781..eb16333 100644 --- a/translations/harbour-seaprint-fr.ts +++ b/translations/harbour-seaprint-fr.ts @@ -524,6 +524,10 @@ sur cette imprimante Inconsistent duplex setting + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint-nl.ts b/translations/harbour-seaprint-nl.ts index 6ded455..1f92e08 100644 --- a/translations/harbour-seaprint-nl.ts +++ b/translations/harbour-seaprint-nl.ts @@ -523,6 +523,10 @@ Inconsistent duplex setting + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint-pl.ts b/translations/harbour-seaprint-pl.ts index 672ff29..6d28d24 100644 --- a/translations/harbour-seaprint-pl.ts +++ b/translations/harbour-seaprint-pl.ts @@ -523,6 +523,10 @@ Inconsistent duplex setting + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint-zh_CN.ts b/translations/harbour-seaprint-zh_CN.ts index b01724c..7412e97 100644 --- a/translations/harbour-seaprint-zh_CN.ts +++ b/translations/harbour-seaprint-zh_CN.ts @@ -523,6 +523,10 @@ Inconsistent duplex setting + + Multiline label not supported + + RangeSetting diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index 4cdaabd..6793b23 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -523,6 +523,10 @@ Inconsistent duplex setting + + Multiline label not supported + + RangeSetting