23
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 *******************************************************************************/
|
|
11 module dwt.graphics.Transform;
|
|
12
|
|
13 import dwt.SWT;
|
|
14 import dwt.internal.Compatibility;
|
|
15 import dwt.internal.cairo.Cairo;
|
|
16 import dwt.graphics.Resource;
|
|
17 import dwt.graphics.Device;
|
|
18
|
|
19 import tango.text.convert.Format;
|
|
20
|
|
21 /**
|
|
22 * Instances of this class represent transformation matrices for
|
|
23 * points expressed as (x, y) pairs of floating point numbers.
|
|
24 * <p>
|
|
25 * Application code must explicitly invoke the <code>Transform.dispose()</code>
|
|
26 * method to release the operating system resources managed by each instance
|
|
27 * when those instances are no longer required.
|
|
28 * </p>
|
|
29 * <p>
|
|
30 * This class requires the operating system's advanced graphics subsystem
|
|
31 * which may not be available on some platforms.
|
|
32 * </p>
|
|
33 *
|
|
34 * @since 3.1
|
|
35 */
|
|
36 public class Transform : Resource {
|
|
37 /**
|
|
38 * the OS resource for the Transform
|
|
39 * (Warning: This field is platform dependent)
|
|
40 * <p>
|
|
41 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
42 * public API. It is marked public only so that it can be shared
|
|
43 * within the packages provided by SWT. It is not available on all
|
|
44 * platforms and should never be accessed from application code.
|
|
45 * </p>
|
|
46 */
|
|
47 public double[] handle;
|
|
48
|
|
49 /**
|
|
50 * Constructs a new identity Transform.
|
|
51 * <p>
|
|
52 * This operation requires the operating system's advanced
|
|
53 * graphics subsystem which may not be available on some
|
|
54 * platforms.
|
|
55 * </p>
|
|
56 *
|
|
57 * @param device the device on which to allocate the Transform
|
|
58 *
|
|
59 * @exception IllegalArgumentException <ul>
|
|
60 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
61 * </ul>
|
|
62 * @exception SWTException <ul>
|
|
63 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
64 * </ul>
|
|
65 * @exception SWTError <ul>
|
|
66 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
|
|
67 * </ul>
|
|
68 *
|
|
69 * @see #dispose()
|
|
70 */
|
|
71 public this (Device device) {
|
|
72 this(device, 1, 0, 0, 1, 0, 0);
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Constructs a new Transform given an array of elements that represent the
|
|
77 * matrix that describes the transformation.
|
|
78 * <p>
|
|
79 * This operation requires the operating system's advanced
|
|
80 * graphics subsystem which may not be available on some
|
|
81 * platforms.
|
|
82 * </p>
|
|
83 *
|
|
84 * @param device the device on which to allocate the Transform
|
|
85 * @param elements an array of floats that describe the transformation matrix
|
|
86 *
|
|
87 * @exception IllegalArgumentException <ul>
|
|
88 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device, or the elements array is null</li>
|
|
89 * <li>ERROR_INVALID_ARGUMENT - if the elements array is too small to hold the matrix values</li>
|
|
90 * </ul>
|
|
91 * @exception SWTException <ul>
|
|
92 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
93 * </ul>
|
|
94 * @exception SWTError <ul>
|
|
95 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
|
|
96 * </ul>
|
|
97 *
|
|
98 * @see #dispose()
|
|
99 */
|
|
100 public this(Device device, float[] elements) {
|
|
101 this (device, checkTransform(elements)[0], elements[1], elements[2], elements[3], elements[4], elements[5]);
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Constructs a new Transform given all of the elements that represent the
|
|
106 * matrix that describes the transformation.
|
|
107 * <p>
|
|
108 * This operation requires the operating system's advanced
|
|
109 * graphics subsystem which may not be available on some
|
|
110 * platforms.
|
|
111 * </p>
|
|
112 *
|
|
113 * @param device the device on which to allocate the Transform
|
|
114 * @param m11 the first element of the first row of the matrix
|
|
115 * @param m12 the second element of the first row of the matrix
|
|
116 * @param m21 the first element of the second row of the matrix
|
|
117 * @param m22 the second element of the second row of the matrix
|
|
118 * @param dx the third element of the first row of the matrix
|
|
119 * @param dy the third element of the second row of the matrix
|
|
120 *
|
|
121 * @exception IllegalArgumentException <ul>
|
|
122 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
123 * </ul>
|
|
124 * @exception SWTException <ul>
|
|
125 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
126 * </ul>
|
|
127 * @exception SWTError <ul>
|
|
128 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
|
|
129 * </ul>
|
|
130 *
|
|
131 * @see #dispose()
|
|
132 */
|
|
133 public this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) {
|
|
134 if (device is null) device = Device.getDevice();
|
|
135 if (device is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
136 this.device = device;
|
|
137 device.checkCairo();
|
|
138 handle = new double[6];
|
|
139 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
140 Cairo.cairo_matrix_init( cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy);
|
|
141 if (device.tracking) device.new_Object(this);
|
|
142 }
|
|
143
|
|
144 static float[] checkTransform(float[] elements) {
|
|
145 if (elements == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
146 if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
147 return elements;
|
|
148 }
|
|
149
|
|
150 /**
|
|
151 * Disposes of the operating system resources associated with
|
|
152 * the Transform. Applications must dispose of all Transforms that
|
|
153 * they allocate.
|
|
154 */
|
|
155 public void dispose() {
|
|
156 if (handle is null) return;
|
|
157 if (device.isDisposed()) return;
|
|
158 handle = null;
|
|
159 if (device.tracking) device.dispose_Object(this);
|
|
160 device = null;
|
|
161 }
|
|
162
|
|
163 /**
|
|
164 * Fills the parameter with the values of the transformation matrix
|
|
165 * that the receiver represents, in the order {m11, m12, m21, m22, dx, dy}.
|
|
166 *
|
|
167 * @param elements array to hold the matrix values
|
|
168 *
|
|
169 * @exception SWTException <ul>
|
|
170 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
171 * </ul>
|
|
172 * @exception IllegalArgumentException <ul>
|
|
173 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
|
|
174 * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the matrix values</li>
|
|
175 * </ul>
|
|
176 */
|
|
177 public void getElements(float[] elements) {
|
|
178 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
179 if (elements is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
180 if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
181 elements[0] = cast(float)handle[0];
|
|
182 elements[1] = cast(float)handle[1];
|
|
183 elements[2] = cast(float)handle[2];
|
|
184 elements[3] = cast(float)handle[3];
|
|
185 elements[4] = cast(float)handle[4];
|
|
186 elements[5] = cast(float)handle[5];
|
|
187 }
|
|
188
|
|
189 /**
|
|
190 * Modifies the receiver such that the matrix it represents becomes the
|
|
191 * the mathematical inverse of the matrix it previously represented.
|
|
192 *
|
|
193 * @exception SWTException <ul>
|
|
194 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
195 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li>
|
|
196 * </ul>
|
|
197 */
|
|
198 public void invert() {
|
|
199 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
200 if (Cairo.cairo_matrix_invert(cast(cairo_matrix_t*)handle.ptr) != 0) {
|
|
201 SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
|
|
202 }
|
|
203 }
|
|
204
|
|
205 /**
|
|
206 * Returns <code>true</code> if the Transform has been disposed,
|
|
207 * and <code>false</code> otherwise.
|
|
208 * <p>
|
|
209 * This method gets the dispose state for the Transform.
|
|
210 * When a Transform has been disposed, it is an error to
|
|
211 * invoke any other method using the Transform.
|
|
212 *
|
|
213 * @return <code>true</code> when the Transform is disposed, and <code>false</code> otherwise
|
|
214 */
|
|
215 public bool isDisposed() {
|
|
216 return handle is null;
|
|
217 }
|
|
218
|
|
219 /**
|
|
220 * Returns <code>true</code> if the Transform represents the identity matrix
|
|
221 * and false otherwise.
|
|
222 *
|
|
223 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise
|
|
224 */
|
|
225 public bool isIdentity() {
|
|
226 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
227 float[] m = new float[6];
|
|
228 getElements(m);
|
|
229 return m[0] == 1 && m[1] == 0 && m[2] == 0 && m[3] == 1 && m[4] == 0 && m[5] == 0;
|
|
230 }
|
|
231
|
|
232 /**
|
|
233 * Modifies the receiver such that the matrix it represents becomes the
|
|
234 * the result of multiplying the matrix it previously represented by the
|
|
235 * argument.
|
|
236 *
|
|
237 * @param matrix the matrix to multiply the receiver by
|
|
238 *
|
|
239 * @exception SWTException <ul>
|
|
240 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
241 * </ul>
|
|
242 * @exception IllegalArgumentException <ul>
|
|
243 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
|
|
244 * <li>ERROR_INVALID_ARGUMENT - if the parameter has been disposed</li>
|
|
245 * </ul>
|
|
246 */
|
|
247 public void multiply(Transform matrix) {
|
|
248 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
249 if (matrix == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
250 if (matrix.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
251 Cairo.cairo_matrix_multiply(cast(cairo_matrix_t*)handle.ptr,cast(cairo_matrix_t*)matrix.handle.ptr, cast(cairo_matrix_t*)handle.ptr);
|
|
252 }
|
|
253
|
|
254 /**
|
|
255 * Modifies the receiver so that it represents a transformation that is
|
|
256 * equivalent to its previous transformation rotated by the specified angle.
|
|
257 * The angle is specified in degrees and for the identity transform 0 degrees
|
|
258 * is at the 3 o'clock position. A positive value indicates a clockwise rotation
|
|
259 * while a negative value indicates a counter-clockwise rotation.
|
|
260 *
|
|
261 * @param angle the angle to rotate the transformation by
|
|
262 *
|
|
263 * @exception SWTException <ul>
|
|
264 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
265 * </ul>
|
|
266 */
|
|
267 public void rotate(float angle) {
|
|
268 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
269 Cairo.cairo_matrix_rotate(cast(cairo_matrix_t*)handle.ptr, angle * cast(float)Compatibility.PI / 180);
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Modifies the receiver so that it represents a transformation that is
|
|
274 * equivalent to its previous transformation scaled by (scaleX, scaleY).
|
|
275 *
|
|
276 * @param scaleX the amount to scale in the X direction
|
|
277 * @param scaleY the amount to scale in the Y direction
|
|
278 *
|
|
279 * @exception SWTException <ul>
|
|
280 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
281 * </ul>
|
|
282 */
|
|
283 public void scale(float scaleX, float scaleY) {
|
|
284 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
285 Cairo.cairo_matrix_scale(cast(cairo_matrix_t*)handle.ptr, scaleX, scaleY);
|
|
286 }
|
|
287
|
|
288 /**
|
|
289 * Modifies the receiver to represent a new transformation given all of
|
|
290 * the elements that represent the matrix that describes that transformation.
|
|
291 *
|
|
292 * @param m11 the first element of the first row of the matrix
|
|
293 * @param m12 the second element of the first row of the matrix
|
|
294 * @param m21 the first element of the second row of the matrix
|
|
295 * @param m22 the second element of the second row of the matrix
|
|
296 * @param dx the third element of the first row of the matrix
|
|
297 * @param dy the third element of the second row of the matrix
|
|
298 *
|
|
299 * @exception SWTException <ul>
|
|
300 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
301 * </ul>
|
|
302 */
|
|
303 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) {
|
|
304 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
305 Cairo.cairo_matrix_init(cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy);
|
|
306 }
|
|
307
|
|
308 /**
|
|
309 * Given an array containing points described by alternating x and y values,
|
|
310 * modify that array such that each point has been replaced with the result of
|
|
311 * applying the transformation represented by the receiver to that point.
|
|
312 *
|
|
313 * @param pointArray an array of alternating x and y values to be transformed
|
|
314 *
|
|
315 * @exception IllegalArgumentException <ul>
|
|
316 * <li>ERROR_NULL_ARGUMENT - if the point array is null</li>
|
|
317 * </ul>
|
|
318 * @exception SWTException <ul>
|
|
319 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
320 * </ul>
|
|
321 */
|
|
322 public void transform(float[] pointArray) {
|
|
323 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
324 if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
325 double dx, dy;
|
|
326 int length = pointArray.length / 2;
|
|
327 for (int i = 0, j = 0; i < length; i++, j += 2) {
|
|
328 dx = pointArray[j];
|
|
329 dy = pointArray[j + 1];
|
|
330 Cairo.cairo_matrix_transform_point(cast(cairo_matrix_t*)handle.ptr, &dx, &dy);
|
|
331 pointArray[j] = cast(float)dx;
|
|
332 pointArray[j + 1] = cast(float)dy;
|
|
333 }
|
|
334 }
|
|
335
|
|
336 /**
|
|
337 * Modifies the receiver so that it represents a transformation that is
|
|
338 * equivalent to its previous transformation translated by (offsetX, offsetY).
|
|
339 *
|
|
340 * @param offsetX the distance to translate in the X direction
|
|
341 * @param offsetY the distance to translate in the Y direction
|
|
342 *
|
|
343 * @exception SWTException <ul>
|
|
344 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
345 * </ul>
|
|
346 */
|
|
347 public void translate(float offsetX, float offsetY) {
|
|
348 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
349 Cairo.cairo_matrix_translate(cast(cairo_matrix_t*)handle.ptr, offsetX, offsetY);
|
|
350 }
|
|
351
|
|
352 /**
|
|
353 * Returns a string containing a concise, human-readable
|
|
354 * description of the receiver.
|
|
355 *
|
|
356 * @return a string representation of the receiver
|
|
357 */
|
|
358 public char[] toString() {
|
|
359 if (isDisposed()) return "Transform {*DISPOSED*}";
|
|
360 float[] elements = new float[6];
|
|
361 getElements(elements);
|
|
362 return Format( "Transform {{{}}", elements );
|
|
363 }
|
|
364
|
|
365 }
|