
    2hx\                     x   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlm Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+  ed      Z,ee-e
f   Z.ee/e.e!j`                  ee-df   f   Z1ee1   Z2ee-ee2e1f   f   Z3ee-eee-e/f      f   Z4eee/e/e/e/e/f   ee/e/e/e/f   e!jj                  f   Z6dZ7dZ8dZ9d Z:d Z;d Z<d Z=d Z>d Z?d Z@	 dJde!j                  de4d eee-e/f      fd!ZB	 dJddddd"d#d$e3d%ee/   d eee-e/f      d&ee!j                     d'ee!j                     d(eee-e6f      d)eEd*ej                  fd+ZGd(ee-e6f   d*e!j                  fd,ZId-e6d*e!jj                  fd.ZJ G d/ d0ej                        ZLede-ee-e-f   f   ZMd1eeM   d2ej                  d*eee/      fd3ZO	 	 	 	 dKd4eeeePePePePf         d5eeeL      d6eeeM      d7eeeM      d2eej                     d*ej                  fd8ZRd9e
d*eEfd:ZSd$e3d*ee4e3f   fd;ZTd<e/d*eee/e/f   ddf   fd=ZU G d> d?      ZV G d@ dA      ZWdBe-dCeWdDe1d*e!j                  fdEZYdFee-eZf   d*e-fdGZ[	 dJd"dHd$e3d eee-e/f      d)eEd*eee!j                     e!j                  f   fdIZ]y)Lz8
colorLib.builder: Build COLR/CPAL tables from scratch

    N)partial)ceillog)AnyDict	GeneratorIterableListMappingOptionalSequenceTupleTypeTypeVarUnion)intRect)fixedToFloat)build_n_ary_tree)C_O_L_R_)C_P_A_L_)_n_a_m_e)otTables)
ExtendModeCompositeMode   )ColorLibError)%round_start_circle_stable_containment)BuildCallbackTableBuilderT_PaintInput   g      ?    c                     |d   }|d   }|d   }|d   }|d   }|d   }t        ||f|||f|      }|j                  \  }}|j                  }||d<   ||d<   ||d<   ||d<   ||d<   ||d<   | |fS )Nx0y0r0x1y1r1)r   centreradius)	paintsourcer%   r&   r'   r(   r)   r*   cs	            ?D:\jyotish\venv\Lib\site-packages\fontTools/colorLib/builder.py_beforeBuildPaintRadialGradientr1   9   s    	B	B	B	B	B	B
 	.r2hRHbIAXXFB	
B F4LF4LF4LF4LF4LF4L&=    c                  D    t        j                         } t        | _        | S N)ot	ColorStop_DEFAULT_ALPHAAlpha	colorStops    r0   _defaultColorStopr;   S   s    I$IOr2   c                  D    t        j                         } t        | _        | S r4   )r5   VarColorStopr7   r8   r9   s    r0   _defaultVarColorStopr>   Y   s    !I$IOr2   c                  X    t        j                         } t        j                  | _        | S r4   )r5   	ColorLiner   PADExtend	colorLines    r0   _defaultColorLinerE   _   s    I!~~Ir2   c                  X    t        j                         } t        j                  | _        | S r4   )r5   VarColorLiner   rA   rB   rC   s    r0   _defaultVarColorLinerH   e   s     !I!~~Ir2   c                  D    t        j                         } t        | _        | S r4   )r5   Paintr7   r8   )r-   s    r0   _defaultPaintSolidrK   k   s    HHJE EKLr2   c                     t         j                  t        j                  t        j                  j
                  ft        t         j                  t        j                  t        j                  j                  ft        t         j                  t        j                  ft        t         j                  t        j                  ft        t         j                  t        j                  ft        t         j                  t        j                  ft         t         j                  t        j                  t        j                  j"                  ft$        t         j                  t        j                  t        j                  j&                  ft$        iS r4   )r   BEFORE_BUILDr5   rJ   PaintFormatPaintRadialGradientr1   PaintVarRadialGradientCREATE_DEFAULTr6   r;   r=   r>   r@   rE   rG   rH   
PaintSolidrK   PaintVarSolid r2   r0   _buildPaintCallbacksrU   q   s     &&HHNN..	
 +&&HHNN11	
 +		%	%r||46G		%	%r79M		%	%r||46G		%	%r79M((HHNN%%	
 ((HHNN((	
 1 r2   tablecolorGlyphsV0glyphMapc                    t        |j                         fd      }n|j                         }g }g }|D ]  \  }}t        j                         }||_        t        |      |_        t        |      |_        |j                  |       |D ]8  \  }	}
t        j                         }|	|_
        |
|_        |j                  |       :  dx| _        | _        |r*t        j                         | _        || j                  _        |r*t        j                         | _        || j                  _	        t        |      | _        t        |      | _        y)a  Build v0 color layers and add to existing COLR table.

    Args:
        table: a raw ``otTables.COLR()`` object (not ttLib's ``table_C_O_L_R_``).
        colorGlyphsV0: map of base glyph names to lists of (layer glyph names,
            color palette index) tuples. Can be empty.
        glyphMap: a map from glyph names to glyph indices, as returned from
            ``TTFont.getReverseGlyphMap()``, to optionally sort base records by GID.
    Nc                     | d      S Nr   rT   itemrX   s    r0   <lambda>z populateCOLRv0.<locals>.<lambda>   s    HT!W4Er2   key)sorteditemsr5   BaseGlyphRecord	BaseGlyphlenFirstLayerIndex	NumLayersappendLayerRecord
LayerGlyphPaletteIndexBaseGlyphRecordArrayLayerRecordArrayBaseGlyphRecordCountLayerRecordCount)rV   rW   rX   colorGlyphItemsbaseGlyphRecordslayerRecords	baseGlyphlayersbaseRec
layerGlyphpaletteIndexlayerRecs     `         r0   populateCOLRv0ry      s5     !'E
 (--/L,	6$$&%"%l"3K((.$J~~'H",H$0H!)	 )/ - ;?>E!7%'%<%<%>"5E""2!#!4!4!6-9*!$%5!6E .Er2   T)rX   varStorevarIndexMap	clipBoxesallowLayerReusecolorGlyphsversionrz   r{   r|   r}   returnc                   t        j                         }||dk(  rt        d      |dv r"|s t        |       \  }}	|dk(  r|	rt        d      i | }	}t	        j
                         }
