1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
"""
This page is in the table of contents.
Scalable vector graphics is an export canvas plugin to export the canvas to a scalable vector graphics (.svg) file.
When the export menu item in the file menu in an analyze viewer tool, like skeinview or behold is clicked, the postscript dialog will be displayed. When the 'Export to Scalable Vector Graphics' button on that dialog is clicked, the canvas will be exported as a scalable vector graphics file. If the 'Scalable Vector Graphics Program' is set the default 'webbrowser', the scalable vector graphics file will be sent to the default browser to be opened. If the 'Scalable Vector Graphics Program' is set to a program name, the scalable vector graphics file will be sent to that program to be opened.
If furthermore the 'File Extension' is set to a file extension, the scalable vector graphics file will be sent to the program, along with the file extension for the converted output. The default is blank because some systems do not have an image conversion program; if you have or will install an image conversion program, a common 'File Extension' is png. A good open source conversion program is Image Magick, which is available at:
http://www.imagemagick.org/script/index.php
An export canvas plugin is a script in the export_canvas_plugins folder which has the function getNewRepository, and which has a repository class with the functions setCanvasFileNameSuffix to set variables and execute to save the file. It is meant to be run from an analyze viewer tool, like skeinview or behold. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
"""
from __future__ import absolute_import
import __init__
from skeinforge_tools.skeinforge_utilities import gcodec
from skeinforge_tools.skeinforge_utilities import settings
import cStringIO
import os
import sys
__author__ = "Enrique Perez (perez_enrique@yahoo.com)"
__date__ = "$Date: 2008/21/04 $"
__license__ = "GPL 3.0"
def getNewRepository():
"Get the repository constructor."
return ScalableVectorGraphicsRepository()
def parseLineReplace( firstWordTable, line, output ):
"Parse the line and replace it if the first word of the line is in the first word table."
firstWord = gcodec.getFirstWordFromLine( line )
if firstWord in firstWordTable:
line = firstWordTable[ firstWord ]
gcodec.addLineAndNewlineIfNecessary( line, output )
class ScalableVectorGraphicsRepository:
"A class to handle the export settings."
def __init__( self ):
"Set the default settings, execute title & settings fileName."
settings.addListsToRepository( 'skeinforge_tools.analyze_plugins.export_canvas_plugins.svg.html', '', self )
self.fileExtension = settings.StringSetting().getFromValue( 'File Extension:', self, '' )
self.svgProgram = settings.StringSetting().getFromValue( 'Scalable Vector Graphics Program:', self, 'netscape' )
def addCanvasLineToOutput( self, canvasLinesOutput, objectIDNumber ):
"Add the canvas line to the output."
coordinates = self.canvas.coords( objectIDNumber )
xBegin = coordinates[ 0 ] - self.boxW
xEnd = coordinates[ 2 ] - self.boxW
yBegin = coordinates[ 1 ] - self.boxN
yEnd = coordinates[ 3 ] - self.boxN
west = self.boxW
color = self.canvas.itemcget( objectIDNumber, 'fill' )
width = self.canvas.itemcget( objectIDNumber, 'width' )
line = '<line x1="%s" y1="%s" x2="%s" y2="%s" stroke="%s" stroke-width="%spx"/>\n' % ( xBegin, yBegin, xEnd, yEnd, color, width )
canvasLinesOutput.write( line + '\n' )
def execute( self ):
"Export the canvas as an svg file."
svgFileName = gcodec.getFilePathWithUnderscoredBasename( self.fileName, self.suffix )
boundingBox = self.canvas.bbox( settings.Tkinter.ALL ) # tuple (w, n, e, s)
self.boxW = boundingBox[ 0 ]
self.boxN = boundingBox[ 1 ]
boxWidth = boundingBox[ 2 ] - self.boxW
boxHeight = boundingBox[ 3 ] - self.boxN
print( 'Exported svg file saved as ' + svgFileName )
svgTemplateText = gcodec.getFileTextInFileDirectory( settings.__file__, 'svg_canvas.template' )
output = cStringIO.StringIO()
lines = gcodec.getTextLines( svgTemplateText )
firstWordTable = {}
firstWordTable[ 'height="999px"' ] = ' height="%spx"' % int( round( boxHeight ) )
firstWordTable[ '<!--replaceLineWith_coloredLines-->' ] = self.getCanvasLinesOutput()
firstWordTable[ 'replaceLineWithTitle' ] = gcodec.getSummarizedFileName( self.fileName )
firstWordTable[ 'width="999px"' ] = ' width="%spx"' % int( round( boxWidth ) )
for line in lines:
parseLineReplace( firstWordTable, line, output )
gcodec.writeFileText( svgFileName, output.getvalue() )
fileExtension = self.fileExtension.value
svgProgram = self.svgProgram.value
if svgProgram == '':
return
if svgProgram == 'webbrowser':
settings.openWebPage( svgFileName )
return
svgFilePath = '"' + os.path.normpath( svgFileName ) + '"' # " to send in file name with spaces
shellCommand = svgProgram + ' ' + svgFilePath
print( '' )
if fileExtension == '':
print( 'Sending the shell command:' )
print( shellCommand )
commandResult = os.system( shellCommand )
if commandResult != 0:
print( 'It may be that the system could not find the %s program.' % svgProgram )
print( 'If so, try installing the %s program or look for another one, like the Gnu Image Manipulation Program (Gimp) which can be found at:' % svgProgram )
print( 'http://www.gimp.org/' )
return
convertedFileName = gcodec.getFilePathWithUnderscoredBasename( svgFilePath, '.' + fileExtension + '"' )
shellCommand += ' ' + convertedFileName
print( 'Sending the shell command:' )
print( shellCommand )
commandResult = os.system( shellCommand )
if commandResult != 0:
print( 'The %s program could not convert the svg to the %s file format.' % ( svgProgram, fileExtension ) )
print( 'Try installing the %s program or look for another one, like Image Magick which can be found at:' % svgProgram )
print( 'http://www.imagemagick.org/script/index.php' )
def getCanvasLinesOutput( self ):
"Add the canvas line to the output."
canvasLinesOutput = cStringIO.StringIO()
objectIDNumbers = self.canvas.find_all()
for objectIDNumber in objectIDNumbers:
if self.canvas.type( objectIDNumber ) == 'line':
self.addCanvasLineToOutput( canvasLinesOutput, objectIDNumber )
return canvasLinesOutput.getvalue()
def setCanvasFileNameSuffix( self, canvas, fileName, suffix ):
"Set the canvas and initialize the execute title."
self.canvas = canvas
self.executeTitle = 'Convert to Scalable Vector Graphics'
self.fileName = fileName
self.suffix = suffix + '.svg'
def main():
"Display the file or directory dialog."
settings.startMainLoopFromConstructor( getNewRepository() )
if __name__ == "__main__":
main()
|