]> git.donarmstrong.com Git - lilypond.git/blob - python/fontextract.py
* input/regression/font-name.ly: show Pango fonts for
[lilypond.git] / python / fontextract.py
1 import re
2 import getopt
3 import sys
4 import os
5 import string
6
7 dsr_font_regex = re.compile ('%%DocumentSuppliedResources: font (.*)')
8 begin_font_regex = re.compile ('%%BeginFont: (.*)')
9 end_font_regex = re.compile ('%%EndFont')
10 verbose = 0
11
12 try:
13         import gettext
14         gettext.bindtextdomain ('lilypond', localedir)
15         gettext.textdomain ('lilypond')
16         _ = gettext.gettext
17 except:
18         def _ (s):
19                 return s
20         
21 def scan_files (files):
22         file_of_font_dict = {}
23         for f in files:
24                 if verbose:
25                         sys.stderr.write (_('Scanning %s') % f + '\n')
26                 
27                 header = open (f, 'r').read ()
28                 idx = 0
29
30                 extract_from_this = []
31                 while idx < len (header):
32                         match = dsr_font_regex.search (header[idx:])
33                         if not match:
34                                 break
35                         name = match.group (1)
36                         idx += match.end (1)
37                         if file_of_font_dict.has_key (name):
38                                 continue
39
40                         file_of_font_dict[name] = f
41
42         return file_of_font_dict
43
44 def get_file_fonts_dict (file_of_font_dict):
45         dict = {}
46         for (n, f) in file_of_font_dict.items ():
47                 if not dict.has_key (f):
48                         dict[f] = []
49
50                 dict[f].append (n)
51                 
52         return dict
53
54 def extract_fonts_from_file (extract_from_this, font_dict, filename):
55         if extract_from_this:
56                 curr_font = []
57                 curr_font_name = []
58                 in_font = 0
59                 for l in open (filename).readlines ():
60                         if not in_font and begin_font_regex.match (l):
61                                 in_font = 1
62                                 curr_font_name = begin_font_regex.match (l).group (1)
63                                 curr_font = []
64                         elif in_font and end_font_regex.match (l):
65                                 in_font = 0
66
67                                 if curr_font_name in extract_from_this:
68                                         font_dict[curr_font_name] = string.join (curr_font, '')
69                                         if verbose:
70                                                 sys.stderr.write (_('Extracted %s')
71                                                                   % curr_font_name + '\n')
72
73                                         extract_from_this.remove (curr_font_name)
74                         elif in_font:
75                                 curr_font.append (l)
76                         if not extract_from_this:
77                                 break
78
79                 if extract_from_this:
80                         sys.stderr.write ("Failed to extract %s from %s\n"
81                                           % (string.join (extract_from_this, ', '), filename))
82
83 def write_extracted_fonts (output_file_name, font_dict):
84
85         if verbose:
86                 sys.stderr.write( _('Writing fonts to %s') % output_file_name + '\n')
87         output = open (output_file_name, 'w')
88         output.write ('''%!PS-Adobe-3.0
89 %%VMusage: 0 0 
90 %%Creator: lilypond-extract-fonts
91 ''')
92
93         for x in font_dict.keys ():
94                 output.write ('%%%%DocumentSuppliedResources: font %s\n' % x)
95
96         output.write ('''%%EndComments\n''')
97
98         for (k,v) in font_dict.items ():
99                 output.write ('\n%%%%BeginFont: %s\n' % k)
100                 output.write (v)
101                 output.write ('\n%%%%EndFont')
102
103
104 def extract_fonts (output_file_name, input_files):
105         d = scan_files (input_files)
106         ff = get_file_fonts_dict (d)
107         
108         font_dict = {}
109         for (file, fonts) in ff.items ():
110                 extract_fonts_from_file (fonts, font_dict, file)
111
112         write_extracted_fonts (output_file_name, font_dict)
113
114
115 if __name__ == '__main__':
116         extract_fonts ('fonts.ps', sys.argv[1:])