t        |
||       t        |	||      \  |
_        |
_	        |	|s|	rdnd}n|dvrt        |      |x|_        |
_        |dk(  r|j                  |
      |_        |S |rt        |      nd|
_        ||
_        ||
_        |
|_        |S )	a0  Build COLR table from color layers mapping.

    Args:

        colorGlyphs: map of base glyph name to, either list of (layer glyph name,
            color palette index) tuples for COLRv0; or a single ``Paint`` (dict) or
            list of ``Paint`` for COLRv1.
        version: the version of COLR table. If None, the version is determined
            by the presence of COLRv1 paints or variation data (varStore), which
            require version 1; otherwise, if all base glyphs use only simple color
            layers, version 0 is used.
        glyphMap: a map from glyph names to glyph indices, as returned from
            TTFont.getReverseGlyphMap(), to optionally sort base records by GID.
        varStore: Optional ItemVarationStore for deltas associated with v1 layer.
        varIndexMap: Optional DeltaSetIndexMap for deltas associated with v1 layer.
        clipBoxes: Optional map of base glyph name to clip box 4- or 5-tuples:
            (xMin, yMin, xMax, yMax) or (xMin, yMin, xMax, yMax, varIndexBase).

    Returns:
        A new COLR table.
    Nr   zCan't add VarStore to COLRv0r[   z$Can't encode COLRv1 glyphs in COLRv0r}   r   )r   r   )r   table_C_O_L_R_
