aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/font/LetterQuad.java
blob: 09f0e2431fa2934ca1b65c7c619fcc8371fb8e02 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package com.jme3.font;

import com.jme3.math.ColorRGBA;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * LetterQuad contains the position, color, uv texture information for a character in text.
 * @author YongHoon
 */
class LetterQuad {
    private static final Rectangle UNBOUNDED = new Rectangle(0, 0, Float.MAX_VALUE, Float.MAX_VALUE);
    private static final float LINE_DIR = -1;

    private final BitmapFont font;
    private final char c;
    private final int index;
    private int style;

    private BitmapCharacter bitmapChar = null;
    private float x0 = Integer.MIN_VALUE;
    private float y0 = Integer.MIN_VALUE;
    private float width = Integer.MIN_VALUE;
    private float height = Integer.MIN_VALUE;
    private float xAdvance = 0;
    private float u0;
    private float v0;
    private float u1;
    private float v1;
    private float lineY;
    private boolean eol;

    private LetterQuad previous;
    private LetterQuad next;
    private int colorInt = 0xFFFFFFFF;

    private boolean rightToLeft;
    private float alignX;
    private float alignY;
    private float sizeScale = 1;
    
    /**
     * create head / tail
     * @param font
     * @param rightToLeft
     */
    protected LetterQuad(BitmapFont font, boolean rightToLeft) {
        this.font = font;
        this.c = Character.MIN_VALUE;
        this.rightToLeft = rightToLeft;
        this.index = -1;
        setBitmapChar(null);
    }

    /**
     * create letter and append to previous LetterQuad
     * 
     * @param c
     * @param prev previous character
     */
    protected LetterQuad(char c, LetterQuad prev) {
        this.font = prev.font;
        this.rightToLeft = prev.rightToLeft;
        this.c = c;
        this.index = prev.index+1;
        this.eol = isLineFeed();
        setBitmapChar(c);
        prev.insert(this);
    }
    
    LetterQuad addNextCharacter(char c) {
        LetterQuad n = new LetterQuad(c, this);
        return n;
    }

    BitmapCharacter getBitmapChar() {
        return bitmapChar;
    }
    
    char getChar() {
        return c;
    }
    
    int getIndex() {
        return index;
    }

    private Rectangle getBound(StringBlock block) {
        if (block.getTextBox() != null) {
            return block.getTextBox();
        }
        return UNBOUNDED;
    }

    LetterQuad getPrevious() {
        return previous;
    }

    LetterQuad getNext() {
        return next;
    }

    public float getU0() {
        return u0;
    }

    float getU1() {
        return u1;
    }

    float getV0() {
        return v0;
    }

    float getV1() {
        return v1;
    }
    
    boolean isInvalid() {
        return x0 == Integer.MIN_VALUE;
    }

    boolean isInvalid(StringBlock block) {
        return isInvalid(block, 0);
    }
    
    boolean isInvalid(StringBlock block, float gap) {
        if (isHead() || isTail())
            return false;
        if (x0 == Integer.MIN_VALUE || y0 == Integer.MIN_VALUE) {
            return true;
        }
        Rectangle bound = block.getTextBox();
        if (bound == null) {
            return false;
        }
        return x0 > 0 && bound.x+bound.width-gap < getX1();
    }
    
    float getX0() {
        return x0;
    }

    float getX1() {
        return x0+width;
    }
    
    float getNextX() {
        return x0+xAdvance;
    }
    
    float getNextLine() {
        return lineY+LINE_DIR*font.getCharSet().getLineHeight() * sizeScale;
    }

    float getY0() {
        return y0;
    }

    float getY1() {
        return y0-height;
    }
    
    float getWidth() {
        return width;
    }
    
    float getHeight() {
        return height;
    }

    void insert(LetterQuad ins) {
        LetterQuad n = next;
        next = ins;
        ins.next = n;
        ins.previous = this;
        n.previous = ins;
    }
    
    void invalidate() {
        eol = isLineFeed();
        setBitmapChar(font.getCharSet().getCharacter(c, style));
    }

    boolean isTail() {
        return next == null;
    }

    boolean isHead() {
        return previous == null;
    }

    /**
     * @return next letter
     */
    LetterQuad remove() {
        this.previous.next = next;
        this.next.previous = previous;
        return next;
    }

    void setPrevious(LetterQuad before) {
        this.previous = before;
    }
    
    void setStyle(int style) {
        this.style = style;
        invalidate();
    }
    
    void setColor(ColorRGBA color) {
        this.colorInt = color.asIntRGBA();
        invalidate();
    }

