Layoutlib Canvas and Paint implementation through native delegates
Also fix native delegate generation to put "this" parameter even for methods that don't have any parameters. Change-Id: I5dd0c505871370ff7b4cda16de84a5b3ae438f73
This commit is contained in:
@@ -59,6 +59,20 @@ public class Bitmap_Delegate {
|
||||
|
||||
// ---- Public Helper methods ----
|
||||
|
||||
/**
|
||||
* Returns the native delegate associated to a given {@link Bitmap_Delegate} object.
|
||||
*/
|
||||
public static Bitmap_Delegate getDelegate(Bitmap bitmap) {
|
||||
return sManager.getDelegate(bitmap.mNativeBitmap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the native delegate associated to a given an int referencing a {@link Bitmap} object.
|
||||
*/
|
||||
public static Bitmap_Delegate getDelegate(int native_bitmap) {
|
||||
return sManager.getDelegate(native_bitmap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a {@link Bitmap} initialized with the given file content.
|
||||
*/
|
||||
@@ -118,6 +132,13 @@ public class Bitmap_Delegate {
|
||||
return BufferedImage.TYPE_INT_ARGB;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}.
|
||||
*/
|
||||
public BufferedImage getImage() {
|
||||
return mImage;
|
||||
}
|
||||
|
||||
// ---- native methods ----
|
||||
|
||||
/*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width,
|
||||
@@ -127,8 +148,7 @@ public class Bitmap_Delegate {
|
||||
// create the image
|
||||
BufferedImage image = new BufferedImage(width, height, imageType);
|
||||
|
||||
// fill it
|
||||
//image.setRGB(x, y, rgb)
|
||||
// FIXME fill the bitmap!
|
||||
|
||||
// create a delegate with the content of the stream.
|
||||
Bitmap_Delegate delegate = new Bitmap_Delegate(image);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
559
tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java
Normal file
559
tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java
Normal file
@@ -0,0 +1,559 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.graphics;
|
||||
|
||||
import com.android.layoutlib.api.ILayoutLog;
|
||||
import com.android.layoutlib.bridge.DelegateManager;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Delegate implementing the native methods of android.graphics.Canvas
|
||||
*
|
||||
* Through the layoutlib_create tool, the original native methods of Canvas have been replaced
|
||||
* by calls to methods of the same name in this delegate class.
|
||||
*
|
||||
* This class behaves like the original native implementation, but in Java, keeping previously
|
||||
* native data into its own objects and mapping them to int that are sent back and forth between
|
||||
* it and the original Canvas class.
|
||||
*
|
||||
* @see DelegateManager
|
||||
*
|
||||
*/
|
||||
public class Canvas_Delegate {
|
||||
|
||||
// ---- delegate manager ----
|
||||
private static final DelegateManager<Canvas_Delegate> sManager =
|
||||
new DelegateManager<Canvas_Delegate>();
|
||||
|
||||
// ---- delegate helper data ----
|
||||
|
||||
// ---- delegate data ----
|
||||
private BufferedImage mBufferedImage;
|
||||
private final Stack<Graphics2D> mGraphicsStack = new Stack<Graphics2D>();
|
||||
private ILayoutLog mLogger;
|
||||
|
||||
// ---- Public Helper methods ----
|
||||
|
||||
/**
|
||||
* Returns the native delegate associated to a given {@link Canvas} object.
|
||||
*/
|
||||
public static Canvas_Delegate getDelegate(Canvas canvas) {
|
||||
return sManager.getDelegate(canvas.mNativeCanvas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the native delegate associated to a given an int referencing a {@link Canvas} object.
|
||||
*/
|
||||
public static Canvas_Delegate getDelegate(int native_canvas) {
|
||||
return sManager.getDelegate(native_canvas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the layoutlib logger into the canvas.
|
||||
* @param logger
|
||||
*/
|
||||
public void setLogger(ILayoutLog logger) {
|
||||
mLogger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current {@link Graphics2D} used to draw.
|
||||
*/
|
||||
public Graphics2D getGraphics2d() {
|
||||
return mGraphicsStack.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes of the {@link Graphics2D} stack.
|
||||
*/
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
// ---- native methods ----
|
||||
|
||||
/*package*/ static boolean isOpaque(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int getWidth(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int getHeight(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void translate(Canvas thisCanvas, float dx, float dy) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void rotate(Canvas thisCanvas, float degrees) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void scale(Canvas thisCanvas, float sx, float sy) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void skew(Canvas thisCanvas, float sx, float sy) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean clipRect(Canvas thisCanvas, RectF rect) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean clipRect(Canvas thisCanvas, Rect rect) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean clipRect(Canvas thisCanvas, float left, float top, float right,
|
||||
float bottom) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean clipRect(Canvas thisCanvas, int left, int top, int right,
|
||||
int bottom) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int save(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int save(Canvas thisCanvas, int saveFlags) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void restore(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int getSaveCount(Canvas thisCanvas) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void restoreToCount(Canvas thisCanvas, int saveCount) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void drawPoints(Canvas thisCanvas, float[] pts, int offset, int count,
|
||||
Paint paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void drawPoint(Canvas thisCanvas, float x, float y, Paint paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void drawLines(Canvas thisCanvas, float[] pts, int offset, int count,
|
||||
Paint paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void freeCaches() {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int initRaster(int nativeBitmapOrZero) {
|
||||
if (nativeBitmapOrZero > 0) {
|
||||
// get the Bitmap from the int
|
||||
Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nativeBitmapOrZero);
|
||||
|
||||
// create a new Canvas_Delegate with the given bitmap and return its new native int.
|
||||
Canvas_Delegate newDelegate = new Canvas_Delegate(bitmapDelegate.getImage());
|
||||
|
||||
return sManager.addDelegate(newDelegate);
|
||||
} else {
|
||||
// create a new Canvas_Delegate and return its new native int.
|
||||
Canvas_Delegate newDelegate = new Canvas_Delegate();
|
||||
|
||||
return sManager.addDelegate(newDelegate);
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ static void native_setBitmap(int nativeCanvas, int bitmap) {
|
||||
// get the delegate from the native int.
|
||||
Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
|
||||
if (canvasDelegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
// get the delegate from the native int.
|
||||
Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
|
||||
if (bitmapDelegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
canvasDelegate.setBitmap(bitmapDelegate.getImage());
|
||||
}
|
||||
|
||||
/*package*/ static int native_saveLayer(int nativeCanvas, RectF bounds,
|
||||
int paint, int layerFlags) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_saveLayer(int nativeCanvas, float l,
|
||||
float t, float r, float b,
|
||||
int paint, int layerFlags) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_saveLayerAlpha(int nativeCanvas,
|
||||
RectF bounds, int alpha,
|
||||
int layerFlags) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_saveLayerAlpha(int nativeCanvas, float l,
|
||||
float t, float r, float b,
|
||||
int alpha, int layerFlags) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static void native_concat(int nCanvas, int nMatrix) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_setMatrix(int nCanvas, int nMatrix) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_clipRect(int nCanvas,
|
||||
float left, float top,
|
||||
float right, float bottom,
|
||||
int regionOp) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_clipPath(int nativeCanvas,
|
||||
int nativePath,
|
||||
int regionOp) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_clipRegion(int nativeCanvas,
|
||||
int nativeRegion,
|
||||
int regionOp) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeSetDrawFilter(int nativeCanvas,
|
||||
int nativeFilter) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_getClipBounds(int nativeCanvas,
|
||||
Rect bounds) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_getCTM(int canvas, int matrix) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_quickReject(int nativeCanvas,
|
||||
RectF rect,
|
||||
int native_edgeType) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_quickReject(int nativeCanvas,
|
||||
int path,
|
||||
int native_edgeType) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_quickReject(int nativeCanvas,
|
||||
float left, float top,
|
||||
float right, float bottom,
|
||||
int native_edgeType) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawRGB(int nativeCanvas, int r, int g,
|
||||
int b) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawARGB(int nativeCanvas, int a, int r,
|
||||
int g, int b) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawColor(int nativeCanvas, int color) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawColor(int nativeCanvas, int color,
|
||||
int mode) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawPaint(int nativeCanvas, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawLine(int nativeCanvas, float startX,
|
||||
float startY, float stopX,
|
||||
float stopY, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawRect(int nativeCanvas, RectF rect,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawRect(int nativeCanvas, float left,
|
||||
float top, float right,
|
||||
float bottom, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawOval(int nativeCanvas, RectF oval,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawCircle(int nativeCanvas, float cx,
|
||||
float cy, float radius,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawArc(int nativeCanvas, RectF oval,
|
||||
float startAngle, float sweep,
|
||||
boolean useCenter, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawRoundRect(int nativeCanvas,
|
||||
RectF rect, float rx,
|
||||
float ry, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawPath(int nativeCanvas, int path,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawBitmap(Canvas thisCanvas, int nativeCanvas, int bitmap,
|
||||
float left, float top,
|
||||
int nativePaintOrZero,
|
||||
int canvasDensity,
|
||||
int screenDensity,
|
||||
int bitmapDensity) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawBitmap(Canvas thisCanvas, int nativeCanvas, int bitmap,
|
||||
Rect src, RectF dst,
|
||||
int nativePaintOrZero,
|
||||
int screenDensity,
|
||||
int bitmapDensity) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawBitmap(int nativeCanvas, int bitmap,
|
||||
Rect src, Rect dst,
|
||||
int nativePaintOrZero,
|
||||
int screenDensity,
|
||||
int bitmapDensity) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawBitmap(int nativeCanvas, int[] colors,
|
||||
int offset, int stride, float x,
|
||||
float y, int width, int height,
|
||||
boolean hasAlpha,
|
||||
int nativePaintOrZero) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeDrawBitmapMatrix(int nCanvas, int nBitmap,
|
||||
int nMatrix, int nPaint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeDrawBitmapMesh(int nCanvas, int nBitmap,
|
||||
int meshWidth, int meshHeight,
|
||||
float[] verts, int vertOffset,
|
||||
int[] colors, int colorOffset, int nPaint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeDrawVertices(int nCanvas, int mode, int n,
|
||||
float[] verts, int vertOffset, float[] texs, int texOffset,
|
||||
int[] colors, int colorOffset, short[] indices,
|
||||
int indexOffset, int indexCount, int nPaint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static void native_drawText(int nativeCanvas, char[] text,
|
||||
int index, int count, float x,
|
||||
float y, int flags, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawText(int nativeCanvas, String text,
|
||||
int start, int end, float x,
|
||||
float y, int flags, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static void native_drawTextRun(int nativeCanvas, String text,
|
||||
int start, int end, int contextStart, int contextEnd,
|
||||
float x, float y, int flags, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static void native_drawTextRun(int nativeCanvas, char[] text,
|
||||
int start, int count, int contextStart, int contextCount,
|
||||
float x, float y, int flags, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static void native_drawPosText(int nativeCanvas,
|
||||
char[] text, int index,
|
||||
int count, float[] pos,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawPosText(int nativeCanvas,
|
||||
String text, float[] pos,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawTextOnPath(int nativeCanvas,
|
||||
char[] text, int index,
|
||||
int count, int path,
|
||||
float hOffset,
|
||||
float vOffset, int bidiFlags,
|
||||
int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawTextOnPath(int nativeCanvas,
|
||||
String text, int path,
|
||||
float hOffset,
|
||||
float vOffset,
|
||||
int flags, int paint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_drawPicture(int nativeCanvas,
|
||||
int nativePicture) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void finalizer(int nativeCanvas) {
|
||||
sManager.removeDelegate(nativeCanvas);
|
||||
}
|
||||
|
||||
// ---- Private delegate/helper methods ----
|
||||
|
||||
private Canvas_Delegate(BufferedImage image) {
|
||||
setBitmap(image);
|
||||
}
|
||||
|
||||
private Canvas_Delegate() {
|
||||
}
|
||||
|
||||
private void setBitmap(BufferedImage image) {
|
||||
mBufferedImage = image;
|
||||
mGraphicsStack.push(mBufferedImage.createGraphics());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
750
tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java
Normal file
750
tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java
Normal file
@@ -0,0 +1,750 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.graphics;
|
||||
|
||||
import com.android.layoutlib.bridge.DelegateManager;
|
||||
|
||||
import android.graphics.Paint.FontMetrics;
|
||||
import android.graphics.Paint.FontMetricsInt;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Delegate implementing the native methods of android.graphics.Paint
|
||||
*
|
||||
* Through the layoutlib_create tool, the original native methods of Paint have been replaced
|
||||
* by calls to methods of the same name in this delegate class.
|
||||
*
|
||||
* This class behaves like the original native implementation, but in Java, keeping previously
|
||||
* native data into its own objects and mapping them to int that are sent back and forth between
|
||||
* it and the original Paint class.
|
||||
*
|
||||
* @see DelegateManager
|
||||
*
|
||||
*/
|
||||
public class Paint_Delegate {
|
||||
|
||||
/**
|
||||
* Class associating a {@link Font} and it's {@link java.awt.FontMetrics}.
|
||||
*/
|
||||
public static final class FontInfo {
|
||||
Font mFont;
|
||||
java.awt.FontMetrics mMetrics;
|
||||
}
|
||||
|
||||
// ---- delegate manager ----
|
||||
private static final DelegateManager<Paint_Delegate> sManager =
|
||||
new DelegateManager<Paint_Delegate>();
|
||||
|
||||
// ---- delegate helper data ----
|
||||
private List<FontInfo> mFonts;
|
||||
private final FontRenderContext mFontContext = new FontRenderContext(
|
||||
new AffineTransform(), true, true);
|
||||
|
||||
// ---- delegate data ----
|
||||
private int mFlags;
|
||||
private int mColor;
|
||||
private int mStyle;
|
||||
private int mCap;
|
||||
private int mJoin;
|
||||
private int mAlign;
|
||||
private int mTypeface;
|
||||
private float mStrokeWidth;
|
||||
private float mStrokeMiter;
|
||||
private float mTextSize;
|
||||
private float mTextScaleX;
|
||||
private float mTextSkewX;
|
||||
|
||||
|
||||
// ---- Public Helper methods ----
|
||||
|
||||
/**
|
||||
* Returns the list of {@link Font} objects. The first item is the main font, the rest
|
||||
* are fall backs for characters not present in the main font.
|
||||
*/
|
||||
public List<FontInfo> getFonts() {
|
||||
return mFonts;
|
||||
}
|
||||
|
||||
|
||||
// ---- native methods ----
|
||||
|
||||
/*package*/ static int getFlags(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mFlags;
|
||||
}
|
||||
|
||||
/*package*/ static void setFlags(Paint thisPaint, int flags) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mFlags = flags;
|
||||
}
|
||||
|
||||
/*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
|
||||
setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
|
||||
}
|
||||
|
||||
/*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
|
||||
setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
|
||||
}
|
||||
|
||||
/*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
|
||||
setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
|
||||
}
|
||||
|
||||
/*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
|
||||
setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
|
||||
}
|
||||
|
||||
/*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
|
||||
setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
|
||||
}
|
||||
|
||||
/*package*/ static void setDither(Paint thisPaint, boolean dither) {
|
||||
setFlag(thisPaint, Paint.DITHER_FLAG, dither);
|
||||
}
|
||||
|
||||
/*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
|
||||
setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
|
||||
}
|
||||
|
||||
/*package*/ static int getColor(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mColor;
|
||||
}
|
||||
|
||||
/*package*/ static void setColor(Paint thisPaint, int color) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mColor = color;
|
||||
}
|
||||
|
||||
/*package*/ static int getAlpha(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mColor >>> 24;
|
||||
}
|
||||
|
||||
/*package*/ static void setAlpha(Paint thisPaint, int a) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mColor = (a << 24) | (delegate.mColor & 0x00FFFFFF);
|
||||
}
|
||||
|
||||
/*package*/ static float getStrokeWidth(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
return delegate.mStrokeWidth;
|
||||
}
|
||||
|
||||
/*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mStrokeWidth = width;
|
||||
}
|
||||
|
||||
/*package*/ static float getStrokeMiter(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
return delegate.mStrokeMiter;
|
||||
}
|
||||
|
||||
/*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mStrokeMiter = miter;
|
||||
}
|
||||
|
||||
/*package*/ static void nSetShadowLayer(Paint thisPaint, float radius, float dx, float dy,
|
||||
int color) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float getTextSize(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
return delegate.mTextSize;
|
||||
}
|
||||
|
||||
/*package*/ static void setTextSize(Paint thisPaint, float textSize) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mTextSize = textSize;
|
||||
}
|
||||
|
||||
/*package*/ static float getTextScaleX(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
return delegate.mTextScaleX;
|
||||
}
|
||||
|
||||
/*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mTextScaleX = scaleX;
|
||||
}
|
||||
|
||||
/*package*/ static float getTextSkewX(Paint thisPaint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
return delegate.mTextSkewX;
|
||||
}
|
||||
|
||||
/*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mTextSkewX = skewX;
|
||||
}
|
||||
|
||||
/*package*/ static float ascent(Paint thisPaint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float descent(Paint thisPaint) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
|
||||
int count) {
|
||||
// WARNING: the logic in this method is similar to Canvas.drawText.
|
||||
// Any change to this method should be reflected in Canvas.drawText
|
||||
|
||||
// get the delegate
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (delegate.mFonts.size() > 0) {
|
||||
FontInfo mainFont = delegate.mFonts.get(0);
|
||||
int i = index;
|
||||
int lastIndex = index + count;
|
||||
float total = 0f;
|
||||
while (i < lastIndex) {
|
||||
// always start with the main font.
|
||||
int upTo = mainFont.mFont.canDisplayUpTo(text, i, lastIndex);
|
||||
if (upTo == -1) {
|
||||
// shortcut to exit
|
||||
return total + mainFont.mMetrics.charsWidth(text, i, lastIndex - i);
|
||||
} else if (upTo > 0) {
|
||||
total += mainFont.mMetrics.charsWidth(text, i, upTo - i);
|
||||
i = upTo;
|
||||
// don't call continue at this point. Since it is certain the main font
|
||||
// cannot display the font a index upTo (now ==i), we move on to the
|
||||
// fallback fonts directly.
|
||||
}
|
||||
|
||||
// no char supported, attempt to read the next char(s) with the
|
||||
// fallback font. In this case we only test the first character
|
||||
// and then go back to test with the main font.
|
||||
// Special test for 2-char characters.
|
||||
boolean foundFont = false;
|
||||
for (int f = 1 ; f < delegate.mFonts.size() ; f++) {
|
||||
FontInfo fontInfo = delegate.mFonts.get(f);
|
||||
|
||||
// need to check that the font can display the character. We test
|
||||
// differently if the char is a high surrogate.
|
||||
int charCount = Character.isHighSurrogate(text[i]) ? 2 : 1;
|
||||
upTo = fontInfo.mFont.canDisplayUpTo(text, i, i + charCount);
|
||||
if (upTo == -1) {
|
||||
total += fontInfo.mMetrics.charsWidth(text, i, charCount);
|
||||
i += charCount;
|
||||
foundFont = true;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// in case no font can display the char, measure it with the main font.
|
||||
if (foundFont == false) {
|
||||
int size = Character.isHighSurrogate(text[i]) ? 2 : 1;
|
||||
total += mainFont.mMetrics.charsWidth(text, i, size);
|
||||
i += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end) {
|
||||
return native_measureText(thisPaint, text.toCharArray(), start, end - start);
|
||||
}
|
||||
|
||||
/*package*/ static float native_measureText(Paint thisPaint, String text) {
|
||||
return native_measureText(thisPaint, text.toCharArray(), 0, text.length());
|
||||
}
|
||||
|
||||
/*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count,
|
||||
float maxWidth, float[] measuredWidth) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards,
|
||||
float maxWidth, float[] measuredWidth) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static int native_init() {
|
||||
Paint_Delegate newDelegate = new Paint_Delegate();
|
||||
return sManager.addDelegate(newDelegate);
|
||||
}
|
||||
|
||||
/*package*/ static int native_initWithPaint(int paint) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(paint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Paint_Delegate newDelegate = new Paint_Delegate(delegate);
|
||||
return sManager.addDelegate(newDelegate);
|
||||
}
|
||||
|
||||
/*package*/ static void native_reset(int native_object) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.reset();
|
||||
}
|
||||
|
||||
/*package*/ static void native_set(int native_dst, int native_src) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
|
||||
if (delegate_dst == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate_src = sManager.getDelegate(native_src);
|
||||
if (delegate_src == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate_dst.set(delegate_src);
|
||||
}
|
||||
|
||||
/*package*/ static int native_getStyle(int native_object) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mStyle;
|
||||
}
|
||||
|
||||
/*package*/ static void native_setStyle(int native_object, int style) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mStyle = style;
|
||||
}
|
||||
|
||||
/*package*/ static int native_getStrokeCap(int native_object) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mCap;
|
||||
}
|
||||
|
||||
/*package*/ static void native_setStrokeCap(int native_object, int cap) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mCap = cap;
|
||||
}
|
||||
|
||||
/*package*/ static int native_getStrokeJoin(int native_object) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mJoin;
|
||||
}
|
||||
|
||||
/*package*/ static void native_setStrokeJoin(int native_object, int join) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mJoin = join;
|
||||
}
|
||||
|
||||
/*package*/ static boolean native_getFillPath(int native_object, int src, int dst) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setShader(int native_object, int shader) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setColorFilter(int native_object, int filter) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setXfermode(int native_object, int xfermode) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setPathEffect(int native_object, int effect) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setMaskFilter(int native_object, int maskfilter) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_setTypeface(int native_object, int typeface) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mTypeface = typeface;
|
||||
}
|
||||
|
||||
/*package*/ static int native_setRasterizer(int native_object, int rasterizer) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
/*package*/ static int native_getTextAlign(int native_object) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return delegate.mAlign;
|
||||
}
|
||||
|
||||
/*package*/ static void native_setTextAlign(int native_object, int align) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(native_object);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
delegate.mAlign = align;
|
||||
}
|
||||
|
||||
/*package*/ static float native_getFontMetrics(int native_paint, FontMetrics metrics) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_getTextWidths(int native_object, char[] text, int index,
|
||||
int count, float[] widths) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_getTextWidths(int native_object, String text, int start,
|
||||
int end, float[] widths) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float native_getTextRunAdvances(int native_object,
|
||||
char[] text, int index, int count, int contextIndex, int contextCount,
|
||||
int flags, float[] advances, int advancesIndex) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static float native_getTextRunAdvances(int native_object,
|
||||
String text, int start, int end, int contextStart, int contextEnd,
|
||||
int flags, float[] advances, int advancesIndex) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, char[] text,
|
||||
int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, String text,
|
||||
int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_getTextPath(int native_object, int bidiFlags,
|
||||
char[] text, int index, int count, float x, float y, int path) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void native_getTextPath(int native_object, int bidiFlags,
|
||||
String text, int start, int end, float x, float y, int path) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeGetStringBounds(int nativePaint, String text, int start,
|
||||
int end, Rect bounds) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void nativeGetCharArrayBounds(int nativePaint, char[] text, int index,
|
||||
int count, Rect bounds) {
|
||||
// FIXME
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*package*/ static void finalizer(int nativePaint) {
|
||||
sManager.removeDelegate(nativePaint);
|
||||
}
|
||||
|
||||
// ---- Private delegate/helper methods ----
|
||||
|
||||
private Paint_Delegate() {
|
||||
reset();
|
||||
|
||||
mTypeface = Typeface.sDefaults[0].native_instance;
|
||||
updateFontObject();
|
||||
}
|
||||
|
||||
private Paint_Delegate(Paint_Delegate paint) {
|
||||
set(paint);
|
||||
updateFontObject();
|
||||
}
|
||||
|
||||
private void set(Paint_Delegate paint) {
|
||||
mFlags = paint.mFlags;
|
||||
mColor = paint.mColor;
|
||||
mStyle = paint.mStyle;
|
||||
mCap = paint.mCap;
|
||||
mJoin = paint.mJoin;
|
||||
mAlign = paint.mAlign;
|
||||
mTypeface = paint.mTypeface;
|
||||
mStrokeWidth = paint.mStrokeWidth;
|
||||
mStrokeMiter = paint.mStrokeMiter;
|
||||
mTextSize = paint.mTextSize;
|
||||
mTextScaleX = paint.mTextScaleX;
|
||||
mTextSkewX = paint.mTextSkewX;
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
mFlags = Paint.DEFAULT_PAINT_FLAGS;
|
||||
mColor = 0;
|
||||
mStyle = 0;
|
||||
mCap = 0;
|
||||
mJoin = 0;
|
||||
mAlign = 0;
|
||||
mTypeface = 0;
|
||||
mStrokeWidth = 1.f;
|
||||
mStrokeMiter = 2.f;
|
||||
mTextSize = 20.f;
|
||||
mTextScaleX = 1.f;
|
||||
mTextSkewX = 0.f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the {@link Font} object from the typeface, text size and scaling
|
||||
*/
|
||||
private void updateFontObject() {
|
||||
if (mTypeface != 0) {
|
||||
// Get the fonts from the TypeFace object.
|
||||
List<Font> fonts = Typeface_Delegate.getFonts(mTypeface);
|
||||
|
||||
// create new font objects as well as FontMetrics, based on the current text size
|
||||
// and skew info.
|
||||
ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
|
||||
for (Font font : fonts) {
|
||||
FontInfo info = new FontInfo();
|
||||
info.mFont = font.deriveFont(mTextSize);
|
||||
if (mTextScaleX != 1.0 || mTextSkewX != 0) {
|
||||
// TODO: support skew
|
||||
info.mFont = info.mFont.deriveFont(new AffineTransform(
|
||||
mTextScaleX, mTextSkewX, 0, 0, 1, 0));
|
||||
}
|
||||
info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
|
||||
|
||||
infoList.add(info);
|
||||
}
|
||||
|
||||
mFonts = Collections.unmodifiableList(infoList);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
|
||||
// get the delegate from the native int.
|
||||
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (flagValue) {
|
||||
delegate.mFlags |= flagMask;
|
||||
} else {
|
||||
delegate.mFlags &= ~flagMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,11 @@ public final class Typeface_Delegate {
|
||||
}
|
||||
|
||||
public static List<Font> getFonts(Typeface typeface) {
|
||||
Typeface_Delegate delegate = sManager.getDelegate(typeface.native_instance);
|
||||
return getFonts(typeface.native_instance);
|
||||
}
|
||||
|
||||
public static List<Font> getFonts(int native_int) {
|
||||
Typeface_Delegate delegate = sManager.getDelegate(native_int);
|
||||
if (delegate == null) {
|
||||
assert false;
|
||||
return null;
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.android.layoutlib.api.IProjectCallback;
|
||||
import com.android.layoutlib.api.IResourceValue;
|
||||
import com.android.layoutlib.api.IStyleResourceValue;
|
||||
import com.android.layoutlib.api.IXmlPullParser;
|
||||
import com.android.layoutlib.api.IDensityBasedResourceValue.Density;
|
||||
import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo;
|
||||
import com.android.layoutlib.bridge.LayoutResult.LayoutViewInfo;
|
||||
import com.android.ninepatch.NinePatch;
|
||||
@@ -33,7 +34,9 @@ import com.android.tools.layoutlib.create.OverrideMethod;
|
||||
import android.content.ClipData;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Bitmap_Delegate;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Canvas_Delegate;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Region;
|
||||
import android.graphics.Typeface_Delegate;
|
||||
@@ -64,6 +67,7 @@ import android.widget.FrameLayout;
|
||||
import android.widget.TabHost;
|
||||
import android.widget.TabWidget;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -450,13 +454,28 @@ public final class Bridge implements ILayoutBridge {
|
||||
view.layout(0, screenOffset, screenWidth, screenHeight);
|
||||
|
||||
// draw the views
|
||||
Canvas canvas = new Canvas(screenWidth, screenHeight - screenOffset, logger);
|
||||
// create the BufferedImage into which the layout will be rendered.
|
||||
BufferedImage image = new BufferedImage(screenWidth, screenHeight - screenOffset,
|
||||
BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
// create an Android bitmap around the BufferedImage
|
||||
Bitmap bitmap = Bitmap_Delegate.createBitmap(image, Density.getEnum(density));
|
||||
|
||||
// create a Canvas around the Android bitmap
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
|
||||
// to set the logger, get the native delegate
|
||||
Canvas_Delegate canvasDelegate = Canvas_Delegate.getDelegate(canvas);
|
||||
canvasDelegate.setLogger(logger);
|
||||
|
||||
|
||||
root.draw(canvas);
|
||||
canvas.dispose();
|
||||
canvasDelegate.dispose();
|
||||
|
||||
return new LayoutResult(
|
||||
visit(((ViewGroup)view).getChildAt(0), context),
|
||||
image);
|
||||
|
||||
return new LayoutResult(visit(((ViewGroup)view).getChildAt(0), context),
|
||||
canvas.getImage());
|
||||
} catch (PostInflateException e) {
|
||||
return new LayoutResult(ILayoutResult.ERROR, "Error during post inflation process:\n"
|
||||
+ e.getMessage());
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.android.layoutlib.bridge;
|
||||
import com.android.ninepatch.NinePatch;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Canvas_Delegate;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
@@ -80,7 +81,8 @@ public class NinePatchDrawable extends Drawable {
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
Rect r = getBounds();
|
||||
m9Patch.draw(canvas.getGraphics2d(), r.left, r.top, r.width(), r.height());
|
||||
Canvas_Delegate canvasDelegate = Canvas_Delegate.getDelegate(canvas);
|
||||
m9Patch.draw(canvasDelegate.getGraphics2d(), r.left, r.top, r.width(), r.height());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package com.android.layoutlib.bridge;
|
||||
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics._Original_Paint;
|
||||
import android.text.TextPaint;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
@@ -58,14 +56,6 @@ public class AndroidGraphicsTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testPaint() {
|
||||
_Original_Paint o = new _Original_Paint();
|
||||
assertNotNull(o);
|
||||
|
||||
Paint p = new Paint();
|
||||
assertNotNull(p);
|
||||
}
|
||||
|
||||
public void textTextPaint() {
|
||||
TextPaint p = new TextPaint();
|
||||
assertNotNull(p);
|
||||
|
||||
@@ -87,7 +87,11 @@ public class TestNativeDelegate extends TestCase {
|
||||
|
||||
try {
|
||||
// try to load the method with the given parameter types.
|
||||
delegateClass.getDeclaredMethod(originalMethod.getName(), parameters);
|
||||
Method delegateMethod = delegateClass.getDeclaredMethod(originalMethod.getName(),
|
||||
parameters);
|
||||
|
||||
// check that the method is static
|
||||
assertTrue((delegateMethod.getModifiers() & Modifier.STATIC) == Modifier.STATIC);
|
||||
} catch (NoSuchMethodException e) {
|
||||
// compute a full class name that's long but not too long.
|
||||
StringBuilder sb = new StringBuilder(originalMethod.getName() + "(");
|
||||
|
||||
@@ -104,7 +104,9 @@ public final class CreateInfo implements ICreateInfo {
|
||||
*/
|
||||
private final static String[] DELEGATE_CLASS_NATIVES = new String[] {
|
||||
"android.graphics.Bitmap",
|
||||
"android.graphics.Canvas",
|
||||
"android.graphics.Matrix",
|
||||
"android.graphics.Paint",
|
||||
"android.graphics.Typeface",
|
||||
};
|
||||
|
||||
@@ -126,11 +128,9 @@ public final class CreateInfo implements ICreateInfo {
|
||||
new String[] {
|
||||
"android.graphics.BitmapFactory", "android.graphics._Original_BitmapFactory",
|
||||
"android.graphics.BitmapShader", "android.graphics._Original_BitmapShader",
|
||||
"android.graphics.Canvas", "android.graphics._Original_Canvas",
|
||||
"android.graphics.ComposeShader", "android.graphics._Original_ComposeShader",
|
||||
"android.graphics.DashPathEffect", "android.graphics._Original_DashPathEffect",
|
||||
"android.graphics.LinearGradient", "android.graphics._Original_LinearGradient",
|
||||
"android.graphics.Paint", "android.graphics._Original_Paint",
|
||||
"android.graphics.Path", "android.graphics._Original_Path",
|
||||
"android.graphics.PorterDuffXfermode", "android.graphics._Original_PorterDuffXfermode",
|
||||
"android.graphics.RadialGradient", "android.graphics._Original_RadialGradient",
|
||||
@@ -150,13 +150,6 @@ public final class CreateInfo implements ICreateInfo {
|
||||
*/
|
||||
private final static String[] DELETE_RETURNS =
|
||||
new String[] {
|
||||
"android.graphics.Paint", // class to delete methods from
|
||||
"android.graphics.Paint$Align", // list of type identifying methods to delete
|
||||
"android.graphics.Paint$Style",
|
||||
"android.graphics.Paint$Join",
|
||||
"android.graphics.Paint$Cap",
|
||||
"android.graphics.Paint$FontMetrics",
|
||||
"android.graphics.Paint$FontMetricsInt",
|
||||
null }; // separator, for next class/methods list.
|
||||
}
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ class DelegateMethodAdapter implements MethodVisitor {
|
||||
// Construct the descriptor of the delegate. For a static method, it's the same
|
||||
// however for an instance method we need to pass the 'this' reference first
|
||||
String desc = mDesc;
|
||||
if (!mIsStatic && argTypes.length > 0) {
|
||||
if (!mIsStatic) {
|
||||
Type[] argTypes2 = new Type[argTypes.length + 1];
|
||||
|
||||
argTypes2[0] = Type.getObjectType(mClassName);
|
||||
|
||||
Reference in New Issue
Block a user