def makeDocument():
    """Demo page composer."""
    tt = time()  # Keep track of time, in case SHOW_TIMER is True

    # Set some values of the default template
    # (as already generated by the document).
    # Make squential unique names for the flow boxes inside the templates
    flowId0 = MAIN_FLOW + '0'
    flowId1 = MAIN_FLOW + '1'
    flowId2 = MAIN_FLOW + '2'

    # Template 1
    template1 = Template(rs)  # Create template of main size. Front page only.
    # Show grid columns and margins if rootStyle.showGrid
    # or rootStyle.showGridColumns are True
    template1.grid(rs)
    # Show baseline grid if rs.showBaselines is True
    template1.baselineGrid(rs)
    # Create empty image place holders.
    # To be filled by running content on the page.
    template1.cContainer(4, 0, 2, 4, rs)  # Empty image element, cx, cy, cw, ch
    template1.cContainer(0, 5, 2, 3, rs)
    # Create linked text boxes.
    # Note the "nextPageName" to keep on the same page or to next.
    template1.cTextBox('',
                       0,
                       0,
                       2,
                       5,
                       style=rs,
                       prevBox=flowId0,
                       nextBox=flowId1,
                       nextPageName=0,
                       fill=BOX_COLOR)
    template1.cTextBox('',
                       2,
                       0,
                       2,
                       8,
                       style=rs,
                       prevBox=flowId1,
                       nextBox=flowId2,
                       nextPageName=0,
                       fill=BOX_COLOR)
    template1.cTextBox('',
                       4,
                       4,
                       2,
                       4,
                       style=rs,
                       prevBox=flowId2,
                       nextBox=flowId0,
                       nextPageName=1,
                       fill=BOX_COLOR)
    # Create page number box.
    # Pattern pageNumberMarker is replaced by actual page number.
    template1.cText(rs['pageNumberMarker'],
                    6,
                    0,
                    style=rs,
                    font=BOOK,
                    fontSize=12,
                    fill=BOX_COLOR)

    # Template 2
    template2 = Template(rs)  # Create second template.
    # This is for the main pages.

    # Show grid columns and margins if rootStyle.showGrid
    # or rootStyle.showGridColumns are True
    template2.grid(rs)
    # Show baseline grid if rs.showBaselines is True
    template2.baselineGrid(rs)
    template2.cContainer(4, 0, 2, 3, style=rs)  # Empty image element,
    # cx, cy, cw, ch
    template2.cContainer(0, 5, 2, 3, style=rs)
    template2.cContainer(2, 2, 2, 2, style=rs)
    template2.cContainer(2, 0, 2, 2, style=rs)
    template2.cContainer(4, 6, 2, 2, style=rs)
    template2.cTextBox('',
                       0,
                       0,
                       2,
                       5,
                       style=rs,
                       prevBox=flowId0,
                       nextBox=flowId1,
                       nextPageName=0,
                       fill=BOX_COLOR)
    template2.cTextBox('',
                       2,
                       4,
                       2,
                       4,
                       style=rs,
                       prevBox=flowId1,
                       nextBox=flowId2,
                       nextPageName=0,
                       fill=BOX_COLOR)
    template2.cTextBox('',
                       4,
                       3,
                       2,
                       3,
                       style=rs,
                       prevBox=flowId2,
                       nextBox=flowId0,
                       nextPageName=1,
                       fill=BOX_COLOR)
    # Create page number box.
    # Pattern pageNumberMarker is replaced by actual page number.
    template2.cText(rs['pageNumberMarker'],
                    6,
                    0,
                    style=rs,
                    font=BOOK,
                    fontSize=12,
                    fill=BOX_COLOR)

    # 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 template2
    doc = Document(rs, autoPages=2, template=template2)

    # Cache some values from the root style that we need
    # multiple time to create the tag styles.
    fontSize = rs['fontSize']
    leading = rs['leading']
    listIndent = rs['listIndent']
    language = rs['language']

    # Add styles for whole document and text flows.
    # Note that some values are defined here for clarity,
    # even if their default root values are the same.
    doc.newStyle(name='chapter', font=BOOK)
    doc.newStyle(name='title', fontSize=3 * fontSize, font=BOLD)
    doc.newStyle(name='subtitle', fontSize=2 * fontSize, font=BOOK_ITALIC)
    doc.newStyle(name='author',
                 fontSize=2 * fontSize,
                 font=BOOK,
                 fill=color(1, 0, 0))
    doc.newStyle(name='h1',
                 fontSize=fontSize,
                 font=SEMIBOLD,
                 fill=color(1, 0, 0),
                 leading=2 * fontSize,
                 tracking=H1_TRACK,
                 postfix='\n')
    doc.newStyle(name='h2',
                 fontSize=fontSize,
                 font=SEMIBOLD,
                 fill=color(0, 0.5, 1),
                 leading=1 * fontSize,
                 tracking=H2_TRACK,
                 postfix='\n')
    doc.newStyle(name='h3',
                 fontSize=fontSize,
                 font=MEDIUM,
                 fill=blackColor,
                 leading=1 * fontSize,
                 needsBelow=2 * leading,
                 tracking=H3_TRACK,
                 postfix='\n')

    # Spaced paragraphs.
    doc.newStyle(name='p',
                 fontSize=fontSize,
                 font=BOOK,
                 fill=color(0.1),
                 prefix='',
                 postfix='\n',
                 tracking=P_TRACK,
                 leading=14,
                 align=LEFT,
                 hyphenation=True)
    doc.newStyle(name='b', font=SEMIBOLD)
    doc.newStyle(name='em', font=BOOK_ITALIC)
    doc.newStyle(name='hr', stroke=color(1, 0, 0), strokeWidth=4)
    doc.newStyle(name='br', postfix='\n')  # Simplest way to make
    # <br/> be newline
    doc.newStyle(
        name='img',
        leading=leading,
        fontSize=fontSize,
        font=BOOK,
    )

    # Footnote reference index.
    doc.newStyle(name='sup',
                 font=MEDIUM,
                 rBaselineShift=0.6,
                 fontSize=0.65 * fontSize)
    doc.newStyle(
        name='li',
        fontSize=fontSize,
        font=BOOK,
        tracking=P_TRACK,
        leading=leading,
        hyphenation=True,
        # Lists need to copy the listIndex over to the regalar style value.
        tabs=[(listIndent, LEFT)],
        indent=listIndent,
        firstLineIndent=1,
        postfix='\n')
    doc.newStyle(name='ul')
    doc.newStyle(name='literatureref',
                 fill=color(0.5),
                 rBaselineShift=0.2,
                 fontSize=0.8 * fontSize)
    doc.newStyle(name='footnote',
                 fill=color(1, 0, 0),
                 fontSize=0.8 * U,
                 font=BOOK)
    doc.newStyle(name='caption',
                 tracking=P_TRACK,
                 language=language,
                 fill=color(0.2),
                 leading=leading * 0.8,
                 fontSize=0.8 * fontSize,
                 font=BOOK_ITALIC,
                 indent=U / 2,
                 tailIndent=-U / 2,
                 hyphenation=True)

    if SHOW_TIMER:
        print('Time styles %0.3f' % (time() - tt))
        tt = time()

    # Change template of page 1
    page1 = doc[1]
    page1.setTemplate(template1)

    ttt = ''
    for n in range(100):
        ttt += 'abcdefg%d\n' % n
    ttt = doc.context.newString(ttt, rs)
    ttt = page0.textBox(ttt,
                        point=(rs.get('pl'), rs.get('pt')),
                        w=11 * 14,
                        h=50 * 14,
                        fill=color(0.8, 0.8, 0.8, 0.5))
    page0.textBox(ttt,
                  point=(rs.get('pl') + 11 * 14 + 14, rs.get('pt')),
                  w=11 * 14,
                  h=50 * 14,
                  fill=color(0.8, 0.8, 0.8, 0.5))

    if SHOW_TIMER:
        print('Time template %0.3f' % (time() - tt))
        tt = time()

    # Create main Galley for this page, for pasting the sequence of elements.
    g = Galley()
    t = Typesetter(doc, g)
    t.typesetFile(MD_PATH)

    if SHOW_TIMER:
        print('Time typesetter %0.3f' % (time() - tt))
        tt = time()

    # Fill the main flow of text boxes with the ML-->XHTML formatted text.
    c = Composer(doc)
    c.compose(g, doc[1], flowId0)

    if SHOW_TIMER:
        print('Time compose %0.3f' % (time() - tt))

    return doc
