2008-07-19 02:48:31 +12:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008 by BogDan Vatra *
|
|
|
|
* bogdan@licentia.eu *
|
2021-05-26 07:42:26 +12:00
|
|
|
* Copyright (C) 2010-2021 Robin Stuart *
|
2008-07-19 02:48:31 +12:00
|
|
|
* *
|
|
|
|
* 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 <http://www.gnu.org/licenses/>. *
|
|
|
|
***************************************************************************/
|
2020-05-16 21:22:33 +12:00
|
|
|
/* vim: set ts=4 sw=4 et : */
|
2008-07-19 02:48:31 +12:00
|
|
|
|
2021-06-10 22:15:39 +12:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#if _MSC_VER >= 1900 /* MSVC 2015 */
|
|
|
|
#pragma warning(disable: 4996) /* function or variable may be unsafe */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
//#include <QDebug>
|
2008-09-19 02:43:54 +12:00
|
|
|
#include "qzint.h"
|
2009-04-29 03:34:02 +12:00
|
|
|
#include <stdio.h>
|
2018-06-19 14:56:44 +12:00
|
|
|
#include <math.h>
|
2018-06-18 14:55:52 +12:00
|
|
|
#include <QFontMetrics>
|
2020-08-05 01:49:25 +12:00
|
|
|
/* the following include was necessary to compile with QT 5.18 on Windows */
|
|
|
|
/* QT 8.7 did not require it. */
|
|
|
|
#include <QPainterPath>
|
2008-07-19 02:48:31 +12:00
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
namespace Zint {
|
2020-10-01 00:19:12 +13:00
|
|
|
static const char *fontStyle = "Helvetica";
|
|
|
|
static const char *fontStyleError = "Helvetica";
|
|
|
|
static const int fontSizeError = 14; /* Point size */
|
2016-08-14 20:58:38 +12:00
|
|
|
|
|
|
|
QZint::QZint() {
|
|
|
|
m_symbol = BARCODE_CODE128;
|
2021-06-20 00:11:23 +12:00
|
|
|
m_height = 0.0f;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_borderType = 0;
|
2017-06-18 02:17:24 +12:00
|
|
|
m_borderWidth = 0;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_fontSetting = 0;
|
2020-12-20 06:13:35 +13:00
|
|
|
m_option_1 = -1;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_fgColor = Qt::black;
|
|
|
|
m_bgColor = Qt::white;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_cmyk = false;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_zintSymbol = NULL;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_error = 0;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_input_mode = UNICODE_MODE;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_scale = 1.0f;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_option_3 = 0;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_show_hrt = 1;
|
|
|
|
m_eci = 0;
|
|
|
|
m_dotty = false;
|
|
|
|
m_dot_size = 4.0f / 5.0f;
|
2016-12-15 12:33:17 +13:00
|
|
|
target_size_horiz = 0;
|
|
|
|
target_size_vert = 0;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_option_2 = 0;
|
2017-08-06 18:02:07 +12:00
|
|
|
m_whitespace = 0;
|
2021-05-26 07:42:26 +12:00
|
|
|
m_vwhitespace = 0;
|
2021-05-15 23:23:46 +12:00
|
|
|
m_gs1parens = false;
|
2021-07-14 04:39:03 +12:00
|
|
|
m_gs1nocheck = false;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_gssep = false;
|
2020-10-01 00:19:12 +13:00
|
|
|
m_reader_init = false;
|
|
|
|
m_rotate_angle = 0;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_debug = false;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
QZint::~QZint() {
|
|
|
|
if (m_zintSymbol)
|
|
|
|
ZBarcode_Delete(m_zintSymbol);
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
void QZint::resetSymbol() {
|
2016-08-14 20:58:38 +12:00
|
|
|
if (m_zintSymbol)
|
|
|
|
ZBarcode_Delete(m_zintSymbol);
|
|
|
|
|
|
|
|
m_lastError.clear();
|
|
|
|
m_zintSymbol = ZBarcode_Create();
|
2020-10-01 00:19:12 +13:00
|
|
|
m_zintSymbol->output_options |= m_borderType | m_fontSetting;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_zintSymbol->symbology = m_symbol;
|
|
|
|
m_zintSymbol->height = m_height;
|
|
|
|
m_zintSymbol->whitespace_width = m_whitespace;
|
2021-05-26 07:42:26 +12:00
|
|
|
m_zintSymbol->whitespace_height = m_vwhitespace;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_zintSymbol->border_width = m_borderWidth;
|
2020-12-20 06:13:35 +13:00
|
|
|
m_zintSymbol->option_1 = m_option_1;
|
2017-10-30 10:38:39 +13:00
|
|
|
m_zintSymbol->input_mode = m_input_mode;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_zintSymbol->option_2 = m_option_2;
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_dotty) {
|
|
|
|
m_zintSymbol->output_options |= BARCODE_DOTTY_MODE;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
m_zintSymbol->dot_size = m_dot_size;
|
|
|
|
m_zintSymbol->show_hrt = m_show_hrt ? 1 : 0;
|
|
|
|
m_zintSymbol->eci = m_eci;
|
2020-05-16 21:22:33 +12:00
|
|
|
m_zintSymbol->option_3 = m_option_3;
|
2020-06-05 05:45:25 +12:00
|
|
|
m_zintSymbol->scale = m_scale;
|
2021-05-15 23:23:46 +12:00
|
|
|
if (m_gs1parens) {
|
|
|
|
m_zintSymbol->input_mode |= GS1PARENS_MODE;
|
|
|
|
}
|
2021-07-14 04:39:03 +12:00
|
|
|
if (m_gs1nocheck) {
|
|
|
|
m_zintSymbol->input_mode |= GS1NOCHECK_MODE;
|
|
|
|
}
|
2020-06-05 05:45:25 +12:00
|
|
|
if (m_gssep) {
|
|
|
|
m_zintSymbol->output_options |= GS1_GS_SEPARATOR;
|
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_reader_init) {
|
|
|
|
m_zintSymbol->output_options |= READER_INIT;
|
|
|
|
}
|
2020-06-05 05:45:25 +12:00
|
|
|
if (m_debug) {
|
|
|
|
m_zintSymbol->debug |= ZINT_DEBUG_PRINT;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(m_zintSymbol->fgcolour, m_fgColor.name().toLatin1().right(6));
|
2020-08-05 02:10:16 +12:00
|
|
|
if (m_fgColor.alpha() != 0xff) {
|
|
|
|
strcat(m_zintSymbol->fgcolour, m_fgColor.name(QColor::HexArgb).toLatin1().mid(1,2));
|
|
|
|
}
|
2020-06-05 05:45:25 +12:00
|
|
|
strcpy(m_zintSymbol->bgcolour, m_bgColor.name().toLatin1().right(6));
|
2020-08-05 02:10:16 +12:00
|
|
|
if (m_bgColor.alpha() != 0xff) {
|
|
|
|
strcat(m_zintSymbol->bgcolour, m_bgColor.name(QColor::HexArgb).toLatin1().mid(1,2));
|
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_cmyk) {
|
|
|
|
m_zintSymbol->output_options |= CMYK_COLOUR;
|
|
|
|
}
|
2020-06-05 05:45:25 +12:00
|
|
|
strcpy(m_zintSymbol->primary, m_primaryMessage.toLatin1().left(127));
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::encode() {
|
|
|
|
resetSymbol();
|
2016-08-14 20:58:38 +12:00
|
|
|
QByteArray bstr = m_text.toUtf8();
|
2020-10-01 00:19:12 +13:00
|
|
|
m_error = ZBarcode_Encode_and_Buffer_Vector(m_zintSymbol, (unsigned char *) bstr.data(), bstr.length(), 0); /* Note do our own rotation */
|
2018-01-22 03:59:15 +13:00
|
|
|
m_lastError = m_zintSymbol->errtxt;
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_error < ZINT_ERROR) {
|
|
|
|
m_borderType = m_zintSymbol->output_options & (BARCODE_BIND | BARCODE_BOX);
|
|
|
|
m_height = m_zintSymbol->height;
|
|
|
|
m_borderWidth = m_zintSymbol->border_width;
|
|
|
|
m_whitespace = m_zintSymbol->whitespace_width;
|
2021-05-26 07:42:26 +12:00
|
|
|
m_vwhitespace = m_zintSymbol->whitespace_height;
|
2020-10-01 00:19:12 +13:00
|
|
|
emit encoded();
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
int QZint::symbol() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setSymbol(int symbol) {
|
|
|
|
m_symbol = symbol;
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
int QZint::inputMode() const {
|
|
|
|
return m_input_mode;
|
|
|
|
}
|
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
void QZint::setInputMode(int input_mode) {
|
|
|
|
m_input_mode = input_mode;
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
QString QZint::text() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setText(const QString & text) {
|
|
|
|
m_text = text;
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
QString QZint::primaryMessage() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_primaryMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setPrimaryMessage(const QString & primaryMessage) {
|
|
|
|
m_primaryMessage = primaryMessage;
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:11:23 +12:00
|
|
|
float QZint::height() const {
|
2020-06-05 05:45:25 +12:00
|
|
|
return m_height;
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:11:23 +12:00
|
|
|
void QZint::setHeight(float height) {
|
2016-08-14 20:58:38 +12:00
|
|
|
m_height = height;
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
int QZint::option2() const {
|
|
|
|
return m_option_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setOption2(int option) {
|
|
|
|
m_option_2 = option;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
int QZint::option3() const {
|
|
|
|
return m_option_3;
|
|
|
|
}
|
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
void QZint::setOption3(int option) {
|
|
|
|
m_option_3 = option;
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
float QZint::scale() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setScale(float scale) {
|
|
|
|
m_scale = scale;
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
bool QZint::dotty() const {
|
|
|
|
return m_dotty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setDotty(bool dotty) {
|
|
|
|
m_dotty = dotty;
|
|
|
|
}
|
|
|
|
|
2016-09-19 01:09:58 +12:00
|
|
|
void QZint::setDotSize(float dot_size) {
|
|
|
|
m_dot_size = dot_size;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
QColor QZint::fgColor() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_fgColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setFgColor(const QColor & fgColor) {
|
|
|
|
m_fgColor = fgColor;
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
QColor QZint::bgColor() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_bgColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setBgColor(const QColor & bgColor) {
|
|
|
|
m_bgColor = bgColor;
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
void QZint::setCMYK(bool cmyk) {
|
|
|
|
m_cmyk = cmyk;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
int QZint::borderType() const {
|
|
|
|
return m_borderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setBorderType(int borderTypeIndex) {
|
|
|
|
if (borderTypeIndex == 1) {
|
|
|
|
m_borderType = BARCODE_BIND;
|
|
|
|
} else if (borderTypeIndex == 2) {
|
|
|
|
m_borderType = BARCODE_BOX;
|
|
|
|
} else {
|
|
|
|
m_borderType = 0;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2017-09-11 03:03:09 +12:00
|
|
|
int QZint::borderWidth() const {
|
2016-08-14 20:58:38 +12:00
|
|
|
return m_borderWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setBorderWidth(int boderWidth) {
|
2017-06-18 02:17:24 +12:00
|
|
|
if (boderWidth < 0 || boderWidth > 16)
|
|
|
|
boderWidth = 0;
|
2016-08-14 20:58:38 +12:00
|
|
|
m_borderWidth = boderWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setWhitespace(int whitespace) {
|
|
|
|
m_whitespace = whitespace;
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:42:26 +12:00
|
|
|
void QZint::setVWhitespace(int vwhitespace) {
|
|
|
|
m_vwhitespace = vwhitespace;
|
|
|
|
}
|
|
|
|
|
2020-12-20 06:13:35 +13:00
|
|
|
int QZint::option1() const {
|
|
|
|
return m_option_1;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2020-12-20 06:13:35 +13:00
|
|
|
void QZint::setOption1(int option_1) {
|
|
|
|
m_option_1 = option_1;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
void QZint::setFontSetting(int fontSettingIndex) {
|
|
|
|
if (fontSettingIndex == 1) {
|
|
|
|
m_fontSetting = BOLD_TEXT;
|
|
|
|
} else if (fontSettingIndex == 2) {
|
|
|
|
m_fontSetting = SMALL_TEXT;
|
|
|
|
} else if (fontSettingIndex == 3) {
|
|
|
|
m_fontSetting = SMALL_TEXT | BOLD_TEXT;
|
|
|
|
} else {
|
|
|
|
m_fontSetting = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setShowText(bool show) {
|
|
|
|
m_show_hrt = show;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
void QZint::setTargetSize(int width, int height) {
|
|
|
|
target_size_horiz = width;
|
|
|
|
target_size_vert = height;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
void QZint::setGSSep(bool gssep) {
|
|
|
|
m_gssep = gssep;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
int QZint::rotateAngle() const {
|
|
|
|
return m_rotate_angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setRotateAngle(int rotateIndex) {
|
|
|
|
if (rotateIndex == 1) {
|
|
|
|
m_rotate_angle = 90;
|
|
|
|
} else if (rotateIndex == 2) {
|
|
|
|
m_rotate_angle = 180;
|
|
|
|
} else if (rotateIndex == 3) {
|
|
|
|
m_rotate_angle = 270;
|
|
|
|
} else {
|
|
|
|
m_rotate_angle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QZint::setECI(int ECIIndex) {
|
|
|
|
if (ECIIndex >= 1 && ECIIndex <= 11) {
|
|
|
|
m_eci = ECIIndex + 2;
|
|
|
|
} else if (ECIIndex >= 12 && ECIIndex <= 15) {
|
|
|
|
m_eci = ECIIndex + 3;
|
|
|
|
} else if (ECIIndex >= 16 && ECIIndex <= 26) {
|
|
|
|
m_eci = ECIIndex + 4;
|
|
|
|
} else if (ECIIndex == 27) {
|
2021-01-12 07:11:41 +13:00
|
|
|
m_eci = 899; /* 8-bit binary data */
|
2020-10-01 00:19:12 +13:00
|
|
|
} else {
|
|
|
|
m_eci = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:23:46 +12:00
|
|
|
void QZint::setGS1Parens(bool gs1parens) {
|
|
|
|
m_gs1parens = gs1parens;
|
|
|
|
}
|
|
|
|
|
2021-07-14 04:39:03 +12:00
|
|
|
void QZint::setGS1NoCheck(bool gs1nocheck) {
|
|
|
|
m_gs1nocheck = gs1nocheck;
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
void QZint::setReaderInit(bool reader_init) {
|
|
|
|
m_reader_init = reader_init;
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
void QZint::setDebug(bool debug) {
|
|
|
|
m_debug = debug;
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:31:08 +12:00
|
|
|
/* Legacy */
|
|
|
|
void QZint::setWidth(int width) { setOption1(width); }
|
|
|
|
int QZint::width() const { return m_option_1; }
|
|
|
|
void QZint::setSecurityLevel(int securityLevel) { setOption2(securityLevel); }
|
|
|
|
int QZint::securityLevel() const { return m_option_2; }
|
|
|
|
void QZint::setPdf417CodeWords(int /*pdf417CodeWords*/) {}
|
|
|
|
int QZint::pdf417CodeWords() const { return 0; }
|
|
|
|
void QZint::setHideText(bool hide) { setShowText(!hide); }
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
bool QZint::hasHRT(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_HRT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QZint::isExtendable(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_EXTENDABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QZint::supportsECI(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_ECI);
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:23:46 +12:00
|
|
|
bool QZint::supportsGS1(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_GS1);
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
bool QZint::isFixedRatio(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_FIXED_RATIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QZint::isDotty(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_DOTTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QZint::supportsReaderInit(int symbology) const {
|
|
|
|
return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_READER_INIT);
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:45:25 +12:00
|
|
|
int QZint::getError() const {
|
|
|
|
return m_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QZint::error_message() const {
|
|
|
|
return m_lastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString & QZint::lastError() const {
|
|
|
|
return m_lastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QZint::hasErrors() const {
|
|
|
|
return m_lastError.length();
|
|
|
|
}
|
2020-11-23 00:29:45 +13:00
|
|
|
|
|
|
|
int QZint::getVersion() const {
|
|
|
|
return ZBarcode_Version();
|
|
|
|
}
|
2020-06-05 05:45:25 +12:00
|
|
|
|
|
|
|
bool QZint::save_to_file(QString filename) {
|
|
|
|
resetSymbol();
|
|
|
|
strcpy(m_zintSymbol->outfile, filename.toLatin1().left(255));
|
2016-08-14 20:58:38 +12:00
|
|
|
QByteArray bstr = m_text.toUtf8();
|
2020-10-01 00:19:12 +13:00
|
|
|
m_error = ZBarcode_Encode_and_Print(m_zintSymbol, (unsigned char *) bstr.data(), bstr.length(), m_rotate_angle);
|
|
|
|
if (m_error >= ZINT_ERROR) {
|
2016-08-14 20:58:38 +12:00
|
|
|
m_lastError = m_zintSymbol->errtxt;
|
|
|
|
return false;
|
2017-12-20 11:01:45 +13:00
|
|
|
} else {
|
|
|
|
return true;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
Qt::GlobalColor QZint::colourToQtColor(int colour) {
|
|
|
|
switch (colour) {
|
|
|
|
case 1: // Cyan
|
|
|
|
return Qt::cyan;
|
|
|
|
break;
|
|
|
|
case 2: // Blue
|
|
|
|
return Qt::blue;
|
|
|
|
break;
|
|
|
|
case 3: // Magenta
|
|
|
|
return Qt::magenta;
|
|
|
|
break;
|
|
|
|
case 4: // Red
|
|
|
|
return Qt::red;
|
|
|
|
break;
|
|
|
|
case 5: // Yellow
|
|
|
|
return Qt::yellow;
|
|
|
|
break;
|
|
|
|
case 6: // Green
|
|
|
|
return Qt::green;
|
|
|
|
break;
|
|
|
|
case 8: // White
|
|
|
|
return Qt::white;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Qt::black;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
void QZint::render(QPainter & painter, const QRectF & paintRect, AspectRatioMode mode) {
|
2018-06-18 13:36:40 +12:00
|
|
|
struct zint_vector_rect *rect;
|
|
|
|
struct zint_vector_hexagon *hex;
|
|
|
|
struct zint_vector_circle *circle;
|
|
|
|
struct zint_vector_string *string;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-05-16 21:22:33 +12:00
|
|
|
(void)mode; /* Not currently used */
|
|
|
|
|
2016-12-15 12:33:17 +13:00
|
|
|
encode();
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
painter.save();
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_error >= ZINT_ERROR) {
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
QFont font(fontStyleError, fontSizeError);
|
|
|
|
painter.setFont(font);
|
|
|
|
painter.drawText(paintRect, Qt::AlignCenter | Qt::TextWordWrap, m_lastError);
|
|
|
|
painter.restore();
|
2016-08-14 20:58:38 +12:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.setClipRect(paintRect, Qt::IntersectClip);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
qreal xtr = paintRect.x();
|
|
|
|
qreal ytr = paintRect.y();
|
2018-06-18 13:36:40 +12:00
|
|
|
qreal scale;
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
qreal gwidth = m_zintSymbol->vector->width;
|
|
|
|
qreal gheight = m_zintSymbol->vector->height;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_rotate_angle == 90 || m_rotate_angle == 270) {
|
|
|
|
if (paintRect.width() / gheight < paintRect.height() / gwidth) {
|
|
|
|
scale = paintRect.width() / gheight;
|
|
|
|
} else {
|
|
|
|
scale = paintRect.height() / gwidth;
|
|
|
|
}
|
2016-12-15 12:33:17 +13:00
|
|
|
} else {
|
2020-10-01 00:19:12 +13:00
|
|
|
if (paintRect.width() / gwidth < paintRect.height() / gheight) {
|
|
|
|
scale = paintRect.width() / gwidth;
|
|
|
|
} else {
|
|
|
|
scale = paintRect.height() / gheight;
|
|
|
|
}
|
2016-12-15 12:33:17 +13:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2018-06-19 14:56:44 +12:00
|
|
|
xtr += (qreal) (paintRect.width() - gwidth * scale) / 2.0;
|
|
|
|
ytr += (qreal) (paintRect.height() - gheight * scale) / 2.0;
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
if (m_rotate_angle) {
|
|
|
|
painter.translate(paintRect.width() / 2.0, paintRect.height() / 2.0); // Need to rotate around centre
|
|
|
|
painter.rotate(m_rotate_angle);
|
|
|
|
painter.translate(-paintRect.width() / 2.0, -paintRect.height() / 2.0); // Undo
|
|
|
|
}
|
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
painter.translate(xtr, ytr);
|
2018-06-18 13:36:40 +12:00
|
|
|
painter.scale(scale, scale);
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2020-10-27 01:21:43 +13:00
|
|
|
QBrush bgBrush(m_bgColor);
|
|
|
|
painter.fillRect(QRectF(0, 0, gwidth, gheight), bgBrush);
|
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
//Red square for diagnostics
|
|
|
|
//painter.fillRect(QRect(0, 0, m_zintSymbol->vector->width, m_zintSymbol->vector->height), QBrush(QColor(255,0,0,255)));
|
2016-08-14 20:58:38 +12:00
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
// Plot rectangles
|
|
|
|
rect = m_zintSymbol->vector->rectangles;
|
2020-10-01 00:19:12 +13:00
|
|
|
if (rect) {
|
|
|
|
QBrush brush(Qt::SolidPattern);
|
|
|
|
while (rect) {
|
|
|
|
if (rect->colour == -1) {
|
|
|
|
brush.setColor(m_fgColor);
|
|
|
|
} else {
|
|
|
|
brush.setColor(colourToQtColor(rect->colour));
|
2020-04-07 03:37:34 +12:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
painter.fillRect(QRectF(rect->x, rect->y, rect->width, rect->height), brush);
|
|
|
|
rect = rect->next;
|
2020-04-07 03:37:34 +12:00
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
// Plot hexagons
|
|
|
|
hex = m_zintSymbol->vector->hexagons;
|
2020-10-01 00:19:12 +13:00
|
|
|
if (hex) {
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2020-10-27 01:21:43 +13:00
|
|
|
QBrush fgBrush(m_fgColor);
|
|
|
|
qreal previous_diameter = 0.0, radius = 0.0, half_radius = 0.0, half_sqrt3_radius = 0.0;
|
2020-10-01 00:19:12 +13:00
|
|
|
while (hex) {
|
2020-10-27 01:21:43 +13:00
|
|
|
if (previous_diameter != hex->diameter) {
|
|
|
|
previous_diameter = hex->diameter;
|
|
|
|
radius = 0.5 * previous_diameter;
|
|
|
|
half_radius = 0.25 * previous_diameter;
|
|
|
|
half_sqrt3_radius = 0.43301270189221932338 * previous_diameter;
|
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
|
|
|
QPainterPath pt;
|
2020-10-27 01:21:43 +13:00
|
|
|
pt.moveTo(hex->x, hex->y + radius);
|
|
|
|
pt.lineTo(hex->x + half_sqrt3_radius, hex->y + half_radius);
|
|
|
|
pt.lineTo(hex->x + half_sqrt3_radius, hex->y - half_radius);
|
|
|
|
pt.lineTo(hex->x, hex->y - radius);
|
|
|
|
pt.lineTo(hex->x - half_sqrt3_radius, hex->y - half_radius);
|
|
|
|
pt.lineTo(hex->x - half_sqrt3_radius, hex->y + half_radius);
|
|
|
|
pt.lineTo(hex->x, hex->y + radius);
|
|
|
|
painter.fillPath(pt, fgBrush);
|
2020-10-01 00:19:12 +13:00
|
|
|
|
|
|
|
hex = hex->next;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
// Plot dots (circles)
|
|
|
|
circle = m_zintSymbol->vector->circles;
|
2020-10-01 00:19:12 +13:00
|
|
|
if (circle) {
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
QPen p;
|
2020-10-27 01:21:43 +13:00
|
|
|
QBrush fgBrush(m_fgColor);
|
|
|
|
qreal previous_diameter = 0.0, radius = 0.0;
|
2020-10-01 00:19:12 +13:00
|
|
|
while (circle) {
|
2020-10-27 01:21:43 +13:00
|
|
|
if (previous_diameter != circle->diameter) {
|
|
|
|
previous_diameter = circle->diameter;
|
|
|
|
radius = 0.5 * previous_diameter;
|
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
if (circle->colour) { // Set means use background colour
|
|
|
|
p.setColor(m_bgColor);
|
2021-08-23 00:59:01 +12:00
|
|
|
p.setWidthF(circle->width);
|
2020-10-01 00:19:12 +13:00
|
|
|
painter.setPen(p);
|
2021-08-23 00:59:01 +12:00
|
|
|
painter.setBrush(circle->width ? Qt::NoBrush : bgBrush);
|
2020-10-01 00:19:12 +13:00
|
|
|
} else {
|
|
|
|
p.setColor(m_fgColor);
|
2021-08-23 00:59:01 +12:00
|
|
|
p.setWidthF(circle->width);
|
2020-10-01 00:19:12 +13:00
|
|
|
painter.setPen(p);
|
2021-08-23 00:59:01 +12:00
|
|
|
painter.setBrush(circle->width ? Qt::NoBrush : bgBrush);
|
2020-10-01 00:19:12 +13:00
|
|
|
}
|
2020-10-27 01:21:43 +13:00
|
|
|
painter.drawEllipse(QPointF(circle->x, circle->y), radius, radius);
|
2020-10-01 00:19:12 +13:00
|
|
|
circle = circle->next;
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2018-06-18 13:36:40 +12:00
|
|
|
// Plot text
|
|
|
|
string = m_zintSymbol->vector->strings;
|
|
|
|
if (string) {
|
2020-10-01 00:19:12 +13:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2020-10-06 11:22:06 +13:00
|
|
|
QPen p;
|
|
|
|
p.setColor(m_fgColor);
|
|
|
|
painter.setPen(p);
|
2020-10-01 00:19:12 +13:00
|
|
|
bool bold = (m_zintSymbol->output_options & BOLD_TEXT) && (!isExtendable() || (m_zintSymbol->output_options & SMALL_TEXT));
|
|
|
|
QFont font(fontStyle, -1 /*pointSize*/, bold ? QFont::Bold : -1);
|
|
|
|
while (string) {
|
|
|
|
font.setPixelSize(string->fsize);
|
|
|
|
painter.setFont(font);
|
|
|
|
QString content = QString::fromUtf8((const char *) string->text);
|
|
|
|
/* string->y is baseline of font */
|
|
|
|
if (string->halign == 1) { /* Left align */
|
|
|
|
painter.drawText(QPointF(string->x, string->y), content);
|
|
|
|
} else {
|
|
|
|
QFontMetrics fm(painter.fontMetrics());
|
|
|
|
int width = fm.boundingRect(content).width();
|
|
|
|
if (string->halign == 2) { /* Right align */
|
|
|
|
painter.drawText(QPointF(string->x - width, string->y), content);
|
|
|
|
} else { /* Centre align */
|
|
|
|
painter.drawText(QPointF(string->x - (width / 2.0), string->y), content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string = string->next;
|
|
|
|
}
|
2016-08-14 20:58:38 +12:00
|
|
|
}
|
2020-10-01 00:19:12 +13:00
|
|
|
|
2016-08-14 20:58:38 +12:00
|
|
|
painter.restore();
|
|
|
|
}
|
2017-08-06 18:02:07 +12:00
|
|
|
}
|