diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index 31945d0206d68bcf42ff655d168b2634d5c349cf..9a1e56a60a60de2247bb15f7735a8aea826511ac 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -295,14 +295,16 @@
             <flag name="withIconLeft" value="0x1000" />
             <flag name="withIconRight" value="0x2000" />
             <flag name="autoXScale" value="0x4000" />
+            <!-- The autoScale value implies autoXScale bit on to optimize scaling code path. -->
+            <flag name="autoScale" value="0xc000" />
             <!-- If true, character case of code, altCode, moreKeys, keyOutputText, keyLabel,
                  or keyHintLabel will never be subject to change. -->
-            <flag name="preserveCase" value="0x8000" />
+            <flag name="preserveCase" value="0x10000" />
             <!-- If true, use keyShiftedLetterHintActivatedColor for the shifted letter hint and
                  keyTextInactivatedColor for the primary key top label. -->
-            <flag name="shiftedLetterActivated" value="0x10000" />
+            <flag name="shiftedLetterActivated" value="0x20000" />
             <!-- If true, use EditorInfo.actionLabel for the key label. -->
-            <flag name="fromCustomActionLabel" value="0x20000" />
+            <flag name="fromCustomActionLabel" value="0x40000" />
             <!-- If true, disable keyHintLabel. -->
             <flag name="disableKeyHintLabel" value="0x40000000" />
             <!-- If true, disable additionalMoreKeys. -->
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index f7ec9509d54ae5257f611c13c8e39cae0b965e1e..d3f628dcf101cee5a6a33defecfa67f1a40305a8 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -84,10 +84,16 @@ public class Key implements Comparable<Key> {
     private static final int LABEL_FLAGS_HAS_HINT_LABEL = 0x800;
     private static final int LABEL_FLAGS_WITH_ICON_LEFT = 0x1000;
     private static final int LABEL_FLAGS_WITH_ICON_RIGHT = 0x2000;
+    // The bit to calculate the ratio of key label width against key width. If autoXScale bit is on
+    // and autoYScale bit is off, the key label may be shrunk only for X-direction.
+    // If both autoXScale and autoYScale bits are on, the key label text size may be auto scaled.
     private static final int LABEL_FLAGS_AUTO_X_SCALE = 0x4000;
-    private static final int LABEL_FLAGS_PRESERVE_CASE = 0x8000;
-    private static final int LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED = 0x10000;
-    private static final int LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL = 0x20000;
+    private static final int LABEL_FLAGS_AUTO_Y_SCALE = 0x8000;
+    private static final int LABEL_FLAGS_AUTO_SCALE = LABEL_FLAGS_AUTO_X_SCALE
+            | LABEL_FLAGS_AUTO_Y_SCALE;
+    private static final int LABEL_FLAGS_PRESERVE_CASE = 0x10000;
+    private static final int LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED = 0x20000;
+    private static final int LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL = 0x40000;
     private static final int LABEL_FLAGS_DISABLE_HINT_LABEL = 0x40000000;
     private static final int LABEL_FLAGS_DISABLE_ADDITIONAL_MORE_KEYS = 0x80000000;
 
@@ -702,10 +708,14 @@ public class Key implements Comparable<Key> {
         return (mLabelFlags & LABEL_FLAGS_WITH_ICON_RIGHT) != 0;
     }
 
-    public final boolean needsXScale() {
+    public final boolean needsAutoXScale() {
         return (mLabelFlags & LABEL_FLAGS_AUTO_X_SCALE) != 0;
     }
 
+    public final boolean needsAutoScale() {
+        return (mLabelFlags & LABEL_FLAGS_AUTO_SCALE) == LABEL_FLAGS_AUTO_SCALE;
+    }
+
     public final boolean isShiftedLetterActivated() {
         return (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) != 0;
     }
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index 5578713a0753167a74405acc3f93c7cd3985435d..346c641cdc67f7505798d76cebcd948c74427ef2 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -404,9 +404,15 @@ public class KeyboardView extends View {
                 positionX = centerX;
                 paint.setTextAlign(Align.CENTER);
             }
-            if (key.needsXScale()) {
-                paint.setTextScaleX(Math.min(1.0f,
-                        (keyWidth * MAX_LABEL_RATIO) / TypefaceUtils.getLabelWidth(label, paint)));
+            if (key.needsAutoXScale()) {
+                final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) /
+                        TypefaceUtils.getLabelWidth(label, paint));
+                if (key.needsAutoScale()) {
+                    final float autoSize = paint.getTextSize() * ratio;
+                    paint.setTextSize(autoSize);
+                } else {
+                    paint.setTextScaleX(ratio);
+                }
             }
 
             paint.setColor(key.selectTextColor(params));