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