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