put in class, args as string
This commit is contained in:
parent
fc83d43a5b
commit
fda4630621
186
dominion_tabs.py
186
dominion_tabs.py
@ -10,7 +10,31 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|||||||
from reportlab.pdfbase.ttfonts import TTFont
|
from reportlab.pdfbase.ttfonts import TTFont
|
||||||
from reportlab.pdfbase import pdfmetrics
|
from reportlab.pdfbase import pdfmetrics
|
||||||
|
|
||||||
labelImages = {
|
def split(l,n):
|
||||||
|
i = 0
|
||||||
|
while i < len(l) - n:
|
||||||
|
yield l[i:i+n]
|
||||||
|
i += n
|
||||||
|
yield l[i:]
|
||||||
|
|
||||||
|
class Card:
|
||||||
|
def __init__(self,name,cardset,types,cost,description,potcost=0):
|
||||||
|
self.name = name
|
||||||
|
self.cardset = cardset
|
||||||
|
self.types = types
|
||||||
|
self.cost = cost
|
||||||
|
self.potcost = potcost
|
||||||
|
self.description = description
|
||||||
|
self.extra = ""
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '"' + self.name + '"'
|
||||||
|
|
||||||
|
def toString(self):
|
||||||
|
return self.name + ' ' + self.cardset + ' ' + '-'.join(self.types) + ' ' + `self.cost` + ' ' + self.description + ' ' + self.extra
|
||||||
|
|
||||||
|
class DominionTabs:
|
||||||
|
labelImages = {
|
||||||
('Action',) : 'action.png',
|
('Action',) : 'action.png',
|
||||||
('Action','Attack') : 'action.png',
|
('Action','Attack') : 'action.png',
|
||||||
('Action','Attack','Prize') : 'action.png',
|
('Action','Attack','Prize') : 'action.png',
|
||||||
@ -24,47 +48,48 @@ labelImages = {
|
|||||||
('Treasure','Prize') : 'treasure.png',
|
('Treasure','Prize') : 'treasure.png',
|
||||||
('Victory',) : 'victory.png',
|
('Victory',) : 'victory.png',
|
||||||
('Curse',) : 'curse.png'
|
('Curse',) : 'curse.png'
|
||||||
}
|
}
|
||||||
|
|
||||||
def drawTab(card,x,y,useExtra=False):
|
def drawTab(self,c,card,x,y,useExtra=False):
|
||||||
#rightSide = False
|
#rightSide = False
|
||||||
if numTabsHorizontal == 2:
|
if self.numTabsHorizontal == 2:
|
||||||
rightSide = x%2 == 1
|
rightSide = x%2 == 1
|
||||||
else:
|
else:
|
||||||
rightSide = useExtra
|
rightSide = useExtra
|
||||||
c.resetTransforms()
|
c.resetTransforms()
|
||||||
c.translate(horizontalMargin,verticalMargin)
|
c.translate(self.horizontalMargin,self.verticalMargin)
|
||||||
if useExtra:
|
if useExtra:
|
||||||
c.translate(options.back_offset,0)
|
c.translate(self.options.back_offset,0)
|
||||||
c.translate(x*tabWidth,y*tabTotalHeight)
|
c.translate(x*self.tabWidth,y*self.tabTotalHeight)
|
||||||
|
|
||||||
#draw outline
|
#draw outline
|
||||||
#don't draw outline on back, in case lines don't line up with front
|
#don't draw outline on back, in case lines don't line up with front
|
||||||
if not useExtra:
|
if not useExtra:
|
||||||
c.saveState()
|
c.saveState()
|
||||||
c.setLineWidth(0.1)
|
c.setLineWidth(0.1)
|
||||||
if rightSide and not options.sameside:
|
if rightSide and not self.options.sameside:
|
||||||
c.translate(tabWidth,0)
|
c.translate(self.tabWidth,0)
|
||||||
c.scale(-1,1)
|
c.scale(-1,1)
|
||||||
c.lines(tabOutline)
|
c.lines(self.tabOutline)
|
||||||
c.restoreState()
|
c.restoreState()
|
||||||
|
|
||||||
#draw tab flap
|
#draw tab flap
|
||||||
c.saveState()
|
c.saveState()
|
||||||
if not rightSide or options.sameside:
|
if not rightSide or self.options.sameside:
|
||||||
c.translate(tabWidth-tabLabelWidth,tabTotalHeight-tabLabelHeight)
|
c.translate(self.tabWidth-self.tabLabelWidth,
|
||||||
|
self.tabTotalHeight-self.tabLabelHeight)
|
||||||
else:
|
else:
|
||||||
c.translate(0,tabTotalHeight-tabLabelHeight)
|
c.translate(0,self.tabTotalHeight-self.tabLabelHeight)
|
||||||
c.drawImage(labelImages[card.types],1,0,
|
c.drawImage(DominionTabs.labelImages[card.types],1,0,
|
||||||
tabLabelWidth-2,tabLabelHeight-1,
|
self.tabLabelWidth-2,self.tabLabelHeight-1,
|
||||||
preserveAspectRatio=False,anchor='n')
|
preserveAspectRatio=False,anchor='n')
|
||||||
if card.types[0] == 'Treasure' or card.types == ('Curse',):
|
if card.types[0] == 'Treasure' or card.types == ('Curse',):
|
||||||
textHeight = tabLabelHeight/2-4
|
textHeight = self.tabLabelHeight/2-4
|
||||||
costHeight = textHeight
|
costHeight = textHeight
|
||||||
potSize = 12
|
potSize = 12
|
||||||
potHeight = 5
|
potHeight = 5
|
||||||
else:
|
else:
|
||||||
textHeight = tabLabelHeight/2-7
|
textHeight = self.tabLabelHeight/2-7
|
||||||
costHeight = textHeight-1
|
costHeight = textHeight-1
|
||||||
potSize = 11
|
potSize = 11
|
||||||
potHeight = 2
|
potHeight = 2
|
||||||
@ -130,8 +155,8 @@ def drawTab(card,x,y,useExtra=False):
|
|||||||
s = getSampleStyleSheet()['BodyText']
|
s = getSampleStyleSheet()['BodyText']
|
||||||
s.fontName = "Times-Roman"
|
s.fontName = "Times-Roman"
|
||||||
p = Paragraph(d,s)
|
p = Paragraph(d,s)
|
||||||
textHeight = tabTotalHeight - tabLabelHeight + 0.2*cm
|
textHeight = self.tabTotalHeight - self.tabLabelHeight + 0.2*cm
|
||||||
textWidth = tabWidth - cm
|
textWidth = self.tabWidth - cm
|
||||||
|
|
||||||
w,h = p.wrap(textWidth,textHeight)
|
w,h = p.wrap(textWidth,textHeight)
|
||||||
while h > textHeight:
|
while h > textHeight:
|
||||||
@ -143,23 +168,7 @@ def drawTab(card,x,y,useExtra=False):
|
|||||||
p.drawOn(c,cm/2.0,textHeight-height-h-0.5*cm)
|
p.drawOn(c,cm/2.0,textHeight-height-h-0.5*cm)
|
||||||
height += h + 0.2*cm
|
height += h + 0.2*cm
|
||||||
|
|
||||||
class Card:
|
def read_card_extras(self,fname,cards):
|
||||||
def __init__(self,name,cardset,types,cost,description,potcost=0):
|
|
||||||
self.name = name
|
|
||||||
self.cardset = cardset
|
|
||||||
self.types = types
|
|
||||||
self.cost = cost
|
|
||||||
self.potcost = potcost
|
|
||||||
self.description = description
|
|
||||||
self.extra = ""
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return '"' + self.name + '"'
|
|
||||||
|
|
||||||
def toString(self):
|
|
||||||
return self.name + ' ' + self.cardset + ' ' + '-'.join(self.types) + ' ' + `self.cost` + ' ' + self.description + ' ' + self.extra
|
|
||||||
|
|
||||||
def read_card_extras(fname,cards):
|
|
||||||
f = open(fname)
|
f = open(fname)
|
||||||
cardName = re.compile("^:::(?P<name>[ \w']*)")
|
cardName = re.compile("^:::(?P<name>[ \w']*)")
|
||||||
extras = {}
|
extras = {}
|
||||||
@ -186,7 +195,7 @@ def read_card_extras(fname,cards):
|
|||||||
c.extra = extras[c.name]
|
c.extra = extras[c.name]
|
||||||
#print c.name + ' ::: ' + extra
|
#print c.name + ' ::: ' + extra
|
||||||
|
|
||||||
def read_card_defs(fname):
|
def read_card_defs(self,fname):
|
||||||
cards = []
|
cards = []
|
||||||
f = open(fname)
|
f = open(fname)
|
||||||
carddef = re.compile("^\d+\t+(?P<name>[\w' ]+)\t+(?P<set>\w+)\t+(?P<type>[-\w ]+)\t+\$(?P<cost>\d+)( (?P<potioncost>\d)+P)?\t+(?P<description>.*)")
|
carddef = re.compile("^\d+\t+(?P<name>[\w' ]+)\t+(?P<set>\w+)\t+(?P<type>[-\w ]+)\t+\$(?P<cost>\d+)( (?P<potioncost>\d)+P)?\t+(?P<description>.*)")
|
||||||
@ -219,36 +228,28 @@ def read_card_defs(fname):
|
|||||||
#print '----'
|
#print '----'
|
||||||
return cards
|
return cards
|
||||||
|
|
||||||
def split(l,n):
|
def drawCards(self,c,cards):
|
||||||
i = 0
|
cards = split(cards,self.numTabsVertical*self.numTabsHorizontal)
|
||||||
while i < len(l) - n:
|
|
||||||
yield l[i:i+n]
|
|
||||||
i += n
|
|
||||||
yield l[i:]
|
|
||||||
|
|
||||||
def drawCards(c,cards):
|
|
||||||
cards = split(cards,numTabsVertical*numTabsHorizontal)
|
|
||||||
for pageCards in cards:
|
for pageCards in cards:
|
||||||
print 'pageCards:',pageCards
|
print 'pageCards:',pageCards
|
||||||
for i,card in enumerate(pageCards):
|
for i,card in enumerate(pageCards):
|
||||||
#print card
|
#print card
|
||||||
x = i % numTabsHorizontal
|
x = i % self.numTabsHorizontal
|
||||||
y = i / numTabsHorizontal
|
y = i / self.numTabsHorizontal
|
||||||
c.saveState()
|
c.saveState()
|
||||||
drawTab(card,x,numTabsVertical-1-y)
|
self.drawTab(c,card,x,self.numTabsVertical-1-y)
|
||||||
c.restoreState()
|
c.restoreState()
|
||||||
c.showPage()
|
c.showPage()
|
||||||
for i,card in enumerate(pageCards):
|
for i,card in enumerate(pageCards):
|
||||||
#print card
|
#print card
|
||||||
x = (numTabsHorizontal-1-i) % numTabsHorizontal
|
x = (self.numTabsHorizontal-1-i) % self.numTabsHorizontal
|
||||||
y = i / numTabsHorizontal
|
y = i / self.numTabsHorizontal
|
||||||
c.saveState()
|
c.saveState()
|
||||||
drawTab(card,x,numTabsVertical-1-y,useExtra=True)
|
self.drawTab(c,card,x,self.numTabsVertical-1-y,useExtra=True)
|
||||||
c.restoreState()
|
c.restoreState()
|
||||||
c.showPage()
|
c.showPage()
|
||||||
|
|
||||||
if __name__=='__main__':
|
def main(self,argstring):
|
||||||
|
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
parser.add_option("--back_offset",type="int",dest="back_offset",default=5,
|
parser.add_option("--back_offset",type="int",dest="back_offset",default=5,
|
||||||
help="Points to offset the back page to the right; needed for some printers")
|
help="Points to offset the back page to the right; needed for some printers")
|
||||||
@ -265,10 +266,10 @@ if __name__=='__main__':
|
|||||||
help="force all label tabs to be on the same side")
|
help="force all label tabs to be on the same side")
|
||||||
parser.add_option("--expansions",action="append",type="string",
|
parser.add_option("--expansions",action="append",type="string",
|
||||||
help="subset of dominion expansions to produce tabs for")
|
help="subset of dominion expansions to produce tabs for")
|
||||||
(options,args) = parser.parse_args()
|
(self.options,args) = parser.parse_args(argstring)
|
||||||
|
|
||||||
size = options.size.upper()
|
size = self.options.size.upper()
|
||||||
if size == 'SLEEVED' or options.sleeved:
|
if size == 'SLEEVED' or self.options.sleeved:
|
||||||
dominionCardWidth, dominionCardHeight = (9.4*cm, 6.15*cm)
|
dominionCardWidth, dominionCardHeight = (9.4*cm, 6.15*cm)
|
||||||
print 'Using sleeved card size, %.2fcm x %.2fcm' % (dominionCardWidth/cm,dominionCardHeight/cm)
|
print 'Using sleeved card size, %.2fcm x %.2fcm' % (dominionCardWidth/cm,dominionCardHeight/cm)
|
||||||
elif size == 'NORMAL':
|
elif size == 'NORMAL':
|
||||||
@ -280,11 +281,11 @@ if __name__=='__main__':
|
|||||||
print 'Using custom card size, %.2fcm x %.2fcm' % (dominionCardWidth/cm,dominionCardHeight/cm)
|
print 'Using custom card size, %.2fcm x %.2fcm' % (dominionCardWidth/cm,dominionCardHeight/cm)
|
||||||
|
|
||||||
papersize = None
|
papersize = None
|
||||||
if not options.papersize:
|
if not self.options.papersize:
|
||||||
if os.path.exists("/etc/papersize"):
|
if os.path.exists("/etc/papersize"):
|
||||||
papersize = open ("/etc/papersize").readline().upper()
|
papersize = open ("/etc/papersize").readline().upper()
|
||||||
else:
|
else:
|
||||||
papersize = options.papersize.upper()
|
papersize = self.options.papersize.upper()
|
||||||
|
|
||||||
if papersize == 'A4':
|
if papersize == 'A4':
|
||||||
print "Using A4 sized paper."
|
print "Using A4 sized paper."
|
||||||
@ -293,40 +294,47 @@ if __name__=='__main__':
|
|||||||
print "Using letter sized paper."
|
print "Using letter sized paper."
|
||||||
paperwidth, paperheight = LETTER
|
paperwidth, paperheight = LETTER
|
||||||
|
|
||||||
minmarginwidth, minmarginheight = options.minmargin.split ("x", 1)
|
minmarginwidth, minmarginheight = self.options.minmargin.split ("x", 1)
|
||||||
minmarginwidth, minmarginheight = float (minmarginwidth) * cm, float (minmarginheight) * cm
|
minmarginwidth, minmarginheight = float (minmarginwidth) * cm, float (minmarginheight) * cm
|
||||||
|
|
||||||
if options.orientation == "vertical":
|
if self.options.orientation == "vertical":
|
||||||
tabWidth, tabBaseHeight = dominionCardHeight, dominionCardWidth
|
self.tabWidth, self.tabBaseHeight = dominionCardHeight, dominionCardWidth
|
||||||
else:
|
else:
|
||||||
tabWidth, tabBaseHeight = dominionCardWidth, dominionCardHeight
|
self.tabWidth, self.tabBaseHeight = dominionCardWidth, dominionCardHeight
|
||||||
|
|
||||||
tabLabelHeight = 0.9*cm
|
self.tabLabelHeight = 0.9*cm
|
||||||
tabLabelWidth = 3.5*cm
|
self.tabLabelWidth = 3.5*cm
|
||||||
tabTotalHeight = tabBaseHeight + tabLabelHeight
|
self.tabTotalHeight = self.tabBaseHeight + self.tabLabelHeight
|
||||||
|
|
||||||
numTabsVerticalP = int ((paperheight - 2*minmarginheight) / tabTotalHeight)
|
numTabsVerticalP = int ((paperheight - 2*minmarginheight) / self.tabTotalHeight)
|
||||||
numTabsHorizontalP = int ((paperwidth - 2*minmarginwidth) / tabWidth)
|
numTabsHorizontalP = int ((paperwidth - 2*minmarginwidth) / self.tabWidth)
|
||||||
numTabsVerticalL = int ((paperwidth - 2*minmarginwidth) / tabWidth)
|
numTabsVerticalL = int ((paperwidth - 2*minmarginwidth) / self.tabWidth)
|
||||||
numTabsHorizontalL = int ((paperheight - 2*minmarginheight) / tabTotalHeight)
|
numTabsHorizontalL = int ((paperheight - 2*minmarginheight) / self.tabTotalHeight)
|
||||||
|
|
||||||
if numTabsVerticalL * numTabsHorizontalL > numTabsVerticalP * numTabsHorizontalP:
|
if numTabsVerticalL * numTabsHorizontalL > numTabsVerticalP * numTabsHorizontalP:
|
||||||
numTabsVertical, numTabsHorizontal = numTabsVerticalL, numTabsHorizontalL
|
self.numTabsVertical, self.numTabsHorizontal\
|
||||||
|
= numTabsVerticalL, numTabsHorizontalL
|
||||||
paperheight, paperwidth = paperwidth, paperheight
|
paperheight, paperwidth = paperwidth, paperheight
|
||||||
else:
|
else:
|
||||||
numTabsVertical, numTabsHorizontal = numTabsVerticalP, numTabsHorizontalP
|
self.numTabsVertical, self.numTabsHorizontal\
|
||||||
|
= numTabsVerticalP, numTabsHorizontalP
|
||||||
|
|
||||||
horizontalMargin = (paperwidth-numTabsHorizontal*tabWidth)/2
|
self.horizontalMargin = (paperwidth-self.numTabsHorizontal*self.tabWidth)/2
|
||||||
verticalMargin = (paperheight-numTabsVertical*tabTotalHeight)/2
|
self.verticalMargin = (paperheight-self.numTabsVertical*self.tabTotalHeight)/2
|
||||||
|
|
||||||
print "Margins: %fcm h, %fcm v\n" % (horizontalMargin / cm, verticalMargin / cm)
|
print "Margins: %fcm h, %fcm v\n" % (self.horizontalMargin / cm,
|
||||||
|
self.verticalMargin / cm)
|
||||||
|
|
||||||
tabOutline = [(0,0,tabWidth,0),
|
self.tabOutline = [(0,0,self.tabWidth,0),
|
||||||
(tabWidth,0,tabWidth,tabTotalHeight),
|
(self.tabWidth,0,self.tabWidth,self.tabTotalHeight),
|
||||||
(tabWidth,tabTotalHeight,tabWidth-tabLabelWidth,tabTotalHeight),
|
(self.tabWidth,self.tabTotalHeight,
|
||||||
(tabWidth-tabLabelWidth,tabTotalHeight,tabWidth-tabLabelWidth,tabBaseHeight),
|
self.tabWidth-self.tabLabelWidth,self.tabTotalHeight),
|
||||||
(tabWidth-tabLabelWidth,tabBaseHeight,0,tabBaseHeight),
|
(self.tabWidth-self.tabLabelWidth,
|
||||||
(0,tabBaseHeight,0,0)]
|
self.tabTotalHeight,self.tabWidth-self.tabLabelWidth,
|
||||||
|
self.tabBaseHeight),
|
||||||
|
(self.tabWidth-self.tabLabelWidth,
|
||||||
|
self.tabBaseHeight,0,self.tabBaseHeight),
|
||||||
|
(0,self.tabBaseHeight,0,0)]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pdfmetrics.registerFont(TTFont('MinionPro-Regular','MinionPro-Regular.ttf'))
|
pdfmetrics.registerFont(TTFont('MinionPro-Regular','MinionPro-Regular.ttf'))
|
||||||
@ -334,12 +342,12 @@ if __name__=='__main__':
|
|||||||
except:
|
except:
|
||||||
pdfmetrics.registerFont(TTFont('MinionPro-Regular','OptimusPrincepsSemiBold.ttf'))
|
pdfmetrics.registerFont(TTFont('MinionPro-Regular','OptimusPrincepsSemiBold.ttf'))
|
||||||
pdfmetrics.registerFont(TTFont('MinionPro-Bold','OptimusPrinceps.ttf'))
|
pdfmetrics.registerFont(TTFont('MinionPro-Bold','OptimusPrinceps.ttf'))
|
||||||
cards = read_card_defs("dominion_cards.txt")
|
cards = self.read_card_defs("dominion_cards.txt")
|
||||||
if options.expansions:
|
if self.options.expansions:
|
||||||
options.expansions = [o.lower() for o in options.expansions]
|
self.options.expansions = [o.lower() for o in self.options.expansions]
|
||||||
cards=[c for c in cards if c.cardset in options.expansions]
|
cards=[c for c in cards if c.cardset in self.options.expansions]
|
||||||
cards.sort(cmp=lambda x,y: cmp((x.cardset,x.name),(y.cardset,y.name)))
|
cards.sort(cmp=lambda x,y: cmp((x.cardset,x.name),(y.cardset,y.name)))
|
||||||
extras = read_card_extras("dominion_card_extras.txt",cards)
|
extras = self.read_card_extras("dominion_card_extras.txt",cards)
|
||||||
print '%d cards read' % len(cards)
|
print '%d cards read' % len(cards)
|
||||||
sets = {}
|
sets = {}
|
||||||
types = {}
|
types = {}
|
||||||
@ -355,6 +363,10 @@ if __name__=='__main__':
|
|||||||
fname = "dominion_tabs.pdf"
|
fname = "dominion_tabs.pdf"
|
||||||
c = canvas.Canvas(fname, pagesize=(paperwidth, paperheight))
|
c = canvas.Canvas(fname, pagesize=(paperwidth, paperheight))
|
||||||
#pprint.pprint(c.getAvailableFonts())
|
#pprint.pprint(c.getAvailableFonts())
|
||||||
drawCards(c,cards)
|
self.drawCards(c,cards)
|
||||||
c.save()
|
c.save()
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
import sys
|
||||||
|
tabs = DominionTabs()
|
||||||
|
tabs.main(sys.argv[1:])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user