qrdx 0.0.53 → 0.0.55

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.
Files changed (114) hide show
  1. package/README.md +1 -1
  2. package/dist/core.js +7 -7
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.mjs +7 -7
  5. package/dist/core.mjs.map +1 -1
  6. package/dist/element.browser.mjs +6 -67
  7. package/dist/element.browser.mjs.map +1 -1
  8. package/dist/index.js +7 -7
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +7 -7
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/qr-code-canvas.js +1 -1
  13. package/dist/qr-code-canvas.js.map +1 -1
  14. package/dist/qr-code-canvas.mjs +1 -1
  15. package/dist/qr-code-canvas.mjs.map +1 -1
  16. package/dist/schemas.js +2 -0
  17. package/dist/schemas.js.map +1 -0
  18. package/dist/schemas.mjs +2 -0
  19. package/dist/schemas.mjs.map +1 -0
  20. package/dist/src/art.d.ts +1 -1
  21. package/dist/src/art.d.ts.map +1 -1
  22. package/dist/src/engine.d.ts +2 -1
  23. package/dist/src/engine.d.ts.map +1 -1
  24. package/dist/src/eps.d.ts.map +1 -1
  25. package/dist/src/frame-import.d.ts +1 -1
  26. package/dist/src/frame-import.d.ts.map +1 -1
  27. package/dist/src/frame-spec.d.ts +5 -4
  28. package/dist/src/frame-spec.d.ts.map +1 -1
  29. package/dist/src/frame.d.ts +1 -1
  30. package/dist/src/frame.d.ts.map +1 -1
  31. package/dist/src/frames/builtin.d.ts +1 -1
  32. package/dist/src/frames/builtin.d.ts.map +1 -1
  33. package/dist/src/qr-outline.d.ts +1 -2
  34. package/dist/src/qr-outline.d.ts.map +1 -1
  35. package/dist/src/qr-shape.d.ts +1 -1
  36. package/dist/src/qr-shape.d.ts.map +1 -1
  37. package/dist/src/silhouette.d.ts +1 -2
  38. package/dist/src/silhouette.d.ts.map +1 -1
  39. package/dist/src/template/dot-ring-layout.d.ts +26 -3
  40. package/dist/src/template/dot-ring-layout.d.ts.map +1 -1
  41. package/dist/src/template/dot-ring.d.ts +22 -4
  42. package/dist/src/template/dot-ring.d.ts.map +1 -1
  43. package/dist/src/utils.d.ts +1 -1
  44. package/dist/src/utils.d.ts.map +1 -1
  45. package/dist/types/art.d.ts +1 -18
  46. package/dist/types/art.d.ts.map +1 -1
  47. package/dist/types/color-helpers.d.ts +26 -0
  48. package/dist/types/color-helpers.d.ts.map +1 -0
  49. package/dist/types/color.d.ts +1 -24
  50. package/dist/types/color.d.ts.map +1 -1
  51. package/dist/types/constants.d.ts +97 -0
  52. package/dist/types/constants.d.ts.map +1 -0
  53. package/dist/types/corner-dot.d.ts +2 -12
  54. package/dist/types/corner-dot.d.ts.map +1 -1
  55. package/dist/types/corner-eye.d.ts +2 -11
  56. package/dist/types/corner-eye.d.ts.map +1 -1
  57. package/dist/types/frame-spec-parse.d.ts +21 -0
  58. package/dist/types/frame-spec-parse.d.ts.map +1 -0
  59. package/dist/types/frame-spec-rules.d.ts +95 -0
  60. package/dist/types/frame-spec-rules.d.ts.map +1 -0
  61. package/dist/types/frame-spec.d.ts +2 -83
  62. package/dist/types/frame-spec.d.ts.map +1 -1
  63. package/dist/types/frame.d.ts +1 -8
  64. package/dist/types/frame.d.ts.map +1 -1
  65. package/dist/types/image-pattern.d.ts +1 -1
  66. package/dist/types/image-pattern.d.ts.map +1 -1
  67. package/dist/types/index.d.ts +3 -22
  68. package/dist/types/index.d.ts.map +1 -1
  69. package/dist/types/index.js +2 -2
  70. package/dist/types/index.js.map +1 -1
  71. package/dist/types/index.mjs +2 -2
  72. package/dist/types/index.mjs.map +1 -1
  73. package/dist/types/plain.d.ts +17 -0
  74. package/dist/types/plain.d.ts.map +1 -0
  75. package/dist/types/qr.d.ts +2 -46
  76. package/dist/types/qr.d.ts.map +1 -1
  77. package/dist/types/schemas.d.ts +12 -0
  78. package/dist/types/schemas.d.ts.map +1 -0
  79. package/dist/vanilla.browser.mjs +28 -28
  80. package/dist/vanilla.browser.mjs.map +1 -1
  81. package/package.json +3 -3
  82. package/src/art.tsx +3 -6
  83. package/src/body-patterns.tsx +1 -1
  84. package/src/engine.ts +53 -1
  85. package/src/eps.ts +6 -1
  86. package/src/frame-import.ts +7 -6
  87. package/src/frame-spec.ts +17 -23
  88. package/src/frame.tsx +3 -2
  89. package/src/frames/builtin.ts +2 -5
  90. package/src/helpers.ts +1 -1
  91. package/src/qr-outline.ts +2 -2
  92. package/src/qr-shape.ts +2 -5
  93. package/src/rasterize.ts +1 -1
  94. package/src/scan-score.ts +2 -2
  95. package/src/silhouette.ts +2 -2
  96. package/src/template/dot-ring-layout.ts +281 -15
  97. package/src/template/dot-ring.tsx +27 -8
  98. package/src/utils/print-shared.ts +1 -1
  99. package/src/utils.tsx +6 -5
  100. package/types/art.ts +10 -20
  101. package/types/color-helpers.ts +132 -0
  102. package/types/color.ts +9 -122
  103. package/types/constants.ts +222 -0
  104. package/types/corner-dot.ts +8 -40
  105. package/types/corner-eye.ts +2 -31
  106. package/types/frame-spec-parse.ts +683 -0
  107. package/types/frame-spec-rules.ts +645 -0
  108. package/types/frame-spec.ts +38 -635
  109. package/types/frame.ts +14 -34
  110. package/types/image-pattern.ts +2 -26
  111. package/types/index.ts +6 -162
  112. package/types/plain.ts +117 -0
  113. package/types/qr.ts +29 -51
  114. package/types/schemas.ts +62 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qrdx",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
