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