diff options
Diffstat (limited to 'java/src')
5 files changed, 100 insertions, 75 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index 9aaaff0c4..58bd845e1 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -88,9 +88,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke private float mSpacebarTextSize; private final int mSpacebarTextColor; private final int mSpacebarTextShadowColor; - // If the full language name needs to be smaller than this value to be drawn on space key, - // its short language name will be used instead. - private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f; + // The minimum x-scale to fit the language name on spacebar. + private static final float MINIMUM_XSCALE_OF_LANGUAGE_NAME = 0.8f; // Stuff to draw auto correction LED on spacebar. private boolean mAutoCorrectionSpacebarLedOn; private final boolean mAutoCorrectionSpacebarLedEnabled; @@ -898,47 +897,38 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke } } - // Compute width of text with specified text size using paint. - private int getTextWidth(Paint paint, String text, float textSize) { - paint.setTextSize(textSize); - return (int)getLabelWidth(text, paint); - } - - // Layout locale language name on spacebar. - private String layoutLanguageOnSpacebar(Paint paint, InputMethodSubtype subtype, int width, - float origTextSize) { - paint.setTextAlign(Align.CENTER); - paint.setTypeface(Typeface.DEFAULT); - // Estimate appropriate language name text size to fit in maxTextWidth. - String language = getFullDisplayName(subtype, getResources()); - int textWidth = getTextWidth(paint, language, origTextSize); - // Assuming text width and text size are proportional to each other. - float textSize = origTextSize * Math.min(width / textWidth, 1.0f); - // allow variable text size - textWidth = getTextWidth(paint, language, textSize); - // If text size goes too small or text does not fit, use middle or short name - final boolean useMiddleName = (textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME) - || (textWidth > width); - - final boolean useShortName; - if (useMiddleName) { - 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; + private boolean fitsTextIntoWidth(final int width, String text, Paint paint) { + paint.setTextScaleX(1.0f); + final float textWidth = getLabelWidth(text, paint); + if (textWidth < width) return true; + + final float scaleX = width / textWidth; + if (scaleX < MINIMUM_XSCALE_OF_LANGUAGE_NAME) return false; + + paint.setTextScaleX(scaleX); + return getLabelWidth(text, paint) < width; + } + + // Layout language name on spacebar. + private String layoutLanguageOnSpacebar(Paint paint, InputMethodSubtype subtype, + final int width) { + // Choose appropriate language name to fit into the width. + String text = getFullDisplayName(subtype, getResources()); + if (fitsTextIntoWidth(width, text, paint)) { + return text; + } + + text = getMiddleDisplayName(subtype); + if (fitsTextIntoWidth(width, text, paint)) { + return text; } - if (useShortName) { - language = getShortDisplayName(subtype); - textWidth = getTextWidth(paint, language, origTextSize); - textSize = origTextSize * Math.min(width / textWidth, 1.0f); + text = getShortDisplayName(subtype); + if (fitsTextIntoWidth(width, text, paint)) { + return text; } - paint.setTextSize(textSize); - return language; + return ""; } private void drawSpacebar(Key key, Canvas canvas, Paint paint) { @@ -947,11 +937,12 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke // If input language are explicitly selected. if (mNeedsToDisplayLanguage) { - final String language = layoutLanguageOnSpacebar( - paint, getKeyboard().mId.mSubtype, width, mSpacebarTextSize); + paint.setTextAlign(Align.CENTER); + 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 - // 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 textHeight = -paint.ascent() + descent; final float baseline = height / 2 + textHeight / 2; diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index 9dcffd4e2..3d89226c0 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -159,7 +159,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { // TODO: Create "cache dictionary" to cache fresh words for frequently updated dictionaries, // considering performance regression. protected void addWord(final String word, final int frequency) { - mFusionDictionary.add(word, frequency, null, null); + mFusionDictionary.add(word, frequency, null /* shortcutTargets */); } /** diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 804287309..f2d971ca4 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -182,12 +182,13 @@ public class SubtypeSwitcher { + newSubtype.getLocale() + "/" + newSubtype.getExtraValue() + ", from: " + mCurrentSubtype.getLocale() + "/" + mCurrentSubtype.getExtraValue()); } - if (newSubtype.equals(mCurrentSubtype)) return; final Locale newLocale = SubtypeLocale.getSubtypeLocale(newSubtype); mNeedsToDisplayLanguage.updateIsSystemLanguageSameAsInputLanguage( mCurrentSystemLocale.equals(newLocale)); + if (newSubtype.equals(mCurrentSubtype)) return; + mCurrentSubtype = newSubtype; updateShortcutIME(); mService.onRefreshKeyboard(); diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index cc98010fb..d82d503c4 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -131,6 +131,7 @@ public class BinaryDictInputOutput { // These options need to be the same numeric values as the one in the native reading code. private static final int GERMAN_UMLAUT_PROCESSING_FLAG = 0x1; private static final int FRENCH_LIGATURE_PROCESSING_FLAG = 0x4; + private static final int CONTAINS_BIGRAMS_FLAG = 0x8; // TODO: Make this value adaptative to content data, store it in the header, and // use it in the reading code. @@ -752,9 +753,12 @@ public class BinaryDictInputOutput { /** * Makes the 2-byte value for options flags. */ - private static final int makeOptionsValue(final DictionaryOptions options) { + private static final int makeOptionsValue(final FusionDictionary dictionary) { + final DictionaryOptions options = dictionary.mOptions; + final boolean hasBigrams = dictionary.hasBigrams(); return (options.mFrenchLigatureProcessing ? FRENCH_LIGATURE_PROCESSING_FLAG : 0) - + (options.mGermanUmlautProcessing ? GERMAN_UMLAUT_PROCESSING_FLAG : 0); + + (options.mGermanUmlautProcessing ? GERMAN_UMLAUT_PROCESSING_FLAG : 0) + + (hasBigrams ? CONTAINS_BIGRAMS_FLAG : 0); } /** @@ -970,7 +974,7 @@ public class BinaryDictInputOutput { headerBuffer.write((byte) (0xFF & version)); } // Options flags - final int options = makeOptionsValue(dict.mOptions); + final int options = makeOptionsValue(dict); headerBuffer.write((byte) (0xFF & (options >> 8))); headerBuffer.write((byte) (0xFF & options)); if (version >= FIRST_VERSION_WITH_HEADER_SIZE) { @@ -1317,8 +1321,16 @@ public class BinaryDictInputOutput { 0 != (optionsFlags & GERMAN_UMLAUT_PROCESSING_FLAG), 0 != (optionsFlags & FRENCH_LIGATURE_PROCESSING_FLAG))); if (null != dict) { - for (Word w : dict) { - newDict.add(w.mWord, w.mFrequency, w.mShortcutTargets, w.mBigrams); + for (final Word w : dict) { + newDict.add(w.mWord, w.mFrequency, w.mShortcutTargets); + } + for (final Word w : dict) { + // By construction a binary dictionary may not have bigrams pointing to + // words that are not also registered as unigrams so we don't have to avoid + // them explicitly here. + for (final WeightedString bigram : w.mBigrams) { + newDict.setBigram(w.mWord, bigram.mWord, bigram.mFrequency); + } } } diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index 40bcfc3aa..b08702e47 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -286,7 +286,7 @@ public class FusionDictionary implements Iterable<Word> { for (WeightedString word : words) { final CharGroup t = findWordInTree(mRoot, word.mWord); if (null == t) { - add(getCodePoints(word.mWord), 0, null, null); + add(getCodePoints(word.mWord), 0, null); } } } @@ -305,12 +305,8 @@ public class FusionDictionary implements Iterable<Word> { * @param bigrams a list of bigrams, or null. */ public void add(final String word, final int frequency, - final ArrayList<WeightedString> shortcutTargets, - final ArrayList<WeightedString> bigrams) { - if (null != bigrams) { - addNeutralWords(bigrams); - } - add(getCodePoints(word), frequency, shortcutTargets, bigrams); + final ArrayList<WeightedString> shortcutTargets) { + add(getCodePoints(word), frequency, shortcutTargets); } /** @@ -344,7 +340,7 @@ public class FusionDictionary implements Iterable<Word> { final CharGroup charGroup2 = findWordInTree(mRoot, word2); if (charGroup2 == null) { // TODO: refactor with the identical code in addNeutralWords - add(getCodePoints(word2), 0, null, null); + add(getCodePoints(word2), 0, null); } charGroup.addBigram(word2, frequency); } else { @@ -355,17 +351,15 @@ public class FusionDictionary implements Iterable<Word> { /** * Add a word to this dictionary. * - * The shortcuts and bigrams, if any, have to be in the dictionary already. If they aren't, + * The shortcuts, if any, have to be in the dictionary already. If they aren't, * an exception is thrown. * * @param word the word, as an int array. * @param frequency the frequency of the word, in the range [0..255]. * @param shortcutTargets an optional list of shortcut targets for this word (null if none). - * @param bigrams an optional list of bigrams for this word (null if none). */ private void add(final int[] word, final int frequency, - final ArrayList<WeightedString> shortcutTargets, - final ArrayList<WeightedString> bigrams) { + final ArrayList<WeightedString> shortcutTargets) { assert(frequency >= 0 && frequency <= 255); Node currentNode = mRoot; int charIndex = 0; @@ -390,7 +384,7 @@ public class FusionDictionary implements Iterable<Word> { final int insertionIndex = findInsertionIndex(currentNode, word[charIndex]); final CharGroup newGroup = new CharGroup( Arrays.copyOfRange(word, charIndex, word.length), - shortcutTargets, bigrams, frequency); + shortcutTargets, null /* bigrams */, frequency); currentNode.mData.add(insertionIndex, newGroup); checkStack(currentNode); } else { @@ -400,21 +394,21 @@ public class FusionDictionary implements Iterable<Word> { // The new word is a prefix of an existing word, but the node on which it // should end already exists as is. Since the old CharNode was not a terminal, // make it one by filling in its frequency and other attributes - currentGroup.update(frequency, shortcutTargets, bigrams); + currentGroup.update(frequency, shortcutTargets, null); } else { // The new word matches the full old word and extends past it. // We only have to create a new node and add it to the end of this. final CharGroup newNode = new CharGroup( Arrays.copyOfRange(word, charIndex + differentCharIndex, word.length), - shortcutTargets, bigrams, frequency); + shortcutTargets, null /* bigrams */, frequency); currentGroup.mChildren = new Node(); currentGroup.mChildren.mData.add(newNode); } } else { if (0 == differentCharIndex) { // Exact same word. Update the frequency if higher. This will also add the - // new bigrams to the existing bigram list if it already exists. - currentGroup.update(frequency, shortcutTargets, bigrams); + // new shortcuts to the existing shortcut list if it already exists. + currentGroup.update(frequency, shortcutTargets, null); } else { // Partial prefix match only. We have to replace the current node with a node // containing the current prefix and create two new ones for the tails. @@ -429,14 +423,14 @@ public class FusionDictionary implements Iterable<Word> { if (charIndex + differentCharIndex >= word.length) { newParent = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, 0, differentCharIndex), - shortcutTargets, bigrams, frequency, newChildren); + shortcutTargets, null /* bigrams */, frequency, newChildren); } else { newParent = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, 0, differentCharIndex), - null, null, -1, newChildren); - final CharGroup newWord = new CharGroup( - Arrays.copyOfRange(word, charIndex + differentCharIndex, - word.length), shortcutTargets, bigrams, frequency); + null /* shortcutTargets */, null /* bigrams */, -1, newChildren); + final CharGroup newWord = new CharGroup(Arrays.copyOfRange(word, + charIndex + differentCharIndex, word.length), + shortcutTargets, null /* bigrams */, frequency); final int addIndex = word[charIndex + differentCharIndex] > currentGroup.mChars[differentCharIndex] ? 1 : 0; newChildren.mData.add(addIndex, newWord); @@ -494,7 +488,8 @@ public class FusionDictionary implements Iterable<Word> { */ private static int findInsertionIndex(final Node node, int character) { final ArrayList<CharGroup> data = node.mData; - final CharGroup reference = new CharGroup(new int[] { character }, null, null, 0); + final CharGroup reference = new CharGroup(new int[] { character }, + null /* shortcutTargets */, null /* bigrams */, 0); int result = Collections.binarySearch(data, reference, CHARGROUP_COMPARATOR); return result >= 0 ? result : -result - 1; } @@ -568,7 +563,7 @@ public class FusionDictionary implements Iterable<Word> { * Recursively count the number of nodes in a given branch of the trie. * * @param node the node to count. - * @result the number of nodes in this branch. + * @return the number of nodes in this branch. */ public static int countNodes(final Node node) { int size = 1; @@ -580,6 +575,32 @@ public class FusionDictionary implements Iterable<Word> { return size; } + // Recursively find out whether there are any bigrams. + // This can be pretty expensive especially if there aren't any (we return as soon + // as we find one, so it's much cheaper if there are bigrams) + private static boolean hasBigramsInternal(final Node node) { + if (null == node) return false; + for (int i = node.mData.size() - 1; i >= 0; --i) { + CharGroup group = node.mData.get(i); + if (null != group.mBigrams) return true; + if (hasBigramsInternal(group.mChildren)) return true; + } + return false; + } + + /** + * Finds out whether there are any bigrams in this dictionary. + * + * @return true if there is any bigram, false otherwise. + */ + // TODO: this is expensive especially for large dictionaries without any bigram. + // The up side is, this is always accurate and correct and uses no memory. We should + // find a more efficient way of doing this, without compromising too much on memory + // and ease of use. + public boolean hasBigrams() { + return hasBigramsInternal(mRoot); + } + // Historically, the tails of the words were going to be merged to save space. // However, that would prevent the code to search for a specific address in log(n) // time so this was abandoned. |