+ /** draw the picture at (dx1, dy1), cropping to (cx1, cy1, cx2, cy2) */
+ protected abstract void drawPicture(Picture source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2);
+
+ /** fill a trapezoid whose top and bottom edges are horizontal */
+ public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
+
+ /**
+ * Same as drawPicture, but only uses the alpha channel of the Picture, and is allowed to destructively modify the RGB
+ * channels of the Picture in the process. This method may assume that the RGB channels of the image are all zero IFF it
+ * restores this invariant before returning.
+ */
+ public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb);
+
+ // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
+ /** draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i> */
+ public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
+
+ if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
+
+ if (x1 == x2) {
+ fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
+ x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
+ return;
+ }
+
+ // fastpath for single-pixel width lines
+ if (w == 1) {
+ float slope = (float)(y2 - y1) / (float)(x2 - x1);
+ int last_x = x1;
+ for(int y=y1; y<=y2; y++) {
+ int new_x = (int)((float)(y - y1) / slope) + x1;
+ if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
+ last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
+ else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
+ y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
+ last_x = new_x;
+ }
+ return;
+ }
+
+ // actually half-width
+ float width = (float)w / 2;
+ float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
+ if (phi < 0.0) phi += (float)Math.PI * 2;
+ float theta = (float)Math.PI / 2 - phi;
+
+ // dx and dy are the x and y distance between each endpoint and the corner of the stroke
+ int dx = (int)(width * Math.cos(theta));
+ int dy = (int)(width * Math.sin(theta));