qr-code-styling-node 1.8.0 → 1.10.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.
package/README.md CHANGED
@@ -31,7 +31,7 @@ npm install qr-code-styling-node
31
31
  <head>
32
32
  <meta charset="UTF-8">
33
33
  <title>QR Code Styling</title>
34
- <script type="text/javascript" src="https://unpkg.com/qr-code-styling-node@1.8.0/lib/qr-code-styling.js"></script>
34
+ <script type="text/javascript" src="https://unpkg.com/qr-code-styling-node@1.9.0/lib/qr-code-styling.js"></script>
35
35
  </head>
36
36
  <body>
37
37
  <div id="canvas"></div>
@@ -77,14 +77,70 @@ The package ships an ES module build (`import`), a CommonJS/UMD build (`require`
77
77
 
78
78
  ---
79
79
 
80
+ ### More styles, frames and labels
81
+
82
+ <img src="https://raw.githubusercontent.com/ducminhquan/qr-code-styling/master/src/assets/style_examples.png" width="720" />
83
+
84
+ New dot types `diamond`, `small-square`, `vertical-lines` and `horizontal-lines`, and a `rounded` type for both corner
85
+ options. A frame, a label and a plate behind the logo:
86
+
87
+ ```js
88
+ const qrCode = new QRCodeStyling({
89
+ data: "https://example.com",
90
+ dotsOptions: { type: "rounded", color: "#1d4ed8" },
91
+ cornersSquareOptions: { type: "rounded" },
92
+ frameOptions: { color: "#1d4ed8", radius: 24 },
93
+ labelOptions: { text: "SCAN ME" },
94
+ image: "https://example.com/logo.svg",
95
+ imageOptions: { backgroundColor: "#ffffff", backgroundRadius: 0.5 }
96
+ });
97
+ ```
98
+
99
+ The frame and label are drawn inside `width` x `height`; the code shrinks to fit.
100
+
101
+ `frameOptions` structure
102
+
103
+ Property|Type |Default Value |Description
104
+ --------|------|-------------------|--------------------------------------------
105
+ color |string|`'#000'` |Frame and label band color
106
+ width |number|4% of the size |Border thickness in px
107
+ radius |number|`0` |Outer corner radius in px
108
+ padding |number|4% of the size |Space between the frame and the code in px
109
+
110
+ `labelOptions` structure
111
+
112
+ Property |Type |Default Value |Description
113
+ ----------|---------------------------|------------------------------------|-----------------------------
114
+ text |string | |The text. Required
115
+ position |string (`'top' 'bottom'`) |`'bottom'` |Where the label goes
116
+ color |string |white on a frame, else the dots color|Text color
117
+ fontSize |number |7% of the size |In px
118
+ fontFamily|string |`'Helvetica, Arial, sans-serif'` |
119
+ fontWeight|string or number |`'bold'` |
120
+
80
121
  ### QR art
81
122
 
82
123
  <img src="https://raw.githubusercontent.com/ducminhquan/qr-code-styling/master/src/assets/art_examples.png" width="720" />
83
124
 
84
- Blend a picture into the code with `art`. Two modes:
125
+ Blend a picture into the code with `art`. Three modes:
85
126
 
86
127
  - `halftone` (default): the picture is redrawn as fine pixels, with each QR module kept in the middle. Use `colored: true` to keep the picture's colors.
87
128
  - `colorize`: dots keep their shape (any `dotsOptions.type`) and are painted with a darkened copy of the picture, over a light copy of it.
129
+ - `blend`: the picture fills the whole image, like an illustration. Dots are drawn only where the picture is not already
130
+ dark or light enough for the code; finders take a dark color from the picture and sit on a light plate.
131
+ Use `placement` to put the code in one part of a larger picture.
132
+
133
+ <img src="https://raw.githubusercontent.com/ducminhquan/qr-code-styling/master/src/assets/blend_examples.png" width="720" />
134
+
135
+ ```js
136
+ const qrCode = new QRCodeStyling({
137
+ width: 760,
138
+ height: 300,
139
+ data: "https://example.com",
140
+ dotsOptions: { type: "dots" }, // round dots give round finders in blend mode
141
+ art: { image: "poster.jpg", mode: "blend", placement: { x: 510, y: 40, size: 220 } }
142
+ });
143
+ ```
88
144
 
89
145
  ```js
90
146
  const qrCode = new QRCodeStyling({
@@ -103,11 +159,15 @@ QR art reads the picture's pixels, so it needs a canvas: it works as is in the b
103
159
  Property |Type |Default Value|Description
104
160
  ----------|----------------------------------|-------------|------------------------------------------------------------
105
161
  image |string | |The picture (URL or data URL). Required
106
- mode |string (`'halftone' 'colorize'`) |`'halftone'` |How the picture is blended in
162
+ mode |string (`'halftone' 'colorize' 'blend'`)|`'halftone'` |How the picture is blended in
107
163
  colored |boolean |`false` |halftone: use the picture's colors instead of dots/background colors
108
164
  background|boolean |`true` |colorize: show a light copy of the picture behind the dots
109
- contrast |number (`0 - 1`) |`0.5` halftone, `0.7` colorize|Higher is easier to scan but shows less of the picture
165
+ contrast |number (`0 - 1`) |`0.5` halftone, `0.7` colorize, `0.6` blend|Higher is easier to scan but shows less of the picture
110
166
  crossOrigin|string | |Overrides `imageOptions.crossOrigin` for the picture
167
+ placement |object `{ x, y, size }` | |Put the code in this square (in pixels) instead of the middle; the margin is ignored. Mostly for `blend`
168
+
169
+ Always check art codes with `verify()` below: a picture with large mid-grey areas or very small modules
170
+ (under about 7 px) may need a higher `contrast`, a larger size or a higher error correction level.
111
171
 
112
172
  ### Checking that a code scans
113
173
 
@@ -157,6 +217,8 @@ nodeCanvas |node-canvas | |Node only, optio
157
217
  jsdom |jsdom | |Node only, optional. Not needed for svg any more; still accepted for backward compatibility
158
218
  resvg |`Resvg` from `@resvg/resvg-js`| |Node only, optional. Produces png without node-canvas, see the Node section below
159
219
  art |object | |Blend a picture into the code, see "QR art" above
220
+ frameOptions |object | |Border around the code, see "Frames and labels" above
221
+ labelOptions |object | |Text above or below the code, see "Frames and labels" above
160
222
 
161
223
  `options.qrOptions` structure
162
224
 
@@ -175,6 +237,8 @@ imageSize |number |`0.4` |Coeffic
175
237
  margin |number |`0` |Margin of the image in px
176
238
  crossOrigin |string(`'anonymous' 'use-credentials'`)| |Set "anonymous" if you want to download QR code from other origins.
177
239
  saveAsBlob |boolean |`false` |Saves image as base64 blob in svg type, see bellow
240
+ backgroundColor |string | |Draws a plate of this color behind the image
241
+ backgroundRadius |number (`0 - 0.5`) |`0.2` |Corner radius of the plate, relative to its size. `0.5` is round
178
242
 
179
243
  When QR type is svg, the image may not load in certain applications as it is saved as a url, and some svg applications will not render url images for security reasons. Setting `saveAsBlob` to true will instead save the image as a blob, allowing it to render correctly in more places, but will also increase the file size.
180
244
 
@@ -184,7 +248,7 @@ Property|Type
184
248
  --------|------------------------------------------------------------------------------|-------------|-------------------
185
249
  color |string |`'#000'` |Color of QR dots
186
250
  gradient|object | |Gradient of QR dots
187
- type |string (`'rounded' 'dots' 'classy' 'classy-rounded' 'square' 'extra-rounded'`)|`'square'` |Style of QR dots
251
+ type |string (`'rounded' 'dots' 'classy' 'classy-rounded' 'square' 'extra-rounded' 'diamond' 'small-square' 'vertical-lines' 'horizontal-lines' 'fluid'`)|`'square'` |Style of QR dots. With `diamond`, `small-square` and the lines, corners and alignment patterns stay solid unless you set a corner type. `fluid` joins neighbouring dots into soft strokes, with solid rounded corners
188
252
 
189
253
  `options.backgroundOptions` structure
190
254
 
@@ -199,7 +263,7 @@ Property|Type |Default Value|Description
199
263
  --------|-----------------------------------------|-------------|-----------------
200
264
  color |string | |Color of Corners Square
201
265
  gradient|object | |Gradient of Corners Square
202
- type |string (`'dot' 'square' 'extra-rounded'`)| |Style of Corners Square
266
+ type |string (`'dot' 'square' 'extra-rounded' 'rounded'`)| |Style of Corners Square
203
267
 
204
268
  `options.cornersDotOptions` structure
205
269
 
@@ -207,7 +271,7 @@ Property|Type |Default Value|Description
207
271
  --------|-------------------------|-------------|-----------------
208
272
  color |string | |Color of Corners Dot
209
273
  gradient|object | |Gradient of Corners Dot
210
- type |string (`'dot' 'square'`)| |Style of Corners Dot
274
+ type |string (`'dot' 'square' 'rounded'`)| |Style of Corners Dot
211
275
 
212
276
  Gradient structure
213
277
 
package/lib/art/art.d.ts CHANGED
@@ -13,12 +13,7 @@ export interface ArtRect {
13
13
  width: number;
14
14
  height: number;
15
15
  color: string;
16
- }
17
- export interface QRArea {
18
- count: number;
19
- dotSize: number;
20
- x: number;
21
- y: number;
16
+ shape?: "rect" | "circle";
22
17
  }
23
18
  export interface HalftoneArt {
24
19
  mode: "halftone";
@@ -31,11 +26,21 @@ export interface ColorizeArt {
31
26
  /** Full-size canvas holding a light copy of the picture, drawn behind the dots (when art.background). */
32
27
  background?: Canvas;
33
28
  }
34
- export type PreparedArt = HalftoneArt | ColorizeArt;
29
+ export interface BlendArt {
30
+ mode: "blend";
31
+ /** Full-size canvas with the picture, drawn behind everything. */
32
+ picture: Canvas;
33
+ /** Light plates behind the finders, then the dots that correct the picture. */
34
+ rects: ArtRect[];
35
+ /** Dark color picked from the picture around each finder (top-left, top-right, bottom-left). */
36
+ finderColors: string[];
37
+ }
38
+ export type PreparedArt = HalftoneArt | ColorizeArt | BlendArt;
35
39
  /** Default contrast per mode: colorize keeps the dot shapes, so it needs more contrast to scan reliably. */
36
40
  export declare const DEFAULT_CONTRAST: {
37
41
  halftone: number;
38
42
  colorize: number;
43
+ blend: number;
39
44
  };
40
45
  export declare const getContrast: (art: ArtOptions) => number;
41
46
  /** Luminance limits for "dark" and "light" pixels at a given contrast. */
@@ -46,6 +51,5 @@ export declare const getLimits: (contrast: number) => {
46
51
  export declare const toDark: (color: RGB, limit: number) => RGB;
47
52
  export declare const toLight: (color: RGB, limit: number) => RGB;
48
53
  export declare function createCanvas(options: RequiredOptions, width: number, height: number): Canvas;
49
- export declare function getQRArea(options: RequiredOptions, count: number): QRArea;
50
54
  export declare function prepareArt(options: RequiredOptions, qr: QRCode, filter: FilterFunction): Promise<PreparedArt>;
51
55
  export {};
@@ -2,6 +2,6 @@
2
2
  * Which modules of a QR symbol are function patterns (finders, timing, alignment, format and
3
3
  * version information). QR art leaves these solid so scanners can still locate and read the code.
4
4
  */
5
- export type ModuleRole = "finder" | "function" | "data";
5
+ export type ModuleRole = "finder" | "function" | "alignment" | "data";
6
6
  /** Returns a lookup of module roles for a symbol with `count` modules per side. */
7
7
  export default function getModuleRoles(count: number): (row: number, col: number) => ModuleRole;
@@ -8,12 +8,15 @@ export default class QRCanvas {
8
8
  _image?: HTMLImageElement;
9
9
  /** Fill used for the dots instead of their color (QR art "colorize"). */
10
10
  _dotsFill?: CanvasPattern;
11
+ /** Per-finder colors used when corners have no color of their own (QR art "blend"). */
12
+ _finderColors?: string[];
11
13
  constructor(options: RequiredOptions);
12
14
  get context(): CanvasRenderingContext2D | null;
13
15
  get width(): number;
14
16
  get height(): number;
15
17
  getCanvas(): Canvas;
16
18
  clear(): void;
19
+ _drawFramed(qr: QRCode): Promise<void>;
17
20
  drawQR(qr: QRCode): Promise<void>;
18
21
  drawBackground(): void;
19
22
  drawDots(filter?: FilterFunction): void;
@@ -16,6 +16,8 @@ export interface RequiredOptions extends Options {
16
16
  imageSize: number;
17
17
  crossOrigin?: string;
18
18
  margin: number;
19
+ backgroundColor?: string;
20
+ backgroundRadius?: number;
19
21
  };
20
22
  dotsOptions: {
21
23
  type: DotType;
@@ -17,11 +17,15 @@ export default class QRSVG {
17
17
  _virtual: boolean;
18
18
  /** Image used to paint the dots instead of their color (QR art "colorize"). */
19
19
  _dotsImageHref?: string;
20
- constructor(options: RequiredOptions);
20
+ /** Per-finder colors used when corners have no color of their own (QR art "blend"). */
21
+ _finderColors?: string[];
22
+ constructor(options: RequiredOptions, sharedWindow?: Window);
21
23
  get width(): number;
22
24
  get height(): number;
23
25
  getElement(): SVGElement;
24
26
  clear(): void;
27
+ _svgElement(name: string, attributes: Record<string, string | number>): SVGElement;
28
+ _drawFramed(qr: QRCode): Promise<void>;
25
29
  drawQR(qr: QRCode): Promise<void>;
26
30
  drawBackground(): void;
27
31
  drawDots(filter?: FilterFunction): void;
@@ -1,3 +1,4 @@
1
+ import { Shape } from "../../shapes";
1
2
  import { DotType, GetNeighbor, RotateFigureArgsCanvas, BasicFigureDrawArgsCanvas, DrawArgsCanvas } from "../../../types";
2
3
  export default class QRDot {
3
4
  _context: CanvasRenderingContext2D;
@@ -7,6 +8,7 @@ export default class QRDot {
7
8
  type: DotType;
8
9
  });
9
10
  draw(x: number, y: number, size: number, getNeighbor: GetNeighbor): void;
11
+ _drawShape(shape: Shape): void;
10
12
  _rotateFigure({ x, y, size, context, rotation, draw }: RotateFigureArgsCanvas): void;
11
13
  _basicDot(args: BasicFigureDrawArgsCanvas): void;
12
14
  _basicSquare(args: BasicFigureDrawArgsCanvas): void;
@@ -1,3 +1,4 @@
1
+ import { Shape } from "../../shapes";
1
2
  import { DotType, GetNeighbor, DrawArgs, BasicFigureDrawArgs, RotateFigureArgs, Window } from "../../../types";
2
3
  export default class QRDot {
3
4
  _element?: SVGElement;
@@ -10,6 +11,7 @@ export default class QRDot {
10
11
  window: Window;
11
12
  });
12
13
  draw(x: number, y: number, size: number, getNeighbor: GetNeighbor): void;
14
+ _drawShape(shape: Shape): void;
13
15
  _rotateFigure({ x, y, size, rotation, draw }: RotateFigureArgs): void;
14
16
  _basicDot(args: BasicFigureDrawArgs): void;
15
17
  _basicSquare(args: BasicFigureDrawArgs): void;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Shapes shared by the canvas and SVG renderers. A shape is a list of path commands in absolute
3
+ * coordinates; `toCanvas` replays them on a 2D context and `toSVGPath` turns them into a path "d".
4
+ * Sub-paths of one module never overlap their neighbours, so the even-odd fill used by the
5
+ * renderers only cuts the holes that are meant to be holes (corner rings).
6
+ */
7
+ export type PathCommand = {
8
+ op: "M" | "L";
9
+ x: number;
10
+ y: number;
11
+ }
12
+ /** Circular arc around (cx, cy), angles in radians, clockwise on screen unless anticlockwise. */
13
+ | {
14
+ op: "A";
15
+ cx: number;
16
+ cy: number;
17
+ r: number;
18
+ start: number;
19
+ end: number;
20
+ anticlockwise?: boolean;
21
+ } | {
22
+ op: "Z";
23
+ };
24
+ export type Shape = PathCommand[];
25
+ export declare function toCanvas(context: CanvasRenderingContext2D, shape: Shape): void;
26
+ export declare function toSVGPath(shape: Shape): string;
27
+ export declare const diamond: (x: number, y: number, size: number) => Shape;
28
+ export declare const rect: (x: number, y: number, width: number, height: number) => Shape;
29
+ export declare const smallSquare: (x: number, y: number, size: number) => Shape;
30
+ /**
31
+ * A bar that joins its neighbours along one axis and gets a round cap where it has none.
32
+ * `before`/`after` are the neighbours above/below (vertical) or left/right (horizontal).
33
+ */
34
+ export declare const line: (x: number, y: number, size: number, vertical: boolean, before: boolean, after: boolean) => Shape;
35
+ /** Rounded rectangle, drawn clockwise. */
36
+ export declare const roundedRect: (x: number, y: number, width: number, height: number, radius: number) => Shape;
37
+ /** Finder ring with rounded corners: outer square minus inner square (even-odd). */
38
+ export declare const roundedRing: (x: number, y: number, size: number) => Shape;
39
+ export declare const roundedSquare: (x: number, y: number, size: number) => Shape;
40
+ /**
41
+ * "fluid" dot: a rounded square that flows into its dark neighbours, so runs of modules read as
42
+ * soft connected strokes and a lone module is a circle. Arms stay inside the module's own cell,
43
+ * so neighbouring shapes touch but never overlap.
44
+ */
45
+ export declare const fluid: (x: number, y: number, size: number, neighbors: {
46
+ left: boolean;
47
+ right: boolean;
48
+ top: boolean;
49
+ bottom: boolean;
50
+ }) => Shape;