Skip to content

Commit 2672182

Browse files
committed
UNITS: qrcode now works up to level 6
1 parent 35d756d commit 2672182

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

units/font1.bas

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
' Original work by [B+=MGA] 2016
33
'
44

5-
Unit Fonts1
5+
Unit Font1
66

77
Export drawtext
88

99
'
1010
' drawtext x, y, message
11-
' x,y = top left of message pixel position on screen
11+
' x,y = top left of message pixel position on screen
1212
' scale - pixel height of character cell, .5 * scale is character width of cell
1313
' message = text to be displayed
1414
'
15-
' Returns whether message was clipped
15+
' Returns whether message was clipped
1616
'
1717
' drawtext requires 2 other subs for drawing thick lines (tl) and arcs (ac)
1818
' the varaibles: thick (line thickness), lx,ly (last x and y) and dr (drawing radius)
1919
' are shared between these 3 subs
20-
' scale for 1x2 cells
20+
' scale for 1x2 cells
2121
'
2222
sub drawtext(x, y, scale, mess)
2323
local i, c, px
@@ -41,12 +41,12 @@ sub drawtext(x, y, scale, mess)
4141
case "d":ac px+p1,py+p3,0,360:lx=px+p2:ly=py:tl 0,p4
4242
case "e":ac px+p1,py+p3,45,360:lx=px:ly=py+p3:tl p2,0
4343
case "f":ac px+p2,py+p1+gf,180,270:lx=px:ly=py+p2:tl p2,0:lx=px+p1:ly=py+p1:tl 0,p3
44-
case "g":ac px+p1,py+p3,0,360:lx=px+p2:ly=py+p2:tl 0,p2:ac px+p1,py+p4,0,90:lx=px:ly=py+p4+p1:tl p1,0
44+
case "g":ac px+p1,py+p3,0,360:lx=px+p2:ly=py+p2:tl 0,p2:ac px+p1,py+p4,0,90:lx=px:ly=py+p4+p1:tl p1,0
4545
case "h":ac px+p1,py+p3,180,360:lx=px:ly=py:tl 0,p4:lx=px+p2:ly=py+p3:tl 0,p1
4646
case "i":lx=px+p1-gf:ly=py+p2:tl gf,0:tl 0,p2:tl -gf,0:tl 2*gf,0
4747
lx=px+p1-gf:ly=py+p1:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf
4848
case "j":lx=px+p1-gf:ly=py+p2:tl gf,0:tl 0,p2:ac px,py+p4,0,120
49-
lx=px+p1-gf:ly=py+p1:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf
49+
lx=px+p1-gf:ly=py+p1:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf
5050
case "k":lx=px:ly=py:tl 0,p4:lx=px+p2:ly=py+p2:tl -p2,p1:tl p2,p1
5151
case "l":lx=px+p1-gf:ly=py:tl gf,0:tl 0,p4:tl gf,0:tl -2*gf,0
5252
case "m":lx=px:ly=py+p2:tl 0,p2:lx=px+p1:ly=py+p4:tl 0,-p2:tl -p1,p1:lx=px+p2:ly=py+p4:tl 0,-p2:tl -p1,p1
@@ -56,42 +56,42 @@ sub drawtext(x, y, scale, mess)
5656
case "q":ac px+p1,py+p3,0,360:lx=px+p2:ly=py+p2-gf:tl 0,p4-2*gf:tl 2*gf,0
5757
case "r":lx=px:ly=py+p2:tl 0,p2:ac px+p1,py+p3,180,340
5858
case "s":dr=1/12*scale:ac px+.5*p1,py+p2+dr,90,270:ac px+1.5*p1,py+p3+dr,270,450:dr=1/6*scale
59-
lx=px+.5*p1:ly=py+p2:tl p1*1.5,0:lx=px:ly=py+p4:tl p1*1.5,0:lx=px+.5*p1:ly=py+p3:tl p1,0
59+
lx=px+.5*p1:ly=py+p2:tl p1*1.5,0:lx=px:ly=py+p4:tl p1*1.5,0:lx=px+.5*p1:ly=py+p3:tl p1,0
6060
case "t":lx=px+p1:ly=py:tl 0,p4:tl 2*gf,0:lx=px:ly=py+p2:tl p2,0
61-
case "u":ac px+p1,py+p3,0,180:lx=px:ly=py+p2:tl 0,p1*1.5:lx=px+p2:ly=py+p2:tl 0,p2+gf
61+
case "u":ac px+p1,py+p3,0,180:lx=px:ly=py+p2:tl 0,p1*1.5:lx=px+p2:ly=py+p2:tl 0,p2+gf
6262
case "v":lx=px:ly=py+p2:tl p1,p2+gf:tl p1,-p2-1.5*gf
63-
case "w":lx=px-gf:ly=py+p2:tl gf+p1*.5,p2:tl .5*p1,-p2:tl p1*.5,p2:tl p1*.5+gf,-p2
64-
case "x":lx=px:ly=py+p2:tl p2,p2:lx=px:ly=py+p4:tl p2,-p2
65-
case "y":lx=px:ly=py+p2:tl p1,p2:lx=px+p2:ly=py+p2:tl -p2,p3+gf
66-
case "z":lx=px:ly=py+p2:tl p2,0:tl -p2,p2:tl p2,0
63+
case "w":lx=px-gf:ly=py+p2:tl gf+p1*.5,p2:tl .5*p1,-p2:tl p1*.5,p2:tl p1*.5+gf,-p2
64+
case "x":lx=px:ly=py+p2:tl p2,p2:lx=px:ly=py+p4:tl p2,-p2
65+
case "y":lx=px:ly=py+p2:tl p1,p2:lx=px+p2:ly=py+p2:tl -p2,p3+gf
66+
case "z":lx=px:ly=py+p2:tl p2,0:tl -p2,p2:tl p2,0
6767
case "A":lx=px:ly=py+p4:tl p1-gf,-p4:tl 2*gf,0:tl p1-gf,p4:lx=px+p1-2*gf:ly=py+p2:tl p1,0
6868
case "B":ac px+p1,py+p1,270,450:ac px+p1,py+p3,270,450
69-
lx=px:ly=py:tl 0,p4:tl p1,0:lx=px:ly=py+p2:tl p1,0:lx=px:ly=py:tl p1,0
70-
case "C":ac px+p1,py+p1,180,320:ac px+p1,py+p3,40,180:lx=px:ly=py+p1:tl 0,p2
69+
lx=px:ly=py:tl 0,p4:tl p1,0:lx=px:ly=py+p2:tl p1,0:lx=px:ly=py:tl p1,0
70+
case "C":ac px+p1,py+p1,180,320:ac px+p1,py+p3,40,180:lx=px:ly=py+p1:tl 0,p2
7171
case "D":ac px+p1,py+p1,270,360:ac px+p1,py+p3,0,90
7272
lx=px+p1:ly=py:tl -p1,0:tl 0,p4:tl p1,0:lx=px+p2:ly=py+p1:tl 0,p2
7373
case "E":lx=px+p2:ly=py:tl -p2,0:tl 0,p4:tl p2,0:lx=px:ly=py+p2:tl p1+2*gf,0
7474
case "F":lx=px+p2:ly=py:tl -p2,0:tl 0,p4:lx=px:ly=py+p2:tl p1+2*gf,0
7575
case "G":ac px+p1,py+p1,180,345:ac px+p1,py+p3,0,180
76-
lx=px:ly=py+p1:tl 0,p2:lx=px+p2:ly=py+p4+gf:tl 0,-p2-gf:tl -p1,0
77-
case "H":lx=px:ly=py:tl 0,p4:tl 0,-p2:tl p2,0:tl 0,p2:tl 0,-p4
78-
case "I":lx=px+p1*.5:ly=py:tl p1,0:lx=px+p1*.5:ly=py+p4:tl p1,0:lx=px+p1:ly=py:tl 0,p4
79-
case "J":ac px+p1,py+p3,0,180:lx=px+p1*1.5:ly=py:tl p1*.5,0:tl 0,p3
76+
lx=px:ly=py+p1:tl 0,p2:lx=px+p2:ly=py+p4+gf:tl 0,-p2-gf:tl -p1,0
77+
case "H":lx=px:ly=py:tl 0,p4:tl 0,-p2:tl p2,0:tl 0,p2:tl 0,-p4
78+
case "I":lx=px+p1*.5:ly=py:tl p1,0:lx=px+p1*.5:ly=py+p4:tl p1,0:lx=px+p1:ly=py:tl 0,p4
79+
case "J":ac px+p1,py+p3,0,180:lx=px+p1*1.5:ly=py:tl p1*.5,0:tl 0,p3
8080
case "K":lx=px:ly=py:tl 0,p4:lx=px+p2:ly=py:tl -p2,p2:tl p2,p2
8181
case "L":lx=px:ly=py:tl 0,p4:tl p2,0
8282
case "M":lx=px:ly=py+p4:tl 0,-p4:tl p1,p2:tl p1,-p2:tl 0,p4
8383
case "N":lx=px:ly=py+p4:tl 0,-p4:tl p2,p4:tl 0,-p4
8484
case "O":ac px+p1,py+p1,180,360:ac px+p1,py+p3,0,180:lx=px:ly=py+p1:tl 0,p2:lx=px+p2:ly=py+p1:tl 0,p2
8585
case "P":ac px+p1,py+p1,270,450:lx=px+p1:ly=py:tl -p1,0:tl 0,p4:lx=px:ly=py+p2:tl p1,0
8686
case "Q":ac px+p1,py+p1,180,360:ac px+p1,py+p3,0,180
87-
lx=px:ly=py+p1:tl 0,p2:lx=px+p2:ly=py+p1:tl 0,p2:lx=px+p1:ly=py+p2:tl p1+gf,p2+gf
88-
case "R":ac px+p1,py+p1,270,450:lx=px+p1:ly=py:tl -p1,0:tl 0,p4:lx=px:ly=py+p2:tl p1,0:tl p1,p2
87+
lx=px:ly=py+p1:tl 0,p2:lx=px+p2:ly=py+p1:tl 0,p2:lx=px+p1:ly=py+p2:tl p1+gf,p2+gf
88+
case "R":ac px+p1,py+p1,270,450:lx=px+p1:ly=py:tl -p1,0:tl 0,p4:lx=px:ly=py+p2:tl p1,0:tl p1,p2
8989
case "S":ac px+p1,py+p1,90,360:ac px+p1,py+p3,270,540
9090
case "T":lx=px:ly=py:tl p2,0:lx=px+p1:ly=py:tl 0,p4
91-
case "U":ac px+p1,py+p3,0,180:lx=px:ly=py:tl 0,p3:lx=px+p2:ly=py:tl 0,p3
91+
case "U":ac px+p1,py+p3,0,180:lx=px:ly=py:tl 0,p3:lx=px+p2:ly=py:tl 0,p3
9292
case "V":lx=px:ly=py:tl p1,p4:tl p1,-p4
93-
case "W":lx=px-gf:ly=py:tl gf,p4:tl p1,-p2:tl p1,p2:tl gf,-p4
94-
case "X":lx=px:ly=py:tl p2,p4:lx=px+p2:ly=py:tl -p2,p4
93+
case "W":lx=px-gf:ly=py:tl gf,p4:tl p1,-p2:tl p1,p2:tl gf,-p4
94+
case "X":lx=px:ly=py:tl p2,p4:lx=px+p2:ly=py:tl -p2,p4
9595
case "Y":lx=px:ly=py:tl p1,p2:tl 0,p2:lx=px+p2:ly=py:tl -p1,p2
9696
case "Z":lx=px:ly=py:tl p2,0:tl -p2,p4:tl p2,0
9797
case "0":ac px+p1,py+p1,180,360:ac px+p1,py+p3,0,180:lx=px:ly=py+p1:tl 0,p2:tl p2,-p2:tl 0,p2
@@ -106,7 +106,7 @@ sub drawtext(x, y, scale, mess)
106106
case "9":ac px+p1,py+p1,0,360:lx=px+p2:ly=py+p1:tl -p1,p3
107107
case ".":lx=px:ly=py+p4:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf
108108
case "!":lx=px:ly=py+p4:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf::lx=px:ly=py:tl 0,p2
109-
case ",":lx=px+gf:ly=py+p4:tl -gf,0:tl 0,-gf:tl gf,0:tl 0,gf:tl -gf,.5*p1+gf
109+
case ",":lx=px+gf:ly=py+p4:tl -gf,0:tl 0,-gf:tl gf,0:tl 0,gf:tl -gf,.5*p1+gf
110110
case "?":lx=px+p1:ly=py+p4:tl gf,0:tl 0,-gf:tl -gf,0:tl 0,gf:ac px+p1,py+p1,180,450:lx=px+p1:ly=py+p2:tl 0,p1-gf
111111
end select
112112
next
@@ -123,12 +123,12 @@ sub ac(x, y, das, dae)
123123
x1=dr*cos(rad(a)) : y1=dr*sin(rad(a))
124124
if int(thick)<1 then pset x+x1,y+y1 else circle x+x1,y+y1,thick filled
125125
next
126-
fi
126+
fi
127127
end
128128

