Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found