Example #1
0
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.
Example #2
0
def coverPage(doc, theme=None, page=None, **kwargs):
    page = _initialize(doc, page)
    # Fill the cover page with a theme background color
    e = Rect(0, 0, page.w, page.h, fill=theme.getColor(1,
                                                       4))  # Dark cover color
    page.addElement(e)

    # Add title and author, centered on top-half of the cover.
    e = TextBox('',
                name='Title',
                x=page.pl / 2,
                y=page.h / 2,
                w=page.pw,
                h=page.ph / 2)
    page.addElement(e)

    e = Image(x=pad, y=pad, w=page.w - 2 * pad)
    page.addElement(e)
Example #3
0
    def title(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.
        e = TextBox('',
                    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
Example #4
0
    def frenchTitle(self, doc):
        """In Dutch it is called the "Franse pagina", which comes from
        "Voordehandse pagina" (the first page in the book)
        """
        page = self._initialize(doc, True)
        # Fill the cover page with a theme background color
        e = Rect(0, 0, page.w, page.h, fill=color(0.8))  # Dark cover color
        page.addElement(e)

        # Add title and author, centered on top-half of the cover.
        e = TextBox('',
                    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
Example #5
0
    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
Example #6
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.templates['french'](self.doc) # 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.
        template = self.templates['title'] # Template function that generates a title page.
        page = template(self.doc) # No page number here.
        page.compose(self.doc, page)
        bs = BabelString(self.title+'\n', headStyle, align=CENTER)
        bs.append(BabelString(self.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