コード例 #1
0
    def page(self, doc):
        """
        >>> from pagebotnano.document import Document
        >>> from pagebotnano.themes import BackToTheCity
        >>> templates = OneColumnTemplates()
        >>> templates
        <OneColumnTemplates>
        >>> theme = BackToTheCity()
        >>> doc = Document(theme=theme, 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
コード例 #2
0
    def oneColumnPage(cls, theme, doc, page=None, parent=None, pageNumbers=None, **kwargs):
        """
        >>> from pagebotnano.document import Document
        >>> from pagebotnano.themes import BackToTheCity
        >>> template = OneColumnTemplate()
        >>> theme = BackToTheCity()
        >>> doc = Document()
        >>> page = template.oneColumnPage(theme, doc, pageNumbers=NONE)
        >>> page.compose(doc)
        >>> page.find(MAIN)
        <TextBox name=mainText w=535 h=782>
        >>> page = doc.newPage(template=oneColumnPage)
        """
        page = cls._initialize(theme, doc, page, parent)
        # 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
コード例 #3
0
 def chapter(self, doc):
     """If this template is called, a new chapter starts.
     Create a new page, select is as doc.page, create a new text box and make select it
     as case.flow.
     """
     page = self._initialize(doc, True)
     e = TextBox('', name=MAIN, x=page.pl, y=page.pb, w=page.pw, h=page.ph)
     page.addElement(e)
     return page
コード例 #4
0
    def coverPage(cls, theme, doc, page=None, parent=None, **kwargs):
        page = cls._initialize(theme, doc, page, parent)
        # 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=page.pl, y=page.pb, w=page.pw)
        page.addElement(e)
コード例 #5
0
    def colophon(self, doc):
        """Compose the template page with the position of the ColophonPage.
        Empty page, with only the title of the book centere on the page width.

        >>> from pagebotnano.document import Document
        >>> from pagebotnano.themes import BackToTheCity
        >>> templates = OneColumnTemplates()
        >>> theme = BackToTheCity()
        >>> doc = Document(theme=theme)
        >>> page = templates.colophon(doc) # Creating a new page
        >>> page.compose(doc)
        """
        page = self._initialize(doc, True)
        e = TextBox('', name=MAIN, x=page.pl, y=page.pb, w=page.pw, h=page.ph)
        page.addElement(e)
        return page
コード例 #6
0
    def colophonPage(cls, theme, doc, page=None, parent=None, **kwargs):
        """Compose the template page with the position of the ColophonPage.
        Empty page, with only the title of the book centere on the page width.

        >>> from pagebotnano.document import Document
        >>> from pagebotnano.themes import BackToTheCity
        >>> template = OneColumnTemplate()
        >>> theme = BackToTheCity()
        >>> doc = Document()
        >>> page = template.colophonPage(theme, doc) # Creating a new page
        >>> page.compose(doc, page)
        >>> page.find(MAIN)
        <TextBox name=mainText w=535 h=782>
        """
        page = _initialize(doc, page, parent)
        e = TextBox('', name=MAIN, x=page.pl, y=page.pb, w=page.pw, h=page.ph)
        page.addElement(e)
        return page
コード例 #7
0
    def frenchTitle(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
コード例 #8
0
    def getTextBox(self, e=None):
        """Answer the last TextBox element if it exists. 
        Otherwise create it first.

        >>> ts = Typesetter()
        >>> e = ts.getTextBox()
        >>> e
        <TextBox name=TextBox w=100 h=None>
        >>> e1 = ts.getTextBox() # Gets the existing last TextBox
        >>> e is e1
        True
        >>> e2 = TextBox('', x=0, y=0, w=DEFAULT_WIDTH)
        >>> ts.galley.addElement(e2)
        <TextBox name=TextBox w=100 h=None>
        >>> e2 is ts.getTextBox() # Now finding the new one as last
        True
        """
        if e is None:
            e = self.galley
        if not e.elements or not isinstance(e.elements[-1], TextBox):
            e.addElement(TextBox('', x=0, y=0, w=e.w or DEFAULT_WIDTH))
        return e.elements[-1]
コード例 #9
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
コード例 #10
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

        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