blob: 25eaaf5968802d5875bbb7b154e6fe7b64ff5be7 [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
Narayan Kamath633d6882014-01-27 14:24:16 +000099 public static Paint_Delegate getDelegate(long native_paint) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700100 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
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700431 /*package*/ static boolean isElegantTextHeight(Paint thisPaint) {
432 return false;
433 }
434
435 @LayoutlibDelegate
436 /*package*/ static void setElegantTextHeight(Paint thisPaint, boolean elegant) {
437 // TODO
438 }
439
440 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700441 /*package*/ static float getTextSize(Paint thisPaint) {
442 // get the delegate from the native int.
443 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
444 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700445 return 1.f;
446 }
447
448 return delegate.mTextSize;
449 }
450
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800451 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700452 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
453 // get the delegate from the native int.
454 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
455 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700456 return;
457 }
458
459 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800460 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700461 }
462
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800463 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700464 /*package*/ static float getTextScaleX(Paint thisPaint) {
465 // get the delegate from the native int.
466 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
467 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700468 return 1.f;
469 }
470
471 return delegate.mTextScaleX;
472 }
473
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800474 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700475 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
476 // get the delegate from the native int.
477 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
478 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700479 return;
480 }
481
482 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800483 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700484 }
485
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800486 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700487 /*package*/ static float getTextSkewX(Paint thisPaint) {
488 // get the delegate from the native int.
489 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
490 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700491 return 1.f;
492 }
493
494 return delegate.mTextSkewX;
495 }
496
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800497 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700498 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
499 // get the delegate from the native int.
500 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
501 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700502 return;
503 }
504
505 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800506 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700507 }
508
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800509 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700510 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800511 // get the delegate
512 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
513 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800514 return 0;
515 }
516
517 if (delegate.mFonts.size() > 0) {
518 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
519 // Android expects negative ascent so we invert the value from Java.
520 return - javaMetrics.getAscent();
521 }
522
523 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700524 }
525
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800526 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700527 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800528 // get the delegate
529 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
530 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800531 return 0;
532 }
533
534 if (delegate.mFonts.size() > 0) {
535 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
536 return javaMetrics.getDescent();
537 }
538
539 return 0;
540
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700541 }
542
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800543 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700544 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700545 // get the delegate
546 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
547 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700548 return 0;
549 }
550
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800551 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700552 }
553
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800554 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700555 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700556 // get the delegate
557 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
558 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700559 return 0;
560 }
561
562 if (delegate.mFonts.size() > 0) {
563 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
564 if (fmi != null) {
565 // Android expects negative ascent so we invert the value from Java.
566 fmi.top = - javaMetrics.getMaxAscent();
567 fmi.ascent = - javaMetrics.getAscent();
568 fmi.descent = javaMetrics.getDescent();
569 fmi.bottom = javaMetrics.getMaxDescent();
570 fmi.leading = javaMetrics.getLeading();
571 }
572
573 return javaMetrics.getHeight();
574 }
575
576 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700577 }
578
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800579 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700580 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700581 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700582 // get the delegate
583 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
584 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700585 return 0;
586 }
587
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800588 RectF bounds = delegate.measureText(text, index, count, isRtl(bidiFlags));
589 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700590 }
591
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800592 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700593 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
594 int bidiFlags) {
595 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700596 }
597
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800598 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700599 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
600 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700601 }
602
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800603 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700604 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700605 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800606
607 // get the delegate
608 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
609 if (delegate == null) {
610 return 0;
611 }
612
613 int inc = count > 0 ? 1 : -1;
614
615 int measureIndex = 0;
616 float measureAcc = 0;
617 for (int i = index; i != index + count; i += inc, measureIndex++) {
618 int start, end;
619 if (i < index) {
620 start = i;
621 end = index;
622 } else {
623 start = index;
624 end = i;
625 }
626
627 // measure from start to end
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800628 RectF bounds = delegate.measureText(text, start, end - start + 1, isRtl(bidiFlags));
629 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800630
631 if (measuredWidth != null) {
632 measuredWidth[measureIndex] = res;
633 }
634
635 measureAcc += res;
636 if (res > maxWidth) {
637 // we should not return this char index, but since it's 0-based
638 // and we need to return a count, we simply return measureIndex;
639 return measureIndex;
640 }
641
642 }
643
644 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700645 }
646
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800647 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700648 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700649 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800650 return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700651 bidiFlags, measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700652 }
653
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800654 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000655 /*package*/ static long native_init() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700656 Paint_Delegate newDelegate = new Paint_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
Narayan Kamath633d6882014-01-27 14:24:16 +0000661 /*package*/ static long native_initWithPaint(long paint) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700662 // get the delegate from the native int.
663 Paint_Delegate delegate = sManager.getDelegate(paint);
664 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700665 return 0;
666 }
667
668 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800669 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700670 }
671
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800672 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000673 /*package*/ static void native_reset(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700674 // get the delegate from the native int.
675 Paint_Delegate delegate = sManager.getDelegate(native_object);
676 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700677 return;
678 }
679
680 delegate.reset();
681 }
682
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800683 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000684 /*package*/ static void native_set(long native_dst, long native_src) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700685 // get the delegate from the native int.
686 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
687 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700688 return;
689 }
690
691 // get the delegate from the native int.
692 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
693 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700694 return;
695 }
696
697 delegate_dst.set(delegate_src);
698 }
699
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800700 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800701 /*package*/ static int native_getStyle(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700702 // get the delegate from the native int.
703 Paint_Delegate delegate = sManager.getDelegate(native_object);
704 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700705 return 0;
706 }
707
708 return delegate.mStyle;
709 }
710
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800711 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000712 /*package*/ static void native_setStyle(long native_object, int style) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700713 // get the delegate from the native int.
714 Paint_Delegate delegate = sManager.getDelegate(native_object);
715 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700716 return;
717 }
718
719 delegate.mStyle = style;
720 }
721
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800722 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800723 /*package*/ static int native_getStrokeCap(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700724 // get the delegate from the native int.
725 Paint_Delegate delegate = sManager.getDelegate(native_object);
726 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700727 return 0;
728 }
729
730 return delegate.mCap;
731 }
732
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800733 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000734 /*package*/ static void native_setStrokeCap(long native_object, int cap) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700735 // get the delegate from the native int.
736 Paint_Delegate delegate = sManager.getDelegate(native_object);
737 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700738 return;
739 }
740
741 delegate.mCap = cap;
742 }
743
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800744 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800745 /*package*/ static int native_getStrokeJoin(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700746 // get the delegate from the native int.
747 Paint_Delegate delegate = sManager.getDelegate(native_object);
748 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700749 return 0;
750 }
751
752 return delegate.mJoin;
753 }
754
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800755 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000756 /*package*/ static void native_setStrokeJoin(long native_object, int join) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700757 // get the delegate from the native int.
758 Paint_Delegate delegate = sManager.getDelegate(native_object);
759 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700760 return;
761 }
762
763 delegate.mJoin = join;
764 }
765
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800766 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000767 /*package*/ static boolean native_getFillPath(long native_object, long src, long dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800768 Paint_Delegate paint = sManager.getDelegate(native_object);
769 if (paint == null) {
770 return false;
771 }
772
773 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
774 if (srcPath == null) {
775 return true;
776 }
777
778 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
779 if (dstPath == null) {
780 return true;
781 }
782
783 Stroke stroke = paint.getJavaStroke();
784 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
785
786 dstPath.setJavaShape(strokeShape);
787
788 // FIXME figure out the return value?
789 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700790 }
791
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800792 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000793 /*package*/ static long native_setShader(long native_object, long shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700794 // get the delegate from the native int.
795 Paint_Delegate delegate = sManager.getDelegate(native_object);
796 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700797 return shader;
798 }
799
Xavier Ducrohet91672792011-02-22 11:54:37 -0800800 delegate.mShader = Shader_Delegate.getDelegate(shader);
801
802 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700803 }
804
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800805 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000806 /*package*/ static long native_setColorFilter(long native_object, long filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700807 // get the delegate from the native int.
808 Paint_Delegate delegate = sManager.getDelegate(native_object);
809 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700810 return filter;
811 }
812
Xavier Ducrohet91672792011-02-22 11:54:37 -0800813 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800814
815 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800816 if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800817 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800818 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800819 }
820
821 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700822 }
823
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800824 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000825 /*package*/ static long native_setXfermode(long native_object, long xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700826 // get the delegate from the native int.
827 Paint_Delegate delegate = sManager.getDelegate(native_object);
828 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700829 return xfermode;
830 }
831
Xavier Ducrohet91672792011-02-22 11:54:37 -0800832 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
833
834 return xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700835 }
836
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800837 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000838 /*package*/ static long native_setPathEffect(long native_object, long effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700839 // get the delegate from the native int.
840 Paint_Delegate delegate = sManager.getDelegate(native_object);
841 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700842 return effect;
843 }
844
Xavier Ducrohet91672792011-02-22 11:54:37 -0800845 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
846
847 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700848 }
849
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800850 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000851 /*package*/ static long native_setMaskFilter(long native_object, long maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700852 // get the delegate from the native int.
853 Paint_Delegate delegate = sManager.getDelegate(native_object);
854 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700855 return maskfilter;
856 }
857
Xavier Ducrohet91672792011-02-22 11:54:37 -0800858 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800859
860 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800861 if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800862 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800863 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800864 }
865
866 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700867 }
868
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800869 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000870 /*package*/ static long native_setTypeface(long native_object, long typeface) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700871 // get the delegate from the native int.
872 Paint_Delegate delegate = sManager.getDelegate(native_object);
873 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700874 return 0;
875 }
876
Xavier Ducrohet91672792011-02-22 11:54:37 -0800877 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800878 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800879 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700880 }
881
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800882 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000883 /*package*/ static long native_setRasterizer(long native_object, long rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800884 // get the delegate from the native int.
885 Paint_Delegate delegate = sManager.getDelegate(native_object);
886 if (delegate == null) {
887 return rasterizer;
888 }
889
Xavier Ducrohet91672792011-02-22 11:54:37 -0800890 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800891
892 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800893 if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800894 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800895 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800896 }
897
898 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700899 }
900
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800901 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800902 /*package*/ static int native_getTextAlign(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700903 // get the delegate from the native int.
904 Paint_Delegate delegate = sManager.getDelegate(native_object);
905 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700906 return 0;
907 }
908
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700909 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700910 }
911
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800912 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000913 /*package*/ static void native_setTextAlign(long native_object, int align) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700914 // get the delegate from the native int.
915 Paint_Delegate delegate = sManager.getDelegate(native_object);
916 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700917 return;
918 }
919
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700920 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700921 }
922
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800923 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000924 /*package*/ static void native_setTextLocale(long native_object, String locale) {
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700925 // get the delegate from the native int.
926 Paint_Delegate delegate = sManager.getDelegate(native_object);
927 if (delegate == null) {
928 return;
929 }
930
931 delegate.setTextLocale(locale);
932 }
933
934 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800935 /*package*/ static int native_getTextWidths(long native_object, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700936 int count, int bidiFlags, float[] widths) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800937 // get the delegate from the native int.
938 Paint_Delegate delegate = sManager.getDelegate(native_object);
939 if (delegate == null) {
940 return 0;
941 }
942
943 if (delegate.mFonts.size() > 0) {
944 // FIXME: handle multi-char characters (see measureText)
945 float totalAdvance = 0;
946 for (int i = 0; i < count; i++) {
947 char c = text[i + index];
948 boolean found = false;
949 for (FontInfo info : delegate.mFonts) {
950 if (info.mFont.canDisplay(c)) {
951 float adv = info.mMetrics.charWidth(c);
952 totalAdvance += adv;
953 if (widths != null) {
954 widths[i] = adv;
955 }
956
957 found = true;
958 break;
959 }
960 }
961
962 if (found == false) {
963 // no advance for this char.
964 if (widths != null) {
965 widths[i] = 0.f;
966 }
967 }
968 }
969
970 return (int) totalAdvance;
971 }
972
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -0800973 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700974 }
975
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800976 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800977 /*package*/ static int native_getTextWidths(long native_object, String text, int start,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700978 int end, int bidiFlags, float[] widths) {
979 return native_getTextWidths(native_object, text.toCharArray(), start, end - start,
980 bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700981 }
982
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800983 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800984 /* package */static int native_getTextGlyphs(long native_object, String text, int start,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700985 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
986 // FIXME
987 return 0;
988 }
989
990 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000991 /*package*/ static float native_getTextRunAdvances(long native_object,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700992 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700993 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700994
995 if (advances != null)
996 for (int i = advancesIndex; i< advancesIndex+count; i++)
997 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700998 // get the delegate from the native int.
999 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001000 if (delegate == null || delegate.mFonts == null || delegate.mFonts.size() == 0) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001001 return 0.f;
1002 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001003 boolean isRtl = isRtl(flags);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001004
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001005 int limit = index + count;
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001006 RectF bounds = new BidiRenderer(null, delegate, text).renderText(
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001007 index, limit, isRtl, advances, advancesIndex, false, 0, 0);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001008 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001009 }
1010
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001011 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001012 /*package*/ static float native_getTextRunAdvances(long native_object,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001013 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001014 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001015 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001016 int count = end - start;
1017 char[] buffer = TemporaryBuffer.obtain(count);
1018 TextUtils.getChars(text, start, end, buffer, 0);
1019
1020 return native_getTextRunAdvances(native_object, buffer, 0, count, contextStart,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001021 contextEnd - contextStart, flags, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001022 }
1023
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001024 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001025 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, char[] text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001026 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1027 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001028 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1029 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1030 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001031 }
1032
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001033 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001034 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, String text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001035 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1036 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001037 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1038 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1039 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001040 }
1041
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001042 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001043 /*package*/ static void native_getTextPath(long native_object, int bidiFlags,
1044 char[] text, int index, int count, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001045 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001046 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1047 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001048 }
1049
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001050 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001051 /*package*/ static void native_getTextPath(long native_object, int bidiFlags,
1052 String text, int start, int end, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001053 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001054 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1055 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001056 }
1057
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001058 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001059 /*package*/ static void nativeGetStringBounds(long nativePaint, String text, int start,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001060 int end, int bidiFlags, Rect bounds) {
1061 nativeGetCharArrayBounds(nativePaint, text.toCharArray(), start, end - start, bidiFlags,
1062 bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001063 }
1064
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001065 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001066 /*package*/ static void nativeGetCharArrayBounds(long nativePaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001067 int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001068
1069 // get the delegate from the native int.
1070 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001071 if (delegate == null || delegate.mFonts == null || delegate.mFonts.size() == 0) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001072 return;
1073 }
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001074 delegate.measureText(text, index, count, isRtl(bidiFlags)).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001075 }
1076
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001077 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001078 /*package*/ static void finalizer(long nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001079 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001080 }
1081
1082 // ---- Private delegate/helper methods ----
1083
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001084 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001085 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001086 }
1087
1088 private Paint_Delegate(Paint_Delegate paint) {
1089 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001090 }
1091
1092 private void set(Paint_Delegate paint) {
1093 mFlags = paint.mFlags;
1094 mColor = paint.mColor;
1095 mStyle = paint.mStyle;
1096 mCap = paint.mCap;
1097 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001098 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001099 mTypeface = paint.mTypeface;
1100 mStrokeWidth = paint.mStrokeWidth;
1101 mStrokeMiter = paint.mStrokeMiter;
1102 mTextSize = paint.mTextSize;
1103 mTextScaleX = paint.mTextScaleX;
1104 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001105 mXfermode = paint.mXfermode;
1106 mColorFilter = paint.mColorFilter;
1107 mShader = paint.mShader;
1108 mPathEffect = paint.mPathEffect;
1109 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001110 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001111 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001112 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001113 }
1114
1115 private void reset() {
1116 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001117 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001118 mStyle = Paint.Style.FILL.nativeInt;
1119 mCap = Paint.Cap.BUTT.nativeInt;
1120 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001121 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001122 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001123 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001124 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001125 mTextSize = 20.f;
1126 mTextScaleX = 1.f;
1127 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001128 mXfermode = null;
1129 mColorFilter = null;
1130 mShader = null;
1131 mPathEffect = null;
1132 mMaskFilter = null;
1133 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001134 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001135 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001136 }
1137
1138 /**
1139 * Update the {@link Font} object from the typeface, text size and scaling
1140 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001141 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001142 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001143 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001144 // Get the fonts from the TypeFace object.
Xavier Ducrohet91672792011-02-22 11:54:37 -08001145 List<Font> fonts = mTypeface.getFonts();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001146
1147 // create new font objects as well as FontMetrics, based on the current text size
1148 // and skew info.
1149 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1150 for (Font font : fonts) {
1151 FontInfo info = new FontInfo();
1152 info.mFont = font.deriveFont(mTextSize);
1153 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1154 // TODO: support skew
1155 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001156 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001157 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001158 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001159 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1160
1161 infoList.add(info);
1162 }
1163
1164 mFonts = Collections.unmodifiableList(infoList);
1165 }
1166 }
1167
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001168 /*package*/ RectF measureText(char[] text, int index, int count, boolean isRtl) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001169 return new BidiRenderer(null, this, text).renderText(
1170 index, index + count, isRtl, null, 0, false, 0, 0);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001171 }
1172
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001173 private float getFontMetrics(FontMetrics metrics) {
1174 if (mFonts.size() > 0) {
1175 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1176 if (metrics != null) {
1177 // Android expects negative ascent so we invert the value from Java.
1178 metrics.top = - javaMetrics.getMaxAscent();
1179 metrics.ascent = - javaMetrics.getAscent();
1180 metrics.descent = javaMetrics.getDescent();
1181 metrics.bottom = javaMetrics.getMaxDescent();
1182 metrics.leading = javaMetrics.getLeading();
1183 }
1184
1185 return javaMetrics.getHeight();
1186 }
1187
1188 return 0;
1189 }
1190
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001191 private void setTextLocale(String locale) {
1192 mLocale = new Locale(locale);
1193 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001194
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001195 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1196 // get the delegate from the native int.
1197 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1198 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001199 return;
1200 }
1201
1202 if (flagValue) {
1203 delegate.mFlags |= flagMask;
1204 } else {
1205 delegate.mFlags &= ~flagMask;
1206 }
1207 }
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001208
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001209 private static boolean isRtl(int flag) {
1210 switch(flag) {
1211 case Paint.BIDI_RTL:
1212 case Paint.BIDI_FORCE_RTL:
1213 case Paint.BIDI_DEFAULT_RTL:
1214 return true;
1215 default:
1216 return false;
1217 }
1218 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001219}