2 # -*- coding: utf-8 -*-
4 # Generate a translit_font file from a UnicodeData file.
5 # Copyright (C) 2015-2016 Free Software Foundation, Inc.
6 # This file is part of the GNU C Library.
8 # The GNU C Library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # The GNU C Library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with the GNU C Library; if not, see
20 # <http://www.gnu.org/licenses/>.
23 Generate a translit_font file from UnicodeData.txt
25 To see how this script is used, call it with the “-h” option:
27 $ ./gen_translit_font -h
28 … prints usage message …
35 def read_input_file(filename
):
36 '''Reads the original glibc translit_font file to get the
37 original head and tail.
39 We want to replace only the part of the file between
40 “translit_start” and “translit_end”
43 with
open(filename
, mode
='r') as translit_file
:
44 for line
in translit_file
:
46 if line
.startswith('translit_start'):
48 for line
in translit_file
:
49 if line
.startswith('translit_end'):
52 for line
in translit_file
:
56 def output_head(translit_file
, unicode_version
, head
=''):
57 '''Write the header of the output file, i.e. the part of the file
58 before the “translit_start” line.
60 if ARGS
.input_file
and head
:
61 translit_file
.write(head
)
63 translit_file
.write('escape_char /\n')
64 translit_file
.write('comment_char %\n')
65 translit_file
.write('\n')
66 translit_file
.write('% Transliterations of font equivalents.\n')
67 translit_file
.write('% Generated automatically from UnicodeData.txt '
68 + 'by gen_translit_font.py '
69 + 'on {:s} '.format(time
.strftime('%Y-%m-%d'))
70 + 'for Unicode {:s}.\n'.format(unicode_version
))
71 translit_file
.write('\n')
72 translit_file
.write('LC_CTYPE\n')
73 translit_file
.write('\n')
74 translit_file
.write('translit_start\n')
76 def output_tail(translit_file
, tail
=''):
77 '''Write the tail of the output file'''
78 if ARGS
.input_file
and tail
:
79 translit_file
.write(tail
)
81 translit_file
.write('translit_end\n')
82 translit_file
.write('\n')
83 translit_file
.write('END LC_CTYPE\n')
85 def output_transliteration(translit_file
):
86 '''Write the new transliteration to the output file'''
87 translit_file
.write('\n')
88 for code_point
in sorted(unicode_utils
.UNICODE_ATTRIBUTES
):
89 name
= unicode_utils
.UNICODE_ATTRIBUTES
[code_point
]['name']
90 decomposition
= unicode_utils
.UNICODE_ATTRIBUTES
[
91 code_point
]['decomposition']
92 if decomposition
.startswith('<font>'):
93 decomposition
= decomposition
[7:]
94 decomposed_code_points
= [[int(x
, 16)
95 for x
in decomposition
.split(' ')]]
96 if decomposed_code_points
[0]:
97 translit_file
.write('{:s} '.format(
98 unicode_utils
.ucs_symbol(code_point
)))
99 for index
in range(0, len(decomposed_code_points
)):
101 translit_file
.write(';')
102 if len(decomposed_code_points
[index
]) > 1:
103 translit_file
.write('"')
104 for decomposed_code_point
in decomposed_code_points
[index
]:
105 translit_file
.write('{:s}'.format(
106 unicode_utils
.ucs_symbol(decomposed_code_point
)))
107 if len(decomposed_code_points
[index
]) > 1:
108 translit_file
.write('"')
109 translit_file
.write(' % {:s}\n'.format(name
))
110 translit_file
.write('\n')
112 if __name__
== "__main__":
113 PARSER
= argparse
.ArgumentParser(
115 Generate a translit_font file from UnicodeData.txt.
118 '-u', '--unicode_data_file',
121 default
='UnicodeData.txt',
122 help=('The UnicodeData.txt file to read, '
123 + 'default: %(default)s'))
125 '-i', '--input_file',
128 help=''' The original glibc/localedata/locales/translit_font
131 '-o', '--output_file',
134 default
='translit_font.new',
135 help='''The new translit_font file, default: %(default)s. If the
136 original glibc/localedata/locales/translit_font file has
137 been given as an option, the header up to the
138 “translit_start” line and the tail from the “translit_end”
139 line to the end of the file will be copied unchanged into the
146 help='The Unicode version of the input files used.')
147 ARGS
= PARSER
.parse_args()
149 unicode_utils
.fill_attributes(ARGS
.unicode_data_file
)
152 (HEAD
, TAIL
) = read_input_file(ARGS
.input_file
)
153 with
open(ARGS
.output_file
, mode
='w') as TRANSLIT_FILE
:
154 output_head(TRANSLIT_FILE
, ARGS
.unicode_version
, head
=HEAD
)
155 output_transliteration(TRANSLIT_FILE
)
156 output_tail(TRANSLIT_FILE
, tail
=TAIL
)