
    BPc"C                       d Z ddlZddlZddlmZ ddlZddlmZmZmZm	Z	 ddl
m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mZmZ  G d dej                  Zed	k    r e                                             dS dS )
aT  
Hershey Text 3.0.6, 2022-07-21

Copyright 2022, Windell H. Oskay, www.evilmadscientist.com

Major revisions in Hershey Text 3.0:

1. Migrate font format to use SVG fonts.
    - SVG fonts support unicode, meaning that we can use a full range of
        characters. We are no longer limited to the ASCII range that the
        historical Hershey font formats used.
    - Arbitrary curves are supported within glyphs; we are no longer limited to
        the straight line segments used in the historical Hershey format.
    - The set of fonts can now be expanded.

2. Add a mechanism for adding your own SVG fonts, either within the
    folder containing the default fonts, or from an external file or directory.
    This is particularly important for installations where one does not
    have access to edit the contents of the Inkscape extensions directory.

3. Support font mapping: If a given font face is used for a given block of
    text, check first to see if a matching SVG font is present. If not,
    substitute with the default (selected) stroke font from the list of
    included fonts.

4. Instead of entering text (one line at a time) in the extension,
    this script now converts text (either all text, or all selected text)
    in the document, replacing it in place. While not every possible
    method of formatting text is supported, many are.

    N)deepcopy)	TransformStyleunitsAbortExtension)inkex_gettext)load_svgGroupTextElementFlowParaSVGfontFontFaceFlowSpanGlyphMissingGlyphTspanFlowRoot	RectangleUsePathElementDefsc                        e Zd ZdZ f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g dgdfdZd Z xZS )Hersheyz0
    An extension for use with Inkscape 1.0
    c                 *   t          t          |                                            | j                            dddd           | j                            dddd	           | j                            d
ddd           | j                            dt
          j        ddd           | j                            dddd           | j                            dddd           t                      | _        d| _	        d | _
        d | _        d| _        d| _        d| _        d| _        t                      | _        g | _        d| _        d| _        g | _        g | _        g | _        g | _        g | _        g | _        d| _        d| _        d| _        d S )Nz--tabmoderenderz-The active tab or mode when Apply was pressed)destdefaulthelpz
