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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.SpannedString;
import android.text.TextUtils;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A paint implementation overridden by the LayoutLib bridge.
*/
public class Paint extends _Original_Paint {
private int mColor = 0xFFFFFFFF;
private float mStrokeWidth = 1.f;
private float mTextSize = 20;
private float mScaleX = 1;
private float mSkewX = 0;
private Align mAlign = Align.LEFT;
private Style mStyle = Style.FILL;
private float mStrokeMiter = 4.0f;
private Cap mCap = Cap.BUTT;
private Join mJoin = Join.MITER;
private int mFlags = 0;
/**
* Class associating a {@link Font} and it's {@link java.awt.FontMetrics}.
*/
public static final class FontInfo {
Font mFont;
java.awt.FontMetrics mMetrics;
}
private List<FontInfo> mFonts;
private final FontRenderContext mFontContext = new FontRenderContext(
new AffineTransform(), true, true);
@SuppressWarnings("hiding")
public static final int ANTI_ALIAS_FLAG = _Original_Paint.ANTI_ALIAS_FLAG;
@SuppressWarnings("hiding")
public static final int FILTER_BITMAP_FLAG = _Original_Paint.FILTER_BITMAP_FLAG;
@SuppressWarnings("hiding")
public static final int DITHER_FLAG = _Original_Paint.DITHER_FLAG;
@SuppressWarnings("hiding")
public static final int UNDERLINE_TEXT_FLAG = _Original_Paint.UNDERLINE_TEXT_FLAG;
@SuppressWarnings("hiding")
public static final int STRIKE_THRU_TEXT_FLAG = _Original_Paint.STRIKE_THRU_TEXT_FLAG;
@SuppressWarnings("hiding")
public static final int FAKE_BOLD_TEXT_FLAG = _Original_Paint.FAKE_BOLD_TEXT_FLAG;
@SuppressWarnings("hiding")
public static final int LINEAR_TEXT_FLAG = _Original_Paint.LINEAR_TEXT_FLAG;
@SuppressWarnings("hiding")
public static final int SUBPIXEL_TEXT_FLAG = _Original_Paint.SUBPIXEL_TEXT_FLAG;
@SuppressWarnings("hiding")
public static final int DEV_KERN_TEXT_FLAG = _Original_Paint.DEV_KERN_TEXT_FLAG;
public static class FontMetrics extends _Original_Paint.FontMetrics {
}
public static class FontMetricsInt extends _Original_Paint.FontMetricsInt {
}
/**
* The Style specifies if the primitive being drawn is filled,
* stroked, or both (in the same color). The default is FILL.
*/
public enum Style {
/**
* Geometry and text drawn with this style will be filled, ignoring all
* stroke-related settings in the paint.
*/
FILL (0),
/**
* Geometry and text drawn with this style will be stroked, respecting
* the stroke-related fields on the paint.
*/
STROKE (1),
/**
* Geometry and text drawn with this style will be both filled and
* stroked at the same time, respecting the stroke-related fields on
* the paint.
*/
FILL_AND_STROKE (2);
Style(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
/**
* The Cap specifies the treatment for the beginning and ending of
* stroked lines and paths. The default is BUTT.
*/
public enum Cap {
/**
* The stroke ends with the path, and does not project beyond it.
*/
BUTT (0),
/**
* The stroke projects out as a square, with the center at the end
* of the path.
*/
ROUND (1),
/**
* The stroke projects out as a semicircle, with the center at the
* end of the path.
*/
SQUARE (2);
private Cap(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
/**
* The Join specifies the treatment where lines and curve segments
* join on a stroked path. The default is MITER.
*/
public enum Join {
/**
* The outer edges of a join meet at a sharp angle
*/
MITER (0),
/**
* The outer edges of a join meet in a circular arc.
*/
ROUND (1),
/**
* The outer edges of a join meet with a straight line
*/
BEVEL (2);
private Join(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
/**
* Align specifies how drawText aligns its text relative to the
* [x,y] coordinates. The default is LEFT.
*/
public enum Align {
/**
* The text is drawn to the right of the x,y origin
*/
LEFT (0),
/**
* The text is drawn centered horizontally on the x,y origin
*/
CENTER (1),
/**
* The text is drawn to the left of the x,y origin
*/
RIGHT (2);
private Align(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
public Paint() {
this(0);
}
public Paint(int flags) {
setFlags(flags | DEFAULT_PAINT_FLAGS);
initFont();
}
public Paint(Paint paint) {
set(paint);
initFont();
}
@Override
public void finalize() throws Throwable {
// pass
}
@Override
public void reset() {
super.reset();
}
/**
* Returns the list of {@link Font} objects. The first item is the main font, the rest
* are fall backs for characters not present in the main font.
*/
public List<FontInfo> getFonts() {
return mFonts;
}
private void initFont() {
mTypeface = Typeface.DEFAULT;
updateFontObject();
}
/**
* Update the {@link Font} object from the typeface, text size and scaling
*/
@SuppressWarnings("deprecation")
private void updateFontObject() {
if (mTypeface != null) {
// Get the fonts from the TypeFace object.
List<Font> fonts = mTypeface.getFonts();
// create new font objects as well as FontMetrics, based on the current text size
// and skew info.
ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
for (Font font : fonts) {
FontInfo info = new FontInfo();
info.mFont = font.deriveFont(mTextSize);
if (mScaleX != 1.0 || mSkewX != 0) {
// TODO: support skew
info.mFont = info.mFont.deriveFont(new AffineTransform(
mScaleX, mSkewX, 0, 0, 1, 0));
}
info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
infoList.add(info);
}
mFonts = Collections.unmodifiableList(infoList);
}
}
//----------------------------------------
public void set(Paint src) {
if (this != src) {
mColor = src.mColor;
mTextSize = src.mTextSize;
mScaleX = src.mScaleX;
mSkewX = src.mSkewX;
mAlign = src.mAlign;
mStyle = src.mStyle;
mFlags = src.mFlags;
super.set(src);
}
}
@Override
public void setCompatibilityScaling(float factor) {
super.setCompatibilityScaling(factor);
}
@Override
public int getFlags() {
return mFlags;
}
@Override
public void setFlags(int flags) {
mFlags = flags;
}
@Override
public boolean isAntiAlias() {
return super.isAntiAlias();
}
@Override
public boolean isDither() {
return super.isDither();
}
@Override
public boolean isLinearText() {
return super.isLinearText();
}
@Override
public boolean isStrikeThruText() {
return super.isStrikeThruText();
}
@Override
public boolean isUnderlineText() {
return super.isUnderlineText();
}
@Override
public boolean isFakeBoldText() {
return super.isFakeBoldText();
}
@Override
public boolean isSubpixelText() {
return super.isSubpixelText();
}
@Override
public boolean isFilterBitmap() {
return super.isFilterBitmap();
}
/**
* Return the font's recommended interline spacing, given the Paint's
* settings for typeface, textSize, etc. If metrics is not null, return the
* fontmetric values in it.
*
* @param metrics If this object is not null, its fields are filled with
* the appropriate values given the paint's text attributes.
* @return the font's recommended interline spacing.
*/
public float getFontMetrics(FontMetrics metrics) {
if (mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
if (metrics != null) {
// Android expects negative ascent so we invert the value from Java.
metrics.top = - javaMetrics.getMaxAscent();
metrics.ascent = - javaMetrics.getAscent();
metrics.descent = javaMetrics.getDescent();
metrics.bottom = javaMetrics.getMaxDescent();
metrics.leading = javaMetrics.getLeading();
}
return javaMetrics.getHeight();
}
return 0;
}
public int getFontMetricsInt(FontMetricsInt metrics) {
if (mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
if (metrics != null) {
// Android expects negative ascent so we invert the value from Java.
metrics.top = - javaMetrics.getMaxAscent();
metrics.ascent = - javaMetrics.getAscent();
metrics.descent = javaMetrics.getDescent();
metrics.bottom = javaMetrics.getMaxDescent();
metrics.leading = javaMetrics.getLeading();
}
return javaMetrics.getHeight();
}
return 0;
}
/**
* Reimplemented to return Paint.FontMetrics instead of _Original_Paint.FontMetrics
*/
public FontMetrics getFontMetrics() {
FontMetrics fm = new FontMetrics();
getFontMetrics(fm);
return fm;
}
/**
* Reimplemented to return Paint.FontMetricsInt instead of _Original_Paint.FontMetricsInt
*/
public FontMetricsInt getFontMetricsInt() {
FontMetricsInt fm = new FontMetricsInt();
getFontMetricsInt(fm);
return fm;
}
@Override
public float getFontMetrics(_Original_Paint.FontMetrics metrics) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
@Override
public int getFontMetricsInt(_Original_Paint.FontMetricsInt metrics) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
@Override
public Typeface setTypeface(Typeface typeface) {
if (typeface != null) {
mTypeface = typeface;
} else {
mTypeface = Typeface.DEFAULT;
}
updateFontObject();
return typeface;
}
@Override
public Typeface getTypeface() {
return super.getTypeface();
}
@Override
public int getColor() {
return mColor;
}
@Override
public void setColor(int color) {
mColor = color;
}
@Override
public void setARGB(int a, int r, int g, int b) {
super.setARGB(a, r, g, b);
}
@Override
public void setAlpha(int alpha) {
mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
}
@Override
public int getAlpha() {
return mColor >>> 24;
}
/**
* Set or clear the shader object.
* <p />
* Pass null to clear any previous shader.
* As a convenience, the parameter passed is also returned.
*
* @param shader May be null. the new shader to be installed in the paint
* @return shader
*/
@Override
public Shader setShader(Shader shader) {
return mShader = shader;
}
@Override
public Shader getShader() {
return super.getShader();
}
/**
* Set or clear the paint's colorfilter, returning the parameter.
*
* @param filter May be null. The new filter to be installed in the paint
* @return filter
*/
@Override
public ColorFilter setColorFilter(ColorFilter filter) {
mColorFilter = filter;
return filter;
}
@Override
public ColorFilter getColorFilter() {
return super.getColorFilter();
}
/**
* Set or clear the xfermode object.
* <p />
* Pass null to clear any previous xfermode.
* As a convenience, the parameter passed is also returned.
*
* @param xfermode May be null. The xfermode to be installed in the paint
* @return xfermode
*/
@Override
public Xfermode setXfermode(Xfermode xfermode) {
return mXfermode = xfermode;
}
@Override
public Xfermode getXfermode() {
return super.getXfermode();
}
@Override
public Rasterizer setRasterizer(Rasterizer rasterizer) {
mRasterizer = rasterizer;
return rasterizer;
}
@Override
public Rasterizer getRasterizer() {
return super.getRasterizer();
}
@Override
public void setShadowLayer(float radius, float dx, float dy, int color) {
// TODO Auto-generated method stub
}
@Override
public void clearShadowLayer() {
super.clearShadowLayer();
}
public void setTextAlign(Align align) {
mAlign = align;
}
@Override
public void setTextAlign(android.graphics._Original_Paint.Align align) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
public Align getTextAlign() {
return mAlign;
}
public void setStyle(Style style) {
mStyle = style;
}
@Override
public void setStyle(android.graphics._Original_Paint.Style style) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
public Style getStyle() {
return mStyle;
}
@Override
public void setDither(boolean dither) {
mFlags |= dither ? DITHER_FLAG : ~DITHER_FLAG;
}
@Override
public void setAntiAlias(boolean aa) {
mFlags |= aa ? ANTI_ALIAS_FLAG : ~ANTI_ALIAS_FLAG;
}
@Override
public void setFakeBoldText(boolean flag) {
mFlags |= flag ? FAKE_BOLD_TEXT_FLAG : ~FAKE_BOLD_TEXT_FLAG;
}
@Override
public void setLinearText(boolean flag) {
mFlags |= flag ? LINEAR_TEXT_FLAG : ~LINEAR_TEXT_FLAG;
}
@Override
public void setSubpixelText(boolean flag) {
mFlags |= flag ? SUBPIXEL_TEXT_FLAG : ~SUBPIXEL_TEXT_FLAG;
}
@Override
public void setUnderlineText(boolean flag) {
mFlags |= flag ? UNDERLINE_TEXT_FLAG : ~UNDERLINE_TEXT_FLAG;
}
@Override
public void setStrikeThruText(boolean flag) {
mFlags |= flag ? STRIKE_THRU_TEXT_FLAG : ~STRIKE_THRU_TEXT_FLAG;
}
@Override
public void setFilterBitmap(boolean flag) {
mFlags |= flag ? FILTER_BITMAP_FLAG : ~FILTER_BITMAP_FLAG;
}
@Override
public float getStrokeWidth() {
return mStrokeWidth;
}
@Override
public void setStrokeWidth(float width) {
mStrokeWidth = width;
}
@Override
public float getStrokeMiter() {
return mStrokeMiter;
}
@Override
public void setStrokeMiter(float miter) {
mStrokeMiter = miter;
}
@Override
public void setStrokeCap(android.graphics._Original_Paint.Cap cap) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
public void setStrokeCap(Cap cap) {
mCap = cap;
}
public Cap getStrokeCap() {
return mCap;
}
@Override
public void setStrokeJoin(android.graphics._Original_Paint.Join join) {
throw new UnsupportedOperationException("CALL TO PARENT FORBIDDEN");
}
public void setStrokeJoin(Join join) {
mJoin = join;
}
public Join getStrokeJoin() {
return mJoin;
}
@Override
public boolean getFillPath(Path src, Path dst) {
return super.getFillPath(src, dst);
}
@Override
public PathEffect setPathEffect(PathEffect effect) {
mPathEffect = effect;
return effect;
}
@Override
public PathEffect getPathEffect() {
return super.getPathEffect();
}
@Override
public MaskFilter setMaskFilter(MaskFilter maskfilter) {
mMaskFilter = maskfilter;
return maskfilter;
}
@Override
public MaskFilter getMaskFilter() {
return super.getMaskFilter();
}
/**
* Return the paint's text size.
*
* @return the paint's text size.
*/
@Override
public float getTextSize() {
return mTextSize;
}
/**
* Set the paint's text size. This value must be > 0
*
* @param textSize set the paint's text size.
*/
@Override
public void setTextSize(float textSize) {
mTextSize = textSize;
updateFontObject();
}
/**
* Return the paint's horizontal scale factor for text. The default value
* is 1.0.
*
* @return the paint's scale factor in X for drawing/measuring text
*/
@Override
public float getTextScaleX() {
return mScaleX;
}
/**
* Set the paint's horizontal scale factor for text. The default value
* is 1.0. Values > 1.0 will stretch the text wider. Values < 1.0 will
* stretch the text narrower.
*
* @param scaleX set the paint's scale in X for drawing/measuring text.
*/
@Override
public void setTextScaleX(float scaleX) {
mScaleX = scaleX;
updateFontObject();
}
/**
* Return the paint's horizontal skew factor for text. The default value
* is 0.
*
* @return the paint's skew factor in X for drawing text.
*/
@Override
public float getTextSkewX() {
return mSkewX;
}
/**
* Set the paint's horizontal skew factor for text. The default value
* is 0. For approximating oblique text, use values around -0.25.
*
* @param skewX set the paint's skew factor in X for drawing text.
*/
@Override
public void setTextSkewX(float skewX) {
mSkewX = skewX;
updateFontObject();
}
@Override
public float getFontSpacing() {
return super.getFontSpacing();
}
/**
* Return the distance above (negative) the baseline (ascent) based on the
* current typeface and text size.
*
* @return the distance above (negative) the baseline (ascent) based on the
* current typeface and text size.
*/
@Override
public float ascent() {
if (mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
// Android expects negative ascent so we invert the value from Java.
return - javaMetrics.getAscent();
}
return 0;
}
/**
* Return the distance below (positive) the baseline (descent) based on the
* current typeface and text size.
*
* @return the distance below (positive) the baseline (descent) based on
* the current typeface and text size.
*/
@Override
public float descent() {
if (mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
return javaMetrics.getDescent();
}
return 0;
}
/**
* Return the width of the text.
*
* @param text The text to measure
* @param index The index of the first character to start measuring
* @param count THe number of characters to measure, beginning with start
* @return The width of the text
*/
@Override
public float measureText(char[] text, int index, int count) {
// WARNING: the logic in this method is similar to Canvas.drawText.
// Any change to this method should be reflected in Canvas.drawText
if (mFonts.size() > 0) {
FontInfo mainFont = mFonts.get(0);
int i = index;
int lastIndex = index + count;
float total = 0f;
while (i < lastIndex) {
// always start with the main font.
int upTo = mainFont.mFont.canDisplayUpTo(text, i, lastIndex);
if (upTo == -1) {
// shortcut to exit
return total + mainFont.mMetrics.charsWidth(text, i, lastIndex - i);
} else if (upTo > 0) {
total += mainFont.mMetrics.charsWidth(text, i, upTo - i);
i = upTo;
// don't call continue at this point. Since it is certain the main font
// cannot display the font a index upTo (now ==i), we move on to the
// fallback fonts directly.
}
// no char supported, attempt to read the next char(s) with the
// fallback font. In this case we only test the first character
// and then go back to test with the main font.
// Special test for 2-char characters.
boolean foundFont = false;
for (int f = 1 ; f < mFonts.size() ; f++) {
FontInfo fontInfo = mFonts.get(f);
// need to check that the font can display the character. We test
// differently if the char is a high surrogate.
int charCount = Character.isHighSurrogate(text[i]) ? 2 : 1;
upTo = fontInfo.mFont.canDisplayUpTo(text, i, i + charCount);
if (upTo == -1) {
total += fontInfo.mMetrics.charsWidth(text, i, charCount);
i += charCount;
foundFont = true;
break;
}
}
// in case no font can display the char, measure it with the main font.
if (foundFont == false) {
int size = Character.isHighSurrogate(text[i]) ? 2 : 1;
total += mainFont.mMetrics.charsWidth(text, i, size);
i += size;
}
}
}
return 0;
}
/**
* Return the width of the text.
*
* @param text The text to measure
* @param start The index of the first character to start measuring
* @param end 1 beyond the index of the last character to measure
* @return The width of the text
*/
@Override
public float measureText(String text, int start, int end) {
return measureText(text.toCharArray(), start, end - start);
}
/**
* Return the width of the text.
*
* @param text The text to measure
* @return The width of the text
*/
@Override
public float measureText(String text) {
return measureText(text.toCharArray(), 0, text.length());
}
/*
* re-implement to call SpannableStringBuilder.measureText with a Paint object
* instead of an _Original_Paint
*/
@Override
public float measureText(CharSequence text, int start, int end) {
if (text instanceof String) {
return measureText((String)text, start, end);
}
if (text instanceof SpannedString ||
text instanceof SpannableString) {
return measureText(text.toString(), start, end);
}
if (text instanceof SpannableStringBuilder) {
return ((SpannableStringBuilder)text).measureText(start, end, this);
}
char[] buf = TemporaryBuffer.obtain(end - start);
TextUtils.getChars(text, start, end, buf, 0);
float result = measureText(buf, 0, end - start);
TemporaryBuffer.recycle(buf);
return result;
}
/**
* Measure the text, stopping early if the measured width exceeds maxWidth.
* Return the number of chars that were measured, and if measuredWidth is
* not null, return in it the actual width measured.
*
* @param text The text to measure
* @param index The offset into text to begin measuring at
* @param count The number of maximum number of entries to measure. If count
* is negative, then the characters before index are measured
* in reverse order. This allows for measuring the end of
* string.
* @param maxWidth The maximum width to accumulate.
* @param measuredWidth Optional. If not null, returns the actual width
* measured.
* @return The number of chars that were measured. Will always be <=
* abs(count).
*/
@Override
public int breakText(char[] text, int index, int count,
float maxWidth, float[] measuredWidth) {
int inc = count > 0 ? 1 : -1;
int measureIndex = 0;
float measureAcc = 0;
for (int i = index ; i != index + count ; i += inc, measureIndex++) {
int start, end;
if (i < index) {
start = i;
end = index;
} else {
start = index;
end = i;
}
// measure from start to end
float res = measureText(text, start, end - start + 1);
if (measuredWidth != null) {
measuredWidth[measureIndex] = res;
}
measureAcc += res;
if (res > maxWidth) {
// we should not return this char index, but since it's 0-based and we need
// to return a count, we simply return measureIndex;
return measureIndex;
}
}
return measureIndex;
}
/**
* Measure the text, stopping early if the measured width exceeds maxWidth.
* Return the number of chars that were measured, and if measuredWidth is
* not null, return in it the actual width measured.
*
* @param text The text to measure
* @param measureForwards If true, measure forwards, starting at index.
* Otherwise, measure backwards, starting with the
* last character in the string.
* @param maxWidth The maximum width to accumulate.
* @param measuredWidth Optional. If not null, returns the actual width
* measured.
* @return The number of chars that were measured. Will always be <=
* abs(count).
*/
@Override
public int breakText(String text, boolean measureForwards,
float maxWidth, float[] measuredWidth) {
// NOTE: javadoc doesn't match. Just a guess.
return breakText(text,
0 /* start */, text.length() /* end */,
measureForwards, maxWidth, measuredWidth);
}
/**
* Measure the text, stopping early if the measured width exceeds maxWidth.
* Return the number of chars that were measured, and if measuredWidth is
* not null, return in it the actual width measured.
*
* @param text The text to measure
* @param start The offset into text to begin measuring at
* @param end The end of the text slice to measure.
* @param measureForwards If true, measure forwards, starting at start.
* Otherwise, measure backwards, starting with end.
* @param maxWidth The maximum width to accumulate.
* @param measuredWidth Optional. If not null, returns the actual width
* measured.
* @return The number of chars that were measured. Will always be <=
* abs(end - start).
*/
@Override
public int breakText(CharSequence text, int start, int end, boolean measureForwards,
float maxWidth, float[] measuredWidth) {
return super.breakText(text, start, end, measureForwards, maxWidth, measuredWidth);
}
/**
* Return the advance widths for the characters in the string.
*
* @param text The text to measure
* @param index The index of the first char to to measure
* @param count The number of chars starting with index to measure
* @param widths array to receive the advance widths of the characters.
* Must be at least a large as count.
* @return the actual number of widths returned.
*/
@Override
public int getTextWidths(char[] text, int index, int count,
float[] widths) {
if (mFonts.size() > 0) {
if ((index | count) < 0 || index + count > text.length
|| count > widths.length) {
throw new ArrayIndexOutOfBoundsException();
}
// FIXME: handle multi-char characters.
// Need to figure out if the lengths of the width array takes into account
// multi-char characters.
for (int i = 0; i < count; i++) {
char c = text[i + index];
boolean found = false;
for (FontInfo info : mFonts) {
if (info.mFont.canDisplay(c)) {
widths[i] = info.mMetrics.charWidth(c);
found = true;
break;
}
}
if (found == false) {
// we stop there.
return i;
}
}
return count;
}
return 0;
}
/**
* Return the advance widths for the characters in the string.
*
* @param text The text to measure
* @param start The index of the first char to to measure
* @param end The end of the text slice to measure
* @param widths array to receive the advance widths of the characters.
* Must be at least a large as the text.
* @return the number of unichars in the specified text.
*/
@Override
public int getTextWidths(String text, int start, int end, float[] widths) {
if ((start | end | (end - start) | (text.length() - end)) < 0) {
throw new IndexOutOfBoundsException();
}
if (end - start > widths.length) {
throw new ArrayIndexOutOfBoundsException();
}
return getTextWidths(text.toCharArray(), start, end - start, widths);
}
/*
* re-implement to call SpannableStringBuilder.getTextWidths with a Paint object
* instead of an _Original_Paint
*/
@Override
public int getTextWidths(CharSequence text, int start, int end, float[] widths) {
if (text instanceof String) {
return getTextWidths((String)text, start, end, widths);
}
if (text instanceof SpannedString || text instanceof SpannableString) {
return getTextWidths(text.toString(), start, end, widths);
}
if (text instanceof SpannableStringBuilder) {
return ((SpannableStringBuilder)text).getTextWidths(start, end, widths, this);
}
char[] buf = TemporaryBuffer.obtain(end - start);
TextUtils.getChars(text, start, end, buf, 0);
int result = getTextWidths(buf, 0, end - start, widths);
TemporaryBuffer.recycle(buf);
return result;
}
@Override
public int getTextWidths(String text, float[] widths) {
return super.getTextWidths(text, widths);
}
/**
* Return the path (outline) for the specified text.
* Note: just like Canvas.drawText, this will respect the Align setting in
* the paint.
*
* @param text The text to retrieve the path from
* @param index The index of the first character in text
* @param count The number of characterss starting with index
* @param x The x coordinate of the text's origin
* @param y The y coordinate of the text's origin
* @param path The path to receive the data describing the text. Must
* be allocated by the caller.
*/
@Override
public void getTextPath(char[] text, int index, int count,
float x, float y, Path path) {
// TODO this is the ORIGINAL implementation. REPLACE AS NEEDED OR REMOVE
if ((index | count) < 0 || index + count > text.length) {
throw new ArrayIndexOutOfBoundsException();
}
// TODO native_getTextPath(mNativePaint, text, index, count, x, y, path.ni());
throw new UnsupportedOperationException("IMPLEMENT AS NEEDED");
}
/**
* Return the path (outline) for the specified text.
* Note: just like Canvas.drawText, this will respect the Align setting
* in the paint.
*
* @param text The text to retrieve the path from
* @param start The first character in the text
* @param end 1 past the last charcter in the text
* @param x The x coordinate of the text's origin
* @param y The y coordinate of the text's origin
* @param path The path to receive the data describing the text. Must
* be allocated by the caller.
*/
@Override
public void getTextPath(String text, int start, int end,
float x, float y, Path path) {
if ((start | end | (end - start) | (text.length() - end)) < 0) {
throw new IndexOutOfBoundsException();
}
getTextPath(text.toCharArray(), start, end - start, x, y, path);
}
/**
* Return in bounds (allocated by the caller) the smallest rectangle that
* encloses all of the characters, with an implied origin at (0,0).
*
* @param text String to measure and return its bounds
* @param start Index of the first char in the string to measure
* @param end 1 past the last char in the string measure
* @param bounds Returns the unioned bounds of all the text. Must be
* allocated by the caller.
*/
@Override
public void getTextBounds(String text, int start, int end, Rect bounds) {
if ((start | end | (end - start) | (text.length() - end)) < 0) {
throw new IndexOutOfBoundsException();
}
if (bounds == null) {
throw new NullPointerException("need bounds Rect");
}
getTextBounds(text.toCharArray(), start, end - start, bounds);
}
/**
* Return in bounds (allocated by the caller) the smallest rectangle that
* encloses all of the characters, with an implied origin at (0,0).
*
* @param text Array of chars to measure and return their unioned bounds
* @param index Index of the first char in the array to measure
* @param count The number of chars, beginning at index, to measure
* @param bounds Returns the unioned bounds of all the text. Must be
* allocated by the caller.
*/
@Override
public void getTextBounds(char[] text, int index, int count, Rect bounds) {
// FIXME
if (mFonts.size() > 0) {
if ((index | count) < 0 || index + count > text.length) {
throw new ArrayIndexOutOfBoundsException();
}
if (bounds == null) {
throw new NullPointerException("need bounds Rect");
}
FontInfo mainInfo = mFonts.get(0);
Rectangle2D rect = mainInfo.mFont.getStringBounds(text, index, index + count, mFontContext);
bounds.set(0, 0, (int)rect.getWidth(), (int)rect.getHeight());
}
}
public static void finalizer(int foo) {
// pass
}
}
|