laymur 0.28.0 → 0.29.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 (81) hide show
  1. package/README.md +53 -4
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +38 -5
  4. package/dist/core/constraints/UICustomConstraint.js +1 -1
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +1 -1
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +1 -1
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +1 -1
  9. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +6 -0
  10. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.js +8 -0
  11. package/dist/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.js +1 -1
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +1 -1
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +1 -1
  15. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  16. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  17. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  18. package/dist/core/elements/UIDummy/UIDummy.js +3 -1
  19. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  20. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  21. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  22. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  23. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  24. package/dist/core/elements/UIImage/UIImage.js +14 -0
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  26. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  27. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  28. package/dist/core/elements/UIScene/UIScene.js +29 -5
  29. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  30. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  31. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  32. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  33. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  34. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  35. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  36. package/dist/core/elements/UIText/UIText.d.ts +58 -28
  37. package/dist/core/elements/UIText/UIText.js +113 -138
  38. package/dist/core/index.d.ts +10 -4
  39. package/dist/core/index.js +5 -1
  40. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  41. package/dist/core/layers/UILayer/UILayer.js +35 -4
  42. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  43. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +3 -0
  44. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  45. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  46. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  47. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  48. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  49. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  50. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  51. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  52. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  53. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  54. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  55. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  56. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  57. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  58. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  59. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  60. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  61. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  62. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  63. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  64. package/dist/core/miscellaneous/shared.d.ts +14 -0
  65. package/dist/core/miscellaneous/shared.js +11 -0
  66. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +88 -30
  67. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  68. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -66
  69. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -294
  70. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  71. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  72. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +1 -1
  73. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +1 -1
  74. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +1 -1
  75. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  76. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  77. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  78. package/dist/particles/elements/UIEmitter.Internal.d.ts +3 -0
  79. package/dist/particles/elements/UIEmitter.Internal.js +1 -0
  80. package/dist/particles/elements/UIEmitter.js +3 -2
  81. package/package.json +1 -1
@@ -1,88 +1,123 @@
1
1
  import { assertValidNonNegativeNumber, assertValidNumber, assertValidPositiveNumber, } from "../asserts.js";
