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 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.
示例#3
0
 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)
示例#4
0
 def __init__(self,
              bs,
              x,
              y,
              w=None,
              h=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,
                      fill=fill,
                      stroke=stroke,
                      strokeWidth=strokeWidth)
     if not isinstance(bs, BabelString):
         bs = BabelString(bs)
     self.bs = bs  # Store the BabelString in self.
示例#5
0
    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

        titleStyle = dict(font='Georgia-Bold', 
            lineHeight=titleSize*1.1, 
            fontSize=titleSize,
            align=CENTER,
            fill=1, # White title on dark cover background
            language=EN, hyphenation=False,
        )
        subTitleStyle = 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 = self.doc.newPage()

        # Fill the cover page with a random dark color
        e = Rect(0, 0, page.w, page.h, fill=self.coverColor) # Dark cover color
        page.addElement(e) 

        # Add title and author, centered on top-half of the cover.
        titleBs = BabelString(self.title+'\n', titleStyle)
        authorBs = BabelString(self.author, subTitleStyle)
        bs = titleBs + authorBs
        e = TextBox(bs, x=pad/2, y=page.h/2, w=page.w-pad, h=page.h/2-pad)
        page.addElement(e)

        if self.coverImagePath is not None: # Only if not defined.
            e = Image(self.coverImagePath, x=pad, y=pad, w=page.w-2*pad)
            page.addElement(e)

        # 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(self.title+'\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 = self.doc.newPage() # No page number here.
        bs = BabelString(self.title+'\n', headStyle, align=CENTER)
        bs.append(BabelString(self.author, subHeadStyle, align=CENTER))
        e = Text(bs, x=page.w/2, y=page.h*3/4)
        page.addElement(e)

        # 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
示例#6
0
 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.