    void setBitmapChar(char c) {
        BitmapCharacterSet charSet = font.getCharSet();
        BitmapCharacter bm = charSet.getCharacter(c, style);
        setBitmapChar(bm);
    }
    
    void setBitmapChar(BitmapCharacter bitmapChar) {
        x0 = Integer.MIN_VALUE;
        y0 = Integer.MIN_VALUE;
        width = Integer.MIN_VALUE;
        height = Integer.MIN_VALUE;
        alignX = 0;
        alignY = 0;
        
        BitmapCharacterSet charSet = font.getCharSet();
        this.bitmapChar = bitmapChar;
        if (bitmapChar != null) {
            u0 = (float) bitmapChar.getX() / charSet.getWidth();
            v0 = (float) bitmapChar.getY() / charSet.getHeight();
            u1 = u0 + (float) bitmapChar.getWidth() / charSet.getWidth();
            v1 = v0 + (float) bitmapChar.getHeight() / charSet.getHeight();
        } else {
            u0 = 0;
            v0 = 0;
            u1 = 0;
            v1 = 0;
        }
    }

    void setNext(LetterQuad next) {
        this.next = next;
    }

    void update(StringBlock block) {
        final float[] tabs = block.getTabPosition();
        final float tabWidth = block.getTabWidth();
        final Rectangle bound = getBound(block);
        sizeScale = block.getSize() / font.getCharSet().getRenderedSize();
        lineY = computeLineY(block);

        if (isHead()) {
            x0 = getBound(block).x;
            y0 = lineY;
            width = 0;
            height = 0;
            xAdvance = 0;
        } else if (isTab()) {
            x0 = previous.getNextX();
            width = tabWidth;
            y0 = lineY;
            height = 0;
            if (tabs != null && x0 < tabs[tabs.length-1]) {
                for (int i = 0; i < tabs.length-1; i++) {
                    if (x0 > tabs[i] && x0 < tabs[i+1]) {
                        width = tabs[i+1] - x0;
                    }
                }
            }
            xAdvance = width;
        } else if (bitmapChar == null) {
            x0 = getPrevious().getX1();
            y0 = lineY;
            width = 0;
            height = 0;
            xAdvance = 0;
        } else {
            float xOffset = bitmapChar.getXOffset() * sizeScale;
            float yOffset = bitmapChar.getYOffset() * sizeScale;
            xAdvance = bitmapChar.getXAdvance() * sizeScale;
            width = bitmapChar.getWidth() * sizeScale;
            height = bitmapChar.getHeight() * sizeScale;
            float incrScale = rightToLeft ? -1f : 1f;
            float kernAmount = 0f;

            if (previous.isHead() || previous.eol) {
                x0 = bound.x;
                
                // The first letter quad will be drawn right at the first
                // position... but it does not offset by the characters offset
                // amount.  This means that we've potentially accumulated extra
                // pixels and the next letter won't get drawn far enough unless
                // we add this offset back into xAdvance.. by subtracting it.
                // This is the same thing that's done below because we've
                // technically baked the offset in just like below.  It doesn't
                // look like it at first glance so I'm keeping it separate with
                // this comment.
                xAdvance -= xOffset * incrScale; 
                
            } else {
                x0 = previous.getNextX() + xOffset * incrScale;
                
                // Since x0 will have offset baked into it then we
                // need to counteract that in xAdvance.  This is better
                // than removing it in getNextX() because we also need
                // to take kerning into account below... which will also
                // get baked in.
                // Without this, getNextX() will return values too far to
                // the left, for example.
                xAdvance -= xOffset * incrScale; 
            }
            y0 = lineY + LINE_DIR*yOffset;

            // Adjust for kerning
            BitmapCharacter lastChar = previous.getBitmapChar();
            if (lastChar != null && block.isKerning()) {
                kernAmount = lastChar.getKerning(c) * sizeScale;
                x0 += kernAmount * incrScale;
                
                // Need to unbake the kerning from xAdvance since it
                // is baked into x0... see above.
                //xAdvance -= kernAmount * incrScale;
                // No, kerning is an inter-character spacing and _does_ affect
                // all subsequent cursor positions. 
            }
        }
        if (isEndOfLine()) {
            xAdvance = bound.x-x0;
        }
    }
    
    /**
     * add temporary linewrap indicator
     */
    void setEndOfLine() {
        this.eol = true;
    }
    
    boolean isEndOfLine() {
        return eol;
    }
    
