Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!sun-barr!newstop!sun!angel!henry From: henry%angel@Sun.COM (Henry McGilton -- Software Products) Newsgroups: comp.lang.postscript Subject: Re: PostScript question (help needed) Summary: Dictionaries really do work. Message-ID: <129367@sun.Eng.Sun.COM> Date: 18 Dec 89 19:41:26 GMT References: <314@vidiot.UUCP> Sender: news@sun.Eng.Sun.COM Lines: 150 In article <314@vidiot.UUCP>, brown@vidiot.UUCP (Vidiot) writes: * I have the three PostScript books, Me too, plus a lot more that I wish I'd never bought. * but that still doesn't help me in figuring out * why this works. * As I understand the books, when one opens up a * dictionary and starts putting things in it, To be a little bit more precise, when you do a begin, the dictionary named in the begin is pushed onto the dictionary stack. * the current dictionary is pushed down on the dictionary stack. Yes. * All good and dandy. But, when an end is done, the * current dictionary that was being used is popped from * the dictionary stack and its information is lost forever. It doesn't work that way. The dictionary at the top of the stack is popped, true. But that dictionary is still around, to be used again, if you wish, by doing another begin. And the things you defined in that dictionary are still in there. None of the books say that the `information is lost forever'. The Red Book says that end: `pops the current dictionary off the dictionary stack'. The Blue Book says: `Once the new dictionary is popped from the dictionary stack, the values for names defined within the context of this dictionary will no longer be found', etc, etc. I don't read either as stating that the values are gone forever. * Well, it seems like forever is not true. Correct. < . . .large amounts of psdit prolog deleted, reading psdit prolog fries the brain. > * OK, all of these macros were created and then thrown * away as nothing was done inside of $DITroff except * creation of definitions. Great -- now you have a dictionary full of definitions. It just isn't on the dictionary stack right now. * Then a definition is created for ditstart that does * another dictionary for $DITroff. This time there isn't * an end, so far so good. * /ditstart{$DITroff begin * /nfonts 60 def * /fonts[nfonts{0}repeat]def * /fontnames[nfonts{()}repeat]def * /docsave save def * }def The $DITroff begin sequence inside the definition of ditstart doesn't do anything until such time as distart is actually executed. * OK, now to the heart of the document. The procedure * ditstart is called, which starts the $DITroff * dictionary again. ditstart pushes $DITroff onto the dictionary stack. * What is my problem you ask? How can all of the * definitions that were created the first time $DITroff * was done be available the second time it is started in * ditstart? See the explanation above. * The book say that an end closes the dictionary and the * info is popped from the dictionary stack. Just the dictionary is popped -- the dictionary, plus its associated key/value pairs, remains. * Can someone please explain this paradox, and/or point * to a page/section in one of the manuals that does * explain this completely? The Red Book. One last item. You might ask, `if the dictionary hangs around, how come we don't run out of VM?'. Placing the entire sequence inside a save/restore pair reclaims the VM. This is my understanding of the mechanisms. I'm sure the REAL PostScript wizards out there will no doubt correct me if I'm wrong. Here's a test program that demonstrates the concepts well enough: =========================== cut here ================================= %!PS /FirstDict 1 dict def % define two dictionaries, each big enough /SecondDict 1 dict def % to hold one item each. FirstDict begin % push FirstDict on dictionary stack /shape { % define a shape -- happens to be a square 0 0 moveto 100 0 lineto 100 100 lineto 0 100 lineto closepath } def end % pop FirstDict off dictionary stack SecondDict begin % push SecondDict on dictionary stack /shape { % define a shape -- happens to be a triangle 0 0 moveto 100 0 lineto 50 100 lineto closepath } def end % pop SecondDict off dictionary stack FirstDict begin % push FirstDict on dictionary stack 144 144 translate gsave shape stroke % execute shape operator -- should be square grestore SecondDict begin % push SecondDict on dictionary stack 144 144 translate gsave shape stroke % execute shape operator -- should be triangle grestore end % pop SecondDict off dictionary stack % Now we should be back to FirstDict 144 144 translate gsave shape stroke % execute shape operator -- should be square grestore end % pop FirstDict off dictionary stack showpage =========================== cut here ================================= ............... Henry +-------------------+---------------------------+---------------------------+ | Henry McGilton | I'll bet those people who | | | Sun Microsystems | put control-D characters | arpa: hmcgilton@sun.com | | 2550 Garcia | in PostScript files also | uucp: ...!sun!angel!henry | | Mountain View, CA | put beans in their chili. | | +-------------------+---------------------------+---------------------------+