Merge "Move reference char/digit width/height method to TypefaceUtils"

main
Tadashi G. Takaoka 2013-11-07 10:18:36 +00:00 committed by Android (Google) Code Review
commit 6bc32968b9
5 changed files with 43 additions and 38 deletions

View File

@ -113,9 +113,6 @@ public class KeyboardView extends View {
private final Canvas mOffscreenCanvas = new Canvas();
private final Paint mPaint = new Paint();
private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
private static final char[] KEY_LABEL_REFERENCE_CHAR = { 'M' };
private static final char[] KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR = { '8' };
public KeyboardView(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.keyboardViewStyle);
}
@ -370,10 +367,8 @@ public class KeyboardView extends View {
if (label != null) {
paint.setTypeface(key.selectTypeface(params));
paint.setTextSize(key.selectTextSize(params));
final float labelCharHeight = TypefaceUtils.getCharHeight(
KEY_LABEL_REFERENCE_CHAR, paint);
final float labelCharWidth = TypefaceUtils.getCharWidth(
KEY_LABEL_REFERENCE_CHAR, paint);
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
// Vertical label text alignment.
final float baseline = centerY + labelCharHeight / 2.0f;
@ -391,12 +386,12 @@ public class KeyboardView extends View {
positionX = centerX - labelCharWidth * 7.0f / 4.0f;
paint.setTextAlign(Align.LEFT);
} else if (key.hasLabelWithIconLeft() && icon != null) {
labelWidth = TypefaceUtils.getLabelWidth(label, paint) + icon.getIntrinsicWidth()
labelWidth = TypefaceUtils.getStringWidth(label, paint) + icon.getIntrinsicWidth()
+ LABEL_ICON_MARGIN * keyWidth;
positionX = centerX + labelWidth / 2.0f;
paint.setTextAlign(Align.RIGHT);
} else if (key.hasLabelWithIconRight() && icon != null) {
labelWidth = TypefaceUtils.getLabelWidth(label, paint) + icon.getIntrinsicWidth()
labelWidth = TypefaceUtils.getStringWidth(label, paint) + icon.getIntrinsicWidth()
+ LABEL_ICON_MARGIN * keyWidth;
positionX = centerX - labelWidth / 2.0f;
paint.setTextAlign(Align.LEFT);
@ -406,7 +401,7 @@ public class KeyboardView extends View {
}
if (key.needsAutoXScale()) {
final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) /
TypefaceUtils.getLabelWidth(label, paint));
TypefaceUtils.getStringWidth(label, paint));
if (key.needsAutoScale()) {
final float autoSize = paint.getTextSize() * ratio;
paint.setTextSize(autoSize);
@ -457,32 +452,28 @@ public class KeyboardView extends View {
// TODO: Should add a way to specify type face for hint letters
paint.setTypeface(Typeface.DEFAULT_BOLD);
blendAlpha(paint, params.mAnimAlpha);
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
final float hintX, hintY;
if (key.hasHintLabel()) {
// The hint label is placed just right of the key label. Used mainly on
// "phone number" layout.
// TODO: Generalize the following calculations.
hintX = positionX
+ TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) * 2.0f;
hintY = centerY
+ TypefaceUtils.getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
hintX = positionX + labelCharWidth * 2.0f;
hintY = centerY + labelCharHeight / 2.0f;
paint.setTextAlign(Align.LEFT);
} else if (key.hasShiftedLetterHint()) {
// The hint label is placed at top-right corner of the key. Used mainly on tablet.
hintX = keyWidth - mKeyShiftedLetterHintPadding
- TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
hintX = keyWidth - mKeyShiftedLetterHintPadding - labelCharWidth / 2.0f;
paint.getFontMetrics(mFontMetrics);
hintY = -mFontMetrics.top;
paint.setTextAlign(Align.CENTER);
} else { // key.hasHintLetter()
// The hint letter is placed at top-right corner of the key. Used mainly on phone.
final float keyNumericHintLabelReferenceCharWidth =
TypefaceUtils.getCharWidth(KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR, paint);
final float keyHintLabelStringWidth =
TypefaceUtils.getStringWidth(hintLabel, paint);
final float hintDigitWidth = TypefaceUtils.getReferenceDigitWidth(paint);
final float hintLabelWidth = TypefaceUtils.getStringWidth(hintLabel, paint);
hintX = keyWidth - mKeyHintLetterPadding
- Math.max(keyNumericHintLabelReferenceCharWidth, keyHintLabelStringWidth)
/ 2.0f;
- Math.max(hintDigitWidth, hintLabelWidth) / 2.0f;
hintY = -paint.ascent();
paint.setTextAlign(Align.CENTER);
}
@ -536,7 +527,7 @@ public class KeyboardView extends View {
paint.setColor(params.mHintLabelColor);
paint.setTextAlign(Align.CENTER);
final float hintX = keyWidth - mKeyHintLetterPadding
- TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
- TypefaceUtils.getReferenceCharWidth(paint) / 2.0f;
final float hintY = keyHeight - mKeyPopupHintLetterPadding;
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);

View File

@ -1193,7 +1193,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
private boolean fitsTextIntoWidth(final int width, final String text, final Paint paint) {
final int maxTextWidth = width - mLanguageOnSpacebarHorizontalMargin * 2;
paint.setTextScaleX(1.0f);
final float textWidth = TypefaceUtils.getLabelWidth(text, paint);
final float textWidth = TypefaceUtils.getStringWidth(text, paint);
if (textWidth < width) {
return true;
}
@ -1204,7 +1204,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
}
paint.setTextScaleX(scaleX);
return TypefaceUtils.getLabelWidth(text, paint) < maxTextWidth;
return TypefaceUtils.getStringWidth(text, paint) < maxTextWidth;
}
// Layout language name on spacebar.

View File

@ -327,7 +327,7 @@ public final class MoreKeysKeyboard extends Keyboard {
// If the label is single letter, minKeyWidth is enough to hold the label.
if (label != null && StringUtils.codePointCount(label) > 1) {
maxWidth = Math.max(maxWidth,
(int)(TypefaceUtils.getLabelWidth(label, paint) + padding));
(int)(TypefaceUtils.getStringWidth(label, paint) + padding));
}
}
return maxWidth;

