ultra-text 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +339 -1039
- package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
- package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
- package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
- package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
- package/dist/atlas/appendAtlas.js +97 -31
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +4 -3
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/colorGlyphs.d.ts +0 -10
- package/dist/atlas/colorGlyphs.js +97 -155
- package/dist/atlas/colorGlyphs.js.map +1 -1
- package/dist/atlas/colorTexture.d.ts +1 -0
- package/dist/atlas/colorTexture.js +64 -0
- package/dist/atlas/colorTexture.js.map +1 -0
- package/dist/atlas/cpuAtlas.d.ts +4 -1
- package/dist/atlas/cpuAtlas.js +14 -7
- package/dist/atlas/cpuAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.d.ts +1 -1
- package/dist/atlas/cpuSdf.js +235 -68
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.d.ts +11 -3
- package/dist/atlas/cpuWorker.js +179 -22
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.js +188 -287
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/gpuCommon.d.ts +257 -0
- package/dist/atlas/gpuCommon.js +845 -0
- package/dist/atlas/gpuCommon.js.map +1 -0
- package/dist/atlas/jfaWgsl.d.ts +1 -1
- package/dist/atlas/jfaWgsl.js +226 -249
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +22 -0
- package/dist/atlas/rasterize.js +329 -158
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/atlas/shapeRaster.js +2 -1
- package/dist/atlas/shapeRaster.js.map +1 -1
- package/dist/atlas/vanillaBounds.d.ts +64 -0
- package/dist/atlas/vanillaBounds.js +42 -0
- package/dist/atlas/vanillaBounds.js.map +1 -0
- package/dist/atlas/vanillaCore.d.ts +100 -0
- package/dist/atlas/vanillaCore.js +1065 -0
- package/dist/atlas/vanillaCore.js.map +1 -0
- package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
- package/dist/atlas/vanillaCpuAtlas.js +72 -0
- package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
- package/dist/atlas/vanillaExtras.d.ts +8 -0
- package/dist/atlas/vanillaExtras.js +167 -0
- package/dist/atlas/vanillaExtras.js.map +1 -0
- package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
- package/dist/atlas/vanillaGpuHelpers.js +354 -0
- package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
- package/dist/dom/DomSdf.js +243 -297
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.js +94 -165
- package/dist/dom/DomSlug.js.map +1 -1
- package/dist/dom/Text.d.ts +4 -1
- package/dist/dom/Text.js +5 -1
- package/dist/dom/Text.js.map +1 -1
- package/dist/dom/browserShaping.js +31 -14
- package/dist/dom/browserShaping.js.map +1 -1
- package/dist/dom/clusters.js +13 -2
- package/dist/dom/clusters.js.map +1 -1
- package/dist/dom/cssEffects.d.ts +4 -0
- package/dist/dom/cssEffects.js +72 -0
- package/dist/dom/cssEffects.js.map +1 -0
- package/dist/dom/domAnalytic.d.ts +53 -0
- package/dist/dom/domAnalytic.js +282 -0
- package/dist/dom/domAnalytic.js.map +1 -0
- package/dist/dom/domExtras.d.ts +27 -0
- package/dist/dom/domExtras.js +137 -0
- package/dist/dom/domExtras.js.map +1 -0
- package/dist/dom/fontCanvas.d.ts +16 -0
- package/dist/dom/fontCanvas.js +31 -13
- package/dist/dom/fontCanvas.js.map +1 -1
- package/dist/dom/layoutSignature.js +175 -57
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/markup.d.ts +10 -0
- package/dist/dom/markup.js +16 -1
- package/dist/dom/markup.js.map +1 -1
- package/dist/dom/pretextLayout.js +78 -28
- package/dist/dom/pretextLayout.js.map +1 -1
- package/dist/dom/snapshot.d.ts +4 -0
- package/dist/dom/snapshot.js +891 -484
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/snapshotsEqual.d.ts +7 -0
- package/dist/dom/snapshotsEqual.js +68 -0
- package/dist/dom/snapshotsEqual.js.map +1 -0
- package/dist/dom/svgCapture.d.ts +8 -1
- package/dist/dom/svgCapture.js +105 -28
- package/dist/dom/svgCapture.js.map +1 -1
- package/dist/dom/textTransform.d.ts +13 -1
- package/dist/dom/textTransform.js +31 -13
- package/dist/dom/textTransform.js.map +1 -1
- package/dist/dom/tone.js +91 -44
- package/dist/dom/tone.js.map +1 -1
- package/dist/index.js +4 -3
- package/dist/slug/SlugMaterial.d.ts +17 -3
- package/dist/slug/SlugMaterial.js +98 -26
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/cff.d.ts +5 -0
- package/dist/slug/cff.js +780 -0
- package/dist/slug/cff.js.map +1 -0
- package/dist/slug/coverage.d.ts +19 -15
- package/dist/slug/coverage.js +380 -225
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +6 -2
- package/dist/slug/preprocessCore.js +199 -148
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.d.ts +1 -0
- package/dist/slug/runtime.js +113 -74
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +3 -2
- package/dist/slug/standaloneRuntime.js +112 -67
- package/dist/slug/standaloneRuntime.js.map +1 -1
- package/dist/slug/truetype.d.ts +14 -6
- package/dist/slug/truetype.js +356 -255
- package/dist/slug/truetype.js.map +1 -1
- package/dist/slug/vanillaCore.d.ts +410 -0
- package/dist/slug/vanillaCore.js +1630 -0
- package/dist/slug/vanillaCore.js.map +1 -0
- package/dist/slug-vanilla.d.ts +13 -364
- package/dist/slug-vanilla.js +7 -1454
- package/dist/slug.d.ts +2 -2
- package/dist/slug.js +7 -7
- package/dist/text/GlyphTextMaterial.d.ts +6 -1
- package/dist/text/GlyphTextMaterial.js +16 -12
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/RectMaterial.js +6 -10
- package/dist/text/RectMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.d.ts +9 -3
- package/dist/text/SdfTextMaterial.js +125 -38
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/text/decorations.d.ts +7 -2
- package/dist/text/decorations.js +187 -79
- package/dist/text/decorations.js.map +1 -1
- package/dist/text/geometry.d.ts +3 -22
- package/dist/text/geometryUtils.d.ts +16 -1
- package/dist/text/geometryUtils.js +83 -8
- package/dist/text/geometryUtils.js.map +1 -1
- package/dist/text/glyphGeometry.d.ts +9 -0
- package/dist/text/glyphGeometry.js +434 -0
- package/dist/text/glyphGeometry.js.map +1 -0
- package/dist/text/index.d.ts +2 -1
- package/dist/text/rectGeometry.d.ts +13 -0
- package/dist/text/rectGeometry.js +101 -0
- package/dist/text/rectGeometry.js.map +1 -0
- package/dist/text/reflow.d.ts +23 -1
- package/dist/text/reflow.js +223 -94
- package/dist/text/reflow.js.map +1 -1
- package/dist/text/snap.d.ts +15 -0
- package/dist/text/snap.js +26 -0
- package/dist/text/snap.js.map +1 -0
- package/dist/text/tone.js +2 -2
- package/dist/text/tone.js.map +1 -1
- package/dist/types.d.ts +23 -7
- package/dist/vanilla.d.ts +13 -363
- package/dist/vanilla.js +7 -1474
- package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
- package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
- package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
- package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
- package/dist/windfoil/WindfoilMaterial.js +71 -8
- package/dist/windfoil/WindfoilMaterial.js.map +1 -1
- package/dist/windfoil/compositeGeometry.js +12 -5
- package/dist/windfoil/compositeGeometry.js.map +1 -1
- package/dist/windfoil/coverage.d.ts +10 -2
- package/dist/windfoil/coverage.js +147 -59
- package/dist/windfoil/coverage.js.map +1 -1
- package/dist/windfoil/geometry.d.ts +17 -2
- package/dist/windfoil/geometry.js +220 -57
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/layerGrid.js +8 -8
- package/dist/windfoil/layerGrid.js.map +1 -1
- package/dist/windfoil/mixed.d.ts +1 -0
- package/dist/windfoil/mixed.js +57 -29
- package/dist/windfoil/mixed.js.map +1 -1
- package/dist/windfoil/preprocess.js +2 -2
- package/dist/windfoil/preprocessCore.d.ts +2 -0
- package/dist/windfoil/preprocessCore.js +69 -37
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/requests.d.ts +1 -1
- package/dist/windfoil/requests.js +11 -4
- package/dist/windfoil/requests.js.map +1 -1
- package/dist/windfoil/resolveOutline.d.ts +9 -0
- package/dist/windfoil/resolveOutline.js +20 -0
- package/dist/windfoil/resolveOutline.js.map +1 -0
- package/dist/windfoil/runtime.d.ts +4 -1
- package/dist/windfoil/runtime.js +69 -63
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/shapeGeometry.js +80 -34
- package/dist/windfoil/shapeGeometry.js.map +1 -1
- package/dist/windfoil/sizeSwitch.d.ts +1 -1
- package/dist/windfoil/sizeSwitch.js +13 -8
- package/dist/windfoil/sizeSwitch.js.map +1 -1
- package/dist/windfoil/storage.d.ts +78 -6
- package/dist/windfoil/storage.js +166 -11
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/svg/curves.d.ts +20 -0
- package/dist/windfoil/svg/curves.js +145 -42
- package/dist/windfoil/svg/curves.js.map +1 -1
- package/dist/windfoil/svg/document.js +10 -1
- package/dist/windfoil/svg/document.js.map +1 -1
- package/dist/windfoil/svg/pathConverter.js +66 -50
- package/dist/windfoil/svg/pathConverter.js.map +1 -1
- package/dist/windfoil/svg/pathData.d.ts +9 -0
- package/dist/windfoil/svg/pathData.js +142 -109
- package/dist/windfoil/svg/pathData.js.map +1 -1
- package/dist/windfoil/svg/winding.js +34 -10
- package/dist/windfoil/svg/winding.js.map +1 -1
- package/dist/windfoil/trace/contours.d.ts +5 -0
- package/dist/windfoil/trace/contours.js +256 -62
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +3 -1
- package/dist/windfoil/trace/fit.js +915 -265
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +55 -30
- package/dist/windfoil/trace/outline.js.map +1 -1
- package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
- package/dist/windfoil/trace/traceWorker.d.ts +13 -0
- package/dist/windfoil/trace/traceWorker.js +192 -0
- package/dist/windfoil/trace/traceWorker.js.map +1 -0
- package/dist/windfoil/trace/types.d.ts +10 -0
- package/dist/windfoil/tracedOutlines.d.ts +2 -0
- package/dist/windfoil/tracedOutlines.js +393 -103
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +48 -29
- package/skills/ultra-text/SKILL.md +9 -10
- package/skills/ultra-text/references/frameworks.md +46 -2
- package/skills/ultra-text/references/three-tsl.md +27 -25
- package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
- package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
- package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
- package/dist/slug-vanilla.js.map +0 -1
- package/dist/text/geometry.js +0 -348
- package/dist/text/geometry.js.map +0 -1
- package/dist/vanilla.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildAtlas.js","names":[],"sources":["../../src/atlas/buildAtlas.ts"],"sourcesContent":["import type { WebGPURenderer } from 'three/webgpu'\n\nimport type { BuildSdfAtlasOptions, RasterAtlas, SdfAtlas } from '../types.js'\nimport { buildGpuSdfAtlas } from './gpuAtlas.js'\n\nfunction retainAtlas(atlas: SdfAtlas, signal?: AbortSignal): SdfAtlas {\n if (signal?.aborted) {\n atlas.dispose()\n signal.throwIfAborted()\n }\n return atlas\n}\n\nasync function withColorLayer(\n atlas: SdfAtlas,\n raster: RasterAtlas,\n renderer: WebGPURenderer,\n options: BuildSdfAtlasOptions,\n): Promise<SdfAtlas> {\n if (!raster.colorBand) return atlas\n let colorLayer: SdfAtlas['colorLayer']\n try {\n const { createColorLayer } = await import('./
|
|
1
|
+
{"version":3,"file":"buildAtlas.js","names":[],"sources":["../../src/atlas/buildAtlas.ts"],"sourcesContent":["import type { WebGPURenderer } from 'three/webgpu'\n\nimport type { BuildSdfAtlasOptions, RasterAtlas, SdfAtlas } from '../types.js'\nimport type { PreparedCpuWorker } from './cpuWorker.js'\nimport { buildGpuSdfAtlas } from './gpuAtlas.js'\n\nfunction retainAtlas(atlas: SdfAtlas, signal?: AbortSignal): SdfAtlas {\n if (signal?.aborted) {\n atlas.dispose()\n signal.throwIfAborted()\n }\n return atlas\n}\n\nasync function withColorLayer(\n atlas: SdfAtlas,\n raster: RasterAtlas,\n renderer: WebGPURenderer,\n options: BuildSdfAtlasOptions,\n): Promise<SdfAtlas> {\n if (!raster.colorBand) return atlas\n let colorLayer: SdfAtlas['colorLayer']\n try {\n const { createColorLayer } = await import('./colorTexture.js')\n options.signal?.throwIfAborted()\n colorLayer = createColorLayer(\n raster.colorBand,\n raster.width,\n options,\n renderer.getMaxAnisotropy(),\n )\n options.signal?.throwIfAborted()\n let disposed = false\n return {\n ...atlas,\n colorLayer,\n dispose() {\n if (disposed) return\n disposed = true\n try {\n atlas.dispose()\n } finally {\n colorLayer!.texture.dispose()\n }\n },\n }\n } catch (error) {\n try {\n atlas.dispose()\n } finally {\n colorLayer?.texture.dispose()\n }\n throw error\n }\n}\n\nlet warnedGpuFallback = false\n\n/** @internal Hook for overlapping GPU SDF validation with downstream geometry creation. */\nexport interface ProvisionalAtlasHook {\n stage(atlas: SdfAtlas): void\n rollback(): void\n}\n\n/**\n * Builds the retained single-channel SDF atlas from a packed glyph mask.\n *\n * `mode: 'gpu'` runs the WebGPU compute encoder and rethrows if a real WebGPU\n * backend is unavailable, if the device's limits cannot hold the passes, or if\n * the device reports an error for them; `'cpu'` runs the exact Euclidean\n * distance transform; the default `'auto'` tries the GPU path first and falls\n * back to the CPU encoder, warning once per page because the fallback is\n * 10-100× slower.\n * An aborted `signal` rejects with the abort reason and disposes any atlas\n * that was already produced. The caller owns the returned atlas.\n */\nexport function buildSdfAtlas(\n renderer: WebGPURenderer,\n raster: RasterAtlas,\n options?: BuildSdfAtlasOptions,\n): Promise<SdfAtlas>\n/** @internal */\nexport function buildSdfAtlas(\n renderer: WebGPURenderer,\n raster: RasterAtlas,\n options: BuildSdfAtlasOptions | undefined,\n provisional: ProvisionalAtlasHook | undefined,\n preparedWorker?: PreparedCpuWorker | Promise<PreparedCpuWorker | null> | null,\n): Promise<SdfAtlas>\nexport async function buildSdfAtlas(\n renderer: WebGPURenderer,\n raster: RasterAtlas,\n options: BuildSdfAtlasOptions = {},\n provisional?: ProvisionalAtlasHook,\n preparedWorker?: PreparedCpuWorker | Promise<PreparedCpuWorker | null> | null,\n): Promise<SdfAtlas> {\n const mode = options.mode\n options.signal?.throwIfAborted()\n\n const buildCpu = async (): Promise<SdfAtlas> => {\n const { buildCpuSdfAtlas } = await import('./cpuAtlas.js')\n const worker = preparedWorker ? await preparedWorker : undefined\n options.signal?.throwIfAborted()\n return retainAtlas(\n await (worker\n ? buildCpuSdfAtlas(raster, options, worker)\n : buildCpuSdfAtlas(raster, options)),\n options.signal,\n )\n }\n let atlas: SdfAtlas\n if (mode === 'cpu') atlas = await buildCpu()\n else {\n try {\n const built =\n provisional && !raster.colorBand && !options.mipmaps\n ? await buildGpuSdfAtlas(renderer, raster, options, (candidate) =>\n provisional.stage(candidate),\n )\n : await buildGpuSdfAtlas(renderer, raster, options)\n atlas = retainAtlas(built, options.signal)\n } catch (error) {\n provisional?.rollback()\n if (mode === 'gpu') throw error\n options.signal?.throwIfAborted()\n if (!warnedGpuFallback) {\n warnedGpuFallback = true\n console.warn('ultra-text: falling back to CPU SDF', error)\n }\n atlas = await buildCpu()\n }\n }\n // `SdfAtlas.shapeTop` names the rows of the monochrome shapes. The texture repeats it for\n // a material that is handed the texture without that field.\n if (atlas.shapeTop !== undefined) atlas.texture.userData['ultraShapeTop'] = atlas.shapeTop\n return withColorLayer(atlas, raster, renderer, options)\n}\n"],"mappings":";;AAMA,SAAS,YAAY,OAAiB,QAAgC;CACpE,IAAI,QAAQ,SAAS;EACnB,MAAM,QAAQ;EACd,OAAO,eAAe;CACxB;CACA,OAAO;AACT;AAEA,eAAe,eACb,OACA,QACA,UACA,SACmB;CACnB,IAAI,CAAC,OAAO,WAAW,OAAO;CAC9B,IAAI;CACJ,IAAI;EACF,MAAM,EAAE,qBAAqB,MAAM,OAAO;EAC1C,QAAQ,QAAQ,eAAe;EAC/B,aAAa,iBACX,OAAO,WACP,OAAO,OACP,SACA,SAAS,iBAAiB,CAC5B;EACA,QAAQ,QAAQ,eAAe;EAC/B,IAAI,WAAW;EACf,OAAO;GACL,GAAG;GACH;GACA,UAAU;IACR,IAAI,UAAU;IACd,WAAW;IACX,IAAI;KACF,MAAM,QAAQ;IAChB,UAAU;KACR,WAAY,QAAQ,QAAQ;IAC9B;GACF;EACF;CACF,SAAS,OAAO;EACd,IAAI;GACF,MAAM,QAAQ;EAChB,UAAU;GACR,YAAY,QAAQ,QAAQ;EAC9B;EACA,MAAM;CACR;AACF;AAEA,IAAI,oBAAoB;AAiCxB,eAAsB,cACpB,UACA,QACA,UAAgC,CAAC,GACjC,aACA,gBACmB;CACnB,MAAM,OAAO,QAAQ;CACrB,QAAQ,QAAQ,eAAe;CAE/B,MAAM,WAAW,YAA+B;EAC9C,MAAM,EAAE,qBAAqB,MAAM,OAAO;EAC1C,MAAM,SAAS,iBAAiB,MAAM,iBAAiB,KAAA;EACvD,QAAQ,QAAQ,eAAe;EAC/B,OAAO,YACL,OAAO,SACH,iBAAiB,QAAQ,SAAS,MAAM,IACxC,iBAAiB,QAAQ,OAAO,IACpC,QAAQ,MACV;CACF;CACA,IAAI;CACJ,IAAI,SAAS,OAAO,QAAQ,MAAM,SAAS;MAEzC,IAAI;EAOF,QAAQ,YALN,eAAe,CAAC,OAAO,aAAa,CAAC,QAAQ,UACzC,MAAM,iBAAiB,UAAU,QAAQ,UAAU,cACjD,YAAY,MAAM,SAAS,CAC7B,IACA,MAAM,iBAAiB,UAAU,QAAQ,OAAO,GAC3B,QAAQ,MAAM;CAC3C,SAAS,OAAO;EACd,aAAa,SAAS;EACtB,IAAI,SAAS,OAAO,MAAM;EAC1B,QAAQ,QAAQ,eAAe;EAC/B,IAAI,CAAC,mBAAmB;GACtB,oBAAoB;GACpB,QAAQ,KAAK,uCAAuC,KAAK;EAC3D;EACA,QAAQ,MAAM,SAAS;CACzB;CAIF,IAAI,MAAM,aAAa,KAAA,GAAW,MAAM,QAAQ,SAAS,mBAAmB,MAAM;CAClF,OAAO,eAAe,OAAO,QAAQ,UAAU,OAAO;AACxD"}
|
|
@@ -1,11 +1 @@
|
|
|
1
|
-
interface ColorSlot {
|
|
2
|
-
readonly x: number;
|
|
3
|
-
readonly y: number;
|
|
4
|
-
readonly width: number;
|
|
5
|
-
readonly height: number;
|
|
6
|
-
}
|
|
7
|
-
export declare function encodeColorBand(white: Uint8ClampedArray, black: Uint8ClampedArray, width: number, height: number, pxRange: number, slots: readonly ColorSlot[]): {
|
|
8
|
-
data: Uint8Array;
|
|
9
|
-
colored: boolean[];
|
|
10
|
-
};
|
|
11
1
|
export {};
|
|
@@ -1,40 +1,101 @@
|
|
|
1
|
-
import { DataTexture, LinearFilter, LinearMipmapLinearFilter, RGBAFormat, SRGBColorSpace, UnsignedByteType } from "three/webgpu";
|
|
2
1
|
// src/atlas/colorGlyphs.ts
|
|
2
|
+
/** @internal Probe a glyph without constructing a colour band. */
|
|
3
|
+
function slotHasColor(white, black, width, slot) {
|
|
4
|
+
const rowBytes = width * 4;
|
|
5
|
+
const slotBytes = slot.width * 4;
|
|
6
|
+
const start = (slot.y * width + slot.x) * 4;
|
|
7
|
+
const end = start + slot.height * rowBytes;
|
|
8
|
+
for (let row = start; row < end; row += rowBytes) for (let at = row; at < row + slotBytes; at += 4) {
|
|
9
|
+
if (white[at + 3] < 128) continue;
|
|
10
|
+
if (white[at] - black[at] < 128 || white[at + 1] - black[at + 1] < 128 || white[at + 2] - black[at + 2] < 128) return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
var scratchDistances = /* @__PURE__ */ new Uint16Array(0);
|
|
3
15
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* alpha of the band is then 1 around colour and 0 around text that shares a slot with it,
|
|
7
|
-
* whatever the distance. A `pxRange` of 0 only classifies.
|
|
16
|
+
* The nearest Manhattan-distance seed, with ties going to the first seed in raster order.
|
|
17
|
+
* First find the nearest seed in each row (left wins ties), then compare rows (top wins ties).
|
|
8
18
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
19
|
+
function spreadColors(words, white, width, firstInk, lastInk) {
|
|
20
|
+
const top = Math.floor(firstInk / width) * width;
|
|
21
|
+
const bottom = (Math.floor(lastInk / width) + 1) * width;
|
|
22
|
+
const count = bottom - top;
|
|
23
|
+
const far = width + count / width;
|
|
24
|
+
let distances;
|
|
25
|
+
if (far > 65535) distances = new Float64Array(count);
|
|
26
|
+
else if (count > 65536) distances = new Uint16Array(count);
|
|
27
|
+
else {
|
|
28
|
+
if (scratchDistances.length < count) scratchDistances = new Uint16Array(count);
|
|
29
|
+
distances = scratchDistances;
|
|
30
|
+
}
|
|
31
|
+
const fillSpan = (start, end, horizontal, step, word) => {
|
|
32
|
+
for (let pixel = start; pixel < end; pixel++, horizontal += step) {
|
|
33
|
+
const i = pixel - top;
|
|
34
|
+
const above = i >= width ? distances[i - width] + 1 : far;
|
|
35
|
+
if (above <= horizontal) {
|
|
36
|
+
distances[i] = above;
|
|
37
|
+
words[pixel] = words[pixel - width];
|
|
38
|
+
} else {
|
|
39
|
+
distances[i] = horizontal;
|
|
40
|
+
words[pixel] = word;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
for (let row = top; row < bottom; row += width) {
|
|
45
|
+
const end = row + width;
|
|
46
|
+
let left = row - 1;
|
|
47
|
+
let leftWord = 0;
|
|
48
|
+
for (let pixel = row; pixel < end; pixel++) {
|
|
49
|
+
if (white[pixel * 4 + 3] < 128) continue;
|
|
50
|
+
const word = words[pixel];
|
|
51
|
+
if (pixel > left + 1) {
|
|
52
|
+
const middle = left < row ? left : left + Math.floor((pixel - left) / 2);
|
|
53
|
+
fillSpan(left + 1, middle + 1, 1, 1, leftWord);
|
|
54
|
+
fillSpan(middle + 1, pixel, pixel - middle - 1, -1, word);
|
|
55
|
+
}
|
|
56
|
+
distances[pixel - top] = 0;
|
|
57
|
+
left = pixel;
|
|
58
|
+
leftWord = word;
|
|
59
|
+
}
|
|
60
|
+
if (left < row) {
|
|
61
|
+
words.copyWithin(row, row - width, row);
|
|
62
|
+
for (let i = row - top; i < end - top; i++) distances[i] = distances[i - width] + 1;
|
|
63
|
+
} else fillSpan(left + 1, end, 1, 1, leftWord);
|
|
64
|
+
}
|
|
65
|
+
for (let pixel = bottom - width - 1; pixel >= top; pixel--) {
|
|
66
|
+
const i = pixel - top;
|
|
67
|
+
const below = distances[i + width] + 1;
|
|
68
|
+
if (below < distances[i]) {
|
|
69
|
+
distances[i] = below;
|
|
70
|
+
words[pixel] = words[pixel + width];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (let row = 0; row < top; row += width) words.copyWithin(row, top, top + width);
|
|
74
|
+
for (let row = bottom; row < words.length; row += width) words.copyWithin(row, bottom - width, bottom);
|
|
75
|
+
}
|
|
76
|
+
/** @internal Classify fill-invariant ink and extend its colour (or foreground transparency). */
|
|
12
77
|
function encodeColorBand(white, black, width, height, pxRange, slots) {
|
|
13
78
|
const count = width * height;
|
|
14
79
|
const data = new Uint8Array(count * 4);
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
80
|
+
const words = new Uint32Array(data.buffer);
|
|
81
|
+
const light = new DataView(white.buffer, white.byteOffset, white.byteLength);
|
|
82
|
+
const dark = new DataView(black.buffer, black.byteOffset, black.byteLength);
|
|
83
|
+
const result = new DataView(data.buffer);
|
|
84
|
+
let first = 0;
|
|
85
|
+
let last = count - 1;
|
|
86
|
+
while (first <= last && white[first * 4 + 3] < 128) first++;
|
|
87
|
+
while (last >= first && white[last * 4 + 3] < 128) last--;
|
|
88
|
+
let inkCount = 0;
|
|
19
89
|
let hasColor = false;
|
|
20
|
-
let
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (white[at] - b0 >= 128 && white[at + 1] - b1 >= 128 && white[at + 2] - b2 >= 128) {
|
|
30
|
-
state[index] = 255;
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
data[at] = b0;
|
|
34
|
-
data[at + 1] = b1;
|
|
35
|
-
data[at + 2] = b2;
|
|
36
|
-
data[at + 3] = 255;
|
|
37
|
-
state[index] = 1;
|
|
90
|
+
for (let index = first; index <= last; index++) {
|
|
91
|
+
const at = index * 4;
|
|
92
|
+
const w = light.getInt32(at, true);
|
|
93
|
+
if (w >= 0) continue;
|
|
94
|
+
inkCount++;
|
|
95
|
+
const b = dark.getInt32(at, true);
|
|
96
|
+
const highBits = 8421504;
|
|
97
|
+
if ((w & ~b & highBits) === highBits && (w - b & highBits) === highBits) continue;
|
|
98
|
+
result.setInt32(at, b | 4278190080, true);
|
|
38
99
|
hasColor = true;
|
|
39
100
|
}
|
|
40
101
|
const colored = slots.map((slot) => {
|
|
@@ -43,139 +104,20 @@ function encodeColorBand(white, black, width, height, pxRange, slots) {
|
|
|
43
104
|
const yEnd = slot.y + slot.height;
|
|
44
105
|
for (let y = slot.y; y < yEnd; y++) {
|
|
45
106
|
const row = y * width;
|
|
46
|
-
for (let x = slot.x; x < xEnd; x++) if (
|
|
107
|
+
for (let x = slot.x; x < xEnd; x++) if (words[row + x]) return true;
|
|
47
108
|
}
|
|
48
109
|
return false;
|
|
49
110
|
});
|
|
50
|
-
if (pxRange <= 0 || !hasColor) {
|
|
51
|
-
if (scratchState.length > 65536) scratchState = /* @__PURE__ */ new Uint8Array(0);
|
|
52
|
-
return {
|
|
53
|
-
data,
|
|
54
|
-
colored
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
if (scratchQueue.length < count) {
|
|
58
|
-
scratchQueue = new Uint32Array(count);
|
|
59
|
-
scratchQueueX = new Int32Array(count);
|
|
60
|
-
}
|
|
61
|
-
const queue = scratchQueue;
|
|
62
|
-
const queueX = scratchQueueX;
|
|
63
|
-
let tail = 0;
|
|
64
|
-
const minInkY = firstInkIndex / width | 0;
|
|
65
|
-
const maxInkY = lastInkIndex / width | 0;
|
|
66
|
-
for (let y = minInkY; y <= maxInkY; y++) {
|
|
67
|
-
const row = y * width;
|
|
68
|
-
for (let x = 0; x < width; x++) {
|
|
69
|
-
const index = row + x;
|
|
70
|
-
if (state[index] === 0) continue;
|
|
71
|
-
if (x > 0 && state[index - 1] === 0 || x + 1 < width && state[index + 1] === 0 || y > 0 && state[index - width] === 0 || y + 1 < height && state[index + width] === 0) {
|
|
72
|
-
queueX[tail] = x;
|
|
73
|
-
queue[tail++] = index;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
for (let head = 0; head < tail; head++) {
|
|
78
|
-
const index = queue[head];
|
|
79
|
-
const x = queueX[head];
|
|
80
|
-
if (state[index] === 1) {
|
|
81
|
-
const colorWord = packed[index];
|
|
82
|
-
if (x > 0) {
|
|
83
|
-
const next = index - 1;
|
|
84
|
-
if (state[next] === 0) {
|
|
85
|
-
state[next] = 1;
|
|
86
|
-
packed[next] = colorWord;
|
|
87
|
-
queueX[tail] = x - 1;
|
|
88
|
-
queue[tail++] = next;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (x + 1 < width) {
|
|
92
|
-
const next = index + 1;
|
|
93
|
-
if (state[next] === 0) {
|
|
94
|
-
state[next] = 1;
|
|
95
|
-
packed[next] = colorWord;
|
|
96
|
-
queueX[tail] = x + 1;
|
|
97
|
-
queue[tail++] = next;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (index >= width) {
|
|
101
|
-
const next = index - width;
|
|
102
|
-
if (state[next] === 0) {
|
|
103
|
-
state[next] = 1;
|
|
104
|
-
packed[next] = colorWord;
|
|
105
|
-
queueX[tail] = x;
|
|
106
|
-
queue[tail++] = next;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (index + width < count) {
|
|
110
|
-
const next = index + width;
|
|
111
|
-
if (state[next] === 0) {
|
|
112
|
-
state[next] = 1;
|
|
113
|
-
packed[next] = colorWord;
|
|
114
|
-
queueX[tail] = x;
|
|
115
|
-
queue[tail++] = next;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
} else {
|
|
119
|
-
if (x > 0) {
|
|
120
|
-
const next = index - 1;
|
|
121
|
-
if (state[next] === 0) {
|
|
122
|
-
state[next] = 2;
|
|
123
|
-
queueX[tail] = x - 1;
|
|
124
|
-
queue[tail++] = next;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (x + 1 < width) {
|
|
128
|
-
const next = index + 1;
|
|
129
|
-
if (state[next] === 0) {
|
|
130
|
-
state[next] = 2;
|
|
131
|
-
queueX[tail] = x + 1;
|
|
132
|
-
queue[tail++] = next;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (index >= width) {
|
|
136
|
-
const next = index - width;
|
|
137
|
-
if (state[next] === 0) {
|
|
138
|
-
state[next] = 2;
|
|
139
|
-
queueX[tail] = x;
|
|
140
|
-
queue[tail++] = next;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
if (index + width < count) {
|
|
144
|
-
const next = index + width;
|
|
145
|
-
if (state[next] === 0) {
|
|
146
|
-
state[next] = 2;
|
|
147
|
-
queueX[tail] = x;
|
|
148
|
-
queue[tail++] = next;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (scratchState.length > 65536) scratchState = /* @__PURE__ */ new Uint8Array(0);
|
|
154
|
-
if (scratchQueue.length > 65536) {
|
|
155
|
-
scratchQueue = /* @__PURE__ */ new Uint32Array(0);
|
|
156
|
-
scratchQueueX = /* @__PURE__ */ new Int32Array(0);
|
|
157
|
-
}
|
|
158
|
-
return {
|
|
111
|
+
if (pxRange <= 0 || !hasColor || inkCount === count) return {
|
|
159
112
|
data,
|
|
160
113
|
colored
|
|
161
114
|
};
|
|
162
|
-
|
|
163
|
-
/** @internal Upload-ready colour band; ownership transfers to its atlas wrapper. */
|
|
164
|
-
function createColorLayer(band, width, options, maxAnisotropy) {
|
|
165
|
-
const texture = new DataTexture(band.data, width, band.height, RGBAFormat, UnsignedByteType);
|
|
166
|
-
texture.colorSpace = SRGBColorSpace;
|
|
167
|
-
texture.flipY = false;
|
|
168
|
-
texture.generateMipmaps = options.mipmaps ?? false;
|
|
169
|
-
texture.minFilter = texture.generateMipmaps ? LinearMipmapLinearFilter : LinearFilter;
|
|
170
|
-
texture.magFilter = LinearFilter;
|
|
171
|
-
texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy));
|
|
172
|
-
texture.needsUpdate = true;
|
|
115
|
+
spreadColors(words, white, width, first, last);
|
|
173
116
|
return {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
height: band.height
|
|
117
|
+
data,
|
|
118
|
+
colored
|
|
177
119
|
};
|
|
178
120
|
}
|
|
179
|
-
export {
|
|
121
|
+
export { encodeColorBand, slotHasColor };
|
|
180
122
|
|
|
181
123
|
//# sourceMappingURL=colorGlyphs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"colorGlyphs.js","names":[],"sources":["../../src/atlas/colorGlyphs.ts"],"sourcesContent":["import {\n DataTexture,\n LinearFilter,\n LinearMipmapLinearFilter,\n RGBAFormat,\n SRGBColorSpace,\n UnsignedByteType,\n} from 'three/webgpu'\nimport type { BuildSdfAtlasOptions, RasterColorBand, SdfColorLayer } from '../types.js'\n\ninterface ColorSlot {\n readonly x: number\n readonly y: number\n readonly width: number\n readonly height: number\n}\n\n/**\n * @internal Classify fillStyle-invariant ink and label every empty texel by its nearest ink:\n * colour carries its colour and full alpha on, foreground ink leaves the texel clear. The\n * alpha of the band is then 1 around colour and 0 around text that shares a slot with it,\n * whatever the distance. A `pxRange` of 0 only classifies.\n */\nlet scratchState = new Uint8Array(0)\nlet scratchQueue = new Uint32Array(0)\nlet scratchQueueX = new Int32Array(0)\n\nexport function encodeColorBand(\n white: Uint8ClampedArray,\n black: Uint8ClampedArray,\n width: number,\n height: number,\n pxRange: number,\n slots: readonly ColorSlot[],\n): { data: Uint8Array; colored: boolean[] } {\n const count = width * height\n const data = new Uint8Array(count * 4)\n // Whole-pixel copies preserve byte order on either endian architecture.\n const packed = new Uint32Array(data.buffer)\n // 0 = empty, 1 = colour or nearest to it, 2 = nearest to foreground, 255 = solid foreground.\n if (scratchState.length < count) scratchState = new Uint8Array(count)\n const state = scratchState\n state.fill(0, 0, count)\n let hasColor = false\n let firstInkIndex = -1\n let lastInkIndex = -1\n for (let index = 0, at = 0; index < count; index++, at += 4) {\n if (white[at + 3]! < 128) continue\n if (firstInkIndex < 0) firstInkIndex = index\n lastInkIndex = index\n const b0 = black[at]!\n const b1 = black[at + 1]!\n const b2 = black[at + 2]!\n if (white[at]! - b0 >= 128 && white[at + 1]! - b1 >= 128 && white[at + 2]! - b2 >= 128) {\n state[index] = 255\n continue\n }\n data[at] = b0\n data[at + 1] = b1\n data[at + 2] = b2\n data[at + 3] = 255\n state[index] = 1\n hasColor = true\n }\n const colored = slots.map((slot) => {\n if (!hasColor) return false\n const xEnd = slot.x + slot.width\n const yEnd = slot.y + slot.height\n for (let y = slot.y; y < yEnd; y++) {\n const row = y * width\n for (let x = slot.x; x < xEnd; x++) {\n if (state[row + x] === 1) return true\n }\n }\n return false\n })\n if (pxRange <= 0 || !hasColor) {\n if (scratchState.length > 65536) scratchState = new Uint8Array(0)\n return { data, colored }\n }\n if (scratchQueue.length < count) {\n scratchQueue = new Uint32Array(count)\n scratchQueueX = new Int32Array(count)\n }\n const queue = scratchQueue\n const queueX = scratchQueueX\n let tail = 0\n const minInkY = (firstInkIndex / width) | 0\n const maxInkY = (lastInkIndex / width) | 0\n // Interior seeds cannot reach empty pixels directly. Keeping only boundary\n // seeds preserves their raster-order tie breaking and avoids four visits per\n // interior texel. The waves below are breadth-first, from both kinds of ink at once.\n for (let y = minInkY; y <= maxInkY; y++) {\n const row = y * width\n for (let x = 0; x < width; x++) {\n const index = row + x\n if (state[index] === 0) continue\n if (\n (x > 0 && state[index - 1] === 0) ||\n (x + 1 < width && state[index + 1] === 0) ||\n (y > 0 && state[index - width] === 0) ||\n (y + 1 < height && state[index + width] === 0)\n ) {\n queueX[tail] = x\n queue[tail++] = index\n }\n }\n }\n for (let head = 0; head < tail; head++) {\n const index = queue[head]!\n const x = queueX[head]!\n if (state[index] === 1) {\n const colorWord = packed[index]!\n if (x > 0) {\n const next = index - 1\n if (state[next] === 0) {\n state[next] = 1\n packed[next] = colorWord\n queueX[tail] = x - 1\n queue[tail++] = next\n }\n }\n if (x + 1 < width) {\n const next = index + 1\n if (state[next] === 0) {\n state[next] = 1\n packed[next] = colorWord\n queueX[tail] = x + 1\n queue[tail++] = next\n }\n }\n if (index >= width) {\n const next = index - width\n if (state[next] === 0) {\n state[next] = 1\n packed[next] = colorWord\n queueX[tail] = x\n queue[tail++] = next\n }\n }\n if (index + width < count) {\n const next = index + width\n if (state[next] === 0) {\n state[next] = 1\n packed[next] = colorWord\n queueX[tail] = x\n queue[tail++] = next\n }\n }\n } else {\n if (x > 0) {\n const next = index - 1\n if (state[next] === 0) {\n state[next] = 2\n queueX[tail] = x - 1\n queue[tail++] = next\n }\n }\n if (x + 1 < width) {\n const next = index + 1\n if (state[next] === 0) {\n state[next] = 2\n queueX[tail] = x + 1\n queue[tail++] = next\n }\n }\n if (index >= width) {\n const next = index - width\n if (state[next] === 0) {\n state[next] = 2\n queueX[tail] = x\n queue[tail++] = next\n }\n }\n if (index + width < count) {\n const next = index + width\n if (state[next] === 0) {\n state[next] = 2\n queueX[tail] = x\n queue[tail++] = next\n }\n }\n }\n }\n if (scratchState.length > 65536) scratchState = new Uint8Array(0)\n if (scratchQueue.length > 65536) {\n scratchQueue = new Uint32Array(0)\n scratchQueueX = new Int32Array(0)\n }\n return { data, colored }\n}\n\n/** @internal Upload-ready colour band; ownership transfers to its atlas wrapper. */\nexport function createColorLayer(\n band: RasterColorBand,\n width: number,\n options: BuildSdfAtlasOptions,\n maxAnisotropy: number,\n): SdfColorLayer {\n const texture = new DataTexture(band.data, width, band.height, RGBAFormat, UnsignedByteType)\n texture.colorSpace = SRGBColorSpace\n texture.flipY = false\n texture.generateMipmaps = options.mipmaps ?? false\n texture.minFilter = texture.generateMipmaps ? LinearMipmapLinearFilter : LinearFilter\n texture.magFilter = LinearFilter\n texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy))\n texture.needsUpdate = true\n return { texture, top: band.top, height: band.height }\n}\n"],"mappings":";;;;;;;;AAuBA,IAAI,+BAAe,IAAI,WAAW,CAAC;AACnC,IAAI,+BAAe,IAAI,YAAY,CAAC;AACpC,IAAI,gCAAgB,IAAI,WAAW,CAAC;AAEpC,SAAgB,gBACd,OACA,OACA,OACA,QACA,SACA,OAC0C;CAC1C,MAAM,QAAQ,QAAQ;CACtB,MAAM,OAAO,IAAI,WAAW,QAAQ,CAAC;CAErC,MAAM,SAAS,IAAI,YAAY,KAAK,MAAM;CAE1C,IAAI,aAAa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,cAAc;CACtB,OAAO;EAAE;EAAS,KAAK,KAAK;EAAK,QAAQ,KAAK;CAAO;AACvD"}
|
|
1
|
+
{"version":3,"file":"colorGlyphs.js","names":[],"sources":["../../src/atlas/colorGlyphs.ts"],"sourcesContent":["interface ColorSlot {\n readonly x: number\n readonly y: number\n readonly width: number\n readonly height: number\n}\n\n/** @internal Probe a glyph without constructing a colour band. */\nexport function slotHasColor(\n white: Uint8ClampedArray,\n black: Uint8ClampedArray,\n width: number,\n slot: ColorSlot,\n): boolean {\n const rowBytes = width * 4\n const slotBytes = slot.width * 4\n const start = (slot.y * width + slot.x) * 4\n const end = start + slot.height * rowBytes\n for (let row = start; row < end; row += rowBytes) {\n for (let at = row; at < row + slotBytes; at += 4) {\n if (white[at + 3]! < 128) continue\n if (\n white[at]! - black[at]! < 128 ||\n white[at + 1]! - black[at + 1]! < 128 ||\n white[at + 2]! - black[at + 2]! < 128\n ) {\n return true\n }\n }\n }\n return false\n}\n\nlet scratchDistances = new Uint16Array(0)\n\n/**\n * The nearest Manhattan-distance seed, with ties going to the first seed in raster order.\n * First find the nearest seed in each row (left wins ties), then compare rows (top wins ties).\n */\nfunction spreadColors(\n words: Uint32Array,\n white: Uint8ClampedArray,\n width: number,\n firstInk: number,\n lastInk: number,\n): void {\n const top = Math.floor(firstInk / width) * width\n const bottom = (Math.floor(lastInk / width) + 1) * width\n const count = bottom - top\n const far = width + count / width\n let distances: Uint16Array | Float64Array\n if (far > 0xffff) distances = new Float64Array(count)\n else if (count > 65536) distances = new Uint16Array(count)\n else {\n if (scratchDistances.length < count) scratchDistances = new Uint16Array(count)\n distances = scratchDistances\n }\n\n // Write an empty row span using its nearest horizontal seed and the previous row's result.\n const fillSpan = (\n start: number,\n end: number,\n horizontal: number,\n step: number,\n word: number,\n ): void => {\n for (let pixel = start; pixel < end; pixel++, horizontal += step) {\n const i = pixel - top\n const above = i >= width ? distances[i - width]! + 1 : far\n if (above <= horizontal) {\n distances[i] = above\n words[pixel] = words[pixel - width]!\n } else {\n distances[i] = horizontal\n words[pixel] = word\n }\n }\n }\n\n for (let row = top; row < bottom; row += width) {\n const end = row + width\n let left = row - 1\n let leftWord = 0\n for (let pixel = row; pixel < end; pixel++) {\n if (white[pixel * 4 + 3]! < 128) continue\n const word = words[pixel]!\n if (pixel > left + 1) {\n const middle = left < row ? left : left + Math.floor((pixel - left) / 2)\n fillSpan(left + 1, middle + 1, 1, 1, leftWord)\n fillSpan(middle + 1, pixel, pixel - middle - 1, -1, word)\n }\n distances[pixel - top] = 0\n left = pixel\n leftWord = word\n }\n if (left < row) {\n words.copyWithin(row, row - width, row)\n for (let i = row - top; i < end - top; i++) distances[i] = distances[i - width]! + 1\n } else {\n fillSpan(left + 1, end, 1, 1, leftWord)\n }\n }\n\n for (let pixel = bottom - width - 1; pixel >= top; pixel--) {\n const i = pixel - top\n const below = distances[i + width]! + 1\n if (below < distances[i]!) {\n distances[i] = below\n words[pixel] = words[pixel + width]!\n }\n }\n // Beyond the outermost ink rows, every seed is equally many extra rows away.\n for (let row = 0; row < top; row += width) words.copyWithin(row, top, top + width)\n for (let row = bottom; row < words.length; row += width)\n words.copyWithin(row, bottom - width, bottom)\n}\n\n/** @internal Classify fill-invariant ink and extend its colour (or foreground transparency). */\nexport function encodeColorBand(\n white: Uint8ClampedArray,\n black: Uint8ClampedArray,\n width: number,\n height: number,\n pxRange: number,\n slots: readonly ColorSlot[],\n): { data: Uint8Array; colored: boolean[] } {\n const count = width * height\n const data = new Uint8Array(count * 4)\n const words = new Uint32Array(data.buffer)\n const light = new DataView(white.buffer, white.byteOffset, white.byteLength)\n const dark = new DataView(black.buffer, black.byteOffset, black.byteLength)\n const result = new DataView(data.buffer)\n let first = 0\n let last = count - 1\n while (first <= last && white[first * 4 + 3]! < 128) first++\n while (last >= first && white[last * 4 + 3]! < 128) last--\n\n let inkCount = 0\n let hasColor = false\n for (let index = first; index <= last; index++) {\n const at = index * 4\n const w = light.getInt32(at, true)\n if (w >= 0) continue // Little-endian RGBA has its alpha high bit in the sign bit.\n inkCount++\n const b = dark.getInt32(at, true)\n // Each light channel must be >= 128 and its dark channel < 128 for their\n // difference to reach 128. The subtraction then cannot borrow across channels.\n const highBits = 0x808080\n if ((w & ~b & highBits) === highBits && ((w - b) & highBits) === highBits) continue\n result.setInt32(at, b | 0xff000000, true)\n hasColor = true\n }\n const colored = slots.map((slot) => {\n if (!hasColor) return false\n const xEnd = slot.x + slot.width\n const yEnd = slot.y + slot.height\n for (let y = slot.y; y < yEnd; y++) {\n const row = y * width\n for (let x = slot.x; x < xEnd; x++) {\n if (words[row + x]) return true\n }\n }\n return false\n })\n if (pxRange <= 0 || !hasColor || inkCount === count) return { data, colored }\n spreadColors(words, white, width, first, last)\n return { data, colored }\n}\n"],"mappings":";;AAQA,SAAgB,aACd,OACA,OACA,OACA,MACS;CACT,MAAM,WAAW,QAAQ;CACzB,MAAM,YAAY,KAAK,QAAQ;CAC/B,MAAM,SAAS,KAAK,IAAI,QAAQ,KAAK,KAAK;CAC1C,MAAM,MAAM,QAAQ,KAAK,SAAS;CAClC,KAAK,IAAI,MAAM,OAAO,MAAM,KAAK,OAAO,UACtC,KAAK,IAAI,KAAK,KAAK,KAAK,MAAM,WAAW,MAAM,GAAG;EAChD,IAAI,MAAM,KAAK,KAAM,KAAK;EAC1B,IACE,MAAM,MAAO,MAAM,MAAO,OAC1B,MAAM,KAAK,KAAM,MAAM,KAAK,KAAM,OAClC,MAAM,KAAK,KAAM,MAAM,KAAK,KAAM,KAElC,OAAO;CAEX;CAEF,OAAO;AACT;AAEA,IAAI,mCAAmB,IAAI,YAAY,CAAC;;;;;AAMxC,SAAS,aACP,OACA,OACA,OACA,UACA,SACM;CACN,MAAM,MAAM,KAAK,MAAM,WAAW,KAAK,IAAI;CAC3C,MAAM,UAAU,KAAK,MAAM,UAAU,KAAK,IAAI,KAAK;CACnD,MAAM,QAAQ,SAAS;CACvB,MAAM,MAAM,QAAQ,QAAQ;CAC5B,IAAI;CACJ,IAAI,MA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SAAS,IAAI,IAAI,YAAY,IAAI;EACxC,WAAW;CACb;CACA,MAAM,UAAU,MAAM,KAAK,SAAS;EAClC,IAAI,CAAC,UAAU,OAAO;EACtB,MAAM,OAAO,KAAK,IAAI,KAAK;EAC3B,MAAM,OAAO,KAAK,IAAI,KAAK;EAC3B,KAAK,IAAI,IAAI,KAAK,GAAG,IAAI,MAAM,KAAK;GAClC,MAAM,MAAM,IAAI;GAChB,KAAK,IAAI,IAAI,KAAK,GAAG,IAAI,MAAM,KAC7B,IAAI,MAAM,MAAM,IAAI,OAAO;EAE/B;EACA,OAAO;CACT,CAAC;CACD,IAAI,WAAW,KAAK,CAAC,YAAY,aAAa,OAAO,OAAO;EAAE;EAAM;CAAQ;CAC5E,aAAa,OAAO,OAAO,OAAO,OAAO,IAAI;CAC7C,OAAO;EAAE;EAAM;CAAQ;AACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DataTexture, ExternalTexture, LinearFilter, LinearMipmapLinearFilter, RGBAFormat, SRGBColorSpace, UnsignedByteType } from "three/webgpu";
|
|
2
|
+
// src/atlas/colorTexture.ts
|
|
3
|
+
var COLOR_BLEED_STATE_KEY = Symbol.for("ultra-text.gpuColorBleed");
|
|
4
|
+
function colorBleedRegistry() {
|
|
5
|
+
return globalThis[COLOR_BLEED_STATE_KEY];
|
|
6
|
+
}
|
|
7
|
+
/** @internal Upload-ready colour band; ownership transfers to its atlas wrapper. */
|
|
8
|
+
function createColorLayer(band, width, options, maxAnisotropy) {
|
|
9
|
+
const reg = colorBleedRegistry();
|
|
10
|
+
const gpuTexture = reg?.ready.get(band.data);
|
|
11
|
+
if (gpuTexture) {
|
|
12
|
+
reg.ready.delete(band.data);
|
|
13
|
+
const mipmaps = options.mipmaps ?? false;
|
|
14
|
+
const texture = new ExternalTexture(gpuTexture);
|
|
15
|
+
const baseDispose = texture.dispose.bind(texture);
|
|
16
|
+
let disposed = false;
|
|
17
|
+
texture.dispose = () => {
|
|
18
|
+
if (disposed) return;
|
|
19
|
+
disposed = true;
|
|
20
|
+
try {
|
|
21
|
+
baseDispose();
|
|
22
|
+
} finally {
|
|
23
|
+
gpuTexture.destroy();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
texture.image = {
|
|
27
|
+
width,
|
|
28
|
+
height: band.height
|
|
29
|
+
};
|
|
30
|
+
texture.colorSpace = SRGBColorSpace;
|
|
31
|
+
texture.flipY = false;
|
|
32
|
+
texture.format = RGBAFormat;
|
|
33
|
+
texture.generateMipmaps = false;
|
|
34
|
+
texture.minFilter = mipmaps ? LinearMipmapLinearFilter : LinearFilter;
|
|
35
|
+
texture.magFilter = LinearFilter;
|
|
36
|
+
texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy));
|
|
37
|
+
return {
|
|
38
|
+
texture,
|
|
39
|
+
top: band.top,
|
|
40
|
+
height: band.height
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const deferred = reg?.bleeds.get(band.data);
|
|
44
|
+
if (deferred) {
|
|
45
|
+
reg.bleeds.delete(band.data);
|
|
46
|
+
deferred.applyCpu();
|
|
47
|
+
}
|
|
48
|
+
const texture = new DataTexture(band.data, width, band.height, RGBAFormat, UnsignedByteType);
|
|
49
|
+
texture.colorSpace = SRGBColorSpace;
|
|
50
|
+
texture.flipY = false;
|
|
51
|
+
texture.generateMipmaps = options.mipmaps ?? false;
|
|
52
|
+
texture.minFilter = texture.generateMipmaps ? LinearMipmapLinearFilter : LinearFilter;
|
|
53
|
+
texture.magFilter = LinearFilter;
|
|
54
|
+
texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy));
|
|
55
|
+
texture.needsUpdate = true;
|
|
56
|
+
return {
|
|
57
|
+
texture,
|
|
58
|
+
top: band.top,
|
|
59
|
+
height: band.height
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export { createColorLayer };
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=colorTexture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colorTexture.js","names":[],"sources":["../../src/atlas/colorTexture.ts"],"sourcesContent":["import {\n DataTexture,\n ExternalTexture,\n LinearFilter,\n LinearMipmapLinearFilter,\n RGBAFormat,\n SRGBColorSpace,\n UnsignedByteType,\n type Texture,\n} from 'three/webgpu'\nimport type { BuildSdfAtlasOptions, RasterColorBand, SdfColorLayer } from '../types.js'\n\ninterface DeferredColorBleedState {\n readonly bleeds: WeakMap<Uint8Array, { readonly applyCpu: () => void }>\n readonly ready: WeakMap<Uint8Array, { destroy(): void }>\n}\n\nconst COLOR_BLEED_STATE_KEY = Symbol.for('ultra-text.gpuColorBleed')\n\nfunction colorBleedRegistry(): DeferredColorBleedState | undefined {\n return (globalThis as Record<symbol, DeferredColorBleedState | undefined>)[COLOR_BLEED_STATE_KEY]\n}\n\n/** @internal Upload-ready colour band; ownership transfers to its atlas wrapper. */\nexport function createColorLayer(\n band: RasterColorBand,\n width: number,\n options: BuildSdfAtlasOptions,\n maxAnisotropy: number,\n): SdfColorLayer {\n const reg = colorBleedRegistry()\n const gpuTexture = reg?.ready.get(band.data)\n if (gpuTexture) {\n reg!.ready.delete(band.data)\n const mipmaps = options.mipmaps ?? false\n const texture: Texture = new ExternalTexture(gpuTexture)\n const baseDispose = texture.dispose.bind(texture)\n let disposed = false\n texture.dispose = () => {\n if (disposed) return\n disposed = true\n try {\n baseDispose()\n } finally {\n gpuTexture.destroy()\n }\n }\n ;(texture as { image: unknown }).image = { width, height: band.height }\n texture.colorSpace = SRGBColorSpace\n texture.flipY = false\n texture.format = RGBAFormat\n texture.generateMipmaps = false\n texture.minFilter = mipmaps ? LinearMipmapLinearFilter : LinearFilter\n texture.magFilter = LinearFilter\n texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy))\n return { texture, top: band.top, height: band.height }\n }\n const deferred = reg?.bleeds.get(band.data)\n if (deferred) {\n reg!.bleeds.delete(band.data)\n deferred.applyCpu()\n }\n const texture = new DataTexture(band.data, width, band.height, RGBAFormat, UnsignedByteType)\n texture.colorSpace = SRGBColorSpace\n texture.flipY = false\n texture.generateMipmaps = options.mipmaps ?? false\n texture.minFilter = texture.generateMipmaps ? LinearMipmapLinearFilter : LinearFilter\n texture.magFilter = LinearFilter\n texture.anisotropy = Math.max(1, Math.min(options.anisotropy ?? 1, maxAnisotropy))\n texture.needsUpdate = true\n return { texture, top: band.top, height: band.height }\n}\n"],"mappings":";;AAiBA,IAAM,wBAAwB,OAAO,IAAI,0BAA0B;AAEnE,SAAS,qBAA0D;CACjE,OAAQ,WAAmE;AAC7E;;AAGA,SAAgB,iBACd,MACA,OACA,SACA,eACe;CACf,MAAM,MAAM,mBAAmB;CAC/B,MAAM,aAAa,KAAK,MAAM,IAAI,KAAK,IAAI;CAC3C,IAAI,YAAY;EACd,IAAK,MAAM,OAAO,KAAK,IAAI;EAC3B,MAAM,UAAU,QAAQ,WAAW;EACnC,MAAM,UAAmB,IAAI,gBAAgB,UAAU;EACvD,MAAM,cAAc,QAAQ,QAAQ,KAAK,OAAO;EAChD,IAAI,WAAW;EACf,QAAQ,gBAAgB;GACtB,IAAI,UAAU;GACd,WAAW;GACX,IAAI;IACF,YAAY;GACd,UAAU;IACR,WAAW,QAAQ;GACrB;EACF;EACC,QAAgC,QAAQ;GAAE;GAAO,QAAQ,KAAK;EAAO;EACtE,QAAQ,aAAa;EACrB,QAAQ,QAAQ;EAChB,QAAQ,SAAS;EACjB,QAAQ,kBAAkB;EAC1B,QAAQ,YAAY,UAAU,2BAA2B;EACzD,QAAQ,YAAY;EACpB,QAAQ,aAAa,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,cAAc,GAAG,aAAa,CAAC;EACjF,OAAO;GAAE;GAAS,KAAK,KAAK;GAAK,QAAQ,KAAK;EAAO;CACvD;CACA,MAAM,WAAW,KAAK,OAAO,IAAI,KAAK,IAAI;CAC1C,IAAI,UAAU;EACZ,IAAK,OAAO,OAAO,KAAK,IAAI;EAC5B,SAAS,SAAS;CACpB;CACA,MAAM,UAAU,IAAI,YAAY,KAAK,MAAM,OAAO,KAAK,QAAQ,YAAY,gBAAgB;CAC3F,QAAQ,aAAa;CACrB,QAAQ,QAAQ;CAChB,QAAQ,kBAAkB,QAAQ,WAAW;CAC7C,QAAQ,YAAY,QAAQ,kBAAkB,2BAA2B;CACzE,QAAQ,YAAY;CACpB,QAAQ,aAAa,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,cAAc,GAAG,aAAa,CAAC;CACjF,QAAQ,cAAc;CACtB,OAAO;EAAE;EAAS,KAAK,KAAK;EAAK,QAAQ,KAAK;CAAO;AACvD"}
|
package/dist/atlas/cpuAtlas.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { BuildSdfAtlasOptions, RasterAtlas, SdfAtlas } from '../types.js';
|
|
2
|
-
|
|
2
|
+
import type { PreparedCpuWorker } from './cpuWorker.js';
|
|
3
|
+
export declare function buildCpuSdfAtlas(raster: RasterAtlas, options?: Omit<BuildSdfAtlasOptions, 'mode'>,
|
|
4
|
+
/** @internal */
|
|
5
|
+
preparedWorker?: PreparedCpuWorker | null): Promise<SdfAtlas>;
|
package/dist/atlas/cpuAtlas.js
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
|
-
import { alphaOfRgba, encodeSdfFromAlpha } from "./cpuSdf.js";
|
|
2
1
|
import { DataTexture, LinearFilter, LinearMipmapLinearFilter, NoColorSpace, RedFormat, UnsignedByteType } from "three/webgpu";
|
|
3
2
|
// src/atlas/cpuAtlas.ts
|
|
3
|
+
/** @internal Overlap the main-thread encoder import with an intentional CPU capture. */
|
|
4
|
+
var loadCpuEncoder = () => import("./cpuSdf.js");
|
|
4
5
|
function readPixels(raster) {
|
|
5
6
|
const context = raster.canvas.getContext("2d", { willReadFrequently: true });
|
|
6
7
|
if (!context) throw new Error("buildCpuSdfAtlas: Canvas 2D is unavailable");
|
|
7
8
|
return context.getImageData(0, 0, raster.width, raster.height).data;
|
|
8
9
|
}
|
|
9
|
-
async function buildCpuSdfAtlas(raster, options = {}) {
|
|
10
|
+
async function buildCpuSdfAtlas(raster, options = {}, preparedWorker) {
|
|
10
11
|
const { width, height, pxRange, bakeSize, glyphs, shapeTop } = raster;
|
|
11
12
|
const { signal, mipmaps = false, anisotropy = 1, cpuWorker } = options;
|
|
12
13
|
signal?.throwIfAborted();
|
|
14
|
+
const workerImport = cpuWorker !== false && typeof Worker !== "undefined" ? import("./cpuWorker.js") : void 0;
|
|
13
15
|
let rgba = readPixels(raster);
|
|
14
16
|
let sdf;
|
|
15
|
-
if (
|
|
16
|
-
const { encodeInWorker } = await
|
|
17
|
-
sdf = await encodeInWorker(rgba, width, height, pxRange, signal);
|
|
17
|
+
if (workerImport) try {
|
|
18
|
+
const { encodeInWorker } = await workerImport;
|
|
19
|
+
sdf = await encodeInWorker(rgba, width, height, pxRange, signal, preparedWorker);
|
|
18
20
|
} catch {
|
|
19
21
|
signal?.throwIfAborted();
|
|
20
22
|
if (rgba.byteLength === 0) rgba = readPixels(raster);
|
|
21
23
|
}
|
|
24
|
+
else preparedWorker?.dispose();
|
|
22
25
|
signal?.throwIfAborted();
|
|
23
|
-
|
|
26
|
+
if (!sdf) {
|
|
27
|
+
const { encodeSdfFromRgba } = await loadCpuEncoder();
|
|
28
|
+
signal?.throwIfAborted();
|
|
29
|
+
sdf = encodeSdfFromRgba(rgba, width, height, pxRange);
|
|
30
|
+
}
|
|
24
31
|
const texture = new DataTexture(sdf, width, height, RedFormat, UnsignedByteType);
|
|
25
32
|
texture.name = "ultra-text runtime SDF atlas (CPU EDT)";
|
|
26
33
|
texture.colorSpace = NoColorSpace;
|
|
@@ -47,6 +54,6 @@ async function buildCpuSdfAtlas(raster, options = {}) {
|
|
|
47
54
|
}
|
|
48
55
|
};
|
|
49
56
|
}
|
|
50
|
-
export { buildCpuSdfAtlas };
|
|
57
|
+
export { buildCpuSdfAtlas, loadCpuEncoder };
|
|
51
58
|
|
|
52
59
|
//# sourceMappingURL=cpuAtlas.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cpuAtlas.js","names":[],"sources":["../../src/atlas/cpuAtlas.ts"],"sourcesContent":["import {\n DataTexture,\n LinearFilter,\n LinearMipmapLinearFilter,\n NoColorSpace,\n RedFormat,\n UnsignedByteType,\n} from 'three/webgpu'\n\nimport type { BuildSdfAtlasOptions, RasterAtlas, SdfAtlas } from '../types.js'\nimport {
|
|
1
|
+
{"version":3,"file":"cpuAtlas.js","names":[],"sources":["../../src/atlas/cpuAtlas.ts"],"sourcesContent":["import {\n DataTexture,\n LinearFilter,\n LinearMipmapLinearFilter,\n NoColorSpace,\n RedFormat,\n UnsignedByteType,\n} from 'three/webgpu'\n\nimport type { BuildSdfAtlasOptions, RasterAtlas, SdfAtlas } from '../types.js'\nimport type { PreparedCpuWorker } from './cpuWorker.js'\nimport type { ShapeRows } from './rasterize.js'\n\n/** @internal Overlap the main-thread encoder import with an intentional CPU capture. */\nexport const loadCpuEncoder = () => import('./cpuSdf.js')\n\nfunction readPixels(raster: RasterAtlas): Uint8ClampedArray {\n const context = raster.canvas.getContext('2d', { willReadFrequently: true })\n if (!context) throw new Error('buildCpuSdfAtlas: Canvas 2D is unavailable')\n return context.getImageData(0, 0, raster.width, raster.height).data\n}\n\nexport async function buildCpuSdfAtlas(\n raster: RasterAtlas,\n options: Omit<BuildSdfAtlasOptions, 'mode'> = {},\n /** @internal */\n preparedWorker?: PreparedCpuWorker | null,\n): Promise<SdfAtlas> {\n const { width, height, pxRange, bakeSize, glyphs, shapeTop } = raster as RasterAtlas & ShapeRows\n const { signal, mipmaps = false, anisotropy = 1, cpuWorker } = options\n signal?.throwIfAborted()\n const useWorker = cpuWorker !== false && typeof Worker !== 'undefined'\n const workerImport = useWorker ? import('./cpuWorker.js') : undefined\n let rgba = readPixels(raster)\n let sdf: Uint8Array | undefined\n if (workerImport) {\n try {\n const { encodeInWorker } = await workerImport\n // The worker takes the alpha out of the pixels directly via encodeSdfFromRgba.\n sdf = await encodeInWorker(rgba, width, height, pxRange, signal, preparedWorker)\n } catch {\n signal?.throwIfAborted()\n // A worker can fail to load under CSP or an older bundler. Its transferred\n // input may already be detached, so recapture before the synchronous fallback.\n if (rgba.byteLength === 0) rgba = readPixels(raster)\n }\n } else {\n preparedWorker?.dispose()\n }\n signal?.throwIfAborted()\n if (!sdf) {\n const { encodeSdfFromRgba } = await loadCpuEncoder()\n signal?.throwIfAborted()\n sdf = encodeSdfFromRgba(rgba, width, height, pxRange)\n }\n const texture = new DataTexture(sdf, width, height, RedFormat, UnsignedByteType)\n texture.name = 'ultra-text runtime SDF atlas (CPU EDT)'\n texture.colorSpace = NoColorSpace\n texture.flipY = false\n texture.generateMipmaps = mipmaps\n texture.minFilter = mipmaps ? LinearMipmapLinearFilter : LinearFilter\n texture.magFilter = LinearFilter\n texture.anisotropy = mipmaps ? Math.max(1, anisotropy) : 1\n texture.needsUpdate = true\n\n let disposed = false\n return {\n ...(shapeTop === undefined ? {} : { shapeTop }),\n texture,\n width,\n height,\n pxRange,\n bakeSize,\n glyphs,\n mode: 'cpu',\n dispose(): void {\n if (disposed) return\n disposed = true\n texture.dispose()\n },\n }\n}\n"],"mappings":";;;AAcA,IAAa,uBAAuB,OAAO;AAE3C,SAAS,WAAW,QAAwC;CAC1D,MAAM,UAAU,OAAO,OAAO,WAAW,MAAM,EAAE,oBAAoB,KAAK,CAAC;CAC3E,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,4CAA4C;CAC1E,OAAO,QAAQ,aAAa,GAAG,GAAG,OAAO,OAAO,OAAO,MAAM,CAAC,CAAC;AACjE;AAEA,eAAsB,iBACpB,QACA,UAA8C,CAAC,GAE/C,gBACmB;CACnB,MAAM,EAAE,OAAO,QAAQ,SAAS,UAAU,QAAQ,aAAa;CAC/D,MAAM,EAAE,QAAQ,UAAU,OAAO,aAAa,GAAG,cAAc;CAC/D,QAAQ,eAAe;CAEvB,MAAM,eADY,cAAc,SAAS,OAAO,WAAW,cAC1B,OAAO,oBAAoB,KAAA;CAC5D,IAAI,OAAO,WAAW,MAAM;CAC5B,IAAI;CACJ,IAAI,cACF,IAAI;EACF,MAAM,EAAE,mBAAmB,MAAM;EAEjC,MAAM,MAAM,eAAe,MAAM,OAAO,QAAQ,SAAS,QAAQ,cAAc;CACjF,QAAQ;EACN,QAAQ,eAAe;EAGvB,IAAI,KAAK,eAAe,GAAG,OAAO,WAAW,MAAM;CACrD;MAEA,gBAAgB,QAAQ;CAE1B,QAAQ,eAAe;CACvB,IAAI,CAAC,KAAK;EACR,MAAM,EAAE,sBAAsB,MAAM,eAAe;EACnD,QAAQ,eAAe;EACvB,MAAM,kBAAkB,MAAM,OAAO,QAAQ,OAAO;CACtD;CACA,MAAM,UAAU,IAAI,YAAY,KAAK,OAAO,QAAQ,WAAW,gBAAgB;CAC/E,QAAQ,OAAO;CACf,QAAQ,aAAa;CACrB,QAAQ,QAAQ;CAChB,QAAQ,kBAAkB;CAC1B,QAAQ,YAAY,UAAU,2BAA2B;CACzD,QAAQ,YAAY;CACpB,QAAQ,aAAa,UAAU,KAAK,IAAI,GAAG,UAAU,IAAI;CACzD,QAAQ,cAAc;CAEtB,IAAI,WAAW;CACf,OAAO;EACL,GAAI,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS;EAC7C;EACA;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,UAAgB;GACd,IAAI,UAAU;GACd,WAAW;GACX,QAAQ,QAAQ;EAClB;CACF;AACF"}
|
package/dist/atlas/cpuSdf.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare function alphaOfRgba(rgba: Uint8ClampedArray, pixelCount: number)
|
|
|
13
13
|
*
|
|
14
14
|
* Fractional source alpha is retained as a sub-pixel seed cost rather than
|
|
15
15
|
* thresholded away. The two exact Euclidean distance transforms are O(width *
|
|
16
|
-
* height); temporary storage is one
|
|
16
|
+
* height); temporary storage is one active row band plus O(height) scratch.
|
|
17
17
|
*/
|
|
18
18
|
export declare function encodeSdfFromAlpha(alpha: AlphaPixels, width: number, height: number, pxRange: number): Uint8Array;
|
|
19
19
|
export {};
|