
    j,5                     2   d 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mZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZ  ed           ed          ddf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+eeee e!e!e"e#e#e#e&e'e&dZ,dS )zk
Calculate bounding box for SVG shapes and paths.

A bounding box is a (minx, miny, width, height) tuple.

    )acosatancopysigncosfmodhypotisinfpiradianssinsqrttantau   )match_features)PATH_LETTERS	normalizepointsize)Tree)	parse_urlinfc                     d|vr=|j         t          v r/t          |j                  | |          }t          |          r||d<   |                    d          S )ziCalculate ``node``'s bounding box.

    See https://www.w3.org/TR/SVG/coords.html#ObjectBoundingBox

    bounding_box)tagBOUNDING_BOX_METHODSis_non_empty_bounding_boxget)surfacenoder   s      \/home/ubuntu/budget-transfer-bot/.venv/lib/python3.11/site-packages/cairosvg/bounding_box.pycalculate_bounding_boxr"      s^     T!!dh2F&F&F+DH5gtDD$\22 	0#/D 88N###    c                 .   t          | |                    d          d          t          | |                    d          d          }}t          | |                    d          d          }t          | |                    d          d          }||||fS )z(Get the bounding box of a ``rect`` node.xywidthheightr   r   )r   r    r%   r&   r'   r(   s         r!   bounding_box_rectr*   !   s    #,,d7DHHSMM3.O.OqA$((7++S11E'488H--s33Far#   c                     t          | |                    d          d          }t          | |                    d          d          }t          | |                    d                    }||z
  ||z
  d|z  d|z  fS )z*Get the bounding box of a ``circle`` node.cxr%   cyr&   r   r)   )r   r    r,   r-   r.   s        r!   bounding_box_circler0   )   sp    	gtxx~~s	+	+B	gtxx~~s	+	+BWdhhsmm$$A6261q5!a%''r#   c                 F   t          | |                    d          d          }t          | |                    d          d          }t          | |                    d          d          }t          | |                    d          d          }||z
  ||z
  d|z  d|z  fS )z,Get the bounding box of an ``ellipse`` node.rxr%   ryr&   r,   r-   r/   r)   )r   r    r2   r3   r,   r-   s         r!   bounding_box_ellipser4   1   s    	gtxx~~s	+	+B	gtxx~~s	+	+B	gtxx~~s	+	+B	gtxx~~s	+	+B7BGQVQV++r#   c                      t           fddD                       \  }}}}t          ||          t          ||          }}t          ||          |z
  t          ||          |z
  }	}||||	fS )z(Get the bounding box of a ``line`` node.c              3   l   K   | ].}t                              |          |d                    V  /dS )r   Nr)   ).0positionr    r   s     r!   	<genexpr>z$bounding_box_line.<locals>.<genexpr><   sS       2 2 	Wdhhx(((1+662 2 2 2 2 2r#   )x1y1x2y2)tupleminmax)
r   r    r:   r;   r<   r=   r%   r&   r'   r(   s
   ``        r!   bounding_box_linerA   :   s     2 2 2 2 202 2 2 2 2NBB r2;;BqABKK!OSR[[1_6Ear#   c                     t           }g }t          |                    dd                    }|r-t          | |          \  }}}|                    ||f           |-t          ||          S )z;Get the bounding box of a ``polyline`` or ``polygon`` node.points )EMPTY_BOUNDING_BOXr   r   r   appendextend_bounding_box)r   r    r   rC   normalized_pointsr%   r&   s          r!   bounding_box_polylinerI   D   sz    %LF!$((8R"8"899
 "'1B"C"C1q!f   |V444r#   c                    |                     dd          }t          D ]}|                    |d| d          }t          |          }t          }d}d}d}|r|                                }|                    dd          d         t          v r|dz                       dd          \  }}|dv rt          d|          \  }}}|                    dd          \  }	}t          t          |	                    }	|d         |dd                                         }}
|
d	         
                                sA|
|d         z   |dd                                         }}
|
d	         
                                A|d         |dd                                         }}|d	         
                                sA||d         z   |dd                                         }}|d	         
                                At          t          |
                    t          t          |                    }}
