Caption: Difference between revisions

From Scribus
Jump to navigation Jump to search
(Created page with "===caption.py=== Here is a script which allows you to choose the caption position in a dialog. If you enter something other than b/t/r/l it shouldn't do anything. This script...")
 
Line 59: Line 59:


scribus.setRedraw(True)
scribus.setRedraw(True)
</pre>
===Additional modifications===
In case you would like to set the width and height of the right and left captions, you might add the possibility of entering values. I'm going to use a single '''valueDialog()''' for this, then use the python '''.split()''' operator.
Here is the original script section:
<pre>
<pre>
    elif location == "R":
        textf = scribus.createText(fx + fwidth, fy, 150, 40)# frame width set at 150, height at 40 points
</pre>
Let's change it to this by adding four lines, and use the variables instead of numbers in the '''createText()''' command:
<pre>
    elif location == "R":
        cdimensions = scribus.valueDialog("Width/Height of Caption","Enter the width and height of the caption,\n in points (only space between values)", "150 40")
        cwidth, cheight = cdimensions.split()
        cwidth = float(cwidth)
        cheight = float(cheight)
        textf = scribus.createText(fx + fwidth, fy, cwidth, cheight)
</pre>
Remember that everything entered into a '''valueDialog()''' is a string, so after splitting to 2 separate values, we then need to convert to '''float()''' since this is what '''createText()''' requires. Add the extra lines for the left caption also to complete the process, but note that it's a bit trickier, since the upper left corner of the caption frame depends on its width and height.
<pre>
    elif location == "L":
        cdimensions = scribus.valueDialog("Width/Height of Caption","Enter the width and height of the caption,\n in points (only space between values)", "150 40")
        cwidth, cheight = cdimensions.split()
        cwidth = float(cwidth)
        cheight = float(cheight)
        textf = scribus.createText(fx-cwidth, fy + fheight - cheight, cwidth, cheight)
</pre>
===Adding a caption style===
Here is another modification to the '''bcaption.py''' script &ndash; let's call it '''bcaption_style.py'''. We add 2 things here &ndash; one is to create a caption Paragraph Style. I chose a Liberation Serif Italic font at 10 pts. First we need to check to make sure there already isn't a style named '''Caption''', and if not, it's created. It is then used to show the name of the frames that are created, simply for an example of its appearance. The other thing is to create a small Top Distance for the frame, so that the text isn't right up against the bottom of the frame.
<pre>
#!/usr/bin/env python
# -*- coding: utf-8  -*-
# ****************************************************************************
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ****************************************************************************
"""
© 2017 Gregory Pittman
bcaption_style.py
Creates a text frame (caption) below one or more selected frames.
This version also creates a Paragraph Style named Caption (if it does not already exist), and
then uses it in the frames. A small Top Distance (4 pts) is also created for these caption frames.
"""
try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)
numselect = scribus.selectionCount()
count = 0
frames = []
createcaptionstyle = "yes"
if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                      scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
captionstyle = "Caption"
stylelist = scribus.getAllStyles()
for style in stylelist:
    if style =="Caption":
        createcaptionstyle = "no"
if createcaptionstyle == "yes":
    scribus.createCharStyle("captionchar","Liberation Serif Italic", 10.0, 'Black')
    scribus.createParagraphStyle("Caption",1,10.0, 0,0,0,0,0,0,0,0,0,"captionchar")
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)
while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
   
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy+fheight, fwidth, 24)
    scribus.setTextDistances(0,0,4,0,textf)
    scribus.setText(textf,textf)
    scribus.setStyle("Caption",textf)
   
scribus.setUnit(pageunits)
scribus.setRedraw(True)
</pre>

Revision as of 00:08, 13 December 2019

caption.py

Here is a script which allows you to choose the caption position in a dialog. If you enter something other than b/t/r/l it shouldn't do anything. This script is now included with versions 1.4.7svn and 1.5.3svn as Caption.py.

#!/usr/bin/env python
# -*- coding: utf-8  -*-

"""

© 2017 Gregory Pittman

caption.py

Creates a text frame (caption) in selected location relative to 
one or more selected frames.

"""

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

numselect = scribus.selectionCount()
count = 0
frames = []

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

captionloc = scribus.valueDialog("Caption Location","Where to put the caption(s) -\n B/T/R/L?", "b")
captionloc = captionloc[0]
location = captionloc.upper()

pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    if location == "B":
        textf = scribus.createText(fx, fy+fheight, fwidth, 24)# frame height set at 24 points
    elif location == "T":
        textf = scribus.createText(fx, fy-24, fwidth, 24) # frame height set at 24 points
    elif location == "R":
        textf = scribus.createText(fx + fwidth, fy, 150, 40)# frame width set at 150, height at 40 points
    elif location == "L":
        textf = scribus.createText(fx-150, fy + fheight - 40, 150, 40) # frame width set at 150, height at 40 points

scribus.setUnit(pageunits)

scribus.setRedraw(True)

Additional modifications

In case you would like to set the width and height of the right and left captions, you might add the possibility of entering values. I'm going to use a single valueDialog() for this, then use the python .split() operator. Here is the original script section:

    elif location == "R":
        textf = scribus.createText(fx + fwidth, fy, 150, 40)# frame width set at 150, height at 40 points

Let's change it to this by adding four lines, and use the variables instead of numbers in the createText() command:

    elif location == "R":
        cdimensions = scribus.valueDialog("Width/Height of Caption","Enter the width and height of the caption,\n in points (only space between values)", "150 40")
        cwidth, cheight = cdimensions.split()
        cwidth = float(cwidth)
        cheight = float(cheight)
        textf = scribus.createText(fx + fwidth, fy, cwidth, cheight)

Remember that everything entered into a valueDialog() is a string, so after splitting to 2 separate values, we then need to convert to float() since this is what createText() requires. Add the extra lines for the left caption also to complete the process, but note that it's a bit trickier, since the upper left corner of the caption frame depends on its width and height.

    elif location == "L":
        cdimensions = scribus.valueDialog("Width/Height of Caption","Enter the width and height of the caption,\n in points (only space between values)", "150 40")
        cwidth, cheight = cdimensions.split()
        cwidth = float(cwidth)
        cheight = float(cheight)
        textf = scribus.createText(fx-cwidth, fy + fheight - cheight, cwidth, cheight)

Adding a caption style

Here is another modification to the bcaption.py script – let's call it bcaption_style.py. We add 2 things here – one is to create a caption Paragraph Style. I chose a Liberation Serif Italic font at 10 pts. First we need to check to make sure there already isn't a style named Caption, and if not, it's created. It is then used to show the name of the frames that are created, simply for an example of its appearance. The other thing is to create a small Top Distance for the frame, so that the text isn't right up against the bottom of the frame.

#!/usr/bin/env python
# -*- coding: utf-8  -*-

# ****************************************************************************
#  This program is free software; you can redistribute it and/or modify 
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ****************************************************************************


"""

© 2017 Gregory Pittman

bcaption_style.py

Creates a text frame (caption) below one or more selected frames.

This version also creates a Paragraph Style named Caption (if it does not already exist), and 

then uses it in the frames. A small Top Distance (4 pts) is also created for these caption frames.

"""

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

numselect = scribus.selectionCount()
count = 0
frames = []
createcaptionstyle = "yes"

if numselect == 0:
    scribus.messageBox('Selection Count', "You must have at least one image frame selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
captionstyle = "Caption" 
stylelist = scribus.getAllStyles()
for style in stylelist:
    if style =="Caption":
        createcaptionstyle = "no"
if createcaptionstyle == "yes":
    scribus.createCharStyle("captionchar","Liberation Serif Italic", 10.0, 'Black')
    scribus.createParagraphStyle("Caption",1,10.0, 0,0,0,0,0,0,0,0,0,"captionchar")
pageunits = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)

while count < numselect:
    frames.append(scribus.getSelectedObject(count))
    count += 1
    
for frame in frames:
    fwidth, fheight = scribus.getSize(frame)
    fx, fy = scribus.getPosition(frame)
    textf = scribus.createText(fx, fy+fheight, fwidth, 24)
    scribus.setTextDistances(0,0,4,0,textf)
    scribus.setText(textf,textf)
    scribus.setStyle("Caption",textf)
    
scribus.setUnit(pageunits)

scribus.setRedraw(True)