�
��g� � �~ � d dl mZ d dlmZ d dlmZ d dlmZmZm Z d dl
mZ d dlm
Z
d dlmZmZmZmZ d dlmZmZ d d lmZmZmZ d d
lZd d
lZd d
lZ ej e� � Z G d� de � � Z! G d
� de � � Z"d� Z#i Z$d#d�Z%d� Z&d� Z'd� Z(d� Z)d� Z*d� Z+d� Z,d� Z-d� Z.d� Z/g d�Z0g d�Z1d#d�Z2d$d�Z3d � Z4d%d"�Z5d
S )&� )�Config)� xmlWriter)�AbstractConfig)�Tag�byteord�tostr)�deprecateArgument)�
TTLibError)�_TTGlyph�_TTGlyphSetCFF�_TTGlyphSetGlyf�_TTGlyphSetVARC)�
SFNTReader�
SFNTWriter)�BytesIO�StringIO�UnsupportedOperationNc �V � e Zd ZdZdddddddeddddddi fd�Zd � Zd
� Zd� Zd1d�Z d2d
�Z
d3d�Z d4d�Zd5d�Z
d2d�Zd� Zd� ZeZd� Zd2d�Zd� Zd� Zd� Zd� Zd� Zd2d�Zd� Zd� Zd � Zed!� � � Zd"� Zd#� Z d$� Z!d%� Z"d&� Z#d'� Z$d6d(�Z%d)� Z&d2d*�Z'd+� Z( d7d,�Z)d-� Z* d8d/�Z+d0� Z,dS )9�TTFonta_ Represents a TrueType font.
The object manages file input and output, and offers a convenient way of
accessing tables. Tables will be only decompiled when necessary, ie. when
they're actually accessed. This means that simple operations can be extremely fast.
Example usage:
.. code-block:: pycon
>>>
>> from fontTools import ttLib
>> tt = ttLib.TTFont("afont.ttf") # Load an existing font file
>> tt['maxp'].numGlyphs
242
>> tt['OS/2'].achVendID
'B&H '
>> tt['head'].unitsPerEm
2048
For details of the objects returned when accessing each table, see the
:doc:`tables </ttLib/tables>` documentation.
To add a table to the font, use the :py:func:`newTable` function:
.. code-block:: pycon
>>>
>> os2 = newTable("OS/2")
>> os2.version = 4
>> # set other attributes
>> font["OS/2"] = os2
TrueType fonts can also be serialized to and from XML format (see also the
:doc:`ttx </ttx>` binary):
.. code-block:: pycon
>>
>> tt.saveXML("afont.ttx")
Dumping 'LTSH' table...
Dumping 'OS/2' table...
[...]
>> tt2 = ttLib.TTFont() # Create a new font object
>> tt2.importXML("afont.ttx")
>> tt2['maxp'].numGlyphs
242
The TTFont object may be used as a context manager; this will cause the file
reader to be closed after the context ``with`` block is exited::
with TTFont(filename) as f:
# Do stuff
Args:
file: When reading a font from disk, either a pathname pointing to a file,
or a readable file object.
res_name_or_index: If running on a Macintosh, either a sfnt resource name or
an sfnt resource index number. If the index number is zero, TTLib will
autodetect whether the file is a flat file or a suitcase. (If it is a suitcase,
only the first 'sfnt' resource will be read.)
sfntVersion (str): When constructing a font object from scratch, sets the four-byte
sfnt magic number to be used. Defaults to `` `` (TrueType). To create
an OpenType file, use ``OTTO``.
flavor (str): Set this to ``woff`` when creating a WOFF file or ``woff2`` for a WOFF2
file.
checkChecksums (int): How checksum data should be treated. Default is 0
(no checking). Set to 1 to check and warn on wrong checksums; set to 2 to
raise an exception if any wrong checksums are found.
recalcBBoxes (bool): If true (the default), recalculates ``glyf``, ``CFF ``,
``head`` bounding box values and ``hhea``/``vhea`` min/max values on save.
Also compiles the glyphs on importing, which saves memory consumption and
time.
ignoreDecompileErrors (bool): If true, exceptions raised during table decompilation
will be ignored, and the binary data will be returned for those tables instead.
recalcTimestamp (bool): If true (the default), sets the ``modified`` timestamp in
the ``head`` table on save.
fontNumber (int): The index of the font in a TrueType Collection file.
lazy (bool): If lazy is set to True, many data structures are loaded lazily, upon
access only. If it is set to False, many data structures are loaded immediately.
The default is ``lazy=None`` which is somewhere in between.
Nz r TF�����c � � dD ]F}t � � � |� � }|�t |d� � t | ||� � �G|| _ || _ |
| _ i | _ d | _ t |t � � r|� � � nt |� � | _
| | _ |s|| _ || _ d | _ d S d}t% |d� � sud}|�`ddlm} |dk r=|� |� � r|� |d� � }n�t/ |d� � }n�|� ||� � }nqt/ |d� � }n`d }t% |d
� � r|� � � }n9t% |d� � r) |� d� � n# t4 $ r d }Y nw xY w| j sm|r|� d� � t7 |� � � � � }t% |d� � r|j |_ |r|� � � |}n|st? d
� � �|| _ tC |||�� � | _ | j j | _ | j j | _ | j j | _ d S )N)�verbose�quiet�configure logging insteadT�read� )�macUtilsr �rbF�seekable�seek�namez*Input file must be seekable when lazy=True)�
fontNumber)"�locals�getr �setattr�lazy�recalcBBoxes�recalcTimestamp�tables�reader�
isinstancer �copyr �cfg�ignoreDecompileErrors�sfntVersion�flavor�
flavorData�hasattr� r �getSFNTResIndices�SFNTResourceReader�openr r r r r r! �closer
�_tableCacher )�self�file�res_name_or_indexr/ r0 �checkChecksumsr r'