OSDN Git Service

Use getRunAdvance to position cursor
authorRaph Levien <raph@google.com>
Thu, 28 May 2015 23:16:24 +0000 (16:16 -0700)
committerRaph Levien <raph@google.com>
Thu, 28 May 2015 23:36:36 +0000 (16:36 -0700)
TextLine used to use getTextRunAdvances on a substring to compute a
cursor position, but this had a number of problems, especially when
the substring is a wider than the full string (as can happen in
certain Tamil ligatures).

This patch changes the implementation to use getRunAdvance, which was
explicitly designed for this use case.

We should also change Layout.getHorizontal to use the dual
getOffsetForAdvance, but that's basically a performance optimization,
the functionality should be basically equivalent.

Bug: 21125816
Change-Id: I669b85eaecfbf6f7aa6c6a9dddbf1a210eb94571

core/java/android/text/Layout.java
core/java/android/text/TextLine.java

index f176240..f7027f9 100644 (file)
@@ -1121,6 +1121,7 @@ public abstract class Layout {
      * closest to the specified horizontal position.
      */
     public int getOffsetForHorizontal(int line, float horiz) {
+        // TODO: use Paint.getOffsetForAdvance to avoid binary search
         int max = getLineEnd(line) - 1;
         int min = getLineStart(line);
         Directions dirs = getLineDirections(line);
index 479242c..605b91d 100644 (file)
@@ -739,16 +739,14 @@ class TextLine {
 
         float ret = 0;
 
-        int contextLen = contextEnd - contextStart;
         if (needWidth || (c != null && (wp.bgColor != 0 || wp.underlineColor != 0 || runIsRtl))) {
             if (mCharsValid) {
-                ret = wp.getTextRunAdvances(mChars, start, runLen,
-                        contextStart, contextLen, runIsRtl, null, 0);
+                ret = wp.getRunAdvance(mChars, start, contextEnd, contextStart, contextEnd,
+                        runIsRtl, end);
             } else {
                 int delta = mStart;
-                ret = wp.getTextRunAdvances(mText, delta + start,
-                        delta + end, delta + contextStart, delta + contextEnd,
-                        runIsRtl, null, 0);
+                ret = wp.getRunAdvance(mText, delta + start, delta + contextEnd,
+                        delta + contextStart, delta + contextEnd, runIsRtl, delta + end);
             }
         }