ValueError_split_color_glyphs_by_versionr5   COLRry   buildColrV1	LayerListBaseGlyphListNotImplementedErrorr   Version_decompileColorLayersV0ColorLayersbuildClipListClipListVarIndexMapVarStorerV   )r~   r   rX   rz   r{   r|   r}   selfrW   colorGlyphsV1colrs              r0   	buildCOLRr      s   > ""$D1788)H'Ek'R$}a<MCDD (*;}779D41)4'*&DND&  M!		!'**"))DL4<!|77= K 5>i04& 
Kr2   c                     t        j                         }d|_        | j                         D ci c]  \  }}|t	        |       c}}|_        |S c c}}w )Nr   )r5   r   Formatrb   buildClipBoxclips)r|   clipListnameboxs       r0   r   r     sL    {{}HHO?H?PQ?P)$dL--?PQHNO Rs   AclipBoxc                 J   t        | t        j                        r| S t        |       }t        j                         }|dvrt	        d|       t        | d d       \  |_        |_        |_        |_	        t        |dk(        dz   |_        |dk(  rt        | d         |_        |S )N)      z/Invalid ClipBox: expected 4 or 5 values, found r   r   r   )
isinstancer5   ClipBoxre   r   r   xMinyMinxMaxyMaxintr   VarIndexBase)r   nclips      r0   r   r     s    '2::&GA::<DJ1#NOO18!1E.DIty$)TYa1f+/DKAv
OKr2   c                   0     e Zd ZdZdZe fd       Z xZS )ColorPaletteTyper      c                     t        |t              r'|dk  s|dz  dk7  rt        | d| j                         t        |   |      S )Nr   i  z is not a valid )r   r   r   __name__super	_missing_)clsvalue	__class__s     r0   r   zColorPaletteType._missing_  sK     eS!uqyEFNa4Gw&6s||nEFFw ''r2   )r   
__module____qualname__USABLE_WITH_LIGHT_BACKGROUNDUSABLE_WITH_DARK_BACKGROUNDclassmethodr   __classcell__)r   s   @r0   r   r     s!    #) "(( (r2   r   labels	nameTablec                     | D cg c]V  }t        |t              r|j                  |d      n0|t        j                  j
                  n|j                  d|id      X c}S c c}w )NF)macen)r   dictaddMultilingualNamer   table_C_P_A_L_
NO_NAME_ID)r   r   ls      r0   buildPaletteLabelsr   *  s}      A !T" ))!)7 9 ''2222D!9%2H   s   AA#palettespaletteTypespaletteLabelspaletteEntryLabelsc                    t        | D ch c]  }t        |       c}      dk7  rt        d      |s|r|st        d      t        j                         }t        | d         |_        g |_        t        |       D ]  \  }}g }	t        |      D ]  \  }
}t        |t              rt        |      dk7  rt        d| d|
 d|      t        d	 |D              rt        d
| d|
 d|      |\  }}}}|	j                  t        j                  d ||||fD                 |j                  j                  |	        t        d |||fD              rd|_        |ct        |      t        |       k7  r#t        dt        |        dt        |             |D cg c]  }t        |      j                   c}|_        n,t        j                  j"                  gt        |       z  |_        |Lt        |      t        |       k7  r#t        dt        |        dt        |             t%        ||      |_        n,t        j                  j(                  gt        |       z  |_        |Ot        |      |j
                  k7  r$t        d|j
                   dt        |             t%        ||      |_        |S t        j                  j(                  g|j
                  z  |_        |S d|_        |S c c}w c c}w )al  Build CPAL table from list of color palettes.

    Args:
        palettes: list of lists of colors encoded as tuples of (R, G, B, A) floats
            in the range [0..1].
        paletteTypes: optional list of ColorPaletteType, one for each palette.
        paletteLabels: optional list of palette labels. Each lable can be either:
            None (no label), a string (for for default English labels), or a
            localized string (as a dict keyed with BCP47 language codes).
        paletteEntryLabels: optional list of palette entry labels, one for each
            palette entry (see paletteLabels).
        nameTable: optional name table where to store palette and palette entry
            labels. Required if either paletteLabels or paletteEntryLabels is set.

    Return:
        A new CPAL v0 or v1 table, if custom palette types or labels are specified.
    r   z%color palettes have different lengthsz?nameTable is required if palette or palette entries have labelsr   r   zIn palette[z][z$]: expected (R, G, B, A) tuple, got c              3   4   K   | ]  }|d kD  xs |dk    yw)r   r   NrT   .0vs     r0   	<genexpr>zbuildCPAL.<locals>.<genexpr>f  s     15a1q5>AE>5s   zpalette[z)] has invalid out-of-range [0..1] color: c              3   8   K   | ]  }t        |d z          yw)r"   N)roundr   s     r0   r   zbuildCPAL.<locals>.<genexpr>m  s      S9RAq3w9Rs   c              3   $   K   | ]  }|d u 
 y wr4   rT   r   s     r0   r   zbuildCPAL.<locals>.<genexpr>q  s     
