Skip to content

Commit

Permalink
Added ftcli fix monospace command
Browse files Browse the repository at this point in the history
  • Loading branch information
ftCLI committed Mar 24, 2023
1 parent ce25fd3 commit 46c5fc6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Or, to install in editable mode:

## Arguments

* [INPUT_PATH](#input_path)
* [INPUT_PATH](#inputpath)

## Common options

Expand Down Expand Up @@ -71,6 +71,7 @@ Or, to install in editable mode:
* [duplicate-components](#ftcli-fix-duplicate-components)
* [italic-angle](#ftcli-fix-italic-angle)
* [kern-table](#ftcli-fix-kern-table)
* [monospace](#ftcli-fix-monospace)
* [nbsp-missing](#ftcli-fix-nbsp-missing)
* [nbsp-width](#ftcli-fix-nbsp-width)
* [os2-ranges](#ftcli-fix-os2-ranges)
Expand Down Expand Up @@ -866,6 +867,35 @@ fontbakery check id: com.google.fonts/check/kern_table
overwritten.
--help Show this message and exit.

### ftcli fix monospace

If the family is monospaced:

* post.isFixedPitch must be set to a non-zero value
* OS/2.panose.bProportion must be set to 9
* CFF.cff.TopDictIndex[0].isFixedPitch must be set to True

fontbakery check id: com.google.fonts/check/monospace

**Usage**:

ftcli fix monospace [OPTIONS] INPUT_PATH

**Options**:

-out, --output-dir DIRECTORY Specify the directory where output files are
to be saved. If output_dir doesn't exist, will
be created. If not specified, files are saved
to the same folder.
--recalc-timestamp Keep the original font 'modified' timestamp
(head.modified) or set it to current time. By
default, original timestamp is kept.
--no-overwrite Overwrite existing output files or save them
to a new file (numbers are appended at the end
of file name). By default, files are
overwritten.
--help Show this message and exit.

### ftcli fix nbsp-missing

Checks if the font has a non-breaking space character, and if it doesn't, it adds one by double mapping 'space'
Expand Down
74 changes: 74 additions & 0 deletions ftcli/commands/ftcli_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,79 @@
)


@click.group()
def fix_monospaced_fonts():
pass


@fix_monospaced_fonts.command()
@add_file_or_path_argument()
@add_common_options()
def monospace(input_path, outputDir=None, recalcTimestamp=False, overWrite=True):
"""
If the family is monospaced:
\b
* post.isFixedPitch must be set to a non-zero value
* OS/2.panose.bProportion must be set to 9
* CFF.cff.TopDictIndex[0].isFixedPitch must be set to True
fontbakery check id: com.google.fonts/check/monospace
"""

files = get_fonts_list(input_path)
if len(files) == 0:
generic_error_message(f"No valid font files found in {input_path}.")
return

output_dir = get_output_dir(fallback_path=input_path, path=outputDir)
dir_ok, error_message = check_output_dir(output_dir)
if dir_ok is False:
generic_error_message(error_message)
return

for file in files:

try:
font = Font(file, recalcTimestamp=recalcTimestamp)

post_table_copy = deepcopy(font.post_table)
os2_table_copy = deepcopy(font.os_2_table)

font.post_table.set_fixed_pitch(True)
font.os_2_table.panose.bProportion = 9

post_table_changed = False
if post_table_copy.compile(font) != font.post_table.compile(font):
post_table_changed = True

os2_table_changed = False
if os2_table_copy.compile(font) != font.os_2_table.compile(font):
os2_table_changed = True

cff_table_changed = False
if font.is_cff:
cff_table = font["CFF "]
cff_table_copy = deepcopy(cff_table)
top_dict = cff_table.cff.topDictIndex[0]
setattr(top_dict, "isFixedPitch", True)

if cff_table_copy.compile(font) != cff_table.compile(font):
cff_table_changed = True

if post_table_changed or os2_table_changed or cff_table_changed:
output_file = makeOutputFileName(file, outputDir=output_dir, overWrite=overWrite)
font.save(output_file)
file_saved_message(file)

else:
file_not_changed_message(file)

except Exception as e:
generic_error_message(e)



@click.group()
def fix_os2_table_unicode_codepage():
pass
Expand Down Expand Up @@ -719,6 +792,7 @@ def strip_names(input_path, recalcTimestamp=False, outputDir=None, overWrite=Tru
fix_italic_metadata,
fix_caret_offset,
fix_missing_nbspace,
fix_monospaced_fonts,
decompose_transformed_components,
remove_glyf_duplicate_components,
fix_unmapped_glyphs_kern_table,
Expand Down

0 comments on commit 46c5fc6

Please sign in to comment.