Merge "Move reference char/digit width/height method to TypefaceUtils"
This commit is contained in:
commit
6bc32968b9
5 changed files with 43 additions and 38 deletions
|
@ -113,9 +113,6 @@ public class KeyboardView extends View {
|
||||||
private final Canvas mOffscreenCanvas = new Canvas();
|
private final Canvas mOffscreenCanvas = new Canvas();
|
||||||
private final Paint mPaint = new Paint();
|
private final Paint mPaint = new Paint();
|
||||||
private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
|
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) {
|
public KeyboardView(final Context context, final AttributeSet attrs) {
|
||||||
this(context, attrs, R.attr.keyboardViewStyle);
|
this(context, attrs, R.attr.keyboardViewStyle);
|
||||||
}
|
}
|
||||||
|
@ -370,10 +367,8 @@ public class KeyboardView extends View {
|
||||||
if (label != null) {
|
if (label != null) {
|
||||||
paint.setTypeface(key.selectTypeface(params));
|
paint.setTypeface(key.selectTypeface(params));
|
||||||
paint.setTextSize(key.selectTextSize(params));
|
paint.setTextSize(key.selectTextSize(params));
|
||||||
final float labelCharHeight = TypefaceUtils.getCharHeight(
|
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
|
||||||
KEY_LABEL_REFERENCE_CHAR, paint);
|
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
|
||||||
final float labelCharWidth = TypefaceUtils.getCharWidth(
|
|
||||||
KEY_LABEL_REFERENCE_CHAR, paint);
|
|
||||||
|
|
||||||
// Vertical label text alignment.
|
// Vertical label text alignment.
|
||||||
final float baseline = centerY + labelCharHeight / 2.0f;
|
final float baseline = centerY + labelCharHeight / 2.0f;
|
||||||
|
@ -391,12 +386,12 @@ public class KeyboardView extends View {
|
||||||
positionX = centerX - labelCharWidth * 7.0f / 4.0f;
|
positionX = centerX - labelCharWidth * 7.0f / 4.0f;
|
||||||
paint.setTextAlign(Align.LEFT);
|
paint.setTextAlign(Align.LEFT);
|
||||||
} else if (key.hasLabelWithIconLeft() && icon != null) {
|
} else if (key.hasLabelWithIconLeft() && icon != null) {
|
||||||
labelWidth = TypefaceUtils.getLabelWidth(label, paint) + icon.getIntrinsicWidth()
|
labelWidth = TypefaceUtils.getStringWidth(label, paint) + icon.getIntrinsicWidth()
|
||||||
+ LABEL_ICON_MARGIN * keyWidth;
|
+ LABEL_ICON_MARGIN * keyWidth;
|
||||||
positionX = centerX + labelWidth / 2.0f;
|
positionX = centerX + labelWidth / 2.0f;
|
||||||
paint.setTextAlign(Align.RIGHT);
|
paint.setTextAlign(Align.RIGHT);
|
||||||
} else if (key.hasLabelWithIconRight() && icon != null) {
|
} else if (key.hasLabelWithIconRight() && icon != null) {
|
||||||
labelWidth = TypefaceUtils.getLabelWidth(label, paint) + icon.getIntrinsicWidth()
|
labelWidth = TypefaceUtils.getStringWidth(label, paint) + icon.getIntrinsicWidth()
|
||||||
+ LABEL_ICON_MARGIN * keyWidth;
|
+ LABEL_ICON_MARGIN * keyWidth;
|
||||||
positionX = centerX - labelWidth / 2.0f;
|
positionX = centerX - labelWidth / 2.0f;
|
||||||
paint.setTextAlign(Align.LEFT);
|
paint.setTextAlign(Align.LEFT);
|
||||||
|
@ -406,7 +401,7 @@ public class KeyboardView extends View {
|
||||||
}
|
}
|
||||||
if (key.needsAutoXScale()) {
|
if (key.needsAutoXScale()) {
|
||||||
final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) /
|
final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) /
|
||||||
TypefaceUtils.getLabelWidth(label, paint));
|
TypefaceUtils.getStringWidth(label, paint));
|
||||||
if (key.needsAutoScale()) {
|
if (key.needsAutoScale()) {
|
||||||
final float autoSize = paint.getTextSize() * ratio;
|
final float autoSize = paint.getTextSize() * ratio;
|
||||||
paint.setTextSize(autoSize);
|
paint.setTextSize(autoSize);
|
||||||
|
@ -457,32 +452,28 @@ public class KeyboardView extends View {
|
||||||
// TODO: Should add a way to specify type face for hint letters
|
// TODO: Should add a way to specify type face for hint letters
|
||||||
paint.setTypeface(Typeface.DEFAULT_BOLD);
|
paint.setTypeface(Typeface.DEFAULT_BOLD);
|
||||||
blendAlpha(paint, params.mAnimAlpha);
|
blendAlpha(paint, params.mAnimAlpha);
|
||||||
|
final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint);
|
||||||
|
final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint);
|
||||||
final float hintX, hintY;
|
final float hintX, hintY;
|
||||||
if (key.hasHintLabel()) {
|
if (key.hasHintLabel()) {
|
||||||
// The hint label is placed just right of the key label. Used mainly on
|
// The hint label is placed just right of the key label. Used mainly on
|
||||||
// "phone number" layout.
|
// "phone number" layout.
|
||||||
// TODO: Generalize the following calculations.
|
// TODO: Generalize the following calculations.
|
||||||
hintX = positionX
|
hintX = positionX + labelCharWidth * 2.0f;
|
||||||
+ TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) * 2.0f;
|
hintY = centerY + labelCharHeight / 2.0f;
|
||||||
hintY = centerY
|
|
||||||
+ TypefaceUtils.getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
|
|
||||||
paint.setTextAlign(Align.LEFT);
|
paint.setTextAlign(Align.LEFT);
|
||||||
} else if (key.hasShiftedLetterHint()) {
|
} else if (key.hasShiftedLetterHint()) {
|
||||||
// The hint label is placed at top-right corner of the key. Used mainly on tablet.
|
// The hint label is placed at top-right corner of the key. Used mainly on tablet.
|
||||||
hintX = keyWidth - mKeyShiftedLetterHintPadding
|
hintX = keyWidth - mKeyShiftedLetterHintPadding - labelCharWidth / 2.0f;
|
||||||
- TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
|
|
||||||
paint.getFontMetrics(mFontMetrics);
|
paint.getFontMetrics(mFontMetrics);
|
||||||
hintY = -mFontMetrics.top;
|
hintY = -mFontMetrics.top;
|
||||||
paint.setTextAlign(Align.CENTER);
|
paint.setTextAlign(Align.CENTER);
|
||||||
} else { // key.hasHintLetter()
|
} else { // key.hasHintLetter()
|
||||||
// The hint letter is placed at top-right corner of the key. Used mainly on phone.
|
// The hint letter is placed at top-right corner of the key. Used mainly on phone.
|
||||||
final float keyNumericHintLabelReferenceCharWidth =
|
final float hintDigitWidth = TypefaceUtils.getReferenceDigitWidth(paint);
|
||||||
TypefaceUtils.getCharWidth(KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR, paint);
|
final float hintLabelWidth = TypefaceUtils.getStringWidth(hintLabel, paint);
|
||||||
final float keyHintLabelStringWidth =
|
|
||||||
TypefaceUtils.getStringWidth(hintLabel, paint);
|
|
||||||
hintX = keyWidth - mKeyHintLetterPadding
|
hintX = keyWidth - mKeyHintLetterPadding
|
||||||
- Math.max(keyNumericHintLabelReferenceCharWidth, keyHintLabelStringWidth)
|
- Math.max(hintDigitWidth, hintLabelWidth) / 2.0f;
|
||||||
/ 2.0f;
|
|
||||||
hintY = -paint.ascent();
|
hintY = -paint.ascent();
|
||||||
paint.setTextAlign(Align.CENTER);
|
paint.setTextAlign(Align.CENTER);
|
||||||
}
|
}
|
||||||
|
@ -536,7 +527,7 @@ public class KeyboardView extends View {
|
||||||
paint.setColor(params.mHintLabelColor);
|
paint.setColor(params.mHintLabelColor);
|
||||||
paint.setTextAlign(Align.CENTER);
|
paint.setTextAlign(Align.CENTER);
|
||||||
final float hintX = keyWidth - mKeyHintLetterPadding
|
final float hintX = keyWidth - mKeyHintLetterPadding
|
||||||
- TypefaceUtils.getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2.0f;
|
- TypefaceUtils.getReferenceCharWidth(paint) / 2.0f;
|
||||||
final float hintY = keyHeight - mKeyPopupHintLetterPadding;
|
final float hintY = keyHeight - mKeyPopupHintLetterPadding;
|
||||||
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);
|
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);
|
||||||
|
|
||||||
|
|
|
@ -1193,7 +1193,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
|
||||||
private boolean fitsTextIntoWidth(final int width, final String text, final Paint paint) {
|
private boolean fitsTextIntoWidth(final int width, final String text, final Paint paint) {
|
||||||
final int maxTextWidth = width - mLanguageOnSpacebarHorizontalMargin * 2;
|
final int maxTextWidth = width - mLanguageOnSpacebarHorizontalMargin * 2;
|
||||||
paint.setTextScaleX(1.0f);
|
paint.setTextScaleX(1.0f);
|
||||||
final float textWidth = TypefaceUtils.getLabelWidth(text, paint);
|
final float textWidth = TypefaceUtils.getStringWidth(text, paint);
|
||||||
if (textWidth < width) {
|
if (textWidth < width) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1204,7 +1204,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
|
||||||
}
|
}
|
||||||
|
|
||||||
paint.setTextScaleX(scaleX);
|
paint.setTextScaleX(scaleX);
|
||||||
return TypefaceUtils.getLabelWidth(text, paint) < maxTextWidth;
|
return TypefaceUtils.getStringWidth(text, paint) < maxTextWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Layout language name on spacebar.
|
// Layout language name on spacebar.
|
||||||
|
|
|
@ -327,7 +327,7 @@ public final class MoreKeysKeyboard extends Keyboard {
|
||||||
// If the label is single letter, minKeyWidth is enough to hold the label.
|
// If the label is single letter, minKeyWidth is enough to hold the label.
|
||||||
if (label != null && StringUtils.codePointCount(label) > 1) {
|
if (label != null && StringUtils.codePointCount(label) > 1) {
|
||||||
maxWidth = Math.max(maxWidth,
|
maxWidth = Math.max(maxWidth,
|
||||||
(int)(TypefaceUtils.getLabelWidth(label, paint) + padding));
|
(int)(TypefaceUtils.getStringWidth(label, paint) + padding));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return maxWidth;
|
return maxWidth;
|
||||||
|
|
|
@ -75,7 +75,7 @@ public final class MoreSuggestions extends Keyboard {
|
||||||
while (index < size) {
|
while (index < size) {
|
||||||
final String word = suggestedWords.getWord(index);
|
final String word = suggestedWords.getWord(index);
|
||||||
// TODO: Should take care of text x-scaling.
|
// 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 numColumn = index - rowStartIndex + 1;
|
||||||
final int columnWidth =
|
final int columnWidth =
|
||||||
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;
|
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;
|
||||||
|
|
|
@ -22,6 +22,9 @@ import android.graphics.Typeface;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
public final class TypefaceUtils {
|
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() {
|
private TypefaceUtils() {
|
||||||
// This utility class is not publicly instantiable.
|
// This utility class is not publicly instantiable.
|
||||||
}
|
}
|
||||||
|
@ -31,7 +34,7 @@ public final class TypefaceUtils {
|
||||||
// Working variable for the following method.
|
// Working variable for the following method.
|
||||||
private static final Rect sTextHeightBounds = new Rect();
|
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);
|
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
|
||||||
synchronized (sTextHeightCache) {
|
synchronized (sTextHeightCache) {
|
||||||
final Float cachedValue = sTextHeightCache.get(key);
|
final Float cachedValue = sTextHeightCache.get(key);
|
||||||
|
@ -51,7 +54,7 @@ public final class TypefaceUtils {
|
||||||
// Working variable for the following method.
|
// Working variable for the following method.
|
||||||
private static final Rect sTextWidthBounds = new Rect();
|
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);
|
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
|
||||||
synchronized (sTextWidthCache) {
|
synchronized (sTextWidthCache) {
|
||||||
final Float cachedValue = sTextWidthCache.get(key);
|
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) {
|
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
|
||||||
final int labelSize = (int)paint.getTextSize();
|
final int labelSize = (int)paint.getTextSize();
|
||||||
final Typeface face = paint.getTypeface();
|
final Typeface face = paint.getTypeface();
|
||||||
|
@ -86,9 +84,25 @@ public final class TypefaceUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float getLabelWidth(final String label, final Paint paint) {
|
public static float getReferenceCharHeight(final Paint paint) {
|
||||||
final Rect textBounds = new Rect();
|
return getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint);
|
||||||
paint.getTextBounds(label, 0, label.length(), textBounds);
|
}
|
||||||
return textBounds.width();
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue