shaderfont 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.ko.md +47 -0
- package/README.md +46 -2
- package/package.json +21 -4
- package/src/browser.d.ts +5 -0
- package/src/core.js +316 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +26 -0
- package/src/mask-renderer.js +87 -0
- package/src/material-core.js +161 -0
- package/src/material-renderer.js +52 -0
- package/src/options.d.ts +28 -0
- package/src/options.js +75 -0
- package/src/paint-layout.js +24 -0
- package/src/presets.js +19 -0
- package/src/reveal-core.js +69 -0
- package/src/reveal-renderer.js +64 -0
- package/src/selection-gesture.js +64 -0
- package/src/selection-indicator.js +73 -0
- package/src/selection-range.js +17 -0
- package/src/shader-font-element.js +1329 -0
- package/src/shader-font.js +6 -0
- package/src/webgl-surface.js +103 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { registerShaderFontElement } from "./shader-font-element.js";
|
|
2
|
+
|
|
3
|
+
export { ShaderFont, upgradeLegacyShaderfont } from "./shader-font-element.js";
|
|
4
|
+
|
|
5
|
+
// Script API. Frameworks use the SSR-safe root and register after mounting.
|
|
6
|
+
registerShaderFontElement({ legacyAliases: true });
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const VERTEX_SHADER = `#version 300 es
|
|
2
|
+
void main() {
|
|
3
|
+
vec2 position = gl_VertexID == 0 ? vec2(-1.0, -1.0)
|
|
4
|
+
: gl_VertexID == 1 ? vec2(3.0, -1.0) : vec2(-1.0, 3.0);
|
|
5
|
+
gl_Position = vec4(position, 0.0, 1.0);
|
|
6
|
+
}`;
|
|
7
|
+
|
|
8
|
+
export function rendererError(code, message) {
|
|
9
|
+
return Object.assign(new Error(message), { code });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function exportSurface(canvas) {
|
|
13
|
+
const url = canvas.toDataURL("image/png");
|
|
14
|
+
if (!url.startsWith("data:image/png")) throw rendererError("MASK_EXPORT_FAILED", "The canvas could not export a PNG");
|
|
15
|
+
return url;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Owns one program and its GPU resources; consumers own their rendering inputs. */
|
|
19
|
+
export function createWebGLSurface(width, height, fragmentSource, onContextLost) {
|
|
20
|
+
const canvas = document.createElement("canvas");
|
|
21
|
+
canvas.width = width;
|
|
22
|
+
canvas.height = height;
|
|
23
|
+
const gl = canvas.getContext("webgl2", {
|
|
24
|
+
alpha: true, antialias: false, depth: false, stencil: false,
|
|
25
|
+
premultipliedAlpha: false, preserveDrawingBuffer: false, failIfMajorPerformanceCaveat: true
|
|
26
|
+
});
|
|
27
|
+
if (!gl) throw rendererError("WEBGL_UNAVAILABLE", "A WebGL 2 context is unavailable");
|
|
28
|
+
let program, vertexArray, disposed = false;
|
|
29
|
+
const shaders = [], textures = [];
|
|
30
|
+
const lost = () => onContextLost?.(rendererError("WEBGL_CONTEXT_LOST", "The WebGL context was lost"));
|
|
31
|
+
function dispose() {
|
|
32
|
+
if (disposed) return;
|
|
33
|
+
disposed = true;
|
|
34
|
+
canvas.removeEventListener("webglcontextlost", lost);
|
|
35
|
+
for (const shader of shaders) gl.deleteShader(shader);
|
|
36
|
+
for (const texture of textures) gl.deleteTexture(texture);
|
|
37
|
+
if (program) gl.deleteProgram(program);
|
|
38
|
+
if (vertexArray) gl.deleteVertexArray(vertexArray);
|
|
39
|
+
gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
40
|
+
canvas.width = canvas.height = 0;
|
|
41
|
+
}
|
|
42
|
+
function compile(type, source) {
|
|
43
|
+
const shader = gl.createShader(type);
|
|
44
|
+
if (!shader) throw rendererError("WEBGL_RESOURCE_FAILED", "WebGL could not allocate a shader");
|
|
45
|
+
shaders.push(shader);
|
|
46
|
+
gl.shaderSource(shader, source);
|
|
47
|
+
gl.compileShader(shader);
|
|
48
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
49
|
+
throw rendererError("WEBGL_SHADER_FAILED", gl.getShaderInfoLog(shader) || "The shader did not compile");
|
|
50
|
+
}
|
|
51
|
+
return shader;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
if (Math.max(width, height) > gl.getParameter(gl.MAX_TEXTURE_SIZE)) {
|
|
55
|
+
throw rendererError("WEBGL_TEXTURE_FAILED", "The surface exceeds the WebGL texture size limit");
|
|
56
|
+
}
|
|
57
|
+
program = gl.createProgram();
|
|
58
|
+
vertexArray = gl.createVertexArray();
|
|
59
|
+
if (!program || !vertexArray) throw rendererError("WEBGL_RESOURCE_FAILED", "WebGL could not allocate surface resources");
|
|
60
|
+
gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX_SHADER));
|
|
61
|
+
gl.attachShader(program, compile(gl.FRAGMENT_SHADER, fragmentSource));
|
|
62
|
+
gl.linkProgram(program);
|
|
63
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
64
|
+
throw rendererError("WEBGL_LINK_FAILED", gl.getProgramInfoLog(program) || "The program did not link");
|
|
65
|
+
}
|
|
66
|
+
gl.useProgram(program);
|
|
67
|
+
gl.bindVertexArray(vertexArray);
|
|
68
|
+
gl.disable(gl.BLEND);
|
|
69
|
+
gl.disable(gl.DITHER);
|
|
70
|
+
gl.viewport(0, 0, width, height);
|
|
71
|
+
canvas.addEventListener("webglcontextlost", lost);
|
|
72
|
+
return {
|
|
73
|
+
kind: "webgl", canvas, gl,
|
|
74
|
+
uniform(name) {
|
|
75
|
+
const location = gl.getUniformLocation(program, name);
|
|
76
|
+
if (location === null) throw rendererError("WEBGL_UNIFORM_FAILED", `Missing shader uniform: ${name}`);
|
|
77
|
+
return location;
|
|
78
|
+
},
|
|
79
|
+
texture() {
|
|
80
|
+
const texture = gl.createTexture();
|
|
81
|
+
if (!texture) throw rendererError("WEBGL_RESOURCE_FAILED", "WebGL could not allocate a texture");
|
|
82
|
+
textures.push(texture);
|
|
83
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
84
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
85
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
86
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
87
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
88
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
89
|
+
return texture;
|
|
90
|
+
},
|
|
91
|
+
draw() {
|
|
92
|
+
if (disposed || gl.isContextLost()) throw rendererError("WEBGL_CONTEXT_LOST", "The WebGL context was lost");
|
|
93
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
94
|
+
if (gl.getError() !== gl.NO_ERROR) throw rendererError("WEBGL_DRAW_FAILED", "WebGL could not draw the surface");
|
|
95
|
+
return exportSurface(canvas);
|
|
96
|
+
},
|
|
97
|
+
dispose
|
|
98
|
+
};
|
|
99
|
+
} catch (error) {
|
|
100
|
+
dispose();
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|