Extract key information from TrueType font files

  This article takes extracting the revision fontRevision in the properties of the head module as an example.
Reference article for related properties of TrueType font files: TrueType
font interpreter opentype.js source code download address: opentype.js source code

Figure 1: TrueType
Figure 2: OpenType

  TrueType fonts are generally contained in a single TrueType font file with the suffix .TTF.
  An OpenType font is a file similar to a TrueType font, its format is coded as a POSTSCRIPT font, and the suffix is ​​.OTF.

1. Demand analysis

  We need to extract the fixed parameters among many parameters: revision version, and make sure that the revision version is in the head attribute. At this time, we need to see how Font Inspector splits each attribute module, and find our head attribute, and then in the head attribute module Extract the revision to the fontRevision parameter.
  First of all, we created classes with various attribute fields. Each class contains many parameters, such as head, name, etc. are class objects, and versions, fontRevision, etc. are variable attributes in the class.

2. Read the file

  Read the file, read the binary file, load it into an array, and pass it as a parameter to the subsequent interface.
  Read the first x bits of the file, and read the identification bit as the file type according to the characteristics of different file types to obtain the current file type.
insert image description here
  Different file types may have different attribute modules and field lists. Let's take the ttf file as an example, which contains the following class attribute fields, and then there are various variables under these different class attributes.
insert image description here

3. View head related attributes

  Let's take head as an example. There are many variable parameters under this class, and these parameters have default data types and default values.
insert image description here

4. See how Font Inspector splits each attribute module

  Our TrueType is a font file. This font file has a characteristic. All attribute modules in its font table are 4 characters, as shown in the figure: For the head we are looking for, we mark it with a red solid line.
insert image description here
  Now how should we split each attribute field? And how to separate the head field attribute from these font tables? Through the getTag() interface, this interface is to retrieve the 4-character tag from the DataView view. That is, the 4 characters corresponding to the field attribute are returned.
insert image description here
insert image description here
insert image description here
  So far we have found the head attribute field. The head attribute field is at the starting offset 300 of the entire binary file, and the length is 54. We have obtained valid information. The next thing to do is to extract the fontRevision attribute from the head. At this time, read directly from the actual offset 300, read a fixed number of bytes each time, and find the target to be read.
  We built a related view structure in the head, which contains multiple attribute parameters, and the parameters we want to extract are also marked with red eyes.
insert image description here
  We extract the fontRevision parameter in the headTable structure, and we pass the data data and the starting offset start of the head relative to the binary file to the interface as parameters, and then new Parser object, passes the data in, and saves it in the form of an object.
insert image description here
insert image description here
  In fact, it is equivalent to creating a view, which has data data, starting offset offset, and current offset relativeOffset. The HeadTable structure is as shown in the figure below:
insert image description here
  Next, we use the current offset to read one by one. In addition to the parameter field in the head, it also specifies the type of the parameter field, which determines its offset in the current field. We use parseFixed () interface, according to the type of each parameter field, continuously increments the current offset relativeOffset until the fontRevision parameter is read, and then uses the round function in the Math library to round to truncate the data.
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43202123/article/details/125479078