Subversion Repositories Scribus

Compare Revisions

Ignore whitespace Rev 12779 → Rev 12780

/branches/Version133x/Scribus/scribus/plugins/scriptplugin/scripts/ColorChart.py
0,0 → 1,332
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
"""
ABOUT THIS SCRIPT:
 
ColorChart.py allows a user to create color charts with all the colors of a given scribus document.
It generates a color field for each color and a description of the color, containing the color name, the CMYK values and the RGB values.
 
If there is a document opened in scribus, ColorChart uses this document as color source and creates a new document with the color chart.
If there is no document opened in scribus, ColorChart displays a file open dialog to allow the user to chose a scribus file to generate a colorchart of.
You will be asked to give a name for the color chart. This name will be displayed in the pages headlines.
############################
 
LICENSE:
 
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.
 
Author: Sebastian Stetter
please report bugs to: scribusscript@sebastianstetter.de
"""
from __future__ import division
import sys
__version__=1.1
try:
# Please do not use 'from scribus import *' . If you must use a 'from import',
# Do so _after_ the 'import scribus' and only import the names you need, such
# as commonly used constants.
import scribus
except ImportError,err:
print "This Python script is written for the Scribus scripting interface."
print "It can only be run from within Scribus."
sys.exit(1)
 
####################
# IMPORTS GO HERE #
####################
 
COLOR_FIELD_HEIGHT=25
#space between colorfields
HSPACE=5
VSPACE=4
#space for header and footer
HEADERSIZE = 10
FOOTERSIZE = 5
TEXT_BOX_WIDTH = 50
global pageTitle
pageTitle="COLOR CHART"
 
def drawHeaderFooter(pagetitle):
"""draw some info on the pages"""
# get page size
pageSize=scribus.getPageSize()
pageWidth=pageSize[0]
pageHeight=pageSize[1]
#pageMargins
pageMargins=scribus.getPageMargins()
topMargin=pageMargins[0]
leftMargin=pageMargins[1]
rightMargin=pageMargins[2]
bottomMargin=pageMargins[3]
#create textbox and insert text for header
textbox=scribus.createText(leftMargin, topMargin, pageWidth-leftMargin-rightMargin, HEADERSIZE)
#set proper font size and alignment
scribus.setFontSize(18, textbox)
scribus.setTextAlignment(scribus.ALIGN_CENTERED, textbox)
#load the string into the textbox
headerstring=pagetitle
scribus.insertText(headerstring, 0, textbox)
#create textbox and insert text for footer
textbox=scribus.createText(leftMargin, pageHeight-bottomMargin-FOOTERSIZE, pageWidth-leftMargin-rightMargin, FOOTERSIZE)
#set proper font size and alignment
scribus.setFontSize(9, textbox)
scribus.setTextAlignment(scribus.ALIGN_LEFT, textbox)
#load the string into the textbox
footerstring="Created using ColorChart.py V %s script for Scribus by Sebastian Stetter - http://www.sebastianstetter.de" % str(__version__)
scribus.insertText(footerstring, 0, textbox)
 
def getColorsFromDocument():
"""gets colors from opend document. if there is no document, display dialog to chose a file. returns a list[name,c,m,y,k]"""
def getColors():
"""gets the colors and returns a list[name,c,m,y,k]"""
colorNames=scribus.getColorNames()
list=[]
scribus.statusMessage("Reading Colors...")
stepsTotal=len(colorNames)
scribus.progressTotal(stepsTotal)
steps=0
for name in colorNames:
color=scribus.getColor(name)
listitem=[name, color[0], color[1], color[2], color[3]]
list.append(listitem)
#update progress bar
steps=steps+1
scribus.progressSet(steps)
return list
 
#check if we have a document - otherwise display open file dialog
if scribus.haveDoc() == 1:
pass
list=getColors()
return list
else:
pass
#display file open dialog
file=scribus.fileDialog("ColorChart by Sebastian Stetter", 'Scribus files(*.sla *.SLA *.sla.gz *.SLA.GZ)')
#open file
try:
scribus.openDoc(file)
except:
scribus.messageBox("ColorChart by Sebastian Stetter", "could not open file")
sys.exit()
list=getColors()
return list
 
