AI 143314: am: CL 143165 am: CL 142861 Make TextView Emoji scale to match the size of the text.
  Original author: enf
  Merged from: //branches/cupcake/...
  Original author: android-build
  Merged from: //branches/donutburger/...

Automated import of CL 143314
diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java
index 23e740d..a6ed922 100644
--- a/core/java/android/text/Layout.java
+++ b/core/java/android/text/Layout.java
@@ -21,6 +21,7 @@
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Path;
 import com.android.internal.util.ArrayUtils;
 import android.util.Config;
@@ -52,6 +53,8 @@
         }
     };
 
+    private RectF mEmojiRect;
+
     /**
      * Return how wide a layout would be necessary to display the
      * specified text with one line per paragraph.
@@ -1327,7 +1330,7 @@
         return right;
     }
 
-    private static void drawText(Canvas canvas,
+    private void drawText(Canvas canvas,
                                  CharSequence text, int start, int end,
                                  int dir, Directions directions,
                                  float x, int top, int y, int bottom,
@@ -1384,8 +1387,26 @@
                                                  top, y, bottom, paint, workPaint,
                                                  start + j != end);
 
-                            canvas.drawBitmap(bm, x + h, y - bm.getHeight(), paint);
-                            h += bm.getWidth();
+                            if (mEmojiRect == null) {
+                                mEmojiRect = new RectF();
+                            }
+
+                            workPaint.set(paint);
+                            Styled.measureText(paint, workPaint, text,
+                                               start + j, start + j + 1,
+                                               null);
+                                        
+                            float bitmapHeight = bm.getHeight();
+                            float textHeight = -workPaint.ascent();
+                            float scale = textHeight / bitmapHeight;
+                            float width = bm.getWidth() * scale;
+
+                            mEmojiRect.set(x + h, y - textHeight,
+                                           x + h + width, y);
+
+                            canvas.drawBitmap(bm, null, mEmojiRect, paint);
+                            h += width;
+
                             j++;
                             segstart = j + 1;
                         }
@@ -1503,10 +1524,17 @@
                     }
 
                     if (bm != null) {
+                        workPaint.set(paint);
+                        Styled.measureText(paint, workPaint, text,
+                                           offset, offset + 1, null);
+
+                        float wid = (float) bm.getWidth() *
+                                    -workPaint.ascent() / bm.getHeight();
+
                         if (dir == DIR_RIGHT_TO_LEFT) {
-                            h -= bm.getWidth();
+                            h -= wid;
                         } else {
-                            h += bm.getWidth();
+                            h += wid;
                         }
 
                         j++;
@@ -1587,7 +1615,14 @@
                     if (bm == null) {
                         h = nextTab(text, start, end, h, tabs);
                     } else {
-                        h += bm.getWidth();
+                        workPaint.set(paint);
+                        Styled.measureText(paint, workPaint, text,
+                                           start + i, start + i + 1, null);
+
+                        float wid = (float) bm.getWidth() *
+                                    -workPaint.ascent() / bm.getHeight();
+
+                        h += wid;
                         i++;
                     }
                 }
@@ -1607,16 +1642,10 @@
                         bot = fm.bottom;
                     }
 
-                    if (bm != null) {
-                        int ht = -bm.getHeight();
-
-                        if (ht < ab) {
-                            ab = ht;
-                        }
-                        if (ht < top) {
-                            top = ht;
-                        }
-                    }
+                    /*
+                     * No need to take bitmap height into account here,
+                     * since it is scaled to match the text height.
+                     */
                 }
 
                 here = i + 1;
diff --git a/core/java/android/text/StaticLayout.java b/core/java/android/text/StaticLayout.java
index 720d15a..686e8f5 100644
--- a/core/java/android/text/StaticLayout.java
+++ b/core/java/android/text/StaticLayout.java
@@ -567,7 +567,19 @@
                                 getBitmapFromAndroidPua(emoji);
 
                             if (bm != null) {
-                                w += bm.getWidth();
+                                Paint whichPaint;
+
+                                if (spanned == null) {
+                                    whichPaint = paint;
+                                } else {
+                                    whichPaint = mWorkPaint;
+                                }
+
+                                float wid = (float) bm.getWidth() *
+                                            -whichPaint.ascent() /
+                                            bm.getHeight();
+
+                                w += wid;
                                 tab = true;
                                 j++;
                             } else {