
    Oge                       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Z4d7d Z5d8d!Z6 G d" d#      Z7 G d$ d%      Z8 G d& d'e8      Z9 G d( d)e8      Z:d9d*Z;d:d+Z<	 	 	 	 d;d,Z=d<d-Z>d=d.Z?d>d/Z@d?d0ZA G d1 d2      ZBd@d3ZCdAd4ZDe3fdBd5ZEdCd6ZFy)D    )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 @            c                    d| dz   z  S )NzColor_%dr    indexs    Y/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/addons/acadctb.py
color_namer-   f   s    ##    c                    t        | t              r9| j                         }|dk(  rd} | S |dk(  rd} | S t        dt        |       z        | S )NTRUETFALSEFzUnknown bool value '%s'.)
isinstancestrupper
ValueError)valueupperstrs     r,   get_boolr8   j   s]    %;;=vE
 L	  E L 7#e*DEELr.   c                  t   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ddZy)	PlotStyleNc                   |xs i }|| _         t        |      | _        t        |j	                  dt        |                  | _        t        |j	                  dt        |                  | _        t        |j	                  dd            | _        t        |j	                  dt                    | _
        t        | _        | j                  t        k7  r*t        |j	                  d| j                              | _        t        |j	                  dt                    | _        t        |j	                  dt                     | _        t        |j	                  d	t                     | _        t        |j	                  d
d            | _        t)        |j	                  dd            | _        t        |j	                  dt,                    | _        t1        |j	                  dd            | _        t        |j	                  dt4                    | _        t        |j	                  dt8                    | _        t        |j	                  dt<                    | _        t        |j	                  dt@                    | _!        y )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)"parentintr+   r3   getr-   r<   r=   r>   OBJECT_COLOR_color	COLOR_RGB_color_type_mode_colorDITHERING_ON_color_policy	AUTOMATICrC   rD   rE   floatrG   OBJECT_LINETYPErH   r8   rI   OBJECT_LINEWEIGHTrJ   END_STYLE_OBJECTrK   JOIN_STYLE_OBJECTrL   FILL_STYLE_OBJECTrM   )selfr+   datarN   s       r,   __init__zPlotStyle.__init__w   s    zrZ
E):;<	!$((+;Z=N"OPtxxr:; $((7L9:$;;,&"488L$++#FGD .,!GH#&txx0Ey'Q#R "%dhh/CY&O"P$((8S12 %dhh/A3&G HDHHZAB!)$((3F*M!N dhh|5FGHTXXk3CDEdhh|5FGHdhh|5FGHr.   c                T    | j                         ry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	int2colorrU   r_   s    r,   r@   zPlotStyle.color   s+    
   "T--.r22r.   c                n    |\  }}}t        |||| j                        | _        | j                  | _        y)zSet color as RGB values.)
color_typeN)mode_color2intrT   rU   rR   )r_   rgbrgbs        r,   r@   zPlotStyle.color   s7     1a *!Qd>N>NO&&r.   c                <    | j                         ry | j                  S N)rc   rT   re   s    r,   rg   zPlotStyle.color_type   s      "###r.   c                    || _         y rn   )rT   )r_   r6   s     r,   rg   zPlotStyle.color_type   s
     r.   c                0    t         | _        t         | _        y)zSet color to object color.N)rQ   rR   rU   re   s    r,   set_object_colorzPlotStyle.set_object_color   s    "'r.   c                `    | j                   J | j                   j                  |      | _        y)zZSet `lineweight` in millimeters. Use ``0.0`` to set lineweight by
        object.
        N)rN   get_lineweight_indexrJ   r_   rJ   s     r,   set_lineweightzPlotStyle.set_lineweight   s*     {{&&&++:::Fr.   c                d    | j                   J | j                   j                  | j                     S )zZReturns the lineweight in millimeters or `0.0` for use entity
        lineweight.
        )rN   lineweightsrJ   re   s    r,   get_lineweightzPlotStyle.get_lineweight   s-     {{&&&{{&&t77r.   c                2    | j                   t        t        fv S )z#``True`` if style has object color.)rR   rQ   OBJECT_COLOR2re   s    r,   rc   zPlotStyle.has_object_color   s    {{|];;;r.   c                     | j                   dz   S )zg:ref:`ACI` in range from ``1`` to ``255``. Has no meaning for named
        plot styles. (int)
        r   r*   re   s    r,   acizPlotStyle.aci   s    
 zzA~r.   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.

        )boolrW   rV   re   s    r,   	ditheringzPlotStyle.dithering   s     D&&566r.   c                p    |r| xj                   t        z  c_         y | xj                   t         z  c_         y rn   )rW   rV   r_   statuss     r,   r   zPlotStyle.dithering   )    ,.<-/r.   c                :    t        | j                  t        z        S )z Plot colors in grayscale. (bool))r~   rW   GRAYSCALE_ONre   s    r,   	grayscalezPlotStyle.grayscale   s     D&&566r.   c                p    |r| xj                   t        z  c_         y | xj                   t         z  c_         y rn   )rW   r   r   s     r,   r   zPlotStyle.grayscale   r   r.   c                :    t        | j                  t        z        S rn   )r~   rW   NAMED_COLORre   s    r,   named_colorzPlotStyle.named_color   s    D&&455r.   c                p    |r| xj                   t        z  c_         y | xj                   t         z  c_         y rn   )rW   r   r   s     r,   r   zPlotStyle.named_color   s)    +-;,.r.   c                   | j                   }|j                  d|z         |j                  d| j                  z         |j                  d| j                  z         |j                  d| j                  z         |j                  d| j
                  z         | j
                  t        k7  r|j                  d| j                  z         |j                  d| j                  z         |j                  d| j                  z         |j                  d	| j                  z         |j                  d