def prepareDocument():
"""creates the new document, sets up colors """
colorList = getColorsFromDocument()
scribus.statusMessage("Preparing new document...")
scribus.newDocument(scribus.PAPER_A4, (15,15, 20, 20), scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.PAGE_1, 0, 1)
scribus.setUnit(scribus.UNIT_MILLIMETERS)
#delete existing colors
cols = scribus.getColorNames()
for col in cols:
scribus.deleteColor(col, "None")
#create our new colors
for color in colorList:
cname=color[0]
c = int(color[1])
m = int(color[2])
y = int(color[3])
k = int(color[4])
scribus.defineColor(cname, c, m, y, k )
#get the pageTitle form user and store it in PageTitle
global pageTitle
pageTitle=scribus.valueDialog("ColorChart by Sebastian Stetter", "Please enter document title", "Scribus COLOR CHART")
drawHeaderFooter(pageTitle)
def createPage():
"""appends a new page"""
scribus.newPage(-1) #append new page
#new page - new header and footer
drawHeaderFooter(pageTitle)
 
 
def rgbhex(r,g,b):
'''convert rgb values in 0-255 style to hex string in #000000 to #ffffff style'''
hr=hex(r)
hr = hr.replace("0x", "")
if len(hr)== 0:
hr = "00"
elif len(hr)==1:
hr = "0"+hr
else:
pass
hg=hex(g)
hg = hg.replace("0x", "")
if len(hg)== 0:
hg = "00"
elif len(hg)==1:
hg = "0"+hg
else:
pass
hb=hex(b)
hb = hb.replace("0x", "")
if len(hb)== 0:
hb = "00"
elif len(hb)==1:
hb = "0"+hb
else:
pass
rgbstring="#"+hr+hg+hb
rgbstring=rgbstring.upper()
return rgbstring
 
 
def drawColor(colorname, h, v, width, height): #h horizontal position, v vertical position
"""draws a color chart field with its caption for the given colorname at the h and v position"""
#get cmyk values and convert them to 0 - 255 values
color = scribus.getColor(colorname)
c= int(round(color[0]/2.55))
m=int(round(color[1]/2.55))
y=int(round(color[2]/2.55))
k=int(round(color[3]/2.55))
#get rgb color
rgbcolor=scribus.getColorAsRGB(colorname)
r=rgbcolor[0]
g=rgbcolor[1]
b=rgbcolor[2]
#get webcolor
webcolor=rgbhex(r, g, b)
#but String for Textbox together
colorstring="%s\nC %i, M %i, Y %i, K %i, \nR %i, G %i, B %i \nRGB: %s" %(colorname, c, m, y, k, r, g, b, webcolor)
#draw rectangle and set colors
rect=scribus.createRect(h, v, width, height)
scribus.setFillColor(colorname, rect)
#if total amount of color is < 20 draw outline in Black for rectangle, else in same color
if c +m+y+k < 20:
scribus.setLineColor("Black", rect)
else:
scribus.setLineColor(colorname, rect)
#create textbox and insert text
textbox=scribus.createText(h+width+5, v, 50, height)
#set proper font size
scribus.setFontSize(11, textbox)
scribus.setTextAlignment(scribus.ALIGN_LEFT, textbox)
#load the string into the textbox
scribus.insertText(colorstring, 0, textbox)
 
 
def createChart():
"""actually handles the whole chart creation process"""
prepareDocument()
# get page size
pageSize=scribus.getPageSize()
pageWidth=pageSize[0]
pageHeight=pageSize[1]
#pageMargins
pageMargins=scribus.getPageMargins()
topMargin=pageMargins[0]
leftMargin=pageMargins[1]
rightMargin=pageMargins[2]
bottomMargin=pageMargins[3]
#color field dimensions
colorFieldWidth= pageWidth - leftMargin - rightMargin - (TEXT_BOX_WIDTH+HSPACE) #50+5 is the with of the textbox plus the space between textbox and colorfield
#how much space does one field use?
vSpaceUsedByField = COLOR_FIELD_HEIGHT+VSPACE
#how much space is available per row?
vSpaceAvailable=pageHeight-topMargin-bottomMargin-HEADERSIZE-FOOTERSIZE
#counts the colorFields created for a page. reset this variable after creation of new page
colorFieldCounter=0
#get list of all colors in document
colorList = scribus.getColorNames()
#prepare the progressbar
colorNumber=len(colorList)
scribus.progressTotal(colorNumber)
#@TODO: implement possibility to abort script (button2=scribus.BUTTON_CANCEL) buttons should return int 1 or 2
#scribus.messageBox("ColorChart Script by Sebastian Stetter", "...going to create a chart of "+str(colorNumber)+" colors.\n This may take a while.", button1 = scribus.BUTTON_OK)
scribus.statusMessage("Drawing color fields...")
stepCompleted=0
#disable redrawing for better performance
scribus.setRedraw(False)
for color in colorList:
if (vSpaceUsedByField * (colorFieldCounter+1)) <= vSpaceAvailable:
# when there is enought space left draw a color field...
#calculate Position for new colorField
h=leftMargin
v=topMargin + (vSpaceUsedByField * colorFieldCounter)+HEADERSIZE
#draw the colorField
drawColor(color, h, v, colorFieldWidth, COLOR_FIELD_HEIGHT )
colorFieldCounter = colorFieldCounter+1
#update progressbar
stepCompleted = stepCompleted+1
scribus.progressSet(stepCompleted)
else:
#not enough space? create a new page!
createPage()
#reset the colorFieldCounter to '0' since we created a new page
colorFieldCounter = 0
h=leftMargin
v=topMargin + (vSpaceUsedByField * colorFieldCounter)+HEADERSIZE
drawColor(color, h, v, colorFieldWidth, COLOR_FIELD_HEIGHT )
colorFieldCounter = colorFieldCounter+1
 
