Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.bidi; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Color; |
| 22 | import android.graphics.Paint; |
| 23 | import android.graphics.Rect; |
| 24 | import android.graphics.Typeface; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | |
| 29 | public class BiDiTestView extends View { |
| 30 | |
| 31 | private static final String TAG = "BiDiTestView"; |
| 32 | |
| 33 | private static final int BORDER_PADDING = 4; |
| 34 | private static final int TEXT_PADDING = 16; |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 35 | private static final int TEXT_SIZE = 16; |
| 36 | private static final int ORIGIN = 80; |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 37 | |
| 38 | private static final float DEFAULT_ITALIC_SKEW_X = -0.25f; |
| 39 | |
| 40 | private Paint paint = new Paint(); |
| 41 | private Rect rect = new Rect(); |
| 42 | |
| 43 | private String NORMAL_TEXT; |
| 44 | private String NORMAL_LONG_TEXT; |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 45 | private String NORMAL_LONG_TEXT_2; |
| 46 | private String NORMAL_LONG_TEXT_3; |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 47 | private String ITALIC_TEXT; |
| 48 | private String BOLD_TEXT; |
| 49 | private String BOLD_ITALIC_TEXT; |
| 50 | private String ARABIC_TEXT; |
| 51 | private String CHINESE_TEXT; |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 52 | private String MIXED_TEXT_1; |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 53 | private String HEBREW_TEXT; |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 54 | |
| 55 | private Typeface typeface; |
| 56 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 57 | private int currentTextSize; |
| 58 | |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 59 | public BiDiTestView(Context context) { |
| 60 | super(context); |
| 61 | init(context); |
| 62 | } |
| 63 | |
| 64 | public BiDiTestView(Context context, AttributeSet attrs) { |
| 65 | super(context, attrs); |
| 66 | init(context); |
| 67 | } |
| 68 | |
| 69 | public BiDiTestView(Context context, AttributeSet attrs, int defStyle) { |
| 70 | super(context, attrs, defStyle); |
| 71 | init(context); |
| 72 | } |
| 73 | |
| 74 | private void init(Context context) { |
| 75 | NORMAL_TEXT = context.getString(R.string.normal_text); |
| 76 | NORMAL_LONG_TEXT = context.getString(R.string.normal_long_text); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 77 | NORMAL_LONG_TEXT_2 = context.getString(R.string.normal_long_text_2); |
| 78 | NORMAL_LONG_TEXT_3 = context.getString(R.string.normal_long_text_3); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 79 | ITALIC_TEXT = context.getString(R.string.italic_text); |
| 80 | BOLD_TEXT = context.getString(R.string.bold_text); |
| 81 | BOLD_ITALIC_TEXT = context.getString(R.string.bold_italic_text); |
| 82 | ARABIC_TEXT = context.getString(R.string.arabic_text); |
| 83 | CHINESE_TEXT = context.getString(R.string.chinese_text); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 84 | MIXED_TEXT_1 = context.getString(R.string.mixed_text_1); |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 85 | HEBREW_TEXT = context.getString(R.string.hebrew_text); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 86 | |
| 87 | typeface = paint.getTypeface(); |
| 88 | paint.setAntiAlias(true); |
| 89 | } |
| 90 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 91 | public void setCurrentTextSize(int size) { |
| 92 | currentTextSize = size; |
| 93 | invalidate(); |
| 94 | } |
| 95 | |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public void onDraw(Canvas canvas) { |
| 98 | drawInsideRect(canvas, Color.BLACK); |
| 99 | |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 100 | int deltaX = 0; |
| 101 | |
| 102 | deltaX = testString(canvas, NORMAL_TEXT, ORIGIN, ORIGIN, |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 103 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
| 104 | |
| 105 | deltaX += testString(canvas, ITALIC_TEXT, ORIGIN + deltaX, ORIGIN, |
| 106 | paint, typeface, true, false, Paint.DIRECTION_LTR, currentTextSize); |
| 107 | |
| 108 | deltaX += testString(canvas, BOLD_TEXT, ORIGIN + deltaX, ORIGIN, |
| 109 | paint, typeface, false, true, Paint.DIRECTION_LTR, currentTextSize); |
| 110 | |
| 111 | deltaX += testString(canvas, BOLD_ITALIC_TEXT, ORIGIN + deltaX, ORIGIN, |
| 112 | paint, typeface, true, true, Paint.DIRECTION_LTR, currentTextSize); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 113 | |
| 114 | // Test with a long string |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 115 | deltaX = testString(canvas, NORMAL_LONG_TEXT, ORIGIN, ORIGIN + 2 * currentTextSize, |
| 116 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
| 117 | |
| 118 | // Test with a long string |
| 119 | deltaX = testString(canvas, NORMAL_LONG_TEXT_2, ORIGIN, ORIGIN + 4 * currentTextSize, |
| 120 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
| 121 | |
| 122 | // Test with a long string |
| 123 | deltaX = testString(canvas, NORMAL_LONG_TEXT_3, ORIGIN, ORIGIN + 6 * currentTextSize, |
| 124 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 125 | |
| 126 | // Test Arabic ligature |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 127 | deltaX = testString(canvas, ARABIC_TEXT, ORIGIN, ORIGIN + 8 * currentTextSize, |
| 128 | paint, typeface, false, false, Paint.DIRECTION_RTL, currentTextSize); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 129 | |
| 130 | // Test Chinese |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 131 | deltaX = testString(canvas, CHINESE_TEXT, ORIGIN, ORIGIN + 10 * currentTextSize, |
| 132 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 133 | |
| 134 | // Test Mixed (English and Arabic) |
| 135 | deltaX = testString(canvas, MIXED_TEXT_1, ORIGIN, ORIGIN + 12 * currentTextSize, |
| 136 | paint, typeface, false, false, Paint.DIRECTION_LTR, currentTextSize); |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 137 | |
| 138 | // Test Hebrew |
| 139 | deltaX = testString(canvas, HEBREW_TEXT, ORIGIN, ORIGIN + 14 * currentTextSize, |
| 140 | paint, typeface, false, false, Paint.DIRECTION_RTL, currentTextSize); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private int testString(Canvas canvas, String text, int x, int y, Paint paint, Typeface typeface, |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 144 | boolean isItalic, boolean isBold, int dir, int textSize) { |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 145 | paint.setTypeface(typeface); |
| 146 | |
| 147 | // Set paint properties |
| 148 | boolean oldFakeBold = paint.isFakeBoldText(); |
| 149 | paint.setFakeBoldText(isBold); |
| 150 | |
| 151 | float oldTextSkewX = paint.getTextSkewX(); |
| 152 | if (isItalic) { |
| 153 | paint.setTextSkewX(DEFAULT_ITALIC_SKEW_X); |
| 154 | } |
| 155 | |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 156 | Log.v(TAG, "START -- drawTextWithCanvasDrawText"); |
| 157 | drawTextWithCanvasDrawText(text, canvas, x, y, textSize, Color.WHITE, dir); |
| 158 | Log.v(TAG, "END -- drawTextWithCanvasDrawText"); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 159 | |
| 160 | int length = text.length(); |
| 161 | float[] advances = new float[length]; |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 162 | float textWidthHB = paint.getTextRunAdvances(text, 0, length, 0, length, dir, advances, 0); |
| 163 | setPaintDir(paint, dir); |
Fabrice Di Meglio | 0a1413e | 2011-04-21 17:36:26 -0700 | [diff] [blame] | 164 | float textWidthICU = paint.getTextRunAdvances(text, 0, length, 0, length, dir, advances, 0, |
| 165 | 1 /* use ICU */); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 166 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 167 | logAdvances(text, textWidthHB, textWidthICU, advances); |
| 168 | drawMetricsAroundText(canvas, x, y, textWidthHB, textWidthICU, textSize, Color.RED, Color.GREEN); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 169 | |
| 170 | paint.setColor(Color.WHITE); |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 171 | // char[] glyphs = new char[2*length]; |
| 172 | // int count = getGlyphs(text, glyphs, dir); |
| 173 | // |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 174 | // logGlypths(glyphs, count); |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 175 | // drawTextWithDrawGlyph(canvas, glyphs, count, x, y + currentTextSize); |
| 176 | |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 177 | Log.v(TAG, "START -- drawTextWithGlyphs"); |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 178 | drawTextWithGlyphs(canvas, text, x, y + currentTextSize, dir); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 179 | Log.v(TAG, "END -- drawTextWithGlyphs"); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 180 | |
| 181 | // Restore old paint properties |
| 182 | paint.setFakeBoldText(oldFakeBold); |
| 183 | paint.setTextSkewX(oldTextSkewX); |
| 184 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 185 | return (int) Math.ceil(textWidthHB) + TEXT_PADDING; |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 188 | private void setPaintDir(Paint paint, int dir) { |
| 189 | Log.v(TAG, "Setting Paint dir=" + dir); |
| 190 | paint.setBidiFlags(dir); |
| 191 | } |
| 192 | |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 193 | private void drawTextWithDrawGlyph(Canvas canvas, char[] glyphs, int count, int x, int y) { |
| 194 | canvas.drawGlyphs(glyphs, 0, count, x, y, paint); |
| 195 | } |
| 196 | |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 197 | private void drawTextWithGlyphs(Canvas canvas, String text, int x, int y, int dir) { |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 198 | setPaintDir(paint, dir); |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 199 | canvas.drawTextWithGlyphs(text, x, y, paint); |
| 200 | } |
| 201 | |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 202 | private void logGlypths(char[] glyphs, int count) { |
| 203 | Log.v(TAG, "GlyphIds - count=" + count); |
| 204 | for (int n = 0; n < count; n++) { |
| 205 | Log.v(TAG, "GlyphIds - Id[" + n + "]="+ (int)glyphs[n]); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private int getGlyphs(String text, char[] glyphs, int dir) { |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 210 | return paint.getTextGlypths(text, 0, text.length(), 0, text.length(), dir, glyphs); |
| 211 | } |
| 212 | |
| 213 | private void drawInsideRect(Canvas canvas, int color) { |
| 214 | paint.setColor(color); |
| 215 | int width = getWidth(); |
| 216 | int height = getHeight(); |
| 217 | rect.set(BORDER_PADDING, BORDER_PADDING, width - BORDER_PADDING, height - BORDER_PADDING); |
| 218 | canvas.drawRect(rect, paint); |
| 219 | } |
| 220 | |
| 221 | private void drawTextWithCanvasDrawText(String text, Canvas canvas, |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 222 | float x, float y, float textSize, int color, int dir) { |
| 223 | setPaintDir(paint, dir); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 224 | paint.setColor(color); |
| 225 | paint.setTextSize(textSize); |
| 226 | canvas.drawText(text, x, y, paint); |
| 227 | } |
| 228 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 229 | private void drawMetricsAroundText(Canvas canvas, int x, int y, float textWidthHB, |
| 230 | float textWidthICU, int textSize, int color, int colorICU) { |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 231 | paint.setColor(color); |
| 232 | canvas.drawLine(x, y - textSize, x, y + 8, paint); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 233 | canvas.drawLine(x, y + 8, x + textWidthHB, y + 8, paint); |
| 234 | canvas.drawLine(x + textWidthHB, y - textSize, x + textWidthHB, y + 8, paint); |
| 235 | paint.setColor(colorICU); |
| 236 | canvas.drawLine(x + textWidthICU, y - textSize, x + textWidthICU, y + 8, paint); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 239 | private void logAdvances(String text, float textWidth, float textWidthICU, float[] advances) { |
| 240 | Log.v(TAG, "Advances for text: " + text + " total= " + textWidth + " - totalICU= " + textWidthICU); |
| 241 | // int length = advances.length; |
| 242 | // for(int n=0; n<length; n++){ |
| 243 | // Log.v(TAG, "adv[" + n + "]=" + advances[n]); |
| 244 | // } |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 245 | } |
| 246 | } |