def createPlaceholder(wdt, hgt, locationOnDisk): dB.newDrawing() dB.newPage(wdt, hgt) dB.fill(.5) dB.rect(0, 0, dB.width(), dB.height()) dB.stroke(1) dB.strokeWidth(10) dB.line((0, 0), (dB.width(), dB.height())) dB.line((0, dB.height()), (dB.width(), 0)) dB.saveImage(f'{locationOnDisk}') dB.endDrawing()
def test_newPage_following(self): drawBot.newDrawing() drawBot.size(400, 500) drawBot.newPage() self.assertEqual(drawBot.width(), 400) self.assertEqual(drawBot.height(), 500) self.assertEqual(drawBot.pageCount(), 2)
def test_newPage_empty_implicit_first_page(self): drawBot.newDrawing() drawBot.rect(100, 100, 200, 200) drawBot.newPage() self.assertEqual(drawBot.width(), 1000) self.assertEqual(drawBot.height(), 1000) self.assertEqual(drawBot.pageCount(), 2)
def draw(self): self.xFactor = self.targetXheight / currentXheight drawBot.newDrawing() drawBot.newPage("LetterLandscape") pageWidth = drawBot.width() drawBot.fill(0, 0, 0, 0.5) drawBot.stroke(0, 0, 0, 1) drawBot.rect(.5 * dpi, .5 * dpi, pageWidth - (1 * dpi), self.targetXheight) drawBot.translate(0.5 * dpi, .5 * dpi) drawBot.scale(self.xFactor) for g in self.text: pen = CocoaPen(f) f[g].draw(pen) drawBot.drawPath(pen.path) drawBot.translate(f[g].width, 0) pdf = drawBot.pdfImage() # set the pdf data in the canvas self.w.canvas.setPDFDocument(pdf)
def drawLetters(self): xFactor = self.xHeightTarget / xHeightCurrent descenderTarget = descender * xFactor pageWidth = drawBot.width() pageHeight = drawBot.height() if self.descBox == 1: drawBot.stroke(1,0,0, 1) drawBot.line((margin, pageHeight-margin-self.xHeightTarget+descenderTarget),(pageWidth-dpi, pageHeight-margin-self.xHeightTarget+descenderTarget)) elif self.descBox == 0: drawBot.stroke(0,0,0,0) drawBot.fill(0,0,0, 1) drawBot.stroke(0,0,0, 0) drawBot.translate(margin, pageHeight-margin-self.xHeightTarget) drawBot.scale(xFactor) for g in self.text: pen = CocoaPen(f) f[g].draw(pen) drawBot.drawPath(pen.path) drawBot.translate(f[g].width, 0) pdf = drawBot.pdfImage() self.w.canvas.setPDFDocument(pdf)
def pageSetup(self): drawBot.newDrawing() drawBot.newPage("LetterLandscape") pageWidth = drawBot.width() pageHeight = drawBot.height() #drawBot.fill(0,0,0, 0.5) drawBot.stroke(0,0,0,.5) drawBot.strokeWidth(0.5) #this needs major clean up drawBot.line((margin, pageHeight-margin), (pageWidth-dpi, pageHeight-margin)) #xHeight line drawBot.line((margin, pageHeight-margin-self.xHeightTarget), (pageWidth-dpi, pageHeight-margin-self.xHeightTarget)) #baseline drawBot.line((margin, pageHeight-margin-(2.5*self.xHeightTarget)), (pageWidth-dpi, pageHeight-margin-(2.5*self.xHeightTarget))) #xHeight line drawBot.line((margin, pageHeight-margin-self.xHeightTarget-(2.5*self.xHeightTarget)), (pageWidth-dpi, pageHeight-margin-self.xHeightTarget-(2.5*self.xHeightTarget))) #baseline drawBot.line((margin, pageHeight-margin-(5*self.xHeightTarget)), (pageWidth-dpi, pageHeight-margin-(5*self.xHeightTarget))) #xHeight line drawBot.line((margin, pageHeight-margin-self.xHeightTarget-(5*self.xHeightTarget)), (pageWidth-dpi, pageHeight-margin-self.xHeightTarget-(5*self.xHeightTarget))) #baseline #drawBot.rect(margin, pageHeight-margin, pageWidth-dpi, -self.xHeightTarget) pdf = drawBot.pdfImage() # set the pdf data in the canvas self.w.canvas.setPDFDocument(pdf)
def test_newPage_empty_multiple(self): drawBot.newDrawing() drawBot.newPage() drawBot.newPage() drawBot.newPage() self.assertEqual(drawBot.width(), 1000) self.assertEqual(drawBot.height(), 1000) self.assertEqual(drawBot.pageCount(), 3)
def startNewPage(pageWidth, pageHeight, margin, header): db.newPage(pageWidth, pageHeight) # Draw the header with db.savedState(): fs = db.FormattedString("%s Glyph Overview\n" % HEADER_STYLENAME, font="Tilt Neon", fontSize=15) fs.append("by Andy Clymer\n%s" % HEADER_URL, font="Tilt Neon", fontSize=8) db.translate(margin, db.height() - header - margin) db.textBox(fs, (0, 0, db.width() - (margin * 2), header))
def test_image(test: unittest.TestCase, path, rect=Rect(300, 300)): img = (renders / path) hash_before = hash_img(img) if img.exists(): img.unlink() test.assertEqual(img.exists(), False) with new_drawing(rect, save_to=img) as (i, r): yield (i, r) test.assertEqual(r.w, db.width()) test.assertEqual(r.h, db.height()) hash_after = hash_img(img) test.assertEqual(hash_after, hash_before) test.assertEqual(img.exists(), True)
def drawText(sf, ma, md, texts): if len(texts) != 0: try: # this depends on Glyphs versions? columnX = texts[0].parent().width + 20 / sf except: columnX = texts[0].parent.width + 20 / sf d.stroke(None) d.fill(1, 0, 0, 1) d.font(".SF Compact Text", 10 / sf) columnText = "" for i, a in enumerate(texts): x, y, wid = a.x, a.y, a.width d.text(str(i + 1), (x, y)) columnText += "%s\t%s\n\n" % (i + 1, a.text) t = d.FormattedString() t.fill(1, 0, 0, 1) t.font(".SF Compact Text", 10 / sf) t.firstLineIndent(-10 / sf) t.tabs((10, "left")) t += columnText columnW = min(250 / sf, (d.width() - margin) / sf - a.layer.bounds[1][0]) d.textBox(t, (columnX, md, columnW, ma - md))
if not os.path.exists(localPath): continue w, h = drawBot.imageSize(path) a = compareImages(localPath, path) padding = 0 if a > 0.0012: padding = 50 pathPadding = 30 drawBot.newPage(w * 4 + padding * 2, h + padding * 2 + pathPadding) drawBot.translate(0, pathPadding) if padding: with drawBot.savedState(): drawBot.fill(None) drawBot.stroke(1, 0, 0) drawBot.strokeWidth(padding * 2) drawBot.rect(0, 0, drawBot.width(), drawBot.height()) drawBot.translate(padding, padding) drawBot.text(f"{os.path.basename(path)} - {os.path.basename(localPath)}", (10, 10 - padding - pathPadding)) drawBot.image(path, (0, 0)) drawBot.image(localPath, (w, 0)) im1 = Image.open(path) im2 = Image.open(localPath) diff = ImageChops.difference(im1, im2) with tempfile.NamedTemporaryFile("wb", suffix=".png") as f: diff.save(f, "png") imDiff = drawBot.ImageObject(f.name) drawBot.image(imDiff, (w * 2, 0))
import drawBot drawBot.size(200, 200) drawBot.text("hello world", (10, 10)) drawBot.fill(1, 0, 0) drawBot.text("foo bar", (10, 30)) drawBot.fill(1, 0, 1) drawBot.stroke(0, 1, 0) drawBot.strokeWidth(4) drawBot.font("Times", 50) drawBot.text("foo bar", (10, 50)) drawBot.fill(None) drawBot.stroke(0, 1, 0) drawBot.strokeWidth(1) drawBot.line((0, 50), (drawBot.width(), 50)) drawBot.stroke(None) drawBot.fill(0, 1, 1) drawBot.fontSize(20) drawBot.text("foo bar", (drawBot.width() * .5, 100), align="right") drawBot.text("foo bar", (drawBot.width() * .5, 120), align="center") drawBot.text("foo bar", (drawBot.width() * .5, 140), align="left")
# from the project folder from HSLdonut import hslDonut ### Variables discs = 16 rings = 22 ringThickness = 5 holeRadius = 45 ### Instructions if __name__ == '__main__': newDrawing() newPage(952, 488) translate(width() * .27, height() * .25) save() for eachDisc in range(discs): with savedState(): scale(1, .65) hslDonut(rings, ringThickness, holeRadius, fixedValue=eachDisc / (discs - 1), isLuminosityConst=True, captions=False) translate(0, 16) restore() translate(width() * .44, 0) save()
def caption(name): typeQualities(12, 12) text(name, (width() / 2, height() - 20), align='center')
def background(clr): fill(*clr) rect(0, 0, width(), height())
def new(layer, totalPages): d.newPage(paperSize) w, h = d.width(), d.height() m = l.associatedFontMaster() d.font(".SF Compact Text", 10) d.text("%s %s" % (layer.parent.name, layer.name), (margin, margin)) d.text("%s/%s" % (d.pageCount(), totalPages - 1), (w - margin, margin), align="right") ma, md, mx, mc = m.ascender, m.descender, m.xHeight, m.capHeight zones = [az.position + az.size for az in m.alignmentZones] + [ma, md] boundsTop, boundsBtm = max(zones), min(zones) sf = float(h - margin * 3) / (boundsTop - boundsBtm) #scalefactor d.scale(sf) wNew = w / sf # scaled paper size d.translate((margin / sf), -boundsBtm + (margin * 2) / sf) # drawing metrics lines d.stroke(0, 0, 0, 0.5) d.strokeWidth(0.5 / sf) d.fill(None) lw = layer.width d.rect(0, md, lw, ma - md) d.line((0, mc), (lw, mc)) # x-height d.line((0, mx), (lw, mx)) # x-height d.line((0, 0), (lw, 0)) # baseline # alignment zones d.stroke(None) d.fill(0.7, 0.3, 0, 0.1) for az in m.alignmentZones: d.rect(0, az.position, lw, az.size) # drawing nodes offcurves = [] smooths = [] sharps = [] for p in layer.paths: smooths += [n for n in p.nodes if n.connection == GSSMOOTH] sharps += [ n for n in p.nodes if n.type != OFFCURVE and n.connection != GSSMOOTH ] offcurves += [n for n in p.nodes if n.type == OFFCURVE] d.stroke(0, 0, 0, 0.2) for n in p.nodes: if n.type == OFFCURVE: if n.nextNode.type != OFFCURVE: d.line((n.x, n.y), (n.nextNode.x, n.nextNode.y)) elif n.prevNode.type != OFFCURVE: d.line((n.prevNode.x, n.prevNode.y), (n.x, n.y)) d.stroke(None) nodeSize = 3 / sf hf = nodeSize / 2 #half d.fill(0, 0, 1, 0.5) for n in sharps: d.rect(n.x - hf, n.y - hf, nodeSize, nodeSize) d.fill(0, 0.7, 0, 0.5) for n in smooths: d.oval(n.x - hf, n.y - hf, nodeSize, nodeSize) d.fill(0, 0, 0, 0.2) for n in offcurves: d.oval(n.x - hf, n.y - hf, nodeSize, nodeSize) # drawing anchors d.stroke(None) d.fill(0.7, 0.25, 0.0, 0.75) nodeSize = 4 / sf hf = nodeSize * 0.7 for a in layer.anchors: # print(a, (ma, md, mc, mx, 0)) if a.y in (ma, md, mc, mx, 0): d.polygon((a.x - hf, a.y), (a.x, a.y - hf), (a.x + hf, a.y), (a.x, a.y + hf), close=True) else: d.oval(a.x - hf, a.y - hf, nodeSize, nodeSize) # glyph outline d.fill(0, 0, 0, 0.3) d.stroke(0, 0, 0, 1) d.drawPath(layer.completeBezierPath) return sf, ma, md
def page(): if db: return Rect((0, 0, db.width(), db.height())) else: raise ImportError( "DrawBot was not found, so `page` cannot be called")
def page_rect() -> Rect: return Rect(db.width(), db.height())
txt += "encaissasses seize cas cane ni nies naine cessasses cessas sens an aise assainissez zen scia sains cannisses sana sic sis seize cannisse assainissez encaissasses encaissiez encaissassiez encens essais aines encaissez encaissassiez casasses scies cas cas incises zinc an ans incisasse sens sise sis canna canisse saines aines cessa assainisses naissais inca encan cannes cassas anis ces encaisse nazies ac assassiniez science aisance sic sciences canne zinc naisses canines cas naine cas encaissiez se saisie assainissez cc encaissais sans incisassiez sain saisissez cessassiez se sic ceci anse ac aines ananas saisissiez nia encaissez aise zizanies saisies nain ci ai cannas nez saisi nias ananas naine ancien cc naissance" # Spacing txt += "\n" * 2 for glyphName in txt.listFontGlyphNames(): txt.appendGlyph("n", "n", glyphName, "n", "n", "space") txt.appendGlyph("e", "e", glyphName, "e", "e", "space") while txt: drawBot.newPage("A4Landscape") txt = drawBot.textBox(txt, (40, 40, drawBot.width()-80, drawBot.height()-100)) # Add the date and name of the font psName = drawBot.font(fontURL) drawBot.font("Arial") drawBot.fontSize(11) drawBot.text(psName, (40, 40)) date = datetime.today().strftime("%d/%m/%Y") drawBot.text(date,(drawBot.width()-40,40), align="right") drawBot.saveImage("specimen.pdf") drawBot.endDrawing()
drawPath() with savedState(): if angle % 10 == 0 and captions: captionRadius = radius + ringThickness rotate(angle) lineQualities(thickness=.5) line((captionRadius - 2, 0), (captionRadius + 2, 0)) typeQualities() text(f'{angle}', (radius + ringThickness + 6, 0)) ### Instructions if __name__ == '__main__': newDrawing() newPage(400, 400) fill(.8) rect(0, 0, width(), height()) translate(width() / 2, height() / 2) hslDonut(rings=8, ringThickness=15, holeRadius=45, fixedValue=.75, isLuminosityConst=False) saveImage('HSL Donut L.pdf') endDrawing()
import drawBot f = CurrentFont() s = CurrentFont().selection fontName = '%s-%s' % (f.info.familyName, f.info.styleName) if s is not None: drawBot.newPage('Letter') drawBot.font(fontName) drawBot.fontSize(200) txt = str(s) for char in "[ ' ] ": txt = txt.replace(char, "") drawBot.textBox(txt, (100, 0, drawBot.width() - 60, drawBot.height() - 120), align="left") drawBot.printImage()
scale = bodyHeightOnPaper / (m.ascender - m.descender) for l in sel: d.newPage(paperSize) # get position and scale right d.translate(margin, margin) d.scale(scale) # font scaled down to paper size d.translate( 0, -m.descender) # move origin point of the paper to include descender # draw metric lines d.stroke(0, 0, 0, 1) # black d.line((0, m.ascender), (l.width, m.ascender)) # draw ascender d.line((0, m.capHeight), (l.width, m.capHeight)) # draw descender d.line((0, m.xHeight), (l.width, m.xHeight)) # draw descender d.line((0, 0), ((d.width() - margin * 2) / scale, 0)) # draw baseline, all the way to the margin d.line((0, m.descender), (l.width, m.descender)) # draw descender d.stroke(None) d.fill(1, 0, 0, 0.75) path = l.completeBezierPath # outlines including paths and components d.drawPath(path) d.newPage(paperSize) # blank page # 4 Typeset text using the glyphs file as if the instance is already installed d.newPage(paperSize) d.fill(0, 0, 0, 1) tempFolder = os.path.expanduser("~/Library/Application Support/Glyphs/Temp") ins = f.instances[
import drawBot f = drawBot.FormattedString() f.fontSize(40) f.font("Helvetica") f.align("left") f.append("left\n") f.align("center") f.append("center\n") f.font("Times") f.align("right") f.append("right\n") _, height = f.size() x, y = drawBot.width() * .25, 200 with drawBot.savedState(): drawBot.stroke(0) drawBot.line((x, 0), (x, 1000)) drawBot.text(f, (x, y)) y += height drawBot.text(f, (x, y), align="left") y += height drawBot.text(f, (x, y), align="center") y += height drawBot.text(f, (x, y), align="right") x, y = drawBot.width() * .75, 200 with drawBot.savedState(): drawBot.stroke(0)
import drawBot drawBot.size(200, 200) drawBot.text("hello world", (10, 10)) drawBot.fill(1, 0, 0) drawBot.text("foo bar", (10, 30)) drawBot.fill(1, 0, 1) drawBot.stroke(0, 1, 0) drawBot.strokeWidth(4) drawBot.font("Times", 50) drawBot.text("foo bar", (10, 50)) drawBot.fill(None) drawBot.stroke(0, 1, 0) drawBot.strokeWidth(1) drawBot.line((0, 50), (drawBot.width(), 50)) drawBot.stroke(None) drawBot.fill(0, 1, 1) drawBot.fontSize(20) drawBot.text("foo bar", (drawBot.width()*.5, 100), align="right") drawBot.text("foo bar", (drawBot.width()*.5, 120), align="center") drawBot.text("foo bar", (drawBot.width()*.5, 140), align="left")