@kubohiroya/turbowarp-svg-text 0.3.0 → 0.4.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
@@ -1,138 +1,84 @@
1
1
  # TurboWarp SVG Text
2
2
 
3
- TurboWarp SVG Text provides responsive text with reusable named styles. The same style can be shared by say/think bubbles and SVG skins that turn a sprite itself into a text actor. Text remains proportional to the stage when its dimensions change.
3
+ TurboWarp SVG Text provides responsive named styles and SVG text actors. It is a text provider for host extensions such as `turbowarp-bubble`; Bubble shape, placement, portraits, and animation belong to the host.
4
4
 
5
- See the [English guide](https://kubohiroya.github.io/turbowarp-svg-text/) or [Japanese guide](https://kubohiroya.github.io/turbowarp-svg-text/ja/) for usage and examples. The extension palette also opens the English guide through TurboWarp's `docsURI` feature.
5
+ ## Responsibilities
6
6
 
7
- ## Features
7
+ - Define reusable text styles for font, size, color, background, and alignment.
8
+ - Create, replace, measure, and release SVG text skins.
9
+ - Keep text actors proportional to the stage.
10
+ - Restyle visible text actors when a named style changes.
8
11
 
9
- - Define background color, text color, font, relative font size, and left/center/right alignment under any style name.
10
- - Choose one of sixteen bubble directions, including the eight intermediate 22.5-degree positions.
11
- - Display say and think bubbles with a named style.
12
- - Replace the current sprite's skin with multiline SVG text using `set this sprite text`.
13
- - Apply the redefinable `default` style to standard say, think, and ask bubbles.
14
- - Convert literal `\n`, `\r\n`, and `\r` sequences in displayed strings into line breaks.
15
- - Immediately update visible bubbles and SVG text when a style is redefined or the stage size changes.
16
- - Keep legacy size-based say/think opcodes hidden but executable for saved-project compatibility.
17
-
18
- Animation is not included in this release.
12
+ This package does not provide `say` or `think` bubbles, bubble tails, actor-relative placement, portrait images, or bubble animation. Those responsibilities belong to the host package and its capabilities.
19
13
 
20
14
  ## Blocks
21
15
 
22
- ### `define text style [STYLE] background [BACKGROUND] text [TEXT_COLOR] font [FONT] size [SIZE] align [ALIGN] bubble direction [DIRECTION]`
23
-
24
- Defines or replaces a named style. `ALIGN` accepts `left`, `center`, or `right`. `DIRECTION` accepts the following sixteen canonical values and their compass aliases; direction applies only to bubbles.
25
-
26
- | Canonical value | Compass alias |
27
- | ------------------ | ----------------- |
28
- | `up` | `north` |
29
- | `up-up-right` | `north-northeast` |
30
- | `up-right` | `northeast` |
31
- | `right-up-right` | `east-northeast` |
32
- | `right` | `east` |
33
- | `right-down-right` | `east-southeast` |
34
- | `down-right` | `southeast` |
35
- | `down-down-right` | `south-southeast` |
36
- | `down` | `south` |
37
- | `down-down-left` | `south-southwest` |
38
- | `down-left` | `southwest` |
39
- | `left-down-left` | `west-southwest` |
40
- | `left` | `west` |
41
- | `left-up-left` | `west-northwest` |
42
- | `up-left` | `northwest` |
43
- | `up-up-left` | `north-northwest` |
44
-
45
- Direction input is trimmed and case-insensitive. Direction is measured from the actor center to the bubble body center. Changing the bubble width or height does not change the requested angle, although stage-edge clamping can move the bubble when necessary.
16
+ ### `define text style [STYLE] background [BACKGROUND] text [TEXT_COLOR] font [FONT] size [SIZE] align [ALIGN]`
46
17
 
47
- `DIRECTION` also accepts any number from `0` through `360` and uses Scratch sprite direction semantics: `0` is up, `90` is right, `180` is down, `270` is left, and `360` is up again. Values between the named directions are placed continuously without rounding to the nearest of the sixteen menu positions. Values outside this range fall back to the initial `up-right` direction.
48
-
49
- Redefining a name immediately redraws visible bubbles and SVG text actors that use it. Styles are runtime state, so projects should normally define them immediately after the green flag. A blank or unknown style name falls back to `default`.
18
+ Defines or replaces a named text style. Alignment accepts `left`, `center`, or `right`. The background is the rectangle behind an SVG text actor; a host Bubble can define a transparent background and render its own outer shape.
50
19
 
51
20
  ### `set this sprite text [TEXT] with style [STYLE]`
52
21
 
53
- Creates an SVG skin and applies it to the current sprite's drawable, making the sprite itself a text actor. Background color, text color, font, size, and alignment come from the selected style. A string containing `\n` is rendered as multiple safely escaped SVG `<tspan>` elements.
54
-
55
- ### `say [MESSAGE] with style [STYLE]`
56
-
57
- Displays a say bubble using the selected named style.
58
-
59
- ### `think [MESSAGE] with style [STYLE]`
22
+ Creates an SVG skin and applies it to the current sprite's drawable. Newline characters render as separate lines. Reusing the same style name after redefinition redraws existing text actors.
60
23
 
61
- Displays a think bubble using the selected named style.
24
+ ## Composition API
62
25
 
63
- ## Responsive sizing
26
+ The composition API does not register the standalone extension. The host supplies a TurboWarp runtime and explicit targets.
64
27
 
65
- Size 100 is TurboWarp's default 14-pixel bubble font on a 480×360 stage. Bubbles and SVG text actors use the same formula:
66
-
67
- ```text
68
- stageScale = min(stageWidth / 480, stageHeight / 360)
69
- fontSize = 14 × stageScale × styleSize / 100
70
- ```
71
-
72
- | Stage | Style size | Font size |
73
- | ------- | ---------: | --------: |
74
- | 480×360 | 100 | 14px |
75
- | 960×720 | 100 | 28px |
76
- | 960×720 | 150 | 42px |
28
+ ```ts
29
+ import { createSvgTextComposition } from "@kubohiroya/turbowarp-svg-text/composition";
77
30
 
78
- Style sizes are clamped to 1–1000. A blank or non-finite value uses 100.
31
+ const svgText = createSvgTextComposition({ runtime: Scratch.vm.runtime });
79
32
 
80
- ## Usage
33
+ svgText.defineStyle({
34
+ name: "dialogue-text",
35
+ alignment: "left",
36
+ backgroundColor: "transparent",
37
+ font: "Noto Sans JP",
38
+ fontPercent: 100,
39
+ textColor: "#332200",
40
+ });
81
41
 
82
- Load the following URL as a TurboWarp custom extension and allow it to run unsandboxed:
42
+ svgText.setText({
43
+ styleName: "dialogue-text",
44
+ target,
45
+ text: "The End",
46
+ });
83
47
 
84
- ```text
85
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-svg-text@0.3.0/dist/svg-text.js
86
- ```
48
+ const width = svgText.measureText({
49
+ styleName: "dialogue-text",
50
+ text: "The End",
51
+ });
87
52
 
88
- ```text
89
- define text style [narration] background [#fff4cc] text [#332200] font [Noto Sans JP] size [125] align [center] bubble direction [up]
90
- say [Once upon a time...\nIn a village by the sea...] with style [narration]
91
- set this sprite text [Act One\nUrashima Taro] with style [narration]
53
+ svgText.releaseTarget(target);
54
+ svgText.releaseAll();
92
55
  ```
93
56
 
94
- ### Composition API
95
-
96
- Composite runtimes can import `@kubohiroya/turbowarp-svg-text/composition` without registering the
97
- Standalone extension or adding blocks. The caller supplies one TurboWarp runtime and explicit
98
- targets.
57
+ `setText` creates or replaces the SVG skin owned by the composition. `measureText` returns the maximum measured line width in stage-relative pixels. `releaseTarget` and `releaseAll` destroy skins owned by the composition.
99
58
 
100
- ```js
101
- import { createSvgTextComposition } from "@kubohiroya/turbowarp-svg-text/composition";
59
+ `SvgTextComposition` is intentionally suitable for a host Capability adapter:
102
60
 
103
- const svgText = createSvgTextComposition({ runtime: Scratch.vm.runtime });
104
- svgText.defineStyle({
105
- name: "title",
106
- alignment: "center",
107
- backgroundColor: "#112233",
108
- direction: "up",
109
- font: "Noto Sans JP",
110
- fontPercent: 150,
111
- textColor: "#ffffff",
112
- });
113
- svgText.setText({ target, text: "The End", styleName: "title" });
61
+ ```ts
62
+ interface TextCapability {
63
+ setText(input: {
64
+ styleName: string;
65
+ target: { drawableID: number };
66
+ text: string;
67
+ }): void;
68
+ measureText(input: { styleName: string; text: string }): number;
69
+ releaseTarget(target: { drawableID: number }): void;
70
+ }
114
71
  ```
115
72
 
116
- Each composition owns its styles and the SVG skins it applies. Use `releaseTarget(target)` when a
117
- target leaves the composition, or call the idempotent finalizer `releaseAll()` to destroy every
118
- owned skin. Mutating methods reject calls after `releaseAll()`.
73
+ ## Integration with TurboWarp Bubble
74
+
75
+ `@kubohiroya/turbowarp-bubble` owns the Bubble outer shape, placement, portrait layers, and animation. It can use this package as its `BubbleTextCapability`, but the Bubble core does not depend on SVG Text itself.
119
76
 
120
77
  ## Development
121
78
 
122
- ```bash
123
- corepack enable
124
- pnpm install --frozen-lockfile
79
+ ```sh
80
+ pnpm install
125
81
  pnpm check
126
82
  ```
127
83
 
128
- The TypeScript source is built by `@kubohiroya/vite-plugin-turbowarp-extension` into a single `dist/svg-text.js` file. The build also generates `dist/extension-manifest.json` for saved-project API compatibility checks.
129
-
130
- ## Compatibility and limitations
131
-
132
- The extension runs unsandboxed because it uses the TurboWarp VM and renderer. Bubble support feature-detects the `SAY` and `STAGE_SIZE_CHANGED` events, the `Scratch.looks` custom state, drawable positioning, and scratch-render's `TextBubbleSkin.setStyle()`. SVG text actors use `createSVGSkin`, `updateDrawableSkinId`, and `destroySkin`.
133
-
134
- If compatible private drawing hooks are unavailable, center/right bubble alignment falls back to left alignment. If drawable positioning is unavailable, TurboWarp's standard bubble placement is retained. `setText` reports an explicit error when SVG skin APIs are unavailable. Standard Scratch Looks continues to handle bubble stopping, replacement, target tracking, the 330-character limit, and number formatting.
135
-
136
- ## License
137
-
138
- MPL-2.0
84
+ The package is distributed under MPL-2.0. See `LICENSE` for the source code terms.