| j                  z         |j                  dt        | j                        z         |j                  d| j                  z         |j                  dt        t        | j                               j#                         z         |j                  dt        | j$                        z         |j                  d| j&                  z         |j                  d| j(                  z         |j                  d| j*                  z         |j                  d       y)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+   writer<   r=   r>   rR   rQ   rU   rW   rC   rD   rE   r3   rG   rH   r~   rI   r4   rJ   rM   rK   rL   )r_   streamr+   s      r,   r   zPlotStyle.write  s   

X%&^dii/0-0C0CCD*T-=-==>^dkk12;;,&LL,t/?/??@*T-?-??@1D4L4LLM043J3JJK_t{{23.T5J5J1KKL&67&T$2H2H-I)J)P)P)RR	
 	(3t+??@(4??:;'$..89(4??:;Vr.   NN)r+   rO   r`   Optional[dict]rN   zOptional[PlotStyleTable])returnzOptional[tuple[int, int, int]])ri   ztuple[int, int, int]r   None)r6   rO   )r   r   )rJ   rY   r   r   )r   rY   )r   r~   )r   rO   )r   r~   r   r   r   r   r   r   )__name__
__module____qualname__ra   propertyr@   setterrg   rq   ru   rx   rc   r|   r   r   r   r   r)   r.   r,   r:   r:   v   sB     $+/	II I )	I@ 3 3 \\
' 
' $ $ ! !(
G8<   
7 
7 0 0 7 7 0 0 6 6 / /r.   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y)PlotStyleTablezPlotStyle containerc                f    || _         || _        || _        d| _        t	        dt
              | _        y )Nr   f)r>   scale_factorapply_factorcustom_lineweight_display_unitsr   DEFAULT_LINE_WEIGHTSrw   )r_   r>   r   r   s       r,   ra   zPlotStyleTable.__init__  s6     '(( 01, &:;r.   c                    	 | j                   j                  |      S # t        $ r6 | j                   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   )rw   r+   r5   appendlenrt   s     r,   rs   z#PlotStyleTable.get_lineweight_index.  sW    	-##))*55 	-##J/t''(1,,	-s    <AAc                    	 || j                   |<   |S # t        $ r6 | j                   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   )rw   
