@lightningjs/renderer 2.9.0-beta3 → 2.9.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 (217) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/exports/core-api.d.ts +74 -0
  5. package/dist/exports/core-api.js +96 -0
  6. package/dist/exports/core-api.js.map +1 -0
  7. package/dist/exports/main-api.d.ts +30 -0
  8. package/dist/exports/main-api.js +45 -0
  9. package/dist/exports/main-api.js.map +1 -0
  10. package/dist/src/core/CoreExtension.d.ts +12 -0
  11. package/dist/src/core/CoreExtension.js +29 -0
  12. package/dist/src/core/CoreExtension.js.map +1 -0
  13. package/dist/src/core/CoreNode.d.ts +33 -7
  14. package/dist/src/core/CoreNode.js +113 -75
  15. package/dist/src/core/CoreNode.js.map +1 -1
  16. package/dist/src/core/CoreTextNode.d.ts +2 -2
  17. package/dist/src/core/CoreTextNode.js +7 -4
  18. package/dist/src/core/CoreTextNode.js.map +1 -1
  19. package/dist/src/core/CoreTextureManager.d.ts +24 -0
  20. package/dist/src/core/CoreTextureManager.js.map +1 -1
  21. package/dist/src/core/Stage.js +1 -0
  22. package/dist/src/core/Stage.js.map +1 -1
  23. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  24. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  25. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  26. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  27. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  28. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  29. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  30. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  31. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  32. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  33. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  34. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  35. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  36. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  37. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  38. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +12 -12
  39. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  40. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  41. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +6 -0
  42. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  43. package/dist/src/core/textures/Texture.d.ts +1 -1
  44. package/dist/src/core/textures/Texture.js +13 -5
  45. package/dist/src/core/textures/Texture.js.map +1 -1
  46. package/dist/src/main-api/ICoreDriver.d.ts +24 -0
  47. package/dist/src/main-api/ICoreDriver.js +20 -0
  48. package/dist/src/main-api/ICoreDriver.js.map +1 -0
  49. package/dist/src/main-api/RendererMain.d.ts +378 -0
  50. package/dist/src/main-api/RendererMain.js +367 -0
  51. package/dist/src/main-api/RendererMain.js.map +1 -0
  52. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +9 -0
  53. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +38 -0
  54. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +1 -0
  55. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +56 -0
  56. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +101 -0
  57. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +1 -0
  58. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +32 -0
  59. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +28 -0
  60. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +1 -0
  61. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +21 -0
  62. package/dist/src/render-drivers/main/MainCoreDriver.js +115 -0
  63. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -0
  64. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +101 -0
  65. package/dist/src/render-drivers/main/MainOnlyNode.js +425 -0
  66. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -0
  67. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +47 -0
  68. package/dist/src/render-drivers/main/MainOnlyTextNode.js +204 -0
  69. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -0
  70. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +93 -0
  71. package/dist/src/render-drivers/threadx/NodeStruct.js +290 -0
  72. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -0
  73. package/dist/src/render-drivers/threadx/SharedNode.d.ts +40 -0
  74. package/dist/src/render-drivers/threadx/SharedNode.js +61 -0
  75. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -0
  76. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +44 -0
  77. package/dist/src/render-drivers/threadx/TextNodeStruct.js +203 -0
  78. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -0
  79. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +25 -0
  80. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +232 -0
  81. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -0
  82. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +24 -0
  83. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +113 -0
  84. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +1 -0
  85. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +46 -0
  86. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +160 -0
  87. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -0
  88. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +28 -0
  89. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +55 -0
  90. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +1 -0
  91. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +70 -0
  92. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +32 -0
  93. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -0
  94. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +19 -0
  95. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +184 -0
  96. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -0
  97. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +27 -0
  98. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +109 -0
  99. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -0
  100. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +1 -0
  101. package/dist/src/render-drivers/threadx/worker/renderer.js +147 -0
  102. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -0
  103. package/dist/src/render-drivers/utils.d.ts +12 -0
  104. package/dist/src/render-drivers/utils.js +74 -0
  105. package/dist/src/render-drivers/utils.js.map +1 -0
  106. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  107. package/dist/tsconfig.tsbuildinfo +1 -0
  108. package/exports/canvas.ts +39 -39
  109. package/exports/index.ts +89 -89
  110. package/exports/inspector.ts +24 -24
  111. package/exports/utils.ts +44 -44
  112. package/exports/webgl.ts +38 -38
  113. package/package.json +1 -1
  114. package/scripts/please-use-pnpm.js +13 -13
  115. package/src/common/CommonTypes.ts +139 -139
  116. package/src/common/EventEmitter.ts +77 -77
  117. package/src/common/IAnimationController.ts +92 -92
  118. package/src/common/IEventEmitter.ts +28 -28
  119. package/src/core/CoreNode.test.ts +199 -95
  120. package/src/core/CoreNode.ts +2335 -2282
  121. package/src/core/CoreShaderManager.ts +292 -292
  122. package/src/core/CoreTextNode.ts +455 -450
  123. package/src/core/CoreTextureManager.ts +548 -522
  124. package/src/core/Stage.ts +700 -699
  125. package/src/core/TextureMemoryManager.ts +277 -277
  126. package/src/core/animations/AnimationManager.ts +38 -38
  127. package/src/core/animations/CoreAnimation.ts +340 -340
  128. package/src/core/animations/CoreAnimationController.ts +157 -157
  129. package/src/core/lib/ContextSpy.ts +41 -41
  130. package/src/core/lib/ImageWorker.ts +270 -270
  131. package/src/core/lib/Matrix3d.ts +244 -244
  132. package/src/core/lib/RenderCoords.ts +86 -86
  133. package/src/core/lib/WebGlContextWrapper.ts +1322 -1322
  134. package/src/core/lib/textureCompression.ts +152 -152
  135. package/src/core/lib/textureSvg.ts +78 -78
  136. package/src/core/lib/utils.ts +306 -306
  137. package/src/core/platform.ts +61 -61
  138. package/src/core/renderers/CoreContextTexture.ts +43 -43
  139. package/src/core/renderers/CoreRenderOp.ts +22 -22
  140. package/src/core/renderers/CoreRenderer.ts +114 -114
  141. package/src/core/renderers/CoreShader.ts +41 -41
  142. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -364
  143. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -150
  144. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  145. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  146. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  147. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  148. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  149. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -298
  150. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  151. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -805
  152. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  153. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  154. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  155. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  156. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  157. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  158. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  159. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  160. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  161. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  162. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  163. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  164. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  165. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  166. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  167. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  168. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  169. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  170. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  171. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  172. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  173. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  174. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  175. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  176. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  177. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  178. package/src/core/text-rendering/TrFontManager.ts +183 -183
  179. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -169
  180. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  181. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  182. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  183. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  184. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
  185. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  186. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
  187. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  188. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  189. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  190. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  191. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  192. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  193. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  194. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  195. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
  196. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  197. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  198. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  199. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  200. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  201. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  202. package/src/core/textures/ColorTexture.ts +102 -102
  203. package/src/core/textures/ImageTexture.ts +378 -378
  204. package/src/core/textures/NoiseTexture.ts +104 -104
  205. package/src/core/textures/RenderTexture.ts +85 -85
  206. package/src/core/textures/SubTexture.ts +171 -171
  207. package/src/core/textures/Texture.ts +423 -407
  208. package/src/core/utils.ts +227 -227
  209. package/src/env.d.ts +7 -7
  210. package/src/main-api/DynamicShaderController.ts +104 -104
  211. package/src/main-api/INode.ts +101 -101
  212. package/src/main-api/Inspector.ts +505 -505
  213. package/src/main-api/Renderer.ts +693 -693
  214. package/src/main-api/ShaderController.ts +80 -80
  215. package/src/main-api/utils.ts +45 -45
  216. package/src/utils.ts +248 -248
  217. package/COPYING +0 -1
