def _getValidColor(self, c): u"""Answer the color tuple that is valid for self.fileType, otherwise Flat gives an error.""" # TODO: Make better match for all file types, transparance and spot color import flat if self.fileType in (FILETYPE_JPG, FILETYPE_PNG): return flat.rgb(*c.rgb) #return c.rgb if self.fileType in (FILETYPE_PDF): return flat.rgb(*c.rgb) return flat.rgb(*c.rgb)
def getFlatRGB(self, c): """Answers the color tuple that is valid for self.fileType, otherwise Flat gives an error. TODO: Make better match for all file types, transparency and spot color.""" from flat import rgb return rgb(*to255(c.rgb))
def make_chromachip_png(colour_list, width=600, height=600): """ Use flat to generate and return png data with the colours specified in the colour_list. """ doc_height = height doc_width = width d = document(doc_width, doc_height, 'pt') p = d.addpage() chip_height = doc_height / len(colour_list) for i, row in enumerate(colour_list): chip_width = doc_width / len(row) chip_y = i * chip_height for j, colour in enumerate(row): chip_rgb = rgb(*hex_to_rgb(colour)) colour_thing = shape().nostroke().fill(chip_rgb) p.place(colour_thing.rect(j * chip_width, chip_y, chip_width, chip_height)) return p.image(kind='rgb').png()
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')
import random import wand from flat import rgb, font, shape, strike, document, command r = random red = rgb(255, 0, 0) #lato = font.open('Lato-Reg.otf') figure = shape().stroke(red).width(0.5) #headline = strike(lato).color(red).size(20, 24) d = document(100, 100, 'mm') p = d.addpage() p.place(figure.circle(50, 50, 20)) p.place(figure.circle(50, 50, 20)).position(random.randint(50, 50), random.randint(50, 50)) d.pdf('hello.pdf') from wand.image import Image as wi pdf = wi(filename="hello.pdf", resolution=300) pdfimage = pdf.convert("jpeg") i = 1 for img in pdfimage.sequence: page = wi(image=img) page.save(filename=str(i) + ".jpg") i += 1
# 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')
# ----------------------------------------------------------------------------- # # flatstring.py # # xxyxyz.org/flat # xxyxyz.org/flat import os import re from pagebot.contexts.strings.babelstring import BabelString from pagebot.style import css, LEFT, DEFAULT_FONT_SIZE, DEFAULT_FONT_PATH, DEFAULT_LEADING from pagebot.toolbox.units import upt from flat import rgb DEFAULT_COLOR = rgb(0, 0, 0) class FlatString(BabelString): BABEL_STRING_TYPE = 'flat' UNITS = 'pt' """FlatString is a wrapper around the Flat string.""" def __init__(self, s, context, style=None): """Constructor of the DrawBotString, wrapper around DrawBot.FormattedString. Optionally store the (latest) style that was used to produce the formatted string. >>> from pagebot.contexts.flatcontext import FlatContext >>> context = FlatContext() >>> context.isFlat True
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')
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')