View File

@ -75,7 +75,7 @@ public final class MoreSuggestions extends Keyboard {
while (index < size) {
final String word = suggestedWords.getWord(index);
// TODO: Should take care of text x-scaling.
mWidths[index] = (int)(TypefaceUtils.getLabelWidth(word, paint) + padding);
mWidths[index] = (int)(TypefaceUtils.getStringWidth(word, paint) + padding);
final int numColumn = index - rowStartIndex + 1;
final int columnWidth =
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;

View File

@ -22,6 +22,9 @@ import android.graphics.Typeface;
import android.util.SparseArray;
public final class TypefaceUtils {
private static final char[] KEY_LABEL_REFERENCE_CHAR = { 'M' };
private static final char[] KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR = { '8' };
private TypefaceUtils() {
// This utility class is not publicly instantiable.
}
@ -31,7 +34,7 @@ public final class TypefaceUtils {
// Working variable for the following method.
private static final Rect sTextHeightBounds = new Rect();
public static float getCharHeight(final char[] referenceChar, final Paint paint) {
private static float getCharHeight(final char[] referenceChar, final Paint paint) {
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
synchronized (sTextHeightCache) {
final Float cachedValue = sTextHeightCache.get(key);
@ -51,7 +54,7 @@ public final class TypefaceUtils {
// Working variable for the following method.
private static final Rect sTextWidthBounds = new Rect();
public static float getCharWidth(final char[] referenceChar, final Paint paint) {
private static float getCharWidth(final char[] referenceChar, final Paint paint) {
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
synchronized (sTextWidthCache) {
final Float cachedValue = sTextWidthCache.get(key);
@ -66,11 +69,6 @@ public final class TypefaceUtils {
}
}
public static float getStringWidth(final String string, final Paint paint) {
paint.getTextBounds(string, 0, string.length(), sTextWidthBounds);
return sTextWidthBounds.width();
}
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
final int labelSize = (int)paint.getTextSize();
final Typeface face = paint.getTypeface();
@ -86,9 +84,25 @@ public final class TypefaceUtils {
}
}
public static float getLabelWidth(final String label, final Paint paint) {
final Rect textBounds = new Rect();
paint.getTextBounds(label, 0, label.length(), textBounds);
return textBounds.width();
public static float getReferenceCharHeight(final Paint paint) {
return getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint);
}
public static float getReferenceCharWidth(final Paint paint) {
return getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint);
}
public static float getReferenceDigitWidth(final Paint paint) {
return getCharWidth(KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR, paint);
}
// Working variable for the following method.
private static final Rect sStringWidthBounds = new Rect();
public static float getStringWidth(final String string, final Paint paint) {
synchronized (sStringWidthBounds) {
paint.getTextBounds(string, 0, string.length(), sStringWidthBounds);
return sStringWidthBounds.width();
}
}
}