Skip to content

Commit ce64610

Browse files
committed
fuqr bitflags
1 parent a463c79 commit ce64610

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+392
-945
lines changed

README.md

+24-25
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ A preset must export `paramsSchema` and either `renderSVG` or `renderCanvas`
122122
This schema defines the UI components whose values are passed into `renderSVG` or `renderCanvas` via the `params` object.
123123

124124
All properties besides `type` are optional, except
125+
125126
- type `select` must have a nonempty options array
126127
- type `array` must have a valid `props` value.
127128

128129
In this example, `default` is set explicitly to the implicit default value.
130+
129131
```js
130132
export const paramsSchema = {
131133
Example1: {
132-
type: "number"
134+
type: "number",
133135
min: 0,
134136
max: 10,
135137
step: 0.1,
@@ -141,29 +143,29 @@ export const paramsSchema = {
141143
},
142144
Example3: {
143145
type: "color",
144-
default: "#000000"
146+
default: "#000000", // css color string (hex/rgba/hsla)
145147
},
146148
Example4: {
147149
type: "select",
148-
options: ["I'm feeling", 22]
149-
default: "I'm feeling",
150+
options: ["I'm feeling", 22],
151+
default: "I'm feeling", // first option
150152
},
151153
Example5: {
152154
type: "file",
153-
accept: ".jpeg, .jpg, .png"
155+
accept: ".jpeg, .jpg, .png",
154156
default: null,
155157
},
156158
Example6: {
157159
type: "array",
158160
props: {
159-
type: "number" // any type except "array"
161+
type: "number", // any type except "array"
160162
// corresponding props
161163
},
162164
resizable: true,
163165
defaultLength: 5, // overridden by default
164166
default: [], // overrides defaultLength
165-
}
166-
}
167+
},
168+
};
167169
```
168170

169171
## `renderSVG` and `renderCanvas`
@@ -180,27 +182,24 @@ type renderCanvas = (qr: Qr, params: Params, canvas: OffscreenCanvas) => void;
180182

181183
```ts
182184
type Qr = {
183-
matrix: Module[] // see below
185+
matrix: Uint8Array; // see below
184186
version: number; // 1- 40
185-
mask: number; // 0 - 8,
187+
mask: number; // 0 - 7,
186188
ecl: number; // 0 - 3, Low, Medium, Quartile, High
187189
mode: number; // 0 - 2, Numeric, Alphanumeric, Byte
188190
};
189191

190-
enum Module {
191-
DataOFF = 0,
192-
DataON = 1,
193-
FinderOFF = 2,
194-
FinderON = 3,
195-
AlignmentOFF = 4,
196-
AlignmentON = 5,
197-
TimingOFF = 6,
198-
TimingON = 7,
199-
FormatOFF = 8,
200-
FormatON = 9,
201-
VersionOFF = 10,
202-
VersionON = 11,
203-
SeparatorOFF = 12,
204-
}
192+
// bit flags for each u8 in matrix
193+
const Module = {
194+
ON: 1 << 0,
195+
DATA: 1 << 1,
196+
FINDER: 1 << 2,
197+
ALIGNMENT: 1 << 3,
198+
TIMING: 1 << 4,
199+
FORMAT: 1 << 5,
200+
VERSION: 1 << 6,
201+
MODIFIER: 1 << 7,
202+
};
205203
```
206204