129129
'tl stands for thick line in the LINE STEP x,y format
130-
sub tl(stepx,stepy) 'tl=thickline
131-
'lastx, lasty globals for last drawn position
130+
sub tl(stepx,stepy) 'tl=thickline
131+
'lastx, lasty globals for last drawn position
132132
'thick has to be global
133133
'note thick=0 still draws a line, use COLOR so line is drawn from this global
134134
local length,dx,dy,i

units/font2.bas

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
REM
22
REM ROUTINE TO DRAW CHARACTERS WITH DRAW COMMANDREM By Miguel A. MAroto 2019 (from routines of TurboBasic of 2001)
3-
REM
3+
REM
44
REM The text are in variable letters
55
REM xc1 and yc1 are the initial position of the text
66
REM Escala is the scale of the text (NO OPERATIVE NOW !!!!!!!)
@@ -13,7 +13,7 @@ REM DEFINITION OF THE MOVEMENTS NEEDED TO DRAW EACH CHARACTER WITH THE DRAW CO
1313
REM the number of each chararter in the array car(n) is the same of the ASCII code
1414
REM
1515

16-
Unit Fonts2
16+
Unit Font2
1717

1818
Export drawtext
1919

@@ -77,34 +77,34 @@ separator = .4
7777
sub _draw(s)
7878
if (s != "0") then
7979
draw(s)
80-
endif
80+
endif
8181
end
8282

