a
    w=icy                  )   @   s  d gZ ddlmZmZ ddlmZ ddlmZ ddlZddl	m
Z
 edddd	d
ddddddddd
ddddZeddddddddddddddddZedddZedddddddddd	ZeddddddddddddddddddZeddddddddddddddddddZedddd
ddddddddddd d!d"d#d$d%d&d'd(d)d*d+Zeddddddddddd,
Zed
dd-dddddddddddddeddddd.dddddddddddddd/dddd0'ZG d1d  d eZd2d3 Zd4d5 ZdS )6FontBuilder   )TTFontnewTable)cmap_classes)timestampNow    N)OrderedDictg      ?i<_   i     )tableVersionZfontRevisionZcheckSumAdjustmentZmagicNumberflags
unitsPerEmcreatedmodifiedZxMinZyMinZxMaxZyMaxZmacStyleZlowestRecPPEMZfontDirectionHintZindexToLocFormatZglyphDataFormat   )r   	numGlyphsZ	maxPointsZmaxContoursZmaxCompositePointsZmaxCompositeContoursZmaxZonesZmaxTwilightPointsZ
maxStorageZmaxFunctionDefsZmaxInstructionDefsZmaxStackElementsZmaxSizeOfInstructionsZmaxComponentElementsZmaxComponentDepthi P  )r   r         @)	
formatTypeZitalicAngleZunderlinePositionZunderlineThicknessZisFixedPitchZminMemType42ZmaxMemType42ZminMemType1ZmaxMemType1)r   ascentdescentlineGapZadvanceWidthMaxZminLeftSideBearingZminRightSideBearingZ
xMaxExtentcaretSlopeRisecaretSlopeRunZcaretOffset	reserved0	reserved1	reserved2	reserved3metricDataFormatZnumberOfHMetrics)r   r   r   r   ZadvanceHeightMaxZminTopSideBearingZminBottomSideBearingZ
yMaxExtentr   r   r   r   r   r   Z	reserved4r   ZnumberOfVMetrics               	   
                                             )	copyrightZ
familyName	styleNameZuniqueFontIdentifierZfullNameversionpsNameZ	trademarkZmanufacturerZdesignerdescriptionZ	vendorURLZdesignerURLZlicenseDescriptionZlicenseInfoURLZtypographicFamilyZtypographicSubfamilyZcompatibleFullNameZ
sampleTextZpostScriptCIDFindfontNameZwwsFamilyNameZwwsSubfamilyNameZlightBackgroundPaletteZdarkBackgroundPaletteZvariationsPostScriptNamePrefix)
ZbFamilyTypeZbSerifStyleZbWeightZbProportionZ	bContrastZbStrokeVariationZ	bArmStyleZbLetterFormZbMidlineZbXHeighti  z????    )'r5   xAvgCharWidthZusWeightClassZusWidthClassZfsTypeZySubscriptXSizeZySubscriptYSizeZySubscriptXOffsetZySubscriptYOffsetZySuperscriptXSizeZySuperscriptYSizeZySuperscriptXOffsetZySuperscriptYOffsetZyStrikeoutSizeZyStrikeoutPositionZsFamilyClassZpanoseulUnicodeRange1ulUnicodeRange2ulUnicodeRange3ZulUnicodeRange4Z	achVendIDZfsSelectionZusFirstCharIndexZusLastCharIndexZsTypoAscenderZsTypoDescenderZsTypoLineGapZusWinAscentZusWinDescentZulCodePageRange1ZulCodePageRange2ZsxHeightZ
sCapHeightZusDefaultCharZusBreakCharZusMaxContextZusLowerOpticalPointSizeZusUpperOpticalPointSizec                   @   s$  e Zd ZdFddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dGddZ
dHddZdd Zdd ZdIddZdd ZdJddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdKd2d3ZdLd4d5Zd6d7 Zd8d9 ZdMd:d;ZdNd=d>ZdOd?d@Z dPdAdBZ!dQdDdEZ"dS )Rr   NTc                 C   sf   |du rFt dd| _|| _t }|dus,J | j|||d |   n|du sRJ || _d|v | _dS )a  Initialize a FontBuilder instance.

        If the `font` argument is not given, a new `TTFont` will be
        constructed, and `unitsPerEm` must be given. If `isTTF` is True,
        the font will be a glyf-based TTF; if `isTTF` is False it will be
        a CFF-based OTF.

        If `font` is given, it must be a `TTFont` instance and `unitsPerEm`
        must _not_ be given. The `isTTF` argument will be ignored.
        NF)ZrecalcTimestamp)r   r   r   glyf)r   fontisTTFr   	setupHead	setupMaxp)selfr   r>   r?   now rD   f/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/fontTools/fontBuilder.py__init__B  s    
