Fix text rendering issue with text scaling.

Change-Id: I1f3ae40025697e8f8ca0616ee6550fe215cadcc8
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 5d7f8bf..6bcaef6 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -166,6 +166,8 @@
     renderUTF(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds);
 }
 
+#define SkAutoKern_AdjustF(prev, next)    (((next) - (prev) + 32) >> 6 << 16)
+
 void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
         int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
         uint32_t bitmapW, uint32_t bitmapH,Rect *bounds) {
@@ -173,12 +175,16 @@
         return;
     }
 
-    int penX = x, penY = y;
+    SkFixed penX = SkIntToFixed(x);
+    int penY = y;
     int glyphsLeft = 1;
     if (numGlyphs > 0) {
         glyphsLeft = numGlyphs;
     }
 
+    SkFixed prevRsbDelta = 0;
+    penX += SK_Fixed1 / 2;
+
     text += start;
 
     while (glyphsLeft > 0) {
@@ -190,23 +196,25 @@
         }
 
         CachedGlyphInfo* cachedGlyph = getCachedUTFChar(paint, utfChar);
+        penX += SkAutoKern_AdjustF(prevRsbDelta, cachedGlyph->mLsbDelta);
+        prevRsbDelta = cachedGlyph->mRsbDelta;
 
         // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
         if (cachedGlyph->mIsValid) {
             switch(mode) {
             case FRAMEBUFFER:
-                drawCachedGlyph(cachedGlyph, penX, penY);
+                drawCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY);
                 break;
             case BITMAP:
-                drawCachedGlyph(cachedGlyph, penX, penY, bitmap, bitmapW, bitmapH);
+                drawCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY, bitmap, bitmapW, bitmapH);
                 break;
             case MEASURE:
-                measureCachedGlyph(cachedGlyph, penX, penY, bounds);
+                measureCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY, bounds);
                 break;
             }
         }
 
-        penX += SkFixedFloor(cachedGlyph->mAdvanceX);
+        penX += cachedGlyph->mAdvanceX;
 
         // If we were given a specific number of glyphs, decrement
         if (numGlyphs > 0) {
@@ -220,6 +228,8 @@
     glyph->mAdvanceY = skiaGlyph.fAdvanceY;
     glyph->mBitmapLeft = skiaGlyph.fLeft;
     glyph->mBitmapTop = skiaGlyph.fTop;
+    glyph->mLsbDelta = skiaGlyph.fLsbDelta;
+    glyph->mRsbDelta = skiaGlyph.fRsbDelta;
 
     uint32_t startX = 0;
     uint32_t startY = 0;
@@ -352,7 +362,7 @@
 
 bool FontRenderer::cacheBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) {
     // If the glyph is too tall, don't cache it
-    if (glyph.fWidth > mCacheLines[mCacheLines.size() - 1]->mMaxHeight) {
+    if (glyph.fHeight > mCacheLines[mCacheLines.size() - 1]->mMaxHeight) {
         LOGE("Font size to large to fit in cache. width, height = %i, %i",
                 (int) glyph.fWidth, (int) glyph.fHeight);
         return false;
@@ -616,7 +626,7 @@
     const float maxPrecacheFontSize = 40.0f;
     bool isNewFont = currentNumFonts != mActiveFonts.size();
 
-    if(isNewFont && fontSize <= maxPrecacheFontSize ){
+    if (isNewFont && fontSize <= maxPrecacheFontSize) {
         precacheLatin(paint);
     }
 }
diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h
index a03ea92..de5c019 100644
--- a/libs/hwui/FontRenderer.h
+++ b/libs/hwui/FontRenderer.h
@@ -94,6 +94,9 @@
         // Values below contain a glyph's origin in the bitmap
         int32_t mBitmapLeft;
         int32_t mBitmapTop;
+        // Auto-kerning
+        SkFixed mLsbDelta;
+        SkFixed mRsbDelta;
     };
 
     Font(FontRenderer* state, uint32_t fontId, float fontSize);
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index de27090..7cc4880 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -571,14 +571,6 @@
     }
     paint->setAntiAlias(true);
 
-    float scaleX = paint->getTextScaleX();
-    bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
-    if (applyScaleX) {
-        save(SkCanvas::kMatrix_SaveFlag);
-        translate(x - (x * scaleX), 0.0f);
-        scale(scaleX, 1.0f);
-    }
-
     float length = -1.0f;
     switch (paint->getTextAlign()) {
         case SkPaint::kCenter_Align:
@@ -634,10 +626,6 @@
     glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
 
     drawTextDecorations(text, bytesCount, length, x, y, paint);
-
-    if (applyScaleX) {
-        restore();
-    }
 }
 
 void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {