Programming in PostScript - Computerphile

The Art of PostScript: A Journey Through Computer Programming

When it comes to computer programming, there are many languages and dialects to learn and master. One such language is PostScript, a scripting language used for creating graphical user interfaces, graphics, and other visual elements. In this article, we'll delve into the world of PostScript, exploring its syntax, operators, and applications.

**The Basics of PostScript**

PostScript is a programming language that uses a syntax similar to handwriting. The program is read from top to bottom, with each instruction or operator being executed in sequence. This means that the order in which the instructions are written is crucial, as it can affect the outcome of the program.

One of the key features of PostScript is its use of operators to perform tasks. Operators are symbols that represent a specific action or operation, such as "move to" or "draw line." These operators are used to create graphics and other visual elements on the screen. The program uses a stack to store values and instructions, which are then executed in sequence.

**The For Loop**

One of the most basic programming constructs is the for loop. In PostScript, the for loop is implemented using a simple "for" operator followed by an expression that increments or decrements a counter variable. This allows the program to repeat a block of code multiple times, which can be useful for creating repetitive graphics or patterns.

For example, consider the following PostScript program:

```

for (i = 0; i < 10; i++) {

show "looping"

move to (i * 20) 50

}

```

This program uses a for loop to repeat the instruction "show 'looping'" ten times. Each time, it moves to a new position on the page, creating a pattern of lines that spells out the word "looping."

**Moving Around on the Page**

Another important aspect of PostScript programming is moving around on the page. This can be done using the "move to" operator, which takes two arguments: the x-coordinate and y-coordinate of the new position.

When we move around on the page in this way, we need to make sure that we're following a logical path. In this example, let's say we want to create a banner with a repeating pattern of text. We can use the "move to" operator to draw lines at regular intervals, creating a grid-like pattern.

To do this, we'll move up and down between every instance of the banner by approximately 7th of an inch about an inch up. Every time we do that, we then go to the next aisle and it says "show the string computer file."

**Strings in PostScript**