2
- import { TEXT_STYLE_DEFAULT_ALIGN, TEXT_STYLE_DEFAULT_COLOR, TEXT_STYLE_DEFAULT_ENABLE_SHADOW, TEXT_STYLE_DEFAULT_ENABLE_STROKE, TEXT_STYLE_DEFAULT_FONT_FAMILY, TEXT_STYLE_DEFAULT_FONT_SIZE, TEXT_STYLE_DEFAULT_FONT_STYLE, TEXT_STYLE_DEFAULT_FONT_WEIGHT, TEXT_STYLE_DEFAULT_LINE_HEIGHT, TEXT_STYLE_DEFAULT_SHADOW_BLUR, TEXT_STYLE_DEFAULT_SHADOW_COLOR, TEXT_STYLE_DEFAULT_SHADOW_OFFSET_X, TEXT_STYLE_DEFAULT_SHADOW_OFFSET_Y, TEXT_STYLE_DEFAULT_STROKE_COLOR, TEXT_STYLE_DEFAULT_STROKE_THICKNESS, } from "./UITextStyle.Internal.js";
3
- /** Text style configuration for UI text rendering */
2
+ import { toCanvasColor } from "../color/canvasColor.js";
3
+ import { resolvePaint } from "../paint/UIPaint.js";
4
+ import { TEXT_STYLE_DEFAULT_ALIGN, TEXT_STYLE_DEFAULT_COLOR, TEXT_STYLE_DEFAULT_FONT_FAMILY, TEXT_STYLE_DEFAULT_FONT_SIZE, TEXT_STYLE_DEFAULT_FONT_STYLE, TEXT_STYLE_DEFAULT_FONT_WEIGHT, TEXT_STYLE_DEFAULT_LINE_HEIGHT, TEXT_STYLE_DEFAULT_PAINT_BOX, TEXT_STYLE_DEFAULT_SHADOWS, TEXT_STYLE_DEFAULT_STROKE_JOIN, TEXT_STYLE_DEFAULT_STROKES, } from "./UITextStyle.Internal.js";
5
+ /** Value of every property after `setDefaults` */
6
+ const TEXT_STYLE_DEFAULTS = {
7
+ color: TEXT_STYLE_DEFAULT_COLOR,
8
+ paintBox: TEXT_STYLE_DEFAULT_PAINT_BOX,
9
+ align: TEXT_STYLE_DEFAULT_ALIGN,
10
+ fontFamily: TEXT_STYLE_DEFAULT_FONT_FAMILY,
11
+ fontSize: TEXT_STYLE_DEFAULT_FONT_SIZE,
12
+ fontStyle: TEXT_STYLE_DEFAULT_FONT_STYLE,
13
+ fontWeight: TEXT_STYLE_DEFAULT_FONT_WEIGHT,
14
+ lineHeight: TEXT_STYLE_DEFAULT_FONT_SIZE * TEXT_STYLE_DEFAULT_LINE_HEIGHT,
15
+ strokes: TEXT_STYLE_DEFAULT_STROKES,
16
+ shadows: TEXT_STYLE_DEFAULT_SHADOWS,
17
+ };
18
+ const TEXT_STYLE_KEYS = Object.keys(TEXT_STYLE_DEFAULTS);
19
+ /**
20
+ * Sets a property of a resolved style when the value is defined.
21
+ * @param style Style to change
22
+ * @param key Property
23
+ * @param value Value, or undefined to keep the current one
24
+ */
25
+ function assignDefined(style, key, value) {
26
+ if (value !== undefined) {
27
+ style[key] = value;
28
+ }
29
+ }
30
+ /**
31
+ * Converts outline configs to stored outlines.
32
+ * @param strokes Outline configs
33
+ * @returns Frozen outlines with colors as CSS strings
34
+ */
35
+ function resolveStrokes(strokes) {
36
+ return Object.freeze(strokes.map((stroke) => {
37
+ assertValidNonNegativeNumber(stroke.thickness, "UITextStyle.strokes.thickness");
38
+ return Object.freeze({
39
+ paint: resolvePaint(stroke.paint),
40
+ thickness: stroke.thickness,
41
+ join: stroke.join ?? TEXT_STYLE_DEFAULT_STROKE_JOIN,
42
+ });
43
+ }));
44
+ }
45
+ /**
46
+ * Converts shadow configs to stored shadows.
47
+ * @param shadows Shadow configs
48
+ * @returns Frozen shadows with colors as CSS strings
49
+ */
50
+ function resolveShadows(shadows) {
51
+ return Object.freeze(shadows.map((shadow) => {
52
+ const offsetX = shadow.offsetX ?? 0;
53
+ const offsetY = shadow.offsetY ?? 0;
54
+ const blur = shadow.blur ?? 0;
55
+ assertValidNumber(offsetX, "UITextStyle.shadows.offsetX");
56
+ assertValidNumber(offsetY, "UITextStyle.shadows.offsetY");
57
+ assertValidNonNegativeNumber(blur, "UITextStyle.shadows.blur");
58
+ return Object.freeze({ color: toCanvasColor(shadow.color), offsetX, offsetY, blur });
59
+ }));
60
+ }
61
+ /**
62
+ * Text style of a span or of a whole text. Properties left undefined are taken from the
63
+ * fallback style, the common style of the text for a span, then from the defaults. Lists of
64
+ * strokes and shadows are taken as a whole.
65
+ */
4
66
  export class UITextStyle {
5
67
  /** @param config Partial style configuration. Unspecified properties remain undefined. */
6
68
  constructor(config) {
69
+ this.values = {};
7
70
  this.dirtyInternal = true;
8
- this.colorInternal = config?.color;
9
- this.alignInternal = config?.align;
10
- this.fontFamilyInternal = config?.fontFamily;
11
- this.fontSizeInternal = config?.fontSize;
12
- this.fontStyleInternal = config?.fontStyle;
13
- this.fontWeightInternal = config?.fontWeight;
14
- this.lineHeightInternal = config?.lineHeight;
15
- this.enableShadowInternal = config?.enableShadow;
16
- this.shadowOffsetXInternal = config?.shadowOffsetX;
17
- this.shadowOffsetYInternal = config?.shadowOffsetY;
18
- this.shadowBlurInternal = config?.shadowBlur;
19
- this.shadowColorInternal = config?.shadowColor;
20
- this.enableStrokeInternal = config?.enableStroke;
21
- this.strokeColorInternal = config?.strokeColor;
22
- this.strokeThicknessInternal = config?.strokeThickness;
23
- }
24
- /** Text color */
71
+ this.color = config?.color;
72
+ this.paintBox = config?.paintBox;
73
+ this.align = config?.align;
74
+ this.fontFamily = config?.fontFamily;
75
+ this.fontSize = config?.fontSize;
76
+ this.fontStyle = config?.fontStyle;
77
+ this.fontWeight = config?.fontWeight;
78
+ this.lineHeight = config?.lineHeight;
79
+ this.strokes = config?.strokes;
80
+ this.shadows = config?.shadows;
81
+ }
82
+ /** Color or gradient of the glyphs. A color is kept as a CSS string. */
25
83
  get color() {
26
- return this.colorInternal;
84
+ return this.values.color;
85
+ }
86
+ /** Box the gradients of the fill and of the strokes are laid over */
87
+ get paintBox() {
88
+ return this.values.paintBox;
27
89
  }