t          d|          \  }}}|d
k    r
||z  }||z  }t          |||||	|
|||	  	        }|\  }}}}||z   }||z   }||f||ff}t          ||          }|}|}n|dv rt          d|          \  }}}t          d|          \  }}}t          d|          \  }}}|dk    r||z  }||z  }||z  }||z  }||z  }||z  }t          |||f||f||ff          }|}|}n@|dv rN|dz                       dd          \  }}t          | |d          }|dk    r||z  }t          |||ff          }|}n|dv r:t          d|          \  }}}|dv r
||z  }||z  }t          |||ff          }|}|}n|dv r[t          d|          \  }}}t          d|          \  }}}|dv r||z  }||z  }||z  }||z  }t          |||f||ff          }|}|}nQ|dv rM|dz                       dd          \  }}t          | |d          }|dk    r||z  }t          |||ff          }|}|                                }||S )z(Get the bounding box of a ``path`` node.drD    r   Mr   aANacCchHr%   hlLmMtTlmtqQsSqsvVr&   v)r   r   replacer   rE   stripsplitr   r   floatisdigitboolintbounding_box_elliptical_arcrG   r   )r   r    	path_dataletterr   
previous_x
previous_yr2   r3   rotationlargesweepr%   r&   arc_bounding_boxr:   r;   r'   r(   r<   r=   rC   s                         r!   bounding_box_pathrk   O   s   b!!I  = =%%fm&mmm<<		)$$I%LJJF
 y&OO%%	??3""1%55!*S 7 7Q ? ?FIT>> %dI 6 6BI"+//#q"9"9HiuX//H  )|Yqrr]-@-@-B-B9EBi'')) O#(9Q<#71229L9L9N9Ny Bi'')) O(|Yqrr]-@-@-B-B9EBi'')) O#(9Q<#71229L9L9N9Ny Bi'')) O  E

++T#e**-=-=5E#D)44OAq) }}ZZ  ;JB%1 N  N$4!BE6eBfB"XBx'F.|VDDLJJJt^^ %dI 6 6BI %dI 6 6BI#D)44OAq) }}j j j j ZZ /Bx"bAq6:< <LJJJt^^%O223::LAyWa%%A }}Z /:02 2LJJx#D)44OAq) ZZ /|q!fYGGLJJJv %dI 6 6BI#D)44OAq) ~~j j ZZ /Bx!Q02 2LJJJt^^%O223::LAyWa%%A }}Z /
A02 2LJOO%%	s  y&v r#   c                 ,    |                     d          S )z(Get the bounding box of a ``text`` node.text_bounding_box)r   )r   r    s     r!   bounding_box_textrn      s    88'(((r#   c                     t          t          t          t          | t	          | |          z            |          z   t                    S )z6Get the angle between vector (1,0) and vector (bx,by).)r   r   r   r   r   )bxbys     r!   anglerr      s6    htBr2$677<<<cBBBr#   c	                 (   t          |          t          |          }}|dk    s|dk    rBt          ||           t          ||          t          || z
            t          ||z
            fS t          |          | |z
  z  dz  t          |          ||z
  z  dz  z   }	t          |           | |z
  z  dz  t          |          ||z
  z  dz  z   }
|dz  |dz  z  |dz  |
dz  z  z
  |dz  |	dz  z  z
  }||dz  |
dz  z  |dz  |	dz  z  z   z  }dx}}|dk     rs||z  }|
dz  |	dz  |dz  z  z   }|dk     rBt          ||           t          ||          t          || z
            t          ||z
            fS t	          |          }||z  }n1||k    rdndt	          |          z  }||z  |
z  |z  }| |z  |	z  |z  }|t          |          z  |t          |          z  z
  | |z   dz  z   }|t          |          z  |t          |          z  z   ||z   dz  z   }|dt
          fv rX||z
  }t          | d          }||z   }t          |d          }||z
  }t          d|           }||z   }t          d|          }n|t
          dz  dt
          z  dz  fv rX||z
  }t          | d          }||z   }t          |d          }||z
  }t          d|           }||z   }t          d|          }nMt          |t          |          z  |z             }t
          t          |t          |          z  |z            z
  }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z
  }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z
  }||k    r||}}||}}||t          |          z  t          |          z  z   |t          |          z  t          |          z  z   }t          ||z
  ||z
            }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z   }t          ||z
  ||z
            }t          |t          |          |z  z            }t          |t          |          |z  z            t
          z   }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z   }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z   }||k    r||}}||}}||t          |          z  t          |          z  z   |t          |          z  t          |          z  z
  }t          ||z
  ||z
            }||t          |          z  t          |          z  z   |t          |          z  t          |          z  z
  }t          ||z
  ||z
            }t          | |z
  ||z
            }t          ||z
  ||z
            }|s||}}d}||k    r||}}d}|||cxk    o|k    nc k    rt          ||           }|||cxk    o|k    nc k    rt          ||           }|||cxk    o|k    nc k    rt          ||          }|||cxk    o|k    nc k    rt          ||          }||||z
  ||z
  fS )zGet the bounding box of an elliptical arc described by the parameters.

    See following website for original code:
    http://fridrich.blogspot.nl/2011/06/bounding-box-of-svg-elliptical-arc.html

    r   r/   rO   r      FT)
