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