/skeinforge_application/skeinforge_plugins/analyze_plugins/export_canvas_plugins/postscript.py

https://github.com/greenarrow/skeinforge · Python · 97 lines · 81 code · 12 blank · 4 comment · 6 complexity · 1fd22cabca8ff88c6e770db995118dfc MD5 · raw file

  1. """
  2. This page is in the table of contents.
  3. Postscript is an export canvas plugin to export the canvas to a postscript file.
  4. When the export menu item in the file menu in an analyze viewer tool, like skeinlayer or skeiniso is clicked, the postscript dialog will be displayed. When the 'Export to Postscript' button on that dialog is clicked, the canvas will be exported as a postscript file. If the 'Postscript Program' is set to a program name, the postscript file will be sent to that program to be opened. The default is gimp, the Gnu Image Manipulation Program (Gimp), which is open source, can open postscript and save in a variety of formats. It is available at:
  5. http://www.gimp.org/
  6. If furthermore the 'File Extension' is set to a file extension, the postscript 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:
  7. http://www.imagemagick.org/script/index.php
  8. 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 skeinlayer or skeiniso. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
  9. """
  10. from __future__ import absolute_import
  11. #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
  12. import __init__
  13. from fabmetheus_utilities import archive
  14. from fabmetheus_utilities import gcodec
  15. from fabmetheus_utilities import settings
  16. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  17. import cStringIO
  18. import os
  19. import sys
  20. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  21. __date__ = '$Date: 2008/21/04 $'
  22. __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
  23. def getNewRepository():
  24. 'Get new repository.'
  25. return PostscriptRepository()
  26. class PostscriptRepository:
  27. "A class to handle the export settings."
  28. def __init__(self):
  29. "Set the default settings, execute title & settings fileName."
  30. skeinforge_profile.addListsToCraftTypeRepository(
  31. 'skeinforge_application.skeinforge_plugins.analyze_plugins.export_canvas_plugins.postscript.html', self)
  32. self.fileExtension = settings.StringSetting().getFromValue('File Extension:', self, '')
  33. self.postscriptProgram = settings.StringSetting().getFromValue('Postscript Program:', self, 'gimp')
  34. def execute(self):
  35. "Convert to postscript button has been clicked. Export the canvas as a postscript file."
  36. postscriptFileName = archive.getFilePathWithUnderscoredBasename( self.fileName, self.suffix )
  37. boundingBox = self.canvas.bbox( settings.Tkinter.ALL ) # tuple (w, n, e, s)
  38. boxW = boundingBox[0]
  39. boxN = boundingBox[1]
  40. boxWidth = boundingBox[2] - boxW
  41. boxHeight = boundingBox[3] - boxN
  42. print('Exported postscript file saved as ' + postscriptFileName )
  43. self.canvas.postscript( file = postscriptFileName, height = boxHeight, width = boxWidth, pageheight = boxHeight, pagewidth = boxWidth, x = boxW, y = boxN )
  44. fileExtension = self.fileExtension.value
  45. postscriptProgram = self.postscriptProgram.value
  46. if postscriptProgram == '':
  47. return
  48. postscriptFilePath = '"' + os.path.normpath( postscriptFileName ) + '"' # " to send in file name with spaces
  49. shellCommand = postscriptProgram
  50. print('')
  51. if fileExtension == '':
  52. shellCommand += ' ' + postscriptFilePath
  53. print('Sending the shell command:')
  54. print(shellCommand)
  55. commandResult = os.system(shellCommand)
  56. if commandResult != 0:
  57. print('It may be that the system could not find the %s program.' % postscriptProgram )
  58. 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:' % postscriptProgram )
  59. print('http://www.gimp.org/')
  60. return
  61. shellCommand += ' ' + archive.getFilePathWithUnderscoredBasename( postscriptFilePath, '.' + fileExtension + '"')
  62. print('Sending the shell command:')
  63. print(shellCommand)
  64. commandResult = os.system(shellCommand)
  65. if commandResult != 0:
  66. print('The %s program could not convert the postscript to the %s file format.' % ( postscriptProgram, fileExtension ) )
  67. print('Try installing the %s program or look for another one, like Image Magick which can be found at:' % postscriptProgram )
  68. print('http://www.imagemagick.org/script/index.php')
  69. def setCanvasFileNameSuffix( self, canvas, fileName, suffix ):
  70. "Set the canvas and initialize the execute title."
  71. self.canvas = canvas
  72. self.executeTitle = 'Export to Postscript'
  73. self.fileName = fileName
  74. self.suffix = suffix + '.ps'
  75. def main():
  76. "Display the file or directory dialog."
  77. settings.startMainLoopFromConstructor(getNewRepository())
  78. if __name__ == "__main__":
  79. main()