absr?   r   r   r   r
   rr   r   r   r@   )r:   r;   r2   r3   phirh   ri   r%   r&   x1primey1primeradicantcxprimecyprimeratiofactorr,   r-   minxtminxmaxxtmaxxminytminymaxytmaxytmp_ytmp_xangle1angle2	other_arcs                                  r!   rb   rb      sL    WWc"ggB	Qww"''1bzz3q"::s1r6{{CBKK??#hh"q&!A%CBF(;a(??G3xxi26"Q&SR!V)<q)@@G 	a"'B!Ggl22R1Ww!|5KK a'Q,&q7a<)???HGg!||Ra<'Q,!";;a<<q"::s1bzz3q2v;;AFCC(^^RZ""Ah?2+'",'B,(2-	3s88	gC0	0BFa<	?B	3s88	gC0	0BFa<	?B
q"g~~Bwrc1Bwb!Bwa"Bwa	aR!$	$	$Bwrc1Bwb!Bwa"Bwab3s88mb()))T"s3xx-",---BUOc#hh..c%jj3s881KKBUOc#hh..c%jj3s881KK$;;t$D %5ER#e**_s3xx//"s5zz/CHH2LLdRi,,R#e**_s3xx//"s5zz/CHH2LLdRi,,R3s88b=)**R3s88b=)**R/BUOc#hh..c%jj3s881KKBUOc#hh..c%jj3s881KK$;;t$D %5ER#e**_s3xx//"s5zz/CHH2LLebj$),,R#e**_s3xx//"s5zz/CHH2LLebj$),,27BG$$F1r61r6""F (I	Vu........//1bzzVu........//1bzzVu........//1bzzVu........//1bzztd{D4K//r#   c                 d    t           }|j        D ] }t          |t          | |                    }!|S )z%Get the bounding box of a ``g`` node.)rE   childrencombine_bounding_boxr"   )r   r    r   childs       r!   bounding_box_groupr   I  sC    %L B B+0%@@B Br#   c                     t          |                                                                          }t          ||j        ||j                  }t          |j                  sdS t          | |          S )z'Get the bounding box of a ``use`` node.)urlurl_fetcherparentunsafeN)	r   get_hrefgeturlr   r   r   r   xml_treer"   )r   r    hreftrees       r!   bounding_box_user   R  so    T]]__%%,,..Dd.t{  D $-(( t!'4000r#   c                 :   | \  }}}}t          |          rt          d          n||z   t          |          rt          d          n||z   }}t          | \  }}	t          |g|R  t          |g|	R  t	          |g|R  t	          |g|	R  f\  }}}}||||z
  ||z
  fS )z*Extend the ``bounding_box`` by the points.z-inf)r	   r^   zipr?   r@   )
r   rC   r~   r   r'   r(   r   r   x_listy_lists
             r!   rG   rG   ]  s     ,D$vt6f$,t7f$- D &\NFFD6C.v...D6C.v...0D$d td{D4K//r#   c                 r    t          |          r'|\  }}}}||z   ||z   }}t          | ||f||ff          } | S )z;Combine the ``bounding_box`` with ``another_bounding_box``.)is_valid_bounding_boxrG   )r   another_bounding_boxr~   r   r'   r(   r   r   s           r!   r   r   j  sY    122 8$8!dE6E\4&=d*D$<$68 8r#   c                 D    | ot          | d         | d         z              S )z/Know whether bounding box has been initialized.r   r   )r	   r   s    r!   r   r   t  s'     Hl1oQ&G H HHHr#   c                 8    t          |           od| dd         vS )z2Know whether bounding box is valid and has a size.r   r/   N)r   r   s    r!   r   r   {  s#     ..L1L<L3LLr#   )rectcircleellipselinepolylinepolygonpathtexttspantextPathgusemarkerN)-__doc__mathr   r   r   r   r   r   r	   r
   r   r   r   r   r   featuresr   helpersr   r   r   r   parserr   r   r   r^   rE   r"   r*   r0   r4   rA   rI   rk   rn   rr   rb   r   r   rG   r   r   r   r    r#   r!   <module>r      s]   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 % $ $ $ $ $ 9 9 9 9 9 9 9 9 9 9 9 9            U5\\55<<A5 
$ 
$ 
$  ( ( (, , ,  5 5 5H H HV) ) )
C C C
b0 b0 b0J  1 1 1
0 
0 
0  I I IM M M !#%$!	     r#   