
     h,5                     $   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,y)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<   |j                  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      q/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/cairosvg/bounding_box.pycalculate_bounding_boxr"      sN     T!dhh2F&F+DHH5gtD$\2#/D 88N##    c                     t        | |j                  d      d      t        | |j                  d      d      }}t        | |j                  d      d      }t        | |j                  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*   !   sj    #,d7DHHSM3.OqA$((7+S1E'488H-s3Far#   c                     t        | |j                  d      d      }t        | |j                  d      d      }t        | |j                  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   )   sa    	gtxx~s	+B	gtxx~s	+BWdhhsm$A6261q5!a%''r#   c                    t        | |j                  d      d      }t        | |j                  d      d      }t        | |j                  d      d      }t        | |j                  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   sy    	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   \   K   | ]#  }t        j                  |      |d           % yw)r   Nr)   ).0positionr    r   s     r!   	<genexpr>z$bounding_box_line.<locals>.<genexpr><   s/      2 	Wdhhx((1+62s   ),)x1y1x2y2)tupleminmax)
r   r    r:   r;   r<   r=   r%   r&   r'   r(   s
   ``        r!   bounding_box_linerA   :   se     202 2NBB r2;BqABK!OSR[1_6Ear#   c                     t         }g }t        |j                  dd            }|r&t        | |      \  }}}|j	                  ||f       |r&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   s^    %LF!$((8R"89
"'1B"C1q!f  |V44r#   c                 D   |j                  dd      }t        D ]  }|j                  |d| d      } t        |      }t        }d}d}d}|rR|j                         }|j                  dd      d   t        v r|dz   j                  dd      \  }}|dv rrt        d|      \  }}}|j                  dd      \  }	}t        t        |	            }	|d   |dd j                         }}
|
d	   j                         s/|
|d   z   |dd j                         }}
|
d	   j                         s/|d   |dd j                         }}|d	   j                         s/||d   z   |dd j                         }}|d	   j                         s/t        t        |
            t        t        |            }}
t        d|      \  }}}|d
k(  r
||z  }||z  }t        |||||	|
|||	      }|\  }}}}||z   }||z   }||f||ff}t        ||      }|}|}n|dv rnt        d|      \  }}}t        d|      \  }}}t        d|      \  }}}|dk(  r||z  }||z  }||z  }||z  }||z  }||z  }t        |||f||f||ff      }|}|}n|dv rA|dz   j                  dd      \  }}t        | |d      }|dk(  r||z  }t        |||ff      }|}n|dv r2t        d|      \  }}}|dv r
||z  }||z  }t        |||ff      }|}|}n|dv rOt        d|      \  }}}t        d|      \  }}}|dv r||z  }||z  }||z  }||z  }t        |||f||ff      }|}|}nD|dv r@|dz   j                  dd      \  }}t        | |d      }|dk(  r||z  }t        |||ff      }|}|j                         }|rR|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  =%%f&m<	=)$I%LJJF
OO%	??3"1%5!*S 7 7Q ?FIT> %dI 6BI"+//#q"9HiuX/H  )|Yqr]-@-@-B9EBi'')#(9Q<#7129L9L9Ny Bi'')(|Yqr]-@-@-B9EBi'')#(9Q<#7129L9L9Ny Bi'')  E
+T#e*-=5E#D)4OAq) }ZZ  ;JB%1 N$4!BE6eBfB"XBx'F.|VDLJJt^ %dI 6BI %dI 6BI#D)4OAq) }j j j j ZZ /Bx"bAq6:<LJJt^%O223:LAyWa%A }Z /:02LJx#D)4OAq) ZZ /|q!fYGLJJv %dI 6BI#D)4OAq) ~j j ZZ /Bx!Q02LJJt^%O223:LAyWa%A }Z /
A02LJOO%	s v r#   c                 $    |j                  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                 p    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      s+    htBr2$67<<cBBr#   c	                    t        |      t        |      }}|dk(  s|dk(  r2t        ||       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  r^||z  }|
dz  |	dz  |dz  z  z   }|dk  r2t        ||       t        ||      t        || z
        t        ||z
        fS t	        |      }||z  }n,||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 rH||z
  }t        | d      }||z   }t        |d      }||z
  }t        d|       }||z   }t        d|      }n|t
        dz  dt
        z  dz  fv rH||z
  }t        | d      }||z   }t        |d      }||z
  }t        d|       }||z   }t        d|      }nt        |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
  }||kD  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   }||kD  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}||kD  r||}}d}|||cxk  xr |k  nc k(  rt        ||       }|||cxk  xr |k  nc k(  rt        ||       }|||cxk  xr |k  nc k(  rt        ||      }|||cxk  xr |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      s0    Wc"gB	Qw"'1bz3q":s1r6{CBK??#h"q&!A%CBF(;a(??G3xi2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":s1bz3q2v;AFCC(^RZ"Ah?2+'",'B,(2-	3s8	gC0	0BFa<	?B	3s8	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b3s8mb())T"s3x-",--BUOc#h..c%j3s81KKBUOc#h..c%j3s81KK$;t$D %5ER#e*_s3x//"s5z/CH2LLdRi,R#e*_s3x//"s5z/CH2LLdRi,R3s8b=)*R3s8b=)*R/BUOc#h..c%j3s81KKBUOc#h..c%j3s81KK$;t$D %5ER#e*_s3x//"s5z/CH2LLebj$),R#e*_s3x//"s5z/CH2LLebj$),27BG$F1r61r6"FI	Vu../1bzVu../1bzVu../1bzVu../1bztd{D4K//r#   c                 `    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  s<    %L B+0%@BB r#   c                     t        |j                               j                         }t        ||j                  ||j
                        }t        |j                        sy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  sW    T]]_%,,.Dd..t{{D $--(!'400r#   c                    | \  }}}}t        |      rt        d      n||z   t        |      rt        d      n||z   }}t        | \  }}	t        |g| t        |g|	 t	        |g| t	        |g|	 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f$,tf$- D &\NFFD6C.v.D6C.v.0D$d td{D4K//r#   c                 b    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  sL    12$8!dE6E\4&=d*D$<$68r#   c                 4    | xr 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Hr#   c                 *    t        |       xr 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  5\5<A5 
$(,5HV)
C
b0J1
0IM !#%$!	  r#   