1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.graphics;
15 
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.internal.*;
18 import org.eclipse.swt.internal.gdip.*;
19 import org.eclipse.swt.internal.win32.*;
20 
21 /**
22  * Instances of this class represent paths through the two-dimensional
23  * coordinate system. Paths do not have to be continuous, and can be
24  * described using lines, rectangles, arcs, cubic or quadratic bezier curves,
25  * glyphs, or other paths.
26  * <p>
27  * Application code must explicitly invoke the <code>Path.dispose()</code>
28  * method to release the operating system resources managed by each instance
29  * when those instances are no longer required.
30  * </p>
31  * <p>
32  * This class requires the operating system's advanced graphics subsystem
33  * which may not be available on some platforms.
34  * </p>
35  *
36  * @see <a href="http://www.eclipse.org/swt/snippets/#path">Path, Pattern snippets</a>
37  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: GraphicsExample</a>
38  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
39  *
40  * @since 3.1
41  */
42 public class Path extends Resource {
43 
44 	/**
45 	 * the OS resource for the Path
46 	 * (Warning: This field is platform dependent)
47 	 * <p>
48 	 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
49 	 * public API. It is marked public only so that it can be shared
50 	 * within the packages provided by SWT. It is not available on all
51 	 * platforms and should never be accessed from application code.
52 	 * </p>
53 	 *
54 	 * @noreference This field is not intended to be referenced by clients.
55 	 */
56 	public long handle;
57 
58 	PointF currentPoint = new PointF(), startPoint = new PointF();
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  * <p>
68  * You must dispose the path when it is no longer required.
69  * </p>
70  *
71  * @param device the device on which to allocate the path
72  *
73  * @exception IllegalArgumentException <ul>
74  *    <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
75  * </ul>
76  * @exception SWTException <ul>
77  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
78  * </ul>
79  * @exception SWTError <ul>
80  *    <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
81  * </ul>
82  *
83  * @see #dispose()
84  */
Path(Device device)85 public Path (Device device) {
86 	super(device);
87 	this.device.checkGDIP();
88 	handle = Gdip.GraphicsPath_new(Gdip.FillModeAlternate);
89 	if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
90 	init();
91 }
92 
93 /**
94  * Constructs a new Path that is a copy of <code>path</code>. If
95  * <code>flatness</code> is less than or equal to zero, an unflatten
96  * copy of the path is created. Otherwise, it specifies the maximum
97  * error between the path and its flatten copy. Smaller numbers give
98  * better approximation.
99  * <p>
100  * This operation requires the operating system's advanced
101  * graphics subsystem which may not be available on some
102  * platforms.
103  * </p>
104  * <p>
105  * You must dispose the path when it is no longer required.
106  * </p>
107  *
108  * @param device the device on which to allocate the path
109  * @param path the path to make a copy
110  * @param flatness the flatness value
111  *
112  * @exception IllegalArgumentException <ul>
113  *    <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
114  *    <li>ERROR_NULL_ARGUMENT - if the path is null</li>
115  *    <li>ERROR_INVALID_ARGUMENT - if the path has been disposed</li>
116  * </ul>
117  * @exception SWTException <ul>
118  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
119  * </ul>
120  * @exception SWTError <ul>
121  *    <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
122  * </ul>
123  *
124  * @see #dispose()
125  * @since 3.4
126  */
Path(Device device, Path path, float flatness)127 public Path (Device device, Path path, float flatness) {
128 	super(device);
129 	if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
130 	if (path.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
131 	flatness = Math.max(0, flatness);
132 	handle = Gdip.GraphicsPath_Clone(path.handle);
133 	if (flatness != 0) Gdip.GraphicsPath_Flatten(handle, 0, flatness);
134 	if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
135 	init();
136 }
137 
138 /**
139  * Constructs a new Path with the specified PathData.
140  * <p>
141  * This operation requires the operating system's advanced
142  * graphics subsystem which may not be available on some
143  * platforms.
144  * </p>
145  * <p>
146  * You must dispose the path when it is no longer required.
147  * </p>
148  *
149  * @param device the device on which to allocate the path
150  * @param data the data for the path
151  *
152  * @exception IllegalArgumentException <ul>
153  *    <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device</li>
154  *    <li>ERROR_NULL_ARGUMENT - if the data is null</li>
155  * </ul>
156  * @exception SWTException <ul>
157  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
158  * </ul>
159  * @exception SWTError <ul>
160  *    <li>ERROR_NO_HANDLES if a handle for the path could not be obtained</li>
161  * </ul>
162  *
163  * @see #dispose()
164  * @since 3.4
165  */
Path(Device device, PathData data)166 public Path (Device device, PathData data) {
167 	this(device);
168 	if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
169 	init(data);
170 }
171 
172 /**
173  * Adds to the receiver a circular or elliptical arc that lies within
174  * the specified rectangular area.
175  * <p>
176  * The resulting arc begins at <code>startAngle</code> and extends
177  * for <code>arcAngle</code> degrees.
178  * Angles are interpreted such that 0 degrees is at the 3 o'clock
179  * position. A positive value indicates a counter-clockwise rotation
180  * while a negative value indicates a clockwise rotation.
181  * </p><p>
182  * The center of the arc is the center of the rectangle whose origin
183  * is (<code>x</code>, <code>y</code>) and whose size is specified by the
184  * <code>width</code> and <code>height</code> arguments.
185  * </p><p>
186  * The resulting arc covers an area <code>width + 1</code> points wide
187  * by <code>height + 1</code> points tall.
188  * </p>
189  *
190  * @param x the x coordinate of the upper-left corner of the arc
191  * @param y the y coordinate of the upper-left corner of the arc
192  * @param width the width of the arc
193  * @param height the height of the arc
194  * @param startAngle the beginning angle
195  * @param arcAngle the angular extent of the arc, relative to the start angle
196  *
197  * @exception SWTException <ul>
198  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
199  * </ul>
200  */
addArc(float x, float y, float width, float height, float startAngle, float arcAngle)201 public void addArc (float x, float y, float width, float height, float startAngle, float arcAngle) {
202 	x = DPIUtil.autoScaleUp(x);
203 	y = DPIUtil.autoScaleUp(y);
204 	width = DPIUtil.autoScaleUp(width);
205 	height = DPIUtil.autoScaleUp(height);
206 	addArcInPixels(x, y, width, height, startAngle, arcAngle);
207 }
208 
addArcInPixels(float x, float y, float width, float height, float startAngle, float arcAngle)209 void addArcInPixels(float x, float y, float width, float height, float startAngle, float arcAngle) {
210 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
211 	if (width < 0) {
212 		x = x + width;
213 		width = -width;
214 	}
215 	if (height < 0) {
216 		y = y + height;
217 		height = -height;
218 	}
219 	if (width == 0 || height == 0 || arcAngle == 0) return;
220 	if (width == height) {
221 		Gdip.GraphicsPath_AddArc(handle, x, y, width, height, -startAngle, -arcAngle);
222 	} else {
223 		long path = Gdip.GraphicsPath_new(Gdip.FillModeAlternate);
224 		if (path == 0) SWT.error(SWT.ERROR_NO_HANDLES);
225 		long matrix = Gdip.Matrix_new(width, 0, 0, height, x, y);
226 		if (matrix == 0) SWT.error(SWT.ERROR_NO_HANDLES);
227 		Gdip.GraphicsPath_AddArc(path, 0, 0, 1, 1, -startAngle, -arcAngle);
228 		Gdip.GraphicsPath_Transform(path, matrix);
229 		Gdip.GraphicsPath_AddPath(handle, path, true);
230 		Gdip.Matrix_delete(matrix);
231 		Gdip.GraphicsPath_delete(path);
232 	}
233 	Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
234 }
235 
236 /**
237  * Adds to the receiver the path described by the parameter.
238  *
239  * @param path the path to add to the receiver
240  *
241  * @exception IllegalArgumentException <ul>
242  *    <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
243  *    <li>ERROR_INVALID_ARGUMENT - if the parameter has been disposed</li>
244  * </ul>
245  * @exception SWTException <ul>
246  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
247  * </ul>
248  */
addPath(Path path)249 public void addPath(Path path) {
250 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
251 	if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
252 	if (path.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
253 	//TODO - expose connect?
254 	Gdip.GraphicsPath_AddPath(handle, path.handle, false);
255 	currentPoint.X = path.currentPoint.X;
256 	currentPoint.Y = path.currentPoint.Y;
257 }
258 
259 /**
260  * Adds to the receiver the rectangle specified by x, y, width and height.
261  *
262  * @param x the x coordinate of the rectangle to add
263  * @param y the y coordinate of the rectangle to add
264  * @param width the width of the rectangle to add
265  * @param height the height of the rectangle to add
266  *
267  * @exception SWTException <ul>
268  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
269  * </ul>
270  */
addRectangle(float x, float y, float width, float height)271 public void addRectangle (float x, float y, float width, float height) {
272 	x = DPIUtil.autoScaleUp(x);
273 	y = DPIUtil.autoScaleUp(y);
274 	width = DPIUtil.autoScaleUp(width);
275 	height = DPIUtil.autoScaleUp(height);
276 	addRectangleInPixels(x, y, width, height);
277 }
278 
addRectangleInPixels(float x, float y, float width, float height)279 void addRectangleInPixels(float x, float y, float width, float height) {
280 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
281 	RectF rect = new RectF();
282 	rect.X = x;
283 	rect.Y = y;
284 	rect.Width = width;
285 	rect.Height = height;
286 	Gdip.GraphicsPath_AddRectangle(handle, rect);
287 	currentPoint.X = x;
288 	currentPoint.Y = y;
289 }
290 
291 /**
292  * Adds to the receiver the pattern of glyphs generated by drawing
293  * the given string using the given font starting at the point (x, y).
294  *
295  * @param string the text to use
296  * @param x the x coordinate of the starting point
297  * @param y the y coordinate of the starting point
298  * @param font the font to use
299  *
300  * @exception IllegalArgumentException <ul>
301  *    <li>ERROR_NULL_ARGUMENT - if the font is null</li>
302  *    <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li>
303  * </ul>
304  * @exception SWTException <ul>
305  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
306  * </ul>
307  */
addString(String string, float x, float y, Font font)308 public void addString (String string, float x, float y, Font font) {
309 	x = DPIUtil.autoScaleUp(x);
310 	y = DPIUtil.autoScaleUp(y);
311 	addStringInPixels(string, x, y, font);
312 }
313 
addStringInPixels(String string, float x, float y, Font font)314 void addStringInPixels(String string, float x, float y, Font font) {
315 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
316 	if (font == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
317 	if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
318 	char[] buffer = string.toCharArray();
319 	long hDC = device.internal_new_GC(null);
320 	long [] family = new long [1];
321 	long gdipFont = GC.createGdipFont(hDC, font.handle, 0, device.fontCollection, family, null);
322 	PointF point = new PointF();
323 	point.X = x - (Gdip.Font_GetSize(gdipFont) / 6);
324 	point.Y = y;
325 	int style = Gdip.Font_GetStyle(gdipFont);
326 	float size = Gdip.Font_GetSize(gdipFont);
327 	Gdip.GraphicsPath_AddString(handle, buffer, buffer.length, family[0], style, size, point, 0);
328 	Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
329 	Gdip.FontFamily_delete(family[0]);
330 	Gdip.Font_delete(gdipFont);
331 	device.internal_dispose_GC(hDC, null);
332 }
333 
334 /**
335  * Closes the current sub path by adding to the receiver a line
336  * from the current point of the path back to the starting point
337  * of the sub path.
338  *
339  * @exception SWTException <ul>
340  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
341  * </ul>
342  */
close()343 public void close() {
344 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
345 	Gdip.GraphicsPath_CloseFigure(handle);
346 	/*
347 	* Feature in GDI+. CloseFigure() does affect the last
348 	* point, so GetLastPoint() does not return the starting
349 	* point of the subpath after calling CloseFigure().  The
350 	* fix is to remember the subpath starting point and use
351 	* it instead.
352 	*/
353 	currentPoint.X = startPoint.X;
354 	currentPoint.Y = startPoint.Y;
355 }
356 
357 /**
358  * Returns <code>true</code> if the specified point is contained by
359  * the receiver and false otherwise.
360  * <p>
361  * If outline is <code>true</code>, the point (x, y) checked for containment in
362  * the receiver's outline. If outline is <code>false</code>, the point is
363  * checked to see if it is contained within the bounds of the (closed) area
364  * covered by the receiver.
365  *
366  * @param x the x coordinate of the point to test for containment
367  * @param y the y coordinate of the point to test for containment
368  * @param gc the GC to use when testing for containment
369  * @param outline controls whether to check the outline or contained area of the path
370  * @return <code>true</code> if the path contains the point and <code>false</code> otherwise
371  *
372  * @exception IllegalArgumentException <ul>
373  *    <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
374  *    <li>ERROR_INVALID_ARGUMENT - if the gc has been disposed</li>
375  * </ul>
376  * @exception SWTException <ul>
377  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
378  * </ul>
379  */
contains(float x, float y, GC gc, boolean outline)380 public boolean contains (float x, float y, GC gc, boolean outline) {
381 	x = DPIUtil.autoScaleUp(x);
382 	y = DPIUtil.autoScaleUp(y);
383 	return containsInPixels(x, y, gc, outline);
384 }
385 
containsInPixels(float x, float y, GC gc, boolean outline)386 boolean containsInPixels(float x, float y, GC gc, boolean outline) {
387 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
388 	if (gc == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
389 	if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
390 	//TODO - should use GC transformation
391 	gc.initGdip();
392 	gc.checkGC(GC.LINE_CAP | GC.LINE_JOIN | GC.LINE_STYLE | GC.LINE_WIDTH);
393 	int mode = OS.GetPolyFillMode(gc.handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
394 	Gdip.GraphicsPath_SetFillMode(handle, mode);
395 	if (outline) {
396 		return Gdip.GraphicsPath_IsOutlineVisible(handle, x, y, gc.data.gdipPen, gc.data.gdipGraphics);
397 	} else {
398 		return Gdip.GraphicsPath_IsVisible(handle, x, y, gc.data.gdipGraphics);
399 	}
400 }
401 
402 /**
403  * Adds to the receiver a cubic bezier curve based on the parameters.
404  *
405  * @param cx1 the x coordinate of the first control point of the spline
406  * @param cy1 the y coordinate of the first control of the spline
407  * @param cx2 the x coordinate of the second control of the spline
408  * @param cy2 the y coordinate of the second control of the spline
409  * @param x the x coordinate of the end point of the spline
410  * @param y the y coordinate of the end point of the spline
411  *
412  * @exception SWTException <ul>
413  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
414  * </ul>
415  */
cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y)416 public void cubicTo (float cx1, float cy1, float cx2, float cy2, float x, float y) {
417 	cx1 = DPIUtil.autoScaleUp(cx1);
418 	cy1 = DPIUtil.autoScaleUp(cy1);
419 	cx2 = DPIUtil.autoScaleUp(cx2);
420 	cy2 = DPIUtil.autoScaleUp(cy2);
421 	x = DPIUtil.autoScaleUp(x);
422 	y = DPIUtil.autoScaleUp(y);
423 	cubicToInPixels(cx1, cy1, cx2, cy2, x, y);
424 }
425 
cubicToInPixels(float cx1, float cy1, float cx2, float cy2, float x, float y)426 void cubicToInPixels(float cx1, float cy1, float cx2, float cy2, float x, float y) {
427 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
428 	Gdip.GraphicsPath_AddBezier(handle, currentPoint.X, currentPoint.Y, cx1, cy1, cx2, cy2, x, y);
429 	Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
430 }
431 
432 @Override
destroy()433 void destroy() {
434 	Gdip.GraphicsPath_delete(handle);
435 	handle = 0;
436 }
437 
438 /**
439  * Replaces the first four elements in the parameter with values that
440  * describe the smallest rectangle that will completely contain the
441  * receiver (i.e. the bounding box).
442  *
443  * @param bounds the array to hold the result
444  *
445  * @exception IllegalArgumentException <ul>
446  *    <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
447  *    <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the bounding box</li>
448  * </ul>
449  * @exception SWTException <ul>
450  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
451  * </ul>
452  */
getBounds(float[] bounds)453 public void getBounds (float[] bounds) {
454 	if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
455 	getBoundsInPixels(bounds);
456 	bounds = DPIUtil.autoScaleDown(bounds);
457 }
458 
getBoundsInPixels(float[] bounds)459 void getBoundsInPixels(float[] bounds) {
460 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
461 	if (bounds.length < 4) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
462 	RectF rect = new RectF();
463 	Gdip.GraphicsPath_GetBounds(handle, rect, 0, 0);
464 	bounds[0] = rect.X;
465 	bounds[1] = rect.Y;
466 	bounds[2] = rect.Width;
467 	bounds[3] = rect.Height;
468 }
469 
470 /**
471  * Replaces the first two elements in the parameter with values that
472  * describe the current point of the path.
473  *
474  * @param point the array to hold the result
475  *
476  * @exception IllegalArgumentException <ul>
477  *    <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
478  *    <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the end point</li>
479  * </ul>
480  * @exception SWTException <ul>
481  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
482  * </ul>
483  */
getCurrentPoint(float[] point)484 public void getCurrentPoint (float[] point) {
485 	if (point == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
486 	getCurrentPointInPixels(point);
487 	point = DPIUtil.autoScaleDown(point);
488 }
489 
getCurrentPointInPixels(float[] point)490 void getCurrentPointInPixels(float[] point) {
491 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
492 	if (point.length < 2) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
493 	point[0] = currentPoint.X;
494 	point[1] = currentPoint.Y;
495 }
496 
497 /**
498  * Returns a device independent representation of the receiver.
499  *
500  * @return the PathData for the receiver
501  *
502  * @exception SWTException <ul>
503  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
504  * </ul>
505  *
506  * @see PathData
507  */
getPathData()508 public PathData getPathData() {
509 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
510 	PathData result = getPathDataInPixels();
511 	result.points = DPIUtil.autoScaleDown(result.points);
512 	return result;
513 }
514 
getPathDataInPixels()515 PathData getPathDataInPixels() {
516 	int count = Gdip.GraphicsPath_GetPointCount(handle);
517 	byte[] gdipTypes = new byte[count];
518 	float[] points = new float[count * 2];
519 	Gdip.GraphicsPath_GetPathTypes(handle, gdipTypes, count);
520 	Gdip.GraphicsPath_GetPathPoints(handle, points, count);
521 	byte[] types = new byte[count * 2];
522 	int index = 0, typesIndex = 0;
523 	while (index < count) {
524 		byte type = gdipTypes[index];
525 		boolean close = false;
526 		switch (type & Gdip.PathPointTypePathTypeMask) {
527 			case Gdip.PathPointTypeStart:
528 				types[typesIndex++] = SWT.PATH_MOVE_TO;
529 				close = (type & Gdip.PathPointTypeCloseSubpath) != 0;
530 				index += 1;
531 				break;
532 			case Gdip.PathPointTypeLine:
533 				types[typesIndex++] = SWT.PATH_LINE_TO;
534 				close = (type & Gdip.PathPointTypeCloseSubpath) != 0;
535 				index += 1;
536 				break;
537 			case Gdip.PathPointTypeBezier:
538 				types[typesIndex++] = SWT.PATH_CUBIC_TO;
539 				close = (gdipTypes[index + 2] & Gdip.PathPointTypeCloseSubpath) != 0;
540 				index += 3;
541 				break;
542 			default:
543 				index++;
544 		}
545 		if (close) {
546 			types[typesIndex++] = SWT.PATH_CLOSE;
547 		}
548 	}
549 	if (typesIndex != types.length) {
550 		byte[] newTypes = new byte[typesIndex];
551 		System.arraycopy(types, 0, newTypes, 0, typesIndex);
552 		types = newTypes;
553 	}
554 	PathData result = new PathData();
555 	result.types = types;
556 	result.points = points;
557 	return result;
558 }
559 
560 /**
561  * Adds to the receiver a line from the current point to
562  * the point specified by (x, y).
563  *
564  * @param x the x coordinate of the end of the line to add
565  * @param y the y coordinate of the end of the line to add
566  *
567  * @exception SWTException <ul>
568  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
569  * </ul>
570  */
lineTo(float x, float y)571 public void lineTo (float x, float y) {
572 	lineToInPixels(DPIUtil.autoScaleUp(x), DPIUtil.autoScaleUp(y));
573 }
574 
lineToInPixels(float x, float y)575 void lineToInPixels(float x, float y) {
576 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
577 	Gdip.GraphicsPath_AddLine(handle, currentPoint.X, currentPoint.Y, x, y);
578 	Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
579 }
580 
init(PathData data)581 void init(PathData data) {
582 	byte[] types = data.types;
583 	float[] points = data.points;
584 	for (int i = 0, j = 0; i < types.length; i++) {
585 		switch (types[i]) {
586 			case SWT.PATH_MOVE_TO:
587 				moveTo(points[j++], points[j++]);
588 				break;
589 			case SWT.PATH_LINE_TO:
590 				lineTo(points[j++], points[j++]);
591 				break;
592 			case SWT.PATH_CUBIC_TO:
593 				cubicTo(points[j++], points[j++], points[j++], points[j++], points[j++], points[j++]);
594 				break;
595 			case SWT.PATH_QUAD_TO:
596 				quadTo(points[j++], points[j++], points[j++], points[j++]);
597 				break;
598 			case SWT.PATH_CLOSE:
599 				close();
600 				break;
601 			default:
602 				dispose();
603 				SWT.error(SWT.ERROR_INVALID_ARGUMENT);
604 		}
605 	}
606 }
607 
608 /**
609  * Returns <code>true</code> if the Path has been disposed,
610  * and <code>false</code> otherwise.
611  * <p>
612  * This method gets the dispose state for the Path.
613  * When a Path has been disposed, it is an error to
614  * invoke any other method (except {@link #dispose()}) using the Path.
615  *
616  * @return <code>true</code> when the Path is disposed, and <code>false</code> otherwise
617  */
618 @Override
isDisposed()619 public boolean isDisposed() {
620 	return handle == 0;
621 }
622 
623 /**
624  * Sets the current point of the receiver to the point
625  * specified by (x, y). Note that this starts a new
626  * sub path.
627  *
628  * @param x the x coordinate of the new end point
629  * @param y the y coordinate of the new end point
630  *
631  * @exception SWTException <ul>
632  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
633  * </ul>
634  */
moveTo(float x, float y)635 public void moveTo (float x, float y) {
636 	moveToInPixels(DPIUtil.autoScaleUp(x), DPIUtil.autoScaleUp(y));
637 }
638 
moveToInPixels(float x, float y)639 void moveToInPixels(float x, float y) {
640 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
641 	Gdip.GraphicsPath_StartFigure(handle);
642 	currentPoint.X = startPoint.X = x;
643 	currentPoint.Y = startPoint.Y = y;
644 }
645 
646 /**
647  * Adds to the receiver a quadratic curve based on the parameters.
648  *
649  * @param cx the x coordinate of the control point of the spline
650  * @param cy the y coordinate of the control point of the spline
651  * @param x the x coordinate of the end point of the spline
652  * @param y the y coordinate of the end point of the spline
653  *
654  * @exception SWTException <ul>
655  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
656  * </ul>
657  */
quadTo(float cx, float cy, float x, float y)658 public void quadTo (float cx, float cy, float x, float y) {
659 	cx = DPIUtil.autoScaleUp(cx);
660 	cy = DPIUtil.autoScaleUp(cy);
661 	x = DPIUtil.autoScaleUp(x);
662 	y = DPIUtil.autoScaleUp(y);
663 	quadToInPixels(cx, cy, x, y);
664 }
665 
quadToInPixels(float cx, float cy, float x, float y)666 void quadToInPixels(float cx, float cy, float x, float y) {
667 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
668 	float cx1 = currentPoint.X + 2 * (cx - currentPoint.X) / 3;
669 	float cy1 = currentPoint.Y + 2 * (cy - currentPoint.Y) / 3;
670 	float cx2 = cx1 + (x - currentPoint.X) / 3;
671 	float cy2 = cy1 + (y - currentPoint.Y) / 3;
672 	Gdip.GraphicsPath_AddBezier(handle, currentPoint.X, currentPoint.Y, cx1, cy1, cx2, cy2, x, y);
673 	Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
674 }
675 
676 /**
677  * Returns a string containing a concise, human-readable
678  * description of the receiver.
679  *
680  * @return a string representation of the receiver
681  */
682 @Override
toString()683 public String toString() {
684 	if (isDisposed()) return "Path {*DISPOSED*}";
685 	return "Path {" + handle + "}";
686 }
687 
688 }
689