blob: 3474c2b560908b0ab89a721661579ad7827321e7 [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
Raph Levien531c30c2015-04-30 16:29:59 -070019import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.graphics.Paint;
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -070021import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.text.style.LeadingMarginSpan;
Gilles Debunne66111472010-11-19 11:04:37 -080023import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.text.style.LineHeightSpan;
25import android.text.style.MetricAffectingSpan;
Doug Feltc982f602010-05-25 11:51:40 -070026import android.text.style.TabStopSpan;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -070027import android.util.Log;
Raph Levien39b4db72015-03-25 13:18:20 -070028import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Doug Feltcb3791202011-07-07 11:57:48 -070030import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050031import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070032
Raph Levien091dba22015-08-31 16:21:20 -070033import java.nio.ByteBuffer;
Anish Athalyec8f9e622014-07-21 15:26:34 -070034import java.util.Arrays;
Raph Levien4c1f12e2015-03-02 16:29:23 -080035import java.util.Locale;
Anish Athalyec8f9e622014-07-21 15:26:34 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
38 * StaticLayout is a Layout for text that will not be edited after it
39 * is laid out. Use {@link DynamicLayout} for text that may change.
40 * <p>This is used by widgets to control text layout. You should not need
41 * to use this class directly unless you are implementing your own widget
42 * or custom display object, or would be tempted to call
Doug Felt4e0c5e52010-03-15 16:56:02 -070043 * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int,
44 * float, float, android.graphics.Paint)
45 * Canvas.drawText()} directly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 */
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -080047public class StaticLayout extends Layout {
48
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -070049 static final String TAG = "StaticLayout";
50
Raph Leviend3ab6922015-03-02 14:30:53 -080051 /**
Raph Levien531c30c2015-04-30 16:29:59 -070052 * Builder for static layouts. The builder is a newer pattern for constructing
53 * StaticLayout objects and should be preferred over the constructors,
54 * particularly to access newer features. To build a static layout, first
55 * call {@link #obtain} with the required arguments (text, paint, and width),
56 * then call setters for optional parameters, and finally {@link #build}
57 * to build the StaticLayout object. Parameters not explicitly set will get
58 * default values.
Raph Leviend3ab6922015-03-02 14:30:53 -080059 */
60 public final static class Builder {
Raph Levien4c1f12e2015-03-02 16:29:23 -080061 private Builder() {
62 mNativePtr = nNewBuilder();
63 }
64
Raph Levien531c30c2015-04-30 16:29:59 -070065 /**
66 * Obtain a builder for constructing StaticLayout objects
67 *
68 * @param source The text to be laid out, optionally with spans
69 * @param start The index of the start of the text
70 * @param end The index + 1 of the end of the text
71 * @param paint The base paint used for layout
72 * @param width The width in pixels
73 * @return a builder object used for constructing the StaticLayout
74 */
Raph Levienebd66ca2015-04-30 15:27:57 -070075 public static Builder obtain(CharSequence source, int start, int end, TextPaint paint,
76 int width) {
Raph Levien39b4db72015-03-25 13:18:20 -070077 Builder b = sPool.acquire();
Raph Leviend3ab6922015-03-02 14:30:53 -080078 if (b == null) {
79 b = new Builder();
80 }
81
82 // set default initial values
Raph Levien39b4db72015-03-25 13:18:20 -070083 b.mText = source;
84 b.mStart = start;
85 b.mEnd = end;
Raph Levienebd66ca2015-04-30 15:27:57 -070086 b.mPaint = paint;
Raph Levien39b4db72015-03-25 13:18:20 -070087 b.mWidth = width;
88 b.mAlignment = Alignment.ALIGN_NORMAL;
Raph Leviend3ab6922015-03-02 14:30:53 -080089 b.mTextDir = TextDirectionHeuristics.FIRSTSTRONG_LTR;
90 b.mSpacingMult = 1.0f;
91 b.mSpacingAdd = 0.0f;
92 b.mIncludePad = true;
Raph Levien39b4db72015-03-25 13:18:20 -070093 b.mEllipsizedWidth = width;
Raph Leviend3ab6922015-03-02 14:30:53 -080094 b.mEllipsize = null;
95 b.mMaxLines = Integer.MAX_VALUE;
Raph Levien3bd60c72015-05-06 14:26:35 -070096 b.mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE;
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070097 b.mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -070098 b.mJustificationMode = Layout.JUSTIFICATION_MODE_NONE;
Raph Leviend3ab6922015-03-02 14:30:53 -080099
100 b.mMeasuredText = MeasuredText.obtain();
101 return b;
102 }
103
Raph Levien39b4db72015-03-25 13:18:20 -0700104 private static void recycle(Builder b) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800105 b.mPaint = null;
106 b.mText = null;
107 MeasuredText.recycle(b.mMeasuredText);
Raph Levien3bd60c72015-05-06 14:26:35 -0700108 b.mMeasuredText = null;
Raph Levien2ea52902015-07-01 14:39:31 -0700109 b.mLeftIndents = null;
110 b.mRightIndents = null;
Raph Levien3bd60c72015-05-06 14:26:35 -0700111 nFinishBuilder(b.mNativePtr);
Raph Levien39b4db72015-03-25 13:18:20 -0700112 sPool.release(b);
Raph Leviend3ab6922015-03-02 14:30:53 -0800113 }
114
115 // release any expensive state
116 /* package */ void finish() {
Raph Levien4c1f12e2015-03-02 16:29:23 -0800117 nFinishBuilder(mNativePtr);
Raph Levien22ba7862015-07-29 12:34:13 -0700118 mText = null;
119 mPaint = null;
120 mLeftIndents = null;
121 mRightIndents = null;
Raph Leviend3ab6922015-03-02 14:30:53 -0800122 mMeasuredText.finish();
123 }
124
125 public Builder setText(CharSequence source) {
126 return setText(source, 0, source.length());
127 }
128
Raph Levien531c30c2015-04-30 16:29:59 -0700129 /**
130 * Set the text. Only useful when re-using the builder, which is done for
131 * the internal implementation of {@link DynamicLayout} but not as part
132 * of normal {@link StaticLayout} usage.
133 *
134 * @param source The text to be laid out, optionally with spans
135 * @param start The index of the start of the text
136 * @param end The index + 1 of the end of the text
137 * @return this builder, useful for chaining
138 *
139 * @hide
140 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800141 public Builder setText(CharSequence source, int start, int end) {
142 mText = source;
143 mStart = start;
144 mEnd = end;
145 return this;
146 }
147
Raph Levien531c30c2015-04-30 16:29:59 -0700148 /**
149 * Set the paint. Internal for reuse cases only.
150 *
151 * @param paint The base paint used for layout
152 * @return this builder, useful for chaining
153 *
154 * @hide
155 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800156 public Builder setPaint(TextPaint paint) {
157 mPaint = paint;
158 return this;
159 }
160
Raph Levien531c30c2015-04-30 16:29:59 -0700161 /**
162 * Set the width. Internal for reuse cases only.
163 *
164 * @param width The width in pixels
165 * @return this builder, useful for chaining
166 *
167 * @hide
168 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800169 public Builder setWidth(int width) {
170 mWidth = width;
171 if (mEllipsize == null) {
172 mEllipsizedWidth = width;
173 }
174 return this;
175 }
176
Raph Levien531c30c2015-04-30 16:29:59 -0700177 /**
178 * Set the alignment. The default is {@link Layout.Alignment#ALIGN_NORMAL}.
179 *
180 * @param alignment Alignment for the resulting {@link StaticLayout}
181 * @return this builder, useful for chaining
182 */
Raph Levien39b4db72015-03-25 13:18:20 -0700183 public Builder setAlignment(Alignment alignment) {
184 mAlignment = alignment;
185 return this;
186 }
187
Raph Levien531c30c2015-04-30 16:29:59 -0700188 /**
189 * Set the text direction heuristic. The text direction heuristic is used to
190 * resolve text direction based per-paragraph based on the input text. The default is
191 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR}.
192 *
193 * @param textDir text direction heuristic for resolving BiDi behavior.
194 * @return this builder, useful for chaining
195 */
Raph Leviena6a08282015-06-03 13:20:45 -0700196 public Builder setTextDirection(TextDirectionHeuristic textDir) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800197 mTextDir = textDir;
198 return this;
199 }
200
Raph Levien531c30c2015-04-30 16:29:59 -0700201 /**
202 * Set line spacing parameters. The default is 0.0 for {@code spacingAdd}
203 * and 1.0 for {@code spacingMult}.
204 *
205 * @param spacingAdd line spacing add
206 * @param spacingMult line spacing multiplier
207 * @return this builder, useful for chaining
208 * @see android.widget.TextView#setLineSpacing
209 */
210 public Builder setLineSpacing(float spacingAdd, float spacingMult) {
211 mSpacingAdd = spacingAdd;
Raph Leviend3ab6922015-03-02 14:30:53 -0800212 mSpacingMult = spacingMult;
213 return this;
214 }
215
Raph Levien531c30c2015-04-30 16:29:59 -0700216 /**
217 * Set whether to include extra space beyond font ascent and descent (which is
218 * needed to avoid clipping in some languages, such as Arabic and Kannada). The
219 * default is {@code true}.
220 *
221 * @param includePad whether to include padding
222 * @return this builder, useful for chaining
223 * @see android.widget.TextView#setIncludeFontPadding
224 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800225 public Builder setIncludePad(boolean includePad) {
226 mIncludePad = includePad;
227 return this;
228 }
229
Raph Levien531c30c2015-04-30 16:29:59 -0700230 /**
231 * Set the width as used for ellipsizing purposes, if it differs from the
232 * normal layout width. The default is the {@code width}
233 * passed to {@link #obtain}.
234 *
235 * @param ellipsizedWidth width used for ellipsizing, in pixels
236 * @return this builder, useful for chaining
237 * @see android.widget.TextView#setEllipsize
238 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800239 public Builder setEllipsizedWidth(int ellipsizedWidth) {
240 mEllipsizedWidth = ellipsizedWidth;
241 return this;
242 }
243
Raph Levien531c30c2015-04-30 16:29:59 -0700244 /**
245 * Set ellipsizing on the layout. Causes words that are longer than the view
246 * is wide, or exceeding the number of lines (see #setMaxLines) in the case
247 * of {@link android.text.TextUtils.TruncateAt#END} or
248 * {@link android.text.TextUtils.TruncateAt#MARQUEE}, to be ellipsized instead
249 * of broken. The default is
250 * {@code null}, indicating no ellipsis is to be applied.
251 *
252 * @param ellipsize type of ellipsis behavior
253 * @return this builder, useful for chaining
254 * @see android.widget.TextView#setEllipsize
255 */
256 public Builder setEllipsize(@Nullable TextUtils.TruncateAt ellipsize) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800257 mEllipsize = ellipsize;
258 return this;
259 }
260
Raph Levien531c30c2015-04-30 16:29:59 -0700261 /**
262 * Set maximum number of lines. This is particularly useful in the case of
263 * ellipsizing, where it changes the layout of the last line. The default is
264 * unlimited.
265 *
266 * @param maxLines maximum number of lines in the layout
267 * @return this builder, useful for chaining
268 * @see android.widget.TextView#setMaxLines
269 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800270 public Builder setMaxLines(int maxLines) {
271 mMaxLines = maxLines;
272 return this;
273 }
274
Raph Levien531c30c2015-04-30 16:29:59 -0700275 /**
276 * Set break strategy, useful for selecting high quality or balanced paragraph
277 * layout options. The default is {@link Layout#BREAK_STRATEGY_SIMPLE}.
278 *
279 * @param breakStrategy break strategy for paragraph layout
280 * @return this builder, useful for chaining
281 * @see android.widget.TextView#setBreakStrategy
282 */
Raph Levien39b4db72015-03-25 13:18:20 -0700283 public Builder setBreakStrategy(@BreakStrategy int breakStrategy) {
284 mBreakStrategy = breakStrategy;
285 return this;
286 }
287
Raph Levien531c30c2015-04-30 16:29:59 -0700288 /**
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700289 * Set hyphenation frequency, to control the amount of automatic hyphenation used. The
290 * default is {@link Layout#HYPHENATION_FREQUENCY_NONE}.
291 *
292 * @param hyphenationFrequency hyphenation frequency for the paragraph
293 * @return this builder, useful for chaining
294 * @see android.widget.TextView#setHyphenationFrequency
295 */
296 public Builder setHyphenationFrequency(@HyphenationFrequency int hyphenationFrequency) {
297 mHyphenationFrequency = hyphenationFrequency;
298 return this;
299 }
300
301 /**
Raph Levien531c30c2015-04-30 16:29:59 -0700302 * Set indents. Arguments are arrays holding an indent amount, one per line, measured in
303 * pixels. For lines past the last element in the array, the last element repeats.
304 *
305 * @param leftIndents array of indent values for left margin, in pixels
306 * @param rightIndents array of indent values for right margin, in pixels
307 * @return this builder, useful for chaining
Raph Levien531c30c2015-04-30 16:29:59 -0700308 */
Raph Leviene319d5a2015-04-14 23:51:07 -0700309 public Builder setIndents(int[] leftIndents, int[] rightIndents) {
Raph Levien2ea52902015-07-01 14:39:31 -0700310 mLeftIndents = leftIndents;
311 mRightIndents = rightIndents;
Raph Leviene319d5a2015-04-14 23:51:07 -0700312 int leftLen = leftIndents == null ? 0 : leftIndents.length;
313 int rightLen = rightIndents == null ? 0 : rightIndents.length;
314 int[] indents = new int[Math.max(leftLen, rightLen)];
315 for (int i = 0; i < indents.length; i++) {
316 int leftMargin = i < leftLen ? leftIndents[i] : 0;
317 int rightMargin = i < rightLen ? rightIndents[i] : 0;
318 indents[i] = leftMargin + rightMargin;
319 }
320 nSetIndents(mNativePtr, indents);
321 return this;
322 }
323
Raph Levien70616ec2015-03-04 10:41:30 -0800324 /**
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700325 * Set paragraph justification mode. The default value is
326 * {@link Layout#JUSTIFICATION_MODE_NONE}. If the last line is too short for justification,
327 * the last line will be displayed with the alignment set by {@link #setAlignment}.
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900328 *
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700329 * @param justificationMode justification mode for the paragraph.
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900330 * @return this builder, useful for chaining.
331 */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700332 public Builder setJustificationMode(@JustificationMode int justificationMode) {
333 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900334 return this;
335 }
336
Siyamed Sinir442c1512017-07-24 12:18:27 -0700337 /**
338 * Sets whether the line spacing should be applied for the last line. Default value is
339 * {@code false}.
340 *
341 * @hide
342 */
343 /* package */ Builder setAddLastLineLineSpacing(boolean value) {
344 mAddLastLineLineSpacing = value;
345 return this;
346 }
347
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700348 private long[] getHyphenators(LocaleList locales) {
349 final int length = locales.size();
350 final long[] result = new long[length];
351 for (int i = 0; i < length; i++) {
352 final Locale locale = locales.get(i);
353 result[i] = Hyphenator.get(locale).getNativePtr();
354 }
355 return result;
356 }
357
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900358 /**
Raph Levien70616ec2015-03-04 10:41:30 -0800359 * Measurement and break iteration is done in native code. The protocol for using
360 * the native code is as follows.
361 *
Raph Levien26d443a2015-03-30 14:18:32 -0700362 * For each paragraph, do a nSetupParagraph, which sets paragraph text, line width, tab
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700363 * stops, break strategy, and hyphenation frequency (and possibly other parameters in the
364 * future).
Raph Levienc94f7422015-03-06 19:19:48 -0800365 *
366 * Then, for each run within the paragraph:
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700367 * - setLocales (this must be done at least for the first run, optional afterwards)
Raph Levien70616ec2015-03-04 10:41:30 -0800368 * - one of the following, depending on the type of run:
369 * + addStyleRun (a text run, to be measured in native code)
370 * + addMeasuredRun (a run already measured in Java, passed into native code)
371 * + addReplacementRun (a replacement run, width is given)
372 *
373 * After measurement, nGetWidths() is valid if the widths are needed (eg for ellipsis).
374 * Run nComputeLineBreaks() to obtain line breaks for the paragraph.
375 *
376 * After all paragraphs, call finish() to release expensive buffers.
377 */
378
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700379 private void setLocales(LocaleList locales) {
380 if (!locales.equals(mLocales)) {
381 nSetLocales(mNativePtr, locales.toLanguageTags(), getHyphenators(locales));
382 mLocales = locales;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800383 }
384 }
385
Raph Levien70616ec2015-03-04 10:41:30 -0800386 /* package */ float addStyleRun(TextPaint paint, int start, int end, boolean isRtl) {
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700387 setLocales(paint.getTextLocales());
Raph Levien70616ec2015-03-04 10:41:30 -0800388 return nAddStyleRun(mNativePtr, paint.getNativeInstance(), paint.mNativeTypeface,
389 start, end, isRtl);
390 }
391
392 /* package */ void addMeasuredRun(int start, int end, float[] widths) {
393 nAddMeasuredRun(mNativePtr, start, end, widths);
394 }
395
396 /* package */ void addReplacementRun(int start, int end, float width) {
397 nAddReplacementRun(mNativePtr, start, end, width);
398 }
399
Raph Levien531c30c2015-04-30 16:29:59 -0700400 /**
401 * Build the {@link StaticLayout} after options have been set.
402 *
403 * <p>Note: the builder object must not be reused in any way after calling this
404 * method. Setting parameters after calling this method, or calling it a second
405 * time on the same builder object, will likely lead to unexpected results.
406 *
407 * @return the newly constructed {@link StaticLayout} object
408 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800409 public StaticLayout build() {
Raph Levien39b4db72015-03-25 13:18:20 -0700410 StaticLayout result = new StaticLayout(this);
411 Builder.recycle(this);
Raph Leviend3ab6922015-03-02 14:30:53 -0800412 return result;
413 }
414
Raph Levien4c1f12e2015-03-02 16:29:23 -0800415 @Override
416 protected void finalize() throws Throwable {
417 try {
418 nFreeBuilder(mNativePtr);
419 } finally {
420 super.finalize();
421 }
422 }
423
424 /* package */ long mNativePtr;
425
Raph Leviend3ab6922015-03-02 14:30:53 -0800426 CharSequence mText;
427 int mStart;
428 int mEnd;
429 TextPaint mPaint;
430 int mWidth;
Raph Levien39b4db72015-03-25 13:18:20 -0700431 Alignment mAlignment;
Raph Leviend3ab6922015-03-02 14:30:53 -0800432 TextDirectionHeuristic mTextDir;
433 float mSpacingMult;
434 float mSpacingAdd;
435 boolean mIncludePad;
436 int mEllipsizedWidth;
437 TextUtils.TruncateAt mEllipsize;
438 int mMaxLines;
Raph Levien39b4db72015-03-25 13:18:20 -0700439 int mBreakStrategy;
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700440 int mHyphenationFrequency;
Raph Levien2ea52902015-07-01 14:39:31 -0700441 int[] mLeftIndents;
442 int[] mRightIndents;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700443 int mJustificationMode;
Siyamed Sinir442c1512017-07-24 12:18:27 -0700444 boolean mAddLastLineLineSpacing;
Raph Leviend3ab6922015-03-02 14:30:53 -0800445
446 Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt();
447
448 // This will go away and be subsumed by native builder code
449 MeasuredText mMeasuredText;
450
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700451 LocaleList mLocales;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800452
Raph Levien39b4db72015-03-25 13:18:20 -0700453 private static final SynchronizedPool<Builder> sPool = new SynchronizedPool<Builder>(3);
Raph Leviend3ab6922015-03-02 14:30:53 -0800454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public StaticLayout(CharSequence source, TextPaint paint,
457 int width,
458 Alignment align, float spacingmult, float spacingadd,
459 boolean includepad) {
460 this(source, 0, source.length(), paint, width, align,
461 spacingmult, spacingadd, includepad);
462 }
463
Doug Feltcb3791202011-07-07 11:57:48 -0700464 /**
465 * @hide
466 */
467 public StaticLayout(CharSequence source, TextPaint paint,
468 int width, Alignment align, TextDirectionHeuristic textDir,
469 float spacingmult, float spacingadd,
470 boolean includepad) {
471 this(source, 0, source.length(), paint, width, align, textDir,
472 spacingmult, spacingadd, includepad);
473 }
474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 public StaticLayout(CharSequence source, int bufstart, int bufend,
476 TextPaint paint, int outerwidth,
477 Alignment align,
478 float spacingmult, float spacingadd,
479 boolean includepad) {
480 this(source, bufstart, bufend, paint, outerwidth, align,
481 spacingmult, spacingadd, includepad, null, 0);
482 }
483
Doug Feltcb3791202011-07-07 11:57:48 -0700484 /**
485 * @hide
486 */
487 public StaticLayout(CharSequence source, int bufstart, int bufend,
488 TextPaint paint, int outerwidth,
489 Alignment align, TextDirectionHeuristic textDir,
490 float spacingmult, float spacingadd,
491 boolean includepad) {
492 this(source, bufstart, bufend, paint, outerwidth, align, textDir,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700493 spacingmult, spacingadd, includepad, null, 0, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700494}
495
496 public StaticLayout(CharSequence source, int bufstart, int bufend,
497 TextPaint paint, int outerwidth,
498 Alignment align,
499 float spacingmult, float spacingadd,
500 boolean includepad,
501 TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
502 this(source, bufstart, bufend, paint, outerwidth, align,
503 TextDirectionHeuristics.FIRSTSTRONG_LTR,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700504 spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700505 }
506
507 /**
508 * @hide
509 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public StaticLayout(CharSequence source, int bufstart, int bufend,
511 TextPaint paint, int outerwidth,
Doug Feltcb3791202011-07-07 11:57:48 -0700512 Alignment align, TextDirectionHeuristic textDir,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 float spacingmult, float spacingadd,
514 boolean includepad,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700515 TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 super((ellipsize == null)
Doug Felt4e0c5e52010-03-15 16:56:02 -0700517 ? source
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 : (source instanceof Spanned)
519 ? new SpannedEllipsizer(source)
520 : new Ellipsizer(source),
Doug Feltcb3791202011-07-07 11:57:48 -0700521 paint, outerwidth, align, textDir, spacingmult, spacingadd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
Raph Levienebd66ca2015-04-30 15:27:57 -0700523 Builder b = Builder.obtain(source, bufstart, bufend, paint, outerwidth)
Raph Levien39b4db72015-03-25 13:18:20 -0700524 .setAlignment(align)
Raph Leviena6a08282015-06-03 13:20:45 -0700525 .setTextDirection(textDir)
Raph Levien531c30c2015-04-30 16:29:59 -0700526 .setLineSpacing(spacingadd, spacingmult)
Raph Leviend3ab6922015-03-02 14:30:53 -0800527 .setIncludePad(includepad)
528 .setEllipsizedWidth(ellipsizedWidth)
529 .setEllipsize(ellipsize)
530 .setMaxLines(maxLines);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /*
532 * This is annoying, but we can't refer to the layout until
533 * superclass construction is finished, and the superclass
534 * constructor wants the reference to the display text.
Doug Felt4e0c5e52010-03-15 16:56:02 -0700535 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 * This will break if the superclass constructor ever actually
537 * cares about the content instead of just holding the reference.
538 */
539 if (ellipsize != null) {
540 Ellipsizer e = (Ellipsizer) getText();
541
542 e.mLayout = this;
543 e.mWidth = ellipsizedWidth;
544 e.mMethod = ellipsize;
545 mEllipsizedWidth = ellipsizedWidth;
546
547 mColumns = COLUMNS_ELLIPSIZE;
548 } else {
549 mColumns = COLUMNS_NORMAL;
550 mEllipsizedWidth = outerwidth;
551 }
552
Siyamed Siniraf398512017-07-25 19:08:42 -0700553 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2);
554 mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700555 mMaximumVisibleLineCount = maxLines;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
Raph Levien70616ec2015-03-04 10:41:30 -0800557 generate(b, b.mIncludePad, b.mIncludePad);
Doug Felte8e45f22010-03-29 14:58:40 -0700558
Raph Leviend3ab6922015-03-02 14:30:53 -0800559 Builder.recycle(b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700562 /* package */ StaticLayout(CharSequence text) {
563 super(text, null, 0, null, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
565 mColumns = COLUMNS_ELLIPSIZE;
Siyamed Siniraf398512017-07-25 19:08:42 -0700566 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2);
567 mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569
Raph Levien39b4db72015-03-25 13:18:20 -0700570 private StaticLayout(Builder b) {
571 super((b.mEllipsize == null)
572 ? b.mText
573 : (b.mText instanceof Spanned)
574 ? new SpannedEllipsizer(b.mText)
575 : new Ellipsizer(b.mText),
576 b.mPaint, b.mWidth, b.mAlignment, b.mSpacingMult, b.mSpacingAdd);
577
578 if (b.mEllipsize != null) {
579 Ellipsizer e = (Ellipsizer) getText();
580
581 e.mLayout = this;
582 e.mWidth = b.mEllipsizedWidth;
583 e.mMethod = b.mEllipsize;
584 mEllipsizedWidth = b.mEllipsizedWidth;
585
586 mColumns = COLUMNS_ELLIPSIZE;
587 } else {
588 mColumns = COLUMNS_NORMAL;
589 mEllipsizedWidth = b.mWidth;
590 }
591
Siyamed Siniraf398512017-07-25 19:08:42 -0700592 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2);
593 mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns);
Raph Levien39b4db72015-03-25 13:18:20 -0700594 mMaximumVisibleLineCount = b.mMaxLines;
595
Raph Levien2ea52902015-07-01 14:39:31 -0700596 mLeftIndents = b.mLeftIndents;
597 mRightIndents = b.mRightIndents;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700598 setJustificationMode(b.mJustificationMode);
Raph Levien2ea52902015-07-01 14:39:31 -0700599
Raph Levien39b4db72015-03-25 13:18:20 -0700600 generate(b, b.mIncludePad, b.mIncludePad);
601 }
602
Raph Leviend3ab6922015-03-02 14:30:53 -0800603 /* package */ void generate(Builder b, boolean includepad, boolean trackpad) {
604 CharSequence source = b.mText;
605 int bufStart = b.mStart;
606 int bufEnd = b.mEnd;
607 TextPaint paint = b.mPaint;
608 int outerWidth = b.mWidth;
609 TextDirectionHeuristic textDir = b.mTextDir;
610 float spacingmult = b.mSpacingMult;
611 float spacingadd = b.mSpacingAdd;
612 float ellipsizedWidth = b.mEllipsizedWidth;
613 TextUtils.TruncateAt ellipsize = b.mEllipsize;
Siyamed Sinir442c1512017-07-24 12:18:27 -0700614 final boolean addLastLineSpacing = b.mAddLastLineLineSpacing;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800615 LineBreaks lineBreaks = new LineBreaks(); // TODO: move to builder to avoid allocation costs
Anish Athalyec8f9e622014-07-21 15:26:34 -0700616 // store span end locations
617 int[] spanEndCache = new int[4];
618 // store fontMetrics per span range
619 // must be a multiple of 4 (and > 0) (store top, bottom, ascent, and descent per range)
620 int[] fmCache = new int[4 * 4];
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700621 b.setLocales(paint.getTextLocales());
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 mLineCount = 0;
624
625 int v = 0;
626 boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
627
Raph Leviend3ab6922015-03-02 14:30:53 -0800628 Paint.FontMetricsInt fm = b.mFontMetricsInt;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800629 int[] chooseHtv = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
Raph Leviend3ab6922015-03-02 14:30:53 -0800631 MeasuredText measured = b.mMeasuredText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 Spanned spanned = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 if (source instanceof Spanned)
635 spanned = (Spanned) source;
636
Doug Felte8e45f22010-03-29 14:58:40 -0700637 int paraEnd;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800638 for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
639 paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
Doug Felte8e45f22010-03-29 14:58:40 -0700640 if (paraEnd < 0)
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800641 paraEnd = bufEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 else
Doug Felte8e45f22010-03-29 14:58:40 -0700643 paraEnd++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644
Anish Athalyec8f9e622014-07-21 15:26:34 -0700645 int firstWidthLineCount = 1;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800646 int firstWidth = outerWidth;
647 int restWidth = outerWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800649 LineHeightSpan[] chooseHt = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
651 if (spanned != null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -0700652 LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd,
Doug Felte8e45f22010-03-29 14:58:40 -0700653 LeadingMarginSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 for (int i = 0; i < sp.length; i++) {
Mark Wagner7b5676e2009-10-16 11:44:23 -0700655 LeadingMarginSpan lms = sp[i];
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800656 firstWidth -= sp[i].getLeadingMargin(true);
657 restWidth -= sp[i].getLeadingMargin(false);
Doug Feltcb3791202011-07-07 11:57:48 -0700658
Doug Feltc982f602010-05-25 11:51:40 -0700659 // LeadingMarginSpan2 is odd. The count affects all
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700660 // leading margin spans, not just this particular one
Doug Feltc982f602010-05-25 11:51:40 -0700661 if (lms instanceof LeadingMarginSpan2) {
662 LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700663 firstWidthLineCount = Math.max(firstWidthLineCount,
664 lms2.getLeadingMarginLineCount());
Mark Wagner7b5676e2009-10-16 11:44:23 -0700665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800668 chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700670 if (chooseHt.length == 0) {
671 chooseHt = null; // So that out() would not assume it has any contents
672 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800673 if (chooseHtv == null ||
674 chooseHtv.length < chooseHt.length) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500675 chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800678 for (int i = 0; i < chooseHt.length; i++) {
679 int o = spanned.getSpanStart(chooseHt[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680
Doug Felte8e45f22010-03-29 14:58:40 -0700681 if (o < paraStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 // starts in this layout, before the
683 // current paragraph
684
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800685 chooseHtv[i] = getLineTop(getLineForOffset(o));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 } else {
687 // starts in this paragraph
688
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800689 chooseHtv[i] = v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691 }
692 }
693 }
694
Raph Levien70616ec2015-03-04 10:41:30 -0800695 measured.setPara(source, paraStart, paraEnd, textDir, b);
Doug Felte8e45f22010-03-29 14:58:40 -0700696 char[] chs = measured.mChars;
697 float[] widths = measured.mWidths;
698 byte[] chdirs = measured.mLevels;
699 int dir = measured.mDir;
700 boolean easy = measured.mEasy;
Raph Levienc94f7422015-03-06 19:19:48 -0800701
702 // tab stop locations
703 int[] variableTabStops = null;
704 if (spanned != null) {
705 TabStopSpan[] spans = getParagraphSpans(spanned, paraStart,
706 paraEnd, TabStopSpan.class);
707 if (spans.length > 0) {
708 int[] stops = new int[spans.length];
709 for (int i = 0; i < spans.length; i++) {
710 stops[i] = spans[i].getTabStop();
711 }
712 Arrays.sort(stops, 0, stops.length);
713 variableTabStops = stops;
714 }
715 }
716
Raph Levienc94f7422015-03-06 19:19:48 -0800717 nSetupParagraph(b.mNativePtr, chs, paraEnd - paraStart,
718 firstWidth, firstWidthLineCount, restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900719 variableTabStops, TAB_INCREMENT, b.mBreakStrategy, b.mHyphenationFrequency,
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700720 // TODO: Support more justification mode, e.g. letter spacing, stretching.
721 b.mJustificationMode != Layout.JUSTIFICATION_MODE_NONE);
Raph Levien2ea52902015-07-01 14:39:31 -0700722 if (mLeftIndents != null || mRightIndents != null) {
723 // TODO(raph) performance: it would be better to do this once per layout rather
724 // than once per paragraph, but that would require a change to the native
725 // interface.
726 int leftLen = mLeftIndents == null ? 0 : mLeftIndents.length;
727 int rightLen = mRightIndents == null ? 0 : mRightIndents.length;
Siyamed Sinirf9a08862016-04-12 19:30:44 -0700728 int indentsLen = Math.max(1, Math.max(leftLen, rightLen) - mLineCount);
Raph Levien2ea52902015-07-01 14:39:31 -0700729 int[] indents = new int[indentsLen];
730 for (int i = 0; i < indentsLen; i++) {
731 int leftMargin = mLeftIndents == null ? 0 :
732 mLeftIndents[Math.min(i + mLineCount, leftLen - 1)];
733 int rightMargin = mRightIndents == null ? 0 :
734 mRightIndents[Math.min(i + mLineCount, rightLen - 1)];
735 indents[i] = leftMargin + rightMargin;
736 }
737 nSetIndents(b.mNativePtr, indents);
738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739
Anish Athalyec8f9e622014-07-21 15:26:34 -0700740 // measurement has to be done before performing line breaking
741 // but we don't want to recompute fontmetrics or span ranges the
742 // second time, so we cache those and then use those stored values
743 int fmCacheCount = 0;
744 int spanEndCacheCount = 0;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700745 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700746 if (fmCacheCount * 4 >= fmCache.length) {
747 int[] grow = new int[fmCacheCount * 4 * 2];
748 System.arraycopy(fmCache, 0, grow, 0, fmCacheCount * 4);
749 fmCache = grow;
750 }
751
752 if (spanEndCacheCount >= spanEndCache.length) {
753 int[] grow = new int[spanEndCacheCount * 2];
754 System.arraycopy(spanEndCache, 0, grow, 0, spanEndCacheCount);
755 spanEndCache = grow;
756 }
Doug Felte8e45f22010-03-29 14:58:40 -0700757
Gilles Debunnecd943a72012-06-07 17:54:47 -0700758 if (spanned == null) {
759 spanEnd = paraEnd;
Doug Felt23241882010-06-02 14:41:06 -0700760 int spanLen = spanEnd - spanStart;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700761 measured.addStyleRun(paint, spanLen, fm);
762 } else {
763 spanEnd = spanned.nextSpanTransition(spanStart, paraEnd,
764 MetricAffectingSpan.class);
765 int spanLen = spanEnd - spanStart;
766 MetricAffectingSpan[] spans =
Doug Felt23241882010-06-02 14:41:06 -0700767 spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
Gilles Debunnecd943a72012-06-07 17:54:47 -0700768 spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class);
769 measured.addStyleRun(paint, spans, spanLen, fm);
Doug Felt23241882010-06-02 14:41:06 -0700770 }
771
Anish Athalyec8f9e622014-07-21 15:26:34 -0700772 // the order of storage here (top, bottom, ascent, descent) has to match the code below
773 // where these values are retrieved
774 fmCache[fmCacheCount * 4 + 0] = fm.top;
775 fmCache[fmCacheCount * 4 + 1] = fm.bottom;
776 fmCache[fmCacheCount * 4 + 2] = fm.ascent;
777 fmCache[fmCacheCount * 4 + 3] = fm.descent;
778 fmCacheCount++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
Anish Athalyec8f9e622014-07-21 15:26:34 -0700780 spanEndCache[spanEndCacheCount] = spanEnd;
781 spanEndCacheCount++;
782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783
Raph Levien70616ec2015-03-04 10:41:30 -0800784 nGetWidths(b.mNativePtr, widths);
Raph Levienc94f7422015-03-06 19:19:48 -0800785 int breakCount = nComputeLineBreaks(b.mNativePtr, lineBreaks, lineBreaks.breaks,
786 lineBreaks.widths, lineBreaks.flags, lineBreaks.breaks.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
Anish Athalyec8f9e622014-07-21 15:26:34 -0700788 int[] breaks = lineBreaks.breaks;
789 float[] lineWidths = lineBreaks.widths;
Raph Levien26d443a2015-03-30 14:18:32 -0700790 int[] flags = lineBreaks.flags;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700791
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900792 final int remainingLineCount = mMaximumVisibleLineCount - mLineCount;
793 final boolean ellipsisMayBeApplied = ellipsize != null
794 && (ellipsize == TextUtils.TruncateAt.END
795 || (mMaximumVisibleLineCount == 1
796 && ellipsize != TextUtils.TruncateAt.MARQUEE));
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -0700797 if (0 < remainingLineCount && remainingLineCount < breakCount
798 && ellipsisMayBeApplied) {
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900799 // Calculate width and flag.
800 float width = 0;
801 int flag = 0;
802 for (int i = remainingLineCount - 1; i < breakCount; i++) {
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700803 if (i == breakCount - 1) {
804 width += lineWidths[i];
805 } else {
806 for (int j = (i == 0 ? 0 : breaks[i - 1]); j < breaks[i]; j++) {
807 width += widths[j];
808 }
809 }
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900810 flag |= flags[i] & TAB_MASK;
811 }
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700812 // Treat the last line and overflowed lines as a single line.
813 breaks[remainingLineCount - 1] = breaks[breakCount - 1];
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900814 lineWidths[remainingLineCount - 1] = width;
815 flags[remainingLineCount - 1] = flag;
816
817 breakCount = remainingLineCount;
818 }
819
Anish Athalyec8f9e622014-07-21 15:26:34 -0700820 // here is the offset of the starting character of the line we are currently measuring
821 int here = paraStart;
822
823 int fmTop = 0, fmBottom = 0, fmAscent = 0, fmDescent = 0;
824 int fmCacheIndex = 0;
825 int spanEndCacheIndex = 0;
826 int breakIndex = 0;
827 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
828 // retrieve end of span
829 spanEnd = spanEndCache[spanEndCacheIndex++];
830
831 // retrieve cached metrics, order matches above
832 fm.top = fmCache[fmCacheIndex * 4 + 0];
833 fm.bottom = fmCache[fmCacheIndex * 4 + 1];
834 fm.ascent = fmCache[fmCacheIndex * 4 + 2];
835 fm.descent = fmCache[fmCacheIndex * 4 + 3];
836 fmCacheIndex++;
837
838 if (fm.top < fmTop) {
839 fmTop = fm.top;
840 }
841 if (fm.ascent < fmAscent) {
842 fmAscent = fm.ascent;
843 }
844 if (fm.descent > fmDescent) {
845 fmDescent = fm.descent;
846 }
847 if (fm.bottom > fmBottom) {
848 fmBottom = fm.bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850
Anish Athalyec8f9e622014-07-21 15:26:34 -0700851 // skip breaks ending before current span range
852 while (breakIndex < breakCount && paraStart + breaks[breakIndex] < spanStart) {
853 breakIndex++;
854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
Anish Athalyec8f9e622014-07-21 15:26:34 -0700856 while (breakIndex < breakCount && paraStart + breaks[breakIndex] <= spanEnd) {
857 int endPos = paraStart + breaks[breakIndex];
858
Raph Levience4155a2015-03-11 11:02:33 -0700859 boolean moreChars = (endPos < bufEnd);
Raph Levien4c02e832014-12-12 11:17:01 -0800860
Anish Athalyec8f9e622014-07-21 15:26:34 -0700861 v = out(source, here, endPos,
862 fmAscent, fmDescent, fmTop, fmBottom,
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700863 v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, flags[breakIndex],
Anish Athalyec8f9e622014-07-21 15:26:34 -0700864 needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad,
Siyamed Sinir442c1512017-07-24 12:18:27 -0700865 addLastLineSpacing, chs, widths, paraStart, ellipsize,
866 ellipsizedWidth, lineWidths[breakIndex], paint, moreChars);
Anish Athalyec8f9e622014-07-21 15:26:34 -0700867
868 if (endPos < spanEnd) {
869 // preserve metrics for current span
870 fmTop = fm.top;
871 fmBottom = fm.bottom;
872 fmAscent = fm.ascent;
873 fmDescent = fm.descent;
874 } else {
875 fmTop = fmBottom = fmAscent = fmDescent = 0;
876 }
877
878 here = endPos;
879 breakIndex++;
880
Siyamed Sinir0745c722016-05-31 20:39:33 -0700881 if (mLineCount >= mMaximumVisibleLineCount && mEllipsized) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700882 return;
883 }
884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
886
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800887 if (paraEnd == bufEnd)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 break;
889 }
890
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700891 if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) &&
Fabrice Di Meglioad0b0512011-10-04 17:21:26 -0700892 mLineCount < mMaximumVisibleLineCount) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800893 // Log.e("text", "output last " + bufEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894
Raph Levien70616ec2015-03-04 10:41:30 -0800895 measured.setPara(source, bufEnd, bufEnd, textDir, b);
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 paint.getFontMetricsInt(fm);
898
899 v = out(source,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800900 bufEnd, bufEnd, fm.ascent, fm.descent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 fm.top, fm.bottom,
902 v,
903 spacingmult, spacingadd, null,
Raph Levien26d443a2015-03-30 14:18:32 -0700904 null, fm, 0,
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700905 needMultiply, measured.mLevels, measured.mDir, measured.mEasy, bufEnd,
Siyamed Sinir442c1512017-07-24 12:18:27 -0700906 includepad, trackpad, addLastLineSpacing, null,
Gilles Debunned300e752011-10-17 13:37:36 -0700907 null, bufStart, ellipsize,
908 ellipsizedWidth, 0, paint, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910 }
911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 private int out(CharSequence text, int start, int end,
913 int above, int below, int top, int bottom, int v,
914 float spacingmult, float spacingadd,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800915 LineHeightSpan[] chooseHt, int[] chooseHtv,
Raph Levien26d443a2015-03-30 14:18:32 -0700916 Paint.FontMetricsInt fm, int flags,
Gilles Debunned300e752011-10-17 13:37:36 -0700917 boolean needMultiply, byte[] chdirs, int dir,
918 boolean easy, int bufEnd, boolean includePad,
Siyamed Sinir442c1512017-07-24 12:18:27 -0700919 boolean trackPad, boolean addLastLineLineSpacing, char[] chs,
Gilles Debunned300e752011-10-17 13:37:36 -0700920 float[] widths, int widthStart, TextUtils.TruncateAt ellipsize,
921 float ellipsisWidth, float textWidth,
922 TextPaint paint, boolean moreChars) {
Siyamed Siniraf398512017-07-25 19:08:42 -0700923 final int j = mLineCount;
924 final int off = j * mColumns;
925 final int want = off + mColumns + TOP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 int[] lines = mLines;
927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (want >= lines.length) {
Siyamed Siniraf398512017-07-25 19:08:42 -0700929 final int[] grow = ArrayUtils.newUnpaddedIntArray(GrowingArrayUtils.growSize(want));
Adam Lesinski776abc22014-03-07 11:30:59 -0500930 System.arraycopy(lines, 0, grow, 0, lines.length);
931 mLines = grow;
932 lines = grow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934
Siyamed Siniraf398512017-07-25 19:08:42 -0700935 if (j >= mLineDirections.length) {
936 final Directions[] grow = ArrayUtils.newUnpaddedArray(Directions.class,
937 GrowingArrayUtils.growSize(j));
938 System.arraycopy(mLineDirections, 0, grow, 0, mLineDirections.length);
939 mLineDirections = grow;
940 }
941
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800942 if (chooseHt != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 fm.ascent = above;
944 fm.descent = below;
945 fm.top = top;
946 fm.bottom = bottom;
947
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800948 for (int i = 0; i < chooseHt.length; i++) {
949 if (chooseHt[i] instanceof LineHeightSpan.WithDensity) {
950 ((LineHeightSpan.WithDensity) chooseHt[i]).
951 chooseHeight(text, start, end, chooseHtv[i], v, fm, paint);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700952
953 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800954 chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957
958 above = fm.ascent;
959 below = fm.descent;
960 top = fm.top;
961 bottom = fm.bottom;
962 }
963
Raph Leviend97b0972014-04-24 12:51:35 -0700964 boolean firstLine = (j == 0);
965 boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount);
Siyamed Sinir0745c722016-05-31 20:39:33 -0700966
967 if (ellipsize != null) {
968 // If there is only one line, then do any type of ellipsis except when it is MARQUEE
969 // if there are multiple lines, just allow END ellipsis on the last line
970 boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount);
971
972 boolean doEllipsis =
973 (((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) &&
974 ellipsize != TextUtils.TruncateAt.MARQUEE) ||
975 (!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) &&
976 ellipsize == TextUtils.TruncateAt.END);
977 if (doEllipsis) {
978 calculateEllipsis(start, end, widths, widthStart,
979 ellipsisWidth, ellipsize, j,
980 textWidth, paint, forceEllipsis);
981 }
982 }
983
Siyamed Sinirfb0b2dc2017-07-26 22:08:24 -0700984 final boolean lastLine;
985 if (mEllipsized) {
986 lastLine = true;
987 } else {
988 final boolean lastCharIsNewLine = widthStart != bufEnd && bufEnd > 0
989 && text.charAt(bufEnd - 1) == CHAR_NEW_LINE;
990 if (end == bufEnd && !lastCharIsNewLine) {
991 lastLine = true;
992 } else if (start == bufEnd && lastCharIsNewLine) {
993 lastLine = true;
994 } else {
995 lastLine = false;
996 }
997 }
Raph Leviend97b0972014-04-24 12:51:35 -0700998
999 if (firstLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001000 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 mTopPadding = top - above;
1002 }
1003
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001004 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 above = top;
1006 }
1007 }
Raph Leviend97b0972014-04-24 12:51:35 -07001008
1009 int extra;
1010
1011 if (lastLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001012 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 mBottomPadding = bottom - below;
1014 }
1015
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001016 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 below = bottom;
1018 }
1019 }
1020
Siyamed Sinir442c1512017-07-24 12:18:27 -07001021 if (needMultiply && (addLastLineLineSpacing || !lastLine)) {
Doug Felt10657582010-02-22 11:19:01 -08001022 double ex = (below - above) * (spacingmult - 1) + spacingadd;
1023 if (ex >= 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001024 extra = (int)(ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -08001025 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001026 extra = -(int)(-ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -08001027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 } else {
1029 extra = 0;
1030 }
1031
1032 lines[off + START] = start;
1033 lines[off + TOP] = v;
1034 lines[off + DESCENT] = below + extra;
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001035 lines[off + EXTRA] = extra;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036
Siyamed Sinir0745c722016-05-31 20:39:33 -07001037 // special case for non-ellipsized last visible line when maxLines is set
1038 // store the height as if it was ellipsized
1039 if (!mEllipsized && currentLineIsTheLastVisibleOne) {
1040 // below calculation as if it was the last line
1041 int maxLineBelow = includePad ? bottom : below;
1042 // similar to the calculation of v below, without the extra.
1043 mMaxLineHeight = v + (maxLineBelow - above);
1044 }
1045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 v += (below - above) + extra;
1047 lines[off + mColumns + START] = end;
1048 lines[off + mColumns + TOP] = v;
1049
Raph Levien26d443a2015-03-30 14:18:32 -07001050 // TODO: could move TAB to share same column as HYPHEN, simplifying this code and gaining
1051 // one bit for start field
1052 lines[off + TAB] |= flags & TAB_MASK;
1053 lines[off + HYPHEN] = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054
Doug Felt9f7a4442010-03-01 12:45:56 -08001055 lines[off + DIR] |= dir << DIR_SHIFT;
1056 Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT;
1057 // easy means all chars < the first RTL, so no emoji, no nothing
Doug Felt4e0c5e52010-03-15 16:56:02 -07001058 // XXX a run with no text or all spaces is easy but might be an empty
Doug Felt9f7a4442010-03-01 12:45:56 -08001059 // RTL paragraph. Make sure easy is false if this is the case.
1060 if (easy) {
1061 mLineDirections[j] = linedirs;
1062 } else {
Gilles Debunnef3fa0cd2011-02-03 14:17:05 -08001063 mLineDirections[j] = AndroidBidi.directions(dir, chdirs, start - widthStart, chs,
1064 start - widthStart, end - start);
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 mLineCount++;
1068 return v;
1069 }
1070
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001071 private void calculateEllipsis(int lineStart, int lineEnd,
1072 float[] widths, int widthStart,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 float avail, TextUtils.TruncateAt where,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001074 int line, float textWidth, TextPaint paint,
1075 boolean forceEllipsis) {
Selim Cinek365ec092017-03-09 00:10:52 -08001076 avail -= getTotalInsets(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001077 if (textWidth <= avail && !forceEllipsis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 // Everything fits!
1079 mLines[mColumns * line + ELLIPSIS_START] = 0;
1080 mLines[mColumns * line + ELLIPSIS_COUNT] = 0;
1081 return;
1082 }
1083
Roozbeh Pournader9ea756f2017-07-25 11:20:29 -07001084 float ellipsisWidth = paint.measureText(TextUtils.getEllipsisString(where));
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001085 int ellipsisStart = 0;
1086 int ellipsisCount = 0;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001087 int len = lineEnd - lineStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001089 // We only support start ellipsis on a single line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (where == TextUtils.TruncateAt.START) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001091 if (mMaximumVisibleLineCount == 1) {
1092 float sum = 0;
1093 int i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094
Keisuke Kuroyanagied2eea12015-04-14 18:18:35 +09001095 for (i = len; i > 0; i--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001096 float w = widths[i - 1 + lineStart - widthStart];
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001097 if (w + sum + ellipsisWidth > avail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001098 while (i < len && widths[i + lineStart - widthStart] == 0.0f) {
1099 i++;
1100 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001101 break;
1102 }
1103
1104 sum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001107 ellipsisStart = 0;
1108 ellipsisCount = i;
1109 } else {
1110 if (Log.isLoggable(TAG, Log.WARN)) {
1111 Log.w(TAG, "Start Ellipsis only supported with one line");
1112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001114 } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE ||
1115 where == TextUtils.TruncateAt.END_SMALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 float sum = 0;
1117 int i;
1118
1119 for (i = 0; i < len; i++) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001120 float w = widths[i + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001122 if (w + sum + ellipsisWidth > avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 break;
1124 }
1125
1126 sum += w;
1127 }
1128
1129 ellipsisStart = i;
1130 ellipsisCount = len - i;
Fabrice Di Meglioaef455f2011-08-29 15:39:11 -07001131 if (forceEllipsis && ellipsisCount == 0 && len > 0) {
1132 ellipsisStart = len - 1;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001133 ellipsisCount = 1;
1134 }
1135 } else {
1136 // where = TextUtils.TruncateAt.MIDDLE We only support middle ellipsis on a single line
1137 if (mMaximumVisibleLineCount == 1) {
1138 float lsum = 0, rsum = 0;
1139 int left = 0, right = len;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001141 float ravail = (avail - ellipsisWidth) / 2;
Raph Levien0e3c5e82014-12-04 13:26:07 -08001142 for (right = len; right > 0; right--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001143 float w = widths[right - 1 + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001145 if (w + rsum > ravail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001146 while (right < len && widths[right + lineStart - widthStart] == 0.0f) {
1147 right++;
1148 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001149 break;
1150 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001151 rsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001154 float lavail = avail - ellipsisWidth - rsum;
1155 for (left = 0; left < right; left++) {
1156 float w = widths[left + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001158 if (w + lsum > lavail) {
1159 break;
1160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001162 lsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001165 ellipsisStart = left;
1166 ellipsisCount = right - left;
1167 } else {
1168 if (Log.isLoggable(TAG, Log.WARN)) {
1169 Log.w(TAG, "Middle Ellipsis only supported with one line");
1170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
Siyamed Sinir0745c722016-05-31 20:39:33 -07001173 mEllipsized = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart;
1175 mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount;
1176 }
1177
Selim Cinek365ec092017-03-09 00:10:52 -08001178 private float getTotalInsets(int line) {
1179 int totalIndent = 0;
1180 if (mLeftIndents != null) {
1181 totalIndent = mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1182 }
1183 if (mRightIndents != null) {
1184 totalIndent += mRightIndents[Math.min(line, mRightIndents.length - 1)];
1185 }
1186 return totalIndent;
1187 }
1188
Doug Felte8e45f22010-03-29 14:58:40 -07001189 // Override the base class so we can directly access our members,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 // rather than relying on member functions.
1191 // The logic mirrors that of Layout.getLineForVertical
1192 // FIXME: It may be faster to do a linear search for layouts without many lines.
Gilles Debunne66111472010-11-19 11:04:37 -08001193 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 public int getLineForVertical(int vertical) {
1195 int high = mLineCount;
1196 int low = -1;
1197 int guess;
1198 int[] lines = mLines;
1199 while (high - low > 1) {
1200 guess = (high + low) >> 1;
1201 if (lines[mColumns * guess + TOP] > vertical){
1202 high = guess;
1203 } else {
1204 low = guess;
1205 }
1206 }
1207 if (low < 0) {
1208 return 0;
1209 } else {
1210 return low;
1211 }
1212 }
1213
Gilles Debunne66111472010-11-19 11:04:37 -08001214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 public int getLineCount() {
1216 return mLineCount;
1217 }
1218
Gilles Debunne66111472010-11-19 11:04:37 -08001219 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 public int getLineTop(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001221 return mLines[mColumns * line + TOP];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001224 /**
1225 * @hide
1226 */
1227 @Override
1228 public int getLineExtra(int line) {
1229 return mLines[mColumns * line + EXTRA];
1230 }
1231
Gilles Debunne66111472010-11-19 11:04:37 -08001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public int getLineDescent(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001234 return mLines[mColumns * line + DESCENT];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236
Gilles Debunne66111472010-11-19 11:04:37 -08001237 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 public int getLineStart(int line) {
1239 return mLines[mColumns * line + START] & START_MASK;
1240 }
1241
Gilles Debunne66111472010-11-19 11:04:37 -08001242 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 public int getParagraphDirection(int line) {
1244 return mLines[mColumns * line + DIR] >> DIR_SHIFT;
1245 }
1246
Gilles Debunne66111472010-11-19 11:04:37 -08001247 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 public boolean getLineContainsTab(int line) {
1249 return (mLines[mColumns * line + TAB] & TAB_MASK) != 0;
1250 }
1251
Gilles Debunne66111472010-11-19 11:04:37 -08001252 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 public final Directions getLineDirections(int line) {
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001254 if (line > getLineCount()) {
1255 throw new ArrayIndexOutOfBoundsException();
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 return mLineDirections[line];
1258 }
1259
Gilles Debunne66111472010-11-19 11:04:37 -08001260 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 public int getTopPadding() {
1262 return mTopPadding;
1263 }
1264
Gilles Debunne66111472010-11-19 11:04:37 -08001265 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 public int getBottomPadding() {
1267 return mBottomPadding;
1268 }
1269
Raph Levien26d443a2015-03-30 14:18:32 -07001270 /**
1271 * @hide
1272 */
1273 @Override
1274 public int getHyphen(int line) {
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001275 return mLines[mColumns * line + HYPHEN] & HYPHEN_MASK;
Raph Levien26d443a2015-03-30 14:18:32 -07001276 }
1277
Raph Levien2ea52902015-07-01 14:39:31 -07001278 /**
1279 * @hide
1280 */
1281 @Override
1282 public int getIndentAdjust(int line, Alignment align) {
1283 if (align == Alignment.ALIGN_LEFT) {
1284 if (mLeftIndents == null) {
1285 return 0;
1286 } else {
1287 return mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1288 }
1289 } else if (align == Alignment.ALIGN_RIGHT) {
1290 if (mRightIndents == null) {
1291 return 0;
1292 } else {
1293 return -mRightIndents[Math.min(line, mRightIndents.length - 1)];
1294 }
1295 } else if (align == Alignment.ALIGN_CENTER) {
1296 int left = 0;
1297 if (mLeftIndents != null) {
1298 left = mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1299 }
1300 int right = 0;
1301 if (mRightIndents != null) {
1302 right = mRightIndents[Math.min(line, mRightIndents.length - 1)];
1303 }
1304 return (left - right) >> 1;
1305 } else {
1306 throw new AssertionError("unhandled alignment " + align);
1307 }
1308 }
1309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 @Override
1311 public int getEllipsisCount(int line) {
1312 if (mColumns < COLUMNS_ELLIPSIZE) {
1313 return 0;
1314 }
1315
1316 return mLines[mColumns * line + ELLIPSIS_COUNT];
1317 }
1318
1319 @Override
1320 public int getEllipsisStart(int line) {
1321 if (mColumns < COLUMNS_ELLIPSIZE) {
1322 return 0;
1323 }
1324
1325 return mLines[mColumns * line + ELLIPSIS_START];
1326 }
1327
1328 @Override
1329 public int getEllipsizedWidth() {
1330 return mEllipsizedWidth;
1331 }
1332
Siyamed Sinir0745c722016-05-31 20:39:33 -07001333 /**
1334 * Return the total height of this layout.
1335 *
1336 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
1337 *
1338 * @hide
1339 */
1340 public int getHeight(boolean cap) {
1341 if (cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight == -1 &&
1342 Log.isLoggable(TAG, Log.WARN)) {
1343 Log.w(TAG, "maxLineHeight should not be -1. "
1344 + " maxLines:" + mMaximumVisibleLineCount
1345 + " lineCount:" + mLineCount);
1346 }
1347
1348 return cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight != -1 ?
1349 mMaxLineHeight : super.getHeight();
1350 }
1351
Raph Levien70616ec2015-03-04 10:41:30 -08001352 private static native long nNewBuilder();
1353 private static native void nFreeBuilder(long nativePtr);
1354 private static native void nFinishBuilder(long nativePtr);
Raph Levien26d443a2015-03-30 14:18:32 -07001355
Roozbeh Pournadera59c3fe2017-02-27 10:13:44 -08001356 /* package */ static native long nLoadHyphenator(ByteBuffer buf, int offset,
1357 int minPrefix, int minSuffix);
Raph Levien26d443a2015-03-30 14:18:32 -07001358
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -07001359 private static native void nSetLocales(long nativePtr, String locales,
1360 long[] nativeHyphenators);
Raph Levien70616ec2015-03-04 10:41:30 -08001361
Raph Leviene319d5a2015-04-14 23:51:07 -07001362 private static native void nSetIndents(long nativePtr, int[] indents);
1363
Raph Levienc94f7422015-03-06 19:19:48 -08001364 // Set up paragraph text and settings; done as one big method to minimize jni crossings
1365 private static native void nSetupParagraph(long nativePtr, char[] text, int length,
1366 float firstWidth, int firstWidthLineCount, float restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001367 int[] variableTabStops, int defaultTabStop, int breakStrategy, int hyphenationFrequency,
1368 boolean isJustified);
Raph Levien70616ec2015-03-04 10:41:30 -08001369
1370 private static native float nAddStyleRun(long nativePtr, long nativePaint,
1371 long nativeTypeface, int start, int end, boolean isRtl);
1372
1373 private static native void nAddMeasuredRun(long nativePtr,
1374 int start, int end, float[] widths);
1375
1376 private static native void nAddReplacementRun(long nativePtr, int start, int end, float width);
1377
1378 private static native void nGetWidths(long nativePtr, float[] widths);
1379
Anish Athalyec8f9e622014-07-21 15:26:34 -07001380 // populates LineBreaks and returns the number of breaks found
1381 //
1382 // the arrays inside the LineBreaks objects are passed in as well
1383 // to reduce the number of JNI calls in the common case where the
1384 // arrays do not have to be resized
Raph Levienc94f7422015-03-06 19:19:48 -08001385 private static native int nComputeLineBreaks(long nativePtr, LineBreaks recycle,
Raph Levien26d443a2015-03-30 14:18:32 -07001386 int[] recycleBreaks, float[] recycleWidths, int[] recycleFlags, int recycleLength);
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 private int mLineCount;
1389 private int mTopPadding, mBottomPadding;
1390 private int mColumns;
1391 private int mEllipsizedWidth;
1392
Siyamed Sinir0745c722016-05-31 20:39:33 -07001393 /**
1394 * Keeps track if ellipsize is applied to the text.
1395 */
1396 private boolean mEllipsized;
1397
1398 /**
1399 * If maxLines is set, ellipsize is not set, and the actual line count of text is greater than
1400 * or equal to maxLine, this variable holds the ideal visual height of the maxLine'th line
1401 * starting from the top of the layout. If maxLines is not set its value will be -1.
1402 *
1403 * The value is the same as getLineTop(maxLines) for ellipsized version where structurally no
1404 * more than maxLines is contained.
1405 */
1406 private int mMaxLineHeight = -1;
1407
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001408 private static final int COLUMNS_NORMAL = 5;
1409 private static final int COLUMNS_ELLIPSIZE = 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 private static final int START = 0;
1411 private static final int DIR = START;
1412 private static final int TAB = START;
1413 private static final int TOP = 1;
1414 private static final int DESCENT = 2;
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001415 private static final int EXTRA = 3;
1416 private static final int HYPHEN = 4;
1417 private static final int ELLIPSIS_START = 5;
1418 private static final int ELLIPSIS_COUNT = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419
1420 private int[] mLines;
1421 private Directions[] mLineDirections;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001422 private int mMaximumVisibleLineCount = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423
1424 private static final int START_MASK = 0x1FFFFFFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 private static final int DIR_SHIFT = 30;
1426 private static final int TAB_MASK = 0x20000000;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001427 private static final int HYPHEN_MASK = 0xFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428
Doug Feltc982f602010-05-25 11:51:40 -07001429 private static final int TAB_INCREMENT = 20; // same as Layout, but that's private
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001431 private static final char CHAR_NEW_LINE = '\n';
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001432
1433 private static final double EXTRA_ROUNDING = 0.5;
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001434
Anish Athalyec8f9e622014-07-21 15:26:34 -07001435 // This is used to return three arrays from a single JNI call when
1436 // performing line breaking
Deepanshu Gupta70539192014-10-15 15:57:40 -07001437 /*package*/ static class LineBreaks {
Anish Athalyec8f9e622014-07-21 15:26:34 -07001438 private static final int INITIAL_SIZE = 16;
1439 public int[] breaks = new int[INITIAL_SIZE];
1440 public float[] widths = new float[INITIAL_SIZE];
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001441 public int[] flags = new int[INITIAL_SIZE]; // hasTab
Anish Athalyec8f9e622014-07-21 15:26:34 -07001442 // breaks, widths, and flags should all have the same length
1443 }
1444
Raph Levien2ea52902015-07-01 14:39:31 -07001445 private int[] mLeftIndents;
1446 private int[] mRightIndents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447}