1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.graphics;
9 * A block of pixels which can be drawn on.
13 * Implementations of the Platform class should return objects
14 * supporting this interface from the _createPixelBuffer()
15 * method. These implementations may choose to use off-screen video
16 * ram for this purpose (for example, a Pixmap on X11).
20 * Many of these functions come in pairs, one that uses ints and one
21 * that uses floats. The int functions are intended for situations
22 * in which the CTM is the identity transform.
25 public abstract class PixelBuffer {
27 /** draw the picture at (dx1, dy1), cropping to (cx1, cy1, cx2, cy2) */
28 public abstract void drawPicture(Picture source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2);
30 /** fill a trapezoid whose top and bottom edges are horizontal */
31 public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
34 * Same as drawPicture, but only uses the alpha channel of the Picture, and is allowed to destructively modify the RGB
35 * channels of the Picture in the process. This method may assume that the RGB channels of the image are all zero IFF it
36 * restores this invariant before returning.
38 public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb);
40 // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
41 /** draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i> */
42 public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
44 if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
47 fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
48 x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
52 // fastpath for single-pixel width lines
54 float slope = (float)(y2 - y1) / (float)(x2 - x1);
56 for(int y=y1; y<=y2; y++) {
57 int new_x = (int)((float)(y - y1) / slope) + x1;
58 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
59 last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
60 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
61 y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
67 // actually half-width
68 float width = (float)w / 2;
69 float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
70 if (phi < 0.0) phi += (float)Math.PI * 2;
71 float theta = (float)Math.PI / 2 - phi;
73 // dx and dy are the x and y distance between each endpoint and the corner of the stroke
74 int dx = (int)(width * Math.cos(theta));
75 int dy = (int)(width * Math.sin(theta));
77 // slice is the longest possible length of a horizontal line across the stroke
78 int slice = (int)(2 * width / Math.cos(theta));
81 x1 -= width * Math.cos(phi);
82 x2 += width * Math.cos(phi);
83 y1 -= width * Math.sin(phi);
84 y2 += width * Math.sin(phi);
87 fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color); // top corner
88 fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color); // bottom corner
89 fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color); // middle