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
10from __future__ import print_function
11
12import os
13import shutil
14import subprocess
15import sys
16
17URL_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.
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.
24SOURCES = [
25  {
26    'name': 'notofonts',
27    'version': '20161129'
28  }, {
29    'name': 'robotofonts',
30    'version': '2.132'
31  }
32]
33
34URLS = sorted([URL_TEMPLATE % d for d in SOURCES])
35FONTS_DIR = '/usr/local/share/fonts'
36
37def main(args):
38  if not sys.platform.startswith('linux'):
39    print("Error: %s must be run on Linux." % __file__)
40    return 1
41
42  if os.getuid() != 0:
43    print("Error: %s must be run as root." % __file__)
44    return 1
45
46  if not os.path.isdir(FONTS_DIR):
47    print("Error: Destination directory does not exist: %s" % FONTS_DIR)
48    return 1
49
50  dest_dir = os.path.join(FONTS_DIR, 'chromeos')
51
52  stamp = os.path.join(dest_dir, ".stamp02")
53  if os.path.exists(stamp):
54    with open(stamp) as s:
55      if s.read() == '\n'.join(URLS):
56        print("Chrome OS fonts already up to date in %s." % dest_dir)
57        return 0
58
59  if os.path.isdir(dest_dir):
60    shutil.rmtree(dest_dir)
61  os.mkdir(dest_dir)
62  os.chmod(dest_dir, 0755)
63
64  print("Installing Chrome OS fonts to %s." % dest_dir)
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)
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:
79    s.write('\n'.join(URLS))
80
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
87  print("""\
88
89Chrome OS font rendering settings are specified using Fontconfig. If your
90system's configuration doesn't match Chrome OS's (which vary for different
91devices), fonts may be rendered with different subpixel rendering, subpixel
92positioning, or hinting settings. This may affect font metrics.
93
94Chrome OS's settings are stored in the media-libs/fontconfig package, which is
95at src/third_party/chromiumos-overlay/media-libs/fontconfig in a Chrome OS
96checkout. You can configure your system to match Chrome OS's defaults by
97creating 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
111To load additional per-font configs (and assuming you have Chrome OS checked
112out), 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>
115""")
116
117  return 0
118
119if __name__ == '__main__':
120  sys.exit(main(sys.argv[1:]))
121