1
2 package org.freehep.graphicsio.swf;
3
4 import java.awt.Color;
5 import java.awt.geom.AffineTransform;
6 import java.io.IOException;
7
8
9
10
11
12
13
14
15 public class FillStyle {
16
17 public static final int SOLID = 0x00;
18
19 public static final int LINEAR_GRADIENT = 0x10;
20
21 public static final int RADIAL_GRADIENT = 0x12;
22
23 public static final int FOCAL_GRADIENT = 0x13;
24
25 public static final int TILED_BITMAP = 0x40;
26
27 public static final int CLIPPED_BITMAP = 0x41;
28
29 public static final int TILED_BITMAP_NOT_SMOOTHED = 0x42;
30
31 public static final int CLIPPED_BITMAP_NOT_SMOOTHED = 0x43;
32
33 public static final int SPREAD_MODE_PAD = 0;
34
35 public static final int SPREAD_MODE_REFLECT = 1;
36
37 public static final int SPREAD_MODE_REPEAT = 2;
38
39 public static final int INTERPOLATION_MODE_NORMAL_RGB = 0;
40
41 public static final int INTERPOLATION_MODE_LINEAR_RGB = 1;
42
43 private int type;
44
45 private Color color, endColor;
46
47 private AffineTransform matrix, endMatrix;
48
49 private Gradient[] gradient;
50
51 private int spreadMode;
52
53 private int interpolationMode;
54
55 private float focalPoint;
56
57 private int bitmap;
58
59 public FillStyle(Color color) {
60 type = SOLID;
61 this.color = color;
62 }
63
64 public FillStyle(Color color, Color endColor) {
65 this(color);
66 this.endColor = endColor;
67 }
68
69 public FillStyle(Gradient[] gradient, boolean linear, AffineTransform matrix) {
70 this(gradient, LINEAR_GRADIENT, SPREAD_MODE_PAD, INTERPOLATION_MODE_NORMAL_RGB, 0, matrix);
71 }
72
73 public FillStyle(Gradient[] gradient, int gradientType, int spreadMode, int interpolationMode, float focalPoint,
74 AffineTransform matrix) {
75 this.type = gradientType;
76 this.gradient = gradient;
77 this.spreadMode = spreadMode;
78 this.interpolationMode = interpolationMode;
79 this.focalPoint = focalPoint;
80 this.matrix = matrix;
81 }
82
83 public FillStyle(Gradient[] gradient, boolean linear,
84 AffineTransform matrix, AffineTransform endMatrix) {
85 this(gradient, linear, matrix);
86 this.endMatrix = endMatrix;
87 }
88
89 public FillStyle(int bitmap, boolean tiled, AffineTransform matrix) {
90 type = (tiled) ? TILED_BITMAP : CLIPPED_BITMAP;
91 this.bitmap = bitmap;
92 this.matrix = matrix;
93 }
94
95 public FillStyle(int bitmap, boolean tiled, AffineTransform matrix,
96 AffineTransform endMatrix) {
97 this(bitmap, tiled, matrix);
98 this.endMatrix = endMatrix;
99 }
100
101 public FillStyle(SWFInputStream input, boolean isMorphStyle,
102 boolean hasAlpha) throws IOException {
103 type = input.readUnsignedByte();
104 switch (type) {
105 case SOLID:
106 color = input.readColor(hasAlpha);
107 if (isMorphStyle)
108 endColor = input.readColor(hasAlpha);
109 break;
110
111 case LINEAR_GRADIENT:
112 case RADIAL_GRADIENT:
113 case FOCAL_GRADIENT:
114 matrix = input.readMatrix();
115 if (isMorphStyle)
116 endMatrix = input.readMatrix();
117 input.byteAlign();
118 spreadMode = (int)input.readUBits(2);
119 interpolationMode = (int)input.readUBits(2);
120 int gradientCount = (int)input.readUBits(4);
121 gradient = new Gradient[gradientCount];
122 for (int i = 0; i < gradientCount; i++) {
123 gradient[i] = new Gradient(input, isMorphStyle, hasAlpha);
124 }
125 if (type == FOCAL_GRADIENT) {
126 focalPoint = input.readFixed8();
127 }
128 break;
129
130 case TILED_BITMAP:
131 case CLIPPED_BITMAP:
132 case TILED_BITMAP_NOT_SMOOTHED:
133 case CLIPPED_BITMAP_NOT_SMOOTHED:
134 bitmap = input.readUnsignedShort();
135 matrix = input.readMatrix();
136 if (isMorphStyle)
137 endMatrix = input.readMatrix();
138 break;
139
140 default:
141 System.err.println("FillStyle type " + type + " not implemented.");
142 break;
143 }
144 }
145
146 public int getType() {
147 return type;
148 }
149
150 public void write(SWFOutputStream swf, boolean isMorphStyle, boolean hasAlpha) throws IOException {
151
152 swf.writeUnsignedByte(type);
153
154 switch (type) {
155 case SOLID:
156 swf.writeColor(color, hasAlpha || (endColor != null));
157 if (endColor != null)
158 swf.writeColor(endColor, true);
159 break;
160
161 case LINEAR_GRADIENT:
162 case RADIAL_GRADIENT:
163 case FOCAL_GRADIENT:
164 swf.writeMatrix(matrix);
165 if (isMorphStyle)
166 swf.writeMatrix(endMatrix);
167 swf.byteAlign();
168 swf.writeUBits(spreadMode, 2);
169 swf.writeUBits(interpolationMode, 2);
170 swf.writeUBits(gradient.length, 4);
171 for (int i = 0; i < gradient.length; i++) {
172 gradient[i].write(swf, isMorphStyle, hasAlpha);
173 }
174 if (type == FOCAL_GRADIENT) {
175 swf.writeFixed8(focalPoint);
176 }
177 break;
178
179 case TILED_BITMAP:
180 case CLIPPED_BITMAP:
181 case TILED_BITMAP_NOT_SMOOTHED:
182 case CLIPPED_BITMAP_NOT_SMOOTHED:
183 swf.writeUnsignedShort(bitmap);
184 swf.writeMatrix(matrix);
185 if (isMorphStyle)
186 swf.writeMatrix(endMatrix);
187 break;
188
189 default:
190 System.err.println("FillStyle type " + type + " not implemented.");
191 break;
192 }
193 }
194
195 public String toString() {
196 StringBuffer s = new StringBuffer();
197 switch (type) {
198 case SOLID:
199 s.append("Solid: ");
200 s.append(color);
201 if (endColor != null)
202 s.append("; " + endColor);
203 break;
204 case LINEAR_GRADIENT:
205 case RADIAL_GRADIENT:
206 s.append("Gradient: ");
207 for (int i = 0; i < gradient.length; i++) {
208 s.append(gradient[i]);
209 s.append("; ");
210 }
211 s.append(matrix);
212 if (endMatrix != null)
213 s.append("; " + endMatrix);
214 break;
215 case TILED_BITMAP:
216 case CLIPPED_BITMAP:
217 s.append("BitMap: ");
218 s.append(bitmap);
219 s.append(", ");
220 s.append(matrix);
221 if (endMatrix != null)
222 s.append("; " + endMatrix);
223 break;
224 default:
225 s.append("Unknown Type:");
226 s.append(type);
227 break;
228 }
229 return s.toString();
230 }
231 }