DynamicLayout.java revision 33b7de85b6918b7714641f12f1ba2ff03a344740
1/*
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
19import android.graphics.Paint;
20import android.text.style.UpdateLayout;
21import android.text.style.WrapTogetherSpan;
22
23import com.android.internal.util.ArrayUtils;
24
25import java.lang.ref.WeakReference;
26
27/**
28 * DynamicLayout is a text layout that updates itself as the text is edited.
29 * <p>This is used by widgets to control text layout. You should not need
30 * to use this class directly unless you are implementing your own widget
31 * or custom display object, or need to call
32 * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, float, float, android.graphics.Paint)
33 *  Canvas.drawText()} directly.</p>
34 */
35public class DynamicLayout extends Layout
36{
37    private static final int PRIORITY = 128;
38
39    /**
40     * Make a layout for the specified text that will be updated as
41     * the text is changed.
42     */
43    public DynamicLayout(CharSequence base,
44                         TextPaint paint,
45                         int width, Alignment align,
46                         float spacingmult, float spacingadd,
47                         boolean includepad) {
48        this(base, base, paint, width, align, spacingmult, spacingadd,
49             includepad);
50    }
51
52    /**
53     * Make a layout for the transformed text (password transformation
54     * being the primary example of a transformation)
55     * that will be updated as the base text is changed.
56     */
57    public DynamicLayout(CharSequence base, CharSequence display,
58                         TextPaint paint,
59                         int width, Alignment align,
60                         float spacingmult, float spacingadd,
61                         boolean includepad) {
62        this(base, display, paint, width, align, spacingmult, spacingadd,
63             includepad, null, 0);
64    }
65
66    /**
67     * Make a layout for the transformed text (password transformation
68     * being the primary example of a transformation)
69     * that will be updated as the base text is changed.
70     * If ellipsize is non-null, the Layout will ellipsize the text
71     * down to ellipsizedWidth.
72     */
73    public DynamicLayout(CharSequence base, CharSequence display,
74                         TextPaint paint,
75                         int width, Alignment align,
76                         float spacingmult, float spacingadd,
77                         boolean includepad,
78                         TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
79        this(base, display, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
80                spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth);
81    }
82
83    /**
84     * Make a layout for the transformed text (password transformation
85     * being the primary example of a transformation)
86     * that will be updated as the base text is changed.
87     * If ellipsize is non-null, the Layout will ellipsize the text
88     * down to ellipsizedWidth.
89     * *
90     * *@hide
91     */
92    public DynamicLayout(CharSequence base, CharSequence display,
93                         TextPaint paint,
94                         int width, Alignment align, TextDirectionHeuristic textDir,
95                         float spacingmult, float spacingadd,
96                         boolean includepad,
97                         TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
98        super((ellipsize == null)
99                ? display
100                : (display instanceof Spanned)
101                    ? new SpannedEllipsizer(display)
102                    : new Ellipsizer(display),
103              paint, width, align, textDir, spacingmult, spacingadd);
104
105        mBase = base;
106        mDisplay = display;
107
108        if (ellipsize != null) {
109            mInts = new PackedIntVector(COLUMNS_ELLIPSIZE);
110            mEllipsizedWidth = ellipsizedWidth;
111            mEllipsizeAt = ellipsize;
112        } else {
113            mInts = new PackedIntVector(COLUMNS_NORMAL);
114            mEllipsizedWidth = width;
115            mEllipsizeAt = null;
116        }
117
118        mObjects = new PackedObjectVector<Directions>(1);
119
120        mBlockEnds = new int[] { 0 };
121        mBlockIndices = new int[] { INVALID_BLOCK_INDEX };
122        mNumberOfBlocks = 1;
123
124        mIncludePad = includepad;
125
126        /*
127         * This is annoying, but we can't refer to the layout until
128         * superclass construction is finished, and the superclass
129         * constructor wants the reference to the display text.
130         *
131         * This will break if the superclass constructor ever actually
132         * cares about the content instead of just holding the reference.
133         */
134        if (ellipsize != null) {
135            Ellipsizer e = (Ellipsizer) getText();
136
137            e.mLayout = this;
138            e.mWidth = ellipsizedWidth;
139            e.mMethod = ellipsize;
140            mEllipsize = true;
141        }
142
143        // Initial state is a single line with 0 characters (0 to 0),
144        // with top at 0 and bottom at whatever is natural, and
145        // undefined ellipsis.
146
147        int[] start;
148
149        if (ellipsize != null) {
150            start = new int[COLUMNS_ELLIPSIZE];
151            start[ELLIPSIS_START] = ELLIPSIS_UNDEFINED;
152        } else {
153            start = new int[COLUMNS_NORMAL];
154        }
155
156        Directions[] dirs = new Directions[] { DIRS_ALL_LEFT_TO_RIGHT };
157
158        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
159        int asc = fm.ascent;
160        int desc = fm.descent;
161
162        start[DIR] = DIR_LEFT_TO_RIGHT << DIR_SHIFT;
163        start[TOP] = 0;
164        start[DESCENT] = desc;
165        mInts.insertAt(0, start);
166
167        start[TOP] = desc - asc;
168        mInts.insertAt(1, start);
169
170        mObjects.insertAt(0, dirs);
171
172        // Update from 0 characters to whatever the real text is
173
174        reflow(base, 0, 0, base.length());
175
176        if (base instanceof Spannable) {
177            if (mWatcher == null)
178                mWatcher = new ChangeWatcher(this);
179
180            // Strip out any watchers for other DynamicLayouts.
181            Spannable sp = (Spannable) base;
182            ChangeWatcher[] spans = sp.getSpans(0, sp.length(), ChangeWatcher.class);
183            for (int i = 0; i < spans.length; i++)
184                sp.removeSpan(spans[i]);
185
186            sp.setSpan(mWatcher, 0, base.length(),
187                       Spannable.SPAN_INCLUSIVE_INCLUSIVE |
188                       (PRIORITY << Spannable.SPAN_PRIORITY_SHIFT));
189        }
190    }
191
192    private void reflow(CharSequence s, int where, int before, int after) {
193        if (s != mBase)
194            return;
195
196        CharSequence text = mDisplay;
197        int len = text.length();
198
199        // seek back to the start of the paragraph
200
201        int find = TextUtils.lastIndexOf(text, '\n', where - 1);
202        if (find < 0)
203            find = 0;
204        else
205            find = find + 1;
206
207        {
208            int diff = where - find;
209            before += diff;
210            after += diff;
211            where -= diff;
212        }
213
214        // seek forward to the end of the paragraph
215
216        int look = TextUtils.indexOf(text, '\n', where + after);
217        if (look < 0)
218            look = len;
219        else
220            look++; // we want the index after the \n
221
222        int change = look - (where + after);
223        before += change;
224        after += change;
225
226        // seek further out to cover anything that is forced to wrap together
227
228        if (text instanceof Spanned) {
229            Spanned sp = (Spanned) text;
230            boolean again;
231
232            do {
233                again = false;
234
235                Object[] force = sp.getSpans(where, where + after,
236                                             WrapTogetherSpan.class);
237
238                for (int i = 0; i < force.length; i++) {
239                    int st = sp.getSpanStart(force[i]);
240                    int en = sp.getSpanEnd(force[i]);
241
242                    if (st < where) {
243                        again = true;
244
245                        int diff = where - st;
246                        before += diff;
247                        after += diff;
248                        where -= diff;
249                    }
250
251                    if (en > where + after) {
252                        again = true;
253
254                        int diff = en - (where + after);
255                        before += diff;
256                        after += diff;
257                    }
258                }
259            } while (again);
260        }
261
262        // find affected region of old layout
263
264        int startline = getLineForOffset(where);
265        int startv = getLineTop(startline);
266
267        int endline = getLineForOffset(where + before);
268        if (where + after == len)
269            endline = getLineCount();
270        int endv = getLineTop(endline);
271        boolean islast = (endline == getLineCount());
272
273        // generate new layout for affected text
274
275        StaticLayout reflowed;
276
277        synchronized (sLock) {
278            reflowed = sStaticLayout;
279            sStaticLayout = null;
280        }
281
282        if (reflowed == null) {
283            reflowed = new StaticLayout(null);
284        } else {
285            reflowed.prepare();
286        }
287
288        reflowed.generate(text, where, where + after,
289                getPaint(), getWidth(), getTextDirectionHeuristic(), getSpacingMultiplier(),
290                getSpacingAdd(), false,
291                true, mEllipsizedWidth, mEllipsizeAt);
292        int n = reflowed.getLineCount();
293
294        // If the new layout has a blank line at the end, but it is not
295        // the very end of the buffer, then we already have a line that
296        // starts there, so disregard the blank line.
297
298        if (where + after != len &&
299            reflowed.getLineStart(n - 1) == where + after)
300            n--;
301
302        // remove affected lines from old layout
303        mInts.deleteAt(startline, endline - startline);
304        mObjects.deleteAt(startline, endline - startline);
305        updateBlocks(startline, endline - 1, n);
306
307        // adjust offsets in layout for new height and offsets
308
309        int ht = reflowed.getLineTop(n);
310        int toppad = 0, botpad = 0;
311
312        if (mIncludePad && startline == 0) {
313            toppad = reflowed.getTopPadding();
314            mTopPadding = toppad;
315            ht -= toppad;
316        }
317        if (mIncludePad && islast) {
318            botpad = reflowed.getBottomPadding();
319            mBottomPadding = botpad;
320            ht += botpad;
321        }
322
323        mInts.adjustValuesBelow(startline, START, after - before);
324        mInts.adjustValuesBelow(startline, TOP, startv - endv + ht);
325
326        // insert new layout
327
328        int[] ints;
329
330        if (mEllipsize) {
331            ints = new int[COLUMNS_ELLIPSIZE];
332            ints[ELLIPSIS_START] = ELLIPSIS_UNDEFINED;
333        } else {
334            ints = new int[COLUMNS_NORMAL];
335        }
336
337        Directions[] objects = new Directions[1];
338
339        for (int i = 0; i < n; i++) {
340            ints[START] = reflowed.getLineStart(i) |
341                          (reflowed.getParagraphDirection(i) << DIR_SHIFT) |
342                          (reflowed.getLineContainsTab(i) ? TAB_MASK : 0);
343
344            int top = reflowed.getLineTop(i) + startv;
345            if (i > 0)
346                top -= toppad;
347            ints[TOP] = top;
348
349            int desc = reflowed.getLineDescent(i);
350            if (i == n - 1)
351                desc += botpad;
352
353            ints[DESCENT] = desc;
354            objects[0] = reflowed.getLineDirections(i);
355
356            if (mEllipsize) {
357                ints[ELLIPSIS_START] = reflowed.getEllipsisStart(i);
358                ints[ELLIPSIS_COUNT] = reflowed.getEllipsisCount(i);
359            }
360
361            mInts.insertAt(startline + i, ints);
362            mObjects.insertAt(startline + i, objects);
363        }
364
365        synchronized (sLock) {
366            sStaticLayout = reflowed;
367            reflowed.finish();
368        }
369    }
370
371    /**
372     * This method is called every time the layout is reflowed after an edition.
373     * It updates the internal block data structure. The text is split in blocks
374     * of contiguous lines, with at least one block for the entire text.
375     * When a range of lines is edited, new blocks (from 0 to 3 depending on the
376     * overlap structure) will replace the set of overlapping blocks.
377     * Blocks are listed in order and are represented by their ending line number.
378     * An index is associated to each block (which will be used by display lists),
379     * this class simply invalidates the index of blocks overlapping a modification.
380     *
381     * @param startLine the first line of the range of modified lines
382     * @param endLine the last line of the range, possibly equal to startLine, lower
383     * than getLineCount()
384     * @param newLineCount the number of lines that will replace the range, possibly 0
385     */
386    private void updateBlocks(int startLine, int endLine, int newLineCount) {
387        int firstBlock = -1;
388        int lastBlock = -1;
389        for (int i = 0; i < mNumberOfBlocks; i++) {
390            if (mBlockEnds[i] >= startLine) {
391                firstBlock = i;
392                break;
393            }
394        }
395        for (int i = firstBlock; i < mNumberOfBlocks; i++) {
396            if (mBlockEnds[i] >= endLine) {
397                lastBlock = i;
398                break;
399            }
400        }
401        final int lastBlockEndLine = mBlockEnds[lastBlock];
402
403        boolean createBlockBefore = startLine > (firstBlock == 0 ? 0 :
404                mBlockEnds[firstBlock - 1] + 1);
405        boolean createBlock = newLineCount > 0;
406        boolean createBlockAfter = endLine < mBlockEnds[lastBlock];
407
408        int numAddedBlocks = 0;
409        if (createBlockBefore) numAddedBlocks++;
410        if (createBlock) numAddedBlocks++;
411        if (createBlockAfter) numAddedBlocks++;
412
413        final int numRemovedBlocks = lastBlock - firstBlock + 1;
414        final int newNumberOfBlocks = mNumberOfBlocks + numAddedBlocks - numRemovedBlocks;
415
416        if (newNumberOfBlocks == 0) {
417            // Even when text is empty, there is actually one line and hence one block
418            mBlockEnds[0] = 0;
419            mBlockIndices[0] = INVALID_BLOCK_INDEX;
420            mNumberOfBlocks = 1;
421            return;
422        }
423
424        if (newNumberOfBlocks > mBlockEnds.length) {
425            final int newSize = ArrayUtils.idealIntArraySize(newNumberOfBlocks);
426            int[] blockEnds = new int[newSize];
427            int[] blockIndices = new int[newSize];
428            System.arraycopy(mBlockEnds, 0, blockEnds, 0, firstBlock);
429            System.arraycopy(mBlockIndices, 0, blockIndices, 0, firstBlock);
430            System.arraycopy(mBlockEnds, lastBlock + 1,
431                    blockEnds, firstBlock + numAddedBlocks, mNumberOfBlocks - lastBlock - 1);
432            System.arraycopy(mBlockIndices, lastBlock + 1,
433                    blockIndices, firstBlock + numAddedBlocks, mNumberOfBlocks - lastBlock - 1);
434            mBlockEnds = blockEnds;
435            mBlockIndices = blockIndices;
436        } else {
437            System.arraycopy(mBlockEnds, lastBlock + 1,
438                    mBlockEnds, firstBlock + numAddedBlocks, mNumberOfBlocks - lastBlock - 1);
439            System.arraycopy(mBlockIndices, lastBlock + 1,
440                    mBlockIndices, firstBlock + numAddedBlocks, mNumberOfBlocks - lastBlock - 1);
441        }
442
443        mNumberOfBlocks = newNumberOfBlocks;
444        final int deltaLines = newLineCount - (endLine - startLine + 1);
445        for (int i = firstBlock + numAddedBlocks; i < mNumberOfBlocks; i++) {
446            mBlockEnds[i] += deltaLines;
447        }
448
449        int blockIndex = firstBlock;
450        if (createBlockBefore) {
451            mBlockEnds[blockIndex] = startLine - 1;
452            mBlockIndices[blockIndex] = INVALID_BLOCK_INDEX;
453            blockIndex++;
454        }
455
456        if (createBlock) {
457            mBlockEnds[blockIndex] = startLine + newLineCount - 1;
458            mBlockIndices[blockIndex] = INVALID_BLOCK_INDEX;
459            blockIndex++;
460        }
461
462        if (createBlockAfter) {
463            mBlockEnds[blockIndex] = lastBlockEndLine + deltaLines;
464            mBlockIndices[blockIndex] = INVALID_BLOCK_INDEX;
465        }
466    }
467
468    /**
469     * @hide
470     */
471    public int[] getBlockEnds() {
472        return mBlockEnds;
473    }
474
475    /**
476     * @hide
477     */
478    public int[] getBlockIndices() {
479        return mBlockIndices;
480    }
481
482    /**
483     * @hide
484     */
485    public int getNumberOfBlocks() {
486        return mNumberOfBlocks;
487    }
488
489    @Override
490    public int getLineCount() {
491        return mInts.size() - 1;
492    }
493
494    @Override
495    public int getLineTop(int line) {
496        return mInts.getValue(line, TOP);
497    }
498
499    @Override
500    public int getLineDescent(int line) {
501        return mInts.getValue(line, DESCENT);
502    }
503
504    @Override
505    public int getLineStart(int line) {
506        return mInts.getValue(line, START) & START_MASK;
507    }
508
509    @Override
510    public boolean getLineContainsTab(int line) {
511        return (mInts.getValue(line, TAB) & TAB_MASK) != 0;
512    }
513
514    @Override
515    public int getParagraphDirection(int line) {
516        return mInts.getValue(line, DIR) >> DIR_SHIFT;
517    }
518
519    @Override
520    public final Directions getLineDirections(int line) {
521        return mObjects.getValue(line, 0);
522    }
523
524    @Override
525    public int getTopPadding() {
526        return mTopPadding;
527    }
528
529    @Override
530    public int getBottomPadding() {
531        return mBottomPadding;
532    }
533
534    @Override
535    public int getEllipsizedWidth() {
536        return mEllipsizedWidth;
537    }
538
539    private static class ChangeWatcher implements TextWatcher, SpanWatcher {
540        public ChangeWatcher(DynamicLayout layout) {
541            mLayout = new WeakReference<DynamicLayout>(layout);
542        }
543
544        private void reflow(CharSequence s, int where, int before, int after) {
545            DynamicLayout ml = mLayout.get();
546
547            if (ml != null)
548                ml.reflow(s, where, before, after);
549            else if (s instanceof Spannable)
550                ((Spannable) s).removeSpan(this);
551        }
552
553        public void beforeTextChanged(CharSequence s, int where, int before, int after) {
554            // Intentionally empty
555        }
556
557        public void onTextChanged(CharSequence s, int where, int before, int after) {
558            reflow(s, where, before, after);
559        }
560
561        public void afterTextChanged(Editable s) {
562            // Intentionally empty
563        }
564
565        public void onSpanAdded(Spannable s, Object o, int start, int end) {
566            if (o instanceof UpdateLayout)
567                reflow(s, start, end - start, end - start);
568        }
569
570        public void onSpanRemoved(Spannable s, Object o, int start, int end) {
571            if (o instanceof UpdateLayout)
572                reflow(s, start, end - start, end - start);
573        }
574
575        public void onSpanChanged(Spannable s, Object o, int start, int end, int nstart, int nend) {
576            if (o instanceof UpdateLayout) {
577                reflow(s, start, end - start, end - start);
578                reflow(s, nstart, nend - nstart, nend - nstart);
579            }
580        }
581
582        private WeakReference<DynamicLayout> mLayout;
583    }
584
585    @Override
586    public int getEllipsisStart(int line) {
587        if (mEllipsizeAt == null) {
588            return 0;
589        }
590
591        return mInts.getValue(line, ELLIPSIS_START);
592    }
593
594    @Override
595    public int getEllipsisCount(int line) {
596        if (mEllipsizeAt == null) {
597            return 0;
598        }
599
600        return mInts.getValue(line, ELLIPSIS_COUNT);
601    }
602
603    private CharSequence mBase;
604    private CharSequence mDisplay;
605    private ChangeWatcher mWatcher;
606    private boolean mIncludePad;
607    private boolean mEllipsize;
608    private int mEllipsizedWidth;
609    private TextUtils.TruncateAt mEllipsizeAt;
610
611    private PackedIntVector mInts;
612    private PackedObjectVector<Directions> mObjects;
613
614    /*
615     * Value used in mBlockIndices when a block has been created or recycled and indicating that its
616     * display list needs to be re-created.
617     * @hide
618     */
619    public static final int INVALID_BLOCK_INDEX = -1;
620    // Stores the line numbers of the last line of each block
621    private int[] mBlockEnds;
622    // The indices of this block's display list in TextView's internal display list array or
623    // INVALID_BLOCK_INDEX if this block has been invalidated during an edition
624    private int[] mBlockIndices;
625    // Number of items actually currently being used in the above 2 arrays
626    private int mNumberOfBlocks;
627
628    private int mTopPadding, mBottomPadding;
629
630    private static StaticLayout sStaticLayout = new StaticLayout(null);
631
632    private static final Object[] sLock = new Object[0];
633
634    private static final int START = 0;
635    private static final int DIR = START;
636    private static final int TAB = START;
637    private static final int TOP = 1;
638    private static final int DESCENT = 2;
639    private static final int COLUMNS_NORMAL = 3;
640
641    private static final int ELLIPSIS_START = 3;
642    private static final int ELLIPSIS_COUNT = 4;
643    private static final int COLUMNS_ELLIPSIZE = 5;
644
645    private static final int START_MASK = 0x1FFFFFFF;
646    private static final int DIR_SHIFT  = 30;
647    private static final int TAB_MASK   = 0x20000000;
648
649    private static final int ELLIPSIS_UNDEFINED = 0x80000000;
650}
651