28
90
  /** Text alignment */
29
91
  get align() {
30
- return this.alignInternal;
92
+ return this.values.align;
31
93
  }
32
94
  /** Font family name */
33
95
  get fontFamily() {
34
- return this.fontFamilyInternal;
96
+ return this.values.fontFamily;
35
97
  }
36
98
  /** Font size in pixels */
37
99
  get fontSize() {
38
- return this.fontSizeInternal;
100
+ return this.values.fontSize;
39
101
  }
40
102
  /** Font style */
41
103
  get fontStyle() {
42
- return this.fontStyleInternal;
104
+ return this.values.fontStyle;
43
105
  }
44
106
  /** Font weight */
45
107
  get fontWeight() {
46
- return this.fontWeightInternal;
108
+ return this.values.fontWeight;
47
109
  }
48
- /**
49
- * Line height in pixels: the distance between baselines, not a multiple of the font size.
50
- * Defaults to 1.2 times the font size.
51
- */
110
+ /** Line height in pixels, the distance between baselines */
52
111
  get lineHeight() {
53
- return this.lineHeightInternal;
54
- }
55
- /** Whether shadow is enabled */
56
- get enableShadow() {
57
- return this.enableShadowInternal;
58
- }
59
- /** Shadow horizontal offset in pixels */
60
- get shadowOffsetX() {
61
- return this.shadowOffsetXInternal;
112
+ return this.values.lineHeight;
62
113
  }
