[smc-discuss] [Git][smc/keraleeyam][master] Update build scripts

Santhosh Thottingal gitlab at mg.gitlab.com
Thu Dec 15 07:00:16 PST 2016


Santhosh Thottingal pushed to branch master at SMC / Keraleeyam


Commits:
f8ee4da7 by Santhosh Thottingal at 2016-12-15T20:29:05+05:30
Update build scripts

- - - - -


4 changed files:

- Makefile
- README.md
- tools/build.py
- + tools/requirements.txt


Changes:

=====================================
Makefile
=====================================
--- a/Makefile
+++ b/Makefile
@@ -2,34 +2,45 @@
 
 fontpath=/usr/share/fonts/truetype/malayalam
 fonts=Keraleeyam
-feature=features/features.fea
+features=features
 PY=python2.7
+version=0.1
 buildscript=tools/build.py
-default: compile
-all: compile webfonts
+default: otf
+all: compile webfonts test
+compile: ttf otf
+otf:
+	@for font in `echo ${fonts}`;do \
+		$(PY) $(buildscript) -t otf -i $$font.sfd -f $(features)/features.fea -v $(version);\
+	done;
 
-compile:
+ttf:
 	@for font in `echo ${fonts}`;do \
-		echo "Generating $$font.ttf";\
-		$(PY) $(buildscript) $$font.sfd $(feature);\
+		$(PY) $(buildscript) -t ttf -i $$font.sfd -f $(features)/features.fea -v $(version);\
 	done;
 
-webfonts:compile
-	@echo "Generating webfonts";
+webfonts:woff woff2
+woff: ttf
+	@rm -rf *.woff
+	@for font in `echo ${fonts}`;do \
+		$(PY) $(buildscript) -t woff -i $$font.ttf;\
+	done;
+woff2: ttf
+	@rm -rf *.woff2
 	@for font in `echo ${fonts}`;do \
-		sfntly -w $${font}.ttf $${font}.woff;\
-		sfntly -e -x $${font}.ttf $${font}.eot;\
-		[ -x `which woff2_compress` ] && woff2_compress $${font}.ttf;\
+		$(PY) $(buildscript) -t woff2 -i $$font.ttf;\
 	done;
 
 install: compile
 	@for font in `echo ${fonts}`;do \
-		install -D -m 0644 $${font}.ttf ${DESTDIR}/${fontpath}/$${font}.ttf;\
+		install -D -m 0644 $${font}.otf ${DESTDIR}/${fontpath}/$${font}.otf;\
 	done;
 
-test: compile
+test: otf
 # Test the fonts
 	@for font in `echo ${fonts}`; do \
 		echo "Testing font $${font}";\
-		hb-view $${font}.ttf --text-file tests/tests.txt --output-file tests/$${font}.pdf;\
+		hb-view $${font}.otf --font-size 14 --margin 100 --line-space 1.5 --foreground=333333  --text-file tests/tests.txt --output-file tests/$${font}.pdf;\
 	done;
+clean:
+	@rm -rf *.otf *.ttf *.woff *.woff2 *.sfd-*


=====================================
README.md
=====================================
--- a/README.md
+++ b/README.md
@@ -12,4 +12,17 @@ on comprehensive character set of traditional Malayalam script with many unique 
 Latin glyphs were designed and added to match the malayalam sript style in 2015.
 
 The font is maintained by Swathanthra Malayalam Computing project. 
-Source code is available at https://gitlab.com/smc/keraleeyam
\ No newline at end of file
+Source code is available at https://gitlab.com/smc/keraleeyam
+
+
+Building from source
+--------------------
+1. Install fontforge and python-fontforge
+2. Install the python libraries required for build script:
+    ```
+    pip install -r tools/requirements.txt
+    ```
+3. Build the ttf, woff, woff2 files: 
+   ``` 
+   make
+   ```


=====================================
tools/build.py
=====================================
--- a/tools/build.py
+++ b/tools/build.py
@@ -1,19 +1,159 @@
+#!/usr/bin/python
+# coding=utf-8
+#
+# Font build utility
+#
+
 import sys
 import time
+import os
 import fontforge
