forked from yas78/QRCodeLibVBA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbols.cls
More file actions
511 lines (412 loc) · 13.2 KB
/
Copy pathSymbols.cls
File metadata and controls
511 lines (412 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Symbols"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Private m_items As Collection
Private m_minVersion As Long
Private m_maxVersion As Long
Private m_errorCorrectionLevel As ErrorCorrectionLevel
Private m_structuredAppend As Boolean
Private m_encoding As Encoding
Private m_parity As Long
Private m_currSymbol As Symbol
Private m_encNum As IQRCodeEncoder
Private m_encAlpha As IQRCodeEncoder
Private m_encByte As IQRCodeEncoder
Private m_encKanji As IQRCodeEncoder
Private Sub Class_Initialize()
Set m_encNum = New NumericEncoder
Set m_encAlpha = New AlphanumericEncoder
End Sub
Friend Sub Init(ByVal ecLevel As ErrorCorrectionLevel, _
ByVal minVer As Long, _
ByVal maxVer As Long, _
ByVal allowStructuredAppend As Boolean, _
ByVal charEncoding As Encoding)
Set m_items = New Collection
m_minVersion = minVer
m_maxVersion = maxVer
m_errorCorrectionLevel = ecLevel
m_structuredAppend = allowStructuredAppend
Set m_encoding = charEncoding
m_parity = 0
If Charset.IsJP(charEncoding.Charset) Then
Set m_encKanji = New KanjiEncoder
Call m_encKanji.Init(charEncoding)
End If
Set m_encByte = New ByteEncoder
Call m_encByte.Init(charEncoding)
Set m_currSymbol = New Symbol
Call m_currSymbol.Init(Me)
Call m_items.Add(m_currSymbol)
End Sub
Public Property Get Item(ByVal idx As Long) As Symbol
Attribute Item.VB_UserMemId = 0
' Default Member [Attribute Item.VB_UserMemId = 0]
Set Item = m_items(idx + 1)
End Property
Public Property Get Count() As Long
Count = m_items.Count
End Property
Friend Property Get StructuredAppend() As Boolean
StructuredAppend = m_structuredAppend
End Property
Friend Property Get Parity() As Long
Parity = m_parity
End Property
Friend Property Get MinVersion() As Long
MinVersion = m_minVersion
End Property
Friend Property Let MinVersion(ByVal Value As Long)
m_minVersion = Value
End Property
Friend Property Get MaxVersion() As Long
MaxVersion = m_maxVersion
End Property
Friend Property Get ErrorCorrectionLevel() As ErrorCorrectionLevel
ErrorCorrectionLevel = m_errorCorrectionLevel
End Property
Friend Property Get Encoding() As Encoding
Set Encoding = m_encoding
End Property
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
' [Attribute NewEnum.VB_UserMemId = -4]
Set NewEnum = m_items.[_NewEnum]
End Property
Private Function Add() As Symbol
Set m_currSymbol = New Symbol
Call m_currSymbol.Init(Me)
Call m_items.Add(m_currSymbol)
Set Add = m_currSymbol
End Function
Public Sub AppendText(ByVal s As String)
If Len(s) = 0 Then Call Err.Raise(5)
Dim oldMode As EncodingMode
Dim newMode As EncodingMode
Dim c As String
Dim i As Long
For i = 1 To Len(s)
oldMode = m_currSymbol.CurrentEncodingMode
Select Case oldMode
Case EncodingMode.UNKNOWN
newMode = SelectInitialMode(s, i)
Case EncodingMode.NUMERIC
newMode = SelectModeWhileInNumeric(s, i)
Case EncodingMode.ALPHA_NUMERIC
newMode = SelectModeWhileInAlphanumeric(s, i)
Case EncodingMode.EIGHT_BIT_BYTE
newMode = SelectModeWhileInByte(s, i)
Case EncodingMode.KANJI
newMode = SelectInitialMode(s, i)
Case Else
Call Err.Raise(51)
End Select
c = Mid$(s, i, 1)
If newMode <> oldMode Then
If Not m_currSymbol.TrySetEncodingMode(newMode, c) Then
If Not m_structuredAppend Or m_items.Count = 16 Then
Call Err.Raise(6, Description:="String too long")
End If
Call Add
newMode = SelectInitialMode(s, i)
Call m_currSymbol.TrySetEncodingMode(newMode, c)
End If
End If
If Not m_currSymbol.TryAppend(c) Then
If Not m_structuredAppend Or m_items.Count = 16 Then
Call Err.Raise(6, Description:="String too long")
End If
Call Add
newMode = SelectInitialMode(s, i)
Call m_currSymbol.TrySetEncodingMode(newMode, c)
Call m_currSymbol.TryAppend(c)
End If
Next
End Sub
Friend Sub UpdateParity(ByVal c As String)
Dim charBytes() As Byte
charBytes = m_encoding.GetBytes(c)
Dim i As Long
For i = 0 To UBound(charBytes)
m_parity = m_parity Xor charBytes(i)
Next
End Sub
Private Function SelectInitialMode( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
Dim c As String
c = Mid$(s, startIndex, 1)
If Not (m_encKanji Is Nothing) Then
If m_encKanji.InSubset(c) Then
SelectInitialMode = EncodingMode.KANJI
Exit Function
End If
End If
If m_encByte.InExclusiveSubset(c) Then
SelectInitialMode = EncodingMode.EIGHT_BIT_BYTE
Exit Function
End If
If m_encAlpha.InExclusiveSubset(c) Then
SelectInitialMode = SelectModeWhenInitialDataAlphaNumeric(s, startIndex)
Exit Function
End If
If m_encNum.InSubset(c) Then
SelectInitialMode = SelectModeWhenInitialDataNumeric(s, startIndex)
Exit Function
End If
Call Err.Raise(51)
End Function
Private Function SelectModeWhenInitialDataAlphaNumeric( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
Dim cnt As Long
cnt = 0
Dim i As Long
For i = startIndex To Len(s)
If m_encAlpha.InExclusiveSubset(Mid$(s, i, 1)) Then
cnt = cnt + 1
Else
Exit For
End If
Next
Dim flg As Boolean
Select Case m_currSymbol.Version
Case 1 To 9
flg = cnt < 6
Case 10 To 26
flg = cnt < 7
Case 27 To 40
flg = cnt < 8
Case Else
Call Err.Raise(51)
End Select
If flg Then
If (startIndex + cnt) <= Len(s) Then
If m_encByte.InSubset(Mid$(s, startIndex + cnt, 1)) Then
SelectModeWhenInitialDataAlphaNumeric = EncodingMode.EIGHT_BIT_BYTE
Exit Function
End If
End If
End If
SelectModeWhenInitialDataAlphaNumeric = EncodingMode.ALPHA_NUMERIC
End Function
Private Function SelectModeWhenInitialDataNumeric( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
Dim cnt As Long
cnt = 0
Dim i As Long
For i = startIndex To Len(s)
If m_encNum.InSubset(Mid$(s, i, 1)) Then
cnt = cnt + 1
Else
Exit For
End If
Next
Dim flg As Boolean
Select Case m_currSymbol.Version
Case 1 To 9
flg = cnt < 4
Case 10 To 26
flg = cnt < 4
Case 27 To 40
flg = cnt < 5
Case Else
Call Err.Raise(51)
End Select
If flg Then
If (startIndex + cnt) <= Len(s) Then
If m_encByte.InExclusiveSubset(Mid$(s, startIndex + cnt, 1)) Then
SelectModeWhenInitialDataNumeric = EncodingMode.EIGHT_BIT_BYTE
Exit Function
End If
End If
End If
Select Case m_currSymbol.Version
Case 1 To 9
flg = cnt < 7
Case 10 To 26
flg = cnt < 8
Case 27 To 40
flg = cnt < 9
Case Else
Call Err.Raise(51)
End Select
If flg Then
If (startIndex + cnt) <= Len(s) Then
If m_encAlpha.InExclusiveSubset(Mid$(s, startIndex + cnt, 1)) Then
SelectModeWhenInitialDataNumeric = EncodingMode.ALPHA_NUMERIC
Exit Function
End If
End If
End If
SelectModeWhenInitialDataNumeric = EncodingMode.NUMERIC
End Function
Private Function SelectModeWhileInNumeric( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
Dim c As String
c = Mid$(s, startIndex, 1)
If Not (m_encKanji Is Nothing) Then
If m_encKanji.InSubset(c) Then
SelectModeWhileInNumeric = EncodingMode.KANJI
Exit Function
End If
End If
If m_encByte.InExclusiveSubset(c) Then
SelectModeWhileInNumeric = EncodingMode.EIGHT_BIT_BYTE
Exit Function
End If
If m_encAlpha.InExclusiveSubset(c) Then
SelectModeWhileInNumeric = EncodingMode.ALPHA_NUMERIC
Exit Function
End If
SelectModeWhileInNumeric = EncodingMode.NUMERIC
End Function
Private Function SelectModeWhileInAlphanumeric( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
Dim c As String
c = Mid$(s, startIndex, 1)
If Not (m_encKanji Is Nothing) Then
If m_encKanji.InSubset(c) Then
SelectModeWhileInAlphanumeric = EncodingMode.KANJI
Exit Function
End If
End If
If m_encByte.InExclusiveSubset(c) Then
SelectModeWhileInAlphanumeric = EncodingMode.EIGHT_BIT_BYTE
Exit Function
End If
If ShouldChangeAlphanumericToNumeric(s, startIndex) Then
SelectModeWhileInAlphanumeric = EncodingMode.NUMERIC
Exit Function
End If
SelectModeWhileInAlphanumeric = EncodingMode.ALPHA_NUMERIC
End Function
Private Function ShouldChangeAlphanumericToNumeric( _
ByRef s As String, ByVal startIndex As Long) As Boolean
Dim ret As Boolean
ret = False
Dim cnt As Long
cnt = 0
Dim c As String
Dim i As Long
For i = startIndex To Len(s)
c = Mid$(s, i, 1)
If Not m_encAlpha.InSubset(c) Then
Exit For
End If
If m_encNum.InSubset(c) Then
cnt = cnt + 1
Else
ret = True
Exit For
End If
Next
If ret Then
Select Case m_currSymbol.Version
Case 1 To 9
ret = cnt >= 13
Case 10 To 26
ret = cnt >= 15
Case 27 To 40
ret = cnt >= 17
Case Else
Call Err.Raise(51)
End Select
End If
ShouldChangeAlphanumericToNumeric = ret
End Function
Private Function SelectModeWhileInByte( _
ByRef s As String, ByVal startIndex As Long) As EncodingMode
If Not (m_encKanji Is Nothing) Then
If m_encKanji.InSubset(Mid$(s, startIndex, 1)) Then
SelectModeWhileInByte = EncodingMode.KANJI
Exit Function
End If
End If
If ShouldChangeByteToNumeric(s, startIndex) Then
SelectModeWhileInByte = EncodingMode.NUMERIC
Exit Function
End If
If ShouldChangeByteToAlphanumeric(s, startIndex) Then
SelectModeWhileInByte = EncodingMode.ALPHA_NUMERIC
Exit Function
End If
SelectModeWhileInByte = EncodingMode.EIGHT_BIT_BYTE
End Function
Private Function ShouldChangeByteToNumeric( _
ByRef s As String, ByVal startIndex As Long) As Boolean
Dim ret As Boolean
ret = False
Dim cnt As Long
cnt = 0
Dim c As String
Dim i As Long
For i = startIndex To Len(s)
c = Mid$(s, i, 1)
If Not m_encByte.InSubset(c) Then
Exit For
End If
If m_encNum.InSubset(c) Then
cnt = cnt + 1
ElseIf m_encByte.InExclusiveSubset(c) Then
ret = True
Exit For
Else
Exit For
End If
Next
If ret Then
Select Case m_currSymbol.Version
Case 1 To 9
ret = cnt >= 6
Case 10 To 26
ret = cnt >= 8
Case 27 To 40
ret = cnt >= 9
Case Else
Call Err.Raise(51)
End Select
End If
ShouldChangeByteToNumeric = ret
End Function
Private Function ShouldChangeByteToAlphanumeric( _
ByRef s As String, ByVal startIndex As Long) As Boolean
Dim ret As Boolean
ret = False
Dim cnt As Long
cnt = 0
Dim c As String
Dim i As Long
For i = startIndex To Len(s)
c = Mid$(s, i, 1)
If Not m_encByte.InSubset(c) Then
Exit For
End If
If m_encAlpha.InExclusiveSubset(c) Then
cnt = cnt + 1
ElseIf m_encByte.InExclusiveSubset(c) Then
ret = True
Exit For
Else
Exit For
End If
Next
If ret Then
Select Case m_currSymbol.Version
Case 1 To 9
ret = cnt >= 11
Case 10 To 26
ret = cnt >= 15
Case 27 To 40
ret = cnt >= 16
Case Else
Call Err.Raise(51)
End Select
End If
ShouldChangeByteToAlphanumeric = ret
End Function