From 37a3558c10b7f1e971cb273831aebbdd27fcd953 Mon Sep 17 00:00:00 2001 From: Andrey Chalkin Date: Thu, 19 Aug 2021 12:21:53 +0700 Subject: [PATCH] reformat python code --- .editorconfig | 343 +++++++++++++++++++++++++++- tasks/bin/eotlitetool.py | 57 +++-- tasks/engines/fontforge/generate.py | 139 ++++++----- 3 files changed, 437 insertions(+), 102 deletions(-) diff --git a/.editorconfig b/.editorconfig index 9c73aba..0342804 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,338 @@ [*] -charset=utf-8 -end_of_line=lf -trim_trailing_whitespace=true -insert_final_newline=true -indent_style=space -indent_size=2 +charset = utf-8 +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = space +indent_size = 2 +max_line_length = 200 +tab_width = 4 +ij_continuation_indent_size = 8 +ij_formatter_off_tag = @formatter:off +ij_formatter_on_tag = @formatter:on +ij_formatter_tags_enabled = false +ij_smart_tabs = false +ij_visual_guides = none +ij_wrap_on_typing = false + +[*.css] +ij_css_align_closing_brace_with_properties = false +ij_css_blank_lines_around_nested_selector = 1 +ij_css_blank_lines_between_blocks = 1 +ij_css_brace_placement = end_of_line +ij_css_enforce_quotes_on_format = false +ij_css_hex_color_long_format = false +ij_css_hex_color_lower_case = false +ij_css_hex_color_short_format = false +ij_css_hex_color_upper_case = false +ij_css_keep_blank_lines_in_code = 2 +ij_css_keep_indents_on_empty_lines = false +ij_css_keep_single_line_blocks = false +ij_css_properties_order = font, font-family, font-size, font-weight, font-style, font-variant, font-size-adjust, font-stretch, line-height, position, z-index, top, right, bottom, left, display, visibility, float, clear, overflow, overflow-x, overflow-y, clip, zoom, align-content, align-items, align-self, flex, flex-flow, flex-basis, flex-direction, flex-grow, flex-shrink, flex-wrap, justify-content, order, box-sizing, width, min-width, max-width, height, min-height, max-height, margin, margin-top, margin-right, margin-bottom, margin-left, padding, padding-top, padding-right, padding-bottom, padding-left, table-layout, empty-cells, caption-side, border-spacing, border-collapse, list-style, list-style-position, list-style-type, list-style-image, content, quotes, counter-reset, counter-increment, resize, cursor, user-select, nav-index, nav-up, nav-right, nav-down, nav-left, transition, transition-delay, transition-timing-function, transition-duration, transition-property, transform, transform-origin, animation, animation-name, animation-duration, animation-play-state, animation-timing-function, animation-delay, animation-iteration-count, animation-direction, text-align, text-align-last, vertical-align, white-space, text-decoration, text-emphasis, text-emphasis-color, text-emphasis-style, text-emphasis-position, text-indent, text-justify, letter-spacing, word-spacing, text-outline, text-transform, text-wrap, text-overflow, text-overflow-ellipsis, text-overflow-mode, word-wrap, word-break, tab-size, hyphens, pointer-events, opacity, color, border, border-width, border-style, border-color, border-top, border-top-width, border-top-style, border-top-color, border-right, border-right-width, border-right-style, border-right-color, border-bottom, border-bottom-width, border-bottom-style, border-bottom-color, border-left, border-left-width, border-left-style, border-left-color, border-radius, border-top-left-radius, border-top-right-radius, border-bottom-right-radius, border-bottom-left-radius, border-image, border-image-source, border-image-slice, border-image-width, border-image-outset, border-image-repeat, outline, outline-width, outline-style, outline-color, outline-offset, background, background-color, background-image, background-repeat, background-attachment, background-position, background-position-x, background-position-y, background-clip, background-origin, background-size, box-decoration-break, box-shadow, text-shadow +ij_css_space_after_colon = true +ij_css_space_before_opening_brace = true +ij_css_use_double_quotes = true +ij_css_value_alignment = do_not_align + +[.editorconfig] +ij_editorconfig_align_group_field_declarations = false +ij_editorconfig_space_after_colon = false +ij_editorconfig_space_after_comma = true +ij_editorconfig_space_before_colon = false +ij_editorconfig_space_before_comma = false +ij_editorconfig_spaces_around_assignment_operators = true + +[{*.cjs,*.js}] +max_line_length = 160 +tab_width = 2 +ij_continuation_indent_size = 2 +ij_javascript_align_imports = false +ij_javascript_align_multiline_array_initializer_expression = false +ij_javascript_align_multiline_binary_operation = false +ij_javascript_align_multiline_chained_methods = false +ij_javascript_align_multiline_extends_list = true +ij_javascript_align_multiline_for = false +ij_javascript_align_multiline_parameters = false +ij_javascript_align_multiline_parameters_in_calls = false +ij_javascript_align_multiline_ternary_operation = false +ij_javascript_align_object_properties = 0 +ij_javascript_align_union_types = false +ij_javascript_align_var_statements = 0 +ij_javascript_array_initializer_new_line_after_left_brace = false +ij_javascript_array_initializer_right_brace_on_new_line = false +ij_javascript_array_initializer_wrap = off +ij_javascript_assignment_wrap = off +ij_javascript_binary_operation_sign_on_next_line = false +ij_javascript_binary_operation_wrap = off +ij_javascript_blacklist_imports = rxjs/Rx, node_modules/**, **/node_modules/**, @angular/material, @angular/material/typings/** +ij_javascript_blank_lines_after_imports = 1 +ij_javascript_blank_lines_around_class = 1 +ij_javascript_blank_lines_around_field = 0 +ij_javascript_blank_lines_around_function = 1 +ij_javascript_blank_lines_around_method = 1 +ij_javascript_block_brace_style = end_of_line +ij_javascript_call_parameters_new_line_after_left_paren = false +ij_javascript_call_parameters_right_paren_on_new_line = false +ij_javascript_call_parameters_wrap = off +ij_javascript_catch_on_new_line = false +ij_javascript_chained_call_dot_on_new_line = true +ij_javascript_class_brace_style = end_of_line +ij_javascript_comma_on_new_line = false +ij_javascript_do_while_brace_force = if_multiline +ij_javascript_else_on_new_line = false +ij_javascript_enforce_trailing_comma = whenmultiline +ij_javascript_extends_keyword_wrap = off +ij_javascript_extends_list_wrap = off +ij_javascript_field_prefix = _ +ij_javascript_file_name_style = relaxed +ij_javascript_finally_on_new_line = false +ij_javascript_for_brace_force = if_multiline +ij_javascript_for_statement_new_line_after_left_paren = false +ij_javascript_for_statement_right_paren_on_new_line = false +ij_javascript_for_statement_wrap = off +ij_javascript_force_quote_style = true +ij_javascript_force_semicolon_style = true +ij_javascript_function_expression_brace_style = end_of_line +ij_javascript_if_brace_force = if_multiline +ij_javascript_import_merge_members = global +ij_javascript_import_prefer_absolute_path = global +ij_javascript_import_sort_members = true +ij_javascript_import_sort_module_name = true +ij_javascript_import_use_node_resolution = false +ij_javascript_imports_wrap = on_every_item +ij_javascript_indent_case_from_switch = true +ij_javascript_indent_chained_calls = true +ij_javascript_indent_package_children = 0 +ij_javascript_jsx_attribute_value = braces +ij_javascript_keep_blank_lines_in_code = 2 +ij_javascript_keep_first_column_comment = true +ij_javascript_keep_indents_on_empty_lines = false +ij_javascript_keep_line_breaks = true +ij_javascript_keep_simple_blocks_in_one_line = true +ij_javascript_keep_simple_methods_in_one_line = true +ij_javascript_line_comment_add_space = true +ij_javascript_line_comment_at_first_column = false +ij_javascript_method_brace_style = end_of_line +ij_javascript_method_call_chain_wrap = off +ij_javascript_method_parameters_new_line_after_left_paren = false +ij_javascript_method_parameters_right_paren_on_new_line = false +ij_javascript_method_parameters_wrap = off +ij_javascript_object_literal_wrap = on_every_item +ij_javascript_parentheses_expression_new_line_after_left_paren = false +ij_javascript_parentheses_expression_right_paren_on_new_line = false +ij_javascript_place_assignment_sign_on_next_line = false +ij_javascript_prefer_as_type_cast = false +ij_javascript_prefer_explicit_types_function_expression_returns = false +ij_javascript_prefer_explicit_types_function_returns = false +ij_javascript_prefer_explicit_types_vars_fields = false +ij_javascript_prefer_parameters_wrap = false +ij_javascript_reformat_c_style_comments = false +ij_javascript_space_after_colon = true +ij_javascript_space_after_comma = true +ij_javascript_space_after_dots_in_rest_parameter = false +ij_javascript_space_after_generator_mult = true +ij_javascript_space_after_property_colon = true +ij_javascript_space_after_quest = true +ij_javascript_space_after_type_colon = true +ij_javascript_space_after_unary_not = false +ij_javascript_space_before_async_arrow_lparen = true +ij_javascript_space_before_catch_keyword = true +ij_javascript_space_before_catch_left_brace = true +ij_javascript_space_before_catch_parentheses = true +ij_javascript_space_before_class_lbrace = true +ij_javascript_space_before_class_left_brace = true +ij_javascript_space_before_colon = true +ij_javascript_space_before_comma = false +ij_javascript_space_before_do_left_brace = true +ij_javascript_space_before_else_keyword = true +ij_javascript_space_before_else_left_brace = true +ij_javascript_space_before_finally_keyword = true +ij_javascript_space_before_finally_left_brace = true +ij_javascript_space_before_for_left_brace = true +ij_javascript_space_before_for_parentheses = true +ij_javascript_space_before_for_semicolon = false +ij_javascript_space_before_function_left_parenth = false +ij_javascript_space_before_generator_mult = false +ij_javascript_space_before_if_left_brace = true +ij_javascript_space_before_if_parentheses = true +ij_javascript_space_before_method_call_parentheses = false +ij_javascript_space_before_method_left_brace = true +ij_javascript_space_before_method_parentheses = false +ij_javascript_space_before_property_colon = false +ij_javascript_space_before_quest = true +ij_javascript_space_before_switch_left_brace = true +ij_javascript_space_before_switch_parentheses = true +ij_javascript_space_before_try_left_brace = true +ij_javascript_space_before_type_colon = false +ij_javascript_space_before_unary_not = false +ij_javascript_space_before_while_keyword = true +ij_javascript_space_before_while_left_brace = true +ij_javascript_space_before_while_parentheses = true +ij_javascript_spaces_around_additive_operators = true +ij_javascript_spaces_around_arrow_function_operator = true +ij_javascript_spaces_around_assignment_operators = true +ij_javascript_spaces_around_bitwise_operators = true +ij_javascript_spaces_around_equality_operators = true +ij_javascript_spaces_around_logical_operators = true +ij_javascript_spaces_around_multiplicative_operators = true +ij_javascript_spaces_around_relational_operators = true +ij_javascript_spaces_around_shift_operators = true +ij_javascript_spaces_around_unary_operator = false +ij_javascript_spaces_within_array_initializer_brackets = false +ij_javascript_spaces_within_brackets = false +ij_javascript_spaces_within_catch_parentheses = false +ij_javascript_spaces_within_for_parentheses = false +ij_javascript_spaces_within_if_parentheses = false +ij_javascript_spaces_within_imports = false +ij_javascript_spaces_within_interpolation_expressions = false +ij_javascript_spaces_within_method_call_parentheses = false +ij_javascript_spaces_within_method_parentheses = false +ij_javascript_spaces_within_object_literal_braces = false +ij_javascript_spaces_within_object_type_braces = true +ij_javascript_spaces_within_parentheses = false +ij_javascript_spaces_within_switch_parentheses = false +ij_javascript_spaces_within_type_assertion = false +ij_javascript_spaces_within_union_types = true +ij_javascript_spaces_within_while_parentheses = false +ij_javascript_special_else_if_treatment = true +ij_javascript_ternary_operation_signs_on_next_line = true +ij_javascript_ternary_operation_wrap = off +ij_javascript_union_types_wrap = on_every_item +ij_javascript_use_chained_calls_group_indents = false +ij_javascript_use_double_quotes = false +ij_javascript_use_explicit_js_extension = true +ij_javascript_use_path_mapping = always +ij_javascript_use_public_modifier = true +ij_javascript_use_semicolon_after_statement = true +ij_javascript_var_declaration_wrap = split_into_lines +ij_javascript_while_brace_force = if_multiline +ij_javascript_while_on_new_line = false +ij_javascript_wrap_comments = false + +[{*.htm,*.html,*.ng,*.sht,*.shtm,*.shtml}] +max_line_length = 160 +ij_continuation_indent_size = 4 +ij_html_add_new_line_before_tags = body, div, p, form, h1, h2, h3 +ij_html_align_attributes = true +ij_html_align_text = false +ij_html_attribute_wrap = split_into_lines +ij_html_block_comment_at_first_column = true +ij_html_do_not_align_children_of_min_lines = 0 +ij_html_do_not_break_if_inline_tags = title, h1, h2, h3, h4, h5, h6, p +ij_html_do_not_indent_children_of_tags = html, body, thead, tbody, tfoot +ij_html_enforce_quotes = false +ij_html_inline_tags = a, abbr, acronym, b, basefont, bdo, big, br, cite, cite, code, dfn, em, font, i, img, input, kbd, label, q, s, samp, select, small, span, strike, strong, sub, sup, textarea, tt, u, var +ij_html_keep_blank_lines = 2 +ij_html_keep_indents_on_empty_lines = false +ij_html_keep_line_breaks = true +ij_html_keep_line_breaks_in_text = true +ij_html_keep_whitespaces = false +ij_html_keep_whitespaces_inside = span, pre, textarea +ij_html_line_comment_at_first_column = true +ij_html_new_line_after_last_attribute = never +ij_html_new_line_before_first_attribute = never +ij_html_quote_style = double +ij_html_remove_new_line_before_tags = br +ij_html_space_after_tag_name = false +ij_html_space_around_equality_in_attribute = false +ij_html_space_inside_empty_tag = false +ij_html_text_wrap = normal + +[{*.markdown,*.md}] +ij_markdown_force_one_space_after_blockquote_symbol = true +ij_markdown_force_one_space_after_header_symbol = true +ij_markdown_force_one_space_after_list_bullet = true +ij_markdown_force_one_space_between_words = true +ij_markdown_keep_indents_on_empty_lines = false +ij_markdown_max_lines_around_block_elements = 1 +ij_markdown_max_lines_around_header = 1 +ij_markdown_max_lines_between_paragraphs = 1 +ij_markdown_min_lines_around_block_elements = 1 +ij_markdown_min_lines_around_header = 1 +ij_markdown_min_lines_between_paragraphs = 1 + +[{*.py,*.pyw}] +indent_size = 4 +ij_python_align_collections_and_comprehensions = true +ij_python_align_multiline_imports = true +ij_python_align_multiline_parameters = true +ij_python_align_multiline_parameters_in_calls = true +ij_python_blank_line_at_file_end = true +ij_python_blank_lines_after_imports = 1 +ij_python_blank_lines_after_local_imports = 0 +ij_python_blank_lines_around_class = 1 +ij_python_blank_lines_around_method = 1 +ij_python_blank_lines_around_top_level_classes_functions = 2 +ij_python_blank_lines_before_first_method = 0 +ij_python_call_parameters_new_line_after_left_paren = false +ij_python_call_parameters_right_paren_on_new_line = false +ij_python_call_parameters_wrap = normal +ij_python_dict_alignment = 0 +ij_python_dict_new_line_after_left_brace = false +ij_python_dict_new_line_before_right_brace = false +ij_python_dict_wrapping = 1 +ij_python_from_import_new_line_after_left_parenthesis = false +ij_python_from_import_new_line_before_right_parenthesis = false +ij_python_from_import_parentheses_force_if_multiline = false +ij_python_from_import_trailing_comma_if_multiline = false +ij_python_from_import_wrapping = 1 +ij_python_hang_closing_brackets = false +ij_python_keep_blank_lines_in_code = 1 +ij_python_keep_blank_lines_in_declarations = 1 +ij_python_keep_indents_on_empty_lines = false +ij_python_keep_line_breaks = true +ij_python_method_parameters_new_line_after_left_paren = false +ij_python_method_parameters_right_paren_on_new_line = false +ij_python_method_parameters_wrap = normal +ij_python_new_line_after_colon = false +ij_python_new_line_after_colon_multi_clause = true +ij_python_optimize_imports_always_split_from_imports = false +ij_python_optimize_imports_case_insensitive_order = false +ij_python_optimize_imports_join_from_imports_with_same_source = false +ij_python_optimize_imports_sort_by_type_first = true +ij_python_optimize_imports_sort_imports = true +ij_python_optimize_imports_sort_names_in_from_imports = false +ij_python_space_after_comma = true +ij_python_space_after_number_sign = true +ij_python_space_after_py_colon = true +ij_python_space_before_backslash = true +ij_python_space_before_comma = false +ij_python_space_before_for_semicolon = false +ij_python_space_before_lbracket = false +ij_python_space_before_method_call_parentheses = false +ij_python_space_before_method_parentheses = false +ij_python_space_before_number_sign = true +ij_python_space_before_py_colon = false +ij_python_space_within_empty_method_call_parentheses = false +ij_python_space_within_empty_method_parentheses = false +ij_python_spaces_around_additive_operators = true +ij_python_spaces_around_assignment_operators = true +ij_python_spaces_around_bitwise_operators = true +ij_python_spaces_around_eq_in_keyword_argument = false +ij_python_spaces_around_eq_in_named_parameter = false +ij_python_spaces_around_equality_operators = true +ij_python_spaces_around_multiplicative_operators = true +ij_python_spaces_around_power_operator = true +ij_python_spaces_around_relational_operators = true +ij_python_spaces_around_shift_operators = true +ij_python_spaces_within_braces = false +ij_python_spaces_within_brackets = false +ij_python_spaces_within_method_call_parentheses = false +ij_python_spaces_within_method_parentheses = false +ij_python_use_continuation_indent_for_arguments = false +ij_python_use_continuation_indent_for_collection_and_comprehensions = false +ij_python_use_continuation_indent_for_parameters = true +ij_python_wrap_long_lines = false + +[{*.yaml,*.yml}] +indent_size = 2 +ij_yaml_align_values_properties = do_not_align +ij_yaml_autoinsert_sequence_marker = true +ij_yaml_block_mapping_on_new_line = false +ij_yaml_indent_sequence_value = true +ij_yaml_keep_indents_on_empty_lines = false +ij_yaml_keep_line_breaks = true +ij_yaml_sequence_on_new_line = false +ij_yaml_space_before_colon = false +ij_yaml_spaces_within_braces = true +ij_yaml_spaces_within_brackets = true diff --git a/tasks/bin/eotlitetool.py b/tasks/bin/eotlitetool.py index 1fa4983..b6a3dd9 100644 --- a/tasks/bin/eotlitetool.py +++ b/tasks/bin/eotlitetool.py @@ -226,26 +226,28 @@ # BYTE RootString[RootStringSize] // Root string, little-endian UTF-16 - import optparse import struct + class FontError(Exception): """Error related to font handling""" - pass -def multichar(str): - vals = struct.unpack('4B', (str[:4]).encode()) - return (vals[0] << 24) + (vals[1] << 16) + (vals[2] << 8) + vals[3] + +def multichar(string): + values = struct.unpack('4B', (string[:4]).encode()) + return (values[0] << 24) + (values[1] << 16) + (values[2] << 8) + values[3] + def multicharval(v): return struct.pack('4B', (v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) + class EOT: EOT_VERSION = 0x00020001 EOT_MAGIC_NUMBER = 0x504c EOT_DEFAULT_CHARSET = 0x01 - EOT_FAMILY_NAME_INDEX = 0 # order of names in variable portion of EOT header + EOT_FAMILY_NAME_INDEX = 0 # order of names in variable portion of EOT header EOT_STYLE_NAME_INDEX = 1 EOT_VERSION_NAME_INDEX = 2 EOT_FULL_NAME_INDEX = 3 @@ -253,15 +255,16 @@ class EOT: EOT_HEADER_PACK = '<4L10B2BL2H7L18x' + class OpenType: - SFNT_CFF = multichar('OTTO') # Postscript CFF SFNT version - SFNT_TRUE = 0x10000 # Standard TrueType version - SFNT_APPLE = multichar('true') # Apple TrueType version + SFNT_CFF = multichar('OTTO') # Postscript CFF SFNT version + SFNT_TRUE = 0x10000 # Standard TrueType version + SFNT_APPLE = multichar('true') # Apple TrueType version SFNT_UNPACK = '>I4H' TABLE_DIR_UNPACK = '>4I' - TABLE_HEAD = multichar('head') # TrueType table tags + TABLE_HEAD = multichar('head') # TrueType table tags TABLE_NAME = multichar('name') TABLE_OS2 = multichar('OS/2') TABLE_GLYF = multichar('glyf') @@ -279,10 +282,11 @@ class OpenType: NAME_ID_FULL = 4 NAME_ID_VERSION = 5 NAME_ID_POSTSCRIPT = 6 - PLATFORM_ID_UNICODE = 0 # Mac OS uses this typically + PLATFORM_ID_UNICODE = 0 # Mac OS uses this typically PLATFORM_ID_MICROSOFT = 3 - ENCODING_ID_MICROSOFT_UNICODEBMP = 1 # with Microsoft platformID BMP-only Unicode encoding - LANG_ID_MICROSOFT_EN_US = 0x0409 # with Microsoft platformID EN US lang code + ENCODING_ID_MICROSOFT_UNICODEBMP = 1 # with Microsoft platformID BMP-only Unicode encoding + LANG_ID_MICROSOFT_EN_US = 0x0409 # with Microsoft platformID EN US lang code + def eotname(ttf): i = ttf.rfind('.') @@ -290,10 +294,12 @@ def eotname(ttf): ttf = ttf[:i] return ttf + '.eotlite' + def readfont(f): data = open(f, 'rb').read() return data + def get_table_directory(data): """read the SFNT header and table directory""" datalen = len(data) @@ -304,9 +310,7 @@ def get_table_directory(data): if sfntvers != OpenType.SFNT_CFF and sfntvers != OpenType.SFNT_TRUE: raise FontError('invalid font type') - font = {} - font['version'] = sfntvers - font['numTables'] = numTables + font = {'version': sfntvers, 'numTables': numTables} # create set of offsets, lengths for tables table_dir_size = struct.calcsize(OpenType.TABLE_DIR_UNPACK) @@ -323,6 +327,7 @@ def get_table_directory(data): return font + def get_name_records(nametable): """reads through the name records within name table""" name = {} @@ -342,19 +347,20 @@ def get_name_records(nametable): end = start + namerecsize platformID, encodingID, languageID, nameID, namelen, offset = struct.unpack(OpenType.NAME_RECORD_UNPACK, nametable[start:end]) if platformID != OpenType.PLATFORM_ID_MICROSOFT or \ - encodingID != OpenType.ENCODING_ID_MICROSOFT_UNICODEBMP or \ - languageID != OpenType.LANG_ID_MICROSOFT_EN_US: + encodingID != OpenType.ENCODING_ID_MICROSOFT_UNICODEBMP or \ + languageID != OpenType.LANG_ID_MICROSOFT_EN_US: continue namerecs[nameID] = {'offset': offset, 'length': namelen} name['namerecords'] = namerecs return name + def make_eot_name_headers(fontdata, nameTableDir): """extracts names from the name table and generates the names header portion of the EOT header""" nameoffset = nameTableDir['offset'] namelen = nameTableDir['length'] - name = get_name_records(fontdata[nameoffset : nameoffset + namelen]) + name = get_name_records(fontdata[nameoffset: nameoffset + namelen]) namestroffset = name['strOffset'] namerecs = name['namerecords'] @@ -365,7 +371,7 @@ def make_eot_name_headers(fontdata, nameTableDir): namerecord = namerecs[nameid] noffset = namerecord['offset'] nlen = namerecord['length'] - nformat = '%dH' % (nlen / 2) # length is in number of bytes + nformat = '%dH' % (nlen / 2) # length is in number of bytes start = nameoffset + namestroffset + noffset end = start + nlen nstr = struct.unpack('>' + nformat, fontdata[start:end]) @@ -375,10 +381,12 @@ def make_eot_name_headers(fontdata, nameTableDir): return b''.join(nameheaders) + # just return a null-string (len = 0) def make_root_string(): return struct.pack('2x') + def make_eot_header(fontdata): """given ttf font data produce an EOT header""" fontDataSize = len(fontdata) @@ -410,7 +418,7 @@ def make_eot_header(fontdata): if os2size > os2Dir['length']: raise FontError('OS/2 table invalid length') - os2fields = struct.unpack(OpenType.OS2_UNPACK, fontdata[os2offset : os2offset + os2size]) + os2fields = struct.unpack(OpenType.OS2_UNPACK, fontdata[os2offset: os2offset + os2size]) panose = [] urange = [] @@ -432,7 +440,7 @@ def make_eot_header(fontdata): if headsize > headDir['length']: raise FontError('head table invalid length') - headfields = struct.unpack(OpenType.HEAD_UNPACK, fontdata[headoffset : headoffset + headsize]) + headfields = struct.unpack(OpenType.HEAD_UNPACK, fontdata[headoffset: headoffset + headsize]) checkSumAdjustment = headfields[0] # make name headers @@ -449,11 +457,10 @@ def make_eot_header(fontdata): def write_eot_font(eot, header, data): - open(eot,'wb').write(b''.join((header, data))) - return + open(eot, 'wb').write(b''.join((header, data))) -def main(): +def main(): # deal with options p = optparse.OptionParser() p.add_option('--output', '-o') diff --git a/tasks/engines/fontforge/generate.py b/tasks/engines/fontforge/generate.py index ffefdf0..f4b154c 100644 --- a/tasks/engines/fontforge/generate.py +++ b/tasks/engines/fontforge/generate.py @@ -18,68 +18,66 @@ f.descent = args['descent'] f.ascent = args['fontHeight'] - args['descent'] if args['version']: - f.version = args['version'] + f.version = args['version'] if args['normalize']: - f.autoWidth(0, 0, args['fontHeight']) + f.autoWidth(0, 0, args['fontHeight']) KERNING = 15 def create_empty_char(f, c): - pen = f.createChar(ord(c), c).glyphPen() - pen.moveTo((0, 0)) - pen = None + f.createChar(ord(c), c).glyphPen().moveTo((0, 0)) if args['addLigatures']: - f.addLookup('liga', 'gsub_ligature', (), (('liga', (('latn', ('dflt')), )), )) - f.addLookupSubtable('liga', 'liga') + f.addLookup('liga', 'gsub_ligature', (), (('liga', (('latn', ('dflt')),)),)) + f.addLookupSubtable('liga', 'liga') for dirname, dirnames, filenames in os.walk(args['inputDir']): - for filename in sorted(filenames): - name, ext = os.path.splitext(filename) - filePath = os.path.join(dirname, filename) - size = os.path.getsize(filePath) - - if ext in ['.svg']: - # HACK: Remove tags - svgfile = open(filePath, 'r+') - svgtext = svgfile.read() - svgfile.seek(0) - - # Replace the tags with nothing - svgtext = svgtext.replace('', '') - svgtext = svgtext.replace('', '') - - if args['normalize']: - # Replace the width and the height - svgtext = re.sub(r'(]*)width="[^"]*"([^>]*>)', r'\1\2', svgtext) - svgtext = re.sub(r'(]*)height="[^"]*"([^>]*>)', r'\1\2', svgtext) - - # Remove all contents of file so that we can write out the new contents - svgfile.truncate() - svgfile.write(svgtext) - svgfile.close() - - cp = args['codepoints'][name] - - if args['addLigatures']: - name = str(name) # Convert Unicode to a regular string because addPosSub doesn't work with Unicode - for char in name: - create_empty_char(f, char) - glyph = f.createChar(cp, name) - glyph.addPosSub('liga', tuple(name)) - else: - glyph = f.createChar(cp, str(name)) - glyph.importOutlines(filePath) - - if args['normalize']: - glyph.left_side_bearing = glyph.right_side_bearing = 0 - else: - glyph.width = args['fontHeight'] - - if args['round']: - glyph.round(int(args['round'])) + for filename in sorted(filenames): + name, ext = os.path.splitext(filename) + filePath = os.path.join(dirname, filename) + size = os.path.getsize(filePath) + + if ext in ['.svg']: + # HACK: Remove tags + svgfile = open(filePath, 'r+') + svgtext = svgfile.read() + svgfile.seek(0) + + # Replace the tags with nothing + svgtext = svgtext.replace('', '') + svgtext = svgtext.replace('', '') + + if args['normalize']: + # Replace the width and the height + svgtext = re.sub(r'(]*)width="[^"]*"([^>]*>)', r'\1\2', svgtext) + svgtext = re.sub(r'(]*)height="[^"]*"([^>]*>)', r'\1\2', svgtext) + + # Remove all contents of file so that we can write out the new contents + svgfile.truncate() + svgfile.write(svgtext) + svgfile.close() + + cp = args['codepoints'][name] + + if args['addLigatures']: + name = str(name) # Convert Unicode to a regular string because addPosSub doesn't work with Unicode + for char in name: + create_empty_char(f, char) + glyph = f.createChar(cp, name) + glyph.addPosSub('liga', tuple(name)) + else: + glyph = f.createChar(cp, str(name)) + glyph.importOutlines(filePath) + + if args['normalize']: + glyph.left_side_bearing = glyph.right_side_bearing = 0 + else: + glyph.width = args['fontHeight'] + + if args['round']: + glyph.round(int(args['round'])) fontfile = args['dest'] + os.path.sep + args['fontFilename'] @@ -88,46 +86,45 @@ def create_empty_char(f, c): f.fullname = args['fontFamilyName'] if args['addLigatures']: - def generate(filename): - f.generate(filename, flags=('opentype')) + def generate(filename): + f.generate(filename, flags=('opentype')) else: - def generate(filename): - f.generate(filename) - + def generate(filename): + f.generate(filename) # TTF generate(fontfile + '.ttf') # Hint the TTF file # ttfautohint is optional -if (find_executable('ttfautohint') and args['autoHint']): - call('ttfautohint --symbol --fallback-script=latn --no-info "%(font)s.ttf" "%(font)s-hinted.ttf" && mv "%(font)s-hinted.ttf" "%(font)s.ttf"' % {'font': fontfile}, shell=True) - f = fontforge.open(fontfile + '.ttf') +if args['autoHint'] and find_executable('ttfautohint'): + call('ttfautohint --symbol --fallback-script=latn --no-info "%(font)s.ttf" "%(font)s-hinted.ttf" && mv "%(font)s-hinted.ttf" "%(font)s.ttf"' % {'font': fontfile}, shell=True) + f = fontforge.open(fontfile + '.ttf') # SVG if 'svg' in args['types']: - generate(fontfile + '.svg') + generate(fontfile + '.svg') - # Fix SVG header for webkit (from: https://github.com/fontello/font-builder/blob/master/bin/fontconvert.py) - svgfile = open(fontfile + '.svg', 'r+') - svgtext = svgfile.read() - svgfile.seek(0) - svgfile.write(svgtext.replace('', '')) - svgfile.close() + # Fix SVG header for webkit (from: https://github.com/fontello/font-builder/blob/master/bin/fontconvert.py) + svgfile = open(fontfile + '.svg', 'r+') + svgtext = svgfile.read() + svgfile.seek(0) + svgfile.write(svgtext.replace('', '')) + svgfile.close() scriptPath = os.path.dirname(os.path.realpath(__file__)) # WOFF if 'woff' in args['types']: - generate(fontfile + '.woff') + generate(fontfile + '.woff') # EOT if 'eot' in args['types']: - # eotlitetool.py script to generate IE7-compatible .eot fonts - call('python "%(path)s/../../bin/eotlitetool.py" "%(font)s.ttf" --output "%(font)s.eot"' % {'path': scriptPath, 'font': fontfile}, shell=True) + # eotlitetool.py script to generate IE7-compatible .eot fonts + call('python "%(path)s/../../bin/eotlitetool.py" "%(font)s.ttf" --output "%(font)s.eot"' % {'path': scriptPath, 'font': fontfile}, shell=True) # Delete TTF if not needed -if (not 'ttf' in args['types']) and (not 'woff2' in args['types']): - os.remove(fontfile + '.ttf') +if ('ttf' not in args['types']) and ('woff2' not in args['types']): + os.remove(fontfile + '.ttf') print(json.dumps({'file': fontfile}))