]> git.donarmstrong.com Git - lilypond.git/blob - python/fontextract.py
* python/fontextract.py (write_extracted_fonts): new file. Extract
[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         output = open (output_file_name, 'w')
85         output.write ('''%!PS-Adobe-3.0
86 %%Creator: lilypond-extract-fonts
87 ''')
88
89         for x in font_dict.keys ():
90                 output.write ('%%%%DocumentSuppliedResources: font %s\n' % x)
91
92         output.write ('''%%EndComments\n''')
93
94         for (k,v) in font_dict.items ():
95                 output.write ('\n%%%%BeginFont: %s\n' % k)
96                 output.write (v)
97                 output.write ('\n%%%%EndFont')
98
99
100 def extract_fonts (output_file_name, input_files):
101         d = scan_files (input_files)
102         ff = get_file_fonts_dict (d)
103         
104         font_dict = {}
105         for (file, fonts) in ff.items ():
106                 extract_fonts_from_file (fonts, font_dict, file)
107
108         write_extracted_fonts (output_file_name, font_dict)
109
110
111 if __name__ == '__main__':
112         extract_fonts ('fonts.ps', sys.argv[1:])