zFontBuilder.__init__c                 C   s   | j | dS )zgSave the font. The 'file' argument can be either a pathname or a
        writable file object.
        N)r>   save)rB   filerD   rD   rE   rG   Y  s    zFontBuilder.savec                 C   sR   t | }| j|< | D ]\}}t||| q| D ]\}}t||| q8|S N)r   r>   itemssetattr)rB   tableTagdefaultsvaluestablekvrD   rD   rE   _initTableWithValues_  s    z FontBuilder._initTableWithValuesc                 C   s,   | j | }| D ]\}}t||| qd S rI   )r>   rJ   rK   )rB   rL   rN   rO   rP   rQ   rD   rD   rE   _updateTableWithValuesg  s    
z"FontBuilder._updateTableWithValuesc                 K   s   |  dt| dS )zCreate a new `head` table and initialize it with default values,
        which can be overridden by keyword arguments.
        headN)rR   _headDefaultsrB   rN   rD   rD   rE   r@   l  s    zFontBuilder.setupHeadc                 K   s   |  d| dS )z^Update the head table with the fields and values passed as
        keyword arguments.
        rT   N)rS   rV   rD   rD   rE   
updateHeadr  s    zFontBuilder.updateHeadc                 C   s   | j | dS )z!Set the glyph order for the font.N)r>   ZsetGlyphOrder)rB   
glyphOrderrD   rD   rE   setupGlyphOrderx  s    zFontBuilder.setupGlyphOrderFc                 C   sd  g }|rt |nd}|dkrLtdd | D }t|ddd}|| n|}d}t||dd	}	z|	| j W n2 tjy   |st	d
d}t||dd	}	Y n0 ||	 t||dd}
||
 |dur:i }|D ]@\}}}|
||krd}||vrg ||< || ||f qti ddd}||_|| td| jd< d| jd _|| jd _dS )a  Build the `cmap` table for the font. The `cmapping` argument should
        be a dict mapping unicode code points as integers to glyph names.

        The `uvs` argument, when passed, must be a list of tuples, describing
        Unicode Variation Sequences. These tuples have three elements:
            (unicodeValue, variationSelector, glyphName)
        `unicodeValue` and `variationSelector` are integer code points.
        `glyphName` may be None, to indicate this is the default variation.
        Text processors will then use the cmap to find the glyph name.
        Each Unicode Variation Sequence should be an officially supported
        sequence, but this is not policed.
        r   i  c                 s   s"   | ]\}}|d k r||fV  qdS )r   NrD   ).0rP   rQ   rD   rD   rE   	<genexpr>      z0FontBuilder.setupCharacterMap.<locals>.<genexpr>r&   r	   r$   r   r   zFcmap format 4 subtable overflowed; sort glyph order by unicode to fix.Nr(   r   cmap)maxdictrJ   buildCmapSubTableappendcompiler>   structerror
ValueErrorgetuvsDictr   r   tables)rB   cmappingZuvsZallowFallbackZ	subTablesZhighestUnicodeZcmapping_3_1ZsubTable_3_10formatZsubTable_3_1ZsubTable_0_3rg   ZunicodeValueZvariationSelector	glyphNameZuvsSubTablerD   rD   rE   setupCharacterMap|  sF    




