1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/* Copyright (C) 2001-2020 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Ghostscript DLL loader for Windows */
#include "windows_.h"
#include <shellapi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gscdefs.h"
#define GSREVISION gs_revision
#include "ierrors.h"
#include "iapi.h"
#include "dwres.h"
#include "dwdll.h"
#include "dwtext.h"
#include "dwimg.h"
#include "dwtrace.h"
#include "dwreg.h"
#include "gdevdsp.h"
/* public handles */
HINSTANCE ghInstance;
/* redirected stdio */
TW *tw;
static const LPSTR szAppName = "Ghostscript";
#ifdef _WIN64
const LPSTR szIniName = "gswin64.ini";
const char *szDllName = "gsdll64.dll";
#else
const LPSTR szIniName = "gswin32.ini";
const char *szDllName = "gsdll32.dll";
#endif
const LPSTR szIniSection = "Text";
GSDLL gsdll;
void *instance = NULL;
HWND hwndtext;
char start_string[] = "systemdict /start get exec\n";
static int poll(void)
{
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* If text window closing then abort Ghostscript */
if (tw->quitnow)
return gs_error_Fatal;
return 0;
}
/*********************************************************************/
/* stdio functions */
static int GSDLLCALL
gsdll_stdin(void *instance, char *buf, int len)
{
return text_read_line(tw, buf, len);
}
static int GSDLLCALL
gsdll_stdout(void *instance, const char *str, int len)
{
text_write_buf(tw, str, len);
return len;
}
static int GSDLLCALL
gsdll_stderr(void *instance, const char *str, int len)
{
text_write_buf(tw, str, len);
return len;
}
/* Poll the caller for cooperative multitasking. */
/* If this function is NULL, polling is not needed */
static int GSDLLCALL gsdll_poll(void *handle)
{
return poll();
}
/* make a file readable, for drag'n'drop */
int dwmain_add_file_control_path(const TCHAR *pathfile)
{
LPSTR p;
int code, i;
p = malloc(wchar_to_utf8(NULL, (wchar_t *)pathfile));
if (p) {
wchar_to_utf8(p, (wchar_t *)pathfile);
for (i = 0; i < strlen(p); i++) {
if (p[i] == '\\') {
p[i] = '/';
}
}
code = gsdll.add_control_path(instance, GS_PERMIT_FILE_READING, p);
free(p);
}
else {
code = -1;
}
return code;
}
void dwmain_remove_file_control_path(const TCHAR *pathfile)
{
LPSTR p;
int i;
p = malloc(wchar_to_utf8(NULL, (wchar_t *)pathfile));
if (p) {
wchar_to_utf8(p, (wchar_t *)pathfile);
for (i = 0; i < strlen(p); i++) {
if (p[i] == '\\') {
p[i] = '/';
}
}
gsdll.remove_control_path(instance, GS_PERMIT_FILE_READING, p);
free(p);
}
}
/*********************************************************************/
/* new dll display device */
/*
#define DISPLAY_DEBUG
*/
/* New device has been opened */
/* This is the first event from this device. */
static int display_open(void *handle, void *device)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_open(0x%x, 0x%x)\n", handle, device);
text_puts(tw, buf);
#endif
img = image_new(handle, device); /* create and add to list */
if (img)
image_open(img);
return 0;
}
/* Device is about to be closed. */
/* Device will not be closed until this function returns. */
static int display_preclose(void *handle, void *device)
{
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_preclose(0x%x, 0x$x)\n", handle, device);
text_puts(tw, buf);
#endif
/* do nothing - no thread synchonisation needed */
return 0;
}
/* Device has been closed. */
/* This is the last event from this device. */
static int display_close(void *handle, void *device)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_close(0x%x, 0x$x)\n", handle, device);
text_puts(tw, buf);
#endif
img = image_find(handle, device);
if (img) {
image_delete(img); /* remove from list but don't free */
image_close(img);
}
return 0;
}
/* Device is about to be resized. */
/* Resize will only occur if this function returns 0. */
static int display_presize(void *handle, void *device, int width, int height,
int raster, unsigned int format)
{
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_presize(0x%x, 0x%x, width=%d height=%d raster=%d\n\
format=%d)\n",
handle, device, width, height, raster, format);
text_puts(tw, buf);
#endif
return 0;
}
/* Device has been resized. */
/* New pointer to raster returned in pimage */
static int display_size(void *handle, void *device, int width, int height,
int raster, unsigned int format, unsigned char *pimage)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_size(0x%x, 0x%x, width=%d height=%d raster=%d\n\
format=%d image=0x%x)\n",
handle, device, width, height, raster, format, pimage);
text_puts(tw, buf);
#endif
img = image_find(handle, device);
if (img != NULL) {
image_size(img, width, height, raster, format, pimage);
image_updatesize(img);
}
return 0;
}
/* flushpage */
static int display_sync(void *handle, void *device)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_sync(0x%x, 0x%x)\n", handle, device);
text_puts(tw, buf);
#endif
img = image_find(handle, device);
if (img != NULL)
image_sync(img);
return 0;
}
/* showpage */
/* If you want to pause on showpage, then don't return immediately */
static int display_page(void *handle, void *device, int copies, int flush)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
char buf[256];
sprintf(buf, "display_page(0x%x, 0x%x, copies=%d flush=%d)\n",
handle, device, copies, flush);
text_puts(tw, buf);
#endif
img = image_find(handle, device);
if (img != NULL)
image_page(img);
return 0;
}
/* Poll the caller for cooperative multitasking. */
/* If this function is NULL, polling is not needed */
static int display_update(void *handle, void *device,
int x, int y, int w, int h)
{
IMAGE *img;
img = image_find(handle, device);
if (img != NULL)
image_poll(img); /* redraw the window periodically */
return poll();
}
int display_separation(void *handle, void *device,
int comp_num, const char *name,
unsigned short c, unsigned short m,
unsigned short y, unsigned short k)
{
IMAGE *img;
#ifdef DISPLAY_DEBUG
fprintf(stdout, "display_separation(0x%x, 0x%x, %d '%s' %d,%d,%d,%d)\n",
handle, device, comp_num, name, (int)c, (int)m, (int)y, (int)k);
#endif
img = image_find(handle, device);
if (img)
image_separation(img, comp_num, name, c, m, y, k);
return 0;
}
display_callback display = {
sizeof(display_callback),
DISPLAY_VERSION_MAJOR,
DISPLAY_VERSION_MINOR,
display_open,
display_preclose,
display_close,
display_presize,
display_size,
display_sync,
display_page,
display_update,
NULL, /* memalloc */
NULL, /* memfree */
display_separation
};
/*********************************************************************/
/* program really starts at WinMain */
int new_main(int argc, char *argv[])
{
int code, code1;
int exit_status;
int exit_code;
int nargc;
char **nargv;
char dformat[64];
char ddpi[64];
char buf[256];
memset(buf, 0, sizeof(buf));
if (load_dll(&gsdll, buf, sizeof(buf))) {
text_puts(tw, "Can't load Ghostscript DLL\n");
text_puts(tw, buf);
text_puts(tw, "\n");
return 1;
}
if (gsdll.new_instance(&instance, NULL) < 0) {
text_puts(tw, "Can't create Ghostscript instance\n");
return 1;
}
gsdll.set_stdio(instance, gsdll_stdin, gsdll_stdout, gsdll_stderr);
gsdll.set_poll(instance, gsdll_poll);
gsdll.set_display_callback(instance, &display);
/* insert display device defaults as first arguments */
{ int format = DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE |
DISPLAY_DEPTH_1 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST;
HDC hdc = GetDC(NULL); /* get hdc for desktop */
int depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
sprintf(ddpi, "-dDisplayResolution=%d", GetDeviceCaps(hdc, LOGPIXELSY));
ReleaseDC(NULL, hdc);
if (depth == 32)
format = DISPLAY_COLORS_RGB | DISPLAY_UNUSED_LAST |
DISPLAY_DEPTH_8 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST;
else if (depth == 16)
format = DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE |
DISPLAY_DEPTH_16 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST |
DISPLAY_NATIVE_555;
else if (depth > 8)
format = DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE |
DISPLAY_DEPTH_8 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST;
else if (depth >= 8)
format = DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE |
DISPLAY_DEPTH_8 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST;
else if (depth >= 4)
format = DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE |
DISPLAY_DEPTH_4 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST;
sprintf(dformat, "-dDisplayFormat=%d", format);
}
nargc = argc + 2;
nargv = (char **)malloc(nargc * sizeof(char *));
nargv[0] = argv[0];
nargv[1] = dformat;
nargv[2] = ddpi;
memcpy(&nargv[3], &argv[1], (argc-1) * sizeof(char *));
#if defined(_MSC_VER) || defined(__BORLANDC__)
__try {
#endif
code = gsdll.set_arg_encoding(instance, GS_ARG_ENCODING_UTF8);
if (code == 0)
code = gsdll.init_with_args(instance, nargc, nargv);
if (code == 0)
code = gsdll.run_string(instance, start_string, 0, &exit_code);
code1 = gsdll.exit(instance);
if (code == 0 || (code == gs_error_Quit && code1 != 0))
code = code1;
#if defined(_MSC_VER) || defined(__BORLANDC__)
} __except(exception_code() == EXCEPTION_STACK_OVERFLOW) {
code = gs_error_Fatal;
text_puts(tw, "*** C stack overflow. Quiting...\n");
}
#endif
text_clear_drag_and_drop_list(tw, 1);
gsdll.delete_instance(instance);
unload_dll(&gsdll);
free(nargv);
exit_status = 0;
switch (code) {
case 0:
case gs_error_Quit:
break;
case gs_error_Fatal:
exit_status = 1;
break;
case gs_error_Info:
default:
exit_status = 255;
}
return exit_status;
}
void
set_font(void)
{
int fontsize;
char fontname[256];
char buf[32];
/* read ini file */
GetPrivateProfileString(szIniSection, "FontName", "Courier New", fontname, sizeof(fontname), szIniName);
fontsize = GetPrivateProfileInt(szIniSection, "FontSize", 10, szIniName);
/* set font */
text_font(tw, fontname, fontsize);
/* write ini file */
WritePrivateProfileString(szIniSection, "FontName", fontname, szIniName);
sprintf(buf, "%d", fontsize);
WritePrivateProfileString(szIniSection, "FontSize", buf, szIniName);
}
typedef BOOL (SetProcessDPIAwareFn)(void);
static void
avoid_windows_scale(void)
{
/* Fetch the function address and only call it if it is there; this keeps
* compatability with Windows < 8.1 */
HMODULE hUser32 = LoadLibrary(TEXT("user32.dll"));
SetProcessDPIAwareFn *ptr;
ptr = (SetProcessDPIAwareFn *)GetProcAddress(hUser32, "SetProcessDPIAware");
if (ptr != NULL)
ptr();
FreeLibrary(hUser32);
}
int PASCAL
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
{
int dll_exit_status;
#define MAXCMDTOKENS 128
/* BC++ 4.5 will give us _argc and _argv[], but they will be */
/* incorrect if there is a space in the program name. */
/* Provide our own parsing code to create argc and argv[]. */
int argc;
LPSTR argv[MAXCMDTOKENS];
LPSTR p;
LPSTR pstart;
char command[256];
char *args;
char *d, *e;
char winposbuf[256];
int len = sizeof(winposbuf);
int x, y, cx, cy;
/* Mark us as being 'system dpi aware' to avoid horrid scaling */
avoid_windows_scale();
/* copy the hInstance into a variable so it can be used */
ghInstance = hInstance;
if (hPrevInstance) {
MessageBox((HWND)NULL,"Can't run twice", szAppName,
MB_ICONHAND | MB_OK);
return FALSE;
}
/* If called with "gswin32c.exe arg1 arg2"
* lpszCmdLine returns:
* "arg1 arg2" if called with CreateProcess(NULL, command, ...)
* "arg2" if called with CreateProcess(command, args, ...)
* GetCommandLine() returns
* ""gswin32c.exe" arg1 arg2"
* if called with CreateProcess(NULL, command, ...)
* " arg1 arg2"
* if called with CreateProcess(command, args, ...)
* Consequently we must use GetCommandLine()
*/
{
wchar_t *uni = GetCommandLineW();
pstart = p = malloc(wchar_to_utf8(NULL, uni));
if (p != NULL)
wchar_to_utf8(p, uni);
}
argc = 0;
args = (char *)malloc(lstrlen(p)+1);
if (args == (char *)NULL) {
fprintf(stdout, "Insufficient memory in WinMain()\n");
return 1;
}
/* Parse command line handling quotes. */
d = args;
while (*p) {
/* for each argument */
if (argc >= MAXCMDTOKENS - 1)
break;
e = d;
while ((*p) && (*p != ' ')) {
if (*p == '\042') {
/* Remove quotes, skipping over embedded spaces. */
/* Doesn't handle embedded quotes. */
p++;
while ((*p) && (*p != '\042'))
*d++ =*p++;
}
else
*d++ = *p;
if (*p)
p++;
}
*d++ = '\0';
argv[argc++] = e;
while ((*p) && (*p == ' '))
p++; /* Skip over trailing spaces */
}
argv[argc] = NULL;
free(pstart);
if (strlen(argv[0]) == 0) {
GetModuleFileName(hInstance, command, sizeof(command)-1);
argv[0] = command;
}
tw = text_new();
if (tw == NULL) {
MessageBox((HWND)NULL, "Can't create text window",
szAppName, MB_OK | MB_ICONSTOP);
return 1;
}
/* start up the text window */
if (!hPrevInstance) {
HICON hicon = LoadIcon(hInstance, (LPSTR)MAKEINTRESOURCE(GSTEXT_ICON));
text_register_class(tw, hicon);
}
set_font();
text_size(tw, 80, 80);
text_drag(tw, "(", ") run\r");
if (win_get_reg_value("Text", winposbuf, &len) == 0) {
if (sscanf(winposbuf, "%d %d %d %d", &x, &y, &cx, &cy) == 4)
text_setpos(tw, x, y, cx, cy);
}
/* create the text window */
if (text_create(tw, szAppName, cmdShow))
exit(1);
hwndtext = text_get_handle(tw);
image_textwindow(hwndtext);
dll_exit_status = new_main(argc, argv);
if (dll_exit_status && !tw->quitnow) {
/* display error message in text window */
MSG msg;
text_puts(tw, "\nClose this window with the close button on the title bar or the system menu.\n");
if (IsIconic(text_get_handle(tw)))
ShowWindow(text_get_handle(tw), SW_SHOWNORMAL);
BringWindowToTop(text_get_handle(tw)); /* make text window visible */
FlashWindow(text_get_handle(tw), TRUE);
/* Wait until error message is read */
while (!tw->quitnow && GetMessage(&msg, (HWND)NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
/* Save the text window size */
if (text_getpos(tw, &x, &y, &cx, &cy) == 0) {
sprintf(winposbuf, "%d %d %d %d", x, y, cx, cy);
win_set_reg_value("Text", winposbuf);
}
text_destroy(tw);
tw = NULL;
return dll_exit_status;
}
|