Search code examples
ghostscriptpostscript

When hand coding Postscript, Ghostscript produces an undefined error


I haven't hand-coded Postscript in about 20 years, so I opened the Bluebook and entered a few examples into a file called test.ps. Here's one such example:

%!PS
/doACircle
{ 0 0 54 0 360 arc stroke } def
/doAnEllipse
{ 1 .75 scale
doACircle
stroke } def
300 500 translate doACircle
4 {0 −72 translate
doAnEllipse} repeat
showpage

I'm using Ghostscript and Imagemagick on a Debian Linux system. Both have worked fine for other things. When I run a command like this:

convert test.ps test.png

I get an error and a dump:

Error: /undefined in �
Operand stack:
   0
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1992   1   3   %oparray_pop   1991   1   3   %oparray_pop   1979   1   3   %oparray_pop   1833   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   3   --nostringval--   %repeat_continue   --nostringval--
Dictionary stack:
   --dict:727/1123(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Current file position is 167
GPL Ghostscript 9.27: Unrecoverable error, exit code 1
Error: /undefined in �
Operand stack:
   0
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1992   1   3   %oparray_pop   1991   1   3   %oparray_pop   1979   1   3   %oparray_pop   1833   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   3   --nostringval--   %repeat_continue   --nostringval--
Dictionary stack:
   --dict:727/1123(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Current file position is 167
GPL Ghostscript 9.27: Unrecoverable error, exit code 1
convert-im6.q16: no images defined `test.png' @ error/convert.c/ConvertImageCommand/3258.

I know I'm forgetting something basic - something bounding box-related perhaps?


Solution

  • It's a weird character in the program stream. My guess is you're using a Macintosh and it's for some reason your keyboard handler is replacing the hyphen with some ostensibly more proper dash character. This creates a malformed negative number which gets parsed as a name and ... voila ... there's no such name defined in the dict stack.

    Some clues from the error message that can help solve similar problems are:

    • the /undefined error is the result of a failure in dictionary lookup. This can happen with load, known, get or the automatic lookup of an executable name in the program stream.
    • the operand stack dump shows a single 0 so it's got to be happening where there's a single 0 sitting on the stack.
    • the shows that GS encountered a character that it doesn't know what to do with.