zFontBuilder.setupCharacterMapc                 C   sp   t d }| jd< g |_| D ]J\}}t|tr8|}nt| }t|trTt|d}|j	|| j|||d q dS )a  Create the `name` table for the font. The `nameStrings` argument must
        be a dict, mapping nameIDs or descriptive names for the nameIDs to name
        record values. A value is either a string, or a dict, mapping language codes
        to strings, to allow localized name table entries.

        By default, both Windows (platformID=3) and Macintosh (platformID=1) name
        records are added, unless any of `windows` or `mac` arguments is False.

        The following descriptive names are available for nameIDs:

            copyright (nameID 0)
            familyName (nameID 1)
            styleName (nameID 2)
            uniqueFontIdentifier (nameID 3)
            fullName (nameID 4)
            version (nameID 5)
            psName (nameID 6)
            trademark (nameID 7)
            manufacturer (nameID 8)
            designer (nameID 9)
            description (nameID 10)
            vendorURL (nameID 11)
            designerURL (nameID 12)
            licenseDescription (nameID 13)
            licenseInfoURL (nameID 14)
            typographicFamily (nameID 16)
            typographicSubfamily (nameID 17)
            compatibleFullName (nameID 18)
            sampleText (nameID 19)
            postScriptCIDFindfontName (nameID 20)
            wwsFamilyName (nameID 21)
            wwsSubfamilyName (nameID 22)
            lightBackgroundPalette (nameID 23)
            darkBackgroundPalette (nameID 24)
            variationsPostScriptNamePrefix (nameID 25)
        nameen)ttFontnameIDwindowsmacN)
r   r>   namesrJ   
isinstanceint_nameIDsstrr_   addMultilingualName)rB   ZnameStringsrr   rs   	nameTableZnameNameZ	nameValuerq   rD   rD   rE   setupNameTable  s    %


zFontBuilder.setupNameTablec                 K   s   |  dt| d|vr:d| jv s(J d| jd | j d|v s~d|v s~d|v s~d|v s~d| jv slJ d	| jd | j d
S )zCreate a new `OS/2` table and initialize it with default values,
        which can be overridden by keyword arguments.
        zOS/2r9   hmtxz6the 'hmtx' table must be setup before the 'OS/2' tabler:   r;   r<   r]   z6the 'cmap' table must be setup before the 'OS/2' tableN)rR   _OS2Defaultsr>   ZrecalcAvgCharWidthZrecalcUnicodeRangesrV   rD   rD   rE   setupOS2  s(    zFontBuilder.setupOS2c                 C   sh  ddl m}m}m}m}m}	m}
 | jr*J d| j_	| }d|_
d|_| j|_|g|_| |_|	 }||_|
 }| D ]\}}t||| qvd }d }| }| j |_||_|j|_| D ]\}}t||| qd|vrd| jd j }|dd|ddg|_|d |j||||}| D ]\}}||_||_|||< q||_|j| td| jd< || jd _d S )Nr   )
CFFFontSetTopDictIndexTopDictCharStringsGlobalSubrsIndexPrivateDictOTTOr   
FontMatrixrT   zCFF )cffLibr   r   r   r   r   r   r?   r>   sfntVersionmajorminorZotFontZ	fontNamestopDictIndexGlobalSubrsrJ   rK   getGlyphOrdercharsetPrivater   r   privateglobalSubrsra   r   cff)rB   r6   ZfontInfocharStringsDictprivateDictr   r   r   r   r   r   fontSetr   r   keyvaluefdSelectfdArraytopDictscalecharStringsrk   
charStringrD   rD   rE   setupCFF  sH     	
zFontBuilder.setupCFFc                 C   s  ddl m}m}m}m}m}m}	m}
m} | j	r2J d| j
_| }d|_d|_| j
j}|d |d |_| }||_|d u r|i g}d }|
 }d |_||_|D ]H}| }|d |	 }| D ]\}}t||| q||_|| q| }||_||_d| j
