1e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt/* 2e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Copyright (C) 2010 The Android Open Source Project 3e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 4e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Licensed under the Apache License, Version 2.0 (the "License"); 5e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * you may not use this file except in compliance with the License. 6e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * You may obtain a copy of the License at 7e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 8e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * http://www.apache.org/licenses/LICENSE-2.0 9e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 10e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Unless required by applicable law or agreed to in writing, software 11e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * distributed under the License is distributed on an "AS IS" BASIS, 12e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * See the License for the specific language governing permissions and 14e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * limitations under the License. 15e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 16e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 17e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltpackage android.text; 18e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 19e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.graphics.Bitmap; 20e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.graphics.Canvas; 21e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.graphics.Paint; 22e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.graphics.Paint.FontMetricsInt; 23cc3ec6cdb2b892eb29513e72d8b205acbe997b25Gilles Debunneimport android.graphics.RectF; 24e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.text.Layout.Directions; 25c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Feltimport android.text.Layout.TabStops; 26e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.text.style.CharacterStyle; 27e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.text.style.MetricAffectingSpan; 28e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.text.style.ReplacementSpan; 29e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltimport android.util.Log; 30e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 31dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunneimport com.android.internal.util.ArrayUtils; 32dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 33e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt/** 34e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Represents a line of styled text, for measuring in visual order and 35e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * for rendering. 36e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 37e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * <p>Get a new instance using obtain(), and when finished with it, return it 38e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * to the pool using recycle(). 39e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 40e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * <p>Call set to prepare the instance for use, then either draw, measure, 41e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * metrics, or caretToLeftRightOf. 42e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 43e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @hide 44e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 45e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Feltclass TextLine { 46bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy private static final boolean DEBUG = false; 47bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy 48e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private TextPaint mPaint; 49e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private CharSequence mText; 50e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private int mStart; 51e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private int mLen; 52e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private int mDir; 53e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private Directions mDirections; 54e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private boolean mHasTabs; 55c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt private TabStops mTabs; 56e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private char[] mChars; 57e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private boolean mCharsValid; 58e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private Spanned mSpanned; 59345cb03315a0813ec57e44f97fc3fa4af6b3c309Gilles Debunne private final TextPaint mWorkPaint = new TextPaint(); 60c1f44830809f0a8526855f13822702ea756214faGilles Debunne private final SpanSet<MetricAffectingSpan> mMetricAffectingSpanSpanSet = 61c1f44830809f0a8526855f13822702ea756214faGilles Debunne new SpanSet<MetricAffectingSpan>(MetricAffectingSpan.class); 62c1f44830809f0a8526855f13822702ea756214faGilles Debunne private final SpanSet<CharacterStyle> mCharacterStyleSpanSet = 63c1f44830809f0a8526855f13822702ea756214faGilles Debunne new SpanSet<CharacterStyle>(CharacterStyle.class); 64c1f44830809f0a8526855f13822702ea756214faGilles Debunne private final SpanSet<ReplacementSpan> mReplacementSpanSpanSet = 65c1f44830809f0a8526855f13822702ea756214faGilles Debunne new SpanSet<ReplacementSpan>(ReplacementSpan.class); 66e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 67bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy private static final TextLine[] sCached = new TextLine[3]; 68e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 69e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 70e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Returns a new TextLine from the shared pool. 71e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 72e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return an uninitialized TextLine 73e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 74e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt static TextLine obtain() { 75e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt TextLine tl; 76bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy synchronized (sCached) { 77bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy for (int i = sCached.length; --i >= 0;) { 78bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy if (sCached[i] != null) { 79bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy tl = sCached[i]; 80bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy sCached[i] = null; 81e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return tl; 82e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 83e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 84e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 85e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt tl = new TextLine(); 86bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy if (DEBUG) { 87bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy Log.v("TLINE", "new: " + tl); 88bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy } 89e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return tl; 90e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 91e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 92e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 93e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Puts a TextLine back into the shared pool. Do not use this TextLine once 94e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * it has been returned. 95e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param tl the textLine 96e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return null, as a convenience from clearing references to the provided 97e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * TextLine 98e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 99e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt static TextLine recycle(TextLine tl) { 100e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt tl.mText = null; 101e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt tl.mPaint = null; 102e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt tl.mDirections = null; 103893d6fe48d37f71e683f722457bea646994a10bfSvet Ganov tl.mSpanned = null; 104893d6fe48d37f71e683f722457bea646994a10bfSvet Ganov tl.mTabs = null; 105893d6fe48d37f71e683f722457bea646994a10bfSvet Ganov tl.mChars = null; 106c3fb7a11ad72c5e51ff93e1ad6958f843af0d5b1Gilles Debunne 107c3fb7a11ad72c5e51ff93e1ad6958f843af0d5b1Gilles Debunne tl.mMetricAffectingSpanSpanSet.recycle(); 108c3fb7a11ad72c5e51ff93e1ad6958f843af0d5b1Gilles Debunne tl.mCharacterStyleSpanSet.recycle(); 109c3fb7a11ad72c5e51ff93e1ad6958f843af0d5b1Gilles Debunne tl.mReplacementSpanSpanSet.recycle(); 110c3fb7a11ad72c5e51ff93e1ad6958f843af0d5b1Gilles Debunne 111bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy synchronized(sCached) { 112bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy for (int i = 0; i < sCached.length; ++i) { 113bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy if (sCached[i] == null) { 114bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy sCached[i] = tl; 115f902d7bc49797ec277b4576c921dfffa15d741ddGilles Debunne break; 116e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 117e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 118e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 119e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return null; 120e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 121e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 122e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 123e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Initializes a TextLine and prepares it for use. 124e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 125e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param paint the base paint for the line 126e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param text the text, can be Styled 127e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the start of the line relative to the text 128e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param limit the limit of the line relative to the text 129e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param dir the paragraph direction of this line 130e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param directions the directions information of this line 131112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader * @param hasTabs true if the line might contain tabs 132c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt * @param tabStops the tabStops. Can be null. 133e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 134e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt void set(TextPaint paint, CharSequence text, int start, int limit, int dir, 135c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt Directions directions, boolean hasTabs, TabStops tabStops) { 136e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mPaint = paint; 137e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mText = text; 138e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mStart = start; 139e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mLen = limit - start; 140e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mDir = dir; 141e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mDirections = directions; 1428059e0903e36cbb5cf8b5c5d5d653acc9bbc8402Fabrice Di Meglio if (mDirections == null) { 1438059e0903e36cbb5cf8b5c5d5d653acc9bbc8402Fabrice Di Meglio throw new IllegalArgumentException("Directions cannot be null"); 1448059e0903e36cbb5cf8b5c5d5d653acc9bbc8402Fabrice Di Meglio } 145e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mHasTabs = hasTabs; 146e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mSpanned = null; 147e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 148e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean hasReplacement = false; 149e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (text instanceof Spanned) { 150e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt mSpanned = (Spanned) text; 151c1f44830809f0a8526855f13822702ea756214faGilles Debunne mReplacementSpanSpanSet.init(mSpanned, start, limit); 152c1f44830809f0a8526855f13822702ea756214faGilles Debunne hasReplacement = mReplacementSpanSpanSet.numberOfSpans > 0; 153e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 154e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 1551e3ac18e7ad03e02819f3e1a89d6a80a2bb7645fGilles Debunne mCharsValid = hasReplacement || hasTabs || directions != Layout.DIRS_ALL_LEFT_TO_RIGHT; 156e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 157e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mCharsValid) { 158e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mChars == null || mChars.length < mLen) { 159776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mChars = ArrayUtils.newUnpaddedCharArray(mLen); 160e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 161e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt TextUtils.getChars(text, start, limit, mChars, 0); 1620c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (hasReplacement) { 1630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // Handle these all at once so we don't have to do it as we go. 1640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // Replace the first character of each replacement run with the 1650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // object-replacement character and the remainder with zero width 1660c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // non-break space aka BOM. Cursor movement code skips these 1670c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // zero-width characters. 1680c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt char[] chars = mChars; 1690c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt for (int i = start, inext; i < limit; i = inext) { 170c1f44830809f0a8526855f13822702ea756214faGilles Debunne inext = mReplacementSpanSpanSet.getNextTransition(i, limit); 171c1f44830809f0a8526855f13822702ea756214faGilles Debunne if (mReplacementSpanSpanSet.hasSpansIntersecting(i, inext)) { 1721e3ac18e7ad03e02819f3e1a89d6a80a2bb7645fGilles Debunne // transition into a span 1730c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt chars[i - start] = '\ufffc'; 1740c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt for (int j = i - start + 1, e = inext - start; j < e; ++j) { 1750c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt chars[j] = '\ufeff'; // used as ZWNBS, marks positions to skip 1760c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 1770c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 1780c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 1790c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 180e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 181c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt mTabs = tabStops; 182e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 183e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 184e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 185e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Renders the TextLine. 186e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 187e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas to render on 188e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the leading margin position 189e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param top the top of the line 190e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline 191e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param bottom the bottom of the line 192e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 193e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt void draw(Canvas c, float x, int top, int y, int bottom) { 194e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (!mHasTabs) { 195e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mDirections == Layout.DIRS_ALL_LEFT_TO_RIGHT) { 196bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy drawRun(c, 0, mLen, false, x, top, y, bottom, false); 197e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return; 198e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 199e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mDirections == Layout.DIRS_ALL_RIGHT_TO_LEFT) { 200bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy drawRun(c, 0, mLen, true, x, top, y, bottom, false); 201e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return; 202e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 203e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 204e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 205e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float h = 0; 206e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int[] runs = mDirections.mDirections; 207e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 208e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int lastRunIndex = runs.length - 2; 209e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (int i = 0; i < runs.length; i += 2) { 210e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int runStart = runs[i]; 211e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int runLimit = runStart + (runs[i+1] & Layout.RUN_LENGTH_MASK); 212e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (runLimit > mLen) { 213e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLimit = mLen; 214e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 215e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean runIsRtl = (runs[i+1] & Layout.RUN_RTL_FLAG) != 0; 216e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 217e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int segstart = runStart; 218e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; j++) { 219e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int codept = 0; 220e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mHasTabs && j < runLimit) { 221e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt codept = mChars[j]; 222112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (codept >= 0xD800 && codept < 0xDC00 && j + 1 < runLimit) { 223e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt codept = Character.codePointAt(mChars, j); 224112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (codept > 0xFFFF) { 225e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt ++j; 226e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt continue; 227e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 228e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 229e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 230e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 231112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (j == runLimit || codept == '\t') { 232bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy h += drawRun(c, segstart, j, runIsRtl, x+h, top, y, bottom, 233e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt i != lastRunIndex || j != mLen); 234e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 235e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (codept == '\t') { 236e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt h = mDir * nextTab(h * mDir); 237e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 238e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt segstart = j + 1; 239e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 240e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 241e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 242e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 243e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 244e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 245e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Returns metrics information for the entire line. 246e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 247e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives font metrics information, can be null 248e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width of the line 249e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 250e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float metrics(FontMetricsInt fmi) { 251e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return measure(mLen, false, fmi); 252e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 253e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 254e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 255e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Returns information about a position on the line. 256e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 257e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param offset the line-relative character offset, between 0 and the 258e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * line length, inclusive 259e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param trailing true to measure the trailing edge of the character 260e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * before offset, false to measure the leading edge of the character 261e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * at offset. 262e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives metrics information about the requested 263e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * character, can be null. 264e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed offset from the leading margin to the requested 265e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * character edge. 266e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 267e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float measure(int offset, boolean trailing, FontMetricsInt fmi) { 268e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int target = trailing ? offset - 1 : offset; 269e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (target < 0) { 270e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return 0; 271e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 272e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 273e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float h = 0; 274e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 275e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (!mHasTabs) { 276e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mDirections == Layout.DIRS_ALL_LEFT_TO_RIGHT) { 277bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return measureRun(0, offset, mLen, false, fmi); 278e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 279e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mDirections == Layout.DIRS_ALL_RIGHT_TO_LEFT) { 280bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return measureRun(0, offset, mLen, true, fmi); 281e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 282e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 283e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 284e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt char[] chars = mChars; 285e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int[] runs = mDirections.mDirections; 286e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (int i = 0; i < runs.length; i += 2) { 287e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int runStart = runs[i]; 288e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int runLimit = runStart + (runs[i+1] & Layout.RUN_LENGTH_MASK); 289e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (runLimit > mLen) { 290e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLimit = mLen; 291e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 292e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean runIsRtl = (runs[i+1] & Layout.RUN_RTL_FLAG) != 0; 293e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 294e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int segstart = runStart; 295e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; j++) { 296e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int codept = 0; 297e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mHasTabs && j < runLimit) { 298e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt codept = chars[j]; 299112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (codept >= 0xD800 && codept < 0xDC00 && j + 1 < runLimit) { 300e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt codept = Character.codePointAt(chars, j); 301112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (codept > 0xFFFF) { 302e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt ++j; 303e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt continue; 304e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 305e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 306e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 307e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 308112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader if (j == runLimit || codept == '\t') { 309e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean inSegment = target >= segstart && target < j; 310e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 311e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean advance = (mDir == Layout.DIR_RIGHT_TO_LEFT) == runIsRtl; 312e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (inSegment && advance) { 313bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return h += measureRun(segstart, offset, j, runIsRtl, fmi); 314e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 315e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 316bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy float w = measureRun(segstart, j, j, runIsRtl, fmi); 317e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt h += advance ? w : -w; 318e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 319e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (inSegment) { 320bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return h += measureRun(segstart, offset, j, runIsRtl, null); 321e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 322e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 323e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (codept == '\t') { 324e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (offset == j) { 325e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return h; 326e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 327e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt h = mDir * nextTab(h * mDir); 328e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (target == j) { 329e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return h; 330e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 331e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 332e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 333e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt segstart = j + 1; 334e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 335e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 336e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 337e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 338e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return h; 339e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 340e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 341e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 342e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Draws a unidirectional (but possibly multi-styled) run of text. 343e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 344bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy * 345e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas to draw on 346e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the line-relative start 347e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param limit the line-relative limit 348e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIsRtl true if the run is right-to-left 349e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the position of the run that is closest to the leading margin 350e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param top the top of the line 351e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline 352e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param bottom the bottom of the line 353e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param needWidth true if the width value is required. 354e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width of the run, based on the paragraph direction. 355e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Only valid if needWidth is true. 356e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 357bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy private float drawRun(Canvas c, int start, 358e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int limit, boolean runIsRtl, float x, int top, int y, int bottom, 359e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean needWidth) { 360e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 361e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if ((mDir == Layout.DIR_LEFT_TO_RIGHT) == runIsRtl) { 362bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy float w = -measureRun(start, limit, limit, runIsRtl, null); 363bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy handleRun(start, limit, limit, runIsRtl, c, x + w, top, 3640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt y, bottom, null, false); 365e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return w; 366e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 367e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 368bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return handleRun(start, limit, limit, runIsRtl, c, x, top, 3690c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt y, bottom, null, needWidth); 370e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 371e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 372e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 373e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Measures a unidirectional (but possibly multi-styled) run of text. 374e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 375bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy * 376e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the line-relative start of the run 377e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param offset the offset to measure to, between start and limit inclusive 378e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param limit the line-relative limit of the run 379e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIsRtl true if the run is right-to-left 380e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives metrics information about the requested 381e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * run, can be null. 382e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width from the start of the run to the leading edge 383e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * of the character at offset, based on the run (not paragraph) direction 384e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 385bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy private float measureRun(int start, int offset, int limit, boolean runIsRtl, 386bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy FontMetricsInt fmi) { 387bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy return handleRun(start, offset, limit, runIsRtl, null, 0, 0, 0, 0, fmi, true); 388e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 389e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 390e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 391e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Walk the cursor through this line, skipping conjuncts and 392e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * zero-width characters. 393e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 394e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * <p>This function cannot properly walk the cursor off the ends of the line 395e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * since it does not know about any shaping on the previous/following line 396e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * that might affect the cursor position. Callers must either avoid these 397e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * situations or handle the result specially. 398e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 399e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param cursor the starting position of the cursor, between 0 and the 400e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * length of the line, inclusive 401e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param toLeft true if the caret is moving to the left. 402e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the new offset. If it is less than 0 or greater than the length 403e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * of the line, the previous/following line should be examined to get the 404e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * actual offset. 405e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 406e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int getOffsetToLeftRightOf(int cursor, boolean toLeft) { 407e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // 1) The caret marks the leading edge of a character. The character 408e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // logically before it might be on a different level, and the active caret 409e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // position is on the character at the lower level. If that character 410e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // was the previous character, the caret is on its trailing edge. 411e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // 2) Take this character/edge and move it in the indicated direction. 412e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // This gives you a new character and a new edge. 413e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // 3) This position is between two visually adjacent characters. One of 414e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // these might be at a lower level. The active position is on the 415e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // character at the lower level. 416e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // 4) If the active position is on the trailing edge of the character, 417e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // the new caret position is the following logical character, else it 418e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // is the character. 419e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 420e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int lineStart = 0; 421e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int lineEnd = mLen; 422e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean paraIsRtl = mDir == -1; 423e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int[] runs = mDirections.mDirections; 424e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 425e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int runIndex, runLevel = 0, runStart = lineStart, runLimit = lineEnd, newCaret = -1; 426e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean trailing = false; 427e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 428e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (cursor == lineStart) { 429e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runIndex = -2; 430e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } else if (cursor == lineEnd) { 431e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runIndex = runs.length; 432e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } else { 433e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // First, get information about the run containing the character with 434e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // the active caret. 435e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (runIndex = 0; runIndex < runs.length; runIndex += 2) { 436e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runStart = lineStart + runs[runIndex]; 437e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (cursor >= runStart) { 438e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLimit = runStart + (runs[runIndex+1] & Layout.RUN_LENGTH_MASK); 439e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (runLimit > lineEnd) { 440e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLimit = lineEnd; 441e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 442e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (cursor < runLimit) { 443e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLevel = (runs[runIndex+1] >>> Layout.RUN_LEVEL_SHIFT) & 444e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt Layout.RUN_LEVEL_MASK; 445e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (cursor == runStart) { 446e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // The caret is on a run boundary, see if we should 447e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // use the position on the trailing edge of the previous 448e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // logical character instead. 449e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int prevRunIndex, prevRunLevel, prevRunStart, prevRunLimit; 450e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int pos = cursor - 1; 451e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt for (prevRunIndex = 0; prevRunIndex < runs.length; prevRunIndex += 2) { 452e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt prevRunStart = lineStart + runs[prevRunIndex]; 453e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (pos >= prevRunStart) { 454e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt prevRunLimit = prevRunStart + 455e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt (runs[prevRunIndex+1] & Layout.RUN_LENGTH_MASK); 456e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (prevRunLimit > lineEnd) { 457e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt prevRunLimit = lineEnd; 458e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 459e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (pos < prevRunLimit) { 460e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt prevRunLevel = (runs[prevRunIndex+1] >>> Layout.RUN_LEVEL_SHIFT) 461e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt & Layout.RUN_LEVEL_MASK; 462e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (prevRunLevel < runLevel) { 463e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Start from logically previous character. 464e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runIndex = prevRunIndex; 465e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLevel = prevRunLevel; 466e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runStart = prevRunStart; 467e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLimit = prevRunLimit; 468e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt trailing = true; 469e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 470e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 471e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 472e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 473e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 474e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 475e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 476e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 477e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 478e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 479e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 480e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // caret might be == lineEnd. This is generally a space or paragraph 481e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // separator and has an associated run, but might be the end of 482e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // text, in which case it doesn't. If that happens, we ran off the 483e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // end of the run list, and runIndex == runs.length. In this case, 484e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // we are at a run boundary so we skip the below test. 485e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (runIndex != runs.length) { 486e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean runIsRtl = (runLevel & 0x1) != 0; 487e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean advance = toLeft == runIsRtl; 488e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (cursor != (advance ? runLimit : runStart) || advance != trailing) { 489e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Moving within or into the run, so we can move logically. 4900c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt newCaret = getOffsetBeforeAfter(runIndex, runStart, runLimit, 4910c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt runIsRtl, cursor, advance); 492e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // If the new position is internal to the run, we're at the strong 493e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // position already so we're finished. 494e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (newCaret != (advance ? runLimit : runStart)) { 495e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return newCaret; 496e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 497e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 498e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 499e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 500e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 501e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // If newCaret is -1, we're starting at a run boundary and crossing 502e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // into another run. Otherwise we've arrived at a run boundary, and 503e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // need to figure out which character to attach to. Note we might 504e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // need to run this twice, if we cross a run boundary and end up at 505e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // another run boundary. 506e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt while (true) { 507e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean advance = toLeft == paraIsRtl; 508e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int otherRunIndex = runIndex + (advance ? 2 : -2); 509e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (otherRunIndex >= 0 && otherRunIndex < runs.length) { 510e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int otherRunStart = lineStart + runs[otherRunIndex]; 511e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int otherRunLimit = otherRunStart + 512e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt (runs[otherRunIndex+1] & Layout.RUN_LENGTH_MASK); 513e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (otherRunLimit > lineEnd) { 514e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt otherRunLimit = lineEnd; 515e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 516e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int otherRunLevel = (runs[otherRunIndex+1] >>> Layout.RUN_LEVEL_SHIFT) & 517e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt Layout.RUN_LEVEL_MASK; 518e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt boolean otherRunIsRtl = (otherRunLevel & 1) != 0; 519e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 520e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt advance = toLeft == otherRunIsRtl; 521e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (newCaret == -1) { 5220c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt newCaret = getOffsetBeforeAfter(otherRunIndex, otherRunStart, 5230c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt otherRunLimit, otherRunIsRtl, 524e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt advance ? otherRunStart : otherRunLimit, advance); 525e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (newCaret == (advance ? otherRunLimit : otherRunStart)) { 526e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Crossed and ended up at a new boundary, 527e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // repeat a second and final time. 528e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runIndex = otherRunIndex; 529e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt runLevel = otherRunLevel; 530e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt continue; 531e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 532e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 533e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 534e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 535e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // The new caret is at a boundary. 536e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (otherRunLevel < runLevel) { 537e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // The strong character is in the other run. 538e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt newCaret = advance ? otherRunStart : otherRunLimit; 539e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 540e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 541e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 542e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 543e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (newCaret == -1) { 544e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // We're walking off the end of the line. The paragraph 545e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // level is always equal to or lower than any internal level, so 546e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // the boundaries get the strong caret. 5470c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt newCaret = advance ? mLen + 1 : -1; 548e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 549e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 550e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 551e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Else we've arrived at the end of the line. That's a strong position. 552e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // We might have arrived here by crossing over a run with no internal 553e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // breaks and dropping out of the above loop before advancing one final 554e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // time, so reset the caret. 555e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Note, we use '<=' below to handle a situation where the only run 556e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // on the line is a counter-directional run. If we're not advancing, 557e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // we can end up at the 'lineEnd' position but the caret we want is at 558e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // the lineStart. 559e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (newCaret <= lineEnd) { 560e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt newCaret = advance ? lineEnd : lineStart; 561e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 562e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt break; 563e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 564e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 565e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return newCaret; 566e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 567e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 568e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 569e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Returns the next valid offset within this directional run, skipping 570e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * conjuncts and zero-width characters. This should not be called to walk 571cc3ec6cdb2b892eb29513e72d8b205acbe997b25Gilles Debunne * off the end of the line, since the returned values might not be valid 5720c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * on neighboring lines. If the returned offset is less than zero or 5730c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * greater than the line length, the offset should be recomputed on the 5740c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * preceding or following line, respectively. 575e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 576e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIndex the run index 5770c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param runStart the start of the run 5780c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param runLimit the limit of the run 5790c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param runIsRtl true if the run is right-to-left 580e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param offset the offset 581e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param after true if the new offset should logically follow the provided 582e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * offset 583e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the new offset 584e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 5850c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt private int getOffsetBeforeAfter(int runIndex, int runStart, int runLimit, 5860c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt boolean runIsRtl, int offset, boolean after) { 587e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 5880c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (runIndex < 0 || offset == (after ? mLen : 0)) { 5890c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // Walking off end of line. Since we don't know 5900c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // what cursor positions are available on other lines, we can't 5910c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // return accurate values. These are a guess. 592e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (after) { 5930c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt return TextUtils.getOffsetAfter(mText, offset + mStart) - mStart; 5940c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 5950c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt return TextUtils.getOffsetBefore(mText, offset + mStart) - mStart; 5960c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 5970c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 5980c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt TextPaint wp = mWorkPaint; 5990c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt wp.set(mPaint); 6000c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 6010c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int spanStart = runStart; 6020c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int spanLimit; 6030c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (mSpanned == null) { 6040c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt spanLimit = runLimit; 6050c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } else { 6060c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int target = after ? offset + 1 : offset; 6070c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int limit = mStart + runLimit; 6080c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt while (true) { 6090c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt spanLimit = mSpanned.nextSpanTransition(mStart + spanStart, limit, 6100c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt MetricAffectingSpan.class) - mStart; 6110c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (spanLimit >= target) { 6120c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt break; 613e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 6140c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt spanStart = spanLimit; 6150c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 6160c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 6170c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt MetricAffectingSpan[] spans = mSpanned.getSpans(mStart + spanStart, 6180c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt mStart + spanLimit, MetricAffectingSpan.class); 6191e3ac18e7ad03e02819f3e1a89d6a80a2bb7645fGilles Debunne spans = TextUtils.removeEmptySpans(spans, mSpanned, MetricAffectingSpan.class); 6200c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 6210c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (spans.length > 0) { 6220c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt ReplacementSpan replacement = null; 6230c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt for (int j = 0; j < spans.length; j++) { 6240c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt MetricAffectingSpan span = spans[j]; 6250c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (span instanceof ReplacementSpan) { 6260c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt replacement = (ReplacementSpan)span; 6270c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } else { 6280c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt span.updateMeasureState(wp); 6290c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 6300c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 6310c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 6320c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (replacement != null) { 6330c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // If we have a replacement span, we're moving either to 6340c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // the start or end of this span. 6350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt return after ? spanLimit : spanStart; 636e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 637e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 638e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 639e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 640051910b9f998030dacb8a0722588cc715813fde1Raph Levien int dir = runIsRtl ? Paint.DIRECTION_RTL : Paint.DIRECTION_LTR; 6410c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int cursorOpt = after ? Paint.CURSOR_AFTER : Paint.CURSOR_BEFORE; 6420c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (mCharsValid) { 6430c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt return wp.getTextRunCursor(mChars, spanStart, spanLimit - spanStart, 644051910b9f998030dacb8a0722588cc715813fde1Raph Levien dir, offset, cursorOpt); 6450c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } else { 6460c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt return wp.getTextRunCursor(mText, mStart + spanStart, 647051910b9f998030dacb8a0722588cc715813fde1Raph Levien mStart + spanLimit, dir, mStart + offset, cursorOpt) - mStart; 648e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 649e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 650e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 651e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 6520bb000931bb841e75903d655552d1626ae158707Gilles Debunne * @param wp 6530bb000931bb841e75903d655552d1626ae158707Gilles Debunne */ 6540bb000931bb841e75903d655552d1626ae158707Gilles Debunne private static void expandMetricsFromPaint(FontMetricsInt fmi, TextPaint wp) { 6550bb000931bb841e75903d655552d1626ae158707Gilles Debunne final int previousTop = fmi.top; 6560bb000931bb841e75903d655552d1626ae158707Gilles Debunne final int previousAscent = fmi.ascent; 6570bb000931bb841e75903d655552d1626ae158707Gilles Debunne final int previousDescent = fmi.descent; 6580bb000931bb841e75903d655552d1626ae158707Gilles Debunne final int previousBottom = fmi.bottom; 6590bb000931bb841e75903d655552d1626ae158707Gilles Debunne final int previousLeading = fmi.leading; 6600bb000931bb841e75903d655552d1626ae158707Gilles Debunne 6610bb000931bb841e75903d655552d1626ae158707Gilles Debunne wp.getFontMetricsInt(fmi); 6620bb000931bb841e75903d655552d1626ae158707Gilles Debunne 6638a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, 6648a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousLeading); 6658a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio } 6668a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio 6678a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio static void updateMetrics(FontMetricsInt fmi, int previousTop, int previousAscent, 6688a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousDescent, int previousBottom, int previousLeading) { 6690bb000931bb841e75903d655552d1626ae158707Gilles Debunne fmi.top = Math.min(fmi.top, previousTop); 6700bb000931bb841e75903d655552d1626ae158707Gilles Debunne fmi.ascent = Math.min(fmi.ascent, previousAscent); 6710bb000931bb841e75903d655552d1626ae158707Gilles Debunne fmi.descent = Math.max(fmi.descent, previousDescent); 6720bb000931bb841e75903d655552d1626ae158707Gilles Debunne fmi.bottom = Math.max(fmi.bottom, previousBottom); 6730bb000931bb841e75903d655552d1626ae158707Gilles Debunne fmi.leading = Math.max(fmi.leading, previousLeading); 6740bb000931bb841e75903d655552d1626ae158707Gilles Debunne } 6750bb000931bb841e75903d655552d1626ae158707Gilles Debunne 6760bb000931bb841e75903d655552d1626ae158707Gilles Debunne /** 677e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Utility function for measuring and rendering text. The text must 678112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader * not include a tab. 679e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 680e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param wp the working paint 681e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the start of the text 6820c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param end the end of the text 683e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIsRtl true if the run is right-to-left 684e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas, can be null if rendering is not needed 685e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the edge of the run closest to the leading margin 686e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param top the top of the line 687e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline 688e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param bottom the bottom of the line 689e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives metrics information, can be null 690e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param needWidth true if the width of the run is needed 691909c7bca570b6f50650d0872e2037389b29252e3Raph Levien * @param offset the offset for the purpose of measuring 692e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width of the run based on the run direction; only 693e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * valid if needWidth is true 694e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 6950c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt private float handleText(TextPaint wp, int start, int end, 6960c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int contextStart, int contextEnd, boolean runIsRtl, 6970c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt Canvas c, float x, int top, int y, int bottom, 698909c7bca570b6f50650d0872e2037389b29252e3Raph Levien FontMetricsInt fmi, boolean needWidth, int offset) { 699e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 700850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio // Get metrics first (even for empty strings or "0" width runs) 701850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio if (fmi != null) { 702850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio expandMetricsFromPaint(fmi, wp); 703850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio } 704e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 7050c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int runLen = end - start; 706850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio // No need to do anything if the run width is "0" 707850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio if (runLen == 0) { 708850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio return 0f; 709850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio } 710850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio 711850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio float ret = 0; 712850dffa01ba9111799f24800ae8550eca457d757Fabrice Di Meglio 713c9fd978da60f76c0576150c55629a034e1fa19fbGilles Debunne if (needWidth || (c != null && (wp.bgColor != 0 || wp.underlineColor != 0 || runIsRtl))) { 714e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mCharsValid) { 715ea2b4a40f7236172398ebcaa273612e00340d847Raph Levien ret = wp.getRunAdvance(mChars, start, end, contextStart, contextEnd, 716909c7bca570b6f50650d0872e2037389b29252e3Raph Levien runIsRtl, offset); 717e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } else { 7180c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int delta = mStart; 719ea2b4a40f7236172398ebcaa273612e00340d847Raph Levien ret = wp.getRunAdvance(mText, delta + start, delta + end, 720909c7bca570b6f50650d0872e2037389b29252e3Raph Levien delta + contextStart, delta + contextEnd, runIsRtl, delta + offset); 721e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 722e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 723e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 724e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (c != null) { 725e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (runIsRtl) { 726e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt x -= ret; 727e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 728e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 729e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (wp.bgColor != 0) { 730dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne int previousColor = wp.getColor(); 731dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne Paint.Style previousStyle = wp.getStyle(); 732dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 733e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt wp.setColor(wp.bgColor); 734e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt wp.setStyle(Paint.Style.FILL); 735e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt c.drawRect(x, top, x + ret, bottom, wp); 736e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 737dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne wp.setStyle(previousStyle); 738dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne wp.setColor(previousColor); 739dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne } 740dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 741c9fd978da60f76c0576150c55629a034e1fa19fbGilles Debunne if (wp.underlineColor != 0) { 742dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne // kStdUnderline_Offset = 1/9, defined in SkTextFormatParams.h 743e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin float underlineTop = y + wp.baselineShift + (1.0f / 9.0f) * wp.getTextSize(); 744dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 745dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne int previousColor = wp.getColor(); 746dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne Paint.Style previousStyle = wp.getStyle(); 747e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin boolean previousAntiAlias = wp.isAntiAlias(); 748dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 749dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne wp.setStyle(Paint.Style.FILL); 750e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin wp.setAntiAlias(true); 751e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin 752c9fd978da60f76c0576150c55629a034e1fa19fbGilles Debunne wp.setColor(wp.underlineColor); 753c9fd978da60f76c0576150c55629a034e1fa19fbGilles Debunne c.drawRect(x, underlineTop, x + ret, underlineTop + wp.underlineThickness, wp); 754dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne 755dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne wp.setStyle(previousStyle); 756dd8f5ed79c7baed35b3f04e4778aff7867653255Gilles Debunne wp.setColor(previousColor); 757e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin wp.setAntiAlias(previousAntiAlias); 758e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 759e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 760da12f389eb4be0c08ca3fa9ca7663f4977858df5Fabrice Di Meglio drawTextRun(c, wp, start, end, contextStart, contextEnd, runIsRtl, 761da12f389eb4be0c08ca3fa9ca7663f4977858df5Fabrice Di Meglio x, y + wp.baselineShift); 762e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 763e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 764e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return runIsRtl ? -ret : ret; 765e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 766e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 767e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 768e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Utility function for measuring and rendering a replacement. 769e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 770bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy * 771e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param replacement the replacement 772e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param wp the work paint 773e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the start of the run 774e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param limit the limit of the run 775e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIsRtl true if the run is right-to-left 776e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas, can be null if not rendering 777e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the edge of the replacement closest to the leading margin 778e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param top the top of the line 779e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline 780e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param bottom the bottom of the line 781e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives metrics information, can be null 782e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param needWidth true if the width of the replacement is needed 783e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width of the run based on the run direction; only 784e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * valid if needWidth is true 785e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 786e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private float handleReplacement(ReplacementSpan replacement, TextPaint wp, 787bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy int start, int limit, boolean runIsRtl, Canvas c, 788e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float x, int top, int y, int bottom, FontMetricsInt fmi, 7890c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt boolean needWidth) { 790e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 791e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float ret = 0; 792e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 7930c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int textStart = mStart + start; 7940c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int textLimit = mStart + limit; 795e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 7960c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (needWidth || (c != null && runIsRtl)) { 7978a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousTop = 0; 7988a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousAscent = 0; 7998a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousDescent = 0; 8008a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousBottom = 0; 8018a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio int previousLeading = 0; 8028a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio 8038a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio boolean needUpdateMetrics = (fmi != null); 8048a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio 8058a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio if (needUpdateMetrics) { 8068a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousTop = fmi.top; 8078a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousAscent = fmi.ascent; 8088a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousDescent = fmi.descent; 8098a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousBottom = fmi.bottom; 8108a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousLeading = fmi.leading; 8118a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio } 8128a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio 8130c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt ret = replacement.getSize(wp, mText, textStart, textLimit, fmi); 8148a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio 8158a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio if (needUpdateMetrics) { 8168a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, 8178a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio previousLeading); 8188a5137a5aeba39cbc2c57c83ef79241b446d0cb7Fabrice Di Meglio } 8190c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt } 820e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 8210c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (c != null) { 8220c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt if (runIsRtl) { 8230c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt x -= ret; 824e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 8250c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt replacement.draw(c, mText, textStart, textLimit, 8260c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt x, top, y, bottom, wp); 827e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 828e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 829e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt return runIsRtl ? -ret : ret; 830e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 831945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 832e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 833e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Utility function for handling a unidirectional run. The run must not 834112d9c7f116bec0a52badde81bd778e59e88cb63Roozbeh Pournader * contain tabs but can contain styles. 835e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 836bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy * 837e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param start the line-relative start of the run 8380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param measureLimit the offset to measure to, between start and limit inclusive 839e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param limit the limit of the run 840e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param runIsRtl true if the run is right-to-left 841e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas, can be null 842e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the end of the run closest to the leading margin 843e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param top the top of the line 844e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline 845e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param bottom the bottom of the line 846e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param fmi receives metrics information, can be null 847e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param needWidth true if the width is required 848e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the signed width of the run based on the run direction; only 849e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * valid if needWidth is true 850e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 851bc7cdb6783d059249133b1c0baf52c305c6b4a33Romain Guy private float handleRun(int start, int measureLimit, 852e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt int limit, boolean runIsRtl, Canvas c, float x, int top, int y, 8530c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int bottom, FontMetricsInt fmi, boolean needWidth) { 854e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 855f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne // Case of an empty line, make sure we update fmi according to mPaint 856f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne if (start == measureLimit) { 857f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne TextPaint wp = mWorkPaint; 858f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne wp.set(mPaint); 85915c097a1c23105cdc0dd66dd5605ff35467d7118Fabrice Di Meglio if (fmi != null) { 86015c097a1c23105cdc0dd66dd5605ff35467d7118Fabrice Di Meglio expandMetricsFromPaint(fmi, wp); 86115c097a1c23105cdc0dd66dd5605ff35467d7118Fabrice Di Meglio } 86215c097a1c23105cdc0dd66dd5605ff35467d7118Fabrice Di Meglio return 0f; 863f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne } 864f483e514d4ed3b93cc5ba22beb9c85efcda75535Gilles Debunne 865945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne if (mSpanned == null) { 866945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne TextPaint wp = mWorkPaint; 867945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne wp.set(mPaint); 868945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne final int mlimit = measureLimit; 869909c7bca570b6f50650d0872e2037389b29252e3Raph Levien return handleText(wp, start, limit, start, limit, runIsRtl, c, x, top, 870909c7bca570b6f50650d0872e2037389b29252e3Raph Levien y, bottom, fmi, needWidth || mlimit < measureLimit, mlimit); 871945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne } 872945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 873c1f44830809f0a8526855f13822702ea756214faGilles Debunne mMetricAffectingSpanSpanSet.init(mSpanned, mStart + start, mStart + limit); 874c1f44830809f0a8526855f13822702ea756214faGilles Debunne mCharacterStyleSpanSet.init(mSpanned, mStart + start, mStart + limit); 875945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 876e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // Shaping needs to take into account context up to metric boundaries, 877e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt // but rendering needs to take into account character style boundaries. 8780c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // So we iterate through metric runs to get metric bounds, 8790c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // then within each metric run iterate through character style runs 8800c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt // for the run bounds. 881945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne final float originalX = x; 8820c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt for (int i = start, inext; i < measureLimit; i = inext) { 883e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt TextPaint wp = mWorkPaint; 884e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt wp.set(mPaint); 885e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 886c1f44830809f0a8526855f13822702ea756214faGilles Debunne inext = mMetricAffectingSpanSpanSet.getNextTransition(mStart + i, mStart + limit) - 887c1f44830809f0a8526855f13822702ea756214faGilles Debunne mStart; 888945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne int mlimit = Math.min(inext, measureLimit); 889945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 890945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne ReplacementSpan replacement = null; 891945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 892c1f44830809f0a8526855f13822702ea756214faGilles Debunne for (int j = 0; j < mMetricAffectingSpanSpanSet.numberOfSpans; j++) { 893945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne // Both intervals [spanStarts..spanEnds] and [mStart + i..mStart + mlimit] are NOT 894945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne // empty by construction. This special case in getSpans() explains the >= & <= tests 895c1f44830809f0a8526855f13822702ea756214faGilles Debunne if ((mMetricAffectingSpanSpanSet.spanStarts[j] >= mStart + mlimit) || 896c1f44830809f0a8526855f13822702ea756214faGilles Debunne (mMetricAffectingSpanSpanSet.spanEnds[j] <= mStart + i)) continue; 897c1f44830809f0a8526855f13822702ea756214faGilles Debunne MetricAffectingSpan span = mMetricAffectingSpanSpanSet.spans[j]; 898945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne if (span instanceof ReplacementSpan) { 899945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne replacement = (ReplacementSpan)span; 900945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne } else { 901945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne // We might have a replacement that uses the draw 902945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne // state, otherwise measure state would suffice. 903945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne span.updateDrawState(wp); 904e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 905e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 906e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 907945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne if (replacement != null) { 908945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne x += handleReplacement(replacement, wp, i, mlimit, runIsRtl, c, x, top, y, 909945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne bottom, fmi, needWidth || mlimit < measureLimit); 910945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne continue; 911945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne } 912945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne 91342ef515d185d4fc038d602172789cc264f1d9960Raph Levien for (int j = i, jnext; j < mlimit; j = jnext) { 914909c7bca570b6f50650d0872e2037389b29252e3Raph Levien jnext = mCharacterStyleSpanSet.getNextTransition(mStart + j, mStart + inext) - 91542ef515d185d4fc038d602172789cc264f1d9960Raph Levien mStart; 916909c7bca570b6f50650d0872e2037389b29252e3Raph Levien int offset = Math.min(jnext, mlimit); 91742ef515d185d4fc038d602172789cc264f1d9960Raph Levien 91842ef515d185d4fc038d602172789cc264f1d9960Raph Levien wp.set(mPaint); 91942ef515d185d4fc038d602172789cc264f1d9960Raph Levien for (int k = 0; k < mCharacterStyleSpanSet.numberOfSpans; k++) { 92042ef515d185d4fc038d602172789cc264f1d9960Raph Levien // Intentionally using >= and <= as explained above 921909c7bca570b6f50650d0872e2037389b29252e3Raph Levien if ((mCharacterStyleSpanSet.spanStarts[k] >= mStart + offset) || 92242ef515d185d4fc038d602172789cc264f1d9960Raph Levien (mCharacterStyleSpanSet.spanEnds[k] <= mStart + j)) continue; 9230c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt 92442ef515d185d4fc038d602172789cc264f1d9960Raph Levien CharacterStyle span = mCharacterStyleSpanSet.spans[k]; 92542ef515d185d4fc038d602172789cc264f1d9960Raph Levien span.updateDrawState(wp); 926e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 92742ef515d185d4fc038d602172789cc264f1d9960Raph Levien 92826d443aee4ee5a8791417b4ca09e8c78ba8dc78bRaph Levien // Only draw hyphen on last run in line 92926d443aee4ee5a8791417b4ca09e8c78ba8dc78bRaph Levien if (jnext < mLen) { 93026d443aee4ee5a8791417b4ca09e8c78ba8dc78bRaph Levien wp.setHyphenEdit(0); 93126d443aee4ee5a8791417b4ca09e8c78ba8dc78bRaph Levien } 93242ef515d185d4fc038d602172789cc264f1d9960Raph Levien x += handleText(wp, j, jnext, i, inext, runIsRtl, c, x, 933909c7bca570b6f50650d0872e2037389b29252e3Raph Levien top, y, bottom, fmi, needWidth || jnext < measureLimit, offset); 934e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 935e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 936e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 937945ee9b1661e60e0074d4f16f61fc147c728c6bfGilles Debunne return x - originalX; 938e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 939e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 940e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 941e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Render a text run with the set-up paint. 942e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 943e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param c the canvas 944e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param wp the paint used to render the text 9450c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param start the start of the run 9460c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param end the end of the run 9470c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param contextStart the start of context for the run 9480c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt * @param contextEnd the end of the context for the run 949da12f389eb4be0c08ca3fa9ca7663f4977858df5Fabrice Di Meglio * @param runIsRtl true if the run is right-to-left 950e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param x the x position of the left edge of the run 951e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param y the baseline of the run 952e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 9530c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt private void drawTextRun(Canvas c, TextPaint wp, int start, int end, 954da12f389eb4be0c08ca3fa9ca7663f4977858df5Fabrice Di Meglio int contextStart, int contextEnd, boolean runIsRtl, float x, int y) { 955e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 956e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt if (mCharsValid) { 9570c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int count = end - start; 9580c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int contextCount = contextEnd - contextStart; 9590c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt c.drawTextRun(mChars, start, count, contextStart, contextCount, 960051910b9f998030dacb8a0722588cc715813fde1Raph Levien x, y, runIsRtl, wp); 961e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } else { 9620c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt int delta = mStart; 9630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt c.drawTextRun(mText, delta + start, delta + end, 964051910b9f998030dacb8a0722588cc715813fde1Raph Levien delta + contextStart, delta + contextEnd, x, y, runIsRtl, wp); 965e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 966e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 967e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 968e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt /** 969e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * Returns the next tab position. 970e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * 971e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @param h the (unsigned) offset from the leading margin 972e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt * @return the (unsigned) tab position after this offset 973e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt */ 974e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt float nextTab(float h) { 975c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt if (mTabs != null) { 976c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt return mTabs.nextTab(h); 977e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 978c982f60e982c1d2df9f115ed9a5c3ef3643d0892Doug Felt return TabStops.nextDefaultStop(h, TAB_INCREMENT); 979e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt } 980e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt 981e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt private static final int TAB_INCREMENT = 20; 982e8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7aDoug Felt} 983