8383
sub drawtext(xc, yc, scale, bold, text)
8484
REM set the initial point for the drawing
8585
local S, position, moverse, bit
86-
86+
8787
for S = 1 to len(text)
8888
REM Set the initial position that will be moved to the right for each character
8989
position ="bm" + str(xc) + "," + str(yc)
9090
_draw(position)
9191

9292
REM Extract each character one by one and I draw it
9393
bit = mid(text, S, 1)
94-
94+
9595
REM Draw the first stroke of the character
9696
_draw(car[asc(bit)])
97-
97+
9898
REM Go back the width of a character plus 1 (with scale 1:1)
9999
xc += 1
100100
REM origin of the characters plus 1
101101

102-
if (bold) then
102+
if (bold) then
103103
moverse="bm" + str(xc) + "," + str(yc)
104104
_draw(moverse)
105105
_draw(car[asc(bit)])
106106
endif
107-
107+
108108
xc = int(xc + 1 + separator * (scale / 4))
109109
next S
110110
end

units/qrcode.bas

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ const qrcodegen_Mask_7 = 7
8282
const INT16_MAX = maxint / 2
8383

8484
sub assert(t, l)
85-
if (!t) then throw "Assertion failed at line: " + l
85+
if (!t) then throw "Assertion failed at line:" + l
86+
end
87+
88+
sub assertEq(a, b, l)
89+
if (a != b) then throw "Assertion failed at line:" + l + " expected:" + a + " actual:" + b
8690
end
8791