d j }|dd|ddg|_|d j}|d d ||||}| D ]\}}||_||_|||< q8||_|j| td| j
d< || j
d _|r| | d S )	Nr   )r   r   r   r   r   r   FDArrayIndexFontDictr   r
   r   TrT   CFF2) r   r   r   r   r   r   r   r   r   r?   r>   r   r   r   r   r   r   stringsZsetCFF2rJ   rK   r   ra   cff2GetGlyphOrderFDArrayr   r   r   r   r   r   setupCFF2Regions)rB   r   ZfdArrayListregionsr   r   r   r   r   r   r   r   r   r   r   r   r   r   fontDictr   r   r   r   r   r   rk   r   rD   rD   rE   	setupCFF2-  sR    (


zFontBuilder.setupCFF2c                 C   s   ddl m}m}m} ddlm} d| jv s2J dd| jv sDJ ddd	 | jd jD }|||}|tt	t
|d d
d}|||g}	||	d}
| jd jjd }|
|_|jD ]}|
|j_qd S )Nr   )buildVarRegionListbuildVarDatabuildVarStore)VarStoreDatafvarzfvar must to be set up firstr   zCFF2 must to be set up firstc                 S   s   g | ]
}|j qS rD   )axisTag)rZ   arD   rD   rE   
<listcomp>p  r\   z0FontBuilder.setupCFF2Regions.<locals>.<listcomp>F)optimize)Z
otVarStorer   )ZvarLib.builderr   r   r   r   r   r>   axeslistrangelenr   r   ZVarStorer   r   vstore)rB   r   r   r   r   r   ZaxisTagsZvarRegionListZvarDatavarStorer   r   r   rD   rD   rE   r   j  s    


zFontBuilder.setupCFF2Regionsc                 C   s^   | j s
J td| jd< td| jd< || jd _t| jdrN| jj| jd _|rZ|   dS )a|  Create the `glyf` table from a dict, that maps glyph names
        to `fontTools.ttLib.tables._g_l_y_f.Glyph` objects, for example
        as made by `fontTools.pens.ttGlyphPen.TTGlyphPen`.

        If `calcGlyphBounds` is True, the bounds of all glyphs will be
        calculated. Only pass False if your glyph objects already have
        their bounding box values set.
        Zlocar=   rX   N)r?   r   r>   glyphshasattrrX   calcGlyphBounds)rB   r   r   rD   rD   rE   	setupGlyfz  s    	
zFontBuilder.setupGlyfc                 C   s   t | j|| dS )a  Adds an font variations table to the font.

        Args:
            axes (list): See below.
            instances (list): See below.

        ``axes`` should be a list of axes, with each axis either supplied as
        a py:class:`.designspaceLib.AxisDescriptor` object, or a tuple in the
        format ```tupletag, minValue, defaultValue, maxValue, name``.
        The ``name`` is either a string, or a dict, mapping language codes
        to strings, to allow localized name table entries.

        ```instances`` should be a list of instances, with each instance either
        supplied as a py:class:`.designspaceLib.InstanceDescriptor` object, or a
        dict with keys ``location`` (mapping of axis tags to float values),
        ``stylename`` and (optionally) ``postscriptfontname``.
        The ``stylename`` is either a string, or a dict, mapping language codes
        to strings, to allow localized name table entries.
        N)addFvarr>   )rB   r   	instancesrD   rD   rE   	setupFvar  s    zFontBuilder.setupFvarc                 C   s$   ddl m} || jtt| dS )zAdds an axis variations table to the font.

        Args:
            axes (list): A list of py:class:`.designspaceLib.AxisDescriptor` objects.
        r   )	_add_avarN)varLibr   r>   r   	enumerate)rB   r   r   rD   rD   rE   	setupAvar  s    zFontBuilder.setupAvarc                 C   s(   t d }| jd< d|_d|_||_d S )Ngvarr   r   )r   r>   r5   reserved
variations)rB   r   r   rD   rD   rE   	setupGvar  s    zFontBuilder.setupGvarc                 C   s(   | j d }|j D ]}|| qdS )zCalculate the bounding boxes of all glyphs in the `glyf` table.
        This is usually not called explicitly by client code.
        r=   N)r>   r   rN   ZrecalcBounds)rB   Z
