
    'je                       d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZ d dlZd ZdZd	Zd
ZdZd ZdZd	Zd
ZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'd	Z(dZ)d Z*d Z+dZ,dZ-dZ.dZ/g dZ0dZ1dZ2dZ3dZ4dKd$Z5dLd(Z6 G d) d*          Z7 G d+ d,          Z8 G d- d.e8          Z9 G d/ d0e8          Z:dMd3Z;dNd4Z<dOd8Z=dPd9Z>dQd:Z?dRd<Z@dSd>ZA G d? d@          ZBdTdAZCdUdDZDe3fdVdHZEdWdJZFdS )X    )annotations)UnionOptionalBinaryIOTextIOIterableIteratorAnyN)abstractmethod)StringIO)array)pack               @   A   B   C   D   E   F   G   H   I      i   )        g?g
ףp=
?g?gp=
ף?g333333?g
ףp=
?g?g      ?g333333?gffffff?g?g?      ?g(\?g333333?g?gffffff?g?g?      ?g(\?g333333?gffffff?gHzG?g       @gzG @            indexintreturnstrc                    d| dz   z  S )NzColor_%dr    r(   s    N/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/acadctb.py
color_namer0   f   s    ##    valueUnion[str, bool]boolc                    t          | t                    rE|                                 }|dk    rd} n(|dk    rd} nt          dt          |           z            | S )NTRUETFALSEFzUnknown bool value '%s'.)
isinstancer+   upper
ValueError)r2   upperstrs     r/   get_boolr<   j   se    % F;;==vEE  EE7#e**DEEELr1   c                  |   e Zd Z	 	 d%d&dZed'd            Zej        d(d            Zed             Zej        d)d            Zd*dZd+dZ	d,dZ
d-dZed.d            Zed-d            Zej        d/d            Zed-d            Zej        d/d            Zed-d             Zej        d/d!            Zd0d$ZdS )1	PlotStyleNr(   r)   dataOptional[dict]parentOptional[PlotStyleTable]c                   |pi }|| _         t          |          | _        t          |                    dt          |                              | _        t          |                    dt          |                              | _        t          |                    dd                    | _        t          |                    dt                              | _
        t          | _        | j
        t          k    r-t          |                    d| j
                            | _        t          |                    dt                              | _        t          |                    dt                               | _        t          |                    d	t                               | _        t          |                    d
