@journey-to-code/symbology 0.0.0-stage → 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.
package/schema.js ADDED
@@ -0,0 +1,131 @@
1
+ // The style contract as JSON Schema (2020-12), written for both validators and language models:
2
+ // every description says what a field does to the printed code. `normalizeStyle` in style.js is
3
+ // the enforcing check; this schema is what tools and models are shown. A test keeps them in step.
4
+
5
+ import { EYE_BALLS, EYE_FRAMES, MAX_LOGO_SIZE, MIN_MODULE_SCALE, MODULE_SHAPES, PRESETS } from './style.js';
6
+
7
+ const color = {
8
+ type: 'string',
9
+ pattern: '^(#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6}|rgb\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*\\))$',
10
+ description: 'A solid colour: #rgb, #rrggbb, or rgb(r, g, b). No transparency.',
11
+ };
12
+
13
+ const paint = {
14
+ description: 'A solid colour, or a gradient whose every stop is dark enough to contrast with the background.',
15
+ anyOf: [
16
+ color,
17
+ {
18
+ type: 'object',
19
+ additionalProperties: false,
20
+ required: ['type', 'stops'],
21
+ properties: {
22
+ type: { enum: ['linear', 'radial'] },
23
+ angle: { type: 'number', minimum: 0, maximum: 360, description: 'Linear only: degrees, 0 left to right, 90 top to bottom.' },
24
+ stops: { type: 'array', minItems: 2, maxItems: 4, items: color, description: 'Colours from start to end.' },
25
+ },
26
+ },
27
+ ],
28
+ };
29
+
30
+ const modules = {
31
+ type: 'object',
32
+ additionalProperties: false,
33
+ description: 'The small squares that carry the data.',
34
+ properties: {
35
+ shape: {
36
+ enum: MODULE_SHAPES,
37
+ description: 'square is crispest; dot and rounded are friendly; diamond is sharp; squircle is soft; liquid merges neighbours into flowing blobs; bars-vertical and bars-horizontal join runs into pills.',
38
+ },
39
+ scale: { type: 'number', minimum: MIN_MODULE_SCALE, maximum: 1, description: 'Size of each shape within its cell. Smaller looks airy but prints fainter; 0.85 to 1 is safest.' },
40
+ },
41
+ };
42
+
43
+ const eyes = {
44
+ type: 'object',
45
+ additionalProperties: false,
46
+ description: 'The three large corner squares (finder patterns) scanners lock on to.',
47
+ properties: {
48
+ frame: { enum: EYE_FRAMES, description: 'Outer ring shape.' },
49
+ ball: { enum: EYE_BALLS, description: 'Inner square shape.' },
50
+ },
51
+ };
52
+
53
+ const colors = {
54
+ type: 'object',
55
+ additionalProperties: false,
56
+ properties: {
57
+ foreground: paint,
58
+ background: { ...color, description: 'Solid and light. Keep it the lightest colour in the design.' },
59
+ eyeFrame: { ...paint, description: 'Optional accent for the eye rings; defaults to the foreground.' },
60
+ eyeBall: { ...paint, description: 'Optional accent for the eye centres; defaults to the eye frame colour.' },
61
+ },
62
+ };
63
+
64
+ const logo = {
65
+ type: 'object',
66
+ additionalProperties: false,
67
+ description: 'Placement of the centred logo. The image itself is supplied by the application.',
68
+ properties: {
69
+ size: { type: 'number', minimum: 0.05, maximum: MAX_LOGO_SIZE, description: 'Logo width as a fraction of the code. 0.18 to 0.25 suits most URLs; larger needs a shorter URL.' },
70
+ shape: { enum: ['circle', 'square'], description: 'Shape of the clear area behind the logo.' },
71
+ padding: { type: 'number', minimum: 0, maximum: 2, description: 'Clear space around the logo, in modules.' },
72
+ plate: { ...color, description: 'Colour behind the logo; defaults to the background. Keep it light.' },
73
+ },
74
+ };
75
+
76
+ const frame = {
77
+ type: 'object',
78
+ additionalProperties: false,
79
+ description: 'An optional border with a call to action below the code.',
80
+ properties: {
81
+ text: { type: 'string', maxLength: 32, description: 'Short call to action, for example SCAN ME or VIEW MY PORTFOLIO.' },
82
+ color,
83
+ textColor: color,
84
+ },
85
+ };
86
+
87
+ /** The style object `styledQr()` accepts, minus the logo image itself (which the caller supplies). */
88
+ export const styleSchema = {
89
+ $schema: 'https://json-schema.org/draft/2020-12/schema',
90
+ $id: 'https://github.com/journey-to-code-org/symbology/style.schema.json',
91
+ title: 'Symbology QR style',
92
+ description:
93
+ 'How to draw a QR code for a URL. Scanning comes first: foreground colours must be clearly darker than a light background, and a logo is only allowed where error correction can recover what it hides. Anything outside these limits is refused.',
94
+ type: 'object',
95
+ additionalProperties: false,
96
+ properties: {
97
+ preset: { enum: Object.keys(PRESETS), description: 'A starting point; other fields override it.' },
98
+ modules,
99
+ eyes,
100
+ colors,
101
+ logo,
102
+ frame,
103
+ margin: { type: 'integer', minimum: 4, maximum: 8, description: 'Quiet zone around the code, in modules. 4 is the standard minimum.' },
104
+ level: { enum: ['auto', 'L', 'M', 'Q', 'H'], description: 'Error correction. auto picks the strongest that fits; leave it on auto.' },
105
+ maxVersion: { type: 'integer', minimum: 1, maximum: 10, description: 'Largest symbol version allowed. Lower keeps modules bigger on small cards.' },
106
+ safety: { type: 'number', minimum: 0.1, maximum: 0.8, description: 'Share of the error-correction budget a logo may use; the rest is kept for wear and glare. 0.5 is a good default.' },
107
+ minContrast: { type: 'number', minimum: 3, maximum: 21, description: 'Least contrast ratio allowed between any foreground colour and the background. Default 4.' },
108
+ title: { type: 'string', maxLength: 200, description: 'Accessible name, for example "QR code for Jane Doe\'s portfolio".' },
109
+ },
110
+ };
111
+
112
+ /** The same schema with `logo.svg`, for asking a model to draw a simple mark as well. */
113
+ export const styleWithLogoSchema = {
114
+ ...styleSchema,
115
+ $id: 'https://github.com/journey-to-code-org/symbology/style-with-logo.schema.json',
116
+ properties: {
117
+ ...styleSchema.properties,
118
+ logo: {
119
+ ...logo,
120
+ description: 'The centred logo, including svg: a small self-contained SVG mark.',
121
+ properties: {
122
+ ...logo.properties,
123
+ svg: {
124
+ type: 'string',
125
+ maxLength: 8000,
126
+ description: 'A simple flat SVG, for example a monogram drawn with <path> elements in one or two colours. Requires a viewBox; no scripts, links, external images, or <text> (fonts vary).',
127
+ },
128
+ },
129
+ },
130
+ },
131
+ };
package/style.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ import type { Layout, Level, LogoBudget, Qr } from './qr.js';
2
+
3
+ export type ModuleShape = 'square' | 'dot' | 'rounded' | 'diamond' | 'squircle' | 'liquid' | 'bars-vertical' | 'bars-horizontal';
4
+ export type EyeFrame = 'square' | 'rounded' | 'circle' | 'leaf';
5
+ export type EyeBall = 'square' | 'rounded' | 'circle' | 'diamond' | 'leaf';
6
+ export type PresetName = 'classic' | 'corporate' | 'minimal-dots' | 'liquid' | 'monochrome-print' | 'bars';
7
+
8
+ /** `#rgb`, `#rrggbb`, or `rgb(r, g, b)`. */
9
+ export type Color = string;
10
+ export type Paint = Color | { type: 'linear'; angle?: number; stops: Color[] } | { type: 'radial'; stops: Color[] };
11
+
12
+ export interface LogoStyle {
13
+ /** SVG markup; checked, then embedded as a data URI. */
14
+ svg?: string;
15
+ /** A base64 data URI of a PNG, JPEG, WebP, or SVG image. */
16
+ href?: string;
17
+ /** Fraction of the symbol, 0.05 to 0.4. Default 0.22. */
18
+ size?: number;
19
+ shape?: 'circle' | 'square';
20
+ /** Clear space in modules, 0 to 2. Default 0.5. */
21
+ padding?: number;
22
+ /** Colour behind the logo; defaults to the background. */
23
+ plate?: Color;
24
+ }
25
+
26
+ export interface QrStyle {
27
+ preset?: PresetName;
28
+ modules?: { shape?: ModuleShape; scale?: number };
29
+ eyes?: { frame?: EyeFrame; ball?: EyeBall };
30
+ colors?: { foreground?: Paint; background?: Color; eyeFrame?: Paint; eyeBall?: Paint };
31
+ logo?: LogoStyle | null;
32
+ frame?: { text?: string; color?: Color; textColor?: Color } | null;
33
+ /** Quiet zone, 4 to 8 modules. */
34
+ margin?: number;
35
+ level?: Level | 'auto';
36
+ maxVersion?: number;
37
+ /** Share of the correction budget a logo may use, 0.1 to 0.8. Default 0.5. */
38
+ safety?: number;
39
+ /** Least contrast ratio between foreground colours and the background. Default 4. */
40
+ minContrast?: number;
41
+ title?: string;
42
+ }
43
+
44
+ export interface Check {
45
+ name: string;
46
+ ok: boolean;
47
+ }
48
+
49
+ export interface StyleReport {
50
+ /** True only when the style is valid and every read-back check passed. */
51
+ ok: boolean;
52
+ errors: string[];
53
+ warnings: string[];
54
+ score: number;
55
+ checks: Check[];
56
+ level?: Level;
57
+ version?: number;
58
+ mask?: number;
59
+ size?: number;
60
+ budget?: LogoBudget;
61
+ contrast?: number;
62
+ verified?: boolean;
63
+ print?: { widthModules: number; minWidthMm: number };
64
+ }
65
+
66
+ export interface StyledResult {
67
+ /** The SVG, or null when `report.ok` is false. */
68
+ svg: string | null;
69
+ report: StyleReport;
70
+ layout: Layout | null;
71
+ /** The normalized style, or null when it was invalid. */
72
+ style: object | null;
73
+ }
74
+
75
+ export const MODULE_SHAPES: readonly ModuleShape[];
76
+ export const EYE_FRAMES: readonly EyeFrame[];
77
+ export const EYE_BALLS: readonly EyeBall[];
78
+ export const LEVELS: readonly (Level | 'auto')[];
79
+ export const PRESETS: Readonly<Record<PresetName, QrStyle>>;
80
+ export const MIN_MODULE_SCALE: number;
81
+ export const MAX_LOGO_SIZE: number;
82
+ export const MAX_LOGO_BYTES: number;
83
+ export const PRINT_MODULE_MM: number;
84
+
85
+ export function styledQr(qr: Qr, data: string | Uint8Array, style?: QrStyle, options?: { verify?: boolean; pixelSize?: number }): StyledResult;
86
+ export function normalizeStyle(style?: QrStyle): { style: object | null; errors: string[]; warnings: string[] };
87
+ export function sanitizeLogoSvg(markup: string): { href: string | null; errors: string[] };
88
+ export function checkUrl(data: string | Uint8Array): { bytes: number; warnings: string[] };
89
+ export function parseColor(value: string): [number, number, number] | null;
90
+ export function luminance(rgb: [number, number, number]): number;
91
+ export function contrast(a: [number, number, number], b: [number, number, number]): number;