Ps-style.htm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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>Ghostscript PostScript coding guidelines</title>
  5. <!-- $Id: Ps-style.htm,v 1.13.2.2 2002/02/01 05:31:25 raph Exp $ -->
  6. <link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
  7. </head>
  8. <body>
  9. <!-- [1.0 begin visible header] ============================================ -->
  10. <!-- [1.1 begin headline] ================================================== -->
  11. <h1>Ghostscript PostScript coding guidelines</h1>
  12. <!-- [1.1 end headline] ==================================================== -->
  13. <!-- [1.2 begin table of contents] ========================================= -->
  14. <h2>Table of contents</h2>
  15. <blockquote><ul>
  16. <li><a href="#Summary">Summary of the coding guidelines</a>
  17. <li><a href="#Introduction">Introduction</a>
  18. <li><a href="#PS_features">Use of PostScript language features</a>
  19. <ul>
  20. <li><a href="#Restrictions">Restrictions</a>
  21. <li><a href="#Protection">Protection</a>
  22. <li><a href="#Standard_constructions">Standard constructions</a>
  23. </ul>
  24. <li><a href="#File_structuring">File structuring</a>
  25. <li><a href="#Commenting">Commenting</a>
  26. <li><a href="#Formatting">Formatting</a>
  27. <ul>
  28. <li><a href="#Indentation">Indentation</a>
  29. <li><a href="#Spaces">Spaces</a>
  30. </ul>
  31. <li><a href="#Naming">Naming</a>
  32. <li><a href="#Miscellany">Miscellany</a>
  33. <ul>
  34. <li><a href="#Non_standard_operators">Some useful non-standard operators</a>
  35. <li><a href="#Useful_procedures">Some useful procedures</a>
  36. <li><a href="#Other">Other</a>
  37. </ul>
  38. </ul></blockquote>
  39. <!-- [1.2 end table of contents] =========================================== -->
  40. <!-- [1.3 begin hint] ====================================================== -->
  41. <p>
  42. For other information, see the <a href="Readme.htm">Ghostscript
  43. overview</a>.
  44. <!-- [1.3 end hint] ======================================================== -->
  45. <hr>
  46. <!-- [1.0 end visible header] ============================================== -->
  47. <!-- [2.0 begin contents] ================================================== -->
  48. <h2><a name="Summary"></a>Summary of the coding guidelines</h2>
  49. <ul>
  50. <li>Don't store into literals.
  51. <li>Use <b><tt>loop</tt></b> to create a block with multiple exits.
  52. <li>Use a dictionary or an array for multi-way switches.
  53. <li>Start every file with a copyright notice, the file name, and a one-line
  54. summary.
  55. <li>Comment every procedure with the arguments and result, and with the
  56. function of the procedure unless it's obvious.
  57. <li>Comment the stack contents ad lib, and particularly at the beginning of
  58. every loop body.
  59. <li>Indent every 2 spaces.
  60. <li>Always put { at the end of a line, and } at the beginning of a line,
  61. unless the contents are very short.
  62. <li>Always put spaces between adjacent tokens.
  63. <li>Use only lower-case letters and digits for names, or Vienna style names,
  64. except for an initial "." for names only used within a single file.
  65. <li>Don't allocate objects in heavily used code.
  66. <li>Consider factoring out code into a procedure if it is used more than
  67. once.
  68. </ul>
  69. <hr>
  70. <h2><a name="Introduction"></a>Introduction</h2>
  71. <p>
  72. The many rules that Ghostscript's code follows almost everywhere are meant
  73. to produce code that is easy to read. It's important to observe them as
  74. much as possible in order to maintain a consistent style, but if you find a
  75. rule getting in your way or producing ugly-looking results once in a while,
  76. it's OK to break it.
  77. <hr>
  78. <h2><a name="PS_features"></a>Use of PostScript language features</h2>
  79. <h3><a name="Restrictions"></a>Restrictions</h3>
  80. <p>
  81. If you need to store a value temporarily, don't write into a literal in the
  82. code, as in this fragment to show a character given the character code:
  83. <blockquote><pre>
  84. ( ) dup 0 4 -1 roll put show
  85. </pre></blockquote>
  86. <p>
  87. Instead, allocate storage for it:
  88. <blockquote><pre>
  89. 1 string dup 0 4 -1 roll put show
  90. </pre></blockquote>
  91. <h3><a name="Protection"></a>Protection</h3>
  92. <p>
  93. If an object is never supposed to change, use <b><tt>readonly</tt></b> to
  94. make it read-only. This applies especially to permanently allocated objects
  95. such as constant strings or dictionaries.
  96. <p>
  97. During initialization, and occasionally afterwards, it may be necessary to
  98. store into a read-only dictionary, or to store a pointer to a dictionary in
  99. local VM into a dictionary in global VM. The operators
  100. <b><tt>.forceput</tt></b> and <b><tt>.forceundef</tt></b> are available for
  101. this purpose. To make these operators inaccessible to ordinary programs,
  102. they are removed from <b><tt>systemdict</tt></b> at the end of
  103. initialization: system code that uses them should always use
  104. <b><tt>bind</tt></b> and <b><tt>odef</tt></b> (or
  105. <b><tt>executeonly</tt></b>) to make uses of them inaccessible as well.
  106. <h3><a name="Standard_constructions"></a>Standard constructions</h3>
  107. <h4>Multi-way conditionals</h4>
  108. <p>
  109. If you write a block of code with more than about 3 exit points, the usual
  110. way to do it would be like this:
  111. <blockquote><pre>
  112. {
  113. ... {
  114. ...1
  115. } {
  116. ... {
  117. ...2
  118. } {
  119. ... {
  120. ...3
  121. } {
  122. ...4
  123. } ifelse
  124. } ifelse
  125. } ifelse
  126. }
  127. </pre></blockquote>
  128. <p>
  129. However, this causes the 4 logically somewhat parallel code blocks to be
  130. indented differently, and as the indentation increases, it becomes harder to
  131. see the structure visually. As an alternative, you can do it this way:
  132. <blockquote><pre>
  133. { % The loop doesn't actually loop: it just provides a common exit.
  134. ... {
  135. ...1
  136. exit
  137. } if
  138. ... {
  139. ...2
  140. exit
  141. } if
  142. ... {
  143. ...3
  144. exit
  145. } if
  146. ...4
  147. exit
  148. } loop
  149. </pre></blockquote>
  150. <p>
  151. Don't forget the final exit, to prevent the loop from actually looping.
  152. <h4>Switches</h4>
  153. <p>
  154. Use a dictionary or an array of procedures to implement a 'switch', rather
  155. than a series of conditionals, if there are more than about 3 cases. For
  156. example, rather than:
  157. <blockquote><pre>
  158. dup /a eq {
  159. pop ...a
  160. } {
  161. dup /b eq {
  162. pop ...b
  163. } {
  164. dup /c eq {
  165. pop ...c
  166. } {
  167. ...x
  168. } ifelse
  169. } ifelse
  170. } ifelse
  171. </pre></blockquote>
  172. <p>
  173. (or using the loop/exit construct suggested above), consider:
  174. <blockquote><pre>
  175. /xyzdict mark
  176. /a {...a} bind
  177. /b {...b} bind
  178. /c {...c} bind
  179. .dicttomark readonly def
  180. ...
  181. //xyzdict 1 index .knownget {
  182. exch pop exec
  183. } {
  184. ...x
  185. } ifelse
  186. </pre></blockquote>
  187. <hr>
  188. <h2><a name="File_structuring"></a>File structuring</h2>
  189. <p>
  190. Every code file should start with comments containing
  191. <ol>
  192. <li>a copyright notice;
  193. <li>the name of the file in the form of an RCS Id:
  194. <blockquote><pre>
  195. % Id$: filename.ps $
  196. </pre></blockquote>
  197. <li>a very brief summary (preferably only one line) of what the file
  198. contains.
  199. </ol>
  200. <p>
  201. If you create a file by copying the beginning of another file, be sure to
  202. update the copyright year and change the file name.
  203. <hr>
  204. <h2><a name="Commenting"></a>Commenting</h2>
  205. <p>
  206. If a file has well-defined functional sections, put a comment at the
  207. beginning of each section to describe its purpose or function.
  208. <p>
  209. Put a comment before every procedure to describe what the procedure does,
  210. unless it's obvious or the procedure's function is defined by the PLRM. In
  211. case of doubt, don't assume it's obvious. If the procedure may execute a
  212. deliberate 'stop' or 'exit' not enclosed in 'stopped' or a loop
  213. respectively, that should be mentioned. However, information about the
  214. arguments and results should go in the argument and result comment
  215. (described just below) if possible, not the functional comment.
  216. <p>
  217. Put a comment on every procedure to describe the arguments and results:
  218. <blockquote><pre>
  219. /hypot { % &lt;num1&gt; &lt;num2&gt; hypot &lt;real&gt;
  220. dup mul exch dup mul add sqrt
  221. } def
  222. </pre></blockquote>
  223. <p>
  224. There is another commenting style that some people prefer to the above:
  225. <blockquote><pre>
  226. /hypot { % num1 num2 --> realnum
  227. dup mul exch dup mul add sqrt
  228. } def
  229. </pre></blockquote>
  230. <p>
  231. We have adopted the first style for consistency with Adobe's documentation,
  232. but we recognize that there are technical arguments for and against both
  233. styles, and might consider switching some time in the future. If you have
  234. strong feelings either way, please make your opinion known to
  235. <b><tt>gs-devel@ghostscript.com</tt></b>.
  236. <p>
  237. Put comments describing the stack contents wherever you think they will be
  238. helpful; put such a comment at the beginning of every loop body unless you
  239. have a good reason not to.
  240. <p>
  241. When you change a piece of code, do <em>not</em> include a comment with your
  242. name or initials. Also, do <em>not</em> retain the old code in a comment,
  243. unless you consider it essential to explain something about the new code; in
  244. that case, retain as little as possible. (CVS logs do both of these things
  245. better than manual editing.) However, if you make major changes in a
  246. procedure or a file, you may put your initials, the date, and a brief
  247. comment at the head of the procedure or file respectively.
  248. <hr>
  249. <h2><a name="Formatting"></a>Formatting</h2>
  250. <h3><a name="Indentation"></a>Indentation</h3>
  251. <p>
  252. Indent 2 spaces per indentation level. You may use tabs at the left margin
  253. for indentation, with 1 tab = 8 spaces, but you should not use tabs anywhere
  254. else, except to place comments.
  255. <p>
  256. Indent { } constructs like this:
  257. <blockquote><pre>
  258. ... {
  259. ...
  260. } {
  261. ...
  262. } ...
  263. </pre></blockquote>
  264. <p>
  265. If the body of a conditional or loop is no more than about 20 characters,
  266. you can put the entire construct on a single line if you want:
  267. <blockquote><pre>
  268. ... { ... } if
  269. </pre></blockquote>
  270. rather than
  271. <blockquote><pre>
  272. ... {
  273. ...
  274. } if
  275. </pre></blockquote>
  276. <p>
  277. There is another indentation style that many people prefer to the above:
  278. <blockquote><pre>
  279. ...
  280. { ...
  281. }
  282. { ...
  283. } ...
  284. </pre></blockquote>
  285. <p>
  286. We have adopted the first style for consistency with our C code, but we
  287. recognize that there are technical arguments for and against both styles,
  288. and might consider switching some time in the future. If you have strong
  289. feelings either way, please make your opinion known to
  290. <b><tt>gs-devel@ghostscript.com</tt></b>.
  291. <h3><a name="Spaces"></a>Spaces</h3>
  292. <p>
  293. Always put spaces between two adjacent tokens, even if this isn't strictly
  294. required. E.g.,
  295. <blockquote><pre>
  296. /Halftone /Category findresource
  297. </pre></blockquote>
  298. <p>
  299. not
  300. <blockquote><pre>
  301. /Halftone/Category findresource
  302. </pre></blockquote>
  303. <hr>
  304. <h2><a name="Naming"></a>Naming</h2>
  305. <p>
  306. All names should consist only of letters and digits, possibly with an
  307. initial ".", except for names drawn from the PostScript or PDF reference
  308. manual, which must be capitalized as in the manual. In general, an initial
  309. "." should be used for those and only those names that are not defined in a
  310. private dictionary but that are meant to be used only in the file where they
  311. are defined.
  312. <p>
  313. For edits to existing code, names made up of multiple words should not use
  314. any punctuation, or capitalization, to separate the words, again except for
  315. names that must match a specification. For new code, you may use this
  316. convention, or you may use the "Vienna" convention of capitalizing the first
  317. letter of words, e.g., <b><tt>readSubrs</tt></b> rather than
  318. <b><tt>readsubrs</tt></b>. If you use the Vienna convention, function names
  319. should start with an upper case letter, variable names with a lower case
  320. letter. Using the first letter of a variable name to indicate the
  321. variable's type is optional, but if you do it, you should follow existing
  322. codified usage (****** WE NEED A REFERENCE FOR THIS ******).
  323. <hr>
  324. <h2><a name="Miscellany"></a>Miscellany</h2>
  325. <h3><a name="Non_standard_operators"></a>Some useful non-standard
  326. operators</h3>
  327. <dl>
  328. <dt><b><tt>&lt;obj1&gt; &lt;obj2&gt; ... &lt;objn&gt; &lt;n&gt; .execn ...</tt></b>
  329. <dd>This executes <b><tt>obj1</tt></b> through <b><tt>objn</tt></b> in that
  330. order, essentially equivalent to
  331. <blockquote><pre>
  332. &lt;obj1&gt; &lt;obj2&gt; ... &lt;objn&gt; &lt;n&gt; array astore {exec} forall
  333. </pre></blockquote>
  334. <p>
  335. except that it doesn't actually create the array.
  336. <dt><b><tt>&lt;dict&gt; &lt;key&gt; <b>.knownget</b> &lt;value&gt; true</tt></b>
  337. <dt><b><tt>&lt;dict&gt; &lt;key&gt; <b>.knownget</b> false</tt></b>
  338. <dd>This combines <b><tt>known</tt></b> and <b><tt>get</tt></b> in the
  339. obvious way.
  340. <dt><b><tt>&lt;name&gt; &lt;proc&gt; odef -</tt></b>
  341. <dd>This defines <b><tt>name</tt></b> as a "pseudo-operator". The value of
  342. <b><tt>name</tt></b> will be executable, will have type
  343. <b><tt>operatortype</tt></b>, and will be executed if it appears directly in
  344. the body of a procedure (like an operator, unlike a procedure), but what
  345. will actually be executed will be <b><tt>proc</tt></b>. In addition, if the
  346. execution of <b><tt>proc</tt></b> is ended prematurely (by
  347. <b><tt>stop</tt></b>, including the <b><tt>stop</tt></b> that is normally
  348. executed when an error occurs, or <b><tt>exit</tt></b>) and the operand and
  349. dictionary stacks are at least as deep as they were when the "operator" was
  350. invoked, the stacks will be cut back to their original depths before the
  351. error is processed. Thus, if pseudo-operator procedures are careful not to
  352. remove any of their operands until they reach a point in execution beyond
  353. which they cannot possibly cause an error, they will behave just like
  354. operators in that the stacks will appear to be unchanged if an error occurs.
  355. </dl>
  356. <h3><a name="Useful_procedures"></a>Some useful procedures</h3>
  357. <dl>
  358. <dt><b><tt>&lt;object&gt; &lt;errorname&gt; signalerror -</tt></b>
  359. <dd>Signal an error with the given name and the given "current object".
  360. This does exactly what the interpreter does when an error occurs.
  361. </dl>
  362. <h3><a name="Other"></a>Other</h3>
  363. <p>
  364. If you can avoid it, don't allocate objects (strings, arrays, dictionaries,
  365. gstates, etc.) in commonly used operators or procedures: these will need to
  366. be garbage collected later, slowing down execution. Instead, keep values on
  367. the stack, if you can. The <b><tt>.execn</tt></b> operator discussed above
  368. may be helpful in doing this.
  369. <p>
  370. If you find yourself writing the same stretch of code (more than about half
  371. a dozen tokens) more than once, ask yourself whether it performs a function
  372. that could be made into a procedure.
  373. <!-- [2.0 end contents] ==================================================== -->
  374. <!-- [3.0 begin visible trailer] =========================================== -->
  375. <hr>
  376. <p>
  377. <small>Copyright &copy; 2000 Aladdin Enterprises. All rights
  378. reserved.</small>
  379. <p>
  380. <small>This file is part of AFPL Ghostscript. See the <a
  381. href="Public.htm">Aladdin Free Public License</a> (the "License") for full
  382. details of the terms of using, copying, modifying, and redistributing AFPL
  383. Ghostscript.</small>
  384. <p>
  385. <small>Ghostscript version 7.04, 31 January 2002
  386. <!-- [3.0 end visible trailer] ============================================= -->
  387. </body>
  388. </html>