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