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