home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / SimpleCaption.PspScript < prev    next >
Encoding:
Text File  |  2003-04-22  |  14.0 KB  |  336 lines

  1. from JascApp import *
  2. import JascUtils
  3.  
  4. def ScriptProperties():
  5.     return {
  6.         'Author': 'Joe Fromm',
  7.         'Copyright': 'Copyright (C) 2002-2003, Jasc Software Inc., All Rights Reserved. Permission to create derivate works of this script is granted provided this copyright notice is included',
  8.         'Description': 'Place a caption on an image by expanding the canvas and placing the caption below the image.',
  9.         'Host': 'Paint Shop Pro',
  10.         'Host Version': '8.00'
  11.         }
  12.  
  13. FontName = 'Times New Roman'        # font name that will be used by the text
  14. ExpandEdges = 0.1   # expand top, left and right by 10%
  15. ExpandBottom = 0.15 # expand bottom by 15%
  16.  
  17. def Do(Environment):
  18.     ''' Add a caption by doing the following steps:
  19.         (Thanks to Angela Cable for advice on artistic aspect of this)
  20.          1. Prompt the user for the caption.  If they press cancel abort
  21.          2. Select the bottommost layer
  22.          3. If we are starting on a background layer, promote it to a full layer. To keep
  23.             things straight,rename the layer to "Image"
  24.          4. Add a new empty layer.  Call it Page Surface.
  25.          5. Arrange the Page Surface to the bottom
  26.          6. Increase the canvas size by 10% on the left, right and top,
  27.             and by 15% on the bottom.
  28.          7. Move the Page Surface layer into a layer group.  Call the group Album Page
  29.          8. Select the Page Surface layer
  30.          9. Flood fill it with white.
  31.         10. Using the backdrop texture and a 192 grey, flood fill it again.
  32.         11. Select the image layer again.
  33.         12. Add drop shadow on a new layer.
  34.         13. Arrange the layer into the Album page group
  35.         14. Create a vector layer
  36.         15. Place the caption text the user entered at the beginning on the image.
  37.             Font used is set in the FontName variable
  38.         16. Select the image layer again
  39.  
  40.        When done, the original layer is unchanged, and all of the new layers are placed
  41.        inside of a layer group.  The canvas size is changed however.
  42.     '''
  43.  
  44.     if JascUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
  45.         return
  46.     
  47.     # save off the target doc before we do anything
  48.     Target = App.TargetDocument
  49.     
  50.     # we work with percentages for a number of things, and the expectation is that the
  51.     # text should be visible - lets not support any image with a dimension less than 200
  52.     if Target.Height < 200 or Target.Width < 200:
  53.         App.Do( Environment, 'MsgBox', {
  54.                 'Buttons': App.Constants.MsgButtons.OK, 
  55.                 'Icon': App.Constants.MsgIcons.Stop, 
  56.                 'Text': 'The current image is too small to caption - it must be at least 200x200.', 
  57.                 })
  58.         return
  59.  
  60.     # if we are running on something that is not a flat image, suggest
  61.     # that they flatten before continuing
  62.     if JascUtils.GetLayerCount( Environment, Target ) != 1:
  63.         result = App.Do( Environment, 'MsgBox', {
  64.                 'Buttons': App.Constants.MsgButtons.YesNo, 
  65.                 'Icon': App.Constants.MsgIcons.Question, 
  66.                 'Text': 'The current image has multiple layers.  This may lead to odd results.\n'
  67.                         'We suggest flattening the image before continuing.  Do you wish to flatten?' 
  68.                 })
  69.         if result == 1: # yes
  70.             App.Do( Environment, 'LayerMergeAll' )
  71.     
  72.             
  73.     DefaultCaption = Target.Title
  74.     ExtensionPos = DefaultCaption.rfind( '.' )
  75.     if ExtensionPos != -1:
  76.         DefaultCaption = DefaultCaption[ : ExtensionPos ]
  77.     
  78.     # prompt for the caption at the very beginning - if they press cancel in the dialog we
  79.     # can return before any changes are made to the image
  80.     # now prompt the user for the caption to user
  81.     Result = App.Do( Environment, 'GetString', {
  82.             'DefaultText': DefaultCaption,
  83.             'DialogTitle': 'Enter Image Caption',
  84.             'Prompt': 'Enter a caption for the image.  This will be below the image and centered.',
  85.             'MaxLength': 40,
  86.             'GeneralSettings': {
  87.                 'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
  88.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  89.                 }
  90.         })
  91.     if Result[ 'OKButton' ] == App.Constants.Boolean.false:
  92.         return
  93.     
  94.     CaptionText = Result[ 'EnteredText' ]   # save this for later
  95.   
  96.     # go to the bottommost layer - this could throw an exception if we are already on the bottommost layer
  97.     App.Do( Environment, 'SelectLayer', {
  98.             'Path': ( 9999, -9999, [], App.Constants.Boolean.false),
  99.             'GeneralSettings': {
  100.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  101.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  102.                 }
  103.             })
  104.     
  105.     # promote a background layer so that we can put a drop shadow on it
  106.     if JascUtils.LayerIsBackground( Environment, Target ):
  107.         App.Do( Environment, 'LayerPromoteBackground', {
  108.                 'GeneralSettings': {
  109.                     'ExecutionMode': App.Constants.ExecutionMode.Default, 
  110.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  111.                     }
  112.                 })
  113.  
  114.         # promoting the background will give it a default name - give it a
  115.         # modestly more descriptive name so that we can tell what is what
  116.         App.Do( Environment, 'LayerProperties', {
  117.                 'General': {
  118.                     'Name': 'Image', 
  119.                     }, 
  120.                 'GeneralSettings': {
  121.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  122.                     'AutoActionMode': App.Constants.AutoActionMode.Default
  123.                     }
  124.                 })
  125.  
  126.     # now add a new layer
  127.     App.Do( Environment, 'NewRasterLayer', {
  128.             'General': {
  129.                 'Opacity': 100, 
  130.                 'Name': 'Page Surface', 
  131.                 'IsVisible': App.Constants.Boolean.true, 
  132.                 }, 
  133.             'GeneralSettings': {
  134.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  135.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  136.                 }
  137.             })
  138.  
  139.     # arrange the new layer to the bottom
  140.     App.Do( Environment, 'LayerArrange', {
  141.             'Path': (0,-1,[],App.Constants.Boolean.false), 
  142.             'MoveAboveSibling': App.Constants.Boolean.false, 
  143.             'GeneralSettings': {
  144.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  145.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  146.                 }
  147.             })
  148.  
  149.     # by the time we are done we are going to have added 3 layers - backdrop, text, and shadow.
  150.     # to be tidy, lets wrap all of it in a layer group
  151.     # create a layer group.  This will place the 'page surface' layer in the group
  152.     App.Do( Environment, 'NewLayerGroup', {
  153.             'General': {
  154.                 'Name': 'Album Page', 
  155.                 }, 
  156.             'GeneralSettings': {
  157.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  158.                 }
  159.             })
  160.     
  161.     # select the 'page surface' layer so that the vector layer gets created above it
  162.     App.Do( Environment, 'SelectLayer', {
  163.             'Path': (0,0,[1],App.Constants.Boolean.false), 
  164.             })
  165.  
  166.     # now increase the canvas size, based on the original size
  167.     XAmount = int(Target.Width * ExpandEdges)
  168.     YAmountTop = int(Target.Height * ExpandEdges)
  169.     YAmountBottom = int(Target.Height * ExpandBottom)
  170.     
  171.     NewWidth = Target.Width + 2 * XAmount
  172.     NewHeight = Target.Height + YAmountTop + YAmountBottom
  173.  
  174.     App.Do( Environment, 'ResizeCanvas', {
  175.             'HoriPlace': App.Constants.HorizontalType.Custom, 
  176.             'MaintainAspect': App.Constants.Boolean.false, 
  177.             'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
  178.             'NewHeight': NewHeight, 
  179.             'NewWidth': NewWidth, 
  180.             'OrigDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
  181.             'PlaceBottom': YAmountBottom, 
  182.             'PlaceLeft': XAmount, 
  183.             'PlaceRight': XAmount, 
  184.             'PlaceTop': YAmountTop, 
  185.             'VertPlace': App.Constants.VerticalType.Center, 
  186.             'GeneralSettings': {
  187.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  188.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  189.                 }
  190.             })
  191.     # we are still on the Backdrop layer - fill it with white
  192.     App.Do( Environment, 'Fill', {
  193.             'BlendMode': 0, 
  194.             'MatchMode': 1, 
  195.             'Material': {
  196.                 'Color': (255,255,255), 
  197.                 'Pattern': None, 
  198.                 'Gradient': None, 
  199.                 'Texture': None
  200.                 }, 
  201.             'UseForground': App.Constants.Boolean.true, 
  202.             'Opacity': 100, 
  203.             'Point': (1, 1), 
  204.             'SampleMerged': 0, 
  205.             'Tolerance': 20, 
  206.             'GeneralSettings': {
  207.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  208.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  209.                 }
  210.             })
  211.     
  212.     # now fill it again with a light grey texture
  213.     App.Do( Environment, 'Fill', {
  214.             'BlendMode': 0, 
  215.             'MatchMode': 1, 
  216.             'Material': {
  217.                 'Color': (160,160,160), 
  218.                 'Pattern': None, 
  219.                 'Gradient': None, 
  220.                 'Texture': {
  221.                     'Name': 'Backdrop', 
  222.                     'Image': None, 
  223.                     'Angle': 0.000, 
  224.                     'Scale': 100.000
  225.                     }
  226.                 }, 
  227.             'UseForground': App.Constants.Boolean.true, 
  228.             'Opacity': 100, 
  229.             'Point': (1, 1), 
  230.             'SampleMerged': 0, 
  231.             'Tolerance': 20, 
  232.             'GeneralSettings': {
  233.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  234.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  235.                 }
  236.             })
  237.  
  238.     # now select the image layer
  239.     App.Do( Environment, 'SelectLayer', {
  240.             'Path': (1,1,[],App.Constants.Boolean.false), 
  241.             'GeneralSettings': {
  242.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  243.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  244.                 }
  245.             })
  246.  
  247.     # place a drop shadow.  Make it a little bit more on the bottom than on the right
  248.     # we put the drop shadow on a new layer so that the image layer remains untouched
  249.     # drop shadow amount is 10 on the right and 12 on the bottom
  250.     App.Do( Environment, 'DropShadow', {
  251.             'Blur': 5.000, 
  252.             'Color': (0,0,0), 
  253.             'Horizontal': 10, 
  254.             'Vertical': 12, 
  255.             'NewLayer': App.Constants.Boolean.true, 
  256.             'Opacity': 60, 
  257.             'GeneralSettings': {
  258.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  259.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  260.                 }
  261.             })
  262.  
  263.     # The default name for a drop shadow is kind of lame.  Lets rename it
  264.     App.Do( Environment, 'LayerProperties', {
  265.             'General': {
  266.                 'Name': 'Drop shadow',
  267.                 'Opacity': 75,
  268.                 }, 
  269.             'GeneralSettings': {
  270.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  271.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  272.                 }
  273.             })
  274.  
  275.  
  276.     # placing the drop shadow added a new layer, and made it active.
  277.     # let's arrange it into the group
  278.     App.Do( Environment, 'LayerArrangeMoveIn' )
  279.     
  280.     # select one layer down to get back to the backdrop
  281.     App.Do( Environment, 'SelectLayer', { 'Path': ( 0, -1, [], App.Constants.Boolean.false ) } )
  282.  
  283.     # now create a vector layer to put text on - creating the layer will make it active
  284.     # we could let the text tool create the layer for us, but this way we get to name it
  285.     App.Do( Environment, 'NewVectorLayer', {
  286.             'General': {
  287.                 'Opacity': 100, 
  288.                 'Name': 'Caption Text', 
  289.                 }, 
  290.             'GeneralSettings': {
  291.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  292.                 }
  293.             })
  294.  
  295.     # finally place the text.  Make it centered in the X axis, and centered in the bottom
  296.     # margin.  We make the text height 1/4 the margin amount, but never less than 12 pixels
  297.     TextSize = max( 12, int(YAmountBottom / 4))
  298.     
  299.     TextBaseLine = Target.Height - (YAmountBottom / 4)
  300.     App.Do( Environment, 'Text', {
  301.             'CreateAs': App.Constants.CreateAs.Vector, 
  302.             'Segments': [{
  303.                 'Fill': {
  304.                     'Color': (0,0,0), 
  305.                     'Pattern': None, 
  306.                     'Gradient': None, 
  307.                     'Texture': None
  308.                     }, 
  309.                 'Font': FontName, 
  310.                 'Italic': App.Constants.Boolean.true, 
  311.                 'Stroke': None,
  312.                 'PointSize': TextSize, 
  313.                 'Start': (Target.Width / 2, TextBaseLine), 
  314.                 'LineStyle': None,
  315.                 'LineWidth':0,
  316.                 'SetText': App.Constants.Justify.Center
  317.                 }, {
  318.                 'Characters': unicode(CaptionText, 'latin-1')
  319.                 }], 
  320.             'FinalApply': App.Constants.Boolean.false, 
  321.             'GeneralSettings': {
  322.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  323.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  324.                 }
  325.             })
  326.  
  327.     # finish with the image layer selected
  328.     App.Do( Environment, 'SelectLayer', {
  329.             'Path': (1,1,[],App.Constants.Boolean.false), 
  330.             'GeneralSettings': {
  331.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  332.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  333.                 }
  334.             })
  335.  
  336.