def run(): c = newRect(name='myContainerElement') print('Container we made:' + str(c)) print('No elements yet:' + str(c.elements)) # Currently no elements in the container child1 = newRect(parent=c, name='Child1') # Make child containers with unique Id child2 = newRect(parent=c, name='Child2') # Get unique element eIds eId1 = child1.eId eId2 = child2.eId print('-- Now the container got 2 named child containers.') print('Elements:' + str(c.elements)) # Currently no elements in the container print('-- None of the children are placed on default position (0, 0, 0)') for e in c.elements: print(e.name, e.x, e.y, e.z) print( '-- Place the Child1 element on a fixed position (x,y), z is undefined/untouched' ) child1.x = 20 child1.y = 30 child1.z = 100 print(child1) print( '-- Place the same Child2 element on another fixed position (x,y,z), a point tuple.' ) child2.point = (120, 30, 20) print(child2) print( '-- The container behaves as a dictionary of child elements with e.eId as key.' ) print(c[eId1]) print(c[eId2])
def buildSpecimenPages(doc, fontNames): for index, fontName in enumerate(sorted(fontNames)): font = getFontByName(fontName) page = doc[index] pageTitle = font.info.familyName + ' ' + font.info.styleName # Add filling rectangle for background color of the old paper book. newRect(z=-1, parent=page, conditions=[Bleed2Sides()], fill=PAPER_COLOR) # Centered title: family name and style name of the current font. titleBs = context.newString(pageTitle, style=dict(font=fontName, fontSize=24, textFill=0)) newText(titleBs, x=50, y=100, parent=page, w=400)
def makeDocument(): # Create new document with (w,h) size and fixed amount of pages. # Note that most of the rootStyle is cascading through the e.css('name') call, # except that values of x, y, z, w, h, d # Just to show here how to get the root style. If not altered, it can be omitted. # as Document( ) will create a RootStyle by default. rootStyle = getRootStyle() doc = Document(rootStyle, originTop=False, w=W, h=H, autoPages=1) page = doc[1] # Get the first/single page of the document. page.padding = 40 # TODO: order if 4 values? # Make rect as page element centered with centered origin. if RedRect: c = Color(1, 0, 0) else: c = Color(0.5) conditions = (Center2Center(), Middle2Middle()) newRect(fill=c, parent=page, w=RectSize, h=RectSize, conditions=conditions, xAlign=CENTER, yAlign=MIDDLE) # Solve the layout conditions of the red rectangle. # Show if one of the conditions failed to solve. score = page.solve() if score.fails: print('Failed conditions', score.fails) # Set the view parameters for the required output. view = doc.getView() view.w = view.h = W, H view.padding = 40 # Make view padding to show crop marks and frame view.showFrame = True # Show frame of the page in blue view.showPadding = False view.showCropMarks = True # Show crop marks view.showOrigin = ShowOrigins # Show origin alignment markers on each element. view.showDimensions = ShowOrigins view.showElementInfo = ShowElementInfo # Show baxes with element info element. return doc
def makeDocument(font): u"""Create the main document in the defined size with a couple of automatic empty pages.""" # Build 4 pages, two for the original scan, the two for the generated version. doc = Document(w=W, h=H, title='Variable Font Sample Page', originTop=False, context=context, gridX=GRID_X, fontSize=24) # Get default view from the document and set the viewing parameters. view = doc.view view.padding = 0 # For showing cropmarks and such, make > mm(20) or inch(1). view.showCropMarks = True # Won't show if there is not padding in the view. view.showFrame = SHOW_FRAMES # No frame in case PAPER_COLOR exists to be shown. view.showPadding = SHOW_FRAMES # No frame in case PAPER_COLOR exists to be shown. view.showRegistrationMarks = True view.showOrigin = True # Show position of xAlign and yAlign view.showBaselines = set([BASE_LINE, BASE_INDEX_LEFT]) view.showGrid = DEFAULT_GRID # Show GRID_X lines view.showInfo = True # Show file name and date of the document view.showTextOverflowMarker = False # Don't show marker in case Filibuster blurb is too long. page = doc[1] # During development, draw the template scan as background # Set z-azis != 0, to make floating elements not get stuck at the background page.padding = {True: PADDING_LEFT, False: PADDING_RIGHT}[page.isLeft] page.bleed = pt(6) if SHOW_TEMPLATE: newImage(BERTHOLD_PATH, index=1, parent=page, conditions=[Fit2Sides()]) else: newRect(parent=page, fill=PAPER_COLOR, yAlign=BOTTOM, conditions=[Fit2Bleed()]) makePage1(page, sampleFont) print(doc.solve()) return doc
def buildSample(self, doc): page = doc[1] theme = self.getTheme() if doc.view.showFrame: c = theme.mood.body_bgcolor.lessOpaque() newRect(parent=page, fill=c, conditions=[Fit2Sides()]) # By default, the typesetter produces a single Galley with content and code blocks. t = Typesetter(doc.context) t.typesetFile(MD_SAMPLE_PATH) # Create a Composer for this document, then create pages and fill content. composer = Composer(doc) # The composer executes the embedded Python code blocks that indicate where content should go. # by the HtmlContext. Feedback by the code blocks is added to verbose and errors list targets = dict(pub=self, doc=doc, page=page) composer.compose(t.galley, targets=targets) """ if doc.view.showGrid: c = theme.mood.body_color.lessOpaque() for n in range(len(doc.gridX)): colWidth = doc.gridX[n][0] if n: conditions = [Left2Col(n), Fit2Height()] else: conditions = [Left2Left(), Fit2Height()] newRect(parent=page, fill=c, w=colWidth, conditions=conditions) """ """ theme = self.getTheme() newTextBox(str(theme.mood.name), style=headStyle, parent=grp, conditions=[Fit2Width(), Top2Top()]) for colorName in sorted(theme.mood.palette.colorNames): color = theme.mood.palette[colorName] newRect(parent=grp, w=32, h=32, fill=color, conditions=[Right2Right(), Float2Top(), Float2Left()]) """ page.solve()
def makeDocument(): # Creates the publication/document that holds the pages. doc = Document(w=W, h=H, originTop=False, autoPages=1) doc.view.padding = 0 # Don't show cropmarks in this example. doc.view.showPadding = True # Gets page by pageNumber, first in row (at this point there is only one in # this row). page = doc[1] page.padding = 30 conditions = [Right2Right(), Float2Top(), Float2Left()] # TODO: Solve this bug, does not mirror. #conditions = [Left2Left(), Float2Top(), Float2Right()] for n in range(32): newRect(w=40, h=42, mr=4, mt=4, parent=page, fill=color(random() * 0.5 + 0.5, 0, 0.5), conditions=conditions) # Recursively solve the conditions in all pages. # If there are failing conditions, then the status # is returned in the Score instance. score = doc.solve() if score.fails: print(score.fails) doc.export(EXPORT_PATH_SVG) doc.export(EXPORT_PATH_JPG) doc.export(EXPORT_PATH_PNG) doc.export(EXPORT_PATH_PDF) print('Done making document %s' % doc)
def makePage2(page, font): border = dict(stroke=blackColor, strokeWidth=pt(0.5)) r = newRect(parent=page, conditions=[Fit()]) fullName = '%s %s' % (font.info.familyName, font.info.styleName) style = dict(font=font, fontSize=pt(24), xTextAlign=CENTER) bs = context.newString(fullName.upper(), style=style) newTextBox(bs, parent=r, borderTop=border, borderBottom=border, margin=0, padding=0, conditions=[Left2Left(), Top2Top(), Fit2Width()])
# # P A G E B O T # # Copyright (c) 2016+ Buro Petr van Blokland + Claudia Mens # www.pagebot.io # Licensed under MIT conditions # # ----------------------------------------------------------------------------- # from pagebot.document import Document from pagebot.elements import newRect from pagebot.conditions import Center2Center, Middle2Middle from pagebot.toolbox.units import pt from pagebot.toolbox.color import color W, H = pt(300, 200) # Get size units # Create document with default 1 page. doc = Document(w=W, h=H, originTop=False) # First page in the list is uneven (right side) page = doc[1] # Create a new rectangle element with position conditions newRect(parent=page, fill=color('red'), size=pt(240, 140), # Show measure lines on the element. showDimensions=True, conditions=[Center2Center(), Middle2Middle()]) # Make the page apply all conditions. page.solve() # Export the document page as png, so it shows as web image. doc.export('_export/RedSquare.png')
def makeBanner(bd): u"""Demo random book cover generator.""" w, h = bd['w'], bd['h'] imagePaths = bd['imagePaths'] labelSize = bd['labelSize'] title1 = bd['title1'] title2 = bd['title2'] titleSize = bd['titleSize'] context = getContext() context.newDrawing() # 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 doc = Document(w=w, h=h, title=title1, autoPages=FRAMES, context=context, originTop=False) # One page, just the cover. A = (1, 1, 1, 0.5) B = (1, 0, 1, 0.8) C = (1, 1, 1, 0.8) COLORS = ( (B, B, B), (A, B, B), (B, A, B), (B, B, A), (A, B, B), (B, B, A), (B, A, B), ) for pn in range(1, FRAMES + 1): page = doc[pn] # Get the first/single page of the document. page.frameDuration = 1.5 imagePath, imageConditions = imagePaths[pn - 1] # Background image of the slide newImage(PATH + imagePath, conditions=imageConditions, parent=page) ww75 = w * 0.75 newRect(fill=(0.05, 0.05, 0.4, 0.8), w=ww75, conditions=(Fit2HeightSides(), Right2RightSide()), parent=page) ww25 = w * 0.25 #bs = context.newString('Type@Cooper\nTypographics', style=dict(font=fontRegular.path, fontSize=w/28, xTextAlign=CENTER, textFill=1, rLeading=1.05)) #tw, th = bs.textSize() #newTextBox(bs, parent=page, h=th+P/2, w=ww25, padding=(P/4,P/3,0,P/3), fill=(1,0,0,0.8), conditions=(Left2LeftSide(), Top2TopSide())) bs = context.newString('Typographics', style=dict(font=fontRegular.path, fontSize=w / 24, xTextAlign=CENTER, textFill=1, rLeading=1.05)) tw, th = bs.textSize() newTextBox(bs, parent=page, h=th + P / 2, w=ww25, padding=(P / 4, P / 3, 0, P / 3), fill=(1, 0, 0, 0.8), conditions=(Left2LeftSide(), Top2TopSide())) bs = context.newString(title1 + '\n', style=dict(font=fontMedium.path, textFill=C, fontSize=w / 11, rTracking=0.015, rLeading=1.4)) bs += context.newString(title2 + '\n', style=dict(font=fontBook.path, textFill=C, fontSize=w / 12, rLeading=1)) bs += context.newString('SKETCHING', style=dict(font=fontRegular.path, textFill=COLORS[pn - 1][0], fontSize=w / 15, rTracking=0.648, rLeading=1.5)) bs += context.newString('\nCODING', style=dict(font=fontRegular.path, textFill=COLORS[pn - 1][1], fontSize=w / 16, rTracking=0.185, rLeading=1.05)) bs += context.newString(' & ', style=dict(font=fontRegular.path, textFill=A, fontSize=w / 16, rTracking=0.185, rLeading=1.05)) bs += context.newString('MAKING\n', style=dict(font=fontRegular.path, textFill=COLORS[pn - 1][2], fontSize=w / 16, rTracking=0.185, rLeading=1.05)) bs += context.newString('Petr van Blokland April 11-13', style=dict(font=fontRegular.path, textFill=C, fontSize=w / 17.5, rTracking=0.032, rLeading=1.6)) newTextBox(bs, parent=page, x=ww25, y=0, padding=(0, 0, 0, w / 30), w=ww75 + w / 100, conditions=[Fit2HeightSides()]) score = page.evaluate() if score.fails: page.solve() # Evaluate again, result should now be >= 0 return doc
def makeBanner(bd): w, h = bd['w'], bd['h'] imagePaths = bd['imagePaths'] labelSize = bd['labelSize'] title1 = bd['title1'] title2 = bd['title2'] title3 = bd['title3'] sideImage = bd['sideImage'] titleSize = bd['titleSize'] L1 = bd['l1'] L2 = bd['l2'] padding = bd['padding'] type = bd['type'] context = getContext() context.newDrawing() # 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 doc = Document(w=w, h=h, title=title1, autoPages=FRAMES, context=context, originTop=False) # One page, just the cover. for pn in range(1, FRAMES + 1): page = doc[pn] # Get the first/single page of the document. page.frameDuration = 0.75 imagePath, imageConditions = imagePaths[0] # Background image of the slide im = newImage(PATH + imagePath, y=h / FRAMES * (pn - 1), conditions=imageConditions, parent=page) newImage(PATH + imagePath, y=h / FRAMES * (pn - 1) - 2 * im.h, conditions=imageConditions, parent=page) newImage(PATH + imagePath, y=h / FRAMES * (pn - 1) - im.h, conditions=imageConditions, parent=page) newImage(PATH + imagePath, y=h / FRAMES * (pn - 1) + im.h, conditions=imageConditions, parent=page) newImage(PATH + imagePath, y=h / FRAMES * (pn - 1) + 2 * im.h, conditions=imageConditions, parent=page) ww75 = w * 0.75 ww25 = w * 0.25 bs = context.newString('Type@Cooper\nTypographics', style=dict(font=fontRegular.path, fontSize=w / 30, xTextAlign=CENTER, textFill=1, rLeading=1.05, rTracking=0.02)) tw, th = bs.textSize() newRect(fill=(0.15, 0.17, 0.15, 0.7), w=ww75, conditions=(Fit2HeightSides(), Right2RightSide()), parent=page) newTextBox(bs, parent=page, h=th + P, w=ww25, padding=(P / 2, P, 0, P), fill=(1, 0, 0), conditions=(Left2LeftSide(), Top2TopSide())) # Show design measures m = padding * 2 m2 = m / 2 m4 = m / 4 designProcess = newGroup(x=0, y=0, fill=(0.3, 0.32, 0.3, 0.7), w=ww25, h=h - th - P, padding=padding, parent=page) design = newImage(PATH + sideImage, y=padding, h=h - th - P - 2 * padding, fill=None, conditions=[Center2Center()], parent=designProcess) designProcess.solve() if pn > 1: # Horizontal frame newLine(x=design.x - m4, y=design.y, w=design.w + m2, h=0, fill=None, stroke=1, strokeWidth=L1, parent=designProcess) newLine(x=design.x - m4, y=design.y + design.h, w=design.w + m2, h=0, fill=None, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 2: # Vertical frame newLine(x=design.x, y=design.y - m4, w=0, h=design.h + m2, fill=None, stroke=1, strokeWidth=L1, parent=designProcess) newLine(x=design.x + design.w, y=design.y - m4, w=0, h=design.h + m2, fill=None, stroke=1, strokeWidth=L1, parent=designProcess) if pn == 4: # Diagonal newLine(x=design.x, y=design.y, w=design.w, h=design.h, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) newLine(x=design.x + design.w, y=design.y, w=-design.w, h=design.h, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) if pn > 5: # V-lines newLine(x=design.x + design.w / 2, y=design.y - m4, w=0, h=design.h + m2, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 6: # V-lines newLine(x=design.x + design.w * 0.35, y=design.y - m4, w=0, h=design.h + m2, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 7: # V-lines newLine(x=design.x + design.w * 0.92, y=design.y - m4, w=0, h=design.h + m2, stroke=1, strokeWidth=L1, parent=designProcess) if pn == 9: # H-lines 1/2 newLine(x=design.x - m2, y=design.y + design.h * 0.5, w=design.w + 2 * m2, h=0, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 9: # H=lines newLine(x=design.x - m2, y=design.y + design.h * 0.53, w=design.w + 2 * m2, h=0, stroke=1, strokeWidth=L1, parent=designProcess) newLine(x=design.x - m2, y=design.y + design.h * 0.59, w=design.w + 2 * m2, h=0, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 10: # H=lines newLine(x=design.x - m2, y=design.y + design.h * 0.24, w=design.w + 2 * m2, h=0, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 11: # H=lines newLine(x=design.x - m2, y=design.y + design.h * 0.655, w=design.w + 2 * m2, h=0, stroke=1, strokeWidth=L1, parent=designProcess) if pn > 12: # Diagonal newLine(x=design.x, y=design.y, w=design.w, h=design.h * 0.53, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) newLine(x=design.x + design.w, y=design.y, w=-design.w, h=design.h * 0.53, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) if pn > 13: # Diagonal newLine(x=design.x, y=design.y + design.h * 0.59, w=design.w, h=design.h - design.h * 0.59, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) newLine(x=design.x + design.w, y=design.y + +design.h * 0.59, w=-design.w, h=design.h - design.h * 0.59, fill=None, stroke=(1, 0, 0), strokeWidth=L2, parent=designProcess) C0 = 1 C1 = C2 = (0, 1, 0.1, 0.95) if pn > 10: C2 = C0 if type == 'Small': bs = context.newString(title1 + '\n', style=dict(font=fontMedium.path, textFill=C0, fontSize=w / 20, rTracking=0.015, rLeading=1.25)) bs += context.newString(title2 + '\n', style=dict(font=fontBook.path, textFill=C1, fontSize=w / 20, rLeading=1.2, rTracking=0.03)) if title3 is not None: bs += context.newString(title3 + '\n', style=dict(font=fontRegular.path, textFill=1, fontSize=w / 30, rLeading=1.2, rTracking=0.03)) bs += context.newString( 'By DrawBot + PageBot + Petr van Blokland, June 17th', style=dict(font=fontRegular.path, textFill=C2, fontSize=w / 33, rTracking=0.030, rLeading=1.8)) newTextBox(bs, parent=page, x=ww25, y=0, padding=(w / 30, 0, 0, w / 30), w=ww75 + w / 100, conditions=[Fit2HeightSides()]) else: bs = context.newString(title1 + '\n', style=dict(font=fontMedium.path, textFill=C0, fontSize=w / 20, rTracking=0.015, rLeading=1.25)) bs += context.newString(title2 + '\n', style=dict(font=fontBook.path, textFill=C1, fontSize=w / 20, rLeading=1.2, rTracking=0.03)) if title3 is not None: bs += context.newString(title3 + '\n', style=dict(font=fontRegular.path, textFill=1, fontSize=w / 27, rLeading=1.2, rTracking=0.03)) bs += context.newString( 'By DrawBot + PageBot + Petr van Blokland, June 17th', style=dict(font=fontRegular.path, textFill=C2, fontSize=w / 33, rTracking=0.030, rLeading=1.8)) newTextBox(bs, parent=page, x=ww25, y=0, padding=(w / 30, 0, 0, w / 30), w=ww75 + w / 100, conditions=[Fit2HeightSides()]) score = page.evaluate() if score.fails: page.solve() # Evaluate again, result should now be >= 0 return doc
from pagebot.constants import HalfLetter from pagebot.toolbox.color import blackColor, color W, H = HalfLetter class VariableFontPlay(Element): """This element will show a summery of VF functions. Including but not limited to showing file size as an animation in relation to several axes, such as width and weight.""" from pagebot.document import Document document = Document(w=W, h=H, originTop=False) view = document.view view.padding = 30 view.showCropMarks = True view.showRegistrationMarks = True view.showPadding = True view.showFrame = True view.showNameInfo = True view.showPageMetaInfo = True page = document[1] page.padding = 30 vfp = VariableFontPlay(fill=blackColor, parent=page, padding=20, conditions=[Top2Top(), Left2Left(), Fit()]) for n in range(4): newRect(parent=vfp, w=30, h=40, margin=10, fill=color(1,0,0), stroke=0, conditions=[Right2Right(), Bottom2Bottom(), Float2Left(), Float2Top()]) document.solve() document.export("_export/VariableFontPlay.png")
def makeDocument(): """Make a new document.""" doc = Document(w=PageSize, h=PageSize, originTop=False, autoPages=1) view = doc.view view.padding = pt(40) # Show cropmarks and such. view.showCropMarks = True # Add crop marks view.showRegistrationMarks = True # Add registration marks view.showNameInfo = True # Add file name view.showMargin = True view.showFrame = True #view.showOrigin = True #view.showColorBars = True # Gives error view.showDimensions = False view.showElementInfo = ShowElementInfo page = doc[1] # Get the single page from te document. # Hard coded padding, just for simple demo, instead of # filling padding an columns in the root style. page.margin = 0 page.padding = SQ pageArea = PageSize - 2 * SQ print(PageSize, pageArea, SQ) # Make new container for adding elements inside with alignment. newRect(z=10, w=pageArea, h=pageArea, fill=color(0.8, 0.8, 0.8, 0.4), parent=page, margin=0, padding=0, yAlign=MIDDLE, xAlign=CENTER, stroke=noColor, conditions=(Center2Center(), Middle2Middle())) fontSize = RedHeight / 3 fs = doc.context.newString('Headline in red box.', style=dict(textFill=whiteColor, fontSize=fontSize, leading=fontSize, font='LucidaGrande')) newTextBox(fs, z=0, w=RedWidth, h=RedHeight, name='RedRect', parent=page, fill=color(1, 0.1, 0.1), yAlign=TOP, padding=4, conditions=(Center2Center(), Top2Top())) if not hasattr(scriptGlobals, 'blurbText'): scriptGlobals.blurbText = blurb.getBlurb('article_summary', noTags=True) fs = doc.context.newString('Headline of formatted text.\n', style=dict(font='LucidaGrande-Bold', fontSize=12, leading=14, textFill=blackColor)) fs += doc.context.newString(scriptGlobals.blurbText, style=dict(font='LucidaGrande', fontSize=10, leading=12, textFill=blackColor)) newTextBox(fs, z=0, w=YellowWidth, h=YellowHeight, parent=page, padding=4, fill=0.7, conditions=(Left2Left(), Float2Top())) path = getResourcesPath() + 'cookbot10.jpg' newImage(path, z=0, w=BlueWidth, parent=page, fill=0.7, padding=8, conditions=(Right2Right(), Float2Top())) newRect(z=0, w=BlueWidth, h=20, parent=page, fill=0.2, conditions=(Fit2Width(), Float2Top())) score = page.solve() if score.fails: print('Condition fails', score.fails) return doc # Answer the doc for further doing.
# Copyright (c) 2016+ Buro Petr van Blokland + Claudia Mens # www.pagebot.io # # P A G E B O T # # Licensed under MIT conditions # # Supporting DrawBot, www.drawbot.com # Supporting Flat, xxyxyz.org/flat # ----------------------------------------------------------------------------- # # 04_RTDExample.py # from pagebot.document import Document from pagebot.elements import newRect from pagebot.conditions import Center2Center, Middle2Middle from pagebot.toolbox.units import pt from pagebot.toolbox.color import color W, H = pt(300, 200) # Get size units doc = Document(w=W, h=H, originTop=False) page = doc[1] newRect(parent=page, fill=color('red'), size=pt(240, 140), showDimensions=True, conditions=[Center2Center(), Middle2Middle()]) page.solve() doc.export('_export/RedSquare.png')
def makeDocument(): u"""Make a new document.""" doc = Document(w=PageSize, h=PageSize, originTop=False, autoPages=1) view = doc.getView() view.padding = 10 # Don't show cropmarks and such. view.showPageCropMarks = True view.showElementOrigin = ShowOrigin view.showElementDimensions = False view.showElementInfo = ShowElementInfo page = doc[0] # Get the single page from te document. # Hard coded padding, just for simple demo, instead of # filling padding an columns in the root style. page.margin = 0 page.padding = SQ pageArea = PageSize-2*SQ print PageSize, pageArea, SQ # Make new container for adding elements inside with alignment. newRect(z=10, w=pageArea, h=pageArea, fill=(0.8, 0.8, 0.8, 0.4), parent=page, margin=0, padding=0, yAlign=MIDDLE, maxW=pageArea, maxH=pageArea, xAlign=CENTER, stroke=None, conditions=(Center2Center(), Middle2Middle())) fontSize = RedHeight/3 fs = doc.context.newString('Headline in red box.', style=dict(textFill=1, fontSize=fontSize, maxW=pageArea, maxH=pageArea, leading=fontSize, font='LucidaGrande')) newTextBox(fs, z=0, w=RedWidth, h=RedHeight, name='RedRect', parent=page, fill=(1, 0.1, 0.1), yAlign=TOP, maxW=pageArea, maxH=pageArea, padding=4, conditions=(Center2Center(), Top2Top())) if not hasattr(scriptGlobals, 'blurbText'): scriptGlobals.blurbText = blurb.getBlurb('article_summary', noTags=True) fs = doc.context.newString('Headline of formatted text.\n', style=dict(font='LucidaGrande-Bold', fontSize=12, leading=14, textFill=0)) fs += doc.context.newString(scriptGlobals.blurbText, style=dict(font='LucidaGrande', fontSize=10, leading=12, textFill=0)) newTextBox(fs, z=0, w=YellowWidth, h=YellowHeight, parent=page, padding=4, fill=0.7, maxW=pageArea, maxH=pageArea, conditions=(Left2Left(), Float2Top())) newImage('images/cookbot10.jpg', z=0, w=BlueWidth, parent=page, fill=0.7, padding=8, maxW=pageArea, maxH=pageArea, conditions=(Right2Right(), Float2Top())) newRect(z=0, w=BlueWidth, h=20, parent=page, fill=0.2, conditions=(Fit2Width(), Float2Top())) score = page.solve() if score.fails: print('Condition fails', score.fails) return doc # Answer the doc for further doing.
def buildSpecimenPages(page, family): page.padding = PADDING page.gridX = GRID_X pageTitle = family.name # Add filling rectangle for background color of the old paper book. # Set z-azis != 0, to make floating elements not get stuck at the background newRect(z=-10, w=W, h=H, parent=page, fill=PAPER_COLOR) # During development, draw the template scan as background # Set z-azis != 0, to make floating elements not get stuck at the background if SHOW_TEMPLATE: newImage(FB_PATH_L, x=0, y=0, z=-10, w=W / 2, parent=page) newImage(FB_PATH_R, x=W / 2, y=0, z=-10, w=W / 2, parent=page) # Left and right family name the current font. titleBs = context.newString(pageTitle, style=dict(font=labelFont.path, fontSize=16, xTextAlign=LEFT, textFill=0)) titleBox = newTextBox(titleBs, parent=page, h=3 * U, w=C3, conditions=[Top2Top(), Left2Left()], fill=DEBUG_COLOR0) titleBs = context.newString(pageTitle, style=dict(font=labelFont.path, fontSize=16, xTextAlign=RIGHT, textFill=0)) titleBox = newTextBox(titleBs, parent=page, h=3 * U, w=C3, conditions=[Top2Top(), Right2Right()], fill=DEBUG_COLOR0) lineL = newLine(parent=page, w=C3, h=1, mb=U, strokeWidth=1, stroke=0, conditions=(Float2Top(), Left2Left())) lineR = newLine(parent=page, w=C3, h=1, mb=U, strokeWidth=1, stroke=0, conditions=(Float2Top(), Right2Right())) descriptionFont = family.findRegularFont() description = '' if descriptionFont.info.description: description += descriptionFont.info.description + ' ' if descriptionFont.info.trademark: description += descriptionFont.info.trademark if description: description = context.newString(description, style=descriptionStyle) _, descriptionH = context.textSize(description, w=C3) else: descriptionH = 0 # Get the weightClass-->fonts dictionary, sorted by weight. weightClasses = family.getWeights() # Text samples on the right y = 300 #lineR.bottom for weightClass, fonts in sorted(weightClasses.items()): for font in fonts: if font.isItalic(): continue sample = context.newString(blurb.getBlurb('article'), style=dict(font=font.path, fontSize=9, leading=em(1.1))) h = H / len(family) + L newTextBox(sample, parent=page, w=C3, h=h, conditions=(Right2Right(), Float2Top()), fill=DEBUG_COLOR1) y -= h if y <= PB + descriptionH: break if description: newTextBox(description, parent=page, w=C3, h=descriptionH, conditions=(Right2Right(), Bottom2Bottom()), fill=DEBUG_COLOR1) weightNames = getWeightNames(family) charSetString = context.newString(weightNames + '\n\n', style=fontSetStyle) charSetString += context.newString(GLYPH_SET, style=charSetStyle) _, charSetStringH = context.textSize(charSetString, w=C3) newTextBox(charSetString, parent=page, w=C3, h=charSetStringH, conditions=(Left2Left(), Bottom2Bottom())) page.solve() # So far with conditional placement. # It's easier for this example to position the elements by y-coordinate now, because there # is mismatch between the position of the pixel image of the lines and the content of text # boxes. Although it is possible to build by floating elements or by on single text, which # will show in another proof-revival example. y = lineL.bottom - U # Position at where this went to by solving the layout conditions. # Stacked lines on the left, by separate elements, so we can squeeze them by pixel image size. for n in range(100): # That's enough headline = blurb.getBlurb('_headline', cnt=choice( (2, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8))) if random() <= 0.2: headline = headline.upper() stackLine = context.newString(headline, style=dict(font=choice( context.installedFonts()), leading=em(-0.2), paragraphTopSpacing=0, paragraphBottomSpacing=0), w=C3, pixelFit=False) _, by, bw, bh = stackLine.bounds() tw, th = context.textSize(charSetString) if y - bh < PB + th: # Reserve space for glyph set break # Filled the page. newTextBox(stackLine, parent=page, x=PL, y=y - bh - by - U, w=C3, h=th + 2, fill=DEBUG_COLOR1) y -= bh + by + U page = page.nextc
def initialize(self, padding=None, gutter=None, columns=None, **kwargs): u"""Initialize the generic book templates. """ blurb = Blurb() # TODO: Solve for left/right templates. if padding is None: padding = self.PADDING if gutter is None: gutter = self.GUTTER if columns is None: columns = self.COLUMNS fillColor1 = (0.2, 0.2, 0.9, 0.6) # Temp fill of markers. fillColor2 = (0.9, 0.2, 0.9, 0.6) # Temp fill of markers. fillColor3 = (0.9, 0.2, 0.3, 0.6) # Temp fill of markers. fillColor4 = (0.9, 0.9, 0.3, 0.6) # Temp fill of markers. fillColor1 = fillColor2 = fillColor3 = fillColor4 = None w, h = self.w, self.h cw = (w - 2 * padding - gutter * (columns - 1)) / columns cwg = cw + gutter lineW = 4 # Max amount of words return by the blurb generator. maxHeadline = 6 maxHeadlineShort = 4 maxAnkeiler = 30 fontPaths = getFontPaths() #for fontName, path in getFontPaths().items(): # if 'Escrow' in fontName: # print(fontName, path) #newspaperTitleFont = fontPaths['Escrow-Black'] #newspaperTitleFont = 'Proforma Book' newspaperTitleFont = 'Upgrade Semibold' h1Font = 'Upgrade Medium' bodyFont = 'Upgrade Book' titleStyle = dict(font=newspaperTitleFont, fontSize=140, w=(columns - 2) * cw, textFill=0) h1Style = dict(font=h1Font, fontSize=90, leading=90, textFill=0) h2Style = dict(font=h1Font, fontSize=60, leading=60, textFill=0) bodyStyle = dict(font=bodyFont, fontSize=14, hyphenation=True, leading=18, textFill=0, firstParagraphIndent=2 * gutter, firstLineIndent=gutter) h1IntroStyle = dict(font=bodyFont, fontSize=45, hyphenation=True, leading=52, textFill=0) h2IntroStyle = dict(font=bodyFont, fontSize=30, hyphenation=True, leading=36, textFill=0) titleLine = dict(strokeWidth=1, stroke=0) # grid-template-columns, grid-template-rows, grid-auto-rows, grid-column-gap, grid-row-gap, gridX = [] for n in range(columns): gridX.append([cw, gutter]) gridX[-1][-1] = 0 gridY = [(None, 0)] # Default is full height of columns # Template 'Front' t = Template(w=w, h=h, name='Front', padding=padding, gridX=gridX, gridY=gridY) # Newspaper name with border lines on top and bottom #self.title = 'NORTHAMPTON GLOBE' bs = self.view.newString(self.title.upper(), style=titleStyle) _, nameHeight = bs.size() title = Title(parent=t, mb=2 * gutter, h=nameHeight, conditions=[Top2Top(), Fit2Width()]) tb = newTextBox(bs, parent=title, h=nameHeight, xTextAlign=CENTER, pt=gutter, borderTop=titleLine, borderBottom=titleLine, conditions=[Fit2Width()]) # Place article 3 columns cc = 3 # Column width of this article. article = Article(parent=t, h=h / 3, w=cc * cwg - gutter, mr=gutter, mb=gutter, fill=fillColor1, conditions=[Left2Left(), Float2Top()]) s = None #'Happy birthday, Jill' headLine = self.getHeadline(s, h2Style, cnt=maxHeadline, w=cc * cwg - gutter) newTextBox(headLine, parent=article, w=cc * cwg - gutter, fill=fillColor2, conditions=[Left2Left(), Float2Top()]) intro = self.getAnkeiler(cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=article, w=cc * cwg - gutter, mt=gutter, mb=gutter, fill=fillColor3, conditions=[Left2Left(), Float2Top()]) for n in range(cc): dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, w=cw, mr=gutter, h=10, fill=fillColor4, conditions=[ Right2Right(), Float2Top(), Float2Left(), Fit2Bottom() ]) cc = 3 # Column width of this article. article = Article(parent=t, h=h / 4, w=cc * cwg - gutter, mr=gutter, mb=gutter, pt=gutter, borderTop=titleLine, conditions=[Left2Left(), Float2Top()]) s = None #'Explore Northampton in spring' headLine = self.getHeadline(s, h2Style, cnt=maxHeadline, w=cc * cwg - 2 * gutter) newTextBox(headLine, parent=article, pr=gutter, w=cc * cwg, conditions=[Left2Left(), Float2Top()]) intro = blurb.getBlurb('article_ankeiler', cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=article, pr=gutter, w=cc * cwg, mt=gutter, mb=gutter, conditions=[Left2Left(), Float2Top()]) for n in range(cc): dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, w=cw, mr=gutter, conditions=[ Left2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) cc = 3 # Column width of this article. article = Article(parent=t, h=h / 4, w=cc * cwg - gutter, mr=gutter, mb=gutter, pt=gutter, borderTop=titleLine, borderBottom=titleLine, conditions=[Left2Left(), Float2Top(), Fit2Bottom()]) s = None #'Mothersday for Sara & Jill' headLine = self.getHeadline(s, h2Style, cnt=maxHeadline, w=cc * cwg - 2 * gutter) newTextBox(headLine, parent=article, pr=gutter, w=cc * cwg, conditions=[Left2Left(), Float2Top()]) intro = self.getAnkeiler(cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=article, w=cc * cwg, mt=gutter, mb=gutter, conditions=[Left2Left(), Float2Top()]) for n in range(cc): dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, pr=gutter, w=cw, mr=gutter, conditions=[ Left2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) # Place article 4 columns with photo cc = 4 article = Article( parent=t, w=cc * cwg, h=h / 2, pr=gutter, conditions=[Right2RightSide(), Float2Top(), Float2Left()]) newRect(h=cc * cw * 2 / 3, mb=gutter, parent=article, fill=0.8, stroke=0, strokeWidth=0.5, conditions=[Left2Left(), Top2Top(), Fit2Width()]) s = None #'Petr & Claudia visiting soon' headLine = self.getHeadline(s, h1Style, cnt=5, w=cc * cwg - 2 * gutter) newTextBox(headLine, parent=article, pr=gutter, w=cc * cwg, pb=gutter, conditions=[Left2Left(), Float2Top(), Fit2Width()]) for n in range(cc): if n == 3: newRect(mb=gutter, parent=article, w=cw, fill=0.8, stroke=0, strokeWidth=0.5, conditions=[ Right2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) else: dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, pr=gutter, w=cw, mr=gutter, h=10, conditions=[ Right2Right(), Float2Top(), Float2Left(), Fit2Bottom() ]) cc = 2 # Column width of this article. article = Article(parent=t, w=cc * cwg, borderTop=titleLine, mb=gutter, borderBottom=titleLine, conditions=[ Right2Right(), Float2Top(), Float2Left(), Fit2Bottom() ]) s = None #'AirB&B stock up 450%' headLine = self.getHeadline(s, h2Style, cnt=maxHeadlineShort, w=cc * cwg - gutter) newTextBox(headLine, parent=article, pr=gutter, w=cc * cwg, pt=gutter, conditions=[Left2Left(), Float2Top()]) intro = self.getAnkeiler(cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=article, pr=gutter, w=cc * cwg, mt=gutter, mb=gutter, conditions=[Left2Left(), Float2Top()]) for n in range(cc): dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, pr=gutter, w=cwg, conditions=[ Left2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) cc = 2 # Column width of this article. article = Article(parent=t, w=cc * cwg, borderTop=titleLine, mb=gutter, borderBottom=titleLine, conditions=[ Right2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) s = None #u'Tay & Lan’s best moms' headLine = self.getHeadline(s, h2Style, cnt=maxHeadlineShort, w=cc * cwg - gutter) newTextBox(headLine, parent=article, pr=gutter, w=cc * cwg, pt=gutter, conditions=[Left2Left(), Float2Top()]) intro = self.getAnkeiler(cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=article, pr=gutter, w=cc * cwg - gutter, mr=gutter, mt=gutter, mb=gutter, conditions=[Left2Left(), Float2Top()]) newRect( mb=gutter, parent=article, h=200, maxH= MAX_HEIGHT, # TODO: Why need to set this, as r.maxH is 100 here. fill=0.8, stroke=0, strokeWidth=0.5, conditions=[Left2Left(), Float2Top(), Fit2Width()]) for n in range(cc): dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=article, pr=gutter, w=cwg, conditions=[ Left2RightSide(), Float2Top(), Float2Left(), Fit2Bottom() ]) self.addTemplate(t.name, t) # Template 'MainPage' t = Template(w=w, h=h, name='MainPage', padding=padding, gridX=gridX, gridY=gridY) for n in range(columns): if n == 0: cc = 3 headLine = self.getHeadline(None, h2Style, cnt=maxHeadline) newTextBox(headLine, parent=t, pr=gutter, w=cc * cwg, conditions=[Left2Left(), Float2Top()]) intro = self.getAnkeiler(cnt=maxAnkeiler) bs = self.view.newString(intro, style=h2IntroStyle) newTextBox(bs, parent=t, pr=gutter, w=cc * cwg, mt=gutter, mb=gutter, conditions=[Left2Left(), Float2Top()]) dummyArticle = blurb.getBlurb('article', newLines=True) bs = self.view.newString(dummyArticle, style=bodyStyle) newTextBox(bs, parent=t, pr=gutter, w=cw + gutter, z=0, conditions=[ Right2RightSide(), Float2Top(), Fit2Bottom(), Float2Left() ]) self.addTemplate(t.name, t)
view.padding = p(4) view.showCropMarks = True view.showFrame = True view.showPadding = True # Show padding and margin on page view.showMargin = True page = doc[1] # Get the single page from te document. page.margin = page.bleed = MARGIN page.padding = PADDING # Add element without conditions. Hard positioning from bottom-left newRect(parent=page, x=60, y=60, fill=color(0.7, 0.7, 0.7, 0.3), w=150, h=200, showMargin=True, showPadding=True, margin=MARGIN, padding=PADDING) # Condition alignment takes the element margin into account. newRect(parent=page, fill=color(0.7, 0.7, 0.7, 0.3), w=150, h=200, showMargin=True, showPadding=True, margin=MARGIN, padding=PADDING, conditions=[Right2Right(), Top2Top()])
from pagebot.document import Document from pagebot.elements import newRect from pagebot.toolbox.units import p from pagebot.toolbox.color import color from pagebot.conditions import * W = H = 500 PADDING = p(5) doc = Document(w=W, h=H, originTop=False) page = doc[1] # Get the single page from te document. page.padding = PADDING page.showPadding = True e = newRect(parent=page, fill=color('red')) e.left = page.pl e.bottom = page.pb e = newRect(parent=page, fill=color('green')) e.top = page.h - page.pt e.center = page.w/2 e = newRect(parent=page, fill=color('blue')) e.top = page.h - page.pt e.right = page.w - page.pr e = newRect(parent=page, fill=color('orange')) e.left = page.pl e.middle = page.h/2
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
doc.view.padding = 0 # Don't show cropmarks in this example. doc.view.showPagePadding = True # Get page by pageNumber, first in row (there is only one now in this row). page = doc[0] page.padding = 30 conditions = [Right2Right(), Float2Top(), Float2Left()] # TODO: Solve this bug, does not mirror. #conditions = [Left2Left(), Float2Top(), Float2Right()] for n in range(32): newRect(w=40, h=42, mr=4, mt=4, parent=page, fill=(random() * 0.5 + 0.5, 0, 0.5), conditions=conditions) # Recursively solve the conditions in all pages. # If there are failing conditions, then the status # is returned in the Score instance. score = doc.solve() if score.fails: print(score.fails) doc.export(EXPORT_PATH_SVG) doc.export(EXPORT_PATH_JPG) doc.export(EXPORT_PATH_PNG) doc.export(EXPORT_PATH_PDF)
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
def makeDocument(rootStyle): 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 doc = Document(rootStyle, w=W, h=H, autoPages=1) # One page, just the cover. page = doc[0] # Get the first/single page of the document. from random import random C1 = (random() * 0.2, random() * 0.2, 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.solve() # Solve element position, before we can make # other elements depend on position and size. #colorRect2: M = 64 newRect( z=-10, name='Frame 2', conditions=[Center2Center(), Middle2Middle()], fill=darker(C1, 0.5), # Default parameter: # 50% between background color and white stroke=None, w=colorRect1.w - M, h=colorRect1.h - M, xAlign=CENTER, yAlign=CENTER) # Add some title (same width, different height) at the "wrongOrigin" position. # They will be repositioned by solving the colorConditions. newText('Book Cover', style=rootStyle, parent=page, name='Other element', font='Georgia', fontSize=40, fill=(0.3, 0.3, 0.5), textFill=(1, 0, 0), conditions=[Top2Middle(), Top2Top()], xAlign=CENTER) #yAlign=TOP) """ page.rect(point=wrongOrigin, style=rootStyle, w=W2, h=H2, name='Floating element 2', conditions=colorCondition2, fill=(1, 1, 0), xAlign=LEFT, yAlign=TOP) page.rect(point=wrongOrigin, style=rootStyle, w=W3, h=H3, name='Floating element 3', conditions=colorCondition2, fill=(1, 0, 1), xAlign=LEFT, yAlign=TOP) # Make text box at wrong origin. Apply same width a the color rect, # which may be too wide from typographic point ogf view. # The MaxWidthByFontSize will set the self.w to the maximum # width for this pointSize. if not hasattr(pbglobals, 'blurbText'): the_blurb = blurb.getBlurb('article_summary', noTags=True) pbglobals.blurbText = doc.context.newString(the_blurb, page, style=dict(font='Georgia', fontSize=12, rLeading=0.2, textColor=0)) page.textBox(pbglobals.blurbText, point=wrongOrigin, style=rootStyle, w=WT, conditions=textCondition, xAlign=CENTER, yAlign=CENTER) page.rect(point=wrongOrigin, style=rootStyle, w=W4, h=H4, name='Floating element 4', conditions=colorCondition2, fill=(0, 1, 1), xAlign=LEFT, yAlign=TOP) page.rect(point=wrongOrigin, style=rootStyle, w=W5, h=H5, name='Floating element 5', conditions=[FloatRightTopSides()], fill=(0, 1, 0), xAlign=LEFT, yAlign=TOP) """ score = page.evaluate() #print 'Page value on evaluation:', score #print score.fails # Try to solve the problems if evaluation < 0 if score.fails: print('Solving', score.fails) page.solve() #print score.fails # Evaluate again, result should now be >= 0 score = page.evaluate() print('Page value after solving the problems:', score) for fail in score.fails: print(fail) return doc
view.showCropMarks = True # Set the view flags what to show. view.showRegistrationMarks = True view.showPadding = True view.showFrame = True view.showNameInfo = True view.showPageMetaInfo = True # Get the page of the document. Since right pages are uneven, the list of pages # startes at #1. page = doc[1] # Set the padding of the page, as we don't use a template here. # In multipage documents this also can be done in the document, so pages inherit. page.padding = PAD, PAD, 2 * PAD, PAD # Create rectangles. Default position and size is (0, 0, 100, 100). The conditions define the layout. newRect(fill=color(1, 0, 0), parent=page, h=pt(62), conditions=(Left2Left(), Float2Top(), Fit2Right())) newRect(fill=color(1, 0, 1), parent=page, conditions=(Left2LeftSide(), Float2Top())) newRect(fill=color(spot=300), parent=page, conditions=(Right2Right(), Float2Top(), Float2Left(), Fit2Right())) # Add a floating text box. newTextBox('BB', parent=page, x=100, y=150, w=300, h=400,
from pagebot.toolbox.color import color from pagebot.conditions import * W = H = pt(500) PADDING = p(4) w = p(8) doc = Document(w=W, h=H, originTop=False) page = doc[1] # Get the single page from te document. page.padding = PADDING page.showPadding = True newRect(parent=page, w=w, h=w, fill=color('red'), conditions=[Left2Left(), Bottom2Bottom()]) newRect(parent=page, w=w, h=w, fill=color('green'), conditions=[Center2Center(), Top2Top()]) newRect(parent=page, w=w, h=w, fill=color('blue'), conditions=[Right2Right(), Top2Top()]) newRect(parent=page, w=w, h=w,
from pagebot.constants import * W = H = 500 PADDING = p(5) doc = Document(w=W, h=H, padding=PADDING, originTop=False) view = doc.view view.padding = PADDING view.showOrigin = True view.showPadding = True view.showFrame = True view.showCropMarks = True page = doc[1] # Get the single page from te document. print(page, 'Origin on top:', page.originTop) # Inherited from document e = newRect(x=0, y=0, w=100, h=100, parent=page, fill=0.5) print(e, 'yAlign', e.yAlign) e = newRect(w=100, h=100, parent=page, fill=0.5) e.top = page.h e.right = page.w page = page.next page.originTop = True # Force this page to have origin on top print(page, 'Origin on top:', page.originTop) # Auto aligns on top, yAlign initializing from page.originTop e = newRect(x=0, y=0, w=100, h=100, parent=page, fill=0.5) print(e, 'yAlign', e.yAlign) e = newRect(w=100, h=100, parent=page, fill=0.5, yAlign=TOP, xAlign=RIGHT) e.top = page.h e.right = page.w
def buildSpecimenPages(doc, family, pn): for font in family.getFonts(): page = doc[pn] page.padding = PADDING page.gridX = GRID_X pageTitle = path2FontName(font.path) # Add filling rectangle for background color of the old paper book. # Set z-azis != 0, to make floating elements not get stuck at the background newRect(z=-10, w=W, h=H, parent=page, fill=PAPER_COLOR) # During development, draw the template scan as background # Set z-azis != 0, to make floating elements not get stuck at the background if SHOW_TEMPLATE: newImage(ATF_PATH, x=0, y=0, z=-10, w=W, parent=page) # Centered title: family name and style name of the current font. titleBs = context.newString(pageTitle, style=dict(font=font.path, xTextAlign=CENTER, textFill=0)) titleBox = newTextBox(titleBs, parent=page, h=2*L, conditions=[Top2Top(), Fit2Width()], fill=DEBUG_COLOR0) titleBox.solve() largeSampleBox = newTextBox('', parent=page, w=C1+G/2, conditions=[Float2Top(), Left2Left(), Fit2Bottom()], fill=DEBUG_COLOR1) largeSampleBox.solve() # In order to fit different words in the fixed space, they will vary in size. # But as the variation in sizes is larger than the variation in size, we'll calculate the strings # first for the various word lengths and then sort them by size. largeSampleSizes = {} for n in (4, 5, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10): word = getCapitalizedWord(n) if word is None: continue if len(largeSampleSizes) > 10: break sample = context.newString(word+'\n', style=dict(font=font.path, rLeading=1), w=C1, pixelFit=False) sampleFontSize = int(round(sample.fontSize)) if not sampleFontSize in largeSampleSizes: largeSampleSizes[sampleFontSize] = sample print sorted(largeSampleSizes.keys()) largeSample = context.newString('') for sampleFontSize, sample in sorted(largeSampleSizes.items(), reverse=True): label = context.newString('%d Points\n' % round(sampleFontSize), style=labelStyle) #if largeSample.h + sample.h > largeSampleBox.h: # break largeSample += label + sample largeSampleBox.setText(largeSample) for fontSize, numChars in ((12, 8), (10, 13), (8, 16)): smallSamples = context.newString(getCapWord(numChars), style=dict(font=font.path), w=C2) label = context.newString('%d Points\n' % round(smallSamples.fontSize), style=labelStyle) shortWordsSample = context.newString(getShortWordText(), style=dict(font=font.path, fontSize=smallSamples.fontSize, rLeading=1)) newTextBox(label + smallSamples + ' ' + shortWordsSample, parent=page, w=C2+G/2, h=80, ml=G/2, mb=L, conditions=[Right2Right(), Float2Top(), Float2Left()], fill=DEBUG_COLOR1) label = context.newString('%d Points\n' % fontSize, style=labelStyle) smallSamples = context.newString(blurb.getBlurb('article', noTags=True), style=dict(font=font.path, fontSize=fontSize)) newTextBox(label + smallSamples, parent=page, w=C2-2, h=80, mb=L, ml=G/2, conditions=[Right2Right(), Float2Top()], fill=DEBUG_COLOR1) glyphSetFrame = newRect(parent=page, mb=L, ml=G/2, padding=L, borders=dict(line=INLINE, stroke=0, strokeWidth=0.5), conditions=[Right2Right(), Float2Top(), Float2Left(), Fit2Right(), Fit2Bottom()], fill=DEBUG_COLOR2) glyphSet = context.newString('Subset of characters in Complete Font\n', style=dict(font=font.path, fontSize=8, xTextAlign=CENTER, rParagraphTopSpacing=0.25, rParagraphBottomSpacing=0.5)) glyphSet += context.newString(GLYPH_SET, style=dict(font=font.path, fontSize=23, xTextAlign=CENTER, leading=32)) newTextBox(glyphSet, parent=glyphSetFrame, padding=(1.5*L, L, L, L), borders=dict(line=INLINE, stroke=0, strokeWidth=0.25), conditions=[Left2Left(), Fit2Right(), Top2Top(), Fit2Bottom() ], fill=DEBUG_COLOR3) pn += 1 return pn
W, H = A4 # Standard page size # To see how the "responsive" layout works in landscape, swap W and H #H, W = A4 PAD = 50 # Page padding # Create a Document instance of the defined size and one automatic page. doc = Document(w=W, h=H, title="Demo pages", originTop=False, context=context) # Get the view of the document. By default this is a PageView, writing # on a DrawBotContext or FlatContext, whatever is avaialbe as default. view = doc.view view.padding = 40 # Space around the page to accommodate registratio and crop marks. view.showPageCropMarks = True # Set the view flags what to show. view.showPageRegistrationMarks = True view.showPagePadding = True view.showPageFrame = True view.showPageNameInfo = True # Get the page of the document. Since right pages are uneven, the list of pages # startes at #1. page = doc[1] page.padding = PAD, PAD, 2*PAD, PAD # Set the padding of the page, as we don't use a template here. # Create rectangles. Default position and size is (0, 0, 100, 100). The conditions define the layout. newRect(fill=(1, 0, 0), parent=page, h=50, conditions=(Left2Left(), Float2Top(),Fit2Right())) newRect(fill=(1, 0, 1), parent=page, conditions=(Left2LeftSide(), Float2Top())) newRect(fill=(0, 1, 1), parent=page, conditions=(Right2Right(), Float2Top(), Float2Left(), Fit2Right())) # Solve the conditions of the layout in defined order. doc.solve() # Export to PDF. doc.export('_export/SimpleDocument.pdf')
def makeDocument(): u"""Make a new document.""" doc = Document(w=W, h=H, originTop=False, autoPages=1) page = doc[0] # Get the single page from te document. # Hard coded padding, just for simple demo, instead of filling padding an # columns in the root style. page.padding = SQ # Position square in the 4 corners of the page area. # Notice that their alignment (left) does not matter for the conditions. cornerConditions = [(Float2Right(), Float2Top()), (Float2Right(), Float2Bottom()), (Float2Left(), Float2Bottom()), (Float2Left(), Float2Top())] z = 1 for condition in cornerConditions: newRect(z=z, w=SQ, h=SQ, parent=page, conditions=condition, fill=0.7) z += 1 # Make new container for adding elements inside with alignment. #cnt = newRect(z=z, w=W-2*SQ, h=H-2*SQ, fill=(0.8, 0.8, 0.8, 0.4), # parent=page, margin=SQ, yAlign=BOTTOM, # xAlign=CENTER, stroke=None, # conditions=(Center2Center(), Middle2Middle())) z += 1 newRect(z=z, w=SQ, h=SQ, stroke=None, parent=page, xAlign=CENTER, conditions=(Center2Center(), Middle2Middle()), fill=(1, 0, 0)) z += 1 # Side conditions conditions = [(Center2Center(), Float2Top()), (Center2Center(), Float2Bottom()), (Float2Left(), Middle2Middle()), (Float2Right(), Middle2Middle())] for condition in conditions: newRect(z=z, w=SQ, h=SQ, stroke=None, parent=page, xAlign=CENTER, conditions=condition, fill=(1, 1, 0)) z += z sideConditions = [(Center2Center(), Float2TopSide()), (Center2Center(), Float2BottomSide()), (Float2LeftSide(), Middle2Middle()), (Float2RightSide(), Middle2Middle())] for condition in sideConditions: newRect(z=z, w=SQ, h=SQ, stroke=None, parent=page, xAlign=CENTER, conditions=condition, fill=(0.5, 1, 0)) z += 1 cornerConditions = [(Float2LeftSide(), Float2TopSide()), (Float2RightSide(), Float2TopSide()), (Float2LeftSide(), Float2BottomSide()), (Float2RightSide(), Float2BottomSide())] for condition in cornerConditions: newRect(z=z, w=SQ, h=SQ, stroke=None, parent=page, xAlign=CENTER, conditions=condition, fill=(0, 0, 1)) z += 1 # Solve the layout placement conditions on the pages of doc by moving the # elements that are not on the right positions (which is all of them, # because we did not add point attributes when creating them. score = doc.solve() # Solves all document. page.solve() only solves page. if score.fails: print('Failed to solve %d conditions:' % len(score.fails)) for condition, e in score.fails: print(e.bottom2BottomSide()) print(condition, e, e.bottom, Bottom2BottomSide().test(e), e.isBottomOnBottomSide(), e.bottom) view = doc.getView() view.w, view.h = W, H view.padding = 40 # Don't show cropmarks and such. view.showElementOrigin = ShowOrigins # Show origin alignment # markers on each element. view.showElementDimensions = ShowDimensions view.showPageFrame = True view.showPagePadding = True view.showPageCropMarks = True view.showElementInfo = ShowElementInfo # Show baxes with element info return doc # Answer the doc for further doing.
def makeDocument(context): """Make a new document.""" doc = Document(w=W, h=H, originTop=False, autoPages=1, context=context) page = doc[1] # Get the single page from te document. # Hard coded padding, just for simple demo, # instead of filling padding and columns in the root style. page.padding = SQ # Position square in the 4 corners of the page area. # Notice that their alignment (left) does not matter for the conditions. newRect(w=SQ, h=SQ, parent=page, conditions=(Right2Right(), Top2Top()), fill=darkGrayColor) newRect(w=SQ, h=SQ, parent=page, conditions=(Left2Left(), Bottom2Bottom()), fill=darkGrayColor) newRect(w=SQ, h=SQ, parent=page, conditions=(Left2Left(), Top2Top()), fill=darkGrayColor) newRect(w=SQ, h=SQ, parent=page, conditions=(Right2Right(), Bottom2Bottom()), fill=darkGrayColor) # Make new container for adding elements inside with alignment. # cnt = newRect(w=W-2*SQ, h=H-2*SQ, # fill=color(0.8, 0.8, 0.8, 0.4), # parent=page, margin=SQ, yAlign=BOTTOM, # xAlign=CENTER, stroke=noColor, # conditions=(Center2Center(), Middle2Middle())) # Add rectangles to the page, # using alignment conditions to position rules. newRect(w=SQ, h=SQ, stroke=noColor, parent=page, xAlign=CENTER, conditions=(Center2Center(), Middle2Middle()), fill=redColor) conditions = [(Center2Center(), Top2Top()), (Center2Center(), Bottom2Bottom()), (Left2Left(), Middle2Middle()), (Right2Right(), Middle2Middle())] for condition in conditions: newRect(w=SQ, h=SQ, stroke=noColor, parent=page, xAlign=CENTER, conditions=condition, fill=color(1, 1, 0)) sideConditions = [(Center2Center(), Top2TopSide()), (Center2Center(), Bottom2BottomSide()), (Left2LeftSide(), Middle2Middle()), (Right2RightSide(), Middle2Middle())] for condition in sideConditions: newRect(w=SQ, h=SQ, stroke=noColor, parent=page, xAlign=CENTER, conditions=condition, fill=color(0.5, 1, 0)) cornerConditions = [(Left2LeftSide(), Top2TopSide()), (Right2RightSide(), Top2TopSide()), (Left2LeftSide(), Bottom2BottomSide()), (Right2RightSide(), Bottom2BottomSide())] for condition in cornerConditions: newRect(w=SQ, h=SQ, stroke=noColor, parent=page, xAlign=CENTER, conditions=condition, fill=blueColor) # Solve the layout placement conditions on the page by moving the # elements that are not on the right positions (which is all of them, # because we did not add point attributes when creating them. score = page.solve() if score.fails: print('Failed to solve %d conditions:' % len(score.fails)) for condition, e in score.fails: print(e.bottom2BottomSide()) print(condition, e, e.bottom, Bottom2BottomSide().test(e), e.isBottomOnBottomSide(), e.bottom) # 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 = 30 # Showing cropmarks and registration marks # need >= 20 padding of the view. view.showRegistrationMarks = True view.showCropMarks = True view.showFrame = True view.showPadding = True view.showNameInfo = True # These values can be changed in the Variable window, # when in DrawBot context. view.showOrigin = ShowOrigins # Show origin alignment # markers on each element. view.showDimensions = ShowDimensions view.showElementInfo = ShowElementInfo # Show boxes with element info return doc # Answer the doc.