IndexErrorr   r   )r_   r+   rJ   s      r,   set_table_lineweightz#PlotStyleTable.set_table_lineweight8  sU    	-&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

        )rw   )r_   r+   s     r,   get_table_lineweightz#PlotStyleTable.get_table_lineweightG  s     &&r.   c                h    t        |d      5 }| j                  |       ddd       y# 1 sw Y   yxY w)z6Save CTB or STB file as `filename` to the file system.wbN)openr   )r_   filenamer   s      r,   savezPlotStyleTable.saveS  s/    (D! 	VJJv	 	 	s   (1c                    t               }| j                  |       |j                  t        d             |j	                         }|j                          t        ||       y)z:Compress and write the CTB or STB file to binary `stream`.r   N)r   write_contentr   chrgetvalueclose	_compress)r_   r   memfilebodys       r,   r   zPlotStyleTable.writeX  sH    *7#c!f!&$r.   c                     y rn   r)   r_   r   s     r,   r   zPlotStyleTable.write_contenta      r.   c                    |j                  d       t        | j                        D ]  \  }}|j                  d||fz          |j                  d       y)z/Write custom lineweight table to text `stream`.zcustom_lineweight_table{
z	 %d=%.2f
}
N)r   	enumeraterw   )r_   r   r+   weights       r,   _write_lineweightsz!PlotStyleTable._write_lineweightse  sN    12&t'7'78 	9ME6LL78	9Ur.   c                     fd}t        |      }|j                  dd       _        t        |j                  dd             _        t        |j                  dd             _        t        |j                  dd	             _         ||j                  d
d              j                  |j                  di              y)z)Parse plot styles from CTB string `text`.c                    | y t        ddgt        |       z        _        | j                         D ]&  \  }}t	        |      j                  t        |      <   ( y )Nr   r!   )r   r   rw   itemsrY   rO   )rw   keyr6   r_   s      r,   set_lineweightsz-PlotStyleTable.parse.<locals>.set_lineweightso  s[    "$S3%#k2B*BCD)//1 :
U-25\  S*:r.   r>   r?   r   r#   r   Tr   r   custom_lineweight_tableN
plot_style)
PlotStyleFileParserrP   r>   rY   r   r8   r   rO   r   load_styles)r_   textr   parsers   `   r,   parsezPlotStyleTable.parsel  s    	: %T*!::mR8!&**^S"AB$VZZ%EF/2JJ8!<0
, 	

#<dCDL"56r.   c                     y rn   r)   )r_   styless     r,   r   zPlotStyleTable.load_styles  r   r.   Nr?   r#   Fr>   r3   r   rY   r   r~   )rJ   rY   r   rO   )r+   rO   rJ   rY   r   rO   )r+   rO   r   rY   )r   str | os.PathLiker   r   )r   r   r   r   r   )r   r3   r   r   )r   r   r   __doc__ra   rs   r   r   r   r   r   r   r   r   r   r)   r.   r,   r   r     s|     !"	<< < 	<--
'
   7(  r.   r   c                       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 )ColorDependentPlotStylesc                    t         |   |||       t        t              D cg c]  }t	        ||        c}| _        | j
                  j                  dt	        d             y c c}w )N)rN   r      )superra   rangeSTYLE_COUNTr:   _stylesinsert)r_   r>   r   r   r+   	__class__s        r,   ra   z!ColorDependentPlotStyles.__init__  s[     	lLA7<[7I)
.3IeD))
 	y~	
)
s   A&c                f    d|cxk  rdk  rn t        |      | j                   |   S t        |      )z0Returns :class:`PlotStyle` for :ref:`ACI` `aci`.r   r   )r   r   )r_   r|   s     r,   __getitem__z$ColorDependentPlotStyles.__getitem__  s4    s=S= S/! <<$$S/!r.   c                v    d|cxk  rdk  r$n t        |      | |_         || j                  |<   yt        |      )zSet plot `style` for `aci`.r   r   N)rN   r   r   )r_   r|   styles      r,   __setitem__z$ColorDependentPlotStyles.__setitem__  s:    s=S= S/!  EL %DLLS/!r.   c                2    t        | j                  dd       S )zIterable of all plot styles.r   N)iterr   re   s    r,   __iter__z!ColorDependentPlotStyles.__iter__  s    DLL$%%r.   c                F    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:   rS   rg   )r_   r|   r`   r   s       r,   	new_stylez"ColorDependentPlotStyles.new_style  s*     ad3$S	r.   c                <    | |   }|j                         }|dk(  ry|S )z\Returns the assigned lineweight for :class:`PlotStyle` `aci` in
        millimeter.
        r!   Nrx   )r_   r|   r   rJ   s       r,   rx   z'ColorDependentPlotStyles.get_lineweight  s,     S	))+