#update progressbar
stepCompleted = stepCompleted+1
scribus.progressSet(stepCompleted)
#make shure pages are redrawn
scribus.setRedraw(True)
 
 
 
 
 
 
def main(argv):
"""just invokes createChart() and displays a message after the chart is finished."""
createChart()
scribus.messageBox("ColorChart Script by Sebastian Stetter", "Your chart has been created, but not saved, yet!\nThank's for using ColorChart and Scribus!")
 
 
 
def main_wrapper(argv):
"""The main_wrapper() function disables redrawing, sets a sensible generic
status bar message, and optionally sets up the progress bar. It then runs
the main() function. Once everything finishes it cleans up after the main()
function, making sure everything is sane before the script terminates."""
try:
scribus.statusMessage("Creating color chart...")
scribus.progressReset()
main(argv)
finally:
# Exit neatly even if the script terminated with an exception,
# so we leave the progress bar and status bar blank and make sure
# drawing is enabled.
if scribus.haveDoc():
scribus.setRedraw(True)
scribus.statusMessage("")
scribus.progressReset()
 
# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
main_wrapper(sys.argv)
/branches/Version133x/Scribus/scribus/plugins/scriptplugin/scripts/color2csv.py
0,0 → 1,160
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
ABOUT THIS SCRIPT:
 
Export Scribus Colors to CSV
 
color2csv.py allows a user to export the colors of a given scribus document in a csv file. The file will be a text file with comma seperated values in the following format:
"colorname", c,m,y,k
 
If there is a document opened in scribus, color2csv uses this document as color source.
If there is no document opened in scribus, color2csv displays a file open dialog to allow the user to chose a scribus file to get the colors from.
 
Use csv2color to import the colors into a scribus document from a csv file.
 
############################
 
LICENSE:
 
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.
 
Author: Sebastian Stetter
 
