[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gfx / vector_icons / aggregate_vector_icons.py
blob72fbc11cc935891429ad50d5af4dcd4165131706
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import fileinput
6 import glob
7 import optparse
8 import os
9 import textwrap
12 def AggregateVectorIcons(working_directory, output_cc, output_h):
13 """Compiles all .icon files in a directory into two C++ files.
15 Args:
16 working_directory: The path to the directory that holds the .icon files
17 and C++ templates.
18 output_cc: The path that should be used to write the .cc file.
19 output_h: The path that should be used to write the .h file.
20 """
22 icon_list = glob.glob(working_directory + "*.icon")
24 input_header_template = open(os.path.join(working_directory,
25 "vector_icons.h.template"))
26 header_template_contents = input_header_template.readlines()
27 input_header_template.close()
28 output_header = open(output_h, "w")
29 for line in header_template_contents:
30 if not "TEMPLATE_PLACEHOLDER" in line:
31 output_header.write(line)
32 continue
34 for icon_path in icon_list:
35 # The icon name should be of the format "foo.icon" or "foo.1x.icon".
36 (icon_name, extension) = os.path.splitext(os.path.basename(icon_path))
37 (icon_name, scale_factor) = os.path.splitext(icon_name)
38 if not scale_factor:
39 output_header.write(" {},\n".format(icon_name.upper()))
40 output_header.close()
42 input_cc_template = open(
43 os.path.join(working_directory, "vector_icons.cc.template"))
44 cc_template_contents = input_cc_template.readlines()
45 input_cc_template.close()
46 output_cc = open(output_cc, "w")
47 for line in cc_template_contents:
48 if not "TEMPLATE_PLACEHOLDER" in line:
49 output_cc.write(line)
50 continue;
52 for icon_path in icon_list:
53 (icon_name, extension) = os.path.splitext(os.path.basename(icon_path))
54 (icon_name, scale_factor) = os.path.splitext(icon_name)
55 assert not scale_factor or scale_factor == ".1x"
56 if (("1X" in line and scale_factor != ".1x") or
57 (not "1X" in line and scale_factor == ".1x")):
58 continue
60 icon_file = open(icon_path)
61 vector_commands = "".join(icon_file.readlines())
62 icon_file.close()
63 output_cc.write("ICON_TEMPLATE({}, {})\n".format(icon_name.upper(),
64 vector_commands))
65 output_cc.close()
68 def main():
69 parser = optparse.OptionParser()
70 parser.add_option("--working_directory",
71 help="The directory to look for template C++ as well as "
72 "icon files.")
73 parser.add_option("--output_cc",
74 help="The path to output the CC file to.")
75 parser.add_option("--output_h",
76 help="The path to output the header file to.")
78 (options, args) = parser.parse_args()
80 AggregateVectorIcons(options.working_directory,
81 options.output_cc,
82 options.output_h)
85 if __name__ == "__main__":
86 main()