js-draw 0.18.2 → 0.20.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 (269) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/bundledStyles.js +1 -0
  5. package/dist/cjs/src/Color4.d.ts +8 -0
  6. package/dist/cjs/src/Color4.js +67 -0
  7. package/dist/cjs/src/Editor.d.ts +2 -2
  8. package/dist/cjs/src/Editor.js +7 -7
  9. package/dist/cjs/src/SVGLoader.js +77 -12
  10. package/dist/cjs/src/Viewport.d.ts +2 -0
  11. package/dist/cjs/src/Viewport.js +6 -2
  12. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  13. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  14. package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  15. package/dist/cjs/src/components/BackgroundComponent.js +309 -0
  16. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  17. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  18. package/dist/cjs/src/components/Stroke.js +37 -3
  19. package/dist/cjs/src/components/TextComponent.d.ts +27 -17
  20. package/dist/cjs/src/components/TextComponent.js +23 -1
  21. package/dist/cjs/src/components/lib.d.ts +4 -3
  22. package/dist/cjs/src/components/lib.js +2 -2
  23. package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
  24. package/dist/cjs/src/lib.d.ts +30 -0
  25. package/dist/cjs/src/lib.js +30 -0
  26. package/dist/cjs/src/localizations/de.js +1 -1
  27. package/dist/cjs/src/localizations/es.js +1 -1
  28. package/dist/cjs/src/math/Path.js +1 -1
  29. package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  30. package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
  31. package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  32. package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
  33. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  34. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  35. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  36. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  37. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  38. package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
  39. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  40. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  41. package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
  42. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  43. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  44. package/dist/cjs/src/toolbar/localization.d.ts +2 -1
  45. package/dist/cjs/src/toolbar/localization.js +3 -2
  46. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  47. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  48. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
  49. package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
  50. package/dist/cjs/src/tools/FindTool.js +1 -1
  51. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  52. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  53. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  54. package/dist/cjs/src/tools/ToolController.js +6 -1
  55. package/dist/cjs/src/tools/lib.d.ts +1 -0
  56. package/dist/cjs/src/tools/lib.js +3 -1
  57. package/dist/cjs/src/tools/localization.d.ts +3 -0
  58. package/dist/cjs/src/tools/localization.js +3 -0
  59. package/dist/mjs/src/Color4.d.ts +8 -0
  60. package/dist/mjs/src/Color4.mjs +64 -0
  61. package/dist/mjs/src/Editor.d.ts +2 -2
  62. package/dist/mjs/src/Editor.mjs +6 -6
  63. package/dist/mjs/src/SVGLoader.mjs +76 -11
  64. package/dist/mjs/src/Viewport.d.ts +2 -0
  65. package/dist/mjs/src/Viewport.mjs +6 -2
  66. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  67. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  68. package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  69. package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
  70. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  71. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  72. package/dist/mjs/src/components/Stroke.mjs +37 -3
  73. package/dist/mjs/src/components/TextComponent.d.ts +27 -17
  74. package/dist/mjs/src/components/TextComponent.mjs +23 -1
  75. package/dist/mjs/src/components/lib.d.ts +4 -3
  76. package/dist/mjs/src/components/lib.mjs +2 -2
  77. package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
  78. package/dist/mjs/src/lib.d.ts +30 -0
  79. package/dist/mjs/src/lib.mjs +30 -0
  80. package/dist/mjs/src/localizations/de.mjs +1 -1
  81. package/dist/mjs/src/localizations/es.mjs +1 -1
  82. package/dist/mjs/src/math/Path.mjs +1 -1
  83. package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  84. package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
  85. package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  86. package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
  87. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  88. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  89. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  90. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  91. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  92. package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
  93. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  94. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  95. package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
  96. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  97. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  98. package/dist/mjs/src/toolbar/localization.d.ts +2 -1
  99. package/dist/mjs/src/toolbar/localization.mjs +3 -2
  100. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  101. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  102. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
  103. package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
  104. package/dist/mjs/src/tools/FindTool.mjs +1 -1
  105. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  106. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  107. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  108. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  109. package/dist/mjs/src/tools/lib.d.ts +1 -0
  110. package/dist/mjs/src/tools/lib.mjs +1 -0
  111. package/dist/mjs/src/tools/localization.d.ts +3 -0
  112. package/dist/mjs/src/tools/localization.mjs +3 -0
  113. package/jest.config.js +1 -1
  114. package/package.json +19 -17
  115. package/src/Editor.css +2 -2
  116. package/src/tools/SoundUITool.css +15 -0
  117. package/src/tools/tools.css +4 -0
  118. package/dist/cjs/src/components/ImageBackground.js +0 -146
  119. package/dist/mjs/src/components/ImageBackground.mjs +0 -139
  120. package/src/Color4.test.ts +0 -40
  121. package/src/Color4.ts +0 -236
  122. package/src/Editor.loadFrom.test.ts +0 -24
  123. package/src/Editor.toSVG.test.ts +0 -111
  124. package/src/Editor.ts +0 -1122
  125. package/src/EditorImage.test.ts +0 -120
  126. package/src/EditorImage.ts +0 -603
  127. package/src/EventDispatcher.test.ts +0 -123
  128. package/src/EventDispatcher.ts +0 -71
  129. package/src/Pointer.ts +0 -127
  130. package/src/SVGLoader.test.ts +0 -114
  131. package/src/SVGLoader.ts +0 -511
  132. package/src/UndoRedoHistory.test.ts +0 -33
  133. package/src/UndoRedoHistory.ts +0 -102
  134. package/src/Viewport.ts +0 -319
  135. package/src/bundle/bundled.ts +0 -7
  136. package/src/commands/Command.ts +0 -45
  137. package/src/commands/Duplicate.ts +0 -48
  138. package/src/commands/Erase.ts +0 -74
  139. package/src/commands/SerializableCommand.ts +0 -49
  140. package/src/commands/UnresolvedCommand.ts +0 -37
  141. package/src/commands/invertCommand.ts +0 -51
  142. package/src/commands/lib.ts +0 -16
  143. package/src/commands/localization.ts +0 -47
  144. package/src/commands/uniteCommands.test.ts +0 -23
  145. package/src/commands/uniteCommands.ts +0 -135
  146. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  147. package/src/components/AbstractComponent.ts +0 -364
  148. package/src/components/ImageBackground.test.ts +0 -35
  149. package/src/components/ImageBackground.ts +0 -176
  150. package/src/components/ImageComponent.ts +0 -171
  151. package/src/components/RestylableComponent.ts +0 -142
  152. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  153. package/src/components/Stroke.test.ts +0 -139
  154. package/src/components/Stroke.ts +0 -245
  155. package/src/components/TextComponent.test.ts +0 -99
  156. package/src/components/TextComponent.ts +0 -315
  157. package/src/components/UnknownSVGObject.test.ts +0 -10
  158. package/src/components/UnknownSVGObject.ts +0 -60
  159. package/src/components/builders/ArrowBuilder.ts +0 -107
  160. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  161. package/src/components/builders/LineBuilder.ts +0 -77
  162. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  163. package/src/components/builders/RectangleBuilder.ts +0 -74
  164. package/src/components/builders/types.ts +0 -15
  165. package/src/components/lib.ts +0 -25
  166. package/src/components/localization.ts +0 -22
  167. package/src/components/util/StrokeSmoother.ts +0 -293
  168. package/src/components/util/describeComponentList.ts +0 -18
  169. package/src/lib.ts +0 -37
  170. package/src/localization.ts +0 -34
  171. package/src/localizations/de.ts +0 -98
  172. package/src/localizations/en.ts +0 -8
  173. package/src/localizations/es.ts +0 -74
  174. package/src/localizations/getLocalizationTable.test.ts +0 -27
  175. package/src/localizations/getLocalizationTable.ts +0 -55
  176. package/src/math/LineSegment2.test.ts +0 -99
  177. package/src/math/LineSegment2.ts +0 -160
  178. package/src/math/Mat33.test.ts +0 -244
  179. package/src/math/Mat33.ts +0 -437
  180. package/src/math/Path.fromString.test.ts +0 -223
  181. package/src/math/Path.test.ts +0 -198
  182. package/src/math/Path.toString.test.ts +0 -77
  183. package/src/math/Path.ts +0 -790
  184. package/src/math/Rect2.test.ts +0 -204
  185. package/src/math/Rect2.ts +0 -315
  186. package/src/math/Triangle.ts +0 -29
  187. package/src/math/Vec2.test.ts +0 -30
  188. package/src/math/Vec2.ts +0 -18
  189. package/src/math/Vec3.test.ts +0 -44
  190. package/src/math/Vec3.ts +0 -218
  191. package/src/math/lib.ts +0 -15
  192. package/src/math/rounding.test.ts +0 -65
  193. package/src/math/rounding.ts +0 -156
  194. package/src/rendering/Display.ts +0 -249
  195. package/src/rendering/RenderingStyle.test.ts +0 -68
  196. package/src/rendering/RenderingStyle.ts +0 -55
  197. package/src/rendering/TextRenderingStyle.ts +0 -45
  198. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  199. package/src/rendering/caching/CacheRecord.ts +0 -77
  200. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  201. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  202. package/src/rendering/caching/RenderingCache.ts +0 -66
  203. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  204. package/src/rendering/caching/testUtils.ts +0 -35
  205. package/src/rendering/caching/types.ts +0 -34
  206. package/src/rendering/lib.ts +0 -6
  207. package/src/rendering/localization.ts +0 -20
  208. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  209. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  210. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  211. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  212. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  213. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  214. package/src/testing/beforeEachFile.ts +0 -8
  215. package/src/testing/createEditor.ts +0 -11
  216. package/src/testing/global.d.ts +0 -17
  217. package/src/testing/lib.ts +0 -3
  218. package/src/testing/loadExpectExtensions.ts +0 -25
  219. package/src/testing/sendPenEvent.ts +0 -31
  220. package/src/testing/sendTouchEvent.ts +0 -78
  221. package/src/toolbar/HTMLToolbar.ts +0 -492
  222. package/src/toolbar/IconProvider.ts +0 -736
  223. package/src/toolbar/lib.ts +0 -4
  224. package/src/toolbar/localization.ts +0 -106
  225. package/src/toolbar/makeColorInput.ts +0 -145
  226. package/src/toolbar/types.ts +0 -5
  227. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  228. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  229. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  230. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  231. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  232. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  233. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  234. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  235. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  236. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  237. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  238. package/src/toolbar/widgets/lib.ts +0 -13
  239. package/src/tools/BaseTool.ts +0 -76
  240. package/src/tools/Eraser.test.ts +0 -103
  241. package/src/tools/Eraser.ts +0 -139
  242. package/src/tools/FindTool.ts +0 -152
  243. package/src/tools/PanZoom.test.ts +0 -310
  244. package/src/tools/PanZoom.ts +0 -520
  245. package/src/tools/PasteHandler.ts +0 -95
  246. package/src/tools/Pen.test.ts +0 -194
  247. package/src/tools/Pen.ts +0 -226
  248. package/src/tools/PipetteTool.ts +0 -55
  249. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  250. package/src/tools/SelectionTool/Selection.ts +0 -607
  251. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  252. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  253. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  254. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  255. package/src/tools/SelectionTool/types.ts +0 -11
  256. package/src/tools/TextTool.ts +0 -326
  257. package/src/tools/ToolController.ts +0 -178
  258. package/src/tools/ToolEnabledGroup.ts +0 -14
  259. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  260. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  261. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  262. package/src/tools/UndoRedoShortcut.ts +0 -25
  263. package/src/tools/lib.ts +0 -21
  264. package/src/tools/localization.ts +0 -66
  265. package/src/types.ts +0 -234
  266. package/src/util/assertions.ts +0 -55
  267. package/src/util/fileToBase64.ts +0 -18
  268. package/src/util/untilNextAnimationFrame.ts +0 -9
  269. package/src/util/waitForTimeout.ts +0 -9