8892
def qrcodegen_BUFFER_LEN_FOR_VERSION(n) = ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8 + 1)
@@ -294,7 +298,7 @@ func qrcodegen_encodeSegmentsAdvanced(byref segs, lenSegs, ecl, minVersion, maxV
294298
next j
295299
next i
296300

297-
assert(bitLen == dataUsedBits, PROGLINE)
301+
assertEq(bitLen, dataUsedBits, PROGLINE)
298302

299303
' Add terminator and pad up to a byte if applicable
300304
local dataCapacityBits = getNumDataCodewords(version, ecl) * 8
@@ -303,7 +307,7 @@ func qrcodegen_encodeSegmentsAdvanced(byref segs, lenSegs, ecl, minVersion, maxV
303307
if (terminatorBits > 4) then terminatorBits = 4
304308
bitLen = appendBitsToBuffer(0, terminatorBits, qrcode, bitLen)
305309
bitLen = appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, bitLen)
306-
assert(bitLen % 8 == 0, PROGLINE)
310+
assertEq(bitLen % 8, 0, PROGLINE)
307311

308312
' Pad with alternating bytes until data capacity is reached
309313
local padByte = 0xEC
@@ -511,6 +515,7 @@ sub initializeFunctionModules(version, byref qrcode)
511515
fillRectangle(0, qrsize - 8, 9, 8, qrcode)
512516

513517
' Fill numerous alignment patterns
518+
local alignPatPos
514519
dim alignPatPos(7)
515520
local numAlign = getAlignmentPatternPositions(version, alignPatPos)
516521
local i, j
@@ -585,7 +590,7 @@ sub drawWhiteFunctionModules(byref qrcode, version)
585590
vrem = (vrem lshift 1) xor ((vrem rshift 11) * 0x1F25)
586591
next i
587592
local bits = version lshift 12 | vrem ' uint18
588-
assert(bits rshift 18 == 0, PROGLINE)
593+
assertEq(bits rshift 18, 0, PROGLINE)
589594

590595
' Draw two copies
591596
for i = 0 to 5
@@ -616,7 +621,7 @@ sub drawFormatBits(ecl, mask, byref qrcode)
616621
qrem = (qrem lshift 1) xor ((qrem rshift 9) * 0x537)
617622
next i
618623
local bits = (qdata lshift 10 | qrem) xor 0x5412 ' uint15
619-
assert(bits rshift 15 == 0, PROGLINE)
624+
assertEq(bits rshift 15, 0, PROGLINE)
620625

621626
' Draw first copy
622627
for i = 0 to 5
@@ -705,7 +710,7 @@ sub drawCodewords(byref _data, dataLen, byref qrcode)
705710
next j
706711
next vert
707712
next xright
708-
assert(i == dataLen * 8, PROGLINE)
713+
assertEq(i, dataLen * 8, PROGLINE)
709714
end
710715

711716
REM
@@ -735,7 +740,7 @@ sub applyMask(byref functionModules, byref qrcode, mask)
735740
case else assert(false, PROGLINE)
736741
end select
737742
_val = getModule(qrcode, x, y)
738-
setModule(qrcode, x, y, _val xor invert)
743+
setModule(qrcode, x, y, _val xor int(invert))
739744
endif
740745
next x
741746
next y
@@ -818,7 +823,7 @@ func getPenaltyScore(byref qrcode)
818823

819824
local total = qrsize * qrsize ' Note that size is odd, so black/total != 1/2
820825
' Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
821-
local k = ((abs(black * 20 - total * 10) + total - 1) / total) - 1
826+
local k = int((abs(black * 20 - total * 10) + total - 1) / total) - 1
822827
result += k * PENALTY_N4
823828
return result
824829
end
@@ -976,7 +981,7 @@ func qrcodegen_calcSegmentBufferSize(mode, numChars)
976981
local temp = calcSegmentBitLength(mode, numChars)
977982
if (temp == -1) then return maxint
978983
assert(0 <= temp && temp <= maxint, PROGLINE)
979-
return (temp + 7) / 8
984+
return int((temp + 7) / 8)
980985
end
981986

982987
REM
@@ -1071,7 +1076,7 @@ func qrcodegen_makeNumeric(digits, byref buf)
10711076
' 1 or 2 digits remaining
10721077
result.bitLength = appendBitsToBuffer(accumData, accumCount * 3 + 1, buf, result.bitLength)
10731078
endif
1074-
assert(result.bitLength == bitLen, PROGLINE)
1079+
assertEq(result.bitLength, bitLen, PROGLINE)
10751080
result._data = buf
10761081
return result
10771082
end
@@ -1118,7 +1123,7 @@ func qrcodegen_makeAlphanumeric(text, byref buf)
11181123
result.bitLength = appendBitsToBuffer(accumData, 6, buf, result.bitLength)
11191124
endif
11201125

1121-
assert(result.bitLength == round(bitLen), PROGLINE)
1126+
assertEq(result.bitLength, bitLen, PROGLINE)
11221127
result._data = buf
11231128
return result
11241129
end
@@ -1185,7 +1190,7 @@ REM in a QR Code at the given version number. The result is in the range [0, 16]
11851190
REM
11861191
func numCharCountBits(mode, version)
11871192
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX, PROGLINE)
1188-
local i = (version + 7) / 17
1193+
local i = int((version + 7) / 17)
11891194
local m
11901195
select case mode
11911196
case qrcodegen_Mode_NUMERIC : m = [10, 12, 14]: return m[i]

0 commit comments

Comments
 (0)