示例#1
0
def makeDocument():
    u"""Demo random book cover generator."""

    # Create new document with (w,h) and fixed amount of pages.
    # Make number of pages with default document size.
    # Initially make all pages default with template
    # One page, just the cover.
    doc = Document(w=W,
                   h=H,
                   title='A Demo Book Cover',
                   autoPages=1,
                   originTop=False)

    page = doc[1]  # Get the first/single page of the document.
    page.name = 'Cover'

    # Get the current view of the document. This allows setting of
    # parameters how the document is represented on output.
    view = doc.view
    view.w, view.h = W, H
    # Set view options. Full list is in elements/views/baseviews.py
    view.padding = 40  # Showing cropmarks and registration marks
    # need >= 20 padding of the view.
    view.showPageRegistrationMarks = True
    view.showPageCropMarks = True
    view.showPageFrame = True
    view.showPagePadding = False
    view.showPageNameInfo = True
    view.showTextOverflowMarker = False

    context = view.context

    C1 = Color(r=random() * 0.2, g=random() * 0.2, b=random() * 0.9)

    # Make background element, filling the page color and bleed.
    colorRect1 = newRect(z=-10,
                         name='Page area',
                         parent=page,
                         conditions=[
                             Top2TopSide(),
                             Left2LeftSide(),
                             Fit2RightSide(),
                             Fit2BottomSide()
                         ],
                         fill=C1)
    colorRect1.bleed = BLEED
    colorRect1.solve()  # Solve element position, before we can make
    # other elements depend on position and size.

    M = BLEED + 64
    newRect(
        z=-10,
        name='Frame 2',
        parent=colorRect1,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.5),  # Default parameter:
        # 50% between background color and white
        stroke=None,
        w=colorRect1.w - M,
        h=colorRect1.h - M,
        xAlign=CENTER,
        yAlign=MIDDLE)

    # Make random blurb name and titles
    title = blurb.getBlurb('book_phylosophy_title')
    subTitle = blurb.getBlurb('book_pseudoscientific').capitalize()
    if random() < 0.2:  # 1/5 chance to add editions text
        subTitle += '\nEdition ' + blurb.getBlurb('edition')
    authorName = blurb.getBlurb('name', noTags=True)
    if random() < 0.33:  # 1/3 chance for a second author name
        authorName += '\n' + blurb.getBlurb('name')

    page.pt = 100  # Now the rectangles positioned automatic, alter the paddings
    page.pl = page.pr = 80
    page.pb = 20
    # Add some title (same width, different height) at the "wrongOrigin" position.
    # They will be repositioned by solving the colorConditions.
    title = context.newString(title + '\n\n',
                              style=dict(font=fontBold.path,
                                         fontSize=40,
                                         rLeading=1.2,
                                         xTextAlign=CENTER,
                                         textFill=1))
    title += context.newString(subTitle + '\n\n',
                               style=dict(font=fontRegular.path,
                                          fontSize=32,
                                          xTextAlign=CENTER,
                                          textFill=(1, 1, 1, 0.5)))
    title += context.newString(authorName,
                               style=dict(font=fontItalic.path,
                                          fontSize=24,
                                          rTracking=0.025,
                                          xTextAlign=CENTER,
                                          textFill=(1, 0.5, 1, 0.7)))
    newTextBox(title,
               parent=page,
               name='Other element',
               conditions=[Fit2Width(),
                           Center2Center(),
                           Top2Top()],
               xAlign=CENTER,
               yAlign=TOP)

    typoIllustration = context.newString('&',
                                         style=dict(font=ampersandFont.path,
                                                    fontSize=300,
                                                    xTextAlign=CENTER,
                                                    textFill=(1, 0.5, 1, 0.7)))
    newTextBox(typoIllustration,
               parent=page,
               conditions=[Fit2Width(),
                           Center2Center(),
                           Bottom2Bottom()],
               xAlign=CENTER,
               yAlign=TOP)

    # Evaluate again, result should now be >= 0
    score = page.evaluate()
    if score.fails:  # There is new "failing" elements. Solve their layout.
        page.solve()

    return doc
