Merge "LayoutLib: implement more of Canvas/Paint."

This commit is contained in:
Xavier Ducrohet
2010-12-13 18:24:11 -08:00
committed by Android (Google) Code Review
2 changed files with 106 additions and 39 deletions

View File

@@ -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());

View File

@@ -390,13 +390,37 @@ 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;
}
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 descent(Paint thisPaint) {
// FIXME
throw new UnsupportedOperationException();
// get the delegate
Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint);
if (delegate == null) {
assert false;
return 0;
}
if (delegate.mFonts.size() > 0) {
java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
return javaMetrics.getDescent();
}
return 0;
}
/*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
@@ -407,21 +431,7 @@ public class Paint_Delegate {
return 0;
}
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 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);