qzint.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /***************************************************************************
  2. * Copyright (C) 2008 by BogDan Vatra *
  3. * bogdan@licentia.eu *
  4. * Copyright (C) 2010-2024 Robin Stuart *
  5. * *
  6. * This program is free software: you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation, either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  16. ***************************************************************************/
  17. /* SPDX-License-Identifier: GPL-3.0-or-later */
  18. #ifdef _MSC_VER
  19. #if _MSC_VER >= 1900 /* MSVC 2015 */
  20. #pragma warning(disable: 4996) /* function or variable may be unsafe */
  21. #endif
  22. #endif
  23. //#include <QDebug>
  24. #include <QFontDatabase>
  25. #include <QFontMetrics>
  26. /* The following include is necessary to compile with Qt 5.15 on Windows; Qt 5.7 did not require it */
  27. #include <QPainterPath>
  28. #include <QRegularExpression>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include "qzint.h"
  32. #include "../backend/fonts/normal_ttf.h" /* Arimo */
  33. #include "../backend/fonts/upcean_ttf.h" /* OCR-B subset (digits, "<", ">") */
  34. // Shorthand
  35. #define QSL QStringLiteral
  36. #define QSEmpty QLatin1String("")
  37. namespace Zint {
  38. static const int maxSegs = 256;
  39. static const int maxCLISegs = 10; /* CLI restricted to 10 segments (including main data) */
  40. /* Matches RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string */
  41. static const QString colorREstr(
  42. QSL("^([0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?)|(((100|[0-9]{0,2}),){3}(100|[0-9]{0,2}))$"));
  43. Q_GLOBAL_STATIC_WITH_ARGS(QRegularExpression, colorRE, (colorREstr))
  44. static const QString normalFontFamily = QSL("Arimo"); /* Sans-serif metrically compatible with Arial */
  45. static const QString upceanFontFamily = QSL("OCRB"); /* Monospace OCR-B */
  46. static const QString fontFamilyError = QSL("Arimo");
  47. static const int fontSizeError = 14; /* Point size */
  48. static int normalFontID = -2; /* Use -2 as `addApplicationFontFromData()` returns -1 on error */
  49. /* Load Arimo from static array */
  50. static int loadNormalFont() {
  51. static const QByteArray normalFontArray
  52. = QByteArray::fromRawData((const char *) normal_ttf, sizeof(normal_ttf));
  53. normalFontID = QFontDatabase::addApplicationFontFromData(normalFontArray);
  54. return normalFontID;
  55. }
  56. static int upceanFontID = -2; /* Use -2 as `addApplicationFontFromData()` returns -1 on error */
  57. /* Load OCR-B EAN/UPC subset from static array */
  58. static int loadUpceanFont() {
  59. static const QByteArray upceanFontArray
  60. = QByteArray::fromRawData((const char *) upcean_ttf, sizeof(upcean_ttf));
  61. upceanFontID = QFontDatabase::addApplicationFontFromData(upceanFontArray);
  62. return upceanFontID;
  63. }
  64. /* Helper to convert QColor to RGB(A) hex string */
  65. static QString qcolor_to_str(const QColor &color) {
  66. if (color.alpha() == 0xFF) {
  67. return QString::asprintf("%02X%02X%02X", color.red(), color.green(), color.blue());
  68. }
  69. return QString::asprintf("%02X%02X%02X%02X", color.red(), color.green(), color.blue(), color.alpha());
  70. }
  71. /* Helper to convert RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string) to QColor */
  72. static QColor str_to_qcolor(const QString &text) {
  73. QColor color;
  74. int r, g, b, a;
  75. if (text.contains(',')) {
  76. int comma1 = text.indexOf(',');
  77. int comma2 = text.indexOf(',', comma1 + 1);
  78. int comma3 = text.indexOf(',', comma2 + 1);
  79. int black = 100 - text.mid(comma3 + 1).toInt();
  80. int val = 100 - text.mid(0, comma1).toInt();
  81. r = (int) roundf((0xFF * val * black) / 10000.0f);
  82. val = 100 - text.mid(comma1 + 1, comma2 - comma1 - 1).toInt();
  83. g = (int) roundf((0xFF * val * black) / 10000.0f);
  84. val = 100 - text.mid(comma2 + 1, comma3 - comma2 - 1).toInt();
  85. b = (int) roundf((0xFF * val * black) / 10000.0f);
  86. a = 0xFF;
  87. } else {
  88. r = text.mid(0, 2).toInt(nullptr, 16);
  89. g = text.mid(2, 2).toInt(nullptr, 16);
  90. b = text.mid(4, 2).toInt(nullptr, 16);
  91. a = text.length() == 8 ? text.mid(6, 2).toInt(nullptr, 16) : 0xFF;
  92. }
  93. color.setRgb(r, g, b, a);
  94. return color;
  95. }
  96. /* Helper to convert ECI combo index to ECI value */
  97. static int ECIIndexToECI(const int ECIIndex) {
  98. int ret;
  99. if (ECIIndex >= 1 && ECIIndex <= 11) {
  100. ret = ECIIndex + 2;
  101. } else if (ECIIndex >= 12 && ECIIndex <= 15) {
  102. ret = ECIIndex + 3;
  103. } else if (ECIIndex >= 16 && ECIIndex <= 31) {
  104. ret = ECIIndex + 4;
  105. } else if (ECIIndex == 32) {
  106. ret = 170; /* ISO 646 Invariant */
  107. } else if (ECIIndex == 33) {
  108. ret = 899; /* 8-bit binary data */
  109. } else {
  110. ret = 0;
  111. }
  112. return ret;
  113. }
  114. /* Helper to calculate max right and bottom of elements for fudging `render()` */
  115. static void getMaxRectsRightBottom(struct zint_vector *vector, int &maxRight, int &maxBottom) {
  116. struct zint_vector_rect *rect;
  117. struct zint_vector_hexagon *hex;
  118. struct zint_vector_circle *circle;
  119. maxRight = maxBottom = -1;
  120. for (rect = vector->rectangles; rect; rect = rect->next) {
  121. if (rect->x + rect->width > maxRight) {
  122. maxRight = rect->x + rect->width;
  123. }
  124. if (rect->y + rect->height > maxBottom) {
  125. maxBottom = rect->y + rect->height;
  126. }
  127. }
  128. for (hex = vector->hexagons; hex; hex = hex->next) {
  129. if (hex->x + hex->diameter > maxRight) {
  130. maxRight = hex->x + hex->diameter;
  131. }
  132. if (hex->y + hex->diameter > maxBottom) {
  133. maxBottom = hex->y + hex->diameter;
  134. }
  135. }
  136. for (circle = vector->circles; circle; circle = circle->next) {
  137. if (circle->x + circle->diameter + circle->width > maxRight) {
  138. maxRight = circle->x + circle->diameter + circle->width;
  139. }
  140. if (circle->y + circle->diameter + circle->width > maxBottom) {
  141. maxBottom = circle->y + circle->diameter + circle->width;
  142. }
  143. }
  144. // TODO: Strings?
  145. }
  146. /* Segment constructors */
  147. QZintSeg::QZintSeg() : m_eci(0) {}
  148. QZintSeg::QZintSeg(const QString& text, const int ECIIndex) : m_text(text), m_eci(ECIIndexToECI(ECIIndex)) {}
  149. QZint::QZint()
  150. : m_zintSymbol(nullptr), m_symbol(BARCODE_CODE128), m_input_mode(UNICODE_MODE),
  151. m_height(0.0f),
  152. m_option_1(-1), m_option_2(0), m_option_3(0),
  153. m_dpmm(0.0f),
  154. m_scale(1.0f),
  155. m_dotty(false), m_dot_size(4.0f / 5.0f),
  156. m_guardDescent(5.0f),
  157. m_textGap(1.0f),
  158. m_fgStr(QSL("000000")), m_bgStr(QSL("FFFFFF")), m_cmyk(false),
  159. m_borderType(0), m_borderWidth(0),
  160. m_whitespace(0), m_vwhitespace(0),
  161. m_fontSetting(0),
  162. m_show_hrt(true),
  163. m_gssep(false),
  164. m_quiet_zones(false), m_no_quiet_zones(false),
  165. m_compliant_height(false),
  166. m_rotate_angle(0),
  167. m_eci(0),
  168. m_gs1parens(false), m_gs1nocheck(false),
  169. m_reader_init(false),
  170. m_guard_whitespace(false),
  171. m_embed_vector_font(false),
  172. m_warn_level(WARN_DEFAULT), m_debug(false),
  173. m_encodedWidth(0), m_encodedRows(0), m_encodedHeight(0.0f),
  174. m_vectorWidth(0.0f), m_vectorHeight(0.0f),
  175. m_error(0),
  176. target_size_horiz(0), target_size_vert(0) // Legacy
  177. {
  178. memset(&m_structapp, 0, sizeof(m_structapp));
  179. }
  180. QZint::~QZint() {
  181. if (m_zintSymbol)
  182. ZBarcode_Delete(m_zintSymbol);
  183. }
  184. bool QZint::resetSymbol() {
  185. m_error = 0;
  186. m_lastError.clear();
  187. if (m_zintSymbol) {
  188. ZBarcode_Reset(m_zintSymbol);
  189. } else if (!(m_zintSymbol = ZBarcode_Create())) {
  190. m_error = ZINT_ERROR_MEMORY;
  191. m_lastError = QSL("Insufficient memory for Zint structure");
  192. return false;
  193. }
  194. m_zintSymbol->symbology = m_symbol;
  195. m_zintSymbol->height = m_height;
  196. m_zintSymbol->scale = m_scale;
  197. m_zintSymbol->whitespace_width = m_whitespace;
  198. m_zintSymbol->whitespace_height = m_vwhitespace;
  199. m_zintSymbol->border_width = m_borderWidth;
  200. m_zintSymbol->output_options = m_borderType | m_fontSetting;
  201. if (m_dotty) {
  202. m_zintSymbol->output_options |= BARCODE_DOTTY_MODE;
  203. }
  204. if (m_cmyk) {
  205. m_zintSymbol->output_options |= CMYK_COLOUR;
  206. }
  207. if (m_gssep) {
  208. m_zintSymbol->output_options |= GS1_GS_SEPARATOR;
  209. }
  210. if (m_quiet_zones) {
  211. m_zintSymbol->output_options |= BARCODE_QUIET_ZONES;
  212. }
  213. if (m_no_quiet_zones) {
  214. m_zintSymbol->output_options |= BARCODE_NO_QUIET_ZONES;
  215. }
  216. if (m_compliant_height) {
  217. m_zintSymbol->output_options |= COMPLIANT_HEIGHT;
  218. }
  219. if (m_reader_init) {
  220. m_zintSymbol->output_options |= READER_INIT;
  221. }
  222. if (m_guard_whitespace) {
  223. m_zintSymbol->output_options |= EANUPC_GUARD_WHITESPACE;
  224. }
  225. if (m_embed_vector_font) {
  226. m_zintSymbol->output_options |= EMBED_VECTOR_FONT;
  227. }
  228. strcpy(m_zintSymbol->fgcolour, m_fgStr.toLatin1().left(15));
  229. strcpy(m_zintSymbol->bgcolour, m_bgStr.toLatin1().left(15));
  230. strcpy(m_zintSymbol->primary, m_primaryMessage.toLatin1().left(127));
  231. m_zintSymbol->option_1 = m_option_1;
  232. m_zintSymbol->option_2 = m_option_2;
  233. m_zintSymbol->option_3 = m_option_3;
  234. m_zintSymbol->show_hrt = m_show_hrt ? 1 : 0;
  235. m_zintSymbol->input_mode = m_input_mode;
  236. if (m_gs1parens) {
  237. m_zintSymbol->input_mode |= GS1PARENS_MODE;
  238. }
  239. if (m_gs1nocheck) {
  240. m_zintSymbol->input_mode |= GS1NOCHECK_MODE;
  241. }
  242. m_zintSymbol->eci = m_eci;
  243. m_zintSymbol->dpmm = m_dpmm;
  244. m_zintSymbol->dot_size = m_dot_size;
  245. m_zintSymbol->guard_descent = m_guardDescent;
  246. m_zintSymbol->text_gap = m_textGap;
  247. m_zintSymbol->structapp = m_structapp;
  248. m_zintSymbol->warn_level = m_warn_level;
  249. m_zintSymbol->debug = m_debug ? ZINT_DEBUG_PRINT : 0;
  250. return true;
  251. }
  252. void QZint::encode() {
  253. if (resetSymbol()) {
  254. if (m_segs.empty()) {
  255. QByteArray bstr = m_text.toUtf8();
  256. /* Note do our own rotation */
  257. m_error = ZBarcode_Encode_and_Buffer_Vector(m_zintSymbol, (unsigned char *) bstr.data(),
  258. bstr.length(), 0);
  259. } else {
  260. struct zint_seg segs[maxSegs];
  261. std::vector<QByteArray> bstrs;
  262. int seg_count = convertSegs(segs, bstrs);
  263. /* Note do our own rotation */
  264. m_error = ZBarcode_Encode_Segs_and_Buffer_Vector(m_zintSymbol, segs, seg_count, 0);
  265. }
  266. m_lastError = m_zintSymbol->errtxt;
  267. }
  268. if (m_error < ZINT_ERROR) {
  269. m_borderType = m_zintSymbol->output_options & (BARCODE_BIND | BARCODE_BOX | BARCODE_BIND_TOP);
  270. m_height = m_zintSymbol->height;
  271. m_borderWidth = m_zintSymbol->border_width;
  272. m_whitespace = m_zintSymbol->whitespace_width;
  273. m_vwhitespace = m_zintSymbol->whitespace_height;
  274. m_encodedWidth = m_zintSymbol->width;
  275. m_encodedRows = m_zintSymbol->rows;
  276. m_encodedHeight = m_zintSymbol->height;
  277. m_vectorWidth = m_zintSymbol->vector->width;
  278. m_vectorHeight = m_zintSymbol->vector->height;
  279. emit encoded();
  280. } else {
  281. m_encodedWidth = m_encodedRows = 0;
  282. m_encodedHeight = m_vectorWidth = m_vectorHeight = 0.0f;
  283. emit errored();
  284. }
  285. }
  286. /* Symbology to use (see BARCODE_XXX) */
  287. int QZint::symbol() const {
  288. return m_symbol;
  289. }
  290. void QZint::setSymbol(int symbol) {
  291. m_symbol = symbol;
  292. }
  293. /* Input data encoding. Default UNICODE_MODE */
  294. int QZint::inputMode() const {
  295. return m_input_mode;
  296. }
  297. void QZint::setInputMode(int input_mode) {
  298. m_input_mode = input_mode;
  299. }
  300. /* Input data (segment 0 text) */
  301. QString QZint::text() const {
  302. return m_text;
  303. }
  304. /* Set input data. Note: clears segs */
  305. void QZint::setText(const QString& text) {
  306. m_text = text;
  307. m_segs.clear();
  308. }
  309. /* Input segments. */
  310. std::vector<QZintSeg> QZint::segs() const {
  311. return m_segs;
  312. }
  313. /* Set segments. Note: clears text and sets eci */
  314. void QZint::setSegs(const std::vector<QZintSeg>& segs) {
  315. m_segs = segs;
  316. m_text.clear();
  317. if (m_segs.size()) { /* Make sure `symbol->eci` synced */
  318. m_eci = m_segs[0].m_eci;
  319. }
  320. }
  321. /* Primary message (Maxicode, Composite) */
  322. QString QZint::primaryMessage() const {
  323. return m_primaryMessage;
  324. }
  325. void QZint::setPrimaryMessage(const QString& primaryMessage) {
  326. m_primaryMessage = primaryMessage;
  327. }
  328. /* Symbol height in X-dimensions */
  329. float QZint::height() const {
  330. return m_height;
  331. }
  332. void QZint::setHeight(float height) {
  333. m_height = height;
  334. }
  335. /* Symbol-specific options (see "../docs/manual.txt") */
  336. int QZint::option1() const {
  337. return m_option_1;
  338. }
  339. void QZint::setOption1(int option_1) {
  340. m_option_1 = option_1;
  341. }
  342. /* Symbol-specific options */
  343. int QZint::option2() const {
  344. return m_option_2;
  345. }
  346. void QZint::setOption2(int option) {
  347. m_option_2 = option;
  348. }
  349. int QZint::option3() const {
  350. return m_option_3;
  351. }
  352. void QZint::setOption3(int option) {
  353. m_option_3 = option;
  354. }
  355. /* Scale factor when printing barcode, i.e. adjusts X-dimension */
  356. float QZint::scale() const {
  357. return m_scale;
  358. }
  359. void QZint::setScale(float scale) {
  360. m_scale = scale;
  361. }
  362. /* Resolution of output in dots per mm (BMP/EMF/PCX/PNG/TIF only) */
  363. float QZint::dpmm() const {
  364. return m_dpmm;
  365. }
  366. void QZint::setDPMM(float dpmm) {
  367. m_dpmm = dpmm;
  368. }
  369. /* Dotty mode */
  370. bool QZint::dotty() const {
  371. return m_dotty;
  372. }
  373. void QZint::setDotty(bool dotty) {
  374. m_dotty = dotty;
  375. }
  376. /* Size of dots used in BARCODE_DOTTY_MODE */
  377. float QZint::dotSize() const {
  378. return m_dot_size;
  379. }
  380. void QZint::setDotSize(float dotSize) {
  381. m_dot_size = dotSize;
  382. }
  383. /* Height in X-dimensions that EAN/UPC guard bars descend */
  384. float QZint::guardDescent() const {
  385. return m_guardDescent;
  386. }
  387. void QZint::setGuardDescent(float guardDescent) {
  388. m_guardDescent = guardDescent;
  389. }
  390. /* Structured Append info */
  391. int QZint::structAppCount() const {
  392. return m_structapp.count;
  393. }
  394. int QZint::structAppIndex() const {
  395. return m_structapp.index;
  396. }
  397. QString QZint::structAppID() const {
  398. return m_structapp.id;
  399. }
  400. void QZint::setStructApp(const int count, const int index, const QString& id) {
  401. if (count) {
  402. m_structapp.count = count;
  403. m_structapp.index = index;
  404. memset(m_structapp.id, 0, sizeof(m_structapp.id));
  405. if (!id.isEmpty()) {
  406. QByteArray idArr = id.toLatin1();
  407. #if defined(__GNUC__) && __GNUC__ >= 8 && !defined(__clang__)
  408. #pragma GCC diagnostic push
  409. #pragma GCC diagnostic ignored "-Wstringop-truncation"
  410. #endif
  411. strncpy(m_structapp.id, idArr, sizeof(m_structapp.id));
  412. #if defined(__GNUC__) && !defined(__clang__)
  413. #pragma GCC diagnostic pop
  414. #endif
  415. }
  416. } else {
  417. clearStructApp();
  418. }
  419. }
  420. void QZint::clearStructApp() {
  421. memset(&m_structapp, 0, sizeof(m_structapp));
  422. }
  423. /* Foreground colour (may be RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string) */
  424. QString QZint::fgStr() const {
  425. return m_fgStr;
  426. }
  427. bool QZint::setFgStr(const QString& fgStr) {
  428. if (fgStr.indexOf(*colorRE) == 0) {
  429. m_fgStr = fgStr;
  430. return true;
  431. }
  432. return false;
  433. }
  434. /* Foreground colour as QColor */
  435. QColor QZint::fgColor() const {
  436. return str_to_qcolor(m_fgStr);
  437. }
  438. void QZint::setFgColor(const QColor& fgColor) {
  439. m_fgStr = qcolor_to_str(fgColor);
  440. }
  441. /* Background colour (may be RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string) */
  442. QString QZint::bgStr() const {
  443. return m_bgStr;
  444. }
  445. bool QZint::setBgStr(const QString& bgStr) {
  446. if (bgStr.indexOf(*colorRE) == 0) {
  447. m_bgStr = bgStr;
  448. return true;
  449. }
  450. return false;
  451. }
  452. /* Background colour as QColor */
  453. QColor QZint::bgColor() const {
  454. return str_to_qcolor(m_bgStr);
  455. }
  456. void QZint::setBgColor(const QColor& bgColor) {
  457. m_bgStr = qcolor_to_str(bgColor);
  458. }
  459. /* Use CMYK colour space (Encapsulated PostScript and TIF) */
  460. bool QZint::cmyk() const {
  461. return m_cmyk;
  462. }
  463. void QZint::setCMYK(bool cmyk) {
  464. m_cmyk = cmyk;
  465. }
  466. /* Type of border above/below/around barcode */
  467. int QZint::borderType() const {
  468. return m_borderType;
  469. }
  470. void QZint::setBorderType(int borderTypeIndex) {
  471. if (borderTypeIndex == 1) {
  472. m_borderType = BARCODE_BIND;
  473. } else if (borderTypeIndex == 2) {
  474. m_borderType = BARCODE_BOX;
  475. } else if (borderTypeIndex == 3) {
  476. m_borderType = BARCODE_BIND_TOP;
  477. } else {
  478. m_borderType = 0;
  479. }
  480. }
  481. /* Size of border in X-dimensions */
  482. int QZint::borderWidth() const {
  483. return m_borderWidth;
  484. }
  485. void QZint::setBorderWidth(int borderWidth) {
  486. if (borderWidth < 0 || borderWidth > 16)
  487. borderWidth = 0;
  488. m_borderWidth = borderWidth;
  489. }
  490. /* Width in X-dimensions of whitespace to left & right of barcode */
  491. int QZint::whitespace() const {
  492. return m_whitespace;
  493. }
  494. void QZint::setWhitespace(int whitespace) {
  495. m_whitespace = whitespace;
  496. }
  497. /* Height in X-dimensions of whitespace above & below the barcode */
  498. int QZint::vWhitespace() const {
  499. return m_vwhitespace;
  500. }
  501. void QZint::setVWhitespace(int vWhitespace) {
  502. m_vwhitespace = vWhitespace;
  503. }
  504. /* Type of font to use i.e. normal, small, bold or (vector only) small bold */
  505. int QZint::fontSetting() const {
  506. return m_fontSetting;
  507. }
  508. void QZint::setFontSetting(int fontSettingIndex) { // Sets from comboBox index
  509. if (fontSettingIndex == 1) {
  510. m_fontSetting = BOLD_TEXT;
  511. } else if (fontSettingIndex == 2) {
  512. m_fontSetting = SMALL_TEXT;
  513. } else if (fontSettingIndex == 3) {
  514. m_fontSetting = SMALL_TEXT | BOLD_TEXT;
  515. } else {
  516. m_fontSetting = 0;
  517. }
  518. }
  519. void QZint::setFontSettingValue(int fontSetting) { // Sets literal value
  520. if ((fontSetting & (BOLD_TEXT | SMALL_TEXT)) == fontSetting) {
  521. m_fontSetting = fontSetting;
  522. } else {
  523. m_fontSetting = 0;
  524. }
  525. }
  526. /* Text gap */
  527. float QZint::textGap() const {
  528. return m_textGap;
  529. }
  530. void QZint::setTextGap(float textGap) {
  531. m_textGap = textGap;
  532. }
  533. /* Show (true) or hide (false) Human Readable Text (HRT) */
  534. bool QZint::showText() const {
  535. return m_show_hrt;
  536. }
  537. void QZint::setShowText(bool showText) {
  538. m_show_hrt = showText;
  539. }
  540. /* Set to true to use GS (Group Separator) instead of FNC1 as GS1 separator (Data Matrix) */
  541. bool QZint::gsSep() const {
  542. return m_gssep;
  543. }
  544. void QZint::setGSSep(bool gsSep) {
  545. m_gssep = gsSep;
  546. }
  547. /* Add compliant quiet zones (additional to any specified whitespace)
  548. Note: CODE16K, CODE49, CODABLOCKF, ITF14, EAN/UPC have default quiet zones */
  549. bool QZint::quietZones() const {
  550. return m_quiet_zones;
  551. }
  552. void QZint::setQuietZones(bool quietZones) {
  553. m_quiet_zones = quietZones;
  554. }
  555. /* Disable quiet zones, notably those with defaults as listed above */
  556. bool QZint::noQuietZones() const {
  557. return m_no_quiet_zones;
  558. }
  559. void QZint::setNoQuietZones(bool noQuietZones) {
  560. m_no_quiet_zones = noQuietZones;
  561. }
  562. /* Warn if height not compliant and use standard height (if any) as default */
  563. bool QZint::compliantHeight() const {
  564. return m_compliant_height;
  565. }
  566. void QZint::setCompliantHeight(bool compliantHeight) {
  567. m_compliant_height = compliantHeight;
  568. }
  569. /* Rotate barcode by angle (degrees 0, 90, 180 and 270) */
  570. int QZint::rotateAngle() const {
  571. return m_rotate_angle;
  572. }
  573. void QZint::setRotateAngle(int rotateIndex) { // Sets from comboBox index
  574. if (rotateIndex == 1) {
  575. m_rotate_angle = 90;
  576. } else if (rotateIndex == 2) {
  577. m_rotate_angle = 180;
  578. } else if (rotateIndex == 3) {
  579. m_rotate_angle = 270;
  580. } else {
  581. m_rotate_angle = 0;
  582. }
  583. }
  584. void QZint::setRotateAngleValue(int rotateAngle) { // Sets literal value
  585. if (rotateAngle == 90) {
  586. m_rotate_angle = 90;
  587. } else if (rotateAngle == 180) {
  588. m_rotate_angle = 180;
  589. } else if (rotateAngle == 270) {
  590. m_rotate_angle = 270;
  591. } else {
  592. m_rotate_angle = 0;
  593. }
  594. }
  595. /* Extended Channel Interpretation (segment 0 eci) */
  596. int QZint::eci() const {
  597. return m_eci;
  598. }
  599. void QZint::setECI(int ECIIndex) { // Sets from comboBox index
  600. m_eci = ECIIndexToECI(ECIIndex);
  601. }
  602. void QZint::setECIValue(int eci) { // Sets literal value
  603. if (eci < 3 || (eci > 35 && eci != 170 && eci != 899) || eci == 14 || eci == 19) {
  604. m_eci = 0;
  605. } else {
  606. m_eci = eci;
  607. }
  608. }
  609. /* Process parentheses as GS1 AI delimiters (instead of square brackets) */
  610. bool QZint::gs1Parens() const {
  611. return m_gs1parens;
  612. }
  613. void QZint::setGS1Parens(bool gs1Parens) {
  614. m_gs1parens = gs1Parens;
  615. }
  616. /* Do not check validity of GS1 data (except that printable ASCII only) */
  617. bool QZint::gs1NoCheck() const {
  618. return m_gs1nocheck;
  619. }
  620. void QZint::setGS1NoCheck(bool gs1NoCheck) {
  621. m_gs1nocheck = gs1NoCheck;
  622. }
  623. /* Reader Initialisation (Programming) */
  624. bool QZint::readerInit() const {
  625. return m_reader_init;
  626. }
  627. void QZint::setReaderInit(bool readerInit) {
  628. m_reader_init = readerInit;
  629. }
  630. /* Whether to add quiet zone indicators ("<", ">") to HRT (EAN/UPC) */
  631. bool QZint::guardWhitespace() const {
  632. return m_guard_whitespace;
  633. }
  634. void QZint::setGuardWhitespace(bool guardWhitespace) {
  635. m_guard_whitespace = guardWhitespace;
  636. }
  637. /* Whether to embed the font in vector output - currently only for SVG output of EAN/UPC */
  638. bool QZint::embedVectorFont() const {
  639. return m_embed_vector_font;
  640. }
  641. void QZint::setEmbedVectorFont(bool embedVectorFont) {
  642. m_embed_vector_font = embedVectorFont;
  643. }
  644. /* Affects error/warning value returned by Zint API (see `getError()` below) */
  645. int QZint::warnLevel() const {
  646. return m_warn_level;
  647. }
  648. void QZint::setWarnLevel(int warnLevel) {
  649. m_warn_level = warnLevel;
  650. }
  651. /* Debugging flags */
  652. bool QZint::debug() const {
  653. return m_debug;
  654. }
  655. void QZint::setDebug(bool debug) {
  656. m_debug = debug;
  657. }
  658. /* Symbol output info set by Zint on successful `render()` */
  659. int QZint::encodedWidth() const { // Read-only, encoded width (no. of modules encoded)
  660. return m_encodedWidth;
  661. }
  662. int QZint::encodedRows() const { // Read-only, no. of rows encoded
  663. return m_encodedRows;
  664. }
  665. float QZint::encodedHeight() const { // Read-only, in X-dimensions
  666. if (m_symbol == BARCODE_MAXICODE) { // Maxicode encoded height is meaningless, so return fixed value
  667. return 33 * 0.866f; // √3 / 2
  668. }
  669. return m_encodedHeight;
  670. }
  671. float QZint::vectorWidth() const { // Read-only, scaled width
  672. return m_vectorWidth;
  673. }
  674. float QZint::vectorHeight() const { // Read-only, scaled height
  675. return m_vectorHeight;
  676. }
  677. /* Legacy property getters/setters */
  678. void QZint::setWidth(int width) { setOption2(width); }
  679. int QZint::width() const { return m_option_2; }
  680. void QZint::setSecurityLevel(int securityLevel) { setOption1(securityLevel); }
  681. int QZint::securityLevel() const { return m_option_1; }
  682. void QZint::setPdf417CodeWords(int /*pdf417CodeWords*/) {}
  683. int QZint::pdf417CodeWords() const { return 0; }
  684. void QZint::setHideText(bool hide) { setShowText(!hide); }
  685. void QZint::setTargetSize(int width, int height) {
  686. target_size_horiz = width;
  687. target_size_vert = height;
  688. }
  689. QString QZint::error_message() const { return m_lastError; } /* Same as lastError() */
  690. /* Test capabilities - `ZBarcode_Cap()` */
  691. bool QZint::hasHRT(int symbology) const {
  692. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_HRT);
  693. }
  694. bool QZint::isStackable(int symbology) const {
  695. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_STACKABLE);
  696. }
  697. bool QZint::isEANUPC(int symbology) const {
  698. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_EANUPC);
  699. }
  700. bool QZint::isExtendable(int symbology) const { /* Legacy - same as `isEANUPC()` */
  701. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_EANUPC);
  702. }
  703. bool QZint::isComposite(int symbology) const {
  704. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_COMPOSITE);
  705. }
  706. bool QZint::supportsECI(int symbology) const {
  707. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_ECI);
  708. }
  709. bool QZint::supportsGS1(int symbology) const {
  710. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_GS1);
  711. }
  712. bool QZint::isDotty(int symbology) const {
  713. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_DOTTY);
  714. }
  715. bool QZint::hasDefaultQuietZones(int symbology) const {
  716. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_QUIET_ZONES);
  717. }
  718. bool QZint::isFixedRatio(int symbology) const {
  719. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_FIXED_RATIO);
  720. }
  721. bool QZint::supportsReaderInit(int symbology) const {
  722. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_READER_INIT);
  723. }
  724. bool QZint::supportsFullMultibyte(int symbology) const {
  725. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_FULL_MULTIBYTE);
  726. }
  727. bool QZint::hasMask(int symbology) const {
  728. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_MASK);
  729. }
  730. bool QZint::supportsStructApp(int symbology) const {
  731. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_STRUCTAPP);
  732. }
  733. bool QZint::hasCompliantHeight(int symbology) const {
  734. return ZBarcode_Cap(symbology ? symbology : m_symbol, ZINT_CAP_COMPLIANT_HEIGHT);
  735. }
  736. /* Whether takes GS1 AI-delimited data */
  737. bool QZint::takesGS1AIData(int symbology) const {
  738. if (symbology == 0) {
  739. symbology = m_symbol;
  740. }
  741. switch (symbology) {
  742. case BARCODE_GS1_128:
  743. case BARCODE_DBAR_EXP:
  744. case BARCODE_DBAR_EXPSTK:
  745. return true;
  746. break;
  747. default:
  748. return symbology >= BARCODE_EANX_CC && symbology <= BARCODE_DBAR_EXPSTK_CC;
  749. break;
  750. }
  751. }
  752. /* Error or warning returned by Zint on `render()` or `save_to_file()` */
  753. int QZint::getError() const {
  754. return m_error;
  755. }
  756. /* Error message returned by Zint on `render()` or `save_to_file()` */
  757. const QString& QZint::lastError() const {
  758. return m_lastError;
  759. }
  760. /* Whether `lastError()` set */
  761. bool QZint::hasErrors() const {
  762. return m_lastError.length();
  763. }
  764. bool QZint::save_to_file(const QString& filename) {
  765. if (resetSymbol()) {
  766. strcpy(m_zintSymbol->outfile, filename.toUtf8().left(255));
  767. if (m_segs.empty()) {
  768. QByteArray bstr = m_text.toUtf8();
  769. m_error = ZBarcode_Encode_and_Print(m_zintSymbol, (unsigned char *) bstr.data(), bstr.length(),
  770. m_rotate_angle);
  771. } else {
  772. struct zint_seg segs[maxSegs];
  773. std::vector<QByteArray> bstrs;
  774. int seg_count = convertSegs(segs, bstrs);
  775. m_error = ZBarcode_Encode_Segs_and_Print(m_zintSymbol, segs, seg_count, m_rotate_angle);
  776. }
  777. }
  778. if (m_error >= ZINT_ERROR) {
  779. m_lastError = m_zintSymbol->errtxt;
  780. m_encodedWidth = m_encodedRows = 0;
  781. m_encodedHeight = m_vectorWidth = m_vectorHeight = 0.0f;
  782. emit errored();
  783. return false;
  784. }
  785. return true;
  786. }
  787. /* Convert `zint_vector_rect->colour` to Qt color */
  788. Qt::GlobalColor QZint::colourToQtColor(int colour) {
  789. switch (colour) {
  790. case 1: // Cyan
  791. return Qt::cyan;
  792. break;
  793. case 2: // Blue
  794. return Qt::blue;
  795. break;
  796. case 3: // Magenta
  797. return Qt::magenta;
  798. break;
  799. case 4: // Red
  800. return Qt::red;
  801. break;
  802. case 5: // Yellow
  803. return Qt::yellow;
  804. break;
  805. case 6: // Green
  806. return Qt::green;
  807. break;
  808. case 8: // White
  809. return Qt::white;
  810. break;
  811. default:
  812. return Qt::black;
  813. break;
  814. }
  815. }
  816. /* Helper to convert `m_segs` to `struct zint_seg[]` */
  817. int QZint::convertSegs(struct zint_seg segs[], std::vector<QByteArray>& bstrs) {
  818. bstrs.reserve(m_segs.size());
  819. int i;
  820. for (i = 0; i < (int) m_segs.size() && i < maxSegs && !m_segs[i].m_text.isEmpty(); i++) {
  821. segs[i].eci = m_segs[i].m_eci;
  822. bstrs.push_back(m_segs[i].m_text.toUtf8());
  823. segs[i].source = (unsigned char *) bstrs.back().data();
  824. segs[i].length = bstrs.back().length();
  825. }
  826. return i;
  827. }
  828. /* Encode and display barcode in `paintRect` using `painter`.
  829. Note: legacy argument `mode` is not used */
  830. void QZint::render(QPainter& painter, const QRectF& paintRect, AspectRatioMode /*mode*/) {
  831. struct zint_vector_rect *rect;
  832. struct zint_vector_hexagon *hex;
  833. struct zint_vector_circle *circle;
  834. struct zint_vector_string *string;
  835. QColor fgColor = str_to_qcolor(m_fgStr);
  836. QColor bgColor = str_to_qcolor(m_bgStr);
  837. encode();
  838. painter.save();
  839. if (m_error >= ZINT_ERROR) {
  840. painter.setRenderHint(QPainter::Antialiasing);
  841. QFont font(fontFamilyError, fontSizeError);
  842. painter.setFont(font);
  843. painter.drawText(paintRect, Qt::AlignCenter | Qt::TextWordWrap, m_lastError);
  844. painter.restore();
  845. return;
  846. }
  847. painter.setClipRect(paintRect, Qt::IntersectClip);
  848. qreal xtr = paintRect.x();
  849. qreal ytr = paintRect.y();
  850. qreal scale;
  851. qreal gwidth = m_zintSymbol->vector->width;
  852. qreal gheight = m_zintSymbol->vector->height;
  853. if (m_rotate_angle == 90 || m_rotate_angle == 270) {
  854. if (paintRect.width() / gheight < paintRect.height() / gwidth) {
  855. scale = paintRect.width() / gheight;
  856. } else {
  857. scale = paintRect.height() / gwidth;
  858. }
  859. } else {
  860. if (paintRect.width() / gwidth < paintRect.height() / gheight) {
  861. scale = paintRect.width() / gwidth;
  862. } else {
  863. scale = paintRect.height() / gheight;
  864. }
  865. }
  866. xtr += (qreal) (paintRect.width() - gwidth * scale) / 2.0;
  867. ytr += (qreal) (paintRect.height() - gheight * scale) / 2.0;
  868. if (m_rotate_angle) {
  869. painter.translate(paintRect.width() / 2.0, paintRect.height() / 2.0); // Need to rotate around centre
  870. painter.rotate(m_rotate_angle);
  871. painter.translate(-paintRect.width() / 2.0, -paintRect.height() / 2.0); // Undo
  872. }
  873. painter.translate(xtr, ytr);
  874. painter.scale(scale, scale);
  875. QBrush bgBrush(bgColor);
  876. if (bgColor.alpha() != 0) {
  877. painter.fillRect(QRectF(0, 0, gwidth, gheight), bgBrush);
  878. }
  879. // Plot rectangles
  880. rect = m_zintSymbol->vector->rectangles;
  881. if (rect) {
  882. int maxRight = -1, maxBottom = -1; // Used for fudging below
  883. getMaxRectsRightBottom(m_zintSymbol->vector, maxRight, maxBottom);
  884. QBrush brush(Qt::SolidPattern);
  885. while (rect) {
  886. if (rect->colour == -1) {
  887. brush.setColor(fgColor);
  888. } else {
  889. brush.setColor(colourToQtColor(rect->colour));
  890. }
  891. // Allow for rounding errors on translation/scaling TODO: proper calc
  892. float fudgeW = rect->x + rect->width == maxRight ? 0.1f : 0.0f;
  893. float fudgeH = rect->y + rect->height == maxBottom ? 0.1f : 0.0f;
  894. painter.fillRect(QRectF(rect->x, rect->y, rect->width + fudgeW, rect->height + fudgeH), brush);
  895. rect = rect->next;
  896. }
  897. }
  898. // Plot hexagons
  899. hex = m_zintSymbol->vector->hexagons;
  900. if (hex) {
  901. painter.setRenderHint(QPainter::Antialiasing);
  902. QBrush fgBrush(fgColor);
  903. qreal previous_diameter = 0.0, radius = 0.0, half_radius = 0.0, half_sqrt3_radius = 0.0;
  904. while (hex) {
  905. if (previous_diameter != hex->diameter) {
  906. previous_diameter = hex->diameter;
  907. radius = 0.5 * previous_diameter;
  908. half_radius = 0.25 * previous_diameter;
  909. half_sqrt3_radius = 0.43301270189221932338 * previous_diameter;
  910. }
  911. QPainterPath pt;
  912. pt.moveTo(hex->x, hex->y + radius);
  913. pt.lineTo(hex->x + half_sqrt3_radius, hex->y + half_radius);
  914. pt.lineTo(hex->x + half_sqrt3_radius, hex->y - half_radius);
  915. pt.lineTo(hex->x, hex->y - radius);
  916. pt.lineTo(hex->x - half_sqrt3_radius, hex->y - half_radius);
  917. pt.lineTo(hex->x - half_sqrt3_radius, hex->y + half_radius);
  918. pt.lineTo(hex->x, hex->y + radius);
  919. painter.fillPath(pt, fgBrush);
  920. hex = hex->next;
  921. }
  922. }
  923. // Plot dots (circles)
  924. circle = m_zintSymbol->vector->circles;
  925. if (circle) {
  926. painter.setRenderHint(QPainter::Antialiasing);
  927. QPen p;
  928. QBrush fgBrush(fgColor);
  929. qreal previous_diameter = 0.0, radius = 0.0;
  930. while (circle) {
  931. if (previous_diameter != circle->diameter) {
  932. previous_diameter = circle->diameter;
  933. radius = 0.5 * previous_diameter;
  934. }
  935. if (circle->colour) { // Set means use background colour (legacy, no longer used)
  936. p.setColor(bgColor);
  937. p.setWidthF(circle->width);
  938. painter.setPen(p);
  939. painter.setBrush(circle->width ? Qt::NoBrush : bgBrush);
  940. } else {
  941. p.setColor(fgColor);
  942. p.setWidthF(circle->width);
  943. painter.setPen(p);
  944. painter.setBrush(circle->width ? Qt::NoBrush : fgBrush);
  945. }
  946. painter.drawEllipse(QPointF(circle->x, circle->y), radius, radius);
  947. circle = circle->next;
  948. }
  949. }
  950. // Plot text
  951. string = m_zintSymbol->vector->strings;
  952. if (string) {
  953. if (normalFontID == -2) { /* First time? */
  954. loadNormalFont();
  955. }
  956. if (upceanFontID == -2) { /* First time? */
  957. loadUpceanFont();
  958. }
  959. painter.setRenderHint(QPainter::Antialiasing);
  960. QPen p;
  961. p.setColor(fgColor);
  962. painter.setPen(p);
  963. bool bold = (m_zintSymbol->output_options & BOLD_TEXT) && !isEANUPC();
  964. QFont font(isEANUPC() ? upceanFontFamily : normalFontFamily, -1 /*pointSize*/,
  965. bold ? QFont::Bold : -1);
  966. while (string) {
  967. font.setPixelSize(string->fsize);
  968. painter.setFont(font);
  969. QString content = QString::fromUtf8((const char *) string->text);
  970. /* string->y is baseline of font */
  971. if (string->halign == 1) { /* Left align */
  972. painter.drawText(QPointF(string->x, string->y), content);
  973. } else {
  974. QFontMetrics fm(painter.fontMetrics());
  975. int width = fm.horizontalAdvance(content);
  976. if (string->halign == 2) { /* Right align */
  977. painter.drawText(QPointF(string->x - width, string->y), content);
  978. } else { /* Centre align */
  979. painter.drawText(QPointF(string->x - (width / 2.0), string->y), content);
  980. }
  981. }
  982. string = string->next;
  983. }
  984. }
  985. painter.restore();
  986. }
  987. /* Returns the default X-dimension (`ZBarcode_Default_Xdim()`).
  988. If `symbology` non-zero then used instead of `symbol()` */
  989. float QZint::defaultXdim(int symbology) const {
  990. return ZBarcode_Default_Xdim(symbology ? symbology : m_symbol);
  991. }
  992. /* Returns the scale to use for X-dimension `x_dim_mm` at `dpmm` for `filetype`.
  993. If `symbology` non-zero then used instead of `symbol()` */
  994. float QZint::getScaleFromXdimDp(float x_dim_mm, float dpmm, const QString& fileType, int symbology) const {
  995. return ZBarcode_Scale_From_XdimDp(symbology ? symbology : m_symbol, x_dim_mm, dpmm, fileType.toLatin1());
  996. }
  997. /* Reverse of `getScaleFromXdimDp()` above, returning the X-dimension or dot density given the scale `scale`.
  998. If `symbology` non-zero then used instead of `symbol()` */
  999. float QZint::getXdimDpFromScale(float scale, float x_dim_mm_or_dpmm, const QString& fileType,
  1000. int symbology) const {
  1001. return ZBarcode_XdimDp_From_Scale(symbology ? symbology : m_symbol, scale, x_dim_mm_or_dpmm,
  1002. fileType.toLatin1());
  1003. }
  1004. /* Set `width_x_dim` and `height_x_dim` with estimated size of barcode based on X-dimension `x_dim`. To be called
  1005. after a successful `render()`. Returns false if `scale()` zero or render is in error, otherwise true */
  1006. bool QZint::getWidthHeightXdim(float x_dim, float &width_x_dim, float &height_x_dim) const {
  1007. if (m_scale == 0.0f || m_vectorWidth == 0.0f || m_vectorHeight == 0.0f) {
  1008. width_x_dim = height_x_dim = 0.0f;
  1009. return false;
  1010. }
  1011. const float scale = m_scale * 2.0f;
  1012. const float width = m_vectorWidth / scale;
  1013. const float height = m_vectorHeight / scale;
  1014. if (rotateAngle() == 90 || rotateAngle() == 270) { // Sideways - swop
  1015. width_x_dim = (height * x_dim);
  1016. height_x_dim = (width * x_dim);
  1017. } else {
  1018. width_x_dim = (width * x_dim);
  1019. height_x_dim = (height * x_dim);
  1020. }
  1021. return true;
  1022. }
  1023. /* Return the BARCODE_XXX name of `symbology` */
  1024. QString QZint::barcodeName(const int symbology) {
  1025. char buf[32];
  1026. if (ZBarcode_BarcodeName(symbology, buf) == 0) {
  1027. return QString(buf);
  1028. }
  1029. return QSEmpty;
  1030. }
  1031. /* Whether Zint library "libzint" built with PNG support or not */
  1032. bool QZint::noPng() {
  1033. return ZBarcode_NoPng() == 1;
  1034. }
  1035. /* Version of Zint library "libzint" linked to */
  1036. int QZint::getVersion() {
  1037. return ZBarcode_Version();
  1038. }
  1039. /* Translate settings into Command Line equivalent. Set `win` to use Windows escaping of data.
  1040. If `autoHeight` set then `--height=` option will not be emitted.
  1041. If HEIGHTPERROW_MODE set and non-zero `heightPerRow` given then use that for height instead of internal
  1042. height */
  1043. QString QZint::getAsCLI(const bool win, const bool longOptOnly, const bool barcodeNames, const bool noEXE,
  1044. const bool autoHeight, const float heightPerRow, const QString& outfile,
  1045. const QZintXdimDpVars *xdimdpVars) const {
  1046. QString cmd(win && !noEXE ? QSL("zint.exe") : QSL("zint"));
  1047. const bool nobackground = bgColor().alpha() == 0;
  1048. const bool notext = hasHRT() && !showText();
  1049. char name_buf[32];
  1050. if (barcodeNames && ZBarcode_BarcodeName(m_symbol, name_buf) == 0) {
  1051. QString name(name_buf + 8); // Strip "BARCODE_" prefix
  1052. arg_str(cmd, longOptOnly ? "--barcode=" : "-b ", name);
  1053. } else {
  1054. arg_int(cmd, longOptOnly ? "--barcode=" : "-b ", m_symbol);
  1055. }
  1056. if (isEANUPC()) {
  1057. arg_int(cmd, "--addongap=", option2());
  1058. }
  1059. if (bgStr() != QSL("FFFFFF") && !nobackground) {
  1060. arg_str(cmd, "--bg=", bgStr());
  1061. }
  1062. bool default_bind = false, default_bind_top = false, default_box = false, default_border = false;
  1063. if (m_symbol == BARCODE_ITF14) {
  1064. if ((borderType() & BARCODE_BOX) && borderWidth() == 5) {
  1065. default_box = default_border = true;
  1066. }
  1067. } else if (m_symbol == BARCODE_CODABLOCKF || m_symbol == BARCODE_HIBC_BLOCKF || m_symbol == BARCODE_CODE16K
  1068. || m_symbol == BARCODE_CODE49) {
  1069. if ((borderType() & BARCODE_BIND) && borderWidth() == 1) {
  1070. default_bind = default_border = true;
  1071. }
  1072. } else if (m_symbol == BARCODE_DPD) {
  1073. if ((borderType() & BARCODE_BIND_TOP) && borderWidth() == 3) {
  1074. default_bind_top = default_border = true;
  1075. }
  1076. }
  1077. arg_bool(cmd, "--binary", (inputMode() & 0x07) == DATA_MODE);
  1078. if (!default_bind) {
  1079. arg_bool(cmd, "--bind", borderType() & BARCODE_BIND);
  1080. }
  1081. if (!default_bind_top) {
  1082. arg_bool(cmd, "--bindtop", borderType() & BARCODE_BIND_TOP);
  1083. }
  1084. arg_bool(cmd, "--bold", !notext && (fontSetting() & BOLD_TEXT) && !isEANUPC());
  1085. if (!default_border) {
  1086. arg_int(cmd, "--border=", borderWidth());
  1087. }
  1088. if (!default_box) {
  1089. arg_bool(cmd, "--box", borderType() & BARCODE_BOX);
  1090. }
  1091. arg_bool(cmd, "--cmyk", cmyk());
  1092. if (m_symbol == BARCODE_DBAR_EXPSTK || m_symbol == BARCODE_DBAR_EXPSTK_CC
  1093. || m_symbol == BARCODE_PDF417 || m_symbol == BARCODE_PDF417COMP || m_symbol == BARCODE_HIBC_PDF
  1094. || m_symbol == BARCODE_MICROPDF417 || m_symbol == BARCODE_HIBC_MICPDF
  1095. || m_symbol == BARCODE_DOTCODE || m_symbol == BARCODE_CODABLOCKF || m_symbol == BARCODE_HIBC_BLOCKF) {
  1096. arg_int(cmd, "--cols=", option2());
  1097. }
  1098. arg_bool(cmd, "--compliantheight", hasCompliantHeight() && compliantHeight());
  1099. if (m_segs.empty()) {
  1100. if (supportsECI()) {
  1101. arg_int(cmd, "--eci=", eci());
  1102. }
  1103. arg_data(cmd, longOptOnly ? "--data=" : "-d ", m_text, win);
  1104. } else {
  1105. arg_int(cmd, "--eci=", m_segs.front().m_eci);
  1106. arg_data(cmd, longOptOnly ? "--data=" : "-d ", m_segs.front().m_text, win);
  1107. for (int i = 1; i < (int) m_segs.size() && i < maxCLISegs && !m_segs[i].m_text.isEmpty(); i++) {
  1108. arg_seg(cmd, i, m_segs[i], win);
  1109. }
  1110. }
  1111. if (m_symbol == BARCODE_DATAMATRIX || m_symbol == BARCODE_HIBC_DM) {
  1112. arg_bool(cmd, "--dmiso144", (option3() & DM_ISO_144) == DM_ISO_144);
  1113. arg_bool(cmd, "--dmre", (option3() & 0x7F) == DM_DMRE);
  1114. }
  1115. if ((m_symbol == BARCODE_DOTCODE || (isDotty() && dotty())) && dotSize() != 0.8f) {
  1116. arg_float(cmd, "--dotsize=", dotSize());
  1117. }
  1118. if (m_symbol != BARCODE_DOTCODE && isDotty() && dotty()) {
  1119. arg_bool(cmd, "--dotty", dotty());
  1120. }
  1121. if (showText()) {
  1122. arg_bool(cmd, "--embedfont", embedVectorFont());
  1123. }
  1124. arg_bool(cmd, "--esc", inputMode() & ESCAPE_MODE);
  1125. arg_bool(cmd, "--extraesc", inputMode() & EXTRA_ESCAPE_MODE);
  1126. arg_bool(cmd, "--fast", inputMode() & FAST_MODE);
  1127. if (fgStr() != QSL("000000") && fgStr() != QSL("000000FF")) {
  1128. arg_str(cmd, "--fg=", fgStr());
  1129. }
  1130. arg_bool(cmd, "--fullmultibyte", supportsFullMultibyte() && (option3() & 0xFF) == ZINT_FULL_MULTIBYTE);
  1131. if (supportsGS1()) {
  1132. arg_bool(cmd, "--gs1", (inputMode() & 0x07) == GS1_MODE);
  1133. arg_bool(cmd, "--gs1parens", gs1Parens() || (inputMode() & GS1PARENS_MODE));
  1134. arg_bool(cmd, "--gs1nocheck", gs1NoCheck() || (inputMode() & GS1NOCHECK_MODE));
  1135. arg_bool(cmd, "--gssep", gsSep());
  1136. }
  1137. if (isEANUPC() && guardDescent() != 5.0f) {
  1138. arg_float(cmd, "--guarddescent=", guardDescent(), true /*allowZero*/);
  1139. }
  1140. if (isEANUPC() && showText()) {
  1141. arg_bool(cmd, "--guardwhitespace", guardWhitespace());
  1142. }
  1143. if (!autoHeight && !isFixedRatio()) {
  1144. if (inputMode() & HEIGHTPERROW_MODE) {
  1145. arg_float(cmd, "--height=", heightPerRow ? heightPerRow : height());
  1146. arg_bool(cmd, "--heightperrow", true);
  1147. } else {
  1148. arg_float(cmd, "--height=", height());
  1149. }
  1150. }
  1151. arg_bool(cmd, "--init", supportsReaderInit() && readerInit());
  1152. if (hasMask()) {
  1153. arg_int(cmd, "--mask=", (option3() >> 8) - 1, true /*allowZero*/);
  1154. }
  1155. if (m_symbol == BARCODE_MAXICODE || isComposite()) {
  1156. arg_int(cmd, "--mode=", option1());
  1157. }
  1158. arg_bool(cmd, "--nobackground", nobackground);
  1159. arg_bool(cmd, "--noquietzones", hasDefaultQuietZones() && noQuietZones());
  1160. arg_bool(cmd, "--notext", notext);
  1161. arg_data(cmd, longOptOnly ? "--output=" : "-o ", outfile, win);
  1162. if (m_symbol == BARCODE_MAXICODE || isComposite()) {
  1163. arg_data(cmd, "--primary=", primaryMessage(), win);
  1164. }
  1165. arg_bool(cmd, "--quietzones", !hasDefaultQuietZones() && quietZones());
  1166. arg_int(cmd, "--rotate=", rotateAngle());
  1167. if (m_symbol == BARCODE_CODE16K || m_symbol == BARCODE_CODABLOCKF || m_symbol == BARCODE_HIBC_BLOCKF
  1168. || m_symbol == BARCODE_CODE49) {
  1169. arg_int(cmd, "--rows=", option1());
  1170. } else if (m_symbol == BARCODE_DBAR_EXPSTK || m_symbol == BARCODE_DBAR_EXPSTK_CC
  1171. || m_symbol == BARCODE_PDF417 || m_symbol == BARCODE_PDF417COMP || m_symbol == BARCODE_HIBC_PDF) {
  1172. arg_int(cmd, "--rows=", option3());
  1173. }
  1174. if (dpmm()) {
  1175. arg_scalexdimdp(cmd, "--scalexdimdp", scale(), dpmm(), symbol(), xdimdpVars);
  1176. } else if (scale() != 1.0f) {
  1177. arg_float(cmd, "--scale=", scale());
  1178. }
  1179. if (m_symbol == BARCODE_MAXICODE) {
  1180. arg_int(cmd, "--scmvv=", option2() - 1, true /*allowZero*/);
  1181. }
  1182. if (m_symbol == BARCODE_PDF417 || m_symbol == BARCODE_PDF417COMP || m_symbol == BARCODE_HIBC_PDF
  1183. || m_symbol == BARCODE_AZTEC || m_symbol == BARCODE_HIBC_AZTEC
  1184. || m_symbol == BARCODE_QRCODE || m_symbol == BARCODE_HIBC_QR || m_symbol == BARCODE_MICROQR
  1185. || m_symbol == BARCODE_RMQR || m_symbol == BARCODE_GRIDMATRIX || m_symbol == BARCODE_HANXIN
  1186. || m_symbol == BARCODE_ULTRA) {
  1187. arg_int(cmd, "--secure=", option1());
  1188. }
  1189. if (m_symbol == BARCODE_CODE16K || m_symbol == BARCODE_CODABLOCKF || m_symbol == BARCODE_HIBC_BLOCKF
  1190. || m_symbol == BARCODE_CODE49) {
  1191. arg_int(cmd, "--separator=", option3());
  1192. }
  1193. arg_bool(cmd, "--small", !notext && (fontSetting() & SMALL_TEXT));
  1194. if (m_symbol == BARCODE_DATAMATRIX || m_symbol == BARCODE_HIBC_DM) {
  1195. arg_bool(cmd, "--square", (option3() & 0x7F) == DM_SQUARE);
  1196. }
  1197. if (supportsStructApp()) {
  1198. arg_structapp(cmd, "--structapp=", structAppCount(), structAppIndex(), structAppID(), win);
  1199. }
  1200. if (!notext && textGap() != 1.0f) {
  1201. arg_float(cmd, "--textgap=", textGap(), true /*allowZero*/);
  1202. }
  1203. arg_bool(cmd, "--verbose", debug());
  1204. if (m_symbol == BARCODE_AZTEC || m_symbol == BARCODE_HIBC_AZTEC
  1205. || m_symbol == BARCODE_MSI_PLESSEY || m_symbol == BARCODE_CODE11
  1206. || m_symbol == BARCODE_C25STANDARD || m_symbol == BARCODE_C25INTER || m_symbol == BARCODE_C25IATA
  1207. || m_symbol == BARCODE_C25LOGIC || m_symbol == BARCODE_C25IND
  1208. || m_symbol == BARCODE_CODE39 || m_symbol == BARCODE_HIBC_39 || m_symbol == BARCODE_EXCODE39
  1209. || m_symbol == BARCODE_LOGMARS || m_symbol == BARCODE_CODABAR
  1210. || m_symbol == BARCODE_DATAMATRIX || m_symbol == BARCODE_HIBC_DM
  1211. || m_symbol == BARCODE_QRCODE || m_symbol == BARCODE_HIBC_QR || m_symbol == BARCODE_MICROQR
  1212. || m_symbol == BARCODE_RMQR || m_symbol == BARCODE_GRIDMATRIX || m_symbol == BARCODE_HANXIN
  1213. || m_symbol == BARCODE_CHANNEL || m_symbol == BARCODE_CODEONE || m_symbol == BARCODE_CODE93
  1214. || m_symbol == BARCODE_ULTRA || m_symbol == BARCODE_VIN) {
  1215. arg_int(cmd, "--vers=", option2());
  1216. } else if (m_symbol == BARCODE_DAFT && option2() != 250) {
  1217. arg_int(cmd, "--vers=", option2());
  1218. }
  1219. arg_int(cmd, "--vwhitesp=", vWhitespace());
  1220. arg_int(cmd, longOptOnly ? "--whitesp=" : "-w ", whitespace());
  1221. arg_bool(cmd, "--werror", warnLevel() == WARN_FAIL_ALL);
  1222. return cmd;
  1223. }
  1224. /* `getAsCLI()` helpers */
  1225. void QZint::arg_str(QString& cmd, const char *const opt, const QString& val) {
  1226. if (!val.isEmpty()) {
  1227. QByteArray bstr = val.toUtf8();
  1228. cmd += QString::asprintf(" %s%.*s", opt, (int) bstr.length(), bstr.data());
  1229. }
  1230. }
  1231. void QZint::arg_int(QString& cmd, const char *const opt, const int val, const bool allowZero) {
  1232. if (val > 0 || (val == 0 && allowZero)) {
  1233. cmd += QString::asprintf(" %s%d", opt, val);
  1234. }
  1235. }
  1236. void QZint::arg_bool(QString& cmd, const char *const opt, const bool val) {
  1237. if (val) {
  1238. cmd += QString::asprintf(" %s", opt);
  1239. }
  1240. }
  1241. void QZint::arg_data(QString& cmd, const char *const opt, const QString& val, const bool win) {
  1242. if (!val.isEmpty()) {
  1243. QString text(val);
  1244. arg_data_esc(cmd, opt, text, win);
  1245. }
  1246. }
  1247. void QZint::arg_seg(QString& cmd, const int seg_no, const QZintSeg& val, const bool win) {
  1248. QString text(val.m_text);
  1249. QString opt = QString::asprintf("--seg%d=%d,", seg_no, val.m_eci);
  1250. arg_data_esc(cmd, opt.toUtf8(), text, win);
  1251. }
  1252. void QZint::arg_data_esc(QString& cmd, const char *const opt, QString& text, const bool win) {
  1253. const char delim = win ? '"' : '\'';
  1254. if (win) {
  1255. // Difficult (impossible?) to fully escape strings on Windows, e.g. "blah%PATH%" will substitute
  1256. // env var PATH, so just doing basic escaping here
  1257. text.replace("\\\\", "\\\\\\\\"); // Double-up backslashed backslash `\\` -> `\\\\`
  1258. text.replace("\"", "\\\""); // Backslash quote `"` -> `\"`
  1259. QByteArray bstr = text.toUtf8();
  1260. cmd += QString::asprintf(" %s%c%.*s%c", opt, delim, (int) bstr.length(), bstr.data(), delim);
  1261. } else {
  1262. text.replace("'", "'\\''"); // Single quote `'` -> `'\''`
  1263. QByteArray bstr = text.toUtf8();
  1264. cmd += QString::asprintf(" %s%c%.*s%c", opt, delim, (int) bstr.length(), bstr.data(), delim);
  1265. }
  1266. }
  1267. void QZint::arg_float(QString& cmd, const char *const opt, const float val, const bool allowZero) {
  1268. if (val > 0 || (val == 0 && allowZero)) {
  1269. cmd += QString::asprintf(" %s%g", opt, val);
  1270. }
  1271. }
  1272. void QZint::arg_structapp(QString& cmd, const char *const opt, const int count, const int index,
  1273. const QString& id, const bool win) {
  1274. if (count >= 2 && index >= 1) {
  1275. if (id.isEmpty()) {
  1276. cmd += QString::asprintf(" %s%d,%d", opt, index, count);
  1277. } else {
  1278. QByteArray bstr = id.toUtf8();
  1279. arg_data(cmd, opt, QString::asprintf("%d,%d,%.*s", index, count, (int) bstr.length(), bstr.data()),
  1280. win);
  1281. }
  1282. }
  1283. }
  1284. void QZint::arg_scalexdimdp(QString& cmd, const char *const opt, const float scale, const float dpmm,
  1285. const int symbol, const QZintXdimDpVars *xdimdpVars) {
  1286. if (dpmm) {
  1287. float resolution = dpmm;
  1288. float x_dim;
  1289. const char *x_dim_units_str = "";
  1290. const char *resolution_units_str = "";
  1291. if (xdimdpVars && xdimdpVars->set) {
  1292. x_dim = xdimdpVars->x_dim;
  1293. resolution = xdimdpVars->resolution;
  1294. if (xdimdpVars->x_dim_units || xdimdpVars->resolution_units) {
  1295. x_dim_units_str = xdimdpVars->x_dim_units ? "in" : "mm";
  1296. resolution_units_str = xdimdpVars->resolution_units ? "dpi" : "dpmm";
  1297. }
  1298. } else {
  1299. x_dim = ZBarcode_XdimDp_From_Scale(symbol, scale, resolution, nullptr);
  1300. }
  1301. cmd += QString::asprintf(" %s=%g%s,%g%s",
  1302. opt, x_dim, x_dim_units_str, resolution, resolution_units_str);
  1303. }
  1304. }
  1305. /* Helper to return "GIF"/"SVG"(/"EMF") if `msg` false, "raster"/"vector"(/"EMF") otherwise
  1306. (EMF only if `symbol` is MaxiCode) */
  1307. const char *QZintXdimDpVars::getFileType(int symbol, const struct QZintXdimDpVars *vars, bool msg) {
  1308. static const char *filetypes[3] = { "GIF", "SVG", "EMF" };
  1309. static const char *msg_types[3] = { "raster", "vector", "EMF" };
  1310. if (!vars) return "";
  1311. const int idx = std::max(std::min(symbol == BARCODE_MAXICODE ? vars->filetype_maxicode
  1312. : vars->filetype, 2), 0);
  1313. return msg ? msg_types[idx] : filetypes[idx];
  1314. }
  1315. } /* namespace Zint */
  1316. /* vim: set ts=4 sw=4 et : */