@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 @@
1
+ {"version":3,"file":"wrap-text.d.ts","sourceRoot":"","sources":["../src/wrap-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AA4G5C;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAC/B,MAAM,EAAE,CAgBV;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM;;;IAK3E"}
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Where a run of text may be broken, and how it is filled into lines.
3
+ *
4
+ * One implementation, shared by both renderers and by the hit test, for a
5
+ * reason worth stating: `Intl.Segmenter` does not exist on React Native.
6
+ * Hermes implements `Collator`, `DateTimeFormat` and `NumberFormat` and
7
+ * nothing else from ECMA-402's later additions -- `intl402/Segmenter/` is
8
+ * on its test262 skip list, and there is no Segmenter in its platform
9
+ * Intl on either Android or Apple. So "use `Intl.Segmenter` where it
10
+ * exists" would mean a dictionary on the Web and a fallback on every
11
+ * phone, permanently, and the same string breaking in different places
12
+ * depending on where it was opened.
13
+ *
14
+ * The rules here are a practical subset of UAX #14: spaces, the
15
+ * boundaries around CJK, and the two kinsoku classes that keep Japanese
16
+ * punctuation off the start and end of a line. Deterministic, and the
17
+ * same on both platforms. Line *positions* still differ, because the
18
+ * fonts differ -- but that is a measurement, and each renderer's
19
+ * measurement is its own.
20
+ */
21
+ /**
22
+ * Characters that may be broken between, rather than only after a space.
23
+ *
24
+ * Kana, ideographs, Hangul, and the fullwidth forms. UAX #14 calls this
25
+ * ID, and it is what makes Japanese wrap at all: a sentence of it holds
26
+ * no spaces, so a space-only breaker leaves it on one line forever.
27
+ */
28
+ function isIdeographic(codePoint) {
29
+ return ((codePoint >= 0x1100 && codePoint <= 0x11ff) || // Hangul Jamo
30
+ (codePoint >= 0x2e80 && codePoint <= 0x303f) || // CJK radicals, punctuation
31
+ (codePoint >= 0x3040 && codePoint <= 0x30ff) || // Hiragana, Katakana
32
+ (codePoint >= 0x3130 && codePoint <= 0x318f) || // Hangul compatibility
33
+ (codePoint >= 0x3400 && codePoint <= 0x4dbf) || // Extension A
34
+ (codePoint >= 0x4e00 && codePoint <= 0x9fff) || // Unified ideographs
35
+ (codePoint >= 0xac00 && codePoint <= 0xd7a3) || // Hangul syllables
36
+ (codePoint >= 0xf900 && codePoint <= 0xfaff) || // Compatibility ideographs
37
+ (codePoint >= 0xff00 && codePoint <= 0xff60) || // Fullwidth forms
38
+ (codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
39
+ (codePoint >= 0x20000 && codePoint <= 0x3ffff) // Extensions B and beyond
40
+ );
41
+ }
42
+ /**
43
+ * 行頭禁則: what may not begin a line.
44
+ *
45
+ * Closing brackets, the small kana, the sound marks, and the punctuation
46
+ * that belongs to the word before it. A line starting with 。 is the
47
+ * thing everyone recognises as wrong without being able to name it.
48
+ */
49
+ const NO_START = new Set([
50
+ ...')]})〕]}〉》」』】〙〗」、。,.・:;?!‼⁇ー〜~',
51
+ ...'ぁぃぅぇぉっゃゅょゎゕゖァィゥェォッャュョヮヵヶ',
52
+ ...'々〻゛゜‐–—…‥%‰℃°',
53
+ ]);
54
+ /** 行末禁則: what may not end a line. Opening brackets, and the leading marks. */
55
+ const NO_END = new Set([...'([{(〔[{〈《「『【〘〖「$¥#@§']);
56
+ /**
57
+ * Whether the text may be broken between two adjacent code points.
58
+ *
59
+ * Spaces are handled by the caller, which absorbs them into the line they
60
+ * end rather than carrying them to the next.
61
+ */
62
+ function breakableBetween(before, after) {
63
+ if (NO_END.has(before) || NO_START.has(after))
64
+ return false;
65
+ const beforePoint = before.codePointAt(0) ?? 0;
66
+ const afterPoint = after.codePointAt(0) ?? 0;
67
+ return isIdeographic(beforePoint) || isIdeographic(afterPoint);
68
+ }
69
+ /**
70
+ * The smallest pieces the text may be split into, spaces included.
71
+ *
72
+ * A chunk is what has to travel together: a Latin word with the spaces
73
+ * that follow it, or a single ideograph. Nothing here hyphenates, so a
74
+ * word longer than the width goes on a line of its own and overflows --
75
+ * a break inside it would be a guess at a language's rules.
76
+ */
77
+ function chunk(text) {
78
+ const points = [...text];
79
+ const chunks = [];
80
+ let current = '';
81
+ for (let index = 0; index < points.length; index += 1) {
82
+ const point = points[index];
83
+ const next = points[index + 1];
84
+ current += point;
85
+ if (next === undefined)
86
+ break;
87
+ // A run of spaces ends a chunk, and stays with the line it ends.
88
+ if (point === ' ' && next !== ' ') {
89
+ chunks.push(current);
90
+ current = '';
91
+ continue;
92
+ }
93
+ if (next === ' ')
94
+ continue;
95
+ if (breakableBetween(point, next)) {
96
+ chunks.push(current);
97
+ current = '';
98
+ }
99
+ }
100
+ if (current !== '')
101
+ chunks.push(current);
102
+ return chunks;
103
+ }
104
+ /**
105
+ * Greedily fills lines no wider than `maxWidth`, measured by `widthOf`.
106
+ *
107
+ * Greedy rather than balanced (Knuth-Plass and the like): a chart label
108
+ * is two or three lines, where the two agree, and greedy is the rule
109
+ * every browser and both renderers already follow for everything else on
110
+ * the screen.
111
+ *
112
+ * `\n` always breaks, and a line's trailing spaces are dropped -- they
113
+ * would otherwise push a line over the width with nothing visible in the
114
+ * space they take.
115
+ */
116
+ export function wrapText(text, maxWidth, widthOf) {
117
+ const lines = [];
118
+ for (const paragraph of text.split('\n')) {
119
+ let line = '';
120
+ for (const piece of chunk(paragraph)) {
121
+ const candidate = line + piece;
122
+ if (line !== '' && widthOf(candidate.trimEnd()) > maxWidth) {
123
+ lines.push(line.trimEnd());
124
+ line = piece.trimStart();
125
+ }
126
+ else {
127
+ line = candidate;
128
+ }
129
+ }
130
+ lines.push(line.trimEnd());
131
+ }
132
+ return lines;
133
+ }
134
+ /**
135
+ * Every line of a run, with the baseline each one sits on.
136
+ *
137
+ * One line when there is no `maxWidth`, so everything downstream is
138
+ * written against lines rather than against a special case. Lives here
139
+ * rather than in the hit test because both renderers need it too, and
140
+ * a renderer importing from the hit test would have the arrow pointing
141
+ * the wrong way.
142
+ */
143
+ export function textLines(props, widthOf) {
144
+ const height = (props.lineHeight ?? 1.2) * props.fontSize;
145
+ const texts = props.maxWidth === undefined ? [props.text] : wrapText(props.text, props.maxWidth, widthOf);
146
+ return texts.map((text, index) => ({ text, y: props.y + index * height }));
147
+ }
148
+ //# sourceMappingURL=wrap-text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrap-text.js","sourceRoot":"","sources":["../src/wrap-text.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,SAAiB;IACtC,OAAO,CACL,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,cAAc;QAC9D,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,4BAA4B;QAC5E,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,qBAAqB;QACrE,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,uBAAuB;QACvE,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,cAAc;QAC9D,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,qBAAqB;QACrE,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,mBAAmB;QACnE,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,2BAA2B;QAC3E,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,kBAAkB;QAClE,CAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC;QA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ACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAC,KAAgB,EAAE,OAAgC;IAC1E,MAAM,MAAM,GAAG,CAAC,KAAK,CAAC,UAAU,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAA;IACzD,MAAM,KAAK,GACT,KAAK,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;IAC7F,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,GAAG,KAAK,GAAG,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5E,CAAC"}
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@hozo/canvas",
3
+ "version": "0.1.0",
4
+ "description": "A declarative Canvas scene for Hozo, rendered by Canvas 2D on Web and Skia on React Native.",
5
+ "keywords": [
6
+ "react-native",
7
+ "canvas",
8
+ "graphics",
9
+ "skia",
10
+ "charts"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/iray-tno/hozo.git",
16
+ "directory": "packages/canvas"
17
+ },
18
+ "homepage": "https://iray-tno.github.io/hozo/",
19
+ "bugs": {
20
+ "url": "https://github.com/iray-tno/hozo/issues"
21
+ },
22
+ "type": "module",
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "sideEffects": false,
26
+ "exports": {
27
+ ".": {
28
+ "react-native": {
29
+ "types": "./dist/index.native.d.ts",
30
+ "default": "./dist/index.native.js"
31
+ },
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "src",
40
+ "!src/**/*.test.ts",
41
+ "!src/**/*.test.tsx",
42
+ "!src/**/*.bench.ts"
43
+ ],
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "provenance": true
47
+ },
48
+ "dependencies": {
49
+ "@hozo/engine": "^0.1.0"
50
+ },
51
+ "peerDependencies": {
52
+ "react": "^19.0.0",
53
+ "react-native": ">=0.86",
54
+ "@shopify/react-native-skia": ">=1.0.0"
55
+ },
56
+ "peerDependenciesMeta": {
57
+ "react-native": {
58
+ "optional": true
59
+ },
60
+ "@shopify/react-native-skia": {
61
+ "optional": true
62
+ }
63
+ },
64
+ "devDependencies": {
65
+ "@shopify/react-native-skia": "^2.11.1",
66
+ "@types/react": "^19.2.18",
67
+ "@types/react-dom": "^19.2.4",
68
+ "react": "^19.2.8",
69
+ "react-dom": "^19.2.8",
70
+ "react-native": "^0.87.1",
71
+ "react-test-renderer": "^19.2.8"
72
+ },
73
+ "scripts": {
74
+ "build": "node ../../scripts/clean-package-dist.mjs && tsc -p .",
75
+ "typecheck": "tsc -p . --noEmit",
76
+ "test": "node -e \"require('node:fs').rmSync('.test-build', { recursive: true, force: true })\" && tsc -p tsconfig.test.json && node --test .test-build/*.test.js",
77
+ "test:performance": "node -e \"require('node:fs').rmSync('.test-build', { recursive: true, force: true })\" && tsc -p tsconfig.test.json && node --test .test-build/*.bench.js"
78
+ }
79
+ }
@@ -0,0 +1,28 @@
1
+ import type { ReactNode } from 'react'
2
+
3
+ /** A fallback must render something; nullish and boolean React nodes do not. */
4
+ export type CanvasAccessibleFallback = Exclude<ReactNode, boolean | null | undefined>
5
+
6
+ export type CanvasAccessibilityProps =
7
+ | { decorative: true; accessibilityLabel?: never; accessibleFallback?: never }
8
+ | {
9
+ decorative?: false
10
+ accessibilityLabel: string
11
+ accessibleFallback?: CanvasAccessibleFallback
12
+ }
13
+ | {
14
+ decorative?: false
15
+ accessibilityLabel?: string
16
+ accessibleFallback: CanvasAccessibleFallback
17
+ }
18
+
19
+ export type CanvasAccessibilityMode = 'decorative' | 'label' | 'fallback'
20
+
21
+ /** Shared policy; each platform decides how that mode maps to its accessibility tree. */
22
+ export function canvasAccessibilityMode(
23
+ props: Pick<CanvasAccessibilityProps, 'decorative' | 'accessibilityLabel' | 'accessibleFallback'>,
24
+ ): CanvasAccessibilityMode {
25
+ if (props.decorative) return 'decorative'
26
+ if (props.accessibleFallback !== undefined) return 'fallback'
27
+ return 'label'
28
+ }