diff options
author | Xavier Ducrohet <xav@android.com> | 2010-12-13 16:42:01 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2010-12-13 16:42:01 -0800 |
commit | 8da36314fd76ae6fe4549773ad00dc1883cb6bff (patch) | |
tree | 17aac3ac742f63f6e7eeca3eb8e6b0ab11d15057 /tools | |
parent | c0734f6c8d67deab00b10bd0bb75516cd1502dc0 (diff) | |
download | frameworks_base-8da36314fd76ae6fe4549773ad00dc1883cb6bff.zip frameworks_base-8da36314fd76ae6fe4549773ad00dc1883cb6bff.tar.gz frameworks_base-8da36314fd76ae6fe4549773ad00dc1883cb6bff.tar.bz2 |
LayoutLib: implement more of Canvas/Paint.
Change-Id: I8e2a7a3bddbce08db5eb641b5075bedd75f7be27
Diffstat (limited to 'tools')
-rw-r--r-- | tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java | 66 | ||||
-rw-r--r-- | tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java | 73 |
2 files changed, 103 insertions, 36 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java index b116d2b..f6d7b3a 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java @@ -170,7 +170,8 @@ public class Canvas_Delegate { } /*package*/ static boolean clipRect(Canvas thisCanvas, RectF rect) { - return clipRect(thisCanvas, rect.left, rect.top, rect.right, rect.bottom); + return clipRect(thisCanvas, + (int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom); } /*package*/ static boolean clipRect(Canvas thisCanvas, Rect rect) { @@ -179,16 +180,7 @@ public class Canvas_Delegate { /*package*/ static boolean clipRect(Canvas thisCanvas, float left, float top, float right, float bottom) { - // get the delegate from the native int. - Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); - if (canvasDelegate == null) { - assert false; - return false; - } - - canvasDelegate.getGraphics2d().clipRect((int)left, (int)top, (int)(right-left), - (int)(bottom-top)); - return true; + return clipRect(thisCanvas, (int) left, (int) top, (int) right, (int) bottom); } /*package*/ static boolean clipRect(Canvas thisCanvas, int left, int top, int right, @@ -200,8 +192,7 @@ public class Canvas_Delegate { return false; } - canvasDelegate.getGraphics2d().clipRect(left, top, right - left, bottom - top); - return true; + return canvasDelegate.clipRect(left, top, right, bottom, Region.Op.INTERSECT.nativeInt); } /*package*/ static int save(Canvas thisCanvas) { @@ -277,8 +268,31 @@ public class Canvas_Delegate { /*package*/ static void drawLines(Canvas thisCanvas, float[] pts, int offset, int count, Paint paint) { - // FIXME - throw new UnsupportedOperationException(); + // get the delegate from the native int. + Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); + if (canvasDelegate == null) { + assert false; + return; + } + + Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(paint.mNativePaint); + if (paintDelegate == null) { + assert false; + return; + } + + // get a Graphics2D object configured with the drawing parameters. + Graphics2D g = canvasDelegate.getCustomGraphics(paintDelegate); + + try { + for (int i = 0 ; i < count ; i += 4) { + g.drawLine((int)pts[i + offset], (int)pts[i + offset + 1], + (int)pts[i + offset + 2], (int)pts[i + offset + 3]); + } + } finally { + // dispose Graphics2D object + g.dispose(); + } } /*package*/ static void freeCaches() { @@ -410,8 +424,16 @@ public class Canvas_Delegate { float left, float top, float right, float bottom, int regionOp) { - // FIXME - throw new UnsupportedOperationException(); + + // get the delegate from the native int. + Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); + if (canvasDelegate == null) { + assert false; + } + + return canvasDelegate.clipRect( + (int) left, (int) top, (int) right, (int) bottom, + regionOp); } /*package*/ static boolean native_clipPath(int nativeCanvas, @@ -1001,6 +1023,16 @@ public class Canvas_Delegate { } } + private boolean clipRect(int left, int top, int right, int bottom, int regionOp) { + if (regionOp == Region.Op.INTERSECT.nativeInt) { + Graphics2D gc = getGraphics2d(); + gc.clipRect(left, top, right - left, bottom - top); + return gc.getClip().getBounds().isEmpty() == false; + } else { + throw new UnsupportedOperationException(); + } + } + private void setBitmap(BufferedImage image) { mBufferedImage = image; mGraphicsStack.push(mBufferedImage.createGraphics()); diff --git a/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java index c09f8ad..fa26bcf 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java @@ -390,16 +390,23 @@ public class Paint_Delegate { } /*package*/ static float ascent(Paint thisPaint) { - // FIXME - throw new UnsupportedOperationException(); - } + // get the delegate + Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); + if (delegate == null) { + assert false; + return 0; + } - /*package*/ static float descent(Paint thisPaint) { - // FIXME - throw new UnsupportedOperationException(); + if (delegate.mFonts.size() > 0) { + java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics; + // Android expects negative ascent so we invert the value from Java. + return - javaMetrics.getAscent(); + } + + return 0; } - /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) { + /*package*/ static float descent(Paint thisPaint) { // get the delegate Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); if (delegate == null) { @@ -409,19 +416,22 @@ public class Paint_Delegate { if (delegate.mFonts.size() > 0) { java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics; - if (metrics != null) { - // Android expects negative ascent so we invert the value from Java. - metrics.top = - javaMetrics.getMaxAscent(); - metrics.ascent = - javaMetrics.getAscent(); - metrics.descent = javaMetrics.getDescent(); - metrics.bottom = javaMetrics.getMaxDescent(); - metrics.leading = javaMetrics.getLeading(); - } - - return javaMetrics.getHeight(); + return javaMetrics.getDescent(); } return 0; + + } + + /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) { + // get the delegate + Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); + if (delegate == null) { + assert false; + return 0; + } + + return delegate.getFontMetrics(metrics); } /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) { @@ -698,8 +708,14 @@ public class Paint_Delegate { } /*package*/ static float native_getFontMetrics(int native_paint, FontMetrics metrics) { - // FIXME - throw new UnsupportedOperationException(); + // get the delegate from the native int. + Paint_Delegate delegate = sManager.getDelegate(native_paint); + if (delegate == null) { + assert false; + return 0.f; + } + + return delegate.getFontMetrics(metrics); } /*package*/ static int native_getTextWidths(int native_object, char[] text, int index, @@ -941,9 +957,28 @@ public class Paint_Delegate { } return 0; + } + private float getFontMetrics(FontMetrics metrics) { + if (mFonts.size() > 0) { + java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics; + if (metrics != null) { + // Android expects negative ascent so we invert the value from Java. + metrics.top = - javaMetrics.getMaxAscent(); + metrics.ascent = - javaMetrics.getAscent(); + metrics.descent = javaMetrics.getDescent(); + metrics.bottom = javaMetrics.getMaxDescent(); + metrics.leading = javaMetrics.getLeading(); + } + + return javaMetrics.getHeight(); + } + + return 0; } + + private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) { // get the delegate from the native int. Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |