Move to own folder

This commit is contained in:
Brahim Hadriche committed 2022-11-03 19:00:11 -04:00
1 parent 94192446af
commit d13976fd83
27 files changed
+5 -3

No files matched your search

File renamed without changes.
File renamed without changes.
+142
View File
@@ -0,0 +1,142 @@
sub init()
m.top.observeFieldScoped("qrcode", "onQRCodeChanged", ["width", "height", "loadWidth", "loadHeight", "loadPadding"])
m.top.observeFieldScoped("text", "onTextChanged")
end sub
' @private
sub onQRCodeChanged(msg as object)
qrcode = msg.getData()
if qrcode <> invalid
if qrcode.status = "ready"
toPNG(qrcode, msg.getInfo())
end if
else
m.top.uri = ""
end if
end sub
' @private
sub onTextChanged(msg as object)
qrcode = createObject("roSGNode", "QRCode")
qrcode.callFunc("addData", msg.getData())
qrcode.callFunc("make")
m.top.qrcode = qrcode
end sub
' @private
function toPNG(qrcode as object, params as object) as string
if params = invalid or params.loadWidth = invalid or params.loadHeight = invalid
m.top.loadStatus = "failed"
return ""
end if
padding = params.loadPadding
if padding = invalid
padding = 0
end if
' Calculate sizes and coords
moduleCount = qrcode.moduleCount
maxSize = fix(params.loadWidth - padding - padding)
if params.loadWidth > params.loadHeight
maxSize = fix(params.loadHeight - padding - padding)
end if
if maxSize < moduleCount
m.top.loadStatus = "failed"
return ""
end if
cellSize = maxSize \ moduleCount
size = cellSize * moduleCount
center = size \ 2
left = (fix(params.loadWidth - padding - padding) \ 2) - center + padding
top = (fix(params.loadHeight - padding - padding) \ 2) - center + padding
' Prepare colors
white = &Hffffffff
black = &H000000ff
' Prepare bitmap
if params.AlphaEnable = invalid
params.AlphaEnable = false
end if
if params.name = invalid
params.name = "QRCode"
end if
bitmap = createObject("roBitmap", {
name: params.name
width: params.loadWidth
height: params.loadHeight
AlphaEnable: params.AlphaEnable
})
bitmap.clear(white)
' Gather bytes to use for unique file name
ba = CreateObject("roByteArray")
ba.setResize(moduleCount * moduleCount / 8, false)
' Render QRCode to bitmap
y = top
bits = 0
byte = 0
byteIndex = 0
for row = 0 to moduleCount - 1 step 1
x = left
for col = 0 to moduleCount - 1 step 1
if qrcode.callFunc("isDark", row, col)
bitmap.drawRect(x, y, cellSize, cellSize, black)
else
byte = byte or 1
end if
byte = byte << 1
bits += 1
if bits >= 8
ba[byteIndex] = byte
byteIndex += 1
' bslint gets confused by these two
' bs:disable-next-line
bits = 0
' bs:disable-next-line
byte = 0
end if
x += cellSize
end for
y += cellSize
end for
bitmap.finish()
' Prepare hash sum use as a file name
digest = CreateObject("roEVPDigest")
digest.setup("md5")
' Prepare file name
fileName = "tmp:/" + digest.process(ba) + ".png"
' Save bitmap to file
pngData = bitmap.getPNG(0, 0, params.loadWidth, params.loadHeight)
pngData.writeFile(fileName)
m.top.uri = fileName
' Update width and height if they were not set
if params.width = 0
m.top.width = params.loadWidth
end if
if params.height = 0
m.top.height = params.loadHeight
end if
return fileName
end function
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="QRPoster" extends="Poster">
<script type="text/brightscript" uri="QRPoster.brs" />
<interface>
<!--
Usage:
1. Set `loadWidth` and `loadHeight` to desired values.
2. Set `loadPadding` if you want to leave a "frame" around QRCode image.
QRPoster will keep size as specified through `loadWidth` and `loadHeight`.
3. Set one of:
3.a. `qrcode` to pre-made QRCode object,
3.b. `text` to a value that you want QRPoster to generate QRCode for.
4. `uri` will be set to generated image tmp:/ path
Setting `text` will also set `qrcode` to a new QRCode object.
-->
<!-- Set to minimum number of pixels to be left on the sides -->
<field id="loadPadding" type="integer" value="0" />
<!-- Set QRCode object to show as a new image -->
<field id="qrcode" type="node" />
<!-- Set to a new value to replace `qrcode` and show a new image -->
<field id="text" type="string" value="" />
</interface>
</component>
@@ -0,0 +1,145 @@
sub init()
m.top.observeFieldScoped("qrcode", "onQRCodeChanged")
m.top.observeFieldScoped("text", "onTextChanged")
end sub
' @private
sub onQRCodeChanged(msg as object)
qrcode = msg.getData()
if qrcode <> invalid
if qrcode.status = "ready"
m.top.qrstring = toASCII(qrcode, m.top.cellSize, m.top.padding)
end if
else
m.top.qrstring = ""
end if
end sub
' @private
sub onTextChanged(msg as object)
qrcode = createObject("roSGNode", "QRCode")
qrcode.callFunc("addData", msg.getData())
qrcode.callFunc("make")
m.top.qrcode = qrcode
end sub
' @public
function toASCII(qrcode as object, cellSize = 1 as integer, padding = 1 as integer) as string
if cellSize < 1
cellSize = 1
end if
if cellSize < 2
return toHalfASCII(qrcode, padding)
end if
cellSize -= 1
if padding < 0
padding = cellSize * 2
end if
size = (qrcode.moduleCount * cellSize) + (padding * 2)
min = padding
max = size - padding
white = String(cellSize, "██")
black = String(cellSize, " ")
ascii = ""
line = ""
NL = chr(10)
for y = 0 to size - 1 step 1
r = fix((y - min) / cellSize)
line = ""
for x = 0 to size - 1 step 1
p = 1
if min <= x and x < max and min <= y and y < max and qrcode.callFunc("isDark", r, (x - min) \ cellSize)
p = 0
end if
' Output 2 characters per pixel, to create full square. 1 character per pixels gives only half width of square.
if p = 1
line += white
else
line += black
end if
end for
for r = 0 to cellSize - 1 step 1
ascii += line + NL
end for
end for
return ascii.left(ascii.len() - 1)
end function
' @private
function toHalfASCII(qrcode as object, padding = 1 as integer) as string
cellSize = 1
if padding < 0
padding = 2
end if
size = (qrcode.moduleCount * cellSize) + (padding * 2)
min = padding
max = size - padding
blocks = {
"██": "█"
"█ ": "▀"
" █": "▄"
" ": " "
}
blocksLastLineNoPadding = {
"██": "▀"
"█ ": "▀"
" █": " "
" ": " "
}
white = "█"
black = " "
ascii = ""
NL = chr(10)
for y = 0 to size - 1 step 2
r1 = fix((y - min) / cellSize)
r2 = fix((y + 1 - min) / cellSize)
for x = 0 to size - 1 step 1
p = white
if min <= x and x < max and min <= y and y < max and qrcode.callFunc("isDark", r1, fix((x - min) / cellSize))
p = black
end if
if min <= x and x < max and min <= y + 1 and y + 1 < max and qrcode.callFunc("isDark", r2, fix((x - min) / cellSize))
p += black
else
p += white
end if
' Output 2 characters per pixel, to create full square. 1 character per pixels gives only half width of square.
if padding < 1 and y + 1 >= max
ascii += blocksLastLineNoPadding[p]
else
ascii += blocks[p]
end if
end for
ascii += NL
end for
if size mod 2 <> 0 and padding > 0
return ascii.left(ascii.len() - size - 1) + String(size, "▀")
end if
return ascii.left(ascii.len() - 1)
end function
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="QRPrinter" extends="Node">
<script type="text/brightscript" uri="QRPrinter.brs" />
<interface>
<!--
Usage:
1. Set `cellSize` to desired value, e.g., 1 means 1 character per module of QRCode.
2. Set `padding` to desired "width" of a "frame" around QRCode rendered using UTF-8 blocks.
3. Set one of:
3.a. `qrcode` to pre-made QRCode object,
3.b. `text` to a value that you want QRPrinter to render QRCode string data for.
4. print `qrstring` or set some Label.text to its value (remember to use monospaced font!)
Setting `text` will also set `qrcode` to a new QRCode object.
Alternative usage:
1. callFunc("toASCII", qrcode, cellSize, padding) to get string of UTF-8 characters.
This will not update any of the fields!
-->
<!-- Set to number of characters to be used per module (or "dot") of QRCode -->
<field id="cellSize" type="integer" value="1" />
<!-- Set to number of characters to be added to create a "frame" around QRCode -->
<!-- For example: 1 means 1 character on the left, one on the right, 1 above and 1 below -->
<field id="padding" type="integer" value="1" />
<!-- Set QRCode object to render to `qrstring` as UTF-8 characters -->
<field id="qrcode" type="node" alwaysNotify="true" />
<!-- Set to a new value to replace `qrcode` and update `qrstring` -->
<field id="text" type="string" value="" />
<!-- Read-only! This will contain QRCode rendered as UTF-8 blocks and spaces -->
<field id="qrstring" type="string" value="" />
<!--
WARNING: It's called `toASCII` even though it uses UTF-8 blocks
because of terms like `ASCII Art`, i.e., "drawing using text only".
-->
<function name="toASCII" /><!-- QRCode, cellSize=1 integer, padding=1 integer -->
</interface>
</component>
+28
View File
@@ -0,0 +1,28 @@
' Credits to https://github.com/kazuhikoarase/qrcode-generator/pull/110
sub init()
print "Init: "; m.top.subtype()
timer = CreateObject("roTimespan")
timer.mark()
m.QRCode = createObject("roSGNode", "TestQRCode")
print "Creating QRCode took:", timer.TotalMilliseconds().ToStr();"ms"
timer.mark()
m.QRPoster = m.top.findNode("TestQRPoster")
m.QRPoster.qrcode = m.QRCode
print "Creating PNG took:", timer.TotalMilliseconds().ToStr();"ms"
print "QRCode image created as:", m.QRPoster.uri
timer.mark()
m.QRPrinter = createObject("roSGNode", "QRPrinter")
m.QRPrinter.qrcode = m.QRCode
print m.QRPrinter.qrstring
print "Printing to console took:", timer.TotalMilliseconds().ToStr();"ms"
end sub
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="QRTestScene" extends="Scene">
<script type="text/brightscript" uri="QRTestScene.brs" />
<children>
<!-- This is example use of QRPoster: show QRCode centered on screen -->
<QRPoster id="TestQRPoster"
loadWidth="500"
loadHeight="500"
loadPadding="20"
translation="[390, 110]" />
</children>
</component>
@@ -0,0 +1,80 @@
sub init()
m.top.typeNumber = -1
m.top.errorCorrectionLevel = "M"
m.top.sourceText = "http://www.example.com/ążśźęćńół"
m.top.correctHalfWidth = [
"█████████████████████████████████",
"██ ▄▄▄▄▄ █ ▄█▀▄██ ▀▄ ▄██ ▄▄▄▄▄ ██",
"██ █ █ █▀ █▀▄█▀▀█▄▄ ▄█ █ █ ██",
"██ █▄▄▄█ ███ ▀ █▄▀▄▄▀ ▀█ █▄▄▄█ ██",
"██▄▄▄▄▄▄▄█ ▀▄█▄▀▄▀ █▄▀▄█▄▄▄▄▄▄▄██",
"██▄█ ▀▄▄▄█▄▄█▀█▀▀▄█▀▀▀█ ▀▀▄█▄ ██",
"██▀▀▄▀▄█▄█ ▄▄█▄ ▄█ ███ ▀█▀▄▄▀██",
"██ ▄▀█▄▀▄ ▄▀▄ █ ▄▀▀█▀██▀██▄▄▀▀██",
"██ █▀ ▄▄▀▀ ▀█ █▀▄ ▀█▄▀▄▄▄ ▄██",
"██▄██ ▄▄▄▄▄█ ▀▄▄ ▀▀▄▄▄█▄▄█▀ ███",
"██▄█▄██▄▄▄ █ █▄▄▀█▀███▄▀▄▄█▄ ████",
"███▄▄██▄▄▄ ▀▀▄█ █▀ █ ▄ ▄▄▄ ▀▀██",
"██ ▄▄▄▄▄ █ █ ▀█▄█ ▀ ▄ █▄█ ▄█ ▀██",
"██ █ █ █▀▄▄ ▄▀ ▀▄█ █ ▄▄ ▄ ██",
"██ █▄▄▄█ █▄█▄▀█ ▀ ▀ ██ █ █▀▄▀▄██",
"██▄▄▄▄▄▄▄█▄▄█▄█▄███▄▄▄▄████▄█▄███",
"▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀",].join(chr(10))
m.top.correctHalfWidthWithoutBorder = [
" ▄▄▄▄▄ █ ▄█▀▄██ ▀▄ ▄██ ▄▄▄▄▄ ",
" █ █ █▀ █▀▄█▀▀█▄▄ ▄█ █ █ ",
" █▄▄▄█ ███ ▀ █▄▀▄▄▀ ▀█ █▄▄▄█ ",
"▄▄▄▄▄▄▄█ ▀▄█▄▀▄▀ █▄▀▄█▄▄▄▄▄▄▄",
"▄█ ▀▄▄▄█▄▄█▀█▀▀▄█▀▀▀█ ▀▀▄█▄ ",
"▀▀▄▀▄█▄█ ▄▄█▄ ▄█ ███ ▀█▀▄▄▀",
" ▄▀█▄▀▄ ▄▀▄ █ ▄▀▀█▀██▀██▄▄▀▀",
" █▀ ▄▄▀▀ ▀█ █▀▄ ▀█▄▀▄▄▄ ▄",
"▄██ ▄▄▄▄▄█ ▀▄▄ ▀▀▄▄▄█▄▄█▀ █",
"▄█▄██▄▄▄ █ █▄▄▀█▀███▄▀▄▄█▄ ██",
"█▄▄██▄▄▄ ▀▀▄█ █▀ █ ▄ ▄▄▄ ▀▀",
" ▄▄▄▄▄ █ █ ▀█▄█ ▀ ▄ █▄█ ▄█ ▀",
" █ █ █▀▄▄ ▄▀ ▀▄█ █ ▄▄ ▄ ",
" █▄▄▄█ █▄█▄▀█ ▀ ▀ ██ █ █▀▄▀▄",
" ▀ ▀ ▀ ▀▀▀ ▀▀▀▀ ▀ ▀",].join(chr(10))
m.top.correctFullWidth = [
"██████████████████████████████████████████████████████████████████",
"██████████████████████████████████████████████████████████████████",
"████ ██ ████ ████ ██ ████ ████",
"████ ██████████ ██ ████ ██████ ██ ██████ ██████████ ████",
"████ ██ ██ ████ ████ ████████ ██ ██ ██ ████",
"████ ██ ██ ██ ██ ████ ██████ ████ ██ ██ ████",
"████ ██ ██ ██████ ██ ██ ██ ██ ████ ██ ██ ████",
"████ ██████████ ██████ ████ ████ ██ ██████████ ████",
"████ ██ ██ ██ ██ ██ ██ ██ ██ ████",
"████████████████████ ██████ ██ ████ ██████████████████████",
"████ ██ ██ ██ ██████████ ██████████ ████ ██ ████",
"████████ ██████████████ ██ ████ ██ ██████ ████",
"████████ ██ ██ ██ ██ ██ ██████ ██████ ██████",
"████ ██ ████████ ████████ ████ ██████ ██ ████ ████",
"████ ████ ██ ██ ██ ██████████████████ ████████",
"████ ██ ████ ██ ██ ██ ██ ██ ██ ████ ████████ ████",
"████ ████ ████ ████ ████ ████ ██ ████",
"████ ██ ████ ██ ██ ██ ████ ██████ ██████",
"████ ████ ██ ██ ████ ██ ████ ██████",
"██████████ ████████████ ████ ██████████████ ██████",
"████ ██ ████ ██ ██ ████████████ ██ ██ ████████",
"████████████████████ ██ ██████ ██ ████████ ████████ ████████",
"██████ ████ ████ ██ ████ ██ ████████",
"████████████████████ ████ ██ ██ ██ ██████ ████",
"████ ██ ██ ████ ██ ██ ██ ██ ██ ██████",
"████ ██████████ ██ ██ ██████ ██ ██████ ████ ████",
"████ ██ ██ ████ ██ ██ ██ ██ ████",
"████ ██ ██ ██ ████ ██ ████ ██ ████ ██ ████",
"████ ██ ██ ██ ██ ████ ██ ██ ████ ██ ████ ██ ████",
"████ ██████████ ████████ ██ ████ ██ ██ ██ ██████",
"████ ██ ██ ██ ██████ ████████ ██ ██████",
"██████████████████████████████████████████████████████████████████",
"██████████████████████████████████████████████████████████████████",].join(chr(10))
addData(m.top.sourceText)
make()
end sub
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
This is just for testing!
Since we try to test both with `brs` (on pc) and Rooibos (on Roku),
we want to have exact same test data for both.
It's less error-prone to keep it in a single place.
-->
<component name="TestQRCode" extends="QRCode">
<script type="text/brightscript" uri="TestQRCode.brs" />
<interface>
<!-- Read-only! -->
<field id="sourceText" type="string" />
<field id="correctHalfWidth" type="string" />
<field id="correctHalfWidthWithoutBorder" type="string" />
<field id="correctFullWidth" type="string" />
</interface>
</component>
@@ -0,0 +1,9 @@
' @private
function glog(n as integer) as dynamic
return QRMath().glog(n)
end function
' @private
function gexp(n as integer) as integer
return QRMath().gexp(n)
end function
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
This is just for testing stuff that's private for QRCode!
-->
<component name="TestQRMath" extends="Node">
<script type="text/brightscript" uri="TestQRMath.brs" />
<script type="text/brightscript" uri="pkg:/components/QrCode/QRCode/QRCode.exports.brs" />
<interface>
<!-- Private, exported for testing -->
<function name="glog" />
<function name="gexp" />
</interface>
</component>