r.   c                    | j                  |       | j                  |       | j                  |       | j                  |       y)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  s:    6"f%''r.   c                V   |j                  d| j                  z         |j                  d       |j                  d| j                  z         |j                  dt        | j                        j                         z         |j                  dt        | j                        z         y)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   r>   r   r3   r   r4   r   r   s     r,   r   z&ColorDependentPlotStyles._write_header  s    (4+;+;;<12*T->->>?(3t/@/@+A+G+G+IIJ2$6678	
r.   c                    |j                  d       | D ]-  }|j                  }|j                  d|t        |      fz         / |j                  d       y)z1Write AutoCAD Color Index table to text `stream`.zaci_table{
z %d="%s
r   N)r   r+   r-   )r_   r   r   r+   s       r,   r   z)ColorDependentPlotStyles._write_aci_table  sQ    ^$ 	CEKKELLz%/@'AAB	C 	Ur.   c                x    |j                  d       | D ]  }|j                  |        |j                  d       yz#Write user styles to text `stream`.zplot_style{
r   N)r   )r_   r   r   s      r,   r   z+ColorDependentPlotStyles._write_plot_styles  s5    _% 	 EKK	 Ur.   c                    |j                         D ]1  \  }}t        |      }t        ||      }t        |_        |dz   }|| |<   3 y )Nr   )r   rO   r:   rS   rg   )r_   r   r+   r   r|   s        r,   r   z$ColorDependentPlotStyles.load_styles  sJ    "LLN 	LE5JEeU+E(E!)CDI	r.   r   r   )r|   rO   r   r:   )r|   rO   r   r:   rn   )r|   rO   r`   r   r   r:   )r|   rO   r   )r   r   r   ra   r   r   r   r   rx   r   r   r   r   r   __classcell__r   s   @r,   r   r     sa     !"	

 
 	
