diff options
Diffstat (limited to 'bin/ucstoany.py')
-rw-r--r-- | bin/ucstoany.py | 96 |
1 files changed, 40 insertions, 56 deletions
diff --git a/bin/ucstoany.py b/bin/ucstoany.py index fd483e971bab..6715c2456f0c 100644 --- a/bin/ucstoany.py +++ b/bin/ucstoany.py @@ -1,15 +1,19 @@ # -# Copyright (c) 2019 Dimitar Toshkov Zhekov <dimitar.zhekov@gmail.com> +# Copyright (C) 2017-2020 Dimitar Toshkov Zhekov <dimitar.zhekov@gmail.com> # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) +# any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # import re @@ -20,15 +24,16 @@ import fncli import fnio import bdf - +# -- Params -- class Params(fncli.Params): def __init__(self): fncli.Params.__init__(self) - self.filter = False - self.family = None - self.output = None + self.filter_ffff = False + self.family_name = None + self.output_name = None +# -- Options -- HELP = ('' + 'usage: ucstoany [-f] [-F FAMILY] [-o OUTPUT] INPUT REGISTRY ENCODING TABLE...\n' + 'Generate a BDF font subset.\n' + @@ -43,11 +48,10 @@ HELP = ('' + ' --version display the program version and license, and exit\n' + ' --excstk display the exception stack on error\n' + '\n' + - 'The input must be a BDF 2.1 font encoded in the unicode range.\n' + - 'Any COMMENT-s are discarded, duplicate properties are collapsed.\n' + + 'The input must be a BDF 2.1 font with unicode encoding.\n' + 'Unlike ucs2any, all TABLE-s form a single subset of the input font.\n') -VERSION = 'ucstoany 1.55, Copyright (C) 2019 Dimitar Toshkov Zhekov\n\n' + fnutil.GPL2PLUS_LICENSE +VERSION = 'ucstoany 1.62, Copyright (C) 2017-2020 Dimitar Toshkov Zhekov\n\n' + fnutil.GPL2PLUS_LICENSE class Options(fncli.Options): def __init__(self): @@ -56,25 +60,24 @@ class Options(fncli.Options): def parse(self, name, value, params): if name in ['-f', '--filter']: - params.filter = True + params.filter_ffff = True elif name == '-F': - params.family = bytes(value, 'ascii') + params.family_name = bytes(value, 'ascii') if '-' in value: - raise Exception('family name may not contain "-"') + raise Exception('FAMILY may not contain "-"') elif name == '-o': - params.output = value + params.output_name = value else: self.fallback(name, params) +# -- Main -- def main_program(nonopt, parsed): - bstr = lambda number: bytes(str(number), 'ascii') - # NON-OPTIONS if len(nonopt) < 4: raise Exception('invalid number of arguments, try --help') - input = nonopt[0] + input_name = nonopt[0] registry = nonopt[1] encoding = nonopt[2] new_codes = [] @@ -83,26 +86,14 @@ def main_program(nonopt, parsed): raise Exception('invalid registry or encoding') # READ INPUT - ifs = fnio.InputStream(input) - - try: - old_font = bdf.Font.read(ifs) - ifs.close() - except Exception as ex: - raise Exception(ifs.location() + str(ex)) + old_font = fnio.read_file(input_name, bdf.Font.read) # READ TABLES def load_code(line): new_codes.append(fnutil.parse_hex('unicode', line)) - for name in nonopt[3:]: - ifs = fnio.InputStream(name) - - try: - ifs.read_lines(load_code) - ifs.close() - except Exception as ex: - raise Exception(ifs.location() + str(ex)) + for table_name in nonopt[3:]: + fnio.read_file(table_name, lambda ifs: ifs.read_lines(load_code)) if not new_codes: raise Exception('no characters in the output font') @@ -112,13 +103,13 @@ def main_program(nonopt, parsed): charmap = {char.code:char for char in old_font.chars} index = 0 unstart = 0 - family = parsed.family if parsed.family is not None else old_font.xlfd[bdf.XLFD.FAMILY_NAME] + family = parsed.family_name if parsed.family_name is not None else old_font.xlfd[bdf.XLFD.FAMILY_NAME] - if parsed.filter: + if parsed.filter_ffff: unstart = 32 if registry == 'ISO10646' else bdf.CHARS_MAX for code in new_codes: - if code == 0xFFFF and parsed.filter: + if code == 0xFFFF and parsed.filter_ffff: index += 1 continue @@ -141,8 +132,8 @@ def main_program(nonopt, parsed): new_char = copy.copy(old_char) new_char.code = code if index >= unstart else index index += 1 - new_char.props = new_char.props.clone() - new_char.props.set('ENCODING', bstr(new_char.code)) + new_char.props = copy.copy(old_char.props) + new_char.props.set('ENCODING', new_char.code) new_font.chars.append(new_char) if uni_ffff: @@ -171,33 +162,26 @@ def main_program(nonopt, parsed): value = fnutil.quote(encoding) elif name == 'DEFAULT_CHAR': if new_font.default_code != -1: - value = bstr(new_font.default_code) + value = new_font.default_code else: num_props -= 1 continue elif name == 'ENDPROPERTIES': if new_font.default_code != -1 and new_font.props.get('DEFAULT_CHAR') is None: - new_font.props.add('DEFAULT_CHAR', bstr(new_font.default_code)) + new_font.props.set('DEFAULT_CHAR', new_font.default_code) num_props += 1 - new_font.props.set('STARTPROPERTIES', bstr(num_props)) + new_font.props.set('STARTPROPERTIES', num_props) elif name == 'CHARS': - value = bstr(len(new_font.chars)) + value = len(new_font.chars) - new_font.props.add(name, value) + new_font.props.set(name, value) # COPY FIELDS new_font.bbx = old_font.bbx - new_font.finis = old_font.finis # WRITE OUTPUT - ofs = fnio.OutputStream(parsed.output) - - try: - new_font.write(ofs) - ofs.close() - except Exception as ex: - raise Exception(ofs.location() + str(ex) + ofs.destroy()) + fnio.write_file(parsed.output_name, lambda ofs: new_font.write(ofs)) if __name__ == '__main__': |