Drivers.htm 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>The interface between Ghostscript and device drivers</title>
  5. <!-- $Id: Drivers.htm,v 1.21.2.2 2002/02/01 05:31:24 raph Exp $ -->
  6. <!-- Originally: drivers.txt -->
  7. <link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
  8. </head>
  9. <body>
  10. <!-- [1.0 begin visible header] ============================================ -->
  11. <!-- [1.1 begin headline] ================================================== -->
  12. <h1>The interface between Ghostscript and device drivers</h1>
  13. <!-- [1.1 end headline] ==================================================== -->
  14. <!-- [1.2 begin table of contents] ========================================= -->
  15. <h2>Table of contents</h2>
  16. <blockquote><ul>
  17. <li><a href="#Adding_drivers">Adding a driver</a>
  18. <li><a href="#KISS">Keeping things simple</a>
  19. <li><a href="#Structure">Driver structure</a>
  20. <ul>
  21. <li><a href="#Structure_definition">Structure definition</a>
  22. <li><a href="#Sophisticated">For sophisticated developers only</a>
  23. </ul>
  24. <li><a href="#coordinates_and_types">Coordinates and types</a>
  25. <ul>
  26. <li><a href="#Coordinate_system">Coordinate system</a>
  27. <li><a href="#Color_definition">Color definition</a>
  28. <li><a href="#Types">Types</a>
  29. </ul>
  30. <li><a href="#Coding_conventions">Coding conventions</a>
  31. <ul>
  32. <li><a href="#Allocating_storage">Allocating storage</a>
  33. <li><a href="#Driver_instance_allocation">Driver instance allocation</a>
  34. </ul>
  35. <li><a href="#Printer_drivers">Printer drivers</a>
  36. <li><a href="#Driver_procedures">Driver procedures</a>
  37. <ul>
  38. <li><a href="#Life_cycle">Life cycle</a>
  39. <li><a href="#Open_close">Open, close, sync, copy</a>
  40. <li><a href="#Color_mapping">Color and alpha mapping</a>
  41. <li><a href="#Pixel_level_drawing">Pixel-level drawing</a>
  42. <ul>
  43. <li><a href="#Bitmap_imaging">Bitmap imaging</a>
  44. <li><a href="#Pixmap_imaging">Pixmap imaging</a>
  45. <li><a href="#Compositing">Compositing</a>
  46. [<a href="#S_spec">S</a>, <a href="#T_spec">T</a>, <a href="#F_spec">f</a>,
  47. <a href="#Compositing_notes">Notes</a>]
  48. </ul>
  49. <li><a href="#Polygon_level_drawing">Polygon-level drawing</a>
  50. <li><a href="#High_level_drawing">High-level drawing</a>
  51. <ul>
  52. <li><a href="#Paths">Paths</a>
  53. <li><a href="#Images">Images</a> [<a href="#Images_notes">Notes</a>]
  54. <li><a href="#Text">Text</a> [<a href="#Text_notes">Notes</a>]
  55. </ul>
  56. <li><a href="#Reading_bits_back">Reading bits back</a>
  57. <li><a href="#Parameters">Parameters</a>
  58. <ul>
  59. <li><a href="#Default_CRD_parameters">Default color rendering dictionary (CRD) parameters</a>
  60. </ul>
  61. <li><a href="#External_fonts">External fonts</a>
  62. <li><a href="#Page_devices">Page devices</a>
  63. <li><a href="#Miscellaneous">Miscellaneous</a>
  64. </ul>
  65. </ul></blockquote>
  66. <!-- [1.2 end table of contents] =========================================== -->
  67. <!-- [1.3 begin hint] ====================================================== -->
  68. <p>For other information, see the <a href="Readme.htm">Ghostscript
  69. overview</a> and the documentation on <a href="Make.htm">how to build
  70. Ghostscript</a>.
  71. <!-- [1.3 end hint] ======================================================== -->
  72. <hr>
  73. <!-- [1.0 end visible header] ============================================== -->
  74. <!-- [2.0 begin contents] ================================================== -->
  75. <h2><a name="Adding_drivers"></a>Adding a driver</h2>
  76. <p>
  77. To add a driver to Ghostscript, first pick a name for your device, say
  78. "<b><tt>smurf</tt></b>". (Device names must be 1 to 8 characters, begin
  79. with a letter, and consist only of letters, digits, and underscores. Case
  80. is significant: all current device names are lower case.) Then all you
  81. need do is edit <b><tt>contrib.mak</tt></b> in two places.
  82. <ol>
  83. <li>The list of devices, in the section headed "Catalog". Add
  84. <b><tt>smurf</tt></b> to the list.
  85. <li>The section headed "Device drivers".
  86. <p>
  87. Suppose the files containing the smurf driver are called
  88. "<b><tt>joe</tt></b>" and "<b><tt>fred</tt></b>". Then you should add the
  89. following lines:
  90. <blockquote>
  91. <pre># ------ The SMURF device ------ #
  92. smurf_=$(GLOBJ)joe.$(OBJ) $(GLOBJ)fred.$(OBJ)
  93. $(DD)smurf.dev: $(smurf_)
  94. $(SETDEV) $(DD)smurf $(smurf_)
  95. $(GLOBJ)joe.$(OBJ) : $(GLSRC)joe.c
  96. $(GLCC) $(GLO_)joe.$(OBJ) $(C_) $(GLSRC)joe.c
  97. $(GLOBJ)fred.$(OBJ) : $(GLSRC)fred.c
  98. $(GLCC) $(GLO_)fred.$(OBJ) $(C_) $(GLSRC)fred.c</pre>
  99. </blockquote>
  100. <p>
  101. and whatever <b><tt>joe.c</tt></b> and <b><tt>fred.c</tt></b> depend on.
  102. If the smurf driver also needs special libraries, for instance a library
  103. named "<b><tt>gorf</tt></b>", then the entry should look like this:
  104. <blockquote>
  105. <pre>$(DD)smurf.dev : $(smurf_)
  106. $(SETDEV) $(DD)smurf $(smurf_)
  107. $(ADDMOD) $(DD)smurf -lib gorf</pre>
  108. </blockquote>
  109. <p>
  110. If, as will usually be the case, your driver is a printer driver (as
  111. <a href="#Printer_drivers">discussed below</a>), the device entry should
  112. look like this:
  113. <blockquote>
  114. <pre>$(DD)smurf.dev : $(smurf_) $(GLD)page.dev
  115. $(SETPDEV) $(DD)smurf $(smurf_)</pre>
  116. </blockquote>
  117. <p>
  118. or
  119. <blockquote>
  120. <pre>$(DD)smurf.dev : $(smurf_) $(GLD)page.dev
  121. $(SETPDEV) $(DD)smurf $(smurf_)
  122. $(ADDMOD) $(DD)smurf -lib gorf</pre>
  123. </blockquote>
  124. <p>
  125. Note that the space before the :, and the explicit compilation rules for the
  126. .c files, are required for portability,
  127. </ol>
  128. <hr>
  129. <h2><a name="KISS"></a>Keeping things simple</h2>
  130. <p>
  131. If you want to add a simple device (specifically, a monochrome printer), you
  132. probably don't need to read the rest of this document; just use the code in
  133. an existing driver as a guide. The Epson and Canon BubbleJet drivers <a
  134. href="../src/gdevepsn.c">gdevepsn.c</a> and <a
  135. href="../src/gdevbj10.c">gdevbj10.c</a> are good models for dot-matrix
  136. printers, which require presenting the data for many scan lines at once; the
  137. DeskJet/LaserJet drivers in <a href="../src/gdevdjet.c">gdevdjet.c</a> are
  138. good models for laser printers, which take a single scan line at a time but
  139. support data compression. For color printers, there are unfortunately no
  140. good models: the two major color inkjet printer drivers, <a
  141. href="../src/gdevcdj.c">gdevcdj.c</a> and <a
  142. href="../src/gdevstc.c">gdevstc.c</a>, are far too complex to read.
  143. <p>
  144. On the other hand, if you're writing a driver for some more esoteric
  145. device, you probably do need at least some of the information in the rest
  146. of this document. It might be a good idea for you to read it in
  147. conjunction with one of the existing drivers.
  148. <p>
  149. Duplication of code, and sheer volume of code, is a serious maintenance and
  150. distribution problem for Ghostscript. If your device is similar to an
  151. existing one, try to implement your driver by adding some parameterization
  152. to an existing driver rather than by copying code to create an entirely new
  153. source module. <a href="../src/gdevepsn.c">gdevepsn.c</a> and <a
  154. href="../src/gdevdjet.c">gdevdjet.c</a> are good examples of this approach.
  155. <hr>
  156. <h2><a name="Structure"></a>Driver structure</h2>
  157. <p>
  158. A device is represented by a structure divided into three parts:
  159. <ul>
  160. <li>procedures that are (normally) shared by all instances of each device;
  161. <li>parameters that are present in all devices but may be different for
  162. each device or instance; and
  163. <li>device-specific parameters that may be different for each instance.
  164. </ul>
  165. <p>
  166. Normally the procedure structure is defined and initialized at compile
  167. time. A prototype of the parameter structure (including both generic and
  168. device-specific parameters) is defined and initialized at compile time, but
  169. is copied and filled in when an instance of the device is created. Both of
  170. these structures should be declared as <b><tt>const</tt></b>, but for backward
  171. compatibility reasons the latter is not.
  172. <p>
  173. The <b><tt>gx_device_common</tt></b> macro defines the common structure
  174. elements, with the intent that devices define and export a structure along
  175. the following lines. Do not fill in the individual generic parameter values
  176. in the usual way for C structures: use the macros defined for this purpose
  177. in <a href="../src/gxdevice.h">gxdevice.h</a> or, if applicable, <a
  178. href="../src/gdevprn.h">gdevprn.h</a>.
  179. <blockquote>
  180. <pre>typedef struct smurf_device_s {
  181. gx_device_common;
  182. <b><em>... device-specific parameters ...</em></b>
  183. } smurf_device;
  184. smurf_device gs_smurf_device = {
  185. <b><em>... macro for generic parameter values ...,</em></b>
  186. { <b><em>... procedures ...</em></b> }, /* std_procs */
  187. <b><em>... device-specific parameter values if any ...</em></b>
  188. };</pre>
  189. </blockquote>
  190. <p>
  191. The device structure instance <b>must</b> have the name
  192. <b><tt>gs_smurf_device</tt></b>, where <b><tt>smurf</tt></b> is the device
  193. name used in <b><tt>contrib.mak</tt></b>. <b><tt>gx_device_common</tt></b>
  194. is a macro consisting only of the element definitions.
  195. <p>
  196. All the device procedures are called with the device as the first argument.
  197. Since each device type is actually a different structure type, the device
  198. procedures must be declared as taking a <b><tt>gx_device&nbsp;*</tt></b> as
  199. their first argument, and must cast it to
  200. <b><tt>smurf_device&nbsp;*</tt></b> internally. For example, in the code
  201. for the "memory" device, the first argument to all routines is called
  202. <b><tt>dev</tt></b>, but the routines actually use <b><tt>mdev</tt></b> to
  203. refer to elements of the full structure, using the following standard
  204. initialization statement at the beginning of each procedure:
  205. <blockquote>
  206. <pre>gx_memory_device *const mdev = (gx_device_memory *)dev;</pre>
  207. </blockquote>
  208. <p>
  209. (This is a cheap version of "object-oriented" programming: in C++, for
  210. example, the cast would be unnecessary, and in fact the procedure table
  211. would be constructed by the compiler.)
  212. <h3><a name="Structure_definition"></a>Structure definition</h3>
  213. <p>
  214. You should consult the definition of struct <b><tt>gx_device_s</tt></b> in
  215. <a href="../src/gxdevice.h">gxdevice.h</a> for the complete details of the
  216. generic device structure. Some of the most important members of this
  217. structure for ordinary drivers are:
  218. <blockquote><table cellpadding=0 cellspacing=0>
  219. <tr valign=top> <td><b><tt>const char *dname;</tt></b>
  220. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  221. <td>The device name
  222. <tr valign=top> <td><b><tt>bool is_open;</tt></b>
  223. <td>&nbsp;
  224. <td>True if device has been opened
  225. <tr valign=top> <td><b><tt>gx_device_color_info color_info;</tt></b>
  226. <td>&nbsp;
  227. <td>Color information
  228. <tr valign=top> <td><b><tt>int width;</tt></b>
  229. <td>&nbsp;
  230. <td>Width in pixels
  231. <tr valign=top> <td><b><tt>int height;</tt></b>
  232. <td>&nbsp;
  233. <td>Height in pixels
  234. </table></blockquote>
  235. <p>
  236. The name in the structure (<b><tt>dname</tt></b>) should be the same as the
  237. name in <a href="../src/contrib.mak">contrib.mak</a>.
  238. <h3><a name="Sophisticated"></a>For sophisticated developers only</h3>
  239. <p>
  240. If for any reason you need to change the definition of the basic device
  241. structure, or to add procedures, you must change the following places:
  242. <blockquote><ul>
  243. <li>This document and the <a href="News.htm">news document</a> (if you want
  244. to keep the documentation up to date).
  245. <li>The definition of <b><tt>gx_device_common</tt></b> and the procedures
  246. in <a href="../src/gxdevcli.h">gxdevcli.h</a>.
  247. <li>Possibly, the default forwarding procedures declared in
  248. <a href="../src/gxdevice.h">gxdevice.h</a> and implemented in
  249. <a href="../src/gdevnfwd.c">gdevnfwd.c</a>.
  250. <li>The device procedure record completion routines in
  251. <a href="../src/gdevdflt.c">gdevdflt.c</a>.
  252. <li>Possibly, the default device implementation in
  253. <a href="../src/gdevdflt.c">gdevdflt.c</a>,
  254. <a href="../src/gdevddrw.c">gdevddrw.c</a>, and
  255. <a href="../src/gxcmap.c">gxcmap.c</a>.
  256. <li>The bounding box device in <a href="../src/gdevbbox.c">gdevbbox.c</a>
  257. (probably just adding <b><tt>NULL</tt></b> procedure entries if the
  258. new procedures don't produce output).
  259. <li>These devices that must have complete (non-defaulted) procedure vectors:
  260. <ul>
  261. <li>The null device in <a href="../src/gdevnfwd.c">gdevnfwd.c</a>.
  262. <li>The command list "device" in <a href="../src/gxclist.c">gxclist.c</a>.
  263. This is not an actual device; it only defines procedures.
  264. <li>The "memory" devices in <a href="../src/gdevmem.h">gdevmem.h</a> and
  265. <b><tt>gdevm*.c</tt></b>.
  266. <li>The halftoning device in <a href="../src/gdevht.c">gdevht.c</a>.
  267. </ul>
  268. <li>The clip list accumulation "device" in
  269. <a href="../src/gxacpath.c">gxacpath.c</a>.
  270. <li>The clipping "devices" <a href="../src/gxclip.c">gxclip.c</a>,
  271. <a href="../src/gxclip2.c">gxclip2.c</a>,
  272. and <a href="../src/gxclipm.c">gxclipm.c</a>.
  273. <li>The pattern accumulation "device" in
  274. <a href="../src/gxpcmap.c">gxpcmap.c</a>.
  275. <li>The hit detection "device" in <a href="../src/gdevhit.c">gdevhit.c</a>.
  276. <li>The generic printer device macros in
  277. <a href="../src/gdevprn.h">gdevprn.h</a>.
  278. <li>The generic printer device code in
  279. <a href="../src/gdevprn.c">gdevprn.c</a>.
  280. <li>The RasterOp source device in
  281. <a href="../src/gdevrops.c">gdevrops.c</a>.
  282. </ul></blockquote>
  283. <p>
  284. You may also have to change the code for
  285. <b><tt>gx_default_get_params</tt></b> or
  286. <b><tt>gx_default_put_params</tt></b> in <a
  287. href="../src/gsdparam.c">gsdparam.c</a>.
  288. <p>
  289. You should not have to change any of the real devices in the standard
  290. Ghostscript distribution (listed in <a href="../src/devs.mak">devs.mak</a>
  291. and <a href="../src/contrib.mak">contrib.mak</a>) or any of your own
  292. devices, because all of them are supposed to use the macros in <a
  293. href="../src/gxdevice.h">gxdevice.h</a> or <a
  294. href="../src/gdevprn.h">gdevprn.h</a> to define and initialize their state.
  295. <hr>
  296. <h2><a name="coordinates_and_types"></a>Coordinates and types</h2>
  297. <h3><a name="Coordinate_system"></a>Coordinate system</h3>
  298. <p>
  299. Since each driver specifies the initial transformation from user
  300. coordinates to device coordinates, the driver can use any coordinate system
  301. it wants, as long as a device coordinate will fit in an
  302. <b><tt>int</tt></b>. (This is only an issue on DOS systems, where ints are
  303. only 16 bits. User coordinates are represented as floats.) Most current
  304. drivers use a coordinate system with (0,0) in the upper left corner, with
  305. <b><em>X</em></b> increasing to the right and <b><em>Y</em></b> increasing
  306. toward the bottom. However, there is supposed to be nothing in the rest of
  307. Ghostscript that assumes this, and indeed some drivers use a coordinate
  308. system with (0,0) in the lower left corner.
  309. <p>
  310. Drivers must check (and, if necessary, clip) the coordinate parameters given
  311. to them: they should not assume the coordinates will be in bounds. The
  312. <b><tt>fit_fill</tt></b> and <b><tt>fit_copy</tt></b> macros in <a
  313. href="../src/gxdevice.h">gxdevice.h</a> are very helpful in doing this.
  314. <h3><a name="Color_definition"></a>Color definition</h3>
  315. <p>
  316. Ghostscript represents colors internally as RGB or CMYK values. In
  317. communicating with devices, however, it assumes that each device has a
  318. palette of colors identified by integers (to be precise, elements of type
  319. <b><tt>gx_color_index</tt></b>). Drivers may provide a uniformly spaced
  320. gray ramp or color cube for halftoning, or they may do their own color
  321. approximation, or both.
  322. <p>
  323. The <b><tt>color_info</tt></b> member of the device structure defines the
  324. color and gray-scale capabilities of the device. Its type is defined as
  325. follows:
  326. <blockquote>
  327. <pre>typedef struct gx_device_color_info_s {
  328. int num_components; /* 1 = gray only, 3 = RGB, */
  329. /* 4 = CMYK */
  330. int depth; /* # of bits per pixel */
  331. gx_color_value max_gray; /* # of distinct gray levels -1 */
  332. gx_color_value max_rgb; /* # of distinct color levels -1 */
  333. /* (only relevant if num_comp. &gt; 1) */
  334. gx_color_value dither_gray; /* size of gray ramp for halftoning */
  335. gx_color_value dither_rgb; /* size of color cube ditto */
  336. /* (only relevant if num_comp. &gt; 1) */
  337. } gx_device_color_info;</pre>
  338. </blockquote>
  339. <p>
  340. The following macros (in <a href="../src/gxdevice.h">gxdevice.h</a>) provide
  341. convenient shorthands for initializing this structure for ordinary
  342. black-and-white or color devices:
  343. <blockquote>
  344. <b><tt>#define dci_black_and_white</tt></b> ...<br>
  345. <b><tt>#define dci_color(depth,maxv,dither)</tt></b> ...
  346. </blockquote>
  347. <p>
  348. The idea is that a device has a certain number of gray levels
  349. (<b><tt>max_gray</tt></b>+1) and a certain number of colors
  350. (<b><tt>max_rgb</tt></b>+1) that it can produce directly. When Ghostscript
  351. wants to render a given RGB or CMYK color as a device color, it first tests
  352. whether the color is a gray level (if <b><tt>num_components</tt></b> is 1,
  353. it converts all colors to gray levels), and if so:
  354. <blockquote>
  355. If <b><tt>max_gray</tt></b> is large (&gt;= 31), Ghostscript asks the
  356. device to approximate the gray level directly. If the device returns a
  357. valid <b><tt>gx_color_index</tt></b>, Ghostscript uses it. Otherwise,
  358. Ghostscript assumes that the device can represent
  359. <b><tt>dither_gray</tt></b> distinct gray levels, equally spaced along the
  360. diagonal of the color cube, and uses the two nearest ones to the desired
  361. color for halftoning.
  362. </blockquote>
  363. <p>
  364. If the color is not a gray level:
  365. <blockquote>
  366. If <b><tt>max_rgb</tt></b> is large (&gt;= 31), Ghostscript asks the device
  367. to approximate the color directly. If the device returns a valid
  368. <b><tt>gx_color_index</tt></b>, Ghostscript uses it. Otherwise,
  369. Ghostscript assumes that the device can represent
  370. <blockquote>
  371. <b><tt>dither_rgb</tt></b> &times; <b><tt>dither_rgb</tt></b> &times; <b><tt>dither_rgb</tt></b>
  372. </blockquote>
  373. <p>
  374. distinct colors, equally spaced throughout the color cube, and uses two of
  375. the nearest ones to the desired color for halftoning.
  376. </blockquote>
  377. <h3><a name="Types"></a>Types</h3>
  378. <p>
  379. Here is a brief explanation of the various types that appear as parameters
  380. or results of the drivers.
  381. <dl>
  382. <dt><b><tt>gx_color_value</tt></b> (defined in <b><tt>gxdevice.h</tt></b>)
  383. <dd>This is the type used to represent RGB or CMYK color values. It is
  384. currently equivalent to unsigned short. However, Ghostscript may use less
  385. than the full range of the type to represent color values:
  386. <b><tt>gx_color_value_bits</tt></b> is the number of bits actually used,
  387. and <b><tt>gx_max_color_value</tt></b> is the maximum value, equal to
  388. (2^<small><sup><b><tt>gx_max_color_value_bits</tt></b></sup></small>)-1.
  389. </dl>
  390. <dl>
  391. <dt><b><tt>gx_device</tt></b> (defined in <b><tt>gxdevice.h</tt></b>)
  392. <dd>This is the device structure, as explained above.
  393. </dl>
  394. <dl>
  395. <dt><b><tt>gs_matrix</tt></b> (defined in <b><tt>gsmatrix.h</tt></b>)
  396. <dd>This is a 2-D homogeneous coordinate transformation matrix, used by
  397. many Ghostscript operators.
  398. </dl>
  399. <dl>
  400. <dt><b><tt>gx_color_index</tt></b> (defined in <b><tt>gxdevice.h</tt></b>)
  401. <dd>This is meant to be whatever the driver uses to represent a device
  402. color. For example, it might be an index in a color map, or it might be R,
  403. G, and B values packed into a single integer. Ghostscript doesn't ever do
  404. any computations with <b><tt>gx_color_index</tt></b> values: it gets them
  405. from <b><tt>map_rgb_color</tt></b> or <b><tt>map_cmyk_color</tt></b> and
  406. hands them back as arguments to several other procedures. The special
  407. value <b><tt>gx_no_color_index</tt></b> (defined as
  408. <b><tt>(gx_color_index)(-1)</tt></b>&nbsp;) means "transparent" for some of
  409. the procedures. The type definition is simply:
  410. <blockquote><b><tt>
  411. typedef unsigned long gx_color_index;
  412. </tt></b></blockquote>
  413. </dl>
  414. <dl>
  415. <dt><b><tt>gs_param_list</tt></b> (defined in <a
  416. href="../src/gsparam.h">gsparam.h</a>)
  417. <dd>This is a parameter list, which is used to read and set attributes in a
  418. device. See the comments in <a href="../src/gsparam.h">gsparam.h</a>, and
  419. the <a href="#Parameters">description of the <b><tt>get_params</tt></b> and
  420. <b><tt>put_params</tt></b> procedures</a> below, for more detail.
  421. </dl>
  422. <dl>
  423. <dt><b><tt>gx_tile_bitmap</tt></b> (defined in
  424. <a href="../src/gxbitmap.h">gxbitmap.h</a>)
  425. <br><b><tt>gx_strip_bitmap</tt></b> (defined in
  426. <a href="../src/gxbitmap.h">gxbitmap.h</a>)
  427. <dd>These structure types represent bitmaps to be used as a tile for
  428. filling a region (rectangle). <b><tt>gx_tile_bitmap</tt></b> is an older
  429. type lacking <b><tt>shift</tt></b> and <b><tt>rep_shift</tt></b>;
  430. <b><tt>gx_strip_bitmap</tt></b> has superseded it, and it should not be
  431. used in new code. Here is a copy of the relevant part of the file:
  432. <blockquote>
  433. <pre>/*
  434. * Structure for describing stored bitmaps.
  435. * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  436. * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  437. * do word-oriented operations on them if you're on a little-endian
  438. * platform like the Intel 80x86 or VAX). Each scan line must start on
  439. * a (32-bit) word boundary, and hence is padded to a word boundary,
  440. * although this should rarely be of concern, since the raster and width
  441. * are specified individually. The first scan line corresponds to y=0
  442. * in whatever coordinate system is relevant.
  443. *
  444. * For bitmaps used as halftone tiles, we may replicate the tile in
  445. * X and/or Y, but it is still valuable to know the true tile dimensions
  446. * (i.e., the dimensions prior to replication). Requirements:
  447. * width % rep_width = 0
  448. * height % rep_height = 0
  449. *
  450. * For halftones at arbitrary angles, we provide for storing the halftone
  451. * data as a strip that must be shifted in X for different values of Y.
  452. * For an ordinary (non-shifted) halftone that has a repetition width of
  453. * W and a repetition height of H, the pixel at coordinate (X,Y)
  454. * corresponds to halftone pixel (X mod W, Y mod H), ignoring phase;
  455. * for a shifted halftone with shift S, the pixel at (X,Y) corresponds
  456. * to halftone pixel ((X + S * floor(Y/H)) mod W, Y mod H). Requirements:
  457. * strip_shift &lt; rep_width
  458. * strip_height % rep_height = 0
  459. * shift = (strip_shift * (size.y / strip_height)) % rep_width
  460. */
  461. typedef struct gx_strip_bitmap_s {
  462. byte *data;
  463. int raster; /* bytes per scan line */
  464. gs_int_point size; /* width, height */
  465. gx_bitmap_id id;
  466. ushort rep_width, rep_height; /* true size of tile */
  467. ushort strip_height;
  468. ushort strip_shift;
  469. ushort shift;
  470. } gx_strip_bitmap;</pre>
  471. </blockquote>
  472. </dl>
  473. <hr>
  474. <h2><a name="Coding_conventions"></a>Coding conventions</h2>
  475. <p>
  476. All the driver procedures defined below that return <b><tt>int</tt></b>
  477. results return 0 on success, or an appropriate negative error code in the
  478. case of error conditions. The error codes are defined in <a
  479. href="../src/gserrors.h">gserrors.h</a>; they correspond directly to the
  480. errors defined in the PostScript language reference manuals. The most
  481. common ones for drivers are:
  482. <blockquote><dl>
  483. <dt><b><tt>gs_error_invalidfileaccess</tt></b>
  484. <dd>An attempt to open a file failed.
  485. <dt><b><tt>gs_error_ioerror</tt></b>
  486. <dd>An error occurred in reading or writing a file.
  487. <dt><b><tt>gs_error_limitcheck</tt></b>
  488. <dd>An otherwise valid parameter value was too large for the
  489. implementation.
  490. <dt><b><tt>gs_error_rangecheck</tt></b>
  491. <dd>A parameter was outside the valid range.
  492. <dt><b><tt>gs_error_VMerror</tt></b>
  493. <dd>An attempt to allocate memory failed. (If this happens, the procedure
  494. should release all memory it allocated before it returns.)
  495. </dl></blockquote>
  496. <p>
  497. If a driver does return an error, rather than a simple return statement it
  498. should use the <b><tt>return_error</tt></b> macro defined in <a
  499. href="../src/gx.h">gx.h</a>, which is automatically included by <a
  500. href="../src/gdevprn.h">gdevprn.h</a> but not by <a
  501. href="../src/gserrors.h">gserrors.h</a>. For example
  502. <blockquote><b><tt>
  503. return_error(gs_error_VMerror);
  504. </tt></b></blockquote>
  505. <h3><a name="Allocating_storage"></a>Allocating storage</h3>
  506. <p>
  507. While most drivers (especially printer drivers) follow a very similar
  508. template, there is one important coding convention that is not obvious from
  509. reading the code for existing drivers: driver procedures must not use
  510. <b><tt>malloc</tt></b> to allocate any storage that stays around after the
  511. procedure returns. Instead, they must use <b><tt>gs_malloc</tt></b> and
  512. <b><tt>gs_free</tt></b>, which have slightly different calling conventions.
  513. (The prototypes for these are in <a href="../src/gsmemory.h">gsmemory.h</a>,
  514. which is included in <a href="../src/gx.h">gx.h</a>, which is included in <a
  515. href="../src/gdevprn.h">gdevprn.h</a>.) This is necessary so that
  516. Ghostscript can clean up all allocated memory before exiting, which is
  517. essential in environments that provide only single-address-space
  518. multi-tasking (some versions of Microsoft Windows).
  519. <blockquote>
  520. <pre>char *gs_malloc(uint num_elements, uint element_size,
  521. const char *client_name);</pre>
  522. </blockquote>
  523. <p>
  524. Like <b><tt>calloc</tt></b>, but unlike <b><tt>malloc</tt></b>,
  525. <b><tt>gs_malloc</tt></b> takes an element count and an element size. For
  526. structures, <b><tt>num_elements</tt></b> is 1 and
  527. <b><tt>element_size</tt></b> is <b><tt>sizeof</tt></b> the structure; for
  528. byte arrays, <b><tt>num_elements</tt></b> is the number of bytes and
  529. <b><tt>element_size</tt></b> is 1. Unlike <b><tt>calloc</tt></b>,
  530. <b><tt>gs_malloc</tt></b> does <b>not</b> clear the block of storage.
  531. <p>
  532. The <b><tt>client_name</tt></b> is used for tracing and debugging. It must
  533. be a real string, not <b><tt>NULL</tt></b>. Normally it is the name of the
  534. procedure in which the call occurs.
  535. <blockquote>
  536. <pre>void gs_free(char *data, uint num_elements, uint element_size,
  537. const char *client_name);</pre>
  538. </blockquote>
  539. <p>
  540. Unlike <b><tt>free</tt></b>, <b><tt>gs_free</tt></b> demands that
  541. <b><tt>num_elements</tt></b> and element_size be supplied. It also
  542. requires a client name, like <b><tt>gs_malloc</tt></b>.
  543. <h3><a name="Driver_instance_allocation"></a>Driver instance allocation</h3>
  544. <p>
  545. All driver instances allocated by Ghostscript's standard allocator must
  546. point to a "structure descriptor" that tells the garbage collector how to
  547. trace pointers in the structure. For drivers registered in the normal way
  548. (using the makefile approach described above), no special care is needed as
  549. long as instances are created only by calling the
  550. <b><tt>gs_copydevice</tt></b> procedure defined in <a
  551. href="../src/gsdevice.h">gsdevice.h</a>. If you have a need to define
  552. devices that are not registered in this way, you must fill in the stype
  553. member in any dynamically allocated instances with a pointer to the same
  554. structure descriptor used to allocate the instance. For more information
  555. about structure descriptors, see <a href="../src/gsmemory.h">gsmemory.h</a>
  556. and <a href="../src/gsstruct.h">gsstruct.h</a>.
  557. <hr>
  558. <h2><a name="Printer_drivers"></a>Printer drivers</h2>
  559. <p>
  560. Printer drivers (which include drivers that write some kind of raster file)
  561. are especially simple to implement. Of the driver procedures defined in the
  562. next section, they only need implement two: <b><tt>map_rgb_color</tt></b>
  563. (or <b><tt>map_cmyk_color</tt></b>) and <b><tt>map_color_rgb</tt></b>. In
  564. addition, they must implement a <b><tt>print_page</tt></b> or
  565. <b><tt>print_page_copies</tt></b> procedure. There are macros in <a
  566. href="../src/gdevprn.h">gdevprn.h</a> that generate the device structure for
  567. such devices, of which the simplest is <b><tt>prn_device</tt></b>; for an
  568. example, see <a href="../src/gdevbj10.c">gdevbj10.c</a>. If you are writing
  569. a printer driver, we suggest you start by reading <a
  570. href="../src/gdevprn.h">gdevprn.h</a> and the <a
  571. href="#Color_mapping">subsection on "Color mapping"</a> below; you may be
  572. able to ignore all the rest of the driver procedures.
  573. <p>
  574. The <b><tt>print_page</tt></b> procedures are defined as follows:
  575. <blockquote>
  576. <pre>int (*print_page)(P2(gx_device_printer *, FILE *))
  577. int (*print_page_copies)(P3(gx_device_printer *, FILE *, int))</pre>
  578. </blockquote>
  579. <p>
  580. This procedure must read out the rendered image from the device and write
  581. whatever is appropriate to the file. To read back one or more scan lines
  582. of the image, the <b><tt>print_page</tt></b> procedure must call one of the
  583. following procedures:
  584. <blockquote>
  585. <pre>int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,
  586. uint size)</pre>
  587. </blockquote>
  588. <p>
  589. For this procedure, <b><tt>str</tt></b> is where the data should be copied to, and <b><tt>size</tt></b> is
  590. the size of the buffer starting at <b><tt>str</tt></b>. This procedure returns the number
  591. of scan lines copied, or &lt;0 for an error. <b><tt>str</tt></b> need not be aligned.
  592. <blockquote>
  593. <pre>int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,
  594. byte **actual_data)</pre>
  595. </blockquote>
  596. <p>
  597. This procedure reads out exactly one scan line. If the scan line is
  598. available in the correct format already, <b><tt>*actual_data</tt></b> is
  599. set to point to it; otherwise, the scan line is copied to the buffer
  600. starting at <b><tt>str</tt></b>, and <b><tt>*actual_data</tt></b> is set to
  601. <b><tt>str</tt></b>. This saves a copying step most of the time.
  602. <b><tt>str</tt></b> need not be aligned; however, if
  603. <b><tt>*actual_data</tt></b> is set to point to an existing scan line, it
  604. will be aligned. (See the description of the <b><tt>get_bits</tt></b>
  605. procedure below for more details.)
  606. <p>
  607. In either case, each row of the image is stored in the form described in
  608. the comment under <b><tt>gx_tile_bitmap</tt></b> above; each pixel takes
  609. the number of bits specified as <b><tt>color_info.depth</tt></b> in the
  610. device structure, and holds values returned by the device's
  611. <b><tt>map_</tt></b>{<b><tt>rgb</tt></b>,<b><tt>cmyk</tt></b>}<b><tt>_color</tt></b>
  612. procedure.
  613. <p>
  614. The <b><tt>print_page</tt></b> procedure can determine the number of bytes
  615. required to hold a scan line by calling:
  616. <blockquote>
  617. <pre>uint gdev_prn_raster(P1(gx_device_printer *))</pre>
  618. </blockquote>
  619. <p>
  620. For a very simple concrete example, we suggest reading the code in
  621. <b><tt>bit_print_page</tt></b> in <a href="../src/gdevbit.c">gdevbit.c</a>.
  622. <p>
  623. If the device provides <b><tt>print_page</tt></b>, Ghostscript will call
  624. <b><tt>print_page</tt></b> the requisite number of times to print the
  625. desired number of copies; if the device provides
  626. <b><tt>print_page_copies</tt></b>, Ghostscript will call
  627. <b><tt>print_page_copies</tt></b> once per page, passing it the desired
  628. number of copies.
  629. <hr>
  630. <h2><a name="Driver_procedures"></a>Driver procedures</h2>
  631. <p>
  632. Most of the procedures that a driver may implement are optional. If a
  633. device doesn't supply an optional procedure <b><tt>WXYZ</tt></b>, the entry
  634. in the procedure structure may be either <b><tt>gx_default_WXYZ</tt></b>,
  635. for instance <b><tt>gx_default_tile_rectangle</tt></b>, or
  636. <b><tt>NULL</tt></b> or 0. (The device procedure must also call the
  637. <b><tt>gx_default_</tt></b> procedure if it doesn't implement the function
  638. for particular values of the arguments.) Since C compilers supply 0 as the
  639. value for omitted structure elements, this convention means that statically
  640. initialized procedure structures continue to work even if new (optional)
  641. members are added.
  642. <h3><a name="Life_cycle"></a>Life cycle</h3>
  643. <p>
  644. A device instance begins life in a closed state. In this state, no output
  645. operations will occur. Only the following procedures may be called:
  646. <blockquote><b><tt>
  647. open_device<br>
  648. finish_copydevice<br>
  649. get_initial_matrix<br>
  650. get_params<br>
  651. put_params<br>
  652. get_hardware_params
  653. </tt></b></blockquote>
  654. <p>
  655. When <b><tt>setdevice</tt></b> installs a device instance in the graphics
  656. state, it checks whether the instance is closed or open. If the instance
  657. is closed, <b><tt>setdevice</tt></b> calls the open routine, and then sets
  658. the state to open. There is currently no user-accessible operation to
  659. close a device instance. Device instances are only closed when they are
  660. about to be freed, which occurs in three situations:
  661. <ul>
  662. <li>When a <b><tt>restore</tt></b> occurs, if the instance was created since
  663. the corresponding <b><tt>save</tt></b> and is in a VM being restored. I.e.,
  664. if the instance was created in local VM since a <b><tt>save</tt></b>, it
  665. will always be closed and freed by the corresponding
  666. <b><tt>restore</tt></b>; if it was created in global VM, it will only be
  667. closed by the outermost <b><tt>restore</tt></b>, regardless of the save
  668. level at the time the instance was created.
  669. <li>By the garbage collector, if the instance is no longer accessible.
  670. <li>When Ghostscript exits (terminates).
  671. </ul>
  672. <h3><a name="Open_close"></a>Open, close, sync, copy</h3>
  673. <dl>
  674. <dt><b><tt>int (*open_device)(P1(gx_device *))</tt></b> <b><em>[OPTIONAL]</em></b>
  675. <dd>Open the device: do any initialization associated with making the device
  676. instance valid. This must be done before any output to the device. The
  677. default implementation does nothing. <b>NOTE</b>: Clients should never call
  678. a device's <b><tt>open_device</tt></b> procedure directly: they should
  679. always call <b><tt>gs_opendevice</tt></b> instead.
  680. </dl>
  681. <dl>
  682. <dt><b><tt>int (*finish_copydevice)(P2(gx_device *dev, const gx_device
  683. *from_dev))</tt></b> <b><em>[OPTIONAL]</em></b> <dd>Perform any cleanup
  684. required after <b><tt>copydevice</tt></b> has created a new device instance
  685. by copying <b><tt>from_dev</tt></b>. If the copy operation should not be
  686. allowed, this procedure should return an error; the copy will be freed. The
  687. default implementation allows copying the device prototype, but does not
  688. allow copying device instances, because instances may contain internal
  689. pointers that should not be shared between copies, and there is no way to
  690. determine this from outside the device. <b>NOTE</b>: Clients should never
  691. call a device's <b><tt>finish_copydevice</tt></b> procedure: this procedure
  692. is only intended for use by <b><tt>gs_copydevice[2]</tt></b>.
  693. </dl>
  694. <dl>
  695. <dt><b><tt>void (*get_initial_matrix)(P2(gx_device *, gs_matrix *))</tt></b> <b><em>[OPTIONAL]</em></b>
  696. <dd>Construct the initial transformation matrix mapping user coordinates
  697. (nominally 1/72 inch per unit) to device coordinates. The default
  698. procedure computes this from width, height, and
  699. [<b><tt>xy</tt></b>]<b><tt>_pixels_per_inch</tt></b> on the assumption that
  700. the origin is in the upper left corner, that is
  701. <blockquote>
  702. <b><tt>xx</tt></b> = <b><tt>x_pixels_per_inch</tt></b>/72, <b><tt>xy</tt></b> = 0,<br>
  703. <b><tt>yx = 0, yy = -y_pixels_per_inch</tt></b>/72,<br>
  704. <b><tt>tx = 0, ty = height</tt></b>.
  705. </blockquote>
  706. </dl>
  707. <dl>
  708. <dt><b><tt>int (*sync_output)(P1(gx_device *))</tt></b> <b><em>[OPTIONAL]</em></b>
  709. <dd>Synchronize the device. If any output to the device has been
  710. buffered, send or write it now. Note that this may be called several times
  711. in the process of constructing a page, so printer drivers should <b>not</b>
  712. implement this by printing the page. The default implementation does
  713. nothing.
  714. </dl>
  715. <dl>
  716. <dt><b><tt>int (*output_page)(P3(gx_device *, int num_copies, int flush))</tt></b> <b><em>[OPTIONAL]</em></b>
  717. <dd>Output a fully composed page to the device. The
  718. <b><tt>num_copies</tt></b> argument is the number of copies that should be
  719. produced for a hardcopy device. (This may be ignored if the driver has
  720. some other way to specify the number of copies.) The <b><tt>flush</tt></b>
  721. argument is true for <b><tt>showpage</tt></b>, false for
  722. <b><tt>copypage</tt></b>. The default definition just calls
  723. <b><tt>sync_output</tt></b>. Printer drivers should implement this by
  724. printing and ejecting the page.
  725. </dl>
  726. <dl>
  727. <dt><b><tt>int (*close_device)(P1(gx_device *))</tt></b> <b><em>[OPTIONAL]</em></b>
  728. <dd>Close the device: release any associated resources. After this, output
  729. to the device is no longer allowed. The default implementation does
  730. nothing. <b>NOTE</b>: Clients should never call a device's
  731. <b><tt>close_device</tt></b> procedure directly: they should always call
  732. <b><tt>gs_closedevice</tt></b> instead.
  733. </dl>
  734. <h3><a name="Color_mapping"></a>Color and alpha mapping</h3>
  735. <p>
  736. A given driver normally implements either <b><tt>map_rgb_color</tt></b> or
  737. <b><tt>map_cmyk_color</tt></b>, but not both. Black-and-white drivers need
  738. implement neither. Note that the <b><tt>map_xxx_color</tt></b> procedures
  739. must not return <b><tt>gx_no_color_index</tt></b> (all 1s).
  740. <p>
  741. Note that code in the Ghostscript library may cache the results of calling
  742. one or more of the color mapping procedures. If the result returned by any
  743. of these procedures would change (other than as a result of a change made by
  744. the driver's <b><tt>put_params</tt></b> procedure), the driver must call
  745. <b><tt>gx_device_decache_colors(dev)</tt></b>.
  746. <dl>
  747. <dt><b><tt>gx_color_index (*map_rgb_color)(P4(gx_device&nbsp;*,
  748. gx_color_value&nbsp;red, gx_color_value&nbsp;green,
  749. gx_color_value&nbsp;blue))</tt></b> <b><em>[OPTIONAL]</em></b>
  750. <dd>Map a RGB color to a device color. The range of legal values of the
  751. RGB arguments is 0 to <b><tt>gx_max_color_value</tt></b>. The default
  752. algorithm uses the <b><tt>map_cmyk_color</tt></b> procedure if the driver
  753. supplies one, otherwise returns 1 if any of the values exceeds
  754. <b><tt>gx_max_color_value</tt></b>&nbsp;/&nbsp;2, 0 otherwise.
  755. <p>
  756. Ghostscript assumes that for devices that have color capability (that is,
  757. <b><tt>color_info.num_components</tt></b> &gt; 1),
  758. <b><tt>map_rgb_color</tt></b> returns a color index for a gray level (as
  759. opposed to a non-gray color) iff red = green = blue.
  760. </dl>
  761. <dl>
  762. <dt><b><tt>gx_color_index (*map_cmyk_color)(P5(gx_device&nbsp;*,
  763. gx_color_value&nbsp;cyan, gx_color_value&nbsp;magenta,
  764. gx_color_value&nbsp;yellow, gx_color_value&nbsp;black))</tt></b> <b><em>[OPTIONAL]</em></b>
  765. <dd>Map a CMYK color to a device color. The range of legal values of the
  766. CMYK arguments is 0 to <b><tt>gx_max_color_value</tt></b>. The default
  767. algorithm calls the <b><tt>map_rgb_color</tt></b> procedure, with suitably
  768. transformed arguments.
  769. <p>
  770. Ghostscript assumes that for devices that have color capability (that is,
  771. <b><tt>color_info.num_components</tt></b> &gt; 1),
  772. <b><tt>map_cmyk_color</tt></b> returns a color index for a gray level (as
  773. opposed to a non-gray color) iff cyan = magenta = yellow.
  774. </dl>
  775. <dl>
  776. <dt><b><tt>int (*map_color_rgb)(P3(gx_device&nbsp;*,
  777. gx_color_index&nbsp;color, gx_color_value&nbsp;rgb[3]))</tt></b> <b><em>[OPTIONAL]</em></b>
  778. <dd>Map a device color code to RGB values. The default algorithm returns
  779. (0 if <b><tt>color</tt></b>==0 else <b><tt>gx_max_color_value</tt></b>) for
  780. all three components.
  781. </dl>
  782. <dl>
  783. <dt><b><tt>gx_color_index (*map_rgb_alpha_color)(P5(gx_device&nbsp;*,
  784. gx_color_value&nbsp;red, gx_color_value&nbsp;green,
  785. gx_color_value&nbsp;blue, gx_color_value&nbsp;alpha))</tt></b> <b><em>[OPTIONAL]</em></b>
  786. <dd>Map a RGB color and an opacity value to a device color. The range of
  787. legal values of the RGB and alpha arguments is 0 to
  788. <b><tt>gx_max_color_value</tt></b>; <b><tt>alpha</tt></b> = 0 means
  789. transparent, <b><tt>alpha</tt></b> = <b><tt>gx_max_color_value</tt></b>
  790. means fully opaque. The default is to use the
  791. <b><tt>map_rgb_color</tt></b> procedure and ignore alpha.
  792. <p>
  793. Note that if a driver implements <b><tt>map_rgb_alpha_color</tt></b>, it
  794. must also implement <b><tt>map_rgb_color</tt></b>, and must implement them
  795. in such a way that
  796. <b><tt>map_rgb_alpha_color(dev,&nbsp;r,&nbsp;g,&nbsp;b,&nbsp;gx_max_color_value)</tt></b>
  797. returns the same value as
  798. <b><tt>map_rgb_color(dev,&nbsp;r,&nbsp;g,&nbsp;b)</tt></b>.
  799. </dl>
  800. <dl>
  801. <dt><b><tt>int (*map_color_rgb_alpha)(P3(gx_device&nbsp;*,
  802. gx_color_index&nbsp;color, gx_color_value&nbsp;rgba[4]))</tt></b>
  803. <b><em>[OPTIONAL]</em></b>
  804. <dd>Map a device color code to RGB and alpha values. The default
  805. implementation calls <b><tt>map_color_rgb</tt></b> and fills in
  806. <b><tt>gx_max_color_value</tt></b> for alpha.
  807. <p>
  808. Note that if a driver implements <b><tt>map_color_rgb_alpha</tt></b>, it
  809. must also implement <b><tt>map_color_rgb</tt></b>, and must implement them
  810. in such a way that the first 3 values returned by
  811. <b><tt>map_color_rgb_alpha</tt></b> are the same as the values returned by
  812. <b><tt>map_color_rgb</tt></b>.
  813. <p>
  814. Note that CMYK devices currently do not support variable opacity; alpha is
  815. ignored on such devices.
  816. </dl>
  817. <dl>
  818. <dt><b><tt>typedef&nbsp;enum&nbsp;{&nbsp;go_text,
  819. go_graphics&nbsp;}&nbsp;graphic_object_type;&nbsp;int
  820. (*get_alpha_bits)(P4(gx_device&nbsp;*dev,
  821. graphic_object_type&nbsp;type))</tt></b> <b><em>[OPTIONAL] [OBSOLETE]</em></b>
  822. <dd>This procedure is no longer used: it is replaced by the
  823. color_info.anti_alias member of the driver structure. However, it still
  824. appears in the driver procedure vector for backward compatibility. It
  825. should never be called, and drivers should not implement it.
  826. </dl>
  827. <h3><a name="Pixel_level_drawing"></a>Pixel-level drawing</h3>
  828. <p>
  829. This group of drawing operations specifies data at the pixel level. All
  830. drawing operations use device coordinates and device color values.
  831. <dl>
  832. <dt><b><tt>int (*fill_rectangle)(P6(gx_device&nbsp;*, int&nbsp;x,
  833. int&nbsp;y, int&nbsp;width, int&nbsp;height,
  834. gx_color_index&nbsp;color))</tt></b>
  835. <dd>Fill a rectangle with a color. The set of pixels filled is {(px,py) |
  836. x &lt;= px &lt; x + width and y &lt;= py &lt; y + height}. In other words,
  837. the point <em>(x,y)</em> is included in the rectangle, as are
  838. <em>(x+w-1,y)</em>, <em>(x,y+h-1)</em>, and <em>(x+w-1,y+h-1)</em>, but
  839. <b><em>not</em></b> <em>(x+w,y)</em>, <em>(x,y+h)</em>, or
  840. <em>(x+w,y+h)</em>. If <b><tt>width</tt></b>&nbsp;&lt;=&nbsp;0 or
  841. height&nbsp;&lt;=&nbsp;0, <b><tt>fill_rectangle</tt></b> should return 0
  842. without drawing anything.
  843. <p>
  844. Note that <b><tt>fill_rectangle</tt></b> is the only non-optional procedure
  845. in the driver interface.
  846. </dl>
  847. <h4><a name="Bitmap_imaging"></a>Bitmap imaging</h4>
  848. <p>
  849. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  850. The first byte corresponds to <em>(0,0)</em> in the image coordinate
  851. system: bits (or polybit color values) are packed into it left to right.
  852. There may be padding at the end of each scan line: the distance from one
  853. scan line to the next is always passed as an explicit argument.
  854. <dl>
  855. <dt><b><tt>int (*copy_mono)(P11(gx_device&nbsp;*,
  856. const&nbsp;unsigned&nbsp;char&nbsp;*data, int&nbsp;data_x, int&nbsp;raster,
  857. gx_bitmap_id&nbsp;id, int&nbsp;x, int&nbsp;y, int&nbsp;width,
  858. int&nbsp;height, gx_color_index&nbsp;color0,
  859. gx_color_index&nbsp;color1))</tt></b> <b><em>[OPTIONAL]</em></b>
  860. <dd>Copy a monochrome image (similar to the PostScript image operator).
  861. Each scan line is raster bytes wide. Copying begins at
  862. (<b><tt>data_x</tt></b>,0) and transfers a rectangle of the given width and
  863. height to the device at device coordinate <em>(x,y)</em>. (If the transfer
  864. should start at some non-zero y value in the data, the caller can adjust
  865. the data address by the appropriate multiple of the raster.) The copying
  866. operation writes device color <b><tt>color0</tt></b> at each 0-bit, and
  867. <b><tt>color1</tt></b> at each 1-bit: if <b><tt>color0</tt></b> or
  868. <b><tt>color1</tt></b> is <b><tt>gx_no_color_index</tt></b>, the device
  869. pixel is unaffected if the image bit is 0 or 1 respectively. If
  870. <b><tt>id</tt></b> is different from <b><tt>gx_no_bitmap_id</tt></b>, it
  871. identifies the bitmap contents unambiguously; a call with the same
  872. <b><tt>id</tt></b> will always have the same <b><tt>data</tt></b>,
  873. <b><tt>raster</tt></b>, and data contents.
  874. <p>
  875. This operation, with
  876. <b><tt>color0</tt></b>&nbsp;=&nbsp;<b><tt>gx_no_color_index</tt></b>, is
  877. the workhorse for text display in Ghostscript, so implementing it
  878. efficiently is very important.
  879. </dl>
  880. <dl>
  881. <dt><b><tt>int (*tile_rectangle)(P10(gx_device&nbsp;*,
  882. const&nbsp;gx_tile_bitmap&nbsp;*tile, int&nbsp;x, int&nbsp;y,
  883. int&nbsp;width, int&nbsp;height, gx_color_index&nbsp;color0,
  884. gx_color_index&nbsp;color1, int&nbsp;phase_x, int&nbsp;phase_y))</tt></b>
  885. <b><em>[OPTIONAL] [OBSOLETE]</em></b>
  886. <dd>This procedure is still supported, but has been superseded by
  887. <b><tt>strip_tile_rectangle</tt></b>. New drivers should implement
  888. <b><tt>strip_tile_rectangle</tt></b>; if they cannot cope with non-zero
  889. shift values, they should test for this explicitly and call the default
  890. implementation (<b><tt>gx_default_strip_tile_rectangle</tt></b>) if
  891. shift&nbsp;!=&nbsp;0. Clients should call
  892. <b><tt>strip_tile_rectangle</tt></b>, not <b><tt>tile_rectangle</tt></b>.
  893. </dl>
  894. <dl>
  895. <dt><b><tt>int (*strip_tile_rectangle)(P10(gx_device&nbsp;*,
  896. const&nbsp;gx_strip_bitmap&nbsp;*tile, int&nbsp;x, int&nbsp;y,
  897. int&nbsp;width, int&nbsp;height, gx_color_index&nbsp;color0,
  898. gx_color_index&nbsp;color1, int&nbsp;phase_x, int&nbsp;phase_y))</tt></b>
  899. <b><em>[OPTIONAL]</em></b>
  900. <dd>Tile a rectangle. Tiling consists of doing multiple
  901. <b><tt>copy_mono</tt></b> operations to fill the rectangle with copies of
  902. the tile. The tiles are aligned with the device coordinate system, to
  903. avoid "seams". Specifically, the (<b><tt>phase_x</tt></b>,
  904. <b><tt>phase_y</tt></b>) point of the tile is aligned with the origin of
  905. the device coordinate system. (Note that this is backwards from the
  906. PostScript definition of halftone phase.) <b><tt>phase_x</tt></b> and
  907. <b><tt>phase_y</tt></b> are guaranteed to be in the range
  908. <em>[0..</em><b><tt>tile-&gt;width</tt></b><em>)</em> and
  909. <em>[0..</em><b><tt>tile-&gt;height</tt></b><em>)</em> respectively.
  910. <p>
  911. If <b><tt>color0</tt></b> and <b><tt>color1</tt></b> are both
  912. <b><tt>gx_no_color_index</tt></b>, then the tile is a color pixmap, not a
  913. bitmap: see the next section.
  914. <p>
  915. This operation is the workhorse for halftone filling in Ghostscript, so
  916. implementing it efficiently for solid tiles (that is, where either
  917. <b><tt>color0</tt></b> and <b><tt>color1</tt></b> are both
  918. <b><tt>gx_no_color_index</tt></b>, for colored halftones, or neither one is
  919. <b><tt>gx_no_color_index</tt></b>, for monochrome halftones) is very
  920. important.
  921. </dl>
  922. <h4><a name="Pixmap_imaging"></a>Pixmap imaging</h4>
  923. <p>
  924. Pixmaps are just like bitmaps, except that each pixel occupies more than
  925. one bit. All the bits for each pixel are grouped together (this is
  926. sometimes called "chunky" or "Z" format). For <b><tt>copy_color</tt></b>,
  927. the number of bits per pixel is given by the
  928. <b><tt>color_info.depth</tt></b> parameter in the device structure: the
  929. legal values are 1, 2, 4, 8, 16, 24, or 32. The pixel values are device
  930. color codes (that is, whatever it is that <b><tt>map_rgb_color</tt></b>
  931. returns).
  932. <dl>
  933. <dt><b><tt>int (*copy_color)(P9(gx_device&nbsp;*,
  934. const&nbsp;unsigned&nbsp;char&nbsp;*data, int&nbsp;data_x, int&nbsp;raster,
  935. gx_bitmap_id&nbsp;id, int&nbsp;x, int&nbsp;y, int&nbsp;width,
  936. int&nbsp;height))</tt></b> <b><em>[OPTIONAL]</em></b>
  937. <dd>Copy a color image with multiple bits per pixel. The raster is in
  938. bytes, but <b><tt>x</tt></b> and <b><tt>width</tt></b> are in pixels, not
  939. bits. If <b><tt>id</tt></b> is different from
  940. <b><tt>gx_no_bitmap_id</tt></b>, it identifies the bitmap contents
  941. unambiguously; a call with the same <b><tt>id</tt></b> will always have the
  942. same <b><tt>data</tt></b>, <b><tt>raster</tt></b>, and data contents.
  943. <p>
  944. We do not provide a separate procedure for tiling with a pixmap; instead,
  945. <b><tt>tile_rectangle</tt></b> can also take colored tiles. This is
  946. indicated by the <b><tt>color0</tt></b> and <b><tt>color1</tt></b>
  947. arguments' both being <b><tt>gx_no_color_index</tt></b>. In this case, as
  948. for <b><tt>copy_color</tt></b>, the <b><tt>raster</tt></b> and
  949. <b><tt>height</tt></b> in the "bitmap" are interpreted as for real bitmaps,
  950. but the <b><tt>x</tt></b> and <b><tt>width</tt></b> are in pixels, not
  951. bits.
  952. </dl>
  953. <h4><a name="Compositing"></a>Compositing</h4>
  954. <p>
  955. In addition to direct writing of opaque pixels, devices must also support
  956. compositing. Currently two kinds of compositing are defined
  957. (<b><tt>RasterOp</tt></b> and alpha-based), but more may be added in the
  958. future.
  959. <blockquote>
  960. <b><em>THIS AREA OF THE INTERFACE IS SOMEWHAT UNSTABLE: USE AT YOUR OWN
  961. RISK.</em></b>
  962. </blockquote>
  963. <dl>
  964. <dt><b><tt>int (*copy_alpha)(P11(gx_device&nbsp;*dev,
  965. const&nbsp;unsigned&nbsp;char&nbsp;*data, int&nbsp;data_x, int&nbsp;raster,
  966. gx_bitmap_id&nbsp;id, int&nbsp;x, int&nbsp;y, int&nbsp;width,
  967. int&nbsp;height, gx_color_index&nbsp;color, int&nbsp;depth))</tt></b>
  968. <b><em>[OPTIONAL]</em></b>
  969. <dd>This procedure is somewhat misnamed: it was added to the interface
  970. before we really understood alpha channel and compositing.
  971. <p>
  972. Fill a given region with a given color modified by an individual alpha
  973. value for each pixel. For each pixel, this is equivalent to
  974. alpha-compositing with a source pixel whose alpha value is obtained from
  975. the pixmap (<b><tt>data</tt></b>, <b><tt>data_x</tt></b>, and
  976. <b><tt>raster</tt></b>) and whose color is the given color (which has
  977. <b><em>not</em></b> been premultiplied by the alpha value), using the Sover
  978. rule. <b><tt>depth</tt></b>, the number of bits per alpha value, is either
  979. 2 or 4, and in any case is always a value returned by a previous call on
  980. the <b><tt>get_alpha_bits</tt></b> procedure. Note that if
  981. <b><tt>get_alpha_bits</tt></b> always returns 1, this procedure will never
  982. be called.
  983. </dl>
  984. <dl>
  985. <dt><b><tt>int (*create_compositor)(P5(dev_t&nbsp;*dev,
  986. gx_device_t&nbsp;**pcdev, const&nbsp;gs_composite_t&nbsp;*pcte,
  987. const&nbsp;gs_imager_state&nbsp;*pis, gs_memory_t&nbsp;*memory))</tt></b>
  988. <b><em>[OPTIONAL]</em></b>
  989. <dd>Create a new device (called a "compositing device" or "compositor")
  990. that will composite data written to it with the device's existing data,
  991. according to the compositing function defined by <b><tt>*pcte</tt></b>.
  992. Devices will normally implement this in one of the following standard ways:
  993. <ul>
  994. <li>Devices that don't do any imaging and don't forward any imaging
  995. operations (for example, the null device, the hit detection device, and the
  996. clipping list accumulation device) simply return themselves, which
  997. effectively ignores the compositing function.
  998. <li>"Leaf" devices that do imaging and have no special optimizations for
  999. compositing (for example, some memory devices) ask the
  1000. <b><tt>gs_composite_t</tt></b> to create a default compositor.
  1001. <li>Leaf devices that can implement some kinds of compositing operation
  1002. efficiently (for example, monobit memory devices and RasterOp) inspect the
  1003. type and values of <b><tt>*pcte</tt></b> to determine whether it specifies
  1004. such an operation: if so, they create a specialized compositor, and if not,
  1005. they ask the <b><tt>gs_composite_t</tt></b> to create a default compositor.
  1006. </ul>
  1007. <p>
  1008. Other kinds of forwarding devices, which don't fall into any of these
  1009. categories, require special treatment. In principle, what they do is ask
  1010. their target to create a compositor, and then create and return a copy of
  1011. themselves with the target's new compositor as the target of the copy.
  1012. There is a possible default implementation of this approach: if the
  1013. original device was <b>D</b> with target <b>T</b>, and <b>T</b> creates a
  1014. compositor <b>C</b>, then the default implementation creates a device
  1015. <b>F</b> that for each operation temporarily changes <b>D</b>'s target to
  1016. <b>C</b>, forwards the operation to <b>D</b>, and then changes <b>D</b>'s
  1017. target back to <b>T</b>. However, the Ghostscript library currently only
  1018. creates a compositor with an imaging forwarding device as target in a few
  1019. specialized situations (banding, and bounding box computation), and these
  1020. are handled as special cases.
  1021. <p>
  1022. Note that the compositor may have a different color space, color
  1023. representation, or bit depth from the device to which it is compositing.
  1024. For example, alpha-compositing devices use standard-format chunky color
  1025. even if the underlying device doesn't.
  1026. <p>
  1027. Closing a compositor frees all of its storage, including the compositor
  1028. itself. However, since the <b><tt>create_compositor</tt></b> call may
  1029. return the same device, clients must check for this case, and only call the
  1030. close procedure if a separate device was created.
  1031. </dl>
  1032. <p>
  1033. <font size="+1">
  1034. <b><em>[strip_]copy_rop WILL BE SUPERSEDED BY COMPOSITORS</em></b>
  1035. </font>
  1036. <dl>
  1037. <dt><b><tt>int (*copy_rop)(P15(gx_device&nbsp;*dev,
  1038. const&nbsp;byte&nbsp;*sdata, int&nbsp;sourcex, uint&nbsp;sraster,
  1039. gx_bitmap_id&nbsp;id, const&nbsp;gx_color_index&nbsp;*scolors,
  1040. const&nbsp;gx_tile_bitmap&nbsp;*texture,
  1041. const&nbsp;gx_color_index&nbsp;*tcolors, int&nbsp;x, int&nbsp;y,
  1042. int&nbsp;width, int&nbsp;height, int&nbsp;phase_x, int&nbsp;phase_y,
  1043. int&nbsp;command))</tt></b> <b><em>[OPTIONAL]</em></b>
  1044. <dd>This procedure is still supported, but has been superseded by
  1045. <b><tt>strip_copy_rop</tt></b>. New drivers should implement
  1046. <b><tt>strip_copy_rop</tt></b>; if they cannot cope with non-zero shift
  1047. values in the texture, they should test for this explicitly and call the
  1048. default implementation (<b><tt>gx_default_strip_copy_rop</tt></b>) if
  1049. shift&nbsp;!=&nbsp;0. Clients should call <b><tt>strip_copy_rop</tt></b>,
  1050. not <b><tt>copy_rop</tt></b>.
  1051. </dl>
  1052. <dl>
  1053. <dt><b><tt>int (*strip_copy_rop)(P15(gx_device&nbsp;*dev,
  1054. const&nbsp;byte&nbsp;*sdata, int&nbsp;sourcex, uint&nbsp;sraster,
  1055. gx_bitmap_id&nbsp;id, const&nbsp;gx_color_index&nbsp;*scolors,
  1056. const&nbsp;gx_strip_bitmap&nbsp;*texture,
  1057. const&nbsp;gx_color_index&nbsp;*tcolors, int&nbsp;x, int&nbsp;y,
  1058. int&nbsp;width, int&nbsp;height, int&nbsp;phase_x, int&nbsp;phase_y,
  1059. int&nbsp;command))</tt></b> <b><em>[OPTIONAL]</em></b>
  1060. <dd>Combine an optional source image <b>S</b> (as for
  1061. <b><tt>copy_mono</tt></b> or <b><tt>copy_color</tt></b>) and an optional
  1062. texture <b>T</b> (a tile, as for <b><tt>tile_rectangle</tt></b>) with the
  1063. existing bitmap or pixmap <b>D</b> held by the driver, pixel by pixel,
  1064. using any 3-input Boolean operation as modified by "transparency" flags:
  1065. schematically, set <b>D&nbsp;=&nbsp;f(D,S,T)</b>, computing <b>f</b> in RGB
  1066. space rather than using actual device pixel values. <b>S</b> and <b>T</b>
  1067. may each (independently) be a solid color, a bitmap with "foreground" and
  1068. "background" colors, or a pixmap. This is a complex (and currently rather
  1069. slow) operation. The arguments are as follows:
  1070. <blockquote><table cellpadding=0 cellspacing=0>
  1071. <tr valign=top> <td><b><tt>dev</tt></b>
  1072. <td>&nbsp;
  1073. <td>the device, as for all driver procedures
  1074. <tr valign=top> <td><b><tt>sdata</tt></b>, <b><tt>sourcex</tt></b>, <b><tt>sraster</tt></b>, <b><tt>id</tt></b>, <b><tt>scolors</tt></b>
  1075. <td>&nbsp;
  1076. <td>specify <b>S</b>, <a href="#S_spec">see below</a>
  1077. <tr valign=top> <td><b><tt>texture</tt></b>, <b><tt>tcolors</tt></b>
  1078. <td>&nbsp;
  1079. <td>specify <b>T</b>, <a href="#T_spec">see below</a>
  1080. <tr valign=top> <td><b><tt>x</tt></b>, <b><tt>y</tt></b>, <b><tt>width</tt></b>, <b><tt>height</tt></b>
  1081. <td>&nbsp;
  1082. <td>as for the other copy and fill procedures
  1083. <tr valign=top> <td><b><tt>phase_x</tt></b>, <b><tt>phase_y</tt></b>
  1084. <td>&nbsp;
  1085. <td>part of <b>T</b> specification, <a href="#T_spec">see below</a>
  1086. <tr valign=top> <td><b><tt>command</tt></b>
  1087. <td>&nbsp;
  1088. <td><a href="#F_spec">see below</a>
  1089. </table></blockquote>
  1090. </dl>
  1091. <h5><a name="S_spec"></a>The source specification S</h5>
  1092. <p>
  1093. As noted above, the source <b>S</b> may be a solid color, a bitmap, or a
  1094. pixmap. If <b>S</b> is a solid color:
  1095. <ul>
  1096. <li><b><tt>sdata</tt></b>, <b><tt>sourcex</tt></b>,
  1097. <b><tt>sraster</tt></b>, and <b><tt>id</tt></b> are irrelevant.
  1098. <li><b><tt>scolors</tt></b> points to two <b><tt>gx_color_index</tt></b>
  1099. values; <b><tt>scolors[0]</tt></b> = <b><tt>scolors[1]</tt></b> = the
  1100. color.
  1101. </ul>
  1102. <p>
  1103. If <b>S</b> is a bitmap:
  1104. <ul>
  1105. <li><b><tt>sdata</tt></b>, <b><tt>sourcex</tt></b>,
  1106. <b><tt>sraster</tt></b>, and <b><tt>id</tt></b> arguments are as for
  1107. <b><tt>copy_mono</tt></b> or <b><tt>copy_color</tt></b>
  1108. (<b><tt>data</tt></b>, <b><tt>data_x</tt></b>, <b><tt>raster</tt></b>,
  1109. <b><tt>id</tt></b>), and specify a source bitmap.
  1110. <li><b><tt>scolors</tt></b> points to two <b><tt>gx_color_index</tt></b>
  1111. values; <b><tt>scolors[0]</tt></b> is the background color (the color
  1112. corresponding to 0-bits in the bitmap), <b><tt>scolors[1]</tt></b> is the
  1113. foreground color (the color corresponding to 1-bits in the bitmap).
  1114. </ul>
  1115. <p>
  1116. If <b>S</b> is a pixmap:
  1117. <ul>
  1118. <li><b><tt>sdata</tt></b>, <b><tt>sourcex</tt></b>,
  1119. <b><tt>sraster</tt></b>, and <b><tt>id</tt></b> arguments are as for
  1120. <b><tt>copy_mono</tt></b> or <b><tt>copy_color</tt></b>
  1121. (<b><tt>data</tt></b>, <b><tt>data_x</tt></b>, <b><tt>raster</tt></b>,
  1122. <b><tt>id</tt></b>), and specify a source pixmap whose depth is the same as
  1123. the depth of the destination.
  1124. <li><b><tt>scolors</tt></b> is <b><tt>NULL</tt></b>.
  1125. </ul>
  1126. <p>
  1127. Note that if the source is a bitmap with background=0 and foreground=1, and
  1128. the destination is 1 bit deep, then the source can be treated as a pixmap
  1129. (scolors=<b><tt>NULL</tt></b>).
  1130. <h5><a name="T_spec"></a>The texture specification T</h5>
  1131. <p>
  1132. Similar to the source, the texture <b>T</b> may be a solid color, a bitmap,
  1133. or a pixmap. If <b>T</b> is a solid color:
  1134. <ul>
  1135. <li>The texture pointer is irrelevant.
  1136. <li><b><tt>tcolors</tt></b> points to two <b><tt>gx_color_index</tt></b>
  1137. values; <b><tt>tcolors[0]</tt></b> = <b><tt>tcolors[1]</tt></b> = the
  1138. color.
  1139. </ul>
  1140. <p>
  1141. If <b>T</b> is a bitmap:
  1142. <ul>
  1143. <li>The texture argument points to a <b><tt>gx_tile_bitmap</tt></b>, as for
  1144. the <b><tt>tile_rectangle</tt></b> procedure. Similarly,
  1145. <b><tt>phase_x</tt></b> and <b><tt>phase_y</tt></b> specify the offset of
  1146. the texture relative to the device coordinate system origin, again as for
  1147. <b><tt>tile_rectangle</tt></b>. The tile is a bitmap (1 bit per pixel).
  1148. <li><b><tt>tcolors</tt></b> points to two <b><tt>gx_color_index</tt></b>
  1149. values; <b><tt>tcolors[0]</tt></b> is the background color (the color
  1150. corresponding to 0-bits in the bitmap), <b><tt>tcolors[1]</tt></b> is the
  1151. foreground color (the color corresponding to 1-bits in the bitmap).
  1152. </ul>
  1153. <p>
  1154. If <b>T</b> is a pixmap:
  1155. <ul>
  1156. <li>The texture argument points to a <b><tt>gx_tile_bitmap</tt></b> whose
  1157. depth is the same as the depth of the destination.
  1158. <li>tcolors is <b><tt>NULL</tt></b>.
  1159. </ul>
  1160. <p>
  1161. Again, if the texture is a bitmap with background=0 and foreground=1, and
  1162. the destination depth is 1, the texture bitmap can be treated as a pixmap
  1163. (tcolors=<b><tt>NULL</tt></b>).
  1164. <p>
  1165. Note that while a source bitmap or pixmap has the same width and height as
  1166. the destination, a texture bitmap or pixmap has its own width and height
  1167. specified in the <b><tt>gx_tile_bitmap</tt></b> structure, and is
  1168. replicated or clipped as needed.
  1169. <h5><a name="F_spec"></a>The function specification f</h5>
  1170. <p>
  1171. "Command" indicates the raster operation and transparency as follows:
  1172. <blockquote><table cellpadding=0 cellspacing=0>
  1173. <tr valign=bottom>
  1174. <th>Bits
  1175. <td>&nbsp;
  1176. <td>&nbsp;
  1177. <tr valign=top> <td>7-0
  1178. <td>&nbsp;
  1179. <td>raster op
  1180. <tr valign=top> <td>8
  1181. <td>&nbsp;
  1182. <td>0 if source opaque, 1 if source transparent
  1183. <tr valign=top> <td>9
  1184. <td>&nbsp;
  1185. <td>0 if texture opaque, 1 if texture transparent
  1186. <tr valign=top> <td>?-10
  1187. <td>&nbsp;
  1188. <td>unused, must be 0
  1189. </table></blockquote>
  1190. <p>
  1191. The raster operation follows the Microsoft and H-P specification. It is an
  1192. 8-element truth table that specifies the output value for each of the
  1193. possible 2&times;2&times;2 input values as follows:
  1194. <blockquote><table cellpadding=0 cellspacing=0>
  1195. <tr valign=bottom>
  1196. <th>Bit
  1197. <td>&nbsp;
  1198. <th>Texture
  1199. <td>&nbsp;
  1200. <th>Source
  1201. <td>&nbsp;
  1202. <th>Destination
  1203. <tr> <td colspan=7><hr>
  1204. <tr valign=top> <td align=center>7
  1205. <td>&nbsp;
  1206. <td align=center>1
  1207. <td>&nbsp;
  1208. <td align=center>1
  1209. <td>&nbsp;
  1210. <td align=center>1
  1211. <tr valign=top> <td align=center>6
  1212. <td>&nbsp;
  1213. <td align=center>1
  1214. <td>&nbsp;
  1215. <td align=center>1
  1216. <td>&nbsp;
  1217. <td align=center>0
  1218. <tr valign=top> <td align=center>5
  1219. <td>&nbsp;
  1220. <td align=center>1
  1221. <td>&nbsp;
  1222. <td align=center>0
  1223. <td>&nbsp;
  1224. <td align=center>1
  1225. <tr valign=top> <td align=center>4
  1226. <td>&nbsp;
  1227. <td align=center>1
  1228. <td>&nbsp;
  1229. <td align=center>0
  1230. <td>&nbsp;
  1231. <td align=center>0
  1232. <tr valign=top> <td align=center>3
  1233. <td>&nbsp;
  1234. <td align=center>0
  1235. <td>&nbsp;
  1236. <td align=center>1
  1237. <td>&nbsp;
  1238. <td align=center>1
  1239. <tr valign=top> <td align=center>2
  1240. <td>&nbsp;
  1241. <td align=center>0
  1242. <td>&nbsp;
  1243. <td align=center>1
  1244. <td>&nbsp;
  1245. <td align=center>0
  1246. <tr valign=top> <td align=center>1
  1247. <td>&nbsp;
  1248. <td align=center>0
  1249. <td>&nbsp;
  1250. <td align=center>0
  1251. <td>&nbsp;
  1252. <td align=center>1
  1253. <tr valign=top> <td align=center>0
  1254. <td>&nbsp;
  1255. <td align=center>0
  1256. <td>&nbsp;
  1257. <td align=center>0
  1258. <td>&nbsp;
  1259. <td align=center>0
  1260. </table></blockquote>
  1261. <p>
  1262. Transparency affects the output in the following way. A source or texture
  1263. pixel is considered transparent if its value is all 1s (for instance, 1 for
  1264. bitmaps, <tt>0xffffff</tt> for 24-bit RGB pixmaps) <b><em>and</em></b> the
  1265. corresponding transparency bit is set in the command. For each pixel, the
  1266. result of the Boolean operation is written into the destination iff neither
  1267. the source nor the texture pixel is transparent. (Note that the H-P
  1268. RasterOp specification, on which this is based, specifies that if the
  1269. source and texture are both all 1s and the command specifies transparent
  1270. source and opaque texture, the result <b><em>should</em></b> be written in
  1271. the output. We think this is an error in the documentation.)
  1272. <h5><a name="Compositing_notes"></a>Notes</h5>
  1273. <p>
  1274. <b><tt>copy_rop</tt></b> is defined to operate on pixels in RGB space,
  1275. again following the H-P and Microsoft specification. For devices that
  1276. don't use RGB (or gray-scale with black = 0, white = all 1s) as their
  1277. native color representation, the implementation of <b><tt>copy_rop</tt></b>
  1278. must convert to RGB or gray space, do the operation, and convert back (or
  1279. do the equivalent of this). Here are the <b><tt>copy_rop</tt></b>
  1280. equivalents of the most important previous imaging calls. We assume the
  1281. declaration:
  1282. <blockquote><b><tt>
  1283. static const gx_color_index white2[2] = { 1, 1 };
  1284. </tt></b></blockquote>
  1285. <p>
  1286. Note that <b><tt>rop3_S</tt></b> may be replaced by any other Boolean operation.
  1287. For monobit devices, we assume that black = 1.
  1288. <blockquote>
  1289. <pre>/* For all devices: */
  1290. (*fill_rectangle)(dev, x, y, w, h, color) ==&gt;
  1291. { gx_color_index colors[2];
  1292. colors[0] = colors[1] = color;
  1293. (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id, colors,
  1294. NULL, colors /*irrelevant*/,
  1295. x, y, w, h, 0, 0, rop3_S);
  1296. }
  1297. /* For black-and-white devices only: */
  1298. (*copy_mono)(dev, base, sourcex, sraster, id,
  1299. x, y, w, h, (gx_color_index)0, (gx_color_index)1) ==&gt;
  1300. (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  1301. NULL, white2 /*irrelevant*/,
  1302. x, y, w, h, 0, 0, rop3_S);
  1303. /* For color devices, where neither color0 nor color1 is gx_no_color_index: */
  1304. (*copy_mono)(dev, base, sourcex, sraster, id,
  1305. x, y, w, h, color0, color1) ==&gt;
  1306. { gx_color_index colors[2];
  1307. colors[0] = color0, colors[1] = color1;
  1308. (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, colors,
  1309. NULL, white2 /*irrelevant*/,
  1310. x, y, w, h, 0, 0, rop3_S);
  1311. }
  1312. /* For black-and-white devices only: */
  1313. (*copy_mono)(dev, base, sourcex, sraster, id,
  1314. x, y, w, h, gx_no_color_index, (gx_color_index)1) ==&gt;
  1315. (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  1316. NULL, white2 /*irrelevant*/,
  1317. x, y, w, h, 0, 0,
  1318. rop3_S | lop_S_transparent);
  1319. /* For all devices: */
  1320. (*copy_color)(dev, base, sourcex, sraster, id,
  1321. x, y, w, h) ==&gt; [same as first copy_mono above]
  1322. /* For black-and-white devices only: */
  1323. (*tile_rectangle)(dev, tile, x, y, w, h,
  1324. (gx_color_index)0, (gx_color_index)1, px, py) ==&gt;
  1325. (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id,
  1326. white2 /*irrelevant*/,
  1327. tile, NULL,
  1328. x, y, w, h, px, py, rop3_T)
  1329. </pre></blockquote>
  1330. <h3><a name="Polygon_level_drawing"></a>Polygon-level drawing</h3>
  1331. <p>
  1332. In addition to the pixel-level drawing operations that take integer device
  1333. coordinates and pure device colors, the driver interface includes
  1334. higher-level operations that draw polygons using fixed-point coordinates,
  1335. possibly halftoned colors, and possibly a non-default logical operation.
  1336. <p>
  1337. The <b><tt>fill_</tt></b>* drawing operations all use the center-of-pixel
  1338. rule: a pixel is colored iff its center falls within the polygonal region
  1339. being filled. If a pixel center <em>(X+0.5,Y+0.5)</em> falls exactly on
  1340. the boundary, the pixel is filled iff the boundary is horizontal and the
  1341. filled region is above it, or the boundary is not horizontal and the filled
  1342. region is to the right of it.
  1343. <dl>
  1344. <dt><b><tt>int (*fill_trapezoid)(P10(gx_device&nbsp;*dev,
  1345. const&nbsp;gs_fixed_edge&nbsp;*left, const&nbsp;gs_fixed_edge&nbsp;*right,
  1346. fixed&nbsp;ybot, fixed&nbsp;ytop, bool&nbsp;swap_axes,
  1347. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1348. gs_logical_operation_t&nbsp;lop))</tt></b> <b><em>[OPTIONAL]</em></b>
  1349. <dd>Fill a trapezoid. The bottom and top edges are parallel to the x
  1350. axis, and are defined by <b><tt>ybot</tt></b> and <b><tt>ytop</tt></b>,
  1351. respectively. The left and right edges are defined by <b><tt>left</tt></b>
  1352. and <b><tt>right</tt></b>. Both of these respresent lines (<b><tt>gs_fixed_edge</tt></b>
  1353. is defined in <a href="../src/gxdevcli.h">gxdevcli.h</a> and consists
  1354. of <b><tt>gs_fixed_point</tt></b> <b><tt>start</tt></b> and <b><tt>end</tt></b> points).
  1355. The y coordinates of these lines need not have any specific relation to
  1356. <b><tt>ybot</tt></b> and <b><tt>ytop</tt></b>. The routine is defined this way so
  1357. that the filling algorithm can subdivide edges and still guarantee
  1358. that the exact same pixels will be filled. If
  1359. <b><tt>swap_axes</tt></b> is set, the meanings of X and Y are
  1360. interchanged.
  1361. </dl>
  1362. <dl>
  1363. <dt><b><tt>int (*fill_parallelogram)(P9(gx_device&nbsp;*dev,
  1364. fixed&nbsp;px, fixed&nbsp;py, fixed&nbsp;ax, fixed&nbsp;ay, fixed&nbsp;bx,
  1365. fixed&nbsp;by, const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1366. gs_logical_operation_t&nbsp;lop))</tt></b> <b><em>[OPTIONAL]</em></b>
  1367. <dd>Fill a parallelogram whose corners are <em>(px,py)</em>,
  1368. <em>(px+ax,py+ay)</em>, <em>(px+bx,py+by)</em>, and
  1369. <em>(px+ax+bx,py+ay+by)</em>. There are no constraints on the values of
  1370. any of the parameters, so the parallelogram may have any orientation
  1371. relative to the coordinate axes.
  1372. </dl>
  1373. <dl>
  1374. <dt><b><tt>int (*fill_triangle)(P9(gx_device&nbsp;*dev, fixed&nbsp;px,
  1375. fixed&nbsp;py, fixed&nbsp;ax, fixed&nbsp;ay, fixed&nbsp;bx, fixed&nbsp;by,
  1376. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1377. gs_logical_operation_t&nbsp;lop))</tt></b> <b><em>[OPTIONAL]</em></b>
  1378. <dd>Fill a triangle whose corners are <em>(px,py)</em>,
  1379. <em>(px+ax,py+ay)</em>, and <em>(px+bx,py+by)</em>.
  1380. </dl>
  1381. <dl>
  1382. <dt><b><tt>int (*draw_thin_line)(P7(gx_device&nbsp;*dev,
  1383. fixed&nbsp;fx0, fixed&nbsp;fy0, fixed&nbsp;fx1, fixed&nbsp;fy1,
  1384. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1385. gs_logical_operation_t&nbsp;lop))</tt></b> <b><em>[OPTIONAL]</em></b>
  1386. <dd>Draw a one-pixel-wide line from <em>(fx0,fy0)</em> to
  1387. <em>(fx1,fy1)</em>.
  1388. </dl>
  1389. <dl>
  1390. <dt><b><tt>int (*draw_line)(P6(gx_device&nbsp;*dev, int&nbsp;x0, int&nbsp;y0,
  1391. int&nbsp;x1, int&nbsp;y1, gx_color_index&nbsp;color))</tt></b>
  1392. <b><em>[OPTIONAL] [OBSOLETE]</em></b>
  1393. <dd>This procedure is no longer used: it is replaced by the draw_thin_line
  1394. procedure. However, still appears in the driver procedure vector for
  1395. backward compatibility. It should never be called, and drivers should not
  1396. implement it.
  1397. </dl>
  1398. <h3><a name="High_level_drawing"></a>High-level drawing</h3>
  1399. <p>
  1400. In addition to the lower-level drawing operations described above, the
  1401. driver interface provides a set of high-level operations. Normally these
  1402. will have their default implementation, which converts the high-level
  1403. operation to the low-level ones just described; however, drivers that
  1404. generate high-level output formats such as CGM, or communicate with devices
  1405. that have firmware for higher-level operations such as polygon fills, may
  1406. implement these high-level operations directly. For more details, please
  1407. consult the source code, specifically:
  1408. <blockquote><table cellpadding=0 cellspacing=0>
  1409. <tr valign=top> <th align=left>Header
  1410. <td>&nbsp;&nbsp;&nbsp;
  1411. <th align=left>Defines
  1412. <tr valign=top> <td><a href="../src/gxpaint.h">gxpaint.h</a>
  1413. <td>&nbsp;
  1414. <td><b><tt>gx_fill_params</tt></b>, <b><tt>gx_stroke_params</tt></b>
  1415. <tr valign=top> <td><a href="../src/gxfixed.h">gxfixed.h</a>
  1416. <td>&nbsp;
  1417. <td><b><tt>fixed</tt></b>, <b><tt>gs_fixed_point</tt></b> (used by
  1418. <b><tt>gx_*_params</tt></b>)
  1419. <tr valign=top> <td><a href="../src/gxistate.h">gxistate.h</a>
  1420. <td>&nbsp;
  1421. <td><b><tt>gs_imager_state</tt></b> (used by <b><tt>gx_*_params</tt></b>)
  1422. <tr valign=top> <td><a href="../src/gxline.h">gxline.h</a>
  1423. <td>&nbsp;
  1424. <td><b><tt>gx_line_params</tt></b> (used by <b><tt>gs_imager_state</tt></b>)
  1425. <tr valign=top> <td><a href="../src/gslparam.h">gslparam.h</a>
  1426. <td>&nbsp;
  1427. <td>line cap/join values (used by <b><tt>gx_line_params</tt></b>)
  1428. <tr valign=top> <td><a href="../src/gxmatrix.h">gxmatrix.h</a>
  1429. <td>&nbsp;
  1430. <td><b><tt>gs_matrix_fixed</tt></b> (used by <b><tt>gs_imager_state</tt></b>)
  1431. <tr valign=top> <td><a href="../src/gspath.h">gspath.h</a>, <a href="../src/gxpath.h">gxpath.h</a>, <a href="../src/gzpath.h">gzpath.h</a>
  1432. <td>&nbsp;
  1433. <td><b><tt>gx_path</tt></b>
  1434. <tr valign=top> <td><a href="../src/gxcpath.h">gxcpath.h</a>, <a href="../src/gzcpath.h">gzcpath.h</a>
  1435. <td>&nbsp;
  1436. <td><b><tt>gx_clip_path</tt></b>
  1437. </table></blockquote>
  1438. <p>
  1439. For a minimal example of how to implement the high-level drawing operations,
  1440. see <a href="../src/gdevtrac.c">gdevtrac.c</a>.
  1441. <h4><a name="Paths"></a>Paths</h4>
  1442. <dl>
  1443. <dt><b><tt>int (*fill_path)(P6(gx_device&nbsp;*dev,
  1444. const&nbsp;gs_imager_state&nbsp;*pis, gx_path&nbsp;*ppath,
  1445. const&nbsp;gx_fill_params&nbsp;*params,
  1446. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1447. const&nbsp;gx_clip_path&nbsp;*pcpath))</tt></b> <b><em>[OPTIONAL]</em></b>
  1448. <dd>Fill the given path, clipped by the given clip path, according to the
  1449. given parameters, with the given color. The clip path pointer may be
  1450. <b><tt>NULL</tt></b>, meaning do not clip.
  1451. </dl>
  1452. <dl>
  1453. <dt><b><tt>int (*stroke_path)(P6(gx_device&nbsp;*dev,
  1454. const&nbsp;gs_imager_state&nbsp;*pis, gx_path&nbsp;*ppath,
  1455. const&nbsp;gx_stroke_params&nbsp;*params,
  1456. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1457. const&nbsp;gx_clip_path&nbsp;*pcpath))</tt></b> <b><em>[OPTIONAL]</em></b>
  1458. <dd>Stroke the given path, clipped by the given clip path, according to the
  1459. given parameters, with the given color. The clip path pointer may be
  1460. <b><tt>NULL</tt></b>, meaning not to clip.
  1461. </dl>
  1462. <dl>
  1463. <dt><b><tt>int (*fill_mask)(P13(gx_device&nbsp;*dev,
  1464. const&nbsp;byte&nbsp;*data, int&nbsp;data_x, int&nbsp;raster,
  1465. gx_bitmap_id&nbsp;id, int&nbsp;x, int&nbsp;y, int&nbsp;width,
  1466. int&nbsp;height, const&nbsp;gx_drawing_color&nbsp;*pdcolor, int&nbsp;depth,
  1467. int&nbsp;command, const&nbsp;gx_clip_path&nbsp;*pcpath))</tt></b>
  1468. <b><em>[OPTIONAL]</em></b>
  1469. <dd>Color the 1-bits in the given mask (or according to the alpha values,
  1470. if <b><tt>depth</tt></b>&nbsp;&gt;&nbsp;1), clipped by the given clip path,
  1471. with the given color and logical operation. The clip path pointer may be
  1472. <b><tt>NULL</tt></b>, meaning do not clip. The parameters
  1473. <b><tt>data</tt></b>, ..., <b><tt>height</tt></b> are as for
  1474. <b><tt>copy_mono</tt></b>; depth is as for <b><tt>copy_alpha</tt></b>;
  1475. command is as for <b><tt>copy_rop</tt></b>.
  1476. </dl>
  1477. <h4><a name="Images"></a>Images</h4>
  1478. <p>
  1479. Similar to the high-level interface for fill and stroke graphics, a high-level
  1480. interface exists for bitmap images. The procedures in this part of the
  1481. interface are optional.
  1482. <p>
  1483. Bitmap images come in a variety of types, corresponding closely (but not
  1484. precisely) to the PostScript ImageTypes. The generic or common part of all
  1485. bitmap images is defined by:
  1486. <blockquote>
  1487. <pre>typedef struct {
  1488. const gx_image_type_t *type;
  1489. gs_matrix ImageMatrix;
  1490. } gs_image_common_t;</pre>
  1491. </blockquote>
  1492. <p>
  1493. Bitmap images that supply data (all image types except
  1494. <b><tt>image_type_from_device</tt></b> (2)) are defined by:
  1495. <blockquote>
  1496. <pre>#define gs_image_max_components 5
  1497. typedef struct {
  1498. &lt;&lt; gs_image_common_t &gt;&gt;
  1499. int Width;
  1500. int Height;
  1501. int BitsPerComponent;
  1502. float Decode[gs_image_max_components * 2];
  1503. bool Interpolate;
  1504. } gs_data_image_t;</pre>
  1505. </blockquote>
  1506. <p>
  1507. Images that supply pixel (as opposed to mask) data are defined by:
  1508. <blockquote>
  1509. <pre>typedef enum {
  1510. /* Single plane, chunky pixels. */
  1511. gs_image_format_chunky = 0,
  1512. /* num_components planes, chunky components. */
  1513. gs_image_format_component_planar = 1,
  1514. /* BitsPerComponent * num_components planes, 1 bit per plane */
  1515. gs_image_format_bit_planar = 2
  1516. } gs_image_format_t;
  1517. typedef struct {
  1518. &lt;&lt; gs_data_image_t &gt;&gt;
  1519. const gs_color_space *ColorSpace;
  1520. bool CombineWithColor;
  1521. } gs_pixel_image_t;</pre>
  1522. </blockquote>
  1523. <p>
  1524. Ordinary PostScript Level 1 or Level 2 (<b><tt>ImageType</tt></b> 1) images
  1525. are defined by:
  1526. <blockquote>
  1527. <pre>typedef enum {
  1528. /* No alpha. */
  1529. gs_image_alpha_none = 0,
  1530. /* Alpha precedes color components. */
  1531. gs_image_alpha_first,
  1532. /* Alpha follows color components. */
  1533. gs_image_alpha_last
  1534. } gs_image_alpha_t;
  1535. typedef struct {
  1536. &lt;&lt; gs_pixel_image_t &gt;&gt;
  1537. bool ImageMask;
  1538. bool adjust;
  1539. gs_image_alpha_t Alpha;
  1540. } gs_image1_t;
  1541. typedef gs_image1_t gs_image_t;</pre>
  1542. </blockquote>
  1543. <p>
  1544. Of course, standard PostScript images don't have an alpha component. For
  1545. more details, consult the source code in <a
  1546. href="../src/gsiparam.h">gsiparam.h</a> and <b><tt>gsiparm*.h</tt></b>,
  1547. which define parameters for an image.
  1548. <p>
  1549. The <b><tt>begin[_typed_]image</tt></b> driver procedures create image
  1550. enumeration structures. The common part of these structures consists of:
  1551. <blockquote>
  1552. <pre>typedef struct gx_image_enum_common_s {
  1553. const gx_image_type_t *image_type;
  1554. const gx_image_enum_procs_t *procs;
  1555. gx_device *dev;
  1556. gs_id id;
  1557. int num_planes;
  1558. int plane_depths[gs_image_max_planes]; /* [num_planes] */
  1559. int plane_widths[gs_image_max_planes] /* [num_planes] */
  1560. } gx_image_enum_common_t;</pre>
  1561. </blockquote>
  1562. <p>
  1563. where <b><tt>procs</tt></b> consists of:
  1564. <blockquote>
  1565. <pre>typedef struct gx_image_enum_procs_s {
  1566. /*
  1567. * Pass the next batch of data for processing.
  1568. */
  1569. #define image_enum_proc_plane_data(proc)\
  1570. int proc(P4(gx_device *dev,\
  1571. gx_image_enum_common_t *info, const gx_image_plane_t *planes,\
  1572. int height))
  1573. image_enum_proc_plane_data((*plane_data));
  1574. /*
  1575. * End processing an image, freeing the enumerator.
  1576. */
  1577. #define image_enum_proc_end_image(proc)\
  1578. int proc(P3(gx_device *dev,\
  1579. gx_image_enum_common_t *info, bool draw_last))
  1580. image_enum_proc_end_image((*end_image));
  1581. /*
  1582. * Flush any intermediate buffers to the target device.
  1583. * We need this for situations where two images interact
  1584. * (currently, only the mask and the data of ImageType 3).
  1585. * This procedure is optional (may be 0).
  1586. */
  1587. #define image_enum_proc_flush(proc)\
  1588. int proc(P1(gx_image_enum_common_t *info))
  1589. image_enum_proc_flush((*flush));
  1590. } gx_image_enum_procs_t;</pre>
  1591. </blockquote>
  1592. <p> In other words, <b><tt>begin[_typed]_image</tt></b> sets up an
  1593. enumeration structure that contains the procedures that will process the
  1594. image data, together with all variables needed to maintain the state of the
  1595. process. Since this is somewhat tricky to get right, if you plan to create
  1596. one of your own you should probably read an existing implementation of
  1597. <b><tt>begin[_typed]_image</tt></b>, such as the one in <a
  1598. href="../src/gdevbbox.c">gdevbbox.c</a> or <a
  1599. href="../src/gdevps.c">gdevps.c</a>.
  1600. <p>
  1601. The data passed at each call of <b><tt>image_plane_data</tt></b> consists of
  1602. one or more planes, as appropriate for the type of image.
  1603. <b><tt>begin[_typed]_image</tt></b> must initialize the
  1604. <b><tt>plane_depths</tt></b> array in the enumeration structure with the
  1605. depths (bits per element) of the planes. The array of
  1606. <b><tt>gx_image_plane_t</tt></b> structures passed to each call of
  1607. <b><tt>image_plane_data</tt></b> then defines where the data are stored, as
  1608. follows:
  1609. <blockquote>
  1610. <pre>typedef struct gx_image_plane_s {
  1611. const byte *data;
  1612. int data_x;
  1613. uint raster;
  1614. } gx_image_plane_t;</pre>
  1615. </blockquote>
  1616. <dl>
  1617. <dt><b><tt>int (*begin_image)(P9(gx_device&nbsp;*dev,
  1618. const&nbsp;gs_imager_state&nbsp;*pis, const&nbsp;gs_image_t&nbsp;*pim,
  1619. gs_image_format_t&nbsp;format, gs_int_rect&nbsp;*prect,
  1620. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1621. const&nbsp;gx_clip_path&nbsp;*pcpath, gs_memory_t&nbsp;*memory,
  1622. gx_image_enum_common_t&nbsp;**pinfo))</tt></b> <b><em>[OPTIONAL]</em></b>
  1623. <dd>Begin the transmission of an image. Zero or more calls of
  1624. <b><tt>image_plane_data</tt></b> will follow, and then a call of
  1625. <b><tt>end_image</tt></b>. The parameters of <b><tt>begin_image</tt></b>
  1626. are as follows:
  1627. <blockquote><table cellpadding=0 cellspacing=0>
  1628. <tr valign=top> <td><b><tt>pis</tt></b>
  1629. <td>&nbsp;&nbsp;&nbsp;
  1630. <td>pointer to an imager state. The only relevant elements of the
  1631. imager state are the CTM (coordinate transformation matrix),
  1632. the logical operation (<b><tt>RasterOp</tt></b> or
  1633. transparency), and the color rendering information.
  1634. <tr valign=top> <td><b><tt>pim</tt></b>
  1635. <td>&nbsp;
  1636. <td>pointer to the <b><tt>gs_image_t</tt></b> structure that
  1637. defines the image parameters
  1638. <tr valign=top> <td><b><tt>format</tt></b>
  1639. <td>&nbsp;
  1640. <td>defines how pixels are represented for
  1641. <b><tt>image_plane_data</tt></b>. See the description of
  1642. <b><tt>image_plane_data</tt></b> below
  1643. <tr valign=top> <td><b><tt>prect</tt></b>
  1644. <td>&nbsp;
  1645. <td>if not <b><tt>NULL</tt></b>, defines a subrectangle of the
  1646. image; only the data for this subrectangle will be passed to
  1647. <b><tt>image_plane_data</tt></b>, and only this subrectangle should
  1648. be drawn
  1649. <tr valign=top> <td><b><tt>pdcolor</tt></b>
  1650. <td>&nbsp;
  1651. <td>defines a drawing color, only needed for masks or if
  1652. <b><tt>CombineWithColor</tt></b> is true
  1653. <tr valign=top> <td><b><tt>pcpath</tt></b>
  1654. <td>&nbsp;
  1655. <td>if not <b><tt>NULL</tt></b>, defines an optional clipping path
  1656. <tr valign=top> <td><b><tt>memory</tt></b>
  1657. <td>&nbsp;
  1658. <td>defines the allocator to be used for allocating bookkeeping
  1659. information
  1660. <tr valign=top> <td><b><tt>pinfo</tt></b>
  1661. <td>&nbsp;
  1662. <td>the implementation should return a pointer to its state
  1663. structure here
  1664. </table></blockquote>
  1665. <p>
  1666. <b><tt>begin_image</tt></b> is expected to allocate a structure for its
  1667. bookkeeping needs, using the allocator defined by the memory parameter, and
  1668. return it in <b><tt>*pinfo</tt></b>. <b><tt>begin_image</tt></b> should not assume that
  1669. the structures in <b><tt>*pim</tt></b>, <b><tt>*prect</tt></b>, or
  1670. <b><tt>*pdcolor</tt></b> will survive the call on
  1671. <b><tt>begin_image</tt></b> (except for the color space in
  1672. <b><tt>*pim-&gt;ColorSpace</tt></b>): it should copy any necessary parts of
  1673. them into its own bookkeeping structure. It may, however, assume that
  1674. <b><tt>*pis</tt></b>, <b><tt>*pcpath</tt></b>, and of course
  1675. <b><tt>*memory</tt></b> will live at least until <b><tt>end_image</tt></b>
  1676. is called.
  1677. <p>
  1678. <b><tt>begin_image</tt></b> returns 0 normally, or 1 if the image does not
  1679. need any data. In the latter case, <b><tt>begin_image</tt></b> does not
  1680. allocate an enumeration structure.
  1681. </dl>
  1682. <dl>
  1683. <dt><b><tt>int (*begin_typed_image)(P9(gx_device&nbsp;*dev,
  1684. const&nbsp;gs_imager_state&nbsp;*pis, const&nbsp;gs_matrix&nbsp;*pmat,
  1685. const&nbsp;gs_image_common_t&nbsp;*pim, gs_int_rect&nbsp;*prect,
  1686. const&nbsp;gx_drawing_color&nbsp;*pdcolor,
  1687. const&nbsp;gx_clip_path&nbsp;*pcpath, gs_memory_t&nbsp;*memory,
  1688. gx_image_enum_common_t&nbsp;**pinfo))</tt></b> <b><em>[OPTIONAL]</em></b>
  1689. <dd>This has the same function as <b><tt>begin_image</tt></b>, except
  1690. <ul>
  1691. <li>The image may be of any <b><tt>ImageType</tt></b>, not only
  1692. <b><tt>image_type_simple</tt></b> (1);
  1693. <li>The image format is included in the image structure, not supplied as a
  1694. separate argument;
  1695. <li>The optional <b><tt>pmat</tt></b> argument provides a matrix that
  1696. substitutes for the one in the imager state;
  1697. <li>For mask images, if <b><tt>pmat</tt></b> is not <b><tt>NULL</tt></b>
  1698. and the color is pure, <b><tt>pis</tt></b> may be <b><tt>NULL</tt></b>.
  1699. </ul>
  1700. </dl>
  1701. <p>
  1702. The actual transmission of data uses the procedures in the enumeration
  1703. structure, not driver procedures, since the handling of the data usually
  1704. depends on the image type and parameters rather than the device. These
  1705. procedures are specified as follows.
  1706. <dl>
  1707. <dt><b><tt>int (*image_plane_data)(P4(gx_device&nbsp;*dev,
  1708. gx_image_enum_common_t&nbsp;*info,
  1709. const&nbsp;gx_image_plane_t&nbsp;*planes, int&nbsp;height))</tt></b>
  1710. <dd>This call provides more of the image source data: specifically,
  1711. <b><tt>height</tt></b> rows, with <b><tt>Width</tt></b> pixels supplied for
  1712. each row.
  1713. <p>
  1714. The data for each row are packed big-endian within each byte, as for
  1715. <b><tt>copy_color</tt></b>. The <b><tt>data_x</tt></b> (starting X position
  1716. within the row) and <b><tt>raster</tt></b> (number of bytes per row) are
  1717. specified separately for each plane, and may include some padding at the
  1718. beginning or end of each row. Note that for non-mask images, the input data
  1719. may be in any color space and may have any number of bits per component (1,
  1720. 2, 4, 8, 12); currently mask images always have 1 bit per component, but in
  1721. the future, they might allow multiple bits of alpha. Note also that each
  1722. call of <b><tt>image_plane_data</tt></b> passes complete pixels: for example, for
  1723. a chunky image with 24 bits per pixel, each call of
  1724. <b><tt>image_plane_data</tt></b> passes 3N bytes of data (specifically,
  1725. 3&nbsp;&times;&nbsp;Width&nbsp;&times;&nbsp;height).
  1726. <p>
  1727. The interpretation of planes depends on the <b><tt>format</tt></b> member of
  1728. the <b><tt>gs_image[_common]_t</tt></b> structure:
  1729. <ul>
  1730. <li>If the format is <b><tt>gs_image_format_chunky</tt></b>,
  1731. <b><tt>planes[0].data</tt></b> points to data in "chunky" format, in which
  1732. the components follow each other (for instance, RGBRGBRGB....)
  1733. <li>If the format is <b><tt>gs_image_format_component_planar</tt></b>,
  1734. <b><tt>planes[0&nbsp;..&nbsp;N-1].data</tt></b> point to data for the
  1735. <b><em>N</em></b> components (for example, <b><em>N</em></b>=3 for RGB
  1736. data); each plane contains samples for a single component, for instance,
  1737. RR..., GG..., BB.... Note that the planes are divided by component, not by
  1738. bit: for example, for 24-bit RGB data, <b><em>N</em></b>=3, with 8-bit
  1739. values in each plane of data.
  1740. <li>If the format is <b><tt>gs_image_format_bit_planar</tt></b>,
  1741. <b><tt>planes[0&nbsp;..&nbsp;N*B-1].data</tt></b> point to data for the
  1742. <b><em>N</em></b> components of <b><em>B</em></b> bits each (for example,
  1743. <b><em>N</em></b>=3 and <b><em>B</em></b>=4 for RGB data with 4 bits per
  1744. component); each plane contains samples for a single bit, for instance, R0
  1745. R1 R2 R3 G0 G1 G2 G3 B0 B1 B2 B3. Note that the most significant bit of
  1746. each plane comes first.
  1747. </ul>
  1748. <p>
  1749. If, as a result of this call, <b><tt>image_plane_data</tt></b> has been called with all
  1750. the data for the (sub-)image, it returns 1; otherwise, it returns 0 or an
  1751. error code as usual.
  1752. <p>
  1753. <b><tt>image_plane_data</tt></b>, unlike most other procedures that take bitmaps as
  1754. arguments, does not require the data to be aligned in any way.
  1755. <p>
  1756. Note that for some image types, different planes may have different
  1757. numbers of bits per pixel, as defined in the <b><tt>plane_depths</tt></b> array.
  1758. </dl>
  1759. <dl>
  1760. <dt><b><tt>int (*end_image)(P3(gx_device&nbsp;*dev, void&nbsp;*info,
  1761. bool&nbsp;draw_last))</tt></b>
  1762. <dd>Finish processing an image, either because all data have been supplied
  1763. or because the caller has decided to abandon this image.
  1764. <b><tt>end_image</tt></b> may be called at any time after
  1765. <b><tt>begin_image</tt></b>. It should free the info structure and any
  1766. subsidiary structures. If <b><tt>draw_last</tt></b> is true, it should
  1767. finish drawing any buffered lines of the image.
  1768. </dl>
  1769. <h5><a name="Images_notes"></a>Notes</h5>
  1770. <p>
  1771. While there will almost never be more than one image enumeration in
  1772. progress -- that is, after a <b><tt>begin_image</tt></b>,
  1773. <b><tt>end_image</tt></b> will almost always be called before the next
  1774. <b><tt>begin_image</tt></b> -- driver code should not rely on this
  1775. property; in particular, it should store all information regarding the
  1776. image in the info structure, not in the driver structure.
  1777. <p>
  1778. Note that if <b><tt>begin_[typed_]image</tt></b> saves its parameters in
  1779. the info structure, it can decide on each call whether to use its own
  1780. algorithms or to use the default implementation. (It may need to call
  1781. <b><tt>gx_default_begin</tt></b>/<b><tt>end_image</tt></b> partway
  1782. through.) [A later revision of this document may include an example here.]
  1783. <h4><a name="Text"></a>Text</h4>
  1784. <p>
  1785. The third high-level interface handles text. As for images, the interface
  1786. is based on creating an enumerator which then may execute the operation in
  1787. multiple steps. As for the other high-level interfaces, the procedures are
  1788. optional.
  1789. <dl>
  1790. <dt><b><tt>int (*text_begin)(P9(gx_device&nbsp;*dev,
  1791. gs_imager_state&nbsp;*pis, const&nbsp;gs_text_params_t&nbsp;*text,
  1792. gs_font&nbsp;*font, gx_path&nbsp;*path,
  1793. const&nbsp;gx_device_color&nbsp;*pdcolor,
  1794. const&nbsp;gx_clip_path&nbsp;*pcpath, gs_memory_t&nbsp;*memory,
  1795. gs_text_enum_t&nbsp;**ppte))</tt></b> <b><em>[OPTIONAL]</em></b>
  1796. <dd>
  1797. Begin processing text, by creating a state structure and storing it in
  1798. <b><tt>*ppte</tt></b>. The parameters of <b><tt>text_begin</tt></b> are as
  1799. follows:
  1800. </dl>
  1801. <blockquote><table cellpadding=0 cellspacing=0>
  1802. <tr valign=top> <td><b><tt>dev</tt></b>
  1803. <td>&nbsp;&nbsp;&nbsp;
  1804. <td>The usual pointer to the device.
  1805. <tr valign=top> <td><b><tt>pis</tt></b>
  1806. <td>&nbsp;&nbsp;&nbsp;
  1807. <td>A pointer to an imager state. All elements may be relevant,
  1808. depending on how the text is rendered.
  1809. <tr valign=top> <td><b><tt>text</tt></b>
  1810. <td>&nbsp;
  1811. <td>A pointer to the structure that defines the text operation
  1812. and parameters. See <a href="../src/gstext.h">gstext.h</a> for details.
  1813. <tr valign=top> <td><b><tt>font</tt></b>
  1814. <td>&nbsp;
  1815. <td>Defines the font for drawing.
  1816. <tr valign=top> <td><b><tt>path</tt></b>
  1817. <td>&nbsp;
  1818. <td>Defines the path where the character outline will be appended
  1819. (if the text operation includes <b><tt>TEXT_DO_...PATH</tt></b>),
  1820. and whose current point indicates where drawing should occur
  1821. and will be updated by the string width (unless the text
  1822. operation includes <b><tt>TEXT_DO_NONE</tt></b>).
  1823. <tr valign=top> <td><b><tt>pdcolor</tt></b>
  1824. <td>&nbsp;
  1825. <td>Defines the drawing color for the text. Only relevant if
  1826. the text operation includes <b><tt>TEXT_DO_DRAW</tt></b>.
  1827. <tr valign=top> <td><b><tt>pcpath</tt></b>
  1828. <td>&nbsp;
  1829. <td>If not <b><tt>NULL</tt></b>, defines an optional clipping path.
  1830. Only relevant if the text operation includes
  1831. <b><tt>TEXT_DO_DRAW</tt></b>.
  1832. <tr valign=top> <td><b><tt>memory</tt></b>
  1833. <td>&nbsp;
  1834. <td>Defines the allocator to be used for allocating bookkeeping
  1835. information.
  1836. <tr valign=top> <td><b><tt>ppte</tt></b>
  1837. <td>&nbsp;
  1838. <td>The implementation should return a pointer to its state
  1839. structure here.
  1840. </table></blockquote>
  1841. <p>
  1842. <b><tt>text_begin</tt></b> must allocate a structure for its bookkeeping
  1843. needs, using the allocator defined by the <b><tt>memory</tt></b> parameter,
  1844. and return it in <b><tt>*ppte</tt></b>. <b><tt>text_begin</tt></b> may
  1845. assume that the structures passed as parameters will survive until text
  1846. processing is complete.
  1847. <p>
  1848. Clients should not call the driver <b><tt>text_begin</tt></b> procedure
  1849. directly. Instead, they should call <b><tt>gx_device_text_begin</tt></b>,
  1850. which takes the same parameters and also initializes certain common elements
  1851. of the text enumeration structure, or <b><tt>gs_text_begin</tt></b>, which
  1852. takes many of the parameters from a graphics state structure. For details,
  1853. see <a href="../src/gstext.h">gstext.h</a>.
  1854. <p>
  1855. The actual processing of text uses the procedures in the enumeration
  1856. structure, not driver procedures, since the handling of the text may depend
  1857. on the font and parameters rather than the device. Text processing may also
  1858. require the client to take action between characters, either because the
  1859. client requested it (<b><tt>TEXT_INTERVENE</tt></b> in the operation) or
  1860. because rendering a character requires suspending text processing to call an
  1861. external package such as the PostScript interpreter. (It is a deliberate
  1862. design decision to handle this by returning to the client, rather than
  1863. calling out of the text renderer, in order to avoid potentially unknown
  1864. stack requirements.) Specifically, the client must call the following
  1865. procedures, which in turn call the procedures in the text enumerator.
  1866. <dl>
  1867. <dt><b><tt>int gs_text_process(P1(gs_text_enum_t&nbsp;*pte))</tt></b>
  1868. <dd>Continue processing text. This procedure may return 0 or a negative
  1869. error code as usual, or one of the following values (see
  1870. <a href="../src/gstext.h">gstext.h</a> for details).
  1871. <blockquote><table cellpadding=0 cellspacing=0>
  1872. <tr valign=top> <td><b><tt>TEXT_PROCESS_RENDER</tt></b>
  1873. <td>The client must cause the current character to be rendered.
  1874. This currently only is used for PostScript Type 0-4 fonts
  1875. and their CID-keyed relatives.
  1876. <tr valign=top> <td><b><tt>TEXT_PROCESS_INTERVENE</tt></b>
  1877. <td>The client has asked to intervene between characters.
  1878. This is used for <b><tt>cshow</tt></b> and <b><tt>kshow</tt></b>.
  1879. </table></blockquote>
  1880. </dl>
  1881. <dl>
  1882. <dt><b><tt>int gs_text_release(P2(gs_text_enum_t&nbsp;*pte,
  1883. client_name_t&nbsp;cname))</tt></b> <dd>Finish processing text and release
  1884. all associated structures. Clients must call this procedure after
  1885. <b><tt>gs_text_process</tt></b> returns 0 or an error, and may call it at
  1886. any time.
  1887. </dl>
  1888. <p>
  1889. There are numerous other procedures that clients may call during text
  1890. processing. See <a href="../src/gstext.h">gstext.h</a> for details.
  1891. <h5><a name="Text_notes"></a>Notes</h5>
  1892. <p>
  1893. Note that unlike many other optional procedures, the default implementation
  1894. of <b><tt>text_begin</tt></b> cannot simply return: like the default
  1895. implementation of <b><tt>begin[_typed]_image</tt></b>, it must create and
  1896. return an enumerator. Furthermore, the implementation of the
  1897. <b><tt>process</tt></b> procedure (in the enumerator structure, called by
  1898. <b><tt>gs_text_process</tt></b>) cannot simply return without doing
  1899. anything, even if it doesn't want to draw anything on the output. See the
  1900. comments in <a href="../src/gxtext.h">gxtext.h</a> for details.
  1901. <h3><a name="Reading_bits_back"></a>Reading bits back</h3>
  1902. <dl>
  1903. <dt><b><tt>int (*get_bits_rectangle)(P4(gx_device&nbsp;*dev,
  1904. const&nbsp;gs_int_rect&nbsp;*prect, gs_get_bits_params_t&nbsp;*params,
  1905. gs_int_rect&nbsp;**unread))</tt></b> <b><em>[OPTIONAL]</em></b>
  1906. <dd>
  1907. Read a rectangle of bits back from the device. The <b><tt>params</tt></b>
  1908. structure consists of:
  1909. <table cellpadding=0 cellspacing=0>
  1910. <tr valign=top> <td><b><tt>options</tt></b>
  1911. <td>&nbsp;
  1912. <td>the allowable formats for returning the data
  1913. <tr valign=top> <td><b><tt>data[32]</tt></b>
  1914. <td>&nbsp;
  1915. <td>pointers to the returned data
  1916. <tr valign=top> <td><b><tt>x_offset</tt></b>
  1917. <td>&nbsp;
  1918. <td>the X offset of the first returned pixel in data
  1919. <tr valign=top> <td><b><tt>raster</tt></b>
  1920. <td>&nbsp;
  1921. <td>the distance between scan lines in the returned data
  1922. </table>
  1923. <p>
  1924. <b><tt>options</tt></b> is a bit mask specifying what formats the client is
  1925. willing to accept. (If the client has more flexibility, the implementation
  1926. may be able to return the data more efficiently, by avoiding representation
  1927. conversions.) The options are divided into groups.
  1928. <blockquote><dl>
  1929. <dt><b><em>alignment</em></b>
  1930. <dd>Specifies whether the returned data must be aligned in the normal
  1931. manner for bitmaps, or whether unaligned data are acceptable.
  1932. <dt><b><em>pointer or copy</em></b>
  1933. <dd>Specifies whether the data may be copied into storage provided by the
  1934. client and/or returned as pointers to existing storage. (Note that if
  1935. copying is not allowed, it is much more likely that the implementation will
  1936. return an error, since this requires that the client accept the data in the
  1937. implementation's internal format.)
  1938. <dt><b><em>X offset</em></b>
  1939. <dd>Specifies whether the returned data must have a specific X offset
  1940. (usually zero, but possibly other values to avoid skew at some later stage
  1941. of processing) or whether it may have any X offset (which may avoid skew in
  1942. the <b><tt>get_bits_rectangle</tt></b> operation itself).
  1943. <dt><b><em>raster</em></b>
  1944. <dd>Specifies whether the raster (distance between returned scan lines)
  1945. must have its standard value, must have some other specific value, or may
  1946. have any value. The standard value for the raster is the device width
  1947. padded out to the alignment modulus when using pointers, or the minimum
  1948. raster to accommodate the X offset + width when copying (padded out to the
  1949. alignment modulus if standard alignment is required).
  1950. <dt><b><em>format</em></b>
  1951. <dd>Specifies whether the data are returned in chunky (all components of a
  1952. single pixel together), component-planar (each component has its own scan
  1953. lines), or bit-planar (each bit has its own scan lines) format.
  1954. <dt><b><em>color space</em></b>
  1955. <dd>Specifies whether the data are returned as native device pixels, or in
  1956. a standard color space. Currently the only supported standard space is
  1957. RGB.
  1958. <dt><b><em>standard component depth</em></b>
  1959. <dd>Specifies the number of bits per component if the data are returned in
  1960. the standard color space. (Native device pixels use
  1961. <b><tt>dev</tt></b>-&gt;<b><tt>color_info.depth</tt></b> bits per pixel.)
  1962. <dt><b><em>alpha</em></b>
  1963. <dd>Specifies whether alpha channel information should be returned as the
  1964. first component, the last component, or not at all. Note that for devices
  1965. that have no alpha capability, the returned alpha values will be all 1s.
  1966. </dl></blockquote>
  1967. <p>
  1968. The client may set more than one option in each of the above groups; the
  1969. implementation will choose one of the selected options in each group to
  1970. determine the actual form of the returned data, and will update
  1971. <b><tt>params[].options</tt></b> to indicate the form. The returned
  1972. <b><tt>params[].options</tt></b> will normally have only one option set per
  1973. group.
  1974. <p>
  1975. For further details on <b><tt>params</tt></b>, see <a
  1976. href="../src/gxgetbit.h">gxgetbit.h</a>. For further details on
  1977. <b><tt>options</tt></b>, see <a href="../src/gxbitfmt.h">gxbitfmt.h</a>.
  1978. <p>
  1979. Define w = <b><tt>prect</tt></b>-&gt;q.x - <b><tt>prect</tt></b>-&gt;p.x, h
  1980. = <b><tt>prect</tt></b>-&gt;q.y - <b><tt>prect</tt></b>-&gt;p.y. If the
  1981. bits cannot be read back (for example, from a printer), return
  1982. <b><tt>gs_error_unknownerror</tt></b>; if raster bytes is not enough space
  1983. to hold <b><tt>offset_x</tt></b> + w pixels, or if the source rectangle
  1984. goes outside the device dimensions (p.x &lt; 0 || p.y &lt; 0 || q.x &gt;
  1985. <b><tt>dev</tt></b>-&gt;width || q.y &gt; <b><tt>dev</tt></b>-&gt;height),
  1986. return <b><tt>gs_error_rangecheck</tt></b>; if any regions could not be
  1987. read, return <b><tt>gs_error_ioerror</tt></b> if unpainted is
  1988. <b><tt>NULL</tt></b>, otherwise the number of rectangles (see below);
  1989. otherwise return 0.
  1990. <p>
  1991. The caller supplies a buffer of <b><tt>raster</tt></b>&nbsp;&times;&nbsp;h
  1992. bytes starting at <b><tt>data[0]</tt></b> for the returned data in chunky
  1993. format, or <b><em>N</em></b> buffers of
  1994. <b><tt>raster</tt></b>&nbsp;&times;&nbsp;h bytes starting at
  1995. <b><tt>data[0]</tt></b> through
  1996. <b><tt>data[</tt></b><b><em>N-1</em></b><b><tt>]</tt></b> in planar format
  1997. where <b><em>N</em></b> is the number of components or bits. The contents
  1998. of the bits beyond the last valid bit in each scan line (as defined by w)
  1999. are unpredictable. data need not be aligned in any way. If
  2000. <b><tt>x_offset</tt></b> is non-zero, the bits before the first valid bit
  2001. in each scan line are undefined. If the implementation returns pointers to
  2002. the data, it stores them into <b><tt>data[0]</tt></b> or
  2003. <b><tt>data[</tt></b><b><em>0..N-1</em></b><b><tt>]</tt></b>.
  2004. <p>
  2005. If not all the source data are available (for example, because the source
  2006. was a partially obscured window and backing store was not available or not
  2007. used), or if the rectangle does not fall completely within the device's
  2008. coordinate system, any unread bits are undefined, and the value returned
  2009. depends on whether unread is <b><tt>NULL</tt></b>. If unread is
  2010. <b><tt>NULL</tt></b>, return <b><tt>gs_error_ioerror</tt></b>; in this case,
  2011. some bits may or may not have been read. If unread is not
  2012. <b><tt>NULL</tt></b>, allocate (using <b><tt>dev</tt></b>-&gt;memory) and
  2013. fill in a list of rectangles that could not be read, store the pointer to
  2014. the list in <b><tt>*unread</tt></b>, and return the number of rectangles; in
  2015. this case, all bits not listed in the rectangle list have been read back
  2016. properly. The list is not sorted in any particular order, but the
  2017. rectangles do not overlap. Note that the rectangle list may cover a
  2018. superset of the region actually obscured: for example, a lazy implementation
  2019. could return a single rectangle that was the bounding box of the region.
  2020. </dl>
  2021. <dl>
  2022. <dt><b><tt>int (*get_bits)(P4(gx_device&nbsp;*dev, int&nbsp;y,
  2023. byte&nbsp;*data, byte&nbsp;**actual_data))</tt></b>
  2024. <b><em>[OPTIONAL]</em></b>
  2025. <dd>Read scan line <b><tt>y</tt></b> of bits back from the device into the
  2026. area starting at data. This call is functionally equivalent to
  2027. <blockquote>
  2028. <pre>(*get_bits_rectangle)
  2029. (dev, {0, y, dev-&gt;width, y+1},
  2030. {(GB_ALIGN_ANY | (GB_RETURN_COPY | GB_RETURN_POINTER) | GB_OFFSET_0 |
  2031. GB_RASTER_STANDARD | GB_FORMAT_CHUNKY | GB_COLORS_NATIVE |
  2032. GB_ALPHA_NONE),
  2033. {data}})</pre></blockquote>
  2034. <p>
  2035. with the returned value of
  2036. <b><tt>params</tt></b>-&gt;<b><tt>data[0]</tt></b> stored in
  2037. <b><tt>*actual_data</tt></b>, and will in fact be implemented this way if
  2038. the device defines a <b><tt>get_bits_rectangle</tt></b> procedure and does
  2039. not define one for <b><tt>get_bits</tt></b>. (If
  2040. <b><tt>actual_data</tt></b> is <b><tt>NULL</tt></b>,
  2041. <b><tt>GB_RETURN_POINTER</tt></b> is omitted from the options.)
  2042. </dl>
  2043. <h3><a name="Parameters"></a>Parameters</h3>
  2044. <p>
  2045. Devices may have an open-ended set of parameters, which are simply pairs
  2046. consisting of a name and a value. The value may be of various types:
  2047. integer (int or long), boolean, float, string, name, <b><tt>NULL</tt></b>,
  2048. array of integer, array of float, or arrays or dictionaries of mixed types.
  2049. For example, the <b><tt>Name</tt></b> of a device is a string; the
  2050. <b><tt>Margins</tt></b> of a device is an array of two floats. See
  2051. <a href="../src/gsparam.h">gsparam.h</a> for more details.
  2052. <p>
  2053. If a device has parameters other than the ones applicable to all devices
  2054. (or, in the case of printer devices, all printer devices), it must provide
  2055. <b><tt>get_params</tt></b> and <b><tt>put_params</tt></b> procedures. If
  2056. your device has parameters beyond those of a straightforward display or
  2057. printer, we strongly advise using the <b><tt>_get_params</tt></b> and
  2058. <b><tt>_put_params</tt></b> procedures in an existing device (for example,
  2059. <a href="../src/gdevcdj.c">gdevcdj.c</a> or <a
  2060. href="../src/gdevbit.c">gdevbit.c</a>) as a model for your own code.
  2061. <dl>
  2062. <dt><b><tt>int (*get_params)(P2(gx_device&nbsp;*dev,
  2063. gs_param_list&nbsp;*plist))</tt></b> <b><em>[OPTIONAL]</em></b>
  2064. <dd>Read the parameters of the device into the parameter list at
  2065. <b><tt>plist</tt></b>, using the <b><tt>param_write_*</tt></b>
  2066. macros or procedures defined in <a href="../src/gsparam.h">gsparam.h</a>.
  2067. </dl>
  2068. <dl>
  2069. <dt><b><tt>int (*get_hardware_params)(P2(gx_device&nbsp;*dev,
  2070. gs_param_list&nbsp;*plist))</tt></b> <b><em>[OPTIONAL]</em></b>
  2071. <dd>Read the hardware-related parameters of the device into the parameter
  2072. list at plist. These are any parameters whose values are under control of
  2073. external forces rather than the program -- for example, front panel
  2074. switches, paper jam or tray empty sensors, etc. If a parameter involves
  2075. significant delay or hardware action, the driver should only determine the
  2076. value of the parameter if it is "requested" by the
  2077. <b><tt>gs_param_list</tt></b> [<b><tt>param_requested</tt></b>(plist,
  2078. <b><tt>key_name</tt></b>)]. This function may cause the asynchronous
  2079. rendering pipeline (if enabled) to be drained, so it should be used
  2080. sparingly.
  2081. </dl>
  2082. <dl>
  2083. <dt><b><tt>int (*put_params)(P2(gx_device&nbsp;*dev,
  2084. gs_param_list&nbsp;*plist))</tt></b> <b><em>[OPTIONAL]</em></b>
  2085. <dd>Set the parameters of the device from the parameter list at
  2086. <b><tt>plist</tt></b>, using the <b><tt>param_read_</tt></b>*
  2087. macros/procedures defined in <a href="../src/gsparam.h">gsparam.h</a>. All
  2088. <b><tt>put_params</tt></b> procedures must use a "two-phase commit"
  2089. algorithm; see <a href="../src/gsparam.h">gsparam.h</a> for details.
  2090. </dl>
  2091. <h4><a name="Default_CRD_parameters"></a>Default color rendering
  2092. dictionary (CRD) parameters</h4>
  2093. <p>
  2094. Drivers that want to provide one or more default CIE color rendering
  2095. dictionaries (CRDs) can do so through <b><tt>get_params</tt></b>. To do
  2096. this, they create the CRD in the usual way (normally using the
  2097. <b><tt>gs_cie_render1_build</tt></b> and <b><tt>_initialize</tt></b>
  2098. procedures defined in <a href="../src/gscrd.h">gscrd.h</a>), and then write
  2099. it as a parameter using <b><tt>param_write_cie_render1</tt></b> defined in
  2100. <a href="../src/gscrdp.h">gscrdp.h</a>. However, the TransformPQR procedure
  2101. requires special handling. If the CRD uses a TransformPQR procedure
  2102. different from the default (identity), the driver must do the following:
  2103. <ul>
  2104. <li>The TransformPQR element of the CRD must include a
  2105. <b><tt>proc_name</tt></b>, and optionally <b><tt>proc_data</tt></b>. The
  2106. <b><tt>proc_name</tt></b> is an arbitrary name chosen by the driver to
  2107. designate the particular TransformPQR function. It must not be the same as
  2108. any device parameter name; we strongly suggest it include the device name,
  2109. for instance, "<b><tt>bitTPQRDefault</tt></b>".
  2110. <li>For each such named TransformPQR procedure, the driver's
  2111. <b><tt>get_param</tt></b> procedure must provide a parameter of the same
  2112. name. The parameter value must be a string whose bytes are the actual
  2113. procedure address.
  2114. </ul>
  2115. <p>
  2116. For a complete example, see the <b><tt>bit_get_params</tt></b> procedure in
  2117. <a href="../src/gdevbit.c">gdevbit.c</a>. Note that it is essential that
  2118. the driver return the CRD or the procedure address only if specifically
  2119. requested (<b><tt>param_requested(...)</tt></b> &gt; 0); otherwise, errors
  2120. will occur.
  2121. <h3><a name="External_fonts"></a>External fonts</h3>
  2122. <p>
  2123. Drivers may include the ability to display text. More precisely, they may
  2124. supply a set of procedures that in turn implement some font and text
  2125. handling capabilities, described in <a href="Xfonts.htm">a separate
  2126. document</a>. The link between the two is the driver procedure that
  2127. supplies the font and text procedures:
  2128. <dl>
  2129. <dt><b><tt>xfont_procs&nbsp;*(*get_xfont_procs)(P1(gx_device&nbsp;*dev))</tt></b> <b><em>[OPTIONAL]</em></b>
  2130. <dd>Return a structure of procedures for handling external fonts and text
  2131. display. A <b><tt>NULL</tt></b> value means that this driver doesn't
  2132. provide this capability.
  2133. </dl>
  2134. <p>
  2135. For technical reasons, a second procedure is also needed:
  2136. <dl>
  2137. <dt><b><tt>gx_device&nbsp;*(*get_xfont_device)(P1(gx_device&nbsp;*dev))</tt></b> <b><em>[OPTIONAL]</em></b>
  2138. <dd>Return the device that implements <b><tt>get_xfont_procs</tt></b> in a
  2139. non-default way for this device, if any. Except for certain special
  2140. internal devices, this is always the device argument.
  2141. </dl>
  2142. <h3><a name="Page_devices"></a>Page devices</h3>
  2143. <dl>
  2144. <dt><b><tt>gx_device&nbsp;*(*get_page_device)(P1(gx_device&nbsp;*dev))</tt></b>
  2145. <b><em>[OPTIONAL]</em></b>
  2146. <dd>According to the Adobe specifications, some devices are "page devices"
  2147. and some are not. This procedure returns <b><tt>NULL</tt></b> if the
  2148. device is not a page device, or the device itself if it is a page device.
  2149. In the case of forwarding devices, <b><tt>get_page_device</tt></b> returns
  2150. the underlying page device (or <b><tt>NULL</tt></b> if the underlying
  2151. device is not a page device).
  2152. </dl>
  2153. <h3><a name="Miscellaneous"></a>Miscellaneous</h3>
  2154. <dl>
  2155. <dt><b><tt>int (*get_band)(P3(gx_device&nbsp;*dev, int&nbsp;y,
  2156. int&nbsp;*band_start))</tt></b> <b><em>[OPTIONAL]</em></b>
  2157. <dd>If the device is a band device, this procedure stores in
  2158. <b><tt>*band_start</tt></b> the scan line (device Y coordinate) of the band
  2159. that includes the given Y coordinate, and returns the number of scan lines
  2160. in the band. If the device is not a band device, this procedure returns 0.
  2161. The latter is the default implementation.
  2162. </dl>
  2163. <dl>
  2164. <dt><b><tt>void (*get_clipping_box)(P2(gx_device&nbsp;*dev,
  2165. gs_fixed_rect&nbsp;*pbox))</tt></b> <b><em>[OPTIONAL]</em></b>
  2166. <dd>Stores in <b><tt>*pbox</tt></b> a rectangle that defines the device's
  2167. clipping region. For all but a few specialized devices, this is
  2168. <em>((0,0),(width,height))</em>.
  2169. </dl>
  2170. <!-- [2.0 end contents] ==================================================== -->
  2171. <!-- [3.0 begin visible trailer] =========================================== -->
  2172. <hr>
  2173. <p>
  2174. <small>Copyright &copy; 1996, 2000 Aladdin Enterprises.
  2175. All rights reserved.</small>
  2176. <p>
  2177. <small>This file is part of AFPL Ghostscript. See the
  2178. <a href="Public.htm">Aladdin Free Public License</a> (the "License") for
  2179. full details of the terms of using, copying, modifying, and redistributing
  2180. AFPL Ghostscript.</small>
  2181. <p>
  2182. <small>Ghostscript version 7.04, 31 January 2002
  2183. <!-- [3.0 end visible trailer] ============================================= -->
  2184. </body>
  2185. </html>