blob: 911f4e7af94a04ff451ee1ff35d091dcec3bfa3a [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 /**
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 Gupta017c0622014-05-19 16:14:23 -070084 private boolean mIsCompact = true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070085
Xavier Ducrohet91672792011-02-22 11:54:37 -080086 private Xfermode_Delegate mXfermode;
87 private ColorFilter_Delegate mColorFilter;
88 private Shader_Delegate mShader;
89 private PathEffect_Delegate mPathEffect;
90 private MaskFilter_Delegate mMaskFilter;
91 private Rasterizer_Delegate mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070092
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070093 private Locale mLocale = Locale.getDefault();
94
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070095
96 // ---- Public Helper methods ----
97
Narayan Kamath633d6882014-01-27 14:24:16 +000098 public static Paint_Delegate getDelegate(long native_paint) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -070099 return sManager.getDelegate(native_paint);
100 }
101
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700102 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700103 * Returns the list of {@link Font} objects.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700104 */
105 public List<FontInfo> getFonts() {
106 return mFonts;
107 }
108
Xavier Ducroheta313b652010-11-01 18:45:20 -0700109 public boolean isAntiAliased() {
110 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
111 }
112
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700113 public boolean isFilterBitmap() {
114 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
115 }
116
117 public int getStyle() {
118 return mStyle;
119 }
120
121 public int getColor() {
122 return mColor;
123 }
124
Xavier Ducrohet66225222010-12-21 01:33:04 -0800125 public int getAlpha() {
126 return mColor >>> 24;
127 }
128
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800129 public void setAlpha(int alpha) {
130 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
131 }
132
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700133 public int getTextAlign() {
134 return mTextAlign;
135 }
136
137 public float getStrokeWidth() {
138 return mStrokeWidth;
139 }
140
Xavier Ducrohet66225222010-12-21 01:33:04 -0800141 /**
142 * returns the value of stroke miter needed by the java api.
143 */
144 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800145 float miter = mStrokeMiter * mStrokeWidth;
146 if (miter < 1.f) {
147 miter = 1.f;
148 }
149 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700150 }
151
152 public int getJavaCap() {
153 switch (Paint.sCapArray[mCap]) {
154 case BUTT:
155 return BasicStroke.CAP_BUTT;
156 case ROUND:
157 return BasicStroke.CAP_ROUND;
158 default:
159 case SQUARE:
160 return BasicStroke.CAP_SQUARE;
161 }
162 }
163
164 public int getJavaJoin() {
165 switch (Paint.sJoinArray[mJoin]) {
166 default:
167 case MITER:
168 return BasicStroke.JOIN_MITER;
169 case ROUND:
170 return BasicStroke.JOIN_ROUND;
171 case BEVEL:
172 return BasicStroke.JOIN_BEVEL;
173 }
174 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700175
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800176 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800177 if (mPathEffect != null) {
178 if (mPathEffect.isSupported()) {
179 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800180 assert stroke != null;
181 if (stroke != null) {
182 return stroke;
183 }
184 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800185 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800186 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800187 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800188 }
189 }
190
191 // if no custom stroke as been set, set the default one.
192 return new BasicStroke(
193 getStrokeWidth(),
194 getJavaCap(),
195 getJavaJoin(),
196 getJavaStrokeMiter());
197 }
198
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800199 /**
200 * Returns the {@link Xfermode} delegate or null if none have been set
201 *
202 * @return the delegate or null.
203 */
204 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800205 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700206 }
207
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800208 /**
209 * Returns the {@link ColorFilter} delegate or null if none have been set
210 *
211 * @return the delegate or null.
212 */
213 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800214 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700215 }
216
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800217 /**
218 * Returns the {@link Shader} delegate or null if none have been set
219 *
220 * @return the delegate or null.
221 */
222 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800223 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700224 }
225
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800226 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800227 * Returns the {@link MaskFilter} delegate or null if none have been set
228 *
229 * @return the delegate or null.
230 */
231 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800232 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800233 }
234
235 /**
236 * Returns the {@link Rasterizer} delegate or null if none have been set
237 *
238 * @return the delegate or null.
239 */
240 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800241 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700242 }
243
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700244 // ---- native methods ----
245
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800246 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700247 /*package*/ static int getFlags(Paint thisPaint) {
248 // get the delegate from the native int.
249 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
250 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700251 return 0;
252 }
253
254 return delegate.mFlags;
255 }
256
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700257
258
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800259 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700260 /*package*/ static void setFlags(Paint thisPaint, int flags) {
261 // get the delegate from the native int.
262 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
263 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700264 return;
265 }
266
267 delegate.mFlags = flags;
268 }
269
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800270 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700271 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700272 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700273 }
274
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800275 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700276 /*package*/ static int getHinting(Paint thisPaint) {
277 // get the delegate from the native int.
278 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
279 if (delegate == null) {
280 return Paint.HINTING_ON;
281 }
282
283 return delegate.mHintingMode;
284 }
285
286 @LayoutlibDelegate
287 /*package*/ static void setHinting(Paint thisPaint, int mode) {
288 // get the delegate from the native int.
289 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
290 if (delegate == null) {
291 return;
292 }
293
294 delegate.mHintingMode = mode;
295 }
296
297 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700298 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
299 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
300 }
301
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800302 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700303 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
304 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
305 }
306
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800307 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700308 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
309 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
310 }
311
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800312 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700313 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
314 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
315 }
316
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800317 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700318 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
319 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
320 }
321
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800322 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700323 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
324 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
325 }
326
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800327 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700328 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
329 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
330 }
331
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800332 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700333 /*package*/ static int getColor(Paint thisPaint) {
334 // get the delegate from the native int.
335 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
336 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700337 return 0;
338 }
339
340 return delegate.mColor;
341 }
342
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800343 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700344 /*package*/ static void setColor(Paint thisPaint, int color) {
345 // get the delegate from the native int.
346 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
347 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700348 return;
349 }
350
351 delegate.mColor = color;
352 }
353
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800354 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700355 /*package*/ static int getAlpha(Paint thisPaint) {
356 // get the delegate from the native int.
357 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
358 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700359 return 0;
360 }
361
Xavier Ducrohet66225222010-12-21 01:33:04 -0800362 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700363 }
364
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800365 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700366 /*package*/ static void setAlpha(Paint thisPaint, int a) {
367 // get the delegate from the native int.
368 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
369 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700370 return;
371 }
372
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800373 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700374 }
375
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800376 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700377 /*package*/ static float getStrokeWidth(Paint thisPaint) {
378 // get the delegate from the native int.
379 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
380 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700381 return 1.f;
382 }
383
384 return delegate.mStrokeWidth;
385 }
386
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800387 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700388 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
389 // get the delegate from the native int.
390 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
391 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700392 return;
393 }
394
395 delegate.mStrokeWidth = width;
396 }
397
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800398 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700399 /*package*/ static float getStrokeMiter(Paint thisPaint) {
400 // get the delegate from the native int.
401 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
402 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700403 return 1.f;
404 }
405
406 return delegate.mStrokeMiter;
407 }
408
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800409 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700410 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
411 // get the delegate from the native int.
412 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
413 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700414 return;
415 }
416
417 delegate.mStrokeMiter = miter;
418 }
419
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800420 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700421 /*package*/ static void native_setShadowLayer(long paint, float radius, float dx, float dy,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700422 int color) {
423 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800424 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800425 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700426 }
427
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800428 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700429 /*package*/ static boolean native_hasShadowLayer(long paint) {
430 // FIXME
431 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
432 "Paint.hasShadowLayer is not supported.", null, null /*data*/);
433 return false;
434 }
435
436 @LayoutlibDelegate
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700437 /*package*/ static boolean isElegantTextHeight(Paint thisPaint) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700438 // get the delegate from the native int.
439 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
440 return delegate != null && !delegate.mIsCompact;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700441 }
442
443 @LayoutlibDelegate
444 /*package*/ static void setElegantTextHeight(Paint thisPaint, boolean elegant) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700445 // get the delegate from the native int.
446 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
447 if (delegate == null) {
448 return;
449 }
450
451 delegate.mIsCompact = !elegant;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700452 }
453
454 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700455 /*package*/ static float getTextSize(Paint thisPaint) {
456 // get the delegate from the native int.
457 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
458 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700459 return 1.f;
460 }
461
462 return delegate.mTextSize;
463 }
464
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800465 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700466 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
467 // get the delegate from the native int.
468 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
469 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700470 return;
471 }
472
473 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800474 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700475 }
476
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800477 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700478 /*package*/ static float getTextScaleX(Paint thisPaint) {
479 // get the delegate from the native int.
480 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
481 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700482 return 1.f;
483 }
484
485 return delegate.mTextScaleX;
486 }
487
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800488 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700489 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
490 // get the delegate from the native int.
491 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
492 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700493 return;
494 }
495
496 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800497 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700498 }
499
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800500 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700501 /*package*/ static float getTextSkewX(Paint thisPaint) {
502 // get the delegate from the native int.
503 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
504 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700505 return 1.f;
506 }
507
508 return delegate.mTextSkewX;
509 }
510
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800511 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700512 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
513 // get the delegate from the native int.
514 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
515 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700516 return;
517 }
518
519 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800520 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700521 }
522
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800523 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700524 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800525 // get the delegate
526 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
527 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800528 return 0;
529 }
530
531 if (delegate.mFonts.size() > 0) {
532 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
533 // Android expects negative ascent so we invert the value from Java.
534 return - javaMetrics.getAscent();
535 }
536
537 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700538 }
539
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800540 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700541 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800542 // get the delegate
543 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
544 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800545 return 0;
546 }
547
548 if (delegate.mFonts.size() > 0) {
549 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
550 return javaMetrics.getDescent();
551 }
552
553 return 0;
554
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700555 }
556
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800557 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700558 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700559 // get the delegate
560 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
561 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700562 return 0;
563 }
564
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800565 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700566 }
567
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800568 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700569 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700570 // get the delegate
571 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
572 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700573 return 0;
574 }
575
576 if (delegate.mFonts.size() > 0) {
577 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
578 if (fmi != null) {
579 // Android expects negative ascent so we invert the value from Java.
580 fmi.top = - javaMetrics.getMaxAscent();
581 fmi.ascent = - javaMetrics.getAscent();
582 fmi.descent = javaMetrics.getDescent();
583 fmi.bottom = javaMetrics.getMaxDescent();
584 fmi.leading = javaMetrics.getLeading();
585 }
586
587 return javaMetrics.getHeight();
588 }
589
590 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700591 }
592
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800593 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700594 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700595 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700596 // get the delegate
597 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
598 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700599 return 0;
600 }
601
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800602 RectF bounds = delegate.measureText(text, index, count, isRtl(bidiFlags));
603 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700604 }
605
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800606 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700607 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
608 int bidiFlags) {
609 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700610 }
611
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800612 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700613 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
614 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700615 }
616
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800617 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700618 /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700619 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800620
621 // get the delegate
622 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
623 if (delegate == null) {
624 return 0;
625 }
626
627 int inc = count > 0 ? 1 : -1;
628
629 int measureIndex = 0;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800630 for (int i = index; i != index + count; i += inc, measureIndex++) {
631 int start, end;
632 if (i < index) {
633 start = i;
634 end = index;
635 } else {
636 start = index;
637 end = i;
638 }
639
640 // measure from start to end
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800641 RectF bounds = delegate.measureText(text, start, end - start + 1, isRtl(bidiFlags));
642 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800643
644 if (measuredWidth != null) {
645 measuredWidth[measureIndex] = res;
646 }
647
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800648 if (res > maxWidth) {
649 // we should not return this char index, but since it's 0-based
650 // and we need to return a count, we simply return measureIndex;
651 return measureIndex;
652 }
653
654 }
655
656 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700657 }
658
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800659 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700660 /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700661 float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800662 return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700663 bidiFlags, measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700664 }
665
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800666 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000667 /*package*/ static long native_init() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700668 Paint_Delegate newDelegate = new Paint_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 long native_initWithPaint(long paint) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700674 // get the delegate from the native int.
675 Paint_Delegate delegate = sManager.getDelegate(paint);
676 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700677 return 0;
678 }
679
680 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800681 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700682 }
683
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800684 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000685 /*package*/ static void native_reset(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700686 // get the delegate from the native int.
687 Paint_Delegate delegate = sManager.getDelegate(native_object);
688 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700689 return;
690 }
691
692 delegate.reset();
693 }
694
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800695 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000696 /*package*/ static void native_set(long native_dst, long native_src) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700697 // get the delegate from the native int.
698 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
699 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700700 return;
701 }
702
703 // get the delegate from the native int.
704 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
705 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700706 return;
707 }
708
709 delegate_dst.set(delegate_src);
710 }
711
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800712 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800713 /*package*/ static int native_getStyle(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700714 // get the delegate from the native int.
715 Paint_Delegate delegate = sManager.getDelegate(native_object);
716 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700717 return 0;
718 }
719
720 return delegate.mStyle;
721 }
722
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800723 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000724 /*package*/ static void native_setStyle(long native_object, int style) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700725 // get the delegate from the native int.
726 Paint_Delegate delegate = sManager.getDelegate(native_object);
727 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700728 return;
729 }
730
731 delegate.mStyle = style;
732 }
733
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800734 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800735 /*package*/ static int native_getStrokeCap(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700736 // get the delegate from the native int.
737 Paint_Delegate delegate = sManager.getDelegate(native_object);
738 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700739 return 0;
740 }
741
742 return delegate.mCap;
743 }
744
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800745 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000746 /*package*/ static void native_setStrokeCap(long native_object, int cap) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700747 // get the delegate from the native int.
748 Paint_Delegate delegate = sManager.getDelegate(native_object);
749 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700750 return;
751 }
752
753 delegate.mCap = cap;
754 }
755
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800756 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800757 /*package*/ static int native_getStrokeJoin(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700758 // get the delegate from the native int.
759 Paint_Delegate delegate = sManager.getDelegate(native_object);
760 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700761 return 0;
762 }
763
764 return delegate.mJoin;
765 }
766
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800767 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000768 /*package*/ static void native_setStrokeJoin(long native_object, int join) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700769 // get the delegate from the native int.
770 Paint_Delegate delegate = sManager.getDelegate(native_object);
771 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700772 return;
773 }
774
775 delegate.mJoin = join;
776 }
777
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800778 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000779 /*package*/ static boolean native_getFillPath(long native_object, long src, long dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800780 Paint_Delegate paint = sManager.getDelegate(native_object);
781 if (paint == null) {
782 return false;
783 }
784
785 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
786 if (srcPath == null) {
787 return true;
788 }
789
790 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
791 if (dstPath == null) {
792 return true;
793 }
794
795 Stroke stroke = paint.getJavaStroke();
796 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
797
798 dstPath.setJavaShape(strokeShape);
799
800 // FIXME figure out the return value?
801 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700802 }
803
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800804 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000805 /*package*/ static long native_setShader(long native_object, long shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700806 // get the delegate from the native int.
807 Paint_Delegate delegate = sManager.getDelegate(native_object);
808 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700809 return shader;
810 }
811
Xavier Ducrohet91672792011-02-22 11:54:37 -0800812 delegate.mShader = Shader_Delegate.getDelegate(shader);
813
814 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700815 }
816
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800817 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000818 /*package*/ static long native_setColorFilter(long native_object, long filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700819 // get the delegate from the native int.
820 Paint_Delegate delegate = sManager.getDelegate(native_object);
821 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700822 return filter;
823 }
824
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700825 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800826
827 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800828 if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800829 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800830 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800831 }
832
833 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700834 }
835
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800836 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000837 /*package*/ static long native_setXfermode(long native_object, long xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700838 // get the delegate from the native int.
839 Paint_Delegate delegate = sManager.getDelegate(native_object);
840 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700841 return xfermode;
842 }
843
Xavier Ducrohet91672792011-02-22 11:54:37 -0800844 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
845
846 return xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700847 }
848
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800849 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000850 /*package*/ static long native_setPathEffect(long native_object, long effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700851 // get the delegate from the native int.
852 Paint_Delegate delegate = sManager.getDelegate(native_object);
853 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700854 return effect;
855 }
856
Xavier Ducrohet91672792011-02-22 11:54:37 -0800857 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
858
859 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700860 }
861
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800862 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000863 /*package*/ static long native_setMaskFilter(long native_object, long maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700864 // get the delegate from the native int.
865 Paint_Delegate delegate = sManager.getDelegate(native_object);
866 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700867 return maskfilter;
868 }
869
Xavier Ducrohet91672792011-02-22 11:54:37 -0800870 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800871
872 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800873 if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800874 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800875 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800876 }
877
878 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700879 }
880
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800881 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000882 /*package*/ static long native_setTypeface(long native_object, long typeface) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700883 // get the delegate from the native int.
884 Paint_Delegate delegate = sManager.getDelegate(native_object);
885 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700886 return 0;
887 }
888
Xavier Ducrohet91672792011-02-22 11:54:37 -0800889 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800890 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800891 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700892 }
893
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800894 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000895 /*package*/ static long native_setRasterizer(long native_object, long rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800896 // get the delegate from the native int.
897 Paint_Delegate delegate = sManager.getDelegate(native_object);
898 if (delegate == null) {
899 return rasterizer;
900 }
901
Xavier Ducrohet91672792011-02-22 11:54:37 -0800902 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800903
904 // since none of those are supported, display a fidelity warning right away
Xavier Ducrohet91672792011-02-22 11:54:37 -0800905 if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800906 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800907 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800908 }
909
910 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700911 }
912
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800913 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800914 /*package*/ static int native_getTextAlign(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700915 // get the delegate from the native int.
916 Paint_Delegate delegate = sManager.getDelegate(native_object);
917 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700918 return 0;
919 }
920
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700921 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700922 }
923
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800924 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000925 /*package*/ static void native_setTextAlign(long native_object, int align) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700926 // get the delegate from the native int.
927 Paint_Delegate delegate = sManager.getDelegate(native_object);
928 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700929 return;
930 }
931
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700932 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700933 }
934
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800935 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000936 /*package*/ static void native_setTextLocale(long native_object, String locale) {
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700937 // get the delegate from the native int.
938 Paint_Delegate delegate = sManager.getDelegate(native_object);
939 if (delegate == null) {
940 return;
941 }
942
943 delegate.setTextLocale(locale);
944 }
945
946 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700947 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
948 char[] text, int index, int count, int bidiFlags, float[] widths) {
949 return (int) native_getTextRunAdvances(native_object, native_typeface, text, index, count,
950 index, count, bidiFlags, widths, 0);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700951 }
952
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800953 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700954 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
955 String text, int start, int end, int bidiFlags, float[] widths) {
956 return native_getTextWidths(native_object, native_typeface, text.toCharArray(), start,
957 end - start, bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700958 }
959
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800960 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800961 /* package */static int native_getTextGlyphs(long native_object, String text, int start,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700962 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
963 // FIXME
964 return 0;
965 }
966
967 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000968 /*package*/ static float native_getTextRunAdvances(long native_object,
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700969 long native_typeface /*ignored*/,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700970 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700971 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700972
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700973 // native_typeface is passed here since Framework's old implementation did not have the
974 // typeface object associated with the Paint. Since, we follow the new framework way,
975 // we store the typeface with the paint and use it directly.
976
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700977 if (advances != null)
978 for (int i = advancesIndex; i< advancesIndex+count; i++)
979 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700980 // get the delegate from the native int.
981 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700982 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700983 return 0.f;
984 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700985 boolean isRtl = isRtl(flags);
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700986
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700987 int limit = index + count;
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800988 RectF bounds = new BidiRenderer(null, delegate, text).renderText(
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700989 index, limit, isRtl, advances, advancesIndex, false, 0, 0);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800990 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700991 }
992
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800993 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700994 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700995 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700996 int flags, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700997 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700998 int count = end - start;
999 char[] buffer = TemporaryBuffer.obtain(count);
1000 TextUtils.getChars(text, start, end, buffer, 0);
1001
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001002 return native_getTextRunAdvances(native_object, native_typeface, buffer, 0, count,
1003 contextStart, contextEnd - contextStart, flags, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001004 }
1005
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001006 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001007 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, char[] text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001008 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1009 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001010 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1011 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1012 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001013 }
1014
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001015 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001016 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, String text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001017 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1018 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001019 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1020 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1021 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001022 }
1023
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001024 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001025 /*package*/ static void native_getTextPath(long native_object, int bidiFlags,
1026 char[] text, int index, int count, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001027 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001028 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1029 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001030 }
1031
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001032 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001033 /*package*/ static void native_getTextPath(long native_object, int bidiFlags,
1034 String text, int start, int end, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001035 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001036 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1037 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001038 }
1039
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001040 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001041 /*package*/ static void nativeGetStringBounds(long nativePaint, String text, int start,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001042 int end, int bidiFlags, Rect bounds) {
1043 nativeGetCharArrayBounds(nativePaint, text.toCharArray(), start, end - start, bidiFlags,
1044 bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001045 }
1046
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001047 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001048 /*package*/ static void nativeGetCharArrayBounds(long nativePaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -07001049 int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001050
1051 // get the delegate from the native int.
1052 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001053 if (delegate == null) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001054 return;
1055 }
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001056 delegate.measureText(text, index, count, isRtl(bidiFlags)).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001057 }
1058
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001059 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001060 /*package*/ static void finalizer(long nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001061 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001062 }
1063
1064 // ---- Private delegate/helper methods ----
1065
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001066 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001067 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001068 }
1069
1070 private Paint_Delegate(Paint_Delegate paint) {
1071 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001072 }
1073
1074 private void set(Paint_Delegate paint) {
1075 mFlags = paint.mFlags;
1076 mColor = paint.mColor;
1077 mStyle = paint.mStyle;
1078 mCap = paint.mCap;
1079 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001080 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001081 mTypeface = paint.mTypeface;
1082 mStrokeWidth = paint.mStrokeWidth;
1083 mStrokeMiter = paint.mStrokeMiter;
1084 mTextSize = paint.mTextSize;
1085 mTextScaleX = paint.mTextScaleX;
1086 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001087 mXfermode = paint.mXfermode;
1088 mColorFilter = paint.mColorFilter;
1089 mShader = paint.mShader;
1090 mPathEffect = paint.mPathEffect;
1091 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001092 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001093 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001094 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001095 }
1096
1097 private void reset() {
1098 mFlags = Paint.DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001099 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001100 mStyle = Paint.Style.FILL.nativeInt;
1101 mCap = Paint.Cap.BUTT.nativeInt;
1102 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001103 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001104 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001105 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001106 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001107 mTextSize = 20.f;
1108 mTextScaleX = 1.f;
1109 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001110 mXfermode = null;
1111 mColorFilter = null;
1112 mShader = null;
1113 mPathEffect = null;
1114 mMaskFilter = null;
1115 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001116 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001117 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001118 }
1119
1120 /**
1121 * Update the {@link Font} object from the typeface, text size and scaling
1122 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001123 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001124 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001125 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001126 // Get the fonts from the TypeFace object.
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001127 List<Font> fonts = mTypeface.getFonts(mIsCompact);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001128
1129 // create new font objects as well as FontMetrics, based on the current text size
1130 // and skew info.
1131 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1132 for (Font font : fonts) {
1133 FontInfo info = new FontInfo();
1134 info.mFont = font.deriveFont(mTextSize);
1135 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1136 // TODO: support skew
1137 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001138 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001139 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001140 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001141 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1142
1143 infoList.add(info);
1144 }
1145
1146 mFonts = Collections.unmodifiableList(infoList);
1147 }
1148 }
1149
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001150 /*package*/ RectF measureText(char[] text, int index, int count, boolean isRtl) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001151 return new BidiRenderer(null, this, text).renderText(
1152 index, index + count, isRtl, null, 0, false, 0, 0);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001153 }
1154
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001155 private float getFontMetrics(FontMetrics metrics) {
1156 if (mFonts.size() > 0) {
1157 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1158 if (metrics != null) {
1159 // Android expects negative ascent so we invert the value from Java.
1160 metrics.top = - javaMetrics.getMaxAscent();
1161 metrics.ascent = - javaMetrics.getAscent();
1162 metrics.descent = javaMetrics.getDescent();
1163 metrics.bottom = javaMetrics.getMaxDescent();
1164 metrics.leading = javaMetrics.getLeading();
1165 }
1166
1167 return javaMetrics.getHeight();
1168 }
1169
1170 return 0;
1171 }
1172
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001173 private void setTextLocale(String locale) {
1174 mLocale = new Locale(locale);
1175 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001176
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001177 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1178 // get the delegate from the native int.
1179 Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
1180 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001181 return;
1182 }
1183
1184 if (flagValue) {
1185 delegate.mFlags |= flagMask;
1186 } else {
1187 delegate.mFlags &= ~flagMask;
1188 }
1189 }
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001190
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001191 private static boolean isRtl(int flag) {
1192 switch(flag) {
1193 case Paint.BIDI_RTL:
1194 case Paint.BIDI_FORCE_RTL:
1195 case Paint.BIDI_DEFAULT_RTL:
1196 return true;
1197 default:
1198 return false;
1199 }
1200 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001201}