-font = fontforge.open(sys.argv[1])
-# Remove all GSUB lookups
-for lookup in font.gsub_lookups:
-	font.removeLookup(lookup)
-
-# Remove all GPOS lookups 
-for lookup in font.gpos_lookups:
-	font.removeLookup(lookup)        
-
-# Merge the new featurefile 
-font.mergeFeature(sys.argv[2])
-font.version = time.strftime('%Y%m%d')
-font.simplify()
-font.autoInstr()
-font.generate(sys.argv[1].replace(".sfd",".ttf"), flags=("omit-instructions", "round", "opentype"))
-font.close() 
+import psMat
+from tempfile import mkstemp
+from fontTools.ttLib import TTFont
+from fontTools.ttx import makeOutputFileName
+import argparse
+
+
+def flattenNestedReferences(font, ref, new_transform=(1, 0, 0, 1, 0, 0)):
+    """Flattens nested references by replacing them with the ultimate reference
+    and applying any transformation matrices involved, so that the final font
+    has only simple composite glyphs. This to work around what seems to be an
+    Apple bug that results in ignoring transformation matrix of nested
+    references."""
+
+    name = ref[0]
+    transform = ref[1]
+    glyph = font[name]
+    new_ref = []
+    if glyph.references and glyph.foreground.isEmpty():
+        for nested_ref in glyph.references:
+            for i in flattenNestedReferences(font, nested_ref, transform):
+                matrix = psMat.compose(i[1], new_transform)
+                new_ref.append((i[0], matrix))
+    else:
+        matrix = psMat.compose(transform, new_transform)
+        new_ref.append((name, matrix))
+
+    return new_ref
+
+
+def validateGlyphs(font):
+    """Fixes some common FontForge validation warnings, currently handles:
+        * wrong direction
+        * flipped references
+    In addition to flattening nested references."""
+
+    wrong_dir = 0x8
+    flipped_ref = 0x10
+    for glyph in font.glyphs():
+        state = glyph.validate(True)
+        refs = []
+
+        if state & flipped_ref:
+            glyph.unlinkRef()
+            glyph.correctDirection()
+        if state & wrong_dir:
+            glyph.correctDirection()
+
+        for ref in glyph.references:
+            for i in flattenNestedReferences(font, ref):
+                refs.append(i)
+        if refs:
+            glyph.references = refs
+
+
+def opentype(infont, type, feature, version):
+    font = fontforge.open(infont)
+    if args.type == 'otf':
+        outfont = infont.replace(".sfd", ".otf")
+        flags = ("opentype", "dummy-dsig", "round", "omit-instructions")
+    else:
+        outfont = infont.replace(".sfd", ".ttf")
+        flags = ("opentype", "dummy-dsig", "round", "omit-instructions")
+    print("Generating %s => %s" % (infont, outfont))
+    tmpfont = mkstemp(suffix=os.path.basename(outfont))[1]
+
+    # Remove all GSUB lookups
+    for lookup in font.gsub_lookups:
+        font.removeLookup(lookup)
+
+    # Remove all GPOS lookups
+    for lookup in font.gpos_lookups:
+        font.removeLookup(lookup)
+
+    # Merge the new featurefile
+    font.mergeFeature(feature)
+    font.version = version
+    font.appendSFNTName('English (US)', 'Version',
+                        version + '.0+' + time.strftime('%Y%m%d'))
+    font.selection.all()
+    font.correctReferences()
+    # font.simplify()
+    font.selection.none()
+    # fix some common font issues
+    validateGlyphs(font)
+    font.generate(tmpfont, flags=flags)
+    font.close()
+    # now open in fontTools
+    font = TTFont(tmpfont, recalcBBoxes=0)
+
+    # our 'name' table is a bit bulky, and of almost no use in for web fonts,
+    # so we strip all unnecessary entries.
+    name = font['name']
+    names = []
+    for record in name.names:
+        platID = record.platformID
+        langID = record.langID
+        nameID = record.nameID
+
+        # we keep only en_US entries in Windows and Mac platform id, every
+        # thing else is dropped
+        if (platID == 1 and langID == 0) or (platID == 3 and langID == 1033):
+            if nameID == 13:
+                # the full OFL text is too much, replace it with a simple
+                # string
+                if platID == 3:
+                    # MS strings are UTF-16 encoded
+                    text = 'OFL v1.1'.encode('utf_16_be')
+                else:
+                    text = 'OFL v1.1'
+                record.string = text
+                names.append(record)
+                # keep every thing else except Descriptor, Sample Text
+            elif nameID not in (10, 19):
+                names.append(record)
+
+    name.names = names
+
+    # FFTM is FontForge specific, remove it
+    del(font['FFTM'])
+    # force compiling GPOS/GSUB tables by fontTools, saves few tens of KBs
+    for tag in ('GPOS', 'GSUB'):
+        font[tag].compile(font)
+
+    font.save(outfont)
+    os.remove(tmpfont)
+
+def webfonts(infont, type):
+    font = TTFont(infont, recalcBBoxes=0)
+    # Generate WOFF2
+    woffFileName = makeOutputFileName(infont, outputDir=None, extension='.' + type)
+    print("Processing %s => %s" % (infont, woffFileName))
+    font.flavor = type
+    font.save(woffFileName, reorderTables=False)
+
+    font.close()
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description='Build fonts')
+    parser.add_argument('-i', '--input', help='Input font', required=True)
+    parser.add_argument('-v', '--version', help='Version')
+    parser.add_argument('-f', '--feature', help='Feature file')
+    parser.add_argument('-t', '--type', help='Output type', default='otf')
+    args = parser.parse_args()
+    if args.type == 'otf' or args.type == 'ttf':
+        opentype(args.input, args.type, args.feature, args.version)
+    if args.type == 'woff' or args.type == 'woff2':
+        webfonts(args.input, args.type)


=====================================
tools/requirements.txt
=====================================
--- /dev/null
+++ b/tools/requirements.txt
@@ -0,0 +1,2 @@
+git+https://github.com/google/brotli@v0.3.0#egg=Brotli
+fonttools >= 3.0
\ No newline at end of file



View it on GitLab: https://gitlab.com/smc/keraleeyam/commit/f8ee4da70331e9d70f063a532eaa2b248085e4a2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.smc.org.in/pipermail/discuss-smc.org.in/attachments/20161215/73e75b8e/attachment.html>


More information about the discuss mailing list