blob: d6e97f5fa63cc650e93ae5fc011580950061dd0f [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
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -070024import android.graphics.FontFamily_Delegate.FontVariant;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070025import android.graphics.Paint.FontMetrics;
26import android.graphics.Paint.FontMetricsInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070027import android.text.TextUtils;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070028
Xavier Ducrohet37f21802010-11-01 16:17:18 -070029import java.awt.BasicStroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070030import java.awt.Font;
Xavier Ducrohetb9761242010-12-23 10:22:14 -080031import java.awt.Shape;
32import java.awt.Stroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070033import java.awt.Toolkit;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070034import 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 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -070056 * Class associating a {@link Font} and its {@link java.awt.FontMetrics}.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070057 */
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;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070069
70 // ---- delegate data ----
71 private int mFlags;
72 private int mColor;
73 private int mStyle;
74 private int mCap;
75 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070076 private int mTextAlign;
Xavier Ducrohet91672792011-02-22 11:54:37 -080077 private Typeface_Delegate mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070078 private float mStrokeWidth;
79 private float mStrokeMiter;
80 private float mTextSize;
81 private float mTextScaleX;
82 private float mTextSkewX;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -070083 private int mHintingMode = Paint.HINTING_ON;
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -070084 // Variant of the font.
85 private FontVariant mFontVariant = FontVariant.NONE;
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 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700104 * Returns the list of {@link Font} objects.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700105 */
106 public List<FontInfo> getFonts() {
107 return mFonts;
108 }
109
Xavier Ducroheta313b652010-11-01 18:45:20 -0700110 public boolean isAntiAliased() {
111 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
112 }
113
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700114 public boolean isFilterBitmap() {
115 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
116 }
117
118 public int getStyle() {
119 return mStyle;
120 }
121
122 public int getColor() {
123 return mColor;
124 }
125
Xavier Ducrohet66225222010-12-21 01:33:04 -0800126 public int getAlpha() {
127 return mColor >>> 24;
128 }
129
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800130 public void setAlpha(int alpha) {
131 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
132 }
133
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700134 public int getTextAlign() {
135 return mTextAlign;
136 }
137
138 public float getStrokeWidth() {
139 return mStrokeWidth;
140 }
141
Xavier Ducrohet66225222010-12-21 01:33:04 -0800142 /**
143 * returns the value of stroke miter needed by the java api.
144 */
145 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800146 float miter = mStrokeMiter * mStrokeWidth;
147 if (miter < 1.f) {
148 miter = 1.f;
149 }
150 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700151 }
152
153 public int getJavaCap() {
154 switch (Paint.sCapArray[mCap]) {
155 case BUTT:
156 return BasicStroke.CAP_BUTT;
157 case ROUND:
158 return BasicStroke.CAP_ROUND;
159 default:
160 case SQUARE:
161 return BasicStroke.CAP_SQUARE;
162 }
163 }
164
165 public int getJavaJoin() {
166 switch (Paint.sJoinArray[mJoin]) {
167 default:
168 case MITER:
169 return BasicStroke.JOIN_MITER;
170 case ROUND:
171 return BasicStroke.JOIN_ROUND;
172 case BEVEL:
173 return BasicStroke.JOIN_BEVEL;
174 }
175 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700176
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800177 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800178 if (mPathEffect != null) {
179 if (mPathEffect.isSupported()) {
180 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800181 assert stroke != null;
182 if (stroke != null) {
183 return stroke;
184 }
185 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800186 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800187 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800188 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800189 }
190 }
191
192 // if no custom stroke as been set, set the default one.
193 return new BasicStroke(
194 getStrokeWidth(),
195 getJavaCap(),
196 getJavaJoin(),
197 getJavaStrokeMiter());
198 }
199
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800200 /**
201 * Returns the {@link Xfermode} delegate or null if none have been set
202 *
203 * @return the delegate or null.
204 */
205 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800206 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700207 }
208
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800209 /**
210 * Returns the {@link ColorFilter} delegate or null if none have been set
211 *
212 * @return the delegate or null.
213 */
214 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800215 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700216 }
217
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800218 /**
219 * Returns the {@link Shader} delegate or null if none have been set
220 *
221 * @return the delegate or null.
222 */
223 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800224 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700225 }
226
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800227 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800228 * Returns the {@link MaskFilter} delegate or null if none have been set
229 *
230 * @return the delegate or null.
231 */
232 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800233 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800234 }
235
236 /**
237 * Returns the {@link Rasterizer} delegate or null if none have been set
238 *
239 * @return the delegate or null.
240 */
241 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800242 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700243 }
244
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700245 // ---- native methods ----
246
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800247 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700248 /*package*/ static int getFlags(Paint thisPaint) {
249 // get the delegate from the native int.
250 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
251 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700252 return 0;
253 }
254
255 return delegate.mFlags;
256 }
257
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700258
259
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800260 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700261 /*package*/ static void setFlags(Paint thisPaint, int flags) {
262 // get the delegate from the native int.
263 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
264 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700265 return;
266 }
267
268 delegate.mFlags = flags;
269 }
270
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800271 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700272 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700273 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700274 }
275
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800276 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700277 /*package*/ static int getHinting(Paint thisPaint) {
278 // get the delegate from the native int.
279 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
280 if (delegate == null) {
281 return Paint.HINTING_ON;
282 }
283
284 return delegate.mHintingMode;
285 }
286
287 @LayoutlibDelegate
288 /*package*/ static void setHinting(Paint thisPaint, int mode) {
289 // get the delegate from the native int.
290 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
291 if (delegate == null) {
292 return;
293 }
294
295 delegate.mHintingMode = mode;
296 }
297
298 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700299 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
300 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
301 }
302
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800303 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700304 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
305 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
306 }
307
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800308 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700309 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
310 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
311 }
312
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800313 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700314 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
315 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
316 }
317
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800318 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700319 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
320 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
321 }
322
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800323 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700324 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
325 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
326 }
327
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800328 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700329 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
330 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
331 }
332
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800333 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700334 /*package*/ static int getColor(Paint thisPaint) {
335 // get the delegate from the native int.
336 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
337 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700338 return 0;
339 }
340
341 return delegate.mColor;
342 }
343
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800344 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700345 /*package*/ static void setColor(Paint thisPaint, int color) {
346 // get the delegate from the native int.
347 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
348 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700349 return;
350 }
351
352 delegate.mColor = color;
353 }
354
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800355 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700356 /*package*/ static int getAlpha(Paint thisPaint) {
357 // get the delegate from the native int.
358 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
359 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700360 return 0;
361 }
362
Xavier Ducrohet66225222010-12-21 01:33:04 -0800363 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700364 }
365
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800366 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700367 /*package*/ static void setAlpha(Paint thisPaint, int a) {
368 // get the delegate from the native int.
369 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
370 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700371 return;
372 }
373
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800374 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700375 }
376
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800377 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700378 /*package*/ static float getStrokeWidth(Paint thisPaint) {
379 // get the delegate from the native int.
380 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
381 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700382 return 1.f;
383 }
384
385 return delegate.mStrokeWidth;
386 }
387
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800388 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700389 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
390 // get the delegate from the native int.
391 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
392 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700393 return;
394 }
395
396 delegate.mStrokeWidth = width;
397 }
398
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800399 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700400 /*package*/ static float getStrokeMiter(Paint thisPaint) {
401 // get the delegate from the native int.
402 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
403 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700404 return 1.f;
405 }
406
407 return delegate.mStrokeMiter;
408 }
409
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800410 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700411 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
412 // get the delegate from the native int.
413 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
414 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700415 return;
416 }
417
418 delegate.mStrokeMiter = miter;
419 }
420
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800421 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700422 /*package*/ static void native_setShadowLayer(long paint, float radius, float dx, float dy,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700423 int color) {
424 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800425 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800426 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700427 }
428
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800429 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700430 /*package*/ static boolean native_hasShadowLayer(long paint) {
431 // FIXME
432 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
433 "Paint.hasShadowLayer is not supported.", null, null /*data*/);
434 return false;
435 }
436
437 @LayoutlibDelegate
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700438 /*package*/ static boolean isElegantTextHeight(Paint thisPaint) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700439 // get the delegate from the native int.
440 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700441 return delegate != null && delegate.mFontVariant == FontVariant.ELEGANT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700442 }
443
444 @LayoutlibDelegate
445 /*package*/ static void setElegantTextHeight(Paint thisPaint, boolean elegant) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700446 // get the delegate from the native int.
447 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
448 if (delegate == null) {
449 return;
450 }
451
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700452 delegate.mFontVariant = elegant ? FontVariant.ELEGANT : FontVariant.COMPACT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700453 }
454
455 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700456 /*package*/ static float getTextSize(Paint thisPaint) {
457 // get the delegate from the native int.
458 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
459 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700460 return 1.f;
461 }
462
463 return delegate.mTextSize;
464 }
465
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800466 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700467 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
468 // get the delegate from the native int.
469 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
470 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700471 return;
472 }
473
474 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800475 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700476 }
477
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800478 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700479 /*package*/ static float getTextScaleX(Paint thisPaint) {
480 // get the delegate from the native int.
481 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
482 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700483 return 1.f;
484 }
485
486 return delegate.mTextScaleX;
487 }
488
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800489 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700490 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
491 // get the delegate from the native int.
492 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
493 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700494 return;
495 }
496
497 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800498 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700499 }
500
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800501 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700502 /*package*/ static float getTextSkewX(Paint thisPaint) {
503 // get the delegate from the native int.
504 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
505 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700506 return 1.f;
507 }
508
509 return delegate.mTextSkewX;
510 }
511
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800512 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700513 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
514 // get the delegate from the native int.
515 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
516 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700517 return;
518 }
519
520 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800521 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700522 }
523
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800524 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700525 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800526 // get the delegate
527 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
528 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800529 return 0;
530 }
531
532 if (delegate.mFonts.size() > 0) {
533 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
534 // Android expects negative ascent so we invert the value from Java.
535 return - javaMetrics.getAscent();
536 }
537
538 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700539 }
540
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800541 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700542 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800543 // get the delegate
544 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
545 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800546 return 0;
547 }
548
549 if (delegate.mFonts.size() > 0) {
550 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
551 return javaMetrics.getDescent();
552 }
553
554 return 0;
555
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700556 }
557
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800558 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700559 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700560 // get the delegate
561 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
562 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700563 return 0;
564 }
565
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800566 return delegate.getFontMetrics(metrics);
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 int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700571 // get the delegate
572 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
573 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700574 return 0;
575 }
576
577 if (delegate.mFonts.size() > 0) {
578 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
579 if (fmi != null) {
580 // Android expects negative ascent so we invert the value from Java.
581 fmi.top = - javaMetrics.getMaxAscent();
582 fmi.ascent = - javaMetrics.getAscent();
583 fmi.descent = javaMetrics.getDescent();
584 fmi.bottom = javaMetrics.getMaxDescent();
585 fmi.leading = javaMetrics.getLeading();
586 }
587
588 return javaMetrics.getHeight();
589 }
590
591 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700592 }
593
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800594 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700595 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700596 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700597 // get the delegate
598 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
599 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700600 return 0;
601 }
602
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800603 RectF bounds = delegate.measureText(text, index, count, isRtl(bidiFlags));
604 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700605 }
606
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800607 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700608 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
609 int bidiFlags) {
610 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700611 }
612
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800613 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700614 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
615 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700616 }
617
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800618 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700619 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700620 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800621
622 // get the delegate
623 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
624 if (delegate == null) {
625 return 0;
626 }
627
628 int inc = count > 0 ? 1 : -1;
629
630 int measureIndex = 0;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800631 for (int i = index; i != index + count; i += inc, measureIndex++) {
632 int start, end;
633 if (i < index) {
634 start = i;
635 end = index;
636 } else {
637 start = index;
638 end = i;
639 }
640
641 // measure from start to end
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800642 RectF bounds = delegate.measureText(text, start, end - start + 1, isRtl(bidiFlags));
643 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800644
645 if (measuredWidth != null) {
646 measuredWidth[measureIndex] = res;
647 }
648
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800649 if (res > maxWidth) {
650 // we should not return this char index, but since it's 0-based
651 // and we need to return a count, we simply return measureIndex;
652 return measureIndex;
653 }
654
655 }
656
657 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700658 }
659
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800660 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700661 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700662 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800663 return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700664 bidiFlags, measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700665 }
666
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800667 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000668 /*package*/ static long native_init() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700669 Paint_Delegate newDelegate = new Paint_Delegate();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800670 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700671 }
672
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800673 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000674 /*package*/ static long native_initWithPaint(long paint) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700675 // get the delegate from the native int.
676 Paint_Delegate delegate = sManager.getDelegate(paint);
677 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700678 return 0;
679 }
680
681 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800682 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700683 }
684
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800685 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000686 /*package*/ static void native_reset(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700687 // get the delegate from the native int.
688 Paint_Delegate delegate = sManager.getDelegate(native_object);
689 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700690 return;
691 }
692
693 delegate.reset();
694 }
695
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800696 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000697 /*package*/ static void native_set(long native_dst, long native_src) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700698 // get the delegate from the native int.
699 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
700 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700701 return;
702 }
703
704 // get the delegate from the native int.
705 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
706 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700707 return;
708 }
709
710 delegate_dst.set(delegate_src);
711 }
712
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800713 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800714 /*package*/ static int native_getStyle(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700715 // get the delegate from the native int.
716 Paint_Delegate delegate = sManager.getDelegate(native_object);
717 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700718 return 0;
719 }
720
721 return delegate.mStyle;
722 }
723
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800724 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000725 /*package*/ static void native_setStyle(long native_object, int style) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700726 // get the delegate from the native int.
727 Paint_Delegate delegate = sManager.getDelegate(native_object);
728 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700729 return;
730 }
731
732 delegate.mStyle = style;
733 }
734
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800735 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800736 /*package*/ static int native_getStrokeCap(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700737 // get the delegate from the native int.
738 Paint_Delegate delegate = sManager.getDelegate(native_object);
739 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700740 return 0;
741 }
742
743 return delegate.mCap;
744 }
745
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800746 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000747 /*package*/ static void native_setStrokeCap(long native_object, int cap) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700748 // get the delegate from the native int.
749 Paint_Delegate delegate = sManager.getDelegate(native_object);
750 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700751 return;
752 }
753
754 delegate.mCap = cap;
755 }
756
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800757 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800758 /*package*/ static int native_getStrokeJoin(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700759 // get the delegate from the native int.
760 Paint_Delegate delegate = sManager.getDelegate(native_object);
761 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700762 return 0;
763 }
764
765 return delegate.mJoin;
766 }
767
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800768 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000769 /*package*/ static void native_setStrokeJoin(long native_object, int join) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700770 // get the delegate from the native int.
771 Paint_Delegate delegate = sManager.getDelegate(native_object);
772 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700773 return;
774 }
775
776 delegate.mJoin = join;
777 }
778
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800779 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000780 /*package*/ static boolean native_getFillPath(long native_object, long src, long dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800781 Paint_Delegate paint = sManager.getDelegate(native_object);
782 if (paint == null) {
783 return false;
784 }
785
786 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
787 if (srcPath == null) {
788 return true;
789 }
790
791 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
792 if (dstPath == null) {
793 return true;
794 }
795
796 Stroke stroke = paint.getJavaStroke();
797 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
798
799 dstPath.setJavaShape(strokeShape);
800
801 // FIXME figure out the return value?
802 return true;
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_setShader(long native_object, long shader) {
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 shader;
811 }
812
Xavier Ducrohet91672792011-02-22 11:54:37 -0800813 delegate.mShader = Shader_Delegate.getDelegate(shader);
814
815 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700816 }
817
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800818 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000819 /*package*/ static long native_setColorFilter(long native_object, long filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700820 // get the delegate from the native int.
821 Paint_Delegate delegate = sManager.getDelegate(native_object);
822 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700823 return filter;
824 }
825
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700826 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800827
828 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800829 if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800830 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800831 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800832 }
833
834 return filter;
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_setXfermode(long native_object, long xfermode) {
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 xfermode;
843 }
844
Xavier Ducrohet91672792011-02-22 11:54:37 -0800845 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
846
847 return xfermode;
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_setPathEffect(long native_object, long effect) {
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 effect;
856 }
857
Xavier Ducrohet91672792011-02-22 11:54:37 -0800858 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
859
860 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700861 }
862
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800863 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000864 /*package*/ static long native_setMaskFilter(long native_object, long maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700865 // get the delegate from the native int.
866 Paint_Delegate delegate = sManager.getDelegate(native_object);
867 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700868 return maskfilter;
869 }
870
Xavier Ducrohet91672792011-02-22 11:54:37 -0800871 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800872
873 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800874 if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800875 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800876 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800877 }
878
879 return maskfilter;
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_setTypeface(long native_object, long typeface) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700884 // get the delegate from the native int.
885 Paint_Delegate delegate = sManager.getDelegate(native_object);
886 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700887 return 0;
888 }
889
Xavier Ducrohet91672792011-02-22 11:54:37 -0800890 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800891 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800892 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700893 }
894
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800895 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000896 /*package*/ static long native_setRasterizer(long native_object, long rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800897 // get the delegate from the native int.
898 Paint_Delegate delegate = sManager.getDelegate(native_object);
899 if (delegate == null) {
900 return rasterizer;
901 }
902
Xavier Ducrohet91672792011-02-22 11:54:37 -0800903 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800904
905 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800906 if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800907 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800908 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800909 }
910
911 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700912 }
913
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800914 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800915 /*package*/ static int native_getTextAlign(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700916 // get the delegate from the native int.
917 Paint_Delegate delegate = sManager.getDelegate(native_object);
918 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700919 return 0;
920 }
921
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700922 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700923 }
924
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800925 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000926 /*package*/ static void native_setTextAlign(long native_object, int align) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700927 // get the delegate from the native int.
928 Paint_Delegate delegate = sManager.getDelegate(native_object);
929 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700930 return;
931 }
932
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700933 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700934 }
935
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800936 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000937 /*package*/ static void native_setTextLocale(long native_object, String locale) {
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700938 // get the delegate from the native int.
939 Paint_Delegate delegate = sManager.getDelegate(native_object);
940 if (delegate == null) {
941 return;
942 }
943
944 delegate.setTextLocale(locale);
945 }
946
947 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700948 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
949 char[] text, int index, int count, int bidiFlags, float[] widths) {
950 return (int) native_getTextRunAdvances(native_object, native_typeface, text, index, count,
951 index, count, bidiFlags, widths, 0);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700952 }
953
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800954 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700955 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
956 String text, int start, int end, int bidiFlags, float[] widths) {
957 return native_getTextWidths(native_object, native_typeface, text.toCharArray(), start,
958 end - start, bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700959 }
960
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800961 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800962 /* package */static int native_getTextGlyphs(long native_object, String text, int start,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700963 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
964 // FIXME
965 return 0;
966 }
967
968 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000969 /*package*/ static float native_getTextRunAdvances(long native_object,
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700970 long native_typeface /*ignored*/,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700971 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700972 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700973
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700974 // native_typeface is passed here since Framework's old implementation did not have the
975 // typeface object associated with the Paint. Since, we follow the new framework way,
976 // we store the typeface with the paint and use it directly.
977
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700978 if (advances != null)
979 for (int i = advancesIndex; i< advancesIndex+count; i++)
980 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700981 // get the delegate from the native int.
982 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700983 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700984 return 0.f;
985 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700986 boolean isRtl = isRtl(flags);
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700987
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700988 int limit = index + count;
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800989 RectF bounds = new BidiRenderer(null, delegate, text).renderText(
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700990 index, limit, isRtl, advances, advancesIndex, false, 0, 0);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800991 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700992 }
993
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800994 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700995 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700996 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700997 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700998 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700999 int count = end - start;
1000 char[] buffer = TemporaryBuffer.obtain(count);
1001 TextUtils.getChars(text, start, end, buffer, 0);
1002
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001003 return native_getTextRunAdvances(native_object, native_typeface, buffer, 0, count,
1004 contextStart, contextEnd - contextStart, flags, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001005 }
1006
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001007 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001008 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, char[] text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001009 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1010 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001011 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1012 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1013 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001014 }
1015
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001016 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001017 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, String text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001018 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1019 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001020 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1021 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1022 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001023 }
1024
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001025 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001026 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1027 int bidiFlags, char[] text, int index, int count, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001028 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001029 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1030 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001031 }
1032
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001033 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001034 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1035 int bidiFlags, String text, int start, int end, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001036 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001037 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1038 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001039 }
1040
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001041 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001042 /*package*/ static void nativeGetStringBounds(long nativePaint, long native_typeface,
1043 String text, int start, int end, int bidiFlags, Rect bounds) {
1044 nativeGetCharArrayBounds(nativePaint, native_typeface, text.toCharArray(), start,
1045 end - start, bidiFlags, bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001046 }
1047
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001048 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001049 /*package*/ static void nativeGetCharArrayBounds(long nativePaint, long native_typeface,
1050 char[] text, int index, int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001051
1052 // get the delegate from the native int.
1053 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001054 if (delegate == null) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001055 return;
1056 }
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001057 delegate.measureText(text, index, count, isRtl(bidiFlags)).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001058 }
1059
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001060 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001061 /*package*/ static void finalizer(long nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001062 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001063 }
1064
1065 // ---- Private delegate/helper methods ----
1066
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001067 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001068 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001069 }
1070
1071 private Paint_Delegate(Paint_Delegate paint) {
1072 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001073 }
1074
1075 private void set(Paint_Delegate paint) {
1076 mFlags = paint.mFlags;
1077 mColor = paint.mColor;
1078 mStyle = paint.mStyle;
1079 mCap = paint.mCap;
1080 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001081 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001082 mTypeface = paint.mTypeface;
1083 mStrokeWidth = paint.mStrokeWidth;
1084 mStrokeMiter = paint.mStrokeMiter;
1085 mTextSize = paint.mTextSize;
1086 mTextScaleX = paint.mTextScaleX;
1087 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001088 mXfermode = paint.mXfermode;
1089 mColorFilter = paint.mColorFilter;
1090 mShader = paint.mShader;
1091 mPathEffect = paint.mPathEffect;
1092 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001093 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001094 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001095 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001096 }
1097
1098 private void reset() {
1099 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001100 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001101 mStyle = Paint.Style.FILL.nativeInt;
1102 mCap = Paint.Cap.BUTT.nativeInt;
1103 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001104 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001105 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001106 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001107 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001108 mTextSize = 20.f;
1109 mTextScaleX = 1.f;
1110 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001111 mXfermode = null;
1112 mColorFilter = null;
1113 mShader = null;
1114 mPathEffect = null;
1115 mMaskFilter = null;
1116 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001117 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001118 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001119 }
1120
1121 /**
1122 * Update the {@link Font} object from the typeface, text size and scaling
1123 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001124 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001125 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001126 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001127 // Get the fonts from the TypeFace object.
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001128 List<Font> fonts = mTypeface.getFonts(mFontVariant);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001129
1130 // create new font objects as well as FontMetrics, based on the current text size
1131 // and skew info.
1132 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1133 for (Font font : fonts) {
1134 FontInfo info = new FontInfo();
1135 info.mFont = font.deriveFont(mTextSize);
1136 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1137 // TODO: support skew
1138 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001139 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001140 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001141 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001142 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1143
1144 infoList.add(info);
1145 }
1146
1147 mFonts = Collections.unmodifiableList(infoList);
1148 }
1149 }
1150
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001151 /*package*/ RectF measureText(char[] text, int index, int count, boolean isRtl) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001152 return new BidiRenderer(null, this, text).renderText(
1153 index, index + count, isRtl, null, 0, false, 0, 0);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001154 }
1155
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001156 private float getFontMetrics(FontMetrics metrics) {
1157 if (mFonts.size() > 0) {
1158 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1159 if (metrics != null) {
1160 // Android expects negative ascent so we invert the value from Java.
1161 metrics.top = - javaMetrics.getMaxAscent();
1162 metrics.ascent = - javaMetrics.getAscent();
1163 metrics.descent = javaMetrics.getDescent();
1164 metrics.bottom = javaMetrics.getMaxDescent();
1165 metrics.leading = javaMetrics.getLeading();
1166 }
1167
1168 return javaMetrics.getHeight();
1169 }
1170
1171 return 0;
1172 }
1173
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001174 private void setTextLocale(String locale) {
1175 mLocale = new Locale(locale);
1176 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001177
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001178 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1179 // get the delegate from the native int.
1180 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1181 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001182 return;
1183 }
1184
1185 if (flagValue) {
1186 delegate.mFlags |= flagMask;
1187 } else {
1188 delegate.mFlags &= ~flagMask;
1189 }
1190 }
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001191
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001192 private static boolean isRtl(int flag) {
1193 switch(flag) {
1194 case Paint.BIDI_RTL:
1195 case Paint.BIDI_FORCE_RTL:
1196 case Paint.BIDI_DEFAULT_RTL:
1197 return true;
1198 default:
1199 return false;
1200 }
1201 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001202}