home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / gs403osk.tgz / gs403osk.tar / dll.txt < prev    next >
Text File  |  1996-09-22  |  20KB  |  555 lines

  1. /* Copyright (C) 1994-1996, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  20.  
  21. This file, dll.txt, describes how to use the Ghostscript Dynamic
  22. Link Library.
  23.  
  24. For an overview of Ghostscript and a list of the documentation files, see
  25. README.  
  26.  
  27. ==================================
  28. DLL.TXT   1996-09-05
  29. ==================================
  30.  
  31. For the OS/2, Win16 and Win32 platforms, Ghostscript is compiled
  32. as a dynamic link library.  
  33.  
  34. To provide the interface described in use.txt, a smaller EXE loads 
  35. this Ghostscript DLL.  
  36. This EXE provides image windows and if necessary, a text window.  
  37. The source for the EXE is in dp*.* for OS/2 and dw*.* for Windows.
  38. Refer to these source files for examples of DLL usage.
  39.  
  40. This document document describes the DLL interface.
  41.  
  42. The Win16 DLL (GSDLL16.DLL) is a large memory model DLL with far 
  43. static data.  Due to the limitations of 16 bit MS-Windows, the DLL 
  44. can be used by only one program at a time.  
  45.  
  46. The Win32 DLL (GSDLL32.DLL) has MULTIPLE NONSHARED data segments.
  47. Under Win32s it can be used by only one program at a time.
  48. Under Windows 95 or NT it can be called by multiple programs.
  49.  
  50. The OS/2 DLL (GSDLL2.DLL) has MULTIPLE NONSHARED data segments and
  51. can be called by multiple programs.
  52.  
  53. The interface to the DLL consists of 8 main functions, 1 provided 
  54. by the caller and the other 7 by the DLL.  Some other platform
  55. dependant functions are provided by the DLL for display devices.
  56.  
  57. =============
  58. DLL functions
  59. =============
  60. The seven functions provided by the DLL are:
  61.   int GSDLLAPI gsdll_revision(char **product, char **copyright, 
  62.         long *gs_revision, long *gs_revisiondate)
  63.   int GSDLLAPI gsdll_init(GSDLL_CALLBACK callback, HWND hwnd, 
  64.         int argc, char *argv[]);
  65.   int GSDLLAPI gsdll_execute_begin(void);
  66.   int GSDLLAPI gsdll_execute_cont(const char *str, int len);
  67.   int GSDLLAPI gsdll_execute_end(void);
  68.   int GSDLLAPI gsdll_exit(void);
  69.   int GSDLLAPI gsdll_lock_device(unsigned char *device, int flag);
  70.  
  71. For OS/2, GSDLLAPI is defined as
  72.   #define GSDLLAPI
  73. For Win32 and Win16, GSDLLAPI is defined as
  74.   #define GSDLLAPI CALLBACK _export
  75.  
  76. ----------------
  77. gsdll_revision()
  78. ----------------
  79. This returns the revision numbers and strings of the Ghostscript DLL.
  80. This function may be called before gsdll_init().
  81. An example:
  82.   char *product;
  83.   char *copyright;
  84.   long revision;
  85.   long revisiondate;
  86.   gsdll_revision(&product, ©right, &revision, &revisiondate);
  87. NULL pointers may be used if you do not want a particular value.
  88.  
  89. It is recommended that this function be called before gsdll_init()
  90. to make sure that the correct version of the Ghostscript DLL
  91. has been loaded.
  92.  
  93. ------------
  94. gsdll_init()
  95. ------------
  96. The function gsdll_init() must be called after loading
  97. the DLL and before executing any ghostscript commands.
  98. The arguments are the address of the callback function,
  99. a parent window handle, the count of arguments and an 
  100. array of pointers to the arguments.
  101. For example
  102.   char *argv[5];
  103.   argv[0] = "gswin.exe";
  104.   argv[1] = "-Ic:\\gs;c:\gs\\fonts";
  105.   argv[2] = "-dNOPAUSE",
  106.   argv[3] = "-sDEVICE=djet500",
  107.   argv[4] = NULL;
  108.   };
  109.   argc = 4;
  110.   code = gsdll_init(gsdll_callback, hwnd, argc, argv);
  111.  
  112. hwnd is used as the parent window handle for any windows
  113. created by Ghostscript.
  114. hwnd may be NULL if the caller does not have any windows, but
  115. if NULL you should avoid using devices which may open windows.
  116.  
  117. If the return code is zero then there is no error.
  118. gsdll_execute_begin() or gsdll_exit() may now be called.
  119.  
  120. If the return value is non-zero then gsdll_exit() must not be called.  
  121.  
  122. A return value of GSDLL_INIT_QUIT indicates that one of the
  123. command line files or arguments called 'quit', or that GS
  124. was reading stdin and reached EOF.  This is not an error.
  125. gsdll_exit() must not be called.
  126.  
  127. A return value of GSDLL_INIT_IN_USE indicates that the DLL is
  128. in use by another application (Windows 3.1 only).
  129. The DLL should be immediately unloaded (or the caller terminated).  
  130. gsdll_exit() must not be called.
  131.  
  132.  
  133. ---------------------
  134. gsdll_execute_begin()
  135. ---------------------
  136. This must be called after gsdll_init() and before gsdll_execute_cont();
  137.  
  138.  
  139. --------------------
  140. gsdll_execute_cont()
  141. --------------------
  142. After successfully calling gsdll_init() and gsdll_execute_begin(), 
  143. commands may be given to Ghostscript with gsdll_execute_cont().  
  144. Examples are:
  145.   char *command = "1 2 add == flush\n";
  146.   code = gsdll_execute_cont(command, strlen(command));
  147.   command = "qu"
  148.   code = gsdll_execute_cont(command, strlen(command));
  149.   command = "it\n"
  150.   code = gsdll_execute_cont(command, strlen(command));
  151. return code is zero if there are no errors.
  152. return code is less than zero if an error has occured.
  153. return code is less than or equal to -100 if "quit" has been 
  154.   executed or a fatal error has occured.
  155.   gsdll_exit() must then be called - do not call gsdll_execute_end().
  156. gsdll_execute_cont does not flush stdio - if you want to see output from
  157. Ghostscript you must do this explicitly as shown in the example above.
  158.  
  159. When executing a string with gsdll_execute_cont, currentfile is the
  160. input from gsdll_execute_cont.  Reading from %stdin will use the 
  161. callback.
  162.  
  163.  
  164. -------------------
  165. gsdll_execute_end()
  166. -------------------
  167. If gsdll_execute_cont() did not return an error, then gsdll_execute_end()
  168. must be called after gsdll_execute_cont() and before gsdll_exit();
  169.  
  170.  
  171. ----------
  172. gsdll_exit
  173. ----------
  174. To terminate the Ghostscript DLL, gsdll_exit() is called.
  175. This must be called if a fatal error has occured (see return
  176. value of gsdll_execute_cont).
  177. After calling gsdll_exit(), there are two options:
  178. 1. Unload the DLL, either by terminating the application or by 
  179. calling DosFreeModule (OS/2) or FreeLibrary (MS-Windows).
  180. 2. Call gsdll_init() again to restart Ghostscript.
  181.  
  182.  
  183. -----------------
  184. gsdll_lock_device
  185. -----------------
  186. Since the caller may be multithreaded, a lock is needed to control 
  187. access to the display device.  This is accessed via the following 
  188. function.
  189.  
  190. int gsdll_lock_device(unsigned char *device, int flag);
  191.  /* Lock the device if flag = TRUE */
  192.  /* Unlock the device if flag = FALSE */
  193.  /* device is a pointer to Ghostscript os2dll or mswindll device */
  194.  /* from GSDLL_DEVICE message. */
  195.  /* Return value is the lock count. */
  196.  
  197. To lock the device use
  198.   gsdll_lock_device(device, 1);
  199. To unlock the device use
  200.   gsdll_lock_device(device, 0);
  201.  
  202. Locking the device prevents the Ghostscript DLL from closing 
  203. the device or changing the device size or depth.  
  204. Ghostscript may draw into the device bitmap or update the palette 
  205. entries while the device is locked by the caller.
  206.  
  207. This function is typically used to lock the device while
  208. repainting a window, or copying the device bitmap to the clipboard.
  209.  
  210. Under OS/2, Win95 and WinNT, this lock is implemented using
  211. a mutual exclusion semaphore (mutex).  The return value is 
  212. the lock count, which will be either 0 or 1, for unlocked and 
  213. locked respectively.
  214. This function will block until the device is locked by the caller.
  215.  
  216. Under Win16 or Win32s, gsdll_lock_device will always return 
  217. immediately, giving a lock count as the return value.  If
  218. a lock value of 2 or more is returned, beware!
  219. Access to the device should be controlled by checking the 
  220. windows message queue only when the bitmap is not being accessed.
  221.  
  222. =================
  223. Callback function
  224. =================
  225. A callback function must be provided by the caller and given
  226. as an argument to gsdll_init().
  227. The callback function is called by the DLL for stdio and to notify 
  228. the caller about device events.
  229.  
  230. The function provided by the caller has the following prototype:
  231.   int gsdll_callback(int message, char *str, unsigned long count);
  232. The pascal calling convention is not used.
  233.  
  234. An example callback function is:
  235.     int 
  236.     gsdll_callback(int message, char *str, unsigned long count)
  237.     {
  238.     char *p;
  239.         switch (message) {
  240.             case GSDLL_STDIN:
  241.                 p = fgets(str, count, stdin);
  242.                 if (p)
  243.                     return strlen(str);
  244.                 else
  245.                     return 0;
  246.             case GSDLL_STDOUT:
  247.                 if (str != (char *)NULL)
  248.                     fwrite(str, 1, count, stdout);
  249.                 return count;
  250.             case GSDLL_DEVICE:
  251.                 fprintf(stdout,"Callback: DEVICE %p %s\n", str,
  252.                     count ? "open" : "close");
  253.                 break;
  254.             case GSDLL_SYNC:
  255.                 fprintf(stdout,"Callback: SYNC %p\n", str);
  256.                 break;
  257.             case GSDLL_PAGE:
  258.                 fprintf(stdout,"Callback: PAGE %p\n", str);
  259.                 break;
  260.             case GSDLL_SIZE:
  261.                 fprintf(stdout,"Callback: SIZE %p width=%d height=%d\n", str,
  262.                     (int)(count & 0xffff), (int)((count>>16) & 0xffff) );
  263.                 break;
  264.             case GSDLL_POLL:
  265.         return 0; /* no error */
  266.             default:
  267.                 fprintf(stdout,"Callback: Unknown message=%d\n",message);
  268.                 break;
  269.         }
  270.         return 0;
  271.     }
  272.  
  273.  
  274. The messages used by the callback are:
  275.   #define GSDLL_STDIN 1   /* get count characters to str from stdin */
  276.                           /* return number of characters read */
  277.   #define GSDLL_STDOUT 2  /* put count characters from str to stdout*/
  278.                           /* return number of characters written */
  279.   #define GSDLL_DEVICE 3  /* device = str has been opened if count=1 */
  280.                           /*                    or closed if count=0 */
  281.   #define GSDLL_SYNC 4    /* sync_output for device str */ 
  282.   #define GSDLL_PAGE 5    /* output_page for device str */
  283.   #define GSDLL_SIZE 6    /* resize for device str */
  284.                           /* LOWORD(count) is new xsize */
  285.                           /* HIWORD(count) is new ysize */
  286.   #define GSDLL_POLL 7    /* Called from gp_check_interrupt */
  287.               /* Can be used by caller to poll the message queue */
  288.               /* Normally returns 0 */
  289.               /* To abort gsdll_execute_cont(), return a */
  290.               /* non zero error code until gsdll_execute_cont() */
  291.               /* returns */
  292.  
  293. ==========================
  294. Example DLL usage for OS/2
  295. ==========================
  296. The following example shows a minimal usage of the Ghostscript DLL.
  297. The example callback function above is needed.
  298.  
  299. #define INCL_DOS
  300. #include <os2.h>
  301. #include <stdio.h>
  302. #include "gsdll.h"
  303.  
  304. PFN_gsdll_init pgsdll_init;
  305. PFN_gsdll_execute_begin pgsdll_execute_begin;
  306. PFN_gsdll_execute_cont pgsdll_execute_cont;
  307. PFN_gsdll_execute_end pgsdll_execute_end;
  308. PFN_gsdll_exit pgsdll_exit;
  309.  
  310. HMODULE hmodule_gsdll;
  311. char buf[256];
  312.  
  313. int
  314. main(int argc, char *argv[])
  315. {
  316. int code;
  317. APIRET rc;
  318.     if (!DosLoadModule(buf, sizeof(buf), "GSDLL2", &hmodule_gsdll)) {
  319.         fprintf(stderr, "Loaded GSDLL2\n");
  320.         DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_init", (PFN *)(&pgsdll_init));
  321.         DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_begin", (PFN *)(&pgsdll_execute_begin));
  322.         DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_cont", (PFN *)(&pgsdll_execute_cont));
  323.         DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_execute_end", (PFN *)(&pgsdll_execute_end));
  324.         DosQueryProcAddr(hmodule_gsdll, 0, "gsdll_exit", (PFN *)(&pgsdll_exit));
  325.     }
  326.     else {
  327.         fprintf(stderr, "Can't load GSDLL2\n");
  328.     }
  329.  
  330.     code = (*pgsdll_init)(gsdll_callback, NULL, argc, argv);
  331.     fprintf(stdout,"gsdll_init returns %d\n", code);
  332.     code = (*pgsdll_execute_begin)();
  333.     if (code==0) {
  334.         while (fgets(buf, sizeof(buf), stdin)) {
  335.             code = (*pgsdll_execute_cont)(buf, strlen(buf));
  336.             fprintf(stdout,"gsdll_execute returns %d\n", code);
  337.         if (code < 0)
  338.            break;
  339.         }
  340.         if (!code)
  341.             code = (*pgsdll_execute_end)();
  342.         code = (*pgsdll_exit)();
  343.         fprintf(stdout,"gsdll_exit returns %d\n", code);
  344.     }
  345.     rc = DosFreeModule(hmodule_gsdll);
  346.     fprintf(stdout,"DosFreeModule returns %d\n", rc);
  347.     return 0;
  348. }
  349.  
  350. ===============================
  351. Ghostscript DLL device for OS/2
  352. ===============================
  353. The os2dll device is provided in the Ghostscript DLL for use
  354. by the caller.  No drawing facilities are provided by the DLL
  355. because the DLL may be loaded by a text only (non PM) application.
  356.  
  357. The caller will be notified via the gsdll_callback when a new
  358. os2dll device is opened or closed (GSDLL_DEVICE), when the window 
  359. should be redrawn (GSDLL_SYNC or GSDLL_PAGE) or when the bitmap 
  360. size changes (GSDLL_SIZE).
  361.  
  362. Note that more than one os2dll device may be opened.
  363.  
  364. One DLL function is available for accessing the os2dll device:
  365.  
  366. ----------------
  367. gsdll_get_bitmap
  368. ----------------
  369. The following function returns a pointer to a bitmap in BMP format.
  370. The os2dll device draws into this bitmap.
  371.  
  372. unsigned long gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap);
  373.  /* return in pbitmap the address of the bitmap */
  374.  /* device is a pointer to Ghostscript os2dll device from GSDLL_DEVICE message */
  375.  
  376. The caller can then display the bitmap however it likes, but should 
  377. lock the bitmap with gsdll_lock_device() before painting from it, 
  378. and unlock it afterwards.  The bitmap address will not change until
  379. the os2dll device is closed, however the bitmap size and palette
  380. may change at any time the bitmap is not locked.
  381.  
  382. =====================================
  383. Ghostscript DLL device for MS-Windows
  384. =====================================
  385. The mswindll device is provided in the Ghostscript DLL for use
  386. by the caller.  
  387.  
  388. The caller will be notified via the gsdll_callback when a new
  389. mswindll device is opened or closed (GSDLL_DEVICE), when the window 
  390. should be redrawn (GSDLL_SYNC or GSDLL_PAGE) or when the bitmap 
  391. size changes (GSDLL_SIZE).
  392.  
  393. Note that more than one mswindll device may be opened.
  394.  
  395. Four extra DLL functions are available for accessing the mswindll device:
  396.  
  397. --------------
  398. gsdll_copy_dib
  399. --------------
  400. This function is commonly used when copying the mswindll bitmap
  401. to the clipboard.
  402.  
  403. /* make a copy of the device bitmap and return shared memory handle to it */
  404. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  405. HGLOBAL GSDLLAPI gsdll_copy_dib(unsigned char *device);
  406.  
  407. ------------------
  408. gsdll_copy_palette
  409. ------------------
  410. This function can be used when copying the mswindll palette
  411. to the clipboard.
  412.  
  413. /* make a copy of the device palette and return a handle to it */
  414. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  415. HPALETTE GSDLLAPI gsdll_copy_palette(unsigned char *device);
  416.  
  417. ----------
  418. gsdll_draw
  419. ----------
  420. This function is used for displaying output from the mswindll device.  
  421. The caller should create a window and call gsdll_draw in response to 
  422. the WM_PAINT message.  The device context hdc must be for a device 
  423. because SetDIBitsToDevice() is used.
  424.  
  425. /* copy the rectangle src from the device bitmap */
  426. /* to the rectangle dest on the device given by hdc */
  427. /* hdc must be a device context for a device (NOT a bitmap) */
  428. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  429. void GSDLLAPI gsdll_draw(unsigned char *device, HDC hdc, 
  430.                                   LPRECT dest, LPRECT src);
  431.  
  432. --------------------
  433. gsdll_get_bitmap_row
  434. --------------------
  435. The following function returns a BMP header, a palette and a pointer
  436. to a row in the bitmap.
  437.  
  438. int GSDLLAPI gsdll_get_bitmap_row(unsigned char *device, LPBITMAPINFOHEADER pbmih,
  439.     LPRGBQUAD prgbquad, LPBYTE *ppbyte, unsigned int row)
  440. /* Copy bitmap
  441.  * If pbmih nonzero, copy the BITMAPINFOHEADER.
  442.  * If prgbquad nonzero, copy the palette.
  443.  *   number of entries copied is given by pbmih->biClrUsed
  444.  * If ppbyte nonzero, return pointer to row.
  445.  *   pointer is only valid while device is locked
  446.  * GS can change the palette while the device is locked.
  447.  * Do not call this function while GS is busy.
  448.  *
  449.  * This function exists to allow the bitmap to be copied to a file
  450.  * or structured storage, without the overhead of having two copies
  451.  * of the bitmap in memory at the same time.
  452.  */
  453.  
  454. ================
  455. MS-Windows 16bit
  456. ================
  457. This platform has the most problems of the three.
  458. Support for it may be dropped in future.
  459.  
  460. The Win16 DLL (GSDLL16.DLL) is a large memory model DLL with far 
  461. static data.  Due to the limitations of 16 bit MS-Windows, the DLL 
  462. can used by only one program at a time.
  463. However, GSDLL16 is marked as having SINGLE SHARED data segments which
  464. allows multiple applications to load GSDLL16.  (The DLL wouldn't load
  465. at all if MULTIPLE NONSHARED was used).  Applications loading GSDLL16
  466. should check the return value of gsdll_init().  If it is non-zero
  467. then GSDLL16 is already in use by another application and should NOT
  468. be used.  GSDLL16 should be unloaded immediately using FreeLibrary(),
  469. or the caller program should terminate.
  470.  
  471. The segmented architecture of the 80286 causes the usual amount of
  472. grief when using GSDLL16.
  473. Because the callback is called from the DLL which is using a different
  474. data segment, the callback must be declared as _far _export.
  475.   int _far _export gsdll_callback(int message, char *str, unsigned long count);
  476. Instead of giving gsdll_init the address of gsdll_callback, it should
  477. instead be given the address of a thunk created by MakeProcInstance.
  478. This thunk changes the data segment back to that used by the caller:
  479.   FARPROC lpfnCallback;
  480.   lpfnCallback = (FARPROC)MakeProcInstance((FARPROC)gsdll_callback, hInstance);
  481.   code = (*pgsdll_init)((GSDLL_CALLBACK)lpfnCallback, NULL, argc, argv);
  482.   if (!code) {
  483.       fprintf(stderr, "GSDLL16 is already in use\n");
  484.       return -1;
  485.   }
  486.  
  487. ==================
  488. Changes 1996-09-05
  489. ==================
  490. Ghostscript locks the device just before closing it, to prevent 
  491. closing the device while the caller is drawing from it.
  492.  
  493. Added gsdll_get_bitmap_row() to MS-Windows.
  494.  
  495. ==================
  496. Changes 1996-07-02
  497. ==================
  498. gsdll_init() error code changed.
  499.  
  500. ==================
  501. Changes 1996-05-30
  502. ==================
  503. gsdll_init() error code changed.
  504.  
  505. ==================
  506. Changes 1996-05-16
  507. ==================
  508. Added HWND as a parameter to gsdll_init().
  509. THIS IS A NON BACKWARD COMPATIBLE CHANGE.
  510.  
  511. Documented that gsdll_execute_end() must not be called if 
  512. gsdll_execute_cont() returns with error.
  513.  
  514.  
  515. ==================
  516. Changes 1996-03-16
  517. ==================
  518. New call back message GSDLL_POLL.
  519.  
  520. Removed OS/2 gsdll_lock_bitmap().  Replaced by gsdll_lock_device().
  521.  
  522. Add gsdll_lock_device to OS/2 and MS-Windows.
  523.  For non multi-threaded environments (Win16, Win32s) this will
  524.  return a lock count but return immediately.
  525.  For multi-threaded environments this will maintain a lock
  526.  using a mutex (which may block).
  527.  
  528. Changed gsdll_init() to take argc and argv.  Now supports the
  529.   full range of Ghostscript command line options.
  530.  
  531. *** OS/2 has't been tested yet.
  532.  
  533. ==================
  534. Changes 1995-07-26
  535. ==================
  536. gsdll_revision needs to be checked before using other APIs,
  537.  to protect against finding a later version of the GS DLL
  538.  with changed APIs.
  539.  
  540. gsdll_execute removed.
  541. gsdll_execute_begin/cont/end added.
  542.  Keywords can now be split across calls to gsdll_execute_cont.
  543.  DLL input can now be done without using the stdin callback
  544.  
  545. Now use GSDLLAPI instead of WINAPI
  546.  Under Windows, the calling convention is CALLBACK, not pascal.
  547.  For Win32 this is _stdcall, for Win16 this is _far _pascal.
  548.  
  549.  
  550. Original version 1994-07-03
  551.  
  552. ====================
  553. /* end of dll.txt */
  554. ====================
  555.