@hozo/canvas 0.1.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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -0
  3. package/dist/accessibility.d.ts +20 -0
  4. package/dist/accessibility.d.ts.map +1 -0
  5. package/dist/accessibility.js +9 -0
  6. package/dist/accessibility.js.map +1 -0
  7. package/dist/hit-test.d.ts +96 -0
  8. package/dist/hit-test.d.ts.map +1 -0
  9. package/dist/hit-test.js +354 -0
  10. package/dist/hit-test.js.map +1 -0
  11. package/dist/index.d.ts +80 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +317 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/index.native.d.ts +83 -0
  16. package/dist/index.native.d.ts.map +1 -0
  17. package/dist/index.native.js +435 -0
  18. package/dist/index.native.js.map +1 -0
  19. package/dist/render-canvas-2d.d.ts +6 -0
  20. package/dist/render-canvas-2d.d.ts.map +1 -0
  21. package/dist/render-canvas-2d.js +213 -0
  22. package/dist/render-canvas-2d.js.map +1 -0
  23. package/dist/scene.d.ts +495 -0
  24. package/dist/scene.d.ts.map +1 -0
  25. package/dist/scene.js +591 -0
  26. package/dist/scene.js.map +1 -0
  27. package/dist/viewport.d.ts +9 -0
  28. package/dist/viewport.d.ts.map +1 -0
  29. package/dist/viewport.js +2 -0
  30. package/dist/viewport.js.map +1 -0
  31. package/dist/wrap-text.d.ts +28 -0
  32. package/dist/wrap-text.d.ts.map +1 -0
  33. package/dist/wrap-text.js +148 -0
  34. package/dist/wrap-text.js.map +1 -0
  35. package/package.json +79 -0
  36. package/src/accessibility.ts +28 -0
  37. package/src/hit-test.ts +499 -0
  38. package/src/index.native.tsx +733 -0
  39. package/src/index.tsx +510 -0
  40. package/src/render-canvas-2d.ts +259 -0
  41. package/src/scene.tsx +1037 -0
  42. package/src/viewport.ts +8 -0
  43. package/src/wrap-text.ts +157 -0
