def makeCoverTemplate(imagePath, w, h): bleed = 0 textColor = 1 # Make styles # TODO: Make this fit, using size/wdth axis combination of Amstelvar coverTitleSize = 160 # Not optical size yet. Play more with the axes coverTitleFont = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0.02))#, opsz=coverTitleSize)) coverTitleStyle = dict(font=coverTitleFont.installedName, fontSize=coverTitleSize, textShadow=shadow, textFill=textColor, tracking=-3) coverSubTitleSize = 80 # Not optical size yet. Play more with the axes coverSubTitleFont = getVariableFont(FONT_PATH, dict(wght=0.6, wdth=0.02)) #opsz=coverSubTitleSize)) coverSubTitleStyle = dict(font=coverSubTitleFont.installedName, fontSize=coverSubTitleSize, textFill=(1, 1, 1, 0.3), tracking=0) # Cover coverTemplate = Template(w=w, h=h, padding=PADDING) # Cover template of the magazine. newImage(imagePath, parent=coverTemplate, conditions=[Fit2WidthSides(), Bottom2BottomSide()]) # Title of the magazine cover. coverTitle = context.newString('Fashion', style=coverTitleStyle) # Calculate width if single "F" for now, to align "Slow" # TODO: Change in example to go through the coverTitle to get positions and widths. FWidth, _ = textSize(context.newString('F', style=coverTitleStyle)) coversubTitle = context.newString('Slow', style=coverSubTitleStyle) newTextBox(coversubTitle, parent=coverTemplate, pl=FWidth*0.5, conditions=[Left2Left(), Fit2Width(), Top2TopSide()]) tw, th = textSize(coverTitle) newText(coverTitle, parent=coverTemplate, z=20, h=th*0.4, textShadow=shadow, conditions=[Fit2Width(), Top2TopSide()]) # Make actual date in top-right with magazine title. Draw a bit transparant on background photo. dt = datetime.now() d = dt.strftime("%B %Y") fs = context.newString(d, style=dict(font=MEDIUM.installedName, fontSize=17, textFill=(1, 1, 1, 0.6), tracking=0.5)) # TODO: padding righ could come from right stem of the "n" newTextBox(fs, parent=coverTemplate, xTextAlign=RIGHT, pr=10, pt=6, conditions=[Top2Top(), Right2Right()]) # Titles could come automatic from chapters in the magazine. fs = context.newString('$6.95', style=dict(font=BOOK.installedName, fontSize=12, textFill=textColor, tracking=1, leading=12)) newText(fs, parent=coverTemplate, mt=8, conditions=[Top2Bottom(), Right2Right()]) makeCoverTitles(coverTemplate) return coverTemplate
def makeSpecimen(rs): # 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) page1 = doc[1] vCube = VariableCube(FONT_PATH, w=500, h=500, s='a', fontSize=86, dimensions=dict(wght=4, rnds=4)) page1.place(vCube, 50, 160) font = getVariableFont(FONT_PATH, location=dict(wght=-0.5, rnds=2, diam=0.5)) page2 = doc[2] for n in range(600): page2.text( FormattedString('@', font=font, fontSize=800, fill=(random(), random(), random(), 0.8)), 50 + random() * 100, 200 + random() * 100) return doc
def _drawGlyphMarker(self, axisName, mx, my, glyphName, fontSize, location, strokeW=2): # Middle circle context.fill(1) context.stroke(0.7) context.strokeWidth(strokeW) context.oval(mx - fontSize / 2 * self.R, my - fontSize / 2 * self.R, fontSize * self.R, fontSize * self.R) variableFont = getVariableFont(self.font, location) # Show axis name below circle marker? if self.showAxisNames and axisName is not None: fs = context.newString(axisName, style=dict(font=variableFont.installedName, fontSize=fontSize / 4, textFill=0)) tw, th = context.textSize(fs) context.text( fs, (mx - tw / 2, my - fontSize / 2 * self.R - th * 2 / 3)) glyphPathScale = fontSize / self.font.info.unitsPerEm context.drawGlyphPath(variableFont, glyphName, mx, my - fontSize / 3, s=glyphPathScale, fillColor=0)
def main(): #print _drawBotDrawingTool._tempInstalledFonts template = Template(w=W, h=H, padding=PADDING) doc = Document(title="sample", w=W, h=H, autoPages=1, originTop=False, pageTemplate=template, startPage=1) view = doc.getView() view.padding = 40 page1 = doc[0] for ypos, ytch in enumerate(range(200, 800+1, 200)): for xpos, xtch in enumerate(range(200, 800+1, 200)): f = getVariableFont(FONT_PATH, location=dict(xtch=xtch, ytch=ytch)) newText("我H", point=(70*xpos, 750-40*ypos), parent=page1, name="", font=f.installedName, fontSize=40) doc.solve() doc.export(EXPORT_PATH)
import pagebot from pagebot.fonttoolbox.objects.font import Font, AXES from pagebot.fonttoolbox.variablefontbuilder import getVariableFont FONT_PATH = pagebot.getFontPath() fontPath = FONT_PATH + 'fontbureau/AmstelvarAlpha-VF.ttf' def printFontInfo(aFont): print '-' * 80 print 'Family name', aFont.info.familyName print 'Style name', aFont.info.styleName print 'Number of glyphs', len(font) if aFont.axes: print 'Number of axes', len(aFont.axes) for axisTag, axisValues in sorted(aFont.axes.items()): print '\t' + axisTag, axisValues, if axisTag in AXES: print AXES[axisTag]['name'], AXES[axisTag]['description'] else: print 'Unknown axis' else: print 'No Variable axes. This is a non-Variable font or an Variable instance.' # Usage of PageBot Font class. font = Font(fontPath) printFontInfo(font) # Create an instance instance = getVariableFont(font, dict(wdth=500, wght=200)) printFontInfo(instance)
def fitVariableWidth(varFont, s, w, fontSize, condensedLocation, wideLocation, fixedSize=False, tracking=None, rTracking=None): u""" Answer the font instance that makes string s width on the given width *w* for the given *fontSize*. The *condensedLocation* dictionary defines the most condensed font instance (optionally including the opsz) and the *wideLocation* dictionary defines the most wide font instance (optionally including the opsz). The string width for s is calculated with both locations and then the [wdth] value is interpolated and iterated until the location is found where the string *s* fits width *w). Note that interpolation may not be enough, as the width axis may contain non-linear masters. If the requested w outside of what is possible with two locations, then interations are performed to change the size. Again this cannot be done by simple interpolation, as the [opsz] also changes the width. It one of the axes does not exist in the font, then use the default setting of the font. """ condFont = getVariableFont(varFont, condensedLocation) condensedFs = context.newString(s, style=dict(font=condFont.installedName, fontSize=fontSize, tracking=tracking, rTracking=rTracking, textFill=0)) condWidth, _ = textSize(condensedFs) wideFont = getVariableFont(varFont, wideLocation) wideFs = context.newString(s, style=dict(font=wideFont.installedName, fontSize=fontSize, tracking=tracking, rTracking=rTracking, textFill=0)) wideWidth, _ = textSize(wideFs) # Check if the requested with is inside the boundaries of the font width axis if w < condensedWidth: font = condFont fs = condensedFs location = condensedLocation elif w > wideWidth: font = wideFont fs = wideFs location = wideLocation else: # Now interpolation the fitting location widthRange = wideLocation['wdth'] - condensedLocation['wdth'] location = copy.copy(condensedLocation) location['wdth'] += widthRange * (w - condWidth) / (wideWidth - condWidth) font = getVariableFont(varFont, location) fs = context.newString(s, style=dict(font=font.installedName, fontSize=fontSize, tracking=tracking, rTracking=rTracking, textFill=0)) return dict(condensendFont=condFont, condensedFs=condensedFs, condWidth=condWidth, condensedLocation=condensedLocation, wideFont=wideFont, wideFs=wideFs, wideWidth=wideWidth, wideLocation=wideLocation, font=font, fs=fs, width=textSize(fs)[0], location=location)
# import pagebot from pagebot.contexts import defaultContext as context from pagebot.fonttoolbox.objects.font import Font, getFontByName from pagebot.fonttoolbox.variablefontbuilder import getVariableFont from pagebot.style import CENTER W = H = 500 ROOT_PATH = pagebot.getRootPath() FONT_PATH = ROOT_PATH + '/Fonts/fontbureau/AmstelvarAlpha-VF.ttf' f = Font(FONT_PATH, install=True) # Get PageBot Font instance of Variable font. ULTRALIGHT_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0.7), styleName='Utrla Light Condensed') ULTRALIGHT = getVariableFont(FONT_PATH, dict(wght=1, wdth=0), styleName='Ultra Light') LIGHT_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0.7), styleName='Light Condensed') LIGHT = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0), styleName='Light') THIN_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.8, wdth=0.7), styleName='Thin Condensed') THIN = getVariableFont(FONT_PATH, dict(wght=0.8, wdth=0), styleName='Thin') BOOK_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.7, wdth=0.7), styleName='Book Condensed')
# In the future these classes will be part of the main PageBot library, # which may make them incompatible with this particular example. # import pagebot from pagebot.contexts import defaultContext as context from pagebot.fonttoolbox.objects.font import Font, getFontByName from pagebot.fonttoolbox.variablefontbuilder import getVariableFont from pagebot.style import CENTER W = H = 500 ROOT_PATH = pagebot.getRootPath() FONT_PATH = ROOT_PATH + '/Fonts/fontbureau/AmstelvarAlpha-VF.ttf' f = Font(FONT_PATH, install=True) # Get PageBot Font instance of Variable font. LIGHT72 = getVariableFont(FONT_PATH, dict(wght=0.5, wdth=0.6, opsz=72), styleName='Light72') BOOK_LIGHT = getVariableFont(FONT_PATH, dict(wght=0.5, wdth=0.7), styleName='Book Light') BOOK_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.6, wdth=0.7), styleName='Book Condensed') BOOK = getVariableFont(FONT_PATH, dict(wght=0.6, wdth=0), styleName='Book') LIGHT_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0.7), styleName='Light Condensed') LIGHT = getVariableFont(FONT_PATH, dict(wght=0.9, wdth=0), styleName='Light') BOOK_ITALIC = getVariableFont(FONT_PATH, dict(wght=0.25, wdth=1), styleName='Book Italic') MEDIUM = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=0), styleName='Medium') SEMIBOLD = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=1), styleName='Semibold') SEMIBOLD_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=0.5), styleName='Semibold Condensed') BOLD = getVariableFont(FONT_PATH, dict(wght=0.0, wdth=0), styleName='Bold') BOLD_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.0, wdth=0.7), styleName='Bold Condensed') BOLD_ITALIC = getVariableFont(FONT_PATH, dict(wght=0.7, wdth=0), styleName='Bold Italic') LABEL_FONT = BOOK
def makeDocument(fontPath): u"""Create Document instance with a single page. Fill the page with elements and perform a conditional layout run, until all conditions are solved.""" f = Font(fontPath) # Get PageBot Font instance of Variable font. fHeader = getVariableFont(f, dict(wght=0.5)) fHeaderCondensed = getVariableFont(f, dict(wght=0.5, wdth=0.4)) doc = Document(w=PageWidth, h=PageHeight, originTop=False, autoPages=1) # Get default view from the document and set the viewing parameters. view = doc.getView() view.style['fill'] = 1 view.padding = 0 # To show cropmarks and such, make >40 or so. view.showPageCropMarks = True # Won't show if there is not padding in the view. view.showPageRegistrationMarks = True view.showPageFrame = True view.showPagePadding = True view.showPageNameInfo = True view.showElementOrigin = False view.showElementDimensions = False #ShowDimensions view.showElementInfo = False page = doc[0] # Get the single frint page from the document. # Hard coded padding, just for simple demo, instead of filling padding an columns in the root style. page.margin = 0 page.padding = pagePadding pageAreaW = PageWidth-pl-pr pageAreaH = PageHeight-pt-pb # Resources blockFill = None #(1, 1, 0) # Use color to debug page area gradient = Gradient(locations=[1,0], colors=((0, 0, 0), (0.8, 0.8, 0.8))) shadow = Shadow(offset=(6, -6), blur=10, color=(0.2, 0.2, 0.2, 0.5)) bookBorders = dict(stroke=(1, 1, 1, 0.5),strokeWidth=0.1,line=OUTLINE) bookPadding = (25, 30, 40, 30) # Styles titleStyle =dict(font=fHeader.installedName, fontSize=26, rLeading=1.4, xTextAlign=CENTER, textFill=1) authorStyle = dict(font='Georgia-Italic', textFill=1, fontSize=18, xTextAlign=CENTER) headStyle = dict(font=fHeader.installedName, textFill=0, fontSize=14, rLeading=1.4, xTextAlign=LEFT, paragraphTopSpacing=30, paragraphBottomSpacing=0) bodyStyle = dict(font='Verdana', textFill=0, fontSize=12, rLeading=1.4, xTextAlign=LEFT, paragraphTopSpacing=10, hyphenation=True) italicBodyStyle = copy.copy(bodyStyle) italicBodyStyle['font'] = 'Verdana-Italic' italicBodyStyle['paragraphTopSpacing'] = 0 fs = newFS(f.info.familyName + ' ' + f.info.styleName, style=titleStyle) _, th = textSize(fs) title = newTextBox(fs, conditions=[Top2Top(), Fit2Width()], parent=page, h=th*1.2) # Make new container for adding elements inside with alignment. newRect(z=10, w=pageAreaW, h=pageAreaH, fill=blockFill, parent=page, margin=0, padding=0, yAlign=MIDDLE, maxW=pageAreaW, maxH=pageAreaH, xAlign=CENTER, conditions=(Center2Center(), Middle2Middle())) t1 = newTextBox('Inside the Amstelvar', z=0, font=fHeader.installedName, fontSize=40, w=pageAreaW/2, parent=page, conditions=(Left2Left(), Top2Top())) w = pageAreaW*0.75 # Used as element width and relative font size. padding = PageHeight/24 fs = newFS('Variable Fonts ', style=dict(font=fHeader.installedName, textFill=0, fontSize=90)) fs += newFS('Explained', style=dict(font=fHeaderCondensed.installedName, textFill=0, fontSize=90)) t2 = newTextBox(fs, z=0, w=PageWidth*0.75, parent=page, conditions=(Left2Left(), Fit2Width(), Float2Top())) circle = VariableCircle(f, s=GLYPH_NAME, name='VariableCircleSpeciment', parent=page, padding=4, x=100, fontSize=150, maxW=PageWidth-2*PADDING, minW=100, showAxisName=True, # Conditions make the element move to top-left of the page. # And the condition that there should be no overflow, otherwise the text box # will try to solve it. #conditions=[Right2Right(), Float2Top()], conditions=[Left2Left(), Fit2Width(), Bottom2Bottom()], # Position of the origin of the element. Just to show where it is. # Has no effect on the position conditions. yAlign=BOTTOM, xAlign=LEFT, fill=CIRCLE_ELEMENT_FILL, borders=1, ) score = page.solve() if score.fails: print 'Condition fails', score.fails return doc # Answer the doc for further doing.
from pagebot.fonttoolbox.objects.font import Font from pagebot.fonttoolbox.variablefontbuilder import getVariableFont, drawGlyphPath, getVarLocation f = Font('/Library/Fonts/Skia.ttf') wghtMin, wghtDef, wghtMax = f.axes['wght'] wdthMin, wdthDef, wdthMax = f.axes['wdth'] #wghtMin, wghtDef, wghtMax = (-1, 0, 1) #wdthMin, wdthDef, wdthMax = (-1, 0, 1) print 'wght', wghtMin, wghtDef, wghtMax print 'wdth', wdthMin, wdthDef, wdthMax NORMAL = getVariableFont(f, dict(wght=wghtDef, wdth=wdthDef), styleName='Normal', normalize=False) LIGHT = getVariableFont(f, dict(wght=wghtMin, wdth=wdthDef), styleName='Light', normalize=False) BOLD = getVariableFont(f, dict(wght=wghtMax, wdth=wdthDef), styleName='Bold', normalize=False) COND = getVariableFont(f, dict(wght=wghtDef, wdth=wdthMin), styleName='Cond', normalize=False) WIDE = getVariableFont(f, dict(wght=wghtDef, wdth=wdthMax),
else: # NL version of the article. RS['language'] = 'nl-be' # Tell RootStyle instance to use Dutch hyphenation.. MD_PATH = 'automatischePaginaCompositie_nl.md' # Path of the MarkDown text in local folder. EXPORT_PATH = '_export/AutomatischePaginaOpmaak.pdf' # Export part of generated PDF output document. MAIN_FLOW = 'main' # ELement id of the text box on pages the hold the main text flow. # Some tracking presets H1_TRACK = H2_TRACK = 0.015 # 1/1000 of fontSize, multiplier factor. H3_TRACK = 0.030 # Tracking as relative factor to font size. P_TRACK = 0.030 # Path to the Variable font source, from which width/weight instances will be created. FONT_PATH = '../../fonts/PromisePageBot-GX.ttf' # Create instance, depending on the location in the Variable axes. LIGHT = getVariableFont(FONT_PATH, {"wght": 100, "wdth": 1000}) LIGHT_CONDENSED = getVariableFont(FONT_PATH, {"wght": 100, "wdth": 800}) BOOK_LIGHT = getVariableFont(FONT_PATH, {"wght": 175, "wdth": 1000}) BOOK_CONDENSED = getVariableFont(FONT_PATH, {"wght": 250, "wdth": 800}) BOOK = getVariableFont(FONT_PATH, {"wght": 250, "wdth": 1000}) BOOK_ITALIC = getVariableFont(FONT_PATH, {"wght": 250, "wdth": 1000}) MEDIUM = getVariableFont(FONT_PATH, {"wght": 400, "wdth": 1000}) SEMIBOLD = getVariableFont(FONT_PATH, {"wght": 400, "wdth": 1000}) SEMIBOLD_CONDENSED = getVariableFont(FONT_PATH, {"wght": 600, "wdth": 500}) BOLD = getVariableFont(FONT_PATH, {"wght": 800, "wdth": 1000}) BOLD_ITALIC = getVariableFont(FONT_PATH, {"wght": 800, "wdth": 1000}) BLACK = getVariableFont(FONT_PATH, {"wght": 1000, "wdth": 1000}) RS['font'] = BOOK
from pagebot.fonttoolbox.variablefontbuilder import getVariableFont, Font W, H = int(A5[0]), int(A5[1]) MAX_PAGES = 10 ARTICLE_CNT = 10 ROOT_PATH = pagebot.getRootPath() FONT_PATH = ROOT_PATH + '/Fonts/fontbureau/AmstelvarAlpha-VF.ttf' #FONT_PATH = getMasterPath() + 'BitcountGrid-GX.ttf' f = Font(FONT_PATH) print f.axes # TODO: Check axis conversions! h1Font = getVariableFont(FONT_PATH, dict(wght=0.8, wdth=0, opsz=0.5)) h2Font = getVariableFont(FONT_PATH, dict(wght=0.6, wdth=0, opsz=0.5)) h3Font = getVariableFont(FONT_PATH, dict(wght=0.6, wdth=0, opsz=0.5)) pFont = getVariableFont(FONT_PATH, dict(wght=0.5, wdth=0, opsz=0.5)) doc = Document(w=W, h=H, originTop=False, autoPages=1) doc.newStyle(name='h1', fontSize=30, font=h1Font.installedName, textFill=(1, 0, 0), rLeading=1.3, rParagraphTopSpacing=1.3, hyphenation=True) doc.newStyle(name='h2', fontSize=22, font=h2Font.installedName,
# ----------------------------------------------------------------------------- # # P A G E B O T # # Copyright (c) 2016+ Buro Petr van Blokland + Claudia Mens & Font Bureau # www.pagebot.io # Licensed under MIT conditions # # Supporting usage of DrawBot, www.drawbot.com # Supporting usage of Flat, https://github.com/xxyxyz/flat # ----------------------------------------------------------------------------- # # ModifyFonts.py # import pagebot from pagebot.contexts import defaultContext as c from pagebot.fonttoolbox.objects.font import Font from pagebot.fonttoolbox.variablefontbuilder import getVariableFont, getConstrainedLocation fontPath = pagebot.getFontPath() f = Font(fontPath + '/fontbureau/AmstelvarAlpha-VF.ttf', install=False) # Now we have a variable font open. print f.axes # Get an instance at a certain location location = getConstrainedLocation(f, dict(wdth=3944 / 10, wght=760 / 10)) instance = getVariableFont(f, location, install=False, cached=False) c.drawPath(instance['H'].path, (100, 100), sx=0.4)
FLOWID2 = MAIN_FLOW + '2' FLOWID3 = MAIN_FLOW + '3' # Get the root path of open source fonts, enclosed in PageBot. ROOT_PATH = pagebot.getRootPath() # Main Variable Font for all text in the magazine. Change this line to build with # another Variable Font. Using Optical Size (opsz), Weight (wght) and Width (wdth) axes. FONT_PATH = ROOT_PATH + '/Fonts/fontbureau/AmstelvarAlpha-VF.ttf' # Open the font, so we can query values that are not available in standard DrawBot functions, # such as stem width, overshoot of roundings, etc. f = Font(FONT_PATH) #print f.axes Uncomment to see the available axes printed. # Pre-calculate instances of locations in the Variable Font. LIGHT72 = getVariableFont(FONT_PATH, dict(wght=0.5, wdth=0.6, opsz=72)) BOOK_LIGHT = getVariableFont(FONT_PATH, dict(wght=0.5, wdth=0.7)) BOOK_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.7, wdth=0.4)) BOOK = getVariableFont(FONT_PATH, dict(wght=0.25, wdth=0)) BOOK_ITALIC = getVariableFont(FONT_PATH, dict(wght=0.25, wdth=1)) MEDIUM = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=0)) SEMIBOLD = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=1)) SEMIBOLD_CONDENSED = getVariableFont(FONT_PATH, dict(wght=0.40, wdth=0.5)) BOLD = getVariableFont(FONT_PATH, dict(wght=0.70, wdth=1)) BOLD_ITALIC = getVariableFont(FONT_PATH, dict(wght=0.7, wdth=1)) shadow = Shadow(offset=(6, -6), blur=10, color=(0.2, 0.2, 0.2, 0.5)) imo = ImageObject() imo.photoEffectTonal() #imo.lineOverlay() #imo.pixellate()