4 # mftrace - Generate Type1 or TrueType font from Metafont source.
6 # Copyright (c) 2001--2006 by
7 # Han-Wen Nienhuys, Jan Nieuwenhuizen
9 # Distributed under terms of the GNU General Public License. It comes with
24 localedir
= datadir
+ '/locale'
26 exec_prefix
= '@exec_prefix@'
28 def interpolate (str):
29 str = string
.replace (str, '{', '(')
30 str = string
.replace (str, '}', ')s')
31 str = string
.replace (str, '$', '%')
34 if prefix
!= '@' + 'prefix@':
35 exec_prefix
= interpolate (exec_prefix
) % vars ()
36 bindir
= interpolate (bindir
) % vars ()
37 datadir
= os
.path
.join (interpolate (datadir
) % vars (), 'mftrace')
38 libdir
= interpolate (libdir
) % vars ()
40 if datadir
== '@' + "datadir" + "@":
41 datadir
= os
.getcwd ()
44 sys
.path
.append (datadir
)
49 errorport
= sys
.stderr
51 ################################################################
52 # lilylib.py -- options and stuff
54 # source file of the GNU LilyPond music typesetter
58 gettext
.bindtextdomain ('mftrace', localedir
)
59 gettext
.textdomain ('mftrace')
65 def shell_escape_filename (str):
66 str = re
.sub ('([\'" ])', r
'\\\1', str)
70 port
.write ('%s %s\n' % (program_name
, program_version
))
74 sys
.stdout
.write ('\n')
75 sys
.stdout
.write (_ ('Copyright (c) %s by' % ' 2001--2004'))
76 sys
.stdout
.write ('\n')
77 sys
.stdout
.write (' Han-Wen Nienhuys')
78 sys
.stdout
.write (' Jan Nieuwenhuizen')
79 sys
.stdout
.write ('\n')
80 sys
.stdout
.write (_ (r
'''
81 Distributed under terms of the GNU General Public License. It comes with
83 sys
.stdout
.write ('\n')
89 errorport
.write (_ ("warning: ") + s
)
92 '''Report the error S. Exit by raising an exception. Please
93 do not abuse by trying to catch this error. If you do not want
94 a stack trace, write to the output directly.
102 errorport
.write (_ ("error: ") + s
+ '\n')
103 raise _ ("Exiting ... ")
107 Create a temporary directory, and return its name.
110 if not options
.keep_temp_dir
:
111 temp_dir
= tempfile
.mkdtemp (program_name
)
114 os
.mkdir (temp_dir
, 0700)
120 def popen (cmd
, mode
= 'r', ignore_error
= 0):
122 progress (_ ("Opening pipe `%s\'") % cmd
)
123 pipe
= os
.popen (cmd
, mode
)
128 def system (cmd
, ignore_error
= 0):
129 """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
137 progress (_ ("Invoking `%s\'\n") % cmd
)
140 name
= re
.match ('[ \t]*([^ \t]*)', cmd
).group (1)
141 msg
= name
+ ': ' + _ ("command exited with value %d") % st
143 warning (msg
+ ' ' + _ ("(ignored)") + ' ')
151 if not options
.keep_temp_dir
:
153 progress (_ ("Cleaning %s...") % temp_dir
)
154 shutil
.rmtree (temp_dir
)
157 def strip_extension (f
, ext
):
158 (p
, e
) = os
.path
.splitext (f
)
164 ################################################################
172 program_name
= 'mftrace'
173 temp_dir
= os
.path
.join (os
.getcwd (), program_name
+ '.dir')
174 program_version
= '@VERSION@'
175 origdir
= os
.getcwd ()
180 'TeX typewriter text': '09fbbfac.enc', # cmtt10
181 'TeX math symbols':'10037936.enc ', # cmbsy
182 'ASCII caps and digits':'1b6d048e', # cminch
183 'TeX math italic': 'aae443f0.enc ', # cmmi10
184 'TeX extended ASCII':'d9b29452.enc',
185 'TeX text': 'f7b6d320.enc',
186 'TeX text without f-ligatures': '0ef0afca.enc',
188 'Extended TeX Font Encoding - Latin': 'tex256.enc',
191 'fetaBraces': 'feta-braces-a.enc',
192 'fetaNumber': 'feta-nummer10.enc',
193 'fetaMusic': 'feta20.enc',
194 'parmesanMusic': 'parmesan20.enc',
199 for d
in include_dirs
:
200 p
= os
.path
.join (d
, nm
)
207 p
= popen ('kpsewhich %s' % shell_escape_filename (nm
)).read ()
210 if options
.dos_kpath
:
213 p
= re
.sub ('^([a-z]):', '/cygdrive/\\1', p
)
214 p
= re
.sub ('\\\\', '/', p
)
215 sys
.stderr
.write ("Got `%s' from kpsewhich, using `%s'\n" % (orig
, p
))
223 ################################################################
225 ################################################################
227 def autotrace_command (fn
, opts
):
228 opts
= " " + opts
+ " --background-color=FFFFFF --output-format=eps --input-format=pbm "
229 return options
.trace_binary
+ opts
+ backend_options \
230 + " --output-file=char.eps %s " % fn
232 def potrace_command (fn
, opts
):
233 return options
.trace_binary
+ opts \
234 + ' -u %d ' % options
.grid_scale \
236 + " -q -c --eps --output=char.eps %s " % (fn
)
239 path_to_type1_ops
= None
241 def trace_one (pbmfile
, id):
243 Run tracer, do error handling
246 status
= system (trace_command (pbmfile
, ''), 1)
249 sys
.stderr
.write ("\nUser interrupt. Exiting\n")
252 if status
== 0 and options
.keep_temp_dir
:
253 shutil
.copy2 (pbmfile
, '%s.pbm' % id)
254 shutil
.copy2 ('char.eps', '%s.eps' % id)
257 error_file
= os
.path
.join (origdir
, 'trace-bug-%s.pbm' % id)
258 shutil
.copy2 (pbmfile
, error_file
)
259 msg
= """Trace failed on bitmap. Bitmap left in `%s\'
264 Please submit a bugreport to %s development.""" \
265 % (error_file
, trace_command (error_file
, ''), options
.trace_binary
)
267 if options
.keep_trying
:
269 sys
.stderr
.write ("\nContinuing trace...\n")
272 msg
= msg
+ '\nRun mftrace with --keep-trying to produce a font anyway\n'
278 warning ("Failed, skipping character.\n")
283 def make_pbm (filename
, outname
, char_number
):
284 """ Extract bitmap from the PK file FILENAME (absolute) using `gf2pbm'.
285 Return FALSE if the glyph is not valid.
288 command
= "%s/gf2pbm -n %d -o %s %s" % (bindir
, char_number
, outname
, filename
)
289 status
= system (command
, ignore_error
= 1)
292 def read_encoding (file):
293 sys
.stderr
.write (_ ("Using encoding file: `%s'\n") % file)
295 str = open (file).read ()
296 str = re
.sub ("%.*", '', str)
297 str = re
.sub ("[\n\t \f]+", ' ', str)
298 m
= re
.search ('/([^ ]+) \[([^\]]+)\] def', str)
300 error ("Encoding file is invalid")
304 cod
= re
.sub ('[ /]+', ' ', cod
)
305 cods
= string
.split (cod
)
309 def zip_to_pairs (as):
312 r
.append ((as[0], as[1]))
316 def unzip_pairs (tups
):
319 lst
= lst
+ list (tups
[0])
323 def autotrace_path_to_type1_ops (at_file
, bitmap_metrics
, tfm_wid
, magnification
):
324 inv_scale
= 1000.0 / magnification
326 (size_y
, size_x
, off_x
, off_y
) = map (lambda m
, s
= inv_scale
: m
* s
,
328 ls
= open (at_file
).readlines ()
329 bbox
= (10000, 10000, -10000, -10000)
331 while ls
and ls
[0] != '*u\n':
342 while ls
[0] != '*U\n':
346 toks
= string
.split (ell
)
351 args
= map (lambda m
, s
= inv_scale
: s
* float (m
),
353 if options
.round_to_int
:
354 args
= zip_to_pairs (map (round, args
))
356 args
= zip_to_pairs (args
)
357 commands
.append ((cmd
, args
))
367 cy
= size_y
- off_y
- inv_scale
369 # t1asm seems to fuck up when using sbw. Oh well.
370 t1_outline
= ' %d %d hsbw\n' % (- off_x
, tfm_wid
)
371 bbox
= (10000, 10000, -10000, -10000)
373 for (c
, args
) in commands
:
378 if c
== 'l' or c
== 'c':
379 bbox
= update_bbox_with_point (bbox
, a
)
381 na
.append ((nx
- cx
, ny
- cy
))
386 if options
.round_to_int
:
387 a
= map (lambda x
: '%d' % int (round (x
)),
390 a
= map (lambda x
: '%d %d div' \
391 % (int (round (x
* options
.grid_scale
/inv_scale
)),
392 int (round (options
.grid_scale
/inv_scale
))),
395 t1_outline
= t1_outline
+ ' %s %s\n' % (string
.join (a
), c
)
397 t1_outline
= t1_outline
+ ' endchar '
398 t1_outline
= '{\n %s } |- \n' % t1_outline
400 return (bbox
, t1_outline
)
402 # FIXME: Cut and paste programming
403 def potrace_path_to_type1_ops (at_file
, bitmap_metrics
, tfm_wid
, magnification
):
404 inv_scale
= 1000.0 / magnification
406 (size_y
, size_x
, off_x
, off_y
) = map (lambda m
,
407 s
= inv_scale
: m
* s
,
409 ls
= open (at_file
).readlines ()
410 bbox
= (10000, 10000, -10000, -10000)
412 while ls
and ls
[0] != '0 setgray\n':
420 while ls
and ls
[0] != 'grestore\n':
427 toks
= string
.split (ell
)
432 args
= map (lambda m
, s
= inv_scale
: s
* float (m
),
434 args
= zip_to_pairs (args
)
435 commands
.append ((cmd
, args
))
437 # t1asm seems to fuck up when using sbw. Oh well.
438 t1_outline
= ' %d %d hsbw\n' % (- off_x
, tfm_wid
)
439 bbox
= (10000, 10000, -10000, -10000)
441 # Type1 fonts have relative coordinates (doubly relative for
442 # rrcurveto), so must convert moveto and rcurveto.
444 z
= (0.0, size_y
- off_y
- 1.0)
445 for (c
, args
) in commands
:
446 args
= map (lambda x
: (x
[0] * (1.0 / options
.grid_scale
),
447 x
[1] * (1.0 / options
.grid_scale
)), args
)
450 args
= [(args
[0][0] - z
[0], args
[0][1] - z
[1])]
454 lz
= (z
[0] + a
[0], z
[1] + a
[1])
455 bbox
= update_bbox_with_point (bbox
, lz
)
458 if options
.round_to_int
:
459 last_discr_z
= (int (round (z
[0])), int (round (z
[1])))
461 last_discr_z
= (z
[0], z
[1])
464 if options
.round_to_int
:
465 a
= (int (round (a
[0])), int (round (a
[1])))
468 args
.append ((a
[0] - last_discr_z
[0],
469 a
[1] - last_discr_z
[1]))
475 c
= { 'rcurveto': 'rrcurveto',
477 'closepath': 'closepath',
478 'rlineto': 'rlineto'}[c
]
481 t1_outline
+= ' closepath '
483 if options
.round_to_int
:
484 args
= map (lambda x
: '%d' % int (round (x
)),
487 args
= map (lambda x
: '%d %d div' \
488 % (int (round (x
*options
.grid_scale
/inv_scale
)),
489 int (round (options
.grid_scale
/inv_scale
))),
492 t1_outline
= t1_outline
+ ' %s %s\n' % (string
.join (args
), c
)
494 t1_outline
= t1_outline
+ ' endchar '
495 t1_outline
= '{\n %s } |- \n' % t1_outline
497 return (bbox
, t1_outline
)
499 def read_gf_dims (name
, c
):
500 str = popen ('%s/gf2pbm -n %d -s %s' % (bindir
, c
, name
)).read ()
501 m
= re
.search ('size: ([0-9]+)+x([0-9]+), offset: \(([0-9-]+),([0-9-]+)\)', str)
503 return tuple (map (int, m
.groups ()))
505 def trace_font (fontname
, gf_file
, metric
, glyphs
, encoding
,
506 magnification
, fontinfo
):
508 font_bbox
= (10000, 10000, -10000, -10000)
510 progress (_ ("Tracing bitmaps... "))
512 # for single glyph testing.
515 if encoding
[a
] == ".notavail":
517 valid
= metric
.has_char (a
)
519 encoding
[a
] = ".notavail"
522 valid
= make_pbm (gf_file
, 'char.pbm', a
)
524 encoding
[a
] = ".notavail"
527 (w
, h
, xo
, yo
) = read_gf_dims (gf_file
, a
)
529 if not options
.verbose
:
530 sys
.stderr
.write ('[%d' % a
)
533 # this wants the id, not the filename.
534 success
= trace_one ("char.pbm", '%s-%d' % (options
.gffile
, a
))
536 sys
.stderr
.write ("(skipping character)]")
538 encoding
[a
] = ".notavail"
541 if not options
.verbose
:
542 sys
.stderr
.write (']')
544 metric_width
= metric
.get_char (a
).width
545 tw
= int (round (metric_width
/ metric
.design_size
* 1000))
546 (bbox
, t1o
) = path_to_type1_ops ("char.eps", (h
, w
, xo
, yo
),
550 encoding
[a
] = ".notavail"
553 font_bbox
= update_bbox_with_bbox (font_bbox
, bbox
)
555 t1os
.append ('\n/%s %s ' % (encoding
[a
], t1o
))
558 to_type1 (t1os
, font_bbox
, fontname
, encoding
, magnification
, fontinfo
)
560 def ps_encode_encoding (encoding
):
561 str = ' %d array\n0 1 %d {1 index exch /.notdef put} for\n' \
562 % (len (encoding
), len (encoding
)-1)
564 for i
in range (0, len (encoding
)):
565 if encoding
[i
] != ".notavail":
566 str = str + 'dup %d /%s put\n' % (i
, encoding
[i
])
571 def gen_unique_id (dict):
573 return 4000000 + (hash (nm
) % 1000000)
575 def to_type1 (outlines
, bbox
, fontname
, encoding
, magnification
, fontinfo
):
578 Fill in the header template for the font, append charstrings,
579 and shove result through t1asm
581 template
= r
"""%%!PS-AdobeFont-1.0: %(FontName)s %(VVV)s.%(WWW)s
583 /FontInfo 16 dict dup begin
584 /version (%(VVV)s.%(WWW)s) readonly def
585 /Notice (%(Notice)s) readonly def
586 /FullName (%(FullName)s) readonly def
587 /FamilyName (%(FamilyName)s) readonly def
588 /Weight (%(Weight)s) readonly def
589 /ItalicAngle %(ItalicAngle)s def
590 /isFixedPitch %(isFixedPitch)s def
591 /UnderlinePosition %(UnderlinePosition)s def
592 /UnderlineThickness %(UnderlineThickness)s def
594 /FontName /%(FontName)s def
597 /FontMatrix [%(xrevscale)f 0 0 %(yrevscale)f 0 0] readonly def
598 /FontBBox {%(llx)d %(lly)d %(urx)d %(ury)d} readonly def
599 /Encoding %(Encoding)s readonly def
602 dup /Private 20 dict dup begin
603 /-|{string currentfile exch readstring pop}executeonly def
604 /|-{noaccess def}executeonly def
605 /|{noaccess put}executeonly def
608 /MinFeature {16 16} |-
610 /OtherSubrs [ {} {} {} {} ] |-
616 /CharStrings %(CharStringsLen)d dict dup begin
620 /.notdef { 0 0 hsbw endchar } |-
625 dup/FontName get exch definefont
626 pop mark currentfile closefile
629 ## apparently, some fonts end the file with cleartomark. Don't know why.
631 copied_fields
= ['FontName', 'FamilyName', 'FullName', 'DesignSize',
632 'ItalicAngle', 'isFixedPitch', 'Weight']
637 'Notice': 'Generated from MetaFont bitmap by mftrace %s, http://www.xs4all.nl/~hanwen/mftrace/ ' % program_version
,
638 'UnderlinePosition': '-100',
639 'UnderlineThickness': '50',
640 'xrevscale': 1.0/1000.0,
641 'yrevscale': 1.0/1000.0,
646 'Encoding': ps_encode_encoding (encoding
),
648 # need one extra entry for .notdef
649 'CharStringsLen': len (outlines
) + 1,
650 'CharStrings': string
.join (outlines
),
651 'CharBBox': '0 0 0 0',
654 for k
in copied_fields
:
655 vars[k
] = fontinfo
[k
]
657 open ('mftrace.t1asm', 'w').write (template
% vars)
659 def update_bbox_with_point (bbox
, pt
):
660 (llx
, lly
, urx
, ury
) = bbox
661 llx
= min (pt
[0], llx
)
662 lly
= min (pt
[1], lly
)
663 urx
= max (pt
[0], urx
)
664 ury
= max (pt
[1], ury
)
666 return (llx
, lly
, urx
, ury
)
668 def update_bbox_with_bbox (bb
, dims
):
669 (llx
, lly
, urx
, ury
) = bb
670 llx
= min (llx
, dims
[0])
671 lly
= min (lly
, dims
[1])
672 urx
= max (urx
, dims
[2])
673 ury
= max (ury
, dims
[3])
675 return (llx
, lly
, urx
, ury
)
677 def get_binary (name
):
678 search_path
= string
.split (os
.environ
['PATH'], ':')
679 for p
in search_path
:
680 nm
= os
.path
.join (p
, name
)
681 if os
.path
.exists (nm
):
686 def get_fontforge_command ():
688 for ff
in ['fontforge', 'pfaedit']:
694 stat
= system ("%s -usage > pfv 2>&1 " % fontforge_cmd
,
698 warning ("Command `%s -usage' failed. Cannot simplify or convert to TTF.\n" % fontforge_cmd
)
700 if fontforge_cmd
== 'pfaedit' \
701 and re
.search ("-script", open ('pfv').read ()) == None:
702 warning ("pfaedit does not support -script. Install 020215 or later.\nCannot simplify or convert to TTF.\n")
706 def tfm2kpx (tfmname
, encoding
):
708 pl
= popen ("tftopl %s" % (tfmname
))
710 label_pattern
= re
.compile (
711 "\A \(LABEL ([DOHC]{1}) ([A-Za-z0-9]*)\)")
712 krn_pattern
= re
.compile (
713 "\A \(KRN ([DOHC]{1}) ([A-Za-z0-9]*) R (-?[\d\.]+)\)")
718 for line
in pl
.readlines ():
720 label_match
= label_pattern
.search (line
)
721 if not (label_match
is None):
722 if label_match
.group (1) == "D":
723 first
= int (label_match
.group (2))
724 elif label_match
.group (1) == "O":
725 first
= int (label_match
.group (2), 8)
726 elif label_match
.group (1) == "C":
727 first
= ord (label_match
.group (2))
729 krn_match
= krn_pattern
.search (line
)
730 if not (krn_match
is None):
731 if krn_match
.group (1) == "D":
732 second
= int (krn_match
.group (2))
733 elif krn_match
.group (1) == "O":
734 second
= int (krn_match
.group (2), 8)
735 elif krn_match
.group (1) == "C":
736 second
= ord (krn_match
.group (2))
738 krn
= round (float (krn_match
.group (3)) * 1000)
740 if (encoding
[first
] != '.notavail' and
741 encoding
[first
] != '.notdef' and
742 encoding
[second
] != '.notavail' and
743 encoding
[second
] != '.notdef'):
745 kpx_lines
.append ("KPX %s %s %d\n" % (
746 encoding
[first
], encoding
[second
], krn
))
750 def get_afm (t1_path
, tfmname
, encoding
, out_path
):
751 afm_stream
= popen ("printafm %s" % (t1_path
))
753 kpx_lines
= tfm2kpx (tfmname
, encoding
)
755 for line
in afm_stream
.readlines ():
756 afm_lines
.append (line
)
758 if re
.match (r
"^EndCharMetrics", line
, re
.I
):
759 afm_lines
.append ("StartKernData\n")
760 afm_lines
.append ("StartKernPairs %d\n" % len (kpx_lines
))
762 for kpx_line
in kpx_lines
:
763 afm_lines
.append (kpx_line
)
765 afm_lines
.append ("EndKernPairs\n")
766 afm_lines
.append ("EndKernData\n")
768 progress (_ ("Writing metrics to `%s'... ") % out_path
)
769 afm_file
= open (out_path
, 'w')
770 afm_file
.writelines (afm_lines
)
776 def assemble_font (fontname
, format
, is_raw
):
786 outname
= fontname
+ ext
788 progress (_ ("Assembling raw font to `%s'... ") % outname
)
789 system ('t1asm %s mftrace.t1asm %s' % (asm_opt
, shell_escape_filename (outname
)))
793 def make_outputs (fontname
, formats
, encoding
):
795 run pfaedit to convert to other formats
801 if (options
.simplify
or options
.round_to_int
or 'ttf' in formats
or 'svg' in formats
):
804 ff_command
= get_fontforge_command ()
806 if ff_needed
and ff_command
:
807 raw_name
= assemble_font (fontname
, 'pfa', 1)
810 if options
.round_to_int
:
811 simplify_cmd
= 'RoundToInt ();'
814 generate_cmds
+= 'Generate("%s");' % (fontname
+ '.' + f
)
817 simplify_cmd
='''SelectAll ();
822 AutoHint ();''' % vars()
824 pe_script
= ('''#!/usr/bin/env %(ff_command)s
832 open ('to-ttf.pe', 'w').write (pe_script
)
834 print 'Fontforge script', pe_script
835 system ("%s -script to-ttf.pe %s %s" % (ff_command
,
836 shell_escape_filename (raw_name
), shell_escape_filename (options
.tfm_file
)))
840 if ('pfa' in formats
):
841 t1_path
= assemble_font (fontname
, 'pfa', 0)
843 if ('pfb' in formats
):
844 t1_path
= assemble_font (fontname
, 'pfb', 0)
846 if (t1_path
!= '' and 'afm' in formats
):
847 get_afm (t1_path
, options
.tfm_file
, encoding
, fontname
+ '.afm')
850 def getenv (var
, default
):
851 if os
.environ
.has_key (var
):
852 return os
.environ
[var
]
856 def gen_pixel_font (filename
, metric
, magnification
):
858 Generate a GF file for FILENAME, such that `magnification'*mfscale
859 (default 1000 * 1.0) pixels fit on the designsize.
863 size
= metric
.design_size
865 size_points
= size
* 1/72.27 * base_dpi
867 mag
= magnification
/ size_points
869 prod
= mag
* base_dpi
871 open ('%s.%dgf' % (filename
, prod
))
873 os
.environ
['KPSE_DOT'] = '%s:' % origdir
875 os
.environ
['MFINPUTS'] = '%s:%s' % (origdir
,
876 getenv ('MFINPUTS', ''))
877 os
.environ
['TFMFONTS'] = '%s:%s' % (origdir
,
878 getenv ('TFMINPUTS', ''))
880 # FIXME: we should not change to another (tmp) dir?
881 # or else make all relavitive dirs in paths absolute.
882 def abs_dir (x
, dir):
883 if x
and os
.path
.abspath (x
) != x
:
884 return os
.path
.join (dir, x
)
887 def abs_path (path
, dir):
888 # Python's ABSPATH means ABSDIR
889 dir = os
.path
.abspath (dir)
890 return string
.join (map (lambda x
: abs_dir (x
, dir),
895 os
.environ
['MFINPUTS'] = abs_path (os
.environ
['MFINPUTS'],
897 os
.environ
['TFMFONTS'] = abs_path (os
.environ
['TFMFONTS'],
900 progress (_ ("Running Metafont..."))
902 cmdstr
= r
"mf '\mode:=lexmarks; mag:=%f; nonstopmode; input %s'" % (mag
, filename
)
903 if not options
.verbose
:
904 cmdstr
= cmdstr
+ ' 1>/dev/null 2>/dev/null'
905 st
= system (cmdstr
, ignore_error
= 1)
908 logfile
= '%s.log' % filename
911 if os
.path
.exists (logfile
):
912 log
= open (logfile
).read ()
913 m
= re
.search ('Output written on %s.([0-9]+)gf' % re
.escape (filename
), log
)
914 prod
= int (m
.group (1))
917 sys
.stderr
.write ('\n\nMetafont failed. Excerpt from the log file: \n\n*****')
918 m
= re
.search ("\n!", log
)
920 short_log
= log
[start
:start
+200]
921 sys
.stderr
.write (short_log
)
922 sys
.stderr
.write ('\n*****\n')
923 if re
.search ('Arithmetic overflow', log
):
924 sys
.stderr
.write ("""
926 Apparently, some numbers overflowed. Try using --magnification with a
927 lower number. (Current magnification: %d)
930 if not options
.keep_trying
or prod
== 0:
933 sys
.stderr
.write ('\n\nTrying to proceed despite of the Metafont errors...\n')
937 return "%s.%d" % (filename
, prod
)
939 def parse_command_line ():
940 p
= optparse
.OptionParser (version
="""mftrace @VERSION@
942 This program is free software. It is covered by the GNU General Public
943 License and you are welcome to change it and/or distribute copies of it
944 under certain conditions. Invoke as `mftrace --warranty' for more
947 Copyright (c) 2005--2006 by
948 Han-Wen Nienhuys <hanwen@xs4all.nl>
951 p
.usage
= "mftrace [OPTION]... FILE..."
952 p
.description
= _ ("Generate Type1 or TrueType font from Metafont source.")
954 p
.add_option ('-k', '--keep',
956 dest
="keep_temp_dir",
957 help=_ ("Keep all output in directory %s.dir") % program_name
)
958 p
.add_option ('','--magnification',
959 dest
="magnification",
963 help=_("Set magnification for MF to MAG (default: 1000)"))
964 p
.add_option ('-V', '--verbose',
967 help=_ ("Be verbose"))
968 p
.add_option ('-f', '--formats',
972 help=_("Which formats to generate (choices: AFM, PFA, PFB, TTF, SVG)"))
973 p
.add_option ('', '--simplify',
976 help=_ ("Simplify using fontforge"))
977 p
.add_option ('', '--gffile',
979 help= _("Use gf FILE instead of running Metafont"))
980 p
.add_option ('-I', '--include',
984 help=_("Add to path for searching files"))
985 p
.add_option ('','--glyphs',
990 help= _('Process only these glyphs. LIST is comma separated'))
991 p
.add_option ('', '--tfmfile',
995 p
.add_option ('-e', '--encoding',
998 dest
="encoding_file",
1000 help= _ ("Use encoding file FILE"))
1001 p
.add_option ('','--keep-trying',
1004 action
="store_true",
1005 help= _ ("Don't stop if tracing fails"))
1006 p
.add_option ('-w', '--warranty',
1007 action
="store_true",
1008 help=_ ("show warranty and copyright"))
1009 p
.add_option ('','--dos-kpath',
1011 help=_("try to use Miktex kpsewhich"))
1012 p
.add_option ('', '--potrace',
1014 help=_ ("Use potrace"))
1015 p
.add_option ('', '--autotrace',
1017 help=_ ("Use autotrace"))
1018 p
.add_option ('', '--no-afm',
1019 action
='store_false',
1022 help=_("Don't read AFM file"))
1023 p
.add_option ('','--noround',
1024 action
="store_false",
1025 dest
='round_to_int',
1027 help= ("Do not round coordinates of control points to integer values (use with --grid)"))
1028 p
.add_option ('','--grid',
1033 help=_ ("Set reciprocal grid size in em units"))
1034 p
.add_option ('-D','--define',
1035 metavar
="SYMBOL=VALUE",
1038 action
='append',help=_("Set the font info SYMBOL to VALUE"))
1041 (options
, files
) = p
.parse_args ()
1044 sys
.stderr
.write ('Need argument on command line \n')
1048 if options
.warranty
:
1052 options
.font_info
= {}
1053 for d
in options
.defs
:
1056 options
.font_info
[kv
] = 'true'
1058 options
.font_info
[kv
[0]] = '='.join (kv
[1:])
1060 def comma_sepped_to_list (x
):
1063 fs
+= f
.lower ().split (',')
1066 options
.formats
= comma_sepped_to_list (options
.formats
)
1069 for r
in options
.glyphs
:
1070 new_glyphs
+= r
.split (',')
1071 options
.glyphs
= new_glyphs
1074 for r
in options
.glyphs
:
1075 glyph_subrange
= map (int, string
.split (r
, '-'))
1076 if len (glyph_subrange
) == 2 and glyph_subrange
[0] < glyph_subrange
[1] + 1:
1077 glyph_range
+= range (glyph_subrange
[0], glyph_subrange
[1] + 1)
1079 glyph_range
.append (glyph_subrange
[0])
1081 options
.glyphs
= glyph_range
1083 options
.trace_binary
= ''
1085 options
.trace_binary
= 'potrace'
1086 elif options
.autotrace
:
1087 options
.trace_binary
= 'autotrace'
1089 if options
.formats
== []:
1090 options
.formats
= ['pfa']
1094 global trace_command
1095 global path_to_type1_ops
1097 stat
= os
.system ('potrace --version > /dev/null 2>&1 ')
1098 if options
.trace_binary
!= 'autotrace' and stat
== 0:
1099 options
.trace_binary
= 'potrace'
1101 trace_command
= potrace_command
1102 path_to_type1_ops
= potrace_path_to_type1_ops
1104 stat
= os
.system ('autotrace --version > /dev/null 2>&1 ')
1105 if options
.trace_binary
!= 'potrace' and stat
== 0:
1106 options
.trace_binary
= 'autotrace'
1107 trace_command
= autotrace_command
1108 path_to_type1_ops
= autotrace_path_to_type1_ops
1110 if not options
.trace_binary
:
1111 error (_ ("No tracing program found.\nInstall potrace or autotrace."))
1116 def derive_font_name (family
, fullname
):
1117 fullname
= re
.sub (family
, '', fullname
)
1118 family
= re
.sub (' ', '', family
)
1119 fullname
= re
.sub ('Oldstyle Figures', 'OsF', fullname
)
1120 fullname
= re
.sub ('Small Caps', 'SC', fullname
)
1121 fullname
= re
.sub ('[Mm]edium', '', fullname
)
1122 fullname
= re
.sub ('[^A-Za-z0-9]', '', fullname
)
1123 return '%s-%s' % (family
, fullname
)
1125 def cm_guess_font_info (filename
, fontinfo
):
1127 filename
= re
.sub ("cm(.*)tt", r
"cmtt\1", filename
)
1128 m
= re
.search ("([0-9]+)$", filename
)
1131 design_size
= int (m
.group (1))
1132 fontinfo
['DesignSize'] = design_size
1134 prefixes
= [("cmtt", "Computer Modern Typewriter"),
1135 ("cmvtt", "Computer Modern Variable Width Typewriter"),
1136 ("cmss", "Computer Modern Sans"),
1137 ("cm", "Computer Modern")]
1140 for (k
, v
) in prefixes
:
1141 if re
.search (k
, filename
):
1144 fontinfo
['isFixedPitch'] = 'true'
1145 filename
= re
.sub (k
, '', filename
)
1149 prefixes
= [("r", "Roman"),
1150 ("mi", "Math italic"),
1151 ("u", "Unslanted italic"),
1153 ("csc", "Small Caps"),
1154 ("ex", "Math extension"),
1155 ("ti", "Text italic"),
1158 for (k
, v
) in prefixes
:
1159 if re
.search (k
, filename
):
1161 filename
= re
.sub (k
, '', filename
)
1163 prefixes
= [("b", "Bold"),
1166 for (k
, v
) in prefixes
:
1167 if re
.search (k
, filename
):
1169 filename
= re
.sub (k
, '', filename
)
1171 prefixes
= [("c", "Condensed"),
1174 for (k
, v
) in prefixes
:
1175 if re
.search (k
, filename
):
1177 filename
= re
.sub (k
, '', filename
)
1179 fontinfo
['ItalicAngle'] = 0
1180 if re
.search ('[Ii]talic', shape
) or re
.search ('[Oo]blique', shape
):
1182 if re
.search ("Sans", family
):
1185 fontinfo
["ItalicAngle"] = a
1187 fontinfo
['Weight'] = weight
1188 fontinfo
['FamilyName'] = family
1189 full
= '%s %s %s %s %dpt' \
1190 % (family
, shape
, weight
, stretch
, design_size
)
1191 full
= re
.sub (" +", ' ', full
)
1193 fontinfo
['FullName'] = full
1194 fontinfo
['FontName'] = derive_font_name (family
, full
)
1198 def ec_guess_font_info (filename
, fontinfo
):
1200 m
= re
.search ("([0-9]+)$", filename
)
1202 design_size
= int (m
.group (1))
1203 fontinfo
['DesignSize'] = design_size
1205 prefixes
= [("ecss", "European Computer Modern Sans"),
1206 ("ectt", "European Computer Modern Typewriter"),
1207 ("ec", "European Computer Modern")]
1210 for (k
, v
) in prefixes
:
1211 if re
.search (k
, filename
):
1213 fontinfo
['isFixedPitch'] = 'true'
1215 filename
= re
.sub (k
, '', filename
)
1219 prefixes
= [("r", "Roman"),
1220 ("mi", "Math italic"),
1221 ("u", "Unslanted italic"),
1223 ("cc", "Small caps"),
1224 ("ex", "Math extension"),
1229 for (k
, v
) in prefixes
:
1230 if re
.search (k
, filename
):
1232 filename
= re
.sub (k
, '', filename
)
1234 prefixes
= [("b", "Bold"),
1237 for (k
, v
) in prefixes
:
1238 if re
.search (k
, filename
):
1240 filename
= re
.sub (k
, '', filename
)
1242 prefixes
= [("c", "Condensed"),
1245 for (k
, v
) in prefixes
:
1246 if re
.search (k
, filename
):
1248 filename
= re
.sub (k
, '', filename
)
1250 fontinfo
['ItalicAngle'] = 0
1251 if re
.search ('[Ii]talic', shape
) or re
.search ('[Oo]blique', shape
):
1253 if re
.search ("Sans", family
):
1256 fontinfo
["ItalicAngle"] = a
1258 fontinfo
['Weight'] = weight
1259 fontinfo
['FamilyName'] = family
1260 full
= '%s %s %s %s %dpt' \
1261 % (family
, shape
, weight
, stretch
, design_size
)
1262 full
= re
.sub (" +", ' ', full
)
1264 fontinfo
['FontName'] = derive_font_name (family
, full
)
1265 fontinfo
['FullName'] = full
1270 def guess_fontinfo (filename
):
1272 'FontName': filename
,
1273 'FamilyName': filename
,
1274 'Weight': 'Regular',
1277 'isFixedPitch' : 'false',
1278 'FullName': filename
,
1281 if re
.search ('^cm', filename
):
1282 fi
.update (cm_guess_font_info (filename
, fi
))
1283 elif re
.search ("^ec", filename
):
1284 fi
.update (ec_guess_font_info (filename
, fi
))
1285 elif options
.read_afm
:
1288 afmfile
= find_file (filename
+ '.afm')
1291 afmfile
= os
.path
.abspath (afmfile
)
1292 afm_struct
= afm
.read_afm_file (afmfile
)
1293 fi
.update (afm_struct
.__dict
__)
1296 sys
.stderr
.write ("Warning: no extra font information for this font.\n"
1297 + "Consider writing a XX_guess_font_info() routine.\n")
1301 def do_file (filename
):
1302 encoding_file
= options
.encoding_file
1304 include_dirs
= options
.include_dirs
1305 include_dirs
.append (origdir
)
1307 basename
= strip_extension (filename
, '.mf')
1308 progress (_ ("Font `%s'..." % basename
))
1311 if not options
.tfm_file
:
1312 options
.tfm_file
= find_file (basename
+ '.tfm')
1314 if not options
.tfm_file
:
1315 options
.tfm_file
= popen ("mktextfm %s 2>/dev/null" % shell_escape_filename (basename
)).read ()
1316 if options
.tfm_file
:
1317 options
.tfm_file
= options
.tfm_file
[:-1]
1319 if not options
.tfm_file
:
1320 error (_ ("Can not find a TFM file to match `%s'") % basename
)
1322 options
.tfm_file
= os
.path
.abspath (options
.tfm_file
)
1323 metric
= tfm
.read_tfm_file (options
.tfm_file
)
1325 fontinfo
= guess_fontinfo (basename
)
1326 fontinfo
.update (options
.font_info
)
1328 if encoding_file
and not os
.path
.exists (encoding_file
):
1329 encoding_file
= find_file (encoding_file
)
1332 if not encoding_file
:
1333 codingfile
= 'tex256.enc'
1334 if not coding_dict
.has_key (metric
.coding
):
1335 sys
.stderr
.write ("Unknown encoding `%s'; assuming tex256.\n" % metric
.coding
)
1337 codingfile
= coding_dict
[metric
.coding
]
1339 encoding_file
= find_file (codingfile
)
1340 if not encoding_file
:
1341 error (_ ("can't find file `%s'" % codingfile
))
1343 (enc_name
, encoding
) = read_encoding (encoding_file
)
1345 if not len (options
.glyphs
):
1346 options
.glyphs
= range (0, len (encoding
))
1349 temp_dir
= setup_temp ()
1352 progress ('Temporary directory is `%s\' ' % temp_dir
)
1356 if not options
.gffile
:
1358 base
= gen_pixel_font (basename
, metric
, options
.magnification
)
1359 options
.gffile
= base
+ 'gf'
1361 options
.gffile
= find_file (options
.gffile
)
1363 # the heart of the program:
1364 trace_font (basename
, options
.gffile
, metric
, options
.glyphs
, encoding
,
1365 options
.magnification
, fontinfo
)
1367 make_outputs (basename
, options
.formats
, encoding
)
1368 for format
in options
.formats
:
1369 shutil
.copy2 (basename
+ '.' + format
, origdir
)
1379 backend_options
= getenv ('MFTRACE_BACKEND_OPTIONS', '')
1381 files
= parse_command_line ()
1382 identify (sys
.stderr
)
1384 for filename
in files
:
1386 sys
.exit (exit_value
)
1388 if __name__
=='__main__':