Skip to content

Commit

Permalink
Add basic label printer support
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Jan 29, 2023
1 parent d3bc294 commit 62bcecc
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 6 deletions.
2 changes: 1 addition & 1 deletion harbour-seaprint.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion qml/components/ChoiceSetting.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions qml/pages/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ function knownPaperSize(mediaName)
}


function fixupChoices(name, choices, mimeType)
function fixupChoices(name, choices, mimeType, printer)
{
switch(name) {
case "document-format":
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/ippmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 72 additions & 2 deletions src/printerworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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});

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/printerworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint-zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions translations/harbour-seaprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 62bcecc

Please sign in to comment.