示例#1
0
 def copySvgHead(self, svg_code, new_code = None):
     """
         TODO
     """
     xml_tool = SvgXmlTool()
     
     new_code = xml_tool.copyAttrSvgTag(svg_code, \
                                       new_code or self.makeNewSvgImage().getCode())
     new_code = xml_tool.copyTag( svg_code, new_code, 'defs' )
     new_code = xml_tool.copyTag( svg_code, new_code, 'metadata' )
     new_code = xml_tool.copyTag( svg_code, new_code, 'sodipodi:namedview' )
     return new_code
示例#2
0
    def selectGrainsInRegion(self, region, image, new_image):
        """
            Return a new Image with the set of tags which were inside of
            region passed by parameter.
            
            region = {'x', 'y', 'X', 'Y'}, where x,y are the coordinate of the
            top-left vertice and X, Y are the coordinate of the bottom-right 
            vertice of the Rectangle Region define by parameter region.
        """
        tag_id_list = self.defineTagsInRegion(image, region)
        tag_id_list = self.__handleTagIdList(image.getCode(), tag_id_list)
        
        self.copySvgHead(image.getCode(), new_image.getCode())

        xml_tool = SvgXmlTool()
        xml_tool.copyListTagById(image, new_image, tag_id_list)

        new_image.saveSvgImage()        
        
        return new_image
示例#3
0
class SvgXmlToolTest(unittest.TestCase):
    def setUp(self):
        self.xmlTool = SvgXmlTool()
        self.tool = SvgTool()

    def test_copyAttrSvgTag(self):
        svg1 = minidom.parseString('<svg a="0" b="1" c="2"/>')
        svg2 = minidom.parseString('<svg new="0"/>')
        self.xmlTool.copyAttrSvgTag(svg1, svg2)
        self.assertTrue(svg2.documentElement.attributes.get("a"))
        self.assertTrue(svg2.documentElement.attributes.get("b"))
        self.assertTrue(svg2.documentElement.attributes.get("c"))
        self.assertTrue(svg1.documentElement.attributes.get("a"))
        self.assertTrue(svg1.documentElement.attributes.get("b"))
        self.assertTrue(svg1.documentElement.attributes.get("c"))

    def test_copyMetadataTag(self):
        svg1 = minidom.parseString('<svg><metadata a="0" b="1" c="2"/></svg>')
        svg2 = minidom.parseString("<svg/>")
        self.xmlTool.copyTag(svg1, svg2, "metadata")
        self.assertTrue(svg2.documentElement.getElementsByTagName("metadata"))
        self.assertTrue(svg1.documentElement.getElementsByTagName("metadata"))

    def test_copyDefs(self):
        svg1 = minidom.parseString('<svg><defs a="0"/></svg>')
        svg2 = minidom.parseString("<svg/>")
        self.xmlTool.copyTag(svg1, svg2, "defs")
        self.assertTrue(svg2.documentElement.getElementsByTagName("defs"))
        self.assertTrue(svg1.documentElement.getElementsByTagName("defs"))
示例#4
0
 def __handleTagIdList(self, svg_code, tag_id_list):
     """
         This method should remove all the parent tags whose the 
         children are in tag_id_list.
         So when you have to insert a new child and the parent is not into
         the new SVG Code, the parent should be added as well.
         
         tag_id_list before handled
         ['g1','rect2']
         
         tag_id_list after handled
         ['rect2']
         
         
         Per example:
             <svg>
               <g id='g1'> <-- Parent
                 <rect id='rect2' /> <-- Child that should be copied to the new SVG Code.
               </g>
             </svg>
             
             New SVG Code before insertion:
             <svg>
             </svg>
             
             After insertion:
             <svg>
               <g id='g1'>
                 <rect id='rect2' />
               </g>
             </svg>
     """
     xml_tool = SvgXmlTool()        
     all_children = xml_tool.getAllChildren(svg_code)
     for tag in all_children:
         if (tag.tagName in ACCEPT_TAG):
             if tag.attributes.get('id').value in tag_id_list:
                 if tag.parentNode.attributes.get('id').value in tag_id_list:
                     tag_id_list.remove( tag.parentNode.attributes['id'].value )
     return tag_id_list
示例#5
0
 def __init__(self, content):
     """
         This method is the initiator's SvgImage. Its main responsible
         is open the svg content referenced by paramenters.
         If there is the content then open it, else create 
         a new content with basic svg content
     """
     self._content = content
     self.xml_tool = SvgXmlTool()
    
     if (len(self._content.content)==0):
         self._content.setContent(self.xml_tool.getCode(self.xml_tool.parseXml("<svg/>")))
     
     self.updateAttributes()       
示例#6
0
 def setUp(self):
     self.xmlTool = SvgXmlTool()
     self.tool = SvgTool()
示例#7
0
class SvgImage(object):

    """
        A classe SvgImage implementa um tipo de media abordado por nsi.media:
        imagens SVG (Scalable Vector Graphics).
        
        Atributos:
        __file: uma instancia de um SvgFile. Essa instancia armazena estados
        e comportamentos referentes a um arquivo.
        xml_tool: instancia de um SvgXmlTool (ferramenta para manipular 
        xml - especificamente svg)
        __stringCode: codigo da imagem svg referenciada pela instancia
        __xmlCode: instancia do xml parseado e possivel de ser manipulado 
        pela classe SvgXmlTool
    """
    

    def __init__(self, content):
        """
            This method is the initiator's SvgImage. Its main responsible
            is open the svg content referenced by paramenters.
            If there is the content then open it, else create 
            a new content with basic svg content
        """
        self._content = content
        self.xml_tool = SvgXmlTool()
       
        if (len(self._content.content)==0):
            self._content.setContent(self.xml_tool.getCode(self.xml_tool.parseXml("<svg/>")))
        
        self.updateAttributes()       
         
    def getCode(self):
        """
            Return the xml code of instance (content of content)
        """
        return self.__xmlCode
    
        
    def updateAttributes(self):
        """
            Update the atributtes stringCode and xmlCode with content 
            in content, when this is modify in instance
        """
        self._stringCode = self._content.content
        self.__xmlCode = self.xml_tool.parseXml(self._stringCode)
        
        
    
    def saveSvgImage(self):
        """
            This method write the content of attribute __xmlCode in content 
            referenced by instance of SvgContent (attribute _content)
        """
        self._content.setContent(self.__xmlCode.toxml())

    def alterDataFile(self, name=None, directory=None):
        """
            TODO
        """    
        if name:
            self._content.name = name
        if directory:
            self._content.directory = directory
            
    def getFile(self):
        """
            TODO
        """    
        return self._content
        
    def getContentFile(self):
        """
            TODO
        """    
        return self._content.content
        
    def toPng(self, new_name=None, directory=None):
        """
            This method exports the content of SVG Image for PNG format.
        """
        if not directory:
            directory = self._content.directory
            
        if not new_name:
            new_name = self._content.name[:-3]+'png'
            
        path = os.path.join(directory, new_name)
        
        #print self._content.getAbsolutePath() 
        
        os.popen("inkscape %s --export-png %s" \
                  % (self._content.getAbsolutePath(), path))