BigBlueBox
An Inventory Management System for a NYLT Course or other Boy Scout Programs
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
QrSegment.h
Go to the documentation of this file.
1 /*
2  * QR Code generator library (C++)
3  *
4  * Copyright (c) Project Nayuki. (MIT License)
5  * https://www.nayuki.io/page/qr-code-generator-library
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  * - The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  * - The Software is provided "as is", without warranty of any kind, express or
16  * implied, including but not limited to the warranties of merchantability,
17  * fitness for a particular purpose and noninfringement. In no event shall the
18  * authors or copyright holders be liable for any claim, damages or other
19  * liability, whether in an action of contract, tort or otherwise, arising from,
20  * out of or in connection with the Software or the use or other dealings in the
21  * Software.
22  */
23 
24 #pragma once
25 
26 #include <cstdint>
27 #include <vector>
28 #include "qr/BitBuffer.h"
29 
30 
31 namespace qrcodegen {
32 
33 /*
34  * Represents a character string to be encoded in a QR Code symbol. Each segment has
35  * a mode, and a sequence of characters that is already encoded as a sequence of bits.
36  * Instances of this class are immutable.
37  * This segment class imposes no length restrictions, but QR Codes have restrictions.
38  * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
39  * Any segment longer than this is meaningless for the purpose of generating QR Codes.
40  */
41 class QrSegment final {
42 
43  /*---- Public helper enumeration ----*/
44 
45  /*
46  * The mode field of a segment. Immutable. Provides methods to retrieve closely related values.
47  */
48  public: class Mode final {
49 
50  /*-- Constants --*/
51 
52  public: static const Mode NUMERIC;
53  public: static const Mode ALPHANUMERIC;
54  public: static const Mode BYTE;
55  public: static const Mode KANJI;
56  public: static const Mode ECI;
57 
58 
59  /*-- Fields --*/
60 
61  private: int modeBits;
62 
63  private: int numBitsCharCount[3];
64 
65 
66  /*-- Constructor --*/
67 
68  private: Mode(int mode, int cc0, int cc1, int cc2);
69 
70 
71  /*-- Methods --*/
72 
73  /*
74  * (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0 to 15).
75  */
76  public: int getModeBits() const;
77 
78  /*
79  * (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number.
80  */
81  public: int numCharCountBits(int ver) const;
82 
83  };
84 
85 
86 
87  /*---- Public static factory functions ----*/
88 
89  /*
90  * Returns a segment representing the given binary data encoded in byte mode.
91  */
92  public: static QrSegment makeBytes(const std::vector<std::uint8_t> &data);
93 
94 
95  /*
96  * Returns a segment representing the given string of decimal digits encoded in numeric mode.
97  */
98  public: static QrSegment makeNumeric(const char *digits);
99 
100 
101  /*
102  * Returns a segment representing the given text string encoded in alphanumeric mode.
103  * The characters allowed are: 0 to 9, A to Z (uppercase only), space,
104  * dollar, percent, asterisk, plus, hyphen, period, slash, colon.
105  */
106  public: static QrSegment makeAlphanumeric(const char *text);
107 
108 
109  /*
110  * Returns a list of zero or more segments to represent the given text string.
111  * The result may use various segment modes and switch modes to optimize the length of the bit stream.
112  */
113  public: static std::vector<QrSegment> makeSegments(const char *text);
114 
115 
116  /*
117  * Returns a segment representing an Extended Channel Interpretation
118  * (ECI) designator with the given assignment value.
119  */
120  public: static QrSegment makeEci(long assignVal);
121 
122 
123  /*---- Public static helper functions ----*/
124 
125  /*
126  * Tests whether the given string can be encoded as a segment in alphanumeric mode.
127  */
128  public: static bool isAlphanumeric(const char *text);
129 
130 
131  /*
132  * Tests whether the given string can be encoded as a segment in numeric mode.
133  */
134  public: static bool isNumeric(const char *text);
135 
136 
137 
138  /*---- Instance fields ----*/
139 
140  /* The mode indicator for this segment. */
141  private: Mode mode;
142 
143  /* The length of this segment's unencoded data, measured in characters. Always zero or positive. */
144  private: int numChars;
145 
146  /* The data bits of this segment. */
147  private: std::vector<bool> data;
148 
149 
150  /*---- Constructor ----*/
151 
152  /*
153  * Creates a new QR Code data segment with the given parameters and data.
154  */
155  public: QrSegment(Mode md, int numCh, const std::vector<bool> &dt);
156 
157 
158  /*
159  * Creates a new QR Code data segment with the given parameters and data.
160  */
161  public: QrSegment(Mode md, int numCh, std::vector<bool> &&dt);
162 
163 
164  /*---- Methods ----*/
165 
166  public: Mode getMode() const;
167 
168 
169  public: int getNumChars() const;
170 
171 
172  public: const std::vector<bool> &getData() const;
173 
174 
175  // Package-private helper function.
176  public: static int getTotalBits(const std::vector<QrSegment> &segs, int version);
177 
178 
179  /*---- Private constant ----*/
180 
181  /* The set of all legal characters in alphanumeric mode, where each character value maps to the index in the string. */
182  private: static const char *ALPHANUMERIC_CHARSET;
183 
184 };
185 
186 }
Mode getMode() const
Definition: QrSegment.cpp:211
static const Mode ALPHANUMERIC
Definition: QrSegment.h:53
static int getTotalBits(const std::vector< QrSegment > &segs, int version)
Definition: QrSegment.cpp:172
const std::vector< bool > & getData() const
Definition: QrSegment.cpp:221
static QrSegment makeNumeric(const char *digits)
Definition: QrSegment.cpp:74
static QrSegment makeEci(long assignVal)
Definition: QrSegment.cpp:138
static const Mode ECI
Definition: QrSegment.h:56
Definition: QrSegment.h:48
int modeBits
Definition: QrSegment.h:61
int getModeBits() const
Definition: QrSegment.cpp:43
static const char * ALPHANUMERIC_CHARSET
Definition: QrSegment.h:182
int numBitsCharCount[3]
Definition: QrSegment.h:63
static const Mode BYTE
Definition: QrSegment.h:54
static const Mode KANJI
Definition: QrSegment.h:55
static QrSegment makeBytes(const std::vector< std::uint8_t > &data)
Definition: QrSegment.cpp:64
Definition: QrSegment.h:41
QrSegment(Mode md, int numCh, const std::vector< bool > &dt)
Definition: QrSegment.cpp:154
static bool isAlphanumeric(const char *text)
Definition: QrSegment.cpp:192
static bool isNumeric(const char *text)
Definition: QrSegment.cpp:201
static std::vector< QrSegment > makeSegments(const char *text)
Definition: QrSegment.cpp:120
int numChars
Definition: QrSegment.h:144
std::vector< bool > data
Definition: QrSegment.h:147
Mode mode
Definition: QrSegment.h:141
static QrSegment makeAlphanumeric(const char *text)
Definition: QrSegment.cpp:97
int numCharCountBits(int ver) const
Definition: QrSegment.cpp:48
static const Mode NUMERIC
Definition: QrSegment.h:52
Mode(int mode, int cc0, int cc1, int cc2)
Definition: QrSegment.cpp:35
int getNumChars() const
Definition: QrSegment.cpp:216