diff options
author | Leon Scroggins <scroggo@google.com> | 2010-11-18 14:03:03 -0500 |
---|---|---|
committer | Leon Scroggins <scroggo@google.com> | 2010-11-22 09:37:57 -0500 |
commit | 1ca562635117e10fae0888689909e6c39d66b0a4 (patch) | |
tree | aa7f79092efb68d1ae8669d72dd9a17561262b17 | |
parent | 3644bf11bf42ba694bf71d312947c85fa5f84daa (diff) | |
download | frameworks_base-1ca562635117e10fae0888689909e6c39d66b0a4.zip frameworks_base-1ca562635117e10fae0888689909e6c39d66b0a4.tar.gz frameworks_base-1ca562635117e10fae0888689909e6c39d66b0a4.tar.bz2 |
Alter the Layout of WebTextView to match webkit.
Bug:3085564
In order to treat the selection handles properly, the text in
WebTextView needs to line up with the text in webkit. Use the
line spacing style, or the paint if none provided, to determine
the spacing for WebTextView's Layout. When no line spacing is
provided, use the floating point values of ascent and descent,
rather than the rounded result, to determine spacing.
Requires a change in external/webkit:
https://android-git.corp.google.com/g/#change,80641
Change-Id: I51081835e128a0e938028c7c7aeb5111aa5f072c
-rw-r--r-- | core/java/android/webkit/WebTextView.java | 65 | ||||
-rw-r--r-- | core/java/android/webkit/WebView.java | 1 | ||||
-rw-r--r-- | core/java/android/widget/TextView.java | 8 |
3 files changed, 73 insertions, 1 deletions
diff --git a/core/java/android/webkit/WebTextView.java b/core/java/android/webkit/WebTextView.java index 1caa707..fafb6be 100644 --- a/core/java/android/webkit/WebTextView.java +++ b/core/java/android/webkit/WebTextView.java @@ -26,9 +26,12 @@ import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; +import android.text.BoringLayout.Metrics; +import android.text.DynamicLayout; import android.text.Editable; import android.text.InputFilter; import android.text.Layout; +import android.text.Layout.Alignment; import android.text.Selection; import android.text.Spannable; import android.text.TextPaint; @@ -359,6 +362,68 @@ import junit.framework.Assert; } } + @Override + protected void makeNewLayout(int w, int hintWidth, Metrics boring, + Metrics hintBoring, int ellipsisWidth, boolean bringIntoView) { + // Necessary to get a Layout to work with, and to do the other work that + // makeNewLayout does. + super.makeNewLayout(w, hintWidth, boring, hintBoring, ellipsisWidth, + bringIntoView); + + // For fields that do not draw, create a layout which is altered so that + // the text lines up. + if (DebugFlags.DRAW_WEBTEXTVIEW || willNotDraw()) { + float lineHeight = -1; + if (mWebView != null) { + float height = mWebView.nativeFocusCandidateLineHeight(); + if (height != -1) { + lineHeight = height * mWebView.getScale(); + } + } + CharSequence text = getText(); + // Copy from the existing Layout. + mLayout = new WebTextViewLayout(text, text, getPaint(), w, + mLayout.getAlignment(), mLayout.getSpacingMultiplier(), + mLayout.getSpacingAdd(), false, null, ellipsisWidth, + lineHeight); + } + } + + /** + * Custom layout which figures out its line spacing. If -1 is passed in for + * the height, it will use the ascent and descent from the paint to + * determine the line spacing. Otherwise it will use the spacing provided. + */ + private static class WebTextViewLayout extends DynamicLayout { + private float mLineHeight; + private float mDifference; + public WebTextViewLayout(CharSequence base, CharSequence display, + TextPaint paint, + int width, Alignment align, + float spacingMult, float spacingAdd, + boolean includepad, + TextUtils.TruncateAt ellipsize, int ellipsizedWidth, + float lineHeight) { + super(base, display, paint, width, align, spacingMult, spacingAdd, + includepad, ellipsize, ellipsizedWidth); + float paintLineHeight = paint.descent() - paint.ascent(); + if (lineHeight == -1f) { + mLineHeight = paintLineHeight; + mDifference = 0f; + } else { + mLineHeight = lineHeight; + // Through trial and error, I found this calculation to improve + // the accuracy of line placement. + mDifference = (lineHeight - paintLineHeight) / 2; + } + } + + @Override + public int getLineTop(int line) { + return Math.round(mLineHeight * line - mDifference); + } + } + @Override public InputConnection onCreateInputConnection( EditorInfo outAttrs) { InputConnection connection = super.onCreateInputConnection(outAttrs); diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 5bf2ad4..d865f35 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -7461,6 +7461,7 @@ public class WebView extends AbsoluteLayout /* package */ native int nativeFocusCandidatePointer(); private native String nativeFocusCandidateText(); /* package */ native float nativeFocusCandidateTextSize(); + /* package */ native int nativeFocusCandidateLineHeight(); /** * Returns an integer corresponding to WebView.cpp::type. * See WebTextView.setType() diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 80a6a2f..78f3cd9 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -9015,7 +9015,13 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private boolean mUserSetTextScaleX; private final Paint mHighlightPaint; private int mHighlightColor = 0xCC475925; - private Layout mLayout; + /** + * This is temporarily visible to fix bug 3085564 in webView. Do not rely on + * this field being protected. Will be restored as private when lineHeight + * feature request 3215097 is implemented + * @hide + */ + protected Layout mLayout; private long mShowCursor; private Blink mBlink; |