Esempio n. 1
0
    def get_dimensions(self, nodes_ids_list):
        """
        Get dimensions of nodes with given ids.
        """
        if len(nodes_ids_list) > 0:
            svg_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:
                inkscape_pipe = Popen('inkscape --query-all "%s"' % (svg_file),
                                      shell=True,
                                      stdout=PIPE,
                                      stderr=PIPE)
                err = inkscape_pipe.stderr
                inkscape_output = inkscape_pipe.communicate()[0]
                try:
                    reader = csv.CSVParser().parse_string(inkscape_output)
                    #there was a module cvs.py in earlier inkscape that behaved differently
                except:
                    reader = csv.reader(inkscape_output.split(os.linesep))
                err.close()
            else:
                _, inkscape_output, err = os.popen3(
                    'inkscape --query-all "%s"' % (svg_file))
                reader = csv.reader(inkscape_output)
                err.close()

            #build a dictionary with id as the key
            dimensions = dict()
            for line in reader:
                if (len(line) > 0) and (line[0] in nodes_ids_list):
                    dimensions[line[0]] = map(float, line[1:])

            if not BSUBPROCESS:  #close file if opened using os.popen3
                inkscape_output.close

        return dimensions
Esempio n. 2
0
    def restack_positional(self):
        objects = {}
        objlist = []
        file = self.args[-1]

        if self.options.nb_direction == '"custom"':
            self.options.direction = "aa"

        # process selection to get list of objects to be arranged
        firstobject = self.selected[self.options.ids[0]]
        if len(self.selected) == 1 and firstobject.tag == inkex.addNS(
                'g', 'svg'):
            parentnode = firstobject
            for child in parentnode.iterchildren():
                objects[child.get('id')] = child
        else:
            parentnode = self.current_layer
            objects = self.selected

        #get all bounding boxes in file by calling inkscape again with the --query-all command line option
        #it returns a comma separated 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[objects.keys()[0]]
        minx = x
        miny = y
        maxx = x + w
        maxy = y + h

        for id, node in objects.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 objects.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" or (
                    self.options.direction == "aa"
                    and self.options.angle == 270):
                objlist.append([cy, id])
            elif self.options.direction == "bt" or (
                    self.options.direction == "aa"
                    and self.options.angle == 90):
                objlist.append([-cy, id])
            elif self.options.direction == "lr" or (
                    self.options.direction == "aa" and
                (self.options.angle == 0 or self.options.angle == 360)):
                objlist.append([cx, id])
            elif self.options.direction == "rl" or (
                    self.options.direction == "aa"
                    and self.options.angle == 180):
                objlist.append([-cx, id])
            elif self.options.direction == "aa":
                distance = math.hypot(cx, cy) * (math.cos(
                    math.radians(-self.options.angle) - math.atan2(cy, cx)))
                objlist.append([distance, id])
            elif self.options.direction == "ro":
                distance = math.hypot(midx - cx, midy - cy)
                objlist.append([distance, id])
            elif self.options.direction == "ri":
                distance = -math.hypot(midx - cx, midy - cy)
                objlist.append([distance, id])

        objlist.sort()
        #move them to the top of the object stack in this order.
        for item in objlist:
            parentnode.append(objects[item[1]])
Esempio n. 3
0
    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 separated 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.
            
            if self.options.flowtext:
                self.text_element = "flowRoot"
                self.text_span = "flowPara"
            else:
                self.text_element = "text"
                self.text_span = "tspan"
                
            self.textRoot=inkex.etree.SubElement(parentnode,inkex.addNS(self.text_element,'svg'),{inkex.addNS('space','xml'):'preserve'})
            self.textRoot.set(inkex.addNS('style', ''), 'font-size:20px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;')

            for item in objlist:
                self.recurse(self.selected[item[1]], self.textRoot)
                
            if self.options.flowtext:
                self.region=inkex.etree.SubElement(self.textRoot,inkex.addNS('flowRegion','svg'),{inkex.addNS('space','xml'):'preserve'})
                self.rect=inkex.etree.SubElement(self.region,inkex.addNS('rect','svg'),{inkex.addNS('space','xml'):'preserve'})
                self.rect.set(inkex.addNS('height', ''), '200')
                self.rect.set(inkex.addNS('width', ''), '200')
Esempio n. 4
0
    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]])