diff options
Diffstat (limited to 'java/src')
3 files changed, 56 insertions, 75 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 665d9f7a1..af54fb674 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -58,9 +58,6 @@ public class Key implements Comparable<Key> { private final String mHintLabel; /** Flags of the label */ private final int mLabelFlags; - private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01; - private static final int LABEL_FLAGS_ALIGN_RIGHT = 0x02; - private static final int LABEL_FLAGS_ALIGN_BUTTOM = 0x04; private static final int LABEL_FLAGS_ALIGN_LEFT_OF_CENTER = 0x08; // Font typeface specification. private static final int LABEL_FLAGS_FONT_MASK = 0x30; @@ -78,8 +75,6 @@ public class Key implements Comparable<Key> { private static final int LABEL_FLAGS_HAS_POPUP_HINT = 0x200; private static final int LABEL_FLAGS_HAS_SHIFTED_LETTER_HINT = 0x400; 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. @@ -646,18 +641,6 @@ public class Key implements Comparable<Key> { return Typeface.DEFAULT_BOLD; } - public final boolean isAlignLeft() { - return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT) != 0; - } - - public final boolean isAlignRight() { - return (mLabelFlags & LABEL_FLAGS_ALIGN_RIGHT) != 0; - } - - public final boolean isAlignButtom() { - return (mLabelFlags & LABEL_FLAGS_ALIGN_BUTTOM) != 0; - } - public final boolean isAlignLeftOfCenter() { return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT_OF_CENTER) != 0; } @@ -675,14 +658,6 @@ public class Key implements Comparable<Key> { return (mLabelFlags & LABEL_FLAGS_HAS_HINT_LABEL) != 0; } - public final boolean hasLabelWithIconLeft() { - return (mLabelFlags & LABEL_FLAGS_WITH_ICON_LEFT) != 0; - } - - public final boolean hasLabelWithIconRight() { - return (mLabelFlags & LABEL_FLAGS_WITH_ICON_RIGHT) != 0; - } - public final boolean needsAutoXScale() { return (mLabelFlags & LABEL_FLAGS_AUTO_X_SCALE) != 0; } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index c4ca1c495..f967f620a 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -47,7 +47,6 @@ import java.util.HashSet; * @attr ref R.styleable#KeyboardView_functionalKeyBackground * @attr ref R.styleable#KeyboardView_spacebarBackground * @attr ref R.styleable#KeyboardView_spacebarIconWidthRatio - * @attr ref R.styleable#KeyboardView_keyLabelHorizontalPadding * @attr ref R.styleable#KeyboardView_keyHintLetterPadding * @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding * @attr ref R.styleable#KeyboardView_keyShiftedLetterHintPadding @@ -74,7 +73,6 @@ import java.util.HashSet; public class KeyboardView extends View { // XML attributes private final KeyVisualAttributes mKeyVisualAttributes; - private final int mKeyLabelHorizontalPadding; private final float mKeyHintLetterPadding; private final float mKeyPopupHintLetterPadding; private final float mKeyShiftedLetterHintPadding; @@ -90,11 +88,6 @@ public class KeyboardView extends View { // HORIZONTAL ELLIPSIS "...", character for popup hint. private static final String POPUP_HINT_CHAR = "\u2026"; - // Margin between the label and the icon on a key that has both of them. - // Specified by the fraction of the key width. - // TODO: Use resource parameter for this value. - private static final float LABEL_ICON_MARGIN = 0.05f; - // The maximum key label width in the proportion to the key width. private static final float MAX_LABEL_RATIO = 0.90f; @@ -137,8 +130,6 @@ public class KeyboardView extends View { mSpacebarBackground = (spacebarBackground != null) ? spacebarBackground : mKeyBackground; mSpacebarIconWidthRatio = keyboardViewAttr.getFloat( R.styleable.KeyboardView_spacebarIconWidthRatio, 1.0f); - mKeyLabelHorizontalPadding = keyboardViewAttr.getDimensionPixelOffset( - R.styleable.KeyboardView_keyLabelHorizontalPadding, 0); mKeyHintLetterPadding = keyboardViewAttr.getDimension( R.styleable.KeyboardView_keyHintLetterPadding, 0.0f); mKeyPopupHintLetterPadding = keyboardViewAttr.getDimension( @@ -376,27 +367,10 @@ public class KeyboardView extends View { final float baseline = centerY + labelCharHeight / 2.0f; // Horizontal label text alignment - float labelWidth = 0.0f; - if (key.isAlignLeft()) { - positionX = mKeyLabelHorizontalPadding; - paint.setTextAlign(Align.LEFT); - } else if (key.isAlignRight()) { - positionX = keyWidth - mKeyLabelHorizontalPadding; - paint.setTextAlign(Align.RIGHT); - } else if (key.isAlignLeftOfCenter()) { + if (key.isAlignLeftOfCenter()) { // TODO: Parameterise this? positionX = centerX - labelCharWidth * 7.0f / 4.0f; paint.setTextAlign(Align.LEFT); - } else if (key.hasLabelWithIconLeft() && icon != null) { - 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.getStringWidth(label, paint) + icon.getIntrinsicWidth() - + LABEL_ICON_MARGIN * keyWidth; - positionX = centerX - labelWidth / 2.0f; - paint.setTextAlign(Align.LEFT); } else { positionX = centerX; paint.setTextAlign(Align.CENTER); @@ -430,19 +404,6 @@ public class KeyboardView extends View { // Turn off drop shadow and reset x-scale. paint.clearShadowLayer(); paint.setTextScaleX(1.0f); - - if (icon != null) { - final int iconWidth = icon.getIntrinsicWidth(); - final int iconHeight = icon.getIntrinsicHeight(); - final int iconY = (keyHeight - iconHeight) / 2; - if (key.hasLabelWithIconLeft()) { - final int iconX = (int)(centerX - labelWidth / 2.0f); - drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); - } else if (key.hasLabelWithIconRight()) { - final int iconX = (int)(centerX + labelWidth / 2.0f - iconWidth); - drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); - } - } } // Draw hint label. @@ -493,16 +454,9 @@ public class KeyboardView extends View { iconWidth = Math.min(icon.getIntrinsicWidth(), keyWidth); } final int iconHeight = icon.getIntrinsicHeight(); - final int iconY = key.isAlignButtom() ? keyHeight - iconHeight - : (keyHeight - iconHeight) / 2; - final int iconX; - if (key.isAlignLeft()) { - iconX = mKeyLabelHorizontalPadding; - } else if (key.isAlignRight()) { - iconX = keyWidth - mKeyLabelHorizontalPadding - iconWidth; - } else { // Align center - iconX = (keyWidth - iconWidth) / 2; - } + // Align center. + final int iconY = (keyHeight - iconHeight) / 2; + final int iconX = (keyWidth - iconWidth) / 2; drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); } diff --git a/java/src/com/android/inputmethod/latin/utils/StringUtils.java b/java/src/com/android/inputmethod/latin/utils/StringUtils.java index ceb038371..38f0b3fee 100644 --- a/java/src/com/android/inputmethod/latin/utils/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/StringUtils.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin.utils; import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; +import android.text.Spanned; import android.text.TextUtils; import com.android.inputmethod.annotations.UsedForTesting; @@ -26,6 +27,8 @@ import com.android.inputmethod.latin.Constants; import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public final class StringUtils { public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case @@ -503,6 +506,55 @@ public final class StringUtils { return lastIndex - i; } + /** + * Splits the given {@code charSequence} with at occurrences of the given {@code regex}. + * <p> + * This is equivalent to + * {@code charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0)} + * except that the spans are preserved in the result array. + * </p> + * @param input the character sequence to be split. + * @param regex the regex pattern to be used as the separator. + * @param preserveTrailingEmptySegments {@code true} to preserve the trailing empty + * segments. Otherwise, trailing empty segments will be removed before being returned. + * @return the array which contains the result. All the spans in the {@param input} is + * preserved. + */ + @UsedForTesting + public static CharSequence[] split(final CharSequence charSequence, final String regex, + final boolean preserveTrailingEmptySegments) { + // A short-cut for non-spanned strings. + if (!(charSequence instanceof Spanned)) { + // -1 means that trailing empty segments will be preserved. + return charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0); + } + + // Hereafter, emulate String.split for CharSequence. + final ArrayList<CharSequence> sequences = new ArrayList<>(); + final Matcher matcher = Pattern.compile(regex).matcher(charSequence); + int nextStart = 0; + boolean matched = false; + while (matcher.find()) { + sequences.add(charSequence.subSequence(nextStart, matcher.start())); + nextStart = matcher.end(); + matched = true; + } + if (!matched) { + // never matched. preserveTrailingEmptySegments is ignored in this case. + return new CharSequence[] { charSequence }; + } + sequences.add(charSequence.subSequence(nextStart, charSequence.length())); + if (!preserveTrailingEmptySegments) { + for (int i = sequences.size() - 1; i >= 0; --i) { + if (!TextUtils.isEmpty(sequences.get(i))) { + break; + } + sequences.remove(i); + } + } + return sequences.toArray(new CharSequence[sequences.size()]); + } + @UsedForTesting public static class Stringizer<E> { public String stringize(final E element) { |