def layout(author, title, *paragraphs): regular = font.open("/home/macrico/FONTS/Inknut-Antiqua/TTF-OTF/InknutAntiqua-Regular.ttf") bold = font.open("/home/macrico/FONTS/Inknut-Antiqua/TTF-OTF/InknutAntiqua-Bold.ttf") body = strike(regular).size(12, 16) headline = strike(bold).size(12, 16) story = text( body.paragraph(author), headline.paragraph(title), body.paragraph(""), *[body.paragraph(p) for p in paragraphs]) doc = document(148, 210, "mm") page = doc.addpage() block = page.place(story).frame(18, 21, 114, 167) while block.overflow(): page = doc.addpage() block = page.chain(block).frame(18, 21, 114, 167) return doc
def advancefor(f, ch): return f.advances[f.charmap[ord(ch)]] def combine_path(f, s): text_paths = [] cx = 0 for ch in s: glyph_path = glyphcommands(f, ch).translate(cx, 0) text_paths.append(glyph_path) cx += advancefor(f, ch) combined = Path(list(chain(*text_paths))) return combined f = font.open("./NotoSans-Regular.ttf") def generate_path(text, p_sub): fsize = 14 + int(10 * p_sub) factor = fsize / f.density glyph_path = combine_path(f, text).scale(factor) pts = [] for cmd in glyph_path: if type(cmd) in (lineto, curveto, quadto): pts.append([cx + cmd.x / 2, cy + cmd.y / 2]) if type(cmd) != type(closepath): cx = cmd.x cy = cmd.y return pts
def testFlatContext(): context = getContext('Flat') # PageBot implementation, to be used for strings and styles. pagebotFont = findFont(FONTNAME) pagebotFill = color((180.0 / 255, 0, 125.0 / 255)) pagebotStroke = color(100.0 / 255, 180.0 / 255, 0) # Native flat implementation of fonts and colors. flatFont = font.open(pagebotFont.path) flatFill = rgb(180, 0, 125) flatStroke = rgb(100, 180, 0) # Stroke width is the same. strokeWidth = 1 ''' Creates a document. ''' # Creating a Flat document. flatDoc = document(WIDTH, HEIGHT, 'pt') flatPage = flatDoc.addpage() # Pagebot equivalent. context.newDrawing(WIDTH, HEIGHT) pbPage = context.newPage() print(pbPage) ''' Draws a figure. ''' # Flat. #figure = shape().fill(flatFill).stroke(flatStroke).width(strokeWidth) #r = figure.rectangle(50, 50, 20, 20) #p.place(r) # Pagebot. #context.fill(pagebotFill) #context.stroke(pagebotStroke) #context.strokeWidth(strokeWidth) #context.rect(50, 50, 20, 20) #print(p.items[0].item.style.width) #print(context.pages[0].items[0].item.style.width) #s = context.pages[0].items[0] #print(s.item.style.fill) #print(s.item.style.stroke) #print(s.item.style.join) #print(s.item.style.limit) ''' Draws text. ''' msg = 'Hello world!' # Flat. header = strike(flatFont).color(flatStroke).size(FONTSIZE, LEADING, units='pt') t = header.text(msg) placedText = flatPage.place(t).frame(100, 100, 380, 80) # Pagebot. style = dict(font=pagebotFont, fontSize=FONTSIZE, textFill=pagebotStroke, leading=LEADING) bs = context.newString(msg, style=style) context.text('bla', (50, 100)) # TODO: also for native flat. context.text(bs, (100, 100)) #print(headline.style.size) #print(headline.style.leading) #print(headline.style.color.r) #print(headline.style.color.g) # Now for conditions and elements. c = (Left2Left(), Fit2Right(), Float2Top()) style = dict(fontSize=14, font=pagebotFont) msg = 'Testing textBox' print(msg) bs = context.newString(msg, style=style) print(type(bs)) newTextBox(bs, font=pagebotFont, parent=pbPage, conditions=c, fill=0.9, margin=4) #print(p.items) ''' Exports file. ''' im = flatPage.image(kind='rgb') # TODO: #imagePath = getResourcesPath() + '/images/peppertom_lowres_398x530.png' #size = context.imageSize(imagePath) #print(size) if not os.path.exists('_export'): os.mkdir('_export') #print('Exporting native') flatDoc.pdf('_export/native-flat.pdf') #im.png('_export/native-flat.png') #im.jpeg('_export/native-flat.jpg') #p.svg('_export/native-flat.svg') #print(context.doc) context.saveDrawing('_export/pagebot-flat.pdf')
# Only runs under Flat from flat import rgb, font, shape, strike, document #c = rgb(255, 0, 0) c = rgb(0, 0, 0) #lato = font.open('/Library/Fonts/Verdana.ttf') lato = font.open('/Library/Fonts/Upgrade-Middle.ttf') figure = shape().stroke(c).width(2.5) headline = strike(lato).color(c).size(80, 96) d = document(400, 200, 'mm') p = d.addpage() p.place(figure.circle(50, 50, 20)) t = headline.text(u'Hello world! AVT.TeYAYeYé') p.place(t).frame(10, 10, 380, 80) im = p.image(kind='rgb') im.png('_export/hello.png') im.jpeg('_export/hello.jpg') p.svg('_export/hello.svg') d.pdf('_export/hello.pdf')
def testFlat(): context = getContext('Flat') pagebotFont = findFont(FONTNAME) flatFont = font.open(pagebotFont.path) flatFill = rgb(180, 0, 125) pagebotFill = color((180.0 / 255, 0, 125.0 / 255)) flatStroke = rgb(100, 180, 0) pagebotStroke = color(100.0 / 255, 180.0 / 255, 0) strokeWidth = 1 ''' Creates a document. ''' # Flat. doc = document(WIDTH, HEIGHT, 'pt') p = doc.addpage() # Pagebot. context.newDocument(WIDTH, HEIGHT) context.newPage() ''' Draws a figure. ''' # Flat. figure = shape().fill(flatFill).stroke(flatStroke).width(strokeWidth) r = figure.rectangle(50, 50, 20, 20) p.place(r) # Pagebot. context.fill(pagebotFill) context.stroke(pagebotStroke) context.strokeWidth(strokeWidth) context.rect(50, 50, 20, 20) ''' print(p.items[0].item.style.width) print(context.pages[0].items[0].item.style.width) ''' s = context.pages[0].items[0] ''' print(s.item.style.fill) print(s.item.style.stroke) print(s.item.style.join) print(s.item.style.limit) ''' ''' Draws text. ''' msg = 'Hello world!' # Flat. headline = strike(flatFont).color(flatStroke).size(FONTSIZE, LEADING, units='pt') t = headline.text(msg) entity = p.place(t) entity.frame(100, 100, 380, 80) # Pagebot. style = dict(font=pagebotFont, fontSize=FONTSIZE, color=pagebotStroke, leading=LEADING) bs = context.newString(msg, style=style) context.text('bla', (50, 100)) # TODO: also for native flat. context.text(bs, (100, 100)) ''' print(headline.style.size) print(headline.style.leading) print(headline.style.color.r) print(headline.style.color.g) print(headline.style.color.b) ''' ''' Exports file. ''' im = p.image(kind='rgb') #print(p.items) # TODO: #imagePath = getResourcesPath() + '/images/peppertom_lowres_398x530.png' #size = context.imageSize(imagePath) #print(size) if not os.path.exists('_export'): os.mkdir('_export') #print('Exporting native') doc.pdf('_export/native-flat.pdf') ''' im.png('_export/native-flat.png') im.jpeg('_export/native-flat.jpg') p.svg('_export/native-flat.svg') ''' print(context.doc) context.saveDocument('_export/pagebot-flat.pdf')
eli_heuer_txt = "Captures d'écran du compte Twitter \nd'Eli Heuer du 1er avril au 26 juin 2020 (Twitter.com/eliheuer)\n© Eli Heuer/Twitter." typeface_as_program_txt = "Impression de la première version du caractère Programme sur une presse typographique, par David Keshavjee \net Julien Tavelli, 2009 (Optimo.ch/typefaces/programme)\n© David Keshavjee et Julien Tavelli." osp_txt = "Création typographique sur FontForge par Open Source Publishing pour la communication 2020-2021 \ndu théâtre Balsamine, 2020 (Osp.kitchen/work/balsamine.2020-2021)\n© OSP/Free Art License version 1.3/Attribution ShareAlike Creative Commons BY-SA." catalogtree_txt = "Dispositif et prototypage pour la réalisation du projet *Structured Light* par Catalogtree, 2010 (Catalogtree.net/projects/structured_light)\n© Catalogtree." jurg_lehni_txt = "*Hektor Draws a Landscape* & *Hektor Titles a Show*, « Lee 3 Tau Ceti Central Armory Show », Villa Arson, Nice, Villa Arson, Nice, 2003 et « Design and the Elastic Mind », MoMA New York, Jürg Lehni, 2008. (Juerglehni.com/works/hektor)\n© Jürg Lehni & Uli Franke, 2002/Jürg Lehni & Alex Rich, 2003." explication_txt = "La mise en page de ce poster a été réalisée selon les principes exposés dans l'article au recto, \ en utilisant trois bibliothèques du langage Python pour la typographie et le traitement des images : \ \nLa première bibliothèque est une version alpha de Drawbot, adaptée à la librairie graphique Skia \ pour une utilisation sur différents systèmes d'exploitation par Just Van Rossum (pypi.org/project/drawbot-skia). \ \nLa deuxième bibliothèque, Flat (xxyxyz.org) de Juraj Sukop, ne semble toutefois plus maintenue depuis 2018. \ \nLa troisième bibliothèque, Pillow, est très utilisée (pillow.readthedocs.io) dans le domaine du traitement d'image. \ \nLe code qui a permis la réalisation de ce poster est disponible à l'adresse : github.com/jeremien/maj.poster." regular = font.open('font/PlantinMTProRg.TTF') bold = font.open('font/Erbarre-Bold.otf') body = strike(regular).size(10) titre = strike(bold).size(13) d = document(doc_width, doc_height, 'pt') p = d.addpage() s = shape() def setImages(folder: str) -> list: images = [] path = base_path + folder + '/' with os.scandir(path) as entries: for entry in entries: try: if entry.is_file():
def testFlat(): context = getContext('Flat') f = findFont(FONTNAME) ff = font.open(f.path) c1 = rgb(180, 0, 125) c2 = rgb(100, 180, 0) style = dict(font=f, fontSize=FONTSIZE, color=c2, leading=LEADING) # Creates the document. d = document(WIDTH, HEIGHT, 'mm') #print(d.width) #print(d.height) p = d.addpage() #print(p.width) context.newDocument(WIDTH, HEIGHT) #print(context.doc.width) #print(context.doc.height) context.newPage() print(context.pages[0].width) print(context.pages[0].height) figure = shape().fill(c1).stroke(c2).width(2.5) r = figure.rectangle(50, 50, 20, 20) print(r) p.place(r) context.fill((180, 0, 125)) context.stroke((100, 180, 0)) context.strokeWidth(2.5) context.rect(50, 50, 20, 20) #print(p.items[0].item.style.width) #print(context.pages[0].items[0].item.style.width) s = context.pages[0].items[0] #print(s.item.style.fill) #print(s.item.style.stroke) #print(s.item.style.join) #print(s.item.style.limit) headline = strike(ff).color(c2).size(FONTSIZE, LEADING) t = headline.text('Hello world!') entity = p.place(t) entity.frame(10, 10, 380, 80) bs = context.newString('Hello world!', style=style) #print(bs.__class__.__name__) context.text(bs, (10, 10)) ''' print(headline.style.size) print(headline.style.leading) print(headline.style.color.r) print(headline.style.color.g) print(headline.style.color.b) ''' im = p.image(kind='rgb') #print(p.items) # TODO: #imagePath = getResourcesPath() + '/images/peppertom_lowres_398x530.png' #size = context.imageSize(imagePath) #print(size) if not os.path.exists('_export'): os.mkdir('_export') print('Exporting native') d.pdf('_export/native-flat.pdf') im.png('_export/native-flat.png') im.jpeg('_export/native-flat.jpg') p.svg('_export/native-flat.svg') print('Exporting pagebot') context.saveDocument('_export/pagebot-flat.pdf') context.saveDocument('_export/pagebot-flat.png') context.saveDocument('_export/pagebot-flat.jpg') context.saveDocument('_export/pagebot-flat.svg')