forked from aosp-mirror/platform_frameworks_base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayout.java
More file actions
1955 lines (1687 loc) · 68.6 KB
/
Layout.java
File metadata and controls
1955 lines (1687 loc) · 68.6 KB
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
/*
* Copyright (C) 2006 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.text;
import android.emoji.EmojiFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.text.method.TextKeyListener;
import android.text.style.AlignmentSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
import android.text.style.LineBackgroundSpan;
import android.text.style.ParagraphStyle;
import android.text.style.ReplacementSpan;
import android.text.style.TabStopSpan;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
import java.util.Arrays;
/**
* A base class that manages text layout in visual elements on
* the screen.
* <p>For text that will be edited, use a {@link DynamicLayout},
* which will be updated as the text changes.
* For text that will not change, use a {@link StaticLayout}.
*/
public abstract class Layout {
private static final ParagraphStyle[] NO_PARA_SPANS =
ArrayUtils.emptyArray(ParagraphStyle.class);
/* package */ static final EmojiFactory EMOJI_FACTORY = EmojiFactory.newAvailableInstance();
/* package */ static final int MIN_EMOJI, MAX_EMOJI;
static {
if (EMOJI_FACTORY != null) {
MIN_EMOJI = EMOJI_FACTORY.getMinimumAndroidPua();
MAX_EMOJI = EMOJI_FACTORY.getMaximumAndroidPua();
} else {
MIN_EMOJI = -1;
MAX_EMOJI = -1;
}
}
/**
* Return how wide a layout must be in order to display the
* specified text with one line per paragraph.
*/
public static float getDesiredWidth(CharSequence source,
TextPaint paint) {
return getDesiredWidth(source, 0, source.length(), paint);
}
/**
* Return how wide a layout must be in order to display the
* specified text slice with one line per paragraph.
*/
public static float getDesiredWidth(CharSequence source,
int start, int end,
TextPaint paint) {
float need = 0;
int next;
for (int i = start; i <= end; i = next) {
next = TextUtils.indexOf(source, '\n', i, end);
if (next < 0)
next = end;
// note, omits trailing paragraph char
float w = measurePara(paint, source, i, next);
if (w > need)
need = w;
next++;
}
return need;
}
/**
* Subclasses of Layout use this constructor to set the display text,
* width, and other standard properties.
* @param text the text to render
* @param paint the default paint for the layout. Styles can override
* various attributes of the paint.
* @param width the wrapping width for the text.
* @param align whether to left, right, or center the text. Styles can
* override the alignment.
* @param spacingMult factor by which to scale the font size to get the
* default line spacing
* @param spacingAdd amount to add to the default line spacing
*/
protected Layout(CharSequence text, TextPaint paint,
int width, Alignment align,
float spacingMult, float spacingAdd) {
this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
spacingMult, spacingAdd);
}
/**
* Subclasses of Layout use this constructor to set the display text,
* width, and other standard properties.
* @param text the text to render
* @param paint the default paint for the layout. Styles can override
* various attributes of the paint.
* @param width the wrapping width for the text.
* @param align whether to left, right, or center the text. Styles can
* override the alignment.
* @param spacingMult factor by which to scale the font size to get the
* default line spacing
* @param spacingAdd amount to add to the default line spacing
*
* @hide
*/
protected Layout(CharSequence text, TextPaint paint,
int width, Alignment align, TextDirectionHeuristic textDir,
float spacingMult, float spacingAdd) {
if (width < 0)
throw new IllegalArgumentException("Layout: " + width + " < 0");
// Ensure paint doesn't have baselineShift set.
// While normally we don't modify the paint the user passed in,
// we were already doing this in Styled.drawUniformRun with both
// baselineShift and bgColor. We probably should reevaluate bgColor.
if (paint != null) {
paint.bgColor = 0;
paint.baselineShift = 0;
}
mText = text;
mPaint = paint;
mWorkPaint = new TextPaint();
mWidth = width;
mAlignment = align;
mSpacingMult = spacingMult;
mSpacingAdd = spacingAdd;
mSpannedText = text instanceof Spanned;
mTextDir = textDir;
}
/**
* Replace constructor properties of this Layout with new ones. Be careful.
*/
/* package */ void replaceWith(CharSequence text, TextPaint paint,
int width, Alignment align,
float spacingmult, float spacingadd) {
if (width < 0) {
throw new IllegalArgumentException("Layout: " + width + " < 0");
}
mText = text;
mPaint = paint;
mWidth = width;
mAlignment = align;
mSpacingMult = spacingmult;
mSpacingAdd = spacingadd;
mSpannedText = text instanceof Spanned;
}
/**
* Draw this Layout on the specified Canvas.
*/
public void draw(Canvas c) {
draw(c, null, null, 0);
}
/**
* Draw this Layout on the specified canvas, with the highlight path drawn
* between the background and the text.
*
* @param canvas the canvas
* @param highlight the path of the highlight or cursor; can be null
* @param highlightPaint the paint for the highlight
* @param cursorOffsetVertical the amount to temporarily translate the
* canvas while rendering the highlight
*/
public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
int cursorOffsetVertical) {
final long lineRange = getLineRangeForDraw(canvas);
int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
if (lastLine < 0) return;
drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
firstLine, lastLine);
drawText(canvas, firstLine, lastLine);
}
/**
* @hide
*/
public void drawText(Canvas canvas, int firstLine, int lastLine) {
int previousLineBottom = getLineTop(firstLine);
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spanEnd = 0;
TextPaint paint = mPaint;
CharSequence buf = mText;
Alignment paraAlign = mAlignment;
TabStops tabStops = null;
boolean tabStopsIsInitialized = false;
TextLine tl = TextLine.obtain();
// Draw the lines, one at a time.
// The baseline is the top of the following line minus the current line's descent.
for (int i = firstLine; i <= lastLine; i++) {
int start = previousLineEnd;
previousLineEnd = getLineStart(i + 1);
int end = getLineVisibleEnd(i, start, previousLineEnd);
int ltop = previousLineBottom;
int lbottom = getLineTop(i+1);
previousLineBottom = lbottom;
int lbaseline = lbottom - getLineDescent(i);
int dir = getParagraphDirection(i);
int left = 0;
int right = mWidth;
if (mSpannedText) {
Spanned sp = (Spanned) buf;
int textLength = buf.length();
boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
// New batch of paragraph styles, collect into spans array.
// Compute the alignment, last alignment style wins.
// Reset tabStops, we'll rebuild if we encounter a line with
// tabs.
// We expect paragraph spans to be relatively infrequent, use
// spanEnd so that we can check less frequently. Since
// paragraph styles ought to apply to entire paragraphs, we can
// just collect the ones present at the start of the paragraph.
// If spanEnd is before the end of the paragraph, that's not
// our problem.
if (start >= spanEnd && (i == firstLine || isFirstParaLine)) {
spanEnd = sp.nextSpanTransition(start, textLength,
ParagraphStyle.class);
spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
paraAlign = mAlignment;
for (int n = spans.length - 1; n >= 0; n--) {
if (spans[n] instanceof AlignmentSpan) {
paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
break;
}
}
tabStopsIsInitialized = false;
}
// Draw all leading margin spans. Adjust left or right according
// to the paragraph direction of the line.
final int length = spans.length;
boolean useFirstLineMargin = isFirstParaLine;
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan2) {
int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
// if there is more than one LeadingMarginSpan2, use
// the count that is greatest
if (i < startLine + count) {
useFirstLineMargin = true;
break;
}
}
}
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan) {
LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
if (dir == DIR_RIGHT_TO_LEFT) {
margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
lbaseline, lbottom, buf,
start, end, isFirstParaLine, this);
right -= margin.getLeadingMargin(useFirstLineMargin);
} else {
margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
lbaseline, lbottom, buf,
start, end, isFirstParaLine, this);
left += margin.getLeadingMargin(useFirstLineMargin);
}
}
}
}
boolean hasTabOrEmoji = getLineContainsTab(i);
// Can't tell if we have tabs for sure, currently
if (hasTabOrEmoji && !tabStopsIsInitialized) {
if (tabStops == null) {
tabStops = new TabStops(TAB_INCREMENT, spans);
} else {
tabStops.reset(TAB_INCREMENT, spans);
}
tabStopsIsInitialized = true;
}
// Determine whether the line aligns to normal, opposite, or center.
Alignment align = paraAlign;
if (align == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ?
Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (align == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ?
Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
}
int x;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = left;
} else {
x = right;
}
} else {
int max = (int)getLineExtent(i, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = right - max;
} else {
x = left - max;
}
} else { // Alignment.ALIGN_CENTER
max = max & ~1;
x = (right + left - max) >> 1;
}
}
Directions directions = getLineDirections(i);
if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTabOrEmoji) {
// XXX: assumes there's nothing additional to be done
canvas.drawText(buf, start, end, x, lbaseline, paint);
} else {
tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
tl.draw(canvas, x, ltop, lbaseline, lbottom);
}
}
TextLine.recycle(tl);
}
/**
* @hide
*/
public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
int cursorOffsetVertical, int firstLine, int lastLine) {
// First, draw LineBackgroundSpans.
// LineBackgroundSpans know nothing about the alignment, margins, or
// direction of the layout or line. XXX: Should they?
// They are evaluated at each line.
if (mSpannedText) {
if (mLineBackgroundSpans == null) {
mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
}
Spanned buffer = (Spanned) mText;
int textLength = buffer.length();
mLineBackgroundSpans.init(buffer, 0, textLength);
if (mLineBackgroundSpans.numberOfSpans > 0) {
int previousLineBottom = getLineTop(firstLine);
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spansLength = 0;
TextPaint paint = mPaint;
int spanEnd = 0;
final int width = mWidth;
for (int i = firstLine; i <= lastLine; i++) {
int start = previousLineEnd;
int end = getLineStart(i + 1);
previousLineEnd = end;
int ltop = previousLineBottom;
int lbottom = getLineTop(i + 1);
previousLineBottom = lbottom;
int lbaseline = lbottom - getLineDescent(i);
if (start >= spanEnd) {
// These should be infrequent, so we'll use this so that
// we don't have to check as often.
spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
// All LineBackgroundSpans on a line contribute to its background.
spansLength = 0;
// Duplication of the logic of getParagraphSpans
if (start != end || start == 0) {
// Equivalent to a getSpans(start, end), but filling the 'spans' local
// array instead to reduce memory allocation
for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
// equal test is valid since both intervals are not empty by
// construction
if (mLineBackgroundSpans.spanStarts[j] >= end ||
mLineBackgroundSpans.spanEnds[j] <= start) continue;
spans = GrowingArrayUtils.append(
spans, spansLength, mLineBackgroundSpans.spans[j]);
spansLength++;
}
}
}
for (int n = 0; n < spansLength; n++) {
LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
ltop, lbaseline, lbottom,
buffer, start, end, i);
}
}
}
mLineBackgroundSpans.recycle();
}
// There can be a highlight even without spans if we are drawing
// a non-spanned transformation of a spanned editing buffer.
if (highlight != null) {
if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
canvas.drawPath(highlight, highlightPaint);
if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
}
}
/**
* @param canvas
* @return The range of lines that need to be drawn, possibly empty.
* @hide
*/
public long getLineRangeForDraw(Canvas canvas) {
int dtop, dbottom;
synchronized (sTempRect) {
if (!canvas.getClipBounds(sTempRect)) {
// Negative range end used as a special flag
return TextUtils.packRangeInLong(0, -1);
}
dtop = sTempRect.top;
dbottom = sTempRect.bottom;
}
final int top = Math.max(dtop, 0);
final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
}
/**
* Return the start position of the line, given the left and right bounds
* of the margins.
*
* @param line the line index
* @param left the left bounds (0, or leading margin if ltr para)
* @param right the right bounds (width, minus leading margin if rtl para)
* @return the start position of the line (to right of line if rtl para)
*/
private int getLineStartPos(int line, int left, int right) {
// Adjust the point at which to start rendering depending on the
// alignment of the paragraph.
Alignment align = getParagraphAlignment(line);
int dir = getParagraphDirection(line);
if (align == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (align == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
}
int x;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = left;
} else {
x = right;
}
} else {
TabStops tabStops = null;
if (mSpannedText && getLineContainsTab(line)) {
Spanned spanned = (Spanned) mText;
int start = getLineStart(line);
int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
TabStopSpan.class);
TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
TabStopSpan.class);
if (tabSpans.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabSpans);
}
}
int max = (int)getLineExtent(line, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = right - max;
} else {
// max is negative here
x = left - max;
}
} else { // Alignment.ALIGN_CENTER
max = max & ~1;
x = (left + right - max) >> 1;
}
}
return x;
}
/**
* Return the text that is displayed by this Layout.
*/
public final CharSequence getText() {
return mText;
}
/**
* Return the base Paint properties for this layout.
* Do NOT change the paint, which may result in funny
* drawing for this layout.
*/
public final TextPaint getPaint() {
return mPaint;
}
/**
* Return the width of this layout.
*/
public final int getWidth() {
return mWidth;
}
/**
* Return the width to which this Layout is ellipsizing, or
* {@link #getWidth} if it is not doing anything special.
*/
public int getEllipsizedWidth() {
return mWidth;
}
/**
* Increase the width of this layout to the specified width.
* Be careful to use this only when you know it is appropriate—
* it does not cause the text to reflow to use the full new width.
*/
public final void increaseWidthTo(int wid) {
if (wid < mWidth) {
throw new RuntimeException("attempted to reduce Layout width");
}
mWidth = wid;
}
/**
* Return the total height of this layout.
*/
public int getHeight() {
return getLineTop(getLineCount());
}
/**
* Return the base alignment of this layout.
*/
public final Alignment getAlignment() {
return mAlignment;
}
/**
* Return what the text height is multiplied by to get the line height.
*/
public final float getSpacingMultiplier() {
return mSpacingMult;
}
/**
* Return the number of units of leading that are added to each line.
*/
public final float getSpacingAdd() {
return mSpacingAdd;
}
/**
* Return the heuristic used to determine paragraph text direction.
* @hide
*/
public final TextDirectionHeuristic getTextDirectionHeuristic() {
return mTextDir;
}
/**
* Return the number of lines of text in this layout.
*/
public abstract int getLineCount();
/**
* Return the baseline for the specified line (0…getLineCount() - 1)
* If bounds is not null, return the top, left, right, bottom extents
* of the specified line in it.
* @param line which line to examine (0..getLineCount() - 1)
* @param bounds Optional. If not null, it returns the extent of the line
* @return the Y-coordinate of the baseline
*/
public int getLineBounds(int line, Rect bounds) {
if (bounds != null) {
bounds.left = 0; // ???
bounds.top = getLineTop(line);
bounds.right = mWidth; // ???
bounds.bottom = getLineTop(line + 1);
}
return getLineBaseline(line);
}
/**
* Return the vertical position of the top of the specified line
* (0…getLineCount()).
* If the specified line is equal to the line count, returns the
* bottom of the last line.
*/
public abstract int getLineTop(int line);
/**
* Return the descent of the specified line(0…getLineCount() - 1).
*/
public abstract int getLineDescent(int line);
/**
* Return the text offset of the beginning of the specified line (
* 0…getLineCount()). If the specified line is equal to the line
* count, returns the length of the text.
*/
public abstract int getLineStart(int line);
/**
* Returns the primary directionality of the paragraph containing the
* specified line, either 1 for left-to-right lines, or -1 for right-to-left
* lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
*/
public abstract int getParagraphDirection(int line);
/**
* Returns whether the specified line contains one or more
* characters that need to be handled specially, like tabs
* or emoji.
*/
public abstract boolean getLineContainsTab(int line);
/**
* Returns the directional run information for the specified line.
* The array alternates counts of characters in left-to-right
* and right-to-left segments of the line.
*
* <p>NOTE: this is inadequate to support bidirectional text, and will change.
*/
public abstract Directions getLineDirections(int line);
/**
* Returns the (negative) number of extra pixels of ascent padding in the
* top line of the Layout.
*/
public abstract int getTopPadding();
/**
* Returns the number of extra pixels of descent padding in the
* bottom line of the Layout.
*/
public abstract int getBottomPadding();
/**
* Returns true if the character at offset and the preceding character
* are at different run levels (and thus there's a split caret).
* @param offset the offset
* @return true if at a level boundary
* @hide
*/
public boolean isLevelBoundary(int offset) {
int line = getLineForOffset(offset);
Directions dirs = getLineDirections(line);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
return false;
}
int[] runs = dirs.mDirections;
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
if (offset == lineStart || offset == lineEnd) {
int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
int runIndex = offset == lineStart ? 0 : runs.length - 2;
return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
}
offset -= lineStart;
for (int i = 0; i < runs.length; i += 2) {
if (offset == runs[i]) {
return true;
}
}
return false;
}
/**
* Returns true if the character at offset is right to left (RTL).
* @param offset the offset
* @return true if the character is RTL, false if it is LTR
*/
public boolean isRtlCharAt(int offset) {
int line = getLineForOffset(offset);
Directions dirs = getLineDirections(line);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
return false;
}
if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
return true;
}
int[] runs = dirs.mDirections;
int lineStart = getLineStart(line);
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (offset >= start && offset < limit) {
int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
return ((level & 1) != 0);
}
}
// Should happen only if the offset is "out of bounds"
return false;
}
private boolean primaryIsTrailingPrevious(int offset) {
int line = getLineForOffset(offset);
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int[] runs = getLineDirections(line).mDirections;
int levelAt = -1;
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (limit > lineEnd) {
limit = lineEnd;
}
if (offset >= start && offset < limit) {
if (offset > start) {
// Previous character is at same level, so don't use trailing.
return false;
}
levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
break;
}
}
if (levelAt == -1) {
// Offset was limit of line.
levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
}
// At level boundary, check previous level.
int levelBefore = -1;
if (offset == lineStart) {
levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
} else {
offset -= 1;
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (limit > lineEnd) {
limit = lineEnd;
}
if (offset >= start && offset < limit) {
levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
break;
}
}
}
return levelBefore < levelAt;
}
/**
* Get the primary horizontal position for the specified text offset.
* This is the location where a new character would be inserted in
* the paragraph's primary direction.
*/
public float getPrimaryHorizontal(int offset) {
return getPrimaryHorizontal(offset, false /* not clamped */);
}
/**
* Get the primary horizontal position for the specified text offset, but
* optionally clamp it so that it doesn't exceed the width of the layout.
* @hide
*/
public float getPrimaryHorizontal(int offset, boolean clamped) {
boolean trailing = primaryIsTrailingPrevious(offset);
return getHorizontal(offset, trailing, clamped);
}
/**
* Get the secondary horizontal position for the specified text offset.
* This is the location where a new character would be inserted in
* the direction other than the paragraph's primary direction.
*/
public float getSecondaryHorizontal(int offset) {
return getSecondaryHorizontal(offset, false /* not clamped */);
}
/**
* Get the secondary horizontal position for the specified text offset, but
* optionally clamp it so that it doesn't exceed the width of the layout.
* @hide
*/
public float getSecondaryHorizontal(int offset, boolean clamped) {
boolean trailing = primaryIsTrailingPrevious(offset);
return getHorizontal(offset, !trailing, clamped);
}
private float getHorizontal(int offset, boolean trailing, boolean clamped) {
int line = getLineForOffset(offset);
return getHorizontal(offset, trailing, line, clamped);
}
private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
int start = getLineStart(line);
int end = getLineEnd(line);
int dir = getParagraphDirection(line);
boolean hasTabOrEmoji = getLineContainsTab(line);
Directions directions = getLineDirections(line);
TabStops tabStops = null;
if (hasTabOrEmoji && mText instanceof Spanned) {
// Just checking this line should be good enough, tabs should be
// consistent across all lines in a paragraph.
TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
if (tabs.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
}
}
TextLine tl = TextLine.obtain();
tl.set(mPaint, mText, start, end, dir, directions, hasTabOrEmoji, tabStops);
float wid = tl.measure(offset - start, trailing, null);
TextLine.recycle(tl);
if (clamped && wid > mWidth) {
wid = mWidth;
}
int left = getParagraphLeft(line);
int right = getParagraphRight(line);
return getLineStartPos(line, left, right) + wid;
}
/**
* Get the leftmost position that should be exposed for horizontal
* scrolling on the specified line.
*/
public float getLineLeft(int line) {
int dir = getParagraphDirection(line);
Alignment align = getParagraphAlignment(line);
if (align == Alignment.ALIGN_LEFT) {
return 0;
} else if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_RIGHT_TO_LEFT)
return getParagraphRight(line) - getLineMax(line);
else
return 0;
} else if (align == Alignment.ALIGN_RIGHT) {
return mWidth - getLineMax(line);
} else if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_RIGHT_TO_LEFT)
return 0;
else
return mWidth - getLineMax(line);
} else { /* align == Alignment.ALIGN_CENTER */
int left = getParagraphLeft(line);
int right = getParagraphRight(line);
int max = ((int) getLineMax(line)) & ~1;
return left + ((right - left) - max) / 2;
}
}
/**
* Get the rightmost position that should be exposed for horizontal
* scrolling on the specified line.
*/
public float getLineRight(int line) {
int dir = getParagraphDirection(line);
Alignment align = getParagraphAlignment(line);
if (align == Alignment.ALIGN_LEFT) {
return getParagraphLeft(line) + getLineMax(line);
} else if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_RIGHT_TO_LEFT)
return mWidth;
else
return getParagraphLeft(line) + getLineMax(line);
} else if (align == Alignment.ALIGN_RIGHT) {
return mWidth;
} else if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_RIGHT_TO_LEFT)
return getLineMax(line);
else
return mWidth;
} else { /* align == Alignment.ALIGN_CENTER */
int left = getParagraphLeft(line);
int right = getParagraphRight(line);
int max = ((int) getLineMax(line)) & ~1;
return right - ((right - left) - max) / 2;
}
}
/**
* Gets the unsigned horizontal extent of the specified line, including
* leading margin indent, but excluding trailing whitespace.
*/
public float getLineMax(int line) {
float margin = getParagraphLeadingMargin(line);
float signedExtent = getLineExtent(line, false);
return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
}
/**
* Gets the unsigned horizontal extent of the specified line, including
* leading margin indent and trailing whitespace.
*/
public float getLineWidth(int line) {
float margin = getParagraphLeadingMargin(line);
float signedExtent = getLineExtent(line, true);
return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
}
/**
* Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
* tab stops instead of using the ones passed in.
* @param line the index of the line
* @param full whether to include trailing whitespace
* @return the extent of the line
*/
private float getLineExtent(int line, boolean full) {
int start = getLineStart(line);
int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
boolean hasTabsOrEmoji = getLineContainsTab(line);
TabStops tabStops = null;
if (hasTabsOrEmoji && mText instanceof Spanned) {
// Just checking this line should be good enough, tabs should be
// consistent across all lines in a paragraph.
TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
if (tabs.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
}
}
Directions directions = getLineDirections(line);
// Returned directions can actually be null
if (directions == null) {
return 0f;
}
int dir = getParagraphDirection(line);
TextLine tl = TextLine.obtain();
tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
float width = tl.metrics(null);
TextLine.recycle(tl);
return width;
}
/**
* Returns the signed horizontal extent of the specified line, excluding
* leading margin. If full is false, excludes trailing whitespace.
* @param line the index of the line
* @param tabStops the tab stops, can be null if we know they're not used.
* @param full whether to include trailing whitespace
* @return the extent of the text on this line
*/
private float getLineExtent(int line, TabStops tabStops, boolean full) {
int start = getLineStart(line);
int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
boolean hasTabsOrEmoji = getLineContainsTab(line);
Directions directions = getLineDirections(line);
int dir = getParagraphDirection(line);
TextLine tl = TextLine.obtain();
tl.set(mPaint, mText, start, end, dir, directions, hasTabsOrEmoji, tabStops);
float width = tl.metrics(null);
TextLine.recycle(tl);