blob: cb5b073318d8a6c81c0fb4d60dfef162eb7b7c04 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.text.style.LeadingMarginSpan;
Gilles Debunne66111472010-11-19 11:04:37 -080022import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.text.style.LineHeightSpan;
24import android.text.style.MetricAffectingSpan;
Doug Feltc982f602010-05-25 11:51:40 -070025import android.text.style.TabStopSpan;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -070026import android.util.Log;
Raph Levien39b4db72015-03-25 13:18:20 -070027import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Doug Feltcb3791202011-07-07 11:57:48 -070029import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050030import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070031
Raph Levien091dba22015-08-31 16:21:20 -070032import java.nio.ByteBuffer;
Anish Athalyec8f9e622014-07-21 15:26:34 -070033import java.util.Arrays;
Raph Levien4c1f12e2015-03-02 16:29:23 -080034import java.util.Locale;
Anish Athalyec8f9e622014-07-21 15:26:34 -070035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036/**
37 * StaticLayout is a Layout for text that will not be edited after it
38 * is laid out. Use {@link DynamicLayout} for text that may change.
39 * <p>This is used by widgets to control text layout. You should not need
40 * to use this class directly unless you are implementing your own widget
41 * or custom display object, or would be tempted to call
Doug Felt4e0c5e52010-03-15 16:56:02 -070042 * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int,
43 * float, float, android.graphics.Paint)
44 * Canvas.drawText()} directly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -080046public class StaticLayout extends Layout {
47
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -070048 static final String TAG = "StaticLayout";
49
Raph Leviend3ab6922015-03-02 14:30:53 -080050 /**
Raph Levien531c30c2015-04-30 16:29:59 -070051 * Builder for static layouts. The builder is a newer pattern for constructing
52 * StaticLayout objects and should be preferred over the constructors,
53 * particularly to access newer features. To build a static layout, first
54 * call {@link #obtain} with the required arguments (text, paint, and width),
55 * then call setters for optional parameters, and finally {@link #build}
56 * to build the StaticLayout object. Parameters not explicitly set will get
57 * default values.
Raph Leviend3ab6922015-03-02 14:30:53 -080058 */
59 public final static class Builder {
Raph Levien4c1f12e2015-03-02 16:29:23 -080060 private Builder() {
61 mNativePtr = nNewBuilder();
62 }
63
Raph Levien531c30c2015-04-30 16:29:59 -070064 /**
65 * Obtain a builder for constructing StaticLayout objects
66 *
67 * @param source The text to be laid out, optionally with spans
68 * @param start The index of the start of the text
69 * @param end The index + 1 of the end of the text
70 * @param paint The base paint used for layout
71 * @param width The width in pixels
72 * @return a builder object used for constructing the StaticLayout
73 */
Raph Levienebd66ca2015-04-30 15:27:57 -070074 public static Builder obtain(CharSequence source, int start, int end, TextPaint paint,
75 int width) {
Raph Levien39b4db72015-03-25 13:18:20 -070076 Builder b = sPool.acquire();
Raph Leviend3ab6922015-03-02 14:30:53 -080077 if (b == null) {
78 b = new Builder();
79 }
80
81 // set default initial values
Raph Levien39b4db72015-03-25 13:18:20 -070082 b.mText = source;
83 b.mStart = start;
84 b.mEnd = end;
Raph Levienebd66ca2015-04-30 15:27:57 -070085 b.mPaint = paint;
Raph Levien39b4db72015-03-25 13:18:20 -070086 b.mWidth = width;
87 b.mAlignment = Alignment.ALIGN_NORMAL;
Raph Leviend3ab6922015-03-02 14:30:53 -080088 b.mTextDir = TextDirectionHeuristics.FIRSTSTRONG_LTR;
89 b.mSpacingMult = 1.0f;
90 b.mSpacingAdd = 0.0f;
91 b.mIncludePad = true;
Raph Levien39b4db72015-03-25 13:18:20 -070092 b.mEllipsizedWidth = width;
Raph Leviend3ab6922015-03-02 14:30:53 -080093 b.mEllipsize = null;
94 b.mMaxLines = Integer.MAX_VALUE;
Raph Levien3bd60c72015-05-06 14:26:35 -070095 b.mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE;
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070096 b.mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE;
Seigo Nonaka09da71a2016-11-28 16:24:14 +090097 b.mJustify = false;
Raph Leviend3ab6922015-03-02 14:30:53 -080098
99 b.mMeasuredText = MeasuredText.obtain();
100 return b;
101 }
102
Raph Levien39b4db72015-03-25 13:18:20 -0700103 private static void recycle(Builder b) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800104 b.mPaint = null;
105 b.mText = null;
106 MeasuredText.recycle(b.mMeasuredText);
Raph Levien3bd60c72015-05-06 14:26:35 -0700107 b.mMeasuredText = null;
Raph Levien2ea52902015-07-01 14:39:31 -0700108 b.mLeftIndents = null;
109 b.mRightIndents = null;
Raph Levien3bd60c72015-05-06 14:26:35 -0700110 nFinishBuilder(b.mNativePtr);
Raph Levien39b4db72015-03-25 13:18:20 -0700111 sPool.release(b);
Raph Leviend3ab6922015-03-02 14:30:53 -0800112 }
113
114 // release any expensive state
115 /* package */ void finish() {
Raph Levien4c1f12e2015-03-02 16:29:23 -0800116 nFinishBuilder(mNativePtr);
Raph Levien22ba7862015-07-29 12:34:13 -0700117 mText = null;
118 mPaint = null;
119 mLeftIndents = null;
120 mRightIndents = null;
Raph Leviend3ab6922015-03-02 14:30:53 -0800121 mMeasuredText.finish();
122 }
123
124 public Builder setText(CharSequence source) {
125 return setText(source, 0, source.length());
126 }
127
Raph Levien531c30c2015-04-30 16:29:59 -0700128 /**
129 * Set the text. Only useful when re-using the builder, which is done for
130 * the internal implementation of {@link DynamicLayout} but not as part
131 * of normal {@link StaticLayout} usage.
132 *
133 * @param source The text to be laid out, optionally with spans
134 * @param start The index of the start of the text
135 * @param end The index + 1 of the end of the text
136 * @return this builder, useful for chaining
137 *
138 * @hide
139 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800140 public Builder setText(CharSequence source, int start, int end) {
141 mText = source;
142 mStart = start;
143 mEnd = end;
144 return this;
145 }
146
Raph Levien531c30c2015-04-30 16:29:59 -0700147 /**
148 * Set the paint. Internal for reuse cases only.
149 *
150 * @param paint The base paint used for layout
151 * @return this builder, useful for chaining
152 *
153 * @hide
154 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800155 public Builder setPaint(TextPaint paint) {
156 mPaint = paint;
157 return this;
158 }
159
Raph Levien531c30c2015-04-30 16:29:59 -0700160 /**
161 * Set the width. Internal for reuse cases only.
162 *
163 * @param width The width in pixels
164 * @return this builder, useful for chaining
165 *
166 * @hide
167 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800168 public Builder setWidth(int width) {
169 mWidth = width;
170 if (mEllipsize == null) {
171 mEllipsizedWidth = width;
172 }
173 return this;
174 }
175
Raph Levien531c30c2015-04-30 16:29:59 -0700176 /**
177 * Set the alignment. The default is {@link Layout.Alignment#ALIGN_NORMAL}.
178 *
179 * @param alignment Alignment for the resulting {@link StaticLayout}
180 * @return this builder, useful for chaining
181 */
Raph Levien39b4db72015-03-25 13:18:20 -0700182 public Builder setAlignment(Alignment alignment) {
183 mAlignment = alignment;
184 return this;
185 }
186
Raph Levien531c30c2015-04-30 16:29:59 -0700187 /**
188 * Set the text direction heuristic. The text direction heuristic is used to
189 * resolve text direction based per-paragraph based on the input text. The default is
190 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR}.
191 *
192 * @param textDir text direction heuristic for resolving BiDi behavior.
193 * @return this builder, useful for chaining
194 */
Raph Leviena6a08282015-06-03 13:20:45 -0700195 public Builder setTextDirection(TextDirectionHeuristic textDir) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800196 mTextDir = textDir;
197 return this;
198 }
199
Raph Levien531c30c2015-04-30 16:29:59 -0700200 /**
201 * Set line spacing parameters. The default is 0.0 for {@code spacingAdd}
202 * and 1.0 for {@code spacingMult}.
203 *
204 * @param spacingAdd line spacing add
205 * @param spacingMult line spacing multiplier
206 * @return this builder, useful for chaining
207 * @see android.widget.TextView#setLineSpacing
208 */
209 public Builder setLineSpacing(float spacingAdd, float spacingMult) {
210 mSpacingAdd = spacingAdd;
Raph Leviend3ab6922015-03-02 14:30:53 -0800211 mSpacingMult = spacingMult;
212 return this;
213 }
214
Raph Levien531c30c2015-04-30 16:29:59 -0700215 /**
216 * Set whether to include extra space beyond font ascent and descent (which is
217 * needed to avoid clipping in some languages, such as Arabic and Kannada). The
218 * default is {@code true}.
219 *
220 * @param includePad whether to include padding
221 * @return this builder, useful for chaining
222 * @see android.widget.TextView#setIncludeFontPadding
223 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800224 public Builder setIncludePad(boolean includePad) {
225 mIncludePad = includePad;
226 return this;
227 }
228
Raph Levien531c30c2015-04-30 16:29:59 -0700229 /**
230 * Set the width as used for ellipsizing purposes, if it differs from the
231 * normal layout width. The default is the {@code width}
232 * passed to {@link #obtain}.
233 *
234 * @param ellipsizedWidth width used for ellipsizing, in pixels
235 * @return this builder, useful for chaining
236 * @see android.widget.TextView#setEllipsize
237 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800238 public Builder setEllipsizedWidth(int ellipsizedWidth) {
239 mEllipsizedWidth = ellipsizedWidth;
240 return this;
241 }
242
Raph Levien531c30c2015-04-30 16:29:59 -0700243 /**
244 * Set ellipsizing on the layout. Causes words that are longer than the view
245 * is wide, or exceeding the number of lines (see #setMaxLines) in the case
246 * of {@link android.text.TextUtils.TruncateAt#END} or
247 * {@link android.text.TextUtils.TruncateAt#MARQUEE}, to be ellipsized instead
248 * of broken. The default is
249 * {@code null}, indicating no ellipsis is to be applied.
250 *
251 * @param ellipsize type of ellipsis behavior
252 * @return this builder, useful for chaining
253 * @see android.widget.TextView#setEllipsize
254 */
255 public Builder setEllipsize(@Nullable TextUtils.TruncateAt ellipsize) {
Raph Leviend3ab6922015-03-02 14:30:53 -0800256 mEllipsize = ellipsize;
257 return this;
258 }
259
Raph Levien531c30c2015-04-30 16:29:59 -0700260 /**
261 * Set maximum number of lines. This is particularly useful in the case of
262 * ellipsizing, where it changes the layout of the last line. The default is
263 * unlimited.
264 *
265 * @param maxLines maximum number of lines in the layout
266 * @return this builder, useful for chaining
267 * @see android.widget.TextView#setMaxLines
268 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800269 public Builder setMaxLines(int maxLines) {
270 mMaxLines = maxLines;
271 return this;
272 }
273
Raph Levien531c30c2015-04-30 16:29:59 -0700274 /**
275 * Set break strategy, useful for selecting high quality or balanced paragraph
276 * layout options. The default is {@link Layout#BREAK_STRATEGY_SIMPLE}.
277 *
278 * @param breakStrategy break strategy for paragraph layout
279 * @return this builder, useful for chaining
280 * @see android.widget.TextView#setBreakStrategy
281 */
Raph Levien39b4db72015-03-25 13:18:20 -0700282 public Builder setBreakStrategy(@BreakStrategy int breakStrategy) {
283 mBreakStrategy = breakStrategy;
284 return this;
285 }
286
Raph Levien531c30c2015-04-30 16:29:59 -0700287 /**
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700288 * Set hyphenation frequency, to control the amount of automatic hyphenation used. The
289 * default is {@link Layout#HYPHENATION_FREQUENCY_NONE}.
290 *
291 * @param hyphenationFrequency hyphenation frequency for the paragraph
292 * @return this builder, useful for chaining
293 * @see android.widget.TextView#setHyphenationFrequency
294 */
295 public Builder setHyphenationFrequency(@HyphenationFrequency int hyphenationFrequency) {
296 mHyphenationFrequency = hyphenationFrequency;
297 return this;
298 }
299
300 /**
Raph Levien531c30c2015-04-30 16:29:59 -0700301 * Set indents. Arguments are arrays holding an indent amount, one per line, measured in
302 * pixels. For lines past the last element in the array, the last element repeats.
303 *
304 * @param leftIndents array of indent values for left margin, in pixels
305 * @param rightIndents array of indent values for right margin, in pixels
306 * @return this builder, useful for chaining
Raph Levien531c30c2015-04-30 16:29:59 -0700307 */
Raph Leviene319d5a2015-04-14 23:51:07 -0700308 public Builder setIndents(int[] leftIndents, int[] rightIndents) {
Raph Levien2ea52902015-07-01 14:39:31 -0700309 mLeftIndents = leftIndents;
310 mRightIndents = rightIndents;
Raph Leviene319d5a2015-04-14 23:51:07 -0700311 int leftLen = leftIndents == null ? 0 : leftIndents.length;
312 int rightLen = rightIndents == null ? 0 : rightIndents.length;
313 int[] indents = new int[Math.max(leftLen, rightLen)];
314 for (int i = 0; i < indents.length; i++) {
315 int leftMargin = i < leftLen ? leftIndents[i] : 0;
316 int rightMargin = i < rightLen ? rightIndents[i] : 0;
317 indents[i] = leftMargin + rightMargin;
318 }
319 nSetIndents(mNativePtr, indents);
320 return this;
321 }
322
Raph Levien70616ec2015-03-04 10:41:30 -0800323 /**
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900324 * Enables or disables paragraph justification. The default value is disabled (false).
325 *
326 * @param justify true for enabling and false for disabling paragraph justification.
327 * @return this builder, useful for chaining.
328 */
329 public Builder setJustify(boolean justify) {
330 mJustify = justify;
331 return this;
332 }
333
334 /**
Raph Levien70616ec2015-03-04 10:41:30 -0800335 * Measurement and break iteration is done in native code. The protocol for using
336 * the native code is as follows.
337 *
Raph Levien26d443a2015-03-30 14:18:32 -0700338 * For each paragraph, do a nSetupParagraph, which sets paragraph text, line width, tab
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700339 * stops, break strategy, and hyphenation frequency (and possibly other parameters in the
340 * future).
Raph Levienc94f7422015-03-06 19:19:48 -0800341 *
342 * Then, for each run within the paragraph:
Raph Levien70616ec2015-03-04 10:41:30 -0800343 * - setLocale (this must be done at least for the first run, optional afterwards)
344 * - one of the following, depending on the type of run:
345 * + addStyleRun (a text run, to be measured in native code)
346 * + addMeasuredRun (a run already measured in Java, passed into native code)
347 * + addReplacementRun (a replacement run, width is given)
348 *
349 * After measurement, nGetWidths() is valid if the widths are needed (eg for ellipsis).
350 * Run nComputeLineBreaks() to obtain line breaks for the paragraph.
351 *
352 * After all paragraphs, call finish() to release expensive buffers.
353 */
354
355 private void setLocale(Locale locale) {
Raph Levien4c1f12e2015-03-02 16:29:23 -0800356 if (!locale.equals(mLocale)) {
Roozbeh Pournadere7eac6f2015-08-07 15:13:30 -0700357 nSetLocale(mNativePtr, locale.toLanguageTag(),
358 Hyphenator.get(locale).getNativePtr());
Raph Levien4c1f12e2015-03-02 16:29:23 -0800359 mLocale = locale;
360 }
361 }
362
Raph Levien70616ec2015-03-04 10:41:30 -0800363 /* package */ float addStyleRun(TextPaint paint, int start, int end, boolean isRtl) {
364 return nAddStyleRun(mNativePtr, paint.getNativeInstance(), paint.mNativeTypeface,
365 start, end, isRtl);
366 }
367
368 /* package */ void addMeasuredRun(int start, int end, float[] widths) {
369 nAddMeasuredRun(mNativePtr, start, end, widths);
370 }
371
372 /* package */ void addReplacementRun(int start, int end, float width) {
373 nAddReplacementRun(mNativePtr, start, end, width);
374 }
375
Raph Levien531c30c2015-04-30 16:29:59 -0700376 /**
377 * Build the {@link StaticLayout} after options have been set.
378 *
379 * <p>Note: the builder object must not be reused in any way after calling this
380 * method. Setting parameters after calling this method, or calling it a second
381 * time on the same builder object, will likely lead to unexpected results.
382 *
383 * @return the newly constructed {@link StaticLayout} object
384 */
Raph Leviend3ab6922015-03-02 14:30:53 -0800385 public StaticLayout build() {
Raph Levien39b4db72015-03-25 13:18:20 -0700386 StaticLayout result = new StaticLayout(this);
387 Builder.recycle(this);
Raph Leviend3ab6922015-03-02 14:30:53 -0800388 return result;
389 }
390
Raph Levien4c1f12e2015-03-02 16:29:23 -0800391 @Override
392 protected void finalize() throws Throwable {
393 try {
394 nFreeBuilder(mNativePtr);
395 } finally {
396 super.finalize();
397 }
398 }
399
400 /* package */ long mNativePtr;
401
Raph Leviend3ab6922015-03-02 14:30:53 -0800402 CharSequence mText;
403 int mStart;
404 int mEnd;
405 TextPaint mPaint;
406 int mWidth;
Raph Levien39b4db72015-03-25 13:18:20 -0700407 Alignment mAlignment;
Raph Leviend3ab6922015-03-02 14:30:53 -0800408 TextDirectionHeuristic mTextDir;
409 float mSpacingMult;
410 float mSpacingAdd;
411 boolean mIncludePad;
412 int mEllipsizedWidth;
413 TextUtils.TruncateAt mEllipsize;
414 int mMaxLines;
Raph Levien39b4db72015-03-25 13:18:20 -0700415 int mBreakStrategy;
Roozbeh Pournader95c7a132015-05-12 12:01:06 -0700416 int mHyphenationFrequency;
Raph Levien2ea52902015-07-01 14:39:31 -0700417 int[] mLeftIndents;
418 int[] mRightIndents;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900419 boolean mJustify;
Raph Leviend3ab6922015-03-02 14:30:53 -0800420
421 Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt();
422
423 // This will go away and be subsumed by native builder code
424 MeasuredText mMeasuredText;
425
Raph Levien4c1f12e2015-03-02 16:29:23 -0800426 Locale mLocale;
427
Raph Levien39b4db72015-03-25 13:18:20 -0700428 private static final SynchronizedPool<Builder> sPool = new SynchronizedPool<Builder>(3);
Raph Leviend3ab6922015-03-02 14:30:53 -0800429 }
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public StaticLayout(CharSequence source, TextPaint paint,
432 int width,
433 Alignment align, float spacingmult, float spacingadd,
434 boolean includepad) {
435 this(source, 0, source.length(), paint, width, align,
436 spacingmult, spacingadd, includepad);
437 }
438
Doug Feltcb3791202011-07-07 11:57:48 -0700439 /**
440 * @hide
441 */
442 public StaticLayout(CharSequence source, TextPaint paint,
443 int width, Alignment align, TextDirectionHeuristic textDir,
444 float spacingmult, float spacingadd,
445 boolean includepad) {
446 this(source, 0, source.length(), paint, width, align, textDir,
447 spacingmult, spacingadd, includepad);
448 }
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 public StaticLayout(CharSequence source, int bufstart, int bufend,
451 TextPaint paint, int outerwidth,
452 Alignment align,
453 float spacingmult, float spacingadd,
454 boolean includepad) {
455 this(source, bufstart, bufend, paint, outerwidth, align,
456 spacingmult, spacingadd, includepad, null, 0);
457 }
458
Doug Feltcb3791202011-07-07 11:57:48 -0700459 /**
460 * @hide
461 */
462 public StaticLayout(CharSequence source, int bufstart, int bufend,
463 TextPaint paint, int outerwidth,
464 Alignment align, TextDirectionHeuristic textDir,
465 float spacingmult, float spacingadd,
466 boolean includepad) {
467 this(source, bufstart, bufend, paint, outerwidth, align, textDir,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700468 spacingmult, spacingadd, includepad, null, 0, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700469}
470
471 public StaticLayout(CharSequence source, int bufstart, int bufend,
472 TextPaint paint, int outerwidth,
473 Alignment align,
474 float spacingmult, float spacingadd,
475 boolean includepad,
476 TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
477 this(source, bufstart, bufend, paint, outerwidth, align,
478 TextDirectionHeuristics.FIRSTSTRONG_LTR,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700479 spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);
Doug Feltcb3791202011-07-07 11:57:48 -0700480 }
481
482 /**
483 * @hide
484 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 public StaticLayout(CharSequence source, int bufstart, int bufend,
486 TextPaint paint, int outerwidth,
Doug Feltcb3791202011-07-07 11:57:48 -0700487 Alignment align, TextDirectionHeuristic textDir,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 float spacingmult, float spacingadd,
489 boolean includepad,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700490 TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 super((ellipsize == null)
Doug Felt4e0c5e52010-03-15 16:56:02 -0700492 ? source
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 : (source instanceof Spanned)
494 ? new SpannedEllipsizer(source)
495 : new Ellipsizer(source),
Doug Feltcb3791202011-07-07 11:57:48 -0700496 paint, outerwidth, align, textDir, spacingmult, spacingadd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
Raph Levienebd66ca2015-04-30 15:27:57 -0700498 Builder b = Builder.obtain(source, bufstart, bufend, paint, outerwidth)
Raph Levien39b4db72015-03-25 13:18:20 -0700499 .setAlignment(align)
Raph Leviena6a08282015-06-03 13:20:45 -0700500 .setTextDirection(textDir)
Raph Levien531c30c2015-04-30 16:29:59 -0700501 .setLineSpacing(spacingadd, spacingmult)
Raph Leviend3ab6922015-03-02 14:30:53 -0800502 .setIncludePad(includepad)
503 .setEllipsizedWidth(ellipsizedWidth)
504 .setEllipsize(ellipsize)
505 .setMaxLines(maxLines);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 /*
507 * This is annoying, but we can't refer to the layout until
508 * superclass construction is finished, and the superclass
509 * constructor wants the reference to the display text.
Doug Felt4e0c5e52010-03-15 16:56:02 -0700510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 * This will break if the superclass constructor ever actually
512 * cares about the content instead of just holding the reference.
513 */
514 if (ellipsize != null) {
515 Ellipsizer e = (Ellipsizer) getText();
516
517 e.mLayout = this;
518 e.mWidth = ellipsizedWidth;
519 e.mMethod = ellipsize;
520 mEllipsizedWidth = ellipsizedWidth;
521
522 mColumns = COLUMNS_ELLIPSIZE;
523 } else {
524 mColumns = COLUMNS_NORMAL;
525 mEllipsizedWidth = outerwidth;
526 }
527
Adam Lesinski776abc22014-03-07 11:30:59 -0500528 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
529 mLines = new int[mLineDirections.length];
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700530 mMaximumVisibleLineCount = maxLines;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
Raph Levien70616ec2015-03-04 10:41:30 -0800532 generate(b, b.mIncludePad, b.mIncludePad);
Doug Felte8e45f22010-03-29 14:58:40 -0700533
Raph Leviend3ab6922015-03-02 14:30:53 -0800534 Builder.recycle(b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700537 /* package */ StaticLayout(CharSequence text) {
538 super(text, null, 0, null, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539
540 mColumns = COLUMNS_ELLIPSIZE;
Adam Lesinski776abc22014-03-07 11:30:59 -0500541 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
542 mLines = new int[mLineDirections.length];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544
Raph Levien39b4db72015-03-25 13:18:20 -0700545 private StaticLayout(Builder b) {
546 super((b.mEllipsize == null)
547 ? b.mText
548 : (b.mText instanceof Spanned)
549 ? new SpannedEllipsizer(b.mText)
550 : new Ellipsizer(b.mText),
551 b.mPaint, b.mWidth, b.mAlignment, b.mSpacingMult, b.mSpacingAdd);
552
553 if (b.mEllipsize != null) {
554 Ellipsizer e = (Ellipsizer) getText();
555
556 e.mLayout = this;
557 e.mWidth = b.mEllipsizedWidth;
558 e.mMethod = b.mEllipsize;
559 mEllipsizedWidth = b.mEllipsizedWidth;
560
561 mColumns = COLUMNS_ELLIPSIZE;
562 } else {
563 mColumns = COLUMNS_NORMAL;
564 mEllipsizedWidth = b.mWidth;
565 }
566
567 mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns);
568 mLines = new int[mLineDirections.length];
569 mMaximumVisibleLineCount = b.mMaxLines;
570
Raph Levien2ea52902015-07-01 14:39:31 -0700571 mLeftIndents = b.mLeftIndents;
572 mRightIndents = b.mRightIndents;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900573 setJustify(b.mJustify);
Raph Levien2ea52902015-07-01 14:39:31 -0700574
Raph Levien39b4db72015-03-25 13:18:20 -0700575 generate(b, b.mIncludePad, b.mIncludePad);
576 }
577
Raph Leviend3ab6922015-03-02 14:30:53 -0800578 /* package */ void generate(Builder b, boolean includepad, boolean trackpad) {
579 CharSequence source = b.mText;
580 int bufStart = b.mStart;
581 int bufEnd = b.mEnd;
582 TextPaint paint = b.mPaint;
583 int outerWidth = b.mWidth;
584 TextDirectionHeuristic textDir = b.mTextDir;
585 float spacingmult = b.mSpacingMult;
586 float spacingadd = b.mSpacingAdd;
587 float ellipsizedWidth = b.mEllipsizedWidth;
588 TextUtils.TruncateAt ellipsize = b.mEllipsize;
Raph Levien4c1f12e2015-03-02 16:29:23 -0800589 LineBreaks lineBreaks = new LineBreaks(); // TODO: move to builder to avoid allocation costs
Anish Athalyec8f9e622014-07-21 15:26:34 -0700590 // store span end locations
591 int[] spanEndCache = new int[4];
592 // store fontMetrics per span range
593 // must be a multiple of 4 (and > 0) (store top, bottom, ascent, and descent per range)
594 int[] fmCache = new int[4 * 4];
Raph Levien4c1f12e2015-03-02 16:29:23 -0800595 b.setLocale(paint.getTextLocale()); // TODO: also respect LocaleSpan within the text
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 mLineCount = 0;
598
599 int v = 0;
600 boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
601
Raph Leviend3ab6922015-03-02 14:30:53 -0800602 Paint.FontMetricsInt fm = b.mFontMetricsInt;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800603 int[] chooseHtv = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
Raph Leviend3ab6922015-03-02 14:30:53 -0800605 MeasuredText measured = b.mMeasuredText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 Spanned spanned = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 if (source instanceof Spanned)
609 spanned = (Spanned) source;
610
Doug Felte8e45f22010-03-29 14:58:40 -0700611 int paraEnd;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800612 for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
613 paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
Doug Felte8e45f22010-03-29 14:58:40 -0700614 if (paraEnd < 0)
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800615 paraEnd = bufEnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 else
Doug Felte8e45f22010-03-29 14:58:40 -0700617 paraEnd++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
Anish Athalyec8f9e622014-07-21 15:26:34 -0700619 int firstWidthLineCount = 1;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800620 int firstWidth = outerWidth;
621 int restWidth = outerWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800623 LineHeightSpan[] chooseHt = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 if (spanned != null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -0700626 LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd,
Doug Felte8e45f22010-03-29 14:58:40 -0700627 LeadingMarginSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 for (int i = 0; i < sp.length; i++) {
Mark Wagner7b5676e2009-10-16 11:44:23 -0700629 LeadingMarginSpan lms = sp[i];
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800630 firstWidth -= sp[i].getLeadingMargin(true);
631 restWidth -= sp[i].getLeadingMargin(false);
Doug Feltcb3791202011-07-07 11:57:48 -0700632
Doug Feltc982f602010-05-25 11:51:40 -0700633 // LeadingMarginSpan2 is odd. The count affects all
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700634 // leading margin spans, not just this particular one
Doug Feltc982f602010-05-25 11:51:40 -0700635 if (lms instanceof LeadingMarginSpan2) {
636 LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700637 firstWidthLineCount = Math.max(firstWidthLineCount,
638 lms2.getLeadingMarginLineCount());
Mark Wagner7b5676e2009-10-16 11:44:23 -0700639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800642 chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700644 if (chooseHt.length == 0) {
645 chooseHt = null; // So that out() would not assume it has any contents
646 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800647 if (chooseHtv == null ||
648 chooseHtv.length < chooseHt.length) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500649 chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800652 for (int i = 0; i < chooseHt.length; i++) {
653 int o = spanned.getSpanStart(chooseHt[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654
Doug Felte8e45f22010-03-29 14:58:40 -0700655 if (o < paraStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 // starts in this layout, before the
657 // current paragraph
658
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800659 chooseHtv[i] = getLineTop(getLineForOffset(o));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 } else {
661 // starts in this paragraph
662
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800663 chooseHtv[i] = v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
666 }
667 }
668
Raph Levien70616ec2015-03-04 10:41:30 -0800669 measured.setPara(source, paraStart, paraEnd, textDir, b);
Doug Felte8e45f22010-03-29 14:58:40 -0700670 char[] chs = measured.mChars;
671 float[] widths = measured.mWidths;
672 byte[] chdirs = measured.mLevels;
673 int dir = measured.mDir;
674 boolean easy = measured.mEasy;
Raph Levienc94f7422015-03-06 19:19:48 -0800675
676 // tab stop locations
677 int[] variableTabStops = null;
678 if (spanned != null) {
679 TabStopSpan[] spans = getParagraphSpans(spanned, paraStart,
680 paraEnd, TabStopSpan.class);
681 if (spans.length > 0) {
682 int[] stops = new int[spans.length];
683 for (int i = 0; i < spans.length; i++) {
684 stops[i] = spans[i].getTabStop();
685 }
686 Arrays.sort(stops, 0, stops.length);
687 variableTabStops = stops;
688 }
689 }
690
Raph Levienc94f7422015-03-06 19:19:48 -0800691 nSetupParagraph(b.mNativePtr, chs, paraEnd - paraStart,
692 firstWidth, firstWidthLineCount, restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900693 variableTabStops, TAB_INCREMENT, b.mBreakStrategy, b.mHyphenationFrequency,
694 b.mJustify);
Raph Levien2ea52902015-07-01 14:39:31 -0700695 if (mLeftIndents != null || mRightIndents != null) {
696 // TODO(raph) performance: it would be better to do this once per layout rather
697 // than once per paragraph, but that would require a change to the native
698 // interface.
699 int leftLen = mLeftIndents == null ? 0 : mLeftIndents.length;
700 int rightLen = mRightIndents == null ? 0 : mRightIndents.length;
Siyamed Sinirf9a08862016-04-12 19:30:44 -0700701 int indentsLen = Math.max(1, Math.max(leftLen, rightLen) - mLineCount);
Raph Levien2ea52902015-07-01 14:39:31 -0700702 int[] indents = new int[indentsLen];
703 for (int i = 0; i < indentsLen; i++) {
704 int leftMargin = mLeftIndents == null ? 0 :
705 mLeftIndents[Math.min(i + mLineCount, leftLen - 1)];
706 int rightMargin = mRightIndents == null ? 0 :
707 mRightIndents[Math.min(i + mLineCount, rightLen - 1)];
708 indents[i] = leftMargin + rightMargin;
709 }
710 nSetIndents(b.mNativePtr, indents);
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
Anish Athalyec8f9e622014-07-21 15:26:34 -0700713 // measurement has to be done before performing line breaking
714 // but we don't want to recompute fontmetrics or span ranges the
715 // second time, so we cache those and then use those stored values
716 int fmCacheCount = 0;
717 int spanEndCacheCount = 0;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700718 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700719 if (fmCacheCount * 4 >= fmCache.length) {
720 int[] grow = new int[fmCacheCount * 4 * 2];
721 System.arraycopy(fmCache, 0, grow, 0, fmCacheCount * 4);
722 fmCache = grow;
723 }
724
725 if (spanEndCacheCount >= spanEndCache.length) {
726 int[] grow = new int[spanEndCacheCount * 2];
727 System.arraycopy(spanEndCache, 0, grow, 0, spanEndCacheCount);
728 spanEndCache = grow;
729 }
Doug Felte8e45f22010-03-29 14:58:40 -0700730
Gilles Debunnecd943a72012-06-07 17:54:47 -0700731 if (spanned == null) {
732 spanEnd = paraEnd;
Doug Felt23241882010-06-02 14:41:06 -0700733 int spanLen = spanEnd - spanStart;
Gilles Debunnecd943a72012-06-07 17:54:47 -0700734 measured.addStyleRun(paint, spanLen, fm);
735 } else {
736 spanEnd = spanned.nextSpanTransition(spanStart, paraEnd,
737 MetricAffectingSpan.class);
738 int spanLen = spanEnd - spanStart;
739 MetricAffectingSpan[] spans =
Doug Felt23241882010-06-02 14:41:06 -0700740 spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
Gilles Debunnecd943a72012-06-07 17:54:47 -0700741 spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class);
742 measured.addStyleRun(paint, spans, spanLen, fm);
Doug Felt23241882010-06-02 14:41:06 -0700743 }
744
Anish Athalyec8f9e622014-07-21 15:26:34 -0700745 // the order of storage here (top, bottom, ascent, descent) has to match the code below
746 // where these values are retrieved
747 fmCache[fmCacheCount * 4 + 0] = fm.top;
748 fmCache[fmCacheCount * 4 + 1] = fm.bottom;
749 fmCache[fmCacheCount * 4 + 2] = fm.ascent;
750 fmCache[fmCacheCount * 4 + 3] = fm.descent;
751 fmCacheCount++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
Anish Athalyec8f9e622014-07-21 15:26:34 -0700753 spanEndCache[spanEndCacheCount] = spanEnd;
754 spanEndCacheCount++;
755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756
Raph Levien70616ec2015-03-04 10:41:30 -0800757 nGetWidths(b.mNativePtr, widths);
Raph Levienc94f7422015-03-06 19:19:48 -0800758 int breakCount = nComputeLineBreaks(b.mNativePtr, lineBreaks, lineBreaks.breaks,
759 lineBreaks.widths, lineBreaks.flags, lineBreaks.breaks.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
Anish Athalyec8f9e622014-07-21 15:26:34 -0700761 int[] breaks = lineBreaks.breaks;
762 float[] lineWidths = lineBreaks.widths;
Raph Levien26d443a2015-03-30 14:18:32 -0700763 int[] flags = lineBreaks.flags;
Anish Athalyec8f9e622014-07-21 15:26:34 -0700764
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900765 final int remainingLineCount = mMaximumVisibleLineCount - mLineCount;
766 final boolean ellipsisMayBeApplied = ellipsize != null
767 && (ellipsize == TextUtils.TruncateAt.END
768 || (mMaximumVisibleLineCount == 1
769 && ellipsize != TextUtils.TruncateAt.MARQUEE));
Raph Levien04a84552015-07-09 15:38:04 -0700770 if (remainingLineCount > 0 && remainingLineCount < breakCount &&
771 ellipsisMayBeApplied) {
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900772 // Calculate width and flag.
773 float width = 0;
774 int flag = 0;
775 for (int i = remainingLineCount - 1; i < breakCount; i++) {
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700776 if (i == breakCount - 1) {
777 width += lineWidths[i];
778 } else {
779 for (int j = (i == 0 ? 0 : breaks[i - 1]); j < breaks[i]; j++) {
780 width += widths[j];
781 }
782 }
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900783 flag |= flags[i] & TAB_MASK;
784 }
Keisuke Kuroyanagi78f0d832016-05-10 12:21:33 -0700785 // Treat the last line and overflowed lines as a single line.
786 breaks[remainingLineCount - 1] = breaks[breakCount - 1];
Keisuke Kuroyanagif4a3f3a2015-06-10 09:37:32 +0900787 lineWidths[remainingLineCount - 1] = width;
788 flags[remainingLineCount - 1] = flag;
789
790 breakCount = remainingLineCount;
791 }
792
Anish Athalyec8f9e622014-07-21 15:26:34 -0700793 // here is the offset of the starting character of the line we are currently measuring
794 int here = paraStart;
795
796 int fmTop = 0, fmBottom = 0, fmAscent = 0, fmDescent = 0;
797 int fmCacheIndex = 0;
798 int spanEndCacheIndex = 0;
799 int breakIndex = 0;
800 for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
801 // retrieve end of span
802 spanEnd = spanEndCache[spanEndCacheIndex++];
803
804 // retrieve cached metrics, order matches above
805 fm.top = fmCache[fmCacheIndex * 4 + 0];
806 fm.bottom = fmCache[fmCacheIndex * 4 + 1];
807 fm.ascent = fmCache[fmCacheIndex * 4 + 2];
808 fm.descent = fmCache[fmCacheIndex * 4 + 3];
809 fmCacheIndex++;
810
811 if (fm.top < fmTop) {
812 fmTop = fm.top;
813 }
814 if (fm.ascent < fmAscent) {
815 fmAscent = fm.ascent;
816 }
817 if (fm.descent > fmDescent) {
818 fmDescent = fm.descent;
819 }
820 if (fm.bottom > fmBottom) {
821 fmBottom = fm.bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823
Anish Athalyec8f9e622014-07-21 15:26:34 -0700824 // skip breaks ending before current span range
825 while (breakIndex < breakCount && paraStart + breaks[breakIndex] < spanStart) {
826 breakIndex++;
827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
Anish Athalyec8f9e622014-07-21 15:26:34 -0700829 while (breakIndex < breakCount && paraStart + breaks[breakIndex] <= spanEnd) {
830 int endPos = paraStart + breaks[breakIndex];
831
Raph Levience4155a2015-03-11 11:02:33 -0700832 boolean moreChars = (endPos < bufEnd);
Raph Levien4c02e832014-12-12 11:17:01 -0800833
Anish Athalyec8f9e622014-07-21 15:26:34 -0700834 v = out(source, here, endPos,
835 fmAscent, fmDescent, fmTop, fmBottom,
Roozbeh Pournader431e5062015-10-16 02:27:03 -0700836 v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, flags[breakIndex],
Anish Athalyec8f9e622014-07-21 15:26:34 -0700837 needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad,
838 chs, widths, paraStart, ellipsize, ellipsizedWidth,
Raph Levien4c02e832014-12-12 11:17:01 -0800839 lineWidths[breakIndex], paint, moreChars);
Anish Athalyec8f9e622014-07-21 15:26:34 -0700840
841 if (endPos < spanEnd) {
842 // preserve metrics for current span
843 fmTop = fm.top;
844 fmBottom = fm.bottom;
845 fmAscent = fm.ascent;
846 fmDescent = fm.descent;
847 } else {
848 fmTop = fmBottom = fmAscent = fmDescent = 0;
849 }
850
851 here = endPos;
852 breakIndex++;
853
Siyamed Sinir0745c722016-05-31 20:39:33 -0700854 if (mLineCount >= mMaximumVisibleLineCount && mEllipsized) {
Anish Athalyec8f9e622014-07-21 15:26:34 -0700855 return;
856 }
857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800860 if (paraEnd == bufEnd)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 break;
862 }
863
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -0700864 if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) &&
Fabrice Di Meglioad0b0512011-10-04 17:21:26 -0700865 mLineCount < mMaximumVisibleLineCount) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800866 // Log.e("text", "output last " + bufEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867
Raph Levien70616ec2015-03-04 10:41:30 -0800868 measured.setPara(source, bufEnd, bufEnd, textDir, b);
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 paint.getFontMetricsInt(fm);
871
872 v = out(source,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800873 bufEnd, bufEnd, fm.ascent, fm.descent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 fm.top, fm.bottom,
875 v,
876 spacingmult, spacingadd, null,
Raph Levien26d443a2015-03-30 14:18:32 -0700877 null, fm, 0,
Fabrice Di Meglioe6318892013-06-18 20:03:41 -0700878 needMultiply, measured.mLevels, measured.mDir, measured.mEasy, bufEnd,
Gilles Debunned300e752011-10-17 13:37:36 -0700879 includepad, trackpad, null,
880 null, bufStart, ellipsize,
881 ellipsizedWidth, 0, paint, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883 }
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 private int out(CharSequence text, int start, int end,
886 int above, int below, int top, int bottom, int v,
887 float spacingmult, float spacingadd,
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800888 LineHeightSpan[] chooseHt, int[] chooseHtv,
Raph Levien26d443a2015-03-30 14:18:32 -0700889 Paint.FontMetricsInt fm, int flags,
Gilles Debunned300e752011-10-17 13:37:36 -0700890 boolean needMultiply, byte[] chdirs, int dir,
891 boolean easy, int bufEnd, boolean includePad,
892 boolean trackPad, char[] chs,
893 float[] widths, int widthStart, TextUtils.TruncateAt ellipsize,
894 float ellipsisWidth, float textWidth,
895 TextPaint paint, boolean moreChars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 int j = mLineCount;
897 int off = j * mColumns;
898 int want = off + mColumns + TOP;
899 int[] lines = mLines;
900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (want >= lines.length) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500902 Directions[] grow2 = ArrayUtils.newUnpaddedArray(
903 Directions.class, GrowingArrayUtils.growSize(want));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 System.arraycopy(mLineDirections, 0, grow2, 0,
905 mLineDirections.length);
906 mLineDirections = grow2;
Adam Lesinski776abc22014-03-07 11:30:59 -0500907
908 int[] grow = new int[grow2.length];
909 System.arraycopy(lines, 0, grow, 0, lines.length);
910 mLines = grow;
911 lines = grow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800914 if (chooseHt != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 fm.ascent = above;
916 fm.descent = below;
917 fm.top = top;
918 fm.bottom = bottom;
919
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800920 for (int i = 0; i < chooseHt.length; i++) {
921 if (chooseHt[i] instanceof LineHeightSpan.WithDensity) {
922 ((LineHeightSpan.WithDensity) chooseHt[i]).
923 chooseHeight(text, start, end, chooseHtv[i], v, fm, paint);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700924
925 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800926 chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm);
Eric Fischera9f1dd02009-08-12 15:00:10 -0700927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
930 above = fm.ascent;
931 below = fm.descent;
932 top = fm.top;
933 bottom = fm.bottom;
934 }
935
Raph Leviend97b0972014-04-24 12:51:35 -0700936 boolean firstLine = (j == 0);
937 boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount);
Siyamed Sinir0745c722016-05-31 20:39:33 -0700938
939 if (ellipsize != null) {
940 // If there is only one line, then do any type of ellipsis except when it is MARQUEE
941 // if there are multiple lines, just allow END ellipsis on the last line
942 boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount);
943
944 boolean doEllipsis =
945 (((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) &&
946 ellipsize != TextUtils.TruncateAt.MARQUEE) ||
947 (!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) &&
948 ellipsize == TextUtils.TruncateAt.END);
949 if (doEllipsis) {
950 calculateEllipsis(start, end, widths, widthStart,
951 ellipsisWidth, ellipsize, j,
952 textWidth, paint, forceEllipsis);
953 }
954 }
955
956 boolean lastLine = mEllipsized || (end == bufEnd);
Raph Leviend97b0972014-04-24 12:51:35 -0700957
958 if (firstLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800959 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 mTopPadding = top - above;
961 }
962
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800963 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 above = top;
965 }
966 }
Raph Leviend97b0972014-04-24 12:51:35 -0700967
968 int extra;
969
970 if (lastLine) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800971 if (trackPad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 mBottomPadding = bottom - below;
973 }
974
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800975 if (includePad) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 below = bottom;
977 }
978 }
979
Raph Leviend97b0972014-04-24 12:51:35 -0700980 if (needMultiply && !lastLine) {
Doug Felt10657582010-02-22 11:19:01 -0800981 double ex = (below - above) * (spacingmult - 1) + spacingadd;
982 if (ex >= 0) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800983 extra = (int)(ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -0800984 } else {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -0800985 extra = -(int)(-ex + EXTRA_ROUNDING);
Doug Felt10657582010-02-22 11:19:01 -0800986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 } else {
988 extra = 0;
989 }
990
991 lines[off + START] = start;
992 lines[off + TOP] = v;
993 lines[off + DESCENT] = below + extra;
994
Siyamed Sinir0745c722016-05-31 20:39:33 -0700995 // special case for non-ellipsized last visible line when maxLines is set
996 // store the height as if it was ellipsized
997 if (!mEllipsized && currentLineIsTheLastVisibleOne) {
998 // below calculation as if it was the last line
999 int maxLineBelow = includePad ? bottom : below;
1000 // similar to the calculation of v below, without the extra.
1001 mMaxLineHeight = v + (maxLineBelow - above);
1002 }
1003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 v += (below - above) + extra;
1005 lines[off + mColumns + START] = end;
1006 lines[off + mColumns + TOP] = v;
1007
Raph Levien26d443a2015-03-30 14:18:32 -07001008 // TODO: could move TAB to share same column as HYPHEN, simplifying this code and gaining
1009 // one bit for start field
1010 lines[off + TAB] |= flags & TAB_MASK;
1011 lines[off + HYPHEN] = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
Doug Felt9f7a4442010-03-01 12:45:56 -08001013 lines[off + DIR] |= dir << DIR_SHIFT;
1014 Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT;
1015 // easy means all chars < the first RTL, so no emoji, no nothing
Doug Felt4e0c5e52010-03-15 16:56:02 -07001016 // XXX a run with no text or all spaces is easy but might be an empty
Doug Felt9f7a4442010-03-01 12:45:56 -08001017 // RTL paragraph. Make sure easy is false if this is the case.
1018 if (easy) {
1019 mLineDirections[j] = linedirs;
1020 } else {
Gilles Debunnef3fa0cd2011-02-03 14:17:05 -08001021 mLineDirections[j] = AndroidBidi.directions(dir, chdirs, start - widthStart, chs,
1022 start - widthStart, end - start);
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08001023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 mLineCount++;
1026 return v;
1027 }
1028
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001029 private void calculateEllipsis(int lineStart, int lineEnd,
1030 float[] widths, int widthStart,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 float avail, TextUtils.TruncateAt where,
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001032 int line, float textWidth, TextPaint paint,
1033 boolean forceEllipsis) {
1034 if (textWidth <= avail && !forceEllipsis) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 // Everything fits!
1036 mLines[mColumns * line + ELLIPSIS_START] = 0;
1037 mLines[mColumns * line + ELLIPSIS_COUNT] = 0;
1038 return;
1039 }
1040
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001041 float ellipsisWidth = paint.measureText(
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07001042 (where == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00001043 TextUtils.ELLIPSIS_TWO_DOTS : TextUtils.ELLIPSIS_NORMAL, 0, 1);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001044 int ellipsisStart = 0;
1045 int ellipsisCount = 0;
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001046 int len = lineEnd - lineStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001048 // We only support start ellipsis on a single line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 if (where == TextUtils.TruncateAt.START) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001050 if (mMaximumVisibleLineCount == 1) {
1051 float sum = 0;
1052 int i;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053
Keisuke Kuroyanagied2eea12015-04-14 18:18:35 +09001054 for (i = len; i > 0; i--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001055 float w = widths[i - 1 + lineStart - widthStart];
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001056 if (w + sum + ellipsisWidth > avail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001057 while (i < len && widths[i + lineStart - widthStart] == 0.0f) {
1058 i++;
1059 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001060 break;
1061 }
1062
1063 sum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001066 ellipsisStart = 0;
1067 ellipsisCount = i;
1068 } else {
1069 if (Log.isLoggable(TAG, Log.WARN)) {
1070 Log.w(TAG, "Start Ellipsis only supported with one line");
1071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001073 } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE ||
1074 where == TextUtils.TruncateAt.END_SMALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 float sum = 0;
1076 int i;
1077
1078 for (i = 0; i < len; i++) {
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001079 float w = widths[i + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001081 if (w + sum + ellipsisWidth > avail) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 break;
1083 }
1084
1085 sum += w;
1086 }
1087
1088 ellipsisStart = i;
1089 ellipsisCount = len - i;
Fabrice Di Meglioaef455f2011-08-29 15:39:11 -07001090 if (forceEllipsis && ellipsisCount == 0 && len > 0) {
1091 ellipsisStart = len - 1;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001092 ellipsisCount = 1;
1093 }
1094 } else {
1095 // where = TextUtils.TruncateAt.MIDDLE We only support middle ellipsis on a single line
1096 if (mMaximumVisibleLineCount == 1) {
1097 float lsum = 0, rsum = 0;
1098 int left = 0, right = len;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001100 float ravail = (avail - ellipsisWidth) / 2;
Raph Levien0e3c5e82014-12-04 13:26:07 -08001101 for (right = len; right > 0; right--) {
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001102 float w = widths[right - 1 + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001104 if (w + rsum > ravail) {
Keisuke Kuroyanagi82d1c442016-09-14 14:30:14 +09001105 while (right < len && widths[right + lineStart - widthStart] == 0.0f) {
1106 right++;
1107 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001108 break;
1109 }
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001110 rsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001113 float lavail = avail - ellipsisWidth - rsum;
1114 for (left = 0; left < right; left++) {
1115 float w = widths[left + lineStart - widthStart];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001117 if (w + lsum > lavail) {
1118 break;
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001121 lsum += w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001124 ellipsisStart = left;
1125 ellipsisCount = right - left;
1126 } else {
1127 if (Log.isLoggable(TAG, Log.WARN)) {
1128 Log.w(TAG, "Middle Ellipsis only supported with one line");
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
Siyamed Sinir0745c722016-05-31 20:39:33 -07001132 mEllipsized = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart;
1134 mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount;
1135 }
1136
Doug Felte8e45f22010-03-29 14:58:40 -07001137 // Override the base class so we can directly access our members,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // rather than relying on member functions.
1139 // The logic mirrors that of Layout.getLineForVertical
1140 // FIXME: It may be faster to do a linear search for layouts without many lines.
Gilles Debunne66111472010-11-19 11:04:37 -08001141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 public int getLineForVertical(int vertical) {
1143 int high = mLineCount;
1144 int low = -1;
1145 int guess;
1146 int[] lines = mLines;
1147 while (high - low > 1) {
1148 guess = (high + low) >> 1;
1149 if (lines[mColumns * guess + TOP] > vertical){
1150 high = guess;
1151 } else {
1152 low = guess;
1153 }
1154 }
1155 if (low < 0) {
1156 return 0;
1157 } else {
1158 return low;
1159 }
1160 }
1161
Gilles Debunne66111472010-11-19 11:04:37 -08001162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 public int getLineCount() {
1164 return mLineCount;
1165 }
1166
Gilles Debunne66111472010-11-19 11:04:37 -08001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public int getLineTop(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001169 return mLines[mColumns * line + TOP];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171
Gilles Debunne66111472010-11-19 11:04:37 -08001172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public int getLineDescent(int line) {
Raph Levien07e6c232016-04-04 12:34:06 -07001174 return mLines[mColumns * line + DESCENT];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176
Gilles Debunne66111472010-11-19 11:04:37 -08001177 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 public int getLineStart(int line) {
1179 return mLines[mColumns * line + START] & START_MASK;
1180 }
1181
Gilles Debunne66111472010-11-19 11:04:37 -08001182 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 public int getParagraphDirection(int line) {
1184 return mLines[mColumns * line + DIR] >> DIR_SHIFT;
1185 }
1186
Gilles Debunne66111472010-11-19 11:04:37 -08001187 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 public boolean getLineContainsTab(int line) {
1189 return (mLines[mColumns * line + TAB] & TAB_MASK) != 0;
1190 }
1191
Gilles Debunne66111472010-11-19 11:04:37 -08001192 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public final Directions getLineDirections(int line) {
1194 return mLineDirections[line];
1195 }
1196
Gilles Debunne66111472010-11-19 11:04:37 -08001197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 public int getTopPadding() {
1199 return mTopPadding;
1200 }
1201
Gilles Debunne66111472010-11-19 11:04:37 -08001202 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public int getBottomPadding() {
1204 return mBottomPadding;
1205 }
1206
Raph Levien26d443a2015-03-30 14:18:32 -07001207 /**
1208 * @hide
1209 */
1210 @Override
1211 public int getHyphen(int line) {
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001212 return mLines[mColumns * line + HYPHEN] & HYPHEN_MASK;
Raph Levien26d443a2015-03-30 14:18:32 -07001213 }
1214
Raph Levien2ea52902015-07-01 14:39:31 -07001215 /**
1216 * @hide
1217 */
1218 @Override
1219 public int getIndentAdjust(int line, Alignment align) {
1220 if (align == Alignment.ALIGN_LEFT) {
1221 if (mLeftIndents == null) {
1222 return 0;
1223 } else {
1224 return mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1225 }
1226 } else if (align == Alignment.ALIGN_RIGHT) {
1227 if (mRightIndents == null) {
1228 return 0;
1229 } else {
1230 return -mRightIndents[Math.min(line, mRightIndents.length - 1)];
1231 }
1232 } else if (align == Alignment.ALIGN_CENTER) {
1233 int left = 0;
1234 if (mLeftIndents != null) {
1235 left = mLeftIndents[Math.min(line, mLeftIndents.length - 1)];
1236 }
1237 int right = 0;
1238 if (mRightIndents != null) {
1239 right = mRightIndents[Math.min(line, mRightIndents.length - 1)];
1240 }
1241 return (left - right) >> 1;
1242 } else {
1243 throw new AssertionError("unhandled alignment " + align);
1244 }
1245 }
1246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 @Override
1248 public int getEllipsisCount(int line) {
1249 if (mColumns < COLUMNS_ELLIPSIZE) {
1250 return 0;
1251 }
1252
1253 return mLines[mColumns * line + ELLIPSIS_COUNT];
1254 }
1255
1256 @Override
1257 public int getEllipsisStart(int line) {
1258 if (mColumns < COLUMNS_ELLIPSIZE) {
1259 return 0;
1260 }
1261
1262 return mLines[mColumns * line + ELLIPSIS_START];
1263 }
1264
1265 @Override
1266 public int getEllipsizedWidth() {
1267 return mEllipsizedWidth;
1268 }
1269
Siyamed Sinir0745c722016-05-31 20:39:33 -07001270 /**
1271 * Return the total height of this layout.
1272 *
1273 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
1274 *
1275 * @hide
1276 */
1277 public int getHeight(boolean cap) {
1278 if (cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight == -1 &&
1279 Log.isLoggable(TAG, Log.WARN)) {
1280 Log.w(TAG, "maxLineHeight should not be -1. "
1281 + " maxLines:" + mMaximumVisibleLineCount
1282 + " lineCount:" + mLineCount);
1283 }
1284
1285 return cap && mLineCount >= mMaximumVisibleLineCount && mMaxLineHeight != -1 ?
1286 mMaxLineHeight : super.getHeight();
1287 }
1288
Raph Levien70616ec2015-03-04 10:41:30 -08001289 private static native long nNewBuilder();
1290 private static native void nFreeBuilder(long nativePtr);
1291 private static native void nFinishBuilder(long nativePtr);
Raph Levien26d443a2015-03-30 14:18:32 -07001292
Raph Levien091dba22015-08-31 16:21:20 -07001293 /* package */ static native long nLoadHyphenator(ByteBuffer buf, int offset);
Raph Levien26d443a2015-03-30 14:18:32 -07001294
1295 private static native void nSetLocale(long nativePtr, String locale, long nativeHyphenator);
Raph Levien70616ec2015-03-04 10:41:30 -08001296
Raph Leviene319d5a2015-04-14 23:51:07 -07001297 private static native void nSetIndents(long nativePtr, int[] indents);
1298
Raph Levienc94f7422015-03-06 19:19:48 -08001299 // Set up paragraph text and settings; done as one big method to minimize jni crossings
1300 private static native void nSetupParagraph(long nativePtr, char[] text, int length,
1301 float firstWidth, int firstWidthLineCount, float restWidth,
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001302 int[] variableTabStops, int defaultTabStop, int breakStrategy, int hyphenationFrequency,
1303 boolean isJustified);
Raph Levien70616ec2015-03-04 10:41:30 -08001304
1305 private static native float nAddStyleRun(long nativePtr, long nativePaint,
1306 long nativeTypeface, int start, int end, boolean isRtl);
1307
1308 private static native void nAddMeasuredRun(long nativePtr,
1309 int start, int end, float[] widths);
1310
1311 private static native void nAddReplacementRun(long nativePtr, int start, int end, float width);
1312
1313 private static native void nGetWidths(long nativePtr, float[] widths);
1314
Anish Athalyec8f9e622014-07-21 15:26:34 -07001315 // populates LineBreaks and returns the number of breaks found
1316 //
1317 // the arrays inside the LineBreaks objects are passed in as well
1318 // to reduce the number of JNI calls in the common case where the
1319 // arrays do not have to be resized
Raph Levienc94f7422015-03-06 19:19:48 -08001320 private static native int nComputeLineBreaks(long nativePtr, LineBreaks recycle,
Raph Levien26d443a2015-03-30 14:18:32 -07001321 int[] recycleBreaks, float[] recycleWidths, int[] recycleFlags, int recycleLength);
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 private int mLineCount;
1324 private int mTopPadding, mBottomPadding;
1325 private int mColumns;
1326 private int mEllipsizedWidth;
1327
Siyamed Sinir0745c722016-05-31 20:39:33 -07001328 /**
1329 * Keeps track if ellipsize is applied to the text.
1330 */
1331 private boolean mEllipsized;
1332
1333 /**
1334 * If maxLines is set, ellipsize is not set, and the actual line count of text is greater than
1335 * or equal to maxLine, this variable holds the ideal visual height of the maxLine'th line
1336 * starting from the top of the layout. If maxLines is not set its value will be -1.
1337 *
1338 * The value is the same as getLineTop(maxLines) for ellipsized version where structurally no
1339 * more than maxLines is contained.
1340 */
1341 private int mMaxLineHeight = -1;
1342
Raph Levien26d443a2015-03-30 14:18:32 -07001343 private static final int COLUMNS_NORMAL = 4;
1344 private static final int COLUMNS_ELLIPSIZE = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 private static final int START = 0;
1346 private static final int DIR = START;
1347 private static final int TAB = START;
1348 private static final int TOP = 1;
1349 private static final int DESCENT = 2;
Raph Levien26d443a2015-03-30 14:18:32 -07001350 private static final int HYPHEN = 3;
1351 private static final int ELLIPSIS_START = 4;
1352 private static final int ELLIPSIS_COUNT = 5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353
1354 private int[] mLines;
1355 private Directions[] mLineDirections;
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001356 private int mMaximumVisibleLineCount = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357
1358 private static final int START_MASK = 0x1FFFFFFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 private static final int DIR_SHIFT = 30;
1360 private static final int TAB_MASK = 0x20000000;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001361 private static final int HYPHEN_MASK = 0xFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
Doug Feltc982f602010-05-25 11:51:40 -07001363 private static final int TAB_INCREMENT = 20; // same as Layout, but that's private
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001365 private static final char CHAR_NEW_LINE = '\n';
Fabrice Di Meglio121c82c2011-02-15 15:44:49 -08001366
1367 private static final double EXTRA_ROUNDING = 0.5;
Fabrice Di Megliocb332642011-09-23 19:08:04 -07001368
Anish Athalyec8f9e622014-07-21 15:26:34 -07001369 // This is used to return three arrays from a single JNI call when
1370 // performing line breaking
Deepanshu Gupta70539192014-10-15 15:57:40 -07001371 /*package*/ static class LineBreaks {
Anish Athalyec8f9e622014-07-21 15:26:34 -07001372 private static final int INITIAL_SIZE = 16;
1373 public int[] breaks = new int[INITIAL_SIZE];
1374 public float[] widths = new float[INITIAL_SIZE];
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001375 public int[] flags = new int[INITIAL_SIZE]; // hasTab
Anish Athalyec8f9e622014-07-21 15:26:34 -07001376 // breaks, widths, and flags should all have the same length
1377 }
1378
Raph Levien2ea52902015-07-01 14:39:31 -07001379 private int[] mLeftIndents;
1380 private int[] mRightIndents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381}