@@ -1,306 +1,306 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- export const PROTOCOL_REGEX = /^(data|ftps?|https?):/;
21
-
22
- export type RGBA = [r: number, g: number, b: number, a: number];
23
-
24
- export const getNormalizedRgbaComponents = (rgba: number): RGBA => {
25
- const r = rgba >>> 24;
26
- const g = (rgba >>> 16) & 0xff;
27
- const b = (rgba >>> 8) & 0xff;
28
- const a = rgba & 0xff;
29
- return [r / 255, g / 255, b / 255, a / 255];
30
- };
31
-
32
- export const getRgbaComponents = (rgba: number): RGBA => {
33
- const r = rgba >>> 24;
34
- const g = (rgba >>> 16) & 0xff;
35
- const b = (rgba >>> 8) & 0xff;
36
- const a = rgba & 0xff;
37
- return [r, g, b, a];
38
- };
39
-
40
- export const norm = (rgba: number): number => {
41
- const r = rgba >>> 24;
42
- const g = (rgba >>> 16) & 0xff;
43
- const b = (rgba >>> 8) & 0xff;
44
- const a = rgba & 0xff;
45
- const rgbaArr: RGBA = [r / 255, g / 255, b / 255, a / 255];
46
-
47
- rgbaArr[0] = Math.max(0, Math.min(255, rgbaArr[0]));
48
- rgbaArr[1] = Math.max(0, Math.min(255, rgbaArr[1]));
49
- rgbaArr[2] = Math.max(0, Math.min(255, rgbaArr[2]));
50
- rgbaArr[3] = Math.max(0, Math.min(255, rgbaArr[3]));
51
- let v =
52
- ((rgbaArr[3] | 0) << 24) +
53
- ((rgbaArr[0] | 0) << 16) +
54
- ((rgbaArr[1] | 0) << 8) +
55
- (rgbaArr[2] | 0);
56
- if (v < 0) {
57
- v = 0xffffffff + v + 1;
58
- }
59
- return v;
60
- };
61
-
62
- export function getNormalizedAlphaComponent(rgba: number): number {
63
- return (rgba & 0xff) / 255.0;
64
- }
65
-
66
- /**
67
- * Get a CSS color string from a RGBA color
68
- *
69
- * @param color
70
- * @returns
71
- */
72
- export function getRgbaString(color: RGBA) {
73
- const r = Math.floor(color[0] * 255.0);
74
- const g = Math.floor(color[1] * 255.0);
75
- const b = Math.floor(color[2] * 255.0);
76
- const a = Math.floor(color[3] * 255.0);
77
- return `rgba(${r},${g},${b},${a.toFixed(4)})`;
78
- }
79
-
80
- export interface Rect {
81
- x: number;
82
- y: number;
83
- width: number;
84
- height: number;
85
- }
86
-
87
- export interface RectWithValid extends Rect {
88
- valid: boolean;
89
- }
90
-
91
- export interface Bound {
92
- x1: number;
93
- y1: number;
94
- x2: number;
95
- y2: number;
96
- }
97
-
98
- export interface BoundWithValid extends Bound {
99
- valid: boolean;
100
- }
101
-
102
- export function createBound<T extends Bound = Bound>(
103
- x1: number,
104
- y1: number,
105
- x2: number,
106
- y2: number,
107
- out?: T,
108
- ): T {
109
- if (out) {
110
- out.x1 = x1;
111
- out.y1 = y1;
112
- out.x2 = x2;
113
- out.y2 = y2;
114
- return out;
115
- }
116
- return {
117
- x1,
118
- y1,
119
- x2,
120
- y2,
121
- } as T;
122
- }
123
-
124
- export function intersectBound<T extends Bound = Bound>(
125
- a: Bound,
126
- b: Bound,
127
- out?: T,
128
- ): T {
129
- const intersection = createBound(
130
- Math.max(a.x1, b.x1),
131
- Math.max(a.y1, b.y1),
132
- Math.min(a.x2, b.x2),
133
- Math.min(a.y2, b.y2),
134
- out,
135
- );
136
- if (intersection.x1 < intersection.x2 && intersection.y1 < intersection.y2) {
137
- return intersection;
138
- }
139
- return createBound(0, 0, 0, 0, intersection);
140
- }
141
-
142
- export function boundsOverlap(a: Bound, b: Bound): boolean {
143
- return a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1;
144
- }
145
-
146
- export function convertBoundToRect(bound: Bound): Rect;
147
- export function convertBoundToRect<T extends Rect = Rect>(
148
- bound: Bound,
149
- out: T,
150
- ): T;
151
- export function convertBoundToRect(bound: Bound, out?: Rect): Rect {
152
- if (out) {
153
- out.x = bound.x1;
154
- out.y = bound.y1;
155
- out.width = bound.x2 - bound.x1;
156
- out.height = bound.y2 - bound.y1;
157
- return out;
158
- }
159
- return {
160
- x: bound.x1,
161
- y: bound.y1,
162
- width: bound.x2 - bound.x1,
163
- height: bound.y2 - bound.y1,
164
- };
165
- }
166
-
167
- export function intersectRect(a: Rect, b: Rect): Rect;
168
- export function intersectRect<T extends Rect = Rect>(
169
- a: Rect,
170
- b: Rect,
171
- out: T,
172
- ): T;
173
- export function intersectRect(a: Rect, b: Rect, out?: Rect): Rect {
174
- const x = Math.max(a.x, b.x);
175
- const y = Math.max(a.y, b.y);
176
- const width = Math.min(a.x + a.width, b.x + b.width) - x;
177
- const height = Math.min(a.y + a.height, b.y + b.height) - y;
178
- if (width > 0 && height > 0) {
179
- if (out) {
180
- out.x = x;
181
- out.y = y;
182
- out.width = width;
183
- out.height = height;
184
- return out;
185
- }
186
- return {
187
- x,
188
- y,
189
- width,
190
- height,
191
- };
192
- }
193
- if (out) {
194
- out.x = 0;
195
- out.y = 0;
196
- out.width = 0;
197
- out.height = 0;
198
- return out;
199
- }
200
- return {
201
- x: 0,
202
- y: 0,
203
- width: 0,
204
- height: 0,
205
- };
206
- }
207
-
208
- export function copyRect(a: Rect): Rect;
209
- export function copyRect<T extends Rect = Rect>(a: Rect, out: T): T;
210
- export function copyRect(a: Rect, out?: Rect): Rect {
211
- if (out) {
212
- out.x = a.x;
213
- out.y = a.y;
214
- out.width = a.width;
215
- out.height = a.height;
216
- return out;
217
- }
218
- return {
219
- x: a.x,
220
- y: a.y,
221
- width: a.width,
222
- height: a.height,
223
- };
224
- }
225
-
226
- export function compareRect(a: Rect | null, b: Rect | null): boolean {
227
- if (a === b) {
228
- return true;
229
- }
230
- if (a === null || b === null) {
231
- return false;
232
- }
233
- return (
234
- a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height
235
- );
236
- }
237
-
238
- export function boundInsideBound(bound1: Bound, bound2: Bound) {
239
- return (
240
- bound1.x1 <= bound2.x2 &&
241
- bound1.y1 <= bound2.y2 &&
242
- bound1.x2 >= bound2.x1 &&
243
- bound1.y2 >= bound2.y1
244
- );
245
- }
246
-
247
- export function boundLargeThanBound(bound1: Bound, bound2: Bound) {
248
- return (
249
- bound1.x1 < bound2.x1 &&
250
- bound1.x2 > bound2.x2 &&
251
- bound1.y1 < bound2.y1 &&
252
- bound1.y2 > bound2.y2
253
- );
254
- }
255
-
256
- export function isBoundPositive(bound: Bound): boolean {
257
- return bound.x1 < bound.x2 && bound.y1 < bound.y2;
258
- }
259
-
260
- export function isRectPositive(rect: Rect): boolean {
261
- return rect.width > 0 && rect.height > 0;
262
- }
263
-
264
- /**
265
- * Create a preload bounds from a strict bound
266
- *
267
- * @param strictBound The strict boundary of the node
268
- * @param boundsMargin Boundary margin to apply to the strictBound
269
- * @returns
270
- */
271
- export function createPreloadBounds(
272
- strictBound: Bound,
273
- boundsMargin: [number, number, number, number],
274
- ): Bound {
275
- return createBound(
276
- strictBound.x1 - boundsMargin[3],
277
- strictBound.y1 - boundsMargin[0],
278
- strictBound.x2 + boundsMargin[1],
279
- strictBound.y2 + boundsMargin[2],
280
- );
281
- }
282
-
283
- export function convertUrlToAbsolute(url: string): string {
284
- // handle local file imports if the url isn't remote resource or data blob
285
- if (self.location.protocol === 'file:' && !PROTOCOL_REGEX.test(url)) {
286
- const path = self.location.pathname.split('/');
287
- path.pop();
288
- const basePath = path.join('/');
289
- const baseUrl = self.location.protocol + '//' + basePath;
290
-
291
- // check if url has a leading dot
292
- if (url.charAt(0) === '.') {
293
- url = url.slice(1);
294
- }
295
-
296
- // check if url has a leading slash
297
- if (url.charAt(0) === '/') {
298
- url = url.slice(1);
299
- }
300
-
301
- return baseUrl + '/' + url;
302
- }
303
-
304
- const absoluteUrl = new URL(url, self.location.href);
305
- return absoluteUrl.href;
306
- }
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
4
+ *
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the License);
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ export const PROTOCOL_REGEX = /^(data|ftps?|https?):/;
21
+
22
+ export type RGBA = [r: number, g: number, b: number, a: number];
23
+
24
+ export const getNormalizedRgbaComponents = (rgba: number): RGBA => {
25
+ const r = rgba >>> 24;
26
+ const g = (rgba >>> 16) & 0xff;
27
+ const b = (rgba >>> 8) & 0xff;
28
+ const a = rgba & 0xff;
29
+ return [r / 255, g / 255, b / 255, a / 255];
30
+ };
31
+
32
+ export const getRgbaComponents = (rgba: number): RGBA => {
33
+ const r = rgba >>> 24;
34
+ const g = (rgba >>> 16) & 0xff;
35
+ const b = (rgba >>> 8) & 0xff;
36
+ const a = rgba & 0xff;
37
+ return [r, g, b, a];
38
+ };
39
+
40
+ export const norm = (rgba: number): number => {
41
+ const r = rgba >>> 24;
42
+ const g = (rgba >>> 16) & 0xff;
43
+ const b = (rgba >>> 8) & 0xff;
44
+ const a = rgba & 0xff;
45
+ const rgbaArr: RGBA = [r / 255, g / 255, b / 255, a / 255];
46
+
47
+ rgbaArr[0] = Math.max(0, Math.min(255, rgbaArr[0]));
48
+ rgbaArr[1] = Math.max(0, Math.min(255, rgbaArr[1]));
49
+ rgbaArr[2] = Math.max(0, Math.min(255, rgbaArr[2]));
50
+ rgbaArr[3] = Math.max(0, Math.min(255, rgbaArr[3]));
51
+ let v =
52
+ ((rgbaArr[3] | 0) << 24) +
53
+ ((rgbaArr[0] | 0) << 16) +
54
+ ((rgbaArr[1] | 0) << 8) +
55
+ (rgbaArr[2] | 0);
56
+ if (v < 0) {
57
+ v = 0xffffffff + v + 1;
58
+ }
59
+ return v;
60
+ };
61
+
62
+ export function getNormalizedAlphaComponent(rgba: number): number {
63
+ return (rgba & 0xff) / 255.0;
64
+ }
65
+
66
+ /**
67
+ * Get a CSS color string from a RGBA color
68
+ *
69
+ * @param color
70
+ * @returns
71
+ */
72
+ export function getRgbaString(color: RGBA) {
73
+ const r = Math.floor(color[0] * 255.0);
74
+ const g = Math.floor(color[1] * 255.0);
75
+ const b = Math.floor(color[2] * 255.0);
76
+ const a = Math.floor(color[3] * 255.0);
77
+ return `rgba(${r},${g},${b},${a.toFixed(4)})`;
78
+ }
79
+
80
+ export interface Rect {
81
+ x: number;
82
+ y: number;
83
+ width: number;
84
+ height: number;
85
+ }
86
+
87
+ export interface RectWithValid extends Rect {
88
+ valid: boolean;
89
+ }
90
+
91
+ export interface Bound {
92
+ x1: number;
93
+ y1: number;
94
+ x2: number;
95
+ y2: number;
96
+ }
97
+
98
+ export interface BoundWithValid extends Bound {
99
+ valid: boolean;
100
+ }
101
+
102
+ export function createBound<T extends Bound = Bound>(
103
+ x1: number,
104
+ y1: number,
105
+ x2: number,
106
+ y2: number,
107
+ out?: T,
108
+ ): T {
109
+ if (out) {
110
+ out.x1 = x1;
111
+ out.y1 = y1;
112
+ out.x2 = x2;
113
+ out.y2 = y2;
114
+ return out;
115
+ }
116
+ return {
117
+ x1,
118
+ y1,
119
+ x2,
120
+ y2,
121
+ } as T;
122
+ }
123
+
124
+ export function intersectBound<T extends Bound = Bound>(
125
+ a: Bound,
126
+ b: Bound,
127
+ out?: T,
128
+ ): T {
129
+ const intersection = createBound(
130
+ Math.max(a.x1, b.x1),
131
+ Math.max(a.y1, b.y1),
132
+ Math.min(a.x2, b.x2),
133
+ Math.min(a.y2, b.y2),
134
+ out,
135
+ );
136
+ if (intersection.x1 < intersection.x2 && intersection.y1 < intersection.y2) {
137
+ return intersection;
138
+ }
139
+ return createBound(0, 0, 0, 0, intersection);
140
+ }
141
+
142
+ export function boundsOverlap(a: Bound, b: Bound): boolean {
143
+ return a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1;
144
+ }
145
+
146
+ export function convertBoundToRect(bound: Bound): Rect;
147
+ export function convertBoundToRect<T extends Rect = Rect>(
148
+ bound: Bound,
149
+ out: T,
150
+ ): T;
151
+ export function convertBoundToRect(bound: Bound, out?: Rect): Rect {
152
+ if (out) {
153
+ out.x = bound.x1;
154
+ out.y = bound.y1;
155
+ out.width = bound.x2 - bound.x1;
156
+ out.height = bound.y2 - bound.y1;
157
+ return out;
158
+ }
159
+ return {
160
+ x: bound.x1,
161
+ y: bound.y1,
162
+ width: bound.x2 - bound.x1,
163
+ height: bound.y2 - bound.y1,
164
+ };
165
+ }
166
+
167
+ export function intersectRect(a: Rect, b: Rect): Rect;
168
+ export function intersectRect<T extends Rect = Rect>(
169
+ a: Rect,
170
+ b: Rect,
171
+ out: T,
172
+ ): T;
173
+ export function intersectRect(a: Rect, b: Rect, out?: Rect): Rect {
174
+ const x = Math.max(a.x, b.x);
175
+ const y = Math.max(a.y, b.y);
176
+ const width = Math.min(a.x + a.width, b.x + b.width) - x;
177
+ const height = Math.min(a.y + a.height, b.y + b.height) - y;
178
+ if (width > 0 && height > 0) {
179
+ if (out) {
180
+ out.x = x;
181
+ out.y = y;
182
+ out.width = width;
183
+ out.height = height;
184
+ return out;
185
+ }
186
+ return {
187
+ x,
188
+ y,
189
+ width,
190
+ height,
191
+ };
192
+ }
193
+ if (out) {
194
+ out.x = 0;
195
+ out.y = 0;
196
+ out.width = 0;
197
+ out.height = 0;
198
+ return out;
199
+ }
200
+ return {
201
+ x: 0,
202
+ y: 0,
203
+ width: 0,
204
+ height: 0,
205
+ };
206
+ }
207
+
208
+ export function copyRect(a: Rect): Rect;
209
+ export function copyRect<T extends Rect = Rect>(a: Rect, out: T): T;
210
+ export function copyRect(a: Rect, out?: Rect): Rect {
211
+ if (out) {
212
+ out.x = a.x;
213
+ out.y = a.y;
214
+ out.width = a.width;
215
+ out.height = a.height;
216
+ return out;
217
+ }
218
+ return {
219
+ x: a.x,
220
+ y: a.y,
221
+ width: a.width,
222
+ height: a.height,
223
+ };
224
+ }
225
+
226
+ export function compareRect(a: Rect | null, b: Rect | null): boolean {
227
+ if (a === b) {
228
+ return true;
229
+ }
230
+ if (a === null || b === null) {
231
+ return false;
232
+ }
233
+ return (
234
+ a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height
235
+ );
236
+ }
237
+
238
+ export function boundInsideBound(bound1: Bound, bound2: Bound) {
239
+ return (
240
+ bound1.x1 <= bound2.x2 &&
241
+ bound1.y1 <= bound2.y2 &&
242
+ bound1.x2 >= bound2.x1 &&
243
+ bound1.y2 >= bound2.y1
244
+ );
245
+ }
246
+
247
+ export function boundLargeThanBound(bound1: Bound, bound2: Bound) {
248
+ return (
249
+ bound1.x1 < bound2.x1 &&
250
+ bound1.x2 > bound2.x2 &&
251
+ bound1.y1 < bound2.y1 &&
252
+ bound1.y2 > bound2.y2
253
+ );
254
+ }
255
+
256
+ export function isBoundPositive(bound: Bound): boolean {
257
+ return bound.x1 < bound.x2 && bound.y1 < bound.y2;
258
+ }
259
+
260
+ export function isRectPositive(rect: Rect): boolean {
261
+ return rect.width > 0 && rect.height > 0;
262
+ }
263
+
264
+ /**
265
+ * Create a preload bounds from a strict bound
266
+ *
267
+ * @param strictBound The strict boundary of the node
268
+ * @param boundsMargin Boundary margin to apply to the strictBound
269
+ * @returns
270
+ */
271
+ export function createPreloadBounds(
272
+ strictBound: Bound,
273
+ boundsMargin: [number, number, number, number],
274
+ ): Bound {
275
+ return createBound(
276
+ strictBound.x1 - boundsMargin[3],
277
+ strictBound.y1 - boundsMargin[0],
278
+ strictBound.x2 + boundsMargin[1],
279
+ strictBound.y2 + boundsMargin[2],
280
+ );
281
+ }
282
+
283
+ export function convertUrlToAbsolute(url: string): string {
284
+ // handle local file imports if the url isn't remote resource or data blob
285
+ if (self.location.protocol === 'file:' && !PROTOCOL_REGEX.test(url)) {
286
+ const path = self.location.pathname.split('/');
287
+ path.pop();
288
+ const basePath = path.join('/');
289
+ const baseUrl = self.location.protocol + '//' + basePath;
290
+
291
+ // check if url has a leading dot
292
+ if (url.charAt(0) === '.') {
293
+ url = url.slice(1);
294
+ }
295
+
296
+ // check if url has a leading slash
297
+ if (url.charAt(0) === '/') {
298
+ url = url.slice(1);
299
+ }
300
+
301
+ return baseUrl + '/' + url;
302
+ }
303
+
304
+ const absoluteUrl = new URL(url, self.location.href);
305
+ return absoluteUrl.href;
306
+ }