blob: 13826414cb2731c5708e80c62fd2c3246acb8675 [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;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -080035import java.awt.geom.Rectangle2D;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070036import java.util.ArrayList;
37import java.util.Collections;
38import java.util.List;
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070039import java.util.Locale;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070040
41/**
42 * Delegate implementing the native methods of android.graphics.Paint
43 *
44 * Through the layoutlib_create tool, the original native methods of Paint have been replaced
45 * by calls to methods of the same name in this delegate class.
46 *
47 * This class behaves like the original native implementation, but in Java, keeping previously
48 * native data into its own objects and mapping them to int that are sent back and forth between
49 * it and the original Paint class.
50 *
51 * @see DelegateManager
52 *
53 */
54public class Paint_Delegate {
55
56 /**
57 * Class associating a {@link Font} and it's {@link java.awt.FontMetrics}.
58 */
Xavier Ducrohet37f21802010-11-01 16:17:18 -070059 /*package*/ static final class FontInfo {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070060 Font mFont;
61 java.awt.FontMetrics mMetrics;
62 }
63
64 // ---- delegate manager ----
65 private static final DelegateManager<Paint_Delegate> sManager =
Xavier Ducrohetb2de8392011-02-23 16:51:08 -080066 new DelegateManager<Paint_Delegate>(Paint_Delegate.class);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070067
68 // ---- delegate helper data ----
69 private List<FontInfo> mFonts;
70 private final FontRenderContext mFontContext = new FontRenderContext(
71 new AffineTransform(), true, true);
72
73 // ---- delegate data ----
74 private int mFlags;
75 private int mColor;
76 private int mStyle;
77 private int mCap;
78 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070079 private int mTextAlign;
Xavier Ducrohet91672792011-02-22 11:54:37 -080080 private Typeface_Delegate mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070081 private float mStrokeWidth;
82 private float mStrokeMiter;
83 private float mTextSize;
84 private float mTextScaleX;
85 private float mTextSkewX;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -070086 private int mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070087
Xavier Ducrohet91672792011-02-22 11:54:37 -080088 private Xfermode_Delegate mXfermode;
89 private ColorFilter_Delegate mColorFilter;
90 private Shader_Delegate mShader;
91 private PathEffect_Delegate mPathEffect;
92 private MaskFilter_Delegate mMaskFilter;
93 private Rasterizer_Delegate mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070094
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070095 private Locale mLocale = Locale.getDefault();
96
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070097
98 // ---- Public Helper methods ----
99
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700100 public static Paint_Delegate getDelegate(int native_paint) {
101 return sManager.getDelegate(native_paint);
102 }
103
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700104 /**
105 * Returns the list of {@link Font} objects. The first item is the main font, the rest
106 * are fall backs for characters not present in the main font.
107 */
108 public List<FontInfo> getFonts() {
109 return mFonts;
110 }
111
Xavier Ducroheta313b652010-11-01 18:45:20 -0700112 public boolean isAntiAliased() {
113 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
114 }
115
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700116 public boolean isFilterBitmap() {
117 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
118 }
119
120 public int getStyle() {
121 return mStyle;
122 }
123
124 public int getColor() {
125 return mColor;
126 }
127
Xavier Ducrohet66225222010-12-21 01:33:04 -0800128 public int getAlpha() {
129 return mColor >>> 24;
130 }
131
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800132 public void setAlpha(int alpha) {
133 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
134 }
135
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700136 public int getTextAlign() {
137 return mTextAlign;
138 }
139
140 public float getStrokeWidth() {
141 return mStrokeWidth;
142 }
143
Xavier Ducrohet66225222010-12-21 01:33:04 -0800144 /**
145 * returns the value of stroke miter needed by the java api.
146 */
147 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800148 float miter = mStrokeMiter * mStrokeWidth;
149 if (miter < 1.f) {
150 miter = 1.f;
151 }
152 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700153 }
154
155 public int getJavaCap() {
156 switch (Paint.sCapArray[mCap]) {
157 case BUTT:
158 return BasicStroke.CAP_BUTT;
159 case ROUND:
160 return BasicStroke.CAP_ROUND;
161 default:
162 case SQUARE:
163 return BasicStroke.CAP_SQUARE;
164 }
165 }
166
167 public int getJavaJoin() {
168 switch (Paint.sJoinArray[mJoin]) {
169 default:
170 case MITER:
171 return BasicStroke.JOIN_MITER;
172 case ROUND:
173 return BasicStroke.JOIN_ROUND;
174 case BEVEL:
175 return BasicStroke.JOIN_BEVEL;
176 }
177 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700178
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800179 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800180 if (mPathEffect != null) {
181 if (mPathEffect.isSupported()) {
182 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800183 assert stroke != null;
184 if (stroke != null) {
185 return stroke;
186 }
187 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800188 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800189 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800190 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800191 }
192 }
193
194 // if no custom stroke as been set, set the default one.
195 return new BasicStroke(
196 getStrokeWidth(),
197 getJavaCap(),
198 getJavaJoin(),
199 getJavaStrokeMiter());
200 }
201
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800202 /**
203 * Returns the {@link Xfermode} delegate or null if none have been set
204 *
205 * @return the delegate or null.
206 */
207 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800208 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700209 }
210
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800211 /**
212 * Returns the {@link ColorFilter} delegate or null if none have been set
213 *
214 * @return the delegate or null.
215 */
216 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800217 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700218 }
219
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800220 /**
221 * Returns the {@link Shader} delegate or null if none have been set
222 *
223 * @return the delegate or null.
224 */
225 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800226 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700227 }
228
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800229 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800230 * Returns the {@link MaskFilter} delegate or null if none have been set
231 *
232 * @return the delegate or null.
233 */
234 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800235 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800236 }
237
238 /**
239 * Returns the {@link Rasterizer} delegate or null if none have been set
240 *
241 * @return the delegate or null.
242 */
243 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800244 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700245 }
246
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700247 // ---- native methods ----
248
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800249 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700250 /*package*/ static int getFlags(Paint thisPaint) {
251 // get the delegate from the native int.
252 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
253 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700254 return 0;
255 }
256
257 return delegate.mFlags;
258 }
259
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700260
261
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800262 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700263 /*package*/ static void setFlags(Paint thisPaint, int flags) {
264 // get the delegate from the native int.
265 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
266 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700267 return;
268 }
269
270 delegate.mFlags = flags;
271 }
272
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800273 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700274 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700275 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700276 }
277
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800278 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700279 /*package*/ static int getHinting(Paint thisPaint) {
280 // get the delegate from the native int.
281 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
282 if (delegate == null) {
283 return Paint.HINTING_ON;
284 }
285
286 return delegate.mHintingMode;
287 }
288
289 @LayoutlibDelegate
290 /*package*/ static void setHinting(Paint thisPaint, int mode) {
291 // get the delegate from the native int.
292 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
293 if (delegate == null) {
294 return;
295 }
296
297 delegate.mHintingMode = mode;
298 }
299
300 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700301 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
302 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
303 }
304
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800305 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700306 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
307 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
308 }
309
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800310 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700311 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
312 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
313 }
314
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800315 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700316 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
317 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
318 }
319
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800320 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700321 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
322 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
323 }
324
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800325 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700326 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
327 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
328 }
329
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800330 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700331 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
332 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
333 }
334
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800335 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700336 /*package*/ static int getColor(Paint thisPaint) {
337 // get the delegate from the native int.
338 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
339 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700340 return 0;
341 }
342
343 return delegate.mColor;
344 }
345
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800346 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700347 /*package*/ static void setColor(Paint thisPaint, int color) {
348 // get the delegate from the native int.
349 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
350 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700351 return;
352 }
353
354 delegate.mColor = color;
355 }
356
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800357 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700358 /*package*/ static int getAlpha(Paint thisPaint) {
359 // get the delegate from the native int.
360 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
361 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700362 return 0;
363 }
364
Xavier Ducrohet66225222010-12-21 01:33:04 -0800365 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700366 }
367
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800368 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700369 /*package*/ static void setAlpha(Paint thisPaint, int a) {
370 // get the delegate from the native int.
371 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
372 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700373 return;
374 }
375
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800376 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700377 }
378
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800379 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700380 /*package*/ static float getStrokeWidth(Paint thisPaint) {
381 // get the delegate from the native int.
382 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
383 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700384 return 1.f;
385 }
386
387 return delegate.mStrokeWidth;
388 }
389
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800390 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700391 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
392 // get the delegate from the native int.
393 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
394 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700395 return;
396 }
397
398 delegate.mStrokeWidth = width;
399 }
400
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800401 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700402 /*package*/ static float getStrokeMiter(Paint thisPaint) {
403 // get the delegate from the native int.
404 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
405 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700406 return 1.f;
407 }
408
409 return delegate.mStrokeMiter;
410 }
411
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800412 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700413 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
414 // get the delegate from the native int.
415 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
416 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700417 return;
418 }
419
420 delegate.mStrokeMiter = miter;
421 }
422
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800423 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700424 /*package*/ static void nSetShadowLayer(Paint thisPaint, float radius, float dx, float dy,
425 int color) {
426 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800427 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800428 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700429 }
430
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800431 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700432 /*package*/ static float getTextSize(Paint thisPaint) {
433 // get the delegate from the native int.
434 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
435 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700436 return 1.f;
437 }
438
439 return delegate.mTextSize;
440 }
441
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800442 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700443 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
444 // get the delegate from the native int.
445 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
446 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700447 return;
448 }
449
450 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800451 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700452 }
453
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800454 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700455 /*package*/ static float getTextScaleX(Paint thisPaint) {
456 // get the delegate from the native int.
457 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
458 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700459 return 1.f;
460 }
461
462 return delegate.mTextScaleX;
463 }
464
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800465 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700466 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
467 // get the delegate from the native int.
468 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
469 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700470 return;
471 }
472
473 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800474 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700475 }
476
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800477 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700478 /*package*/ static float getTextSkewX(Paint thisPaint) {
479 // get the delegate from the native int.
480 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
481 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700482 return 1.f;
483 }
484
485 return delegate.mTextSkewX;
486 }
487
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800488 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700489 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
490 // get the delegate from the native int.
491 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
492 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700493 return;
494 }
495
496 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800497 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700498 }
499
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800500 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700501 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800502 // get the delegate
503 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
504 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800505 return 0;
506 }
507
508 if (delegate.mFonts.size() > 0) {
509 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
510 // Android expects negative ascent so we invert the value from Java.
511 return - javaMetrics.getAscent();
512 }
513
514 return 0;
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 float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800519 // get the delegate
520 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
521 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800522 return 0;
523 }
524
525 if (delegate.mFonts.size() > 0) {
526 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
527 return javaMetrics.getDescent();
528 }
529
530 return 0;
531
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700532 }
533
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800534 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700535 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700536 // get the delegate
537 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
538 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700539 return 0;
540 }
541
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800542 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700543 }
544
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800545 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700546 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700547 // get the delegate
548 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
549 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700550 return 0;
551 }
552
553 if (delegate.mFonts.size() > 0) {
554 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
555 if (fmi != null) {
556 // Android expects negative ascent so we invert the value from Java.
557 fmi.top = - javaMetrics.getMaxAscent();
558 fmi.ascent = - javaMetrics.getAscent();
559 fmi.descent = javaMetrics.getDescent();
560 fmi.bottom = javaMetrics.getMaxDescent();
561 fmi.leading = javaMetrics.getLeading();
562 }
563
564 return javaMetrics.getHeight();
565 }
566
567 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700568 }
569
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800570 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700571 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
572 int count) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700573 // get the delegate
574 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
575 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700576 return 0;
577 }
578
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700579 return delegate.measureText(text, index, count);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700580 }
581
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800582 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700583 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end) {
584 return native_measureText(thisPaint, text.toCharArray(), start, end - start);
585 }
586
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800587 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700588 /*package*/ static float native_measureText(Paint thisPaint, String text) {
589 return native_measureText(thisPaint, text.toCharArray(), 0, text.length());
590 }
591
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800592 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700593 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
594 float maxWidth, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800595
596 // get the delegate
597 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
598 if (delegate == null) {
599 return 0;
600 }
601
602 int inc = count > 0 ? 1 : -1;
603
604 int measureIndex = 0;
605 float measureAcc = 0;
606 for (int i = index; i != index + count; i += inc, measureIndex++) {
607 int start, end;
608 if (i < index) {
609 start = i;
610 end = index;
611 } else {
612 start = index;
613 end = i;
614 }
615
616 // measure from start to end
617 float res = delegate.measureText(text, start, end - start + 1);
618
619 if (measuredWidth != null) {
620 measuredWidth[measureIndex] = res;
621 }
622
623 measureAcc += res;
624 if (res > maxWidth) {
625 // we should not return this char index, but since it's 0-based
626 // and we need to return a count, we simply return measureIndex;
627 return measureIndex;
628 }
629
630 }
631
632 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700633 }
634
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800635 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700636 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
637 float maxWidth, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800638 return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth,
639 measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700640 }
641
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800642 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700643 /*package*/ static int native_init() {
644 Paint_Delegate newDelegate = new Paint_Delegate();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800645 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700646 }
647
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800648 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700649 /*package*/ static int native_initWithPaint(int paint) {
650 // get the delegate from the native int.
651 Paint_Delegate delegate = sManager.getDelegate(paint);
652 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700653 return 0;
654 }
655
656 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800657 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700658 }
659
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800660 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700661 /*package*/ static void native_reset(int native_object) {
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.reset();
669 }
670
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800671 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700672 /*package*/ static void native_set(int native_dst, int native_src) {
673 // get the delegate from the native int.
674 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
675 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700676 return;
677 }
678
679 // get the delegate from the native int.
680 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
681 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700682 return;
683 }
684
685 delegate_dst.set(delegate_src);
686 }
687
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800688 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700689 /*package*/ static int native_getStyle(int native_object) {
690 // get the delegate from the native int.
691 Paint_Delegate delegate = sManager.getDelegate(native_object);
692 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700693 return 0;
694 }
695
696 return delegate.mStyle;
697 }
698
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800699 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700700 /*package*/ static void native_setStyle(int native_object, int style) {
701 // get the delegate from the native int.
702 Paint_Delegate delegate = sManager.getDelegate(native_object);
703 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700704 return;
705 }
706
707 delegate.mStyle = style;
708 }
709
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800710 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700711 /*package*/ static int native_getStrokeCap(int native_object) {
712 // get the delegate from the native int.
713 Paint_Delegate delegate = sManager.getDelegate(native_object);
714 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700715 return 0;
716 }
717
718 return delegate.mCap;
719 }
720
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800721 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700722 /*package*/ static void native_setStrokeCap(int native_object, int cap) {
723 // get the delegate from the native int.
724 Paint_Delegate delegate = sManager.getDelegate(native_object);
725 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700726 return;
727 }
728
729 delegate.mCap = cap;
730 }
731
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800732 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700733 /*package*/ static int native_getStrokeJoin(int native_object) {
734 // get the delegate from the native int.
735 Paint_Delegate delegate = sManager.getDelegate(native_object);
736 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700737 return 0;
738 }
739
740 return delegate.mJoin;
741 }
742
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800743 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700744 /*package*/ static void native_setStrokeJoin(int native_object, int join) {
745 // get the delegate from the native int.
746 Paint_Delegate delegate = sManager.getDelegate(native_object);
747 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700748 return;
749 }
750
751 delegate.mJoin = join;
752 }
753
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800754 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700755 /*package*/ static boolean native_getFillPath(int native_object, int src, int dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800756 Paint_Delegate paint = sManager.getDelegate(native_object);
757 if (paint == null) {
758 return false;
759 }
760
761 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
762 if (srcPath == null) {
763 return true;
764 }
765
766 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
767 if (dstPath == null) {
768 return true;
769 }
770
771 Stroke stroke = paint.getJavaStroke();
772 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
773
774 dstPath.setJavaShape(strokeShape);
775
776 // FIXME figure out the return value?
777 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700778 }
779
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800780 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700781 /*package*/ static int native_setShader(int native_object, int shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700782 // get the delegate from the native int.
783 Paint_Delegate delegate = sManager.getDelegate(native_object);
784 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700785 return shader;
786 }
787
Xavier Ducrohet91672792011-02-22 11:54:37 -0800788 delegate.mShader = Shader_Delegate.getDelegate(shader);
789
790 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700791 }
792
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800793 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700794 /*package*/ static int native_setColorFilter(int native_object, int filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700795 // get the delegate from the native int.
796 Paint_Delegate delegate = sManager.getDelegate(native_object);
797 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700798 return filter;
799 }
800
Xavier Ducrohet91672792011-02-22 11:54:37 -0800801 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800802
803 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800804 if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800805 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800806 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800807 }
808
809 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700810 }
811
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800812 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700813 /*package*/ static int native_setXfermode(int native_object, int xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700814 // get the delegate from the native int.
815 Paint_Delegate delegate = sManager.getDelegate(native_object);
816 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700817 return xfermode;
818 }
819
Xavier Ducrohet91672792011-02-22 11:54:37 -0800820 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
821
822 return xfermode;
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_setPathEffect(int native_object, int effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700827 // get the delegate from the native int.
828 Paint_Delegate delegate = sManager.getDelegate(native_object);
829 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700830 return effect;
831 }
832
Xavier Ducrohet91672792011-02-22 11:54:37 -0800833 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
834
835 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700836 }
837
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800838 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700839 /*package*/ static int native_setMaskFilter(int native_object, int maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700840 // get the delegate from the native int.
841 Paint_Delegate delegate = sManager.getDelegate(native_object);
842 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700843 return maskfilter;
844 }
845
Xavier Ducrohet91672792011-02-22 11:54:37 -0800846 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800847
848 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800849 if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800850 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800851 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800852 }
853
854 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700855 }
856
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800857 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700858 /*package*/ static int native_setTypeface(int native_object, int typeface) {
859 // get the delegate from the native int.
860 Paint_Delegate delegate = sManager.getDelegate(native_object);
861 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700862 return 0;
863 }
864
Xavier Ducrohet91672792011-02-22 11:54:37 -0800865 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800866 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800867 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700868 }
869
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800870 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700871 /*package*/ static int native_setRasterizer(int native_object, int rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800872 // get the delegate from the native int.
873 Paint_Delegate delegate = sManager.getDelegate(native_object);
874 if (delegate == null) {
875 return rasterizer;
876 }
877
Xavier Ducrohet91672792011-02-22 11:54:37 -0800878 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800879
880 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800881 if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800882 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800883 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800884 }
885
886 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700887 }
888
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800889 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700890 /*package*/ static int native_getTextAlign(int native_object) {
891 // get the delegate from the native int.
892 Paint_Delegate delegate = sManager.getDelegate(native_object);
893 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700894 return 0;
895 }
896
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700897 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700898 }
899
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800900 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700901 /*package*/ static void native_setTextAlign(int native_object, int align) {
902 // get the delegate from the native int.
903 Paint_Delegate delegate = sManager.getDelegate(native_object);
904 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700905 return;
906 }
907
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700908 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700909 }
910
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800911 @LayoutlibDelegate
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700912 /*package*/ static void native_setTextLocale(int native_object, String locale) {
913 // get the delegate from the native int.
914 Paint_Delegate delegate = sManager.getDelegate(native_object);
915 if (delegate == null) {
916 return;
917 }
918
919 delegate.setTextLocale(locale);
920 }
921
922 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700923 /*package*/ static int native_getTextWidths(int native_object, char[] text, int index,
924 int count, float[] widths) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800925 // get the delegate from the native int.
926 Paint_Delegate delegate = sManager.getDelegate(native_object);
927 if (delegate == null) {
928 return 0;
929 }
930
931 if (delegate.mFonts.size() > 0) {
932 // FIXME: handle multi-char characters (see measureText)
933 float totalAdvance = 0;
934 for (int i = 0; i < count; i++) {
935 char c = text[i + index];
936 boolean found = false;
937 for (FontInfo info : delegate.mFonts) {
938 if (info.mFont.canDisplay(c)) {
939 float adv = info.mMetrics.charWidth(c);
940 totalAdvance += adv;
941 if (widths != null) {
942 widths[i] = adv;
943 }
944
945 found = true;
946 break;
947 }
948 }
949
950 if (found == false) {
951 // no advance for this char.
952 if (widths != null) {
953 widths[i] = 0.f;
954 }
955 }
956 }
957
958 return (int) totalAdvance;
959 }
960
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -0800961 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700962 }
963
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800964 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700965 /*package*/ static int native_getTextWidths(int native_object, String text, int start,
966 int end, float[] widths) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800967 return native_getTextWidths(native_object, text.toCharArray(), start, end - start, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700968 }
969
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800970 @LayoutlibDelegate
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700971 /* package */static int native_getTextGlyphs(int native_object, String text, int start,
972 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
973 // FIXME
974 return 0;
975 }
976
977 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700978 /*package*/ static float native_getTextRunAdvances(int native_object,
979 char[] text, int index, int count, int contextIndex, int contextCount,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700980 int flags, float[] advances, int advancesIndex, int reserved) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700981 // get the delegate from the native int.
982 Paint_Delegate delegate = sManager.getDelegate(native_object);
983 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700984 return 0.f;
985 }
986
987 if (delegate.mFonts.size() > 0) {
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700988 // FIXME: handle multi-char characters (see measureText)
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700989 float totalAdvance = 0;
990 for (int i = 0; i < count; i++) {
991 char c = text[i + index];
992 boolean found = false;
993 for (FontInfo info : delegate.mFonts) {
994 if (info.mFont.canDisplay(c)) {
995 float adv = info.mMetrics.charWidth(c);
996 totalAdvance += adv;
997 if (advances != null) {
998 advances[i] = adv;
999 }
1000
1001 found = true;
1002 break;
1003 }
1004 }
1005
1006 if (found == false) {
1007 // no advance for this char.
1008 if (advances != null) {
1009 advances[i] = 0.f;
1010 }
1011 }
1012 }
1013
1014 return totalAdvance;
1015 }
1016
1017 return 0;
1018
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001019 }
1020
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001021 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001022 /*package*/ static float native_getTextRunAdvances(int native_object,
1023 String text, int start, int end, int contextStart, int contextEnd,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -07001024 int flags, float[] advances, int advancesIndex, int reserved) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001025 // FIXME: support contextStart, contextEnd and direction flag
1026 int count = end - start;
1027 char[] buffer = TemporaryBuffer.obtain(count);
1028 TextUtils.getChars(text, start, end, buffer, 0);
1029
1030 return native_getTextRunAdvances(native_object, buffer, 0, count, contextStart,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -07001031 contextEnd - contextStart, flags, advances, advancesIndex, reserved);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001032 }
1033
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001034 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001035 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, char[] text,
1036 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1037 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001038 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1039 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1040 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001041 }
1042
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001043 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001044 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, String text,
1045 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1046 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001047 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1048 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1049 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001050 }
1051
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001052 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001053 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
1054 char[] text, int index, int count, float x, float y, int path) {
1055 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001056 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1057 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001058 }
1059
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001060 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001061 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
1062 String text, int start, int end, float x, float y, int path) {
1063 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001064 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1065 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001066 }
1067
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001068 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001069 /*package*/ static void nativeGetStringBounds(int nativePaint, String text, int start,
1070 int end, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001071 nativeGetCharArrayBounds(nativePaint, text.toCharArray(), start, end - start, bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001072 }
1073
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001074 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001075 /*package*/ static void nativeGetCharArrayBounds(int nativePaint, char[] text, int index,
1076 int count, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001077
1078 // get the delegate from the native int.
1079 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1080 if (delegate == null) {
1081 return;
1082 }
1083
1084 // FIXME should test if the main font can display all those characters.
1085 // See MeasureText
1086 if (delegate.mFonts.size() > 0) {
1087 FontInfo mainInfo = delegate.mFonts.get(0);
1088
1089 Rectangle2D rect = mainInfo.mFont.getStringBounds(text, index, index + count,
1090 delegate.mFontContext);
1091 bounds.set(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
1092 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001093 }
1094
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001095 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001096 /*package*/ static void finalizer(int nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001097 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001098 }
1099
1100 // ---- Private delegate/helper methods ----
1101
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001102 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001103 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001104 }
1105
1106 private Paint_Delegate(Paint_Delegate paint) {
1107 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001108 }
1109
1110 private void set(Paint_Delegate paint) {
1111 mFlags = paint.mFlags;
1112 mColor = paint.mColor;
1113 mStyle = paint.mStyle;
1114 mCap = paint.mCap;
1115 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001116 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001117 mTypeface = paint.mTypeface;
1118 mStrokeWidth = paint.mStrokeWidth;
1119 mStrokeMiter = paint.mStrokeMiter;
1120 mTextSize = paint.mTextSize;
1121 mTextScaleX = paint.mTextScaleX;
1122 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001123 mXfermode = paint.mXfermode;
1124 mColorFilter = paint.mColorFilter;
1125 mShader = paint.mShader;
1126 mPathEffect = paint.mPathEffect;
1127 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001128 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001129 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001130 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001131 }
1132
1133 private void reset() {
1134 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001135 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001136 mStyle = Paint.Style.FILL.nativeInt;
1137 mCap = Paint.Cap.BUTT.nativeInt;
1138 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001139 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001140 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001141 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001142 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001143 mTextSize = 20.f;
1144 mTextScaleX = 1.f;
1145 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001146 mXfermode = null;
1147 mColorFilter = null;
1148 mShader = null;
1149 mPathEffect = null;
1150 mMaskFilter = null;
1151 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001152 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001153 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001154 }
1155
1156 /**
1157 * Update the {@link Font} object from the typeface, text size and scaling
1158 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001159 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001160 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001161 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001162 // Get the fonts from the TypeFace object.
Xavier Ducrohet91672792011-02-22 11:54:37 -08001163 List<Font> fonts = mTypeface.getFonts();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001164
1165 // create new font objects as well as FontMetrics, based on the current text size
1166 // and skew info.
1167 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1168 for (Font font : fonts) {
1169 FontInfo info = new FontInfo();
1170 info.mFont = font.deriveFont(mTextSize);
1171 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1172 // TODO: support skew
1173 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001174 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001175 }
1176 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1177
1178 infoList.add(info);
1179 }
1180
1181 mFonts = Collections.unmodifiableList(infoList);
1182 }
1183 }
1184
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001185 /*package*/ float measureText(char[] text, int index, int count) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001186
1187 // WARNING: the logic in this method is similar to Canvas_Delegate.native_drawText
1188 // Any change to this method should be reflected there as well
1189
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001190 if (mFonts.size() > 0) {
1191 FontInfo mainFont = mFonts.get(0);
1192 int i = index;
1193 int lastIndex = index + count;
1194 float total = 0f;
1195 while (i < lastIndex) {
1196 // always start with the main font.
1197 int upTo = mainFont.mFont.canDisplayUpTo(text, i, lastIndex);
1198 if (upTo == -1) {
1199 // shortcut to exit
1200 return total + mainFont.mMetrics.charsWidth(text, i, lastIndex - i);
1201 } else if (upTo > 0) {
1202 total += mainFont.mMetrics.charsWidth(text, i, upTo - i);
1203 i = upTo;
1204 // don't call continue at this point. Since it is certain the main font
1205 // cannot display the font a index upTo (now ==i), we move on to the
1206 // fallback fonts directly.
1207 }
1208
1209 // no char supported, attempt to read the next char(s) with the
1210 // fallback font. In this case we only test the first character
1211 // and then go back to test with the main font.
1212 // Special test for 2-char characters.
1213 boolean foundFont = false;
1214 for (int f = 1 ; f < mFonts.size() ; f++) {
1215 FontInfo fontInfo = mFonts.get(f);
1216
1217 // need to check that the font can display the character. We test
1218 // differently if the char is a high surrogate.
1219 int charCount = Character.isHighSurrogate(text[i]) ? 2 : 1;
1220 upTo = fontInfo.mFont.canDisplayUpTo(text, i, i + charCount);
1221 if (upTo == -1) {
1222 total += fontInfo.mMetrics.charsWidth(text, i, charCount);
1223 i += charCount;
1224 foundFont = true;
1225 break;
1226
1227 }
1228 }
1229
1230 // in case no font can display the char, measure it with the main font.
1231 if (foundFont == false) {
1232 int size = Character.isHighSurrogate(text[i]) ? 2 : 1;
1233 total += mainFont.mMetrics.charsWidth(text, i, size);
1234 i += size;
1235 }
1236 }
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001237
1238 return total;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001239 }
1240
1241 return 0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001242 }
1243
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001244 private float getFontMetrics(FontMetrics metrics) {
1245 if (mFonts.size() > 0) {
1246 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1247 if (metrics != null) {
1248 // Android expects negative ascent so we invert the value from Java.
1249 metrics.top = - javaMetrics.getMaxAscent();
1250 metrics.ascent = - javaMetrics.getAscent();
1251 metrics.descent = javaMetrics.getDescent();
1252 metrics.bottom = javaMetrics.getMaxDescent();
1253 metrics.leading = javaMetrics.getLeading();
1254 }
1255
1256 return javaMetrics.getHeight();
1257 }
1258
1259 return 0;
1260 }
1261
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001262 private void setTextLocale(String locale) {
1263 mLocale = new Locale(locale);
1264 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001265
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001266 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1267 // get the delegate from the native int.
1268 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1269 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001270 return;
1271 }
1272
1273 if (flagValue) {
1274 delegate.mFlags |= flagMask;
1275 } else {
1276 delegate.mFlags &= ~flagMask;
1277 }
1278 }
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001279
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001280}