I've always been a fan-boy for code generation so when I needed a strong solution to a typesetting problem I didn't hesitate to use as many tools as needed.

The problem? Accurate justification of Figurine Notation for Chess games. I'm in the process of porting my CPAN code to Lisp as a way of comparing the available variety of Lisps out there. As usual this make a fine excuse to embrace and extend my own code. In particular I added code to output Postscript as well as LaTeX and the rest in the current module (Chess::PGN::EPD etc.) Following advice from one of the industry pioneers, Don Lancaster, I have been writing Postscript code right and left (likewise center) with most of the problem coming down to the arcane art of justification. Whoops! Babbling again!!

Long story short, you need font metric information in order to do a decent job of justification. In order to get the necessary tables in my Lisp code, I used Perl to parse .afm files I generated with Ghostscript.

Herewith:

#!/usr/bin/perl # afm.pl # use strict; use warnings; use Font::AFM; my $h = new Font::AFM $ARGV[0]; print "(setq $ARGV[0]afm12 '(\n"; metric($h,"R",12); metric($h,"N",12); metric($h,"B",12); metric($h,"Q",12); metric($h,"K",12); metric($h,"O",12); metric($h,"-",12); metric($h,"x",12); metric($h,"+",12); metric($h,".",12); metric($h,"a",12); metric($h,"b",12); metric($h,"c",12); metric($h,"d",12); metric($h,"e",12); metric($h,"f",12); metric($h,"g",12); metric($h,"h",12); metric($h,"1",12); metric($h,"2",12); metric($h,"3",12); metric($h,"4",12); metric($h,"5",12); metric($h,"6",12); metric($h,"7",12); metric($h,"8",12); metric($h,"9",12); print " (\"0\" ", $h->stringwidth("0",12), ")))\n"; sub metric { my $afm = shift; my $s = shift; my $p = shift; my $w = $afm->stringwidth($s,$p); print " (\"$s\" $w)\n"; }
Which produces (given a font name [.pfb name] on the command line):
(setq ZURIFB__afm12 '( ("R" 10.392) ("N" 11.676) ("B" 11.676) ("Q" 12) ("K" 11.52) ("O" 9.168) ("-" 10.548) ("x" 6.696) ("+" 6.708) ("." 3.396) ("a" 6.672) ("b" 7.488) ("c" 6.048) ("d" 7.62) ("e" 6.24) ("f" 5.28) ("g" 6.696) ("h" 8.268) ("1" 6.708) ("2" 6.708) ("3" 6.708) ("4" 6.708) ("5" 6.708) ("6" 6.888) ("7" 6.708) ("8" 6.708) ("9" 6.708) ("0" 6.708)))
Which eventually produces:
/ZurichFigurine-Bold 12 sf (1.d4 f5 2.c4 Nf6 3.Nc3 g6 4.Nf3 Bg7) justify (5.e3 d6 6.Bd3 O-O 7.O-O Qe8 8.e4) justify (fxe4 9.Nxe4 Bg4 10.Be3 Nbd7 11.Nxf6) justify (Nxf6 12.Be2 h6 13.d5 Ne4 14.Nd4) justify (Bd7 15.Bg4 Bxd4 16.Bxd4 Nf6) justify (17.Bxd7 Qxd7 18.Qd3 Qg4 19.Rae1) justify (Rf7 20.Re6 Raf8 21.h3 Qg5 22.g4) justify (Kh7 23.f4 Nh5 24.fxg5 Rxf1 25.Qxf1) justify (Rxf1 26.Kxf1 Nf4 27.Rxe7 1-0) left-justify newline
So, are we having fun yet?

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

In reply to Code that writes code that writes code: Perl to Lisp to Postscript by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.