Пример #2
0
def makeDocument(rs):
    u"""Demo page composer."""

    # Set some values of the default template (as already generated by the document).
    # Make squential unique names for the flow boxes inside the templates
    flowId1 = MAIN_FLOW+'1' 
    flowId2 = MAIN_FLOW+'2'
    flowId3 = MAIN_FLOW+'3'
        
    # Template 1
    template1 = Template(rs) # Create template of main size. Front page only.
    # Show grid columns and margins if rootStyle.showGrid or rootStyle.showGridColumns are True
    template1.grid(rs) 
    # Show baseline grid if rs.showBaselineGrid is True
    template1.baselineGrid(rs)
    # Create empty image place holders. To be filled by running content on the page.
    template1.cContainer(4, 0, 2, 4, rs)  # Empty image element, cx, cy, cw, ch
    template1.cContainer(0, 5, 2, 3, rs)
    # Create linked text boxes. Note the "nextPage" to keep on the same page or to next.
    template1.cTextBox(FS, 0, 0, 2, 5, rs, flowId1, nextBox=flowId2, nextPage=0, fill=BOX_COLOR)
    template1.cTextBox(FS, 2, 0, 2, 8, rs, flowId2, nextBox=flowId3, nextPage=0, fill=BOX_COLOR)
    template1.cTextBox(FS, 4, 4, 2, 4, rs, flowId3, nextBox=flowId1, nextPage=1, fill=BOX_COLOR)
    # Create page number box. Pattern pageNumberMarker is replaced by actual page number.
    template1.cText(FS+rs['pageIdMarker'], 6, 0, style=rs, font=BOOK, fontSize=12, fill=BOX_COLOR)

    # Template 2
    template2 = Template(rs) # Create second template. This is for the main pages.
    # Show grid columns and margins if rootStyle.showGrid or rootStyle.showGridColumns are True
    template2.grid(rs) 
    # Show baseline grid if rs.showBaselineGrid is True
    template2.baselineGrid(rs)
    template2.cContainer(4, 0, 2, 3, rs)  # Empty image element, cx, cy, cw, ch
    template2.cContainer(0, 5, 2, 3, rs)
    template2.cContainer(2, 2, 2, 2, rs)
    template2.cContainer(2, 0, 2, 2, rs)
    template2.cContainer(4, 6, 2, 2, rs)
    template2.cTextBox(FS, 0, 0, 2, 5, rs, flowId1, nextBox=flowId2, nextPage=0, fill=BOX_COLOR)
    template2.cTextBox(FS, 2, 4, 2, 4, rs, flowId2, nextBox=flowId3, nextPage=0, fill=BOX_COLOR)
    template2.cTextBox(FS, 4, 3, 2, 3, rs, flowId3, nextBox=flowId1, nextPage=1, fill=BOX_COLOR)
    # Create page number box. Pattern pageNumberMarker is replaced by actual page number.
    template2.cText(FS+rs['pageIdMarker'], 6, 0, style=rs, font=BOOK, fontSize=12, fill=BOX_COLOR)
   
    # 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 template2
    doc = Document(rs, autoPages=3, template=template2) 
 
    # Cache some values from the root style that we need multiple time to create the tag styles.
    fontSize = rs['fontSize']
    leading = rs['leading']
    rLeading = rs['rLeading']
    listIndent = rs['listIndent']
    language = rs['language']

    # Add styles for whole document and text flows.  
    # Note that some values are defined here for clarity, even if their default root values
    # are the same.             
    doc.newStyle(name='chapter', font=BOOK)    
    doc.newStyle(name='title', fontSize=3*fontSize, font=BOLD)
    doc.newStyle(name='subtitle', fontSize=2.6*fontSize, font=BOOK_ITALIC)
    doc.newStyle(name='author', fontSize=2*fontSize, font=BOOK, fill=(1, 0, 0))
    doc.newStyle(name='h1', fontSize=2*fontSize, font=BLACK, fill=(1, 0, 0), 
        leading=2.5*leading, tracking=H1_TRACK, postfix='\n')
    doc.newStyle(name='h2', fontSize=5*fontSize, font=LIGHT_CONDENSED, 
        fill=0, leading=1*leading, rLeading=0, tracking=H2_TRACK, 
        prefix='', postfix='\n')
    doc.newStyle(name='h3', fontSize=1.1*fontSize, font=MEDIUM, fill=0, 
        leading=leading, rLeading=0, rNeedsBelow=2*rLeading, tracking=H3_TRACK,
        prefix='', postfix='\n')
    doc.newStyle(name='h4', fontSize=1.1*fontSize, font=BOOK, fill=0, 
        leading=leading, rLeading=0, rNeedsBelow=2*rLeading, tracking=H3_TRACK,
        paragraphTopSpacing=U, paragraphBottomSpacing=U, prefix='', postfix='\n')
    
    # Spaced paragraphs.
    doc.newStyle(name='p', fontSize=fontSize, font=BOOK, fill=0.1, prefix='', postfix='\n',
        rTracking=P_TRACK, leading=14, rLeading=0, align=LEFT_ALIGN, hyphenation=True)
    doc.newStyle(name='b', font=SEMIBOLD)
    doc.newStyle(name='em', font=BOOK_ITALIC)
    doc.newStyle(name='hr', stroke=(1, 0, 0), strokeWidth=4)
    doc.newStyle(name='br', postfix='\n') # Simplest way to make <br/> show newline
    doc.newStyle(name='a', prefix='', postfix='')
    doc.newStyle(name='img', leading=leading, fontSize=fontSize, font=BOOK,
        stroke=1, fill=None)
    
    # Footnote reference index.
    doc.newStyle(name='sup', font=MEDIUM, rBaselineShift=0.6, prefix='', postfix=' ',
        fontSize=0.6*fontSize)
    doc.newStyle(name='li', fontSize=fontSize, font=BOOK, 
        tracking=P_TRACK, leading=leading, hyphenation=True, 
        # Lists need to copy the listIndex over to the regalar style value.
        tabs=[(listIndent, LEFT_ALIGN)], indent=listIndent, 
        firstLineIndent=1, postfix='\n')
    doc.newStyle(name='ul', prefix='', postfix='')
    doc.newStyle(name='literatureref', fill=0.5, rBaselineShift=0.2, fontSize=0.8*fontSize)
    doc.newStyle(name='footnote', fill=(1, 0, 0), fontSize=0.8*U, font=BOOK)
    doc.newStyle(name='caption', tracking=P_TRACK, language=language, fill=0.2, 
        leading=leading*0.8, fontSize=0.8*fontSize, font=BOOK_ITALIC, 
        indent=U/2, tailIndent=-U/2, hyphenation=True)
    
    # Change template of page 1
    page1 = doc[1]
    page1.setTemplate(template1)
    
    # Create main Galley for this page, for pasting the sequence of elements.    
    galley = Galley() 
    t = Typesetter(doc, galley)
    t.typesetFile(MD_PATH)
    
    # Fill the main flow of text boxes with the ML-->XHTML formatted text. 
    c = Composer(doc)
    c.compose(galley, page1, flowId1)
    
    return doc