initial checkin
[org.ibex.nanogoat.git] / src / org / ibex / PixelBuffer.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex;
3
4 /**
5  *  <p>
6  *  A block of pixels which can be drawn on.
7  *  </p>
8  *
9  *  <p>
10  *  Implementations of the Platform class should return objects
11  *  supporting this interface from the _createPixelBuffer()
12  *  method. These implementations may choose to use off-screen video
13  *  ram for this purpose (for example, a Pixmap on X11).
14  *  </p>
15  *
16  *  <p>
17  *  Many of these functions come in pairs, one that uses ints and one
18  *  that uses floats.  The int functions are intended for situations
19  *  in which the CTM is the identity transform.
20  *  </p>
21  */
22 public abstract class PixelBuffer {
23
24     private final static boolean NANOGOAT_KEEP_ALL_METHODS = true;
25
26     /** draw the picture at (dx1, dy1), cropping to (cx1, cy1, cx2, cy2) */
27     protected abstract void drawPicture(Picture source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2);
28
29     /** fill a trapezoid whose top and bottom edges are horizontal */
30     public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
31
32     /**
33      *  Same as drawPicture, but only uses the alpha channel of the Picture, and is allowed to destructively modify the RGB
34      *  channels of the Picture in the process.  This method may assume that the RGB channels of the image are all zero IFF it
35      *  restores this invariant before returning.
36      */
37     public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb);
38
39     // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
40     /** draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i> */
41     public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
42
43         if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
44
45         if (x1 == x2) {
46             fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
47                           x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
48             return;
49         }
50
51         // fastpath for single-pixel width lines
52         if (w == 1) {
53             float slope = (float)(y2 - y1) / (float)(x2 - x1);
54             int last_x = x1;
55             for(int y=y1; y<=y2; y++) {
56                 int new_x = (int)((float)(y - y1) / slope) + x1;
57                 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
58                                               last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
59                 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
60                                    y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
61                 last_x = new_x;
62             }
63             return;
64         }
65
66         // actually half-width
67         float width = (float)w / 2;
68         float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
69         if (phi < 0.0) phi += (float)Math.PI * 2;
70         float theta = (float)Math.PI / 2 - phi;
71
72         // dx and dy are the x and y distance between each endpoint and the corner of the stroke
73         int dx = (int)(width * Math.cos(theta));
74         int dy = (int)(width * Math.sin(theta));
75
76         // slice is the longest possible length of a horizontal line across the stroke
77         int slice = (int)(2 * width / Math.cos(theta));
78
79         if (capped) {
80             x1 -= width * Math.cos(phi);
81             x2 += width * Math.cos(phi);
82             y1 -= width * Math.sin(phi);
83             y2 += width * Math.sin(phi);
84         }
85
86         fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color);           // top corner
87         fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color);           // bottom corner
88         fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color);   // middle
89     }
90
91 }