blob: 4ad1a175853bdb571fe694ab6818b30a691701fc [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;
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070038import java.util.Locale;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070039
40/**
41 * Delegate implementing the native methods of android.graphics.Paint
42 *
43 * Through the layoutlib_create tool, the original native methods of Paint have been replaced
44 * by calls to methods of the same name in this delegate class.
45 *
46 * This class behaves like the original native implementation, but in Java, keeping previously
47 * native data into its own objects and mapping them to int that are sent back and forth between
48 * it and the original Paint class.
49 *
50 * @see DelegateManager
51 *
52 */
53public class Paint_Delegate {
54
55 /**
56 * Class associating a {@link Font} and it's {@link java.awt.FontMetrics}.
57 */
Xavier Ducrohet37f21802010-11-01 16:17:18 -070058 /*package*/ static final class FontInfo {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070059 Font mFont;
60 java.awt.FontMetrics mMetrics;
61 }
62
63 // ---- delegate manager ----
64 private static final DelegateManager<Paint_Delegate> sManager =
Xavier Ducrohetb2de8392011-02-23 16:51:08 -080065 new DelegateManager<Paint_Delegate>(Paint_Delegate.class);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070066
67 // ---- delegate helper data ----
68 private List<FontInfo> mFonts;
69 private final FontRenderContext mFontContext = new FontRenderContext(
70 new AffineTransform(), true, true);
71
72 // ---- delegate data ----
73 private int mFlags;
74 private int mColor;
75 private int mStyle;
76 private int mCap;
77 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070078 private int mTextAlign;
Xavier Ducrohet91672792011-02-22 11:54:37 -080079 private Typeface_Delegate mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070080 private float mStrokeWidth;
81 private float mStrokeMiter;
82 private float mTextSize;
83 private float mTextScaleX;
84 private float mTextSkewX;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -070085 private int mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070086
Xavier Ducrohet91672792011-02-22 11:54:37 -080087 private Xfermode_Delegate mXfermode;
88 private ColorFilter_Delegate mColorFilter;
89 private Shader_Delegate mShader;
90 private PathEffect_Delegate mPathEffect;
91 private MaskFilter_Delegate mMaskFilter;
92 private Rasterizer_Delegate mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070093
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070094 private Locale mLocale = Locale.getDefault();
95
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070096
97 // ---- Public Helper methods ----
98
Xavier Ducrohet37f21802010-11-01 16:17:18 -070099 public static Paint_Delegate getDelegate(int native_paint) {
100 return sManager.getDelegate(native_paint);
101 }
102
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700103 /**
104 * Returns the list of {@link Font} objects. The first item is the main font, the rest
105 * are fall backs for characters not present in the main font.
106 */
107 public List<FontInfo> getFonts() {
108 return mFonts;
109 }
110
Xavier Ducroheta313b652010-11-01 18:45:20 -0700111 public boolean isAntiAliased() {
112 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
113 }
114
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700115 public boolean isFilterBitmap() {
116 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
117 }
118
119 public int getStyle() {
120 return mStyle;
121 }
122
123 public int getColor() {
124 return mColor;
125 }
126
Xavier Ducrohet66225222010-12-21 01:33:04 -0800127 public int getAlpha() {
128 return mColor >>> 24;
129 }
130
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800131 public void setAlpha(int alpha) {
132 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
133 }
134
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700135 public int getTextAlign() {
136 return mTextAlign;
137 }
138
139 public float getStrokeWidth() {
140 return mStrokeWidth;
141 }
142
Xavier Ducrohet66225222010-12-21 01:33:04 -0800143 /**
144 * returns the value of stroke miter needed by the java api.
145 */
146 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800147 float miter = mStrokeMiter * mStrokeWidth;
148 if (miter < 1.f) {
149 miter = 1.f;
150 }
151 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700152 }
153
154 public int getJavaCap() {
155 switch (Paint.sCapArray[mCap]) {
156 case BUTT:
157 return BasicStroke.CAP_BUTT;
158 case ROUND:
159 return BasicStroke.CAP_ROUND;
160 default:
161 case SQUARE:
162 return BasicStroke.CAP_SQUARE;
163 }
164 }
165
166 public int getJavaJoin() {
167 switch (Paint.sJoinArray[mJoin]) {
168 default:
169 case MITER:
170 return BasicStroke.JOIN_MITER;
171 case ROUND:
172 return BasicStroke.JOIN_ROUND;
173 case BEVEL:
174 return BasicStroke.JOIN_BEVEL;
175 }
176 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700177
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800178 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800179 if (mPathEffect != null) {
180 if (mPathEffect.isSupported()) {
181 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800182 assert stroke != null;
183 if (stroke != null) {
184 return stroke;
185 }
186 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800187 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800188 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800189 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800190 }
191 }
192
193 // if no custom stroke as been set, set the default one.
194 return new BasicStroke(
195 getStrokeWidth(),
196 getJavaCap(),
197 getJavaJoin(),
198 getJavaStrokeMiter());
199 }
200
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800201 /**
202 * Returns the {@link Xfermode} delegate or null if none have been set
203 *
204 * @return the delegate or null.
205 */
206 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800207 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700208 }
209
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800210 /**
211 * Returns the {@link ColorFilter} delegate or null if none have been set
212 *
213 * @return the delegate or null.
214 */
215 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800216 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700217 }
218
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800219 /**
220 * Returns the {@link Shader} delegate or null if none have been set
221 *
222 * @return the delegate or null.
223 */
224 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800225 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700226 }
227
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800228 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800229 * Returns the {@link MaskFilter} delegate or null if none have been set
230 *
231 * @return the delegate or null.
232 */
233 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800234 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800235 }
236
237 /**
238 * Returns the {@link Rasterizer} delegate or null if none have been set
239 *
240 * @return the delegate or null.
241 */
242 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800243 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700244 }
245
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700246 // ---- native methods ----
247
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800248 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700249 /*package*/ static int getFlags(Paint thisPaint) {
250 // get the delegate from the native int.
251 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
252 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700253 return 0;
254 }
255
256 return delegate.mFlags;
257 }
258
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700259
260
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800261 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700262 /*package*/ static void setFlags(Paint thisPaint, int flags) {
263 // get the delegate from the native int.
264 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
265 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700266 return;
267 }
268
269 delegate.mFlags = flags;
270 }
271
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800272 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700273 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700274 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700275 }
276
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800277 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700278 /*package*/ static int getHinting(Paint thisPaint) {
279 // get the delegate from the native int.
280 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
281 if (delegate == null) {
282 return Paint.HINTING_ON;
283 }
284
285 return delegate.mHintingMode;
286 }
287
288 @LayoutlibDelegate
289 /*package*/ static void setHinting(Paint thisPaint, int mode) {
290 // get the delegate from the native int.
291 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
292 if (delegate == null) {
293 return;
294 }
295
296 delegate.mHintingMode = mode;
297 }
298
299 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700300 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
301 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
302 }
303
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800304 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700305 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
306 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
307 }
308
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800309 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700310 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
311 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
312 }
313
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800314 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700315 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
316 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
317 }
318
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800319 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700320 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
321 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
322 }
323
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800324 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700325 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
326 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
327 }
328
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800329 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700330 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
331 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
332 }
333
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800334 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700335 /*package*/ static int getColor(Paint thisPaint) {
336 // get the delegate from the native int.
337 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
338 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700339 return 0;
340 }
341
342 return delegate.mColor;
343 }
344
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800345 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700346 /*package*/ static void setColor(Paint thisPaint, int color) {
347 // get the delegate from the native int.
348 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
349 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700350 return;
351 }
352
353 delegate.mColor = color;
354 }
355
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800356 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700357 /*package*/ static int getAlpha(Paint thisPaint) {
358 // get the delegate from the native int.
359 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
360 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700361 return 0;
362 }
363
Xavier Ducrohet66225222010-12-21 01:33:04 -0800364 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700365 }
366
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800367 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700368 /*package*/ static void setAlpha(Paint thisPaint, int a) {
369 // get the delegate from the native int.
370 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
371 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700372 return;
373 }
374
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800375 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700376 }
377
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800378 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700379 /*package*/ static float getStrokeWidth(Paint thisPaint) {
380 // get the delegate from the native int.
381 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
382 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700383 return 1.f;
384 }
385
386 return delegate.mStrokeWidth;
387 }
388
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800389 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700390 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
391 // get the delegate from the native int.
392 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
393 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700394 return;
395 }
396
397 delegate.mStrokeWidth = width;
398 }
399
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800400 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700401 /*package*/ static float getStrokeMiter(Paint thisPaint) {
402 // get the delegate from the native int.
403 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
404 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700405 return 1.f;
406 }
407
408 return delegate.mStrokeMiter;
409 }
410
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800411 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700412 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
413 // get the delegate from the native int.
414 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
415 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700416 return;
417 }
418
419 delegate.mStrokeMiter = miter;
420 }
421
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800422 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700423 /*package*/ static void nSetShadowLayer(Paint thisPaint, float radius, float dx, float dy,
424 int color) {
425 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800426 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800427 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700428 }
429
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800430 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700431 /*package*/ static float getTextSize(Paint thisPaint) {
432 // get the delegate from the native int.
433 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
434 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700435 return 1.f;
436 }
437
438 return delegate.mTextSize;
439 }
440
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800441 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700442 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
443 // get the delegate from the native int.
444 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
445 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700446 return;
447 }
448
449 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800450 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700451 }
452
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800453 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700454 /*package*/ static float getTextScaleX(Paint thisPaint) {
455 // get the delegate from the native int.
456 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
457 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700458 return 1.f;
459 }
460
461 return delegate.mTextScaleX;
462 }
463
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800464 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700465 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
466 // get the delegate from the native int.
467 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
468 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700469 return;
470 }
471
472 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800473 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700474 }
475
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800476 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700477 /*package*/ static float getTextSkewX(Paint thisPaint) {
478 // get the delegate from the native int.
479 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
480 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700481 return 1.f;
482 }
483
484 return delegate.mTextSkewX;
485 }
486
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800487 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700488 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
489 // get the delegate from the native int.
490 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
491 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700492 return;
493 }
494
495 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800496 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700497 }
498
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800499 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700500 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800501 // get the delegate
502 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
503 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800504 return 0;
505 }
506
507 if (delegate.mFonts.size() > 0) {
508 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
509 // Android expects negative ascent so we invert the value from Java.
510 return - javaMetrics.getAscent();
511 }
512
513 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700514 }
515
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800516 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700517 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800518 // get the delegate
519 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
520 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800521 return 0;
522 }
523
524 if (delegate.mFonts.size() > 0) {
525 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
526 return javaMetrics.getDescent();
527 }
528
529 return 0;
530
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700531 }
532
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800533 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700534 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700535 // get the delegate
536 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
537 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700538 return 0;
539 }
540
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800541 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700542 }
543
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800544 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700545 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700546 // get the delegate
547 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
548 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700549 return 0;
550 }
551
552 if (delegate.mFonts.size() > 0) {
553 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
554 if (fmi != null) {
555 // Android expects negative ascent so we invert the value from Java.
556 fmi.top = - javaMetrics.getMaxAscent();
557 fmi.ascent = - javaMetrics.getAscent();
558 fmi.descent = javaMetrics.getDescent();
559 fmi.bottom = javaMetrics.getMaxDescent();
560 fmi.leading = javaMetrics.getLeading();
561 }
562
563 return javaMetrics.getHeight();
564 }
565
566 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700567 }
568
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800569 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700570 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700571 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700572 // get the delegate
573 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
574 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700575 return 0;
576 }
577
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800578 RectF bounds = delegate.measureText(text, index, count, isRtl(bidiFlags));
579 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700580 }
581
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800582 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700583 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
584 int bidiFlags) {
585 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700586 }
587
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800588 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700589 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
590 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700591 }
592
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800593 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700594 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700595 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800596
597 // get the delegate
598 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
599 if (delegate == null) {
600 return 0;
601 }
602
603 int inc = count > 0 ? 1 : -1;
604
605 int measureIndex = 0;
606 float measureAcc = 0;
607 for (int i = index; i != index + count; i += inc, measureIndex++) {
608 int start, end;
609 if (i < index) {
610 start = i;
611 end = index;
612 } else {
613 start = index;
614 end = i;
615 }
616
617 // measure from start to end
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800618 RectF bounds = delegate.measureText(text, start, end - start + 1, isRtl(bidiFlags));
619 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800620
621 if (measuredWidth != null) {
622 measuredWidth[measureIndex] = res;
623 }
624
625 measureAcc += res;
626 if (res > maxWidth) {
627 // we should not return this char index, but since it's 0-based
628 // and we need to return a count, we simply return measureIndex;
629 return measureIndex;
630 }
631
632 }
633
634 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700635 }
636
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800637 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700638 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700639 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800640 return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700641 bidiFlags, measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700642 }
643
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800644 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700645 /*package*/ static int native_init() {
646 Paint_Delegate newDelegate = new Paint_Delegate();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800647 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700648 }
649
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800650 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700651 /*package*/ static int native_initWithPaint(int paint) {
652 // get the delegate from the native int.
653 Paint_Delegate delegate = sManager.getDelegate(paint);
654 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700655 return 0;
656 }
657
658 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800659 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700660 }
661
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800662 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700663 /*package*/ static void native_reset(int native_object) {
664 // get the delegate from the native int.
665 Paint_Delegate delegate = sManager.getDelegate(native_object);
666 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700667 return;
668 }
669
670 delegate.reset();
671 }
672
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800673 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700674 /*package*/ static void native_set(int native_dst, int native_src) {
675 // get the delegate from the native int.
676 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
677 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700678 return;
679 }
680
681 // get the delegate from the native int.
682 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
683 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700684 return;
685 }
686
687 delegate_dst.set(delegate_src);
688 }
689
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800690 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700691 /*package*/ static int native_getStyle(int native_object) {
692 // get the delegate from the native int.
693 Paint_Delegate delegate = sManager.getDelegate(native_object);
694 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700695 return 0;
696 }
697
698 return delegate.mStyle;
699 }
700
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800701 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700702 /*package*/ static void native_setStyle(int native_object, int style) {
703 // get the delegate from the native int.
704 Paint_Delegate delegate = sManager.getDelegate(native_object);
705 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700706 return;
707 }
708
709 delegate.mStyle = style;
710 }
711
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800712 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700713 /*package*/ static int native_getStrokeCap(int native_object) {
714 // get the delegate from the native int.
715 Paint_Delegate delegate = sManager.getDelegate(native_object);
716 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700717 return 0;
718 }
719
720 return delegate.mCap;
721 }
722
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800723 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700724 /*package*/ static void native_setStrokeCap(int native_object, int cap) {
725 // get the delegate from the native int.
726 Paint_Delegate delegate = sManager.getDelegate(native_object);
727 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700728 return;
729 }
730
731 delegate.mCap = cap;
732 }
733
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800734 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700735 /*package*/ static int native_getStrokeJoin(int native_object) {
736 // get the delegate from the native int.
737 Paint_Delegate delegate = sManager.getDelegate(native_object);
738 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700739 return 0;
740 }
741
742 return delegate.mJoin;
743 }
744
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800745 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700746 /*package*/ static void native_setStrokeJoin(int native_object, int join) {
747 // get the delegate from the native int.
748 Paint_Delegate delegate = sManager.getDelegate(native_object);
749 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700750 return;
751 }
752
753 delegate.mJoin = join;
754 }
755
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800756 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700757 /*package*/ static boolean native_getFillPath(int native_object, int src, int dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800758 Paint_Delegate paint = sManager.getDelegate(native_object);
759 if (paint == null) {
760 return false;
761 }
762
763 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
764 if (srcPath == null) {
765 return true;
766 }
767
768 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
769 if (dstPath == null) {
770 return true;
771 }
772
773 Stroke stroke = paint.getJavaStroke();
774 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
775
776 dstPath.setJavaShape(strokeShape);
777
778 // FIXME figure out the return value?
779 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700780 }
781
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800782 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700783 /*package*/ static int native_setShader(int native_object, int shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700784 // get the delegate from the native int.
785 Paint_Delegate delegate = sManager.getDelegate(native_object);
786 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700787 return shader;
788 }
789
Xavier Ducrohet91672792011-02-22 11:54:37 -0800790 delegate.mShader = Shader_Delegate.getDelegate(shader);
791
792 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700793 }
794
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800795 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700796 /*package*/ static int native_setColorFilter(int native_object, int filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700797 // get the delegate from the native int.
798 Paint_Delegate delegate = sManager.getDelegate(native_object);
799 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700800 return filter;
801 }
802
Xavier Ducrohet91672792011-02-22 11:54:37 -0800803 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800804
805 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800806 if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800807 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800808 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800809 }
810
811 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700812 }
813
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800814 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700815 /*package*/ static int native_setXfermode(int native_object, int xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700816 // get the delegate from the native int.
817 Paint_Delegate delegate = sManager.getDelegate(native_object);
818 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700819 return xfermode;
820 }
821
Xavier Ducrohet91672792011-02-22 11:54:37 -0800822 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
823
824 return xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700825 }
826
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800827 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700828 /*package*/ static int native_setPathEffect(int native_object, int effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700829 // get the delegate from the native int.
830 Paint_Delegate delegate = sManager.getDelegate(native_object);
831 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700832 return effect;
833 }
834
Xavier Ducrohet91672792011-02-22 11:54:37 -0800835 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
836
837 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700838 }
839
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800840 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700841 /*package*/ static int native_setMaskFilter(int native_object, int maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700842 // get the delegate from the native int.
843 Paint_Delegate delegate = sManager.getDelegate(native_object);
844 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700845 return maskfilter;
846 }
847
Xavier Ducrohet91672792011-02-22 11:54:37 -0800848 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800849
850 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800851 if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800852 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800853 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800854 }
855
856 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700857 }
858
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800859 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700860 /*package*/ static int native_setTypeface(int native_object, int typeface) {
861 // get the delegate from the native int.
862 Paint_Delegate delegate = sManager.getDelegate(native_object);
863 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700864 return 0;
865 }
866
Xavier Ducrohet91672792011-02-22 11:54:37 -0800867 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800868 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800869 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700870 }
871
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800872 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700873 /*package*/ static int native_setRasterizer(int native_object, int rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800874 // get the delegate from the native int.
875 Paint_Delegate delegate = sManager.getDelegate(native_object);
876 if (delegate == null) {
877 return rasterizer;
878 }
879
Xavier Ducrohet91672792011-02-22 11:54:37 -0800880 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800881
882 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800883 if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800884 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800885 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800886 }
887
888 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700889 }
890
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800891 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700892 /*package*/ static int native_getTextAlign(int native_object) {
893 // get the delegate from the native int.
894 Paint_Delegate delegate = sManager.getDelegate(native_object);
895 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700896 return 0;
897 }
898
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700899 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700900 }
901
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800902 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700903 /*package*/ static void native_setTextAlign(int native_object, int align) {
904 // get the delegate from the native int.
905 Paint_Delegate delegate = sManager.getDelegate(native_object);
906 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700907 return;
908 }
909
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700910 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700911 }
912
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800913 @LayoutlibDelegate
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700914 /*package*/ static void native_setTextLocale(int native_object, String locale) {
915 // get the delegate from the native int.
916 Paint_Delegate delegate = sManager.getDelegate(native_object);
917 if (delegate == null) {
918 return;
919 }
920
921 delegate.setTextLocale(locale);
922 }
923
924 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700925 /*package*/ static int native_getTextWidths(int native_object, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700926 int count, int bidiFlags, float[] widths) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800927 // get the delegate from the native int.
928 Paint_Delegate delegate = sManager.getDelegate(native_object);
929 if (delegate == null) {
930 return 0;
931 }
932
933 if (delegate.mFonts.size() > 0) {
934 // FIXME: handle multi-char characters (see measureText)
935 float totalAdvance = 0;
936 for (int i = 0; i < count; i++) {
937 char c = text[i + index];
938 boolean found = false;
939 for (FontInfo info : delegate.mFonts) {
940 if (info.mFont.canDisplay(c)) {
941 float adv = info.mMetrics.charWidth(c);
942 totalAdvance += adv;
943 if (widths != null) {
944 widths[i] = adv;
945 }
946
947 found = true;
948 break;
949 }
950 }
951
952 if (found == false) {
953 // no advance for this char.
954 if (widths != null) {
955 widths[i] = 0.f;
956 }
957 }
958 }
959
960 return (int) totalAdvance;
961 }
962
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -0800963 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700964 }
965
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800966 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700967 /*package*/ static int native_getTextWidths(int native_object, String text, int start,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700968 int end, int bidiFlags, float[] widths) {
969 return native_getTextWidths(native_object, text.toCharArray(), start, end - start,
970 bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700971 }
972
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800973 @LayoutlibDelegate
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700974 /* package */static int native_getTextGlyphs(int native_object, String text, int start,
975 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
976 // FIXME
977 return 0;
978 }
979
980 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700981 /*package*/ static float native_getTextRunAdvances(int native_object,
982 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700983 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700984
985 if (advances != null)
986 for (int i = advancesIndex; i< advancesIndex+count; i++)
987 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700988 // get the delegate from the native int.
989 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700990 if (delegate == null || delegate.mFonts == null || delegate.mFonts.size() == 0) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700991 return 0.f;
992 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700993 boolean isRtl = isRtl(flags);
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700994
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700995 int limit = index + count;
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800996 RectF bounds = new BidiRenderer(null, delegate, text).renderText(
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700997 index, limit, isRtl, advances, advancesIndex, false, 0, 0);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800998 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700999 }
1000
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001001 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001002 /*package*/ static float native_getTextRunAdvances(int native_object,
1003 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001004 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001005 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001006 int count = end - start;
1007 char[] buffer = TemporaryBuffer.obtain(count);
1008 TextUtils.getChars(text, start, end, buffer, 0);
1009
1010 return native_getTextRunAdvances(native_object, buffer, 0, count, contextStart,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001011 contextEnd - contextStart, flags, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001012 }
1013
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001014 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001015 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, char[] text,
1016 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1017 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001018 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1019 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1020 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001021 }
1022
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001023 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001024 /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, String text,
1025 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1026 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001027 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1028 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1029 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001030 }
1031
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001032 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001033 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
1034 char[] text, int index, int count, float x, float y, int path) {
1035 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001036 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1037 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001038 }
1039
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001040 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001041 /*package*/ static void native_getTextPath(int native_object, int bidiFlags,
1042 String text, int start, int end, float x, float y, int path) {
1043 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001044 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1045 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001046 }
1047
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001048 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001049 /*package*/ static void nativeGetStringBounds(int nativePaint, String text, int start,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001050 int end, int bidiFlags, Rect bounds) {
1051 nativeGetCharArrayBounds(nativePaint, text.toCharArray(), start, end - start, bidiFlags,
1052 bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001053 }
1054
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001055 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001056 /*package*/ static void nativeGetCharArrayBounds(int nativePaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001057 int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001058
1059 // get the delegate from the native int.
1060 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001061 if (delegate == null || delegate.mFonts == null || delegate.mFonts.size() == 0) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001062 return;
1063 }
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001064 delegate.measureText(text, index, count, isRtl(bidiFlags)).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001065 }
1066
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001067 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001068 /*package*/ static void finalizer(int nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001069 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001070 }
1071
1072 // ---- Private delegate/helper methods ----
1073
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001074 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001075 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001076 }
1077
1078 private Paint_Delegate(Paint_Delegate paint) {
1079 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001080 }
1081
1082 private void set(Paint_Delegate paint) {
1083 mFlags = paint.mFlags;
1084 mColor = paint.mColor;
1085 mStyle = paint.mStyle;
1086 mCap = paint.mCap;
1087 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001088 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001089 mTypeface = paint.mTypeface;
1090 mStrokeWidth = paint.mStrokeWidth;
1091 mStrokeMiter = paint.mStrokeMiter;
1092 mTextSize = paint.mTextSize;
1093 mTextScaleX = paint.mTextScaleX;
1094 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001095 mXfermode = paint.mXfermode;
1096 mColorFilter = paint.mColorFilter;
1097 mShader = paint.mShader;
1098 mPathEffect = paint.mPathEffect;
1099 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001100 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001101 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001102 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001103 }
1104
1105 private void reset() {
1106 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001107 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001108 mStyle = Paint.Style.FILL.nativeInt;
1109 mCap = Paint.Cap.BUTT.nativeInt;
1110 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001111 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001112 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001113 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001114 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001115 mTextSize = 20.f;
1116 mTextScaleX = 1.f;
1117 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001118 mXfermode = null;
1119 mColorFilter = null;
1120 mShader = null;
1121 mPathEffect = null;
1122 mMaskFilter = null;
1123 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001124 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001125 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001126 }
1127
1128 /**
1129 * Update the {@link Font} object from the typeface, text size and scaling
1130 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001131 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001132 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001133 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001134 // Get the fonts from the TypeFace object.
Xavier Ducrohet91672792011-02-22 11:54:37 -08001135 List<Font> fonts = mTypeface.getFonts();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001136
1137 // create new font objects as well as FontMetrics, based on the current text size
1138 // and skew info.
1139 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1140 for (Font font : fonts) {
1141 FontInfo info = new FontInfo();
1142 info.mFont = font.deriveFont(mTextSize);
1143 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1144 // TODO: support skew
1145 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001146 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001147 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001148 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001149 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1150
1151 infoList.add(info);
1152 }
1153
1154 mFonts = Collections.unmodifiableList(infoList);
1155 }
1156 }
1157
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001158 /*package*/ RectF measureText(char[] text, int index, int count, boolean isRtl) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001159 return new BidiRenderer(null, this, text).renderText(
1160 index, index + count, isRtl, null, 0, false, 0, 0);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001161 }
1162
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001163 private float getFontMetrics(FontMetrics metrics) {
1164 if (mFonts.size() > 0) {
1165 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1166 if (metrics != null) {
1167 // Android expects negative ascent so we invert the value from Java.
1168 metrics.top = - javaMetrics.getMaxAscent();
1169 metrics.ascent = - javaMetrics.getAscent();
1170 metrics.descent = javaMetrics.getDescent();
1171 metrics.bottom = javaMetrics.getMaxDescent();
1172 metrics.leading = javaMetrics.getLeading();
1173 }
1174
1175 return javaMetrics.getHeight();
1176 }
1177
1178 return 0;
1179 }
1180
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001181 private void setTextLocale(String locale) {
1182 mLocale = new Locale(locale);
1183 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001184
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001185 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1186 // get the delegate from the native int.
1187 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1188 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001189 return;
1190 }
1191
1192 if (flagValue) {
1193 delegate.mFlags |= flagMask;
1194 } else {
1195 delegate.mFlags &= ~flagMask;
1196 }
1197 }
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001198
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001199 private static boolean isRtl(int flag) {
1200 switch(flag) {
1201 case Paint.BIDI_RTL:
1202 case Paint.BIDI_FORCE_RTL:
1203 case Paint.BIDI_DEFAULT_RTL:
1204 return true;
1205 default:
1206 return false;
1207 }
1208 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001209}