1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.keyboard.internal; 18 19import android.graphics.Typeface; 20 21import com.android.inputmethod.latin.utils.ResourceUtils; 22 23import javax.annotation.Nonnull; 24import javax.annotation.Nullable; 25 26public final class KeyDrawParams { 27 @Nonnull 28 public Typeface mTypeface = Typeface.DEFAULT; 29 30 public int mLetterSize; 31 public int mLabelSize; 32 public int mLargeLetterSize; 33 public int mHintLetterSize; 34 public int mShiftedLetterHintSize; 35 public int mHintLabelSize; 36 public int mPreviewTextSize; 37 38 public int mTextColor; 39 public int mTextInactivatedColor; 40 public int mTextShadowColor; 41 public int mFunctionalTextColor; 42 public int mHintLetterColor; 43 public int mHintLabelColor; 44 public int mShiftedLetterHintInactivatedColor; 45 public int mShiftedLetterHintActivatedColor; 46 public int mPreviewTextColor; 47 48 public float mHintLabelVerticalAdjustment; 49 public float mLabelOffCenterRatio; 50 public float mHintLabelOffCenterRatio; 51 52 public int mAnimAlpha; 53 54 public KeyDrawParams() {} 55 56 private KeyDrawParams(@Nonnull final KeyDrawParams copyFrom) { 57 mTypeface = copyFrom.mTypeface; 58 59 mLetterSize = copyFrom.mLetterSize; 60 mLabelSize = copyFrom.mLabelSize; 61 mLargeLetterSize = copyFrom.mLargeLetterSize; 62 mHintLetterSize = copyFrom.mHintLetterSize; 63 mShiftedLetterHintSize = copyFrom.mShiftedLetterHintSize; 64 mHintLabelSize = copyFrom.mHintLabelSize; 65 mPreviewTextSize = copyFrom.mPreviewTextSize; 66 67 mTextColor = copyFrom.mTextColor; 68 mTextInactivatedColor = copyFrom.mTextInactivatedColor; 69 mTextShadowColor = copyFrom.mTextShadowColor; 70 mFunctionalTextColor = copyFrom.mFunctionalTextColor; 71 mHintLetterColor = copyFrom.mHintLetterColor; 72 mHintLabelColor = copyFrom.mHintLabelColor; 73 mShiftedLetterHintInactivatedColor = copyFrom.mShiftedLetterHintInactivatedColor; 74 mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor; 75 mPreviewTextColor = copyFrom.mPreviewTextColor; 76 77 mHintLabelVerticalAdjustment = copyFrom.mHintLabelVerticalAdjustment; 78 mLabelOffCenterRatio = copyFrom.mLabelOffCenterRatio; 79 mHintLabelOffCenterRatio = copyFrom.mHintLabelOffCenterRatio; 80 81 mAnimAlpha = copyFrom.mAnimAlpha; 82 } 83 84 public void updateParams(final int keyHeight, @Nullable final KeyVisualAttributes attr) { 85 if (attr == null) { 86 return; 87 } 88 89 if (attr.mTypeface != null) { 90 mTypeface = attr.mTypeface; 91 } 92 93 mLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight, 94 attr.mLetterSize, attr.mLetterRatio, mLetterSize); 95 mLabelSize = selectTextSizeFromDimensionOrRatio(keyHeight, 96 attr.mLabelSize, attr.mLabelRatio, mLabelSize); 97 mLargeLetterSize = selectTextSize(keyHeight, attr.mLargeLetterRatio, mLargeLetterSize); 98 mHintLetterSize = selectTextSize(keyHeight, attr.mHintLetterRatio, mHintLetterSize); 99 mShiftedLetterHintSize = selectTextSize(keyHeight, 100 attr.mShiftedLetterHintRatio, mShiftedLetterHintSize); 101 mHintLabelSize = selectTextSize(keyHeight, attr.mHintLabelRatio, mHintLabelSize); 102 mPreviewTextSize = selectTextSize(keyHeight, attr.mPreviewTextRatio, mPreviewTextSize); 103 104 mTextColor = selectColor(attr.mTextColor, mTextColor); 105 mTextInactivatedColor = selectColor(attr.mTextInactivatedColor, mTextInactivatedColor); 106 mTextShadowColor = selectColor(attr.mTextShadowColor, mTextShadowColor); 107 mFunctionalTextColor = selectColor(attr.mFunctionalTextColor, mFunctionalTextColor); 108 mHintLetterColor = selectColor(attr.mHintLetterColor, mHintLetterColor); 109 mHintLabelColor = selectColor(attr.mHintLabelColor, mHintLabelColor); 110 mShiftedLetterHintInactivatedColor = selectColor( 111 attr.mShiftedLetterHintInactivatedColor, mShiftedLetterHintInactivatedColor); 112 mShiftedLetterHintActivatedColor = selectColor( 113 attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor); 114 mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor); 115 116 mHintLabelVerticalAdjustment = selectFloatIfNonZero( 117 attr.mHintLabelVerticalAdjustment, mHintLabelVerticalAdjustment); 118 mLabelOffCenterRatio = selectFloatIfNonZero( 119 attr.mLabelOffCenterRatio, mLabelOffCenterRatio); 120 mHintLabelOffCenterRatio = selectFloatIfNonZero( 121 attr.mHintLabelOffCenterRatio, mHintLabelOffCenterRatio); 122 } 123 124 @Nonnull 125 public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight, 126 @Nullable final KeyVisualAttributes attr) { 127 if (attr == null) { 128 return this; 129 } 130 final KeyDrawParams newParams = new KeyDrawParams(this); 131 newParams.updateParams(keyHeight, attr); 132 return newParams; 133 } 134 135 private static int selectTextSizeFromDimensionOrRatio(final int keyHeight, 136 final int dimens, final float ratio, final int defaultDimens) { 137 if (ResourceUtils.isValidDimensionPixelSize(dimens)) { 138 return dimens; 139 } 140 if (ResourceUtils.isValidFraction(ratio)) { 141 return (int)(keyHeight * ratio); 142 } 143 return defaultDimens; 144 } 145 146 private static int selectTextSize(final int keyHeight, final float ratio, 147 final int defaultSize) { 148 if (ResourceUtils.isValidFraction(ratio)) { 149 return (int)(keyHeight * ratio); 150 } 151 return defaultSize; 152 } 153 154 private static int selectColor(final int attrColor, final int defaultColor) { 155 if (attrColor != 0) { 156 return attrColor; 157 } 158 return defaultColor; 159 } 160 161 private static float selectFloatIfNonZero(final float attrFloat, final float defaultFloat) { 162 if (attrFloat != 0) { 163 return attrFloat; 164 } 165 return defaultFloat; 166 } 167} 168