def makeBodyPages(doc, bodyText): """Create a number of new pages in the document, as long as there is overflow. If no new page size is given, it will take over the size of the document. """ bs = BabelString(bodyText, font=fontName, fontSize=bodyFontSize, lineHeight=bodyFontSize * leading) while True: page = doc.newPage() # Add text element with page number pn = BabelString(str(page.pn), align=CENTER, font=fontName, fontSize=bodyFontSize) page.addElement(Text(pn, page.w / 2, padding / 2)) e = TextBox(bs, x=padding, y=padding, w=page.w - 2 * padding, h=page.h - 2 * padding, fill=1) page.addElement(e) bs = e.getOverflow(bs, doc=doc) if not bs.fs: break
def oneColumnPage(doc, theme=None, page=None, pageNumbers=None, **kwargs): """ >>> from pagebotnano_010.document import Document >>> from pagebotnano_010.themes import BackToTheCity >>> theme = BackToTheCity() >>> doc = Document() >>> page = oneColumnPage(doc, theme, pageNumbers=NONE) >>> page.compose(doc) >>> page.find(MAIN) <TextBox name=mainText w=535 h=782> >>> page = doc.newPage(template=oneColumnPage) """ page = _initialize(doc, page) # Add text element with the main text column of this page e = TextBox('', name=MAIN, x=page.pl, y=page.pb, w=page.pw, h=page.ph) page.addElement(e) if pageNumbers is None: pageNumbers = [LEFT, RIGHT] if LEFT in pageNumbers and page.pn % 2 == 0: # Even page number?: bs = BabelString(str(page.pn), style) e = Text(bs, name=PN_LEFT, x=page.pl, y=page.pb / 2) page.addElement(e) if CENTER in pageNumbers: e = Text('', name=PN_CENTER, x=page.pl + page.pw, y=page.pb / 2) page.addElement(e) if RIGHT in pageNumbers: e = Text('', name=PN_RIGHT, x=page.pl + page.pw, y=page.pb / 2) page.addElement(e) return page
def page(self, doc): """ >>> from pagebotnano_010.document import Document >>> templates = OneColumnTemplates() >>> templates <OneColumnTemplates> >>> doc = Document(templates=templates) >>> page = templates.cover(doc) # The "page" template always must be there. >>> page.compose(doc) """ page = self._initialize(doc, True) # Add text element with the main text column of this page e = TextBox('', name=MAIN, x=page.pl, y=page.pb, w=page.pw, h=page.ph) page.addElement(e) leftPageNumberStyle = doc.theme.getStyle('leftPageNumber') centerPageNumberStyle = doc.theme.getStyle('centerPageNumber') rightPageNumberStyle = doc.theme.getStyle('rightPageNumber') if leftPageNumberStyle is not None and page.pn % 2 == 0: # Even page number?: bs = BabelString(str(page.pn), style) e = Text(bs, name=PN_LEFT, x=page.pl, y=page.pb / 2) page.addElement(e) if centerPageNumberStyle is not None: e = Text('', name=PN_CENTER, x=page.pl + page.pw, y=page.pb / 2) page.addElement(e) if rightPageNumberStyle is not None and page.pn % 2 != 0: # Odd page number?: e = Text('', name=PN_RIGHT, x=page.pl + page.pw, y=page.pb / 2) page.addElement(e) return page
def makeCoverPage(doc, title): page = doc.newPage() # Fill the page with a random dark color (< 50% for (r, g, b)) fillColor = random() * 0.5, random() * 0.5, random() * 0.5 rectangleElement = Rect(0, 0, page.w, page.h, fill=fillColor) page.addElement(rectangleElement) # Add the rectangle element to the page. # Make a FormattedString for the text box bs = BabelString(title, font=fontName, fontSize=titleSize, lineHeight=titleSize * 1.1, fill=1) # Make a Text element with an (x, y) position and add it to the page. textElement = Text(bs, x=padding, y=page.h - 1.5 * padding) page.addElement(textElement) # Add the text element to the page. # Add square image with frame around imagePath = '../resources/images/cookbot1.jpg' # Add square with light color (> 50% for (r, g, b)) and lighter frame. rx = ry = padding # Position from bottom-left rw = rh = page.w - 2 * padding # Make a square, so w = h strokeColor = 0.75 + random() * 0.25, 0.75 + random( ) * 0.25, 0.75 + random() * 0.25 imageElement = Image(imagePath, x=rx, y=ry, w=rw, h=rh, stroke=strokeColor, strokeWidth=5) page.addElement(imageElement) # Add the rectangle element to the page.
def addPageNumber(self, page, pad, leftStyle, rightStyle): # Add text element with page number if page.pn % 2 == 0: # Even page number? style = leftStyle x = pad else: # Odd page number style = rightStyle x = page.w - pad pn = BabelString(str(page.pn), style) # Center the page number. #e = Text(pn, page.w/2, pad/2) e = Text(pn, x=x, y=pad*3/4, w=page.w - 2*pad, fill=0.9) page.addElement(e)
def cover(self, doc): page = self._initialize(doc, True) # Fill the cover page with a theme background color e = Rect(0, 0, page.w, page.h, fill=doc.theme.getColor(1, 4)) # Dark cover color page.addElement(e) # Add title and author, centered on top-half of the cover. try: title = doc.cd.elements[0].bs except (IndexError, AttributeError): title = BabelString('Untitled') e = TextBox(title, name='Title', x=page.pl / 2, y=page.h / 2, w=page.pw, h=page.ph / 2) page.addElement(e) e = Image(x=page.pl, y=page.pb, w=page.pw) page.addElement(e) return page
def __init__(self, bs, x, y, w=None, h=None, name=None, fill=None, stroke=None, strokeWidth=None): # Call the base element with all standard attributes. Element.__init__(self, x=x, y=y, w=w, h=h, name=name, fill=fill, stroke=stroke, strokeWidth=strokeWidth) if not isinstance(bs, BabelString): bs = BabelString(bs) self.bs = bs # Store the BabelString in self.
def compose(self): """This is the core of a publication, composing the specific content of the document. The compose method gets called before building and exporting the self.doc document. """ fontSize = 11 headSize = fontSize*1.5 titleSize = 36 subTitleSize = titleSize * 0.5 pad = 48 self.theme.styles['h1'] = dict(font='Georgia-Bold', lineHeight=titleSize*1.1, fontSize=titleSize, align=CENTER, fill=1, # White title on dark cover background language=EN, hyphenation=False, ) self.theme.styles['h2'] = dict(font='Georgia-Italic', paragraphTopSpacing=subTitleSize/2, lineHeight=subTitleSize*1.2, fontSize=subTitleSize, align=CENTER, fill=1, # White title on dark cover background language=EN, hyphenation=False, ) headStyle = dict(font='Georgia', lineHeight=headSize*1.3, fontSize=headSize, fill=0, # Black text language=EN, hyphenation=False, ) subHeadStyle = dict(font='Georgia-Italic', lineHeight=headSize*0.8*1.4, fontSize=headSize*0.8, fill=0, # Black text language=EN, hyphenation=False, ) bodyStyle = dict(font='Georgia', lineHeight=fontSize*1.4, fontSize=fontSize, fill=0, # Black text language=EN, hyphenation=True, ) pageNumberLeftStyle = dict( font='Georgia', fontSize=9, fill=0, # Black text align=LEFT, ) pageNumberRightStyle = copy(pageNumberLeftStyle) pageNumberRightStyle['align'] = RIGHT # Make the cover page. page = coverPage(self.theme, self.doc) # Make “French” “Voordehandse” page. page = self.doc.newPage() # No page number here. # CENTER text alignment overwrites the value in headStyle. # fontSize overwrites the value in headStyle bs = BabelString('AAAA'+'\n', headStyle, fontSize=fontSize, align=CENTER) e = Text(bs, x=page.w/2, y=page.h*4/5) page.addElement(e) # Make Title page. page = titlePage(self.theme, self.doc) page.compose(self.doc, page) bs = BabelString('VVVVV'+'\n', headStyle, align=CENTER) bs.append(BabelString('AUTHOR', subHeadStyle, align=CENTER)) page.find(MAIN).bs = bs # For all the elements that are collected in the galley, assume that # the TextBoxes are chapters, creating a new page for them. # If the TextBox does not fit on the page, keep adding new pages # until all of the BabelString overfill is processed. for ge in self.galley.elements: if isinstance(ge, TextBox): bs = ge.bs # Get the BabelString from the galley box. for n in range(self.MAX_PAGES): page = self.doc.newPage() # Add text element with page number self.addPageNumber(page, pad, pageNumberLeftStyle, pageNumberRightStyle) # Add text element with the main text column of this page e = TextBox(bs, x=pad, y=pad, w=page.w-2*pad, h=page.h-2*pad) page.addElement(e) # If there is overflow on this page, continue looping creating # as many pages as needed to fill all the text in self.content. # Otherwise break the loop, as we are done placing content. bs = e.getOverflow(bs, doc=self.doc) # Test on this “incomplete” BabelString, as it only has a cached FS if not bs.fs: break elif isinstance(ge, Image): # Images not supported yet page = self.doc.newPage() self.addPageNumber(page, pad, pageNumberLeftStyle, pageNumberRightStyle) page.addElement(ge) ge.w = page.w - pad iw, ih = ge.getSize(self.doc) ge.x = pad/2 ge.y = page.h - pad - ih
def textBox(self, bs, r): # Set the cache from the overflow, we don't have the source anymore overFlow = BabelString(hyphenation=bs.hyphenation) overFlow.fs = drawBot.textBox(bs.fs, r) return overFlow # Return this “incomplete” BabelString.