diff options
author | 2013-07-05 18:57:07 +0900 | |
---|---|---|
committer | 2013-07-05 19:24:59 +0900 | |
commit | ccf4a310279b13bbf0b6aac76a0878178c1dfb7d (patch) | |
tree | a9cf2ac3548d85c62e07b2dd8c4b1685dfd8b38d /java/src/com/android/inputmethod/latin/utils | |
parent | 4be6198cb73cc24e10834153c4e049644ed187e3 (diff) | |
download | latinime-ccf4a310279b13bbf0b6aac76a0878178c1dfb7d.tar.gz latinime-ccf4a310279b13bbf0b6aac76a0878178c1dfb7d.tar.xz latinime-ccf4a310279b13bbf0b6aac76a0878178c1dfb7d.zip |
Move TypefaceUtils and ViewUtils to utils package
Change-Id: Iccd47c27670248c43de9a539c1951111d7733111
Diffstat (limited to 'java/src/com/android/inputmethod/latin/utils')
-rw-r--r-- | java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java | 89 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java | 54 |
2 files changed, 143 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java b/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java new file mode 100644 index 000000000..544e4d201 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.utils; + +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.Typeface; +import android.util.SparseArray; + +public final class TypefaceUtils { + private TypefaceUtils() { + // This utility class is not publicly instantiable. + } + + // This sparse array caches key label text height in pixel indexed by key label text size. + private static final SparseArray<Float> sTextHeightCache = CollectionUtils.newSparseArray(); + // Working variable for the following method. + private static final Rect sTextHeightBounds = new Rect(); + + public static float getCharHeight(final char[] referenceChar, final Paint paint) { + final int key = getCharGeometryCacheKey(referenceChar[0], paint); + synchronized (sTextHeightCache) { + final Float cachedValue = sTextHeightCache.get(key); + if (cachedValue != null) { + return cachedValue; + } + + paint.getTextBounds(referenceChar, 0, 1, sTextHeightBounds); + final float height = sTextHeightBounds.height(); + sTextHeightCache.put(key, height); + return height; + } + } + + // This sparse array caches key label text width in pixel indexed by key label text size. + private static final SparseArray<Float> sTextWidthCache = CollectionUtils.newSparseArray(); + // Working variable for the following method. + private static final Rect sTextWidthBounds = new Rect(); + + public static float getCharWidth(final char[] referenceChar, final Paint paint) { + final int key = getCharGeometryCacheKey(referenceChar[0], paint); + synchronized (sTextWidthCache) { + final Float cachedValue = sTextWidthCache.get(key); + if (cachedValue != null) { + return cachedValue; + } + + paint.getTextBounds(referenceChar, 0, 1, sTextWidthBounds); + final float width = sTextWidthBounds.width(); + sTextWidthCache.put(key, width); + return width; + } + } + + private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) { + final int labelSize = (int)paint.getTextSize(); + final Typeface face = paint.getTypeface(); + final int codePointOffset = referenceChar << 15; + if (face == Typeface.DEFAULT) { + return codePointOffset + labelSize; + } else if (face == Typeface.DEFAULT_BOLD) { + return codePointOffset + labelSize + 0x1000; + } else if (face == Typeface.MONOSPACE) { + return codePointOffset + labelSize + 0x2000; + } else { + return codePointOffset + labelSize; + } + } + + public static float getLabelWidth(final String label, final Paint paint) { + final Rect textBounds = new Rect(); + paint.getTextBounds(label, 0, label.length(), textBounds); + return textBounds.width(); + } +} diff --git a/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java b/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java new file mode 100644 index 000000000..f9d853493 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.utils; + +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewGroup.MarginLayoutParams; +import android.widget.FrameLayout; +import android.widget.RelativeLayout; + +public final class ViewLayoutUtils { + private ViewLayoutUtils() { + // This utility class is not publicly instantiable. + } + + public static MarginLayoutParams newLayoutParam(final ViewGroup placer, final int width, + final int height) { + if (placer instanceof FrameLayout) { + return new FrameLayout.LayoutParams(width, height); + } else if (placer instanceof RelativeLayout) { + return new RelativeLayout.LayoutParams(width, height); + } else if (placer == null) { + throw new NullPointerException("placer is null"); + } else { + throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " + + placer.getClass().getName()); + } + } + + public static void placeViewAt(final View view, final int x, final int y, final int w, + final int h) { + final ViewGroup.LayoutParams lp = view.getLayoutParams(); + if (lp instanceof MarginLayoutParams) { + final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp; + marginLayoutParams.width = w; + marginLayoutParams.height = h; + marginLayoutParams.setMargins(x, y, 0, 0); + } + } +} |