blob: a8c6aa6cbbbb76b04e36efe82586b15c0a0d58af [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
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700337 private long[] getHyphenators(LocaleList locales) {
338 final int length = locales.size();
339 final long[] result = new long[length];
340 for (int i = 0; i < length; i++) {
341 final Locale locale = locales.get(i);
342 result[i] = Hyphenator.get(locale).getNativePtr();
343 }
344 return result;
345 }
346
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900347 /**
Raph Levien70616ec2015-03-04 10:41:30 -0800348 * Measurement and break iteration is done in native code. The protocol for using
349 * the native code is as follows.
350 *
Raph Levien26d443a2015-03-30 14:18:32 -0700351 * For each paragraph, do a nSetupParagraph, which sets paragraph text, line width, tab
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700352 * stops, break strategy, and hyphenation frequency (and possibly other parameters in the
353 * future).
Raph Levienc94f7422015-03-06 19:19:48 -0800354 *
355 * Then, for each run within the paragraph:
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700356 * - setLocales (this must be done at least for the first run, optional afterwards)
Raph Levien70616ec2015-03-04 10:41:30 -0800357 * - one of the following, depending on the type of run:
358 * + addStyleRun (a text run, to be measured in native code)
359 * + addMeasuredRun (a run already measured in Java, passed into native code)
360 * + addReplacementRun (a replacement run, width is given)
361 *
362 * After measurement, nGetWidths() is valid if the widths are needed (eg for ellipsis).
363 * Run nComputeLineBreaks() to obtain line breaks for the paragraph.
364 *
365 * After all paragraphs, call finish() to release expensive buffers.
366 */
367
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700368 private void setLocales(LocaleList locales) {
369 if (!locales.equals(mLocales)) {
370 nSetLocales(mNativePtr, locales.toLanguageTags(), getHyphenators(locales));
371 mLocales = locales;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800372 }
373 }
374
Raph Levien70616ec2015-03-04 10:41:30 -0800375 /* package */ float addStyleRun(TextPaint paint, int start, int end, boolean isRtl) {
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700376 setLocales(paint.getTextLocales());
Raph Levien70616ec2015-03-04 10:41:30 -0800377 return nAddStyleRun(mNativePtr, paint.getNativeInstance(), paint.mNativeTypeface,
378 start, end, isRtl);
379 }
380
381 /* package */ void addMeasuredRun(int start, int end, float[] widths) {
382 nAddMeasuredRun(mNativePtr, start, end, widths);
383 }
384
385 /* package */ void addReplacementRun(int start, int end, float width) {
386 nAddReplacementRun(mNativePtr, start, end, width);
387 }
388
Raph Levien531c30c2015-04-30 16:29:59 -0700389 /**
390 * Build the {@link StaticLayout} after options have been set.
391 *
392 * <p>Note: the builder object must not be reused in any way after calling this
393 * method. Setting parameters after calling this method, or calling it a second
394 * time on the same builder object, will likely lead to unexpected results.
395 *
396 * @return the newly constructed {@link StaticLayout} object
397 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800398 public StaticLayout build() {
Raph Levien39b4db72015-03-25 13:18:20 -0700399 StaticLayout result = new StaticLayout(this);
400 Builder.recycle(this);
Raph Leviend3ab6922015-03-02 14:30:53 -0800401 return result;
402 }
403
Raph Levien4c1f12e2015-03-02 16:29:23 -0800404 @Override
405 protected void finalize() throws Throwable {
406 try {
407 nFreeBuilder(mNativePtr);
408 } finally {
409 super.finalize();
410 }
411 }
412
413 /* package */ long mNativePtr;
414
Raph Leviend3ab6922015-03-02 14:30:53 -0800415 CharSequence mText;
416 int mStart;
417 int mEnd;
418 TextPaint mPaint;
419 int mWidth;
Raph Levien39b4db72015-03-25 13:18:20 -0700420 Alignment mAlignment;
Raph Leviend3ab6922015-03-02 14:30:53 -0800421 TextDirectionHeuristic mTextDir;
422 float mSpacingMult;
423 float mSpacingAdd;
424 boolean mIncludePad;
425 int mEllipsizedWidth;
426 TextUtils.TruncateAt mEllipsize;
427 int mMaxLines;
Raph Levien39b4db72015-03-25 13:18:20 -0700428 int mBreakStrategy;
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700429 int mHyphenationFrequency;
Raph Levien2ea52902015-07-01 14:39:31 -0700430 int[] mLeftIndents;
431 int[] mRightIndents;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700432 int mJustificationMode;
Raph Leviend3ab6922015-03-02 14:30:53 -0800433
434 Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt();
435
436 // This will go away and be subsumed by native builder code
437 MeasuredText mMeasuredText;
438
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700439 LocaleList mLocales;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800440
Raph Levien39b4db72015-03-25 13:18:20 -0700441 private static final SynchronizedPool<Builder> sPool = new SynchronizedPool<Builder>(3);
Raph Leviend3ab6922015-03-02 14:30:53 -0800442 }
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public StaticLayout(CharSequence source, TextPaint paint,
445 int width,
446 Alignment align, float spacingmult, float spacingadd,
447 boolean includepad) {
448 this(source, 0, source.length(), paint, width, align,
449 spacingmult, spacingadd, includepad);
450 }
451
Doug Feltcb3791202011-07-07 11:57:48 -0700452 /**
453 * @hide
454 */
455 public StaticLayout(CharSequence source, TextPaint paint,
456 int width, Alignment align, TextDirectionHeuristic textDir,
457 float spacingmult, float spacingadd,
458 boolean includepad) {
459 this(source, 0, source.length(), paint, width, align, textDir,
460 spacingmult, spacingadd, includepad);
461 }
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public StaticLayout(CharSequence source, int bufstart, int bufend,
464 TextPaint paint, int outerwidth,
465 Alignment align,
466 float spacingmult, float spacingadd,
467 boolean includepad) {
468 this(source, bufstart, bufend, paint, outerwidth, align,
469 spacingmult, spacingadd, includepad, null, 0);
470 }
471
Doug Feltcb3791202011-07-07 11:57:48 -0700472 /**
473 * @hide
474 */
475 public StaticLayout(CharSequence source, int bufstart, int bufend,
476 TextPaint paint, int outerwidth,
477 Alignment align, TextDirectionHeuristic textDir,
478 float spacingmult, float spacingadd,
479 boolean includepad) {
480 this(source, bufstart, bufend, paint, outerwidth, align, textDir,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700481 spacingmult, spacingadd, includepad, null, 0, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700482}
483
484 public StaticLayout(CharSequence source, int bufstart, int bufend,
485 TextPaint paint, int outerwidth,
486 Alignment align,
487 float spacingmult, float spacingadd,
488 boolean includepad,
489 TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
490 this(source, bufstart, bufend, paint, outerwidth, align,
491 TextDirectionHeuristics.FIRSTSTRONG_LTR,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700492 spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700493 }
494
495 /**
496 * @hide
497 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public StaticLayout(CharSequence source, int bufstart, int bufend,
499 TextPaint paint, int outerwidth,
Doug Feltcb3791202011-07-07 11:57:48 -0700500 Alignment align, TextDirectionHeuristic textDir,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 float spacingmult, float spacingadd,
502 boolean includepad,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700503 TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 super((ellipsize == null)
Doug Felt4e0c5e52010-03-15 16:56:02 -0700505 ? source
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 : (source instanceof Spanned)
507 ? new SpannedEllipsizer(source)
508 : new Ellipsizer(source),
Doug Feltcb3791202011-07-07 11:57:48 -0700509 paint, outerwidth, align, textDir, spacingmult, spacingadd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
Raph Levienebd66ca2015-04-30 15:27:57 -0700511 Builder b = Builder.obtain(source, bufstart, bufend, paint, outerwidth)
Raph Levien39b4db72015-03-25 13:18:20 -0700512 .setAlignment(align)
Raph Leviena6a08282015-06-03 13:20:45 -0700513 .setTextDirection(textDir)
Raph Levien531c30c2015-04-30 16:29:59 -0700514 .setLineSpacing(spacingadd, spacingmult)
Raph Leviend3ab6922015-03-02 14:30:53 -0800515 .setIncludePad(includepad)
516 .setEllipsizedWidth(ellipsizedWidth)
517 .setEllipsize(ellipsize)
518 .setMaxLines(maxLines);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 /*
520 * This is annoying, but we can't refer to the layout until
521 * superclass construction is finished, and the superclass
522 * constructor wants the reference to the display text.
Doug Felt4e0c5e52010-03-15 16:56:02 -0700523 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * This will break if the superclass constructor ever actually
525 * cares about the content instead of just holding the reference.
526 */
527 if (ellipsize != null) {
528 Ellipsizer e = (Ellipsizer) getText();
529
530 e.mLayout = this;
531 e.mWidth = ellipsizedWidth;
532 e.mMethod = ellipsize;
533 mEllipsizedWidth = ellipsizedWidth;
534
535 mColumns = COLUMNS_ELLIPSIZE;
536 } else {
537 mColumns = COLUMNS_NORMAL;
538 mEllipsizedWidth = outerwidth;
539 }
540
Adam Lesinski776abc22014-03-07 11:30:59 -0500541 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
542 mLines = new int[mLineDirections.length];
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700543 mMaximumVisibleLineCount = maxLines;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
Raph Levien70616ec2015-03-04 10:41:30 -0800545 generate(b, b.mIncludePad, b.mIncludePad);
Doug Felte8e45f22010-03-29 14:58:40 -0700546
Raph Leviend3ab6922015-03-02 14:30:53 -0800547 Builder.recycle(b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700550 /* package */ StaticLayout(CharSequence text) {
551 super(text, null, 0, null, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
553 mColumns = COLUMNS_ELLIPSIZE;
Adam Lesinski776abc22014-03-07 11:30:59 -0500554 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
555 mLines = new int[mLineDirections.length];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557
Raph Levien39b4db72015-03-25 13:18:20 -0700558 private StaticLayout(Builder b) {
559 super((b.mEllipsize == null)
560 ? b.mText
561 : (b.mText instanceof Spanned)
562 ? new SpannedEllipsizer(b.mText)
563 : new Ellipsizer(b.mText),
564 b.mPaint, b.mWidth, b.mAlignment, b.mSpacingMult, b.mSpacingAdd);
565
566 if (b.mEllipsize != null) {
567 Ellipsizer e = (Ellipsizer) getText();
568
569 e.mLayout = this;
570 e.mWidth = b.mEllipsizedWidth;
571 e.mMethod = b.mEllipsize;
572 mEllipsizedWidth = b.mEllipsizedWidth;
573
574 mColumns = COLUMNS_ELLIPSIZE;
575 } else {
576 mColumns = COLUMNS_NORMAL;
577 mEllipsizedWidth = b.mWidth;
578 }
579
580 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
581 mLines = new int[mLineDirections.length];
582 mMaximumVisibleLineCount = b.mMaxLines;
583
Raph Levien2ea52902015-07-01 14:39:31 -0700584 mLeftIndents = b.mLeftIndents;
585 mRightIndents = b.mRightIndents;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700586 setJustificationMode(b.mJustificationMode);
Raph Levien2ea52902015-07-01 14:39:31 -0700587
Raph Levien39b4db72015-03-25 13:18:20 -0700588 generate(b, b.mIncludePad, b.mIncludePad);
589 }
590
Raph Leviend3ab6922015-03-02 14:30:53 -0800591 /* package */ void generate(Builder b, boolean includepad, boolean trackpad) {
592 CharSequence source = b.mText;
593 int bufStart = b.mStart;
594 int bufEnd = b.mEnd;
595 TextPaint paint = b.mPaint;
596 int outerWidth = b.mWidth;
597 TextDirectionHeuristic textDir = b.mTextDir;
598 float spacingmult = b.mSpacingMult;
599 float spacingadd = b.mSpacingAdd;
600 float ellipsizedWidth = b.mEllipsizedWidth;
601 TextUtils.TruncateAt ellipsize = b.mEllipsize;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800602 LineBreaks lineBreaks = new LineBreaks(); // TODO: move to builder to avoid allocation costs
Anish Athalyec8f9e622014-07-21 15:26:34 -0700603 // store span end locations
604 int[] spanEndCache = new int[4];
605 // store fontMetrics per span range
606 // must be a multiple of 4 (and > 0) (store top, bottom, ascent, and descent per range)
607 int[] fmCache = new int[4 * 4];
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -0700608 b.setLocales(paint.getTextLocales());
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mLineCount = 0;
611
612 int v = 0;
613 boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
614
Raph Leviend3ab6922015-03-02 14:30:53 -0800615 Paint.FontMetricsInt fm = b.mFontMetricsInt;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800616 int[] chooseHtv = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
Raph Leviend3ab6922015-03-02 14:30:53 -0800618 MeasuredText measured = b.mMeasuredText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 Spanned spanned = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 if (source instanceof Spanned)
622 spanned = (Spanned) source;
623
Doug Felte8e45f22010-03-29 14:58:40 -0700624 int paraEnd;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800625 for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
626 paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
Doug Felte8e45f22010-03-29 14:58:40 -0700627 if (paraEnd < 0)
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800628 paraEnd = bufEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 else
Doug Felte8e45f22010-03-29 14:58:40 -0700630 paraEnd++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
Anish Athalyec8f9e622014-07-21 15:26:34 -0700632 int firstWidthLineCount = 1;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800633 int firstWidth = outerWidth;
634 int restWidth = outerWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800636 LineHeightSpan[] chooseHt = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 if (spanned != null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -0700639 LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd,
Doug Felte8e45f22010-03-29 14:58:40 -0700640 LeadingMarginSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 for (int i = 0; i < sp.length; i++) {
Mark Wagner7b5676e2009-10-16 11:44:23 -0700642 LeadingMarginSpan lms = sp[i];
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800643 firstWidth -= sp[i].getLeadingMargin(true);
644 restWidth -= sp[i].getLeadingMargin(false);
Doug Feltcb3791202011-07-07 11:57:48 -0700645
Doug Feltc982f602010-05-25 11:51:40 -0700646 // LeadingMarginSpan2 is odd. The count affects all
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700647 // leading margin spans, not just this particular one
Doug Feltc982f602010-05-25 11:51:40 -0700648 if (lms instanceof LeadingMarginSpan2) {
649 LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700650 firstWidthLineCount = Math.max(firstWidthLineCount,
651 lms2.getLeadingMarginLineCount());
Mark Wagner7b5676e2009-10-16 11:44:23 -0700652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800655 chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700657 if (chooseHt.length == 0) {
658 chooseHt = null; // So that out() would not assume it has any contents
659 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800660 if (chooseHtv == null ||
661 chooseHtv.length < chooseHt.length) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500662 chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800665 for (int i = 0; i < chooseHt.length; i++) {
666 int o = spanned.getSpanStart(chooseHt[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
Doug Felte8e45f22010-03-29 14:58:40 -0700668 if (o < paraStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 // starts in this layout, before the
670 // current paragraph
671
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800672 chooseHtv[i] = getLineTop(getLineForOffset(o));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 } else {
674 // starts in this paragraph
675
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800676 chooseHtv[i] = v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678 }
679 }
680 }
681
Raph Levien70616ec2015-03-04 10:41:30 -0800682 measured.setPara(source, paraStart, paraEnd, textDir, b);
Doug Felte8e45f22010-03-29 14:58:40 -0700683 char[] chs = measured.mChars;
684 float[] widths = measured.mWidths;
685 byte[] chdirs = measured.mLevels;
686 int dir = measured.mDir;
687 boolean easy = measured.mEasy;
Raph Levienc94f7422015-03-06 19:19:48 -0800688
689 // tab stop locations
690 int[] variableTabStops = null;
691 if (spanned != null) {
692 TabStopSpan[] spans = getParagraphSpans(spanned, paraStart,
693 paraEnd, TabStopSpan.class);
694 if (spans.length > 0) {
695 int[] stops = new int[spans.length];
696 for (int i = 0; i < spans.length; i++) {
697 stops[i] = spans[i].getTabStop();
698 }
699 Arrays.sort(stops, 0, stops.length);
700 variableTabStops = stops;
701 }
702 }
703
Raph Levienc94f7422015-03-06 19:19:48 -0800704 nSetupParagraph(b.mNativePtr, chs, paraEnd - paraStart,
705 firstWidth, firstWidthLineCount, restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900706 variableTabStops, TAB_INCREMENT, b.mBreakStrategy, b.mHyphenationFrequency,
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700707 // TODO: Support more justification mode, e.g. letter spacing, stretching.
708 b.mJustificationMode != Layout.JUSTIFICATION_MODE_NONE);
Raph Levien2ea52902015-07-01 14:39:31 -0700709 if (mLeftIndents != null || mRightIndents != null) {
710 // TODO(raph) performance: it would be better to do this once per layout rather
711 // than once per paragraph, but that would require a change to the native
712 // interface.
713 int leftLen = mLeftIndents == null ? 0 : mLeftIndents.length;
714 int rightLen = mRightIndents == null ? 0 : mRightIndents.length;
Siyamed Sinirf9a08862016-04-12 19:30:44 -0700715 int indentsLen = Math.max(1, Math.max(leftLen, rightLen) - mLineCount);
Raph Levien2ea52902015-07-01 14:39:31 -0700716 int[] indents = new int[indentsLen];
717 for (int i = 0; i < indentsLen; i++) {
718 int leftMargin = mLeftIndents == null ? 0 :
719 mLeftIndents[Math.min(i + mLineCount, leftLen - 1)];
720 int rightMargin = mRightIndents == null ? 0 :
721 mRightIndents[Math.min(i + mLineCount, rightLen - 1)];
722 indents[i] = leftMargin + rightMargin;
723 }
724 nSetIndents(b.mNativePtr, indents);
725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
Anish Athalyec8f9e622014-07-21 15:26:34 -0700727 // measurement has to be done before performing line breaking
728 // but we don't want to recompute fontmetrics or span ranges the
729 // second time, so we cache those and then use those stored values
730 int fmCacheCount = 0;
731 int spanEndCacheCount = 0;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700732 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700733 if (fmCacheCount * 4 >= fmCache.length) {
734 int[] grow = new int[fmCacheCount * 4 * 2];
735 System.arraycopy(fmCache, 0, grow, 0, fmCacheCount * 4);
736 fmCache = grow;
737 }
738
739 if (spanEndCacheCount >= spanEndCache.length) {
740 int[] grow = new int[spanEndCacheCount * 2];
741 System.arraycopy(spanEndCache, 0, grow, 0, spanEndCacheCount);
742 spanEndCache = grow;
743 }
Doug Felte8e45f22010-03-29 14:58:40 -0700744
Gilles Debunnecd943a72012-06-07 17:54:47 -0700745 if (spanned == null) {
746 spanEnd = paraEnd;
Doug Felt23241882010-06-02 14:41:06 -0700747 int spanLen = spanEnd - spanStart;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700748 measured.addStyleRun(paint, spanLen, fm);
749 } else {
750 spanEnd = spanned.nextSpanTransition(spanStart, paraEnd,
751 MetricAffectingSpan.class);
752 int spanLen = spanEnd - spanStart;
753 MetricAffectingSpan[] spans =
Doug Felt23241882010-06-02 14:41:06 -0700754 spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
Gilles Debunnecd943a72012-06-07 17:54:47 -0700755 spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class);
756 measured.addStyleRun(paint, spans, spanLen, fm);
Doug Felt23241882010-06-02 14:41:06 -0700757 }
758
Anish Athalyec8f9e622014-07-21 15:26:34 -0700759 // the order of storage here (top, bottom, ascent, descent) has to match the code below
760 // where these values are retrieved
761 fmCache[fmCacheCount * 4 + 0] = fm.top;
762 fmCache[fmCacheCount * 4 + 1] = fm.bottom;
763 fmCache[fmCacheCount * 4 + 2] = fm.ascent;
764 fmCache[fmCacheCount * 4 + 3] = fm.descent;
765 fmCacheCount++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
Anish Athalyec8f9e622014-07-21 15:26:34 -0700767 spanEndCache[spanEndCacheCount] = spanEnd;
768 spanEndCacheCount++;
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
Raph Levien70616ec2015-03-04 10:41:30 -0800771 nGetWidths(b.mNativePtr, widths);
Raph Levienc94f7422015-03-06 19:19:48 -0800772 int breakCount = nComputeLineBreaks(b.mNativePtr, lineBreaks, lineBreaks.breaks,
773 lineBreaks.widths, lineBreaks.flags, lineBreaks.breaks.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774
Anish Athalyec8f9e622014-07-21 15:26:34 -0700775 int[] breaks = lineBreaks.breaks;
776 float[] lineWidths = lineBreaks.widths;
Raph Levien26d443a2015-03-30 14:18:32 -0700777 int[] flags = lineBreaks.flags;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700778
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900779 final int remainingLineCount = mMaximumVisibleLineCount - mLineCount;
780 final boolean ellipsisMayBeApplied = ellipsize != null
781 && (ellipsize == TextUtils.TruncateAt.END
782 || (mMaximumVisibleLineCount == 1
783 && ellipsize != TextUtils.TruncateAt.MARQUEE));
Raph Levien04a84552015-07-09 15:38:04 -0700784 if (remainingLineCount > 0 && remainingLineCount < breakCount &&
785 ellipsisMayBeApplied) {
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900786 // Calculate width and flag.
787 float width = 0;
788 int flag = 0;
789 for (int i = remainingLineCount - 1; i < breakCount; i++) {
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700790 if (i == breakCount - 1) {
791 width += lineWidths[i];
792 } else {
793 for (int j = (i == 0 ? 0 : breaks[i - 1]); j < breaks[i]; j++) {
794 width += widths[j];
795 }
796 }
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900797 flag |= flags[i] & TAB_MASK;
798 }
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700799 // Treat the last line and overflowed lines as a single line.
800 breaks[remainingLineCount - 1] = breaks[breakCount - 1];
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900801 lineWidths[remainingLineCount - 1] = width;
802 flags[remainingLineCount - 1] = flag;
803
804 breakCount = remainingLineCount;
805 }
806
Anish Athalyec8f9e622014-07-21 15:26:34 -0700807 // here is the offset of the starting character of the line we are currently measuring
808 int here = paraStart;
809
810 int fmTop = 0, fmBottom = 0, fmAscent = 0, fmDescent = 0;
811 int fmCacheIndex = 0;
812 int spanEndCacheIndex = 0;
813 int breakIndex = 0;
814 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
815 // retrieve end of span
816 spanEnd = spanEndCache[spanEndCacheIndex++];
817
818 // retrieve cached metrics, order matches above
819 fm.top = fmCache[fmCacheIndex * 4 + 0];
820 fm.bottom = fmCache[fmCacheIndex * 4 + 1];
821 fm.ascent = fmCache[fmCacheIndex * 4 + 2];
822 fm.descent = fmCache[fmCacheIndex * 4 + 3];
823 fmCacheIndex++;
824
825 if (fm.top < fmTop) {
826 fmTop = fm.top;
827 }
828 if (fm.ascent < fmAscent) {
829 fmAscent = fm.ascent;
830 }
831 if (fm.descent > fmDescent) {
832 fmDescent = fm.descent;
833 }
834 if (fm.bottom > fmBottom) {
835 fmBottom = fm.bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837
Anish Athalyec8f9e622014-07-21 15:26:34 -0700838 // skip breaks ending before current span range
839 while (breakIndex < breakCount && paraStart + breaks[breakIndex] < spanStart) {
840 breakIndex++;
841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842
Anish Athalyec8f9e622014-07-21 15:26:34 -0700843 while (breakIndex < breakCount && paraStart + breaks[breakIndex] <= spanEnd) {
844 int endPos = paraStart + breaks[breakIndex];
845
Raph Levience4155a2015-03-11 11:02:33 -0700846 boolean moreChars = (endPos < bufEnd);
Raph Levien4c02e832014-12-12 11:17:01 -0800847
Anish Athalyec8f9e622014-07-21 15:26:34 -0700848 v = out(source, here, endPos,
849 fmAscent, fmDescent, fmTop, fmBottom,
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700850 v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, flags[breakIndex],
Anish Athalyec8f9e622014-07-21 15:26:34 -0700851 needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad,
852 chs, widths, paraStart, ellipsize, ellipsizedWidth,
Raph Levien4c02e832014-12-12 11:17:01 -0800853 lineWidths[breakIndex], paint, moreChars);
Anish Athalyec8f9e622014-07-21 15:26:34 -0700854
855 if (endPos < spanEnd) {
856 // preserve metrics for current span
857 fmTop = fm.top;
858 fmBottom = fm.bottom;
859 fmAscent = fm.ascent;
860 fmDescent = fm.descent;
861 } else {
862 fmTop = fmBottom = fmAscent = fmDescent = 0;
863 }
864
865 here = endPos;
866 breakIndex++;
867
Siyamed Sinir0745c722016-05-31 20:39:33 -0700868 if (mLineCount >= mMaximumVisibleLineCount && mEllipsized) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700869 return;
870 }
871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
873
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800874 if (paraEnd == bufEnd)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 break;
876 }
877
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700878 if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) &&
Fabrice Di Meglioad0b0512011-10-04 17:21:26 -0700879 mLineCount < mMaximumVisibleLineCount) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800880 // Log.e("text", "output last " + bufEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
Raph Levien70616ec2015-03-04 10:41:30 -0800882 measured.setPara(source, bufEnd, bufEnd, textDir, b);
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 paint.getFontMetricsInt(fm);
885
886 v = out(source,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800887 bufEnd, bufEnd, fm.ascent, fm.descent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 fm.top, fm.bottom,
889 v,
890 spacingmult, spacingadd, null,
Raph Levien26d443a2015-03-30 14:18:32 -0700891 null, fm, 0,
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700892 needMultiply, measured.mLevels, measured.mDir, measured.mEasy, bufEnd,
Gilles Debunned300e752011-10-17 13:37:36 -0700893 includepad, trackpad, null,
894 null, bufStart, ellipsize,
895 ellipsizedWidth, 0, paint, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897 }
898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 private int out(CharSequence text, int start, int end,
900 int above, int below, int top, int bottom, int v,
901 float spacingmult, float spacingadd,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800902 LineHeightSpan[] chooseHt, int[] chooseHtv,
Raph Levien26d443a2015-03-30 14:18:32 -0700903 Paint.FontMetricsInt fm, int flags,
Gilles Debunned300e752011-10-17 13:37:36 -0700904 boolean needMultiply, byte[] chdirs, int dir,
905 boolean easy, int bufEnd, boolean includePad,
906 boolean trackPad, char[] chs,
907 float[] widths, int widthStart, TextUtils.TruncateAt ellipsize,
908 float ellipsisWidth, float textWidth,
909 TextPaint paint, boolean moreChars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 int j = mLineCount;
911 int off = j * mColumns;
912 int want = off + mColumns + TOP;
913 int[] lines = mLines;
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 if (want >= lines.length) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500916 Directions[] grow2 = ArrayUtils.newUnpaddedArray(
917 Directions.class, GrowingArrayUtils.growSize(want));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 System.arraycopy(mLineDirections, 0, grow2, 0,
919 mLineDirections.length);
920 mLineDirections = grow2;
Adam Lesinski776abc22014-03-07 11:30:59 -0500921
922 int[] grow = new int[grow2.length];
923 System.arraycopy(lines, 0, grow, 0, lines.length);
924 mLines = grow;
925 lines = grow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800928 if (chooseHt != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 fm.ascent = above;
930 fm.descent = below;
931 fm.top = top;
932 fm.bottom = bottom;
933
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800934 for (int i = 0; i < chooseHt.length; i++) {
935 if (chooseHt[i] instanceof LineHeightSpan.WithDensity) {
936 ((LineHeightSpan.WithDensity) chooseHt[i]).
937 chooseHeight(text, start, end, chooseHtv[i], v, fm, paint);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700938
939 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800940 chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943
944 above = fm.ascent;
945 below = fm.descent;
946 top = fm.top;
947 bottom = fm.bottom;
948 }
949
Raph Leviend97b0972014-04-24 12:51:35 -0700950 boolean firstLine = (j == 0);
951 boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount);
Siyamed Sinir0745c722016-05-31 20:39:33 -0700952
953 if (ellipsize != null) {
954 // If there is only one line, then do any type of ellipsis except when it is MARQUEE
955 // if there are multiple lines, just allow END ellipsis on the last line
956 boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount);
957
958 boolean doEllipsis =
959 (((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) &&
960 ellipsize != TextUtils.TruncateAt.MARQUEE) ||
961 (!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) &&
962 ellipsize == TextUtils.TruncateAt.END);
963 if (doEllipsis) {
964 calculateEllipsis(start, end, widths, widthStart,
965 ellipsisWidth, ellipsize, j,
966 textWidth, paint, forceEllipsis);
967 }
968 }
969
970 boolean lastLine = mEllipsized || (end == bufEnd);
Raph Leviend97b0972014-04-24 12:51:35 -0700971
972 if (firstLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800973 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 mTopPadding = top - above;
975 }
976
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800977 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 above = top;
979 }
980 }
Raph Leviend97b0972014-04-24 12:51:35 -0700981
982 int extra;
983
984 if (lastLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800985 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 mBottomPadding = bottom - below;
987 }
988
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800989 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 below = bottom;
991 }
992 }
993
Raph Leviend97b0972014-04-24 12:51:35 -0700994 if (needMultiply && !lastLine) {
Doug Felt10657582010-02-22 11:19:01 -0800995 double ex = (below - above) * (spacingmult - 1) + spacingadd;
996 if (ex >= 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800997 extra = (int)(ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -0800998 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800999 extra = -(int)(-ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -08001000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 } else {
1002 extra = 0;
1003 }
1004
1005 lines[off + START] = start;
1006 lines[off + TOP] = v;
1007 lines[off + DESCENT] = below + extra;
1008
Siyamed Sinir0745c722016-05-31 20:39:33 -07001009 // special case for non-ellipsized last visible line when maxLines is set
1010 // store the height as if it was ellipsized
1011 if (!mEllipsized && currentLineIsTheLastVisibleOne) {
1012 // below calculation as if it was the last line
1013 int maxLineBelow = includePad ? bottom : below;
1014 // similar to the calculation of v below, without the extra.
1015 mMaxLineHeight = v + (maxLineBelow - above);
1016 }
1017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 v += (below - above) + extra;
1019 lines[off + mColumns + START] = end;
1020 lines[off + mColumns + TOP] = v;
1021
Raph Levien26d443a2015-03-30 14:18:32 -07001022 // TODO: could move TAB to share same column as HYPHEN, simplifying this code and gaining
1023 // one bit for start field
1024 lines[off + TAB] |= flags & TAB_MASK;
1025 lines[off + HYPHEN] = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
Doug Felt9f7a4442010-03-01 12:45:56 -08001027 lines[off + DIR] |= dir << DIR_SHIFT;
1028 Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT;
1029 // easy means all chars < the first RTL, so no emoji, no nothing
Doug Felt4e0c5e52010-03-15 16:56:02 -07001030 // XXX a run with no text or all spaces is easy but might be an empty
Doug Felt9f7a4442010-03-01 12:45:56 -08001031 // RTL paragraph. Make sure easy is false if this is the case.
1032 if (easy) {
1033 mLineDirections[j] = linedirs;
1034 } else {
Gilles Debunnef3fa0cd2011-02-03 14:17:05 -08001035 mLineDirections[j] = AndroidBidi.directions(dir, chdirs, start - widthStart, chs,
1036 start - widthStart, end - start);
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 mLineCount++;
1040 return v;
1041 }
1042
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001043 private void calculateEllipsis(int lineStart, int lineEnd,
1044 float[] widths, int widthStart,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 float avail, TextUtils.TruncateAt where,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001046 int line, float textWidth, TextPaint paint,
1047 boolean forceEllipsis) {
Selim Cinek365ec092017-03-09 00:10:52 -08001048 avail -= getTotalInsets(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001049 if (textWidth <= avail && !forceEllipsis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 // Everything fits!
1051 mLines[mColumns * line + ELLIPSIS_START] = 0;
1052 mLines[mColumns * line + ELLIPSIS_COUNT] = 0;
1053 return;
1054 }
1055
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001056 float ellipsisWidth = paint.measureText(
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001057 (where == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001058 TextUtils.ELLIPSIS_TWO_DOTS : TextUtils.ELLIPSIS_NORMAL, 0, 1);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001059 int ellipsisStart = 0;
1060 int ellipsisCount = 0;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001061 int len = lineEnd - lineStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001063 // We only support start ellipsis on a single line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 if (where == TextUtils.TruncateAt.START) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001065 if (mMaximumVisibleLineCount == 1) {
1066 float sum = 0;
1067 int i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068
Keisuke Kuroyanagied2eea12015-04-14 18:18:35 +09001069 for (i = len; i > 0; i--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001070 float w = widths[i - 1 + lineStart - widthStart];
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001071 if (w + sum + ellipsisWidth > avail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001072 while (i < len && widths[i + lineStart - widthStart] == 0.0f) {
1073 i++;
1074 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001075 break;
1076 }
1077
1078 sum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001081 ellipsisStart = 0;
1082 ellipsisCount = i;
1083 } else {
1084 if (Log.isLoggable(TAG, Log.WARN)) {
1085 Log.w(TAG, "Start Ellipsis only supported with one line");
1086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001088 } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE ||
1089 where == TextUtils.TruncateAt.END_SMALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 float sum = 0;
1091 int i;
1092
1093 for (i = 0; i < len; i++) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001094 float w = widths[i + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001096 if (w + sum + ellipsisWidth > avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 break;
1098 }
1099
1100 sum += w;
1101 }
1102
1103 ellipsisStart = i;
1104 ellipsisCount = len - i;
Fabrice Di Meglioaef455f2011-08-29 15:39:11 -07001105 if (forceEllipsis && ellipsisCount == 0 && len > 0) {
1106 ellipsisStart = len - 1;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001107 ellipsisCount = 1;
1108 }
1109 } else {
1110 // where = TextUtils.TruncateAt.MIDDLE We only support middle ellipsis on a single line
1111 if (mMaximumVisibleLineCount == 1) {
1112 float lsum = 0, rsum = 0;
1113 int left = 0, right = len;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001115 float ravail = (avail - ellipsisWidth) / 2;
Raph Levien0e3c5e82014-12-04 13:26:07 -08001116 for (right = len; right > 0; right--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001117 float w = widths[right - 1 + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001119 if (w + rsum > ravail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001120 while (right < len && widths[right + lineStart - widthStart] == 0.0f) {
1121 right++;
1122 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001123 break;
1124 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001125 rsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001128 float lavail = avail - ellipsisWidth - rsum;
1129 for (left = 0; left < right; left++) {
1130 float w = widths[left + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001132 if (w + lsum > lavail) {
1133 break;
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001136 lsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001139 ellipsisStart = left;
1140 ellipsisCount = right - left;
1141 } else {
1142 if (Log.isLoggable(TAG, Log.WARN)) {
1143 Log.w(TAG, "Middle Ellipsis only supported with one line");
1144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
Siyamed Sinir0745c722016-05-31 20:39:33 -07001147 mEllipsized = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart;
1149 mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount;
1150 }
1151
Selim Cinek365ec092017-03-09 00:10:52 -08001152 private float getTotalInsets(int line) {
1153 int totalIndent = 0;
1154 if (mLeftIndents != null) {
1155 totalIndent = mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1156 }
1157 if (mRightIndents != null) {
1158 totalIndent += mRightIndents[Math.min(line, mRightIndents.length - 1)];
1159 }
1160 return totalIndent;
1161 }
1162
Doug Felte8e45f22010-03-29 14:58:40 -07001163 // Override the base class so we can directly access our members,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 // rather than relying on member functions.
1165 // The logic mirrors that of Layout.getLineForVertical
1166 // FIXME: It may be faster to do a linear search for layouts without many lines.
Gilles Debunne66111472010-11-19 11:04:37 -08001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public int getLineForVertical(int vertical) {
1169 int high = mLineCount;
1170 int low = -1;
1171 int guess;
1172 int[] lines = mLines;
1173 while (high - low > 1) {
1174 guess = (high + low) >> 1;
1175 if (lines[mColumns * guess + TOP] > vertical){
1176 high = guess;
1177 } else {
1178 low = guess;
1179 }
1180 }
1181 if (low < 0) {
1182 return 0;
1183 } else {
1184 return low;
1185 }
1186 }
1187
Gilles Debunne66111472010-11-19 11:04:37 -08001188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 public int getLineCount() {
1190 return mLineCount;
1191 }
1192
Gilles Debunne66111472010-11-19 11:04:37 -08001193 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 public int getLineTop(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001195 return mLines[mColumns * line + TOP];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197
Gilles Debunne66111472010-11-19 11:04:37 -08001198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 public int getLineDescent(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001200 return mLines[mColumns * line + DESCENT];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
Gilles Debunne66111472010-11-19 11:04:37 -08001203 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 public int getLineStart(int line) {
1205 return mLines[mColumns * line + START] & START_MASK;
1206 }
1207
Gilles Debunne66111472010-11-19 11:04:37 -08001208 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 public int getParagraphDirection(int line) {
1210 return mLines[mColumns * line + DIR] >> DIR_SHIFT;
1211 }
1212
Gilles Debunne66111472010-11-19 11:04:37 -08001213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 public boolean getLineContainsTab(int line) {
1215 return (mLines[mColumns * line + TAB] & TAB_MASK) != 0;
1216 }
1217
Gilles Debunne66111472010-11-19 11:04:37 -08001218 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 public final Directions getLineDirections(int line) {
1220 return mLineDirections[line];
1221 }
1222
Gilles Debunne66111472010-11-19 11:04:37 -08001223 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public int getTopPadding() {
1225 return mTopPadding;
1226 }
1227
Gilles Debunne66111472010-11-19 11:04:37 -08001228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public int getBottomPadding() {
1230 return mBottomPadding;
1231 }
1232
Raph Levien26d443a2015-03-30 14:18:32 -07001233 /**
1234 * @hide
1235 */
1236 @Override
1237 public int getHyphen(int line) {
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001238 return mLines[mColumns * line + HYPHEN] & HYPHEN_MASK;
Raph Levien26d443a2015-03-30 14:18:32 -07001239 }
1240
Raph Levien2ea52902015-07-01 14:39:31 -07001241 /**
1242 * @hide
1243 */
1244 @Override
1245 public int getIndentAdjust(int line, Alignment align) {
1246 if (align == Alignment.ALIGN_LEFT) {
1247 if (mLeftIndents == null) {
1248 return 0;
1249 } else {
1250 return mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1251 }
1252 } else if (align == Alignment.ALIGN_RIGHT) {
1253 if (mRightIndents == null) {
1254 return 0;
1255 } else {
1256 return -mRightIndents[Math.min(line, mRightIndents.length - 1)];
1257 }
1258 } else if (align == Alignment.ALIGN_CENTER) {
1259 int left = 0;
1260 if (mLeftIndents != null) {
1261 left = mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1262 }
1263 int right = 0;
1264 if (mRightIndents != null) {
1265 right = mRightIndents[Math.min(line, mRightIndents.length - 1)];
1266 }
1267 return (left - right) >> 1;
1268 } else {
1269 throw new AssertionError("unhandled alignment " + align);
1270 }
1271 }
1272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 @Override
1274 public int getEllipsisCount(int line) {
1275 if (mColumns < COLUMNS_ELLIPSIZE) {
1276 return 0;
1277 }
1278
1279 return mLines[mColumns * line + ELLIPSIS_COUNT];
1280 }
1281
1282 @Override
1283 public int getEllipsisStart(int line) {
1284 if (mColumns < COLUMNS_ELLIPSIZE) {
1285 return 0;
1286 }
1287
1288 return mLines[mColumns * line + ELLIPSIS_START];
1289 }
1290
1291 @Override
1292 public int getEllipsizedWidth() {
1293 return mEllipsizedWidth;
1294 }
1295
Siyamed Sinir0745c722016-05-31 20:39:33 -07001296 /**
1297 * Return the total height of this layout.
1298 *
1299 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
1300 *
1301 * @hide
1302 */
1303 public int getHeight(boolean cap) {
1304 if (cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight == -1 &&
1305 Log.isLoggable(TAG, Log.WARN)) {
1306 Log.w(TAG, "maxLineHeight should not be -1. "
1307 + " maxLines:" + mMaximumVisibleLineCount
1308 + " lineCount:" + mLineCount);
1309 }
1310
1311 return cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight != -1 ?
1312 mMaxLineHeight : super.getHeight();
1313 }
1314
Raph Levien70616ec2015-03-04 10:41:30 -08001315 private static native long nNewBuilder();
1316 private static native void nFreeBuilder(long nativePtr);
1317 private static native void nFinishBuilder(long nativePtr);
Raph Levien26d443a2015-03-30 14:18:32 -07001318
Roozbeh Pournadera59c3fe2017-02-27 10:13:44 -08001319 /* package */ static native long nLoadHyphenator(ByteBuffer buf, int offset,
1320 int minPrefix, int minSuffix);
Raph Levien26d443a2015-03-30 14:18:32 -07001321
Roozbeh Pournaderef7cfa12017-06-15 12:39:04 -07001322 private static native void nSetLocales(long nativePtr, String locales,
1323 long[] nativeHyphenators);
Raph Levien70616ec2015-03-04 10:41:30 -08001324
Raph Leviene319d5a2015-04-14 23:51:07 -07001325 private static native void nSetIndents(long nativePtr, int[] indents);
1326
Raph Levienc94f7422015-03-06 19:19:48 -08001327 // Set up paragraph text and settings; done as one big method to minimize jni crossings
1328 private static native void nSetupParagraph(long nativePtr, char[] text, int length,
1329 float firstWidth, int firstWidthLineCount, float restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001330 int[] variableTabStops, int defaultTabStop, int breakStrategy, int hyphenationFrequency,
1331 boolean isJustified);
Raph Levien70616ec2015-03-04 10:41:30 -08001332
1333 private static native float nAddStyleRun(long nativePtr, long nativePaint,
1334 long nativeTypeface, int start, int end, boolean isRtl);
1335
1336 private static native void nAddMeasuredRun(long nativePtr,
1337 int start, int end, float[] widths);
1338
1339 private static native void nAddReplacementRun(long nativePtr, int start, int end, float width);
1340
1341 private static native void nGetWidths(long nativePtr, float[] widths);
1342
Anish Athalyec8f9e622014-07-21 15:26:34 -07001343 // populates LineBreaks and returns the number of breaks found
1344 //
1345 // the arrays inside the LineBreaks objects are passed in as well
1346 // to reduce the number of JNI calls in the common case where the
1347 // arrays do not have to be resized
Raph Levienc94f7422015-03-06 19:19:48 -08001348 private static native int nComputeLineBreaks(long nativePtr, LineBreaks recycle,
Raph Levien26d443a2015-03-30 14:18:32 -07001349 int[] recycleBreaks, float[] recycleWidths, int[] recycleFlags, int recycleLength);
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 private int mLineCount;
1352 private int mTopPadding, mBottomPadding;
1353 private int mColumns;
1354 private int mEllipsizedWidth;
1355
Siyamed Sinir0745c722016-05-31 20:39:33 -07001356 /**
1357 * Keeps track if ellipsize is applied to the text.
1358 */
1359 private boolean mEllipsized;
1360
1361 /**
1362 * If maxLines is set, ellipsize is not set, and the actual line count of text is greater than
1363 * or equal to maxLine, this variable holds the ideal visual height of the maxLine'th line
1364 * starting from the top of the layout. If maxLines is not set its value will be -1.
1365 *
1366 * The value is the same as getLineTop(maxLines) for ellipsized version where structurally no
1367 * more than maxLines is contained.
1368 */
1369 private int mMaxLineHeight = -1;
1370
Raph Levien26d443a2015-03-30 14:18:32 -07001371 private static final int COLUMNS_NORMAL = 4;
1372 private static final int COLUMNS_ELLIPSIZE = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 private static final int START = 0;
1374 private static final int DIR = START;
1375 private static final int TAB = START;
1376 private static final int TOP = 1;
1377 private static final int DESCENT = 2;
Raph Levien26d443a2015-03-30 14:18:32 -07001378 private static final int HYPHEN = 3;
1379 private static final int ELLIPSIS_START = 4;
1380 private static final int ELLIPSIS_COUNT = 5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 private int[] mLines;
1383 private Directions[] mLineDirections;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001384 private int mMaximumVisibleLineCount = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385
1386 private static final int START_MASK = 0x1FFFFFFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 private static final int DIR_SHIFT = 30;
1388 private static final int TAB_MASK = 0x20000000;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001389 private static final int HYPHEN_MASK = 0xFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390
Doug Feltc982f602010-05-25 11:51:40 -07001391 private static final int TAB_INCREMENT = 20; // same as Layout, but that's private
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001393 private static final char CHAR_NEW_LINE = '\n';
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001394
1395 private static final double EXTRA_ROUNDING = 0.5;
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001396
Anish Athalyec8f9e622014-07-21 15:26:34 -07001397 // This is used to return three arrays from a single JNI call when
1398 // performing line breaking
Deepanshu Gupta70539192014-10-15 15:57:40 -07001399 /*package*/ static class LineBreaks {
Anish Athalyec8f9e622014-07-21 15:26:34 -07001400 private static final int INITIAL_SIZE = 16;
1401 public int[] breaks = new int[INITIAL_SIZE];
1402 public float[] widths = new float[INITIAL_SIZE];
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001403 public int[] flags = new int[INITIAL_SIZE]; // hasTab
Anish Athalyec8f9e622014-07-21 15:26:34 -07001404 // breaks, widths, and flags should all have the same length
1405 }
1406
Raph Levien2ea52902015-07-01 14:39:31 -07001407 private int[] mLeftIndents;
1408 private int[] mRightIndents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409}