please report bugs to: scribusscript@sebastianstetter.de
"""
from __future__ import division
import sys
 
__version__=1.0
 
try:
# Please do not use 'from scribus import *' . If you must use a 'from import',
# Do so _after_ the 'import scribus' and only import the names you need, such
# as commonly used constants.
import scribus
except ImportError,err:
print "This Python script is written for the Scribus scripting interface."
print "It can only be run from within Scribus."
sys.exit(1)
 
#########################
# YOUR IMPORTS GO HERE #
#########################
import csv
import os
#define some variables
 
def getColorsFromDoc():
"""returns a list ("name", c,y,m,k)
get all the colors of that doc. """
#get a list of al the colornames
scribus.statusMessage("Reading Colors...")
 
try:
colorlist = scribus.getColorNames()
scribus.progressTotal(len(colorlist))
i=0
colordata=[]
for color in colorlist:
colorvalues=scribus.getColor(color)
c=int(colorvalues[0]/2.55) #convert values from 0-255 to 0-100
m=int(colorvalues[1]/2.55)
y=int(colorvalues[2]/2.55)
k=int(colorvalues[3]/2.55)
name=color.strip() #eliminate leading and tailing whitespace
cd = [name,c ,m,y,k]
colordata.append(cd)
i=i+1
scribus.progressSet(i)
return colordata
except:
scribus.messageBox("color2csv", "Can not retrieve colors - There is no Document", icon=scribus.ICON_WARNING)
sys.exit()
 
def writeColorCsvFile(filename, colorlist):
"""writes all the colors to a csv file"""
scribus.statusMessage("Writing colors to csv file...")
scribus.progressTotal(len(colorlist))
i=0
try:
csvwriter=csv.writer(file(filename, "w"), quoting=csv.QUOTE_NONNUMERIC)
for line in colorlist:
csvwriter.writerow(line)
i=i+1
scribus.progressSet(i)
except:
scribus.messageBox("color2csv", "Could not write file!", icon=scribus.ICON_WARNING)
sys.exit()
 
def main(argv):
"""Main method - here we check if we have a doc - else we open one. we get all the colors and write them to a csv file."""
if scribus.haveDoc(): #DOC OPEN
#get colors, get filename, write stuff
cols = getColorsFromDoc()
filename = scribus.fileDialog("color2csv: Save csv color file", defaultname="colors.csv", issave=True , haspreview=False)
#@TODO: optimize path checking
if filename !="":
if os.path.exists(filename): #make shure we don't accidentaly overwrite existing files
answer= scribus.messageBox("color2csv", "File already exists! \n do you want to overwrite it?", icon=scribus.ICON_WARNING, button1=scribus.BUTTON_YES, button2=scribus.BUTTON_ABORT)
if answer == scribus.BUTTON_YES:
writeColorCsvFile(filename, cols)
else:
sys.exit()
else:
writeColorCsvFile(filename, cols)
else:
sys.exit()
else: # NO DOC OPEN - open one!
scribusfile = scribus.fileDialog("color2csv: Open scribus file", "Scribus files(*.sla *.SLA *.sla.gz *.SLA.GZ)")
if scribusfile !="":
try:
scribus.openDoc(scribusfile)
except:
scribus.messageBox("color2csv", "Could not open file!")
sys.exit()
#get colors, get filename, write stuff
cols = getColorsFromDoc()
filename = scribus.fileDialog("color2csv: Save csv color file", defaultname="colors.csv", issave=True )
writeColorCsvFile(filename, cols)
else:
sys.exit()
 
def main_wrapper(argv):
"""The main_wrapper() function disables redrawing, sets a sensible generic
status bar message, and optionally sets up the progress bar. It then runs
the main() function. Once everything finishes it cleans up after the main()
function, making sure everything is sane before the script terminates."""
try:
#scribus.statusMessage("Running script...")
#scribus.progressReset()
main(argv)
finally:
# Exit neatly even if the script terminated with an exception,
# so we leave the progress bar and status bar blank and make sure
# drawing is enabled.
if scribus.haveDoc():
scribus.setRedraw(True)
scribus.statusMessage("")
scribus.progressReset()
 
# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
main_wrapper(sys.argv)
/branches/Version133x/Scribus/scribus/plugins/scriptplugin/scripts/csv2color.py
0,0 → 1,187
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
ABOUT THIS SCRIPT:
 
Import Colors from a CSV file to Scribus
 
csv2color.py allows a user to import colors from a given csv file into a scribus document.
The file must be a text file with comma seperated values in the following format:
 
"colorname", c,m,y,k
 
There must be a document opend in scribus where the colors can be defined in.
If the csv contanins one or more color names that already exist in the document, the colors will be imported with a `*` as prefix.
 
This script is especially helpfull if you want to use CMYK color representations of color systems like HKS, Pantone or RAL in Scribus. Lots of such CMYK translation tables can be found on the Web.
One can easily copy such a table into a text file, save it in the obove described format and import it into a scribus document.
 
Use color2csv to export the colors from a scribus document into a csv file.
 
############################
 
LICENSE:
 
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.
 
Author: Sebastian Stetter
 
please report bugs to: scribusscript@sebastianstetter.de
"""
from __future__ import division
import sys
 
__version__=1.1
 
 
try:
# Please do not use 'from scribus import *' . If you must use a 'from import',
# Do so _after_ the 'import scribus' and only import the names you need, such
# as commonly used constants.
import scribus
except ImportError,err:
print "This Python script is written for the Scribus scripting interface."
print "It can only be run from within Scribus."
sys.exit(1)
 
#########################
# YOUR IMPORTS GO HERE #
#########################
import csv
import os
 
PREFIX="*"
 
def checkValue(c, m, y, k):
"""returns true if the cmyk values are between 0 and 255"""
MINVAL=0
MAXVAL=255
valueOk=True
for val in c, m, y, k:
if val >=MINVAL and val <=255:
pass
else:
valueOk=False
return valueOk
 
def getColorsFromCsv(filename):
"""get colors from csv file and return a list with name and cmyk 255 values"""
csvreader=csv.reader(file(filename))
 
csvcolors=[]
i=0
for row in csvreader:
name=row[0]
name=name.strip()
c=int(row[1] )* 2.55
c=int(c)
m=int(row[2] )* 2.55
m=int(m)
y=int(row[3] )* 2.55
y=int(y)
k=int(row[4] )* 2.55
k=int(k)
if checkValue(c, m, y, k) ==False:
scribus.messageBox("csv2color", "At least one CMYK value in your csv file is not correct \n(must be between 0 and 100)\nAborting script - nothing imported.", icon=scribus.ICON_WARNING)
sys.exit()
else:
pass
color=(name, c, m, y, k)
csvcolors.append(color)
i=i+1
return csvcolors
 