d                    | _        t)          |                    dd                    | _        t          |                    dt,                              | _        t1          |                    dd                    | _        t          |                    dt4                              | _        t          |                    dt8                              | _        t          |                    dt<                              | _        t          |                    dt@                              | _!        d S )Nnamelocalized_namedescription color
mode_colorcolor_policyphysical_pen_numbervirtual_pen_numberscreend   linepattern_sizer"   linetypeadaptive_linetypeT
lineweight	end_style
join_style
fill_style)"rA   r)   r(   r+   getr0   rD   rE   rF   OBJECT_COLOR_color	COLOR_RGB_color_type_mode_colorDITHERING_ON_color_policy	AUTOMATICrK   rL   rM   floatrO   OBJECT_LINETYPErP   r<   rQ   OBJECT_LINEWEIGHTrR   END_STYLE_OBJECTrS   JOIN_STYLE_OBJECTrT   FILL_STYLE_OBJECTrU   )selfr(   r?   rA   s       r/   __init__zPlotStyle.__init__w   s1    zrZZ
E):):;;<<	!$((+;Z=N=N"O"OPPtxxr::;; $((7L99::$;,&&"488L$+#F#FGGD .,!G!GHH#&txx0Ey'Q'Q#R#R "%dhh/CY&O&O"P"P$((8S1122 %dhh/A3&G&G H HDHHZAABB!)$((3F*M*M!N!N dhh|5FGGHHTXXk3CDDEEdhh|5FGGHHdhh|5FGGHHr1   r*   Optional[tuple[int, int, int]]c                f    |                                  rdS t          | j                  dd         S )z_Get style color as ``(r, g, b)`` tuple or ``None``, if style has
        object color.
        Nr   )has_object_color	int2colorr[   re   s    r/   rH   zPlotStyle.color   s7    
   "" 	34T-..rr22r1   rgbtuple[int, int, int]Nonec                d    |\  }}}t          |||| j                  | _        | j        | _        dS )zSet color as RGB values.)
color_typeN)mode_color2intrZ   r[   rX   )re   rl   rgbs        r/   rH   zPlotStyle.color   s;     1a *!Qd>NOOO&r1   c                <    |                                  rd S | j        S N)ri   rZ   rk   s    r/   rp   zPlotStyle.color_type   s$      "" 	$4##r1   r2   c                    || _         d S rv   )rZ   )re   r2   s     r/   rp   zPlotStyle.color_type   s     r1   c                6    t           | _        t           | _        dS )zSet color to object color.N)rW   rX   r[   rk   s    r/   set_object_colorzPlotStyle.set_object_color   s    "'r1   rR   r_   c                V    | j         J | j                             |          | _        dS )zZSet `lineweight` in millimeters. Use ``0.0`` to set lineweight by
        object.
        N)rA   get_lineweight_indexrR   re   rR   s     r/   set_lineweightzPlotStyle.set_lineweight   s-     {&&&+:::FFr1   c                B    | j         J | j         j        | j                 S )zZReturns the lineweight in millimeters or `0.0` for use entity
        lineweight.
        )rA   lineweightsrR   rk   s    r/   get_lineweightzPlotStyle.get_lineweight   s$     {&&&{&t77r1   r4   c                ,    | j         t          t          fv S )z#``True`` if style has object color.)rX   rW   OBJECT_COLOR2rk   s    r/   ri   zPlotStyle.has_object_color   s    {|];;;r1   c                    | j         dz   S )zg:ref:`ACI` in range from ``1`` to ``255``. Has no meaning for named
        plot styles. (int)
        r   r.   rk   s    r/   acizPlotStyle.aci   s    
 zA~r1   c                :    t          | j        t          z            S )uy  Depending on the capabilities of your plotter, dithering approximates
        the colors with dot patterns. When this option is ``False``, the colors
        are mapped to the nearest color, resulting in a smaller range of
        colors when plotting.

        Dithering is available only whether you select the object’s color or
        assign a plot style color.

        )r4   r]   r\   rk   s    r/   	ditheringzPlotStyle.dithering   s     D&5666r1   statusc                d    |r| xj         t          z  c_         d S | xj         t           z  c_         d S rv   )r]   r\   re   r   s     r/   r   zPlotStyle.dithering   @     	0,.<-/r1   c                :    t          | j        t          z            S )z Plot colors in grayscale. (bool))r4   r]   GRAYSCALE_ONrk   s    r/   	grayscalezPlotStyle.grayscale   s     D&5666r1   c                d    |r| xj         t          z  c_         d S | xj         t           z  c_         d S rv   )r]   r   r   s     r/   r   zPlotStyle.grayscale   r   r1   c                :    t          | j        t          z            S rv   )r4   r]   NAMED_COLORrk   s    r/   named_colorzPlotStyle.named_color   s    D&4555r1   c                d    |r| xj         t          z  c_         d S | xj         t           z  c_         d S rv   )r]   r   r   s     r/   r   zPlotStyle.named_color   s@     	/+-;,.r1   streamr   c                   | j         }|                    d|z             |                    d| j        z             |                    d| j        z             |                    d| j        z             |                    d| j        z             | j        t          k    r|                    d| j        z             |                    d| j        z             |                    d| j	        z             |                    d	| j
        z             |                    d
