Debian Codegraph @

sys/Graphics_linesAndAreas.cpp

raw symbols SCIP
1 /* Graphics_linesAndAreas.cpp
2 *
3 * Copyright (C) 1992-2005,2007-2022,2024,2025 Paul Boersma, 2013 Tom Naughton
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "GraphicsP.h"
20
21 /* Normally on, because e.g. the intensity contour in the Sound window should not run through the play buttons: */
22 #define FUNCTIONS_ARE_CLIPPED 1
23
24 #define LINE_WIDTH_IN_PIXELS(me) ( my resolution > 192 ? my lineWidth * (my resolution / 192.0) : my lineWidth )
25 #define ORDER_DC { double temp; if (x1DC > x2DC) temp = x1DC, x1DC = x2DC, x2DC = temp; \
26 if (yIsZeroAtTheTop == (y2DC > y1DC)) temp = y1DC, y1DC = y2DC, y2DC = temp; }
27
28 static void psPrepareLine (GraphicsPostscript me) {
29 double lineWidth_pixels = LINE_WIDTH_IN_PIXELS (me);
30 if (my lineType == Graphics_DOTTED)
31 my d_printf (my d_file, "[%td %td] 0 setdash\n", (integer) (my resolution / 100), (integer) (my resolution / 75 + lineWidth_pixels));
32 else if (my lineType == Graphics_DASHED)
33 my d_printf (my d_file, "[%td %td] 0 setdash\n", (integer) (my resolution / 25), (integer) (my resolution / 50 + lineWidth_pixels));
34 else if (my lineType == Graphics_DASHED_DOTTED)
35 my d_printf (my d_file, "[%td %td %td %td] 0 setdash\n",
36 (integer) (my resolution / 100), (integer) (my resolution / 60 + lineWidth_pixels),
37 (integer) (my resolution / 25), (integer) (my resolution / 60 + lineWidth_pixels));
38 if (my lineWidth != 1.0)
39 my d_printf (my d_file, "%g setlinewidth\n", lineWidth_pixels);
40 }
41 static void psRevertLine (GraphicsPostscript me) {
42 if (my lineType != Graphics_DRAWN)
43 my d_printf (my d_file, "[] 0 setdash\n");
44 if (my lineWidth != 1.0)
45 my d_printf (my d_file, "%g setlinewidth\n", my resolution > 192 ? my resolution / 192.0 : 1.0); // 0.375 point
46 }
47
48 #if cairo
49 static void cairoPrepareLine (GraphicsScreen me) {
50 if (! my d_cairoGraphicsContext) return;
51 double dotted_line [] { 2.0, 2.0 };
52 double dashed_line [] { 6.0, 2.0 };
53 double dashed_dotted_line [] { 6.0, 2.0, 2.0, 2.0 };
54 cairo_save (my d_cairoGraphicsContext);
55 switch (my lineType) {
56 case Graphics_DOTTED:
57 cairo_set_dash (my d_cairoGraphicsContext, dotted_line, 2, 1.0);
58 break;
59 case Graphics_DASHED:
60 cairo_set_dash (my d_cairoGraphicsContext, dashed_line, 2, 1.0);
61 break;
62 case Graphics_DASHED_DOTTED:
63 cairo_set_dash (my d_cairoGraphicsContext, dashed_dotted_line, 4, 1.0);
64 break;
65 }
66 cairo_set_line_width (my d_cairoGraphicsContext, my lineWidth);
67 }
68 static void cairoRevertLine (GraphicsScreen me) {
69 if (! my d_cairoGraphicsContext)
70 return;
71 if (my lineType >= Graphics_DOTTED)
72 cairo_set_dash (my d_cairoGraphicsContext, nullptr, 0, 0);
73 cairo_restore (my d_cairoGraphicsContext);
74 }
75 #elif gdi
76 #define MY_BRUSH SelectPen (d_gdiGraphicsContext, GetStockPen (NULL_PEN)), SelectBrush (d_gdiGraphicsContext, d_winBrush);
77 #define DEFAULT SelectPen (d_gdiGraphicsContext, GetStockPen (BLACK_PEN)), SelectBrush (d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH));
78 static void winPrepareLine (GraphicsScreen me) {
79 HPEN newPen;
80 const int lineWidth_pixels = Melder_clippedLeft (1, int (LINE_WIDTH_IN_PIXELS (me) + 0.5));
81 LOGBRUSH brush;
82 brush. lbStyle = BS_SOLID;
83 brush. lbColor = my d_winForegroundColour;
84 brush. lbHatch = ( my lineType == Graphics_DRAWN ? 0 : my lineType == Graphics_DOTTED ? PS_DOT : my lineType == Graphics_DASHED ? PS_DASH : PS_DASHDOT );
85 const double factor = ( my resolution > 192 ? 3.0 : 1.0 );
86 if (my lineType == Graphics_DRAWN) {
87 newPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT, lineWidth_pixels, & brush, 0, nullptr);
88 } else if (my lineType == Graphics_DOTTED) {
89 DWORD style [] = { static_cast <DWORD> (factor * (1 + lineWidth_pixels / 2)), static_cast <DWORD> (factor * (2 + lineWidth_pixels / 4)) };
90 newPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | PS_USERSTYLE, lineWidth_pixels, & brush, 2, style);
91 } else if (my lineType == Graphics_DASHED) {
92 DWORD style [] = { static_cast <DWORD> (factor * (4 + lineWidth_pixels)), static_cast <DWORD> (factor * (2 + lineWidth_pixels / 4)) };
93 newPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | PS_USERSTYLE, lineWidth_pixels, & brush, 2, style);
94 } else {
95 DWORD style [] = { static_cast <DWORD> (factor * (4 + lineWidth_pixels)), static_cast <DWORD> (factor * (2 + lineWidth_pixels / 4)),
96 static_cast <DWORD> (factor * (1 + lineWidth_pixels / 2)), static_cast <DWORD> (factor * (2 + lineWidth_pixels / 4)) };
97 newPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | PS_USERSTYLE, lineWidth_pixels, & brush, 4, style);
98 }
99 SelectPen (my d_gdiGraphicsContext, newPen);
100 DeletePen (my d_winPen);
101 my d_winPen = newPen;
102 }
103 #elif quartz
104 static void quartzPrepareLine (GraphicsScreen me) {
105 CGContextSetLineJoin (my d_macGraphicsContext, kCGLineJoinBevel); // much faster than kCGLineJoinRound
106 CGContextSetRGBStrokeColor (my d_macGraphicsContext, my colour.red, my colour.green, my colour.blue, 1.0);
107 double lineWidth_pixels = LINE_WIDTH_IN_PIXELS (me);
108 CGContextSetLineWidth (my d_macGraphicsContext, lineWidth_pixels);
109
110 CGFloat lengths [4];
111 if (my lineType == Graphics_DOTTED) {
112 lengths [0] = my resolution > 192 ? my resolution / 100.0 : 2;
113 lengths [1] = my resolution > 192 ? my resolution / 75.0 + lineWidth_pixels : 2;
114 } else if (my lineType == Graphics_DASHED) {
115 lengths [0] = my resolution > 192 ? my resolution / 25 : 6;
116 lengths [1] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
117 } else if (my lineType == Graphics_DASHED_DOTTED) {
118 lengths [0] = my resolution > 192 ? my resolution / 25 : 6;
119 lengths [1] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
120 lengths [2] = my resolution > 192 ? my resolution / 100.0 : 2;
121 lengths [3] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
122 }
123 CGContextSetLineDash (my d_macGraphicsContext, 0.0, my lineType == Graphics_DRAWN ? nullptr : lengths,
124 my lineType == 0 ? 0 : my lineType == Graphics_DASHED_DOTTED ? 4 : 2);
125 }
126 static void quartzRevertLine (GraphicsScreen me) {
127 }
128 static void quartzPrepareFill (GraphicsScreen me) {
129 CGContextSetAlpha (my d_macGraphicsContext, 1.0);
130 CGContextSetRGBFillColor (my d_macGraphicsContext, my colour.red, my colour.green, my colour.blue, 1.0);
131 }
132 #endif
133
134 /* First level. */
135
136 void structGraphicsScreen :: v_polyline (integer numberOfPoints, double *xyDC, bool close) {
137 #if cairo
138 if (our d_cairoGraphicsContext == nullptr)
139 return;
140 cairoPrepareLine (this);
141 // cairo_new_path (d_cairoGraphicsContext); // move_to() automatically creates a new path
142 cairo_move_to (our d_cairoGraphicsContext, xyDC [0], xyDC [1]);
143 for (integer i = 1; i < numberOfPoints; i ++)
144 cairo_line_to (our d_cairoGraphicsContext, xyDC [i + i], xyDC [i + i + 1]);
145 if (close)
146 cairo_close_path (our d_cairoGraphicsContext);
147 cairo_stroke (our d_cairoGraphicsContext);
148 cairoRevertLine (this);
149 #elif gdi
150 if (our d_useGdiplus && 0) {
151 Gdiplus::Graphics dcplus (our d_gdiGraphicsContext);
152 Gdiplus::Point *points = Melder_malloc (Gdiplus::Point, numberOfPoints + close);
153 if (points) {
154 for (integer i = 0; i < numberOfPoints; i ++) {
155 points [i]. X = *xyDC, xyDC ++;
156 points [i]. Y = *xyDC, xyDC ++;
157 }
158 if (close)
159 points [numberOfPoints] = points [0];
160 #define LINE_WIDTH_IN_PIXELS2(hjhkj) ( our resolution > 192 ? our lineWidth * (our resolution / 192.0) : our lineWidth )
161 Gdiplus::Pen pen (Gdiplus::Color (255,0,0,0), LINE_WIDTH_IN_PIXELS2 (this) + 0.5);
162 float dotted_line [] = { 2, 2 };
163 float dashed_line [] = { 6, 2 };
164 float dashed_dotted_line [] = { 6, 2, 2, 2 };
165 switch (our lineType) {
166 case Graphics_DOTTED:
167 pen. SetDashPattern (dotted_line, 2);
168 break;
169 case Graphics_DASHED:
170 pen. SetDashPattern (dashed_line, 2);
171 break;
172 case Graphics_DASHED_DOTTED:
173 pen. SetDashPattern (dashed_dotted_line, 4);
174 break;
175 }
176 dcplus. DrawLines (& pen, points, numberOfPoints + close);
177 Melder_free (points);
178 }
179 } else {
180 winPrepareLine (this);
181 POINT *points = Melder_malloc (POINT, numberOfPoints + close);
182 if (points) {
183 for (integer i = 0; i < numberOfPoints; i ++) {
184 points [i]. x = *xyDC, xyDC ++;
185 points [i]. y = *xyDC, xyDC ++;
186 }
187 if (close)
188 points [numberOfPoints] = points [0];
189 Polyline (our d_gdiGraphicsContext, points, numberOfPoints + close);
190 Melder_free (points);
191 }
192 DEFAULT
193 }
194 #elif quartz
195 quartzPrepareLine (this);
196 CGContextBeginPath (our d_macGraphicsContext);
197 trace (U"starting point ", xyDC [0], U" ", xyDC [1]);
198 CGContextMoveToPoint (our d_macGraphicsContext, xyDC [0], xyDC [1]); // starts a new subpath
199 for (integer i = 1; i < numberOfPoints; i ++)
200 CGContextAddLineToPoint (our d_macGraphicsContext, xyDC [i + i], xyDC [i + i + 1]);
201 if (close)
202 CGContextClosePath (our d_macGraphicsContext); // closes only the subpath
203 CGContextStrokePath (our d_macGraphicsContext);
204 quartzRevertLine (this);
205 #endif
206 }
207
208 void structGraphicsPostscript :: v_polyline (integer numberOfPoints, double *xyDC, bool close) {
209 const integer nn = 2 * numberOfPoints;
210 psPrepareLine (this);
211 our d_printf (our d_file, "N %.7g %.7g moveto\n", xyDC [0], xyDC [1]);
212 for (integer i = 2; i < nn; i += 2) {
213 const double dx = xyDC [i] - xyDC [i - 2], dy = xyDC [i + 1] - xyDC [i - 1];
214 our d_printf (our d_file, "%.7g %.7g L\n", dx, dy);
215 }
216 if (close)
217 our d_printf (our d_file, "closepath ");
218 our d_printf (our d_file, "stroke\n");
219 psRevertLine (this);
220 }
221
222 void structGraphicsScreen :: v_fillArea (integer numberOfPoints, double *xyDC) {
223 #if cairo
224 if (our d_cairoGraphicsContext == nullptr)
225 return;
226 // cairo_new_path (our d_cairoGraphicsContext); // move_to() automatically creates a new path
227 cairo_move_to (our d_cairoGraphicsContext, xyDC [0], xyDC [1]);
228 for (integer i = 1; i < numberOfPoints; i ++)
229 cairo_line_to (our d_cairoGraphicsContext, xyDC [i + i], xyDC [i + i + 1]);
230 cairo_close_path (our d_cairoGraphicsContext);
231 cairo_fill (our d_cairoGraphicsContext);
232 #elif gdi
233 MY_BRUSH
234 BeginPath (our d_gdiGraphicsContext);
235 MoveToEx (our d_gdiGraphicsContext, xyDC [0], xyDC [1], nullptr);
236 for (integer i = 1; i < numberOfPoints; i ++)
237 LineTo (our d_gdiGraphicsContext, xyDC [i + i], xyDC [i + i + 1]);
238 EndPath (our d_gdiGraphicsContext);
239 FillPath (our d_gdiGraphicsContext);
240 DEFAULT
241 #elif quartz
242 quartzPrepareFill (this);
243 CGContextBeginPath (our d_macGraphicsContext);
244 CGContextMoveToPoint (our d_macGraphicsContext, xyDC [0], xyDC [1]);
245 for (integer i = 1; i < numberOfPoints; i ++)
246 CGContextAddLineToPoint (our d_macGraphicsContext, xyDC [i + i], xyDC [i + i + 1]);
247 CGContextFillPath (our d_macGraphicsContext);
248 #endif
249 }
250
251 void structGraphicsPostscript :: v_fillArea (integer numberOfPoints, double *xyDC) {
252 integer nn = numberOfPoints + numberOfPoints;
253 our d_printf (our d_file, "N %.7g %.7g M\n", xyDC [0], xyDC [1]);
254 for (integer i = 2; i < nn; i += 2)
255 our d_printf (our d_file, "%.7g %.7g L\n", xyDC [i] - xyDC [i - 2], xyDC [i + 1] - xyDC [i - 1]);
256 our d_printf (our d_file, "closepath fill\n");
257 }
258
259 /* Second level. */
260
261 void structGraphicsScreen :: v_rectangle (double x1DC, double x2DC, double y1DC, double y2DC) {
262 ORDER_DC
263 #if cairo
264 if (! our d_cairoGraphicsContext)
265 return;
266 const double width = x2DC - x1DC, height = y1DC - y2DC;
267 if (width <= 0.0 || height <= 0.0)
268 return;
269 cairoPrepareLine (this);
270 cairo_rectangle (our d_cairoGraphicsContext, x1DC, y2DC, width, height);
271 cairo_stroke (our d_cairoGraphicsContext);
272 cairoRevertLine (this);
273 #elif gdi
274 winPrepareLine (this);
275 Rectangle (our d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1.0, y1DC + 1.0);
276 DEFAULT
277 #elif quartz
278 quartzPrepareLine (this);
279 CGContextStrokeRect (our d_macGraphicsContext, CGRectMake (x1DC, y2DC, x2DC - x1DC, y1DC - y2DC));
280 quartzRevertLine (this);
281 #else
282 double xyDC [8];
283 xyDC [0] = x1DC; xyDC [1] = y1DC;
284 xyDC [2] = x2DC; xyDC [3] = y1DC;
285 xyDC [4] = x2DC; xyDC [5] = y2DC;
286 xyDC [6] = x1DC; xyDC [7] = y2DC;
287 our v_polyline (5, & xyDC [0], true);
288 #endif
289 }
290
291 void structGraphicsPostscript :: v_rectangle (double x1DC, double x2DC, double y1DC, double y2DC) {
292 psPrepareLine (this);
293 our d_printf (our d_file, "N %.7g %.7g M %.7g %.7g lineto %.7g %.7g lineto %.7g %.7g lineto closepath stroke\n",
294 x1DC, y1DC, x2DC, y1DC, x2DC, y2DC, x1DC, y2DC);
295 psRevertLine (this);
296 }
297
298 void structGraphicsScreen :: v_fillRectangle (double x1DC, double x2DC, double y1DC, double y2DC) {
299 ORDER_DC
300 #if cairo
301 if (! our d_cairoGraphicsContext)
302 return;
303 const double width = x2DC - x1DC + 1.0, height = y1DC - y2DC + 1.0;
304 if (width <= 0.0 || height <= 0.0)
305 return;
306 trace (U"x1DC ", x1DC, U", x2DC ", x2DC, U", y1DC ", y1DC, U", y2DC ", y2DC);
307 cairo_rectangle (our d_cairoGraphicsContext, round (x1DC), round (y2DC), round (width), round (height));
308 cairo_fill (our d_cairoGraphicsContext);
309 #elif gdi
310 RECT rect;
311 rect. left = x1DC, rect. right = x2DC, rect. top = y2DC, rect. bottom = y1DC; /* Superfluous? */
312 MY_BRUSH
313 Rectangle (our d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1.0, y1DC + 1.0);
314 DEFAULT
315 #elif quartz
316 quartzPrepareFill (this);
317 CGContextFillRect (our d_macGraphicsContext, CGRectMake (x1DC, y2DC, x2DC - x1DC, y1DC - y2DC));
318 #else
319 double xyDC [10];
320 xyDC [0] = x1DC; xyDC [1] = y1DC;
321 xyDC [2] = x2DC; xyDC [3] = y1DC;
322 xyDC [4] = x2DC; xyDC [5] = y2DC;
323 xyDC [6] = x1DC; xyDC [7] = y2DC;
324 xyDC [8] = x1DC; xyDC [9] = y1DC;
325 our v_fillArea (5, & xyDC [0]);
326 #endif
327 }
328
329 void structGraphicsPostscript :: v_fillRectangle (double x1DC, double x2DC, double y1DC, double y2DC) {
330 our d_printf (our d_file,
331 "N %.7g %.7g M %.7g %.7g lineto %.7g %.7g lineto %.7g %.7g lineto closepath fill\n",
332 x1DC, y1DC, x2DC, y1DC, x2DC, y2DC, x1DC, y2DC);
333 }
334
335 void structGraphicsScreen :: v_circle (double xDC, double yDC, double rDC) {
336 #if cairo
337 if (! our d_cairoGraphicsContext)
338 return;
339 cairoPrepareLine (this);
340 cairo_new_path (our d_cairoGraphicsContext);
341 cairo_arc (our d_cairoGraphicsContext, xDC, yDC, rDC, 0.0, 2.0 * M_PI);
342 cairo_stroke (our d_cairoGraphicsContext);
343 cairoRevertLine (this);
344 #elif gdi
345 winPrepareLine (this);
346 Ellipse (our d_gdiGraphicsContext, xDC - rDC, yDC - rDC, xDC + rDC, yDC + rDC);
347 DEFAULT
348 #elif quartz
349 quartzPrepareLine (this);
350 CGContextBeginPath (our d_macGraphicsContext);
351 CGContextAddArc (our d_macGraphicsContext, xDC, yDC, rDC, 0.0, NUM2pi, 0);
352 CGContextStrokePath (our d_macGraphicsContext);
353 quartzRevertLine (this);
354 #endif
355 }
356
357 void structGraphicsPostscript :: v_circle (double xDC, double yDC, double rDC) {
358 psPrepareLine (this);
359 our d_printf (our d_file, "N %td %td %td C\n", (integer) xDC, (integer) yDC, (integer) rDC);
360 psRevertLine (this);
361 }
362
363 void structGraphicsScreen :: v_ellipse (double x1DC, double x2DC, double y1DC, double y2DC) {
364 ORDER_DC
365 #if cairo
366 if (! our d_cairoGraphicsContext)
367 return;
368 cairoPrepareLine (this);
369 cairo_new_path (our d_cairoGraphicsContext);
370 cairo_save (our d_cairoGraphicsContext);
371 cairo_translate (our d_cairoGraphicsContext, 0.5 * (x2DC + x1DC), 0.5 * (y2DC + y1DC));
372 cairo_scale (our d_cairoGraphicsContext, 0.5 * (x2DC - x1DC), 0.5 * (y2DC - y1DC));
373 cairo_arc (our d_cairoGraphicsContext, 0.0, 0.0, 1.0, 0.0, 2 * M_PI);
374 cairo_restore (our d_cairoGraphicsContext);
375 cairo_stroke (our d_cairoGraphicsContext);
376 cairoRevertLine (this);
377 #elif gdi
378 winPrepareLine (this);
379 Ellipse (our d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1, y1DC + 1);
380 DEFAULT
381 #elif quartz
382 quartzPrepareLine (this);
383 //NSCAssert (our d_macGraphicsContext, @"nil context");
384 CGContextBeginPath (our d_macGraphicsContext);
385 CGContextSaveGState (our d_macGraphicsContext);
386 CGContextTranslateCTM (our d_macGraphicsContext, 0.5 * (x2DC + x1DC), 0.5 * (y2DC + y1DC));
387 CGContextScaleCTM (our d_macGraphicsContext, 0.5 * (x2DC - x1DC), 0.5 * (y2DC - y1DC));
388 CGContextAddArc (our d_macGraphicsContext, 0.0, 0.0, 1.0, 0.0, NUM2pi, 0);
389 CGContextScaleCTM (our d_macGraphicsContext, 2.0 / (x2DC - x1DC), 2.0 / (y2DC - y1DC));
390 CGContextStrokePath (our d_macGraphicsContext);
391 CGContextRestoreGState (our d_macGraphicsContext);
392 quartzRevertLine (this);
393 #endif
394 }
395
396 void structGraphicsPostscript :: v_ellipse (double x1DC, double x2DC, double y1DC, double y2DC) {
397 if (x1DC != x2DC && y1DC != y2DC) { // prevent division by zero
398 psPrepareLine (this);
399 /* To draw an ellipse, we will have to 'translate' and 'scale' and draw a circle. */
400 /* However, we have to scale back before the actual 'stroke', */
401 /* because we want the normal line thickness; */
402 /* So we cannot use 'gsave' and 'grestore', which clear the path (Cookbook 3). */
403 our d_printf (our d_file, "gsave %.7g %.7g translate %.7g %.7g scale N 0 0 1 0 360 arc\n"
404 " %.7g %.7g scale stroke grestore\n",
405 0.5 * (x2DC + x1DC), 0.5 * (y2DC + y1DC), 0.5 * (x2DC - x1DC), 0.5 * (y2DC - y1DC),
406 2.0 / (x2DC - x1DC), 2.0 / (y2DC - y1DC)
407 );
408 psRevertLine (this);
409 }
410 }
411
412 void structGraphicsScreen :: v_arc (double xDC, double yDC, double rDC, double fromAngle, double toAngle) {
413 #if cairo
414 if (! our d_cairoGraphicsContext)
415 return;
416 cairoPrepareLine (this);
417 cairo_new_path (our d_cairoGraphicsContext);
418 cairo_arc (our d_cairoGraphicsContext, xDC, yDC, rDC, -toAngle * (M_PI / 180.0), -fromAngle * (M_PI / 180.0));
419 cairo_stroke (our d_cairoGraphicsContext);
420 cairoRevertLine (this);
421 #elif gdi
422 int arcAngle = (int) toAngle - (int) fromAngle;
423 POINT pt;
424 if (arcAngle < 0.0)
425 arcAngle += 360;
426 winPrepareLine (this);
427 MoveToEx (our d_gdiGraphicsContext, xDC + rDC * cos (NUMpi / 180 * fromAngle), yDC - rDC * sin (NUMpi / 180 * fromAngle), & pt);
428 AngleArc (our d_gdiGraphicsContext, xDC, yDC, rDC, fromAngle, arcAngle);
429 DEFAULT
430 #elif quartz
431 quartzPrepareLine (this);
432 CGContextBeginPath (our d_macGraphicsContext);
433 CGContextAddArc (our d_macGraphicsContext, xDC, yDC, rDC, NUM2pi - NUMpi / 180 * toAngle, NUM2pi - NUMpi / 180 * fromAngle, 0);
434 CGContextStrokePath (our d_macGraphicsContext);
435 quartzRevertLine (this);
436 #endif
437 }
438
439 void structGraphicsPostscript :: v_arc (double xDC, double yDC, double rDC, double fromAngle, double toAngle) {
440 psPrepareLine (this);
441 our d_printf (d_file, "N %.7g %.7g %.7g %.7g %.7g arc stroke\n", xDC, yDC, rDC, fromAngle, toAngle);
442 psRevertLine (this);
443 }
444
445 /* Third level. */
446
447 void structGraphicsScreen :: v_fillCircle (double xDC, double yDC, double rDC) {
448 #if cairo
449 if (! our d_cairoGraphicsContext)
450 return;
451 cairo_new_path (our d_cairoGraphicsContext);
452 cairo_arc (our d_cairoGraphicsContext, xDC, yDC, rDC, 0, 2 * M_PI);
453 cairo_fill (our d_cairoGraphicsContext);
454 #elif gdi
455 MY_BRUSH
456 /*
457 * NT cannot fill circles that span less than five pixels...
458 */
459 Ellipse (our d_gdiGraphicsContext, xDC - rDC, yDC - rDC, xDC + rDC, yDC + rDC);
460 DEFAULT
461 #elif quartz
462 quartzPrepareFill (this);
463 CGContextBeginPath (our d_macGraphicsContext);
464 CGContextAddArc (our d_macGraphicsContext, xDC, yDC, rDC, 0.0, NUM2pi, 0);
465 CGContextFillPath (our d_macGraphicsContext);
466 #else
467 our v_circle (xDC, yDC, rDC);
468 #endif
469 }
470
471 void structGraphicsPostscript :: v_fillCircle (double xDC, double yDC, double rDC) {
472 d_printf (d_file, "N %.7g %.7g %.7g FC\n", xDC, yDC, rDC);
473 }
474
475 void structGraphicsScreen :: v_fillEllipse (double x1DC, double x2DC, double y1DC, double y2DC) {
476 ORDER_DC
477 #if cairo
478 if (! our d_cairoGraphicsContext)
479 return;
480 cairo_new_path (our d_cairoGraphicsContext);
481 cairo_save (our d_cairoGraphicsContext);
482 cairo_translate (our d_cairoGraphicsContext, 0.5 * (x2DC + x1DC), 0.5 * (y2DC + y1DC));
483 cairo_scale (our d_cairoGraphicsContext, 0.5 * (x2DC - x1DC), 0.5 * (y2DC - y1DC));
484 cairo_arc (our d_cairoGraphicsContext, 0.0, 0.0, 1.0, 0.0, 2.0 * M_PI);
485 cairo_restore (our d_cairoGraphicsContext);
486 cairo_fill (our d_cairoGraphicsContext);
487 #elif gdi
488 MY_BRUSH
489 Ellipse (our d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1.0, y1DC + 1.0);
490 DEFAULT
491 #elif quartz
492 quartzPrepareFill (this);
493 //NSCAssert (our d_macGraphicsContext, @"nil context");
494 CGContextBeginPath (our d_macGraphicsContext);
495 CGContextSaveGState (our d_macGraphicsContext);
496 CGContextTranslateCTM (our d_macGraphicsContext, 0.5 * (x2DC + x1DC), 0.5 * (y2DC + y1DC));
497 CGContextScaleCTM (our d_macGraphicsContext, 0.5 * (x2DC - x1DC), 0.5 * (y2DC - y1DC));
498 CGContextAddArc (our d_macGraphicsContext, 0.0, 0.0, 1.0, 0.0, NUM2pi, 0);
499 CGContextScaleCTM (our d_macGraphicsContext, 2.0 / (x2DC - x1DC), 2.0 / (y2DC - y1DC));
500 CGContextFillPath (our d_macGraphicsContext);
501 CGContextRestoreGState (our d_macGraphicsContext);
502 #else
503 our v_ellipse (x1DC, x2DC, y1DC, y2DC);
504 #endif
505 }
506
507 void structGraphicsPostscript :: v_fillEllipse (double x1DC, double x2DC, double y1DC, double y2DC) {
508 our d_printf (our d_file, "gsave %.7g %.7g translate %.7g %.7g scale N 0 0 1 FC grestore\n",
509 (x2DC + x1DC) / 2.0, (y2DC + y1DC) / 2.0, (x2DC - x1DC) / 2.0, (y2DC - y1DC) / 2.0);
510 }
511
512 void structGraphicsScreen :: v_button (double x1DC, double x2DC, double y1DC, double y2DC) {
513 ORDER_DC
514 #if cairo
515 if (x2DC <= x1DC || y1DC <= y2DC)
516 return;
517 cairo_save (our d_cairoGraphicsContext);
518 #if 0
519 if (our d_drawingArea) {
520 // clip to drawing area
521 const int w = gdk_window_get_width (our d_window);
522 const int h = gdk_window_get_height (our d_window);
523 cairo_rectangle (our d_cairoGraphicsContext, 0, 0, w, h);
524 cairo_clip (our d_cairoGraphicsContext);
525 }
526 #endif
527 cairo_set_line_width (our d_cairoGraphicsContext, 1.0);
528 double left = x1DC - 0.5, right = x2DC - 0.5, top = y2DC + 0.5, bottom = y1DC + 0.5;
529 double width = right - left, height = bottom - top;
530 cairo_set_source_rgb (our d_cairoGraphicsContext, 0.1, 0.1, 0.1); // dark grey
531 cairo_rectangle (our d_cairoGraphicsContext, left, top, width, height);
532 cairo_stroke (our d_cairoGraphicsContext);
533
534 left += 1.0;
535 right -= 1.0;
536 top += 1.0;
537 bottom -= 1.0;
538 width -= 2.0;
539 height -= 2.0;
540 if (width > 0.0 && height > 0.0) {
541 cairo_set_source_rgb (our d_cairoGraphicsContext, 0.3, 0.3, 0.3);
542 cairo_move_to (our d_cairoGraphicsContext, left + 1, bottom);
543 cairo_line_to (our d_cairoGraphicsContext, right, bottom);
544 cairo_line_to (our d_cairoGraphicsContext, right, top + 1);
545 cairo_stroke (our d_cairoGraphicsContext);
546
547 cairo_set_source_rgb (our d_cairoGraphicsContext, 1.0, 1.0, 1.0);
548 cairo_move_to (our d_cairoGraphicsContext, left, bottom);
549 cairo_line_to (our d_cairoGraphicsContext, left, top);
550 cairo_line_to (our d_cairoGraphicsContext, right, top);
551 cairo_stroke (our d_cairoGraphicsContext);
552 left += 0.5;
553 right -= 0.5;
554 top += 0.5;
555 bottom -= 0.5;
556 width -= 1.0;
557 height -= 1.0;
558 if (width > 0.0 && height > 0.0) {
559 cairo_set_source_rgb (our d_cairoGraphicsContext, 0.65, 0.65, 0.65);
560 cairo_rectangle (our d_cairoGraphicsContext, left, top, width, height);
561 cairo_fill (our d_cairoGraphicsContext);
562 }
563 }
564 cairo_restore (our d_cairoGraphicsContext);
565 #elif quartz
566 double width = x2DC - x1DC, height = y1DC - y2DC;
567 if (width <= 0.0 || height <= 0.0)
568 return;
569 /*
570 This is pixel-precise drawing, and may therefore be different on retina displays than on 100 dpi displays.
571 */
572 //const bool isRetinaDisplay = [[our d_macView window] backingScaleFactor] == 2.0;
573
574 CGContextSetLineWidth (our d_macGraphicsContext, 1.0);
575 CGContextSetAllowsAntialiasing (our d_macGraphicsContext, false); // because we want to draw by pixel
576 CGFloat red = 0.3, green = 0.3, blue = 0.2;
577 CGContextSetRGBStrokeColor (our d_macGraphicsContext, red, green, blue, 1.0);
578 x1DC += 0.5;
579 x2DC -= 0.5;
580 y1DC -= 0.5;
581 y2DC += 0.5;
582 width = x2DC - x1DC;
583 height = y1DC - y2DC;
584 CGRect rect = CGRectMake (x1DC, y2DC, width, height);
585 CGContextAddRect (our d_macGraphicsContext, rect);
586 CGContextStrokePath (our d_macGraphicsContext);
587 x1DC += 1.0;
588 x2DC -= 1.0;
589 y1DC -= 1.0;
590 y2DC += 1.0;
591 width = x2DC - x1DC;
592 height = y1DC - y2DC;
593 if (width > 0.0 && height > 0.0) {
594 red = 0.5, green = 0.5, blue = 0.4;
595 CGContextSetRGBStrokeColor (our d_macGraphicsContext, red, green, blue, 1.0);
596 CGContextMoveToPoint (our d_macGraphicsContext, x1DC, y1DC);
597 CGContextAddLineToPoint (our d_macGraphicsContext, x2DC, y1DC);
598 CGContextMoveToPoint (our d_macGraphicsContext, x2DC, y1DC);
599 CGContextAddLineToPoint (our d_macGraphicsContext, x2DC, y2DC);
600 CGContextStrokePath (our d_macGraphicsContext);
601 red = 1.0, green = 1.0, blue = 0.9;
602 CGContextSetRGBStrokeColor (our d_macGraphicsContext, red, green, blue, 1.0);
603 CGContextMoveToPoint (our d_macGraphicsContext, x1DC, y1DC);
604 CGContextAddLineToPoint (our d_macGraphicsContext, x1DC, y2DC);
605 CGContextMoveToPoint (our d_macGraphicsContext, x1DC, y2DC);
606 CGContextAddLineToPoint (our d_macGraphicsContext, x2DC, y2DC);
607 CGContextStrokePath (our d_macGraphicsContext);
608 if (width > 2.0 && height > 2.0) {
609 red = 0.75, green = 0.75, blue = 0.65;
610 CGContextSetRGBFillColor (our d_macGraphicsContext, red, green, blue, 1.0);
611 rect = CGRectMake (x1DC, y2DC, width, height);
612 CGContextFillRect (our d_macGraphicsContext, rect);
613 }
614 }
615 CGContextSetAllowsAntialiasing (our d_macGraphicsContext, true);
616 CGContextSetLineDash (our d_macGraphicsContext, 0, nullptr, 0);
617 #elif gdi
618 RECT rect;
619 rect. left = x1DC, rect. right = x2DC, rect. top = y2DC, rect. bottom = y1DC;
620 DrawEdge (our d_gdiGraphicsContext, & rect, EDGE_RAISED, BF_RECT);
621 SelectPen (our d_gdiGraphicsContext, GetStockPen (NULL_PEN));
622 static HBRUSH brush;
623 if (! brush)
624 brush = CreateSolidBrush (RGB (0.75 * 255, 0.75 * 255, 0.65 * 255));
625 SelectBrush (our d_gdiGraphicsContext, brush);
626 Rectangle (our d_gdiGraphicsContext, x1DC + 1, y2DC + 1, x2DC - 1, y1DC - 1);
627 SelectPen (our d_gdiGraphicsContext, GetStockPen (BLACK_PEN));
628 SelectBrush (our d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH));
629 #endif
630 }
631
632 void structGraphics :: v_roundedRectangle (double x1DC, double x2DC, double y1DC, double y2DC, double r) {
633 const double dy = ( yIsZeroAtTheTop ? - r : r );
634 double xyDC [4];
635 ORDER_DC
636 xyDC [0] = x1DC + r;
637 xyDC [1] = y1DC;
638 xyDC [2] = x2DC - r;
639 xyDC [3] = y1DC;
640 our v_polyline (2, xyDC, false);
641 our v_arc (x2DC - r, y1DC + dy, r, -90.0, 0.0);
642 xyDC [0] = x2DC;
643 xyDC [1] = y1DC + dy;
644 xyDC [2] = x2DC;
645 xyDC [3] = y2DC - dy;
646 our v_polyline (2, xyDC, false);
647 our v_arc (x2DC - r, y2DC - dy, r, 0.0, 90.0);
648 xyDC [0] = x2DC - r;
649 xyDC [1] = y2DC;
650 xyDC [2] = x1DC + r;
651 xyDC [3] = y2DC;
652 our v_polyline (2, xyDC, false);
653 our v_arc (x1DC + r, y2DC - dy, r, 90.0, 180.0);
654 xyDC [0] = x1DC;
655 xyDC [1] = y2DC - dy;
656 xyDC [2] = x1DC;
657 xyDC [3] = y1DC + dy;
658 our v_polyline (2, xyDC, false);
659 our v_arc (x1DC + r, y1DC + dy, r, 180.0, 270.0);
660 }
661
662 void structGraphicsScreen :: v_roundedRectangle (double x1DC, double x2DC, double y1DC, double y2DC, double r) {
663 #if gdi
664 const double dy = ( yIsZeroAtTheTop ? - r : r );
665 double xyDC [4];
666 ORDER_DC
667 winPrepareLine (this);
668 RoundRect (d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1.0, y1DC + 1.0, r + r, r + r);
669 DEFAULT
670 return;
671 #else
672 GraphicsScreen_Parent :: v_roundedRectangle (x1DC, x2DC, y1DC, y2DC, r);
673 #endif
674 }
675
676 /* Fourth level. */
677
678 void structGraphics :: v_fillRoundedRectangle (double x1DC, double x2DC, double y1DC, double y2DC, double r) {
679 const double dy = ( yIsZeroAtTheTop ? - r : r );
680 ORDER_DC
681 our v_fillCircle (x2DC - r, y1DC + dy, r);
682 our v_fillCircle (x2DC - r, y2DC - dy, r);
683 our v_fillCircle (x1DC + r, y2DC - dy, r);
684 our v_fillCircle (x1DC + r, y1DC + dy, r);
685 our v_fillRectangle (x1DC, x2DC, y1DC + dy, y2DC - dy);
686 our v_fillRectangle (x1DC + r, x2DC - r, y1DC, y2DC);
687 }
688
689 /* Fifth level. */
690
691 #define wdx(x) ((x) * my scaleX + my deltaX)
692 #define wdy(y) ((y) * my scaleY + my deltaY)
693
694 void Graphics_polyline (Graphics me, integer numberOfPoints, const double *xWC, const double *yWC) { // base 0
695 if (my recording) {
696 op (POLYLINE, 1 + 2 * numberOfPoints);
697 put (numberOfPoints);
698 mput (numberOfPoints, & xWC [0])
699 mput (numberOfPoints, & yWC [0])
700 } else {
701 if (numberOfPoints < 2)
702 return;
703 double *xyDC;
704 try {
705 xyDC = Melder_malloc (double, 2 * numberOfPoints);
706 } catch (MelderError) {
707 /*
708 Out of memory: silently refuse to draw.
709 */
710 Melder_clearError ();
711 return;
712 }
713 for (integer i = 0; i < numberOfPoints; i ++) {
714 xyDC [i + i] = wdx (xWC [i]);
715 xyDC [i + i + 1] = wdy (yWC [i]);
716 }
717 my v_polyline (numberOfPoints, xyDC, false);
718 Melder_free (xyDC);
719 }
720 }
721
722 void Graphics_polyline_closed (Graphics me, integer numberOfPoints, const double *xWC, const double *yWC) { // base 0
723 if (my recording) {
724 op (POLYLINE_CLOSED, 1 + 2 * numberOfPoints);
725 put (numberOfPoints);
726 mput (numberOfPoints, & xWC [0])
727 mput (numberOfPoints, & yWC [0])
728 } else {
729 if (numberOfPoints < 1)
730 return;
731 double *xyDC;
732 try {
733 xyDC = Melder_malloc (double, 2 * numberOfPoints);
734 } catch (MelderError) {
735 /*
736 Out of memory: silently refuse to draw.
737 */
738 Melder_clearError ();
739 return;
740 }
741 for (integer i = 0; i < numberOfPoints; i ++) {
742 xyDC [i + i] = wdx (xWC [i]);
743 xyDC [i + i + 1] = wdy (yWC [i]);
744 }
745 my v_polyline (numberOfPoints, xyDC, true);
746 Melder_free (xyDC);
747 }
748 }
749
750 void Graphics_line (Graphics me, double x1WC, double y1WC, double x2WC, double y2WC) {
751 if (my recording) {
752 op (LINE, 4); put (x1WC); put (y1WC); put (x2WC); put (y2WC);
753 } else {
754 double xyDC [4];
755 trace (x1WC, U" ", y1WC, U" ", x2WC, U" ", y2WC);
756 xyDC [0] = wdx (x1WC);
757 xyDC [1] = wdy (y1WC);
758 xyDC [2] = wdx (x2WC);
759 xyDC [3] = wdy (y2WC);
760 my v_polyline (2, xyDC, false);
761 }
762 }
763
764 void Graphics_fillArea (Graphics me, integer numberOfPoints, double const *xWC, double const *yWC) {
765 if (my recording) {
766 op (FILL_AREA, 1 + 2 * numberOfPoints);
767 put (numberOfPoints);
768 mput (numberOfPoints, & xWC [0])
769 mput (numberOfPoints, & yWC [0])
770 } else {
771 if (numberOfPoints < 3)
772 return;
773 double *xyDC;
774 try {
775 xyDC = Melder_malloc (double, 2 * numberOfPoints);
776 } catch (MelderError) {
777 /*
778 Out of memory: silently refuse to draw.
779 */
780 Melder_clearError ();
781 return;
782 }
783 for (integer i = 0; i < numberOfPoints; i ++) {
784 xyDC [i + i] = wdx (xWC [i]);
785 xyDC [i + i + 1] = wdy (yWC [i]);
786 }
787 my v_fillArea (numberOfPoints, xyDC);
788 Melder_free (xyDC);
789 }
790 }
791
792 template <typename TYPE>
793 void Graphics_function_ (Graphics me, const TYPE yWC [], integer stride, integer ix1, integer ix2, double x1WC, double x2WC) {
794 const integer clipy1 = wdy (my d_y1WC), clipy2 = wdy (my d_y2WC);
795 const integer n = ix2 - ix1 + 1;
796 if (n <= 1 || my scaleX == 0.0)
797 return;
798 const double dx = (x2WC - x1WC) / (n - 1);
799 const double offsetX = x1WC - ix1 * dx;
800 /* xDC = wdx (offsetX + i * dx) */
801 const double translation = wdx (offsetX);
802 const double scale = dx * my scaleX;
803 const integer x1DC = translation + ix1 * scale;
804 const integer x2DC = translation + ix2 * scale;
805 if (n > (x2DC - x1DC + 1) * 2) { // optimize: draw one vertical line for each device x coordinate
806 const integer numberOfPixels = x2DC - x1DC + 1;
807 integer k = 0;
808 const integer numberOfPointsActuallyDrawn = numberOfPixels * 2;
809 TYPE lastMini;
810 if (numberOfPointsActuallyDrawn < 1)
811 return;
812 double *xyDC = Melder_malloc_f (double, 2 * numberOfPointsActuallyDrawn);
813 for (integer i = 0; i < numberOfPixels; i ++) {
814 integer jmin = ix1 + i / scale, jmax = ix1 + (i + 1) / scale;
815 if (jmin > ix2) jmin = ix2;
816 if (jmax > ix2) jmax = ix2;
817 TYPE mini = yWC [stride * jmin], maxi = mini;
818 for (integer j = jmin + 1; j <= jmax; j ++) { // one point overlap
819 TYPE value = yWC [stride * j];
820 if (value > maxi) maxi = value;
821 else if (value < mini) mini = value;
822 }
823 integer minDC = wdy (mini);
824 integer maxDC = wdy (maxi);
825 if (my yIsZeroAtTheTop) {
826 if (minDC > clipy1) minDC = clipy1;
827 if (maxDC > clipy1) maxDC = clipy1;
828 if (maxDC < clipy2) maxDC = clipy2;
829 if (minDC < clipy2) minDC = clipy2;
830 } else {
831 if (minDC < clipy1) minDC = clipy1;
832 if (maxDC < clipy1) maxDC = clipy1;
833 if (maxDC > clipy2) maxDC = clipy2;
834 if (minDC > clipy2) minDC = clipy2;
835 }
836 if (i == 0) {
837 if (yWC [stride * jmin] < yWC [stride * jmax]) {
838 xyDC [k ++] = x1DC;
839 xyDC [k ++] = minDC;
840 xyDC [k ++] = x1DC;
841 xyDC [k ++] = maxDC;
842 } else {
843 xyDC [k ++] = x1DC;
844 xyDC [k ++] = maxDC;
845 xyDC [k ++] = x1DC;
846 xyDC [k ++] = minDC;
847 }
848 } else if (minDC == xyDC [k - 1]) {
849 xyDC [k ++] = x1DC + i;
850 xyDC [k ++] = maxDC;
851 } else if (maxDC == xyDC [k - 1]) {
852 xyDC [k ++] = x1DC + i;
853 xyDC [k ++] = minDC;
854 } else if (mini > lastMini) {
855 xyDC [k ++] = x1DC + i;
856 xyDC [k ++] = minDC;
857 xyDC [k ++] = x1DC + i;
858 xyDC [k ++] = maxDC;
859 } else {
860 xyDC [k ++] = x1DC + i;
861 xyDC [k ++] = maxDC;
862 xyDC [k ++] = x1DC + i;
863 xyDC [k ++] = minDC;
864 }
865 lastMini = mini;
866 }
867 if (k > 1)
868 my v_polyline (k / 2, xyDC, false);
869 Melder_free (xyDC);
870 } else { // normal
871 double *xyDC = Melder_malloc_f (double, 2 * n);
872 for (integer i = 0; i < n; i ++) {
873 integer ix = ix1 + i;
874 integer value = wdy (yWC [stride * ix]);
875 xyDC [i + i] = translation + ix * scale;
876 if (my yIsZeroAtTheTop) {
877 if (FUNCTIONS_ARE_CLIPPED && value > clipy1) value = clipy1;
878 if (FUNCTIONS_ARE_CLIPPED && value < clipy2) value = clipy2;
879 } else {
880 if (FUNCTIONS_ARE_CLIPPED && value < clipy1) value = clipy1;
881 if (FUNCTIONS_ARE_CLIPPED && value > clipy2) value = clipy2;
882 }
883 xyDC [i + i + 1] = value;
884 }
885 my v_polyline (n, xyDC, false);
886 Melder_free (xyDC);
887 }
888 }
889
890 void Graphics_function (Graphics me, const double yWC [], integer ix1, integer ix2, double x1WC, double x2WC) {
891 if (my recording) {
892 const integer n = ix2 - ix1 + 1;
893 if (n >= 2) {
894 op (FUNCTION, 3 + n);
895 put (n);
896 put (x1WC);
897 put (x2WC);
898 mput (n, & yWC [ix1])
899 }
900 } else
901 Graphics_function_ <double> (me, yWC, 1, ix1, ix2, x1WC, x2WC);
902 }
903
904 void Graphics_function16 (Graphics me, const int16 yWC [], integer stride, integer ix1, integer ix2, double x1WC, double x2WC) {
905 if (my recording) {
906 Melder_crash (U"Graphics_function16: cannot be used during graphics recording.");
907 } else
908 Graphics_function_ <int16> (me, yWC, stride, ix1, ix2, x1WC, x2WC);
909 }
910
911 void Graphics_rectangle (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC) {
912 if (my recording) {
913 op (RECTANGLE, 4); put (x1WC); put (x2WC); put (y1WC); put (y2WC);
914 } else
915 my v_rectangle (wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC));
916 }
917
918 void Graphics_fillRectangle (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC) {
919 if (my recording) {
920 op (FILL_RECTANGLE, 4); put (x1WC); put (x2WC); put (y1WC); put (y2WC);
921 } else
922 my v_fillRectangle (wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC));
923 }
924
925 void Graphics_roundedRectangle (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC, double r_mm) {
926 if (my recording) {
927 op (ROUNDED_RECTANGLE, 5); put (x1WC); put (x2WC); put (y1WC); put (y2WC); put (r_mm);
928 } else
929 my v_roundedRectangle (wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC), r_mm * my resolution / 25.4);
930 }
931
932 void Graphics_fillRoundedRectangle (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC, double r_mm) {
933 if (my recording) {
934 op (FILL_ROUNDED_RECTANGLE, 5); put (x1WC); put (x2WC); put (y1WC); put (y2WC); put (r_mm);
935 } else
936 my v_fillRoundedRectangle (wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC), r_mm * my resolution / 25.4);
937 }
938
939 void Graphics_button (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC) {
940 if (my recording) {
941 op (BUTTON, 4); put (x1WC); put (x2WC); put (y1WC); put (y2WC);
942 } else
943 my v_button (wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC));
944 }
945
946 void Graphics_innerRectangle (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC) {
947 if (my recording) {
948 op (INNER_RECTANGLE, 4); put (x1WC); put (x2WC); put (y1WC); put (y2WC);
949 } else {
950 const double dy = ( my yIsZeroAtTheTop ? -1.0 : 1.0 );
951 my v_rectangle (wdx (x1WC) + 1.0, wdx (x2WC) - 1.0, wdy (y1WC) + dy, wdy (y2WC) - dy);
952 }
953 }
954
955 void Graphics_circle (Graphics me, double xWC, double yWC, double rWC) {
956 if (my recording) {
957 op (CIRCLE, 3); put (xWC); put (yWC); put (rWC);
958 } else
959 my v_circle (wdx (xWC), wdy (yWC), my scaleX * rWC);
960 }
961
962 void Graphics_circle_mm (Graphics me, double xWC, double yWC, double diameter) {
963 if (my recording) {
964 op (CIRCLE_MM, 3); put (xWC); put (yWC); put (diameter);
965 } else
966 my v_circle (wdx (xWC), wdy (yWC), 0.5 * diameter * my resolution / 25.4);
967 }
968
969 void Graphics_fillCircle (Graphics me, double xWC, double yWC, double rWC) {
970 if (my recording) {
971 op (FILL_CIRCLE, 3); put (xWC); put (yWC); put (rWC);
972 } else
973 my v_fillCircle (wdx (xWC), wdy (yWC), my scaleX * rWC);
974 }
975
976 void Graphics_fillCircle_mm (Graphics me, double xWC, double yWC, double diameter) {
977 if (my recording) {
978 op (FILL_CIRCLE_MM, 3); put (xWC); put (yWC); put (diameter);
979 } else
980 my v_fillCircle (wdx (xWC), wdy (yWC), 0.5 * diameter * my resolution / 25.4);
981 }
982
983 void Graphics_speckle (Graphics me, double xWC, double yWC) {
984 if (my recording) {
985 op (SPECKLE, 2); put (xWC); put (yWC);
986 } else
987 my v_fillCircle (wdx (xWC), wdy (yWC), 0.5 * my speckleSize * my resolution / 25.4);
988 }
989
990 void Graphics_rectangle_mm (Graphics me, double xWC, double yWC, double horSide, double vertSide) {
991 if (my recording) {
992 op (RECTANGLE_MM, 4); put (xWC); put (yWC); put (horSide); put (vertSide);
993 } else {
994 const double xDC = wdx (xWC), yDC = wdy (yWC);
995 const double halfHorSide = 0.5 * horSide * my resolution / 25.4;
996 const double halfVertSide = 0.5 * vertSide * my resolution / 25.4;
997 if (my yIsZeroAtTheTop) {
998 my v_rectangle (xDC - halfHorSide, xDC + halfHorSide, yDC + halfVertSide, yDC - halfVertSide);
999 } else {
1000 my v_rectangle (xDC - halfHorSide, xDC + halfHorSide, yDC - halfVertSide, yDC + halfVertSide);
1001 }
1002 }
1003 }
1004
1005 void Graphics_fillRectangle_mm (Graphics me, double xWC, double yWC, double horSide, double vertSide) {
1006 if (my recording) {
1007 op (FILL_RECTANGLE_MM, 4); put (xWC); put (yWC); put (horSide); put (vertSide);
1008 } else {
1009 const double xDC = wdx (xWC), yDC = wdy (yWC);
1010 const double halfHorSide = 0.5 * horSide * my resolution / 25.4;
1011 const double halfVertSide = 0.5 * vertSide * my resolution / 25.4;
1012 if (my yIsZeroAtTheTop) {
1013 my v_fillRectangle (xDC - halfHorSide, xDC + halfHorSide, yDC + halfVertSide, yDC - halfVertSide);
1014 } else {
1015 my v_fillRectangle (xDC - halfHorSide, xDC + halfHorSide, yDC - halfVertSide, yDC + halfVertSide);
1016 }
1017 }
1018 }
1019
1020 void Graphics_ellipse (Graphics me, double x1, double x2, double y1, double y2) {
1021 if (my recording) {
1022 op (ELLIPSE, 4); put (x1); put (x2); put (y1); put (y2);
1023 } else
1024 my v_ellipse (wdx (x1), wdx (x2), wdy (y1), wdy (y2));
1025 }
1026
1027 void Graphics_fillEllipse (Graphics me, double x1, double x2, double y1, double y2) {
1028 if (my recording) {
1029 op (FILL_ELLIPSE, 4); put (x1); put (x2); put (y1); put (y2);
1030 } else
1031 my v_fillEllipse (wdx (x1), wdx (x2), wdy (y1), wdy (y2));
1032 }
1033
1034 void Graphics_arc (Graphics me, double xWC, double yWC, double rWC, double fromAngle, double toAngle) {
1035 if (my recording) {
1036 op (ARC, 5); put (xWC); put (yWC); put (rWC); put (fromAngle); put (toAngle);
1037 } else
1038 my v_arc (wdx (xWC), wdy (yWC), my scaleX * rWC, fromAngle, toAngle);
1039 }
1040
1041 void Graphics_fillArc (Graphics me, double xWC, double yWC, double rWC, double fromAngle, double toAngle) {
1042 if (my recording) {
1043 op (FILL_ARC, 5); put (xWC); put (yWC); put (rWC); put (fromAngle); put (toAngle);
1044 } else
1045 my v_arc (wdx (xWC), wdy (yWC), my scaleX * rWC, fromAngle, toAngle); // NYI v_fillArc
1046 }
1047
1048 /* Arrows. */
1049
1050 void structGraphics :: v_arrowHead (double xDC, double yDC, double angle) {
1051 (void) xDC;
1052 (void) yDC;
1053 (void) angle;
1054 }
1055
1056 void structGraphicsScreen :: v_arrowHead (double xDC, double yDC, double angle) {
1057 #if cairo
1058 if (! our d_cairoGraphicsContext) return;
1059 double size = 10.0 * our resolution * our arrowSize / 75.0; // TODO: die 75 zou dat niet de scherm resolutie moeten worden?
1060 cairo_new_path (our d_cairoGraphicsContext);
1061 cairo_move_to (our d_cairoGraphicsContext, xDC + cos ((angle + 160.0) * NUMpi / 180.0) * size, yDC - sin ((angle + 160.0) * NUMpi / 180.0) * size);
1062 cairo_line_to (our d_cairoGraphicsContext, xDC, yDC);
1063 cairo_line_to (our d_cairoGraphicsContext, xDC + cos ((angle - 160.0) * NUMpi / 180.0) * size, yDC - sin ((angle - 160.0) * NUMpi / 180.0) * size);
1064 cairo_close_path (our d_cairoGraphicsContext);
1065 cairo_fill (our d_cairoGraphicsContext);
1066 #elif gdi
1067 double size = 10.0 * our resolution * our arrowSize / 72.0;
1068 MY_BRUSH
1069 BeginPath (our d_gdiGraphicsContext);
1070 MoveToEx (our d_gdiGraphicsContext, xDC + cos ((angle + 160.0) * NUMpi / 180.0) * size, yDC - sin ((angle + 160.0) * NUMpi / 180.0) * size, nullptr);
1071 LineTo (our d_gdiGraphicsContext, xDC, yDC);
1072 LineTo (our d_gdiGraphicsContext, xDC + cos ((angle - 160.0) * NUMpi / 180.0) * size, yDC - sin ((angle - 160.0) * NUMpi / 180.0) * size);
1073 EndPath (our d_gdiGraphicsContext);
1074 FillPath (our d_gdiGraphicsContext);
1075 DEFAULT
1076 #elif quartz
1077 quartzPrepareFill (this);
1078 //NSCAssert (our d_macGraphicsContext, @"nil context");
1079 CGContextSaveGState (our d_macGraphicsContext);
1080 CGContextBeginPath (our d_macGraphicsContext);
1081 CGContextTranslateCTM (our d_macGraphicsContext, xDC, yDC);
1082 CGContextRotateCTM (our d_macGraphicsContext, - angle * NUMpi / 180.0);
1083 CGContextMoveToPoint (our d_macGraphicsContext, 0.0, 0.0);
1084 double size = 10.0 * our resolution * our arrowSize / 72.0;
1085 double radius = our resolution * our arrowSize / 30.0;
1086 CGContextAddArc (our d_macGraphicsContext, - size, 0.0, radius, - NUMpi / 3.0, NUMpi / 3.0, 0);
1087 CGContextAddLineToPoint (our d_macGraphicsContext, 0.0, 0.0);
1088 CGContextFillPath (our d_macGraphicsContext);
1089 CGContextRestoreGState (our d_macGraphicsContext);
1090 #endif
1091 }
1092
1093 void structGraphicsPostscript :: v_arrowHead (double xDC, double yDC, double angle) {
1094 double length = resolution * arrowSize / 10.0, radius = resolution * arrowSize / 30.0;
1095 d_printf (d_file, "gsave %.7g %.7g translate %.7g rotate\n"
1096 "N 0 0 M %.7g 0 %.7g -60 60 arc closepath fill grestore\n", xDC, yDC, angle, - length, radius);
1097 }
1098
1099 void Graphics_arrow (Graphics me, double x1WC, double y1WC, double x2WC, double y2WC) {
1100 if (my recording) {
1101 op (ARROW, 4); put (x1WC); put (y1WC); put (x2WC); put (y2WC);
1102 } else {
1103 const double angle = (180.0 / NUMpi) * atan2 ((wdy (y2WC) - wdy (y1WC)) * (my yIsZeroAtTheTop ? -1.0 : 1.0), wdx (x2WC) - wdx (x1WC));
1104 const double size = my screen ? 10.0 * my resolution * my arrowSize / 72.0 : my resolution * my arrowSize / 10;
1105 double xyDC [4];
1106 xyDC [0] = wdx (x1WC);
1107 xyDC [1] = wdy (y1WC);
1108 xyDC [2] = wdx (x2WC) + (my screen ? 0.7 : 0.6) * cos ((angle - 180.0) * NUMpi / 180.0) * size;
1109 xyDC [3] = wdy (y2WC) + (my yIsZeroAtTheTop ? -1.0 : 1.0) * (my screen ? 0.7 : 0.6) * sin ((angle - 180.0) * NUMpi / 180.0) * size;
1110 my v_polyline (2, xyDC, false);
1111 my v_arrowHead (wdx (x2WC), wdy (y2WC), angle);
1112 }
1113 }
1114
1115 void Graphics_doubleArrow (Graphics me, double x1WC, double y1WC, double x2WC, double y2WC) {
1116 if (my recording) {
1117 op (DOUBLE_ARROW, 4); put (x1WC); put (y1WC); put (x2WC); put (y2WC);
1118 } else {
1119 const double angle = (180.0 / NUMpi) * atan2 ((wdy (y2WC) - wdy (y1WC)) * (my yIsZeroAtTheTop ? -1.0 : 1.0), wdx (x2WC) - wdx (x1WC));
1120 const double size = my screen ? 10.0 * my resolution * my arrowSize / 72.0 : my resolution * my arrowSize / 10.0;
1121 double xyDC [4];
1122 xyDC [0] = wdx (x1WC) + (my screen ? 0.7 : 0.6) * cos (angle * NUMpi / 180.0) * size;
1123 xyDC [1] = wdy (y1WC) + (my yIsZeroAtTheTop ? -1.0 : 1.0) * (my screen ? 0.7 : 0.6) * sin (angle * NUMpi / 180.0) * size;
1124 xyDC [2] = wdx (x2WC) + (my screen ? 0.7 : 0.6) * cos ((angle - 180) * NUMpi / 180.0) * size;
1125 xyDC [3] = wdy (y2WC) + (my yIsZeroAtTheTop ? -1.0 : 1.0) * (my screen ? 0.7 : 0.6) * sin ((angle - 180.0) * NUMpi / 180.0) * size;
1126 my v_polyline (2, xyDC, false);
1127 my v_arrowHead (wdx (x1WC), wdy (y1WC), angle + 180.0);
1128 //my v_polyline (2, xyDC);
1129 my v_arrowHead (wdx (x2WC), wdy (y2WC), angle);
1130 }
1131 }
1132
1133 void Graphics_arcArrow (Graphics me, double xWC, double yWC, double rWC,
1134 double fromAngle, double toAngle, int arrowAtStart, int arrowAtEnd)
1135 {
1136 if (my recording) {
1137 op (ARC_ARROW, 7); put (xWC); put (yWC); put (rWC);
1138 put (fromAngle); put (toAngle); put (arrowAtStart); put (arrowAtEnd);
1139 } else {
1140 my v_arc (wdx (xWC), wdy (yWC), my scaleX * rWC, fromAngle, toAngle);
1141 if (arrowAtStart)
1142 my v_arrowHead (
1143 wdx (xWC + rWC * cos (fromAngle * (NUMpi / 180.0))),
1144 wdy (yWC + rWC * sin (fromAngle * (NUMpi / 180.0))), fromAngle - 90.0
1145 );
1146 if (arrowAtEnd)
1147 my v_arrowHead (
1148 wdx (xWC + rWC * cos (toAngle * (NUMpi / 180.0))),
1149 wdy (yWC + rWC * sin (toAngle * (NUMpi / 180.0))), toAngle + 90.0
1150 );
1151 }
1152 }
1153
1154 /* Output attributes. */
1155
1156 void Graphics_setLineType (Graphics me, int lineType) {
1157 my lineType = lineType;
1158 if (my recording) { op (SET_LINE_TYPE, 1); put (lineType); }
1159 }
1160
1161 void Graphics_setLineWidth (Graphics me, double lineWidth) {
1162 my lineWidth = lineWidth;
1163 if (my recording) { op (SET_LINE_WIDTH, 1); put (lineWidth); }
1164 }
1165
1166 void Graphics_setArrowSize (Graphics me, double arrowSize) {
1167 my arrowSize = arrowSize;
1168 if (my recording) { op (SET_ARROW_SIZE, 1); put (arrowSize); }
1169 }
1170
1171 void Graphics_setSpeckleSize (Graphics me, double speckleSize) {
1172 my speckleSize = speckleSize;
1173 if (my recording) { op (SET_SPECKLE_SIZE, 1); put (speckleSize); }
1174 }
1175
1176 /* Inquiries. */
1177
1178 int Graphics_inqLineType (Graphics me) { return my lineType; }
1179 double Graphics_inqLineWidth (Graphics me) { return my lineWidth; }
1180 double Graphics_inqArrowSize (Graphics me) { return my arrowSize; }
1181 double Graphics_inqSpeckleSize (Graphics me) { return my speckleSize; }
1182
1183 /* End of file Graphics_linesAndAreas.cpp */