]> git.donarmstrong.com Git - lilypond.git/blob - python/fontextract.py
(conv): indent 4 for python files.
[lilypond.git] / python / fontextract.py
1 import re
2
3 import getopt
4 import sys
5 import os
6 import string
7
8 dsr_font_regex = re.compile ('%%DocumentSuppliedResources: font (.*)')
9 begin_font_regex = re.compile ('%%BeginFont: (.*)')
10 end_font_regex = re.compile ('%%EndFont')
11 verbose = 0
12
13 try:
14     import gettext
15     gettext.bindtextdomain ('lilypond', localedir)
16     gettext.textdomain ('lilypond')
17     _ = gettext.gettext
18 except:
19     def _ (s):
20         return s
21
22 def scan_files (files):
23     file_of_font_dict = {}
24     for f in files:
25         if verbose:
26             sys.stderr.write (_('Scanning %s') % f + '\n')
27
28         header = open (f, 'r').read ()
29         idx = 0
30
31         extract_from_this = []
32         while idx < len (header):
33             match = dsr_font_regex.search (header[idx:])
34             if not match:
35                 break
36             name = match.group (1)
37             idx += match.end (1)
38             if file_of_font_dict.has_key (name):
39                 continue
40
41             file_of_font_dict[name] = f
42
43     return file_of_font_dict
44
45 def get_file_fonts_dict (file_of_font_dict):
46     dict = {}
47     for (n, f) in file_of_font_dict.items ():
48         if not dict.has_key (f):
49             dict[f] = []
50
51         dict[f].append (n)
52
53     return dict
54
55 def extract_fonts_from_file (extract_from_this, font_dict, filename):
56     if extract_from_this:
57         curr_font = []
58         curr_font_name = []
59         in_font = 0
60         for l in open (filename).readlines ():
61             if not in_font and begin_font_regex.match (l):
62                 in_font = 1
63                 curr_font_name = begin_font_regex.match (l).group (1)
64                 curr_font = []
65             elif in_font and end_font_regex.match (l):
66                 in_font = 0
67
68                 if curr_font_name in extract_from_this:
69                     font_dict[curr_font_name] = string.join (curr_font, '')
70                     if verbose:
71                         sys.stderr.write (_('Extracted %s')
72                                           % curr_font_name + '\n')
73
74                     extract_from_this.remove (curr_font_name)
75             elif in_font:
76                 curr_font.append (l)
77             if not extract_from_this:
78                 break
79
80         if extract_from_this:
81             sys.stderr.write ("Failed to extract %s from %s\n"
82                               % (string.join (extract_from_this, ', '), filename))
83
84 def write_extracted_fonts (output_file_name, font_dict):
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:])