aboutsummaryrefslogtreecommitdiff
path: root/web/pw-visualizer/src/webgl/msdf_text.ts
blob: 28daf42cc737ca775fa15d2f6cabfe3ddfb2423d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Shader, Uniform1f, Uniform4f, UniformMatrix3fv } from "./shader";
import { Texture } from "./texture";
import { DefaultRenderable } from "./renderer";
import { IndexBuffer, VertexBuffer } from "./buffer";
import { VertexBufferLayout, VertexArray } from "./vertexBufferLayout";
import { robotoMsdfJson } from "../assets";


export enum Align {
    Begin,
    End,
    Middle,
}

export type FontAtlas = {
    atlas: AtlasMeta,
    metrics: Metrics,
    glyphs: Glyph[],
}

export type AtlasMeta = {
    type: string,
    distanceRange: number,
    size: number,
    width: number,
    height: number,
    yOrigin: string,
}

export type Metrics = {
    emSize: number,
    lineHeight: number,
    ascender: number,
    descender: number,
    underlineY: number,
    underlineThickness: number,
}


export type Glyph = {
    unicode: number,
    advance: number,
    planeBounds?: Bounds,
    atlasBounds?: Bounds,
}

export type Bounds = {
    left: number,
    bottom: number,
    right: number,
    top: number,
}


export class MsdfLabelFactory {
    texture: Texture;
    font: FontAtlas;
    shader: Shader;

    constructor(gl: WebGLRenderingContext, fontTexture: Texture, font: FontAtlas, shader: Shader) {
        this.texture = fontTexture;
        this.font = font;
        this.shader = shader;
    }

    build(gl: WebGLRenderingContext, transform?: UniformMatrix3fv): Label {
        return new Label(gl, this.shader, this.texture, this.font, transform);
    }
}

export class Label {
    inner: DefaultRenderable;

    font: FontAtlas;
    charAtlas: {[unicodeNumber: number]: Glyph};

    constructor(gl: WebGLRenderingContext, shader: Shader, tex: Texture, font: FontAtlas, transform: UniformMatrix3fv) {
        this.font = font;
        this.charAtlas = {}
        this.font.glyphs.forEach((glyph) => {
            this.charAtlas[glyph.unicode] = glyph;
        });

        const uniforms = {
            "u_trans": transform,
            "u_trans_next": transform,
            "u_fgColor": new Uniform4f([1.0, 1.0, 1.0, 1.0]),
            "u_bgColor": new Uniform4f([0.0, 0.0, 0.0, 1.0]),
            "u_distanceRange": new Uniform1f(font.atlas.distanceRange),
            "u_glyphSize": new Uniform1f(font.atlas.size),
        };
        const ib = new IndexBuffer(gl, []);
        const vb_pos = new VertexBuffer(gl, []);
        const vb_tex = new VertexBuffer(gl, []);

        const layout_pos = new VertexBufferLayout();
        layout_pos.push(gl.FLOAT, 2, 4, "a_position");

        const layout_tex = new VertexBufferLayout();
        layout_tex.push(gl.FLOAT, 2, 4, "a_texCoord");

        const vao = new VertexArray();
        vao.addBuffer(vb_pos, layout_pos);
        vao.addBuffer(vb_tex, layout_tex);

        this.inner = new DefaultRenderable(ib, vao, shader, [tex], uniforms);
    }

    getRenderable(): DefaultRenderable {
        return this.inner;
    }

    setText(gl: WebGLRenderingContext, text: string, h_align = Align.Begin, v_align = Align.Begin) {
        const idxs = [];
        const verts_pos = [];
        const verts_tex = [];

        let xPos = 0;
        let yPos = 0;
        switch (v_align) {
            case Align.Begin:
                yPos = -1;
                break;
            case Align.End:
                yPos = 0;
                break;
            case Align.Middle:
                yPos = -0.5;
                break;
        }

        // track position in the index buffer
        let bufPos = 0;
        for (let charIndex = 0; charIndex < text.length; charIndex++) {
            let char = this.charAtlas[text.charCodeAt(charIndex)]
            if (char.atlasBounds && char.planeBounds) {
                verts_pos.push(xPos + char.planeBounds.left, yPos-char.planeBounds.top);
                verts_pos.push(xPos + char.planeBounds.right, yPos-char.planeBounds.top);
                verts_pos.push(xPos + char.planeBounds.left, yPos-char.planeBounds.bottom);
                verts_pos.push(xPos + char.planeBounds.right, yPos-char.planeBounds.bottom);

                const atlasWidth = this.font.atlas.width;
                const atlasHeight = this.font.atlas.height;

                verts_tex.push(char.atlasBounds.left / atlasWidth, char.atlasBounds.top / atlasHeight);
                verts_tex.push(char.atlasBounds.right / atlasWidth, char.atlasBounds.top / atlasHeight);
                verts_tex.push(char.atlasBounds.left / atlasWidth, char.atlasBounds.bottom / atlasHeight);
                verts_tex.push(char.atlasBounds.right / atlasWidth, char.atlasBounds.bottom / atlasHeight);
                
                idxs.push(bufPos+0, bufPos+1, bufPos+2);
                idxs.push(bufPos+1, bufPos+2, bufPos+3);
                bufPos += 4;
            }
            xPos += char.advance;
        }

        let shift = 0;
        switch (h_align) {
            case Align.End:
                shift = xPos;
                break;
            case Align.Middle:
                shift = xPos / 2;
                break;
        }

        for (let i = 0; i < verts_pos.length; i += 2) {
            verts_pos[i] -= shift;
        }


        this.inner.updateIndexBuffer(gl, idxs);
        this.inner.updateVAOBuffer(gl, 0, verts_pos);
        this.inner.updateVAOBuffer(gl, 1, verts_tex);
    }
}

export function defaultMsdfLabelFactory(gl: WebGLRenderingContext, fontTexture: Texture, shader: Shader): MsdfLabelFactory {
    return new MsdfLabelFactory(gl, fontTexture, robotoMsdfJson, shader);
}