T"SQ1D="Ss   z	Expected z paletteTypes, got z paletteLabels, got z paletteEntryLabels, got )re   r   	TypeErrorr   r   numPaletteEntriesr   	enumerater   tupleanyrh   Colorr   r   r   r   DEFAULT_PALETTE_TYPEr   r   r   r   )r   r   r   r   r   pcpalipalettecolorsjcolorredgreenbluealphats                    r0   	buildCPALr   ;  s!   0 H%HqCFH%&!+CDD+YM
 	
 ""$D !-DDM)
7!'*HAueU+s5zQ#!!Bqc)MeYW  1511#qcA3&OPUyY  ',#CeMM S$sE9R ST + 	V$! *$ 
T<@R"S
TT#< CM1#H.A#lBSATU  EQ QLq!1!!4!:!:L QD!)!8!8!M!M NQTR !D $=!S]2#H.B3}CUBVW  "4M9!MD"*"9"9"D"D!EH!UD)%&$*@*@@# 6 67 81235  '99KY&WD# K ''22'&&''D# K KG &L !Rs   K/K4layerc                 |    	 | \  }}t        |t              xr t        |t              S # t        t        f$ r Y yw xY w)NF)r   strr   r   r   )r   rv   rw   s      r0   _is_colrv0_layerr     sF    
M#( 
L *c*Lz,/LL z" s   ) ;;c                     i }i }| j                         D ]"  \  }}t        d |D              r|||<   |||<   $ t        |       t        |      t        |      z  k(  sJ ||fS )Nc              3   2   K   | ]  }t        |        y wr4   )r   )r   r   s     r0   r   z1_split_color_glyphs_by_version.<locals>.<genexpr>  s     3Fq"Fs   )rb   allset)r~   rW   r   rs   rt   s        r0   r   r     sw     MM(..0	63F33'-M)$'-M)$	 1 {M 2S5G GHHH-''r2   
num_layersc           	   #      K   t        |       D ]3  }t        |dz   t        | dz   |dz   t        z               D ]  }||f 
 5 y w)Nr   r   )rangemin_MAX_REUSE_LEN)r   lboundubounds      r0   _reuse_rangesr     sP     
# QJJNFQJ,GH
F 6""
	 $s   AAc                   6   e Zd ZU eeedf   ef   ed<   eeeedf   f   ed<   ee	j                     ed<   d Zde	j                  fdZdee	j                     d	eedf   fd