4
4
  "description": "Customizable QR code generator for React and plain JavaScript: SVG and canvas rendering, dot and eye patterns, gradients, logos, frames, Logo QR and Image QR art, PNG/JPG/WebP/SVG/PDF/EPS export, a scan score and QR scanning.",
5
5
  "keywords": [
6
6
  "qr-code",
@@ -20,11 +20,11 @@
20
20
  ],
21
21
  "homepage": "https://qrdx.dev/docs",
22
22
  "bugs": {
23
- "url": "https://github.com/bucharitesh/qrdx/issues"
23
+ "url": "https://github.com/qrdxinc/qrdx/issues"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
27
- "url": "git+https://github.com/bucharitesh/qrdx.git",
27
+ "url": "git+https://github.com/qrdxinc/qrdx.git",
28
28
  "directory": "packages/qrdx"
29
29
  },
30
30
  "license": "MIT",
package/src/art.tsx CHANGED
@@ -24,16 +24,13 @@
24
24
  // value (after Chu et al., "Halftone QR Codes", SIGGRAPH Asia 2013).
25
25
  // Finder, alignment and format patterns always stay crisp.
26
26
 
27
+ import type { QRArt, QRArtImage, QRArtMode, QRArtVariant } from "../types/art";
28
+ import type { Modules } from "../types/common";
27
29
  import {
28
30
  QR_ART_MAX_SAMPLE_SIZE,
29
31
  QR_ART_MODES,
30
32
  QR_ART_VARIANTS,
31
- type QRArt,
32
- type QRArtImage,
33
- type QRArtMode,
34
- type QRArtVariant,
35
- } from "../types/art";
36
- import type { Modules } from "../types/common";
33
+ } from "../types/constants";
37
34
  import type { EyePosition } from "../types/qr";
