1#!/usr/bin/env python
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Generate header file for unicode data."""
18
19import optparse
20import sys
21
22
23UNICODE_EMOJI_TEMPLATE="""
24/* file generated by frameworks/minikin/lib/minikin/Android.mk */
25#ifndef MINIKIN_UNICODE_EMOJI_H
26#define MINIKIN_UNICODE_EMOJI_H
27
28#include <stdint.h>
29
30namespace android {
31namespace generated {
32
33int32_t EMOJI_LIST[] = {
34@@@EMOJI_DATA@@@
35};
36
37}  // namespace generated
38}  // namespace android
39
40#endif  // MINIKIN_UNICODE_EMOJI_H
41"""
42
43
44def _create_opt_parser():
45  parser = optparse.OptionParser()
46  parser.add_option('-i', '--input', type='str', action='store',
47                    help='path to input emoji-data.txt')
48  parser.add_option('-o', '--output', type='str', action='store',
49                    help='path to output UnicodeEmoji.h')
50  return parser
51
52
53def _read_emoji_data(emoji_data_file_path):
54  result = []
55  with open(emoji_data_file_path) as emoji_data_file:
56    for line in emoji_data_file:
57      if '#' in line:
58        line = line[:line.index('#')]  # Drop comments.
59      if not line.strip():
60        continue  # Skip empty line.
61
62      code_points, prop = line.split(';')
63      code_points = code_points.strip()
64      prop = prop.strip()
65      if prop != 'Emoji':
66        break  # Only collect Emoji property code points
67
68      if '..' in code_points:  # code point range
69        cp_start, cp_end = code_points.split('..')
70        result.extend(xrange(int(cp_start, 16), int(cp_end, 16) + 1))
71      else:
72        code_point = int(code_points, 16)
73        result.append(code_point)
74  return result
75
76
77def _generate_header_contents(emoji_list):
78  INDENT = ' ' * 4
79  JOINER = ', '
80
81  hex_list = ['0x%04X' % x for x in emoji_list]
82  lines = []
83  tmp_line = '%s%s' % (INDENT, hex_list[0])
84  for hex_str in hex_list[1:]:
85    if len(tmp_line) + len(JOINER) + len(hex_str) >= 100:
86      lines.append(tmp_line + ',')
87      tmp_line = '%s%s' % (INDENT, hex_str)
88    else:
89      tmp_line = '%s%s%s' % (tmp_line, JOINER, hex_str)
90  lines.append(tmp_line)
91
92  template = UNICODE_EMOJI_TEMPLATE
93  template = template.replace('@@@EMOJI_DATA@@@', '\n'.join(lines))
94  return template
95
96
97if __name__ == '__main__':
98  opt_parser = _create_opt_parser()
99  opts, _ = opt_parser.parse_args()
100
101  emoji_list = _read_emoji_data(opts.input)
102  header = _generate_header_contents(emoji_list)
103  with open(opts.output, 'w') as header_file:
104    header_file.write(header)
105
106