""&$	(	
r.   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 )NamedPlotStylesc                Z    t         |   |||       t        dddd      }d|i| _        y )Nr   Normal)r<   r=   )r`   )r   ra   r:   r   )r_   r>   r   r   normalr   s        r,   ra   zNamedPlotStyles.__init__  s<     	lLA "*
 /7-?r.   c                "    | j                         S )!Iterable of all plot style names.)keysre   s    r,   r   zNamedPlotStyles.__iter__  s    yy{r.   c                     | j                   |   S )z%Returns :class:`PlotStyle` by `name`.)r   r_   r<   s     r,   r   zNamedPlotStyles.__getitem__  s    ||D!!r.   c                >    |dk7  r| j                   |= yt        d      )zCDelete plot style `name`. Plot style ``'Normal'`` is not deletable.r   z"Can't delete plot style 'Normal'. N)r   r5   r   s     r,   __delitem__zNamedPlotStyles.__delitem__  s"    8T"ABBr.   c                    t        | j                  j                               }|j                  d       dg}|j	                  t        |             t        |      S )r   r   )setr   r   discardextendsortedr   )r_   r   results      r,   r   zNamedPlotStyles.keys  sF    4<<$$&'XfTl#F|r.   c              #  \   K   | j                         D ]  }|| j                  |   f  yw)zDIterable of all plot styles as (``name``, class:`PlotStyle`) tuples.N)r   r   )r_   r   s     r,   r   zNamedPlotStyles.items  s/     99; 	)Ct||C(((	)s   *,c              #  D   K   | j                         D ]	  \  }}|  yw)z*Iterable of all class:`PlotStyle` objects.N)r   )r_   r   r6   s      r,   valueszNamedPlotStyles.values  s$     **, 	JCK	s    c                    |j                         dk(  rt        d      |xs i }||d<   |xs ||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'. r<   r=   )r+   r`   rN   T)lowerr5   r   r   r:   	COLOR_ACIrg   r   )r_   r<   r`   r=   r+   r   s         r,   r   zNamedPlotStyles.new_style   s    $ ::<8#LMMzrV!/!74DLL!D>$ "Tr.   c                <    | |   }|j                         }|dk(  ry|S )z]Returns the assigned lineweight for :class:`PlotStyle` `name` in
        millimeter.
        r!   Nr   )r_   r<   r   rJ   s       r,   rx   zNamedPlotStyles.get_lineweight>  s,     T
))+
r.   c                j    | j                  |       | j                  |       | j                  |       y)z$Write the STB-file to text `stream`.N)r   r   r   r   s     r,   r   zNamedPlotStyles.write_contentI  s,    6"''r.   c                V   |j                  d| j                  z         |j                  d       |j                  d| j                  z         |j                  dt        | j                        j                         z         |j                  dt        | j                        z         y)r   r   zaci_table_available=FALSE
r   r   r   Nr   r   s     r,   r   zNamedPlotStyles._write_headerO  s    (4+;+;;<23*T->->>?(3t/@/@+A+G+G+IIJ2$6678	
r.   c                    |j                  d       t        | j                               D ]  \  }}||_        |j                  |        |j                  d       yr   )r   r   r  r+   )r_   r   r+   r   s       r,   r   z"NamedPlotStyles._write_plot_stylesZ  sM    _%%dkkm4 	 LE5EKKK	  	Ur.   c                    |j                         D ]@  \  }}t        |      }t        ||      }t        |_        || j
                  |j                  <   B y rn   )r   rO   r:   r  rg   r   r<   )r_   r   r+   r   s       r,   r   zNamedPlotStyles.load_stylesb  sJ    "LLN 	-LE5JEeU+E(E',DLL$		-r.   r   r   )r   zIterable[str])r<   r3   r   r:   )r<   r3   r   r   )r   zIterator[tuple[str, PlotStyle]])r   zIterable[PlotStyle]r   )r<   r3   r`   r   r=   zOptional[str]r   r:   )r<   r3   r   )r   r   r   ra   r   r   r  r   r   r  r   rx   r   r   r   r   r   r   s   @r,   r   r     s     !"	@@ @ 	@ "C)
  $(,	  &	
 
<	(	
-r.   r   c                n    t        |       }t               }|j                  |j                                |S )z%Read a CTB-file from binary `stream`.)_decompressr   r   decoder   contentr   s      r,   	_read_ctbr  j  s,     (G%'F
LL!"Mr.   c                n    t        |       }t               }|j                  |j                                |S )z%Read a STB-file from binary `stream`.)r  r   r   r  r  s      r,   	_read_stbr  r  s,     (GF
LL!"Mr.   c                J   t        |       } t        | d      5 }| j                         j                  d      rt	        |      cddd       S | j                         j                  d      rt        |      cddd       S t        dj                  |             # 1 sw Y   yxY w)z5Load the CTB or STB file `filename` from file system.rbz.ctbNz.stbzInvalid file type: "{}")r3   r   r  endswithr  r  r5   format)r   r   s     r,   loadr   z  s     8}H	h	 I>>$$V,V$I I ^^&&v.V$	I I 6==hGHHI Is   *B)B?BB"c                     t               S )zCreate a new CTB file.)r   r)   r.   r,   new_ctbr"    s    #%%r.   c                     t               S )zCreate a new STB file.)r   r)   r.   r,   new_stbr$    s    r.   c                \    | j                         }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    s-    kkmG??723<(D9r.   c           
        t        j                  |j                               }t        j                  |      }| j	                  d       | j	                  t        d|t        |      t        |                   | j	                  |       y)z0Compress `content` and write to binary `stream`.s0   PIAFILEVERSION_2.0,CTBVER1,compress
pmzlibcodecLLLN)r(  compressencodeadler32r   r   r   )r   r  	comp_bodyadler_chksums       r,   r   r     s]    gnn./I<<	*L
LLFG
LLe\3w<YHI
LLr.   c                  2    e Zd ZdZddZedd       ZddZy)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.
    c                j    i | _         t        j                  |      D ]  \  }}|| j                   |<    y rn   )r`   r   	iteritems)r_   r   elementr6   s       r,   ra   zPlotStyleFileParser.__init__  s5    	1;;DA 	'NGU!&DIIg	'r.   c              #     K   ddfdd	fdd
fdfd}| j                  d      t              k  r+        }        }||f  |        t              k  r*yyw)z7Iterate over all first level (start at col 0) elements.r   c                        } | j                  d      r| dd }|j                         S | j                  dd      d   }|j                         S )z&Get element name of line <line_index>.{Nr   =r   r   )r  splitstrip)liner<   
line_indexliness     r,   get_namez/PlotStyleFileParser.iteritems.<locals>.get_name  sU    $D}}S!CRy ::< zz#q)!,::<r.   c                 v    fd} t               } |        s        }        }|||<    |        sdz  |S )zmGet mapping of elements enclosed by { }.

            e. g. lineweights, plot_styles, aci_table

            c                 ,        j                  d      S )N})r  r<  r=  s   r,   end_of_listzGPlotStyleFileParser.iteritems.<locals>.get_mapping.<locals>.end_of_list  s    Z(11#66r.   r   )dict)rC  r`   r<   r6   r>  	get_valuer<  r=  s       r,   get_mappingz2PlotStyleFileParser.iteritems.<locals>.get_mapping  sD    7 6D!mz!"T
 "m !OJKr.   c                        } | j                  d      rdz          S | j                  dd      d   }t        |      }dz  |S )zeGet value of line <line_index> or the list that starts in line
            <line_index>.
            r7  r   r8  )r  r9  sanitized_value)r;  r6   rF  r<  r=  s     r,   rE  z0PlotStyleFileParser.iteritems.<locals>.get_value  sY    
 $D}}S!a
