Example #1
0
def BuildRawHdmx(font):
	head_ = font['head']
	os_2_ = font['OS/2']
	maxp_ = font['maxp']
	hmtx_ = font['hmtx']

	head_.flags |= headFlagInstructionsMayAlterAdvanceWidth

	numGlyphs = maxp_.numGlyphs
	recordSize = 4 * ((2 + numGlyphs + 3) // 4)
	pad = (recordSize - 2 - numGlyphs) * b"\0"
	deviceRecords = []

	upm = head_.unitsPerEm
	widthHw = os_2_.xAvgCharWidth
	for ppem in range(ppemMin, ppemMax + 1):
		ppemHw = widthHw * ppem / upm
		if type(ppemHw) == int:
			continue
		d, i = math.modf(ppemHw)
		if (d <= 1/3):
			ppemHw = int(i)
		else:
			ppemHw = int(i) + 1

		widths = []
		for name in font.getGlyphOrder():
			width = hmtx_[name][0]
			widths.append(math.ceil(width / widthHw) * ppemHw)
		record = bytes([ ppem, max(widths) ] + widths) + pad
		deviceRecords.append(record)

	hdmxHeader = sstruct.pack(hdmxHeaderFormat, SimpleNamespace(version = 0, numRecords = len(deviceRecords), recordSize = recordSize))
	
	hdmx_ = DefaultTable('hdmx')
	hdmx_.data = hdmxHeader + b''.join(deviceRecords)
	font['hdmx'] = hdmx_
from fontTools.ttLib.tables.DefaultTable import DefaultTable

font_path = "myfont.ttf"
output_path = "myfont_patched.ttf"

table_tag = "DSIG"


# Get raw table data from the source font

font = TTFont(font_path)
raw_data = font.getTableData(table_tag)


# Do something with the raw table data
# This example just sets an empty DSIG table.

raw_data = b"\0\0\0\1\0\0\0\0"


# Write the data back to the font

# We could re-use the existing table when the source and target font are
# identical, but let's make a new empty table to be more universal.
table = DefaultTable(table_tag)
table.data = raw_data

# Add the new table back into the source font and save under a new name.
font[table_tag] = table
font.save(output_path)
Example #3
0
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables.DefaultTable import DefaultTable

font_path = "myfont.ttf"
output_path = "myfont_patched.ttf"

table_tag = "DSIG"

# Get raw table data from the source font

font = TTFont(font_path)
raw_data = font.getTableData(table_tag)

# Do something with the raw table data
# This example just sets an empty DSIG table.

raw_data = b"\0\0\0\1\0\0\0\0"

# Write the data back to the font

# We could re-use the existing table when the source and target font are
# identical, but let's make a new empty table to be more universal.
table = DefaultTable(table_tag)
table.data = raw_data

# Add the new table back into the source font and save under a new name.
font[table_tag] = table
font.save(output_path)