blob: 39c8a370bdde77b6a49037d9abdfe658f167d7b2 [file] [log] [blame]
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001/*
2 * Copyright (C) 2010 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 android.graphics;
18
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -080019import com.android.ide.common.rendering.api.LayoutLog;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -080020import com.android.layoutlib.bridge.Bridge;
Xavier Ducrohet3bd98982010-11-09 18:25:03 -080021import com.android.layoutlib.bridge.impl.DelegateManager;
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -080022import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070023
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Paint.FontMetricsInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070026import android.text.TextUtils;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070027
Xavier Ducrohet37f21802010-11-01 16:17:18 -070028import java.awt.BasicStroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070029import java.awt.Font;
Xavier Ducrohetb9761242010-12-23 10:22:14 -080030import java.awt.Shape;
31import java.awt.Stroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070032import java.awt.Toolkit;
33import java.awt.font.FontRenderContext;
34import java.awt.geom.AffineTransform;
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.List;
38
39/**
40 * Delegate implementing the native methods of android.graphics.Paint
41 *
42 * Through the layoutlib_create tool, the original native methods of Paint have been replaced
43 * by calls to methods of the same name in this delegate class.
44 *
45 * This class behaves like the original native implementation, but in Java, keeping previously
46 * native data into its own objects and mapping them to int that are sent back and forth between
47 * it and the original Paint class.
48 *
49 * @see DelegateManager
50 *
51 */
52public class Paint_Delegate {
53
54 /**
55 * Class associating a {@link Font} and it's {@link java.awt.FontMetrics}.
56 */
Xavier Ducrohet37f21802010-11-01 16:17:18 -070057 /*package*/ static final class FontInfo {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070058 Font mFont;
59 java.awt.FontMetrics mMetrics;
60 }
61
62 // ---- delegate manager ----
63 private static final DelegateManager<Paint_Delegate> sManager =
64 new DelegateManager<Paint_Delegate>();
65
66 // ---- delegate helper data ----
67 private List<FontInfo> mFonts;
68 private final FontRenderContext mFontContext = new FontRenderContext(
69 new AffineTransform(), true, true);
70
71 // ---- delegate data ----
72 private int mFlags;
73 private int mColor;
74 private int mStyle;
75 private int mCap;
76 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070077 private int mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070078 private int mTypeface;
79 private float mStrokeWidth;
80 private float mStrokeMiter;
81 private float mTextSize;
82 private float mTextScaleX;
83 private float mTextSkewX;
84
Xavier Ducroheta313b652010-11-01 18:45:20 -070085 private int mXfermode;
86 private int mColorFilter;
87 private int mShader;
88 private int mPathEffect;
89 private int mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -080090 private int mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070091
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070092
93 // ---- Public Helper methods ----
94
Xavier Ducrohet37f21802010-11-01 16:17:18 -070095 public static Paint_Delegate getDelegate(int native_paint) {
96 return sManager.getDelegate(native_paint);
97 }
98
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070099 /**
100 * Returns the list of {@link Font} objects. The first item is the main font, the rest
101 * are fall backs for characters not present in the main font.
102 */
103 public List<FontInfo> getFonts() {
104 return mFonts;
105 }
106
Xavier Ducroheta313b652010-11-01 18:45:20 -0700107 public boolean isAntiAliased() {
108 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
109 }
110
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700111 public boolean isFilterBitmap() {
112 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
113 }
114
115 public int getStyle() {
116 return mStyle;
117 }
118
119 public int getColor() {
120 return mColor;
121 }
122
Xavier Ducrohet66225222010-12-21 01:33:04 -0800123 public int getAlpha() {
124 return mColor >>> 24;
125 }
126
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800127 public void setAlpha(int alpha) {
128 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
129 }
130
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700131 public int getTextAlign() {
132 return mTextAlign;
133 }
134
135 public float getStrokeWidth() {
136 return mStrokeWidth;
137 }
138
Xavier Ducrohet66225222010-12-21 01:33:04 -0800139 /**
140 * returns the value of stroke miter needed by the java api.
141 */
142 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800143 float miter = mStrokeMiter * mStrokeWidth;
144 if (miter < 1.f) {
145 miter = 1.f;
146 }
147 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700148 }
149
150 public int getJavaCap() {
151 switch (Paint.sCapArray[mCap]) {
152 case BUTT:
153 return BasicStroke.CAP_BUTT;
154 case ROUND:
155 return BasicStroke.CAP_ROUND;
156 default:
157 case SQUARE:
158 return BasicStroke.CAP_SQUARE;
159 }
160 }
161
162 public int getJavaJoin() {
163 switch (Paint.sJoinArray[mJoin]) {
164 default:
165 case MITER:
166 return BasicStroke.JOIN_MITER;
167 case ROUND:
168 return BasicStroke.JOIN_ROUND;
169 case BEVEL:
170 return BasicStroke.JOIN_BEVEL;
171 }
172 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700173
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800174 public Stroke getJavaStroke() {
175 PathEffect_Delegate effectDelegate = PathEffect_Delegate.getDelegate(mPathEffect);
176 if (effectDelegate != null) {
177 if (effectDelegate.isSupported()) {
178 Stroke stroke = effectDelegate.getStroke(this);
179 assert stroke != null;
180 if (stroke != null) {
181 return stroke;
182 }
183 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800184 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800185 effectDelegate.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800186 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800187 }
188 }
189
190 // if no custom stroke as been set, set the default one.
191 return new BasicStroke(
192 getStrokeWidth(),
193 getJavaCap(),
194 getJavaJoin(),
195 getJavaStrokeMiter());
196 }
197
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800198 /**
199 * Returns the {@link Xfermode} delegate or null if none have been set
200 *
201 * @return the delegate or null.
202 */
203 public Xfermode_Delegate getXfermode() {
204 return Xfermode_Delegate.getDelegate(mXfermode);
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700205 }
206
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800207 /**
208 * Returns the {@link ColorFilter} delegate or null if none have been set
209 *
210 * @return the delegate or null.
211 */
212 public ColorFilter_Delegate getColorFilter() {
213 return ColorFilter_Delegate.getDelegate(mColorFilter);
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700214 }
215
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800216 /**
217 * Returns the {@link Shader} delegate or null if none have been set
218 *
219 * @return the delegate or null.
220 */
221 public Shader_Delegate getShader() {
222 return Shader_Delegate.getDelegate(mShader);
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700223 }
224
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800225 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800226 * Returns the {@link MaskFilter} delegate or null if none have been set
227 *
228 * @return the delegate or null.
229 */
230 public MaskFilter_Delegate getMaskFilter() {
231 return MaskFilter_Delegate.getDelegate(mMaskFilter);
232 }
233
234 /**
235 * Returns the {@link Rasterizer} delegate or null if none have been set
236 *
237 * @return the delegate or null.
238 */
239 public Rasterizer_Delegate getRasterizer() {
240 return Rasterizer_Delegate.getDelegate(mRasterizer);
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700241 }
242
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700243 // ---- native methods ----
244
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800245 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700246 /*package*/ static int getFlags(Paint thisPaint) {
247 // get the delegate from the native int.
248 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
249 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700250 return 0;
251 }
252
253 return delegate.mFlags;
254 }
255
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800256 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700257 /*package*/ static void setFlags(Paint thisPaint, int flags) {
258 // get the delegate from the native int.
259 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
260 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700261 return;
262 }
263
264 delegate.mFlags = flags;
265 }
266
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800267 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700268 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700269 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700270 }
271
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800272 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700273 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
274 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
275 }
276
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800277 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700278 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
279 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
280 }
281
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800282 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700283 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
284 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
285 }
286
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800287 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700288 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
289 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
290 }
291
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800292 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700293 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
294 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
295 }
296
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800297 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700298 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
299 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
300 }
301
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800302 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700303 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
304 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
305 }
306
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800307 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700308 /*package*/ static int getColor(Paint thisPaint) {
309 // get the delegate from the native int.
310 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
311 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700312 return 0;
313 }
314
315 return delegate.mColor;
316 }
317
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800318 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700319 /*package*/ static void setColor(Paint thisPaint, int color) {
320 // get the delegate from the native int.
321 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
322 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700323 return;
324 }
325
326 delegate.mColor = color;
327 }
328
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800329 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700330 /*package*/ static int getAlpha(Paint thisPaint) {
331 // get the delegate from the native int.
332 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
333 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700334 return 0;
335 }
336
Xavier Ducrohet66225222010-12-21 01:33:04 -0800337 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700338 }
339
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800340 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700341 /*package*/ static void setAlpha(Paint thisPaint, int a) {
342 // get the delegate from the native int.
343 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
344 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700345 return;
346 }
347
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800348 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700349 }
350
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800351 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700352 /*package*/ static float getStrokeWidth(Paint thisPaint) {
353 // get the delegate from the native int.
354 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
355 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700356 return 1.f;
357 }
358
359 return delegate.mStrokeWidth;
360 }
361
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800362 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700363 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
364 // get the delegate from the native int.
365 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
366 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700367 return;
368 }
369
370 delegate.mStrokeWidth = width;
371 }
372
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800373 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700374 /*package*/ static float getStrokeMiter(Paint thisPaint) {
375 // get the delegate from the native int.
376 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
377 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700378 return 1.f;
379 }
380
381 return delegate.mStrokeMiter;
382 }
383
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800384 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700385 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
386 // get the delegate from the native int.
387 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
388 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700389 return;
390 }
391
392 delegate.mStrokeMiter = miter;
393 }
394
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800395 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700396 /*package*/ static void nSetShadowLayer(Paint thisPaint, float radius, float dx, float dy,
397 int color) {
398 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800399 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800400 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700401 }
402
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800403 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700404 /*package*/ static float getTextSize(Paint thisPaint) {
405 // get the delegate from the native int.
406 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
407 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700408 return 1.f;
409 }
410
411 return delegate.mTextSize;
412 }
413
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800414 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700415 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
416 // get the delegate from the native int.
417 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
418 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700419 return;
420 }
421
422 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800423 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700424 }
425
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800426 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700427 /*package*/ static float getTextScaleX(Paint thisPaint) {
428 // get the delegate from the native int.
429 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
430 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700431 return 1.f;
432 }
433
434 return delegate.mTextScaleX;
435 }
436
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800437 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700438 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
439 // get the delegate from the native int.
440 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
441 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700442 return;
443 }
444
445 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800446 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700447 }
448
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800449 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700450 /*package*/ static float getTextSkewX(Paint thisPaint) {
451 // get the delegate from the native int.
452 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
453 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700454 return 1.f;
455 }
456
457 return delegate.mTextSkewX;
458 }
459
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800460 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700461 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
462 // get the delegate from the native int.
463 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
464 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700465 return;
466 }
467
468 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800469 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700470 }
471
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800472 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700473 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800474 // get the delegate
475 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
476 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800477 return 0;
478 }
479
480 if (delegate.mFonts.size() > 0) {
481 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
482 // Android expects negative ascent so we invert the value from Java.
483 return - javaMetrics.getAscent();
484 }
485
486 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700487 }
488
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800489 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700490 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800491 // get the delegate
492 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
493 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800494 return 0;
495 }
496
497 if (delegate.mFonts.size() > 0) {
498 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
499 return javaMetrics.getDescent();
500 }
501
502 return 0;
503
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700504 }
505
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800506 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700507 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700508 // get the delegate
509 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
510 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700511 return 0;
512 }
513
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800514 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700515 }
516
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800517 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700518 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700519 // get the delegate
520 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
521 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700522 return 0;
523 }
524
525 if (delegate.mFonts.size() > 0) {
526 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
527 if (fmi != null) {
528 // Android expects negative ascent so we invert the value from Java.
529 fmi.top = - javaMetrics.getMaxAscent();
530 fmi.ascent = - javaMetrics.getAscent();
531 fmi.descent = javaMetrics.getDescent();
532 fmi.bottom = javaMetrics.getMaxDescent();
533 fmi.leading = javaMetrics.getLeading();
534 }
535
536 return javaMetrics.getHeight();
537 }
538
539 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700540 }
541
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800542 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700543 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
544 int count) {
545 // WARNING: the logic in this method is similar to Canvas.drawText.
546 // Any change to this method should be reflected in Canvas.drawText
547
548 // get the delegate
549 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
550 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700551 return 0;
552 }
553
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700554 return delegate.measureText(text, index, count);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700555 }
556
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800557 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700558 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end) {
559 return native_measureText(thisPaint, text.toCharArray(), start, end - start);
560 }
561
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800562 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700563 /*package*/ static float native_measureText(Paint thisPaint, String text) {
564 return native_measureText(thisPaint, text.toCharArray(), 0, text.length());
565 }
566
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800567 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700568 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
569 float maxWidth, float[] measuredWidth) {
570 // FIXME
571 throw new UnsupportedOperationException();
572 }
573
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800574 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700575 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
576 float maxWidth, float[] measuredWidth) {
577 // FIXME
578 throw new UnsupportedOperationException();
579 }
580
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800581 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700582 /*package*/ static int native_init() {
583 Paint_Delegate newDelegate = new Paint_Delegate();
584 return sManager.addDelegate(newDelegate);
585 }
586
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800587 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700588 /*package*/ static int native_initWithPaint(int paint) {
589 // get the delegate from the native int.
590 Paint_Delegate delegate = sManager.getDelegate(paint);
591 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700592 return 0;
593 }
594
595 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
596 return sManager.addDelegate(newDelegate);
597 }
598
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800599 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700600 /*package*/ static void native_reset(int native_object) {
601 // get the delegate from the native int.
602 Paint_Delegate delegate = sManager.getDelegate(native_object);
603 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700604 return;
605 }
606
607 delegate.reset();
608 }
609
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800610 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700611 /*package*/ static void native_set(int native_dst, int native_src) {
612 // get the delegate from the native int.
613 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
614 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700615 return;
616 }
617
618 // get the delegate from the native int.
619 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
620 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700621 return;
622 }
623
624 delegate_dst.set(delegate_src);
625 }
626
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800627 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700628 /*package*/ static int native_getStyle(int native_object) {
629 // get the delegate from the native int.
630 Paint_Delegate delegate = sManager.getDelegate(native_object);
631 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700632 return 0;
633 }
634
635 return delegate.mStyle;
636 }
637
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800638 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700639 /*package*/ static void native_setStyle(int native_object, int style) {
640 // get the delegate from the native int.
641 Paint_Delegate delegate = sManager.getDelegate(native_object);
642 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700643 return;
644 }
645
646 delegate.mStyle = style;
647 }
648
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800649 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700650 /*package*/ static int native_getStrokeCap(int native_object) {
651 // get the delegate from the native int.
652 Paint_Delegate delegate = sManager.getDelegate(native_object);
653 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700654 return 0;
655 }
656
657 return delegate.mCap;
658 }
659
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800660 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700661 /*package*/ static void native_setStrokeCap(int native_object, int cap) {
662 // get the delegate from the native int.
663 Paint_Delegate delegate = sManager.getDelegate(native_object);
664 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700665 return;
666 }
667
668 delegate.mCap = cap;
669 }
670
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800671 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700672 /*package*/ static int native_getStrokeJoin(int native_object) {
673 // get the delegate from the native int.
674 Paint_Delegate delegate = sManager.getDelegate(native_object);
675 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700676 return 0;
677 }
678
679 return delegate.mJoin;
680 }
681
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800682 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700683 /*package*/ static void native_setStrokeJoin(int native_object, int join) {
684 // get the delegate from the native int.
685 Paint_Delegate delegate = sManager.getDelegate(native_object);
686 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700687 return;
688 }
689
690 delegate.mJoin = join;
691 }
692
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800693 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700694 /*package*/ static boolean native_getFillPath(int native_object, int src, int dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800695 Paint_Delegate paint = sManager.getDelegate(native_object);
696 if (paint == null) {
697 return false;
698 }
699
700 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
701 if (srcPath == null) {
702 return true;
703 }
704
705 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
706 if (dstPath == null) {
707 return true;
708 }
709
710 Stroke stroke = paint.getJavaStroke();
711 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
712
713 dstPath.setJavaShape(strokeShape);
714
715 // FIXME figure out the return value?
716 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700717 }
718
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800719 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700720 /*package*/ static int native_setShader(int native_object, int shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700721 // get the delegate from the native int.
722 Paint_Delegate delegate = sManager.getDelegate(native_object);
723 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700724 return shader;
725 }
726
727 return delegate.mShader = shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700728 }
729
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800730 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700731 /*package*/ static int native_setColorFilter(int native_object, int filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700732 // get the delegate from the native int.
733 Paint_Delegate delegate = sManager.getDelegate(native_object);
734 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700735 return filter;
736 }
737
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800738 delegate.mColorFilter = filter;
739
740 // since none of those are supported, display a fidelity warning right away
741 ColorFilter_Delegate filterDelegate = delegate.getColorFilter();
742 if (filterDelegate != null && filterDelegate.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800743 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800744 filterDelegate.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800745 }
746
747 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700748 }
749
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800750 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700751 /*package*/ static int native_setXfermode(int native_object, int xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700752 // get the delegate from the native int.
753 Paint_Delegate delegate = sManager.getDelegate(native_object);
754 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700755 return xfermode;
756 }
757
758 return delegate.mXfermode = xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700759 }
760
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800761 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700762 /*package*/ static int native_setPathEffect(int native_object, int effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700763 // get the delegate from the native int.
764 Paint_Delegate delegate = sManager.getDelegate(native_object);
765 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700766 return effect;
767 }
768
769 return delegate.mPathEffect = effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700770 }
771
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800772 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700773 /*package*/ static int native_setMaskFilter(int native_object, int maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700774 // get the delegate from the native int.
775 Paint_Delegate delegate = sManager.getDelegate(native_object);
776 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700777 return maskfilter;
778 }
779
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800780 delegate.mMaskFilter = maskfilter;
781
782 // since none of those are supported, display a fidelity warning right away
783 MaskFilter_Delegate filterDelegate = delegate.getMaskFilter();
784 if (filterDelegate != null && filterDelegate.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800785 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800786 filterDelegate.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800787 }
788
789 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700790 }
791
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800792 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700793 /*package*/ static int native_setTypeface(int native_object, int typeface) {
794 // get the delegate from the native int.
795 Paint_Delegate delegate = sManager.getDelegate(native_object);
796 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700797 return 0;
798 }
799
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800800 delegate.mTypeface = typeface;
801 delegate.updateFontObject();
802 return delegate.mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700803 }
804
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800805 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700806 /*package*/ static int native_setRasterizer(int native_object, int rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800807 // get the delegate from the native int.
808 Paint_Delegate delegate = sManager.getDelegate(native_object);
809 if (delegate == null) {
810 return rasterizer;
811 }
812
813 delegate.mRasterizer = rasterizer;
814
815 // since none of those are supported, display a fidelity warning right away
816 Rasterizer_Delegate rasterizerDelegate = delegate.getRasterizer();
817 if (rasterizerDelegate != null && rasterizerDelegate.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800818 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800819 rasterizerDelegate.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800820 }
821
822 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700823 }
824
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800825 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700826 /*package*/ static int native_getTextAlign(int native_object) {
827 // get the delegate from the native int.
828 Paint_Delegate delegate = sManager.getDelegate(native_object);
829 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700830 return 0;
831 }
832
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700833 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700834 }
835
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800836 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700837 /*package*/ static void native_setTextAlign(int native_object, int align) {
838 // get the delegate from the native int.
839 Paint_Delegate delegate = sManager.getDelegate(native_object);
840 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700841 return;
842 }
843
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700844 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700845 }
846
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800847 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700848 /*package*/ static float native_getFontMetrics(int native_paint, FontMetrics metrics) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800849 // get the delegate from the native int.
850 Paint_Delegate delegate = sManager.getDelegate(native_paint);
851 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800852 return 0.f;
853 }
854
855 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700856 }
857
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800858 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700859 /*package*/ static int native_getTextWidths(int native_object, char[] text, int index,
860 int count, float[] widths) {
861 // FIXME
862 throw new UnsupportedOperationException();
863 }
864
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800865 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700866 /*package*/ static int native_getTextWidths(int native_object, String text, int start,
867 int end, float[] widths) {
868 // FIXME
869 throw new UnsupportedOperationException();
870 }
871
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800872 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700873 /*package*/ static float native_getTextRunAdvances(int native_object,
874 char[] text, int index, int count, int contextIndex, int contextCount,
875 int flags, float[] advances, int advancesIndex) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700876 // get the delegate from the native int.
877 Paint_Delegate delegate = sManager.getDelegate(native_object);
878 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700879 return 0.f;
880 }
881
882 if (delegate.mFonts.size() > 0) {
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700883 // FIXME: handle multi-char characters (see measureText)
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700884 float totalAdvance = 0;
885 for (int i = 0; i < count; i++) {
886 char c = text[i + index];
887 boolean found = false;
888 for (FontInfo info : delegate.mFonts) {
889 if (info.mFont.canDisplay(c)) {
890 float adv = info.mMetrics.charWidth(c);
891 totalAdvance += adv;
892 if (advances != null) {
893 advances[i] = adv;
894 }
895
896 found = true;
897 break;
898 }
899 }
900
901 if (found == false) {
902 // no advance for this char.
903 if (advances != null) {
904 advances[i] = 0.f;
905 }
906 }
907 }
908
909 return totalAdvance;
910 }
911
912 return 0;
913
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700914 }
915
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800916 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700917 /*package*/ static float native_getTextRunAdvances(int native_object,
918 String text, int start, int end, int contextStart, int contextEnd,
919 int flags, float[] advances, int advancesIndex) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700920 // FIXME: support contextStart, contextEnd and direction flag
921 int count = end - start;
922 char[] buffer = TemporaryBuffer.obtain(count);
923 TextUtils.getChars(text, start, end, buffer, 0);
924
925 return native_getTextRunAdvances(native_object, buffer, 0, count, contextStart,
926 contextEnd - contextStart, flags, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700927 }
928
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800929 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700930 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, char[] text,
931 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
932 // FIXME
933 throw new UnsupportedOperationException();
934 }
935
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800936 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700937 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, String text,
938 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
939 // FIXME
940 throw new UnsupportedOperationException();
941 }
942
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800943 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700944 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
945 char[] text, int index, int count, float x, float y, int path) {
946 // FIXME
947 throw new UnsupportedOperationException();
948 }
949
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800950 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700951 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
952 String text, int start, int end, float x, float y, int path) {
953 // FIXME
954 throw new UnsupportedOperationException();
955 }
956
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800957 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700958 /*package*/ static void nativeGetStringBounds(int nativePaint, String text, int start,
959 int end, Rect bounds) {
960 // FIXME
961 throw new UnsupportedOperationException();
962 }
963
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800964 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700965 /*package*/ static void nativeGetCharArrayBounds(int nativePaint, char[] text, int index,
966 int count, Rect bounds) {
967 // FIXME
968 throw new UnsupportedOperationException();
969 }
970
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800971 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700972 /*package*/ static void finalizer(int nativePaint) {
973 sManager.removeDelegate(nativePaint);
974 }
975
976 // ---- Private delegate/helper methods ----
977
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800978 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700979 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700980 }
981
982 private Paint_Delegate(Paint_Delegate paint) {
983 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700984 }
985
986 private void set(Paint_Delegate paint) {
987 mFlags = paint.mFlags;
988 mColor = paint.mColor;
989 mStyle = paint.mStyle;
990 mCap = paint.mCap;
991 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700992 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700993 mTypeface = paint.mTypeface;
994 mStrokeWidth = paint.mStrokeWidth;
995 mStrokeMiter = paint.mStrokeMiter;
996 mTextSize = paint.mTextSize;
997 mTextScaleX = paint.mTextScaleX;
998 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -0700999 mXfermode = paint.mXfermode;
1000 mColorFilter = paint.mColorFilter;
1001 mShader = paint.mShader;
1002 mPathEffect = paint.mPathEffect;
1003 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001004 mRasterizer = paint.mRasterizer;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001005 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001006 }
1007
1008 private void reset() {
1009 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001010 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001011 mStyle = Paint.Style.FILL.nativeInt;
1012 mCap = Paint.Cap.BUTT.nativeInt;
1013 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001014 mTextAlign = 0;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001015 mTypeface = Typeface.sDefaults[0].native_instance;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001016 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001017 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001018 mTextSize = 20.f;
1019 mTextScaleX = 1.f;
1020 mTextSkewX = 0.f;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001021 mXfermode = 0;
1022 mColorFilter = 0;
1023 mShader = 0;
1024 mPathEffect = 0;
1025 mMaskFilter = 0;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001026 mRasterizer = 0;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001027 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001028 }
1029
1030 /**
1031 * Update the {@link Font} object from the typeface, text size and scaling
1032 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001033 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001034 private void updateFontObject() {
1035 if (mTypeface != 0) {
1036 // Get the fonts from the TypeFace object.
1037 List<Font> fonts = Typeface_Delegate.getFonts(mTypeface);
1038
1039 // create new font objects as well as FontMetrics, based on the current text size
1040 // and skew info.
1041 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1042 for (Font font : fonts) {
1043 FontInfo info = new FontInfo();
1044 info.mFont = font.deriveFont(mTextSize);
1045 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1046 // TODO: support skew
1047 info.mFont = info.mFont.deriveFont(new AffineTransform(
1048 mTextScaleX, mTextSkewX, 0, 0, 1, 0));
1049 }
1050 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1051
1052 infoList.add(info);
1053 }
1054
1055 mFonts = Collections.unmodifiableList(infoList);
1056 }
1057 }
1058
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001059 /*package*/ float measureText(char[] text, int index, int count) {
1060 if (mFonts.size() > 0) {
1061 FontInfo mainFont = mFonts.get(0);
1062 int i = index;
1063 int lastIndex = index + count;
1064 float total = 0f;
1065 while (i < lastIndex) {
1066 // always start with the main font.
1067 int upTo = mainFont.mFont.canDisplayUpTo(text, i, lastIndex);
1068 if (upTo == -1) {
1069 // shortcut to exit
1070 return total + mainFont.mMetrics.charsWidth(text, i, lastIndex - i);
1071 } else if (upTo > 0) {
1072 total += mainFont.mMetrics.charsWidth(text, i, upTo - i);
1073 i = upTo;
1074 // don't call continue at this point. Since it is certain the main font
1075 // cannot display the font a index upTo (now ==i), we move on to the
1076 // fallback fonts directly.
1077 }
1078
1079 // no char supported, attempt to read the next char(s) with the
1080 // fallback font. In this case we only test the first character
1081 // and then go back to test with the main font.
1082 // Special test for 2-char characters.
1083 boolean foundFont = false;
1084 for (int f = 1 ; f < mFonts.size() ; f++) {
1085 FontInfo fontInfo = mFonts.get(f);
1086
1087 // need to check that the font can display the character. We test
1088 // differently if the char is a high surrogate.
1089 int charCount = Character.isHighSurrogate(text[i]) ? 2 : 1;
1090 upTo = fontInfo.mFont.canDisplayUpTo(text, i, i + charCount);
1091 if (upTo == -1) {
1092 total += fontInfo.mMetrics.charsWidth(text, i, charCount);
1093 i += charCount;
1094 foundFont = true;
1095 break;
1096
1097 }
1098 }
1099
1100 // in case no font can display the char, measure it with the main font.
1101 if (foundFont == false) {
1102 int size = Character.isHighSurrogate(text[i]) ? 2 : 1;
1103 total += mainFont.mMetrics.charsWidth(text, i, size);
1104 i += size;
1105 }
1106 }
1107 }
1108
1109 return 0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001110 }
1111
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001112 private float getFontMetrics(FontMetrics metrics) {
1113 if (mFonts.size() > 0) {
1114 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1115 if (metrics != null) {
1116 // Android expects negative ascent so we invert the value from Java.
1117 metrics.top = - javaMetrics.getMaxAscent();
1118 metrics.ascent = - javaMetrics.getAscent();
1119 metrics.descent = javaMetrics.getDescent();
1120 metrics.bottom = javaMetrics.getMaxDescent();
1121 metrics.leading = javaMetrics.getLeading();
1122 }
1123
1124 return javaMetrics.getHeight();
1125 }
1126
1127 return 0;
1128 }
1129
1130
1131
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001132 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1133 // get the delegate from the native int.
1134 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1135 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001136 return;
1137 }
1138
1139 if (flagValue) {
1140 delegate.mFlags |= flagMask;
1141 } else {
1142 delegate.mFlags &= ~flagMask;
1143 }
1144 }
1145}