install-chromeos-fonts.py (4143B)
1 #!/usr/bin/env python3 2 # Copyright 2013 The Chromium Authors 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 # Script to install the Chrome OS fonts on Linux. 7 # This script can be run manually (as root), but is also run as part 8 # install-build-deps.sh. 9 10 11 import os 12 import shutil 13 import subprocess 14 import sys 15 16 URL_TEMPLATE = ('https://commondatastorage.googleapis.com/chromeos-localmirror/' 17 'distfiles/%(name)s-%(version)s.tar.bz2') 18 19 # Taken from the media-fonts/<name> ebuilds in chromiumos-overlay. 20 # noto-cjk used to be here, but is removed because fc-cache takes too long 21 # regenerating the fontconfig cache (See crbug.com/697954.) 22 # TODO(jshin): Add it back when the above issue can be avoided. 23 SOURCES = [ 24 { 25 'name': 'notofonts', 26 'version': '20161129' 27 }, { 28 'name': 'robotofonts', 29 'version': '2.132' 30 } 31 ] 32 33 URLS = sorted([URL_TEMPLATE % d for d in SOURCES]) 34 FONTS_DIR = '/usr/local/share/fonts' 35 36 def main(args): 37 if not sys.platform.startswith('linux'): 38 print("Error: %s must be run on Linux." % __file__) 39 return 1 40 41 if os.getuid() != 0: 42 print("Error: %s must be run as root." % __file__) 43 return 1 44 45 if not os.path.isdir(FONTS_DIR): 46 print("Error: Destination directory does not exist: %s" % FONTS_DIR) 47 return 1 48 49 dest_dir = os.path.join(FONTS_DIR, 'chromeos') 50 51 stamp = os.path.join(dest_dir, ".stamp02") 52 if os.path.exists(stamp): 53 with open(stamp) as s: 54 if s.read() == '\n'.join(URLS): 55 print("Chrome OS fonts already up to date in %s." % dest_dir) 56 return 0 57 58 if os.path.isdir(dest_dir): 59 shutil.rmtree(dest_dir) 60 os.mkdir(dest_dir) 61 os.chmod(dest_dir, 0o755) 62 63 print("Installing Chrome OS fonts to %s." % dest_dir) 64 for url in URLS: 65 tarball = os.path.join(dest_dir, os.path.basename(url)) 66 subprocess.check_call(['curl', '-L', url, '-o', tarball]) 67 subprocess.check_call(['tar', '--no-same-owner', '--no-same-permissions', 68 '-xf', tarball, '-C', dest_dir]) 69 os.remove(tarball) 70 71 readme = os.path.join(dest_dir, "README") 72 with open(readme, 'w') as s: 73 s.write("This directory and its contents are auto-generated.\n") 74 s.write("It may be deleted and recreated. Do not modify.\n") 75 s.write("Script: %s\n" % __file__) 76 77 with open(stamp, 'w') as s: 78 s.write('\n'.join(URLS)) 79 80 for base, dirs, files in os.walk(dest_dir): 81 for dir in dirs: 82 os.chmod(os.path.join(base, dir), 0o755) 83 for file in files: 84 os.chmod(os.path.join(base, file), 0o644) 85 86 print("""\ 87 88 Chrome OS font rendering settings are specified using Fontconfig. If your 89 system's configuration doesn't match Chrome OS's (which vary for different 90 devices), fonts may be rendered with different subpixel rendering, subpixel 91 positioning, or hinting settings. This may affect font metrics. 92 93 Chrome OS's settings are stored in the media-libs/fontconfig package, which is 94 at src/third_party/chromiumos-overlay/media-libs/fontconfig in a Chrome OS 95 checkout. You can configure your system to match Chrome OS's defaults by 96 creating or editing a ~/.fonts.conf file: 97 98 <?xml version="1.0"?> 99 <!DOCTYPE fontconfig SYSTEM "fonts.dtd"> 100 <fontconfig> 101 <match target="font"> 102 <edit name="antialias" mode="assign"><bool>true</bool></edit> 103 <edit name="autohint" mode="assign"><bool>true</bool></edit> 104 <edit name="hinting" mode="assign"><bool>true</bool></edit> 105 <edit name="hintstyle" mode="assign"><const>hintslight</const></edit> 106 <!-- 107 Disable sub-pixel anti-aliasing to work around font corruption issues 108 in the emulator. This disables it for your entire system. See 109 http://crbug.com/1442627 110 --> 111 <edit name="rgba" mode="assign"><const>none</const></edit> 112 </match> 113 </fontconfig> 114 115 To load additional per-font configs (and assuming you have Chrome OS checked 116 out), add the following immediately before the "</fontconfig>" line: 117 118 <include ignore_missing="yes">/path/to/src/third_party/chromiumos-overlay/media-libs/fontconfig/files/local.conf</include> 119 """) 120 121 return 0 122 123 if __name__ == '__main__': 124 sys.exit(main(sys.argv[1:]))