Zdee	j                     d	ee	j                     fdZdee	j                     defdZy)LayerReuseCache.	reusePooltuples	keepAlivec                 .    i | _         i | _        g | _        y r4   )r   r   r   )r   s    r0   __init__zLayerReuseCache.__init__  s    r2   r-   c                     fd| j                   j                  t        |      d       }|; |      }|| j                   t        |      <   | j                  j	                  |       |S )Nc                 2   t        | t        j                        r| S t        | d      r5t	        fdt        | j                  j                               D              S t        | t        j                  j                        rt	        fd| D              S | S )N__dict__c              3   8   K   | ]  \  }}| |      f  y wr4   rT   )r   kr   _tuple_safes      r0   r   zDLayerReuseCache._paint_tuple.<locals>._tuple_safe.<locals>.<genexpr>  s#      4RDAqQA'4Rs   c              3   .   K   | ]  } |        y wr4   rT   )r   er  s     r0   r   zDLayerReuseCache._paint_tuple.<locals>._tuple_safe.<locals>.<genexpr>  s     ;U[^Us   )r   enumEnumhasattrr   ra   r   rb   collectionsabcMutableSequence)r   r  s    r0   r  z1LayerReuseCache._paint_tuple.<locals>._tuple_safe  sx    %+
+ 4:5>>;O;O;Q4R   E;??#B#BC;U;;;Lr2   )r   getidr   rh   )r   r-   resultr  s      @r0   _paint_tuplezLayerReuseCache._paint_tuple  sZ    		 ED1> 'F%+DKK5	"NN!!%(r2   paintsr   c                 ,     t         fd|D              S )Nc              3   @   K   | ]  }j                  |        y wr4   )r  )r   r   r   s     r0   r   z,LayerReuseCache._as_tuple.<locals>.<genexpr>  s     :6aT&&q)6s   )r   )r   r  s   ` r0   	_as_tuplezLayerReuseCache._as_tuple  s    :6:::r2   rt   c                    d}|rd}t        t        t        |            d d      }|D ]  \  }}| j                  j	                  | j                  |||       d      }|dk(  r:t        j                         }t        t        j                  j                        |_        ||z
  |_        ||_        |d | |gz   ||d  z   }d} n |r|S )NTFc                 *    | d   | d   z
  | d   | d   fS )Nr   r   rT   )r   s    r0   r^   z+LayerReuseCache.try_reuse.<locals>.<lambda>  s    qtad{AaD!A$7r2   )r`   reverse)ra   r   re   r   r  r  r5   rJ   r   rN   PaintColrLayersr   rg   rf   )r   rt   found_reuserangesr   r   reuse_lbound	new_slices           r0   	try_reusezLayerReuseCache.try_reuse  s    Kc&k*7F
 #)#~~11NN6&#892   2%HHJ	#&r~~'E'E#F	 &,vo	#,8	)I;6H" #) * r2   first_layer_indexc                     t        t        |            D ])  \  }}||z   | j                  | j                  |||       <   + y r4   )r   re   r   r  )r   rt   r  r   r   s        r0   addzLayerReuseCache.add  s@    +CK8NFF** NN4>>&*?@A 9r2   N)r   r   r   r   r   r   r   __annotations__r
   r5   rJ   r   r  r   r  r  r   rT   r2   r0   r   r     s    uS#X+,,CsCx())BHH~
"(( ,; 2 ;uS#X ;RXX 4> 2$rxx. S r2   r   c                       e Zd ZU eej
                     ed<   eed<   eed<   dddZ	d Z
ded	ej
                  fd
Zd	eej                     fdZy)LayerListBuilderrt   cacher}   Tr   c                   g | _         |rt               | _        nd | _        t               }| j                  |t
        j                  t        j                  t        j                  j                  f<   t        |      | _        y r4   )rt   r   r$  rU   _beforeBuildPaintColrLayersr   rM   r5   rJ   rN   r  r   tableBuilder)r   r}   	callbackss      r0   r   zLayerListBuilder.__init__  sm    (*DJDJ )*	 ,, 	**..	
 )3r2   c                 d    t        |j                  dd       t        j                  j                        r|d   }n|d   }|D cg c]  } j                  |       }}t        |      dk(  r|d   i fS  j                   j                  j                  |      }t        |      t        kD  }t        |t              } fd|D cg c]
  } |       }}t        |      dk(  r|d   i fS t        j                         }t        t        j                  j                        |_        t        |      |_        t         j$                        |_         j$                  j)                  |        j                  (|s& j                  j+                  ||j&                         |i fS c c}w c c}w )Nrg   Layersr   r   )r   c           	          t        | t        j                  j                        r@j	                  t
        j                  j                  | D cg c]
  } |       c}d      S | S c c}w )N)r   r*  )r   r	  r
  r   
buildPaintr5   rN   r  )r   r   listToColrLayersr   s     r0   r-  zFLayerListBuilder._beforeBuildPaintColrLayers.<locals>.listToColrLayers=  s_    %!9!9:"$.."@"@@E"F1#3A#6"F  L #Gs   A(
)r   r  r	  r
  r   r,  re   r$  r  MAX_PAINT_COLR_LAYER_COUNTr   r5   rJ   r   rN   r  r   rg   rt   rf   extendr   )r   destr.   rt   r   is_treer-   r-  s   `      @r0   r&  z,LayerListBuilder._beforeBuildPaintColrLayers#  sz    fjjd3[__5M5MNK(FH%F /55f$//!$f5 v;!!9b= ::! ZZ))&1F f+ ::!&,FG	 066v!"1%v6 v;!!9b= 
