@journey-to-code/symbology 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +1,297 @@
1
- <script>
2
- import { loadQr } from '../qr.js'
3
-
4
- let api
5
- let svgWidth = 0
6
- let svgHeight = 0
7
- let viewBox = '0 0 0 0'
8
- let path = ''
9
- let svgMarkup = ''
10
- let error = ''
11
- let loading = true
12
-
13
- async function initialize() {
14
- try {
15
- api = await loadQr()
16
- loading = false
17
- generate()
18
- } catch (cause) {
19
- error = cause instanceof Error ? cause.message : String(cause)
20
- loading = false
21
- }
22
- }
23
-
24
- function generate() {
25
- if (!api) return
26
- try {
27
- const code = format === 'qr' ? api.encode(value) : api.encodeBarcode(format, value)
28
- const modules = code.modules
29
- path = ''
30
- if (format === 'qr') {
31
- svgWidth = code.size + 8
32
- svgHeight = svgWidth
33
- for (let y = 0; y < code.size; y++) {
34
- for (let x = 0; x < code.size; x++) {
35
- if (!modules[y * code.size + x]) continue
36
- let run = 1
37
- while (x + run < code.size && modules[y * code.size + x + run]) run++
38
- path += `M${x + 4} ${y + 4}h${run}v1h-${run}z`
39
- x += run - 1
40
- }
41
- }
42
- } else {
43
- svgWidth = code.width
44
- svgHeight = 60
45
- let x = 0
46
- while (x < code.width) {
47
- if (!modules[x]) {
48
- x++
49
- continue
50
- }
51
- let run = 1
52
- while (x + run < code.width && modules[x + run]) run++
53
- path += `M${x} 0h${run}v${svgHeight}h-${run}z`
54
- x += run
55
- }
56
- }
57
- viewBox = `0 0 ${svgWidth} ${svgHeight}`
58
- svgMarkup = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${viewBox}" shape-rendering="crispEdges"><rect width="${svgWidth}" height="${svgHeight}" fill="#fff"/><path d="${path}" fill="#000"/></svg>`
59
- error = ''
60
- } catch (cause) {
61
- path = ''
62
- svgMarkup = ''
63
- error = cause instanceof Error ? cause.message : String(cause)
64
- }
65
- }
66
-
67
- function download(event) {
68
- event.preventDefault()
69
- if (!svgMarkup) return
70
- const url = URL.createObjectURL(new Blob([svgMarkup], { type: 'image/svg+xml' }))
71
- const link = document.createElement('a')
72
- link.href = url
73
- link.download = `symbology-${format}.svg`
74
- link.click()
75
- setTimeout(() => URL.revokeObjectURL(url), 0)
76
- }
77
-
78
- function setFormat(event) {
79
- format = event.currentTarget.value
80
- generate()
81
- }
82
-
83
- function setValue(event) {
84
- value = event.currentTarget.value
85
- }
86
-
87
- initialize()
88
- </script>
89
- <style>
90
- :host { color: #17241f; font: 14px/1.5 system-ui, sans-serif; }
91
- form { display: grid; gap: 12px; max-width: 28rem; }
92
- label { display: grid; gap: 5px; font-size: 12px; font-weight: 600; }
93
- select, textarea, button { box-sizing: border-box; font: inherit; }
94
- select, textarea { width: 100%; padding: 9px; border: 1px solid #cbd1c7; border-radius: 4px; background: white; }
95
- textarea { min-height: 76px; resize: vertical; }
96
- button { padding: 10px 14px; border: 0; border-radius: 4px; background: #c9ef5b; color: #17241f; cursor: pointer; font-weight: 700; }
97
- svg { display: block; width: min(100%, 24rem); max-height: 24rem; margin-top: 18px; background: white; }
98
- a { display: inline-block; margin-top: 12px; color: #314c15; }
99
- p { color: #59645d; }
100
- </style>
101
- <symbology-code format="qr" value="https://example.com">
102
- <form onsubmit={(event) => { event.preventDefault(); generate() }}>
103
- <label>Format
104
- <select value={format} onchange={setFormat}>
105
- <option value="qr">QR code</option>
106
- <option value="code128">Code 128</option>
107
- <option value="code39">Code 39</option>
108
- <option value="ean13">EAN-13</option>
109
- </select>
110
- </label>
111
- <label>Content
112
- <textarea value={value} oninput={setValue}></textarea>
113
- </label>
114
- <button type="submit">Generate code</button>
115
- </form>
116
- <p role="status">{loading ? 'Loading the Symbology engine…' : error || 'Ready'}</p>
117
- <svg
118
- xmlns="http://www.w3.org/2000/svg"
119
- viewBox={viewBox}
120
- role="img"
121
- aria-label={`Generated ${format} code`}
122
- shape-rendering="crispEdges"
123
- hidden={!path}
124
- >
125
- <rect width={svgWidth} height={svgHeight} fill="#fff"></rect>
126
- <path d={path} fill="#000"></path>
127
- </svg>
128
- <a href="#" onclick={download} hidden={!path}>Download SVG</a>
129
- </symbology-code>
1
+ <!--
2
+ Generate a QR code or linear barcode and download it as SVG. For QR codes, `preset`, `logo`,
3
+ `logo-size`, `cta` and `qr-style` make a styled code (see style.js); every styled design is
4
+ decoded back before it is shown, and an unscannable one shows why instead of a picture.
5
+ `logo` is an image URL or data URI (SVG, PNG, JPEG or WebP); it is fetched and embedded.
6
+ `qr-style` is a JSON style object, merged over the preset. Visitors can also pick a style and
7
+ upload a logo in the widget, unless `controls="basic"` hides those fields.
8
+ -->
9
+ <script>
10
+ import { loadQr } from '../qr.js'
11
+ import { styledQr, PRESETS } from '../style.js'
12
+
13
+ let api
14
+ let svgWidth = 0
15
+ let svgHeight = 0
16
+ let viewBox = '0 0 0 0'
17
+ let path = ''
18
+ let svgMarkup = ''
19
+ let error = ''
20
+ let loading = true
21
+ let art = null
22
+ let styled = false
23
+ let verdict = ''
24
+ let notes = ''
25
+ let chosenPreset = ''
26
+ let uploadedLogo = null
27
+ let fetchedLogo = null
28
+ let logoNote = ''
29
+ const presetNames = Object.keys(PRESETS)
30
+ const MAX_LOGO_BYTES = 150000
31
+
32
+ async function initialize() {
33
+ try {
34
+ api = await loadQr()
35
+ chosenPreset = preset
36
+ await fetchLogo()
37
+ loading = false
38
+ generate()
39
+ } catch (cause) {
40
+ error = cause instanceof Error ? cause.message : String(cause)
41
+ loading = false
42
+ }
43
+ }
44
+
45
+ function readAsDataUrl(blob) {
46
+ return new Promise((resolve, reject) => {
47
+ const reader = new FileReader()
48
+ reader.onload = () => resolve(reader.result)
49
+ reader.onerror = () => reject(reader.error)
50
+ reader.readAsDataURL(blob)
51
+ })
52
+ }
53
+
54
+ async function logoFrom(blob) {
55
+ if (blob.size > MAX_LOGO_BYTES) throw new Error(`the logo is over ${MAX_LOGO_BYTES / 1000} kB`)
56
+ if (blob.type === 'image/svg+xml') return { svg: await blob.text() }
57
+ return { href: await readAsDataUrl(blob) }
58
+ }
59
+
60
+ async function fetchLogo() {
61
+ fetchedLogo = null
62
+ logoNote = ''
63
+ if (!logo) return
64
+ try {
65
+ if (logo.startsWith('data:image/svg+xml;utf8,')) {
66
+ fetchedLogo = { svg: decodeURIComponent(logo.slice(logo.indexOf(',') + 1)) }
67
+ } else if (logo.startsWith('data:')) {
68
+ fetchedLogo = { href: logo }
69
+ } else {
70
+ const response = await fetch(new URL(logo, document.baseURI))
71
+ if (!response.ok) throw new Error(`${response.status} ${response.statusText}`.trim())
72
+ fetchedLogo = await logoFrom(await response.blob())
73
+ }
74
+ } catch (cause) {
75
+ logoNote = `Logo not loaded: ${cause instanceof Error ? cause.message : String(cause)}`
76
+ }
77
+ }
78
+
79
+ function wantsStyle() {
80
+ return Boolean(chosenPreset || uploadedLogo || fetchedLogo || cta || Object.keys(qrStyle ?? {}).length)
81
+ }
82
+
83
+ function currentStyle() {
84
+ const base = qrStyle && typeof qrStyle === 'object' ? qrStyle : {}
85
+ const style = { ...base, title: base.title ?? `QR code for ${value}` }
86
+ if (chosenPreset) style.preset = chosenPreset
87
+ const image = uploadedLogo ?? fetchedLogo
88
+ if (image) {
89
+ const size = Number(logoSize)
90
+ style.logo = { ...(base.logo ?? {}), ...image, ...(size > 0 ? { size } : {}) }
91
+ }
92
+ if (cta) style.frame = { ...(base.frame ?? {}), text: cta }
93
+ return style
94
+ }
95
+
96
+ function paint(markup) {
97
+ if (art) art.innerHTML = markup
98
+ }
99
+
100
+ function generateStyled() {
101
+ const { svg, report } = styledQr(api, value, currentStyle())
102
+ styled = true
103
+ path = ''
104
+ const lines = [...report.errors, ...report.warnings]
105
+ if (logoNote) lines.unshift(logoNote)
106
+ notes = lines.join('\n')
107
+ if (report.ok) {
108
+ svgMarkup = svg
109
+ verdict = `Scans · level ${report.level}, version ${report.version} · print at least ${report.print.minWidthMm} mm wide`
110
+ error = ''
111
+ } else {
112
+ svgMarkup = ''
113
+ verdict = ''
114
+ error = 'This design would not scan reliably.'
115
+ }
116
+ paint(svgMarkup)
117
+ }
118
+
119
+ function generate() {
120
+ if (!api) return
121
+ if (format === 'qr' && wantsStyle()) {
122
+ generateStyled()
123
+ return
124
+ }
125
+ styled = false
126
+ verdict = ''
127
+ notes = logoNote
128
+ paint('')
129
+ try {
130
+ const code = format === 'qr' ? api.encode(value) : api.encodeBarcode(format, value)
131
+ const modules = code.modules
132
+ path = ''
133
+ if (format === 'qr') {
134
+ svgWidth = code.size + 8
135
+ svgHeight = svgWidth
136
+ for (let y = 0; y < code.size; y++) {
137
+ for (let x = 0; x < code.size; x++) {
138
+ if (!modules[y * code.size + x]) continue
139
+ let run = 1
140
+ while (x + run < code.size && modules[y * code.size + x + run]) run++
141
+ path += `M${x + 4} ${y + 4}h${run}v1h-${run}z`
142
+ x += run - 1
143
+ }
144
+ }
145
+ } else {
146
+ svgWidth = code.width
147
+ svgHeight = 60
148
+ let x = 0
149
+ while (x < code.width) {
150
+ if (!modules[x]) {
151
+ x++
152
+ continue
153
+ }
154
+ let run = 1
155
+ while (x + run < code.width && modules[x + run]) run++
156
+ path += `M${x} 0h${run}v${svgHeight}h-${run}z`
157
+ x += run
158
+ }
159
+ }
160
+ viewBox = `0 0 ${svgWidth} ${svgHeight}`
161
+ svgMarkup = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${viewBox}" shape-rendering="crispEdges"><rect width="${svgWidth}" height="${svgHeight}" fill="#fff"/><path d="${path}" fill="#000"/></svg>`
162
+ error = ''
163
+ } catch (cause) {
164
+ path = ''
165
+ svgMarkup = ''
166
+ error = cause instanceof Error ? cause.message : String(cause)
167
+ }
168
+ }
169
+
170
+ function download(event) {
171
+ event.preventDefault()
172
+ if (!svgMarkup) return
173
+ const url = URL.createObjectURL(new Blob([svgMarkup], { type: 'image/svg+xml' }))
174
+ const link = document.createElement('a')
175
+ link.href = url
176
+ link.download = styled ? 'symbology-qr-styled.svg' : `symbology-${format}.svg`
177
+ link.click()
178
+ setTimeout(() => URL.revokeObjectURL(url), 0)
179
+ }
180
+
181
+ function setFormat(event) {
182
+ format = event.currentTarget.value
183
+ generate()
184
+ }
185
+
186
+ function setValue(event) {
187
+ value = event.currentTarget.value
188
+ }
189
+
190
+ function setPreset(event) {
191
+ chosenPreset = event.currentTarget.value
192
+ generate()
193
+ }
194
+
195
+ async function uploadLogo(event) {
196
+ const file = event.currentTarget.files?.[0]
197
+ if (!file) return
198
+ try {
199
+ uploadedLogo = await logoFrom(file)
200
+ logoNote = ''
201
+ } catch (cause) {
202
+ uploadedLogo = null
203
+ logoNote = `Logo not used: ${cause instanceof Error ? cause.message : String(cause)}`
204
+ }
205
+ generate()
206
+ }
207
+
208
+ function clearLogo() {
209
+ uploadedLogo = null
210
+ generate()
211
+ }
212
+
213
+ initialize()
214
+ </script>
215
+ <style>
216
+ :host { color: #17241f; font: 14px/1.5 system-ui, sans-serif; }
217
+ form { display: grid; gap: 12px; max-width: 28rem; }
218
+ label { display: grid; gap: 5px; font-size: 12px; font-weight: 600; }
219
+ select, textarea, button { box-sizing: border-box; font: inherit; }
220
+ select, textarea { width: 100%; padding: 9px; border: 1px solid #cbd1c7; border-radius: 4px; background: white; }
221
+ textarea { min-height: 76px; resize: vertical; }
222
+ button { padding: 10px 14px; border: 0; border-radius: 4px; background: #c9ef5b; color: #17241f; cursor: pointer; font-weight: 700; }
223
+ svg { display: block; width: min(100%, 24rem); max-height: 24rem; margin-top: 18px; background: white; }
224
+ .art svg { background: transparent; }
225
+ [hidden] { display: none !important; }
226
+ a { display: inline-block; margin-top: 12px; color: #314c15; }
227
+ p { color: #59645d; }
228
+ .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
229
+ input[type="file"] { font: inherit; font-size: 12px; font-weight: 400; }
230
+ .text-button { justify-self: start; padding: 0; background: none; color: #59645d; font-size: 12px; font-weight: 400; text-decoration: underline; }
231
+ .verdict { color: #3b5a10; font-weight: 600; }
232
+ .notes { margin: 6px 0 0; color: #8a3a2c; font-size: 12px; white-space: pre-line; }
233
+ </style>
234
+ <symbology-code
235
+ format="qr"
236
+ value="https://example.com"
237
+ preset=""
238
+ logo=""
239
+ logo-size=""
240
+ cta=""
241
+ qr-style={{}}
242
+ controls="full"
243
+ >
244
+ <form onsubmit={(event) => { event.preventDefault(); generate() }}>
245
+ <label>Format
246
+ <select value={format} onchange={setFormat}>
247
+ <option value="qr">QR code</option>
248
+ <option value="code128">Code 128</option>
249
+ <option value="code39">Code 39</option>
250
+ <option value="ean13">EAN-13</option>
251
+ </select>
252
+ </label>
253
+ <label>Content
254
+ <textarea value={value} oninput={setValue}></textarea>
255
+ </label>
256
+ <div class="row" zb-if={format === 'qr' && controls !== 'basic'}>
257
+ <label>Style
258
+ <select value={chosenPreset} onchange={setPreset}>
259
+ <option value="">Plain</option>
260
+ <zb-each items={presetNames} as="name" key={name}>
261
+ <option value={name}>{name}</option>
262
+ </zb-each>
263
+ </select>
264
+ </label>
265
+ <label>Logo
266
+ <input
267
+ type="file"
268
+ accept="image/svg+xml,image/png,image/jpeg,image/webp"
269
+ onchange={uploadLogo}
270
+ >
271
+ </label>
272
+ </div>
273
+ <button type="button" class="text-button" zb-if={uploadedLogo} onclick={clearLogo}>Remove uploaded logo</button>
274
+ <button type="submit">Generate code</button>
275
+ </form>
276
+ <p role="status">{loading ? 'Loading the Symbology engine…' : error || (styled ? '' : 'Ready')}<span class="verdict" zb-if={verdict}>{verdict}</span></p>
277
+ <p class="notes" zb-if={notes}>{notes}</p>
278
+ <div
279
+ class="art"
280
+ role="img"
281
+ aria-label={`QR code for ${value}`}
282
+ ref={art}
283
+ hidden={!styled || !svgMarkup}
284
+ ></div>
285
+ <svg
286
+ xmlns="http://www.w3.org/2000/svg"
287
+ viewBox={viewBox}
288
+ role="img"
289
+ aria-label={`Generated ${format} code`}
290
+ shape-rendering="crispEdges"
291
+ hidden={!path}
292
+ >
293
+ <rect width={svgWidth} height={svgHeight} fill="#fff"></rect>
294
+ <path d={path} fill="#000"></path>
295
+ </svg>
296
+ <a href="#" onclick={download} hidden={!svgMarkup}>Download SVG</a>
297
+ </symbology-code>
@@ -1,10 +1,30 @@
1
1
  // Generated by zubra from symbology-code.zb. Do not edit.
2
2
 
3
+ /**
4
+ * Generate a QR code or linear barcode and download it as SVG. For QR codes, `preset`, `logo`,
5
+ * `logo-size`, `cta` and `qr-style` make a styled code (see style.js); every styled design is
6
+ * decoded back before it is shown, and an unscannable one shows why instead of a picture.
7
+ * `logo` is an image URL or data URI (SVG, PNG, JPEG or WebP); it is fetched and embedded.
8
+ * `qr-style` is a JSON style object, merged over the preset. Visitors can also pick a style and
9
+ * upload a logo in the widget, unless `controls="basic"` hides those fields.
10
+ */
3
11
  export declare class SymbologyCode extends HTMLElement {
4
12
  /** Attribute `format`. Default: `"qr"`. */
5
13
  format: string;
6
14
  /** Attribute `value`. Default: `"https://example.com"`. */
7
15
  value: string;
16
+ /** Attribute `preset`. Default: `""`. */
17
+ preset: string;
18
+ /** Attribute `logo`. Default: `""`. */
19
+ logo: string;
20
+ /** Attribute `logo-size`. Default: `""`. */
21
+ logoSize: string;
22
+ /** Attribute `cta`. Default: `""`. */
23
+ cta: string;
24
+ /** Attribute `qr-style`. Default: `{}`. */
25
+ qrStyle: Record<string, unknown>;
26
+ /** Attribute `controls`. Default: `"full"`. */
27
+ controls: string;
8
28
  /** Resolves once pending prop changes have reached the DOM. */
9
29
  readonly updateComplete: Promise<void>;
10
30
  }
@@ -13,6 +33,12 @@ export declare class SymbologyCode extends HTMLElement {
13
33
  export interface SymbologyCodeProps {
14
34
  format?: string;
15
35
  value?: string;
36
+ preset?: string;
37
+ logo?: string;
38
+ logoSize?: string;
39
+ cta?: string;
40
+ qrStyle?: Record<string, unknown>;
41
+ controls?: string;
16
42
  }
17
43
 
18
44
  export declare const tag: "symbology-code";