38
35
  import {
39
36
  type ArtBitmap,
@@ -2,7 +2,7 @@
2
2
  /** biome-ignore-all lint/style/noMagicNumbers: module geometry */
3
3
 
4
4
  import type { BodyPattern, Modules } from "../types";
5
- import { MODULE_SCALE_MAX, MODULE_SCALE_MIN } from "../types/qr";
5
+ import { MODULE_SCALE_MAX, MODULE_SCALE_MIN } from "../types/constants";
6
6
  import type { QRNode } from "./jsx/runtime";
7
7
  import {
8
8
  circlePath,
package/src/engine.ts CHANGED
@@ -3,7 +3,59 @@
3
3
  // library's data (core.ts adds it). The no-bundler browser build of
4
4
  // qrdx/vanilla starts here, so a page doesn't download the whole library.
5
5
 
6
- export * from "../types";
6
+ // The zod-free part of qrdx/types, and the schemas from a module of their
7
+ // own: tsup.config.ts builds that as dist/schemas.mjs, which the core and
8
+ // the React entry only re-export, so zod stays out of an app that never
9
+ // uses a schema (nothing here draws with one). Named, not `export *`: a
10
+ // bundled `export *` of another file becomes a runtime copy of its exports,
11
+ // which bundlers can't tree-shake (tests/core-bundles.test.ts checks the
12
+ // list against types/schemas.ts).
13
+ export * from "../types/plain";
14
+ export {
15
+ angularGradientSchema,
16
+ anyFrameSchema,
17
+ baseTemplatePropsSchema,
18
+ bodyPatternSchema,
19
+ colorConfigSchema,
20
+ colorTypeSchema,
21
+ cornerEyeDotPatternSchema,
22
+ cornerEyePatternSchema,
23
+ customFrameSchema,
24
+ errorLevelSchema,
25
+ excavationSchema,
26
+ eyeColorOverrideSchema,
27
+ eyeColorsSchema,
28
+ frameSchema,
29
+ frameSpecColorSchema,
30
+ frameSpecLabelSchema,
31
+ frameSpecPaintSchema,
32
+ frameSpecSchema,
33
+ frameSpecShapeSchema,
34
+ frameStyleSchema,
35
+ getFrameSpecIssues,
36
+ gradientStopSchema,
37
+ imageFitSchema,
38
+ imagePaintSchema,
39
+ imagePositionSchema,
40
+ imageSettingsSchema,
41
+ linearGradientSchema,
42
+ logoOptionsSchema,
43
+ logoShapeSchema,
44
+ pickableCornerEyeDotPatternSchema,
45
+ pickableCornerEyePatternSchema,
46
+ qrArtImageSchema,
47
+ qrArtModeSchema,
48
+ qrArtSchema,
49
+ qrArtVariantSchema,
50
+ qrCodeShapeSchema,
51
+ qrPropsSchema,
52
+ qrShapeSchema,
53
+ qrSilhouetteSchema,
54
+ radialGradientSchema,
55
+ solidColorSchema,
56
+ templateConfigSchema,
57
+ templateDefinitionBaseSchema,
58
+ } from "../types/schemas";
7
59
  export {
8
60
  createQRArtImage,
9
61
  DEFAULT_ART_INTENSITY,
package/src/eps.ts CHANGED
@@ -272,13 +272,18 @@ qrdxdict begin
272
272
  /QRDX-Bold /Helvetica-Bold latin1
273
273
  end`;
274
274
 
275
- /** PostScript string literal; characters outside Latin-1 become "?". */
275
+ /**
276
+ * PostScript string literal; "…" becomes "..." and other characters outside
277
+ * Latin-1 become "?".
278
+ */
276
279
  function psString(text: string): string {
277
280
  let out = "(";
278
281
  for (const char of text) {
279
282
  const code = char.codePointAt(0) ?? 63;
280
283
  if (char === "(" || char === ")" || char === "\\") {
281
284
  out += `\\${char}`;
285
+ } else if (char === "…") {
286
+ out += "...";
282
287
  } else if (code >= 32 && code < 127) {
283
288
  out += char;
284
289
  } else if (code >= 160 && code <= 255) {
@@ -16,15 +16,16 @@
16
16
  // attribute is ever copied, so on* handlers, href / xlink:href and
17
17
  // url(...) references go with them (internal gradients are flattened to
18
18
  // their first color);
19
- // - segment, shape and color caps, and the result must pass frameSpecSchema.
19
+ // - segment, shape and color caps, and the result must pass frameSpecSchema (checked without zod, by
20
+ // parseFrameSpec).
20
21
  // Everything dropped is reported as a warning.
22
+ import type { FrameSpec } from "../types/frame-spec";
23
+ import { parseFrameSpec } from "../types/frame-spec-parse";
21
24
  import {
22
25
  FRAME_SPEC_KIND,
23
26
  FRAME_SPEC_LIMITS,
24
27
  FRAME_SPEC_VERSION,
25
- type FrameSpec,
26
- frameSpecSchema,
27
- } from "../types/frame-spec";
28
+ } from "../types/frame-spec-rules";
28
29
  import { fmt } from "./utils/fmt";
29
30
  import { parseColor } from "./utils/parse-color";
30
31
  import {
@@ -1167,9 +1168,9 @@ export function importFrameSvg(
1167
1168
  labels: [],
1168
1169
  };
1169
1170
 
1170
- const result = frameSpecSchema.safeParse(spec);
1171
+ const result = parseFrameSpec(spec);
1171
1172
  if (!result.success) {
1172
- const issue = result.error.issues[0];
1173
+ const issue = result.issues[0];
1173
1174
  throw new ImportError(
1174
1175
  `The converted frame doesn't fit the frame format: ${issue?.message ?? "invalid"}`
1175
1176
  );
package/src/frame-spec.ts CHANGED
@@ -6,26 +6,25 @@
6
6
  // (src/frames/builtin). Path data is re-written from the numbers it was
7
7
  // read as, never passed through.
8
8
  import type { ColorConfig } from "../types/color";
9
- import {
10
- FRAME_FONT_SCALE_MAX,
11
- FRAME_FONT_SCALE_MIN,
12
- type QRFrame,
13
- } from "../types/frame";
9
+ import { FRAME_FONT_SCALE_MAX, FRAME_FONT_SCALE_MIN } from "../types/constants";
10
+ import type { QRFrame } from "../types/frame";
11
+ import type {
12
+ FrameSpec,
13
+ FrameSpecColor,
14
+ FrameSpecLabel,
15
+ FrameSpecPaint,
16
+ FrameSpecShape,
17
+ QRCustomFrame,
18
+ } from "../types/frame-spec";
19
+ import { frameSpecIssueText, parseFrameSpec } from "../types/frame-spec-parse";
14
20
  import {
15
21
  FRAME_SPEC_DEFAULT_FONT,
16
22
  FRAME_SPEC_LABEL_MAX_FONT,
17
23
  FRAME_SPEC_LIMITS,
18
- type FrameSpec,
19
- type FrameSpecColor,
20
- type FrameSpecLabel,
21
- type FrameSpecPaint,
22
- type FrameSpecShape,
23
24
  frameSpecPadding,
24
- frameSpecSchema,
25
25
  frameSpecShapeBounds,
26
- type QRCustomFrame,
27
26
  sanitizeFrameText,
28
- } from "../types/frame-spec";
27
+ } from "../types/frame-spec-rules";
29
28
  import { DEFAULT_BGCOLOR } from "./constants";
30
29
  import type { FrameLabel, FrameLayout, FrameShape } from "./frame";
31
30
  import { FRAME_ICON_BOX, getFrameIconPath } from "./frame-icons";
@@ -66,8 +65,9 @@ export class FrameSpecError extends Error {
66
65
  const validated = new WeakMap<object, FrameSpec>();
67
66
 
68
67
  /**
69
- * Validates `spec` with `frameSpecSchema` (every limit applies) and returns
70
- * the parsed copy. Throws a `FrameSpecError` listing the problems.
68
+ * Validates `spec` as `frameSpecSchema` does (every limit applies, the same
69
+ * problems reported, without loading zod) and returns the parsed copy.
70
+ * Throws a `FrameSpecError` listing the problems.
71
71
  */
72
72
  export function resolveFrameSpec(spec: unknown): FrameSpec {
73
73
  if (spec && typeof spec === "object") {
@@ -79,15 +79,9 @@ export function resolveFrameSpec(spec: unknown): FrameSpec {
79
79
  if (spec === undefined || spec === null) {
80
80
  throw new FrameSpecError(['A "custom" frame needs a spec']);
81
81
  }
82
- const result = frameSpecSchema.safeParse(spec);
82
+ const result = parseFrameSpec(spec);
83
83
  if (!result.success) {
84
- throw new FrameSpecError(
85
- result.error.issues.map((issue) =>
86
- issue.path.length > 0
87
- ? `${issue.path.join(".")}: ${issue.message}`
88
- : issue.message
89
- )
90
- );
84
+ throw new FrameSpecError(result.issues.map(frameSpecIssueText));
91
85
  }
92
86
  if (typeof spec === "object") {
93
87
  validated.set(spec, result.data);
package/src/frame.tsx CHANGED
@@ -1,8 +1,9 @@
1
1
  /** @jsxImportSource ./jsx */
2
2
  /** biome-ignore-all lint/style/noMagicNumbers: frame proportions */
3
3
  import type { ColorConfig } from "../types";
4
- import { DEFAULT_FRAME_TEXT, FRAME_TEXT_MAX_LENGTH } from "../types/frame";
5
- import { type AnyQRFrame, CUSTOM_FRAME_STYLE } from "../types/frame-spec";
4
+ import { DEFAULT_FRAME_TEXT, FRAME_TEXT_MAX_LENGTH } from "../types/constants";
5
+ import type { AnyQRFrame } from "../types/frame-spec";
6
+ import { CUSTOM_FRAME_STYLE } from "../types/frame-spec-rules";
6
7
  import { getCustomFrameLayout, getFrameSpecGapRatio } from "./frame-spec";
7
8
  import { frameSpecOf, getBuiltInFrameLayout } from "./frames/builtin";
8
9
  import { cloneElement, type QRNode } from "./jsx/runtime";
@@ -13,11 +13,8 @@
13
13
  // hand-written layout for the style drew up to qrdx 0.0.51.
14
14
  import type { ColorConfig } from "../../types/color";
15
15
  import type { FrameStyle, QRFrame } from "../../types/frame";
16
- import {
17
- type AnyQRFrame,
18
- CUSTOM_FRAME_STYLE,
19
- type FrameSpec,
20
- } from "../../types/frame-spec";
16
+ import type { AnyQRFrame, FrameSpec } from "../../types/frame-spec";
17
+ import { CUSTOM_FRAME_STYLE } from "../../types/frame-spec-rules";
21
18
  import type { FrameLayout } from "../frame";
22
19
  import {
23
20
  type FrameSlotFields,
package/src/helpers.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ErrorLevel, ImageSettings, LogoOptions, QRProps } from "../types";
2
- import { LOGO_SIZE_DEFAULT, LOGO_SIZE_MAX } from "../types/qr";
2
+ import { LOGO_SIZE_DEFAULT, LOGO_SIZE_MAX } from "../types/constants";
3
3
  import qrcodegen from "./codegen";
4
4
  import { DEFAULT_LEVEL, DEFAULT_MARGIN, ERROR_LEVEL_MAP } from "./constants";
5
5
  import { fitLogoWidth } from "./logo-budget";
package/src/qr-outline.ts CHANGED
@@ -11,8 +11,8 @@ import {
11
11
  QR_SILHOUETTE_MAX_CELLS,
12
12
  QR_SILHOUETTE_MIN_CELLS,
13
13
  QR_SILHOUETTE_MIN_SHARE,
14
- type QRSilhouette,
15
- } from "../types/qr";
14
+ } from "../types/constants";
15
+ import type { QRSilhouette } from "../types/qr";
16
16
  import { fmt } from "./utils/fmt";
17
17
 
18
18
  type Point = [number, number];
package/src/qr-shape.ts CHANGED
@@ -7,11 +7,8 @@
7
7
  // decorative modules on the same grid, so the code reads as one pattern.
8
8
  // Decoders only ever see the symbol: the ring separates it from the noise.
9
9
  import type { Modules } from "../types";
10
- import {
11
- QR_CIRCLE_QUIET_MIN,
12
- type QRCodeShape,
13
- type QRSilhouette,
14
- } from "../types/qr";
10
+ import { QR_CIRCLE_QUIET_MIN } from "../types/constants";
11
+ import type { QRCodeShape, QRSilhouette } from "../types/qr";
15
12
  import {
16
13
  getHeartOutline,
17
14
  getSilhouetteOutline,
package/src/rasterize.ts CHANGED
@@ -17,7 +17,7 @@ export interface RasterLogo {
17
17
  dataUrl: string;
18
18
  }
19
19
 
20
- /** Largest logo the rasterizer loads (the cap apps/docs/lib/qr-api/logo.ts uses). */
20
+ /** Largest logo the rasterizer loads (the cap apps/app/lib/qr-api/logo.ts uses). */
21
21
  const LOGO_MAX_BYTES = 1024 * 1024;
22
22
 
23
23
  const LOGO_FETCH_TIMEOUT_MS = 10_000;
package/src/scan-score.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /** biome-ignore-all lint/style/noMagicNumbers: scoring weights and thresholds */
2
2
  import type { QRCode as JsQRResult } from "jsqr";
3
3
  import type { ColorConfig, QRProps } from "../types";
4
- import { normalizeColorConfig } from "../types/color";
5
- import { EYE_POSITIONS } from "../types/qr";
4
+ import { normalizeColorConfig } from "../types/color-helpers";
5
+ import { EYE_POSITIONS } from "../types/constants";
6
6
  import type { QRArtPixels } from "./art";
7
7
  import qrcodegen from "./codegen";
8
8
  import {
package/src/silhouette.ts CHANGED
@@ -9,8 +9,8 @@
9
9
  import {
10
10
  QR_SILHOUETTE_MAX_CELLS,
11
11
  QR_SILHOUETTE_MIN_CELLS,
12
- type QRSilhouette,
13
- } from "../types/qr";
12
+ } from "../types/constants";
13
+ import type { QRSilhouette } from "../types/qr";
14
14
  import type { QRArtPixels } from "./art";
15
15
 
16
16
  /** Fewest pixels across the shape itself (after cropping) worth tracing. */
@@ -5,7 +5,8 @@
5
5
  // The dot layout and the way a link picks its dots come from the first
6
6
  // round design built on qrdx's custom templates (2025), so designs made
7
7
  // with it keep their rings.
8
- import { sanitizeFrameText } from "../../types/frame-spec";
8
+ import { FRAME_FONT_SCALE_MIN } from "../../types/constants";
9
+ import { sanitizeFrameText } from "../../types/frame-spec-rules";
9
10
  import { roundTo } from "../utils/fmt";
10
11
 
11
12
  // ── Geometry (a 593 × 593 drawing) ────────────────────────────────────────
@@ -143,8 +144,15 @@ export const DEFAULT_TEXT =
143
144
  "Scan the QR code * Scan the QR code * Scan the QR code * Scan the QR code *";
144
145
  export const DEFAULT_FONT_SIZE = 32;
145
146
  export const DEFAULT_LETTER_SPACING = 3.5;
147
+ /**
148
+ * Smallest font long text shrinks to before it's cut: half the default, as
149
+ * frame labels go no smaller than half their size (FRAME_FONT_SCALE_MIN).
150
+ * That's the share of the code the circle frame's smallest labels take.
151
+ */
152
+ export const MIN_FONT_SIZE = DEFAULT_FONT_SIZE * FRAME_FONT_SCALE_MIN;
146
153
  /** Letters are spaced along this circle. */
147
154
  const TEXT_PATH_RADIUS = 246.5;
155
+ const CIRCUMFERENCE = 2 * Math.PI * TEXT_PATH_RADIUS;
148
156
  /** Half a cap height, in em: the letters' band is centered on TEXT_BAND_RADIUS. */
149
157
  const HALF_CAP = 0.36;
150
158
  const TEXT_BAND_RADIUS = TEXT_PATH_RADIUS + HALF_CAP * DEFAULT_FONT_SIZE;
@@ -179,26 +187,271 @@ export interface RingLetter {
179
187
  rotate: number;
180
188
  }
181
189
 
182
- /** Letters of `text` along the ring, centered at the top or the bottom. */
183
- export function dotRingLetters(
190
+ /** How the text is set: which letters, at what size and spacing. */
191
+ interface RingSetting {
192
+ chars: string[];
193
+ fontSize: number;
194
+ letterSpacing: number;
195
+ /** The last letter is the "…" that ends cut text */
196
+ cut?: boolean;
197
+ }
198
+
199
+ /** Ends text cut short: a deliberate cut, not text running out. */
200
+ const CUT_MARK = "\u2026";
201
+ /** The cut mark's width in em (an ellipsis is about 1 em in a heavy sans) */
202
+ const CUT_MARK_EM = 1;
203
+
204
+ /** Width of `chars[index]`: its rough width, or the cut mark's. */
205
+ function charWidth(
206
+ chars: readonly string[],
207
+ index: number,
208
+ fontSize: number,
209
+ cut = false
210
+ ): number {
211
+ return cut && index === chars.length - 1
212
+ ? CUT_MARK_EM * fontSize
213
+ : letterWidth(chars[index] as string, fontSize);
214
+ }
215
+
216
+ /** Sum of the letters' widths (no spacing). */
217
+ function lettersWidth(
218
+ chars: readonly string[],
219
+ fontSize: number,
220
+ cut = false
221
+ ): number {
222
+ let width = 0;
223
+ for (let index = 0; index < chars.length; index++) {
224
+ width += charWidth(chars, index, fontSize, cut);
225
+ }
226
+ return width;
227
+ }
228
+
229
+ /**
230
+ * True when `chars` go round with room to spare: the line plus a word
231
+ * space where the ring closes, so its end never meets its start.
232
+ */
233
+ function fitsRing(
234
+ chars: readonly string[],
235
+ fontSize: number,
236
+ spacing: number,
237
+ cut = false
238
+ ) {
239
+ const line =
240
+ lettersWidth(chars, fontSize, cut) +
241
+ spacing * Math.max(0, chars.length - 1);
242
+ return line + letterWidth(" ", fontSize) + 2 * spacing <= CIRCUMFERENCE;
243
+ }
244
+
245
+ /** `text`'s words, without the spaces between them. */
246
+ const wordsOf = (chars: readonly string[]) =>
247
+ chars.join("").split(" ").filter(Boolean);
248
+
249
+ /** `phrase` `count` times, a space between the copies. */
250
+ const copiesOf = (phrase: string[], count: number) =>
251
+ Array.from(Array.from({ length: count }, () => phrase.join(" ")).join(" "));
252
+
253
+ /**
254
+ * `chars` as asked when they fit round the ring. Longer text gets tighter
255
+ * spacing first (down to none), then a smaller font (down to
256
+ * MIN_FONT_SIZE), set so the line and a word space close the ring exactly.
257
+ * Past that, text that repeats a phrase keeps as many whole copies as fit,
258
+ * closed like textFill "repeat"; other text is cut after the last whole
259
+ * word that fits and ends with "…".
260
+ */
261
+ function fitRing(
262
+ chars: string[],
263
+ fontSize: number,
264
+ letterSpacing: number
265
+ ): RingSetting {
266
+ if (fitsRing(chars, fontSize, letterSpacing)) {
267
+ return { chars, fontSize, letterSpacing };
268
+ }
269
+ const tightest = Math.min(letterSpacing, 0);
270
+ const smallest = Math.min(fontSize, MIN_FONT_SIZE);
271
+ // The line and a word space closing the ring exactly: spacing first,
272
+ // then the font (null when that would go below the smallest size)
273
+ const close = (line: string[], cut = false): RingSetting | null => {
274
+ // The line plus the closing space, without spacing: it scales with the font
275
+ const width =
276
+ lettersWidth(line, fontSize, cut) + letterWidth(" ", fontSize);
277
+ // n letters and the space take n + 1 gaps round a closed ring
278
+ const gaps = line.length + 1;
279
+ const spacing = (CIRCUMFERENCE - width) / gaps;
280
+ if (spacing >= tightest) {
281
+ return { chars: line, fontSize, letterSpacing: spacing, cut };
282
+ }
283
+ const size = ((CIRCUMFERENCE - tightest * gaps) / width) * fontSize;
284
+ return size >= smallest
285
+ ? { chars: line, fontSize: size, letterSpacing: tightest, cut }
286
+ : null;
287
+ };
288
+ const squeezed = close(chars);
289
+ if (squeezed) {
290
+ return squeezed;
291
+ }
292
+ // Too long even at the smallest size. Copies of one phrase: the most
293
+ // whole copies that fit
294
+ const words = wordsOf(chars);
295
+ const phrase = repeatedWords(words);
296
+ for (let count = words.length / phrase.length - 1; count >= 1; count--) {
297
+ const line = copiesOf(phrase, count);
298
+ if (fitsRing(line, smallest, tightest)) {
299
+ return (
300
+ close(line) ?? {
301
+ chars: line,
302
+ fontSize: smallest,
303
+ letterSpacing: tightest,
304
+ }
305
+ );
306
+ }
307
+ }
308
+ // Anything else: cut after the last word that fits (or within a word
309
+ // longer than the ring) and end with "…". Each letter takes its width
310
+ // and one gap; the mark and the closing space take the rest (fitsRing,
311
+ // counted letter by letter)
312
+ const room =
313
+ CIRCUMFERENCE -
314
+ letterWidth(" ", smallest) -
315
+ CUT_MARK_EM * smallest -
316
+ 2 * tightest;
317
+ let used = 0;
318
+ let whole = 0;
319
+ let letters = 0;
320
+ for (const [index, char] of chars.entries()) {
321
+ used += letterWidth(char, smallest) + tightest;
322
+ if (used > room) {
323
+ break;
324
+ }
325
+ letters = index + 1;
326
+ if (letters === chars.length || chars[letters] === " ") {
327
+ whole = letters;
328
+ }
329
+ }
330
+ const line = [
331
+ ...trimCut(chars.slice(0, whole > 0 ? whole : letters)),
332
+ CUT_MARK,
333
+ ];
334
+ const asked = fitsRing(line, fontSize, letterSpacing, true)
335
+ ? { chars: line, fontSize, letterSpacing, cut: true }
336
+ : null;
337
+ return (
338
+ asked ??
339
+ close(line, true) ?? {
340
+ chars: line,
341
+ fontSize: smallest,
342
+ letterSpacing: tightest,
343
+ cut: true,
344
+ }
345
+ );
346
+ }
347
+
348
+ /** Without trailing spaces and punctuation that would sit before "…". */
349
+ function trimCut(chars: string[]): string[] {
350
+ let end = chars.length;
351
+ while (end > 0 && /[\s.,;:!?*·•\-–—]/u.test(chars[end - 1] as string)) {
352
+ end--;
353
+ }
354
+ return chars.slice(0, end);
355
+ }
356
+
357
+ /** The shortest run of words `words` repeats ("A * A * A *" gives "A *"). */
358
+ function repeatedWords(words: string[]): string[] {
359
+ for (let size = 1; size < words.length; size++) {
360
+ if (
361
+ words.length % size === 0 &&
362
+ words.every((word, index) => word === words[index % size])
363
+ ) {
364
+ return words.slice(0, size);
365
+ }
366
+ }
367
+ return words;
368
+ }
369
+
370
+ /**
371
+ * The phrase `chars` repeats, as many times as fill the ring, spaced so the
372
+ * copies close it exactly: the ring's seam is the same word space as
373
+ * between the copies. Of the counts either side of a whole fit, the one
374
+ * whose spacing is nearest the asked spacing wins; spacing never goes below
375
+ * none (else the phrase is fitted once, as fitRing does).
376
+ */
377
+ function repeatRing(
378
+ chars: string[],
379
+ fontSize: number,
380
+ letterSpacing: number
381
+ ): RingSetting {
382
+ const words = wordsOf(chars);
383
+ if (words.length === 0) {
384
+ return { chars: [], fontSize, letterSpacing };
385
+ }
386
+ const phrase = Array.from(repeatedWords(words).join(" "));
387
+ // One copy and the space after it, without spacing
388
+ const width = lettersWidth(phrase, fontSize) + letterWidth(" ", fontSize);
389
+ const advance = width + (phrase.length + 1) * letterSpacing;
390
+ const tightest = Math.min(letterSpacing, 0);
391
+ let best: RingSetting | null = null;
392
+ for (const copies of new Set([
393
+ Math.floor(CIRCUMFERENCE / advance),
394
+ Math.ceil(CIRCUMFERENCE / advance),
395
+ ])) {
396
+ if (copies < 1) {
397
+ continue;
398
+ }
399
+ const spacing =
400
+ (CIRCUMFERENCE - copies * width) / (copies * (phrase.length + 1));
401
+ if (
402
+ spacing >= tightest &&
403
+ (!best ||
404
+ Math.abs(spacing - letterSpacing) <
405
+ Math.abs(best.letterSpacing - letterSpacing))
406
+ ) {
407
+ const line = Array.from({ length: copies }, () => phrase.join(""));
408
+ best = {
409
+ chars: Array.from(line.join(" ")),
410
+ fontSize,
411
+ letterSpacing: spacing,
412
+ };
413
+ }
414
+ }
415
+ return best ?? fitRing(phrase, fontSize, letterSpacing);
416
+ }
417
+
418
+ /** The text along the ring: its letters, and the size they're drawn at. */
419
+ export interface RingText {
420
+ /** The line as set: the text, cut or repeated to fit */
421
+ text: string;
422
+ letters: RingLetter[];
423
+ /** The asked size, or smaller when long text had to shrink */
424
+ fontSize: number;
425
+ letterSpacing: number;
426
+ }
427
+
428
+ /**
429
+ * Letters of `text` along the ring, centered at the top or the bottom.
430
+ * Text that fits is set as asked. Longer text tightens its spacing, then
431
+ * shrinks, then is cut at a word (fitRing), and never meets its own start.
432
+ * `fill: "repeat"` repeats the phrase round the whole ring (repeatRing).
433
+ */
434
+ export function dotRingText(
184
435
  text: string,
185
436
  options: {
186
437
  fontSize: number;
187
438
  letterSpacing: number;
188
439
  position: "top" | "bottom";
189
440
  rotation: number;
441
+ fill?: "none" | "repeat";
190
442
  }
191
- ): RingLetter[] {
192
- const { fontSize, letterSpacing, position, rotation } = options;
193
- const circumference = 2 * Math.PI * TEXT_PATH_RADIUS;
194
- // Keep the letters that fit around the ring
443
+ ): RingText {
444
+ const { position, rotation } = options;
445
+ const chars = cleanText(text);
446
+ const setting =
447
+ options.fill === "repeat"
448
+ ? repeatRing(chars, options.fontSize, options.letterSpacing)
449
+ : fitRing(chars, options.fontSize, options.letterSpacing);
450
+ const { fontSize, letterSpacing } = setting;
195
451
  const kept: { char: string; along: number }[] = [];
196
452
  let offset = 0;
197
- for (const char of cleanText(text)) {
198
- const width = letterWidth(char, fontSize);
199
- if (offset + width > circumference) {
200
- break;
201
- }
453
+ for (const [index, char] of setting.chars.entries()) {
454
+ const width = charWidth(setting.chars, index, fontSize, setting.cut);
202
455
  kept.push({ char, along: offset + width / 2 });
203
456
  offset += width + letterSpacing;
204
457
  }
@@ -209,13 +462,13 @@ export function dotRingLetters(
209
462
  const radius = top
210
463
  ? TEXT_BAND_RADIUS - HALF_CAP * fontSize
211
464
  : TEXT_BAND_RADIUS + HALF_CAP * fontSize;
212
- const half = (total / circumference) * 180;
465
+ const half = (total / CIRCUMFERENCE) * 180;
213
466
  const letters: RingLetter[] = [];
214
467
  for (const { char, along } of kept) {
215
468
  if (char.trim() === "") {
216
469
  continue;
217
470
  }
218
- const step = (along / circumference) * 360;
471
+ const step = (along / CIRCUMFERENCE) * 360;
219
472
  // Degrees clockwise from twelve o'clock
220
473
  const angle = top ? rotation - half + step : rotation + 180 + half - step;
221
474
  const radians = (angle * Math.PI) / 180;
@@ -226,5 +479,18 @@ export function dotRingLetters(
226
479
  rotate: roundTo(top ? angle : angle - 180),
227
480
  });
228
481
  }
229
- return letters;
482
+ return {
483
+ text: setting.chars.join(""),
484
+ letters,
485
+ fontSize: fontSize === options.fontSize ? fontSize : roundTo(fontSize),
486
+ letterSpacing,
487
+ };
488
+ }
489
+
490
+ /** Letters of `text` along the ring (see dotRingText). */
491
+ export function dotRingLetters(
492
+ text: string,
493
+ options: Parameters<typeof dotRingText>[1]
494
+ ): RingLetter[] {
495
+ return dotRingText(text, options).letters;
230
496
  }