4 # this file is part of mftrace - a tool to generate scalable fonts from MF sources
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2
9 # as published by the Free Software Foundation
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Library General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 # Copyright (c) 2001--2006 by
22 # Han-Wen Nienhuys, Jan Nieuwenhuizen
35 localedir
= datadir
+ '/locale'
37 exec_prefix
= '@exec_prefix@'
39 def interpolate (str):
40 str = string
.replace (str, '{', '(')
41 str = string
.replace (str, '}', ')s')
42 str = string
.replace (str, '$', '%')
45 if prefix
!= '@' + 'prefix@':
46 exec_prefix
= interpolate (exec_prefix
) % vars ()
47 bindir
= interpolate (bindir
) % vars ()
48 datadir
= os
.path
.join (interpolate (datadir
) % vars (), 'mftrace')
49 libdir
= interpolate (libdir
) % vars ()
51 if datadir
== '@' + "datadir" + "@":
52 datadir
= os
.getcwd ()
55 sys
.path
.append (datadir
)
60 errorport
= sys
.stderr
62 ################################################################
63 # lilylib.py -- options and stuff
65 # source file of the GNU LilyPond music typesetter
69 gettext
.bindtextdomain ('mftrace', localedir
)
70 gettext
.textdomain ('mftrace')
76 def shell_escape_filename (str):
77 str = re
.sub ('([\'" ])', r
'\\\1', str)
81 port
.write ('%s %s\n' % (program_name
, program_version
))
85 sys
.stdout
.write ('\n')
86 sys
.stdout
.write (_ ('Copyright (c) %s by' % ' 2001--2004'))
87 sys
.stdout
.write ('\n')
88 sys
.stdout
.write (' Han-Wen Nienhuys')
89 sys
.stdout
.write (' Jan Nieuwenhuizen')
90 sys
.stdout
.write ('\n')
91 sys
.stdout
.write (_ (r
'''
92 Distributed under terms of the GNU General Public License. It comes with
94 sys
.stdout
.write ('\n')
100 errorport
.write (_ ("warning: ") + s
)
103 '''Report the error S and exit with an error status of 1.
111 errorport
.write (_ ("error: ") + s
+ '\n')
112 errorport
.write (_ ("Exiting ...") + '\n')
117 def __init__ (self
, name
=None):
120 if not os
.path
.isdir (name
):
124 self
.dir = tempfile
.mkdtemp ()
130 shutil
.rmtree (self
.dir)
140 def setup_temp (name
):
143 temp_dir
= TempDirectory (name
)
146 def popen (cmd
, mode
= 'r', ignore_error
= 0):
148 progress (_ ("Opening pipe `%s\'") % cmd
)
149 pipe
= os
.popen (cmd
, mode
)
154 def system (cmd
, ignore_error
= 0):
155 """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
163 progress (_ ("Invoking `%s\'\n") % cmd
)
166 name
= re
.match ('[ \t]*([^ \t]*)', cmd
).group (1)
167 msg
= name
+ ': ' + _ ("command exited with value %d") % st
169 warning (msg
+ ' ' + _ ("(ignored)") + ' ')
176 def strip_extension (f
, ext
):
177 (p
, e
) = os
.path
.splitext (f
)
183 ################################################################
191 program_name
= 'mftrace'
193 program_version
= '@VERSION@'
194 origdir
= os
.getcwd ()
199 'TeX typewriter text': '09fbbfac.enc', # cmtt10
200 'TeX math symbols': '10037936.enc', # cmbsy
201 'ASCII caps and digits': '1b6d048e', # cminch
202 'TeX math italic': 'aae443f0.enc', # cmmi10
203 'TeX extended ASCII': 'd9b29452.enc',
204 'TeX text': 'f7b6d320.enc',
205 'TeX text without f-ligatures': '0ef0afca.enc',
206 'Extended TeX Font Encoding - Latin': 'tex256.enc',
209 'fetaBraces': 'feta-braces-a.enc',
210 'fetaNumber': 'feta-nummer10.enc',
211 'fetaMusic': 'feta20.enc',
212 'parmesanMusic': 'parmesan20.enc',
217 for d
in include_dirs
:
218 p
= os
.path
.join (d
, nm
)
221 return os
.path
.abspath (p
)
225 p
= popen ('kpsewhich %s' % shell_escape_filename (nm
)).read ()
228 if options
.dos_kpath
:
231 p
= re
.sub ('^([a-z]):', '/cygdrive/\\1', p
)
232 p
= re
.sub ('\\\\', '/', p
)
233 sys
.stderr
.write ("Got `%s' from kpsewhich, using `%s'\n" % (orig
, p
))
241 ################################################################
243 ################################################################
245 def autotrace_command (fn
, opts
):
246 opts
= " " + opts
+ " --background-color=FFFFFF --output-format=eps --input-format=pbm "
247 return options
.trace_binary
+ opts
+ backend_options \
248 + " --output-file=char.eps %s " % fn
250 def potrace_command (fn
, opts
):
251 return options
.trace_binary
+ opts \
252 + ' -u %d ' % options
.grid_scale \
254 + " -q -c --eps --output=char.eps %s " % (fn
)
257 path_to_type1_ops
= None
259 def trace_one (pbmfile
, id):
261 Run tracer, do error handling
264 status
= system (trace_command (pbmfile
, ''), 1)
267 sys
.stderr
.write ("\nUser interrupt. Exiting\n")
270 if status
== 0 and options
.keep_temp_dir
:
271 shutil
.copy2 (pbmfile
, '%s.pbm' % id)
272 shutil
.copy2 ('char.eps', '%s.eps' % id)
275 error_file
= os
.path
.join (origdir
, 'trace-bug-%s.pbm' % id)
276 shutil
.copy2 (pbmfile
, error_file
)
277 msg
= """Trace failed on bitmap. Bitmap left in `%s\'
282 Please submit a bugreport to %s development.""" \
283 % (error_file
, trace_command (error_file
, ''), options
.trace_binary
)
285 if options
.keep_trying
:
287 sys
.stderr
.write ("\nContinuing trace...\n")
290 msg
= msg
+ '\nRun mftrace with --keep-trying to produce a font anyway\n'
296 warning ("Failed, skipping character.\n")
301 def make_pbm (filename
, outname
, char_number
):
302 """ Extract bitmap from the PK file FILENAME (absolute) using `gf2pbm'.
303 Return FALSE if the glyph is not valid.
306 command
= "%s/gf2pbm -n %d -o %s %s" % (bindir
, char_number
, outname
, filename
)
307 status
= system (command
, ignore_error
= 1)
310 def read_encoding (file):
311 sys
.stderr
.write (_ ("Using encoding file: `%s'\n") % file)
313 str = open (file).read ()
314 str = re
.sub ("%.*", '', str)
315 str = re
.sub ("[\n\t \f]+", ' ', str)
316 m
= re
.search ('/([^ ]+) \[([^\]]+)\] def', str)
318 error ("Encoding file is invalid")
322 cod
= re
.sub ('[ /]+', ' ', cod
)
323 cods
= string
.split (cod
)
327 def zip_to_pairs (xs
):
330 r
.append ((xs
[0], xs
[1]))
334 def unzip_pairs (tups
):
337 lst
= lst
+ list (tups
[0])
341 def autotrace_path_to_type1_ops (at_file
, bitmap_metrics
, tfm_wid
, magnification
):
342 inv_scale
= 1000.0 / magnification
344 (size_y
, size_x
, off_x
, off_y
) = map (lambda m
, s
= inv_scale
: m
* s
,
346 ls
= open (at_file
).readlines ()
347 bbox
= (10000, 10000, -10000, -10000)
349 while ls
and ls
[0] != '*u\n':
360 while ls
[0] != '*U\n':
364 toks
= string
.split (ell
)
369 args
= map (lambda m
, s
= inv_scale
: s
* float (m
),
371 if options
.round_to_int
:
372 args
= zip_to_pairs (map (round, args
))
374 args
= zip_to_pairs (args
)
375 commands
.append ((cmd
, args
))
385 cy
= size_y
- off_y
- inv_scale
387 # t1asm seems to fuck up when using sbw. Oh well.
388 t1_outline
= ' %d %d hsbw\n' % (- off_x
, tfm_wid
)
389 bbox
= (10000, 10000, -10000, -10000)
391 for (c
, args
) in commands
:
396 if c
== 'l' or c
== 'c':
397 bbox
= update_bbox_with_point (bbox
, a
)
399 na
.append ((nx
- cx
, ny
- cy
))
404 if options
.round_to_int
:
405 a
= map (lambda x
: '%d' % int (round (x
)),
408 a
= map (lambda x
: '%d %d div' \
409 % (int (round (x
* options
.grid_scale
/inv_scale
)),
410 int (round (options
.grid_scale
/inv_scale
))),
413 t1_outline
= t1_outline
+ ' %s %s\n' % (string
.join (a
), c
)
415 t1_outline
= t1_outline
+ ' endchar '
416 t1_outline
= '{\n %s } |- \n' % t1_outline
418 return (bbox
, t1_outline
)
420 # FIXME: Cut and paste programming
421 def potrace_path_to_type1_ops (at_file
, bitmap_metrics
, tfm_wid
, magnification
):
422 inv_scale
= 1000.0 / magnification
424 (size_y
, size_x
, off_x
, off_y
) = map (lambda m
,
425 s
= inv_scale
: m
* s
,
427 ls
= open (at_file
).readlines ()
428 bbox
= (10000, 10000, -10000, -10000)
430 while ls
and ls
[0] != '0 setgray\n':
438 while ls
and ls
[0] != 'grestore\n':
445 toks
= string
.split (ell
)
450 args
= map (lambda m
, s
= inv_scale
: s
* float (m
),
452 args
= zip_to_pairs (args
)
453 commands
.append ((cmd
, args
))
455 # t1asm seems to fuck up when using sbw. Oh well.
456 t1_outline
= ' %d %d hsbw\n' % (- off_x
, tfm_wid
)
457 bbox
= (10000, 10000, -10000, -10000)
459 # Type1 fonts have relative coordinates (doubly relative for
460 # rrcurveto), so must convert moveto and rcurveto.
462 z
= (0.0, size_y
- off_y
- 1.0)
463 for (c
, args
) in commands
:
464 args
= map (lambda x
: (x
[0] * (1.0 / options
.grid_scale
),
465 x
[1] * (1.0 / options
.grid_scale
)), args
)
468 args
= [(args
[0][0] - z
[0], args
[0][1] - z
[1])]
472 lz
= (z
[0] + a
[0], z
[1] + a
[1])
473 bbox
= update_bbox_with_point (bbox
, lz
)
476 if options
.round_to_int
:
477 last_discr_z
= (int (round (z
[0])), int (round (z
[1])))
479 last_discr_z
= (z
[0], z
[1])
482 if options
.round_to_int
:
483 a
= (int (round (a
[0])), int (round (a
[1])))
486 args
.append ((a
[0] - last_discr_z
[0],
487 a
[1] - last_discr_z
[1]))
493 c
= { 'rcurveto': 'rrcurveto',
495 'closepath': 'closepath',
496 'rlineto': 'rlineto'}[c
]
499 t1_outline
+= ' closepath '
501 if options
.round_to_int
:
502 args
= map (lambda x
: '%d' % int (round (x
)),
505 args
= map (lambda x
: '%d %d div' \
506 % (int (round (x
*options
.grid_scale
/inv_scale
)),
507 int (round (options
.grid_scale
/inv_scale
))),
510 t1_outline
= t1_outline
+ ' %s %s\n' % (string
.join (args
), c
)
512 t1_outline
= t1_outline
+ ' endchar '
513 t1_outline
= '{\n %s } |- \n' % t1_outline
515 return (bbox
, t1_outline
)
517 def read_gf_dims (name
, c
):
518 str = popen ('%s/gf2pbm -n %d -s %s' % (bindir
, c
, name
)).read ()
519 m
= re
.search ('size: ([0-9]+)+x([0-9]+), offset: \(([0-9-]+),([0-9-]+)\)', str)
521 return tuple (map (int, m
.groups ()))
523 def trace_font (fontname
, gf_file
, metric
, glyphs
, encoding
,
524 magnification
, fontinfo
):
526 font_bbox
= (10000, 10000, -10000, -10000)
528 progress (_ ("Tracing bitmaps..."))
535 # for single glyph testing.
538 if encoding
[a
] == ".notavail":
540 valid
= metric
.has_char (a
)
542 encoding
[a
] = ".notavail"
545 valid
= make_pbm (gf_file
, 'char.pbm', a
)
547 encoding
[a
] = ".notavail"
550 (w
, h
, xo
, yo
) = read_gf_dims (gf_file
, a
)
552 if not options
.verbose
:
553 sys
.stderr
.write ('[%d' % a
)
556 # this wants the id, not the filename.
557 success
= trace_one ("char.pbm", '%s-%d' % (options
.gffile
, a
))
559 sys
.stderr
.write ("(skipping character)]")
561 encoding
[a
] = ".notavail"
564 if not options
.verbose
:
565 sys
.stderr
.write (']')
567 metric_width
= metric
.get_char (a
).width
568 tw
= int (round (metric_width
/ metric
.design_size
* 1000))
569 (bbox
, t1o
) = path_to_type1_ops ("char.eps", (h
, w
, xo
, yo
),
573 encoding
[a
] = ".notavail"
576 font_bbox
= update_bbox_with_bbox (font_bbox
, bbox
)
578 t1os
.append ('\n/%s %s ' % (encoding
[a
], t1o
))
580 if not options
.verbose
:
582 to_type1 (t1os
, font_bbox
, fontname
, encoding
, magnification
, fontinfo
)
584 def ps_encode_encoding (encoding
):
585 str = ' %d array\n0 1 %d {1 index exch /.notdef put} for\n' \
586 % (len (encoding
), len (encoding
)-1)
588 for i
in range (0, len (encoding
)):
589 if encoding
[i
] != ".notavail":
590 str = str + 'dup %d /%s put\n' % (i
, encoding
[i
])
595 def gen_unique_id (dict):
597 return 4000000 + (hash (nm
) % 1000000)
599 def to_type1 (outlines
, bbox
, fontname
, encoding
, magnification
, fontinfo
):
602 Fill in the header template for the font, append charstrings,
603 and shove result through t1asm
605 template
= r
"""%%!PS-AdobeFont-1.0: %(FontName)s %(VVV)s.%(WWW)s
607 /FontInfo 16 dict dup begin
608 /version (%(VVV)s.%(WWW)s) readonly def
609 /Notice (%(Notice)s) readonly def
610 /FullName (%(FullName)s) readonly def
611 /FamilyName (%(FamilyName)s) readonly def
612 /Weight (%(Weight)s) readonly def
613 /ItalicAngle %(ItalicAngle)s def
614 /isFixedPitch %(isFixedPitch)s def
615 /UnderlinePosition %(UnderlinePosition)s def
616 /UnderlineThickness %(UnderlineThickness)s def
618 /FontName /%(FontName)s def
621 /FontMatrix [%(xrevscale)f 0 0 %(yrevscale)f 0 0] readonly def
622 /FontBBox {%(llx)d %(lly)d %(urx)d %(ury)d} readonly def
623 /Encoding %(Encoding)s readonly def
626 dup /Private 20 dict dup begin
627 /-|{string currentfile exch readstring pop}executeonly def
628 /|-{noaccess def}executeonly def
629 /|{noaccess put}executeonly def
632 /MinFeature {16 16} |-
634 /OtherSubrs [ {} {} {} {} ] |-
640 /CharStrings %(CharStringsLen)d dict dup begin
644 /.notdef { 0 0 hsbw endchar } |-
649 dup/FontName get exch definefont
650 pop mark currentfile closefile
653 ## apparently, some fonts end the file with cleartomark. Don't know why.
655 copied_fields
= ['FontName', 'FamilyName', 'FullName', 'DesignSize',
656 'ItalicAngle', 'isFixedPitch', 'Weight']
661 'Notice': 'Generated from MetaFont bitmap by mftrace %s, http://www.xs4all.nl/~hanwen/mftrace/ ' % program_version
,
662 'UnderlinePosition': '-100',
663 'UnderlineThickness': '50',
664 'xrevscale': 1.0/1000.0,
665 'yrevscale': 1.0/1000.0,
670 'Encoding': ps_encode_encoding (encoding
),
672 # need one extra entry for .notdef
673 'CharStringsLen': len (outlines
) + 1,
674 'CharStrings': string
.join (outlines
),
675 'CharBBox': '0 0 0 0',
678 for k
in copied_fields
:
679 vars[k
] = fontinfo
[k
]
681 open ('mftrace.t1asm', 'w').write (template
% vars)
683 def update_bbox_with_point (bbox
, pt
):
684 (llx
, lly
, urx
, ury
) = bbox
685 llx
= min (pt
[0], llx
)
686 lly
= min (pt
[1], lly
)
687 urx
= max (pt
[0], urx
)
688 ury
= max (pt
[1], ury
)
690 return (llx
, lly
, urx
, ury
)
692 def update_bbox_with_bbox (bb
, dims
):
693 (llx
, lly
, urx
, ury
) = bb
694 llx
= min (llx
, dims
[0])
695 lly
= min (lly
, dims
[1])
696 urx
= max (urx
, dims
[2])
697 ury
= max (ury
, dims
[3])
699 return (llx
, lly
, urx
, ury
)
701 def get_binary (name
):
702 search_path
= string
.split (os
.environ
['PATH'], ':')
703 for p
in search_path
:
704 nm
= os
.path
.join (p
, name
)
705 if os
.path
.exists (nm
):
710 def get_fontforge_command ():
712 for ff
in ['fontforge', 'pfaedit']:
718 stat
= system ("%s -usage > pfv 2>&1 " % fontforge_cmd
,
722 warning ("Command `%s -usage' failed. Cannot simplify or convert to TTF.\n" % fontforge_cmd
)
725 if fontforge_cmd
== 'pfaedit' \
726 and re
.search ("-script", open ('pfv').read ()) == None:
727 warning ("pfaedit does not support -script. Install 020215 or later.\nCannot simplify or convert to TTF.\n")
731 def tfm2kpx (tfmname
, encoding
):
733 pl
= popen ("tftopl %s" % (tfmname
))
735 label_pattern
= re
.compile (
736 "\A \(LABEL ([DOHC]{1}) ([A-Za-z0-9]*)\)")
737 krn_pattern
= re
.compile (
738 "\A \(KRN ([DOHC]{1}) ([A-Za-z0-9]*) R (-?[\d\.]+)\)")
743 for line
in pl
.readlines ():
745 label_match
= label_pattern
.search (line
)
746 if not (label_match
is None):
747 if label_match
.group (1) == "D":
748 first
= int (label_match
.group (2))
749 elif label_match
.group (1) == "O":
750 first
= int (label_match
.group (2), 8)
751 elif label_match
.group (1) == "C":
752 first
= ord (label_match
.group (2))
754 krn_match
= krn_pattern
.search (line
)
755 if not (krn_match
is None):
756 if krn_match
.group (1) == "D":
757 second
= int (krn_match
.group (2))
758 elif krn_match
.group (1) == "O":
759 second
= int (krn_match
.group (2), 8)
760 elif krn_match
.group (1) == "C":
761 second
= ord (krn_match
.group (2))
763 krn
= round (float (krn_match
.group (3)) * 1000)
765 if (encoding
[first
] != '.notavail' and
766 encoding
[first
] != '.notdef' and
767 encoding
[second
] != '.notavail' and
768 encoding
[second
] != '.notdef'):
770 kpx_lines
.append ("KPX %s %s %d\n" % (
771 encoding
[first
], encoding
[second
], krn
))
775 def get_afm (t1_path
, tfmname
, encoding
, out_path
):
776 afm_stream
= popen ("printafm %s" % (t1_path
))
778 kpx_lines
= tfm2kpx (tfmname
, encoding
)
780 for line
in afm_stream
.readlines ():
781 afm_lines
.append (line
)
783 if re
.match (r
"^EndCharMetrics", line
, re
.I
):
784 afm_lines
.append ("StartKernData\n")
785 afm_lines
.append ("StartKernPairs %d\n" % len (kpx_lines
))
787 for kpx_line
in kpx_lines
:
788 afm_lines
.append (kpx_line
)
790 afm_lines
.append ("EndKernPairs\n")
791 afm_lines
.append ("EndKernData\n")
793 progress (_ ("Writing metrics to `%s'... ") % out_path
)
794 afm_file
= open (out_path
, 'w')
795 afm_file
.writelines (afm_lines
)
801 def assemble_font (fontname
, format
, is_raw
):
811 outname
= fontname
+ ext
813 progress (_ ("Assembling raw font to `%s'... ") % outname
)
816 system ('t1asm %s mftrace.t1asm %s' % (asm_opt
, shell_escape_filename (outname
)))
820 def make_outputs (fontname
, formats
, encoding
):
822 run pfaedit to convert to other formats
828 if (options
.simplify
or options
.round_to_int
or 'ttf' in formats
or 'svg' in formats
or 'afm' in formats
):
831 ff_command
= get_fontforge_command ()
833 if ff_needed
and ff_command
:
834 raw_name
= assemble_font (fontname
, 'pfa', 1)
837 if options
.round_to_int
:
838 simplify_cmd
= 'RoundToInt ();'
841 generate_cmds
+= 'Generate("%s");' % (fontname
+ '.' + f
)
844 simplify_cmd
='''SelectAll ();
849 AutoHint ();''' % vars()
851 pe_script
= ('''#!/usr/bin/env %(ff_command)s
859 open ('to-ttf.pe', 'w').write (pe_script
)
861 print 'Fontforge script', pe_script
862 system ("%s -script to-ttf.pe %s %s" % (ff_command
,
863 shell_escape_filename (raw_name
), shell_escape_filename (options
.tfm_file
)))
864 elif ff_needed
and (options
.simplify
or options
.round_to_int
or 'ttf' in formats
or 'svg' in formats
):
865 error(_ ("fontforge is not installed; could not perform requested command"))
869 if ('pfa' in formats
):
870 t1_path
= assemble_font (fontname
, 'pfa', 0)
872 if ('pfb' in formats
):
873 t1_path
= assemble_font (fontname
, 'pfb', 0)
875 if (t1_path
!= '' and 'afm' in formats
):
876 if get_binary("printafm"):
877 get_afm (t1_path
, options
.tfm_file
, encoding
, fontname
+ '.afm')
879 error(_ ("Neither fontforge nor ghostscript is not installed; could not perform requested command"))
882 def getenv (var
, default
):
883 if os
.environ
.has_key (var
):
884 return os
.environ
[var
]
888 def gen_pixel_font (filename
, metric
, magnification
):
890 Generate a GF file for FILENAME, such that `magnification'*mfscale
891 (default 1000 * 1.0) pixels fit on the designsize.
895 size
= metric
.design_size
897 size_points
= size
* 1/72.27 * base_dpi
899 mag
= magnification
/ size_points
901 prod
= mag
* base_dpi
903 open ('%s.%dgf' % (filename
, prod
))
906 ## MFINPUTS/TFMFONTS take kpathsea specific values;
907 ## we should analyse them any further.
908 os
.environ
['MFINPUTS'] = '%s:%s' % (origdir
,
909 getenv ('MFINPUTS', ''))
910 os
.environ
['TFMFONTS'] = '%s:%s' % (origdir
,
911 getenv ('TFMINPUTS', ''))
913 progress (_ ("Running Metafont..."))
915 cmdstr
= r
"mf '\mode:=lexmarks; mag:=%f; nonstopmode; input %s'" % (mag
, filename
)
916 if not options
.verbose
:
917 cmdstr
= cmdstr
+ ' 1>/dev/null 2>/dev/null'
918 st
= system (cmdstr
, ignore_error
= 1)
921 logfile
= '%s.log' % filename
924 if os
.path
.exists (logfile
):
925 log
= open (logfile
).read ()
926 m
= re
.search ('Output written on %s.([0-9]+)gf' % re
.escape (filename
), log
)
927 prod
= int (m
.group (1))
930 sys
.stderr
.write ('\n\nMetafont failed. Excerpt from the log file: \n\n*****')
931 m
= re
.search ("\n!", log
)
933 short_log
= log
[start
:start
+200]
934 sys
.stderr
.write (short_log
)
935 sys
.stderr
.write ('\n*****\n')
936 if re
.search ('Arithmetic overflow', log
):
937 sys
.stderr
.write ("""
939 Apparently, some numbers overflowed. Try using --magnification with a
940 lower number. (Current magnification: %d)
943 if not options
.keep_trying
or prod
== 0:
946 sys
.stderr
.write ('\n\nTrying to proceed despite of the Metafont errors...\n')
950 return "%s.%d" % (filename
, prod
)
952 def parse_command_line ():
953 p
= optparse
.OptionParser (version
="""mftrace @VERSION@
955 This program is free software. It is covered by the GNU General Public
956 License and you are welcome to change it and/or distribute copies of it
957 under certain conditions. Invoke as `mftrace --warranty' for more
960 Copyright (c) 2005--2006 by
961 Han-Wen Nienhuys <hanwen@xs4all.nl>
964 p
.usage
= "mftrace [OPTION]... FILE..."
965 p
.description
= _ ("Generate Type1 or TrueType font from Metafont source.")
967 p
.add_option ('-k', '--keep',
969 dest
="keep_temp_dir",
970 help=_ ("Keep all output in directory %s.dir") % program_name
)
971 p
.add_option ('','--magnification',
972 dest
="magnification",
976 help=_("Set magnification for MF to MAG (default: 1000)"))
977 p
.add_option ('-V', '--verbose',
980 help=_ ("Be verbose"))
981 p
.add_option ('-f', '--formats',
985 help=_("Which formats to generate (choices: AFM, PFA, PFB, TTF, SVG)"))
986 p
.add_option ('', '--simplify',
989 help=_ ("Simplify using fontforge"))
990 p
.add_option ('', '--gffile',
992 help= _("Use gf FILE instead of running Metafont"))
993 p
.add_option ('-I', '--include',
997 help=_("Add to path for searching files"))
998 p
.add_option ('','--glyphs',
1003 help= _('Process only these glyphs. LIST is comma separated'))
1004 p
.add_option ('', '--tfmfile',
1009 p
.add_option ('-e', '--encoding',
1012 dest
="encoding_file",
1014 help= _ ("Use encoding file FILE"))
1015 p
.add_option ('','--keep-trying',
1018 action
="store_true",
1019 help= _ ("Don't stop if tracing fails"))
1020 p
.add_option ('-w', '--warranty',
1021 action
="store_true",
1022 help=_ ("show warranty and copyright"))
1023 p
.add_option ('','--dos-kpath',
1025 help=_("try to use Miktex kpsewhich"))
1026 p
.add_option ('', '--potrace',
1028 help=_ ("Use potrace"))
1029 p
.add_option ('', '--autotrace',
1031 help=_ ("Use autotrace"))
1032 p
.add_option ('', '--no-afm',
1033 action
='store_false',
1036 help=_("Don't read AFM file"))
1037 p
.add_option ('','--noround',
1038 action
="store_false",
1039 dest
='round_to_int',
1041 help= ("Do not round coordinates of control points to integer values (use with --grid)"))
1042 p
.add_option ('','--grid',
1047 help=_ ("Set reciprocal grid size in em units"))
1048 p
.add_option ('-D','--define',
1049 metavar
="SYMBOL=VALUE",
1052 action
='append',help=_("Set the font info SYMBOL to VALUE"))
1055 (options
, files
) = p
.parse_args ()
1058 sys
.stderr
.write ('Need argument on command line \n')
1062 if options
.warranty
:
1066 options
.font_info
= {}
1067 for d
in options
.defs
:
1070 options
.font_info
[kv
] = 'true'
1072 options
.font_info
[kv
[0]] = '='.join (kv
[1:])
1074 def comma_sepped_to_list (x
):
1077 fs
+= f
.lower ().split (',')
1080 options
.formats
= comma_sepped_to_list (options
.formats
)
1083 for r
in options
.glyphs
:
1084 new_glyphs
+= r
.split (',')
1085 options
.glyphs
= new_glyphs
1088 for r
in options
.glyphs
:
1089 glyph_subrange
= map (int, string
.split (r
, '-'))
1090 if len (glyph_subrange
) == 2 and glyph_subrange
[0] < glyph_subrange
[1] + 1:
1091 glyph_range
+= range (glyph_subrange
[0], glyph_subrange
[1] + 1)
1093 glyph_range
.append (glyph_subrange
[0])
1095 options
.glyphs
= glyph_range
1097 options
.trace_binary
= ''
1099 options
.trace_binary
= 'potrace'
1100 elif options
.autotrace
:
1101 options
.trace_binary
= 'autotrace'
1103 if options
.formats
== []:
1104 options
.formats
= ['pfa']
1108 global trace_command
1109 global path_to_type1_ops
1111 stat
= os
.system ('potrace --version > /dev/null 2>&1 ')
1112 if options
.trace_binary
!= 'autotrace' and stat
== 0:
1113 options
.trace_binary
= 'potrace'
1115 trace_command
= potrace_command
1116 path_to_type1_ops
= potrace_path_to_type1_ops
1117 elif options
.trace_binary
== 'potrace' and stat
!= 0:
1118 error (_ ("Could not run potrace; have you installed it?"))
1120 stat
= os
.system ('autotrace --version > /dev/null 2>&1 ')
1121 if options
.trace_binary
!= 'potrace' and stat
== 0:
1122 options
.trace_binary
= 'autotrace'
1123 trace_command
= autotrace_command
1124 path_to_type1_ops
= autotrace_path_to_type1_ops
1125 elif options
.trace_binary
== 'autotrace' and stat
!= 0:
1126 error (_ ("Could not run autotrace; have you installed it?"))
1128 if not options
.trace_binary
:
1129 error (_ ("No tracing program found.\nInstall potrace or autotrace."))
1134 def derive_font_name (family
, fullname
):
1135 fullname
= re
.sub (family
, '', fullname
)
1136 family
= re
.sub (' ', '', family
)
1137 fullname
= re
.sub ('Oldstyle Figures', 'OsF', fullname
)
1138 fullname
= re
.sub ('Small Caps', 'SC', fullname
)
1139 fullname
= re
.sub ('[Mm]edium', '', fullname
)
1140 fullname
= re
.sub ('[^A-Za-z0-9]', '', fullname
)
1141 return '%s-%s' % (family
, fullname
)
1143 def cm_guess_font_info (filename
, fontinfo
):
1145 filename
= re
.sub ("cm(.*)tt", r
"cmtt\1", filename
)
1146 m
= re
.search ("([0-9]+)$", filename
)
1149 design_size
= int (m
.group (1))
1150 fontinfo
['DesignSize'] = design_size
1152 prefixes
= [("cmtt", "Computer Modern Typewriter"),
1153 ("cmvtt", "Computer Modern Variable Width Typewriter"),
1154 ("cmss", "Computer Modern Sans"),
1155 ("cm", "Computer Modern")]
1158 for (k
, v
) in prefixes
:
1159 if re
.search (k
, filename
):
1162 fontinfo
['isFixedPitch'] = 'true'
1163 filename
= re
.sub (k
, '', filename
)
1167 prefixes
= [("r", "Roman"),
1168 ("mi", "Math italic"),
1169 ("u", "Unslanted italic"),
1171 ("csc", "Small Caps"),
1172 ("ex", "Math extension"),
1173 ("ti", "Text italic"),
1176 for (k
, v
) in prefixes
:
1177 if re
.search (k
, filename
):
1179 filename
= re
.sub (k
, '', filename
)
1181 prefixes
= [("b", "Bold"),
1184 for (k
, v
) in prefixes
:
1185 if re
.search (k
, filename
):
1187 filename
= re
.sub (k
, '', filename
)
1189 prefixes
= [("c", "Condensed"),
1192 for (k
, v
) in prefixes
:
1193 if re
.search (k
, filename
):
1195 filename
= re
.sub (k
, '', filename
)
1197 fontinfo
['ItalicAngle'] = 0
1198 if re
.search ('[Ii]talic', shape
) or re
.search ('[Oo]blique', shape
):
1200 if re
.search ("Sans", family
):
1203 fontinfo
["ItalicAngle"] = a
1205 fontinfo
['Weight'] = weight
1206 fontinfo
['FamilyName'] = family
1207 full
= '%s %s %s %s %dpt' \
1208 % (family
, shape
, weight
, stretch
, design_size
)
1209 full
= re
.sub (" +", ' ', full
)
1211 fontinfo
['FullName'] = full
1212 fontinfo
['FontName'] = derive_font_name (family
, full
)
1216 def ec_guess_font_info (filename
, fontinfo
):
1218 m
= re
.search ("([0-9]+)$", filename
)
1220 design_size
= int (m
.group (1))
1221 fontinfo
['DesignSize'] = design_size
1223 prefixes
= [("ecss", "European Computer Modern Sans"),
1224 ("ectt", "European Computer Modern Typewriter"),
1225 ("ec", "European Computer Modern")]
1228 for (k
, v
) in prefixes
:
1229 if re
.search (k
, filename
):
1231 fontinfo
['isFixedPitch'] = 'true'
1233 filename
= re
.sub (k
, '', filename
)
1237 prefixes
= [("r", "Roman"),
1238 ("mi", "Math italic"),
1239 ("u", "Unslanted italic"),
1241 ("cc", "Small caps"),
1242 ("ex", "Math extension"),
1247 for (k
, v
) in prefixes
:
1248 if re
.search (k
, filename
):
1250 filename
= re
.sub (k
, '', filename
)
1252 prefixes
= [("b", "Bold"),
1255 for (k
, v
) in prefixes
:
1256 if re
.search (k
, filename
):
1258 filename
= re
.sub (k
, '', filename
)
1260 prefixes
= [("c", "Condensed"),
1263 for (k
, v
) in prefixes
:
1264 if re
.search (k
, filename
):
1266 filename
= re
.sub (k
, '', filename
)
1268 fontinfo
['ItalicAngle'] = 0
1269 if re
.search ('[Ii]talic', shape
) or re
.search ('[Oo]blique', shape
):
1271 if re
.search ("Sans", family
):
1274 fontinfo
["ItalicAngle"] = a
1276 fontinfo
['Weight'] = weight
1277 fontinfo
['FamilyName'] = family
1278 full
= '%s %s %s %s %dpt' \
1279 % (family
, shape
, weight
, stretch
, design_size
)
1280 full
= re
.sub (" +", ' ', full
)
1282 fontinfo
['FontName'] = derive_font_name (family
, full
)
1283 fontinfo
['FullName'] = full
1288 def guess_fontinfo (filename
):
1290 'FontName': filename
,
1291 'FamilyName': filename
,
1292 'Weight': 'Regular',
1295 'isFixedPitch' : 'false',
1296 'FullName': filename
,
1299 if re
.search ('^cm', filename
):
1300 fi
.update (cm_guess_font_info (filename
, fi
))
1301 elif re
.search ("^ec", filename
):
1302 fi
.update (ec_guess_font_info (filename
, fi
))
1303 elif options
.read_afm
:
1306 afmfile
= find_file (filename
+ '.afm')
1309 afmfile
= os
.path
.abspath (afmfile
)
1310 afm_struct
= afm
.read_afm_file (afmfile
)
1311 fi
.update (afm_struct
.__dict
__)
1314 sys
.stderr
.write ("Warning: no extra font information for this font.\n"
1315 + "Consider writing a XX_guess_font_info() routine.\n")
1319 def do_file (filename
):
1320 encoding_file
= options
.encoding_file
1322 include_dirs
= options
.include_dirs
1323 include_dirs
.append (origdir
)
1325 basename
= strip_extension (filename
, '.mf')
1326 progress (_ ("Font `%s'..." % basename
))
1330 if encoding_file
and not os
.path
.exists (encoding_file
):
1331 encoding_file
= find_file (encoding_file
)
1333 encoding_file
= os
.path
.abspath (encoding_file
)
1336 if options
.tfm_file
:
1337 options
.tfm_file
= os
.path
.abspath (options
.tfm_file
)
1339 tfm_try
= find_file (basename
+ '.tfm')
1341 options
.tfm_file
= tfm_try
1343 if not os
.environ
.has_key ("MFINPUTS"):
1344 os
.environ
["MFINPUTS"] = os
.getcwd () + ":"
1346 ## must change dir before calling mktextfm.
1347 if options
.keep_temp_dir
:
1350 setup_temp (os
.path
.join (os
.getcwd (), program_name
+ '.dir'))
1351 temp_dir
.clean
= nop
1356 progress ('Temporary directory is `%s\'\n' % temp_dir
)
1358 if not options
.tfm_file
:
1359 options
.tfm_file
= popen ("mktextfm %s 2>/dev/null" % shell_escape_filename (basename
)).read ()
1360 if options
.tfm_file
:
1361 options
.tfm_file
= options
.tfm_file
.strip ()
1362 options
.tfm_file
= os
.path
.abspath (options
.tfm_file
)
1364 if not options
.tfm_file
:
1365 error (_ ("Can not find a TFM file to match `%s'") % basename
)
1367 metric
= tfm
.read_tfm_file (options
.tfm_file
)
1369 fontinfo
= guess_fontinfo (basename
)
1370 fontinfo
.update (options
.font_info
)
1372 if not encoding_file
:
1373 codingfile
= 'tex256.enc'
1374 if not coding_dict
.has_key (metric
.coding
):
1375 sys
.stderr
.write ("Unknown encoding `%s'; assuming tex256.\n" % metric
.coding
)
1377 codingfile
= coding_dict
[metric
.coding
]
1379 encoding_file
= find_file (codingfile
)
1380 if not encoding_file
:
1381 error (_ ("can't find file `%s'" % codingfile
))
1383 (enc_name
, encoding
) = read_encoding (encoding_file
)
1385 if not len (options
.glyphs
):
1386 options
.glyphs
= range (0, len (encoding
))
1388 if not options
.gffile
:
1390 base
= gen_pixel_font (basename
, metric
, options
.magnification
)
1391 options
.gffile
= base
+ 'gf'
1393 options
.gffile
= find_file (options
.gffile
)
1395 # the heart of the program:
1396 trace_font (basename
, options
.gffile
, metric
, options
.glyphs
, encoding
,
1397 options
.magnification
, fontinfo
)
1399 make_outputs (basename
, options
.formats
, encoding
)
1400 for format
in options
.formats
:
1401 shutil
.copy2 (basename
+ '.' + format
, origdir
)
1410 backend_options
= getenv ('MFTRACE_BACKEND_OPTIONS', '')
1412 files
= parse_command_line ()
1413 identify (sys
.stderr
)
1415 for filename
in files
:
1417 sys
.exit (exit_value
)
1419 if __name__
=='__main__':