<commit_before>#!/usr/bin/env python
import argparse
import csv
import logging
import os
import sys
import tabulate

from fontTools import ttLib

args = argparse.ArgumentParser(
	description='Print out macStyle bitmask of the fonts')
args.add_argument('font', nargs="+")
args.add_argument('--csv', default=False, action='store_true')
args.add_argument('--autofix', default=False, action='store_true')


def getByte2(ttfont):
	return ttfont['head'].macStyle >> 8


def getByte1(ttfont):
	return ttfont['head'].macStyle & 255


def printInfo(fonts, print_csv=False):
	rows = []
	headers = ['filename', 'macStyle']
	for font in fonts:
		ttfont = ttLib.TTFont(font)
		row = [os.path.basename(font)]
		row.append('{:#010b} {:#010b}'.format(getByte2(ttfont), getByte1(ttfont)).replace('0b', ''))
		rows.append(row)

	def as_csv(rows):
		writer = csv.writer(sys.stdout)
		writer.writerows([headers])
		writer.writerows(rows)
		sys.exit(0)

	if print_csv:
		as_csv(rows)

	print(tabulate.tabulate(rows, headers))


if __name__ == '__main__':

	arg = args.parse_args()
	if arg.autofix:
		for font in arg.font:
			ttfont = ttLib.TTFont(font)
			if '-Bold' in font:
				ttfont['head'].macStyle |= 0b1
			if 'Italic' in font:
				ttfont['OS/2'].fsSelection |= 0b10
			ttfont.save(font + '.fix')
		printInfo([f + '.fix' for f in arg.font], print_csv=arg.csv)
		sys.exit(0)

	printInfo(arg.font, print_csv=arg.csv)<commit_msg>Add fixer to fontbakery-fix-fsselection.py & fontbakery-fix-macstyle.py #570<commit_after>#!/usr/bin/env python
import argparse
import csv
import logging
import os
import sys
import tabulate

from fontTools import ttLib

args = argparse.ArgumentParser(
	description='Print out macStyle bitmask of the fonts')
args.add_argument('font', nargs="+")
args.add_argument('--csv', default=False, action='store_true')
args.add_argument('--bit-bold', default=False, action='store_true')
args.add_argument('--bit-italic', default=False, action='store_true')
args.add_argument('--autofix', default=False, action='store_true')


def getByte2(ttfont):
	return ttfont['head'].macStyle >> 8


def getByte1(ttfont):
	return ttfont['head'].macStyle & 255


def printInfo(fonts, print_csv=False):
	rows = []
	headers = ['filename', 'macStyle']
	for font in fonts:
		ttfont = ttLib.TTFont(font)
		row = [os.path.basename(font)]
		row.append('{:#010b} {:#010b}'.format(getByte2(ttfont), getByte1(ttfont)).replace('0b', ''))
		rows.append(row)

	def as_csv(rows):
		writer = csv.writer(sys.stdout)
		writer.writerows([headers])
		writer.writerows(rows)
		sys.exit(0)

	if print_csv:
		as_csv(rows)

	print(tabulate.tabulate(rows, headers))


if __name__ == '__main__':

	arg = args.parse_args()
	if arg.autofix:
		for font in arg.font:
			ttfont = ttLib.TTFont(font)
			if '-Bold' in font or arg.bit_bold:
				ttfont['head'].macStyle |= 0b1
			if 'Italic' in font or arg.bit_italic:
				ttfont['head'].macStyle |= 0b10
			ttfont.save(font + '.fix')
		printInfo([f + '.fix' for f in arg.font], print_csv=arg.csv)
		sys.exit(0)

	printInfo(arg.font, print_csv=arg.csv)
{'fraction_non_alphanumeric': 0.1593586387434555, 'fraction_numerical': 0.015706806282722512, 'mean_word_length': 4.924418604651163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2469537', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1123, 'n_words': 240}