/
RichLabelBitmap.java
727 lines (648 loc) · 24 KB
/
RichLabelBitmap.java
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/****************************************************************************
Author: Luma (stubma@gmail.com)
https://github.com/stubma/cocos2dx-common
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.lib;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout.Alignment;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
public class RichLabelBitmap {
/* The values are the same as cocos2dx/platform/CCImage.h. */
private static final int HORIZONTALALIGN_LEFT = 1;
private static final int HORIZONTALALIGN_RIGHT = 2;
private static final int HORIZONTALALIGN_CENTER = 3;
private static final int VERTICALALIGN_TOP = 1;
private static final int VERTICALALIGN_BOTTOM = 2;
private static final int VERTICALALIGN_CENTER = 3;
private static final char TAG_START = '[';
private static final char TAG_END = ']';
private enum SpanType {
UNKNOWN,
COLOR,
FONT,
SIZE,
BOLD,
ITALIC,
UNDERLINE
}
// span info
final static class Span {
public SpanType type;
public boolean close;
public int pos;
// only for color
public int color;
// only for size
public float fontSize;
// only for font
public String fontName;
}
// tag parse result
final static class TagParseResult {
SpanType type;
boolean close;
int endTagPos;
int dataStart;
int dataEnd;
}
// tag parse state
private enum TagParseState {
READY,
START_TAG,
CLOSE_TAG,
EQUAL,
SUCCESS,
FAIL
}
static class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
public static void createRichLabelBitmap(String pString, final String pFontName, final int pFontSize,
final float fontTintR, final float fontTintG, final float fontTintB, final int pAlignment,
final int pWidth, final int pHeight, final boolean shadow, final float shadowDX, final float shadowDY,
final int shadowColor, final float shadowBlur, final boolean stroke, final float strokeR, final float strokeG,
final float strokeB, final float strokeSize, float contentScaleFactor) {
// extract span info and return text without span style
List<Span> spans = new ArrayList<Span>();
String plain = buildSpan(pString, spans);
// alignment
int horizontalAlignment = pAlignment & 0x0F;
int verticalAlignment = (pAlignment >> 4) & 0x0F;
Alignment align = Alignment.ALIGN_NORMAL;
switch(horizontalAlignment) {
case HORIZONTALALIGN_CENTER:
align = Alignment.ALIGN_CENTER;
break;
case HORIZONTALALIGN_RIGHT:
align = Alignment.ALIGN_OPPOSITE;
break;
}
// create paint
TextPaint paint = new TextPaint();
paint.setColor(Color.WHITE);
paint.setTextSize(pFontSize);
paint.setAntiAlias(true);
if (pFontName.endsWith(".ttf")) {
try {
Typeface typeFace = Cocos2dxTypefaces.get(Cocos2dxActivity.getContext(), pFontName);
paint.setTypeface(typeFace);
} catch (final Exception e) {
Log.e("ColorLabelBitmap", "error to create ttf type face: " + pFontName);
/* The file may not find, use system font. */
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
} else {
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
// shadow
// shadowBlur can be zero in android Paint, so set a min value to 1
if (shadow) {
paint.setShadowLayer(Math.max(shadowBlur, 1), shadowDX, shadowDY, shadowColor);
}
// compute the padding needed by shadow and stroke
float shadowStrokePaddingX = 0.0f;
float shadowStrokePaddingY = 0.0f;
if (stroke) {
shadowStrokePaddingX = (float)Math.ceil(strokeSize);
shadowStrokePaddingY = (float)Math.ceil(strokeSize);
}
if (shadow) {
shadowStrokePaddingX = Math.max(shadowStrokePaddingX, (float)Math.abs(shadowDX));
shadowStrokePaddingY = Math.max(shadowStrokePaddingY, (float)Math.abs(shadowDY));
}
// stack of color and font
int defaultColor = 0xff000000
| ((int)(fontTintR * 255) << 16)
| ((int)(fontTintG * 255) << 8)
| (int)(fontTintB * 255);
Typeface defaultFont = Typeface.DEFAULT;
Stack<Integer> colorStack = new Stack<Integer>();
Stack<Typeface> fontStack = new Stack<Typeface>();
Stack<Float> fontSizeStack = new Stack<Float>();
colorStack.push(defaultColor);
fontStack.push(defaultFont);
fontSizeStack.push(pFontSize / contentScaleFactor);
// build spannable string
int colorStart = 0;
int fontStart = 0;
int sizeStart = 0;
int underlineStart = -1;
SpannableString rich = new SpannableString(plain);
for(Span span : spans) {
if(span.close) {
switch(span.type) {
case COLOR:
{
int curColor = colorStack.pop();
if(span.pos > colorStart) {
rich.setSpan(new ForegroundColorSpan(curColor),
colorStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// start need to be reset
colorStart = span.pos;
}
break;
}
case FONT:
case BOLD:
case ITALIC:
{
// set previous span
Typeface font = fontStack.pop();
if(span.pos > fontStart && font != null) {
rich.setSpan(new CustomTypefaceSpan("", font),
fontStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fontStart = span.pos;
}
break;
}
case SIZE:
{
float size = fontSizeStack.pop();
if(span.pos > sizeStart) {
rich.setSpan(new AbsoluteSizeSpan((int)(size * contentScaleFactor)),
sizeStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sizeStart = span.pos;
}
break;
}
case UNDERLINE:
{
if(underlineStart > -1) {
rich.setSpan(new UnderlineSpan(),
underlineStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
underlineStart = -1;
}
break;
}
}
} else {
switch(span.type) {
case COLOR:
{
// process previous run
if(span.pos > colorStart) {
int curColor = colorStack.peek();
rich.setSpan(new ForegroundColorSpan(curColor),
colorStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// start need to be reset
colorStart = span.pos;
}
// push color
colorStack.push(span.color);
break;
}
case FONT:
{
// set previous span
Typeface curFont = fontStack.peek();
if(span.pos > fontStart) {
rich.setSpan(new CustomTypefaceSpan("", curFont),
fontStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fontStart = span.pos;
}
// create the font
Typeface font = null;
if (span.fontName.endsWith(".ttf")) {
try {
font = Cocos2dxTypefaces.get(Cocos2dxActivity.getContext(), span.fontName);
} catch (final Exception e) {
}
} else {
font = Typeface.create(span.fontName, Typeface.NORMAL);
if(font == null) {
font = Typeface.DEFAULT;
}
}
fontStack.push(font);
break;
}
case SIZE:
{
// set previous span
if(span.pos > sizeStart) {
float size = fontSizeStack.peek();
rich.setSpan(new AbsoluteSizeSpan((int)(size * contentScaleFactor)),
fontStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sizeStart = span.pos;
}
// push new size
fontSizeStack.push(span.fontSize);
break;
}
case BOLD:
{
// set previous span
Typeface curFont = fontStack.peek();
if(span.pos > fontStart) {
rich.setSpan(new CustomTypefaceSpan("", curFont),
fontStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fontStart = span.pos;
}
// create new font
Typeface font = Typeface.create(curFont, Typeface.BOLD | curFont.getStyle());
fontStack.push(font);
break;
}
case ITALIC:
{
// set previous span
Typeface curFont = fontStack.peek();
if(span.pos > fontStart) {
rich.setSpan(new CustomTypefaceSpan("", curFont),
fontStart,
span.pos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fontStart = span.pos;
}
// create new font
Typeface font = Typeface.create(curFont, Typeface.ITALIC | curFont.getStyle());
fontStack.push(font);
break;
}
case UNDERLINE:
{
underlineStart = span.pos;
break;
}
}
}
}
// last segment
if(plain.length() > colorStart) {
int curColor = colorStack.peek();
rich.setSpan(new ForegroundColorSpan(curColor),
colorStart,
plain.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if(plain.length() > sizeStart) {
float size = fontSizeStack.peek();
rich.setSpan(new AbsoluteSizeSpan((int)(size * contentScaleFactor)),
fontStart,
plain.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if(plain.length() > fontStart) {
Typeface curFont = fontStack.peek();
rich.setSpan(new CustomTypefaceSpan("", curFont),
fontStart,
plain.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// layout this text
StaticLayout layout = new StaticLayout(rich,
paint,
pWidth <= 0 ? (int)StaticLayout.getDesiredWidth(rich, paint) : pWidth,
align,
1,
0,
false);
// size of layout
int width = layout.getWidth();
int height = layout.getHeight();
// add padding of stroke
width += shadowStrokePaddingX;
height += shadowStrokePaddingY;
// text origin
int startY = 0;
int startX = 0;
if (pHeight > height) {
// vertical alignment
if (verticalAlignment == VERTICALALIGN_TOP) {
startY = 0;
} else if (verticalAlignment == VERTICALALIGN_CENTER) {
startY = (pHeight - height) / 2;
} else {
startY = pHeight - height;
}
}
if(shadow) {
if(shadowDY < 0) {
startY -= shadowDY;
}
if(shadowDX < 0) {
startX -= shadowDX;
}
}
// adjust layout
if(pWidth > 0 && pWidth > width)
width = pWidth;
if(pHeight > 0 && pHeight > height)
height = pHeight;
// create bitmap and canvas
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
// translate for vertical alignment
c.translate(startX, startY);
// draw text
layout.draw(c);
// draw again if stroke is enabled
if(stroke) {
// reset paint to stroke mode
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeSize);
paint.setARGB(255, (int)strokeR * 255, (int)strokeG * 255, (int)strokeB * 255);
paint.clearShadowLayer();
// clear color and underline span
for(ForegroundColorSpan span : rich.getSpans(0, rich.length(), ForegroundColorSpan.class)) {
rich.removeSpan(span);
}
for(UnderlineSpan span : rich.getSpans(0, rich.length(), UnderlineSpan.class)) {
rich.removeSpan(span);
}
layout.draw(c);
}
// transfer bitmap data to native layer, and release bitmap when done
initNativeObject(bitmap);
bitmap.recycle();
}
// if parse failed, endTagPos will be len, otherwise it is end tag position
private static TagParseResult checkTag(String p, int start) {
TagParseResult r = new TagParseResult();
r.type = SpanType.UNKNOWN;
TagParseState state = TagParseState.READY;
int tagNameStart = 0, tagNameEnd = 0;
r.close = false;
int len = p.length();
r.endTagPos = len;
r.dataStart = -1;
int i = start;
while(i < len) {
switch (state) {
case READY:
if(p.charAt(i) == TAG_START) {
state = TagParseState.START_TAG;
tagNameStart = ++i;
} else {
state = TagParseState.FAIL;
}
break;
case START_TAG:
if((i == start + 1) && p.charAt(i) == '/') {
state = TagParseState.CLOSE_TAG;
r.close = true;
tagNameStart = ++i;
} else if(p.charAt(i) == '=') {
state = TagParseState.EQUAL;
tagNameEnd = i++;
r.type = checkTagName(p, tagNameStart, tagNameEnd);
r.dataStart = i;
} else if(p.charAt(i) == TAG_END) {
state = TagParseState.SUCCESS;
r.endTagPos = i;
r.dataEnd = i;
tagNameEnd = i;
if(r.type == SpanType.UNKNOWN) {
r.type = checkTagName(p, tagNameStart, tagNameEnd);
}
} else {
i++;
}
break;
case CLOSE_TAG:
if(p.charAt(i) == TAG_END) {
state = TagParseState.SUCCESS;
r.endTagPos = i;
tagNameEnd = i;
r.type = checkTagName(p, tagNameStart, tagNameEnd);
} else {
i++;
}
break;
case EQUAL:
if(p.charAt(i) == TAG_END) {
state = TagParseState.SUCCESS;
r.endTagPos = i;
r.dataEnd = i;
} else {
i++;
}
break;
default:
break;
}
if(state == TagParseState.FAIL || state == TagParseState.SUCCESS)
break;
}
if(state != TagParseState.SUCCESS)
r.type = SpanType.UNKNOWN;
return r;
}
private static SpanType checkTagName(String p, int start, int end) {
int len = end - start;
switch(len) {
case 1:
if(p.charAt(start) == 'b') {
return SpanType.BOLD;
} else if(p.charAt(start) == 'i') {
return SpanType.ITALIC;
} else if(p.charAt(start) == 'u') {
return SpanType.UNDERLINE;
}
break;
case 4:
if(p.charAt(start) == 'f' &&
p.charAt(start + 1) == 'o' &&
p.charAt(start + 2) == 'n' &&
p.charAt(start + 3) == 't') {
return SpanType.FONT;
} else if(p.charAt(start) == 's' &&
p.charAt(start + 1) == 'i' &&
p.charAt(start + 2) == 'z' &&
p.charAt(start + 3) == 'e') {
return SpanType.SIZE;
}
case 5:
if(p.charAt(start) == 'c' &&
p.charAt(start + 1) == 'o' &&
p.charAt(start + 2) == 'l' &&
p.charAt(start + 3) == 'o' &&
p.charAt(start + 4) == 'r') {
return SpanType.COLOR;
}
break;
}
return SpanType.UNKNOWN;
}
private static String buildSpan(String text, List<Span> spans) {
int uniLen = text.length();
StringBuilder plain = new StringBuilder();
for(int i = 0; i < uniLen; i++) {
char c = text.charAt(i);
switch(c) {
case '\\':
if(i < uniLen - 1) {
char cc = text.charAt(i + 1);
if(cc == '[' || cc == ']') {
plain.append(cc);
i++;
}
} else {
plain.append(c);
}
break;
case TAG_START:
{
// parse the tag
Span span = new Span();
TagParseResult r = checkTag(text, i);
// fill span info
do {
// if type is unknown, discard
if(r.type == SpanType.UNKNOWN)
break;
// parse span data
span.type = r.type;
span.close = r.close;
span.pos = plain.length();
if(!r.close) {
switch(span.type) {
case COLOR:
span.color = parseColor(text, r.dataStart, r.dataEnd - r.dataStart);
break;
case FONT:
span.fontName = text.substring(r.dataStart, r.dataEnd);
break;
case SIZE:
try {
span.fontSize = Integer.parseInt(text.substring(r.dataStart, r.dataEnd));
} catch (NumberFormatException e) {
span.fontSize = 16;
}
break;
default:
break;
}
}
// add span
spans.add(span);
// set i pos
i = r.endTagPos;
} while(false);
break;
}
case TAG_END:
// just discard it
break;
default:
plain.append(c);
break;
}
}
// return plain str
return plain.toString();
}
private static int parseColor(String text, int start, int len) {
int color = 0;
int end = start + len;
for(int i = start; i < end; i++) {
color <<= 4;
char c = text.charAt(i);
if(c >= '0' && c <= '9') {
color |= c - '0';
} else if(c >= 'a' && c <= 'f') {
color |= c - 'a' + 10;
} else if(c >= 'A' && c <= 'F') {
color |= c - 'A' + 10;
}
}
return color;
}
private static byte[] getPixels(final Bitmap pBitmap) {
if (pBitmap != null) {
final byte[] pixels = new byte[pBitmap.getWidth()
* pBitmap.getHeight() * 4];
final ByteBuffer buf = ByteBuffer.wrap(pixels);
buf.order(ByteOrder.nativeOrder());
pBitmap.copyPixelsToBuffer(buf);
return pixels;
}
return null;
}
private static void initNativeObject(final Bitmap pBitmap) {
final byte[] pixels = getPixels(pBitmap);
if (pixels == null) {
return;
}
nativeInitBitmapDC(pBitmap.getWidth(), pBitmap.getHeight(), pixels);
}
private static native void nativeInitBitmapDC(int pWidth, int pHeight, byte[] pPixels);
}