"}$!ZZQ/2'.a
Lr.   c                      t              k  r9t                  dk(  r' dz    t              k  rt                  dk(  r%y y y y )Nr   r   )r   rB  s   r,   skip_empty_linesz7PlotStyleFileParser.iteritems.<locals>.skip_empty_lines  sQ    s5z)c%
2C.D.Ia
 s5z)c%
2C.D.I).I)r.   
N)r   r3   )r   rD  )r   zUnion[str, dict])r9  r   )	r   rJ  r<   r6   rF  r>  rE  r<  r=  s	       @@@@@r,   r3  zPlotStyleFileParser.iteritems  so      
	 	 	&		 
 

4 3u:%:DKE+	 3u:%s   A)A31A3c                :    | j                   j                  ||      S rn   )r`   rP   )r_   r<   defaults      r,   rP   zPlotStyleFileParser.get  s    yy}}T7++r.   N)r   r3   )r<   r3   rM  r
   r   r
   )r   r   r   r   ra   staticmethodr3  rP   r)   r.   r,   r   r     s&    '
 9 9v,r.   r   c                    | j                         } | j                  d      r| dd  S | j                  d      r| j                  d      d   S | S )N"r   ) r   )r:  
startswithr  r9  )r6   s    r,   rH  rH    sN    KKMEQRy ~~c{{3""Lr.   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)   )r@   rg   redgreenblues        r,   rd   rd     sH    
 *$+J8
"CV^!E4<DtZ''r.   c                     t        | |||       S )z:Convert mode_color (r, g, b, color_type) tuple to integer.)	color2intrX  rY  rZ  rg   s       r,   rh   rh     s    c5$
333r.   c                2    |dz  | dz  z   |dz  z   |z    dz  S )z/Convert color (r, g, b, color_type) to integer.rU  rV  rW  l    r)   r]  s       r,   r\  r\  	  s,    B3"9-!<tCDzQQr.   )r+   rO   r   r3   )r6   zUnion[str, bool]r   r~   )r   r   r   r   )r   r   r   r   )r   r   r   z0Union[ColorDependentPlotStyles, NamedPlotStyles])r   r   )r   r   )r   r   r   bytes)r   r   r  r3   )r6   r3   r   r3   )r@   rO   r   ztuple[int, int, int, int])rX  rO   rY  rO   rZ  rO   r   rO   )
rX  rO   rY  rO   rZ  rO   rg   rO   r   rO   )G
__future__r   typingr   r   r   r   r   r	   r
   osabcr   ior   r   structr   r(  END_STYLE_BUTTEND_STYLE_SQUAREEND_STYLE_ROUNDEND_STYLE_DIAMONDr\   JOIN_STYLE_MITERJOIN_STYLE_BEVELJOIN_STYLE_ROUNDJOIN_STYLE_DIAMONDr]   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_BARSr^   rV   r   r   rX   r[   rZ   rQ   rz   r   r   COLOR_BY_LAYERCOLOR_BY_BLOCKrS   r  r-   r8   r:   r   r   r   r  r  r   r"  r$  r  r   r   rH  rd   rh   r\  r)   r.   r,   <module>ry     s  
 #   
                       	  B   	 	$	c cLf fRf~ fRy-n y-xII5I&

G, G,T		( @I 4
Rr.   