| j        z             |                    dt          | j                  z             |                    d| j        z             |                    dt          t          | j                                                            z             |                    dt          | j                  z             |                    d| j        z             |                    d| j        z             |                    d| j        z             |                    d           dS )z.Write style data to file-like object `stream`.z %d{
z  name="%s
z  localized_name="%s
z  description="%s
z  color=%d
z  mode_color=%d
z  color_policy=%d
z  physical_pen_number=%d
z  virtual_pen_number=%d
z  screen=%d
z  linepattern_size=%s
z  linetype=%d
z  adaptive_linetype=%s
z  lineweight=%s
z  fill_style=%d
z  end_style=%d
z  join_style=%d
z }
N)r(   writerD   rE   rF   rX   rW   r[   r]   rK   rL   rM   r+   rO   rP   r4   rQ   r9   rR   rU   rS   rT   )re   r   r(   s      r/   r   zPlotStyle.write  s   
X%&&&^di/000-0CCDDD*T-==>>>^dk1222;,&&LL,t/??@@@*T-??@@@1D4LLMMM043JJKKK_t{2333.T5J1K1KKLLL&6777&T$2H-I-I)J)J)P)P)R)RR	
 	
 	
 	(3t+?+??@@@(4?:;;;'$.8999(4?:;;;Vr1   NN)r(   r)   r?   r@   rA   rB   )r*   rg   )rl   rm   r*   rn   )r2   r)   )r*   rn   )rR   r_   r*   rn   )r*   r_   )r*   r4   )r*   r)   )r   r4   r*   rn   r   r   r*   rn   )__name__
__module____qualname__rf   propertyrH   setterrp   ry   r}   r   ri   r   r   r   r   r   r-   r1   r/   r>   r>   v   s         $+/	I I I I I@ 3 3 3 X3 \
' 
' 
' \
' $ $ X$ ! ! ! !( ( ( (
G G G G8 8 8 8< < < <    X 
7 
7 
7 X
7 0 0 0 0 7 7 7 X7 0 0 0 0 6 6 6 X6 / / / /     r1   r>   c                      e Zd ZdZ	 	 	 d!d"dZd#dZd$dZd%dZd&dZd'dZ	e
d(d            Zd(dZd)dZe
d             Zd S )*PlotStyleTablezPlotStyle containerrG   r#   FrF   r+   scale_factorr_   apply_factorr4   c                r    || _         || _        || _        d| _        t	          dt
                    | _        d S )Nr   f)rF   r   r   custom_lineweight_display_unitsr   DEFAULT_LINE_WEIGHTSr   )re   rF   r   r   s       r/   rf   zPlotStyleTable.__init__  s?     '(( 01, &:;;r1   rR   r*   r)   c                    	 | j                             |          S # t          $ r4 | j                             |           t	          | j                   dz
  cY S w xY w)znGet index of `lineweight` in the lineweight table or append
        `lineweight` to lineweight table.
        r   )r   r(   r:   appendlenr|   s     r/   r{   z#PlotStyleTable.get_lineweight_index.  sl    	-#))*555 	- 	- 	-##J///t'((1,,,,	-s    ;AAr(   c                    	 || j         |<   |S # t          $ r4 | j                             |           t          | j                   dz
  cY S w xY w)zArgument `index` is the lineweight table index, not the :ref:`ACI`.

        Args:
            index: lineweight table index = :attr:`PlotStyle.lineweight`
            lineweight: in millimeters

        r   )r   