--fontfacefontfaceHersheySans1z-The selected font face when Apply was pressedz--otherfont	otherfont z'Optional other font name or path to usez
--preservepreserve_textFzPreserve original text)typer   r   r   z--action	util_modesamplezThe utility option selectedz--textsample_textz*
The Quick Brown Fox Jumps Over a Lazy DogzText to use for font tablegY
SUU?r   T   )superr   __init__
arg_parseradd_argumentinkexBooleandictfont_file_listfont_load_fail
svg_height	svg_widthoutput_generatedwarn_unflowwarn_unkernwarn_textpath	font_dictnodes_to_deletevb_scale_factortext_stringtext_familiestext_heightstext_spacingstext_alignstext_xtext_yline_numbernew_linerender_width)self	__class__s    )/usr/share/inkscape/extensions/hershey.pyr+   zHershey.__init__V   s   gt%%'''$$@	 	% 	
 	
 	
 	$$"@	 	% 	
 	
 	
 	$$:	 	% 	
 	
 	
 	$$ ) 	% 	
 	
 	
 	$$.	 	% 	
 	
 	
 	$$A-	 	% 	
 	
 	
 #ff# %   	 !+    g      X@a  ====== Hershey Text Help ======

The Hershey Text extension is designed to replace text in your document (either
selected text or all text) with specialized "stroke" or "engraving" fonts
designed for plotters.

Whereas regular "outline" fonts (e.g., TrueType) work by filling in the region
inside an invisible outline, stroke fonts are composed only of individual lines
or strokes with finite width; much like human handwriting when using a physical
pen.

Stroke fonts are most often used for creating text-like paths that computer
controlled drawing and cutting machines (from pen plotters to CNC routers) can
efficiently follow.

A full user guide for Hershey Text is available to download from
    http://wiki.evilmadscientist.com/hershey


   ==== Basic operation ====

To use Hershey Text, start with a document that contains text objects. Select
the "Render" tab of Hershey Text, and choose a font face from the pop-up menu.

When you click Apply, it will render all text elements on your page with the
selected stroke-based typeface. If you would like to convert only certain text
elements, click Apply with just those elements selected.

If the "Preserve original text" box is checked, then the original text elements
on the page will be preserved even when you click Apply. If it is unchecked,
then the original font elements will be removed once rendered.

You can generate a list of available SVG fonts or a list of all glyphs available
in a given font by using the tools available on the "Utilities" tab.


   ==== How Hershey Text works ====

Hershey Text works by performing font substitution, starting with the text in
your document and replacing it with paths generated from the characters in the
selected SVG font.

Hershey Text uses fonts in the SVG font format. While SVG fonts are one of the
few types that support stroke-based characters, it is important to note that
converting an outline font to SVG format does not convert it to a stroke based
font. Indeed, most SVG fonts are actually outline fonts.

This extension *does not* convert outline fonts into stroke fonts, nor does it
convert other fonts into SVG format. Its sole function is to replace the text
in your document with paths from the selected SVG font.


   ==== Using an external SVG font ====

To use an external SVG font -- one not included with the distribution -- select
"Other" for the name of the font in the pop-up menu on the "Render" tab. Then,
do one of the following:

(1) Add your SVG font file (perhaps "example.svg") to the "svg_fonts" directory
within your Inkscape extensions directory, and enter the name of the font
("example") in the "Other SVG font name or path" box on the "Render" tab.

or

(2) Place your SVG font file anywhere on your computer, and enter the full path
to the file  in the "Other SVG font name or path" box on the "Render" tab.
A full path might, for example, look like:
    /Users/Robin/Documents/AxiDraw/fonts/path_handwriting.svg


   ==== Using SVG fonts: Advanced methods ====

In addition to using a single SVG font for substitution, you can also use
font name mapping to automatically use particular stroke fonts in place of
specific font faces, to support various automated workflows and to support
the rapid use of multiple stroke font faces within the same document.

Several SVG fonts are included with this distribution, including both
single-stroke and multi-stroke fonts. These fonts are included within the
"svg_fonts" directory within your Inkscape extensions directory.

You can select the font that you would like to use from the pop-up menu on the
"Render" Tab. You can also make use of your own SVG fonts.

Order of preference for SVG fonts:

(1) If there is an SVG font with name matching that of the font for a given
piece of text, that font will be used. For example, if the original text is in
font "FancyScript" and there is a file in svg_fonts with name FancyScript.svg,
then FancyScript.svg will be used to render the text.

(2) Otherwise (if there is no SVG font available matching the name of the font
for a given block of text), the face selected from the "Font face" pop-up menu
will be used as the default font when rendering text with Hershey Text.

(3) You can also enter text in the "Name/Path" box, which can represent one of
the following: (i) a font name (for a font located in the svg_fonts directory),
(ii) the path to a font file elsewhere on your computer, or (iii) the path to a
directory containing (one or more) font files.

(3a) Using a font name:
If you move a custom SVG font file into your svg_fonts directory, then you can
enter the name of the SVG font in the "Name/Path" text box and select "Other"
from the pop-up menu. Then, the named font will be used as the default.

(3b) Using a file path:
If you enter the path to an SVG font file in the "Name/Path" text box and
select "Other" from the pop-up menu. Then, that font will be used as the
default. All SVG fonts located in the same directory as that font file will
also be available for name-based font substitution. If there are multiple
font-name matches, files in an external directory take precedence over ones in
the svg_fonts directory.

(3c) Using a directory path:
If you enter the path to a directory containing SVG font files in the
"Name/Path" text box, then all SVG font files files in that directory will be
available for name-based font substitution. If there are multiple font-name
matches, files in an external directory take precedence over ones in the
svg_fonts directory.



Tips about using these methods with your own custom fonts:

(A) These methods can be used to render different text elements with different
SVG font faces. You can even rename a font -- either your own custom one or one
of the bundled ones -- to match the name of a font that you're using. For
example, if you rename a script font you name a font to "Helvetica.svg",
then all text in Helvetica will be replaced with that SVG font.

(B) Using a directory path (3c) is a particularly helpful method if you do
not have access to modify the svg_fonts directory.



   ==== Limitations ====

This extension renders text into non-editable paths, generated from the
character geometry of SVG fonts. Once you have rendered the text, the resulting
paths can be edited with path editing tools, but not text editing tools.

Since this extension works by a process of font substitution, text spanning a
single line will generally stay that way, whereas text flowed in a box (that
may span multiple lines) will be re-flowed from scratch. Style information such
as text size and line spacing can be lost in some cases.

We recommend that you use the live preview option to achieve best results with
this extension.


(c) 2021 Windell H. Oskay
Evil Mad Scientist Laboratories
c                     | j                                                             |          }|r:t          j        |          \  }}|dS h d}||v rdS t          j        |d          S dS )a]  
        Get the <svg> attribute with name "name", and parse it as a length,
        into a value and associated units. Return value in inches.

        This may cause scaling issues in some circumstances. Note, for
        example, that Adobe Illustrator uses 72 px per inch, and Inkscape
        used 90 px per inch prior to version 0.92.
        N>   emex%in)documentgetrootgetr   
parse_unitconvert_unit)rF   namestring_to_parsevalueunit	bad_unitss         rH   getlength_inchzHershey.getlength_inch>  s~     -//1155d;; 	=*?;;KE4}t)))Iy  t%ot<<<trI   c                 b    t          j        |          \  }}|dS t          j        |d          S )z
        Custom replacement for the old "unittouu" routine

        Parse the attribute into a value and associated units.
        Return value in user units (typically "px").
        Importantly, return None for malformed inputs.
        Nr#   )r   rR   rS   )rF   input_stringrV   _s       rH   units_to_userunitszHershey.units_to_userunitsS  s5     #L11q=4!,333rI   c                    |dS |                                                     dd                                          }t          |          dk     rdS t	          |d                   }t	          |d                   }t	          |d                   }t	          |d	                   }	|dk    s|	dk    rdS ||t          t          d
                    t	          |          }
t	          |          }|
dk    s|dk    rdS ||
z  }|	|z  }d}d}||                                                     dd                                                                          }t          |          dk    rb|d         }|dk    r7t          |          dk    r#|d         }t          |          dk    r|d         }n|}t          |          dk    r|d         }|dk    r|
|z  }||	z  }| }| }||||fS 	 ||k    r|dk    s||k     r<|dk    r6|
|z  }|}| }||z  }||	z
  }|dv r| }n|dv r| |z   }n	| |dz  z   }||||fS ||	z  }|}| }|	|z  }||z
  }|dv r| }n|dv r| |z   }n	| |dz  z   }||||fS )a  
        Parse SVG viewbox and generate scaling parameters.
        Reference documentation: https://www.w3.org/TR/SVG11/coords.html

        Inputs:
            viewbox:         Contents of SVG viewbox attribute
            p_a_r:      Contents of SVG preserveAspectRatio attribute
            doc_width:  Width of SVG document
            doc_height: Height of SVG document

        Output: s_x, s_y, o_x, o_y
            Scale parameters (s_x, s_y) and offset parameters (o_x, o_y)

        N)r)   r)   r   r   z,     r   r)         z7Width or height attribute missing on toplevel <svg> tagxmidymidmeetdefernoneslice>   xmaxyminxmidyminxminymin>   xmaxymaxxmidymaxxminymax>   rm   xminymidrj   >   rk   xmaxymidrh   )stripreplacesplitlenfloatr   r\   lower)rF   viewboxp_a_r	doc_width
doc_heightvb_arraymin_xmin_ywidthheightd_widthd_heightar_docar_vb	par_alignpar_mos	par_arraypar0s_xs_yo_xo_yscaled_vb_heightexcess_heightscaled_vb_widthexcess_widths                             rH   vb_scalezHershey.vb_scaleb  s=    ?:==??**455;;==x==1:hqk""hqk""hqk""x{##A::1:
 2 KLL   	""$$a<<8q==:G# 	--dC88>>@@FFHHI9~~!! |7??9~~))$-aL	y>>A--&/lG $I9~~))"+A,
 E/CV#C&C&CS#%%	0 u__7f#4#4e^^'W"4"4 E/CC&C%~,v5M@@@f BBBf},
 f}q00 S#%% f 6/&.<<<&CC >>>&<'CC
 &<!++C Cc!!rI   c                     |                     d          r|                    d          r
|dd         S |                     d          r|                    d          r
|dd         S |S )z
        A multi-word font name may have a leading and trailing
        single or double quotes, depending on the source.
        If so, remove those quotes.
        'r)   ")
startswithendswithrF   fontnames     rH   strip_quoteszHershey.strip_quotes  s{     s## 	"(9(9#(>(> 	"AbD>!s## 	"(9(9#(>(> 	"AbD>!rI   c                 t   d}|dS |D ]}t          |t                    r|                     |          c S t          |t                    rh	 t	                      }t	                      }t	                      }t	                      }|                    d          |d<   |                    d          }|t          |          |d<   |D ]}t          |t                    r	 |                    d          }	n#  Y 3xY w|	9|	|v r>t	                      }
|                    d          |
d<   |                    d          }|t          |          |
d<   n|d         |
d<   |                    d	          |
d	<   |
||	<   t          |t                    r|                    d
          |d<   |                    d          }|d|d<   nt          |          |d<   |                    d          }|t          |          |d<   |                    d          }|t          |          |d<   gt          |t                    rR|                    d          }|t          |          |d<   n|d         |d<   |                    d	          |d	<   ||d<   d|d         z  |d<   ||d<   ||d<   |c S dS )a  
         Parse an input svg, searching for an SVG font. If an
         SVG font is found, parse it and return a "digest" containing
         structured information from the font. See below for more
         about the digest format.

         If the font is not found cannot be parsed, return none.

         Notable limitations:

        (1) This function only parses the first font face found within the
         tree. We may, in the future, support discovering multiple fonts
         within an SVG file.

        (2) We are only processing left-to-right and horizontal text,
         not vertical text nor RTL.

        (3) This function currently performs only certain recursive searches,
         within the <defs> element. It will not discover fonts nested within
         groups or other elements. So far as we know, that is not a limitation
         in practice. (If you have a counterexample please contact Evil Mad
         Scientist tech support and let us know!)

        (4) Kerning details are not implemented yet.
        Nidfont_idzhoriz-adv-xhoriz_adv_xunicodez
glyph-name
glyph_namedfont-familyfont_familyzunits-per-emg     @@units_per_emascentdescentmissing_glyph      ?scaleglyphsgeometry)

isinstancer   parse_svg_fontr   r0   rQ   rt   r   r   r   )rF   	node_listdigestnoder   r   r   r   elementuni_text
glyph_dictr   r   r   s                 rH   r   zHershey.parse_svg_font  s   6 4 M	 M	D$%% 1**400000$(( I.` 66 $$(HHTNNy!"hh}55*.3K.@.@H]+  $ C@ C@G!'511 A@%'.{{9'='=HH%$H#+$#v-- %%)VV
3:;;|3L3L
<0&-kk-&@&@&28=k8J8JJ}558@8OJ}5*1++c*:*:
3+5x((#GX66 #@07M0J0J}-'.{{>'B'B'/7=H^447<\7J7JH^4!(X!6!6!-16vHX.")++i"8"8".27..HY/ $G\:: 	@&-kk-&@&@&2;@;M;MM-88;CM;RM-8-4[[-=-=c*2?/ #&(@"@w#)x %-z"SIT ts   #C99C=c                    |dS || j         v rdS || j        v r| j        |         }nd| j         |<   dS 	 	 t          |          }|                     |                                          | j         |<   dS # t
          $ r d| j         |<   Y dS  t          j        t          d          	                    t          |                               d| j         |<   Y dS xY w)z
        Attempt to load an SVG font from a file in our list
        of (likely) SVG font files.
        If we can, add the contents to the font library.
        Otherwise, add a "None" entry to the font library.
        NzError parsing SVG font at {})r9   r1   r	   r   rP   IOErrorr.   errormsgr\   formatstr)rF   r   the_pathfont_svgs       rH   	load_fontzHershey.load_font  s    Ft~%%Ft****84HH'+DN8$F	,
  ))H'+':':8;K;K;M;M'N'NDN8$$$ 	, 	, 	,'+DN8$$$$	,N1;<<CCCMMRRSSS'+DN8$$$$s   ?A5 5CACc           	         d| j         _        | j                                                            t                                }| j        D ]}|                     |           d}t          || j	        z            dz   }t          d|dddd          }|| j	        z  }d	|z  }|}t          | j                  D ]}| j        |         dt          |          dd}	|                    t          di |	          }
||
_        ||
_        t          |          t          |          d}	t          d|d|dd          }|                    t          di |	          }	 | j         j                            d          |_        n!# t$          $ r | j         j        |_        Y nw xY w||_        ||z  }|                     || j                   d
S )zC
        Generate display table of all available SVG fonts
        Fg?pxrf   blackz
sans-serifendstroke	font-sizefillr   text-anchor      ?N0true)xyhershey-ignorer   r   startzutf-8 )optionsr$   svgget_current_layeraddr
   r1   r   r   r;   r   sortedr9   r   textstyler(   decodeAttributeErrorrecursively_traverse_svgdoc_transform)rF   groupr   	font_sizefont_size_textlabeltext_stylex_offsety_offsetr   text_attribstextlinesampletext_style
samplelines                rH   
font_tablezHershey.font_table  s   
 &+" **,,0099+ 	% 	%HNN8$$$$	Y)==>>E ++$ 
 
 t33>t~.. 	 	H~h'/!$3q66VLLLyy!<!<|!<!<==H$HM,HN!$XSVV<<L$$!/##+#*     ;#>#>#>#>??J;"&,":"A"A'"J"J
! ; ; ;"&,":
;  0JMAA%%eT-?@@@@@s   $FF! F!c                    d| j         _        |                     d          }| j        r#t	          j        t          d                     dS | j                                        	                    t                                }d}| j        |         d         D ]&}| j        |         d         |         d         |dz  }'t          t          j        t          j        |                              }d	}t!          || j        z            d
z   }t%          d|d|dd          }d|z  | j        z  }	|	}
|	}|
}d}t'          | j        |         d                   D ]}| j        |         d         |         d         "t)          ||          \  }}|	|dz   z  }|
|dz   z  }t!          |          t!          |          d}|	                    t+          di |          }||_        ||_        |dz   }|                     || j                   dS )z
        Generate display table of glyphs within the current SVG font. Sorted display of
        all printable characters in the font _except_ missing glyph.
        Fnot_a_font_namez/Font not found; Unable to generate glyph table.Nr   r   r   r)   g?r   rf   r   r   r   r   r   r   )r   r$   font_load_wrapperr2   r.   r   r\   r   r   r   r
   r9   intmathfloorsqrtr   r;   r   r   divmodr   r   r   r   r   )rF   r   r   glyph_countglyphcolumnsr   r   glyph_styler   r   r   r   draw_positiony_posx_posr   r   s                     rH   glyph_tablezHershey.glyph_table  s-    &+"))*;<< 	N1NOOPPPF **,,0099 ^H-h7 	! 	!E~h'1%8=Iq dj;!7!78899	Y)==>>E +'& 
 
 ?T%99DN84X>?? 
	. 
	.E~h'1%8=E!-99LE5EAI&AEAI&A!$Qc!ff55L;#>#>#>#>??J#JO*J)A-MM%%eT-?@@@@@rI   c                    t                      | _        d}t          j                            t          j                            t          j                    |                    }t          j        |          D ]}|                    d          rkt          j                            ||          }t          j        	                    |          r,t          j        
                    |          \  }}|| j        |<   t          j        
                    | j        j                  \  }}|| j        v r| j        j        dk    r|| j        _        dS t          j                            | j        j                  }t          j        	                    |          rt          j                            |          \  }}	t          j        
                    |	          \  }}
|| j        |<   | j        j        dk    r|| j        _        t          j        |          D ]}|                    d          rkt          j                            ||          }t          j        	                    |          r,t          j        
                    |          \  }}|| j        |<   dS t          j                            |          rt          j        |          D ]}|                    d          rkt          j                            ||          }t          j        	                    |          r,t          j        
                    |          \  }}|| j        |<   dS dS )a	  
         Create list of "plausible" SVG font files

         List items in primary svg_fonts directory, typically located in the
         directory where this script is being executed from.

         If there is text given in the "Other name/path" input, that text may
         represent one of the following:

        (A) The name of a font file, located in the svg_fonts directory.
             - This may be given with or without the .svg suffix.
             - If it is a font file, and the font face selected is "other",
                 then use this as the default font face.

        (B) The path to a font file, located elsewhere.
             - If it is a font file, and the font face selected is "other",
                 then use this as the default font face.
             - ALSO: Search the directory where that file is located for
                 any other SVG fonts.

        (C) The path to a directory
             - It may or may not have a trailing separator
             - Search that directory for SVG fonts.

         This function will create a list of available files that
         appear to be SVG(SVG font) files. It does not parse the files.
         We will format it as a dictionary, that maps each file name
         (without extension) to a path.
        	svg_fonts)z.svgz.SVGotherN)r0   r1   ospathrealpathjoingetcwdlistdirr   isfilesplitextr   r"   r    rr   isdir)rF   font_directory_namefont_dirdir_item	file_pathrootr\   	test_path	directory	file_nameexts              rH   find_font_fileszHershey.find_font_filesV  s   > #ff *7##BGLL>Q$R$RSS
8,, 	: 	:H  !122 :GLL8<<	7>>),, : g..x88GD!09D'- '""4<#9::a 4&&& |$//(,%FG$$T\%;<<	 7>>)$$ 	#%7==#;#; Iy((33ID#(1D%|$//(,%
 Jy11 > >$$%566 > "Y A AIw~~i00 >"$'"2"28"<"<a4=+D1F 7==## 	>Jy11 > >$$%566 > "Y A AIw~~i00 >"$'"2"28"<"<a4=+D1	> 	>> >rI   c                     |                      |           	 | j        |         ,| j        j        }|| j        vr|                      |           n	 | j        |         	d| _        dS |S )a  

        This implements the following logic:

        * Check to see if the font name is in our lookup table of fonts,
            self.font_dict

        * If the font is not listed in font_dict[]:
            * Check to see if there is a corresponding SVG font file that
            can be opened and parsed.

            * If the font can be opened and parsed:
                * Add that font to font_dict.
            * Otherwise
                * Add the font name to font_dict as None.

        * If the font has value None in font_dict:
            * Try to load fallback font.

        * Fallback font:
            * If an SVG font matching that in the SVG is not available,
            check to see if the default font is available. That font
            is given by self.options.fontface

        * If a font is loaded and available, return the font name.
            Otherwise, return none.

        NT)r   r9   r   r    r2   r   s     rH   r   zHershey.font_load_wrapper  s    < 	x   	 >(#+ |,Ht~--x((((>(#+  4rI   c                    |                      |          }|dS 	 | j        |         d         }n	#  d}Y nxY w	 || j        |         d         vr5| j        |         d         d         }| j        |         d         d         ||fS | j        |         d         |         d         }| j        |         d         |         d         ||fS #  Y dS xY w)z
        Given a font face name and a character(unicode point),
            return an SVG path, horizontal advance value,
            and scaling factor.

        If the font is not available by name, use the default font.
        Nr   gMbP?r   r   r   r   )r   r9   )rF   r   charscale_factorx_advs        rH   get_font_charzHershey.get_font_char  s     ))(334	!>(3G<LL	! LLL	4>(3H===x0A-P N8,_=cB  
 N8,X6t<]KE>(+H5d;C@%UU	44s   / 5A	C A C C	c                    |                      d          | _        |                      d          | _        | j                                        | _        | j                            d          }|rB| j                            d          }|                     ||| j        | j                  \  }}}}ndt          | j	                  z  }|}d}d}t          ||f||f          | _        ||z   dz  | _        d	S )
zE
        Wrapper function for processing viewbox information
        r~   r}   viewBoxpreserveAspectRatior           )r   	translate       @N)rY   r3   r4   rO   rP   r   rQ   r   rt   PX_PER_INCHr   r   r;   )rF   rv   rw   r   r   r   r   s          rH   handle_viewboxzHershey.handle_viewbox  s    
 --h77,,W55=((**(,,y)) 		HLL!677E!%" "Cc33 d.///CCCC 'c3ZC:NNN #c	S0rI   c                 "   |d         }|d         }|d         }|d         }|d         }d}|d         | j         z  }		 |                     ||          \  }
}}n#  d}d	}
d}Y nxY w| j        rdS |||z  z  }d}d}t          || f
          }||	z  }|dk    rd}| j        |z  }t          j        |          }|dk    rd                    |          }n;t          t          j	        |           dz             }d                    ||          }d|i}t          ||z   ||z   f          }||z  }|
K|
                    t                                }|                    |
           ||_        ||_        d| _        |t!          |          |z  z   S )z0
        Render an individual svg glyph
        r
  r   offset
vertoffsetfont_heightr   stroke_scaler   N)r   r)   z	{0:.3f}inrb   z{0:.{1}f}inzstroke-widthr  T)r;   r  r2   r   rE   r   log10r   r   ceilr   r   set_pathr   	transformr5   rt   )rF   chardataparentr
  r   r  r  r  
font_scaler  path_stringadv_xr  h_offsetv_offsetscale_transform_scalestroke_widthlog_tenwidth_stringprecp_stylethe_transformpath_elements                           rH   draw_svg_textzHershey.draw_svg_text!  s    }-(#l+
}-
  /$2FF	/3/A/A+t/T/T,K	EKLLL 	1l[00
 $:
{*CDDD l*Q;;F(61
 *\**Q;;&--l;;LLty'**Q.//D(//dCCL!<0!Vh->
X@U,VWWW("!::kmm44L!!+...!(L%2L"$(D!ez111s   A A!c                    |                                 }|f|                     |          }|                    dd          }||S t          |          j        }||S t          |          t          |          z  S | j        S )zm
        Determine the cumulative transform which node inherits from
        its chain of ancestors.
        Nr  )	getparentrecursive_get_encl_transformrQ   r   matrixr   )rF   r   parent_transformnode_transformtranss        rH   r3  z$Hershey.recursive_get_encl_transform`  s     ~~#@@FF!XXk488N%''n--4E'-..51A1AAA!!rI   c                    |d         }|d         }|d         }|d         }|D ]}|j         } |d          }		 |                     |	          }n# t          $ r Y nw xY w|                      |d                    }	  |d          }
d|
v r&t	          |
                    d                    d	z  }n|
d
k    rd}n|                     |
          }n# t          $ r Y nw xY w |d          }|j        | xj        |j        z  c_        |j        D ]j}| j        	                    |           | j
        	                    |           | j        	                    |           | j        	                    |           kt          |t          t          f          r8t!                      }||d<   ||d<   ||d<   ||d<   |                     ||           |j        |d         }|d         }|d         }|d         }| xj        |j        z  c_        |j        D ]j}| j        	                    |           | j
        	                    |           | j        	                    |           | j        	                    |           kt          |t                    rx| xj        dz  c_        | j        	                    |           | j
        	                    |           | j        	                    |           | j        	                    |           dS )z8
        Parse a flowroot node and its children
        r  r   line_spacingalignr   r   line-heightrM         Y@normal      ?
text-alignN
)r   r]   	TypeErrorr   rt   rstripr   r<   r=   appendr>   r?   r@   r   r   r   r0   recursively_parse_flowroottail)rF   r   parent_infofont_height_localfont_family_localline_spacing_localtext_align_localr   
node_styler  r9  r\   	the_styles                rH   rD  z"Hershey.recursively_parse_flowroots  s    (6'6(8&w/ =	: =	:DJ$*[11K$($;$;K$H$H!!    !% 1 1**]2K2K L L	)z-88,&&).|/B/B3/G/G)H)H5)P&&!X--)-&&)-)@)@)N)N&     *z,77y$  DI-   > >A&--.?@@@%,,->???&--.@AAA$++,<====$8 455 A FF	+<	-(+<	-(,>	.)%5	'"//i@@@y$$/$>!$/$>!%0%@"#.w#7   DI-   > >A&--.?@@@%,,->???&--.@AAA$++,<====$)) :  D(  "))*;<<<!(():;;;"))*<=== ''(8999{=	: =	:s$   A
AA>AC
CCc                 L   |d         }|d         }|d         }|d         }|d         }|d         }|j         }	 |	d          }
	 |                     |
          }n# t          $ r Y nw xY w|                      |	d                    } |	d	          }	 |                    d
          }||}n# t
          $ r Y nw xY w	 |                    d          }||}n!|d}t          |          | j        |z  |z  z   }n# t
          $ r Y nw xY w|j        | xj	        |j        z  c_	        |j        D ]}| j
                            |           | j                            |           | j                            |           | j                            |           | j                            |           |D ]}t!          |t"                    rrt%                      }||d<   ||d<   ||d<   ||d<   ||d<   ||d<   d}|                    d          }|dk    rd}|                     ||           |r| j        dz   | _        |j        |j                                        }||d         }|d         }|d         }|d         }|d         }| xj	        |z  c_	        |D ]}| j                            |           | j
                            |           | j                            |           | j                            |           | j                            |           dS dS dS )z4
        parse a text node and its children
        r  r   anchorr   r   r9  r   r   r   r   Nr   r   Fzsodipodi:rolelineTr)   )r   r]   rA  r   rQ   
ValueErrorrt   rC   r   r<   r=   rC  r>   r@   rA   rB   r   r   r0   recursively_parse_textrE  rp   )rF   r   rF  rG  rH  anchor_localx_localy_localparent_line_spacingrK  r  x_tempy_tempr\   sub_node	node_infoadv_linerole_stripped_tailrJ  s                       rH   rQ  zHershey.recursively_parse_text  s    (6'6"8,g&g&).9Z
 j--	 $ 7 7 D D 	 	 	D	 !--jj.G.GHH!z-00	XXc]]F!  	 	 	D		XXc]]F!  ?G'NN&)<<?PPQ   	 	 	D	 9 	)Y , ,"))*;<<<!(():;;; ''555""7+++""7++++ 	< 	<H (E** < !FF	+<	-(+<	-(&2	(#%,	'"%,	'",?	.) ||O446>>#H++Hi@@@  <'+'7!';D$9 !Y__..N)$/$>!$/$>!#.x#8 %g.%g.  N2  ' 0 0A%,,->???&--.?@@@$++,<===K&&w///K&&w//// ! ))0 0s6   A 
A'&A'B. .
B;:B;?;C; ;
DDr   r  r  r  r   r  visiblec                    |D ]_}|                     d|          }|dk    r|}|dv r&|j        }t          |          t          |          z  }t          |t                    r<d}|                     d          }	|	sn|	dk    rd}|r|                     |||           t          |t                    rs|j        }
|
t          |          }t          |                     d	d
                    }t          |                     dd
                    }|dk    s|dk    r6d	                    ||          }t          |          t          |          z  }n|}	 |
                    t	                                }n.# t          $ r! t          j         |d                     Y w xY w|                    d|           |                    t!          |
                     |D ]}| j                            |           | j        j        s| j                            |           &t          |t(          t*          f          
rd| _        d}d}|                     d          }|                     d          }|                     d	          }|                     d          }d}d}d}d}	 |                     d          }|n# t0          $ r Y nw xY w|j        }	 |                     dd          }|                     |          }n# t4          $ r Y nw xY w|                      |d                    }	  |d          }d|v r&t          |                    d                    dz  }n|dk    rd}n|                     |          }n# t4          $ r Y nw xY w	 |j        }n# t0          $ r Y nw xY w|t          |          j        } 	 t=          j        | d         d         | d         d         z  | d         d         | d         d         z  z             }!t=          j        | d         d         | d         d         z  | d         d         | d         d         z  z             }"|!|"z   dz  }#nd }#|                     d!          }$d| _         g | _!        g | _"        g | _#        g | _$        g | _%        g | _&        |'                                }%|%
                    t	                                }&d|&_(        d"d#d$d$d%}'|'|&_        d}(d})d}*d}+d},	 t          |t*                    r	 |d&         }n# tR          $ r Y nw xY w| j*        +                    d'|$z            d         }-t          |-tX                    r|-j-        }|-j.        }|-j/        }.|-j0        }d}nt          |-t                    r	 |s	d| _1        	 te                      }/||/d(<   ||/d)<   ||/d*<   ||/d+<   | 3                    ||/           | j         dk    r| j         4                                rd}0d}1d}2| j         5                                }3| j         5                    d          }4d}5d,}6tm          |3          D ]\  }7}8to          |8          }9to          |4|7                   }:d})d};|9dk    rB|5}<t          | j"        |<                   }=t          | j#        |<                   }>|>|=z  }?|0|?z   }0n8|)|9k     r1|)}@|)|9k     r|8|)         }A|5|)z   }<| j"        |<         }=| j!        |<         }B	 | 8                    |B|A          \  }}C}Dn#  d}Cd}DY nxY w|+|C|Dz  |=z  z  }+|)dz  })|A4                                r|A|6k    sn|)|9k     d}E|+|k    r|@|;k    rd}End}E|@})n|)|9k    rd}E|Er|&
                    t	                                }Fd}+d}*d| _        d},d}G|;}H|H|)k     rQ|5|Hz   }<t          | j"        |<                   }=t          | j#        |<                   }>|>|=z  }?|?|Gk    r|?}G|Hdz   }H|H|)k     Q|0|Gz   }0te                      }I|0|Id-<   |#|Id.<   |;}H|H|)k     rn|5|Hz   }(| j"        |(         }=| j!        |(         }B| j$        |(         }|8|H         |Id/<   |=|Id(<   |B|Id)<   |*|Id0<   | 9                    |I|F          }*|*},|Hdz   }H|(dz   }(|H|)k     n|)};d}J|d1k    r$t          t          |          |,z
  d2z  3          }Jn&|d4k    r t          t          |          |,z
  3          }J|J|J|F_        |1r|Gd5z  }2d}1|)|9k     1|5|:z   }5|5}(t          |t          |          |2z
  f3          }Jnl	  |d6          }	 te                      }K||Kd(<   ||Kd)<   ||Kd7<   ||Kd8<   ||Kd9<   ||Kd*<   d| _:        | ;                    ||K           | j         dk    r	| j         4                                r	d: | j         D             }Lto          |L          }M|&
                    t	                                }Fd})|)|Mk     r	 t          | j%        |)                   }Nn# t0          $ r d| _<        Y  dS w xY wt          | j&        |)                   }O|)|Mk     r9|L|)         }P| j"        |)         }Qte                      }I|P|Id/<   | j!        |)         |Id)<   |Q|Id(<   |*|Id0<   d|Id-<   |#|Id.<   | 9                    |I|F          }*|*},|*}+d}R|)dz   }S|S|Mk    rd}Rn>t          | j%        |S                   |Nk    st          | j&        |S                   |Ok    rd}R|Rrw| j$        |)         }d}T|d;k    r	|N|,d2z  z
  }Tn|d4k    r|N|,z
  }Tn|N}T|O}Ut          |T|Uf3          }J|J|F_        |&
                    t	                                }Fd| _        d}*|)dz  })n|)dz  })|)|Mk     9|)|Mk     t                      }Jto          |F          dk    r)|F'                                }%|%=                    |F           |t          |          |Jz  }Vn|J}V|V|&_        | j>        s)|&'                                }%|%=                    |&           | j        j        s!| j>        r| j                            |           adS )<zw
        recursively parse the full document and its children,
        looking for nodes that may contain text
        
visibilityinherit)hiddencollapseTzinkscape:labelzHershey TextFNr   r   r   r   ztranslate({0:.6E}, {1:.6E})z7Unable to process text. Consider unlinking cloned text.r  z1.2516pxd   r#   r   r   r      r   r;  rM   r<  r=  r>  r)   r  r   r   z#000000rf   round)r   r   zstroke-linecapzstroke-linejoinr?  z'/svg:svg//*[@id="%s"]/svg:flowRegion[1]r  r   r9  r:      r  r  r
  r  centerra   r  r   rb   r   rN  r   r   c                     g | ]}|S r   r   ).0qs     rH   
<listcomp>z4Hershey.recursively_traverse_svg.<locals>.<listcomp>;  s    "?"?"?1"?"?"?rI   middle)?rQ   r  r   r   r
   r   r   hrefrt   r   r   r   r.   r   setrC  r   r:   r   r$   r   r   rD   r]   rP  r   rA  r   rB  r4  r   r   r<   r=   r>   r?   r@   rA   rB   r2  labelKeyErrorr   
getElementr   lefttopr~   r}   r6   r0   rD  isspace
splitlines	enumeraters   r  r0  rC   rQ  r7   remover5   )WrF   
anode_listmat_currentparent_visibilityr   vis_matrixmat_newrecurse_group	ink_labelrefnodelocal_transformr   r   _trans_stringref_transform	ref_groupr\   rX  start_xstart_yr9  r  bounding_rect
rect_widthr  
text_alignhershey_ignorerK  font_height_tempr   line_spacing_temp
transform2scale_xscale_yscale_rthe_idr!  r   r   str_posiww_tempwidth_this_lineflowrefrect_heightrL  v
first_liney_offs_overall
text_linesextd_text_linesstr_pos_eolnbsprC   	text_lineline_lengthextd_line_length
line_startstr_pos_tempchar_heightcharline_spacingchar_v_spacing
word_start	characterchar_familyr  r  render_line
line_groupline_max_v_spacingj	char_datar.  rY  letter_valsstr_lenx_start_liney_start_lineq_valcharfont_heightset_alignmenti_nextx_shifty_shiftresultsW                                                                                          rH   r   z Hershey.recursively_traverse_svg!  s     y		6 y		6D ((<):;;Ci',,, nG,,y/A/AAG$&& l	6 $ HH%566	   N22! & ! F11$EEED#&& ]	6 )?"+G"4"4$((3,,--$((3,,--FFQ$A$H$HA$N$NM$-g$6$6=9Q9Q$QMM$3M *uww 7 7II%   NSTT   H	 k=999  '!2!2333 ) : :H (//9999 |1 6(//555D;"9:: h6 !%     $66v>>"55f==((3--((3-- %   	$
%)XX.>%?%?N%1  2!   D "Z
'1~~k2'F'F$"&"9"9:J"K"KKK    D #//

=0I0IJJ(2
=(A(A%000',->-E-Ec-J-J'K'Ke'S*h66'+'+'>'>?P'Q'Q    D $II!   D (!*9!5!5!<J
 #i"1a(:a=+;;$Q-*Z]1-==> G #i"1a(:a=+;;$Q-*Z]1-==> G
  '0C7GG!G$ $&  " %'!%'"#%    ))

577++, ("&-'.	  $   dH-- Q0%/%=

#    #h11AFJ G "'955 "),")+&-n%,]
(,#GS11 H ) !+/( 	 !%I/:Im,/:Im,0<In-)3Ig&33D)DDD'2-- '//11 !  A "&J &'N "&!1!<!<!>!>J&*&6&A&A$&G&GO"#K!D2;J2G2G R. R..Y&))nn+.{/K+L+L(
 &'
&!+++6L*/0A,0O*P*PK/4T5G5U/V/V,-=-KN !N 2AA"#k// %& !+ '(+oo 1:()1&I 4??L262CL2QK262D\2RK%9AEASAS,7B* B*(>5,,%90178$*el.B[.P$PF$%FA'0'8'8':': %.9PTCTCT(-/ '(+oo2 /4$*Z$7$7'1Z'?'? 7; 7;,6%&+%5%526K#. M!;165771C1CJ-.F()A48DM67O9:$6(2A*+a%%7BQ6;,0,=l,K7* 7* <A,0,>|,L<* <*(8 :JK9W+9<N+N+NAO,>,-E +,a%% )*,>(>A04I>?Il$;@GIn$=(2A*+a%%2=/6:6G6P6:6H6Q595Eg5N
<EaL	&(9CN	-(@CN	-(@>?	((;,0,>,>y*,U,U:;,-E29A+# +,a%%& 23J 59M'1X'='=8A16z1B1B_1T23149* 9* 9* *4u)<)<8A05j0A0AO0S9* 9* 9*
 (5'@?L
(<'1 %;,>,B )7 6;
y #$k//| '24D&D"-$-#*E'NN^,K"L% % %MM

 ",M!:!:J8 !%I/:Im,/:Im,*4Ih')0Ig&)0Ig&0<In-
 ()D$//i@@@ '2-- '//11 ! "?"?d.>"?"?"?K!+..G "'577!3!3JAg++#+0Q+@+@LL) # # #/3D,"FFF# (-T[^'<'<'kk %0NE.2.?.BO(,I05If-7;7I!7LIm47FIm423Ih/67Il38?In5 $ 2 29j I IA./O%&F
 -2M%&UF%0004"'F(;"<"<"L"L %dk&&9 : :l J J04, !&-1-=a-@
 +,#-#9#9.:oPQ>Q.RGG%/5%8%8.:_.LGG.:G*609WgDV0W0W0W7D
 4-2YY$)GG." ."
 15$% !Q %FAE  'kk g++\ %.KKMz??a'''1133FMM*--- (&y11MAFF*F"(, )"__..FMM%((( |1 6d6K 6(//555sy		6 y		6s   !E<<'F'&F'.K
KK +L
LL;AN
NN N((
N54N5&T//
T<;T<\,,\4h##h98h9c                    | j         j                            d          | j         _        | j         j                            d          | j         _        | j         j                            d          | j         _        | j         j                            d          | j         _        | j         j                            d          | j         _        g dg dg| _        |                                  | 	                                 d| j
        dz  z  }| j        /| j        | j        k     r| j        |z  | _        n| j        |z  | _        | j         j        dk    rt          j        | j                   n| j         j        dk    r:| j         j        d	k    r|                                  n|                                  n| j         j        r\| j         j        D ]N}|                     | j        j        |                   }|                     | j        j        |         g|           On2|                     | j                                        | j                   | j        D ]/}|+|                                }||                    |           0| j        r!t          j        t=          d
                     | j        r!t          j        t=          d                     | j         r#t          j        t=          d                     dS dS )zE
        Main entry point; Execute the extension's function.
        r   r]  r^  r)   g      @Nr   	utilitiesr'   z)Warning: unable to load SVG stroke fonts.zbWarning: unable to render text.
