summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoozbeh Pournader <roozbeh@google.com>2014-12-01 22:02:51 -0800
committerJames Godfrey-Kittle <jamesgk@google.com>2015-04-16 12:16:23 -0700
commit5ed29bb54cd0153d2a927c4fb12ac5d9095acd38 (patch)
tree85ab557968f7da630e36ce7e6be88ec63f473dac
parent6ca0d935997f3a1f1bd53169a6b2f10ab4ae75d3 (diff)
Expand scripts for webfont generation.
-rw-r--r--Makefile7
-rw-r--r--res/buildnumber.txt2
-rw-r--r--res/charsets/web.txt (renamed from res/web_charset.txt)0
-rwxr-xr-xscripts/run_web_tests.py96
-rw-r--r--scripts/subset_for_web.py49
-rwxr-xr-xscripts/touchup_for_web.py163
6 files changed, 176 insertions, 141 deletions
diff --git a/Makefile b/Makefile
index f6da6a0..05196fa 100644
--- a/Makefile
+++ b/Makefile
@@ -58,12 +58,11 @@ web:
mkdir -p out/web
for source in hinted/*.ttf; do \
touched=$$(mktemp); \
- subsetted=$$(mktemp); \
final=out/web/$$(basename $$source); \
+ final="$${final/Roboto/RobotoDraft}"; \
python scripts/touchup_for_web.py $$source $$touched && \
- python scripts/subset_for_web.py $$touched $$subsetted && \
- python scripts/force_yminmax.py $$subsetted $$final && \
- rm $$touched $$subsetted; \
+ python scripts/subset_for_web.py $$touched $$final && \
+ rm $$touched; \
done
test: test-android test-coverage test-general
diff --git a/res/buildnumber.txt b/res/buildnumber.txt
index 1a11189..210f3c5 100644
--- a/res/buildnumber.txt
+++ b/res/buildnumber.txt
@@ -1 +1 @@
-01047 \ No newline at end of file
+01171 \ No newline at end of file
diff --git a/res/web_charset.txt b/res/charsets/web.txt
index ee4d0c8..ee4d0c8 100644
--- a/res/web_charset.txt
+++ b/res/charsets/web.txt
diff --git a/scripts/run_web_tests.py b/scripts/run_web_tests.py
index 779abdc..fdf0fe6 100755
--- a/scripts/run_web_tests.py
+++ b/scripts/run_web_tests.py
@@ -1,12 +1,15 @@
#!/usr/bin/python
-"""Test assumptions that Android relies on."""
+"""Test assumptions that web fonts rely on."""
import glob
+import json
import unittest
from fontTools import ttLib
from nototools import coverage
from nototools import font_data
+from nototools import render
+from nototools import unicode_data
def load_fonts():
@@ -111,6 +114,45 @@ class TestDigitWidths(unittest.TestCase):
self.assertEqual(len(set(widths)), 1)
+class TestNames(unittest.TestCase):
+ """Tests various strings in the name table."""
+
+ def setUp(self):
+ self.family_name = 'RobotoDraft'
+ _, self.fonts = load_fonts()
+ self.names = []
+ for font in self.fonts:
+ self.names.append(font_data.get_name_records(font))
+
+ def test_copyright(self):
+ """Tests the copyright message."""
+ for records in self.names:
+ self.assertEqual(
+ records[0],
+ 'Copyright 2014 Google Inc. All Rights Reserved.')
+
+ def test_family_name(self):
+ """Tests the family name."""
+ for records in self.names:
+ self.assertEqual(records[1], self.family_name)
+ if 16 in records:
+ self.assertEqual(records[16], self.family_name)
+
+ def test_unique_identifier_and_full_name(self):
+ """Tests the unique identifier and full name."""
+ for records in self.names:
+ expected_name = records[1] + ' ' + records[2]
+ self.assertEqual(records[3], expected_name)
+ self.assertEqual(records[4], expected_name)
+ self.assertFalse(records.has_key(18))
+
+ def test_postscript_name(self):
+ """Tests the postscript name."""
+ for records in self.names:
+ expected_name = records[1] + '-' + records[2].replace(' ', '')
+ self.assertEqual(records[6], expected_name)
+
+
class TestHints(unittest.TestCase):
"""Tests hints."""
@@ -121,9 +163,9 @@ class TestHints(unittest.TestCase):
bad_glyphs = ['uniFB01', 'uniFB02', 'uniFB03',
'uniFB04', 'uniFFFC', 'uni048C']
self.known_missing_hints = [
- (g, 'Roboto Light Italic') for g in bad_glyphs]
+ (g, 'RobotoDraft Light Italic') for g in bad_glyphs]
- def test_digit_widths(self):
+ def test_existance_of_hints(self):
"""Tests all glyphs and makes sure non-composite ones have hints."""
missing_hints = []
for font in self.fonts:
@@ -139,5 +181,53 @@ class TestHints(unittest.TestCase):
self.assertTrue(missing_hints <= self.known_missing_hints)
+class TestSpacingMarks(unittest.TestCase):
+ """Tests that spacing marks are indeed spacing."""
+
+ def setUp(self):
+ self.font_files, _ = load_fonts()
+ charset = coverage.character_set(self.font_files[0])
+ self.marks_to_test = [char for char in charset
+ if unicode_data.category(char) in ['Lm', 'Sk']]
+ self.advance_cache = {}
+
+ def get_advances(self, text, font):
+ """Get a list of horizontal advances for text rendered in a font."""
+ try:
+ return self.advance_cache[(text, font)]
+ except KeyError:
+ hb_output = render.run_harfbuzz_on_text(text, font, '')
+ hb_output = json.loads(hb_output)
+ advances = [glyph['ax'] for glyph in hb_output]
+ self.advance_cache[(text, font)] = advances
+ return advances
+
+ def test_individual_spacing_marks(self):
+ """Tests that spacing marks are spacing by themselves."""
+ for font in self.font_files:
+ print 'Testing %s for stand-alone spacing marks...' % font
+ for mark in self.marks_to_test:
+ mark = unichr(mark)
+ advances = self.get_advances(mark, font)
+ assert len(advances) == 1
+ self.assertNotEqual(advances[0], 0)
+
+ def test_spacing_marks_in_combination(self):
+ """Tests that spacing marks do not combine with base letters."""
+ for font in self.font_files:
+ print 'Testing %s for spacing marks in combination...' % font
+ for base_letter in (u'A\u00C6BCDEFGHIJKLMNO\u00D8\u01A0PRST'
+ u'U\u01AFVWXYZ'
+ u'a\u00E6bcdefghi\u0131j\u0237klmn'
+ u'o\u00F8\u01A1prs\u017Ftu\u01B0vwxyz'
+ u'\u03D2'):
+ print 'Testing %s combinations' % base_letter
+ for mark in self.marks_to_test:
+ mark = unichr(mark)
+ advances = self.get_advances(base_letter + mark, font)
+ self.assertEqual(len(advances), 2,
+ 'The sequence <%04X, %04X> combines, '
+ 'but it should not' % (ord(base_letter), ord(mark)))
+
if __name__ == '__main__':
unittest.main()
diff --git a/scripts/subset_for_web.py b/scripts/subset_for_web.py
index e100dfc..fa80590 100644
--- a/scripts/subset_for_web.py
+++ b/scripts/subset_for_web.py
@@ -1,4 +1,5 @@
#!/usr/bin/python
+# coding=UTF-8
#
# Copyright 2014 Google Inc. All rights reserved.
#
@@ -39,14 +40,58 @@ def read_charlist(filename):
charlist.append(char)
return charlist
+LATIN = (
+ range(0x0020, 0x007F) + range(0x00A0, 0x0100) +
+ [0x0131, 0x0152, 0x0153, 0x02C6, 0x02DA, 0x02DC, 0x2013, 0x2014, 0x2018,
+ 0x2019, 0x201A, 0x201C, 0x201D, 0x201E, 0x2022, 0x2039, 0x203A, 0x2044,
+ 0x2074, 0x20AC, 0x2212, 0x2215])
+
+CYRILLIC = range(0x0400, 0x0460) + [0x0490, 0x0491, 0x04B0, 0x04B1, 0x2116]
+
+SUBSETS = {
+ 'cyrillic': LATIN + CYRILLIC,
+ 'cyrillic-ext': (
+ LATIN + CYRILLIC + range(0x0460, 0x0530) + [0x20B4] +
+ range(0x2DE0, 0x2E00) + range(0xA640, 0xA6A0)),
+ 'greek': LATIN + range(0x0384, 0x0400),
+ 'greek-ext': LATIN + range(0x0384, 0x0400) + range(0x1F00, 0x2000),
+ 'latin': LATIN,
+ 'latin-ext': (
+ LATIN + range(0x0100, 0x0250) +
+ [0x02BC, 0x0300, 0x0301, 0x0303, 0x030F] +
+ range(0x1E00, 0x1F00) +
+ [0x2026, 0x2070, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079, 0x207F] +
+ range(0x20A0, 0x20D0) + range(0x2C60, 0x2C80) +
+ range(0xA720, 0xA800)),
+ 'menu': [ord(c) for c in u' ()DNQRabcfgortu΄ΕάαεηικλνКаилрцốữ'],
+}
+SUBSETS = {k: frozenset(v) for k, v in SUBSETS.iteritems()}
+
def main(argv):
"""Subset the first argument to second, dropping unused parts of the font.
"""
- charlist = read_charlist('res/web_charset.txt')
+ charlist = read_charlist('res/charsets/web.txt')
# Add private use characters for legacy reasons
charlist += [0xEE01, 0xEE02, 0xF6C3]
- subset.subset_font(argv[1], argv[2], include=charlist)
+
+ features_to_keep = [
+ 'c2sc', 'ccmp', 'cpsp', 'dlig', 'dnom', 'frac', 'kern', 'liga', 'lnum',
+ 'locl', 'numr', 'onum', 'pnum', 'smcp', 'ss01', 'ss02', 'ss03', 'ss04',
+ 'ss05', 'ss06', 'ss07', 'tnum']
+
+ source_filename = argv[1]
+ target_filename = argv[2]
+ subset.subset_font(
+ source_filename, target_filename,
+ include=charlist,
+ options={'layout_features': features_to_keep})
+
+ for suffix in SUBSETS.keys():
+ subset_filename = target_filename.replace('ttf', suffix)
+ subset.subset_font(
+ target_filename, subset_filename,
+ include=SUBSETS[suffix])
if __name__ == '__main__':
diff --git a/scripts/touchup_for_web.py b/scripts/touchup_for_web.py
index 0f30728..b055d89 100755
--- a/scripts/touchup_for_web.py
+++ b/scripts/touchup_for_web.py
@@ -1,136 +1,17 @@
#!/usr/bin/python
"""Post-build web fonts changes for Roboto."""
-import collections
import os
from os import path
import sys
from fontTools import ttLib
from nototools import font_data
-from nototools import unicode_data
-
-
-def drop_lookup(table, lookup_number):
- """Drop a lookup from an OpenType table by number.
-
- Actually remove pointers from features to the lookup, which should be less
- intrusive.
- """
- for feature in table.table.FeatureList.FeatureRecord:
- if lookup_number in feature.Feature.LookupListIndex:
- feature.Feature.LookupListIndex.remove(lookup_number)
- feature.Feature.LookupCount -= 1
-
-
-def get_font_name(font):
- """Gets the name of the font from the name table."""
- return font_data.get_name_records(font)[4]
-
-
-DIGITS = ['zero', 'one', 'two', 'three', 'four',
- 'five', 'six', 'seven', 'eight', 'nine']
-
-def fix_digit_widths(font):
- """Change all digit widths in the font to be the same."""
- hmtx_table = font['hmtx']
- widths = [hmtx_table[digit][0] for digit in DIGITS]
- if len(set(widths)) > 1:
- width_counter = collections.Counter(widths)
- most_common_width = width_counter.most_common(1)[0][0]
- print 'Digit widths were %s.' % repr(widths)
- print 'Setting all glyph widths to %d.' % most_common_width
- for digit in DIGITS:
- assert abs(hmtx_table[digit][0] - most_common_width) <= 1
- hmtx_table[digit][0] = most_common_width
-
-
-_MAP_SPACING_TO_COMBINING = {
- 'acute': 'acutecomb',
- 'breve': 'brevenosp',
- 'caron': 'uni030C',
- 'cedilla': 'cedillanosp',
- 'circumflex': 'circumflexnosp',
- 'dieresis': 'dieresisnosp',
- 'dotaccent': 'dotnosp',
- 'grave': 'gravecomb',
- 'hungarumlaut': 'acutedblnosp',
- 'macron': 'macroncomb',
- 'ogonek': 'ogoneknosp',
- 'tilde': 'tildecomb',
- 'ring': 'ringnosp',
- 'tonos': 'acutecomb',
- 'uni02F3': 'ringsubnosp',
-}
-
-def fix_ccmp_lookup(font):
- """Fixes the broken ccmp lookup."""
- cmap = font_data.get_cmap(font)
- reverse_cmap = {name: code for (code, name) in cmap.items()}
-
- # Where we know the bad 'ccmp' is
- ccmp_lookup = font['GSUB'].table.LookupList.Lookup[2]
- assert ccmp_lookup.LookupType == 4
- assert ccmp_lookup.SubTableCount == 1
- ligatures = ccmp_lookup.SubTable[0].ligatures
- for first_char, ligtable in ligatures.iteritems():
- ligatures_to_delete = []
- for index, ligature in enumerate(ligtable):
- assert len(ligature.Component) == 1
- component = ligature.Component[0]
- if (component.endswith('comb')
- or component in ['commaaccent',
- 'commaaccentrotate',
- 'ringacute']):
- continue
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=54
- if first_char == 'a' and component == 'uni02BE':
- ligatures_to_delete.append(index)
- continue
- char = reverse_cmap[component]
- general_category = unicode_data.category(char)
- if general_category != 'Mn': # not a combining mark
- ligature.Component[0] = _MAP_SPACING_TO_COMBINING[component]
- ligatures[first_char] = [
- ligature for (index, ligature) in enumerate(ligtable)
- if index not in ligatures_to_delete]
def apply_temporary_fixes(font):
"""Apply some temporary fixes.
"""
- # Make sure macStyle is correct
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=8
- font_name = get_font_name(font)
- bold = ('Bold' in font_name) or ('Black' in font_name)
- italic = 'Italic' in font_name
- font['head'].macStyle = (italic << 1) | bold
-
- # Mark the font free for installation, embedding, etc.
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=29
- os2 = font['OS/2']
- os2.fsType = 0
-
- # Set the font vendor to Google
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=46
- os2.achVendID = 'GOOG'
-
- # Drop the lookup forming the ff ligature
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=47
- drop_lookup(font['GSUB'], 5)
-
- # Correct the ccmp lookup to use combining marks instead of spacing ones
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=48
- fix_ccmp_lookup(font)
-
- # Fix the digit widths
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=49
- fix_digit_widths(font)
-
- # Add cmap for U+2117 SOUND RECORDING COPYRIGHT
- # https://code.google.com/a/google.com/p/roboto/issues/detail?id=44
- font_data.add_to_cmap(font, {0x2117: 'published'})
-
# Fix version number from buildnumber.txt
# https://code.google.com/a/google.com/p/roboto/issues/detail?id=50
from datetime import date
@@ -139,17 +20,8 @@ def apply_temporary_fixes(font):
path.dirname(__file__), os.pardir, 'res', 'buildnumber.txt')
build_number = open(build_number_txt).read().strip()
- version_record = 'Version 2.0%s; %d' % (build_number, date.today().year)
-
- for record in font['name'].names:
- if record.nameID == 5:
- if record.platformID == 1 and record.platEncID == 0: # MacRoman
- record.string = version_record
- elif record.platformID == 3 and record.platEncID == 1:
- # Windows UCS-2
- record.string = version_record.encode('UTF-16BE')
- else:
- assert False
+ version_record = 'Version 2.%s; %d' % (build_number, date.today().year)
+ font_data.set_name_record(font, 5, version_record)
def apply_web_specific_fixes(font):
@@ -167,11 +39,40 @@ def apply_web_specific_fixes(font):
os2.usWinAscent = 1946
os2.usWinDescent = 512
+ family_name = 'RobotoDraft'
+ subfamily_name = font_data.get_name_records(font)[2].encode('ASCII')
+ assert(subfamily_name in
+ ['Thin', 'Thin Italic',
+ 'Light', 'Light Italic',
+ 'Regular', 'Italic',
+ 'Medium', 'Medium Italic',
+ 'Bold', 'Bold Italic',
+ 'Black', 'Black Italic'])
+ full_name = family_name + ' ' + subfamily_name
+ year = '2014'
+
+ # Copyright message
+ font_data.set_name_record(
+ font, 0, 'Copyright %s Google Inc. All Rights Reserved.' % year)
+
+ # Family name
+ font_data.set_name_record(font, 1, family_name)
+ font_data.set_name_record(font, 16, family_name)
+
+ # Unique identifier and full name
+ font_data.set_name_record(font, 3, full_name)
+ font_data.set_name_record(font, 4, full_name)
+ font_data.set_name_record(font, 18, None)
+
+ # PostScript name
+ font_data.set_name_record(
+ font, 6, family_name+'-'+subfamily_name.replace(' ', ''))
+
def correct_font(source_font_name, target_font_name):
"""Corrects metrics and other meta information."""
font = ttLib.TTFont(source_font_name)
-# apply_temporary_fixes(font)
+ apply_temporary_fixes(font)
apply_web_specific_fixes(font)
font.save(target_font_name)