Clear questions and runnable code
get the best and fastest answer
Re^3: [OT] Code that writes code that writes code: Perl to Lisp to Postscriptby almut (Canon)
|on Apr 29, 2008 at 07:33 UTC||Need Help??|
If you really want to do good justification by hand, you do need metrics.
Yes... All I wanted to point out was that all the same font metrics info is readily available on the PS side, so it's no big issue to center or otherwise justify short strings.
My initial understanding was that you maybe wanted to position individual chess pieces/glyphs across the board, or something like that... But upon closer inspection it seems you actually want to do block justification of a whole paragraph of figurine notation (similar to what's shown on the Alpine Fonts sample page). In that case - and as you're outputting individual lines to be justified separately on the PS side - you're of course right in that you need to know the font metrics to pre-compute the proper line breaks in your program.
An alternative approach would be (in theory - not trying to talk you into doing it) to pass the entire paragraph as a single string to the PS code to let it handle the line wrapping all by itself. In which case you wouldn't need to know the font metrics in your program.
For illustration purposes I digged out a sample PostScript block justification routine (see below) which I had written a couple of years ago. It computes the line wrapping depending on the font used, and adjusts the inter-word spacings to block justify the lines. Optionally, it can also slightly condense or stretch the distances between the glyphs (intra-word) to compensate for big gaps between words which might otherwise result. (As some people don't like the appearance produced by the latter approach, you can fine-tune it or disable it altogether — see the respective note in the code.) Otherwise, it uses a simple one-pass algorithm... (I'm explicitly mentioning the latter, as you seem to be doing this at a level of sophistication that I'm not sure if my code is of any interest to you at all... Anyhow, FWIW, maybe someone else will find some use for it some day.)
The sample code uses standard text with standard fonts, but there's no reason that it couldn't equally work with figurine notation fonts...
And no, I haven't yet written the PS code that implements the more advanced algorithm (whole-paragraph optimised line breaks) that D. Knuth uses in TeX (though this might be a little fun project for some rainy weekend...;)
BTW, I'm so rusty writing Postscript, ...
Same here... Kind of a pity that I don't really find much use for it at work any longer. Actually, PostScript was - as Perl (!) - one of those few languages which was "love at first sight" with me (the only other one in this category being Lisp). Powerful, flexible, and fun to play around with. Unfortunately, these days people mostly want PDF (without having to go via Distiller or Ghostscript), which is not quite as much fun, as it lacks PostScript's programming language features...
(View with gs -sDEVICE=x11alpha 683424.ps — (device "x11alpha" for nice anti-aliased display) )