diff options
| author | Nicolas Dufour <nicoduf@yahoo.fr> | 2011-08-25 19:41:31 +0000 |
|---|---|---|
| committer | JazzyNico <nicoduf@yahoo.fr> | 2011-08-25 19:41:31 +0000 |
| commit | 1e4cf85b3e90bcf4f307dc328bdf8dc733ebdfff (patch) | |
| tree | ada5561551cd3a4d3d5610d9d749f5e688abddcb | |
| parent | Filters. Removing unecessary height, width, x and y attributes in some filter... (diff) | |
| download | inkscape-1e4cf85b3e90bcf4f307dc328bdf8dc733ebdfff.tar.gz inkscape-1e4cf85b3e90bcf4f307dc328bdf8dc733ebdfff.zip | |
Extensions. Missing files for Text>Extract...
(bzr r10581)
| -rw-r--r-- | share/extensions/text_extract.inx | 32 | ||||
| -rw-r--r-- | share/extensions/text_extract.py | 153 |
2 files changed, 185 insertions, 0 deletions
diff --git a/share/extensions/text_extract.inx b/share/extensions/text_extract.inx new file mode 100644 index 000000000..bf3a66e60 --- /dev/null +++ b/share/extensions/text_extract.inx @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> + <_name>Extract</_name> + <id>org.inkscape.text.extract</id> + <dependency type="executable" location="extensions">text_extract.py</dependency> + <dependency type="executable" location="extensions">inkex.py</dependency> + <param name="direction" type="enum" _gui-text="Text direction:"> + <_item value="lr">Left to right</_item> + <_item value="bt">Bottom to top</_item> + <_item value="rl">Right to left</_item> + <_item value="tb">Top to bottom</_item> + </param> + <param name="xanchor" type="enum" _gui-text="Horizontal point:"> + <_item value="l">Left</_item> + <_item value="m">Middle</_item> + <_item value="r">Right</_item> + </param> + <param name="yanchor" type="enum" _gui-text="Vertical point:"> + <_item value="t">Top</_item> + <_item value="m">Middle</_item> + <_item value="b">Bottom</_item> + </param> + <effect> + <object-type>all</object-type> + <effects-menu> + <submenu _name="Text"/> + </effects-menu> + </effect> + <script> + <command reldir="extensions" interpreter="python">text_extract.py</command> + </script> +</inkscape-extension> diff --git a/share/extensions/text_extract.py b/share/extensions/text_extract.py new file mode 100644 index 000000000..a27cfff50 --- /dev/null +++ b/share/extensions/text_extract.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +""" +Copyright (C) 2011 Nicolas Dufour (jazzynico) +Direction code from the Restack extension, by Rob Antonishen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +""" +import inkex, os, csv, math, chardataeffect, string + +try: + from subprocess import Popen, PIPE + bsubprocess = True +except: + bsubprocess = False + +class Extract(inkex.Effect): + def __init__(self): + inkex.Effect.__init__(self) + self.OptionParser.add_option("-d", "--direction", + action="store", type="string", + dest="direction", default="tb", + help="direction to extract text") + self.OptionParser.add_option("-x", "--xanchor", + action="store", type="string", + dest="xanchor", default="m", + help="horizontal point to compare") + self.OptionParser.add_option("-y", "--yanchor", + action="store", type="string", + dest="yanchor", default="m", + help="vertical point to compare") + + def effect(self): + if len(self.selected)==0: + for node in self.document.xpath('//svg:text | //svg:flowRoot', namespaces=inkex.NSS): + self.selected[node.get('id')] = node + + if len( self.selected ) > 0: + objlist = [] + svg = self.document.getroot() + parentnode = self.current_layer + file = self.args[ -1 ] + + #get all bounding boxes in file by calling inkscape again with the --query-all command line option + #it returns a comma seperated list structured id,x,y,w,h + if bsubprocess: + p = Popen('inkscape --query-all "%s"' % (file), shell=True, stdout=PIPE, stderr=PIPE) + err = p.stderr + f = p.communicate()[0] + try: + reader=csv.CSVParser().parse_string(f) #there was a module cvs.py in earlier inkscape that behaved differently + except: + reader=csv.reader(f.split( os.linesep )) + err.close() + else: + _,f,err = os.popen3('inkscape --query-all "%s"' % ( file ) ) + reader=csv.reader( f ) + err.close() + + #build a dictionary with id as the key + dimen = dict() + for line in reader: + if len(line) > 0: + dimen[line[0]] = map( float, line[1:]) + + if not bsubprocess: #close file if opened using os.popen3 + f.close + + #find the center of all selected objects **Not the average! + x,y,w,h = dimen[self.selected.keys()[0]] + minx = x + miny = y + maxx = x + w + maxy = y + h + + for id, node in self.selected.iteritems(): + # get the bounding box + x,y,w,h = dimen[id] + if x < minx: + minx = x + if (x + w) > maxx: + maxx = x + w + if y < miny: + miny = y + if (y + h) > maxy: + maxy = y + h + + midx = (minx + maxx) / 2 + midy = (miny + maxy) / 2 + + #calculate distances for each selected object + for id, node in self.selected.iteritems(): + # get the bounding box + x,y,w,h = dimen[id] + + # calc the comparison coords + if self.options.xanchor == "l": + cx = x + elif self.options.xanchor == "r": + cx = x + w + else: # middle + cx = x + w / 2 + + if self.options.yanchor == "t": + cy = y + elif self.options.yanchor == "b": + cy = y + h + else: # middle + cy = y + h / 2 + + #direction chosen + if self.options.direction == "tb": + objlist.append([cy,id]) + elif self.options.direction == "bt": + objlist.append([-cy,id]) + elif self.options.direction == "lr": + objlist.append([cx,id]) + elif self.options.direction == "rl": + objlist.append([-cx,id]) + + objlist.sort() + #move them to the top of the object stack in this order. + for item in objlist: + self.recurse(self.selected[item[1]]) + + def recurse(self, node): + istext = (node.tag == '{http://www.w3.org/2000/svg}flowPara' or node.tag == '{http://www.w3.org/2000/svg}flowDiv' or node.tag == '{http://www.w3.org/2000/svg}text') + if node.text != None: + inkex.errormsg(node.text) + for child in node: + self.recurse(child) + +if __name__ == '__main__': + e = Extract() + e.affect() + +# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99 |