def getColorDict():
"""get the colors that already exist from the opened Document and return a dictionary"""
scribus.statusMessage("Reading existing colors...")
colornames = scribus.getColorNames()
scribus.progressTotal(len(colornames))
i=0
colordict={}
for name in colornames:
colordict[name]=None
i=i+1
scribus.progressSet(i)
return colordict #we can ask this dict if the color already exists
def importColors(colorlist):
"""check if colors exists an import"""
colordict=getColorDict()
scribus.statusMessage("Defining new colors...")
scribus.progressTotal(len(colorlist))
i=0
for color in colorlist:
name=color[0]
c=color[1]
m=color[2]
y=color[3]
k=color[4]
while colordict.has_key(name):# check if color already exists - then add PREFIX to name
name = PREFIX+name
scribus.defineColor(name, c, m, y, k)
i=i+1
scribus.progressSet(i)
 
def main(argv):
"""Main method for importing colors."""
if not scribus.haveDoc(): #do we have a doc?
scribus.messageBox("csv2color", "No document to import colors \n Please open one, first.")
sys.exit()
else:
filename=scribus.fileDialog("csv2color", "CSV files(*.csv *.CSV *.txt *.TXT)")
while os.path.isdir(filename):
filename=scribus.fileDialog("csv2color", "CSV files(*.csv *.CSV *.txt *.TXT)") #proper filename?
else:
try:
colorlist=getColorsFromCsv(filename)
messagestring = "You are going to import %i colors \n This may take a while" % len(colorlist)
answer = scribus.messageBox("csv2color", messagestring, button1=scribus.BUTTON_OK, button2=scribus.BUTTON_CANCEL)
if answer != scribus.BUTTON_OK:
sys.exit()
else:
importColors(colorlist)
scribus.docChanged(True)
scribus.messageBox("csv2color", "Colors imported! \n Thank you for using csv2color and Scribus!")
except:
scribus.messageBox("csv2color", "Could not import file!", icon=scribus.ICON_WARNING)
sys.exit()
 
 
def main_wrapper(argv):
"""The main_wrapper() function disables redrawing, sets a sensible generic
status bar message, and optionally sets up the progress bar. It then runs
the main() function. Once everything finishes it cleans up after the main()
function, making sure everything is sane before the script terminates."""
try:
#scribus.statusMessage("Running script...")
scribus.progressReset()
main(argv)
finally:
# Exit neatly even if the script terminated with an exception,
# so we leave the progress bar and status bar blank and make sure
# drawing is enabled.
if scribus.haveDoc():
scribus.setRedraw(True)
scribus.statusMessage("")
scribus.progressReset()
 
# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
main_wrapper(sys.argv)
/branches/Version133x/Scribus/scribus/plugins/scriptplugin/scripts/Makefile.am
14,6 → 14,9
$(INSTALL_DATA) $(srcdir)/FontSample.py $(prefix)/share/scribus/scripts/FontSample.py
$(INSTALL_DATA) $(srcdir)/InfoBox.py $(prefix)/share/scribus/scripts/InfoBox.py
$(INSTALL_DATA) $(srcdir)/UnflipContent.py $(prefix)/share/scribus/scripts/UnflipContent.py
$(INSTALL_DATA) $(srcdir)/ColorChart.py $(prefix)/share/scribus/scripts/ColorChart.py
$(INSTALL_DATA) $(srcdir)/color2csv.py $(prefix)/share/scribus/scripts/color2csv.py
$(INSTALL_DATA) $(srcdir)/csv2color.py $(prefix)/share/scribus/scripts/csv2color.py
 
 
30,3 → 33,7
-rm -f $(prefix)/share/scribus/scripts/FontSample.py
-rm -f $(prefix)/share/scribus/scripts/InfoBox.py
-rm -f $(prefix)/share/scribus/scripts/UnflipContent.py
-rm -f $(prefix)/share/scribus/scripts/ColorChart.py
-rm -f $(prefix)/share/scribus/scripts/color2csv.py
-rm -f $(prefix)/share/scribus/scripts/csv2color.py
 
/branches/Version133x/Scribus/scribus/plugins/scriptplugin/scripts/CMakeLists.txt
14,5 → 14,8
NEWS
ReadMe
TODO
color2csv.py
csv2color.py
ColorChart.py
DESTINATION ${SCRIPTSDIR}
)