xref: /aosp_15_r20/external/fonttools/Snippets/edit_raw_table_data.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
2*e1fe3e4aSElliott Hughesfrom fontTools.ttLib.tables.DefaultTable import DefaultTable
3*e1fe3e4aSElliott Hughes
4*e1fe3e4aSElliott Hughesfont_path = "myfont.ttf"
5*e1fe3e4aSElliott Hughesoutput_path = "myfont_patched.ttf"
6*e1fe3e4aSElliott Hughes
7*e1fe3e4aSElliott Hughestable_tag = "DSIG"
8*e1fe3e4aSElliott Hughes
9*e1fe3e4aSElliott Hughes
10*e1fe3e4aSElliott Hughes# Get raw table data from the source font
11*e1fe3e4aSElliott Hughes
12*e1fe3e4aSElliott Hughesfont = TTFont(font_path)
13*e1fe3e4aSElliott Hughesraw_data = font.getTableData(table_tag)
14*e1fe3e4aSElliott Hughes
15*e1fe3e4aSElliott Hughes
16*e1fe3e4aSElliott Hughes# Do something with the raw table data
17*e1fe3e4aSElliott Hughes# This example just sets an empty DSIG table.
18*e1fe3e4aSElliott Hughes
19*e1fe3e4aSElliott Hughesraw_data = b"\0\0\0\1\0\0\0\0"
20*e1fe3e4aSElliott Hughes
21*e1fe3e4aSElliott Hughes
22*e1fe3e4aSElliott Hughes# Write the data back to the font
23*e1fe3e4aSElliott Hughes
24*e1fe3e4aSElliott Hughes# We could re-use the existing table when the source and target font are
25*e1fe3e4aSElliott Hughes# identical, but let's make a new empty table to be more universal.
26*e1fe3e4aSElliott Hughestable = DefaultTable(table_tag)
27*e1fe3e4aSElliott Hughestable.data = raw_data
28*e1fe3e4aSElliott Hughes
29*e1fe3e4aSElliott Hughes# Add the new table back into the source font and save under a new name.
30*e1fe3e4aSElliott Hughesfont[table_tag] = table
31*e1fe3e4aSElliott Hughesfont.save(output_path)
32