Debian Codegraph @

sys/Picture.cpp

raw
1 /* Picture.cpp
2 *
3 * Copyright (C) 1992-2022,2024,2025 Paul Boersma, 2008 Stefan de Konink, 2010 Franz Brauße
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 "melder.h"
20 #include "Gui.h"
21 #include "Printer.h"
22 #include "Picture.h"
23 #include "site.h"
24 #ifdef _WIN32
25 #include "GraphicsP.h"
26 #endif
27
28 Thing_implement (Picture, Thing, 0);
29
30 /*
31 The selection grid has a resolution of 1/2 inch.
32 */
33 constexpr integer NUMBER_OF_HORIZONTAL_SQUARES = integer (2.0 * Picture_WIDTH);
34 constexpr integer NUMBER_OF_VERTICAL_SQUARES = integer (2.0 * Picture_HEIGHT);
35
36 static void drawMarkers (Picture me)
37 {
38 /*
39 Vertical and horizontal lines every 3 inches.
40 */
41 #define YELLOW_GRID 3
42 /*
43 Fill the entire canvas with GC's background.
44 */
45 Graphics_setColour (my selectionGraphics.get(), Melder_WHITE);
46 Graphics_fillRectangle (my selectionGraphics.get(), 0, Picture_WIDTH, 0, Picture_HEIGHT);
47
48 /*
49 Draw yellow grid lines for coarse navigation.
50 */
51 Graphics_setColour (my selectionGraphics.get(), Melder_YELLOW);
52 for (int i = YELLOW_GRID; i < Picture_HEIGHT; i += YELLOW_GRID)
53 Graphics_line (my selectionGraphics.get(), 0, i, Picture_WIDTH, i); // horizontal line
54 for (int i = YELLOW_GRID; i < Picture_WIDTH; i += YELLOW_GRID)
55 Graphics_line (my selectionGraphics.get(), i, 0, i, Picture_HEIGHT); // vertical line
56
57 /*
58 Draw red ticks and numbers for feedback on viewport measurement.
59 */
60 Graphics_setColour (my selectionGraphics.get(), Melder_RED);
61 for (int i = 1; i < Picture_WIDTH; i ++) {
62 const double x = i;
63 Graphics_setTextAlignment (my selectionGraphics.get(), Graphics_CENTRE, Graphics_TOP);
64 Graphics_text (my selectionGraphics.get(), x, Picture_HEIGHT, i);
65 Graphics_setTextAlignment (my selectionGraphics.get(), Graphics_CENTRE, Graphics_BOTTOM);
66 Graphics_text (my selectionGraphics.get(), x, 0, i);
67 }
68 for (int i = 1; i < NUMBER_OF_HORIZONTAL_SQUARES; i ++) { // vertical ticks
69 const double x = 0.5 * i;
70 Graphics_line (my selectionGraphics.get(), x, Picture_HEIGHT - 0.04, x, Picture_HEIGHT);
71 Graphics_line (my selectionGraphics.get(), x, 0, x, 0.04);
72 }
73 for (int i = 1; i < Picture_HEIGHT; i ++) {
74 const double y = Picture_HEIGHT - i;
75 Graphics_setTextAlignment (my selectionGraphics.get(), Graphics_LEFT, Graphics_HALF);
76 Graphics_text (my selectionGraphics.get(), 0.04, y, i);
77 Graphics_setTextAlignment (my selectionGraphics.get(), Graphics_RIGHT, Graphics_HALF);
78 Graphics_text (my selectionGraphics.get(), Picture_WIDTH - 0.03, y, i);
79 }
80 for (int i = 1; i < NUMBER_OF_VERTICAL_SQUARES; i ++) { // horizontal ticks
81 const double y = Picture_HEIGHT - 0.5 * i;
82 Graphics_line (my selectionGraphics.get(), Picture_WIDTH - 0.04, y, Picture_WIDTH, y);
83 Graphics_line (my selectionGraphics.get(), 0, y, 0.04, y);
84 }
85 Graphics_setColour (my selectionGraphics.get(), Melder_BLACK);
86 }
87
88 static void drawSelection (Picture me) {
89 const double dy = Melder_clippedRight (2.8 * Graphics_inqFontSize (my graphics.get()) / 72.0,
90 0.4 * (my sely2 - my sely1));
91 const double dx = Melder_clippedRight (1.5 * dy,
92 0.4 * (my selx2 - my selx1));
93 Graphics_highlight2 (my selectionGraphics.get(),
94 my selx1, my selx2, my sely1, my sely2,
95 my selx1 + dx, my selx2 - dx, my sely1 + dy, my sely2 - dy
96 );
97 }
98
99 static void gui_drawingarea_cb_expose (Picture me, GuiDrawingArea_ExposeEvent event) {
100 Melder_assert (event -> widget);
101 drawMarkers (me);
102 Graphics_play (my graphics.get(), my graphics.get());
103 drawSelection (me);
104 }
105
106 static void gui_drawingarea_cb_mouse (Picture me, GuiDrawingArea_MouseEvent event) {
107 /*
108 Dragging the mouse selects an integer number of "blocks".
109 */
110 struct Block {
111 integer ix, iy;
112 };
113 static Block anchorBlock, previousBlock;
114 double xWC, yWC;
115 Graphics_DCtoWC (my selectionGraphics.get(), event -> x, event -> y, & xWC, & yWC);
116 const Block currentBlock {
117 Melder_clipped (1_integer, 1 + (integer) floor (xWC * NUMBER_OF_HORIZONTAL_SQUARES / Picture_WIDTH), NUMBER_OF_HORIZONTAL_SQUARES),
118 Melder_clipped (1_integer, NUMBER_OF_VERTICAL_SQUARES - (integer) floor (yWC * NUMBER_OF_VERTICAL_SQUARES / Picture_HEIGHT), NUMBER_OF_VERTICAL_SQUARES)
119 };
120 bool didBlockChange = false; // optimization: don't redraw if we stay in the same block
121 if (event -> isClick()) {
122 constexpr int INVALID_BLOCK_NUMBER = 0;
123 previousBlock = { INVALID_BLOCK_NUMBER, INVALID_BLOCK_NUMBER };
124 if (event -> shiftKeyPressed) {
125 const integer ix1 = Melder_clipped (1_integer, 1 + (integer) floor (my selx1 * NUMBER_OF_HORIZONTAL_SQUARES / Picture_WIDTH), NUMBER_OF_HORIZONTAL_SQUARES); // BUG not compatible with mouseSelectsInnerViewport
126 const integer ix2 = Melder_clipped (1_integer, (integer) floor (my selx2 * NUMBER_OF_HORIZONTAL_SQUARES / Picture_WIDTH), NUMBER_OF_HORIZONTAL_SQUARES);
127 const integer iy1 = Melder_clipped (1_integer, NUMBER_OF_VERTICAL_SQUARES + 1 - (integer) floor (my sely2 * NUMBER_OF_VERTICAL_SQUARES / Picture_HEIGHT), NUMBER_OF_VERTICAL_SQUARES);
128 const integer iy2 = Melder_clipped (1_integer, NUMBER_OF_VERTICAL_SQUARES - (integer) floor (my sely1 * NUMBER_OF_VERTICAL_SQUARES / Picture_HEIGHT), NUMBER_OF_VERTICAL_SQUARES);
129 anchorBlock = { currentBlock. ix < (ix1 + ix2) / 2 ? ix2 : ix1, currentBlock. iy < (iy1 + iy2) / 2 ? iy2 : iy1 };
130 } else {
131 anchorBlock = currentBlock;
132 }
133 didBlockChange = true;
134 } else if (event -> isDrag() || event -> isDrop()) {
135 didBlockChange = ( currentBlock. ix != previousBlock. ix || currentBlock. iy != previousBlock. iy );
136 }
137 if (didBlockChange) {
138 previousBlock = currentBlock;
139 const auto ix12 = std::minmax (currentBlock. ix, anchorBlock. ix);
140 const integer ix1 = ix12. first, ix2 = ix12. second;
141 const auto iy12 = std::minmax (currentBlock. iy, anchorBlock. iy);
142 const integer iy1 = iy12. first, iy2 = iy12. second;
143 double xmargin = 0.0, ymargin = 0.0;
144 if (my mouseSelectsInnerViewport) {
145 const double fontSize = Graphics_inqFontSize (my graphics.get());
146 xmargin = std::min (fontSize * 4.2 / 72.0, double (ix2 - ix1 + 1));
147 ymargin = std::min (fontSize * 2.8 / 72.0, double (iy2 - iy1 + 1));
148 }
149 Picture_setSelection (me,
150 0.5 * (ix1 - 1) - xmargin, 0.5 * ix2 + xmargin,
151 0.5 * (NUMBER_OF_VERTICAL_SQUARES - iy2) - ymargin, 0.5 * (NUMBER_OF_VERTICAL_SQUARES + 1 - iy1) + ymargin
152 );
153 Graphics_updateWs (my selectionGraphics.get());
154 }
155 if (event -> isDrop() && my selectionChangedCallback)
156 my selectionChangedCallback (me, my selectionChangedClosure,
157 my selx1, my selx2, my sely1, my sely2);
158 }
159
160 autoPicture Picture_create (GuiDrawingArea drawingArea, bool sensitive) {
161 try {
162 autoPicture me = Thing_new (Picture);
163 my drawingArea = drawingArea;
164 /*
165 The initial viewport is a rectangle 6 inches wide and 4 inches high.
166 */
167 my selx1 = 0.0;
168 my selx2 = 6.0;
169 my sely1 = Picture_HEIGHT - 4.0;
170 my sely2 = Picture_HEIGHT;
171 my sensitive = sensitive && drawingArea;
172 /*
173 Create a Graphics to directly draw in.
174 */
175 if (drawingArea) {
176 // the drawing area must have been realized; see manual at XtWindow
177 my graphics = Graphics_create_xmdrawingarea (my drawingArea);
178 GuiDrawingArea_setExposeCallback (my drawingArea, gui_drawingarea_cb_expose, me.get());
179 } else {
180 /*
181 Create a dummy Graphics.
182 It does have to be a GraphicsScreen, because it has to support Graphics_textWidth().
183 */
184 const integer dummyResolution = 600;
185 my graphics = Graphics_create_screen (nullptr, nullptr, dummyResolution);
186 Graphics_setWsViewport (my graphics.get(), 0.0, Picture_WIDTH * dummyResolution, 0.0, Picture_WIDTH * dummyResolution);
187 }
188 Graphics_setWsWindow (my graphics.get(), 0.0, Picture_WIDTH, 0.0, Picture_HEIGHT);
189 Graphics_setViewport (my graphics.get(), my selx1, my selx2, my sely1, my sely2);
190 if (my sensitive) {
191 my selectionGraphics = Graphics_create_xmdrawingarea (my drawingArea);
192 Graphics_setWindow (my selectionGraphics.get(), 0.0, Picture_WIDTH, 0.0, Picture_HEIGHT);
193 GuiDrawingArea_setMouseCallback (my drawingArea, gui_drawingarea_cb_mouse, me.get());
194 }
195 Graphics_startRecording (my graphics.get());
196 return me;
197 } catch (MelderError) {
198 Melder_throw (U"Picture not created.");
199 }
200 }
201
202 void Picture_setSelectionChangedCallback (Picture me,
203 void (*selectionChangedCallback) (Picture, void *, double, double, double, double),
204 void *selectionChangedClosure)
205 {
206 my selectionChangedCallback = selectionChangedCallback;
207 my selectionChangedClosure = selectionChangedClosure;
208 }
209
210 void Picture_setMouseSelectsInnerViewport (Picture me, int mouseSelectsInnerViewport) {
211 my mouseSelectsInnerViewport = mouseSelectsInnerViewport;
212 }
213
214 Graphics Picture_peekGraphics (Picture me) { return my graphics.get(); }
215
216 void Picture_erase (Picture me) {
217 Graphics_clearRecording (my graphics.get());
218 Graphics_updateWs (my graphics.get());
219 }
220
221 void Picture_writeToPraatPictureFile (Picture me, MelderFile file) {
222 try {
223 autofile f = Melder_fopen (file, "wb");
224 if (fprintf (f, "PraatPicture%04d", int (Picture_HEIGHT)) < 0)
225 Melder_throw (U"Write error.");
226 Graphics_writeRecordings (my graphics.get(), f);
227 f.close (file);
228 } catch (MelderError) {
229 Melder_throw (U"Cannot write Praat picture file ", file, U".");
230 }
231 }
232
233 void Picture_readFromPraatPictureFile (Picture me, MelderFile file) {
234 try {
235 autofile f = Melder_fopen (file, "rb");
236 char buffer [1 + 16];
237 integer n = uinteger_to_integer_a (fread (buffer, 1, 16, f));
238 buffer [16] = '\0';
239 if (n != 16)
240 Melder_throw (U"File too short.");
241 if (! strnequ (buffer, "PraatPicture", 12))
242 Melder_throw (U"This is not a Praat picture file.");
243 double filePictureHeight = undefined;
244 if (buffer [12] == 'F' && buffer [13] == 'i' && buffer [14] == 'l' && buffer [15] == 'e') {
245 filePictureHeight = 12.0;
246 } else {
247 Melder_require (
248 buffer [12] >= '0' && buffer [12] <= '9' &&
249 buffer [13] >= '0' && buffer [13] <= '9' &&
250 buffer [14] >= '0' && buffer [14] <= '9' &&
251 buffer [15] >= '0' && buffer [16] <= '9',
252 U"Incorrect header in Praat picture file."
253 );
254 filePictureHeight =
255 (buffer [12] - '0') * 1000 +
256 (buffer [13] - '0') * 100 +
257 (buffer [14] - '0') * 10 +
258 (buffer [15] - '0')
259 ;
260 }
261 fseek (f, 16, SEEK_SET);
262 Graphics_readRecordings (my graphics.get(), f, Picture_HEIGHT - filePictureHeight);
263 Graphics_updateWs (my graphics.get());
264 f.close (file);
265 } catch (MelderError) {
266 Melder_throw (U"Praat picture not read from file ", file);
267 }
268 }
269
270 #ifdef macintosh
271 static size_t appendBytes (void *info, const void *buffer, size_t count) {
272 CFDataAppendBytes ((CFMutableDataRef) info, (const UInt8 *) buffer, uinteger_to_integer_a (count));
273 return count;
274 }
275 void Picture_copyToClipboard (Picture me) {
276 /*
277 Find the clipboard and clear it.
278 */
279 PasteboardRef clipboard = nullptr;
280 PasteboardCreate (kPasteboardClipboard, & clipboard);
281 PasteboardClear (clipboard);
282 /*
283 Add a PDF flavour to the clipboard.
284 */
285 static CGDataConsumerCallbacks callbacks = { appendBytes, nullptr };
286 CFDataRef data = CFDataCreateMutable (kCFAllocatorDefault, 0);
287 CGDataConsumerRef consumer = CGDataConsumerCreate ((void *) data, & callbacks);
288 int resolution = 600;
289 CGRect rect = CGRectMake (0, 0, (my selx2 - my selx1) * resolution, (my sely1 - my sely2) * resolution);
290 CGContextRef context = CGPDFContextCreate (consumer, & rect, nullptr);
291 //my selx1 * RES, (Picture_HEIGHT - my sely2) * RES, my selx2 * RES, (Picture_HEIGHT - my sely1) * RES)
292 {// scope
293 autoGraphics pdfGraphics = Graphics_create_pdf (context, resolution, my selx1, my selx2, my sely1, my sely2);
294 Graphics_play (my graphics.get(), pdfGraphics.get());
295 }
296 PasteboardPutItemFlavor (clipboard, (PasteboardItemID) 1, kUTTypePDF, data, kPasteboardFlavorNoFlags);
297 CFRelease (data);
298 /*
299 Forget the clipboard.
300 */
301 CFRelease (clipboard);
302 }
303 #endif
304
305 #ifdef _WIN32
306 /* Windows pictures.
307 */
308 #define WIN_WIDTH 7.5
309 #define WIN_HEIGHT 11
310 static HENHMETAFILE copyToMetafile (Picture me) {
311 PRINTDLG defaultPrinter;
312 memset (& defaultPrinter, 0, sizeof (PRINTDLG));
313 defaultPrinter. lStructSize = sizeof (PRINTDLG);
314 defaultPrinter. Flags = PD_RETURNDEFAULT | PD_RETURNDC;
315 PrintDlg (& defaultPrinter);
316 RECT rect;
317 SetRect (& rect, my selx1 * 2540, (Picture_HEIGHT - my sely2) * 2540, my selx2 * 2540, (Picture_HEIGHT - my sely1) * 2540);
318 const HDC dc = CreateEnhMetaFile (defaultPrinter. hDC, nullptr, & rect, L"Praat\0");
319 if (! dc)
320 Melder_throw (U"Cannot create Windows metafile.");
321 const int resolution = GetDeviceCaps (dc, LOGPIXELSX); // Virtual PC: 360; Parallels Desktop: 600
322 //Melder_crash (U"resolution ", resolution);
323 if (Melder_debug == 6) {
324 DEVMODE *devMode = * (DEVMODE **) defaultPrinter. hDevMode;
325 MelderInfo_open ();
326 MelderInfo_writeLine (U"DEVICE CAPS:");
327 MelderInfo_writeLine (U"aspect x ", GetDeviceCaps (dc, ASPECTX),
328 U" y ", GetDeviceCaps (dc, ASPECTY));
329 MelderInfo_writeLine (U"res(pixels) hor ", GetDeviceCaps (dc, HORZRES),
330 U" vert ", GetDeviceCaps (dc, VERTRES));
331 MelderInfo_writeLine (U"size(mm) hor ", GetDeviceCaps (dc, HORZSIZE),
332 U" vert ", GetDeviceCaps (dc, VERTSIZE));
333 MelderInfo_writeLine (U"pixels/inch hor ", GetDeviceCaps (dc, LOGPIXELSX),
334 U" vert ", GetDeviceCaps (dc, LOGPIXELSY));
335 MelderInfo_writeLine (U"physicalOffset(pixels) hor ", GetDeviceCaps (dc, PHYSICALOFFSETX),
336 U" vert ", GetDeviceCaps (dc, PHYSICALOFFSETY));
337 MelderInfo_writeLine (U"PRINTER:");
338 MelderInfo_writeLine (U"dmFields ", devMode -> dmFields);
339 if (devMode -> dmFields & DM_YRESOLUTION)
340 MelderInfo_writeLine (U"y resolution ", devMode -> dmYResolution);
341 if (devMode -> dmFields & DM_PRINTQUALITY)
342 MelderInfo_writeLine (U"print quality ", devMode -> dmPrintQuality);
343 if (devMode -> dmFields & DM_PAPERWIDTH)
344 MelderInfo_writeLine (U"paper width ", devMode -> dmPaperWidth);
345 if (devMode -> dmFields & DM_PAPERLENGTH)
346 MelderInfo_writeLine (U"paper length ", devMode -> dmPaperLength);
347 if (devMode -> dmFields & DM_PAPERSIZE)
348 MelderInfo_writeLine (U"paper size ", devMode -> dmPaperSize);
349 if (devMode -> dmFields & DM_ORIENTATION)
350 MelderInfo_writeLine (U"orientation ", devMode -> dmOrientation);
351 MelderInfo_close ();
352 }
353 autoGraphics pictGraphics = Graphics_create_screen ((void *) dc, nullptr, resolution);
354 Graphics_setWsViewport (pictGraphics.get(), 0, WIN_WIDTH * resolution, 0, WIN_HEIGHT * resolution);
355 Graphics_setWsWindow (pictGraphics.get(), 0.0, WIN_WIDTH, Picture_HEIGHT - WIN_HEIGHT, Picture_HEIGHT);
356 Graphics_play (my graphics.get(), pictGraphics.get());
357 HENHMETAFILE metafile = CloseEnhMetaFile (dc);
358 return metafile;
359 }
360 void Picture_copyToClipboard (Picture me) {
361 try {
362 HENHMETAFILE metafile = copyToMetafile (me);
363 OpenClipboard (((GraphicsScreen) my graphics.get()) -> d_winWindow);
364 EmptyClipboard ();
365 SetClipboardData (CF_ENHMETAFILE, metafile);
366 CloseClipboard ();
367 /*
368 We should NOT call DeleteEnhMetaFile,
369 because the clipboard becomes the owner of this global memory object.
370 */
371 } catch (MelderError) {
372 Melder_throw (U"Picture not copied to clipboard.");
373 }
374 }
375 void Picture_writeToWindowsMetafile (Picture me, MelderFile file) {
376 try {
377 HENHMETAFILE metafile = copyToMetafile (me);
378 MelderFile_delete (file); // overwrite any existing file with the same name
379 DeleteEnhMetaFile (CopyEnhMetaFile (metafile, MelderFile_peekPathW (file)));
380 DeleteEnhMetaFile (metafile);
381 } catch (MelderError) {
382 Melder_throw (U"Picture not written to Windows metafile ", file);
383 }
384 }
385 #endif
386
387 void Picture_writeToEpsFile (Picture me, MelderFile file, bool includeFonts, bool useSilipaPS) {
388 try {
389 MelderFile_delete (file); // to kill resources as well (fopen only kills data fork)
390 // BUG: no message if file cannot be deleted (e.g. because still open by Microsoft Word 2001 after reading)
391
392 {// scope
393 autoGraphics postscriptGraphics = Graphics_create_epsfile (file, 600, thePrinter. spots,
394 my selx1, my selx2, my sely1, my sely2, includeFonts, useSilipaPS);
395 Graphics_play (my graphics.get(), postscriptGraphics.get());
396 }
397 } catch (MelderError) {
398 Melder_throw (U"Picture not written to EPS file ", file);
399 }
400 }
401
402 void Picture_writeToPdfFile (Picture me, MelderFile file) {
403 try {
404 autoGraphics pdfGraphics = Graphics_create_pdffile (file, 300, my selx1, my selx2, my sely1, my sely2);
405 Graphics_play (my graphics.get(), pdfGraphics.get());
406 } catch (MelderError) {
407 Melder_throw (U"Picture not written to PDF file ", file, U".");
408 }
409 }
410
411 void Picture_writeToPngFile_300 (Picture me, MelderFile file) {
412 try {
413 autoGraphics pngGraphics = Graphics_create_pngfile (file, 300, my selx1, my selx2, my sely1, my sely2);
414 Graphics_play (my graphics.get(), pngGraphics.get());
415 } catch (MelderError) {
416 Melder_throw (U"Picture not written to PNG file ", file, U".");
417 }
418 }
419
420 void Picture_writeToPngFile_600 (Picture me, MelderFile file) {
421 try {
422 autoGraphics pngGraphics = Graphics_create_pngfile (file, 600, my selx1, my selx2, my sely1, my sely2);
423 Graphics_play (my graphics.get(), pngGraphics.get());
424 } catch (MelderError) {
425 Melder_throw (U"Picture not written to PNG file ", file, U".");
426 }
427 }
428
429 static void print (void *void_me, Graphics printer) {
430 iam (Picture);
431 Graphics_play (my graphics.get(), printer);
432 }
433
434 void Picture_print (Picture me) {
435 try {
436 Printer_print (print, me);
437 } catch (MelderError) {
438 Melder_flushError (U"Picture not printed.");
439 }
440 }
441
442 void Picture_setSelection
443 (Picture me, double x1NDC, double x2NDC, double y1NDC, double y2NDC)
444 {
445 if (my drawingArea)
446 Melder_assert (my drawingArea -> d_widget);
447 my selx1 = x1NDC;
448 my selx2 = x2NDC;
449 my sely1 = y1NDC;
450 my sely2 = y2NDC;
451 }
452
453 /* End of file Picture.cpp */