IndexErrorr   r   )re   r(   rR   s      r/   set_table_lineweightz#PlotStyleTable.set_table_lineweight8  sg    	-&0DU#L 	- 	- 	-##J///t'((1,,,,	-s    ;AAc                    | j         |         S )zReturns lineweight in millimeters of lineweight table entry `index`.

        Args:
            index: lineweight table index = :attr:`PlotStyle.lineweight`

        Returns:
            lineweight in mm or ``0.0`` for use entity lineweight

        )r   )re   r(   s     r/   get_table_lineweightz#PlotStyleTable.get_table_lineweightG  s     &&r1   filenamestr | os.PathLikern   c                    t          |d          5 }|                     |           ddd           dS # 1 swxY w Y   dS )z6Save CTB or STB file as `filename` to the file system.wbN)openr   )re   r   r   s      r/   savezPlotStyleTable.saveS  s    (D!! 	VJJv	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   488r   r   c                    t                      }|                     |           |                    t          d                     |                                }|                                 t          ||           dS )z:Compress and write the CTB or STB file to binary `stream`.r   N)r   write_contentr   chrgetvalueclose	_compress)re   r   memfilebodys       r/   r   zPlotStyleTable.writeX  sk    **7###c!ff!!&$r1   r   c                    d S rv   r-   re   r   s     r/   r   zPlotStyleTable.write_contenta      r1   c                    |                     d           t          | j                  D ]\  }}|                     d||fz              |                     d           dS )z/Write custom lineweight table to text `stream`.zcustom_lineweight_table{
z	 %d=%.2f
}
N)r   	enumerater   )re   r   r(   weights       r/   _write_lineweightsz!PlotStyleTable._write_lineweightse  si    1222&t'788 	9 	9ME6LL78888Ur1   textc                     fd}t          |          }|                    dd           _        t          |                    dd                     _        t          |                    dd                     _        t          |                    dd	                     _         ||                    d
d                      	                    |                    di                      dS )z)Parse plot styles from CTB string `text`.c                    | d S t          ddgt          |           z            _        |                                 D ])\  }}t	          |          j        t          |          <   *d S )Nr   r!   )r   r   r   itemsr_   r)   )r   keyr2   re   s      r/   set_lineweightsz-PlotStyleTable.parse.<locals>.set_lineweightso  st    "$S3%#k2B2B*BCCD)//11 : :
U-25\\ S**: :r1   rF   rG   r   r#   r   Tr   r   custom_lineweight_tableN
plot_style)
PlotStyleFileParserrV   rF   r_   r   r<   r   r)   r   load_styles)re   r   r   parsers   `   r/   parsezPlotStyleTable.parsel  s    	: 	: 	: 	: 	: %T**!::mR88!&**^S"A"ABB$VZZ%E%EFF/2JJ8!<<0
 0
, 	

#<dCCDDDL"5566666r1   c                    d S rv   r-   )re   styless     r/   r   zPlotStyleTable.load_styles  r   r1   NrG   r#   FrF   r+   r   r_   r   r4   )rR   r_   r*   r)   )r(   r)   rR   r_   r*   r)   )r(   r)   r*   r_   )r   r   r*   rn   )r   r   r*   rn   r   )r   r+   r*   rn   )r   r   r   __doc__rf   r{   r   r   r   r   r   r   r   r   r   r-   r1   r/   r   r     s        !"	< < < < <- - - -- - - -
' 
' 
' 
'   
           ^   7 7 7 7(   ^  r1   r   c                  v     e Zd Z	 	 	 d d! fd
Zd"dZd#dZd Zd$d%dZd&dZd'dZ	d'dZ
d'dZd'dZd Z xZS )(ColorDependentPlotStylesrG   r#   FrF   r+   r   r_   r   r4   c                     t                                          |||            fdt          t                    D              _         j                            dt          d                     d S )Nc                2    g | ]}t          |           S ))rA   )r>   ).0r(   re   s     r/   
<listcomp>z5ColorDependentPlotStyles.__init__.<locals>.<listcomp>  s3     )
 )
 )
.3IeD))))
 )
 )
r1   r      )superrf   rangeSTYLE_COUNT_stylesinsertr>   )re   rF   r   r   	__class__s   `   r/   rf   z!ColorDependentPlotStyles.__init__  s     	lLAAA)
 )
 )
 )
7<[7I7I)
 )
 )
 	y~~	
 	
 	
 	
 	
r1   r   r)   r*   r>   c                Z    d|cxk     rdk     rn n| j         |         S t          |          )z0Returns :class:`PlotStyle` for :ref:`ACI` `aci`.r   r   )r   r   )re   r   s     r/   __getitem__z$ColorDependentPlotStyles.__getitem__  s4    s====S=====<$$S//!r1   stylec                f    d|cxk     rdk     rn n| |_         || j        |<   dS t          |          )zSet plot `style` for `aci`.r   r   N)rA   r   r   )re   r   r   s      r/   __setitem__z$ColorDependentPlotStyles.__setitem__  s@    s====S=====EL %DLS//!r1   c                :    t          | j        dd                   S )zIterable of all plot styles.r   N)iterr   rk   s    r/   __iter__z!ColorDependentPlotStyles.__iter__  s    DL$%%%r1   Nr?   r@   c                P    t          |dz
  |          }t          |_        || |<   |S )aw  Set `aci` to new attributes defined by `data` dict.

        Args:
            aci: :ref:`ACI`
            data: ``dict`` of :class:`PlotStyle` attributes: description, color,
                physical_pen_number, virtual_pen_number, screen,
                linepattern_size, linetype, adaptive_linetype,
                lineweight, end_style, join_style, fill_style

        r   )r(   r?   )r>   rY   rp   )re   r   r?   r   s       r/   	new_stylez"ColorDependentPlotStyles.new_style  s0     ad333$S	r1   c                N    | |         }|                                 }|dk    rdS |S )z\Returns the assigned lineweight for :class:`PlotStyle` `aci` in
        millimeter.
        r!   Nr   )re   r   r   rR   s       r/   r   z'ColorDependentPlotStyles.get_lineweight  s4     S	))++