示例#2
0
def makeDocument():
    """Demo random book cover generator."""

    # Create new document with (w,h) and fixed amount of pages.
    # Make number of pages with default document size.
    # Initially make all pages default with template
    # One page, just the cover.
    doc = Document(w=W,
                   h=H,
                   title='A Demo Book Cover',
                   autoPages=1,
                   originTop=False)

    page = doc[1]  # Get the first/single page of the document.
    page.name = 'Cover'

    # Get the current view of the document. This allows setting of
    # parameters how the document is represented on output.
    view = doc.view
    view.w, view.h = W, H
    # Set view options. Full list is in elements/views/baseviews.py
    view.padding = 40  # Showing cropmarks and registration marks
    # need >= 20 padding of the view.
    view.showRegistrationMarks = True
    view.showCropMarks = True
    view.showFrame = False
    view.showPadding = False
    view.showNameInfo = True
    view.showTextOverflowMarker = False

    context = view.context

    C1 = Color(r=0.2 + random() * 0.8,
               g=random() * 0.2,
               b=0.4 + random() * 0.2)

    # Make background element, filling the page color and bleed.
    colorRect1 = newRect(z=-10,
                         name='Page area',
                         parent=page,
                         pt=40,
                         conditions=[
                             Top2TopSide(),
                             Left2LeftSide(),
                             Fit2RightSide(),
                             Fit2BottomSide()
                         ],
                         fill=C1)
    colorRect1.bleed = BLEED
    colorRect1.solve()  # Solve element position, before we can make
    # other elements depend on position and size.

    M = 64
    colorRect2 = newRect(
        z=-10,
        name='Frame 2',
        parent=colorRect1,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.5),  # Default parameter:
        # 50% between background color and white
        stroke=noColor,
        w=colorRect1.w - M - BLEED,
        h=colorRect1.h - M - BLEED,
        xAlign=CENTER,
        yAlign=MIDDLE)

    colorRect3 = newRect(
        z=-10,
        name='Frame 3',
        parent=colorRect2,
        conditions=[Center2Center(), Middle2Middle()],
        fill=C1.darker(0.3),  # Default parameter:
        # 50% between background color and white
        stroke=noColor,
        w=colorRect1.w - 2 * M,
        h=colorRect1.h - 2 * M,
        xAlign=CENTER,
        yAlign=MIDDLE)

    # Make random blurb name and titles
    title = blurb.getBlurb('book_phylosophy_title')
    subTitle = blurb.getBlurb('book_pseudoscientific').capitalize()
    if random() < 0.2:  # 1/5 chance to add editions text
        subTitle += '\nEdition ' + blurb.getBlurb('edition')
    authorName = blurb.getBlurb('name', noTags=True)
    if random() < 0.33:  # 1/3 chance for a second author name
        authorName += '\n' + blurb.getBlurb('name')

    # Add some title (same width, different height) at the "wrongOrigin" position.
    # They will be repositioned by solving the colorConditions.
    titleS = context.newString('')
    for word in title.split(' '):
        titleS += context.newString(' ' + word,
                                    style=dict(font=fontRegular.path,
                                               fontSize=50,
                                               w=page.pw,
                                               leading=em(1.2),
                                               xTextAlign=CENTER,
                                               textFill=whiteColor))
    #title += context.newString(subTitle + '\n\n', style=dict(font=fontRegular.path, fontSize=32, xTextAlign=CENTER, textFill=(1, 1, 1,0.5)))
    #title += context.newString(authorName, style=dict(font=fontItalic.path, fontSize=24, tracking=em(0.025), xTextAlign=CENTER, textFill=(1, 0.5, 1,0.7)))
    newTextBox(titleS,
               parent=colorRect2,
               name='Title',
               conditions=[Fit2Width(),
                           Center2Center(),
                           Top2Top()],
               xAlign=CENTER,
               yAlign=TOP)

    score = page.evaluate()
    if score.fails:
        page.solve()

    # Evaluate again, result should now be >= 0
    return doc