| 1 |
/* GraphicsScreen.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-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 |
#include "Printer.h" |
| 21 |
#include "GuiP.h" |
| 22 |
|
| 23 |
#if cairo |
| 24 |
#include <cairo/cairo-pdf.h> |
| 25 |
static bool _GraphicsCairo_tryToInitializePango () { |
| 26 |
return _GraphicsLin_tryToInitializeFonts (); |
| 27 |
} |
| 28 |
#elif gdi |
| 29 |
//#include "winport_on.h" |
| 30 |
#include <gdiplus.h> |
| 31 |
//#include "winport_off.h" |
| 32 |
//using namespace Gdiplus; |
| 33 |
static bool _GraphicsWindows_tryToInitializeGdiPlus () { |
| 34 |
Gdiplus::GdiplusStartupInput gdiplusStartupInput; |
| 35 |
ULONG_PTR gdiplusToken; |
| 36 |
GdiplusStartup (& gdiplusToken, & gdiplusStartupInput, nullptr); |
| 37 |
return true; |
| 38 |
} |
| 39 |
#elif quartz |
| 40 |
#include "macport_on.h" |
| 41 |
static bool _GraphicsMacintosh_tryToInitializeQuartz () { |
| 42 |
return _GraphicsMac_tryToInitializeFonts (); |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
Thing_implement (GraphicsScreen, Graphics, 0); |
| 47 |
|
| 48 |
void structGraphicsScreen :: v9_destroy () noexcept { |
| 49 |
#if cairo |
| 50 |
if (d_cairoGraphicsContext) { |
| 51 |
cairo_destroy (d_cairoGraphicsContext); |
| 52 |
d_cairoGraphicsContext = nullptr; |
| 53 |
} |
| 54 |
if (d_cairoSurface) { |
| 55 |
cairo_surface_flush (d_cairoSurface); |
| 56 |
if (d_isPng) { |
| 57 |
#if 1 |
| 58 |
cairo_surface_write_to_png (d_cairoSurface, MelderFile_peekPath8 (& d_file)); |
| 59 |
#else |
| 60 |
unsigned char *bitmap = cairo_image_surface_get_data (my d_cairoSurface); // peeking into the internal bits |
| 61 |
// copy bitmap to PNG structure created with the PNG library |
| 62 |
// save the PNG structure to a file |
| 63 |
#endif |
| 64 |
} |
| 65 |
cairo_surface_destroy (d_cairoSurface); |
| 66 |
} |
| 67 |
#elif gdi |
| 68 |
if (d_gdiGraphicsContext) { |
| 69 |
SelectPen (d_gdiGraphicsContext, GetStockPen (BLACK_PEN)); |
| 70 |
SelectBrush (d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); |
| 71 |
} |
| 72 |
if (d_winPen) { |
| 73 |
DeleteObject (d_winPen); |
| 74 |
d_winPen = nullptr; |
| 75 |
} |
| 76 |
if (d_winBrush) { |
| 77 |
DeleteObject (d_winBrush); |
| 78 |
d_winBrush = nullptr; |
| 79 |
} |
| 80 |
if (d_isPng && d_gdiBitmap) { |
| 81 |
trace (U"saving the filled bitmap to a PNG file"); |
| 82 |
/* |
| 83 |
Deselect the bitmap from the device context (otherwise GetDIBits won't work). |
| 84 |
*/ |
| 85 |
//SelectBitmap (d_gdiGraphicsContext, nullptr); |
| 86 |
//SelectBitmap (d_gdiGraphicsContext, CreateCompatibleBitmap (nullptr, 1, 1)); |
| 87 |
|
| 88 |
BITMAP bitmap; |
| 89 |
GetObject (d_gdiBitmap, sizeof (BITMAP), & bitmap); |
| 90 |
int width = bitmap. bmWidth, height = bitmap. bmHeight; |
| 91 |
//int width = 3600, height = 3600; |
| 92 |
trace (U"width ", width, U", height ", height); |
| 93 |
|
| 94 |
/* |
| 95 |
Get the bits from the HBITMAP; |
| 96 |
*/ |
| 97 |
struct { BITMAPINFOHEADER header; } bitmapInfo; |
| 98 |
bitmapInfo. header.biSize = sizeof (BITMAPINFOHEADER); |
| 99 |
bitmapInfo. header.biWidth = width; |
| 100 |
bitmapInfo. header.biHeight = height; |
| 101 |
bitmapInfo. header.biPlanes = 1; |
| 102 |
bitmapInfo. header.biBitCount = 32; |
| 103 |
bitmapInfo. header.biCompression = BI_RGB; |
| 104 |
bitmapInfo. header.biSizeImage = 0; |
| 105 |
bitmapInfo. header.biXPelsPerMeter = 0; |
| 106 |
bitmapInfo. header.biYPelsPerMeter = 0; |
| 107 |
bitmapInfo. header.biClrUsed = 0; |
| 108 |
bitmapInfo. header.biClrImportant = 0; |
| 109 |
unsigned char *bits = Melder_calloc (unsigned char, 4 * width * height); |
| 110 |
//HANDLE handle = GlobalAlloc (GHND, 4 * width * height); |
| 111 |
//unsigned char *bits = (unsigned char *) GlobalLock (handle); |
| 112 |
int numberOfLinesScanned = GetDIBits (GetDC (nullptr), d_gdiBitmap, 0, height, bits, (BITMAPINFO *) & bitmapInfo, DIB_RGB_COLORS); |
| 113 |
trace (numberOfLinesScanned, U" lines scanned"); |
| 114 |
|
| 115 |
trace (U"creating a savable bitmap"); |
| 116 |
|
| 117 |
//Gdiplus::Bitmap gdiplusBitmap (width, height, PixelFormat32bppARGB); |
| 118 |
Gdiplus::Bitmap gdiplusBitmap ((BITMAPINFO *) & bitmapInfo, bits); |
| 119 |
gdiplusBitmap. SetResolution (resolution, resolution); |
| 120 |
|
| 121 |
trace (U"copying the device-independent bits to the savable bitmap."); |
| 122 |
|
| 123 |
/* |
| 124 |
for (integer irow = 1; irow <= height; irow ++) { |
| 125 |
for (integer icol = 1; icol <= width; icol ++) { |
| 126 |
unsigned char blue = *bits ++, green = *bits ++, red = *bits ++, alpha = 255 - *bits ++; |
| 127 |
Gdiplus::Color gdiplusColour (alpha, red, green, blue); |
| 128 |
gdiplusBitmap. SetPixel (icol - 1, height - irow, gdiplusColour); |
| 129 |
} |
| 130 |
} |
| 131 |
*/ |
| 132 |
|
| 133 |
trace (U"saving"); |
| 134 |
|
| 135 |
UINT numberOfImageEncoders, sizeOfImageEncoderArray; |
| 136 |
Gdiplus::GetImageEncodersSize (& numberOfImageEncoders, & sizeOfImageEncoderArray); |
| 137 |
Gdiplus::ImageCodecInfo *imageEncoderInfos = Melder_malloc (Gdiplus::ImageCodecInfo, sizeOfImageEncoderArray); |
| 138 |
Gdiplus::GetImageEncoders (numberOfImageEncoders, sizeOfImageEncoderArray, imageEncoderInfos); |
| 139 |
for (int iencoder = 0; iencoder < numberOfImageEncoders; iencoder ++) { |
| 140 |
if (! wcscmp (imageEncoderInfos [iencoder]. MimeType, L"image/png")) { |
| 141 |
gdiplusBitmap. Save (MelderFile_peekPathW (& our d_file), |
| 142 |
& imageEncoderInfos [iencoder]. Clsid, nullptr); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
trace (U"cleaning up"); |
| 147 |
|
| 148 |
Melder_free (imageEncoderInfos); |
| 149 |
//bits -= 4 * width * height; |
| 150 |
Melder_free (bits); |
| 151 |
//GlobalUnlock (handle); |
| 152 |
//GlobalFree (handle); |
| 153 |
DeleteObject (d_gdiBitmap); |
| 154 |
Melder_assert (d_gdiGraphicsContext); |
| 155 |
DeleteDC (d_gdiGraphicsContext); // this was a memory leak before 5.3.83 |
| 156 |
} |
| 157 |
/* |
| 158 |
No ReleaseDC here, because we have not created it ourselves, |
| 159 |
not even with GetDC. Is this a BUG? |
| 160 |
*/ |
| 161 |
d_gdiGraphicsContext = nullptr; |
| 162 |
#elif quartz |
| 163 |
if (! d_macView && ! d_isPng && d_macGraphicsContext) { |
| 164 |
CGContextEndPage (d_macGraphicsContext); |
| 165 |
CGContextRelease (d_macGraphicsContext); |
| 166 |
} |
| 167 |
if (d_isPng && d_macGraphicsContext) { |
| 168 |
/* |
| 169 |
Turn the offscreen bitmap into an image. |
| 170 |
*/ |
| 171 |
CGImageRef image = CGBitmapContextCreateImage (d_macGraphicsContext); |
| 172 |
Melder_assert (image); |
| 173 |
//CGContextRelease (d_macGraphicsContext); |
| 174 |
/* |
| 175 |
Create a dictionary with resolution properties. |
| 176 |
*/ |
| 177 |
CFTypeRef keys [2], values [2]; |
| 178 |
keys [0] = kCGImagePropertyDPIWidth; |
| 179 |
keys [1] = kCGImagePropertyDPIHeight; |
| 180 |
float resolution_float = resolution; |
| 181 |
values [1] = values [0] = CFNumberCreate (nullptr, kCFNumberFloatType, & resolution_float); |
| 182 |
CFDictionaryRef properties = CFDictionaryCreate (nullptr, |
| 183 |
(const void **) keys, (const void **) values, 2, |
| 184 |
& kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); |
| 185 |
Melder_assert (properties); |
| 186 |
|
| 187 |
CFURLRef url = CFURLCreateWithFileSystemPath (nullptr, |
| 188 |
(CFStringRef) MelderFile_peekPathCfstring (& d_file), kCFURLPOSIXPathStyle, false); |
| 189 |
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL (url, kUTTypePNG, 1, nullptr); |
| 190 |
if (imageDestination) { |
| 191 |
CGImageDestinationAddImage (imageDestination, image, properties); |
| 192 |
CGImageDestinationFinalize (imageDestination); |
| 193 |
CFRelease (imageDestination); |
| 194 |
} |
| 195 |
CGImageRelease (image); |
| 196 |
CFRelease (properties); |
| 197 |
CFRelease (url); |
| 198 |
} |
| 199 |
Melder_free (d_bits); |
| 200 |
#endif |
| 201 |
trace (U"destroying parent"); |
| 202 |
GraphicsScreen_Parent :: v9_destroy (); |
| 203 |
trace (U"exit"); |
| 204 |
} |
| 205 |
|
| 206 |
void structGraphicsScreen :: v_clearWs () { |
| 207 |
#if cairo && gtk |
| 208 |
GdkRectangle rect; |
| 209 |
if (our d_x1DC < our d_x2DC) { |
| 210 |
rect.x = our d_x1DC; |
| 211 |
rect.width = our d_x2DC - our d_x1DC; |
| 212 |
} else { |
| 213 |
rect.x = our d_x2DC; |
| 214 |
rect.width = our d_x1DC - our d_x2DC; |
| 215 |
} |
| 216 |
if (our d_y1DC < our d_y2DC) { |
| 217 |
rect.y = our d_y1DC; |
| 218 |
rect.height = our d_y2DC - our d_y1DC; |
| 219 |
} else { |
| 220 |
rect.y = our d_y2DC; |
| 221 |
rect.height = our d_y1DC - our d_y2DC; |
| 222 |
} |
| 223 |
if (our d_cairoGraphicsContext) { |
| 224 |
cairo_set_source_rgb (our d_cairoGraphicsContext, 1.0, 1.0, 1.0); |
| 225 |
cairo_rectangle (our d_cairoGraphicsContext, rect.x, rect.y, rect.width, rect.height); |
| 226 |
cairo_fill (our d_cairoGraphicsContext); |
| 227 |
cairo_set_source_rgb (our d_cairoGraphicsContext, 0.0, 0.0, 0.0); |
| 228 |
} |
| 229 |
#elif gdi |
| 230 |
RECT rect; |
| 231 |
rect. left = rect. top = 0; |
| 232 |
rect. right = d_x2DC - d_x1DC; |
| 233 |
rect. bottom = d_y2DC - d_y1DC; |
| 234 |
FillRect (our d_gdiGraphicsContext, & rect, GetStockBrush (WHITE_BRUSH)); |
| 235 |
/*if (our d_winWindow) SendMessage (our d_winWindow, WM_ERASEBKGND, (WPARAM) our d_gdiGraphicsContext, 0);*/ |
| 236 |
#elif quartz |
| 237 |
if (! our d_drawingArea) { |
| 238 |
Melder_assert (!! our d_macGraphicsContext); |
| 239 |
CGContextSetAlpha (our d_macGraphicsContext, 1.0); |
| 240 |
CGContextSetRGBFillColor (our d_macGraphicsContext, 1.0, 1.0, 1.0, 1.0); |
| 241 |
CGContextFillRect (d_macGraphicsContext, CGRectMake (our d_x1DC, our d_y2DC, our d_x2DC - our d_x1DC, our d_y1DC - our d_y2DC)); |
| 242 |
return; |
| 243 |
} |
| 244 |
GuiCocoaDrawingArea *cocoaDrawingArea = (GuiCocoaDrawingArea *) our d_drawingArea -> d_widget; |
| 245 |
if (cocoaDrawingArea && ! [cocoaDrawingArea isHiddenOrHasHiddenAncestor]) { // can be called at destruction time |
| 246 |
Melder_assert (!! our d_macGraphicsContext); |
| 247 |
CGContextSetAlpha (our d_macGraphicsContext, 1.0); |
| 248 |
CGContextSetRGBFillColor (our d_macGraphicsContext, 1.0, 1.0, 1.0, 1.0); |
| 249 |
CGContextFillRect (d_macGraphicsContext, CGRectMake (our d_x1DC, our d_y2DC, our d_x2DC - our d_x1DC, our d_y1DC - our d_y2DC)); |
| 250 |
} |
| 251 |
#endif |
| 252 |
} |
| 253 |
|
| 254 |
void Graphics_clearWs (Graphics me) { |
| 255 |
if (my recording) { |
| 256 |
op (CLEAR_WS, 0); |
| 257 |
} else |
| 258 |
my v_clearWs (); |
| 259 |
} |
| 260 |
|
| 261 |
void structGraphicsScreen :: v_updateWs () { |
| 262 |
/* |
| 263 |
A function that invalidates the graphics. |
| 264 |
This function is typically called by the owner of the drawing area |
| 265 |
whenever the data to be displayed in the drawing area has changed; |
| 266 |
the idea is to generate an expose event to which the drawing area will |
| 267 |
respond by redrawing its contents from the (changed) data. |
| 268 |
(last checked 2020-07-12) |
| 269 |
*/ |
| 270 |
if (! our d_drawingArea) |
| 271 |
return; |
| 272 |
#if cairo && gtk |
| 273 |
//GdkWindow *window = gtk_widget_get_parent_window (GTK_WIDGET (our d_drawingArea -> d_widget)); |
| 274 |
GdkRectangle rect; |
| 275 |
|
| 276 |
if (our d_x1DC < our d_x2DC) { |
| 277 |
rect.x = our d_x1DC; |
| 278 |
rect.width = our d_x2DC - our d_x1DC; |
| 279 |
} else { |
| 280 |
rect.x = our d_x2DC; |
| 281 |
rect.width = our d_x1DC - our d_x2DC; |
| 282 |
} |
| 283 |
|
| 284 |
if (our d_y1DC < our d_y2DC) { |
| 285 |
rect.y = our d_y1DC; |
| 286 |
rect.height = our d_y2DC - our d_y1DC; |
| 287 |
} else { |
| 288 |
rect.y = our d_y2DC; |
| 289 |
rect.height = our d_y1DC - our d_y2DC; |
| 290 |
} |
| 291 |
//gdk_window_invalidate_rect (our d_window, & rect, true); |
| 292 |
gtk_widget_queue_draw_area (GTK_WIDGET (our d_drawingArea -> d_widget), rect.x, rect.y, rect.width, rect.height); |
| 293 |
//gdk_window_invalidate_rect (our d_window, nullptr, true); |
| 294 |
//gdk_window_process_updates (our d_window, true); // TODO: this one seems to be for draining instead |
| 295 |
#elif gdi |
| 296 |
//clear (this); // lll |
| 297 |
if (our d_winWindow) |
| 298 |
InvalidateRect (our d_winWindow, nullptr, true); |
| 299 |
#elif quartz |
| 300 |
NSView *view = our d_macView; |
| 301 |
Melder_assert (!! view); |
| 302 |
NSRect rect; |
| 303 |
if (our d_x1DC < our d_x2DC) { |
| 304 |
rect.origin.x = our d_x1DC; |
| 305 |
rect.size.width = our d_x2DC - our d_x1DC; |
| 306 |
} else { |
| 307 |
rect.origin.x = our d_x2DC; |
| 308 |
rect.size.width = our d_x1DC - our d_x2DC; |
| 309 |
} |
| 310 |
if (our d_y1DC < our d_y2DC) { |
| 311 |
rect.origin.y = our d_y1DC; |
| 312 |
rect.size.height = our d_y2DC - our d_y1DC; |
| 313 |
} else { |
| 314 |
rect.origin.y = our d_y2DC; |
| 315 |
rect.size.height = our d_y1DC - our d_y2DC; |
| 316 |
} |
| 317 |
//[view setNeedsDisplayInRect: rect]; |
| 318 |
[view setNeedsDisplay: YES]; |
| 319 |
#endif |
| 320 |
} |
| 321 |
|
| 322 |
void Graphics_updateWs (Graphics me) { |
| 323 |
if (me) |
| 324 |
my v_updateWs (); |
| 325 |
} |
| 326 |
|
| 327 |
void Graphics_beginMovieFrame (Graphics any, MelderColour *p_colour) { |
| 328 |
if (any -> classInfo == classGraphicsScreen) { |
| 329 |
GraphicsScreen me = (GraphicsScreen) any; |
| 330 |
Graphics_startRecording (me); |
| 331 |
if (p_colour) { |
| 332 |
Graphics_clearRecording (me); |
| 333 |
Graphics_setViewport (me, 0.0, 1.0, 0.0, 1.0); |
| 334 |
Graphics_setColour (me, *p_colour); |
| 335 |
Graphics_setWindow (me, 0.0, 1.0, 0.0, 1.0); |
| 336 |
Graphics_fillRectangle (me, 0.0, 1.0, 0.0, 1.0); |
| 337 |
Graphics_setColour (me, Melder_BLACK); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void Graphics_endMovieFrame (Graphics any, double frameDuration) { |
| 343 |
if (any -> classInfo == classGraphicsScreen) { |
| 344 |
GraphicsScreen me = (GraphicsScreen) any; |
| 345 |
Graphics_stopRecording (me); |
| 346 |
my v_updateWs (); |
| 347 |
Melder_sleep (frameDuration); |
| 348 |
GuiShell_drain (my d_drawingArea -> d_shell, false, true); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
static int GraphicsScreen_init (GraphicsScreen me, void *voidDisplay, void *voidWindow) { |
| 353 |
|
| 354 |
/* Fill in new members. */ |
| 355 |
|
| 356 |
#if cairo && gtk |
| 357 |
my d_display = (GdkDisplay *) gdk_display_get_default (); |
| 358 |
_GraphicsScreen_text_init (me); |
| 359 |
if (voidDisplay) |
| 360 |
my d_window = gtk_widget_get_window (GTK_WIDGET (voidDisplay)); |
| 361 |
my d_cairoGraphicsContext = nullptr; // will be created and destroyed at expose time or during Graphics_textWidth(); 2020-11-11 |
| 362 |
#elif gdi |
| 363 |
if (my printer) { |
| 364 |
my d_gdiGraphicsContext = (HDC) voidWindow; |
| 365 |
} else if (voidDisplay) { |
| 366 |
my d_gdiGraphicsContext = (HDC) voidDisplay; |
| 367 |
my metafile = true; |
| 368 |
} else { |
| 369 |
my d_winWindow = (HWND) voidWindow; |
| 370 |
my d_gdiGraphicsContext = GetDC (my d_winWindow); // window must have a constant display context; see XtInitialize () |
| 371 |
} |
| 372 |
Melder_assert (my d_gdiGraphicsContext); |
| 373 |
SetBkMode (my d_gdiGraphicsContext, TRANSPARENT); // not the default! text should not be drawn against a white background |
| 374 |
/* |
| 375 |
Create pens and brushes. |
| 376 |
*/ |
| 377 |
my d_winPen = CreatePen (PS_SOLID, 0, RGB (0, 0, 0)); |
| 378 |
my d_winBrush = CreateSolidBrush (RGB (0, 0, 0)); |
| 379 |
SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); |
| 380 |
SetTextAlign (my d_gdiGraphicsContext, TA_LEFT | TA_BASELINE | TA_NOUPDATECP); // baseline is not the default! |
| 381 |
_GraphicsScreen_text_init (me); |
| 382 |
#elif quartz |
| 383 |
(void) voidDisplay; |
| 384 |
if (my printer) { |
| 385 |
my d_macView = (NSView *) voidWindow; // in case we do view-based printing |
| 386 |
//my d_macGraphicsContext = (CGContextRef) voidWindow; // in case we do context-based printing |
| 387 |
} else { |
| 388 |
my d_macView = (NSView *) voidWindow; |
| 389 |
my d_macGraphicsContext = nullptr; // will be retrieved and nullified at expose time |
| 390 |
} |
| 391 |
my d_depth = ( my resolution > 150 ? 1 : 8 ); // BUG: replace by true depth (1=black/white) |
| 392 |
_GraphicsScreen_text_init (me); |
| 393 |
#endif |
| 394 |
return 1; |
| 395 |
} |
| 396 |
|
| 397 |
autoGraphics Graphics_create_screen (void *display, void *window, int resolution) { |
| 398 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 399 |
my screen = true; |
| 400 |
#if cairo |
| 401 |
_GraphicsCairo_tryToInitializePango (); |
| 402 |
#elif gdi |
| 403 |
my d_useGdiplus = _GraphicsWindows_tryToInitializeGdiPlus (); |
| 404 |
#elif quartz |
| 405 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 406 |
#endif |
| 407 |
my yIsZeroAtTheTop = true; |
| 408 |
Graphics_init (me.get(), resolution); |
| 409 |
Graphics_setWsViewport (me.get(), 0.0, 100.0, 0.0, 100.0); |
| 410 |
GraphicsScreen_init (me.get(), display, window); |
| 411 |
return me.move(); |
| 412 |
} |
| 413 |
|
| 414 |
autoGraphics Graphics_create_screenPrinter (void *display, void *window) { |
| 415 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 416 |
my screen = true; |
| 417 |
my yIsZeroAtTheTop = true; |
| 418 |
my printer = true; |
| 419 |
#if cairo |
| 420 |
_GraphicsCairo_tryToInitializePango (); |
| 421 |
#elif gdi |
| 422 |
my d_useGdiplus = _GraphicsWindows_tryToInitializeGdiPlus (); |
| 423 |
#elif quartz |
| 424 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 425 |
#endif |
| 426 |
Graphics_init (me.get(), thePrinter. resolution); |
| 427 |
my paperWidth = (double) thePrinter. paperWidth / thePrinter. resolution; |
| 428 |
my paperHeight = (double) thePrinter. paperHeight / thePrinter. resolution; |
| 429 |
//NSLog (@"Graphics_create_screenPrinter: %f %f", my paperWidth, my paperHeight); |
| 430 |
my d_x1DC = my d_x1DCmin = thePrinter. resolution / 2; |
| 431 |
my d_x2DC = my d_x2DCmax = (my paperWidth - 0.5) * thePrinter. resolution; |
| 432 |
my d_y1DC = my d_y1DCmin = thePrinter. resolution / 2; |
| 433 |
my d_y2DC = my d_y2DCmax = (my paperHeight - 0.5) * thePrinter. resolution; |
| 434 |
#if gdi |
| 435 |
/* |
| 436 |
* Map page coordinates to paper coordinates. |
| 437 |
*/ |
| 438 |
my d_x1DC -= GetDeviceCaps ((HDC) window, PHYSICALOFFSETX); |
| 439 |
my d_x2DC -= GetDeviceCaps ((HDC) window, PHYSICALOFFSETX); |
| 440 |
my d_y1DC -= GetDeviceCaps ((HDC) window, PHYSICALOFFSETY); |
| 441 |
my d_y2DC -= GetDeviceCaps ((HDC) window, PHYSICALOFFSETY); |
| 442 |
#endif // gdi |
| 443 |
Graphics_setWsWindow (me.get(), 0, my paperWidth - 1.0, 12.000 - (my paperHeight - 1.0), 12.000); |
| 444 |
GraphicsScreen_init (me.get(), display, window); |
| 445 |
return me.move(); |
| 446 |
} |
| 447 |
|
| 448 |
autoGraphics Graphics_create_xmdrawingarea (GuiDrawingArea w) { |
| 449 |
trace (U"begin"); |
| 450 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 451 |
#if cairo && gtk |
| 452 |
GtkRequisition realsize; |
| 453 |
GtkAllocation allocation; |
| 454 |
#elif gdi |
| 455 |
Dimension width, height; |
| 456 |
#endif |
| 457 |
|
| 458 |
my d_drawingArea = static_cast <GuiDrawingArea> (w); /* Now !!!!!!!!!! */ |
| 459 |
Melder_assert (my d_drawingArea -> d_widget); |
| 460 |
my screen = true; |
| 461 |
my yIsZeroAtTheTop = true; |
| 462 |
#if cairo |
| 463 |
_GraphicsCairo_tryToInitializePango (); |
| 464 |
#elif gdi |
| 465 |
my d_useGdiplus = _GraphicsWindows_tryToInitializeGdiPlus (); |
| 466 |
#elif quartz |
| 467 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 468 |
#endif |
| 469 |
#if cairo && gtk |
| 470 |
Graphics_init (me.get(), Gui_getResolution (my d_drawingArea -> d_widget)); |
| 471 |
GraphicsScreen_init (me.get(), GTK_WIDGET (my d_drawingArea -> d_widget), GTK_WIDGET (my d_drawingArea -> d_widget)); |
| 472 |
#elif gdi |
| 473 |
Graphics_init (me.get(), Gui_getResolution (my d_drawingArea -> d_widget)); |
| 474 |
GraphicsScreen_init (me.get(), XtDisplay (my d_drawingArea -> d_widget), XtWindow (my d_drawingArea -> d_widget)); |
| 475 |
#elif quartz |
| 476 |
Graphics_init (me.get(), Gui_getResolution (nullptr)); |
| 477 |
GraphicsScreen_init (me.get(), my d_drawingArea -> d_widget, my d_drawingArea -> d_widget); |
| 478 |
#endif |
| 479 |
|
| 480 |
Melder_assert (w -> numberOfGraphicses < structGuiDrawingArea :: MAXIMUM_NUMBER_OF_GRAPHICSES); |
| 481 |
w -> graphicses [++ w -> numberOfGraphicses] = me.get(); // refer back |
| 482 |
|
| 483 |
#if cairo && gtk |
| 484 |
// fb: is really the request meant or rather the actual size, aka allocation? |
| 485 |
gtk_widget_size_request (GTK_WIDGET (my d_drawingArea -> d_widget), & realsize); |
| 486 |
gtk_widget_get_allocation (GTK_WIDGET (my d_drawingArea -> d_widget), & allocation); |
| 487 |
// HIER WAS IK |
| 488 |
trace (U"requested ", realsize.width, U" x ", realsize.height, U", allocated ", allocation.width, U" x ", allocation.height); |
| 489 |
Graphics_setWsViewport (me.get(), 0.0, realsize.width, 0.0, realsize.height); |
| 490 |
#elif gdi |
| 491 |
XtVaGetValues (my d_drawingArea -> d_widget, XmNwidth, & width, XmNheight, & height, nullptr); |
| 492 |
Graphics_setWsViewport (me.get(), 0.0, width, 0.0, height); |
| 493 |
#elif quartz |
| 494 |
NSView *view = (NSView *) my d_drawingArea -> d_widget; |
| 495 |
NSRect bounds = [view bounds]; |
| 496 |
Graphics_setWsViewport (me.get(), 0.0, bounds.size.width, 0.0, bounds.size.height); |
| 497 |
#endif |
| 498 |
return me.move(); |
| 499 |
} |
| 500 |
|
| 501 |
autoGraphics Graphics_create_pngfile (MelderFile file, int resolution, |
| 502 |
double x1inches, double x2inches, double y1inches, double y2inches) |
| 503 |
{ |
| 504 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 505 |
my screen = true; |
| 506 |
my yIsZeroAtTheTop = true; |
| 507 |
#if cairo |
| 508 |
_GraphicsCairo_tryToInitializePango (); |
| 509 |
#elif gdi |
| 510 |
my d_useGdiplus = _GraphicsWindows_tryToInitializeGdiPlus (); |
| 511 |
#elif quartz |
| 512 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 513 |
#endif |
| 514 |
Graphics_init (me.get(), resolution); |
| 515 |
my d_isPng = true; |
| 516 |
my d_file = *file; |
| 517 |
my d_x1DC = my d_x1DCmin = 0; |
| 518 |
my d_x2DC = my d_x2DCmax = (x2inches - x1inches) * resolution; |
| 519 |
my d_y1DC = my d_y1DCmin = 0; |
| 520 |
my d_y2DC = my d_y2DCmax = (y2inches - y1inches) * resolution; |
| 521 |
Graphics_setWsWindow (me.get(), x1inches, x2inches, y1inches, y2inches); |
| 522 |
#if cairo |
| 523 |
my d_cairoSurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, |
| 524 |
(x2inches - x1inches) * resolution, (y2inches - y1inches) * resolution); |
| 525 |
my d_cairoGraphicsContext = cairo_create (my d_cairoSurface); |
| 526 |
//cairo_scale (my d_cairoGraphicsContext, 72.0 / resolution, 72.0 / resolution); |
| 527 |
/* |
| 528 |
Fill in the whole area with a white background. |
| 529 |
*/ |
| 530 |
cairo_set_source_rgb (my d_cairoGraphicsContext, 1.0, 1.0, 1.0); |
| 531 |
cairo_rectangle (my d_cairoGraphicsContext, 0, 0, my d_x2DC, my d_y2DC); |
| 532 |
cairo_fill (my d_cairoGraphicsContext); |
| 533 |
cairo_set_source_rgb (my d_cairoGraphicsContext, 0.0, 0.0, 0.0); |
| 534 |
#elif gdi |
| 535 |
my metafile = true; |
| 536 |
HDC screenDC = GetDC (nullptr); |
| 537 |
my d_gdiGraphicsContext = CreateCompatibleDC (screenDC); |
| 538 |
trace (U"d_gdiGraphicsContext ", Melder_pointer (my d_gdiGraphicsContext)); |
| 539 |
Melder_assert (my d_gdiGraphicsContext); |
| 540 |
my d_gdiBitmap = CreateCompatibleBitmap (screenDC, (x2inches - x1inches) * resolution, (y2inches - y1inches) * resolution); |
| 541 |
trace (U"d_gdiBitmap ", Melder_pointer (my d_gdiBitmap)); |
| 542 |
Melder_assert (my d_gdiBitmap); |
| 543 |
ReleaseDC (nullptr, screenDC); |
| 544 |
SelectObject (my d_gdiGraphicsContext, my d_gdiBitmap); |
| 545 |
trace (U"bitmap selected into device context"); |
| 546 |
my resolution = resolution; |
| 547 |
SetBkMode (my d_gdiGraphicsContext, TRANSPARENT); |
| 548 |
my d_winPen = CreatePen (PS_SOLID, 0, RGB (0, 0, 0)); |
| 549 |
my d_winBrush = CreateSolidBrush (RGB (0, 0, 0)); |
| 550 |
SetTextAlign (my d_gdiGraphicsContext, TA_LEFT | TA_BASELINE | TA_NOUPDATECP); |
| 551 |
/* |
| 552 |
Fill in the whole area with a white background. |
| 553 |
*/ |
| 554 |
SelectPen (my d_gdiGraphicsContext, GetStockPen (NULL_PEN)); |
| 555 |
SelectBrush (my d_gdiGraphicsContext, GetStockBrush (WHITE_BRUSH)); |
| 556 |
Rectangle (my d_gdiGraphicsContext, 0, 0, my d_x2DC + 1, my d_y2DC + 1); // plus 1, in order to prevent two black edges |
| 557 |
SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN)); |
| 558 |
SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); |
| 559 |
#elif quartz |
| 560 |
integer width = (x2inches - x1inches) * resolution, height = (y2inches - y1inches) * resolution; |
| 561 |
integer stride = width * 4; |
| 562 |
stride = (stride + 15) & ~15; // CommonCode/AppDrawing.c: "a multiple of 16 bytes, for best performance" |
| 563 |
my d_bits = Melder_malloc (uint8_t, stride * height); |
| 564 |
static CGColorSpaceRef colourSpace = nullptr; |
| 565 |
if (! colourSpace) { |
| 566 |
colourSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB); |
| 567 |
Melder_assert (colourSpace); |
| 568 |
} |
| 569 |
my d_macGraphicsContext = CGBitmapContextCreate (my d_bits, |
| 570 |
width, height, |
| 571 |
8, // bits per component |
| 572 |
stride, |
| 573 |
colourSpace, |
| 574 |
kCGImageAlphaPremultipliedLast); |
| 575 |
if (! my d_macGraphicsContext) |
| 576 |
Melder_throw (U"Could not create PNG file ", file, U"."); |
| 577 |
CGRect rect = CGRectMake (0, 0, width, height); |
| 578 |
CGContextSetAlpha (my d_macGraphicsContext, 1.0); |
| 579 |
CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeNormal); |
| 580 |
CGContextSetRGBFillColor (my d_macGraphicsContext, 1.0, 1.0, 1.0, 1.0); |
| 581 |
CGContextFillRect (my d_macGraphicsContext, rect); |
| 582 |
CGContextTranslateCTM (my d_macGraphicsContext, 0, height); |
| 583 |
CGContextScaleCTM (my d_macGraphicsContext, 1.0, -1.0); |
| 584 |
#endif |
| 585 |
return me.move(); |
| 586 |
} |
| 587 |
|
| 588 |
autoGraphics Graphics_create_pdffile (MelderFile file, int resolution, |
| 589 |
double x1inches, double x2inches, double y1inches, double y2inches) |
| 590 |
{ |
| 591 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 592 |
my screen = true; |
| 593 |
my yIsZeroAtTheTop = true; |
| 594 |
#if cairo |
| 595 |
_GraphicsCairo_tryToInitializePango (); |
| 596 |
#elif quartz |
| 597 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 598 |
#endif |
| 599 |
Graphics_init (me.get(), resolution); |
| 600 |
#if cairo |
| 601 |
my d_cairoSurface = cairo_pdf_surface_create (MelderFile_peekPath8 (file), |
| 602 |
( isdefined (x1inches) ? x2inches - x1inches : x2inches ) * 72.0, |
| 603 |
( isdefined (y1inches) ? y2inches - y1inches : y2inches ) * 72.0); |
| 604 |
my d_cairoGraphicsContext = cairo_create (my d_cairoSurface); |
| 605 |
my d_x1DC = my d_x1DCmin = 0; |
| 606 |
my d_x2DC = my d_x2DCmax = ( isdefined (x1inches) ? 7.5 : x2inches ) * resolution; |
| 607 |
my d_y1DC = my d_y1DCmin = 0; |
| 608 |
my d_y2DC = my d_y2DCmax = ( isdefined (y1inches) ? 11.0 : y2inches ) * resolution; |
| 609 |
Graphics_setWsWindow (me.get(), |
| 610 |
isdefined (x1inches) ? 0.0 : 0.0, isdefined (x1inches) ? 7.5 : x2inches, |
| 611 |
isdefined (y1inches) ? 1.0 : 0.0, isdefined (y1inches) ? 12.0 : y2inches |
| 612 |
); |
| 613 |
cairo_scale (my d_cairoGraphicsContext, 72.0 / resolution, 72.0 / resolution); |
| 614 |
cairo_translate (my d_cairoGraphicsContext, |
| 615 |
( isdefined (x1inches) ? - x1inches : 0.0 ) * resolution, |
| 616 |
( isdefined (y1inches) ? (y2inches - 12.0) : 0.0 ) * resolution); |
| 617 |
#elif quartz |
| 618 |
CFURLRef url = CFURLCreateWithFileSystemPath (nullptr, (CFStringRef) MelderFile_peekPathCfstring (file), kCFURLPOSIXPathStyle, false); |
| 619 |
CGRect rect = CGRectMake (0, 0, |
| 620 |
( isdefined (x1inches) ? x2inches - x1inches : x2inches ) * 72.0, |
| 621 |
( isdefined (y1inches) ? y2inches - y1inches : y2inches ) * 72.0); // don't tire PDF viewers with funny origins |
| 622 |
CFStringRef key = (CFStringRef) Melder_peek32toCfstring (U"Creator"); |
| 623 |
CFStringRef value = (CFStringRef) Melder_peek32toCfstring (U"Praat"); |
| 624 |
CFIndex numberOfValues = 1; |
| 625 |
CFDictionaryRef dictionary = CFDictionaryCreate (nullptr, (const void **) & key, (const void **) & value, numberOfValues, |
| 626 |
& kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); |
| 627 |
my d_macGraphicsContext = CGPDFContextCreateWithURL (url, & rect, dictionary); |
| 628 |
CFRelease (url); |
| 629 |
CFRelease (dictionary); |
| 630 |
if (! my d_macGraphicsContext) |
| 631 |
Melder_throw (U"Could not create PDF file ", file, U"."); |
| 632 |
my d_x1DC = my d_x1DCmin = 0; |
| 633 |
my d_x2DC = my d_x2DCmax = ( isdefined (x1inches) ? 7.5 : x2inches ) * resolution; |
| 634 |
my d_y1DC = my d_y1DCmin = 0; |
| 635 |
my d_y2DC = my d_y2DCmax = ( isdefined (y1inches) ? 11.0 : y2inches ) * resolution; |
| 636 |
Graphics_setWsWindow (me.get(), |
| 637 |
( isdefined (x1inches) ? 0.0 : 0.0 ), ( isdefined (x1inches) ? 7.5 : x2inches ), |
| 638 |
( isdefined (y1inches) ? 1.0 : 0.0 ), ( isdefined (y1inches) ? 12.0 : y2inches )); |
| 639 |
CGContextBeginPage (my d_macGraphicsContext, & rect); |
| 640 |
CGContextScaleCTM (my d_macGraphicsContext, 72.0 / resolution, 72.0 / resolution); |
| 641 |
CGContextTranslateCTM (my d_macGraphicsContext, |
| 642 |
( isdefined (x1inches) ? - x1inches : 0.0 ) * resolution, |
| 643 |
( isdefined (y1inches) ? (12.0 - y1inches) : y2inches ) * resolution); |
| 644 |
CGContextScaleCTM (my d_macGraphicsContext, 1.0, -1.0); |
| 645 |
#endif |
| 646 |
return me.move(); |
| 647 |
} |
| 648 |
autoGraphics Graphics_create_pdf (void *context, int resolution, |
| 649 |
double x1inches, double x2inches, double y1inches, double y2inches) |
| 650 |
{ |
| 651 |
autoGraphicsScreen me = Thing_new (GraphicsScreen); |
| 652 |
my screen = true; |
| 653 |
my yIsZeroAtTheTop = true; |
| 654 |
#if quartz |
| 655 |
_GraphicsMacintosh_tryToInitializeQuartz (); |
| 656 |
#endif |
| 657 |
Graphics_init (me.get(), resolution); |
| 658 |
#if quartz |
| 659 |
my d_macGraphicsContext = static_cast <CGContext *> (context); |
| 660 |
CGRect rect = CGRectMake (0, 0, (x2inches - x1inches) * 72, (y2inches - y1inches) * 72); // don't tire PDF viewers with funny origins |
| 661 |
my d_x1DC = my d_x1DCmin = 0; |
| 662 |
my d_x2DC = my d_x2DCmax = 7.5 * resolution; |
| 663 |
my d_y1DC = my d_y1DCmin = 0; |
| 664 |
my d_y2DC = my d_y2DCmax = 11.0 * resolution; |
| 665 |
Graphics_setWsWindow (me.get(), 0, 7.5, 1.0, 12.0); |
| 666 |
Melder_assert (my d_macGraphicsContext); |
| 667 |
CGContextBeginPage (my d_macGraphicsContext, & rect); |
| 668 |
CGContextScaleCTM (my d_macGraphicsContext, 72.0 / resolution, 72.0 / resolution); |
| 669 |
CGContextTranslateCTM (my d_macGraphicsContext, - x1inches * resolution, (12.0 - y1inches) * resolution); |
| 670 |
CGContextScaleCTM (my d_macGraphicsContext, 1.0, -1.0); |
| 671 |
#endif // quartz |
| 672 |
return me.move(); |
| 673 |
} |
| 674 |
|
| 675 |
#if cairo |
| 676 |
void *Graphics_x_getCR (Graphics me) { |
| 677 |
return ((GraphicsScreen) me) -> d_cairoGraphicsContext; |
| 678 |
} |
| 679 |
void Graphics_x_setCR (Graphics me, void *cairoGraphicsContext) { |
| 680 |
((GraphicsScreen) me) -> d_cairoGraphicsContext = (cairo_t *) cairoGraphicsContext; |
| 681 |
} |
| 682 |
#endif // cairo |
| 683 |
|
| 684 |
/* End of file GraphicsScreen.cpp */ |