Skip to content
Snippets Groups Projects
Commit bd2ca9c0 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Fix potential "divided by zero" exception

This change also refactors the language name selection to use text
x-scale.

Bug: 6396854
Change-Id: I31249a85bd042a93d627f40413161aef13617c87
parent 27b42ced
No related branches found
No related tags found
No related merge requests found
...@@ -88,9 +88,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke ...@@ -88,9 +88,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
private float mSpacebarTextSize; private float mSpacebarTextSize;
private final int mSpacebarTextColor; private final int mSpacebarTextColor;
private final int mSpacebarTextShadowColor; private final int mSpacebarTextShadowColor;
// If the full language name needs to be smaller than this value to be drawn on space key, // The minimum x-scale to fit the language name on spacebar.
// its short language name will be used instead. private static final float MINIMUM_XSCALE_OF_LANGUAGE_NAME = 0.8f;
private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f;
// Stuff to draw auto correction LED on spacebar. // Stuff to draw auto correction LED on spacebar.
private boolean mAutoCorrectionSpacebarLedOn; private boolean mAutoCorrectionSpacebarLedOn;
private final boolean mAutoCorrectionSpacebarLedEnabled; private final boolean mAutoCorrectionSpacebarLedEnabled;
...@@ -898,47 +897,38 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke ...@@ -898,47 +897,38 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
} }
} }
// Compute width of text with specified text size using paint. private boolean fitsTextIntoWidth(final int width, String text, Paint paint) {
private int getTextWidth(Paint paint, String text, float textSize) { paint.setTextScaleX(1.0f);
paint.setTextSize(textSize); final float textWidth = getLabelWidth(text, paint);
return (int)getLabelWidth(text, paint); if (textWidth < width) return true;
}
final float scaleX = width / textWidth;
// Layout locale language name on spacebar. if (scaleX < MINIMUM_XSCALE_OF_LANGUAGE_NAME) return false;
private String layoutLanguageOnSpacebar(Paint paint, InputMethodSubtype subtype, int width,
float origTextSize) { paint.setTextScaleX(scaleX);
paint.setTextAlign(Align.CENTER); return getLabelWidth(text, paint) < width;
paint.setTypeface(Typeface.DEFAULT); }
// Estimate appropriate language name text size to fit in maxTextWidth.
String language = getFullDisplayName(subtype, getResources()); // Layout language name on spacebar.
int textWidth = getTextWidth(paint, language, origTextSize); private String layoutLanguageOnSpacebar(Paint paint, InputMethodSubtype subtype,
// Assuming text width and text size are proportional to each other. final int width) {
float textSize = origTextSize * Math.min(width / textWidth, 1.0f); // Choose appropriate language name to fit into the width.
// allow variable text size String text = getFullDisplayName(subtype, getResources());
textWidth = getTextWidth(paint, language, textSize); if (fitsTextIntoWidth(width, text, paint)) {
// If text size goes too small or text does not fit, use middle or short name return text;
final boolean useMiddleName = (textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME) }
|| (textWidth > width);
text = getMiddleDisplayName(subtype);
final boolean useShortName; if (fitsTextIntoWidth(width, text, paint)) {
if (useMiddleName) { return text;
language = getMiddleDisplayName(subtype);
textWidth = getTextWidth(paint, language, origTextSize);
textSize = origTextSize * Math.min(width / textWidth, 1.0f);
useShortName = (textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME)
|| (textWidth > width);
} else {
useShortName = false;
} }
if (useShortName) { text = getShortDisplayName(subtype);
language = getShortDisplayName(subtype); if (fitsTextIntoWidth(width, text, paint)) {
textWidth = getTextWidth(paint, language, origTextSize); return text;
textSize = origTextSize * Math.min(width / textWidth, 1.0f);
} }
paint.setTextSize(textSize);
return language; return "";
} }
private void drawSpacebar(Key key, Canvas canvas, Paint paint) { private void drawSpacebar(Key key, Canvas canvas, Paint paint) {
...@@ -947,11 +937,12 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke ...@@ -947,11 +937,12 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
// If input language are explicitly selected. // If input language are explicitly selected.
if (mNeedsToDisplayLanguage) { if (mNeedsToDisplayLanguage) {
final String language = layoutLanguageOnSpacebar( paint.setTextAlign(Align.CENTER);
paint, getKeyboard().mId.mSubtype, width, mSpacebarTextSize); paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(mSpacebarTextSize);
final InputMethodSubtype subtype = getKeyboard().mId.mSubtype;
final String language = layoutLanguageOnSpacebar(paint, subtype, width);
// Draw language text with shadow // Draw language text with shadow
// In case there is no space icon, we will place the language text at the center of
// spacebar.
final float descent = paint.descent(); final float descent = paint.descent();
final float textHeight = -paint.ascent() + descent; final float textHeight = -paint.ascent() + descent;
final float baseline = height / 2 + textHeight / 2; final float baseline = height / 2 + textHeight / 2;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment