ó
a#nSc           @   s  d  Z  d d l m Z d d l Z d d l Z d Z d Z d Z i% d d 6d d	 6d
 d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d  d! 6d" d# 6d$ d% 6d& d' 6d( d) 6d* d+ 6d, d- 6d. d/ 6d0 d1 6d2 d3 6d4 d5 6d6 d7 6d8 d9 6d: d; 6d< d= 6d> d? 6d@ dA 6dB dC 6dD dE 6dF dG 6d. dH 6dI dJ 6d( dK 6dL dM 6Z dN d^ dO „  ƒ  YZ	 dP e j
 f dQ „  ƒ  YZ d dR „ Z d dS „ Z d dT „ Z d dU „ Z dV „  Z d d dW dX „ Z d dY l m Z dZ „  Z d[ „  Z d d dW d\ „ Z d] „  Z d S(_   sy  
Module StringFormat
The StringFormat module allows for character-by-character formatting of
strings. It imitates the SPING string drawing and string metrics
interface. The string formatting is done with specialized XML syntax
within the string. Therefore, the interface for the StringFormat module
consists of wrapper functions for the SPING string interface and
various XML tags and characters.

StringFormat functions 

       drawString(canvas, s, x, y, [font], [color], [angle]) 
       stringWidth(canvas, s, [font]) 
       fontHeight(canvas, [font]) 
       fontAscent(canvas, [font]) 
       fontDescent(canvas, [font]) 
StringFormat XML tags 

       <b> </b> - bold 
       <i> </i> - italics 
       <u> </u> - underline 
       <super> </super> - superscript 
       <sub> </sub> - subscript 

StringFormat XML characters 

       Greek Letter Symbols as specified in MathML 
iÿÿÿÿ(   t   FontNi   g      à?t   at   alphat   bt   betat   ct   chit   Dt   Deltat   dt   deltat   et   epsivt   ht   etat   Gt   Gammat   gt   gammat   it   iotat   kt   kappat   Lt   Lambdat   lt   lambdat   mt   mut   nt   nut   Wt   Omegat   wt   omegat   xt   omicront   Ft   Phit   ft   phit   jt   phivt   Pt   Pit   pt   pit   vt   pivt   Yt   Psit   yt   psit   rt   rhot   St   Sigmat   st   sigmat   Vt   sigmavt   tt   taut   Qt   Thetat   qt   thetat   thetavt   Xt   Xit   xit   zt   zetat   StringSegmentc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s~   class StringSegment contains the intermediate representation of string
	segments as they are being parsed by the XMLParser. 
	c         C   sL   d |  _  d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   Ni    t    (   t   supert   subt   boldt   italict	   underlineR9   t   widtht   greek(   t   self(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   __init__e   s    							c         C   s}   |  j  r d } n	 | j } t d | d | j |  j t |  j t d |  j pU | j d |  j pg | j d |  j	 py | j	 ƒ S(   si   Given a font (does not accept font==None), creates a 		new font based on the format of this text segment.t   symbolt   facet   sizeRO   RM   RN   (
   RQ   RU   R    RV   RK   t	   sizedeltaRL   RO   RM   RN   (   RR   t   fontRU   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   calcNewFonto   s    			c         C   sD   |  j  d k r | | j t S|  j d k r< | | j t S| Sd S(   s[   Returns a new y coordinate depending on its 		whether the string is a sub and super script.i   N(   RL   RV   t   subFractionRK   t   superFraction(   RR   RX   R3   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   calcNewY�   s
    c         C   sd   d |  j  GHd G|  j GHd G|  j GHd G|  j GHd G|  j GHd G|  j GHd G|  j GHd G|  j GHd  S(	   Ns   StringSegment: ]%s[s	   	super = s   	sub = s   	bold = s
   	italic = s   	underline = s	   	width = s	   	greek = (   R9   RK   RL   RM   RN   RO   RP   RQ   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   dump�   s    (   t   __name__t
   __module__t   __doc__RS   RY   R\   R]   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRI   `   s
   	
		t   StringFormatterc           B   s�   e  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 RS(   c         C   s   d |  _  d  S(   Ni   (   RM   (   RR   t
   attributes(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   start_b¶   s    c         C   s   d |  _  d  S(   Ni    (   RM   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   end_b¹   s    c         C   s   d |  _  d  S(   Ni   (   RN   (   RR   Rb   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   start_i½   s    c         C   s   d |  _  d  S(   Ni    (   RN   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   end_iÀ   s    c         C   s   d |  _  d  S(   Ni   (   RO   (   RR   Rb   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   start_uÄ   s    c         C   s   d |  _  d  S(   Ni    (   RO   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   end_uÇ   s    c         C   s   d |  _  d  S(   Ni   (   RK   (   RR   Rb   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   start_superË   s    c         C   s   d |  _  d  S(   Ni    (   RK   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt	   end_superÎ   s    c         C   s   d |  _  d  S(   Ni   (   RL   (   RR   Rb   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt	   start_subÒ   s    c         C   s   d |  _  d  S(   Ni    (   RL   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   end_subÕ   s    c         C   s   d |  _  |  j | ƒ d  S(   Ni   (   RQ   t   handle_data(   RR   Rb   t   letter(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   start_greekÙ   s    	c         C   s   d |  _  d  S(   Ni    (   RQ   (   RR   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt	   end_greekÞ   s    c         C   s  t  j j |  ƒ g  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ i |  j	 |  j
 f d 6|  j |  j f d 6|  j |  j f d 6|  j |  j f d 6|  j |  j f d 6|  _ x7 t j ƒ  D]) } |  t | d „ |  j f |  j | <q» Wd |  _ x% t j ƒ  D] } d | |  j | <qþ Wd  S(	   Ni    R   t   uR   RK   RL   c         S   s   | j  |  | ƒ S(   N(   Ro   (   t   attrRR   Rn   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   <lambda>ù   s    s   <%s/>(   t   xmllibt	   XMLParserRS   t   segmentlistRL   RK   RM   RN   RO   Rc   Rd   Rg   Rh   Re   Rf   Ri   Rj   Rk   Rl   t   elementst
   greekcharst   keysRp   RQ   t
   entitydefs(   RR   t   item(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRS   â   s$    							c         C   s¡   t  ƒ  } | | _ |  j d k rE |  j d k rE d | _ d | _ n |  j | _ |  j | _ |  j | _ |  j | _ |  j | _ |  j | _ |  j j	 | ƒ d S(   s:   Creates an intermediate representation of string segments.i   i    N(
   RI   R9   RL   RK   RM   RN   RO   RQ   Rv   t   append(   RR   t   datat   segment(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRm     s    			c         C   s?   |  j  d | d ƒ |  j ƒ  |  j ƒ  |  j } g  |  _ | S(   sd   Given a formatted string will return a list of 		StringSegment objects with their calculated widths.s   <formattedstring>s   </formattedstring>(   t   feedt   closet   resetRv   (   RR   R9   Rv   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   parseSegments"  s    

		(   R^   R_   Rc   Rd   Re   Rf   Rg   Rh   Ri   Rj   Rk   Rl   Ro   Rp   RS   Rm   R‚   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRa   ¥   s   													&	c         C   s   |  j  | ƒ S(   s;   Find the total height (ascent + descent) of the given font.(   t
   fontHeight(   t   canvasRX   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRƒ   3  s    c         C   s   |  j  | ƒ S(   s6   Find the ascent (height above base) of the given font.(   t
   fontAscent(   R„   RX   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyR…   7  s    c         C   s   |  j  | ƒ S(   s7   Find the descent (extent below base) of the given font.(   t   fontDescent(   R„   RX   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyR†   ;  s    c         C   sg   t  ƒ  } | j | ƒ } | s* |  j } n  d } x0 | D]( } | |  j | j | j | ƒ ƒ } q7 W| S(   sg   Return the logical width of the string if it were drawn 	in the current font (defaults to canvas.font).i    (   Ra   R‚   t   defaultFontt   stringWidthR9   RY   (   R„   R9   RX   t
   sformatterRv   t   sumt   seg(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyRˆ   F  s    	&c         C   sX   | t  j d } t  j | ƒ |  t  j | ƒ | t  j | ƒ |  t  j | ƒ | f S(   sX   Rotate (x,y) by theta degrees.  Got tranformation 	from page 299 in linear algebra book.g     €f@(   t   mathR.   t   cost   sin(   R#   R3   RB   t   radians(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   rotateXYX  s    !i    c         C   sÃ   t  ƒ  } | j | ƒ } | s* |  j } n  | }	 xŒ | D]„ }
 t |	 | |
 j | | ƒ | | ƒ \ } } |  j |
 j | | | | |
 j | ƒ | | ƒ |	 |  j |
 j |
 j | ƒ ƒ }	 q7 Wd S(   s;   Draw a formatted string starting at location x,y in canvas.N(	   Ra   R‚   R‡   R�   R\   t
   drawStringR9   RY   Rˆ   (   R„   R9   R#   R3   RX   t   colort   angleR‰   Rv   t   startposR‹   t   delta_xt   delta_y(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyR‘   a  s    	,(   t	   PDFCanvasc          C   së   t  d ƒ }  t |  d d d ƒ t |  d d d ƒ d Gt |  d ƒ GHd G|  j d ƒ GHt |  d	 d d
 ƒ d Gt |  d	 ƒ GHt |  d d d t d d ƒ ƒ d G|  j d t d d ƒ ƒ GHt |  d d d ƒ d G|  j d ƒ GH|  j ƒ  d  S(   Ns	   test1.pdfs*   <u><b>hello there</b></u><super>hi</super>i
   i   s   hello!i(   s   'hello!' width = s   'hello!' SPING width = s   <b>hello!</b> goodbyei<   s    '<b>hello!</b> goodbye' width = iP   RM   i   s$   'hello!' Font(bold=1) SPING width = s    goodbyeid   s   ' goodbye' SPING width = (   R—   R‘   Rˆ   R    t   flush(   R„   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   test1€  s    c          C   s@   t  d ƒ }  t |  d d d ƒ t |  d d d ƒ |  j ƒ  d  S(   Ns	   test2.pdfs   <alpha/>i
   s   &alpha;i   (   R—   R‘   R˜   (   R„   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   test2�  s    c   
   	   C   sé  |  j  } | r | |  _  n  | } t |  d ƒ } |  j  j d }	 t |  d | | d | d | ƒ| t |  d ƒ | } t |  d | | d | d | ƒ| t |  d ƒ | } t |  d | | d | d | ƒ| t |  d ƒ | } t |  d | | d | d | ƒ| t |  d ƒ | } t |  d	 | | d | d | ƒ| |	 } t |  d
 | | d | d | ƒ| t |  d
 ƒ | } t |  d | | d | d | ƒ| t |  d ƒ | } t |  d | | d | d | ƒ| |	 } | |  _  | | f S(   sk   Try out all tags and various combinations of them. \ 
	Starts at given x,y and returns possible next (x,y).t    g      ø?s   <b>bold</b>R’   R“   s   <i>italic</i>s   <u>underline</u>s   <super>super</super>s   <sub>sub</sub>s   <b><u>bold+underline</u></b>s"   <super><i>super+italic</i></super>s   <b><sub>bold+sub</sub></b>(   R‡   Rˆ   RV   R‘   (
   R„   R#   R3   RX   R’   R“   t
   oldDefaultt   oldxt   dxt   dy(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   allTagCombosŸ  s0    	 

	c          C   s¦  t  d ƒ }  d } |  j j d } t |  | | ƒ \ } } t |  | | d t d d ƒ ƒ \ } } t |  | | d t d d ƒ ƒ \ } } t |  | | d t d d ƒ d d	 ƒ\ } } t |  | | d t d
 d ƒ ƒ \ } } t |  | | d t d
 d ƒ ƒ \ } } t |  | | d t d d ƒ ƒ \ } } t |  | | d d t ƒ\ } } t |  d ƒ } d Gt |  d ƒ GHd Gt |  d ƒ GHd Gt |  d ƒ GH|  j d t d
 |  j j t d d ƒ ƒ } d G| GH|  j d ƒ } d G| GH|  j d t d
 |  j j t d d ƒ ƒ } d G| GHd G| Gd G| | | GHt  d ƒ }  d } |  j j d } t |  d  | | t d
 d ƒ d t	 ƒd! GH| d } t |  d" | | d t
 ƒ| d } t |  d# | | d t	 ƒ| d } t |  d$ | | d t
 ƒd% GH| d } t |  d& | | d t	 ƒ| d } t |  d' | | d t
 ƒ| d } t |  d( | | d t	 ƒd) GH| d } t |  d* | | d t
 ƒ| d } t |  d+ | | ƒ |  j ƒ  d  S(,   Ns   bigtest1.pdfi
   g      ø?i   RU   t   serift
   monospacedR“   iâÿÿÿRV   i   i	   RO   i   R’   sF   <b><sub>bold+sub</sub></b> hello <u><super>underline+super</super></u>s#   sw("<b><sub>bold+sub</sub></b>") = s   <b><sub>bold+sub</sub></b>s   sw(" hello ") = s    hello s.   sw("<u><super>underline+super</super></u>") = s%   <u><super>underline+super</super></u>s   bold+subRM   s
   pwidth1 = s
   pwidth2 = s   underline+supers
   pwidth3 = s
   sfwidth = s
    pwidth = s   bigtest2.pdfs&   &alpha; &beta; <chi/> &Delta; <delta/>s/   line starting with alpha should be font size 16s   &epsiv; &eta; &Gamma; <gamma/>s!   &iota; &kappa; &Lambda; <lambda/>s(   <u>&mu;</u> &nu; <b>&Omega;</b> <omega/>s5   mu should be underlined, Omega should be big and bolds   &omicron; &Phi; &phi; <phiv/>s"   &Pi; &pi; &piv; <Psi/> &psi; &rho;s&   <u>&Sigma; &sigma; &sigmav; <tau/></u>s8   line starting with sigma should be completely underlineds*   &Theta; &theta; &thetav; <Xi/> &xi; &zeta;s3   That's &alpha;ll <u>folks</u><super>&omega;</super>(   R—   R‡   RV   R    R    t   redRˆ   RW   R‘   t   bluet   greenR˜   (   R„   R#   R3   t   sfwidtht   pwidth1t   pwidth2t   pwidth3(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   stringformatTestÈ  sb    	((.((("	+			"	%







(    (   R`   t   rdkit.sping.pidR    Rt   RŒ   RW   RZ   R[   Rx   RI   Ru   Ra   t   NoneRƒ   R…   R†   Rˆ   R�   R‘   t	   sping.PDFR—   R™   Rš   R    Rª   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/sping/stringformat.pyt   <module>   sr   
EŽ				)