2>>99:f+ #DKK 06" ::!'JJNN65#8#89 by[ 66 7s   F(	F-r-   r   c                 V    | j                   j                  t        j                  |      S r4   )r'  buildr5   rJ   )r   r-   s     r0   r,  zLayerListBuilder.buildPaint[  s      &&rxx77r2   c                     | j                   sy t        j                         }t        | j                         |_        | j                   |_        |S r4   )rt   r5   r   re   
LayerCountrJ   )r   rt   s     r0   r3  zLayerListBuilder.build^  s8    {{,{{r2   N)r   r   r   r
   r5   rJ   r!  r   boolr   r&  r!   r,  r   r   r3  rT   r2   r0   r#  r#  
  sV    N*. 4(6p8 8 8x- r2   r#  rs   layerBuilderr-   c                 h    t        j                         }| |_        |j                  |      |_        |S r4   )r5   BaseGlyphPaintRecordrd   r,  rJ   )rs   r7  r-   r   s       r0   buildBaseGlyphPaintRecordr:  g  s0     ""$DDN((/DJKr2   errorsc           	          g }t        | j                               D ]2  \  }}|j                  d| dt        |      j                   d|        4 dj                  |      S )Nz    z => z: 
)ra   rb   rh   typer   join)r;  linesrs   errors       r0   _format_glyph_errorsrB  p  sZ    E"6<<>2	5tI;d4;+?+?*@5'JK 399Ur2   r   c                2   t        | j                         fd      }n| j                         }i }g }t        |      }|D ]"  \  }}	 |j                  t	        |||             $ |rCt        |      }
t        d|
       }||_
        |t        t        |j                                     |j                         }t        j                          }t#        |      |_        ||_        ||fS # t
        t        t        t        f$ r}	|	||<   Y d }	~	d }	~	ww xY w)Nc                     | d      S r[   rT   r\   s    r0   r^   zbuildColrV1.<locals>.<lambda>  s    (472Cr2   r_   r   zFailed to build BaseGlyphList:
)ra   rb   r#  rh   r:  r   OverflowErrorr   r   rB  r;  nextitervaluesr3  r5   r   re   BaseGlyphCountr9  )r~   rX   r}   rp   r;  
baseGlyphsr7  rs   r-   r  failed_glyphsexcrt   glyphss    `            r0   r   r   w  s     %C
 &++-FJ#ODL+	5	"7	<QVWX , ,V4>}oNO
tD122!FF
OF",FF }j)D 	" !F9	"s   C..DDDr4   )NNNN)^__doc__r	  copyr  	functoolsr   mathr   r   typingr   r   r   r	   r
   r   r   r   r   r   r   r   fontTools.misc.arrayToolsr   fontTools.misc.fixedToolsr   fontTools.misc.treeToolsr   fontTools.ttLib.tablesr   r   r   r   r5   fontTools.ttLib.tables.otTablesr   r   r;  r   geometryr   table_builderr   r   r    r   _Kwargsr   rJ   r!   _PaintInputList_ColorGlyphsDict_ColorGlyphsV0Dictr   _ClipBoxInputr.  r7   r   r1   r;   r>   rE   rH   rK   rU   r   ry   r   DeltaSetIndexMapr6  r   r   r   r   r   IntFlagr   _OptionalLocalizedStringtable__n_a_m_er   floatr   r   r   r   r   r   r#  r   r:  	ExceptionrB  r   r   rT   r2   r0   <module>re     sw  
         . 2 5 + + + 1 E ! ; 6 CL
#s(
C"((E#}2D,EEF;'U?K#?@@A #xc3h889 	#sCc
!"	#sC
JJ ! 4@ -1+/77+/%+/ wsCx()+/` "F -1&*1548 F!Fc]F wsCx()	F
 r{{#F "--.F S-/01F F FRT#}"45 "++ - BJJ 	(t|| 	( !sDcN!:; -.;C;R;R	(3-& :>BFGK37[xeUE5&@ ABC[8$456[ H%=>?[ !*B!CD	[
 //0[ [D
MC 
MD 
M(!(
//0("	#c 	#ic3ht0K&L 	#@ @FZ Zz"2;Fi!8 S  -1! !	!!!wsCx()! 	!
 8BLL!2#3#334!r2   