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;
6 import org.ibex.util.*;
10 * A block of pixels which can be drawn on.
14 * Implementations of the Platform class should return objects
15 * supporting this interface from the _createPixelBuffer()
16 * method. These implementations may choose to use off-screen video
17 * ram for this purpose (for example, a Pixmap on X11).
20 public interface PixelBuffer {
21 public abstract void drawLine(int x1, int y1, int x2, int y2, int color);
22 public abstract void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color);
24 public abstract void drawPicture(Picture p, Affine a, Mesh h);
25 public abstract void drawGlyph(Font.Glyph source, Affine a, Mesh h, int rgb, int bg);
27 //public abstract void stroke(Mesh p, int color);
28 //public abstract void fill(Mesh p, Paint paint);
35 // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
36 // draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i>
37 public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
39 if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
42 fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
43 x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
47 // fastpath for single-pixel width lines
49 float slope = (float)(y2 - y1) / (float)(x2 - x1);
51 for(int y=y1; y<=y2; y++) {
52 int new_x = (int)((float)(y - y1) / slope) + x1;
53 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
54 last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
55 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
56 y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
62 // actually half-width
63 float width = (float)w / 2;
64 float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
65 if (phi < 0.0) phi += (float)Math.PI * 2;
66 float theta = (float)Math.PI / 2 - phi;
68 // dx and dy are the x and y distance between each endpoint and the corner of the stroke
69 int dx = (int)(width * Math.cos(theta));
70 int dy = (int)(width * Math.sin(theta));
72 // slice is the longest possible length of a horizontal line across the stroke
73 int slice = (int)(2 * width / Math.cos(theta));
76 x1 -= width * Math.cos(phi);
77 x2 += width * Math.cos(phi);
78 y1 -= width * Math.sin(phi);
79 y2 += width * Math.sin(phi);
82 fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color); // top corner
83 fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color); // bottom corner
84 fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color); // middle