25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.graphics.Path;
|
|
14
|
|
15 import org.eclipse.swt.SWT;
|
|
16 import org.eclipse.swt.internal.Compatibility;
|
|
17 import org.eclipse.swt.internal.cairo.Cairo;
|
|
18 import org.eclipse.swt.graphics.Resource;
|
|
19 import org.eclipse.swt.graphics.Device;
|
|
20 import org.eclipse.swt.graphics.Font;
|
|
21 import org.eclipse.swt.graphics.GC;
|
|
22 import org.eclipse.swt.graphics.GCData;
|
|
23 import org.eclipse.swt.graphics.PathData;
|
|
24 import java.lang.all;
|
|
25
|
26
|
26 version(Tango){
|
|
27 import tango.stdc.string;
|
|
28 } else {
|
|
29 import std.c.string;
|
|
30 }
|
25
|
31
|
|
32 /**
|
|
33 * Instances of this class represent paths through the two-dimensional
|
|
34 * coordinate system. Paths do not have to be continuous, and can be
|
|
35 * described using lines, rectangles, arcs, cubic or quadratic bezier curves,
|
|
36 * glyphs, or other paths.
|
|
37 * <p>
|
|
38 * Application code must explicitly invoke the <code>Path.dispose()</code>
|
|
39 * method to release the operating system resources managed by each instance
|
|
40 * when those instances are no longer required.
|
|
41 * </p>
|
|
42 * <p>
|
|
43 * This class requires the operating system's advanced graphics subsystem
|
|
44 * which may not be available on some platforms.
|
|
45 * </p>
|
|
46 *
|
|
47 * @see <a href="http://www.eclipse.org/swt/snippets/#path">Path, Pattern snippets</a>
|
|
48 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: GraphicsExample</a>
|
|
49 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
50 *
|
|
51 * @since 3.1
|
|
52 */
|
|
53 public class Path : Resource {
|
|
54 alias Resource.init_ init_;
|
|
55 /**
|
|
56 * the OS resource for the Path
|
|
57 * (Warning: This field is platform dependent)
|
|
58 * <p>
|
|
59 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
60 * public API. It is marked public only so that it can be shared
|
|
61 * within the packages provided by SWT. It is not available on all
|
|
62 * platforms and should never be accessed from application code.
|
|
63 * </p>
|
|
64 */
|
|
65 public cairo_t* handle;
|
|
66
|
|
67 bool moved, closed = true;
|
|
68
|
|
69 /**
|
|
70 * Constructs a new empty Path.
|
|
71 * <p>
|
|
72 * This operation requires the operating system's advanced
|
|
73 * graphics subsystem which may not be available on some
|
|
74 * platforms.
|
|
75 * </p>
|
|
76 *
|
|
77 * @param device the device on which to allocate the path
|
|
78 *
|
|
79 * @exception IllegalArgumentException <ul>
|
|
80 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
|
|
81 * </ul>
|
|
82 * @exception SWTException <ul>
|
|
83 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
84 * </ul>
|
|
85 * @exception SWTError <ul>
|
|
86 * <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
|
|
87 * </ul>
|
|
88 *
|
|
89 * @see #dispose()
|
|
90 */
|
|
91 public this (Device device) {
|
|
92 super(device);
|
|
93 this.device.checkCairo();
|
|
94 auto surface = Cairo.cairo_image_surface_create(Cairo.CAIRO_FORMAT_ARGB32, 1, 1);
|
|
95 if (surface is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
96 handle = Cairo.cairo_create(surface);
|
|
97 Cairo.cairo_surface_destroy(surface);
|
|
98 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
99 init_();
|
|
100 }
|
|
101
|
|
102 /**
|
|
103 * Constructs a new Path that is a copy of <code>path</code>. If
|
|
104 * <code>flatness</code> is less than or equal to zero, an unflatten
|
|
105 * copy of the path is created. Otherwise, it specifies the maximum
|
|
106 * error between the path and its flatten copy. Smaller numbers give
|
|
107 * better approximation.
|
|
108 * <p>
|
|
109 * This operation requires the operating system's advanced
|
|
110 * graphics subsystem which may not be available on some
|
|
111 * platforms.
|
|
112 * </p>
|
|
113 *
|
|
114 * @param device the device on which to allocate the path
|
|
115 * @param path the path to make a copy
|
|
116 * @param flatness the flatness value
|
|
117 *
|
|
118 * @exception IllegalArgumentException <ul>
|
|
119 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
|
|
120 * <li>ERROR_NULL_ARGUMENT - if the path is null</li>
|
|
121 * <li>ERROR_INVALID_ARGUMENT - if the path has been disposed</li>
|
|
122 * </ul>
|
|
123 * @exception SWTException <ul>
|
|
124 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
125 * </ul>
|
|
126 * @exception SWTError <ul>
|
|
127 * <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
|
|
128 * </ul>
|
|
129 *
|
|
130 * @see #dispose()
|
|
131 * @since 3.4
|
|
132 */
|
|
133 public this (Device device, Path path, float flatness) {
|
|
134 super(device);
|
|
135 if (path is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
136 if (path.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
137 auto surface = Cairo.cairo_image_surface_create(Cairo.CAIRO_FORMAT_ARGB32, 1, 1);
|
|
138 if (surface is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
139 handle = Cairo.cairo_create(surface);
|
|
140 Cairo.cairo_surface_destroy(surface);
|
|
141 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
142 cairo_path_t* copy;
|
|
143 flatness = Math.max(0, flatness);
|
|
144 if (flatness is 0) {
|
|
145 copy = Cairo.cairo_copy_path(path.handle);
|
|
146 } else {
|
|
147 double tolerance = Cairo.cairo_get_tolerance(path.handle);
|
|
148 Cairo.cairo_set_tolerance(path.handle, flatness);
|
|
149 copy = Cairo.cairo_copy_path_flat(path.handle);
|
|
150 Cairo.cairo_set_tolerance(path.handle, tolerance);
|
|
151 }
|
|
152 if (copy is null) {
|
|
153 Cairo.cairo_destroy(handle);
|
|
154 SWT.error(SWT.ERROR_NO_HANDLES);
|
|
155 }
|
|
156 Cairo.cairo_append_path(handle, copy);
|
|
157 Cairo.cairo_path_destroy(copy);
|
|
158 init_();
|
|
159 }
|
|
160
|
|
161 /**
|
|
162 * Constructs a new Path with the specifed PathData.
|
|
163 * <p>
|
|
164 * This operation requires the operating system's advanced
|
|
165 * graphics subsystem which may not be available on some
|
|
166 * platforms.
|
|
167 * </p>
|
|
168 *
|
|
169 * @param device the device on which to allocate the path
|
|
170 * @param data the data for the path
|
|
171 *
|
|
172 * @exception IllegalArgumentException <ul>
|
|
173 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
|
|
174 * <li>ERROR_NULL_ARGUMENT - if the data is null</li>
|
|
175 * </ul>
|
|
176 * @exception SWTException <ul>
|
|
177 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
|
|
178 * </ul>
|
|
179 * @exception SWTError <ul>
|
|
180 * <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
|
|
181 * </ul>
|
|
182 *
|
|
183 * @see #dispose()
|
|
184 * @since 3.4
|
|
185 */
|
|
186 public this (Device device, PathData data) {
|
|
187 this(device);
|
|
188 if (data is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
189 init_(data);
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Adds to the receiver a circular or elliptical arc that lies within
|
|
194 * the specified rectangular area.
|
|
195 * <p>
|
|
196 * The resulting arc begins at <code>startAngle</code> and extends
|
|
197 * for <code>arcAngle</code> degrees.
|
|
198 * Angles are interpreted such that 0 degrees is at the 3 o'clock
|
|
199 * position. A positive value indicates a counter-clockwise rotation
|
|
200 * while a negative value indicates a clockwise rotation.
|
|
201 * </p><p>
|
|
202 * The center of the arc is the center of the rectangle whose origin
|
|
203 * is (<code>x</code>, <code>y</code>) and whose size is specified by the
|
|
204 * <code>width</code> and <code>height</code> arguments.
|
|
205 * </p><p>
|
|
206 * The resulting arc covers an area <code>width + 1</code> pixels wide
|
|
207 * by <code>height + 1</code> pixels tall.
|
|
208 * </p>
|
|
209 *
|
|
210 * @param x the x coordinate of the upper-left corner of the arc
|
|
211 * @param y the y coordinate of the upper-left corner of the arc
|
|
212 * @param width the width of the arc
|
|
213 * @param height the height of the arc
|
|
214 * @param startAngle the beginning angle
|
|
215 * @param arcAngle the angular extent of the arc, relative to the start angle
|
|
216 *
|
|
217 * @exception SWTException <ul>
|
|
218 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
219 * </ul>
|
|
220 */
|
|
221 public void addArc(float x, float y, float width, float height, float startAngle, float arcAngle) {
|
|
222 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
223 moved = true;
|
|
224 if (width is height) {
|
|
225 float angle = -startAngle * cast(float)Compatibility.PI / 180;
|
|
226 if (closed) Cairo.cairo_move_to(handle, (x + width / 2f) + width / 2f * Math.cos(angle), (y + height / 2f) + height / 2f * Math.sin(angle));
|
|
227 if (arcAngle >= 0) {
|
|
228 Cairo.cairo_arc_negative(handle, x + width / 2f, y + height / 2f, width / 2f, angle, -(startAngle + arcAngle) * cast(float)Compatibility.PI / 180);
|
|
229 } else {
|
|
230 Cairo.cairo_arc(handle, x + width / 2f, y + height / 2f, width / 2f, angle, -(startAngle + arcAngle) * cast(float)Compatibility.PI / 180);
|
|
231 }
|
|
232 } else {
|
|
233 Cairo.cairo_save(handle);
|
|
234 Cairo.cairo_translate(handle, x + width / 2f, y + height / 2f);
|
|
235 Cairo.cairo_scale(handle, width / 2f, height / 2f);
|
|
236 float angle = -startAngle * cast(float)Compatibility.PI / 180;
|
|
237 if (closed) Cairo.cairo_move_to(handle, Math.cos(angle), Math.sin(angle));
|
|
238 if (arcAngle >= 0) {
|
|
239 Cairo.cairo_arc_negative(handle, 0, 0, 1, angle, -(startAngle + arcAngle) * cast(float)Compatibility.PI / 180);
|
|
240 } else {
|
|
241 Cairo.cairo_arc(handle, 0, 0, 1, angle, -(startAngle + arcAngle) * cast(float)Compatibility.PI / 180);
|
|
242 }
|
|
243 Cairo.cairo_restore(handle);
|
|
244 }
|
|
245 closed = false;
|
|
246 if (Math.abs(arcAngle) >= 360) close();
|
|
247 }
|
|
248
|
|
249 /**
|
|
250 * Adds to the receiver the path described by the parameter.
|
|
251 *
|
|
252 * @param path the path to add to the receiver
|
|
253 *
|
|
254 * @exception IllegalArgumentException <ul>
|
|
255 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
|
|
256 * <li>ERROR_INVALID_ARGUMENT - if the parameter has been disposed</li>
|
|
257 * </ul>
|
|
258 * @exception SWTException <ul>
|
|
259 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
260 * </ul>
|
|
261 */
|
|
262 public void addPath(Path path) {
|
|
263 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
264 if (path is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
265 if (path.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
266 moved = false;
|
|
267 auto copy = Cairo.cairo_copy_path(path.handle);
|
|
268 if (copy is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
269 Cairo.cairo_append_path(handle, copy);
|
|
270 Cairo.cairo_path_destroy(copy);
|
|
271 closed = path.closed;
|
|
272 }
|
|
273
|
|
274 /**
|
|
275 * Adds to the receiver the rectangle specified by x, y, width and height.
|
|
276 *
|
|
277 * @param x the x coordinate of the rectangle to add
|
|
278 * @param y the y coordinate of the rectangle to add
|
|
279 * @param width the width of the rectangle to add
|
|
280 * @param height the height of the rectangle to add
|
|
281 *
|
|
282 * @exception SWTException <ul>
|
|
283 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
284 * </ul>
|
|
285 */
|
|
286 public void addRectangle(float x, float y, float width, float height) {
|
|
287 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
288 moved = false;
|
|
289 Cairo.cairo_rectangle(handle, x, y, width, height);
|
|
290 closed = true;
|
|
291 }
|
|
292
|
|
293 /**
|
|
294 * Adds to the receiver the pattern of glyphs generated by drawing
|
|
295 * the given string using the given font starting at the point (x, y).
|
|
296 *
|
|
297 * @param string the text to use
|
|
298 * @param x the x coordinate of the starting point
|
|
299 * @param y the y coordinate of the starting point
|
|
300 * @param font the font to use
|
|
301 *
|
|
302 * @exception IllegalArgumentException <ul>
|
|
303 * <li>ERROR_NULL_ARGUMENT - if the font is null</li>
|
|
304 * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li>
|
|
305 * </ul>
|
|
306 * @exception SWTException <ul>
|
|
307 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
308 * </ul>
|
|
309 */
|
|
310 public void addString(String str, float x, float y, Font font) {
|
|
311 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
312 if (font is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
313 if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
314 moved = false;
|
|
315 GC.addCairoString(handle, str, x, y, font);
|
|
316 closed = true;
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * Closes the current sub path by adding to the receiver a line
|
|
321 * from the current point of the path back to the starting point
|
|
322 * of the sub path.
|
|
323 *
|
|
324 * @exception SWTException <ul>
|
|
325 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
326 * </ul>
|
|
327 */
|
|
328 public void close() {
|
|
329 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
330 Cairo.cairo_close_path(handle);
|
|
331 moved = false;
|
|
332 closed = true;
|
|
333 }
|
|
334
|
|
335 /**
|
|
336 * Returns <code>true</code> if the specified point is contained by
|
|
337 * the receiver and false otherwise.
|
|
338 * <p>
|
|
339 * If outline is <code>true</code>, the point (x, y) checked for containment in
|
|
340 * the receiver's outline. If outline is <code>false</code>, the point is
|
|
341 * checked to see if it is contained within the bounds of the (closed) area
|
|
342 * covered by the receiver.
|
|
343 *
|
|
344 * @param x the x coordinate of the point to test for containment
|
|
345 * @param y the y coordinate of the point to test for containment
|
|
346 * @param gc the GC to use when testing for containment
|
|
347 * @param outline controls whether to check the outline or contained area of the path
|
|
348 * @return <code>true</code> if the path contains the point and <code>false</code> otherwise
|
|
349 *
|
|
350 * @exception IllegalArgumentException <ul>
|
|
351 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
|
|
352 * <li>ERROR_INVALID_ARGUMENT - if the gc has been disposed</li>
|
|
353 * </ul>
|
|
354 * @exception SWTException <ul>
|
|
355 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
356 * </ul>
|
|
357 */
|
|
358 public bool contains(float x, float y, GC gc, bool outline) {
|
|
359 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
360 if (gc is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
361 if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
362 //TODO - see Windows
|
|
363 gc.initCairo();
|
|
364 gc.checkGC(GC.LINE_CAP | GC.LINE_JOIN | GC.LINE_STYLE | GC.LINE_WIDTH);
|
|
365 bool result = false;
|
|
366 auto cairo = gc.data.cairo;
|
|
367 auto copy = Cairo.cairo_copy_path(handle);
|
|
368 if (copy is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
369 Cairo.cairo_append_path(cairo, copy);
|
|
370 Cairo.cairo_path_destroy(copy);
|
|
371 if (outline) {
|
|
372 result = Cairo.cairo_in_stroke(cairo, x, y) !is 0;
|
|
373 } else {
|
|
374 result = Cairo.cairo_in_fill(cairo, x, y) !is 0;
|
|
375 }
|
|
376 Cairo.cairo_new_path(cairo);
|
|
377 return result;
|
|
378 }
|
|
379
|
|
380 /**
|
|
381 * Adds to the receiver a cubic bezier curve based on the parameters.
|
|
382 *
|
|
383 * @param cx1 the x coordinate of the first control point of the spline
|
|
384 * @param cy1 the y coordinate of the first control of the spline
|
|
385 * @param cx2 the x coordinate of the second control of the spline
|
|
386 * @param cy2 the y coordinate of the second control of the spline
|
|
387 * @param x the x coordinate of the end point of the spline
|
|
388 * @param y the y coordinate of the end point of the spline
|
|
389 *
|
|
390 * @exception SWTException <ul>
|
|
391 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
392 * </ul>
|
|
393 */
|
|
394 public void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) {
|
|
395 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
396 if (!moved) {
|
|
397 double currentX, currentY;
|
|
398 Cairo.cairo_get_current_point(handle, ¤tX, ¤tY);
|
|
399 Cairo.cairo_move_to(handle, currentX, currentY);
|
|
400 moved = true;
|
|
401 }
|
|
402 Cairo.cairo_curve_to(handle, cx1, cy1, cx2, cy2, x, y);
|
|
403 closed = false;
|
|
404 }
|
|
405
|
|
406 /**
|
|
407 * Replaces the first four elements in the parameter with values that
|
|
408 * describe the smallest rectangle that will completely contain the
|
|
409 * receiver (i.e. the bounding box).
|
|
410 *
|
|
411 * @param bounds the array to hold the result
|
|
412 *
|
|
413 * @exception IllegalArgumentException <ul>
|
|
414 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
|
|
415 * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the bounding box</li>
|
|
416 * </ul>
|
|
417 * @exception SWTException <ul>
|
|
418 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
419 * </ul>
|
|
420 */
|
|
421 public void getBounds(float[] bounds) {
|
|
422 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
423 if (bounds is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
424 if (bounds.length < 4) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
425 auto copy = Cairo.cairo_copy_path(handle);
|
|
426 if (copy is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
427 cairo_path_t* path = new cairo_path_t();
|
|
428 memmove(path, copy, cairo_path_t.sizeof);
|
|
429 double minX = 0, minY = 0, maxX = 0, maxY = 0;
|
|
430 if (path.num_data > 0) {
|
|
431 minX = minY = double.max;
|
|
432 maxX = maxY = -double.max;
|
|
433 int i = 0;
|
|
434 cairo_path_data_t* data = new cairo_path_data_t();
|
|
435 while (i < path.num_data) {
|
|
436 *data = path.data[i];
|
|
437 switch (data.header.type) {
|
|
438 case Cairo.CAIRO_PATH_MOVE_TO:
|
|
439 minX = Math.min(minX, path.data[i+1].point.x);
|
|
440 minY = Math.min(minY, path.data[i+1].point.y);
|
|
441 maxX = Math.max(maxX, path.data[i+1].point.x);
|
|
442 maxY = Math.max(maxY, path.data[i+1].point.y);
|
|
443 break;
|
|
444 case Cairo.CAIRO_PATH_LINE_TO:
|
|
445 minX = Math.min(minX, path.data[i+1].point.x);
|
|
446 minY = Math.min(minY, path.data[i+1].point.y);
|
|
447 maxX = Math.max(maxX, path.data[i+1].point.x);
|
|
448 maxY = Math.max(maxY, path.data[i+1].point.y);
|
|
449 break;
|
|
450 case Cairo.CAIRO_PATH_CURVE_TO:
|
|
451 minX = Math.min(minX, path.data[i+1].point.x);
|
|
452 minY = Math.min(minY, path.data[i+1].point.y);
|
|
453 maxX = Math.max(maxX, path.data[i+1].point.x);
|
|
454 maxY = Math.max(maxY, path.data[i+1].point.y);
|
|
455 minX = Math.min(minX, path.data[i+2].point.x);
|
|
456 minY = Math.min(minY, path.data[i+2].point.y);
|
|
457 maxX = Math.max(maxX, path.data[i+2].point.x);
|
|
458 maxY = Math.max(maxY, path.data[i+2].point.y);
|
|
459 minX = Math.min(minX, path.data[i+3].point.x);
|
|
460 minY = Math.min(minY, path.data[i+3].point.y);
|
|
461 maxX = Math.max(maxX, path.data[i+3].point.x);
|
|
462 maxY = Math.max(maxY, path.data[i+3].point.y);
|
|
463 break;
|
|
464 case Cairo.CAIRO_PATH_CLOSE_PATH: break;
|
|
465 default:
|
|
466 }
|
|
467 i += data.header.length;
|
|
468 }
|
|
469 }
|
|
470 bounds[0] = cast(float)minX;
|
|
471 bounds[1] = cast(float)minY;
|
|
472 bounds[2] = cast(float)(maxX - minX);
|
|
473 bounds[3] = cast(float)(maxY - minY);
|
|
474 Cairo.cairo_path_destroy(copy);
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * Replaces the first two elements in the parameter with values that
|
|
479 * describe the current point of the path.
|
|
480 *
|
|
481 * @param point the array to hold the result
|
|
482 *
|
|
483 * @exception IllegalArgumentException <ul>
|
|
484 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
|
|
485 * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the end point</li>
|
|
486 * </ul>
|
|
487 * @exception SWTException <ul>
|
|
488 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
489 * </ul>
|
|
490 */
|
|
491 public void getCurrentPoint(float[] point) {
|
|
492 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
493 if (point is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
494 if (point.length < 2) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
495 double x, y;
|
|
496 Cairo.cairo_get_current_point(handle, &x, &y);
|
|
497 point[0] = cast(float)x;
|
|
498 point[1] = cast(float)y;
|
|
499 }
|
|
500
|
|
501 /**
|
|
502 * Returns a device independent representation of the receiver.
|
|
503 *
|
|
504 * @return the PathData for the receiver
|
|
505 *
|
|
506 * @exception SWTException <ul>
|
|
507 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
508 * </ul>
|
|
509 *
|
|
510 * @see PathData
|
|
511 */
|
|
512 public PathData getPathData() {
|
|
513 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
514 auto copy = Cairo.cairo_copy_path(handle);
|
|
515 if (copy is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
516 cairo_path_t* path = new cairo_path_t();
|
|
517 *path = *copy;
|
|
518 byte[] types = new byte[path.num_data];
|
|
519 float[] pts = new float[path.num_data * 6];
|
|
520 int typeIndex = 0, ptsIndex = 0;
|
|
521 if (path.num_data > 0) {
|
|
522 int i = 0;
|
|
523 double[] points = new double[6];
|
|
524 cairo_path_data_t* data = new cairo_path_data_t();
|
|
525 while (i < path.num_data) {
|
|
526 switch (data.header.type) {
|
|
527 case Cairo.CAIRO_PATH_MOVE_TO:
|
|
528 types[typeIndex++] = SWT.PATH_MOVE_TO;
|
|
529 pts[ptsIndex++] = cast(float)path.data[i+1].point.x;
|
|
530 pts[ptsIndex++] = cast(float)path.data[i+1].point.y;
|
|
531 break;
|
|
532 case Cairo.CAIRO_PATH_LINE_TO:
|
|
533 types[typeIndex++] = SWT.PATH_LINE_TO;
|
|
534 pts[ptsIndex++] = cast(float)path.data[i+1].point.x;
|
|
535 pts[ptsIndex++] = cast(float)path.data[i+1].point.y;
|
|
536 break;
|
|
537 case Cairo.CAIRO_PATH_CURVE_TO:
|
|
538 types[typeIndex++] = SWT.PATH_CUBIC_TO;
|
|
539 pts[ptsIndex++] = cast(float)path.data[i+1].point.x;
|
|
540 pts[ptsIndex++] = cast(float)path.data[i+1].point.y;
|
|
541 pts[ptsIndex++] = cast(float)path.data[i+2].point.x;
|
|
542 pts[ptsIndex++] = cast(float)path.data[i+2].point.y;
|
|
543 pts[ptsIndex++] = cast(float)path.data[i+3].point.x;
|
|
544 pts[ptsIndex++] = cast(float)path.data[i+3].point.y;
|
|
545 break;
|
|
546 case Cairo.CAIRO_PATH_CLOSE_PATH:
|
|
547 types[typeIndex++] = SWT.PATH_CLOSE;
|
|
548 break;
|
|
549 default:
|
|
550 }
|
|
551 i += data.header.length;
|
|
552 }
|
|
553 }
|
|
554 if (typeIndex !is types.length) {
|
|
555 byte[] newTypes = new byte[typeIndex];
|
|
556 System.arraycopy(types, 0, newTypes, 0, typeIndex);
|
|
557 types = newTypes;
|
|
558 }
|
|
559 if (ptsIndex !is pts.length) {
|
|
560 float[] newPts = new float[ptsIndex];
|
|
561 System.arraycopy(pts, 0, newPts, 0, ptsIndex);
|
|
562 pts = newPts;
|
|
563 }
|
|
564 Cairo.cairo_path_destroy(copy);
|
|
565 PathData result = new PathData();
|
|
566 result.types = types;
|
|
567 result.points = pts;
|
|
568 return result;
|
|
569 }
|
|
570
|
|
571 /**
|
|
572 * Adds to the receiver a line from the current point to
|
|
573 * the point specified by (x, y).
|
|
574 *
|
|
575 * @param x the x coordinate of the end of the line to add
|
|
576 * @param y the y coordinate of the end of the line to add
|
|
577 *
|
|
578 * @exception SWTException <ul>
|
|
579 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
580 * </ul>
|
|
581 */
|
|
582 public void lineTo(float x, float y) {
|
|
583 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
584 if (!moved) {
|
|
585 double currentX, currentY;
|
|
586 Cairo.cairo_get_current_point(handle, ¤tX, ¤tY);
|
|
587 Cairo.cairo_move_to(handle, currentX, currentY);
|
|
588 moved = true;
|
|
589 }
|
|
590 Cairo.cairo_line_to(handle, x, y);
|
|
591 closed = false;
|
|
592 }
|
|
593
|
|
594 /**
|
|
595 * Sets the current point of the receiver to the point
|
|
596 * specified by (x, y). Note that this starts a new
|
|
597 * sub path.
|
|
598 *
|
|
599 * @param x the x coordinate of the new end point
|
|
600 * @param y the y coordinate of the new end point
|
|
601 *
|
|
602 * @exception SWTException <ul>
|
|
603 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
604 * </ul>
|
|
605 */
|
|
606 public void moveTo(float x, float y) {
|
|
607 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
608 /*
|
|
609 * Bug in Cairo. If cairo_move_to() is not called at the
|
|
610 * begining of a subpath, the first cairo_line_to() or
|
|
611 * cairo_curve_to() segment do not output anything. The fix
|
|
612 * is to detect that the app did not call cairo_move_to()
|
|
613 * before those calls and call it explicitly.
|
|
614 */
|
|
615 moved = true;
|
|
616 Cairo.cairo_move_to(handle, x, y);
|
|
617 closed = true;
|
|
618 }
|
|
619
|
|
620 /**
|
|
621 * Adds to the receiver a quadratic curve based on the parameters.
|
|
622 *
|
|
623 * @param cx the x coordinate of the control point of the spline
|
|
624 * @param cy the y coordinate of the control point of the spline
|
|
625 * @param x the x coordinate of the end point of the spline
|
|
626 * @param y the y coordinate of the end point of the spline
|
|
627 *
|
|
628 * @exception SWTException <ul>
|
|
629 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
630 * </ul>
|
|
631 */
|
|
632 public void quadTo(float cx, float cy, float x, float y) {
|
|
633 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
634 double currentX, currentY;
|
|
635 Cairo.cairo_get_current_point(handle, ¤tX, ¤tY);
|
|
636 if (!moved) {
|
|
637 Cairo.cairo_move_to(handle, currentX, currentY);
|
|
638 moved = true;
|
|
639 }
|
|
640 float x0 = cast(float)currentX;
|
|
641 float y0 = cast(float)currentY;
|
|
642 float cx1 = x0 + 2 * (cx - x0) / 3;
|
|
643 float cy1 = y0 + 2 * (cy - y0) / 3;
|
|
644 float cx2 = cx1 + (x - x0) / 3;
|
|
645 float cy2 = cy1 + (y - y0) / 3;
|
|
646 Cairo.cairo_curve_to(handle, cx1, cy1, cx2, cy2, x, y);
|
|
647 closed = false;
|
|
648 }
|
|
649
|
|
650 void destroy() {
|
|
651 Cairo.cairo_destroy(handle);
|
|
652 handle = null;
|
|
653 }
|
|
654
|
|
655 void init_(PathData data) {
|
|
656 byte[] types = data.types;
|
|
657 float[] points = data.points;
|
|
658 for (int i = 0, j = 0; i < types.length; i++) {
|
|
659 switch (types[i]) {
|
|
660 case SWT.PATH_MOVE_TO:
|
|
661 moveTo(points[j++], points[j++]);
|
|
662 break;
|
|
663 case SWT.PATH_LINE_TO:
|
|
664 lineTo(points[j++], points[j++]);
|
|
665 break;
|
|
666 case SWT.PATH_CUBIC_TO:
|
|
667 cubicTo(points[j++], points[j++], points[j++], points[j++], points[j++], points[j++]);
|
|
668 break;
|
|
669 case SWT.PATH_QUAD_TO:
|
|
670 quadTo(points[j++], points[j++], points[j++], points[j++]);
|
|
671 break;
|
|
672 case SWT.PATH_CLOSE:
|
|
673 close();
|
|
674 break;
|
|
675 default:
|
|
676 dispose();
|
|
677 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
678 }
|
|
679 }
|
|
680 }
|
|
681
|
|
682 /**
|
|
683 * Returns <code>true</code> if the Path has been disposed,
|
|
684 * and <code>false</code> otherwise.
|
|
685 * <p>
|
|
686 * This method gets the dispose state for the Path.
|
|
687 * When a Path has been disposed, it is an error to
|
|
688 * invoke any other method using the Path.
|
|
689 *
|
|
690 * @return <code>true</code> when the Path is disposed, and <code>false</code> otherwise
|
|
691 */
|
|
692 public override bool isDisposed() {
|
|
693 return handle is null;
|
|
694 }
|
|
695
|
|
696 /**
|
|
697 * Returns a string containing a concise, human-readable
|
|
698 * description of the receiver.
|
|
699 *
|
|
700 * @return a string representation of the receiver
|
|
701 */
|
|
702 public override String toString() {
|
|
703 if (isDisposed()) return "Path {*DISPOSED*}";
|
|
704 return Format( "Path {{{}}", handle );
|
|
705 }
|
|
706
|
|
707 }
|