4r1   r   r   rn   c                    |                      |           |                     |           |                     |           |                     |           dS )z$Write the CTB-file to text `stream`.N)_write_header_write_aci_table_write_plot_stylesr   r   s     r/   r   z&ColorDependentPlotStyles.write_content  sX    6"""f%%%''''''''r1   c                p   |                     d| j        z             |                     d           |                     d| j        z             |                     dt          | j                                                  z             |                     dt          | j                  z             dS )1Write header values of CTB-file to text `stream`.description="%s
zaci_table_available=TRUE
scale_factor=%.1f
apply_factor=%s
#custom_lineweight_display_units=%s
Nr   rF   r   r+   r   r9   r   r   s     r/   r   z&ColorDependentPlotStyles._write_header  s    (4+;;<<<1222*T->>???(3t/@+A+A+G+G+I+IIJJJ2$6778	
 	
 	
 	
 	
r1   c                    |                     d           | D ]0}|j        }|                     d|t          |          fz             1|                     d           dS )z1Write AutoCAD Color Index table to text `stream`.zaci_table{
z %d="%s
r   N)r   r(   r0   )re   r   r   r(   s       r/   r   z)ColorDependentPlotStyles._write_aci_table  sk    ^$$$ 	C 	CEKELLz%/@/@'AABBBBUr1   c                    |                     d           | D ]}|                     |           |                     d           dS z#Write user styles to text `stream`.zplot_style{
r   N)r   )re   r   r   s      r/   r   z+ColorDependentPlotStyles._write_plot_styles  sP    _%%% 	  	 EKKUr1   c                    |                                 D ]:\  }}t          |          }t          ||          }t          |_        |dz   }|| |<   ;d S )Nr   )r   r)   r>   rY   rp   )re   r   r(   r   r   s        r/   r   z$ColorDependentPlotStyles.load_styles  s]    "LLNN 	 	LE5JJEeU++E(E!)CDII	 	r1   r   r   )r   r)   r*   r>   )r   r)   r   r>   rv   )r   r)   r?   r@   r*   r>   )r   r)   r   )r   r   r   rf   r   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r/   r   r     s        !"	
 
 
 
 
 
 
" " " "" " " "& & &    $	 	 	 	( ( ( (	
 	
 	
 	
            r1   r   c                       e Zd Z	 	 	 d%d& fd
Zd'dZd(dZd)dZd'dZd*dZd+dZ		 	 d,d-dZ
d.dZd/d!Zd/d"Zd/d#Zd$ Z xZS )0NamedPlotStylesrG   r#   FrF   r+   r   r_   r   r4   c                    t                                          |||           t          dddd          }d|i| _        d S )Nr   Normal)rD   rE   )r?   )r   rf   r>   r   )re   rF   r   r   normalr   s        r/   rf   zNamedPlotStyles.__init__  sZ     	lLAAA "* 
 
 
 /7-?r1   r*   Iterable[str]c                *    |                                  S )!Iterable of all plot style names.)keysrk   s    r/   r   zNamedPlotStyles.__iter__  s    yy{{r1   rD   r>   c                    | j         |         S )z%Returns :class:`PlotStyle` by `name`.)r   re   rD   s     r/   r   zNamedPlotStyles.__getitem__  s    |D!!r1   rn   c                @    |dk    r
| j         |= dS t          d          )zCDelete plot style `name`. Plot style ``'Normal'`` is not deletable.r  z"Can't delete plot style 'Normal'. N)r   r:   r  s     r/   __delitem__zNamedPlotStyles.__delitem__  s,    8T"""ABBBr1   c                    t          | j                                                  }|                    d           dg}|                    t          |                     t          |          S )r  r  )setr   r  discardextendsortedr   )re   r  results      r/   r  zNamedPlotStyles.keys  sZ    4<$$&&''XfTll###F||r1   Iterator[tuple[str, PlotStyle]]c              #  Z   K   |                                  D ]}|| j        |         fV  dS )zDIterable of all plot styles as (``name``, class:`PlotStyle`) tuples.N)r  r   )re   r   s     r/   r   zNamedPlotStyles.items  sB      99;; 	) 	)Ct|C((((((	) 	)r1   Iterable[PlotStyle]c              #  F   K   |                                  D ]	\  }}|V  
dS )z*Iterable of all class:`PlotStyle` objects.N)r   )re   r   r2   s      r/   valueszNamedPlotStyles.values  s6      **,, 	 	JCKKKK	 	r1   Nr?   r@   rE   Optional[str]c                    |                                 dk    rt          d          |pi }||d<   |p||d<   t          | j                  }t	          |||           }t
          |_        d|_        || j        |<   |S )a  Create new class:`PlotStyle` `name` by attribute dict `data`, replaces
        existing class:`PlotStyle` objects.

        Args:
            name: plot style name
            localized_name: name shown in plot style editor, uses `name` if ``None``
            data: ``dict`` of :class:`PlotStyle` attributes: description, color,
                physical_pen_number, virtual_pen_number, screen,
                linepattern_size, linetype, adaptive_linetype, lineweight,
                end_style, join_style, fill_style

        r  z-Can't replace or modify plot style 'Normal'. rD   rE   )r(   r?   rA   T)lowerr:   r   r   r>   	COLOR_ACIrp   r   )re   rD   r?   rE   r(   r   s         r/   r   zNamedPlotStyles.new_style   s    $ ::<<8##LMMMzrV!/!74DL!!D>>>$ "Tr1   c                N    | |         }|                                 }|dk    rdS |S )z]Returns the assigned lineweight for :class:`PlotStyle` `name` in
        millimeter.
        r!   Nr   )re   rD   r   rR   s       r/   r   zNamedPlotStyles.get_lineweight>  s4     T
))++
4r1   r   r   c                    |                      |           |                     |           |                     |           dS )z$Write the STB-file to text `stream`.N)r   r   r   r   s     r/   r   zNamedPlotStyles.write_contentI  sD    6"""''''''''r1   c                p   |                     d| j        z             |                     d           |                     d| j        z             |                     dt          | j                                                  z             |                     dt          | j                  z             dS )r   r   zaci_table_available=FALSE
r   r   r   Nr   r   s     r/   r   zNamedPlotStyles._write_headerO  s    (4+;;<<<2333*T->>???(3t/@+A+A+G+G+I+IIJJJ2$6778	
 	
 	
 	
 	
r1   c                    |                     d           t          |                                           D ]!\  }}||_        |                     |           "|                     d           dS r   )r   r   r  r(   )re   r   r(   r   s       r/   r   z"NamedPlotStyles._write_plot_stylesZ  sk    _%%%%dkkmm44 	  	 LE5EKKKUr1   c                    |                                 D ]?\  }}t          |          }t          ||          }t          |_        || j        |j        <   @d S rv   )r   r)   r>   r  rp   r   rD   )re   r   r(   r   s       r/   r   zNamedPlotStyles.load_stylesb  sZ    "LLNN 	- 	-LE5JJEeU++E(E',DL$$		- 	-r1   r   r   )r*   r  )rD   r+   r*   r>   )rD   r+   r*   rn   )r*   r  )r*   r  r   )rD   r+   r?   r@   rE   r  r*   r>   )rD   r+   r   )r   r   r   rf   r   r   r
  r  r   r  r   r   r   r   r   r   r   r   s   @r/   r   r     sP        !"	@ @ @ @ @ @ @    " " " "C C C C   ) ) ) )
     $(,	    <	 	 	 	( ( ( (	
 	
 	
 	
   - - - - - - -r1   r   r   r   c                    t          |           }t                      }|                    |                                           |S )z%Read a CTB-file from binary `stream`.)_decompressr   r   decoder   contentr   s      r/   	_read_ctbr$  j  s;     ((G%''F
LL!!"""Mr1   c                    t          |           }t                      }|                    |                                           |S )z%Read a STB-file from binary `stream`.)r   r   r   r!  r"  s      r/   	_read_stbr&  r  s;     ((GF
LL!!"""Mr1   r   r   0Union[ColorDependentPlotStyles, NamedPlotStyles]c                   t          |           } t          | d          5 }|                                                     d          rt	          |          cddd           S |                                                     d          rt          |          cddd           S t          d                    |                     # 1 swxY w Y   dS )z5Load the CTB or STB file `filename` from file system.rbz.ctbNz.stbzInvalid file type: "{}")r+   r   r  endswithr$  r&  r:   format)r   r   s     r/   loadr,  z  s<    8}}H	h		 I>>$$V,, 	IV$$I I I I I I I I ^^&&v.. 	IV$$	I I I I I I I I 6==hGGHHHI I I I I I I I I Is   6C#5C%"CCCc                     t                      S )zCreate a new CTB file.)r   r-   r1   r/   new_ctbr.    s    #%%%r1   c                     t                      S )zCreate a new STB file.)r   r-   r1   r/   new_stbr0    s    r1   bytesc                v    |                                  }t          j        |dd                   }|dd         S )z:Read and decompress the file content from binray `stream`.<   Nr   )readzlib
decompress)r   r#  r?   s      r/   r   r     s4    kkmmG?7233<((D9r1   r#  c           
     L   t          j        |                                          }t          j        |          }|                     d           |                     t          d|t          |          t          |                               |                     |           dS )z0Compress `content` and write to binary `stream`.s0   PIAFILEVERSION_2.0,CTBVER1,compress
pmzlibcodecLLLN)r5  compressencodeadler32r   r   r   )r   r#  	comp_bodyadler_chksums       r/   r   r     s    gnn..//I<	**L
LLFGGG
LLe\3w<<YHHIII
LLr1   c                  :    e Zd ZdZddZedd            Zdd
ZdS )r   zA very simple CTB/STB file parser. CTB/STB files are created by
    applications, so the file structure should be correct in the most cases.
    r   r+   c                h    i | _         t                              |          D ]\  }}|| j         |<   d S rv   )r?   r   	iteritems)re   r   elementr2   s       r/   rf   zPlotStyleFileParser.__init__  sC    	1;;DAA 	' 	'NGU!&DIg	' 	'r1   c              #    K   ddfddfddfdfd	}|                      d
          t                    k     r9             }             }||fV   |             t                    k     7dS dS )z7Iterate over all first level (start at col 0) elements.r   r*   r+   c                              } |                      d          r| dd         }n|                     dd          d         }|                                S )z&Get element name of line <line_index>.{Nr   =r   r   )r*  splitstrip)linerD   
line_indexliness     r/   get_namez/PlotStyleFileParser.iteritems.<locals>.get_name  sV    $D}}S!! -CRCyzz#q))!,::<<r1   dictc                     fd} t                      } |             s#             }             }|||<    |             #dz  |S )zmGet mapping of elements enclosed by { }.

            e. g. lineweights, plot_styles, aci_table

            c                 :                                   d          S )N})r*  rI  rJ  s   r/   end_of_listzGPlotStyleFileParser.iteritems.<locals>.get_mapping.<locals>.end_of_list  s    Z(11#666r1   r   )rL  )rQ  r?   rD   r2   rK  	get_valuerI  rJ  s       r/   get_mappingz2PlotStyleFileParser.iteritems.<locals>.get_mapping  sy    7 7 7 7 7 7 66D!kmm #xzz!	"T
 "kmm # !OJKr1   Union[str, dict]c                              } |                      d          rdz               S |                     dd          d         }t          |          }dz  |S )zeGet value of line <line_index> or the list that starts in line
            <line_index>.
            rD  r   rE  )r*  rF  sanitized_value)rH  r2   rS  rI  rJ  s     r/   rR  z0PlotStyleFileParser.iteritems.<locals>.get_value  sk    
 $D}}S!!  a
"{}}$!ZZQ//2'..a
Lr1   c                      t                    k     rLt                              dk    r7 dz    t                    k     rt                              dk    1d S d S d S d S )Nr   r   )r   rP  s   r/   skip_empty_linesz7PlotStyleFileParser.iteritems.<locals>.skip_empty_lines  sv    s5zz))c%
2C.D.D.I.Ia
 s5zz))c%
2C.D.D.I.I.I.I)))).I.Ir1   
N)r*   r+   )r*   rL  )r*   rT  )rF  r   )	r   rX  rD   r2   rS  rK  rR  rI  rJ  s	       @@@@@r/   r@  zPlotStyleFileParser.iteritems  s       
	  	  	  	  	  	  	 	 	 	 	 	 	 	 	 	&	 	 	 	 	 	 	 		  	  	  	  	  	 
 

4  3u::%%8::DIKKE+	 3u::%%%%%%r1   rD   defaultr
   r*   c                8    | j                             ||          S rv   )r?   rV   )re   rD   rZ  s      r/   rV   zPlotStyleFileParser.get  s    y}}T7+++r1   N)r   r+   )rD   r+   rZ  r
   r*   r
   )r   r   r   r   rf   staticmethodr@  rV   r-   r1   r/   r   r     sg         ' ' ' '
 9 9 9 \9v, , , , , ,r1   r   c                    |                                  } |                     d          r
| dd          S |                     d          r|                     d          d         S | S )N"r   ) r   )rG  
startswithr*  rF  )r2   s    r/   rV  rV    se    KKMME QRRy ~~c #{{3""Lr1   rH   tuple[int, int, int, int]c                H    | dz  dz	  }| dz  dz	  }| dz  dz	  }| dz  }||||fS )zTConvert color integer value from CTB-file to ``(r, g, b, color_type)
    tuple.
    l      ~    i      i      r    r-   )rH   rp   redgreenblues        r/   rj   rj     sH    
 *$+J8
"CV^!E4<DtZ''r1   rg  rh  ri  c                (    t          | |||           S )z:Convert mode_color (r, g, b, color_type) tuple to integer.)	color2intrg  rh  ri  rp   s       r/   rq   rq     s    c5$
3333r1   rp   c                2    |dz  | dz  z   |dz  z   |z    dz  S )z/Convert color (r, g, b, color_type) to integer.rd  re  rf  l    r-   rl  s       r/   rk  rk  	  s,    B3"9-!<tCDzQQr1   )r(   r)   r*   r+   )r2   r3   r*   r4   )r   r   r*   r   )r   r   r*   r   )r   r   r*   r'  )r*   r   )r*   r   )r   r   r*   r1  )r   r   r#  r+   )r2   r+   r*   r+   )rH   r)   r*   rb  )rg  r)   rh  r)   ri  r)   r*   r)   )
rg  r)   rh  r)   ri  r)   rp   r)   r*   r)   )G
__future__r   typingr   r   r   r   r   r	   r
   osabcr   ior   r   structr   r5  END_STYLE_BUTTEND_STYLE_SQUAREEND_STYLE_ROUNDEND_STYLE_DIAMONDrb   JOIN_STYLE_MITERJOIN_STYLE_BEVELJOIN_STYLE_ROUNDJOIN_STYLE_DIAMONDrc   FILL_STYLE_SOLIDFILL_STYLE_CHECKERBOARDFILL_STYLE_CROSSHATCHFILL_STYLE_DIAMONDSFILL_STYLE_HORIZONTAL_BARSFILL_STYLE_SLANT_LEFTFILL_STYLE_SLANT_RIGHTFILL_STYLE_SQUARE_DOTSFILL_STYLE_VERICAL_BARSrd   r\   r   r   r^   ra   r`   rW   r   r   r   COLOR_BY_LAYERCOLOR_BY_BLOCKrY   r  r0   r<   r>   r   r   r   r$  r&  r,  r.  r0  r   r   r   rV  rj   rq   rk  r-   r1   r/   <module>r     s  
 # " " " " "                  
			                                           	    B   	 	$ $ $ $	 	 	 	c c c c c c c cLf f f f f f f fRf f f f f~ f f fRy- y- y- y- y-n y- y- y-x      I I I I& & & &
   
      G, G, G, G, G, G, G, G,T	 	 	 		( 	( 	( 	( @I 4 4 4 4 4
R R R R R Rr1   