+ protected abstract void _fillRect(int x1, int y1, int x2, int y2, int color);
+
+ /** Fill in the trapezoid defined by (x1, y1), (x2, y1), (x3, y2), (x4, y2); leftSlope and rightSlope are provided for convenience */
+ protected void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, double leftSlope, double rightSlope, int color) {
+ for(int y=y1; y<y2; y++) {
+ int _x1 = (int)Math.floor((y - y1) * leftSlope + x1);
+ int _y1 = (int)Math.floor(y);
+ int _x2 = (int)Math.ceil((y - y1) * rightSlope + x2);
+ int _y2 = (int)Math.floor(y) + 1;
+ if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
+ fillRect(_x1, _y1, _x2, _y2, color);
+ }
+ }
+
+
+ // Internal Stuff //////////////////////////////////////////////////////////////////////////////
+
+ protected int width, height;
+ private int clipx = 0, clipy = 0, clipw, cliph;
+ private DoubleBuffer() { }
+ protected DoubleBuffer(int width, int height) { this.width = width; this.height = height; clipw = width; cliph = height; }
+ protected SVG.Affine ctm = null;
+
+
+ // Final methods //////////////////////////////////////////////////////////////////////////////
+
+ public final int getHeight() { return width; }
+ public final int getWidth() { return height; }
+ public final void setTransform(SVG.Affine ctm) { this.ctm = ctm; }
+ public final void setClip(int x, int y, int x2, int y2) { clipx = x; clipy = y; clipw = x2 - x; cliph = y2 - y; }
+ public final void drawPicture(Picture p, int x, int y) { drawPicture(p, x, y, x + p.getWidth(), y + p.getHeight(), 0, 0, p.getWidth(), p.getHeight()); }
+
+ public final void drawPoint(int x, int y, int color) {
+ if (x > clipx && x < clipx + clipw && y > clipy && y < clipy + cliph)
+ _drawPoint(x, y, color);
+ }
+
+ public final void drawLine(int x1, int y1, int x2, int y2, int width, int color) {
+
+ if (x1 == x2 || y1 == y2) return;
+
+ // FIXME: don't compute slope if not needed
+ double slope = (y2 - y1) / (x2 - x1);
+ if (x1 < clipx) { y1 = (int)((clipx - x1) * slope + y1); x1 = clipx; }
+ if (y1 < clipy) { x1 = (int)((clipy - y1) / slope + x1); y1 = clipy; }
+ if (x2 > clipx + clipw) { x2 = clipx + clipw; y2 = (int)((x2 - x1) / slope + y1); }
+ if (y2 > clipy + cliph) { y2 = clipy + cliph; x2 = (int)((y2 - y1) * slope + y1); }
+
+ _drawLine(x1, y1, x2, y2, width, color);
+ }
+
+ /** Stretch the picture to fill (dx1, dy1, dx2, dy2) and blit it, clipping to (cx1, cy1, cx2, cy2) */
+ public final void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int cx1, int cy1, int cx2, int cy2) {
+
+ if (org.xwt.util.Log.assertionsEnabled) {
+ if (dx1 >= dx2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): dx1 >= dx2: dx1=" + dx1 + " dx2=" + dx2, true);
+ if (dy1 >= dy2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): dy1 >= dy2: dy1=" + dy1 + " dy2=" + dy2, true);
+ if (cx1 >= cx2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): cx1 >= cx2: cx1=" + cx1 + " cx2=" + cx2, true);
+ if (cy1 >= cy2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): cy1 >= cy2: cy1=" + cy1 + " cy2=" + cy2, true);
+ }
+
+ // shrink clipping rectangle to be no bigger than drawing rectangle
+ if (cx1 < dx1) cx1 = dx1;
+ if (cy1 < dy1) cy1 = dy1;
+ if (cx2 > dx2) cx2 = dx2;
+ if (cy2 > dy2) cy2 = dy2;
+
+ // intersect the two clipping regions
+ if (cx2 < clipx) return;
+ if (cy2 < clipy) return;
+ if (cx1 > clipx + clipw) return;
+ if (cy1 > clipy + cliph) return;
+ if (cx1 < clipx) cx1 = clipx;
+ if (cy1 < clipy) cy1 = clipy;
+ if (cx2 > clipx + clipw) cx2 = clipx + clipw;
+ if (cy2 > clipy + cliph) cy2 = clipy + cliph;
+
+ if (ctm == null) {
+ _drawPicture(source, dx1, dy1, dx2, dy2, cx1, cy1, cx2, cy2);
+ } else {
+ _drawPicture(source,
+ (int)ctm.multiply_px(dx1, dy1), (int)ctm.multiply_py(dx1, dy1),
+ (int)ctm.multiply_px(dx2, dy2), (int)ctm.multiply_py(dx2, dy2),
+ (int)ctm.multiply_px(cx1, cy1), (int)ctm.multiply_py(cx1, cy1),
+ (int)ctm.multiply_px(cx2, cy2), (int)ctm.multiply_py(cx2, cy2));
+ }
+ }
+
+ public final void drawString(String font, String text, int x, int y, int color) {
+ // FIXME: clipping
+ if (ctm == null) {
+ _drawString(font, text, x, y, color);
+ } else {
+ _drawString(font, text, (int)ctm.multiply_px(x, y), (int)ctm.multiply_py(x, y), color);
+ }
+ }
+
+ public final void fillRect(int x1, int y1, int x2, int y2, int color) {
+
+ x1 = Math.max(Math.min(clipx + clipw, x1), clipx);
+ x2 = Math.max(Math.min(clipx + clipw, x2), clipx);
+ y1 = Math.max(Math.min(clipy + cliph, y1), clipy);
+ y2 = Math.max(Math.min(clipy + cliph, y2), clipy);
+
+ if (ctm == null) {
+ _fillRect(x1, y1, x2, y2, color);
+ } else {
+ _fillRect((int)ctm.multiply_px(x1, y1), (int)ctm.multiply_py(x1, y1),
+ (int)ctm.multiply_px(x2, y2), (int)ctm.multiply_py(x2, y2),
+ color);
+ }
+ }
+
+ public final void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
+ fillTrapezoid(x1, x2, y1, x3, x4, y2, (double)(x3 - x1) / (double)(y2 - y1), (double)(x4 - x2) / (double)(y2 - y1), color);
+ }
+
+ private final void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, double slope1, double slope2, int color) {
+
+ // invariant: x1 <= x2, x3 <= x4, y1 <= y2
+ if (y1 == y2) return;
+ if (org.xwt.util.Log.assertionsEnabled) {
+ if (x1 > x2) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): x1 > x2: x1=" + x1 + " x2=" + x2, true);
+ if (x3 > x4) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): x3 > x4: x3=" + x3 + " x4=" + x4, true);
+ if (y1 >= y2) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): y1 >= y2: y1=" + y1 + " y2=" + y2, true);
+ }
+
+ // clip top and bottom edges
+ if (y1 > clipy + cliph) return;
+ if (y2 < clipy) return;
+ if (y1 < clipy) { x1 = (int)(y1 + slope1 * clipy); x2 = (int)(y1 + slope2 * clipy); y1 = clipy; }
+ if (y2 > clipy + cliph) { y2 = clipy + cliph; x3 = (int)(y1 + slope1 * y2); x4 = (int)(slope2 * y2); }
+
+ if (x1 < clipx && x3 < clipx) {
+ x1 = x3 = clipx;
+ } else if (!(x1 >= clipx && x3 >= clipx)) {
+ int y = (int)((clipx - x1) / slope1 + y1);
+ int x = (int)(slope2 * (y - y2) + x2);
+ // we have to recurse here because we don't know which subtrapezoid might get split on the other side
+ fillTrapezoid(x1, x2, y1, clipx, x, y, slope1, slope2, color);
+ fillTrapezoid(clipx, x, y, x3, x4, y2, slope1, slope2, color);
+ return;
+ }
+
+ if (x2 >= clipx + clipw && x4 >= clipx + clipw) {
+ x2 = x4 = clipx + clipw;
+ } else if (!(x2 < clipx + clipw && x4 < clipx + clipw)) {
+ int y = (int)((clipx + clipw - x3) / slope2 + y1);
+ int x = (int)(slope1 * (y - y1) + x1);
+ _fillTrapezoid(x1, x2, y1, x, clipx + clipw, y, slope1, slope2, color);
+ _fillTrapezoid(x, clipx + clipw, y, x3, x4, y2, slope1, slope2, color);
+ return;
+ }