qzint.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /***************************************************************************
  2. * Copyright (C) 2008 by BogDan Vatra *
  3. * bogdan@licentia.eu *
  4. * Copyright (C) 2010-2023 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. /*
  19. * For version, see "../backend/zintconfig.h"
  20. * For documentation, see "../docs/manual.txt"
  21. */
  22. #ifndef QZINT_H
  23. #define QZINT_H
  24. #include <QObject>
  25. #include <QColor>
  26. #include <QPainter>
  27. #include "zint.h"
  28. namespace Zint
  29. {
  30. /* QString version of `struct zint_seg` */
  31. class QZintSeg {
  32. public:
  33. QString m_text; // `seg->source` and `seg->length`
  34. int m_eci; // `seg->eci`
  35. QZintSeg();
  36. QZintSeg(const QString& text, const int ECIIndex = 0); // `ECIIndex` is comboBox index (not ECI value)
  37. };
  38. struct QZintXdimDpVars; // Forward reference to Printing Scale settings, see end
  39. /* Interface */
  40. class QZint : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. /* Legacy - not used */
  45. enum AspectRatioMode{ IgnoreAspectRatio = 0, KeepAspectRatio = 1, CenterBarCode = 2 };
  46. public:
  47. QZint();
  48. ~QZint();
  49. /* Symbology to use (see BARCODE_XXX) */
  50. int symbol() const; // `symbol->symbology`
  51. void setSymbol(int symbol);
  52. /* Input data encoding. Default UNICODE_MODE */
  53. int inputMode() const; // `symbol->input_mode`
  54. void setInputMode(int input_mode);
  55. /* Note text/eci and segs are mutally exclusive */
  56. /* Input data (segment 0 text) */
  57. QString text() const;
  58. /* Set input data. Note: clears segs */
  59. void setText(const QString& text);
  60. /* Input segments. */
  61. std::vector<QZintSeg> segs() const;
  62. /* Set segments. Note: clears text and sets eci */
  63. void setSegs(const std::vector<QZintSeg>& segs);
  64. /* Primary message (Maxicode, Composite) */
  65. QString primaryMessage() const; // `symbol->primary`
  66. void setPrimaryMessage(const QString& primaryMessage);
  67. /* Symbol height in X-dimensions */
  68. float height() const; // `symbol->height`
  69. void setHeight(float height);
  70. /* Symbol-specific options (see "../docs/manual.txt") */
  71. int option1() const; // `symbol->option_1`
  72. void setOption1(int option_1);
  73. /* Symbol-specific options */
  74. int option2() const; // `symbol->option_2`
  75. void setOption2(int option);
  76. /* Symbol-specific options */
  77. int option3() const; // `symbol->option_3`
  78. void setOption3(int option);
  79. /* Scale factor when printing barcode, i.e. adjusts X-dimension */
  80. float scale() const; // `symbol->scale`
  81. void setScale(float scale);
  82. /* Resolution of output in dots per mm (BMP/EMF/PCX/PNG/TIF only) */
  83. float dpmm() const; // `symbol->dpmm`
  84. void setDPMM(float dpmm);
  85. /* Dotty mode */
  86. bool dotty() const; // `symbol->input_mode | BARCODE_DOTTY_MODE`
  87. void setDotty(bool botty);
  88. /* Size of dots used in BARCODE_DOTTY_MODE */
  89. float dotSize() const; // `symbol->dot_size`
  90. void setDotSize(float dot_size);
  91. /* Height in X-dimensions that EAN/UPC guard bars descend */
  92. float guardDescent() const; // `symbol->guard_descent`
  93. void setGuardDescent(float guardDescent);
  94. /* Structured Append info */
  95. int structAppCount() const; // `symbol->structapp.count`
  96. int structAppIndex() const; // `symbol->structapp.index`
  97. QString structAppID() const; // `symbol->structapp.id`
  98. void setStructApp(const int count, const int index, const QString& id);
  99. void clearStructApp();
  100. /* Foreground colour (may be RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string) */
  101. QString fgStr() const; // `symbol->fgcolour`
  102. bool setFgStr(const QString& fgStr); // Returns false if not valid colour string
  103. /* Foreground colour as QColor */
  104. QColor fgColor() const; // `symbol->fgcolour`
  105. void setFgColor(const QColor& fgColor);
  106. /* Background colour (may be RGB(A) hex string or CMYK decimal "C,M,Y,K" percentage string) */
  107. QString bgStr() const; // `symbol->bgcolour`
  108. bool setBgStr(const QString& bgStr); // Returns false if not valid colour string
  109. /* Background colour as QColor */
  110. QColor bgColor() const; // `symbol->bgcolour`
  111. void setBgColor(const QColor& bgColor);
  112. /* Use CMYK colour space (Encapsulated PostScript and TIF) */
  113. bool cmyk() const; // `symbol->output_options | CMYK_COLOUR`
  114. void setCMYK(bool cmyk);
  115. /* Type of border above/below/around barcode */
  116. int borderType() const; // `symbol->output_options | BARCODE_BIND | BARCODE_BOX | BARCODE_BIND_TOP`
  117. void setBorderType(int borderTypeIndex);
  118. /* Size of border in X-dimensions */
  119. int borderWidth() const; // `symbol->border_width`
  120. void setBorderWidth(int borderWidth);
  121. /* Width in X-dimensions of whitespace to left & right of barcode */
  122. int whitespace() const; // `symbol->whitespace_width`
  123. void setWhitespace(int whitespace);
  124. /* Height in X-dimensions of whitespace above & below the barcode */
  125. int vWhitespace() const; // `symbol->whitespace_height`
  126. void setVWhitespace(int vWhitespace);
  127. /* Type of font to use i.e. normal, small, bold or (vector only) small bold */
  128. int fontSetting() const; // `symbol->output_options | SMALL_TEXT | BOLD_TEXT`
  129. void setFontSetting(int fontSettingIndex); // Sets from comboBox index
  130. void setFontSettingValue(int fontSetting); // Sets literal value
  131. /* Text gap */
  132. float textGap() const; // `symbol->text_gap`
  133. void setTextGap(float textGap);
  134. /* Show (true) or hide (false) Human Readable Text (HRT) */
  135. bool showText() const; // `symbol->show_hrt`
  136. void setShowText(bool showText);
  137. /* Set to true to use GS (Group Separator) instead of FNC1 as GS1 separator (Data Matrix) */
  138. bool gsSep() const; // `symbol->output_options | GS1_GS_SEPARATOR`
  139. void setGSSep(bool gsSep);
  140. /* Add compliant quiet zones (additional to any specified whitespace)
  141. Note: CODE16K, CODE49, CODABLOCKF, ITF14, EAN/UPC have default quiet zones */
  142. bool quietZones() const; // `symbol->output_options | BARCODE_QUIET_ZONES`
  143. void setQuietZones(bool quietZones);
  144. /* Disable quiet zones, notably those with defaults as listed above */
  145. bool noQuietZones() const; // `symbol->output_options | BARCODE_NO_QUIET_ZONES`
  146. void setNoQuietZones(bool noQuietZones);
  147. /* Warn if height not compliant and use standard height (if any) as default */
  148. bool compliantHeight() const; // `symbol->output_options | COMPLIANT_HEIGHT`
  149. void setCompliantHeight(bool compliantHeight);
  150. /* Rotate barcode by angle (degrees 0, 90, 180 and 270) */
  151. int rotateAngle() const;
  152. void setRotateAngle(int rotateIndex); // Sets from comboBox index
  153. void setRotateAngleValue(int rotateAngle); // Sets literal value
  154. /* Extended Channel Interpretation (segment 0 eci) */
  155. int eci() const; // `symbol->eci`
  156. void setECI(int ECIIndex); // Sets from comboBox index
  157. void setECIValue(int eci); // Sets literal value
  158. /* Process parentheses as GS1 AI delimiters (instead of square brackets) */
  159. bool gs1Parens() const; // `symbol->input_mode | GS1PARENS_MODE`
  160. void setGS1Parens(bool gs1Parens);
  161. /* Do not check validity of GS1 data (except that printable ASCII only) */
  162. bool gs1NoCheck() const; // `symbol->input_mode | GS1NOCHECK_MODE`
  163. void setGS1NoCheck(bool gs1NoCheck);
  164. /* Reader Initialisation (Programming) */
  165. bool readerInit() const; // `symbol->output_options | READER_INIT`
  166. void setReaderInit(bool readerInit);
  167. /* Whether to add quiet zone indicators ("<", ">") to HRT (EAN/UPC) */
  168. bool guardWhitespace() const; // `symbol->output_options | EANUPC_GUARD_WHITESPACE`
  169. void setGuardWhitespace(bool guardWhitespace);
  170. /* Whether to embed the font in vector output - currently only for SVG output of EAN/UPC */
  171. bool embedVectorFont() const; // `symbol->output_options | EANUPC_GUARD_WHITESPACE`
  172. void setEmbedVectorFont(bool embedVectorFont);
  173. /* Affects error/warning value returned by Zint API (see `getError()` below) */
  174. int warnLevel() const; // `symbol->warn_level`
  175. void setWarnLevel(int warnLevel);
  176. /* Debugging flags */
  177. bool debug() const; // `symbol->debug`
  178. void setDebug(bool debug);
  179. /* Symbol output info set by Zint on successful `render()` */
  180. int encodedWidth() const; // Read-only, encoded width (no. of modules encoded)
  181. int encodedRows() const; // Read-only, no. of rows encoded
  182. float encodedHeight() const; // Read-only, in X-dimensions
  183. float vectorWidth() const; // Read-only, scaled width
  184. float vectorHeight() const; // Read-only, scaled height
  185. /* Legacy property getters/setters */
  186. void setWidth(int width); /* `symbol->option_2` */
  187. int width() const;
  188. void setSecurityLevel(int securityLevel); /* `symbol->option_1` */
  189. int securityLevel() const;
  190. void setPdf417CodeWords(int pdf417CodeWords); /* No-op */
  191. int pdf417CodeWords() const;
  192. void setHideText(bool hide); /* `setShowText(!hide)` */
  193. void setTargetSize(int width, int height); /* No-op */
  194. QString error_message() const; /* Same as `lastError()` */
  195. /* Test capabilities - `ZBarcode_Cap()` */
  196. bool hasHRT(int symbology = 0) const;
  197. bool isStackable(int symbology = 0) const;
  198. bool isEANUPC(int symbology = 0) const;
  199. bool isExtendable(int symbology = 0) const; /* Legacy - same as `isEANUPC()` */
  200. bool isComposite(int symbology = 0) const;
  201. bool supportsECI(int symbology = 0) const;
  202. bool supportsGS1(int symbology = 0) const;
  203. bool isDotty(int symbology = 0) const;
  204. bool hasDefaultQuietZones(int symbology = 0) const;
  205. bool isFixedRatio(int symbology = 0) const;
  206. bool supportsReaderInit(int symbology = 0) const;
  207. bool supportsFullMultibyte(int symbology = 0) const;
  208. bool hasMask(int symbology = 0) const;
  209. bool supportsStructApp(int symbology = 0) const;
  210. bool hasCompliantHeight(int symbology = 0) const;
  211. /* Whether takes GS1 AI-delimited data */
  212. bool takesGS1AIData(int symbology = 0) const;
  213. /* Error or warning returned by Zint on `render()` or `save_to_file()` */
  214. int getError() const;
  215. /* Error message returned by Zint on `render()` or `save_to_file()` */
  216. const QString& lastError() const; // `symbol->errtxt`
  217. /* Whether `lastError()` set */
  218. bool hasErrors() const; // `symbol->errtxt`
  219. /* Encode and print barcode to file `filename`. Only sets `getError()` on error, not on warning */
  220. bool save_to_file(const QString& filename); // `ZBarcode_Print()`
  221. /* Encode and display barcode in `paintRect` using `painter`.
  222. Note: legacy argument `mode` is not used */
  223. void render(QPainter& painter, const QRectF& paintRect, AspectRatioMode mode = IgnoreAspectRatio);
  224. /* Returns the default X-dimension (`ZBarcode_Default_Xdim()`).
  225. If `symbology` non-zero then used instead of `symbol()` */
  226. float defaultXdim(int symbology = 0) const;
  227. /* Returns the scale to use for X-dimension `x_dim_mm` at `dpmm` for `filetype`.
  228. If `symbology` non-zero then used instead of `symbol()` */
  229. float getScaleFromXdimDp(float x_dim_mm, float dpmm, const QString& fileType, int symbology = 0) const;
  230. // `ZBarcode_Scale_Xdim()`
  231. /* Reverse of `getScaleFromXdimDp()` above, returning the X-dimension or dot density given the scale `scale`.
  232. If `symbology` non-zero then used instead of `symbol()` */
  233. float getXdimDpFromScale(float scale, float x_dim_mm_or_dpmm, const QString& fileType, int symbology = 0) const;
  234. // `ZBarcode_XdimDp_From_Scale()`
  235. /* Set `width_x_dim` and `height_x_dim` with estimated size of barcode based on X-dimension `x_dim`. To be called
  236. after a successful `render()`. Returns false if `scale()` zero or render is in error, otherwise true */
  237. bool getWidthHeightXdim(float x_dim, float &width_x_dim, float &height_x_dim) const;
  238. /* Return the BARCODE_XXX name of `symbology` */
  239. static QString barcodeName(const int symbology); // `ZBarcode_BarcodeName()`
  240. /* Whether Zint library "libzint" built with PNG support or not */
  241. static bool noPng(); // `ZBarcode_NoPng()`
  242. /* Version of Zint library "libzint" linked to */
  243. static int getVersion(); // `ZBarcode_Version()`
  244. /* Translate settings into Command Line equivalent. Set `win` to use Windows escaping of data.
  245. If `autoHeight` set then `--height=` option will not be emitted.
  246. If HEIGHTPERROW_MODE set and non-zero `heightPerRow` given then use that for height instead of internal
  247. height */
  248. QString getAsCLI(const bool win, const bool longOptOnly = false, const bool barcodeNames = false,
  249. const bool noEXE = false, const bool autoHeight = false, const float heightPerRow = 0.0f,
  250. const QString& outfile = "", const QZintXdimDpVars *xdimdpVars = nullptr) const;
  251. signals:
  252. void encoded(); // Emitted on successful `render()`
  253. void errored(); // Emitted if an error (not warning) occurred on `render()`
  254. private:
  255. bool resetSymbol(); // Reset the symbol structure for encoding using member fields
  256. void encode(); // `ZBarcode_Encode_and_Buffer_Vector()` or `ZBarcode_Encode_Segs_and_Buffer_Vector()`
  257. /* Helper to convert `m_segs` to `struct zint_seg[]` */
  258. int convertSegs(struct zint_seg segs[], std::vector<QByteArray>& bstrs);
  259. /* Convert `zint_vector_rect->colour` to Qt color */
  260. static Qt::GlobalColor colourToQtColor(int colour);
  261. /* `getAsCLI()` helpers */
  262. static void arg_str(QString& cmd, const char *const opt, const QString& val);
  263. static void arg_int(QString& cmd, const char *const opt, const int val, const bool allowZero = false);
  264. static void arg_bool(QString& cmd, const char *const opt, const bool val);
  265. static void arg_data(QString& cmd, const char *const opt, const QString& val, const bool win);
  266. static void arg_seg(QString& cmd, const int seg_no, const QZintSeg& val, const bool win);
  267. static void arg_data_esc(QString& cmd, const char *const opt, QString& text, const bool win);
  268. static void arg_float(QString& cmd, const char *const opt, const float val, const bool allowZero = false);
  269. static void arg_structapp(QString& cmd, const char *const opt, const int count, const int index,
  270. const QString& id, const bool win);
  271. static void arg_scalexdimdp(QString& cmd, const char *const opt, const float scale, const float dpmm,
  272. const int symbol, const QZintXdimDpVars *xdimdpVars);
  273. private:
  274. zint_symbol *m_zintSymbol;
  275. int m_symbol;
  276. int m_input_mode;
  277. QString m_text;
  278. QString m_primaryMessage;
  279. std::vector<QZintSeg> m_segs;
  280. float m_height;
  281. int m_option_1;
  282. int m_option_2;
  283. int m_option_3;
  284. float m_dpmm;
  285. float m_scale;
  286. bool m_dotty;
  287. float m_dot_size;
  288. float m_guardDescent;
  289. float m_textGap;
  290. struct zint_structapp m_structapp;
  291. QString m_fgStr;
  292. QString m_bgStr;
  293. bool m_cmyk;
  294. int m_borderType;
  295. int m_borderWidth;
  296. int m_whitespace;
  297. int m_vwhitespace;
  298. int m_fontSetting;
  299. bool m_show_hrt;
  300. bool m_gssep;
  301. bool m_quiet_zones;
  302. bool m_no_quiet_zones;
  303. bool m_compliant_height;
  304. int m_rotate_angle;
  305. int m_eci;
  306. bool m_gs1parens;
  307. bool m_gs1nocheck;
  308. bool m_reader_init;
  309. bool m_guard_whitespace;
  310. bool m_embed_vector_font;
  311. int m_warn_level;
  312. bool m_debug;
  313. int m_encodedWidth;
  314. int m_encodedRows;
  315. float m_encodedHeight;
  316. float m_vectorWidth;
  317. float m_vectorHeight;
  318. QString m_lastError;
  319. int m_error;
  320. int target_size_horiz; /* Legacy */
  321. int target_size_vert; /* Legacy */
  322. };
  323. /* Printing Scale settings */
  324. struct QZintXdimDpVars {
  325. double x_dim = 0.0; // X-dimension in `x_dim_units`
  326. int x_dim_units = 0; // 0 for mm, 1 for inches
  327. int resolution = 0; // Dot density in `resolution_units`
  328. int resolution_units = 0; // 0 for dpmm, 1 for dpi
  329. int filetype = 0; // For non-MaxiCode, 0 for GIF (raster), 1 for SVG (vector)
  330. int filetype_maxicode = 0; // For MaxiCode only, 0 for GIF (raster), 1 for SVG (vector), 2 for EMF
  331. int set = 0; // 1 if explicitly set, 0 if just defaults (in which case the struct isn't applicable to `dpmm()`)
  332. /* Helper to return "GIF"/"SVG"(/"EMF") if `msg` false, "raster"/"vector"(/"EMF") otherwise
  333. (EMF only if `symbol` is MaxiCode) */
  334. static const char *getFileType(int symbol, const struct QZintXdimDpVars *vars, bool msg = false);
  335. };
  336. } /* namespace Zint */
  337. /* vim: set ts=4 sw=4 et : */
  338. #endif /* QZINT_H */