    boolean isLineWrap() {
        return !isHead() && !isTail() && bitmapChar == null && c == Character.MIN_VALUE;
    }
    
    private float computeLineY(StringBlock block) {
        if (isHead()) {
            return getBound(block).y;
        } else if (previous.eol) {
            return previous.getNextLine();
        } else {
            return previous.lineY;
        }
    }

    
    boolean isLineStart() {
        return x0 == 0 || (previous != null && previous.eol);
    }
    
    boolean isBlank() {
        return c == ' ' || isTab();
    }
    
    public void storeToArrays(float[] pos, float[] tc, short[] idx, byte[] colors, int quadIdx){
        float x = x0+alignX;
        float y = y0-alignY;
        float xpw = x+width;
        float ymh = y-height;

        pos[0] = x;   pos[1]  = y;   pos[2]  = 0;
        pos[3] = x;   pos[4]  = ymh; pos[5]  = 0;
        pos[6] = xpw; pos[7]  = ymh; pos[8]  = 0;
        pos[9] = xpw; pos[10] = y;   pos[11] = 0;

        float v0 = 1f - this.v0;
        float v1 = 1f - this.v1;

        tc[0] = u0; tc[1] = v0;
        tc[2] = u0; tc[3] = v1;
        tc[4] = u1; tc[5] = v1;
        tc[6] = u1; tc[7] = v0;

        colors[3] = (byte) (colorInt & 0xff);
        colors[2] = (byte) ((colorInt >> 8) & 0xff);
        colors[1] = (byte) ((colorInt >> 16) & 0xff);
        colors[0] = (byte) ((colorInt >> 24) & 0xff);
        System.arraycopy(colors, 0, colors, 4,  4);
        System.arraycopy(colors, 0, colors, 8,  4);
        System.arraycopy(colors, 0, colors, 12, 4);

        short i0 = (short) (quadIdx * 4);
        short i1 = (short) (i0 + 1);
        short i2 = (short) (i0 + 2);
        short i3 = (short) (i0 + 3);

        idx[0] = i0; idx[1] = i1; idx[2] = i2;
        idx[3] = i0; idx[4] = i2; idx[5] = i3;
    }
    
    public void appendPositions(FloatBuffer fb){
        float sx = x0+alignX;
        float sy = y0-alignY;
        float ex = sx+width;
        float ey = sy-height;
        // NOTE: subtracting the height here
        // because OGL's Ortho origin is at lower-left
        fb.put(sx).put(sy).put(0f);
        fb.put(sx).put(ey).put(0f);
        fb.put(ex).put(ey).put(0f);
        fb.put(ex).put(sy).put(0f);
    }

    public void appendPositions(ShortBuffer sb){
        final float x1 = getX1();
        final float y1 = getY1();
        short x = (short) x0;
        short y = (short) y0;
        short xpw = (short) (x1);
        short ymh = (short) (y1);
        
        sb.put(x).put(y).put((short)0);
        sb.put(x).put(ymh).put((short)0);
        sb.put(xpw).put(ymh).put((short)0);
        sb.put(xpw).put(y).put((short)0);
    }

    public void appendTexCoords(FloatBuffer fb){
        // flip coords to be compatible with OGL
        float v0 = 1 - this.v0;
        float v1 = 1 - this.v1;

        // upper left
        fb.put(u0).put(v0);
        // lower left
        fb.put(u0).put(v1);
        // lower right
        fb.put(u1).put(v1);
        // upper right
        fb.put(u1).put(v0);
    }

    public void appendColors(ByteBuffer bb){
        bb.putInt(colorInt);
        bb.putInt(colorInt);
        bb.putInt(colorInt);
        bb.putInt(colorInt);
    }

    public void appendIndices(ShortBuffer sb, int quadIndex){
        // each quad has 4 indices
        short v0 = (short) (quadIndex * 4);
        short v1 = (short) (v0 + 1);
        short v2 = (short) (v0 + 2);
        short v3 = (short) (v0 + 3);

        sb.put(v0).put(v1).put(v2);
        sb.put(v0).put(v2).put(v3);
//        sb.put(new short[]{ v0, v1, v2,
//                            v0, v2, v3 });
    }


    @Override
    public String toString() {
        return String.valueOf(c);
    }

    void setAlignment(float alignX, float alignY) {
        this.alignX = alignX;
        this.alignY = alignY;
    }

    float getAlignX() {
        return alignX;
    }

    float getAlignY() {
        return alignY;
    }

    boolean isLineFeed() {
        return c == '\n';
    }
    
    boolean isTab() {
        return c == '\t';
    }
    
}