63
- /** Shadow vertical offset in pixels */
64
- get shadowOffsetY() {
65
- return this.shadowOffsetYInternal;
114
+ /** Outlines from the innermost to the outermost */
115
+ get strokes() {
116
+ return this.values.strokes;
66
117
  }
67
- /** Shadow blur radius in pixels */
68
- get shadowBlur() {
69
- return this.shadowBlurInternal;
70
- }
71
- /** Shadow color */
72
- get shadowColor() {
73
- return this.shadowColorInternal;
74
- }
75
- /** Whether stroke is enabled */
76
- get enableStroke() {
77
- return this.enableStrokeInternal;
78
- }
79
- /** Stroke color */
80
- get strokeColor() {
81
- return this.strokeColorInternal;
82
- }
83
- /** Stroke thickness in pixels */
84
- get strokeThickness() {
85
- return this.strokeThicknessInternal;
118
+ /** Shadows and glows, the first one on top */
119
+ get shadows() {
120
+ return this.values.shadows;
86
121
  }
87
122
  /**
88
123
  * Indicates whether any property has been modified.
@@ -92,181 +127,98 @@ export class UITextStyle {
92
127
  get dirty() {
93
128
  return this.dirtyInternal;
94
129
  }
130
+ /** Color or gradient of the glyphs. A string is a `UIColor` name or a CSS color. */
95
131
  set color(value) {
96
- if (value !== this.colorInternal) {
97
- this.colorInternal = value;
98
- this.dirtyInternal = true;
99
- }
132
+ this.setValue("color", value === undefined ? undefined : resolvePaint(value));
133
+ }
134
+ /** Box the gradients of the fill and of the strokes are laid over */
135
+ set paintBox(value) {
136
+ this.setValue("paintBox", value);
100
137
  }
138
+ /** Text alignment */
101
139
  set align(value) {
102
- if (value !== this.alignInternal) {
103
- this.alignInternal = value;
104
- this.dirtyInternal = true;
105
- }
140
+ this.setValue("align", value);
106
141
  }
142
+ /** Font family name */
107
143
  set fontFamily(value) {
108
- if (value !== this.fontFamilyInternal) {
109
- this.fontFamilyInternal = value;
110
- this.dirtyInternal = true;
111
- }
144
+ this.setValue("fontFamily", value);
112
145
  }
146
+ /** Font size in pixels */
113
147
  set fontSize(value) {
114
148
  if (value !== undefined) {
115
149
  assertValidPositiveNumber(value, "UITextStyle.fontSize");
116
150
  }
117
- if (value !== this.fontSizeInternal) {
118
- this.fontSizeInternal = value;
119
- this.dirtyInternal = true;
120
- }
151
+ this.setValue("fontSize", value);
121
152
  }
153
+ /** Font style */
122
154
  set fontStyle(value) {
123
- if (value !== this.fontStyleInternal) {
124
- this.fontStyleInternal = value;
125
- this.dirtyInternal = true;
126
- }
155
+ this.setValue("fontStyle", value);
127
156
  }
157
+ /** Font weight */
128
158
  set fontWeight(value) {
129
159
  if (typeof value === "number") {
130
160
  assertValidPositiveNumber(value, "UITextStyle.fontWeight");
131
161
  }
132
- if (value !== this.fontWeightInternal) {
133
- this.fontWeightInternal = value;
134
- this.dirtyInternal = true;
135
- }
162
+ this.setValue("fontWeight", value);
136
163
  }
164
+ /** Line height in pixels, the distance between baselines */
137
165
  set lineHeight(value) {
138
166
  if (value !== undefined) {
139
167
  assertValidNumber(value, "UITextStyle.lineHeight");
140
168
  }
141
- if (value !== this.lineHeightInternal) {
142
- this.lineHeightInternal = value;
143
- this.dirtyInternal = true;
144
- }
145
- }
146
- set enableShadow(value) {
147
- if (value !== this.enableShadowInternal) {
148
- this.enableShadowInternal = value;
149
- this.dirtyInternal = true;
150
- }
169
+ this.setValue("lineHeight", value);
151
170
  }
152
- set shadowOffsetX(value) {
153
- if (value !== undefined) {
154
- assertValidNumber(value, "UITextStyle.shadowOffsetX");
155
- }
156
- if (value !== this.shadowOffsetXInternal) {
157
- this.shadowOffsetXInternal = value;
158
- this.dirtyInternal = true;
159
- }
160
- }
161
- set shadowOffsetY(value) {
162
- if (value !== undefined) {
163
- assertValidNumber(value, "UITextStyle.shadowOffsetY");
164
- }
165
- if (value !== this.shadowOffsetYInternal) {
166
- this.shadowOffsetYInternal = value;
167
- this.dirtyInternal = true;
168
- }
169
- }
170
- set shadowBlur(value) {
171
- if (value !== undefined) {
172
- assertValidNonNegativeNumber(value, "UITextStyle.shadowBlur");
173
- }
174
- if (value !== this.shadowBlurInternal) {
175
- this.shadowBlurInternal = value;
176
- this.dirtyInternal = true;
177
- }
178
- }
179
- set shadowColor(value) {
180
- if (value !== this.shadowColorInternal) {
181
- this.shadowColorInternal = value;
182
- this.dirtyInternal = true;
183
- }
184
- }
185
- set enableStroke(value) {
186
- if (value !== this.enableStrokeInternal) {
187
- this.enableStrokeInternal = value;
188
- this.dirtyInternal = true;
189
- }
190
- }
191
- set strokeColor(value) {
192
- if (value !== this.strokeColorInternal) {
193
- this.strokeColorInternal = value;
194
- this.dirtyInternal = true;
195
- }
171
+ /**
172
+ * Outlines from the innermost to the outermost. Each one lies outside the previous one. An
173
+ * empty list draws no outline, undefined takes the outlines of the fallback style.
174
+ */
175
+ set strokes(value) {
176
+ this.setValue("strokes", value === undefined ? undefined : resolveStrokes(value));
196
177
  }
197
- set strokeThickness(value) {
198
- if (value !== undefined) {
199
- assertValidNonNegativeNumber(value, "UITextStyle.strokeThickness");
200
- }
201
- if (value !== this.strokeThicknessInternal) {
202
- this.strokeThicknessInternal = value;
203
- this.dirtyInternal = true;
204
- }
178
+ /**
179
+ * Shadows and glows, the first one on top. An empty list draws no shadow, undefined takes the
180
+ * shadows of the fallback style.
181
+ */
182
+ set shadows(value) {
183
+ this.setValue("shadows", value === undefined ? undefined : resolveShadows(value));
205
184
  }
206
185
  /**
207
186
  * Resolves complete style from primary and fallback sources.
208
187
  * @param primary First style to check for each property
209
188
  * @param fallback Second style to check if primary is undefined
210
- * @returns Complete style configuration with all defaults applied
189
+ * @returns Complete style, with defaults for what neither defines. The default line height
190
+ * follows the resolved font size.
191
+ * @internal
211
192
  */
212
193
  static resolve(primary, fallback) {
213
- const fontSize = primary?.fontSizeInternal ?? fallback?.fontSizeInternal ?? TEXT_STYLE_DEFAULT_FONT_SIZE;
214
- return {
215
- color: primary?.colorInternal ?? fallback?.colorInternal ?? TEXT_STYLE_DEFAULT_COLOR,
216
- align: primary?.alignInternal ?? fallback?.alignInternal ?? TEXT_STYLE_DEFAULT_ALIGN,
217
- fontFamily: primary?.fontFamilyInternal ??
218
- fallback?.fontFamilyInternal ??
219
- TEXT_STYLE_DEFAULT_FONT_FAMILY,
220
- fontSize,
221
- fontStyle: primary?.fontStyleInternal ?? fallback?.fontStyleInternal ?? TEXT_STYLE_DEFAULT_FONT_STYLE,
222
- fontWeight: primary?.fontWeightInternal ??
223
- fallback?.fontWeightInternal ??
224
- TEXT_STYLE_DEFAULT_FONT_WEIGHT,
225
- lineHeight: primary?.lineHeightInternal ??
226
- fallback?.lineHeightInternal ??
227
- fontSize * TEXT_STYLE_DEFAULT_LINE_HEIGHT,
228
- enableShadow: primary?.enableShadowInternal ??
229
- fallback?.enableShadowInternal ??
230
- TEXT_STYLE_DEFAULT_ENABLE_SHADOW,
231
- shadowOffsetX: primary?.shadowOffsetXInternal ??
232
- fallback?.shadowOffsetXInternal ??
233
- TEXT_STYLE_DEFAULT_SHADOW_OFFSET_X,
234
- shadowOffsetY: primary?.shadowOffsetYInternal ??
235
- fallback?.shadowOffsetYInternal ??
236
- TEXT_STYLE_DEFAULT_SHADOW_OFFSET_Y,
237
- shadowBlur: primary?.shadowBlurInternal ??
238
- fallback?.shadowBlurInternal ??
239
- TEXT_STYLE_DEFAULT_SHADOW_BLUR,
240
- shadowColor: primary?.shadowColorInternal ??
241
- fallback?.shadowColorInternal ??
242
- TEXT_STYLE_DEFAULT_SHADOW_COLOR,
243
- enableStroke: primary?.enableStrokeInternal ??
244
- fallback?.enableStrokeInternal ??
245
- TEXT_STYLE_DEFAULT_ENABLE_STROKE,
246
- strokeColor: primary?.strokeColorInternal ??
247
- fallback?.strokeColorInternal ??
248
- TEXT_STYLE_DEFAULT_STROKE_COLOR,
249
- strokeThickness: primary?.strokeThicknessInternal ??
250
- fallback?.strokeThicknessInternal ??
251
- TEXT_STYLE_DEFAULT_STROKE_THICKNESS,
252
- };
194
+ const resolved = { ...TEXT_STYLE_DEFAULTS };
195
+ for (const key of TEXT_STYLE_KEYS) {
196
+ assignDefined(resolved, key, primary?.values[key] ?? fallback?.values[key]);
197
+ }
198
+ if (primary?.values.lineHeight === undefined && fallback?.values.lineHeight === undefined) {
199
+ resolved.lineHeight = resolved.fontSize * TEXT_STYLE_DEFAULT_LINE_HEIGHT;
200
+ }
201
+ return resolved;
253
202
  }
254
203
  /**
255
- * Calculates maximum padding required for stroke and shadow of a resolved style.
256
- * The stroke is centered on the glyph outline, so only half of it is outside.
257
- * The shadow is cast by the stroke as well as by the fill.
204
+ * Calculates the padding a resolved style needs on every side for its outlines and shadows.
205
+ * The outlines add up outside the glyphs. A shadow is cast by the outlined glyphs and spreads
206
+ * by its offset and about twice its blur radius.
258
207
  * @param style Resolved style
259
- * @returns Padding in pixels needed on all sides
208
+ * @returns Padding in pixels
209
+ * @internal
260
210
  */
261
211
  static calculatePadding(style) {
262
- let padding = style.enableStroke ? style.strokeThickness / 2 : 0;
263
- if (style.enableShadow) {
264
- const shadowSpread = style.shadowBlur * 2;
265
- const shadowMaxX = Math.abs(style.shadowOffsetX) + shadowSpread;
266
- const shadowMaxY = Math.abs(style.shadowOffsetY) + shadowSpread;
267
- padding += Math.max(shadowMaxX, shadowMaxY);
212
+ let outline = 0;
213
+ for (const stroke of style.strokes) {
214
+ outline += stroke.thickness;
215
+ }
216
+ let shadowSpread = 0;
217
+ for (const shadow of style.shadows) {
218
+ const offset = Math.max(Math.abs(shadow.offsetX), Math.abs(shadow.offsetY));
219
+ shadowSpread = Math.max(shadowSpread, offset + shadow.blur * 2);
268
220
  }
269
- return Math.ceil(padding);
221
+ return Math.ceil(outline + shadowSpread);
270
222
  }
271
223
  /** @internal */
272
224
  setDirtyFalse() {
@@ -274,117 +226,36 @@ export class UITextStyle {
274
226
  }
275
227
  /** Sets all properties to default values */
276
228
  setDefaults() {
277
- if (this.colorInternal !== TEXT_STYLE_DEFAULT_COLOR ||
278
- this.alignInternal !== TEXT_STYLE_DEFAULT_ALIGN ||
279
- this.fontFamilyInternal !== TEXT_STYLE_DEFAULT_FONT_FAMILY ||
280
- this.fontSizeInternal !== TEXT_STYLE_DEFAULT_FONT_SIZE ||
281
- this.fontStyleInternal !== TEXT_STYLE_DEFAULT_FONT_STYLE ||
282
- this.fontWeightInternal !== TEXT_STYLE_DEFAULT_FONT_WEIGHT ||
283
- this.lineHeightInternal !== TEXT_STYLE_DEFAULT_FONT_SIZE * TEXT_STYLE_DEFAULT_LINE_HEIGHT ||
284
- this.enableShadowInternal !== TEXT_STYLE_DEFAULT_ENABLE_SHADOW ||
285
- this.shadowOffsetXInternal !== TEXT_STYLE_DEFAULT_SHADOW_OFFSET_X ||
286
- this.shadowOffsetYInternal !== TEXT_STYLE_DEFAULT_SHADOW_OFFSET_Y ||
287
- this.shadowBlurInternal !== TEXT_STYLE_DEFAULT_SHADOW_BLUR ||
288
- this.shadowColorInternal !== TEXT_STYLE_DEFAULT_SHADOW_COLOR ||
289
- this.enableStrokeInternal !== TEXT_STYLE_DEFAULT_ENABLE_STROKE ||
290
- this.strokeColorInternal !== TEXT_STYLE_DEFAULT_STROKE_COLOR ||
291
- this.strokeThicknessInternal !== TEXT_STYLE_DEFAULT_STROKE_THICKNESS) {
292
- this.colorInternal = TEXT_STYLE_DEFAULT_COLOR;
293
- this.alignInternal = TEXT_STYLE_DEFAULT_ALIGN;
294
- this.fontFamilyInternal = TEXT_STYLE_DEFAULT_FONT_FAMILY;
295
- this.fontSizeInternal = TEXT_STYLE_DEFAULT_FONT_SIZE;
296
- this.fontStyleInternal = TEXT_STYLE_DEFAULT_FONT_STYLE;
297
- this.fontWeightInternal = TEXT_STYLE_DEFAULT_FONT_WEIGHT;
298
- this.lineHeightInternal = TEXT_STYLE_DEFAULT_FONT_SIZE * TEXT_STYLE_DEFAULT_LINE_HEIGHT;
299
- this.enableShadowInternal = TEXT_STYLE_DEFAULT_ENABLE_SHADOW;
300
- this.shadowOffsetXInternal = TEXT_STYLE_DEFAULT_SHADOW_OFFSET_X;
301
- this.shadowOffsetYInternal = TEXT_STYLE_DEFAULT_SHADOW_OFFSET_Y;
302
- this.shadowBlurInternal = TEXT_STYLE_DEFAULT_SHADOW_BLUR;
303
- this.shadowColorInternal = TEXT_STYLE_DEFAULT_SHADOW_COLOR;
304
- this.enableStrokeInternal = TEXT_STYLE_DEFAULT_ENABLE_STROKE;
305
- this.strokeColorInternal = TEXT_STYLE_DEFAULT_STROKE_COLOR;
306
- this.strokeThicknessInternal = TEXT_STYLE_DEFAULT_STROKE_THICKNESS;
307
- this.dirtyInternal = true;
229
+ for (const key of TEXT_STYLE_KEYS) {
230
+ this.setValue(key, TEXT_STYLE_DEFAULTS[key]);
308
231
  }
309
232
  }
310
233
  /** Sets all properties to undefined */
311
234
  clear() {
312
- if (this.colorInternal !== undefined ||
313
- this.alignInternal !== undefined ||
314
- this.fontFamilyInternal !== undefined ||
315
- this.fontSizeInternal !== undefined ||
316
- this.fontStyleInternal !== undefined ||
317
- this.fontWeightInternal !== undefined ||
318
- this.lineHeightInternal !== undefined ||
319
- this.enableShadowInternal !== undefined ||
320
- this.shadowOffsetXInternal !== undefined ||
321
- this.shadowOffsetYInternal !== undefined ||
322
- this.shadowBlurInternal !== undefined ||
323
- this.shadowColorInternal !== undefined ||
324
- this.enableStrokeInternal !== undefined ||
325
- this.strokeColorInternal !== undefined ||
326
- this.strokeThicknessInternal !== undefined) {
327
- this.colorInternal = undefined;
328
- this.alignInternal = undefined;
329
- this.fontFamilyInternal = undefined;
330
- this.fontSizeInternal = undefined;
331
- this.fontStyleInternal = undefined;
332
- this.fontWeightInternal = undefined;
333
- this.lineHeightInternal = undefined;
334
- this.enableShadowInternal = undefined;
335
- this.shadowOffsetXInternal = undefined;
336
- this.shadowOffsetYInternal = undefined;
337
- this.shadowBlurInternal = undefined;
338
- this.shadowColorInternal = undefined;
339
- this.enableStrokeInternal = undefined;
340
- this.strokeColorInternal = undefined;
341
- this.strokeThicknessInternal = undefined;
342
- this.dirtyInternal = true;
235
+ for (const key of TEXT_STYLE_KEYS) {
236
+ this.setValue(key, undefined);
343
237
  }
344
238
  }
345
239
  /** Copies from another style instance */
346
240
  copy(value) {
347
- if (this.colorInternal !== value.colorInternal ||
348
- this.alignInternal !== value.alignInternal ||
349
- this.fontFamilyInternal !== value.fontFamilyInternal ||
350
- this.fontSizeInternal !== value.fontSizeInternal ||
351
- this.fontStyleInternal !== value.fontStyleInternal ||
352
- this.fontWeightInternal !== value.fontWeightInternal ||
353
- this.lineHeightInternal !== value.lineHeightInternal ||
354
- this.enableShadowInternal !== value.enableShadowInternal ||
355
- this.shadowOffsetXInternal !== value.shadowOffsetXInternal ||
356
- this.shadowOffsetYInternal !== value.shadowOffsetYInternal ||
357
- this.shadowBlurInternal !== value.shadowBlurInternal ||
358
- this.shadowColorInternal !== value.shadowColorInternal ||
359
- this.enableStrokeInternal !== value.enableStrokeInternal ||
360
- this.strokeColorInternal !== value.strokeColorInternal ||
361
- this.strokeThicknessInternal !== value.strokeThicknessInternal) {
362
- this.colorInternal = value.colorInternal;
363
- this.alignInternal = value.alignInternal;
364
- this.fontFamilyInternal = value.fontFamilyInternal;
365
- this.fontSizeInternal = value.fontSizeInternal;
366
- this.fontStyleInternal = value.fontStyleInternal;
367
- this.fontWeightInternal = value.fontWeightInternal;
368
- this.lineHeightInternal = value.lineHeightInternal;
369
- this.enableShadowInternal = value.enableShadowInternal;
370
- this.shadowOffsetXInternal = value.shadowOffsetXInternal;
371
- this.shadowOffsetYInternal = value.shadowOffsetYInternal;
372
- this.shadowBlurInternal = value.shadowBlurInternal;
373
- this.shadowColorInternal = value.shadowColorInternal;
374
- this.enableStrokeInternal = value.enableStrokeInternal;
375
- this.strokeColorInternal = value.strokeColorInternal;
376
- this.strokeThicknessInternal = value.strokeThicknessInternal;
377
- this.dirtyInternal = true;
241
+ for (const key of TEXT_STYLE_KEYS) {
242
+ this.setValue(key, value.values[key]);
378
243
  }
379
244
  }
380
245
  /**
381
- * Calculates maximum padding required for stroke and shadow.
246
+ * Calculates the padding needed on every side for outlines and shadows.
382
247
  * Properties left undefined here are taken from `fallback`, then from defaults,
383
- * so a span style is measured with the stroke and shadow it is drawn with.
248
+ * so a span style is measured with the outlines and shadows it is drawn with.
384
249
  * @param fallback Style that supplies undefined properties (usually the common style)
385
- * @returns Padding in pixels needed on all sides
250
+ * @returns Padding in pixels
386
251
  */
387
252
  calculatePadding(fallback) {
388
253
  return UITextStyle.calculatePadding(UITextStyle.resolve(this, fallback));
389
254
  }
255
+ setValue(key, value) {
256
+ if (this.values[key] !== value) {
257
+ this.values[key] = value;
258
+ this.dirtyInternal = true;
259
+ }
260
+ }
390
261
  }
@@ -3,7 +3,8 @@ export declare const TEXTURE_DEFAULT_TEXTURE: Texture;
3
3
  export declare const TEXTURE_DEFAULT_SIZE = 2;
4
4
  /**
5
5
  * Reads the pixel size of a texture image: an image, a video, a canvas, a bitmap
6
- * or the data of a DataTexture.
6
+ * or the data of a DataTexture. An image or video that is not loaded yet reports 0, which is
7
+ * taken as unknown.
7
8
  * @param texture Texture to measure
8
9
  * @returns Width and height in pixels
9
10
  */
@@ -1,16 +1,25 @@
1
1
  import { Texture } from "three";
2
2
  export const TEXTURE_DEFAULT_TEXTURE = new Texture();
3
3
  export const TEXTURE_DEFAULT_SIZE = 2;
4
+ /**
5
+ * First of the sizes that is known, that is, positive.
6
+ * @param sizes Candidate sizes in order of preference
7
+ * @returns The size, or the default size when none is known
8
+ */
9
+ function firstKnownSize(...sizes) {
10
+ return sizes.find((size) => size !== undefined && size > 0) ?? TEXTURE_DEFAULT_SIZE;
11
+ }
4
12
  /**
5
13
  * Reads the pixel size of a texture image: an image, a video, a canvas, a bitmap
6
- * or the data of a DataTexture.
14
+ * or the data of a DataTexture. An image or video that is not loaded yet reports 0, which is
15
+ * taken as unknown.
7
16
  * @param texture Texture to measure
8
17
  * @returns Width and height in pixels
9
18
  */
10
19
  export function getTextureImageSize(texture) {
11
20
  const image = texture.image;
12
21
  return {
13
- w: image?.naturalWidth ?? image?.videoWidth ?? image?.width ?? TEXTURE_DEFAULT_SIZE,
14
- h: image?.naturalHeight ?? image?.videoHeight ?? image?.height ?? TEXTURE_DEFAULT_SIZE,
22
+ w: firstKnownSize(image?.naturalWidth, image?.videoWidth, image?.width),
23
+ h: firstKnownSize(image?.naturalHeight, image?.videoHeight, image?.height),
15
24
  };
16
25
  }
@@ -27,7 +27,7 @@ export interface UIInputCatcherCallbacks {
27
27
  catchPointerCancel: (identifier: number) => void;
28
28
  }
29
29
  export interface UIInputWrapperView {
30
- /** Bumped by every change to the draw order; compare with the last seen value */
30
+ /** Bumped by every change to the draw order, to compare with a value seen before */
31
31
  readonly version: number;
32
32
  createInputCatcher(callbacks: UIInputCatcherCallbacks, zIndex: number): number;
33
33
  destroyInputCatcher(handler: number): void;
@@ -8,7 +8,7 @@ export declare class UIInputWrapper implements UIInputWrapperView {
8
8
  /** Pointers seen moving while not pressed, such as a hovering mouse */
9
9
  private readonly hoveringPointers;
10
10
  private lastHandler;
11
- /** Bumped by every change to the draw order; compare with the last seen value */
11
+ /** Bumped by every change to the draw order, to compare with a value seen before */
12
12
  get version(): number;
13
13
  createInputCatcher(callbacks: UIInputCatcherCallbacks, zIndex: number): number;
14
14
  destroyInputCatcher(handler: number): void;
@@ -9,7 +9,7 @@ export class UIInputWrapper {
9
9
  this.hoveringPointers = new Set();
10
10
  this.lastHandler = 0;
11
11
  }
12
- /** Bumped by every change to the draw order; compare with the last seen value */
12
+ /** Bumped by every change to the draw order, to compare with a value seen before */
13
13
  get version() {
14
14
  return this.versionInternal;
15
15
  }
@@ -1,9 +1,10 @@
1
1
  import type { Constraint, Variable } from "@lume/kiwi";
2
+ import type { UIConstraint } from "../../constraints/UIConstraint.js";
2
3
  import type { UIExpression } from "../../miscellaneous/UIExpression.js";
3
4
  import type { UIPriority } from "../../miscellaneous/UIPriority.js";
4
5
  import type { UIRelation } from "../../miscellaneous/UIRelation.js";
5
6
  export interface UISolverWrapperView {
6
- /** Bumped by every change to the variables or constraints; compare with the last seen value */
7
+ /** Bumped by every change to the variables or constraints, to compare with a value seen before */
7
8
  readonly version: number;
8
9
  createVariable(value: number, priority: UIPriority): number;
9
10
  removeVariable(index: number): void;
@@ -11,7 +12,7 @@ export interface UISolverWrapperView {
11
12
  suggestVariableValue(index: number, value: number): void;
12
13
  setVariablePriority(index: number, priority: UIPriority): void;
13
14
  readVariableValue(index: number): number;
14
- createConstraint(lhs: UIExpression, rhs: UIExpression, relation: UIRelation, priority: UIPriority, enabled: boolean): number;
15
+ createConstraint(lhs: UIExpression, rhs: UIExpression, relation: UIRelation, priority: UIPriority, enabled: boolean, owner: UIConstraint): number;
15
16
  removeConstraint(index: number): void;
16
17
  isConstraintUsingVariable(constraintIndex: number, variableIndex: number): boolean;
17
18
  setConstraintLHS(index: number, lhs: UIExpression): void;
@@ -19,6 +20,7 @@ export interface UISolverWrapperView {
19
20
  setConstraintRelation(index: number, relation: UIRelation): void;
20
21
  setConstraintPriority(index: number, priority: UIPriority): void;
21
22
  setConstraintEnabled(index: number, enabled: boolean): void;
23
+ isConstraintApplied(index: number): boolean;
22
24
  }
23
25
  /**
24
26
  * Internal description of a solver variable with its associated properties.
@@ -49,4 +51,6 @@ export interface ConstraintDescription {
49
51
  relation: UIRelation;
50
52
  /** Whether the constraint is currently active. */
51
53
  enabled: boolean;
54
+ /** Constraint that created it, named when it is rejected */
55
+ owner: UIConstraint;
52
56
  }