205+
`MODIFIER` is set for Finder and Alignment centers, Format and Version copy.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "qrframe",
33
"type": "module",
44
"scripts": {
5-
"dev": "vinxi dev",
5+
"dev": "nr presets dev && vinxi dev",
66
"build": "vinxi build",
77
"start": "vinxi start",
88
"presets": "node updatePresets"
@@ -22,7 +22,7 @@
2222
"@thisbeyond/solid-dnd": "^0.7.5",
2323
"@unocss/reset": "^0.59.4",
2424
"codemirror": "^6.0.1",
25-
"fuqr": "^0.0.6",
25+
"fuqr": "^0.0.7",
2626
"lucide-solid": "^0.378.0",
2727
"solid-js": "^1.8.17",
2828
"unocss": "^0.59.4",

pnpm-lock.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

presets/Alien.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Based on QRBTF's Line style
22
// https://github.com/CPunisher/react-qrbtf/blob/master/src/components/QRLine.tsx
3+
import { Module, getSeededRand } from "REPLACE_URL/utils.js";
4+
35
export const paramsSchema = {
46
Margin: {
57
type: "number",
@@ -28,36 +30,8 @@ export const paramsSchema = {
2830
},
2931
};
3032

31-
const Module = {
32-
DataOFF: 0,
33-
DataON: 1,
34-
FinderOFF: 2,
35-
FinderON: 3,
36-
AlignmentOFF: 4,
37-
AlignmentON: 5,
38-
TimingOFF: 6,
39-
TimingON: 7,
40-
FormatOFF: 8,
41-
FormatON: 9,
42-
VersionOFF: 10,
43-
VersionON: 11,
44-
SeparatorOFF: 12,
45-
};
46-
47-
function splitmix32(a) {
48-
return function () {
49-
a |= 0;
50-
a = (a + 0x9e3779b9) | 0;
51-
let t = a ^ (a >>> 16);
52-
t = Math.imul(t, 0x21f0aaad);
53-
t = t ^ (t >>> 15);
54-
t = Math.imul(t, 0x735a2d97);
55-
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
56-
};
57-
}
58-
5933
export function renderSVG(qr, params) {
60-
const rand = splitmix32(params["Seed"]);
34+
const rand = getSeededRand(params["Seed"]);
6135
const rangeStr = (min, max) => (rand() * (max - min) + min).toFixed(2);
6236

6337
const matrixWidth = qr.version * 4 + 17;
@@ -117,9 +91,9 @@ export function renderSVG(qr, params) {
11791
for (let y = 0; y < matrixWidth; y++) {
11892
for (let x = 0; x < matrixWidth; x++) {
11993
const module = matrix(x, y);
120-
if ((module | 1) === Module.FinderON) continue;
94+
if (module & Module.FINDER) continue;
12195

122-
if (!(module & 1)) continue;
96+
if (!(module & Module.ON)) continue;
12397
dotsLayer += `<circle cx="${x + 0.5}" cy="${y + 0.5}" r="${rangeStr(0.2, 0.4)}"/>`;
12498

12599
if (!visited1(x, y)) {
@@ -128,7 +102,7 @@ export function renderSVG(qr, params) {
128102
while (
129103
nx < matrixWidth &&
130104
ny < matrixWidth &&
131-
matrix(nx, ny) & 1 &&
105+
matrix(nx, ny) & Module.ON &&
132106
!visited1(nx, ny)
133107
) {
134108
setVisited1(nx, ny);
@@ -146,7 +120,7 @@ export function renderSVG(qr, params) {
146120
while (
147121
nx >= 0 &&
148122
ny < matrixWidth &&
149-
matrix(nx, ny) & 1 &&
123+
matrix(nx, ny) & Module.ON &&
150124
!visited2(nx, ny)
151125
) {
152126
setVisited2(nx, ny);

presets/Basic.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Module } from "REPLACE_URL/utils.js";
2+
13
export const paramsSchema = {
24
Margin: {
35
type: "number",
@@ -52,22 +54,6 @@ export const paramsSchema = {
5254
},
5355
};
5456

55-
const Module = {
56-
DataOFF: 0,
57-
DataON: 1,
58-
FinderOFF: 2,
59-
FinderON: 3,
60-
AlignmentOFF: 4,
61-
AlignmentON: 5,
62-
TimingOFF: 6,
63-
TimingON: 7,
64-
FormatOFF: 8,
65-
FormatON: 9,
66-
VersionOFF: 10,
67-
VersionON: 11,
68-
SeparatorOFF: 12,
69-
};
70-
7157
export async function renderSVG(qr, params) {
7258
const matrixWidth = qr.version * 4 + 17;
7359
const margin = params["Margin"];
@@ -142,8 +128,8 @@ export async function renderSVG(qr, params) {
142128
continue;
143129
}
144130
const module = qr.matrix[y * matrixWidth + x];
145-
if (!(module & 1)) continue;
146-
if (module === Module.FinderON) continue;
131+
if (!(module & Module.ON)) continue;
132+
if (module & Module.FINDER) continue;
147133

148134
if (defaultShape) {
149135
if (roundness) {

presets/Blocks.js

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Based on QRBTF's DSJ style
22
// https://github.com/CPunisher/react-qrbtf/blob/master/src/components/QRDsj.tsx
3+
import { Module } from "REPLACE_URL/utils.js";
4+
35
export const paramsSchema = {
46
Margin: {
57
type: "number",
@@ -51,22 +53,6 @@ export const paramsSchema = {
5153
},
5254
};
5355

54-
const Module = {
55-
DataOFF: 0,
56-
DataON: 1,
57-
FinderOFF: 2,
58-
FinderON: 3,
59-
AlignmentOFF: 4,
60-
AlignmentON: 5,
61-
TimingOFF: 6,
62-
TimingON: 7,
63-
FormatOFF: 8,
64-
FormatON: 9,
65-
VersionOFF: 10,
66-
VersionON: 11,
67-
SeparatorOFF: 12,
68-
};
69-
7056
export function renderSVG(qr, params) {
7157
const matrixWidth = qr.version * 4 + 17;
7258
const margin = params["Margin"];
@@ -122,8 +108,8 @@ export function renderSVG(qr, params) {
122108
for (let y = 0; y < matrixWidth; y++) {
123109
for (let x = 0; x < matrixWidth; x++) {
124110
const module = matrix(x, y);
125-
if ((module | 1) === Module.FinderON) continue;
126-
if (!(module & 1)) continue;
111+
if (module & Module.FINDER) continue;
112+
if (!(module & Module.ON)) continue;
127113
if (visited(x, y)) continue;
128114
setVisited(x, y);
129115

@@ -157,9 +143,13 @@ export function renderSVG(qr, params) {
157143
if (
158144
y < matrixWidth - 1 &&
159145
x < matrixWidth - 1 &&
160-
matrix(x + 1, y) & matrix(x, y + 1) & matrix(x + 1, y + 1) & 1
146+
matrix(x + 1, y) & matrix(x, y + 1) & matrix(x + 1, y + 1) & Module.ON
161147
) {
162-
if (!visited(x + 1, y) && !visited(x + 1, y + 1) && !visited(x, y + 1)) {
148+
if (
149+
!visited(x + 1, y) &&
150+
!visited(x + 1, y + 1) &&
151+
!visited(x, y + 1)
152+
) {
163153
crossLayer += `<g>`;
164154
crossLayer += `<line x1="${x + co}" y1="${y + co}" x2="${x + 2 - co}" y2="${y + 2 - co}"/>`;
165155
crossLayer += `<line x1="${x + 2 - co}" y1="${y + co}" x2="${x + co}" y2="${y + 2 - co}"/>`;
@@ -173,7 +163,7 @@ export function renderSVG(qr, params) {
173163
}
174164

175165
let ny = y + 1;
176-
while (ny < matrixWidth && matrix(x, ny) & 1 && !visited(x, ny)) {
166+
while (ny < matrixWidth && matrix(x, ny) & Module.ON && !visited(x, ny)) {
177167
ny++;
178168
}
179169
if (ny - y > 2) {
@@ -186,7 +176,7 @@ export function renderSVG(qr, params) {
186176
}
187177

188178
let nx = x + 1;
189-
while (nx < matrixWidth && matrix(nx, y) & 1 && !visited(nx, y)) {
179+
while (nx < matrixWidth && matrix(nx, y) & Module.ON && !visited(nx, y)) {
190180
setVisited(nx, y);
191181
nx++;
192182
}

0 commit comments

Comments
 (0)