@@ -1,245 +0,0 @@
1
- import SerializableCommand from '../commands/SerializableCommand';
2
- import LineSegment2 from '../math/LineSegment2';
3
- import Mat33 from '../math/Mat33';
4
- import Path from '../math/Path';
5
- import Rect2 from '../math/Rect2';
6
- import Editor from '../Editor';
7
- import AbstractRenderer, { RenderablePathSpec } from '../rendering/renderers/AbstractRenderer';
8
- import RenderingStyle, { styleFromJSON, styleToJSON } from '../rendering/RenderingStyle';
9
- import AbstractComponent from './AbstractComponent';
10
- import { ImageComponentLocalization } from './localization';
11
- import RestyleableComponent, { ComponentStyle, createRestyleComponentCommand } from './RestylableComponent';
12
-
13
- interface StrokePart extends RenderablePathSpec {
14
- path: Path;
15
- }
16
-
17
- export default class Stroke extends AbstractComponent implements RestyleableComponent {
18
- private parts: StrokePart[];
19
- protected contentBBox: Rect2;
20
-
21
- // eslint-disable-next-line @typescript-eslint/prefer-as-const
22
- readonly isRestylableComponent: true = true;
23
-
24
- // See `getProportionalRenderingTime`
25
- private approximateRenderingTime: number;
26
-
27
- // Creates a `Stroke` from the given `parts`. All parts should have the
28
- // same color.
29
- public constructor(parts: RenderablePathSpec[]) {
30
- super('stroke');
31
-
32
- this.approximateRenderingTime = 0;
33
- this.parts = [];
34
-
35
- for (const section of parts) {
36
- const path = Path.fromRenderable(section);
37
- const pathBBox = this.bboxForPart(path.bbox, section.style);
38
-
39
- if (!this.contentBBox) {
40
- this.contentBBox = pathBBox;
41
- } else {
42
- this.contentBBox = this.contentBBox.union(pathBBox);
43
- }
44
-
45
- this.parts.push({
46
- path,
47
-
48
- // To implement RenderablePathSpec
49
- startPoint: path.startPoint,
50
- style: section.style,
51
- commands: path.parts,
52
- });
53
-
54
- this.approximateRenderingTime += path.parts.length;
55
- }
56
- this.contentBBox ??= Rect2.empty;
57
- }
58
-
59
- public getStyle(): ComponentStyle {
60
- if (this.parts.length === 0) {
61
- return { };
62
- }
63
- const firstPart = this.parts[0];
64
-
65
- if (
66
- firstPart.style.stroke === undefined
67
- || firstPart.style.stroke.width === 0
68
- ) {
69
- return {
70
- color: firstPart.style.fill,
71
- };
72
- }
73
-
74
- return {
75
- color: firstPart.style.stroke.color,
76
- };
77
- }
78
-
79
- public updateStyle(style: ComponentStyle): SerializableCommand {
80
- return createRestyleComponentCommand(this.getStyle(), style, this);
81
- }
82
-
83
- public forceStyle(style: ComponentStyle, editor: Editor|null): void {
84
- if (!style.color) {
85
- return;
86
- }
87
-
88
- this.parts = this.parts.map((part) => {
89
- const newStyle = {
90
- ...part.style,
91
- stroke: part.style.stroke ? {
92
- ...part.style.stroke,
93
- } : undefined,
94
- };
95
-
96
- // Change the stroke color if a stroked shape. Else,
97
- // change the fill.
98
- if (newStyle.stroke && newStyle.stroke.width > 0) {
99
- newStyle.stroke.color = style.color!;
100
- } else {
101
- newStyle.fill = style.color!;
102
- }
103
-
104
- return {
105
- path: part.path,
106
- startPoint: part.startPoint,
107
- commands: part.commands,
108
- style: newStyle,
109
- };
110
- });
111
-
112
- if (editor) {
113
- editor.image.queueRerenderOf(this);
114
- editor.queueRerender();
115
- }
116
- }
117
-
118
- public intersects(line: LineSegment2): boolean {
119
- for (const part of this.parts) {
120
- if (part.path.intersection(line).length > 0) {
121
- return true;
122
- }
123
- }
124
- return false;
125
- }
126
-
127
- public render(canvas: AbstractRenderer, visibleRect?: Rect2): void {
128
- canvas.startObject(this.getBBox());
129
- for (const part of this.parts) {
130
- const bbox = this.bboxForPart(part.path.bbox, part.style);
131
- if (visibleRect) {
132
- if (!bbox.intersects(visibleRect)) {
133
- continue;
134
- }
135
-
136
- const muchBiggerThanVisible = bbox.size.x > visibleRect.size.x * 2 || bbox.size.y > visibleRect.size.y * 2;
137
- if (muchBiggerThanVisible && !part.path.roughlyIntersects(visibleRect, part.style.stroke?.width ?? 0)) {
138
- continue;
139
- }
140
- }
141
-
142
- canvas.drawPath(part);
143
- }
144
- canvas.endObject(this.getLoadSaveData());
145
- }
146
-
147
- public getProportionalRenderingTime(): number {
148
- return this.approximateRenderingTime;
149
- }
150
-
151
- // Grows the bounding box for a given stroke part based on that part's style.
152
- private bboxForPart(origBBox: Rect2, style: RenderingStyle) {
153
- if (!style.stroke) {
154
- return origBBox;
155
- }
156
-
157
- return origBBox.grownBy(style.stroke.width / 2);
158
- }
159
-
160
- protected applyTransformation(affineTransfm: Mat33): void {
161
- this.contentBBox = Rect2.empty;
162
- let isFirstPart = true;
163
-
164
- // Update each part
165
- this.parts = this.parts.map((part) => {
166
- const newPath = part.path.transformedBy(affineTransfm);
167
- const newStyle = {
168
- ...part.style,
169
- stroke: part.style.stroke ? {
170
- ...part.style.stroke,
171
- } : undefined,
172
- };
173
-
174
- // Approximate the scale factor.
175
- if (newStyle.stroke) {
176
- const scaleFactor = affineTransfm.getScaleFactor();
177
- newStyle.stroke.width *= scaleFactor;
178
- }
179
-
180
- const newBBox = this.bboxForPart(newPath.bbox, newStyle);
181
-
182
- if (isFirstPart) {
183
- this.contentBBox = newBBox;
184
- isFirstPart = false;
185
- } else {
186
- this.contentBBox = this.contentBBox.union(newBBox);
187
- }
188
-
189
- return {
190
- path: newPath,
191
- startPoint: newPath.startPoint,
192
- commands: newPath.parts,
193
- style: newStyle,
194
- };
195
- });
196
- }
197
-
198
- public getPath() {
199
- let result: Path|null = null;
200
- for (const part of this.parts) {
201
- if (result) {
202
- result = result.union(part.path);
203
- } else {
204
- result ??= part.path;
205
- }
206
- }
207
- return result ?? Path.empty;
208
- }
209
-
210
- public description(localization: ImageComponentLocalization): string {
211
- return localization.stroke;
212
- }
213
-
214
- protected createClone(): AbstractComponent {
215
- return new Stroke(this.parts);
216
- }
217
-
218
- protected serializeToJSON() {
219
- return this.parts.map(part => {
220
- return {
221
- style: styleToJSON(part.style),
222
- path: part.path.serialize(),
223
- };
224
- });
225
- }
226
-
227
- /** @internal */
228
- public static deserializeFromJSON(json: any): Stroke {
229
- if (typeof json === 'string') {
230
- json = JSON.parse(json);
231
- }
232
-
233
- if (typeof json !== 'object' || typeof json.length !== 'number') {
234
- throw new Error(`${json} is missing required field, parts, or parts is of the wrong type.`);
235
- }
236
-
237
- const pathSpec: RenderablePathSpec[] = json.map((part: any) => {
238
- const style = styleFromJSON(part.style);
239
- return Path.fromString(part.path).toRenderable(style);
240
- });
241
- return new Stroke(pathSpec);
242
- }
243
- }
244
-
245
- AbstractComponent.registerComponent('stroke', Stroke.deserializeFromJSON);
@@ -1,99 +0,0 @@
1
- import Color4 from '../Color4';
2
- import EditorImage from '../EditorImage';
3
- import Mat33 from '../math/Mat33';
4
- import TextStyle from '../rendering/TextRenderingStyle';
5
- import createEditor from '../testing/createEditor';
6
- import AbstractComponent from './AbstractComponent';
7
- import TextComponent from './TextComponent';
8
-
9
-
10
- describe('TextComponent', () => {
11
- it('should be serializable', () => {
12
- const style: TextStyle = {
13
- size: 12,
14
- fontFamily: 'serif',
15
- renderingStyle: { fill: Color4.black },
16
- };
17
- const text = new TextComponent([ 'Foo' ], Mat33.identity, style);
18
- const serialized = text.serialize();
19
- const deserialized = AbstractComponent.deserialize(serialized) as TextComponent;
20
- expect(deserialized.getBBox()).objEq(text.getBBox());
21
- expect(deserialized['getText']()).toContain('Foo');
22
- });
23
-
24
- it('should be deserializable', () => {
25
- const textComponent = TextComponent.deserializeFromString(`{
26
- "textObjects": [ { "text": "Foo" } ],
27
- "transform": [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ],
28
- "style": {
29
- "fontFamily": "sans",
30
- "size": 10,
31
- "renderingStyle": { "fill": "#000" }
32
- }
33
- }`);
34
-
35
- expect(textComponent.getText()).toBe('Foo');
36
- expect(textComponent.getTransform()).objEq(Mat33.identity);
37
- expect(textComponent.getStyle().color!).objEq(Color4.black);
38
- expect(textComponent.getTextStyle().fontFamily!).toBe('sans');
39
- });
40
-
41
- it('should be restylable', () => {
42
- const style: TextStyle = {
43
- size: 10,
44
- fontFamily: 'sans',
45
- renderingStyle: { fill: Color4.red },
46
- };
47
- const text = new TextComponent([ 'Foo' ], Mat33.identity, style);
48
-
49
- expect(text.getStyle().color).objEq(Color4.red);
50
- text.forceStyle({
51
- color: Color4.green,
52
- }, null);
53
- expect(text.getStyle().color).objEq(Color4.green);
54
- expect(text.getTextStyle().renderingStyle.fill).objEq(Color4.green);
55
-
56
- const restyleCommand = text.updateStyle({
57
- color: Color4.purple,
58
- });
59
-
60
- // Should queue a re-render after restyling.
61
- const editor = createEditor();
62
- EditorImage.addElement(text).apply(editor);
63
-
64
- editor.rerender();
65
- expect(editor.isRerenderQueued()).toBe(false);
66
- editor.dispatch(restyleCommand);
67
- expect(editor.isRerenderQueued()).toBe(true);
68
-
69
- // Undoing should reset to the correct color.
70
- expect(text.getStyle().color).objEq(Color4.purple);
71
- editor.history.undo();
72
- expect(text.getStyle().color).objEq(Color4.green);
73
- });
74
-
75
- it('calling forceStyle on the duplicate of a TextComponent should preserve the original\'s style', () => {
76
- const originalStyle: TextStyle = {
77
- size: 11,
78
- fontFamily: 'sans-serif',
79
- renderingStyle: { fill: Color4.purple, },
80
- };
81
-
82
- const text1 = new TextComponent([ 'Test' ], Mat33.identity, originalStyle);
83
- const text2 = text1.clone() as TextComponent;
84
-
85
- text1.forceStyle({
86
- color: Color4.red,
87
- }, null);
88
-
89
- expect(text2.getStyle().color).objEq(Color4.purple);
90
- expect(text1.getStyle().color).objEq(Color4.red);
91
-
92
- text2.forceStyle({
93
- textStyle: originalStyle,
94
- }, null);
95
-
96
- expect(text1.getStyle().color).objEq(Color4.red);
97
- expect(text2.getTextStyle()).toMatchObject(originalStyle);
98
- });
99
- });
@@ -1,315 +0,0 @@
1
- import SerializableCommand from '../commands/SerializableCommand';
2
- import LineSegment2 from '../math/LineSegment2';
3
- import Mat33, { Mat33Array } from '../math/Mat33';
4
- import Rect2 from '../math/Rect2';
5
- import Editor from '../Editor';
6
- import { Vec2 } from '../math/Vec2';
7
- import AbstractRenderer from '../rendering/renderers/AbstractRenderer';
8
- import { cloneTextStyle, TextStyle, textStyleFromJSON, textStyleToJSON } from '../rendering/TextRenderingStyle';
9
- import AbstractComponent from './AbstractComponent';
10
- import { ImageComponentLocalization } from './localization';
11
- import RestyleableComponent, { ComponentStyle, createRestyleComponentCommand } from './RestylableComponent';
12
-
13
- const componentTypeId = 'text';
14
- export default class TextComponent extends AbstractComponent implements RestyleableComponent {
15
- protected contentBBox: Rect2;
16
-
17
- // eslint-disable-next-line @typescript-eslint/prefer-as-const
18
- readonly isRestylableComponent: true = true;
19
-
20
- public constructor(
21
- protected readonly textObjects: Array<string|TextComponent>,
22
- private transform: Mat33,
23
- private style: TextStyle,
24
- ) {
25
- super(componentTypeId);
26
- this.recomputeBBox();
27
-
28
- // If this has no direct children, choose a style representative of this' content
29
- // (useful for estimating the style of the TextComponent).
30
- const hasDirectContent = textObjects.some(obj => typeof obj === 'string');
31
- if (!hasDirectContent && textObjects.length > 0) {
32
- this.style = (textObjects[0] as TextComponent).getTextStyle();
33
- }
34
- }
35
-
36
- public static applyTextStyles(ctx: CanvasRenderingContext2D, style: TextStyle) {
37
- // Quote the font family if necessary.
38
- const fontFamily = style.fontFamily.match(/\s/) ? style.fontFamily.replace(/["]/g, '\\"') : style.fontFamily;
39
-
40
- ctx.font = [
41
- (style.size ?? 12) + 'px',
42
- style.fontWeight ?? '',
43
- `${fontFamily}`,
44
- style.fontWeight
45
- ].join(' ');
46
-
47
- ctx.textAlign = 'left';
48
- }
49
-
50
- private static textMeasuringCtx: CanvasRenderingContext2D|null = null;
51
-
52
- // Roughly estimate the bounding box of `text`. Use if no CanvasRenderingContext2D is available.
53
- private static estimateTextDimens(text: string, style: TextStyle): Rect2 {
54
- const widthEst = text.length * style.size;
55
- const heightEst = style.size;
56
-
57
- // Text is drawn with (0, 0) as its baseline. As such, the majority of the text's height should
58
- // be above (0, 0).
59
- return new Rect2(0, -heightEst * 2/3, widthEst, heightEst);
60
- }
61
-
62
- // Returns the bounding box of `text`. This is approximate if no Canvas is available.
63
- private static getTextDimens(text: string, style: TextStyle): Rect2 {
64
- TextComponent.textMeasuringCtx ??= document.createElement('canvas').getContext('2d') ?? null;
65
- if (!TextComponent.textMeasuringCtx) {
66
- return this.estimateTextDimens(text, style);
67
- }
68
-
69
- const ctx = TextComponent.textMeasuringCtx;
70
- TextComponent.applyTextStyles(ctx, style);
71
-
72
- const measure = ctx.measureText(text);
73
-
74
- // Text is drawn with (0,0) at the bottom left of the baseline.
75
- const textY = -measure.actualBoundingBoxAscent;
76
- const textHeight = measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent;
77
- return new Rect2(0, textY, measure.width, textHeight);
78
- }
79
-
80
- private computeBBoxOfPart(part: string|TextComponent) {
81
- if (typeof part === 'string') {
82
- const textBBox = TextComponent.getTextDimens(part, this.style);
83
- return textBBox.transformedBoundingBox(this.transform);
84
- } else {
85
- const bbox = part.contentBBox.transformedBoundingBox(this.transform);
86
- return bbox;
87
- }
88
- }
89
-
90
- private recomputeBBox() {
91
- let bbox: Rect2|null = null;
92
-
93
- for (const textObject of this.textObjects) {
94
- const currentBBox = this.computeBBoxOfPart(textObject);
95
- bbox ??= currentBBox;
96
- bbox = bbox.union(currentBBox);
97
- }
98
-
99
- this.contentBBox = bbox ?? Rect2.empty;
100
- }
101
-
102
- private renderInternal(canvas: AbstractRenderer) {
103
- const cursor = this.transform;
104
-
105
- for (const textObject of this.textObjects) {
106
- if (typeof textObject === 'string') {
107
- canvas.drawText(textObject, cursor, this.style);
108
- } else {
109
- canvas.pushTransform(cursor);
110
- textObject.renderInternal(canvas);
111
- canvas.popTransform();
112
- }
113
- }
114
- }
115
-
116
- public render(canvas: AbstractRenderer, _visibleRect?: Rect2): void {
117
- canvas.startObject(this.contentBBox);
118
- this.renderInternal(canvas);
119
- canvas.endObject(this.getLoadSaveData());
120
- }
121
-
122
- public getProportionalRenderingTime(): number {
123
- return this.textObjects.length;
124
- }
125
-
126
- public intersects(lineSegment: LineSegment2): boolean {
127
-
128
- // Convert canvas space to internal space.
129
- const invTransform = this.transform.inverse();
130
- const p1InThisSpace = invTransform.transformVec2(lineSegment.p1);
131
- const p2InThisSpace = invTransform.transformVec2(lineSegment.p2);
132
- lineSegment = new LineSegment2(p1InThisSpace, p2InThisSpace);
133
-
134
- for (const subObject of this.textObjects) {
135
- if (typeof subObject === 'string') {
136
- const textBBox = TextComponent.getTextDimens(subObject, this.style);
137
-
138
- // TODO: Use a better intersection check. Perhaps draw the text onto a CanvasElement and
139
- // use pixel-testing to check for intersection with its contour.
140
- if (textBBox.getEdges().some(edge => lineSegment.intersection(edge) !== null)) {
141
- return true;
142
- }
143
- } else {
144
- if (subObject.intersects(lineSegment)) {
145
- return true;
146
- }
147
- }
148
- }
149
-
150
- return false;
151
- }
152
-
153
- public getStyle(): ComponentStyle {
154
- return {
155
- color: this.style.renderingStyle.fill,
156
-
157
- // Make a copy
158
- textStyle: {
159
- ...this.style,
160
- renderingStyle: {
161
- ...this.style.renderingStyle,
162
- },
163
- },
164
- };
165
- }
166
-
167
- public updateStyle(style: ComponentStyle): SerializableCommand {
168
- return createRestyleComponentCommand(this.getStyle(), style, this);
169
- }
170
-
171
- public forceStyle(style: ComponentStyle, editor: Editor|null): void {
172
- if (style.textStyle) {
173
- this.style = cloneTextStyle(style.textStyle);
174
- } else if (style.color) {
175
- this.style = {
176
- ...this.style,
177
- renderingStyle: {
178
- ...this.style.renderingStyle,
179
- fill: style.color,
180
- },
181
- };
182
- } else {
183
- return;
184
- }
185
-
186
- for (const child of this.textObjects) {
187
- if (child instanceof TextComponent) {
188
- child.forceStyle(style, editor);
189
- }
190
- }
191
-
192
- if (editor) {
193
- editor.image.queueRerenderOf(this);
194
- editor.queueRerender();
195
- }
196
- }
197
-
198
- // See this.getStyle
199
- public getTextStyle() {
200
- return cloneTextStyle(this.style);
201
- }
202
-
203
- public getBaselinePos() {
204
- return this.transform.transformVec2(Vec2.zero);
205
- }
206
-
207
- public getTransform(): Mat33 {
208
- return this.transform;
209
- }
210
-
211
- protected applyTransformation(affineTransfm: Mat33): void {
212
- this.transform = affineTransfm.rightMul(this.transform);
213
- this.recomputeBBox();
214
- }
215
-
216
- protected createClone(): AbstractComponent {
217
- const clonedTextObjects = this.textObjects.map(obj => {
218
- if (typeof obj === 'string') {
219
- return obj;
220
- } else {
221
- return obj.createClone() as TextComponent;
222
- }
223
- });
224
- return new TextComponent(clonedTextObjects, this.transform, this.style);
225
- }
226
-
227
- public getText() {
228
- const result: string[] = [];
229
-
230
- for (const textObject of this.textObjects) {
231
- if (typeof textObject === 'string') {
232
- result.push(textObject);
233
- } else {
234
- result.push(textObject.getText());
235
- }
236
- }
237
-
238
- return result.join('\n');
239
- }
240
-
241
- public description(localizationTable: ImageComponentLocalization): string {
242
- return localizationTable.text(this.getText());
243
- }
244
-
245
- // Do not rely on the output of `serializeToJSON` taking any particular format.
246
- protected serializeToJSON(): Record<string, any> {
247
- const serializableStyle = textStyleToJSON(this.style);
248
-
249
- const serializedTextObjects = this.textObjects.map(text => {
250
- if (typeof text === 'string') {
251
- return {
252
- text,
253
- };
254
- } else {
255
- return {
256
- json: text.serializeToJSON(),
257
- };
258
- }
259
- });
260
-
261
- return {
262
- textObjects: serializedTextObjects,
263
- transform: this.transform.toArray(),
264
- style: serializableStyle,
265
- };
266
- }
267
-
268
- // @internal
269
- public static deserializeFromString(json: any): TextComponent {
270
- if (typeof json === 'string') {
271
- json = JSON.parse(json);
272
- }
273
-
274
- const style = textStyleFromJSON(json.style);
275
-
276
- const textObjects: Array<string|TextComponent> = json.textObjects.map((data: any) => {
277
- if ((data.text ?? null) !== null) {
278
- return data.text;
279
- }
280
-
281
- return TextComponent.deserializeFromString(data.json);
282
- });
283
-
284
- json.transform = json.transform.filter((elem: any) => typeof elem === 'number');
285
- if (json.transform.length !== 9) {
286
- throw new Error(`Unable to deserialize transform, ${json.transform}.`);
287
- }
288
-
289
- const transformData = json.transform as Mat33Array;
290
- const transform = new Mat33(...transformData);
291
-
292
- return new TextComponent(textObjects, transform, style);
293
- }
294
-
295
- public static fromLines(lines: string[], transform: Mat33, style: TextStyle): AbstractComponent {
296
- let lastComponent: TextComponent|null = null;
297
- const components: TextComponent[] = [];
298
-
299
- for (const line of lines) {
300
- let position = Vec2.zero;
301
- if (lastComponent) {
302
- const lineMargin = Math.floor(style.size);
303
- position = lastComponent.getBBox().bottomLeft.plus(Vec2.unitY.times(lineMargin));
304
- }
305
-
306
- const component = new TextComponent([ line ], Mat33.translation(position), style);
307
- components.push(component);
308
- lastComponent = component;
309
- }
310
-
311
- return new TextComponent(components, transform, style);
312
- }
313
- }
314
-
315
- AbstractComponent.registerComponent(componentTypeId, (data: string) => TextComponent.deserializeFromString(data));
@@ -1,10 +0,0 @@
1
- import AbstractComponent from './AbstractComponent';
2
- import UnknownSVGObject from './UnknownSVGObject';
3
-
4
- describe('UnknownSVGObject', () => {
5
- it('should not be deserializable', () => {
6
- const obj = new UnknownSVGObject(document.createElementNS('http://www.w3.org/2000/svg', 'circle'));
7
- const serialized = obj.serialize();
8
- expect(() => AbstractComponent.deserialize(serialized)).toThrow(/.*cannot be deserialized.*/);
9
- });
10
- });