Debian Codegraph @

texmf-dist/doc/fonts/gnu-freefont/tools/generate/WOFF

raw
1 # vim: set filetype=python :
2 __license__ = """
3 This file is part of Gnu FreeFont.
4
5 Gnu FreeFont is free software: you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation, either version 3 of the License, or (at your option) any later
8 version.
9
10 Gnu FreeFont is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 Gnu FreeFont. If not, see <http://www.gnu.org/licenses/>.
16 """
17 __author__ = "Stevan White"
18 __email__ = "[email protected]"
19 __copyright__ = "Copyright 2011, 2012, Stevan White"
20 __date__ = "$Date: 2011-11-03 01:50:14 +0100 (Thu, 03 Nov 2011) $"
21 __version__ = "$Revision: 1863 $"
22 __doc__ = """
23 Convert fonts from FontForge's native SFD format to Web Open Font Format (WOFF)
24 First auto-hints whole font
25
26 Mainly, this copies information out of the font's existing TT names strings,
27 and uses them to generate XML information for the WOFF metadata.
28 Then it just saves the file as *.woff.
29
30 The settings should otherwise be like those for generic OpenType fonts.
31
32 """
33
34 import fontforge
35 import sys
36 import xml.dom.minidom
37 from buildutils import *
38
39 scriptname = sys.argv[0];
40 argc = len( sys.argv )
41
42 class SFNT_name:
43 def __init__( self, lang, string ):
44 self.lang = lang
45 self.string = string
46
47 def fontStrings2dict( font, lang_filter=None ):
48 sfnt_strings = {}
49 for n in font.sfnt_names:
50 if lang_filter:
51 if n[0].startswith( lang_filter ):
52 sfnt_strings[n[1]] = SFNT_name(n[0],n[2])
53 else:
54 sfnt_strings[n[1]] = SFNT_name(n[0],n[2])
55 return sfnt_strings
56
57 def stripmeta( meta ):
58 lst = meta.split( '\n' )
59 nlst = []
60 for line in lst[1:-2]:
61 if line.startswith( '\t' ):
62 line = line[1:]
63 nlst.append( line )
64 return '\n'.join( nlst )
65
66 def add_text_decorated_with_divs( doc, parent, text ):
67 for line in text.split( '\n' ):
68 if line:
69 div = doc.createElement( 'div' )
70 div.appendChild( doc.createTextNode( line ) )
71 parent.appendChild( div )
72
73 def fontInfo2WOFFxml( font ):
74 sfnt_strings = fontStrings2dict( f, 'English' )
75 copyright = sfnt_strings['Copyright'].string
76 license = sfnt_strings['License'].string
77 licenseURL = sfnt_strings['License URL'].string
78 vendorURL = sfnt_strings['Vendor URL'].string
79 version = sfnt_strings['Version'].string
80 fontFamily = sfnt_strings['Family'].string
81 fontStyle = sfnt_strings['SubFamily'].string
82
83 doc = xml.dom.minidom.Document()
84 meta = doc.createElement( 'metadata' )
85 doc.appendChild( meta )
86 # <uniqueid id="org.gnu.freefont.freeserif.1877" />
87 elt = doc.createElement( 'uniqueid' )
88
89 version_prefix = 'Revision: '
90 if version.startswith( version_prefix ):
91 revision = version[len(version_prefix):]
92 else:
93 revision = version
94
95 elt.setAttribute( 'id', 'org.gnu.freefont.' + fontFamily + '.'
96 + fontStyle + '.' + revision )
97 meta.appendChild( elt )
98
99 # <vendor name="Font Vendor" url="https://savannah.gnu.org/projects/freefont/" />
100 elt = doc.createElement( 'vendor' )
101 elt.setAttribute( 'name', 'Font Vendor' )
102 elt.setAttribute( 'url', vendorURL )
103 meta.appendChild( elt )
104 # <credits>
105 # <credit name="Font Designer"
106 # url="https://savannah.gnu.org/projects/freefont/" />
107 # </credits>
108 elt_credits = doc.createElement( 'credits' )
109 elt = doc.createElement( 'credit' )
110 elt.setAttribute( 'name', 'Font Designer' )
111 elt.setAttribute( 'url', vendorURL )
112 elt_credits.appendChild( elt )
113 meta.appendChild( elt_credits )
114 #<description>
115 # <text xml:lang="en">
116 # FreeSerif-Medium is a member of the GNU FreeFont font family.
117 # </text>
118 #</description>
119 elt_description = doc.createElement( 'description' )
120 elt = doc.createElement( 'text' )
121 elt.setAttribute( 'xml:lang', 'en' )
122 text = doc.createTextNode( fontFamily + '-' + fontStyle
123 + " is a member of the GNU FreeFont font family.")
124 elt.appendChild( text )
125 elt_description.appendChild( elt )
126 meta.appendChild( elt_description )
127 #<license url="http://www.gnu.org/copyleft/gpl.html"
128 # id="fontvendor-Web-corporate-v2">
129 # <text xml:lang="en">
130 # ...
131 elt_license = doc.createElement( 'license' )
132 elt_license.setAttribute( 'url', licenseURL )
133 elt = doc.createElement( 'text' )
134 elt.setAttribute( 'xml:lang', 'en' )
135 add_text_decorated_with_divs( doc, elt, license )
136 elt_license.appendChild( elt )
137 meta.appendChild( elt_license )
138 #<copyright>
139 #<text xml:lang="en">Copyright &#x0089;2002, 2003, 2005, 2008, 2009, 2010, 2011 GNU Freefont contributors.</text>
140 #</copyright>
141 elt_copyright = doc.createElement( 'copyright' )
142 elt = doc.createElement( 'text' )
143 elt.setAttribute( 'xml:lang', 'en' )
144 elt.appendChild( doc.createTextNode( copyright ) )
145 elt_copyright.appendChild( elt )
146 meta.appendChild( elt_copyright )
147 strrep = meta.toprettyxml()
148
149 return stripmeta( strrep )
150
151 if argc > 1:
152 for i in range( 1, argc ):
153 f = fontforge.open( sys.argv[i] )
154 woff_file = f.fontname + ".woff"
155 vstr = trim_version_str( f )
156 print "Generating WOFF file ", woff_file, ' ', vstr
157 # Wanted to set to 'UniocdeBmp' if there were no high unicodes
158 # but all attemtps to determine that from Python failed.
159 f.encoding = 'UnicodeFull'
160 f.layers['Fore'].is_quadratic = True
161 f.selection.all()
162 f.autoHint()
163 f.autoInstr()
164 # Copy copyright info etc from the tt tables into the
165 # WOFF XML section.
166 xml = fontInfo2WOFFxml( f )
167 f.woffMajor = 1
168 f.woffMajor = 0
169 f.woffMetadata = xml
170 #f.generate( woff_file, flags=('opentype','round') )
171 f.generate( woff_file, flags=('opentype','no-hints') )
172 f.close()
173 else:
174 print "Usage: " + scriptname + " font.sfd [font.sfd ...]"