Please use Text > Remove Manual Kerns to convert it prior to use .zWarning: unable to convert text flowed into a frame.
Please use Text > Unflow to convert it prior to use.
If you are unable to identify the object in question, please contact technical support for help.)!r   r   rp   r    r"   r&   r(   r   r  r  r  r4   r3   rE   r.   r   	help_textr   r   idsr3  r   	selectionr   rO   rP   r:   r2  rz  r2   r\   r7   r6   )rF   _rendered_stroke_scaleid_refr  element_to_remover!  s         rH   effectzHershey.effect  s    !L-33C88 $ 5 ; ;C @ @!%!7!=!=c!B!B!%!7!=!=c!B!B#'<#;#A#A##F#F -oo?
 "#d&6&>!?>%~//$(N5K$K!!$(O6L$L!<&&N4>****\+--|%11!!!!  """"| "l.  F $ A A*62! !I 11+F34i   	 --M))++T-?   "&!5 	5 	5 ,*4466%MM"3444 	KN1HIIJJJ 	N[     	NC     	 	rI   )__name__
__module____qualname____doc__r+   r  r  rY   r]   r   r   r   r   r   r   r  r   r  r  r0  r3  rD  rQ  r   r  __classcell__)rG   s   @rH   r   r   P   s        J J J J JX KXIt  *4 4 4V" V" V"p  n n n`&, &, &,P9A 9A 9Av:A :A :AxV> V> V>p4 4 4l     D1 1 16=2 =2 =2~" " "&H: H: H:Tb0 b0 b0N %__ooo6#	D
6 D
6 D
6 D
6LM M M M M M MrI   r   __main__) r  r   r   copyr   r.   r   r   r   r   inkex.localizationr   r\   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Effectr   r  runr   rI   rH   <module>r     s  ( @ 
			         9 9 9 9 9 9 9 9 9 9 9 9 1 1 1 1 1 1                                 &d d d d del d d dN= zGIIMMOOOOO rI   