glyphTableZglyphrD   rD   rE   r     s    
zFontBuilder.calcGlyphBoundsc                 C   s   |  d| dS )zCreate a new `hmtx` table, for horizontal metrics.

        The `metrics` argument must be a dict, mapping glyph names to
        `(width, leftSidebearing)` tuples.
        r|   NsetupMetricsrB   metricsrD   rD   rE   setupHorizontalMetrics  s    z"FontBuilder.setupHorizontalMetricsc                 C   s   |  d| dS )zCreate a new `vmtx` table, for horizontal metrics.

        The `metrics` argument must be a dict, mapping glyph names to
        `(height, topSidebearing)` tuples.
        vmtxNr   r   rD   rD   rE   setupVerticalMetrics  s    z FontBuilder.setupVerticalMetricsc                 C   s^   |dv sJ t | }| j|< i }|D ],}|| \}}tt|tt|f||< q&||_dS )z<See `setupHorizontalMetrics()` and `setupVerticalMetrics()`.)r|   r   N)r   r>   rv   roundr   )rB   rL   r   ZmtxTableZroundedMetricsgnwZlsbrD   rD   rE   r     s    zFontBuilder.setupMetricsc                 K   s   |  dt| dS )z{Create a new `hhea` table initialize it with default values,
        which can be overridden by keyword arguments.
        ZhheaN)rR   _hheaDefaultsrV   rD   rD   rE   setupHorizontalHeader  s    z!FontBuilder.setupHorizontalHeaderc                 K   s   |  dt| dS )z{Create a new `vhea` table initialize it with default values,
        which can be overridden by keyword arguments.
        ZvheaN)rR   _vheaDefaultsrV   rD   rD   rE   setupVerticalHeader  s    zFontBuilder.setupVerticalHeaderc                    s   |du rZi  |D ].}|| }| vr.d |< q |  d7  < qt   fddddd }| di ti |d	 | jd }d|_d|_|D ]}|| ||< qdS )
aY  Create a new `VORG` table. The `verticalOrigins` argument must be
        a dict, mapping glyph names to vertical origin values.

        The `defaultVerticalOrigin` argument should be the most common vertical
        origin value. If omitted, this value will be derived from the actual
        values in the `verticalOrigins` argument.
        Nr   c                    s    |  S rI   rD   )vorgZbagrD   rE   <lambda>  r\   z2FontBuilder.setupVerticalOrigins.<locals>.<lambda>T)r   reverser   ZVORG)ZVOriginRecordsZdefaultVertOriginY)sortedrR   r_   r>   ZmajorVersionZminorVersion)rB   ZverticalOriginsZdefaultVerticalOriginr   r   Z	vorgTablerD   r   rE   setupVerticalOrigins  s,    


z FontBuilder.setupVerticalOriginsc                 K   sD   d| j v }| dt|}| js"|r:|r:d|_g |_i |_nd|_dS )zCreate a new `post` table and initialize it with default values,
        which can be overridden by keyword arguments.
        r   postg       @r   N)r>   rR   _postDefaultsr?   r   Z
extraNamesmapping)rB   ZkeepGlyphNamesrN   ZisCFF2Z	postTablerD   rD   rE   	setupPost   s    
zFontBuilder.setupPostc                 C   s"   | j rt}nt}| d|i  dS )zCreate a new `maxp` table. This is called implicitly by FontBuilder
        itself and is usually not called by client code.
        ZmaxpN)r?   _maxpDefaultsTTF_maxpDefaultsOTFrR   )rB   rM   rD   rD   rE   rA     s    zFontBuilder.setupMaxpc                 C   s"   t dddg d}| di | dS )zThis adds an empty DSIG table to the font to make some MS applications
        happy. This does not properly sign the font.
        r   r   )Z	ulVersionZusFlagZ	usNumSigsZsignatureRecordsZDSIGN)r_   rR   rV   rD   rD   rE   setupDummyDSIG  s    zFontBuilder.setupDummyDSIGc                 C   s"   ddl m} || j|||d dS )a  Add OpenType features to the font from a string containing
        Feature File syntax.

        The `filename` argument is used in error messages and to determine
        where to look for "include" files.

        The optional `tables` argument can be a list of OTL tables tags to
        build, allowing the caller to only build selected OTL tables. See
        `fontTools.feaLib` for details.
        r   )addOpenTypeFeaturesFromString)filenamerh   N)ZfeaLib.builderr   r>   )rB   featuresr   rh   r   rD   rD   rE   addOpenTypeFeatures#  s    
