
    BPc                         d Z ddlZddlmZ g dZddgZddZd Zd	 Z	d
 Z
d Zd Zd Z G d dej                  Zedk    r e                                             dS dS )z
This script finds all fonts in the current drawing that match the
specified find font, and replaces them with the specified replacement
font.

It can also replace all fonts indiscriminately, and list all fonts
currently being used.
    N)inkex_gettext)z!{http://www.w3.org/2000/svg}tspanz {http://www.w3.org/2000/svg}textz${http://www.w3.org/2000/svg}flowRootz${http://www.w3.org/2000/svg}flowParaz${http://www.w3.org/2000/svg}flowSpanzfont-familyz-inkscape-font-specificationc                 X    d}|s| j         }|rt          D ]}||v r|||<   || _         d}|S )z
    Sets the font attribute in the style attribute of node, using the
    font name stored in new_font. If the style dict is open already,
    it can be passed in, otherwise it will be optned anyway.

    Returns a dirty boolean flag
    FT)stylefont_attributes)nodenew_fontr   dirtyatts        ./usr/share/inkscape/extensions/replace_font.pyset_fontr   )   sS     E 
 " 	 	Ce||%c
"
L    c                     d}| j         }|rPt          D ]H} ||                                                                          |k    rt	          | ||           d}I|S )z
    Searches the relevant font attributes/styles of node for find, and
    replaces them with replace.

    Returns a dirty boolean flag
    FT)r   r   striplowerr   )r   findreplacer	   r   r
   s         r   find_replace_fontr   =   so     EJE " 	 	CuSzz!!''))T11w...Lr   c                 0    | j         t          v od| j        v S )z[
    Returns true if the tag in question is a "styled" element that
    can hold text.
    r   )tag	text_tagsattribr   s    r   is_styled_textr   N   s    
 8y ;W%;;r   c                     | j         t          v S )zS
    Returns true if the tag in question is an element that
    can hold text.
    )r   r   r   s    r   is_textr   V   s    
 8y  r   c                 r    g }| j         }|s|S t          D ]!}||v r|                    ||                    "|S )z[
    Given a node, returns a list containing all the fonts that
    the node is using.
    )r   r   append)r   fontssas       r   	get_fontsr!   ^   sQ    
 E
A   66LL1Lr   c                 X    | dk    r#t          j        t          d                     dS dS )z`
    Sends a message to the end user showing success of failure
    of the font replacement
    r   zZCouldn't find anything using that font, please ensure the spelling and spacing is correct.N)inkexerrormsg_)nums    r   report_replacementsr'   m   sD    
 axx* 	
 	
 	
 	
 	
 xr   c                 ^   t          |           dk    r#t          j        t          d                     dS t          |           dk    r,t          j        t          d          | d         z             dS t          j        t          d          d                    |           z             dS )z7
    Tells the user which fonts were found, if any
    r   z1Didn't find any fonts in this document/selection.   z!Found the following font only: %szFound the following fonts:
%s
N)lenr#   r$   r%   join)findingss    r   report_findingsr.   {   s     8}}qLMMNNNNNx==AN1@AAHQKOPPPPPN1=>>8ATATTUUUUUr   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
ReplaceFontz9
    Replaces all instances of one font with another
    c                     |                     d           |                     d           |                     d           |                     d           |                     d           d S )Nz	--fr_findz--fr_replacez--r_replacez--actionz--scope)add_argument)selfparss     r   add_argumentszReplaceFont.add_arguments   sl    +&&&.)))-(((*%%%)$$$$$r   c              #   r   K   t          |          r|V  |D ]}|                     |          D ]}|V  dS )zf
        Recursive method for appending all text-type elements
        to self.selected_items
        N)r   find_child_text_items)r3   r   child	textchilds       r   r7   z!ReplaceFont.find_child_text_items   se      
 4== 	JJJ 	  	 E!77>>    	 	  	 r   c                    g }g }| j         }|dk    r| j         j                                        }|D ]*}|                    |                     |                     +|s!t          j        t          d                    S |S )z
        Depending on the scope, returns all text elements, or all
        selected text elements including nested children
        selection_onlyzThere was nothing selected)svg	selectionvaluesextendr7   r#   r$   r%   )r3   scopeitems	to_returnselecteditems         r   relevant_itemszReplaceFont.relevant_items   s    
 	8$$$x)0022H 	; 	;DLL33D99:::: 	C>!$@"A"ABBBr   c                 ^    d}|D ]}t          |||          r|dz  }t          |           dS )zg
        Walks through nodes, replacing fonts as it goes according
        to find and replace
        r   r)   N)r   r'   )r3   nodesr   r   replacementsr   s         r   find_replacezReplaceFont.find_replace   sK    
  	" 	"D tW55 "!L)))))r   c                 \    d}|D ]}t          ||          r|dz  }t          |           dS )zF
        Walks through nodes, setting fonts indiscriminately.
        r   r)   N)r   r'   )r3   rG   r   rH   r   s        r   replace_allzReplaceFont.replace_all   sI      	" 	"Dg&& "!L)))))r   c                     g }|D ]-}t          |          D ]}||vr|                    |           .t          t          |                     dS )zz
        Walks through nodes, building a list of all fonts found, then
        reports to the user with that list
        N)r!   r   r.   sorted)r3   rG   fonts_foundr   fs        r   list_allzReplaceFont.list_all   sp    
  	* 	*Dt__ * *K''&&q)))* 	{++,,,,,r   c                    | j         j        s!t          j        t	          d                    S | j         j                            d          }| j         j        }|                     |          }|dk    r| j         j        }||dk    r!t          j        t	          d                    S |                                	                                }| j         j
        }||dk    r!t          j        t	          d                    S |                     |||           d S |dk    rM| j         j        }||dk    r!t          j        t	          d                    S |                     ||           d S |d	k    r|                     |           d S d S )
Nz#Nothing to do, no action specified."rI    z-Please enter a search string in the find box.z8Please enter a replacement font in the replace with box.rK   z7Please enter a replacement font in the replace all box.	list_only)optionsactionr#   r$   r%   r   r@   rE   fr_findr   
fr_replacerI   	r_replacerK   rP   )r3   rV   r@   rE   r   r   s         r   effectzReplaceFont.effect   s   |" 	L>!$I"J"JKKK$**
 
 ",,U33^##<'D|trzz~EFF   ::<<%%''Dl-G'R--~PQQ   ndG<<<<<}$$l,G'R--~OPP   ^W55555{""MM.))))) #"r   N)__name__
__module____qualname____doc__r5   r7   rE   rI   rK   rP   rZ    r   r   r0   r0      s         % % %
  
  
   (	* 	* 	** * *
- 
- 
-* * * * *r   r0   __main__)N)r^   r#   inkex.localizationr   r%   r   r   r   r   r   r   r!   r'   r.   EffectExtensionr0   r[   runr_   r   r   <module>rd      s$  (   1 1 1 1 1 1  	 !"@A   (  "< < <! ! !  
 
 

V 
V 
Vl* l* l* l* l*%' l* l* l*^ zKMM r   