@@ -0,0 +1,8 @@
1
+ /** Platform-free geometry used to map a scene onto its drawing surface. */
2
+ export interface CanvasViewport {
3
+ width: number
4
+ height: number
5
+ pixelRatio: number
6
+ viewBox?: readonly [x: number, y: number, width: number, height: number]
7
+ fit?: 'contain' | 'stretch'
8
+ }
@@ -0,0 +1,157 @@
1
+ import type { TextProps } from './scene.tsx'
2
+
3
+ /**
4
+ * Where a run of text may be broken, and how it is filled into lines.
5
+ *
6
+ * One implementation, shared by both renderers and by the hit test, for a
7
+ * reason worth stating: `Intl.Segmenter` does not exist on React Native.
8
+ * Hermes implements `Collator`, `DateTimeFormat` and `NumberFormat` and
9
+ * nothing else from ECMA-402's later additions -- `intl402/Segmenter/` is
10
+ * on its test262 skip list, and there is no Segmenter in its platform
11
+ * Intl on either Android or Apple. So "use `Intl.Segmenter` where it
12
+ * exists" would mean a dictionary on the Web and a fallback on every
13
+ * phone, permanently, and the same string breaking in different places
14
+ * depending on where it was opened.
15
+ *
16
+ * The rules here are a practical subset of UAX #14: spaces, the
17
+ * boundaries around CJK, and the two kinsoku classes that keep Japanese
18
+ * punctuation off the start and end of a line. Deterministic, and the
19
+ * same on both platforms. Line *positions* still differ, because the
20
+ * fonts differ -- but that is a measurement, and each renderer's
21
+ * measurement is its own.
22
+ */
23
+
24
+ /**
25
+ * Characters that may be broken between, rather than only after a space.
26
+ *
27
+ * Kana, ideographs, Hangul, and the fullwidth forms. UAX #14 calls this
28
+ * ID, and it is what makes Japanese wrap at all: a sentence of it holds
29
+ * no spaces, so a space-only breaker leaves it on one line forever.
30
+ */
31
+ function isIdeographic(codePoint: number) {
32
+ return (
33
+ (codePoint >= 0x1100 && codePoint <= 0x11ff) || // Hangul Jamo
34
+ (codePoint >= 0x2e80 && codePoint <= 0x303f) || // CJK radicals, punctuation
35
+ (codePoint >= 0x3040 && codePoint <= 0x30ff) || // Hiragana, Katakana
36
+ (codePoint >= 0x3130 && codePoint <= 0x318f) || // Hangul compatibility
37
+ (codePoint >= 0x3400 && codePoint <= 0x4dbf) || // Extension A
38
+ (codePoint >= 0x4e00 && codePoint <= 0x9fff) || // Unified ideographs
39
+ (codePoint >= 0xac00 && codePoint <= 0xd7a3) || // Hangul syllables
40
+ (codePoint >= 0xf900 && codePoint <= 0xfaff) || // Compatibility ideographs
41
+ (codePoint >= 0xff00 && codePoint <= 0xff60) || // Fullwidth forms
42
+ (codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
43
+ (codePoint >= 0x20000 && codePoint <= 0x3ffff) // Extensions B and beyond
44
+ )
45
+ }
46
+
47
+ /**
48
+ * 行頭禁則: what may not begin a line.
49
+ *
50
+ * Closing brackets, the small kana, the sound marks, and the punctuation
51
+ * that belongs to the word before it. A line starting with 。 is the
52
+ * thing everyone recognises as wrong without being able to name it.
53
+ */
54
+ const NO_START = new Set([
55
+ ...')]})〕]}〉》」』】〙〗」、。,.・:;?!‼⁇ー〜~',
56
+ ...'ぁぃぅぇぉっゃゅょゎゕゖァィゥェォッャュョヮヵヶ',
57
+ ...'々〻゛゜‐–—…‥%‰℃°',
58
+ ])
59
+
60
+ /** 行末禁則: what may not end a line. Opening brackets, and the leading marks. */
61
+ const NO_END = new Set([...'([{(〔[{〈《「『【〘〖「$¥#@§'])
62
+
63
+ /**
64
+ * Whether the text may be broken between two adjacent code points.
65
+ *
66
+ * Spaces are handled by the caller, which absorbs them into the line they
67
+ * end rather than carrying them to the next.
68
+ */
69
+ function breakableBetween(before: string, after: string) {
70
+ if (NO_END.has(before) || NO_START.has(after)) return false
71
+ const beforePoint = before.codePointAt(0) ?? 0
72
+ const afterPoint = after.codePointAt(0) ?? 0
73
+ return isIdeographic(beforePoint) || isIdeographic(afterPoint)
74
+ }
75
+
76
+ /**
77
+ * The smallest pieces the text may be split into, spaces included.
78
+ *
79
+ * A chunk is what has to travel together: a Latin word with the spaces
80
+ * that follow it, or a single ideograph. Nothing here hyphenates, so a
81
+ * word longer than the width goes on a line of its own and overflows --
82
+ * a break inside it would be a guess at a language's rules.
83
+ */
84
+ function chunk(text: string): string[] {
85
+ const points = [...text]
86
+ const chunks: string[] = []
87
+ let current = ''
88
+ for (let index = 0; index < points.length; index += 1) {
89
+ const point = points[index] as string
90
+ const next = points[index + 1]
91
+ current += point
92
+ if (next === undefined) break
93
+ // A run of spaces ends a chunk, and stays with the line it ends.
94
+ if (point === ' ' && next !== ' ') {
95
+ chunks.push(current)
96
+ current = ''
97
+ continue
98
+ }
99
+ if (next === ' ') continue
100
+ if (breakableBetween(point, next)) {
101
+ chunks.push(current)
102
+ current = ''
103
+ }
104
+ }
105
+ if (current !== '') chunks.push(current)
106
+ return chunks
107
+ }
108
+
109
+ /**
110
+ * Greedily fills lines no wider than `maxWidth`, measured by `widthOf`.
111
+ *
112
+ * Greedy rather than balanced (Knuth-Plass and the like): a chart label
113
+ * is two or three lines, where the two agree, and greedy is the rule
114
+ * every browser and both renderers already follow for everything else on
115
+ * the screen.
116
+ *
117
+ * `\n` always breaks, and a line's trailing spaces are dropped -- they
118
+ * would otherwise push a line over the width with nothing visible in the
119
+ * space they take.
120
+ */
121
+ export function wrapText(
122
+ text: string,
123
+ maxWidth: number,
124
+ widthOf: (run: string) => number,
125
+ ): string[] {
126
+ const lines: string[] = []
127
+ for (const paragraph of text.split('\n')) {
128
+ let line = ''
129
+ for (const piece of chunk(paragraph)) {
130
+ const candidate = line + piece
131
+ if (line !== '' && widthOf(candidate.trimEnd()) > maxWidth) {
132
+ lines.push(line.trimEnd())
133
+ line = piece.trimStart()
134
+ } else {
135
+ line = candidate
136
+ }
137
+ }
138
+ lines.push(line.trimEnd())
139
+ }
140
+ return lines
141
+ }
142
+
143
+ /**
144
+ * Every line of a run, with the baseline each one sits on.
145
+ *
146
+ * One line when there is no `maxWidth`, so everything downstream is
147
+ * written against lines rather than against a special case. Lives here
148
+ * rather than in the hit test because both renderers need it too, and
149
+ * a renderer importing from the hit test would have the arrow pointing
150
+ * the wrong way.
151
+ */
152
+ export function textLines(props: TextProps, widthOf: (run: string) => number) {
153
+ const height = (props.lineHeight ?? 1.2) * props.fontSize
154
+ const texts =
155
+ props.maxWidth === undefined ? [props.text] : wrapText(props.text, props.maxWidth, widthOf)
156
+ return texts.map((text, index) => ({ text, y: props.y + index * height }))
157
+ }