8 dsr_font_regex = re.compile ('%%DocumentSuppliedResources: font (.*)')
9 begin_font_regex = re.compile ('%%BeginFont: (.*)')
10 end_font_regex = re.compile ('%%EndFont')
15 gettext.bindtextdomain ('lilypond', localedir)
16 gettext.textdomain ('lilypond')
22 def scan_files (files):
23 file_of_font_dict = {}
26 sys.stderr.write (_('Scanning %s') % f + '\n')
28 header = open (f, 'r').read ()
31 extract_from_this = []
32 while idx < len (header):
33 match = dsr_font_regex.search (header[idx:])
36 name = match.group (1)
38 if file_of_font_dict.has_key (name):
41 file_of_font_dict[name] = f
43 return file_of_font_dict
45 def get_file_fonts_dict (file_of_font_dict):
47 for (n, f) in file_of_font_dict.items ():
48 if not dict.has_key (f):
55 def extract_fonts_from_file (extract_from_this, font_dict, filename):
60 for l in open (filename).readlines ():
61 if not in_font and begin_font_regex.match (l):
63 curr_font_name = begin_font_regex.match (l).group (1)
65 elif in_font and end_font_regex.match (l):
68 if curr_font_name in extract_from_this:
69 font_dict[curr_font_name] = string.join (curr_font, '')
71 sys.stderr.write (_('Extracted %s')
72 % curr_font_name + '\n')
74 extract_from_this.remove (curr_font_name)
77 if not extract_from_this:
81 sys.stderr.write ("Failed to extract %s from %s\n"
82 % (string.join (extract_from_this, ', '), filename))
84 def write_extracted_fonts (output_file_name, font_dict):
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
90 %%Creator: lilypond-extract-fonts
93 for x in font_dict.keys ():
94 output.write ('%%%%DocumentSuppliedResources: font %s\n' % x)
96 output.write ('''%%EndComments\n''')
98 for (k,v) in font_dict.items ():
99 output.write ('\n%%%%BeginFont: %s\n' % k)
101 output.write ('\n%%EndFont')
104 def extract_fonts (output_file_name, input_files):
105 d = scan_files (input_files)
106 ff = get_file_fonts_dict (d)
109 for (file, fonts) in ff.items ():
110 extract_fonts_from_file (fonts, font_dict, file)
112 write_extracted_fonts (output_file_name, font_dict)
115 if __name__ == '__main__':
116 extract_fonts ('fonts.ps', sys.argv[1:])