blob: 0c8c0d6a1827091844d53ab6b3fc1ac70f80fbc0 [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 ----
Deepanshu Gupta382256f2014-08-09 14:14:32 -070068
69 // This list can contain null elements.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070070 private List<FontInfo> mFonts;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070071
72 // ---- delegate data ----
73 private int mFlags;
74 private int mColor;
75 private int mStyle;
76 private int mCap;
77 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070078 private int mTextAlign;
Xavier Ducrohet91672792011-02-22 11:54:37 -080079 private Typeface_Delegate mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070080 private float mStrokeWidth;
81 private float mStrokeMiter;
82 private float mTextSize;
83 private float mTextScaleX;
84 private float mTextSkewX;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -070085 private int mHintingMode = Paint.HINTING_ON;
Deepanshu Gupta8916b212014-06-11 15:40:47 -070086 // Variant of the font. A paint's variant can only be compact or elegant.
87 private FontVariant mFontVariant = FontVariant.COMPACT;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070088
Xavier Ducrohet91672792011-02-22 11:54:37 -080089 private Xfermode_Delegate mXfermode;
90 private ColorFilter_Delegate mColorFilter;
91 private Shader_Delegate mShader;
92 private PathEffect_Delegate mPathEffect;
93 private MaskFilter_Delegate mMaskFilter;
94 private Rasterizer_Delegate mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070095
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070096 private Locale mLocale = Locale.getDefault();
97
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -070098 // Used only to assert invariants.
99 public long mNativeTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700100
101 // ---- Public Helper methods ----
102
Narayan Kamath633d6882014-01-27 14:24:16 +0000103 public static Paint_Delegate getDelegate(long native_paint) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700104 return sManager.getDelegate(native_paint);
105 }
106
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700107 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700108 * Returns the list of {@link Font} objects.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700109 */
110 public List<FontInfo> getFonts() {
111 return mFonts;
112 }
113
Xavier Ducroheta313b652010-11-01 18:45:20 -0700114 public boolean isAntiAliased() {
115 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
116 }
117
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700118 public boolean isFilterBitmap() {
119 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
120 }
121
122 public int getStyle() {
123 return mStyle;
124 }
125
126 public int getColor() {
127 return mColor;
128 }
129
Xavier Ducrohet66225222010-12-21 01:33:04 -0800130 public int getAlpha() {
131 return mColor >>> 24;
132 }
133
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800134 public void setAlpha(int alpha) {
135 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
136 }
137
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700138 public int getTextAlign() {
139 return mTextAlign;
140 }
141
142 public float getStrokeWidth() {
143 return mStrokeWidth;
144 }
145
Xavier Ducrohet66225222010-12-21 01:33:04 -0800146 /**
147 * returns the value of stroke miter needed by the java api.
148 */
149 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800150 float miter = mStrokeMiter * mStrokeWidth;
151 if (miter < 1.f) {
152 miter = 1.f;
153 }
154 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700155 }
156
157 public int getJavaCap() {
158 switch (Paint.sCapArray[mCap]) {
159 case BUTT:
160 return BasicStroke.CAP_BUTT;
161 case ROUND:
162 return BasicStroke.CAP_ROUND;
163 default:
164 case SQUARE:
165 return BasicStroke.CAP_SQUARE;
166 }
167 }
168
169 public int getJavaJoin() {
170 switch (Paint.sJoinArray[mJoin]) {
171 default:
172 case MITER:
173 return BasicStroke.JOIN_MITER;
174 case ROUND:
175 return BasicStroke.JOIN_ROUND;
176 case BEVEL:
177 return BasicStroke.JOIN_BEVEL;
178 }
179 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700180
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800181 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800182 if (mPathEffect != null) {
183 if (mPathEffect.isSupported()) {
184 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800185 assert stroke != null;
186 if (stroke != null) {
187 return stroke;
188 }
189 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800190 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800191 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800192 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800193 }
194 }
195
196 // if no custom stroke as been set, set the default one.
197 return new BasicStroke(
198 getStrokeWidth(),
199 getJavaCap(),
200 getJavaJoin(),
201 getJavaStrokeMiter());
202 }
203
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800204 /**
205 * Returns the {@link Xfermode} delegate or null if none have been set
206 *
207 * @return the delegate or null.
208 */
209 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800210 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700211 }
212
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800213 /**
214 * Returns the {@link ColorFilter} delegate or null if none have been set
215 *
216 * @return the delegate or null.
217 */
218 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800219 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700220 }
221
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800222 /**
223 * Returns the {@link Shader} delegate or null if none have been set
224 *
225 * @return the delegate or null.
226 */
227 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800228 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700229 }
230
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800231 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800232 * Returns the {@link MaskFilter} delegate or null if none have been set
233 *
234 * @return the delegate or null.
235 */
236 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800237 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800238 }
239
240 /**
241 * Returns the {@link Rasterizer} delegate or null if none have been set
242 *
243 * @return the delegate or null.
244 */
245 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800246 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700247 }
248
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700249 // ---- native methods ----
250
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800251 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700252 /*package*/ static int getFlags(Paint thisPaint) {
253 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400254 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700255 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700256 return 0;
257 }
258
259 return delegate.mFlags;
260 }
261
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700262
263
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800264 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700265 /*package*/ static void setFlags(Paint thisPaint, int flags) {
266 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400267 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700268 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700269 return;
270 }
271
272 delegate.mFlags = flags;
273 }
274
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800275 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700276 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700277 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700278 }
279
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800280 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700281 /*package*/ static int getHinting(Paint thisPaint) {
282 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400283 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700284 if (delegate == null) {
285 return Paint.HINTING_ON;
286 }
287
288 return delegate.mHintingMode;
289 }
290
291 @LayoutlibDelegate
292 /*package*/ static void setHinting(Paint thisPaint, int mode) {
293 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400294 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700295 if (delegate == null) {
296 return;
297 }
298
299 delegate.mHintingMode = mode;
300 }
301
302 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700303 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
304 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
305 }
306
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800307 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700308 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
309 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
310 }
311
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800312 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700313 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
314 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
315 }
316
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800317 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700318 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
319 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
320 }
321
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800322 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700323 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
324 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
325 }
326
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800327 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700328 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
329 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
330 }
331
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800332 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700333 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
334 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
335 }
336
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800337 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700338 /*package*/ static int getColor(Paint thisPaint) {
339 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400340 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700341 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700342 return 0;
343 }
344
345 return delegate.mColor;
346 }
347
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800348 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700349 /*package*/ static void setColor(Paint thisPaint, int color) {
350 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400351 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700352 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700353 return;
354 }
355
356 delegate.mColor = color;
357 }
358
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800359 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700360 /*package*/ static int getAlpha(Paint thisPaint) {
361 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400362 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700363 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700364 return 0;
365 }
366
Xavier Ducrohet66225222010-12-21 01:33:04 -0800367 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700368 }
369
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800370 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700371 /*package*/ static void setAlpha(Paint thisPaint, int a) {
372 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400373 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700374 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700375 return;
376 }
377
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800378 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700379 }
380
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800381 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700382 /*package*/ static float getStrokeWidth(Paint thisPaint) {
383 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400384 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700385 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700386 return 1.f;
387 }
388
389 return delegate.mStrokeWidth;
390 }
391
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800392 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700393 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
394 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400395 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700396 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700397 return;
398 }
399
400 delegate.mStrokeWidth = width;
401 }
402
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800403 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700404 /*package*/ static float getStrokeMiter(Paint thisPaint) {
405 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400406 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700407 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700408 return 1.f;
409 }
410
411 return delegate.mStrokeMiter;
412 }
413
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800414 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700415 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
416 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400417 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700418 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700419 return;
420 }
421
422 delegate.mStrokeMiter = miter;
423 }
424
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800425 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700426 /*package*/ static void native_setShadowLayer(long paint, float radius, float dx, float dy,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700427 int color) {
428 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800429 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800430 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700431 }
432
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800433 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700434 /*package*/ static boolean native_hasShadowLayer(long paint) {
435 // FIXME
436 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
437 "Paint.hasShadowLayer is not supported.", null, null /*data*/);
438 return false;
439 }
440
441 @LayoutlibDelegate
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700442 /*package*/ static boolean isElegantTextHeight(Paint thisPaint) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700443 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400444 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700445 return delegate != null && delegate.mFontVariant == FontVariant.ELEGANT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700446 }
447
448 @LayoutlibDelegate
449 /*package*/ static void setElegantTextHeight(Paint thisPaint, boolean elegant) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700450 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400451 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700452 if (delegate == null) {
453 return;
454 }
455
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700456 delegate.mFontVariant = elegant ? FontVariant.ELEGANT : FontVariant.COMPACT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700457 }
458
459 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700460 /*package*/ static float getTextSize(Paint thisPaint) {
461 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400462 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700463 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700464 return 1.f;
465 }
466
467 return delegate.mTextSize;
468 }
469
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800470 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700471 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
472 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400473 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700474 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700475 return;
476 }
477
478 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800479 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700480 }
481
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800482 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700483 /*package*/ static float getTextScaleX(Paint thisPaint) {
484 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400485 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700486 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700487 return 1.f;
488 }
489
490 return delegate.mTextScaleX;
491 }
492
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800493 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700494 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
495 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400496 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700497 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700498 return;
499 }
500
501 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800502 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700503 }
504
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800505 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700506 /*package*/ static float getTextSkewX(Paint thisPaint) {
507 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400508 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700509 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700510 return 1.f;
511 }
512
513 return delegate.mTextSkewX;
514 }
515
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800516 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700517 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
518 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400519 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700520 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700521 return;
522 }
523
524 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800525 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700526 }
527
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800528 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700529 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800530 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400531 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800532 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800533 return 0;
534 }
535
536 if (delegate.mFonts.size() > 0) {
537 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
538 // Android expects negative ascent so we invert the value from Java.
539 return - javaMetrics.getAscent();
540 }
541
542 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700543 }
544
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800545 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700546 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800547 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400548 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800549 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800550 return 0;
551 }
552
553 if (delegate.mFonts.size() > 0) {
554 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
555 return javaMetrics.getDescent();
556 }
557
558 return 0;
559
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700560 }
561
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800562 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700563 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700564 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400565 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700566 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700567 return 0;
568 }
569
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800570 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700571 }
572
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800573 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700574 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700575 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400576 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700577 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700578 return 0;
579 }
580
581 if (delegate.mFonts.size() > 0) {
582 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
583 if (fmi != null) {
584 // Android expects negative ascent so we invert the value from Java.
585 fmi.top = - javaMetrics.getMaxAscent();
586 fmi.ascent = - javaMetrics.getAscent();
587 fmi.descent = javaMetrics.getDescent();
588 fmi.bottom = javaMetrics.getMaxDescent();
589 fmi.leading = javaMetrics.getLeading();
590 }
591
592 return javaMetrics.getHeight();
593 }
594
595 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700596 }
597
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800598 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700599 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700600 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700601 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400602 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700603 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700604 return 0;
605 }
606
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700607 RectF bounds = delegate.measureText(text, index, count, null, 0, bidiFlags);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800608 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700609 }
610
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800611 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700612 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
613 int bidiFlags) {
614 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700615 }
616
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800617 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700618 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
619 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700620 }
621
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800622 @LayoutlibDelegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700623 /*package*/ static int native_breakText(long nativePaint, long nativeTypeface, char[] text,
624 int index, int count, float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800625
626 // get the delegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700627 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800628 if (delegate == null) {
629 return 0;
630 }
631
632 int inc = count > 0 ? 1 : -1;
633
634 int measureIndex = 0;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800635 for (int i = index; i != index + count; i += inc, measureIndex++) {
636 int start, end;
637 if (i < index) {
638 start = i;
639 end = index;
640 } else {
641 start = index;
642 end = i;
643 }
644
645 // measure from start to end
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700646 RectF bounds = delegate.measureText(text, start, end - start + 1, null, 0, bidiFlags);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800647 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800648
649 if (measuredWidth != null) {
650 measuredWidth[measureIndex] = res;
651 }
652
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800653 if (res > maxWidth) {
654 // we should not return this char index, but since it's 0-based
655 // and we need to return a count, we simply return measureIndex;
656 return measureIndex;
657 }
658
659 }
660
661 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700662 }
663
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800664 @LayoutlibDelegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700665 /*package*/ static int native_breakText(long nativePaint, long nativeTypeface, String text,
666 boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700667 float maxWidth, int bidiFlags, float[] measuredWidth) {
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700668 return native_breakText(nativePaint, nativeTypeface, text.toCharArray(), 0, text.length(),
669 maxWidth, bidiFlags, measuredWidth);
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_init() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700674 Paint_Delegate newDelegate = new Paint_Delegate();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800675 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700676 }
677
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800678 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000679 /*package*/ static long native_initWithPaint(long paint) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700680 // get the delegate from the native int.
681 Paint_Delegate delegate = sManager.getDelegate(paint);
682 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700683 return 0;
684 }
685
686 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800687 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700688 }
689
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800690 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000691 /*package*/ static void native_reset(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700692 // get the delegate from the native int.
693 Paint_Delegate delegate = sManager.getDelegate(native_object);
694 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700695 return;
696 }
697
698 delegate.reset();
699 }
700
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800701 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000702 /*package*/ static void native_set(long native_dst, long native_src) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700703 // get the delegate from the native int.
704 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
705 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700706 return;
707 }
708
709 // get the delegate from the native int.
710 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
711 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700712 return;
713 }
714
715 delegate_dst.set(delegate_src);
716 }
717
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800718 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800719 /*package*/ static int native_getStyle(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700720 // get the delegate from the native int.
721 Paint_Delegate delegate = sManager.getDelegate(native_object);
722 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700723 return 0;
724 }
725
726 return delegate.mStyle;
727 }
728
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800729 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000730 /*package*/ static void native_setStyle(long native_object, int style) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700731 // get the delegate from the native int.
732 Paint_Delegate delegate = sManager.getDelegate(native_object);
733 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700734 return;
735 }
736
737 delegate.mStyle = style;
738 }
739
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800740 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800741 /*package*/ static int native_getStrokeCap(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700742 // get the delegate from the native int.
743 Paint_Delegate delegate = sManager.getDelegate(native_object);
744 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700745 return 0;
746 }
747
748 return delegate.mCap;
749 }
750
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800751 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000752 /*package*/ static void native_setStrokeCap(long native_object, int cap) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700753 // get the delegate from the native int.
754 Paint_Delegate delegate = sManager.getDelegate(native_object);
755 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700756 return;
757 }
758
759 delegate.mCap = cap;
760 }
761
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800762 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800763 /*package*/ static int native_getStrokeJoin(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700764 // get the delegate from the native int.
765 Paint_Delegate delegate = sManager.getDelegate(native_object);
766 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700767 return 0;
768 }
769
770 return delegate.mJoin;
771 }
772
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800773 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000774 /*package*/ static void native_setStrokeJoin(long native_object, int join) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700775 // get the delegate from the native int.
776 Paint_Delegate delegate = sManager.getDelegate(native_object);
777 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700778 return;
779 }
780
781 delegate.mJoin = join;
782 }
783
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800784 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000785 /*package*/ static boolean native_getFillPath(long native_object, long src, long dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800786 Paint_Delegate paint = sManager.getDelegate(native_object);
787 if (paint == null) {
788 return false;
789 }
790
791 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
792 if (srcPath == null) {
793 return true;
794 }
795
796 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
797 if (dstPath == null) {
798 return true;
799 }
800
801 Stroke stroke = paint.getJavaStroke();
802 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
803
804 dstPath.setJavaShape(strokeShape);
805
806 // FIXME figure out the return value?
807 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700808 }
809
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800810 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000811 /*package*/ static long native_setShader(long native_object, long shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700812 // get the delegate from the native int.
813 Paint_Delegate delegate = sManager.getDelegate(native_object);
814 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700815 return shader;
816 }
817
Xavier Ducrohet91672792011-02-22 11:54:37 -0800818 delegate.mShader = Shader_Delegate.getDelegate(shader);
819
820 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700821 }
822
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800823 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000824 /*package*/ static long native_setColorFilter(long native_object, long filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700825 // get the delegate from the native int.
826 Paint_Delegate delegate = sManager.getDelegate(native_object);
827 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700828 return filter;
829 }
830
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700831 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800832
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700833 // Log warning if it's not supported.
834 if (delegate.mColorFilter != null && !delegate.mColorFilter.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800835 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800836 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800837 }
838
839 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700840 }
841
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800842 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000843 /*package*/ static long native_setXfermode(long native_object, long xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700844 // get the delegate from the native int.
845 Paint_Delegate delegate = sManager.getDelegate(native_object);
846 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700847 return xfermode;
848 }
849
Xavier Ducrohet91672792011-02-22 11:54:37 -0800850 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
851
852 return xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700853 }
854
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800855 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000856 /*package*/ static long native_setPathEffect(long native_object, long effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700857 // get the delegate from the native int.
858 Paint_Delegate delegate = sManager.getDelegate(native_object);
859 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700860 return effect;
861 }
862
Xavier Ducrohet91672792011-02-22 11:54:37 -0800863 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
864
865 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700866 }
867
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800868 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000869 /*package*/ static long native_setMaskFilter(long native_object, long maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700870 // get the delegate from the native int.
871 Paint_Delegate delegate = sManager.getDelegate(native_object);
872 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700873 return maskfilter;
874 }
875
Xavier Ducrohet91672792011-02-22 11:54:37 -0800876 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800877
878 // since none of those are supported, display a fidelity warning right away
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700879 if (delegate.mMaskFilter != null && !delegate.mMaskFilter.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800880 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800881 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800882 }
883
884 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700885 }
886
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800887 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000888 /*package*/ static long native_setTypeface(long native_object, long typeface) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700889 // get the delegate from the native int.
890 Paint_Delegate delegate = sManager.getDelegate(native_object);
891 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700892 return 0;
893 }
894
Xavier Ducrohet91672792011-02-22 11:54:37 -0800895 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -0700896 delegate.mNativeTypeface = typeface;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800897 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800898 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700899 }
900
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800901 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000902 /*package*/ static long native_setRasterizer(long native_object, long rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800903 // get the delegate from the native int.
904 Paint_Delegate delegate = sManager.getDelegate(native_object);
905 if (delegate == null) {
906 return rasterizer;
907 }
908
Xavier Ducrohet91672792011-02-22 11:54:37 -0800909 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800910
911 // since none of those are supported, display a fidelity warning right away
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700912 if (delegate.mRasterizer != null && !delegate.mRasterizer.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800913 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800914 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800915 }
916
917 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700918 }
919
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800920 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800921 /*package*/ static int native_getTextAlign(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700922 // get the delegate from the native int.
923 Paint_Delegate delegate = sManager.getDelegate(native_object);
924 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700925 return 0;
926 }
927
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700928 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700929 }
930
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800931 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000932 /*package*/ static void native_setTextAlign(long native_object, int align) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700933 // get the delegate from the native int.
934 Paint_Delegate delegate = sManager.getDelegate(native_object);
935 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700936 return;
937 }
938
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700939 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700940 }
941
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800942 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000943 /*package*/ static void native_setTextLocale(long native_object, String locale) {
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700944 // get the delegate from the native int.
945 Paint_Delegate delegate = sManager.getDelegate(native_object);
946 if (delegate == null) {
947 return;
948 }
949
950 delegate.setTextLocale(locale);
951 }
952
953 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700954 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
955 char[] text, int index, int count, int bidiFlags, float[] widths) {
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700956
957 if (widths != null) {
958 for (int i = 0; i< count; i++) {
959 widths[i]=0;
960 }
961 }
962 // get the delegate from the native int.
963 Paint_Delegate delegate = sManager.getDelegate(native_object);
964 if (delegate == null) {
965 return 0;
966 }
967
968 // native_typeface is passed here since Framework's old implementation did not have the
969 // typeface object associated with the Paint. Since, we follow the new framework way,
970 // we store the typeface with the paint and use it directly.
971 assert (native_typeface == delegate.mNativeTypeface);
972
973 RectF bounds = delegate.measureText(text, index, count, widths, 0, bidiFlags);
974 return ((int) (bounds.right - bounds.left));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700975 }
976
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800977 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700978 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
979 String text, int start, int end, int bidiFlags, float[] widths) {
980 return native_getTextWidths(native_object, native_typeface, text.toCharArray(), start,
981 end - start, bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700982 }
983
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800984 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800985 /* package */static int native_getTextGlyphs(long native_object, String text, int start,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700986 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
987 // FIXME
988 return 0;
989 }
990
991 @LayoutlibDelegate
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -0700992 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700993 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700994 boolean isRtl, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -0700995
996 if (advances != null)
997 for (int i = advancesIndex; i< advancesIndex+count; i++)
998 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700999 // get the delegate from the native int.
1000 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001001 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001002 return 0.f;
1003 }
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001004
1005 // native_typeface is passed here since Framework's old implementation did not have the
1006 // typeface object associated with the Paint. Since, we follow the new framework way,
1007 // we store the typeface with the paint and use it directly.
1008 assert (native_typeface == delegate.mNativeTypeface);
1009
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001010 RectF bounds = delegate.measureText(text, index, count, advances, advancesIndex, isRtl);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001011 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001012 }
1013
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001014 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001015 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001016 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001017 boolean isRtl, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001018 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001019 int count = end - start;
1020 char[] buffer = TemporaryBuffer.obtain(count);
1021 TextUtils.getChars(text, start, end, buffer, 0);
1022
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001023 return native_getTextRunAdvances(native_object, native_typeface, buffer, 0, count,
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001024 contextStart, contextEnd - contextStart, isRtl, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001025 }
1026
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001027 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001028 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, char[] text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001029 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1030 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001031 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1032 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1033 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001034 }
1035
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001036 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001037 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, String text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001038 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1039 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001040 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1041 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1042 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001043 }
1044
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001045 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001046 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1047 int bidiFlags, char[] text, int index, int count, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001048 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001049 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1050 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001051 }
1052
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001053 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001054 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1055 int bidiFlags, String text, int start, int end, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001056 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001057 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1058 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001059 }
1060
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001061 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001062 /*package*/ static void nativeGetStringBounds(long nativePaint, long native_typeface,
1063 String text, int start, int end, int bidiFlags, Rect bounds) {
1064 nativeGetCharArrayBounds(nativePaint, native_typeface, text.toCharArray(), start,
1065 end - start, bidiFlags, bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001066 }
1067
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001068 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001069 /*package*/ static void nativeGetCharArrayBounds(long nativePaint, long native_typeface,
1070 char[] text, int index, int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001071
1072 // get the delegate from the native int.
1073 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001074 if (delegate == null) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001075 return;
1076 }
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001077
1078 // assert that the typeface passed is actually the one that we had stored.
1079 assert (native_typeface == delegate.mNativeTypeface);
1080
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001081 delegate.measureText(text, index, count, null, 0, bidiFlags).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001082 }
1083
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001084 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001085 /*package*/ static void finalizer(long nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001086 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001087 }
1088
Deepanshu Guptaa74fdf02014-07-30 20:18:48 -07001089 @LayoutlibDelegate
1090 /*package*/ static float native_getLetterSpacing(long nativePaint) {
1091 // TODO: throw a fidelity warning.
1092 return 0;
1093 }
1094
1095 @LayoutlibDelegate
1096 /*package*/ static void native_setLetterSpacing(long nativePaint, float letterSpacing) {
1097 // pass.
1098 }
1099
1100 @LayoutlibDelegate
1101 /*package*/ static void native_setFontFeatureSettings(long nativePaint, String settings) {
1102 // pass.
1103 }
1104
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001105 // ---- Private delegate/helper methods ----
1106
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001107 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001108 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001109 }
1110
1111 private Paint_Delegate(Paint_Delegate paint) {
1112 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001113 }
1114
1115 private void set(Paint_Delegate paint) {
1116 mFlags = paint.mFlags;
1117 mColor = paint.mColor;
1118 mStyle = paint.mStyle;
1119 mCap = paint.mCap;
1120 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001121 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001122 mTypeface = paint.mTypeface;
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001123 mNativeTypeface = paint.mNativeTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001124 mStrokeWidth = paint.mStrokeWidth;
1125 mStrokeMiter = paint.mStrokeMiter;
1126 mTextSize = paint.mTextSize;
1127 mTextScaleX = paint.mTextScaleX;
1128 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001129 mXfermode = paint.mXfermode;
1130 mColorFilter = paint.mColorFilter;
1131 mShader = paint.mShader;
1132 mPathEffect = paint.mPathEffect;
1133 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001134 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001135 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001136 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001137 }
1138
1139 private void reset() {
Chris Craikf2437d32015-05-13 13:09:50 -07001140 mFlags = Paint.HIDDEN_DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001141 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001142 mStyle = Paint.Style.FILL.nativeInt;
1143 mCap = Paint.Cap.BUTT.nativeInt;
1144 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001145 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001146 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001147 mNativeTypeface = 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001148 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001149 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001150 mTextSize = 20.f;
1151 mTextScaleX = 1.f;
1152 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001153 mXfermode = null;
1154 mColorFilter = null;
1155 mShader = null;
1156 mPathEffect = null;
1157 mMaskFilter = null;
1158 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001159 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001160 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001161 }
1162
1163 /**
1164 * Update the {@link Font} object from the typeface, text size and scaling
1165 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001166 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001167 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001168 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001169 // Get the fonts from the TypeFace object.
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001170 List<Font> fonts = mTypeface.getFonts(mFontVariant);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001171
1172 // create new font objects as well as FontMetrics, based on the current text size
1173 // and skew info.
1174 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1175 for (Font font : fonts) {
Deepanshu Gupta382256f2014-08-09 14:14:32 -07001176 if (font == null) {
1177 // If the font is null, add null to infoList. When rendering the text, if this
1178 // null is reached, a warning will be logged.
1179 infoList.add(null);
1180 continue;
1181 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001182 FontInfo info = new FontInfo();
1183 info.mFont = font.deriveFont(mTextSize);
1184 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1185 // TODO: support skew
1186 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001187 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001188 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001189 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001190 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1191
1192 infoList.add(info);
1193 }
1194
1195 mFonts = Collections.unmodifiableList(infoList);
1196 }
1197 }
1198
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001199 /*package*/ RectF measureText(char[] text, int index, int count, float[] advances,
1200 int advancesIndex, int bidiFlags) {
1201 return new BidiRenderer(null, this, text)
1202 .renderText(index, index + count, bidiFlags, advances, advancesIndex, false);
1203 }
1204
1205 /*package*/ RectF measureText(char[] text, int index, int count, float[] advances,
1206 int advancesIndex, boolean isRtl) {
1207 return new BidiRenderer(null, this, text)
1208 .renderText(index, index + count, isRtl, advances, advancesIndex, false);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001209 }
1210
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001211 private float getFontMetrics(FontMetrics metrics) {
1212 if (mFonts.size() > 0) {
1213 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1214 if (metrics != null) {
1215 // Android expects negative ascent so we invert the value from Java.
1216 metrics.top = - javaMetrics.getMaxAscent();
1217 metrics.ascent = - javaMetrics.getAscent();
1218 metrics.descent = javaMetrics.getDescent();
1219 metrics.bottom = javaMetrics.getMaxDescent();
1220 metrics.leading = javaMetrics.getLeading();
1221 }
1222
1223 return javaMetrics.getHeight();
1224 }
1225
1226 return 0;
1227 }
1228
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001229 private void setTextLocale(String locale) {
1230 mLocale = new Locale(locale);
1231 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001232
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001233 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1234 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -04001235 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001236 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001237 return;
1238 }
1239
1240 if (flagValue) {
1241 delegate.mFlags |= flagMask;
1242 } else {
1243 delegate.mFlags &= ~flagMask;
1244 }
1245 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001246}