blob: a4546f0682b354892f5a79752de5d749776afe90 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
Doug Felt4e0c5e52010-03-15 16:56:02 -070019import com.android.internal.util.ArrayUtils;
20
The Android Open Source Project10592532009-03-18 17:39:46 -070021import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.graphics.Paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.text.style.LeadingMarginSpan;
Gilles Debunne66111472010-11-19 11:04:37 -080024import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.style.LineHeightSpan;
26import android.text.style.MetricAffectingSpan;
Doug Feltc982f602010-05-25 11:51:40 -070027import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29/**
30 * StaticLayout is a Layout for text that will not be edited after it
31 * is laid out. Use {@link DynamicLayout} for text that may change.
32 * <p>This is used by widgets to control text layout. You should not need
33 * to use this class directly unless you are implementing your own widget
34 * or custom display object, or would be tempted to call
Doug Felt4e0c5e52010-03-15 16:56:02 -070035 * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int,
36 * float, float, android.graphics.Paint)
37 * Canvas.drawText()} directly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 */
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -080039public class StaticLayout extends Layout {
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 public StaticLayout(CharSequence source, TextPaint paint,
42 int width,
43 Alignment align, float spacingmult, float spacingadd,
44 boolean includepad) {
45 this(source, 0, source.length(), paint, width, align,
46 spacingmult, spacingadd, includepad);
47 }
48
49 public StaticLayout(CharSequence source, int bufstart, int bufend,
50 TextPaint paint, int outerwidth,
51 Alignment align,
52 float spacingmult, float spacingadd,
53 boolean includepad) {
54 this(source, bufstart, bufend, paint, outerwidth, align,
55 spacingmult, spacingadd, includepad, null, 0);
56 }
57
58 public StaticLayout(CharSequence source, int bufstart, int bufend,
59 TextPaint paint, int outerwidth,
60 Alignment align,
61 float spacingmult, float spacingadd,
62 boolean includepad,
63 TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
64 super((ellipsize == null)
Doug Felt4e0c5e52010-03-15 16:56:02 -070065 ? source
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 : (source instanceof Spanned)
67 ? new SpannedEllipsizer(source)
68 : new Ellipsizer(source),
69 paint, outerwidth, align, spacingmult, spacingadd);
70
71 /*
72 * This is annoying, but we can't refer to the layout until
73 * superclass construction is finished, and the superclass
74 * constructor wants the reference to the display text.
Doug Felt4e0c5e52010-03-15 16:56:02 -070075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * This will break if the superclass constructor ever actually
77 * cares about the content instead of just holding the reference.
78 */
79 if (ellipsize != null) {
80 Ellipsizer e = (Ellipsizer) getText();
81
82 e.mLayout = this;
83 e.mWidth = ellipsizedWidth;
84 e.mMethod = ellipsize;
85 mEllipsizedWidth = ellipsizedWidth;
86
87 mColumns = COLUMNS_ELLIPSIZE;
88 } else {
89 mColumns = COLUMNS_NORMAL;
90 mEllipsizedWidth = outerwidth;
91 }
92
93 mLines = new int[ArrayUtils.idealIntArraySize(2 * mColumns)];
94 mLineDirections = new Directions[
95 ArrayUtils.idealIntArraySize(2 * mColumns)];
96
Doug Felte8e45f22010-03-29 14:58:40 -070097 mMeasured = MeasuredText.obtain();
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 generate(source, bufstart, bufend, paint, outerwidth, align,
100 spacingmult, spacingadd, includepad, includepad,
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800101 ellipsizedWidth, ellipsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Doug Felte8e45f22010-03-29 14:58:40 -0700103 mMeasured = MeasuredText.recycle(mMeasured);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 mFontMetricsInt = null;
105 }
106
107 /* package */ StaticLayout(boolean ellipsize) {
108 super(null, null, 0, null, 0, 0);
109
110 mColumns = COLUMNS_ELLIPSIZE;
111 mLines = new int[ArrayUtils.idealIntArraySize(2 * mColumns)];
112 mLineDirections = new Directions[
113 ArrayUtils.idealIntArraySize(2 * mColumns)];
Doug Felte8e45f22010-03-29 14:58:40 -0700114 mMeasured = MeasuredText.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800117 /* package */ void generate(CharSequence source, int bufStart, int bufEnd,
118 TextPaint paint, int outerWidth,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 Alignment align,
120 float spacingmult, float spacingadd,
121 boolean includepad, boolean trackpad,
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800122 float ellipsizedWidth, TextUtils.TruncateAt ellipsize) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 mLineCount = 0;
124
125 int v = 0;
126 boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
127
128 Paint.FontMetricsInt fm = mFontMetricsInt;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800129 int[] chooseHtv = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Doug Felte8e45f22010-03-29 14:58:40 -0700131 MeasuredText measured = mMeasured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 Spanned spanned = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 if (source instanceof Spanned)
135 spanned = (Spanned) source;
136
137 int DEFAULT_DIR = DIR_LEFT_TO_RIGHT; // XXX
138
Doug Felte8e45f22010-03-29 14:58:40 -0700139 int paraEnd;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800140 for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
141 paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
Doug Felte8e45f22010-03-29 14:58:40 -0700142 if (paraEnd < 0)
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800143 paraEnd = bufEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 else
Doug Felte8e45f22010-03-29 14:58:40 -0700145 paraEnd++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
Doug Feltc982f602010-05-25 11:51:40 -0700147 int firstWidthLineLimit = mLineCount + 1;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800148 int firstWidth = outerWidth;
149 int restWidth = outerWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800151 LineHeightSpan[] chooseHt = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
153 if (spanned != null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -0700154 LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd,
Doug Felte8e45f22010-03-29 14:58:40 -0700155 LeadingMarginSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 for (int i = 0; i < sp.length; i++) {
Mark Wagner7b5676e2009-10-16 11:44:23 -0700157 LeadingMarginSpan lms = sp[i];
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800158 firstWidth -= sp[i].getLeadingMargin(true);
159 restWidth -= sp[i].getLeadingMargin(false);
Doug Feltc982f602010-05-25 11:51:40 -0700160
161 // LeadingMarginSpan2 is odd. The count affects all
162 // leading margin spans, not just this particular one,
163 // and start from the top of the span, not the top of the
164 // paragraph.
165 if (lms instanceof LeadingMarginSpan2) {
166 LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
167 int lmsFirstLine = getLineForOffset(spanned.getSpanStart(lms2));
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800168 firstWidthLineLimit = lmsFirstLine + lms2.getLeadingMarginLineCount();
Mark Wagner7b5676e2009-10-16 11:44:23 -0700169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800172 chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800174 if (chooseHt.length != 0) {
175 if (chooseHtv == null ||
176 chooseHtv.length < chooseHt.length) {
177 chooseHtv = new int[ArrayUtils.idealIntArraySize(
178 chooseHt.length)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800181 for (int i = 0; i < chooseHt.length; i++) {
182 int o = spanned.getSpanStart(chooseHt[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
Doug Felte8e45f22010-03-29 14:58:40 -0700184 if (o < paraStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 // starts in this layout, before the
186 // current paragraph
187
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800188 chooseHtv[i] = getLineTop(getLineForOffset(o));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 } else {
190 // starts in this paragraph
191
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800192 chooseHtv[i] = v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
194 }
195 }
196 }
197
Doug Felte8e45f22010-03-29 14:58:40 -0700198 measured.setPara(source, paraStart, paraEnd, DIR_REQUEST_DEFAULT_LTR);
199 char[] chs = measured.mChars;
200 float[] widths = measured.mWidths;
201 byte[] chdirs = measured.mLevels;
202 int dir = measured.mDir;
203 boolean easy = measured.mEasy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800205 int width = firstWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
207 float w = 0;
Doug Felte8e45f22010-03-29 14:58:40 -0700208 int here = paraStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Doug Felte8e45f22010-03-29 14:58:40 -0700210 int ok = paraStart;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800211 float okWidth = w;
212 int okAscent = 0, okDescent = 0, okTop = 0, okBottom = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Doug Felte8e45f22010-03-29 14:58:40 -0700214 int fit = paraStart;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800215 float fitWidth = w;
216 int fitAscent = 0, fitDescent = 0, fitTop = 0, fitBottom = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Doug Feltc982f602010-05-25 11:51:40 -0700218 boolean hasTabOrEmoji = false;
219 boolean hasTab = false;
220 TabStops tabStops = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
Doug Felt23241882010-06-02 14:41:06 -0700222 for (int spanStart = paraStart, spanEnd = spanStart, nextSpanStart;
223 spanStart < paraEnd; spanStart = nextSpanStart) {
Doug Felte8e45f22010-03-29 14:58:40 -0700224
Doug Felt23241882010-06-02 14:41:06 -0700225 if (spanStart == spanEnd) {
226 if (spanned == null)
227 spanEnd = paraEnd;
228 else
229 spanEnd = spanned.nextSpanTransition(spanStart, paraEnd,
230 MetricAffectingSpan.class);
231
232 int spanLen = spanEnd - spanStart;
233 if (spanned == null) {
234 measured.addStyleRun(paint, spanLen, fm);
235 } else {
236 MetricAffectingSpan[] spans =
237 spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
238 measured.addStyleRun(paint, spans, spanLen, fm);
239 }
240 }
241
242 nextSpanStart = spanEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800244 int fmTop = fm.top;
245 int fmBottom = fm.bottom;
246 int fmAscent = fm.ascent;
247 int fmDescent = fm.descent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Doug Felte8e45f22010-03-29 14:58:40 -0700249 for (int j = spanStart; j < spanEnd; j++) {
250 char c = chs[j - paraStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800252 if (c == CHAR_NEW_LINE) {
Gilles Debunne66111472010-11-19 11:04:37 -0800253 // intentionally left empty
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800254 } else if (c == CHAR_TAB) {
Doug Feltc982f602010-05-25 11:51:40 -0700255 if (hasTab == false) {
256 hasTab = true;
257 hasTabOrEmoji = true;
Kenny Root24ca4542010-06-22 23:46:35 -0700258 if (spanned != null) {
259 // First tab this para, check for tabstops
Eric Fischer74d31ef2010-08-05 15:29:36 -0700260 TabStopSpan[] spans = getParagraphSpans(spanned, paraStart,
Kenny Root24ca4542010-06-22 23:46:35 -0700261 paraEnd, TabStopSpan.class);
262 if (spans.length > 0) {
263 tabStops = new TabStops(TAB_INCREMENT, spans);
264 }
Doug Feltc982f602010-05-25 11:51:40 -0700265 }
266 }
267 if (tabStops != null) {
268 w = tabStops.nextTab(w);
269 } else {
270 w = TabStops.nextDefaultStop(w, TAB_INCREMENT);
271 }
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800272 } else if (c >= CHAR_FIRST_HIGH_SURROGATE && c <= CHAR_LAST_LOW_SURROGATE
273 && j + 1 < spanEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -0700274 int emoji = Character.codePointAt(chs, j - paraStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
The Android Open Source Project10592532009-03-18 17:39:46 -0700276 if (emoji >= MIN_EMOJI && emoji <= MAX_EMOJI) {
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800277 Bitmap bm = EMOJI_FACTORY.getBitmapFromAndroidPua(emoji);
The Android Open Source Project10592532009-03-18 17:39:46 -0700278
279 if (bm != null) {
Eric Fischer423f0e42009-03-27 18:04:12 -0700280 Paint whichPaint;
281
282 if (spanned == null) {
283 whichPaint = paint;
284 } else {
285 whichPaint = mWorkPaint;
286 }
287
Doug Felt4e0c5e52010-03-15 16:56:02 -0700288 float wid = bm.getWidth() *
Eric Fischer423f0e42009-03-27 18:04:12 -0700289 -whichPaint.ascent() /
290 bm.getHeight();
291
292 w += wid;
Doug Feltc982f602010-05-25 11:51:40 -0700293 hasTabOrEmoji = true;
The Android Open Source Project10592532009-03-18 17:39:46 -0700294 j++;
295 } else {
Doug Felte8e45f22010-03-29 14:58:40 -0700296 w += widths[j - paraStart];
The Android Open Source Project10592532009-03-18 17:39:46 -0700297 }
298 } else {
Doug Felte8e45f22010-03-29 14:58:40 -0700299 w += widths[j - paraStart];
The Android Open Source Project10592532009-03-18 17:39:46 -0700300 }
301 } else {
Doug Felte8e45f22010-03-29 14:58:40 -0700302 w += widths[j - paraStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304
305 // Log.e("text", "was " + before + " now " + w + " after " + c + " within " + width);
306
307 if (w <= width) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800308 fitWidth = w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 fit = j + 1;
310
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800311 if (fmTop < fitTop)
312 fitTop = fmTop;
313 if (fmAscent < fitAscent)
314 fitAscent = fmAscent;
315 if (fmDescent > fitDescent)
316 fitDescent = fmDescent;
317 if (fmBottom > fitBottom)
318 fitBottom = fmBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 /*
321 * From the Unicode Line Breaking Algorithm:
322 * (at least approximately)
Doug Felt4e0c5e52010-03-15 16:56:02 -0700323 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 * .,:; are class IS: breakpoints
325 * except when adjacent to digits
326 * / is class SY: a breakpoint
327 * except when followed by a digit.
328 * - is class HY: a breakpoint
329 * except when followed by a digit.
330 *
Eric Fischer549d7242009-03-31 14:19:47 -0700331 * Ideographs are class ID: breakpoints when adjacent,
332 * except for NS (non-starters), which can be broken
333 * after but not before.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
335
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800336 if (c == CHAR_SPACE || c == CHAR_TAB ||
337 ((c == CHAR_DOT || c == CHAR_COMMA ||
338 c == CHAR_COLON || c == CHAR_SEMICOLON) &&
Doug Felte8e45f22010-03-29 14:58:40 -0700339 (j - 1 < here || !Character.isDigit(chs[j - 1 - paraStart])) &&
340 (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) ||
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800341 ((c == CHAR_SLASH || c == CHAR_HYPHEN) &&
Doug Felte8e45f22010-03-29 14:58:40 -0700342 (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) ||
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800343 (c >= CHAR_FIRST_CJK && isIdeographic(c, true) &&
Doug Felte8e45f22010-03-29 14:58:40 -0700344 j + 1 < spanEnd && isIdeographic(chs[j + 1 - paraStart], false))) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800345 okWidth = w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 ok = j + 1;
347
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800348 if (fitTop < okTop)
349 okTop = fitTop;
350 if (fitAscent < okAscent)
351 okAscent = fitAscent;
352 if (fitDescent > okDescent)
353 okDescent = fitDescent;
354 if (fitBottom > okBottom)
355 okBottom = fitBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Gilles Debunne4cf435d2011-01-04 15:35:29 -0800357 } else {
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800358 if (ellipsize != null) {
359 // Break only at spaces using ok indexes.
Gilles Debunned434d232011-01-04 17:15:14 -0800360 if (ok != here) {
361 // Log.e("text", "output ok " + here + " to " +ok);
Gilles Debunne32ea4ff2010-12-21 11:28:34 -0800362
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800363 while (ok < spanEnd && chs[ok - paraStart] == CHAR_SPACE) {
Gilles Debunned434d232011-01-04 17:15:14 -0800364 ok++;
365 }
366
367 v = out(source,
368 here, ok,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800369 okAscent, okDescent, okTop, okBottom,
Gilles Debunned434d232011-01-04 17:15:14 -0800370 v,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800371 spacingmult, spacingadd, chooseHt,
372 chooseHtv, fm, hasTabOrEmoji,
Gilles Debunned434d232011-01-04 17:15:14 -0800373 needMultiply, paraStart, chdirs, dir, easy,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800374 ok == bufEnd, includepad, trackpad,
Gilles Debunned434d232011-01-04 17:15:14 -0800375 chs, widths, here - paraStart,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800376 ellipsize, ellipsizedWidth, okWidth,
Gilles Debunned434d232011-01-04 17:15:14 -0800377 paint);
378
379 here = ok;
380 } else {
381 // Act like it fit even though it didn't.
382
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800383 fitWidth = w;
Gilles Debunned434d232011-01-04 17:15:14 -0800384 here = fit = j + 1;
385
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800386 if (fmTop < fitTop)
387 fitTop = fmTop;
388 if (fmAscent < fitAscent)
389 fitAscent = fmAscent;
390 if (fmDescent > fitDescent)
391 fitDescent = fmDescent;
392 if (fmBottom > fitBottom)
393 fitBottom = fmBottom;
Gilles Debunne32ea4ff2010-12-21 11:28:34 -0800394 }
Gilles Debunne4cf435d2011-01-04 15:35:29 -0800395 } else {
Gilles Debunned434d232011-01-04 17:15:14 -0800396 if (ok != here) {
397 // Log.e("text", "output ok " + here + " to " +ok);
Gilles Debunne4cf435d2011-01-04 15:35:29 -0800398
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800399 while (ok < spanEnd && chs[ok - paraStart] == CHAR_SPACE) {
Gilles Debunned434d232011-01-04 17:15:14 -0800400 ok++;
401 }
Gilles Debunne4cf435d2011-01-04 15:35:29 -0800402
Gilles Debunned434d232011-01-04 17:15:14 -0800403 v = out(source,
404 here, ok,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800405 okAscent, okDescent, okTop, okBottom,
Gilles Debunned434d232011-01-04 17:15:14 -0800406 v,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800407 spacingmult, spacingadd, chooseHt,
408 chooseHtv, fm, hasTabOrEmoji,
Gilles Debunned434d232011-01-04 17:15:14 -0800409 needMultiply, paraStart, chdirs, dir, easy,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800410 ok == bufEnd, includepad, trackpad,
Gilles Debunned434d232011-01-04 17:15:14 -0800411 chs, widths, here - paraStart,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800412 ellipsize, ellipsizedWidth, okWidth,
Gilles Debunned434d232011-01-04 17:15:14 -0800413 paint);
414
415 here = ok;
416 } else if (fit != here) {
417 // Log.e("text", "output fit " + here + " to " +fit);
418 v = out(source,
419 here, fit,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800420 fitAscent, fitDescent,
421 fitTop, fitBottom,
Gilles Debunned434d232011-01-04 17:15:14 -0800422 v,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800423 spacingmult, spacingadd, chooseHt,
424 chooseHtv, fm, hasTabOrEmoji,
Gilles Debunned434d232011-01-04 17:15:14 -0800425 needMultiply, paraStart, chdirs, dir, easy,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800426 fit == bufEnd, includepad, trackpad,
Gilles Debunned434d232011-01-04 17:15:14 -0800427 chs, widths, here - paraStart,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800428 ellipsize, ellipsizedWidth, fitWidth,
Gilles Debunned434d232011-01-04 17:15:14 -0800429 paint);
430
431 here = fit;
432 } else {
433 // Log.e("text", "output one " + here + " to " +(here + 1));
434 // XXX not sure why the existing fm wasn't ok.
435 // measureText(paint, mWorkPaint,
436 // source, here, here + 1, fm, tab,
437 // null);
438
439 v = out(source,
440 here, here+1,
441 fm.ascent, fm.descent,
442 fm.top, fm.bottom,
443 v,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800444 spacingmult, spacingadd, chooseHt,
445 chooseHtv, fm, hasTabOrEmoji,
Gilles Debunned434d232011-01-04 17:15:14 -0800446 needMultiply, paraStart, chdirs, dir, easy,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800447 here + 1 == bufEnd, includepad,
Gilles Debunned434d232011-01-04 17:15:14 -0800448 trackpad,
449 chs, widths, here - paraStart,
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800450 ellipsize, ellipsizedWidth,
Gilles Debunned434d232011-01-04 17:15:14 -0800451 widths[here - paraStart], paint);
452
453 here = here + 1;
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
Doug Felte8e45f22010-03-29 14:58:40 -0700457 if (here < spanStart) {
Doug Felt23241882010-06-02 14:41:06 -0700458 // didn't output all the text for this span
459 // we've measured the raw widths, though, so
460 // just reset the start point
461 j = nextSpanStart = here;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 } else {
463 j = here - 1; // continue looping
464 }
465
466 ok = fit = here;
467 w = 0;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800468 fitAscent = fitDescent = fitTop = fitBottom = 0;
469 okAscent = okDescent = okTop = okBottom = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
Doug Feltc982f602010-05-25 11:51:40 -0700471 if (--firstWidthLineLimit <= 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800472 width = restWidth;
Mark Wagner7b5676e2009-10-16 11:44:23 -0700473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475 }
476 }
477
Doug Felte8e45f22010-03-29 14:58:40 -0700478 if (paraEnd != here) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800479 if ((fitTop | fitBottom | fitDescent | fitAscent) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 paint.getFontMetricsInt(fm);
481
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800482 fitTop = fm.top;
483 fitBottom = fm.bottom;
484 fitAscent = fm.ascent;
485 fitDescent = fm.descent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487
488 // Log.e("text", "output rest " + here + " to " + end);
489
490 v = out(source,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800491 here, paraEnd, fitAscent, fitDescent,
492 fitTop, fitBottom,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 v,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800494 spacingmult, spacingadd, chooseHt,
495 chooseHtv, fm, hasTabOrEmoji,
Doug Felte8e45f22010-03-29 14:58:40 -0700496 needMultiply, paraStart, chdirs, dir, easy,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800497 paraEnd == bufEnd, includepad, trackpad,
Paul Easthambe46d142011-02-04 19:17:54 -0800498 chs, widths, here - paraStart,
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800499 ellipsize, ellipsizedWidth, w, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
Doug Felte8e45f22010-03-29 14:58:40 -0700502 paraStart = paraEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800504 if (paraEnd == bufEnd)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 break;
506 }
507
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800508 if (bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) {
509 // Log.e("text", "output last " + bufEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
511 paint.getFontMetricsInt(fm);
512
513 v = out(source,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800514 bufEnd, bufEnd, fm.ascent, fm.descent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 fm.top, fm.bottom,
516 v,
517 spacingmult, spacingadd, null,
518 null, fm, false,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800519 needMultiply, bufEnd, null, DEFAULT_DIR, true,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 true, includepad, trackpad,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800521 null, null, bufStart,
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800522 ellipsize, ellipsizedWidth, 0, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /**
527 * Returns true if the specified character is one of those specified
528 * as being Ideographic (class ID) by the Unicode Line Breaking Algorithm
529 * (http://www.unicode.org/unicode/reports/tr14/), and is therefore OK
530 * to break between a pair of.
Eric Fischer549d7242009-03-31 14:19:47 -0700531 *
532 * @param includeNonStarters also return true for category NS
533 * (non-starters), which can be broken
534 * after but not before.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
Eric Fischer549d7242009-03-31 14:19:47 -0700536 private static final boolean isIdeographic(char c, boolean includeNonStarters) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 if (c >= '\u2E80' && c <= '\u2FFF') {
538 return true; // CJK, KANGXI RADICALS, DESCRIPTION SYMBOLS
539 }
540 if (c == '\u3000') {
541 return true; // IDEOGRAPHIC SPACE
542 }
543 if (c >= '\u3040' && c <= '\u309F') {
Eric Fischer549d7242009-03-31 14:19:47 -0700544 if (!includeNonStarters) {
545 switch (c) {
546 case '\u3041': // # HIRAGANA LETTER SMALL A
547 case '\u3043': // # HIRAGANA LETTER SMALL I
548 case '\u3045': // # HIRAGANA LETTER SMALL U
549 case '\u3047': // # HIRAGANA LETTER SMALL E
550 case '\u3049': // # HIRAGANA LETTER SMALL O
551 case '\u3063': // # HIRAGANA LETTER SMALL TU
552 case '\u3083': // # HIRAGANA LETTER SMALL YA
553 case '\u3085': // # HIRAGANA LETTER SMALL YU
554 case '\u3087': // # HIRAGANA LETTER SMALL YO
555 case '\u308E': // # HIRAGANA LETTER SMALL WA
556 case '\u3095': // # HIRAGANA LETTER SMALL KA
557 case '\u3096': // # HIRAGANA LETTER SMALL KE
558 case '\u309B': // # KATAKANA-HIRAGANA VOICED SOUND MARK
559 case '\u309C': // # KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
560 case '\u309D': // # HIRAGANA ITERATION MARK
561 case '\u309E': // # HIRAGANA VOICED ITERATION MARK
562 return false;
563 }
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 return true; // Hiragana (except small characters)
566 }
567 if (c >= '\u30A0' && c <= '\u30FF') {
Eric Fischer549d7242009-03-31 14:19:47 -0700568 if (!includeNonStarters) {
569 switch (c) {
570 case '\u30A0': // # KATAKANA-HIRAGANA DOUBLE HYPHEN
571 case '\u30A1': // # KATAKANA LETTER SMALL A
572 case '\u30A3': // # KATAKANA LETTER SMALL I
573 case '\u30A5': // # KATAKANA LETTER SMALL U
574 case '\u30A7': // # KATAKANA LETTER SMALL E
575 case '\u30A9': // # KATAKANA LETTER SMALL O
576 case '\u30C3': // # KATAKANA LETTER SMALL TU
577 case '\u30E3': // # KATAKANA LETTER SMALL YA
578 case '\u30E5': // # KATAKANA LETTER SMALL YU
579 case '\u30E7': // # KATAKANA LETTER SMALL YO
580 case '\u30EE': // # KATAKANA LETTER SMALL WA
581 case '\u30F5': // # KATAKANA LETTER SMALL KA
582 case '\u30F6': // # KATAKANA LETTER SMALL KE
583 case '\u30FB': // # KATAKANA MIDDLE DOT
584 case '\u30FC': // # KATAKANA-HIRAGANA PROLONGED SOUND MARK
585 case '\u30FD': // # KATAKANA ITERATION MARK
586 case '\u30FE': // # KATAKANA VOICED ITERATION MARK
587 return false;
588 }
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 return true; // Katakana (except small characters)
591 }
592 if (c >= '\u3400' && c <= '\u4DB5') {
593 return true; // CJK UNIFIED IDEOGRAPHS EXTENSION A
594 }
595 if (c >= '\u4E00' && c <= '\u9FBB') {
596 return true; // CJK UNIFIED IDEOGRAPHS
597 }
598 if (c >= '\uF900' && c <= '\uFAD9') {
599 return true; // CJK COMPATIBILITY IDEOGRAPHS
600 }
601 if (c >= '\uA000' && c <= '\uA48F') {
602 return true; // YI SYLLABLES
603 }
604 if (c >= '\uA490' && c <= '\uA4CF') {
605 return true; // YI RADICALS
606 }
607 if (c >= '\uFE62' && c <= '\uFE66') {
608 return true; // SMALL PLUS SIGN to SMALL EQUALS SIGN
609 }
610 if (c >= '\uFF10' && c <= '\uFF19') {
611 return true; // WIDE DIGITS
612 }
613
614 return false;
615 }
616
617/*
618 private static void dump(byte[] data, int count, String label) {
619 if (false) {
620 System.out.print(label);
621
622 for (int i = 0; i < count; i++)
623 System.out.print(" " + data[i]);
624
625 System.out.println();
626 }
627 }
628*/
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 private int out(CharSequence text, int start, int end,
631 int above, int below, int top, int bottom, int v,
632 float spacingmult, float spacingadd,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800633 LineHeightSpan[] chooseHt, int[] chooseHtv,
Doug Feltc982f602010-05-25 11:51:40 -0700634 Paint.FontMetricsInt fm, boolean hasTabOrEmoji,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 boolean needMultiply, int pstart, byte[] chdirs,
636 int dir, boolean easy, boolean last,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800637 boolean includePad, boolean trackPad,
638 char[] chs, float[] widths, int widthStart,
639 TextUtils.TruncateAt ellipsize, float ellipsisWidth,
640 float textWidth, TextPaint paint) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 int j = mLineCount;
642 int off = j * mColumns;
643 int want = off + mColumns + TOP;
644 int[] lines = mLines;
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 if (want >= lines.length) {
647 int nlen = ArrayUtils.idealIntArraySize(want + 1);
648 int[] grow = new int[nlen];
649 System.arraycopy(lines, 0, grow, 0, lines.length);
650 mLines = grow;
651 lines = grow;
652
653 Directions[] grow2 = new Directions[nlen];
654 System.arraycopy(mLineDirections, 0, grow2, 0,
655 mLineDirections.length);
656 mLineDirections = grow2;
657 }
658
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800659 if (chooseHt != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 fm.ascent = above;
661 fm.descent = below;
662 fm.top = top;
663 fm.bottom = bottom;
664
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800665 for (int i = 0; i < chooseHt.length; i++) {
666 if (chooseHt[i] instanceof LineHeightSpan.WithDensity) {
667 ((LineHeightSpan.WithDensity) chooseHt[i]).
668 chooseHeight(text, start, end, chooseHtv[i], v, fm, paint);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700669
670 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800671 chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674
675 above = fm.ascent;
676 below = fm.descent;
677 top = fm.top;
678 bottom = fm.bottom;
679 }
680
681 if (j == 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800682 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 mTopPadding = top - above;
684 }
685
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800686 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 above = top;
688 }
689 }
690 if (last) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800691 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 mBottomPadding = bottom - below;
693 }
694
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800695 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 below = bottom;
697 }
698 }
699
700 int extra;
701
702 if (needMultiply) {
Doug Felt10657582010-02-22 11:19:01 -0800703 double ex = (below - above) * (spacingmult - 1) + spacingadd;
704 if (ex >= 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800705 extra = (int)(ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -0800706 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800707 extra = -(int)(-ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -0800708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 } else {
710 extra = 0;
711 }
712
713 lines[off + START] = start;
714 lines[off + TOP] = v;
715 lines[off + DESCENT] = below + extra;
716
717 v += (below - above) + extra;
718 lines[off + mColumns + START] = end;
719 lines[off + mColumns + TOP] = v;
720
Doug Feltc982f602010-05-25 11:51:40 -0700721 if (hasTabOrEmoji)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 lines[off + TAB] |= TAB_MASK;
723
Doug Felt9f7a4442010-03-01 12:45:56 -0800724 lines[off + DIR] |= dir << DIR_SHIFT;
725 Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT;
726 // easy means all chars < the first RTL, so no emoji, no nothing
Doug Felt4e0c5e52010-03-15 16:56:02 -0700727 // XXX a run with no text or all spaces is easy but might be an empty
Doug Felt9f7a4442010-03-01 12:45:56 -0800728 // RTL paragraph. Make sure easy is false if this is the case.
729 if (easy) {
730 mLineDirections[j] = linedirs;
731 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800732 mLineDirections[j] = AndroidBidi.directions(dir, chdirs, widthStart, chs,
733 widthStart, end - start);
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800736 // If ellipsize is in marquee mode, do not apply ellipsis on the first line
737 if (ellipsize != null && (ellipsize != TextUtils.TruncateAt.MARQUEE || j != 0)) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800738 calculateEllipsis(start, end, widths, widthStart,
739 ellipsisWidth, ellipsize, j,
740 textWidth, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 mLineCount++;
744 return v;
745 }
746
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800747 private void calculateEllipsis(int lineStart, int lineEnd,
748 float[] widths, int widthStart,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 float avail, TextUtils.TruncateAt where,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800750 int line, float textWidth, TextPaint paint) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800752 if (textWidth <= avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 // Everything fits!
754 mLines[mColumns * line + ELLIPSIS_START] = 0;
755 mLines[mColumns * line + ELLIPSIS_COUNT] = 0;
756 return;
757 }
758
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800759 float ellipsisWidth = paint.measureText(HORIZONTAL_ELLIPSIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 int ellipsisStart, ellipsisCount;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800761 int len = lineEnd - lineStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
763 if (where == TextUtils.TruncateAt.START) {
764 float sum = 0;
765 int i;
766
767 for (i = len; i >= 0; i--) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800768 float w = widths[i - 1 + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800770 if (w + sum + ellipsisWidth > avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 break;
772 }
773
774 sum += w;
775 }
776
777 ellipsisStart = 0;
778 ellipsisCount = i;
779 } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE) {
780 float sum = 0;
781 int i;
782
783 for (i = 0; i < len; i++) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800784 float w = widths[i + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800786 if (w + sum + ellipsisWidth > avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 break;
788 }
789
790 sum += w;
791 }
792
793 ellipsisStart = i;
794 ellipsisCount = len - i;
795 } else /* where = TextUtils.TruncateAt.MIDDLE */ {
796 float lsum = 0, rsum = 0;
797 int left = 0, right = len;
798
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800799 float ravail = (avail - ellipsisWidth) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 for (right = len; right >= 0; right--) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800801 float w = widths[right - 1 + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802
803 if (w + rsum > ravail) {
804 break;
805 }
806
807 rsum += w;
808 }
809
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800810 float lavail = avail - ellipsisWidth - rsum;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 for (left = 0; left < right; left++) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800812 float w = widths[left + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813
814 if (w + lsum > lavail) {
815 break;
816 }
817
818 lsum += w;
819 }
820
821 ellipsisStart = left;
822 ellipsisCount = right - left;
823 }
824
825 mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart;
826 mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount;
827 }
828
Doug Felte8e45f22010-03-29 14:58:40 -0700829 // Override the base class so we can directly access our members,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 // rather than relying on member functions.
831 // The logic mirrors that of Layout.getLineForVertical
832 // FIXME: It may be faster to do a linear search for layouts without many lines.
Gilles Debunne66111472010-11-19 11:04:37 -0800833 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 public int getLineForVertical(int vertical) {
835 int high = mLineCount;
836 int low = -1;
837 int guess;
838 int[] lines = mLines;
839 while (high - low > 1) {
840 guess = (high + low) >> 1;
841 if (lines[mColumns * guess + TOP] > vertical){
842 high = guess;
843 } else {
844 low = guess;
845 }
846 }
847 if (low < 0) {
848 return 0;
849 } else {
850 return low;
851 }
852 }
853
Gilles Debunne66111472010-11-19 11:04:37 -0800854 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 public int getLineCount() {
856 return mLineCount;
857 }
858
Gilles Debunne66111472010-11-19 11:04:37 -0800859 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 public int getLineTop(int line) {
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800861 int top = mLines[mColumns * line + TOP];
862 if (mMaximumVisibleLineCount > 0 && line >= mMaximumVisibleLineCount &&
863 line != mLineCount) {
864 top += getBottomPadding();
865 }
866 return top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
868
Gilles Debunne66111472010-11-19 11:04:37 -0800869 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 public int getLineDescent(int line) {
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800871 int descent = mLines[mColumns * line + DESCENT];
872 if (mMaximumVisibleLineCount > 0 && line >= mMaximumVisibleLineCount - 1 &&
873 line != mLineCount) {
874 descent += getBottomPadding();
875 }
876 return descent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 }
878
Gilles Debunne66111472010-11-19 11:04:37 -0800879 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 public int getLineStart(int line) {
881 return mLines[mColumns * line + START] & START_MASK;
882 }
883
Gilles Debunne66111472010-11-19 11:04:37 -0800884 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 public int getParagraphDirection(int line) {
886 return mLines[mColumns * line + DIR] >> DIR_SHIFT;
887 }
888
Gilles Debunne66111472010-11-19 11:04:37 -0800889 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 public boolean getLineContainsTab(int line) {
891 return (mLines[mColumns * line + TAB] & TAB_MASK) != 0;
892 }
893
Gilles Debunne66111472010-11-19 11:04:37 -0800894 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 public final Directions getLineDirections(int line) {
896 return mLineDirections[line];
897 }
898
Gilles Debunne66111472010-11-19 11:04:37 -0800899 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 public int getTopPadding() {
901 return mTopPadding;
902 }
903
Gilles Debunne66111472010-11-19 11:04:37 -0800904 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public int getBottomPadding() {
906 return mBottomPadding;
907 }
908
909 @Override
910 public int getEllipsisCount(int line) {
911 if (mColumns < COLUMNS_ELLIPSIZE) {
912 return 0;
913 }
914
915 return mLines[mColumns * line + ELLIPSIS_COUNT];
916 }
917
918 @Override
919 public int getEllipsisStart(int line) {
920 if (mColumns < COLUMNS_ELLIPSIZE) {
921 return 0;
922 }
923
924 return mLines[mColumns * line + ELLIPSIS_START];
925 }
926
927 @Override
928 public int getEllipsizedWidth() {
929 return mEllipsizedWidth;
930 }
931
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800932 /**
933 * @hide
934 */
935 @Override
936 public void setMaximumVisibleLineCount(int line) {
937 mMaximumVisibleLineCount = line;
938 }
939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 private int mLineCount;
941 private int mTopPadding, mBottomPadding;
942 private int mColumns;
943 private int mEllipsizedWidth;
944
945 private static final int COLUMNS_NORMAL = 3;
946 private static final int COLUMNS_ELLIPSIZE = 5;
947 private static final int START = 0;
948 private static final int DIR = START;
949 private static final int TAB = START;
950 private static final int TOP = 1;
951 private static final int DESCENT = 2;
952 private static final int ELLIPSIS_START = 3;
953 private static final int ELLIPSIS_COUNT = 4;
954
955 private int[] mLines;
956 private Directions[] mLineDirections;
Gilles Debunne0a4db3c2011-01-14 12:12:04 -0800957 private int mMaximumVisibleLineCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958
959 private static final int START_MASK = 0x1FFFFFFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 private static final int DIR_SHIFT = 30;
961 private static final int TAB_MASK = 0x20000000;
962
Doug Feltc982f602010-05-25 11:51:40 -0700963 private static final int TAB_INCREMENT = 20; // same as Layout, but that's private
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800965 private static final char CHAR_FIRST_CJK = '\u2E80';
966
967 private static final char CHAR_NEW_LINE = '\n';
968 private static final char CHAR_TAB = '\t';
969 private static final char CHAR_SPACE = ' ';
970 private static final char CHAR_DOT = '.';
971 private static final char CHAR_COMMA = ',';
972 private static final char CHAR_COLON = ':';
973 private static final char CHAR_SEMICOLON = ';';
974 private static final char CHAR_SLASH = '/';
975 private static final char CHAR_HYPHEN = '-';
976
977 private static final double EXTRA_ROUNDING = 0.5;
978 private static final String HORIZONTAL_ELLIPSIS = "\u2026"; // this is "..."
979
980 private static final int CHAR_FIRST_HIGH_SURROGATE = 0xD800;
981 private static final int CHAR_LAST_LOW_SURROGATE = 0xDFFF;
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 /*
Doug Felte8e45f22010-03-29 14:58:40 -0700984 * This is reused across calls to generate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 */
Doug Felte8e45f22010-03-29 14:58:40 -0700986 private MeasuredText mMeasured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 private Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt();
988}