In PostScript, strings are represented using double quotes (`"`) rather than single quotes ('). This is because PostScript uses a syntax similar to handwriting, where the program is read from top to bottom.

When creating strings in PostScript, we need to be careful with quotes and spacing. For example, consider the following string:

```

show "computer file"

```

This string will display on the page, followed by the computer file that's being referenced.

**Multiplication and Division**

In PostScript, multiplication and division are represented using the `*` and `/` operators, respectively. When we want to multiply two numbers together, we can use the `multiply` operator followed by the two numbers.

For example:

```

100 * 2

```

This would result in the value 200 on top of the stack.

**Exchange Operator**

In PostScript, the exchange operator (`exch`) is used to swap the values of two operands on the stack. This can be useful when we want to perform calculations that involve both a numerical value and an operand (such as a string).

For example:

```

100 10 exch

```

This would result in the value 10 being pushed onto the stack, followed by the original value of 100.

**Conclusion**

In this article, we've explored the basics of PostScript programming, including its syntax, operators, and applications. We've also delved into more advanced topics such as for loops, moving around on the page, strings, multiplication, division, and the exchange operator. With a solid understanding of these concepts, you'll be well-equipped to tackle even the most complex PostScript programs.

**PostScript Resources**

If you're interested in learning more about PostScript programming, here are some resources to get you started:

* **Ghost View**: A powerful viewing front end for PostScript scripts.

* **Adobe PostScript**: The official documentation for Adobe's PostScript interpreter.

* **Go Script**: An open-source implementation of the Go Script programming language.

* **Computerphile Video**: A video series exploring computer science and programming concepts, including PostScript.

**Audible.com**

If you'd like to check out some recommended books on programming, we recommend Audible.com. They offer a vast library of audiobooks, including classics like "The Hitchhiker's Guide to the Galaxy" and "To Kill a Mockingbird." With an audible subscription, you can access these titles and more from the comfort of your own home.

By following these resources and practicing your PostScript skills, you'll be well on your way to becoming proficient in this powerful programming language. Happy coding!

"WEBVTTKind: captionsLanguage: enI don't want to be just the person in computerphile who does all the history and nothing but history I'd actually like to do someProgramming with you and some of you in the comments what she saidWhy don't you do Java or teach us some C or something, and I thought noI've got a very good opportunity here to lead off from the PostScript storyThat's I've been telling and actually do some PostScript programming with youIt's pretty easy to get software both for Linux for Mac and for PC that will enable you toRun of the programs as I develop with you and even create ones of your own and I think that's importantIt's no use me getting into some language where the support is utterly minimalPostScript was invented ass Adobe by John Warnock and a team of co helpers it built on work that he'd doneseveral years earlier when he worked with the Evans &amp; Sutherland graphics companyI think he'd done some more work on it'sIn a language called Jam when he was at Xerox PARCThe M in Jam standing for John John and Martin that's right M is for Martin NewellAnyway, it became fully fledged when it got to Adobe. It's a very big languageIt's got something like 400 operators in all but I think I've already said and let me emphasize againIt's far more than a two-dimensional graphics language. It is a full-blown programming languageIt can do anything as any other language can do it can do anything that the Turing machine can do and for that reasonIt's called a Turing complete languageWe're going to print out six instances of the text computer filefirst of allI'm going to do it with if you like a proper conventional PostScript program andNo surprises in guessing that it will involve a for loop that goes around six timesIt's vital that you know, something about PostScript and how it worksFirst of all, it's an interpreted language many of you will know what that means. It means it's things likePerl PythonJavaScript for the older generation among you it's things like listsWhat it means if you have an interpreted language is you look at what has beenRequested in the program and you do it there and then that way you gain great flexibilityIt doesn't necessarily run all that fast. That's the real challengeit's in the very nature of PostScript that it uses anotation called post fix andthat in turn relies for itsExecution environment on this notion of a stack we may be in a position to put this out as a separateFilm in its own right? This is postfix. Ithink his name is pronounced on and likeYanukovych people being unable to pronounce his name correctly and getting all the stresses and accents wrongDecided that rather than pronouncing what they found hard. They would call thispolishNotation now actually polish notation applies to using anything that isn't in fix. I've seen prefix notation being calledForward polish notation. I've seen postfix giving its traditional name of reversepolish notationThe stack is last in first out storageThe last thing I put on the stack I pushed it on was this little white one?The only thing I can directly get at at the top of the stack is the same thingSo it's the last thing in and it's the first thing off and the act of taking it offThe stack is called popping the stack. So we'll keep this to one side just to remind us thatPostScript is totally totally stacked based in every single thing it does and I'm now in a positionThat I can actually go through the four loop version of the program with youI'll be talking about stacks until you're fed up of hearing about themFirst of all, anything beginning percent in PostScript is a commentThere's a very special comment right at the top of your post group programwhich effectivelyinvites you to announce which version of adobe postscript this program will be compliant withWhat I've said here is this requires PostScript defined by Adobe version 2.0 aughtMost of the time in most PostScript interpreters nowadays, you don't really have to bother about thatminimum you can get away with is just the percent and the exclamation markOpinions differ as to whether exclamation mark or typical shriek or bang. It's all from comic books, you know shriekExclamation mark exclamation mark or bang lots of exclamation marks. I don't call it backSo when I say % bang it means it's the top of a postscript program PostScript allows you to have things like charactersReal numbers integer numbers things of that sort that you would expectIt's not what is called a strongly typed languageYou don't have to tell PostScript here that six is an integerIt knows and it's fairly cavalier about what it will let you do with those but then it says if you ask me to defineSix as being n which is what this is doing then fineI'll regard it as being an integer version of six if you mean no six that I can print out on the printerSo it's the ASCII character six then I will give youConversion operators that will convert it into printable stringsSo if it finds out from context what it thinks is plausible for the type of these things. So here we goLook our first example use of reverse polishPostScript notation/n means I want to define a variable called n here it ison the stackThere's the sixDeath is a postcode operator, which does definitions in other wordsIt binds a value to a variable name. So what this is saying ismake my integer variable nRepresent the value 6 and it does that by taking them off storing away n with the value 6 inside it somewhere insidePostscript memory now. Here's anotherwonderful example ofpostscript postfix useFirst op around this time the font nameOcr-a, so what I'm saying to postscript here is you know where the fonts are keptBut I'm using find one called ocra and push on the stack of the name the font nameocra when you found OCR a push on here the required point size of it and theOperator this is called select font. So it binds this point size to this font and says right that's what we're usingI think I saidWay way back and early on that we might encounter conventional programming constructs in PostScript. And we do this is a full loopBut notice it's in postfix the operand which is the four comes lastNext to last in the curly braces is a bunch of program that we wish toExecute every time we go around this for loop the weird thing. Oh,It's maybe not all that weirdActually, is that the start value the increment value and the final value are specified up at the top here?So when you come to think about itif you're only to promote the for up to the top here, it would look a bit likeJava or CBut writing it out this way. Okay, you push the start value you push the increment value andn the terminating value but n has been defined as six so it's saying basicallyStopping a1 in steps of 1 up to 6 do the following piece of program every time it goes roundWhat you can rely onHappening is that the current value of the loop counter will be available to you in the loop bodyevery time round so it will start off as 1And I'll do the loopThen I will increment the 1 to being a toChoose this one for to next try around the loopThe loop cans will be 2 but here's the really handy thingIt could be 2 but you can access that value of 2 at the top of the stackEvery time you look at the top of the stackYou can rely on having current value of the loop counter there what the loop is going to execute is 10100 move - there's the operator here are the operands so this oneis the xposition and the default units areTypographic points and you all know about that 72 points to the inchThis one is the Y position. So all that's happening here your coordinate systemStarts down here at 0 0it starts at the lower left-hand corner and everything is relative to here if I sayMove 10 in the X Direction 2 there and move a hundred in the Y direction to here. That'sAbout an inch and a bit attend is about 1/7 of an inch. So roundabout thereI will print my first computer file to be fairYou sometimes find that you 0 0 gets a little bit misaligned with the bottom of the physical paperParticularly, if you insist on printing onto a full size paper as if it was us letter size paperBut I hope you get the idea here 0 0 we're gonna move a little bit in on the x axislook a 7th of an inch about an inch up between every instance of this banner as we go up and every timewe do that move to we then gone to the next aisle and it's just says show thestring computer fileNow the way you do strings in PostScript is not with double quotes like you would in Java or CYou put them in round brackets parenthesisso the only thing now to say well alright, it goes around 6 times they get six versions ofComputer file coming out what on earth is all this and why are you doing that?first of allI say a hundred mole Mull is going to say to itself to have the multiply now want two things to multiply togetherBut do remember I warned you or I told you that at the top of the stackEvery time around the loop is the current loop counter value. So the first time aroundThis is oneSo basically I'm saying a hundred mole and you say that's not enough operandsYou need two operands from oh, noI don't because one of them is implicitly there already at the top of the stack is a one so I'm pushing a100 on top of it take this big one that represents something big like a hundred I put it on topBut there's the one already there then I say multiplySo the answer is hundred times one is a hundred and I push that back on the top of the stack now the first timearound of course one times under is trivial is 100 but the next time around is going to be two times 100 which will make200 then three times 100 and so onOnce I've got the correct Y positionCalculated I'm going to use this black one to represent the 10 and remember the 10 points is the X offset every timeOkay, soI've got now a hundredI've got tenHow many trouble is?Unfortunately, you don't want a hundred ten move toYou want 10?100 move to in other words if I'd written 10100 move to you stack the 10 you stack the hundred you do the move to the bigger hundred is on topnot underneathWhy did it end up underneath?wellIt ended up there because I wanted to do some calculation and I left it behind as the answerBut PostScript has the answer if your operands on the stack of the wrong way around you do the exchangethe exch operatorI've got a hundred and ten. I need them the other way aroundWell, I exchange themThere. Well I've changed what would have been a hundred ten moved to into 10 100 moved to anddon't forget as you read left to right in all of these things the 10 and the100 the 10 gets pushed on the first the hundred gets pushed on afterwards and that's exactly the order in whichMove to expect to find them. He lifts them off and it saysWhy value will be on top the x value will be underneath?That's the use of exchange then to flip things around at the top of the stack and from then on you knowEverything is utterlyStraightforward, okaySo we've analyzed this PostScript program now with a simple for loop you're familiar withMoving to a new position on the page with the fact that the show operatorShows the immediately preceding string of characters and that there's another operator called select fontWhich can be used to decide which font you're using and what point size you want it at so there we areThat's the original full loop version of it, which has all the right effects the interpreterI'm using the PostScript clone interpreter is one of the most famous ones. It's called go scriptIt was originally written in the early 90s by a guy called Peter Deutsch. It's freely out thereOne of the many things that's been added to it over the years is a slightly more powerfulviewing front end as it were called ghost view but ghost vu still relies on go script underneathSo what I'm going to do now is to run ghost view on the for loop version of the programsee themulti starcrapOkay, so that's the for loop and that produces six computer files running up the pageWe'd like to thank audible.com for supporting us computerphile videoAnd if you'd like to check out one of their huge range of books go to audible.com slashComputerphile and you can try one for free today. I'd like to recommend Douglas Adams's book douglas adamsdid The Hitchhiker's Guide to the galaxy which I love and then rescued from his computer after his sad death was a book called theSalmon of doubt. So check that out if you get the chance, so that's audible.com slash computerphile. You can try one for freeThanks them for supporting this video\n"