Add basic label printer support

This commit is contained in:
Anton Thomasson 2023-01-29 13:47:43 +01:00
parent d3bc294f0a
commit 62bcecccbe
13 changed files with 117 additions and 6 deletions

View file

@ -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 \

View file

@ -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

View file

@ -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;
}

View file

@ -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)

View file

@ -149,9 +149,16 @@ void PrinterWorker::print(QString filename, QString mimeType, QString targetForm
convertPdf(filename, contents, Params);
}
else if(mimeType == Mimer::Plaintext)
{
if(Params.paperSizeH == 0 && Params.getPaperSizeWInMillimeters() < 26)
{
convertPlaintextLabel(filename, contents, Params);
}
else
{
convertPlaintext(filename, contents, Params);
}
}
else if(Mimer::isImage(mimeType))
{
convertImage(filename, contents, Params, margins);
@ -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;

View file

@ -60,6 +60,7 @@ private:
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);

View file

@ -524,6 +524,10 @@ auf diesem Drucker</translation>
<source>Inconsistent duplex setting</source>
<translation>Uneinheitliche Duplex-Einstellung</translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -523,6 +523,10 @@
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -524,6 +524,10 @@ sur cette imprimante</translation>
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -523,6 +523,10 @@
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -523,6 +523,10 @@
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -523,6 +523,10 @@
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>

View file

@ -523,6 +523,10 @@
<source>Inconsistent duplex setting</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Multiline label not supported</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>RangeSetting</name>