zFontBuilder.addOpenTypeFeaturesrvrnc                 C   s4   ddl m} d| jvrtd|j| j||d dS )z|Add conditional substitutions to a Variable Font.

        See `fontTools.varLib.featureVars.addFeatureVariations`.
        r   )featureVarsr   z5'fvar' table is missing; can't add FeatureVariations.)
featureTagN)r   r   r>   KeyErroraddFeatureVariations)rB   ZconditionalSubstitutionsr   r   rD   rD   rE   r   4  s    
z FontBuilder.addFeatureVariationsc           	   	   C   s6   ddl m} | j }||||||||d| jd< dS )zqBuild new COLR table using color layers dictionary.

        Cf. `fontTools.colorLib.builder.buildCOLR`.
        r   )	buildCOLR)r5   glyphMapr   varIndexMap	clipBoxesallowLayerReuseZCOLRN)fontTools.colorLib.builderr   r>   ZgetReverseGlyphMap)	rB   ZcolorLayersr5   r   r   r   r   r   r   rD   rD   rE   	setupCOLRB  s    
zFontBuilder.setupCOLRc                 C   s0   ddl m} |||||| jdd| jd< dS )zBuild new CPAL table using list of palettes.

        Optionally build CPAL v1 table using paletteTypes, paletteLabels and
        paletteEntryLabels.

        Cf. `fontTools.colorLib.builder.buildCPAL`.
        r   )	buildCPALrm   )paletteTypespaletteLabelspaletteEntryLabelsrz   ZCPALN)r   r   r>   rf   )rB   Zpalettesr   r   r   r   rD   rD   rE   	setupCPAL\  s    
zFontBuilder.setupCPALr
   c                 C   s    ddl m} || j||| dS )zBuild a new 'STAT' table.

        See `fontTools.otlLib.builder.buildStatTable` for details about
        the arguments.
        r   )buildStatTableN)ZotlLib.builderr   r>   )rB   r   	locationsZelidedFallbackNamer   rD   rD   rE   	setupStatt  s    zFontBuilder.setupStat)NNT)NF)TT)NN)T)N)T)NN)r   )NNNNT)NNN)Nr
   )#__name__
__module____qualname__rF   rG   rR   rS   r@   rW   rY   rl   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   r   r   r   r   r   r   rD   rD   rD   rE   r   A  sP   

6
33
=







     
   
c                 C   s(   t | |}| |_||_||_d|_|S )Nr   )r   r]   
platformID	platEncIDlanguage)ri   rj   r   r   ZsubTablerD   rD   rE   r`     s    r`   c                 C   s\  ddl m}m} |sJ td}| d }|D ]}| }t|trX|\|_|_|_|_	}	n*|j
|j|j|j|jf\|_|_|_|_	}	t|	trt|	d}	|j|	| d|_|j| q,|D ]}
t|
tr|
d }|
d }	|
d	}n|
j}|
jp|
j}	|
j}t|	trt|	d}	| }|j|	| d|_|d ur<|||_||_|j| q|| d< d S )
Nr   )AxisNamedInstancer   rm   rn   )rp   locationZ	stylenameZpostscriptfontname)ZttLib.tables._f_v_a_rr   r  r   ru   tupler   ZminValuedefaultValueZmaxValuetagminimumdefaultmaximumrm   rx   r_   ry   Z
axisNameIDr   ra   rf   r  ZlocalisedStyleNamer4   ZpostScriptFontNameZsubfamilyNameIDZaddNameZpostscriptNameIDcoordinatesr   )r>   r   r   r   r  r   rz   Zaxis_defZaxisrm   instancer	  ZpsnameinstrD   rD   rE   r     sT    
	




r   )__all__ZttLibr   r   ZttLib.tables._c_m_a_pr   Zmisc.timeToolsr   rc   collectionsr   r_   rU   r   r   r   r   r   rw   Z_panoseDefaultsr}   objectr   r`   r   rD   rD   rD   rE   <module>   sl    +    B	