Beispiel #1
0
def useBabelStrings():
    
    for contextId, context in (
            ('DrawBot', DrawBotContext()),
            ('Flat', FlatContext())):
        W, H = pt(1000, 300)
        M = pt(100)
        

        EXPORT_PATH = '_export/UseBabelStrings-%s.pdf' % contextId
        # Create a page and set y on top margin.
        context.newPage(W, H)
        y = H - M
        cs = context.newString('Context: %s' % contextId, style={'textFill': color(0, 0, 1), 'fontSize': 36})
        context.text(cs, (100, y))
        y -= 20

        # Create formatted string, with default settings of font, fontSize and textFill color
        bs = context.newString('This is a formatted BabelString')
        print(bs.__class__.__name__)
        context.text(bs, (100, y))

        # FIXME: solve for Flat.
        # Add string with formatting style dict
        bs += context.newString('\nAdd an other string with color/size format',
            style=dict(textFill=color(1, 0, 0), fontSize=20, leading=em(1.4)))
        print(bs)

        y -= 50

        context.text(bs, (100, y))
        context.saveImage(EXPORT_PATH)
Beispiel #2
0
def getContext():
    global DEFAULT_CONTEXT, MAMP_PATH
    if DEFAULT_CONTEXT is None:
        try:
            #import ForceImportError # Uncomment for simulate testing of other contexts/platforms
            import AppKit  # Force exception on non-OSX platforms
            from pagebot.contexts.drawbotcontext import DrawBotContext
            DEFAULT_CONTEXT = DrawBotContext(
            )  # Test if platform is supporing DrawBot:
            # MampView.build exports in MAMP folder that does not commit in Git.
            MAMP_PATH = '/Applications/MAMP/htdocs/'

        #except (ImportError, AttributeError, ModuleNotFoundError): # Python3
        except (ImportError, AttributeError):
            #import ForceOtherError # Uncomment for simulate testing of other contexts/platforms
            from pagebot.contexts.flatcontext import FlatContext
            DEFAULT_CONTEXT = FlatContext()
            # MampView.build exports in MAMP folder that does not commit in Git.
            MAMP_PATH = '/tmp/MAMP_PATH/'  # TODO: Where is it located for Linux?
        except:
            raise NotImplementedError('Cannot decide on the platform context.')
    return DEFAULT_CONTEXT
Beispiel #3
0
#     HelloCircleSquare.py
#
#     Example to run in plain Python.
#     Dependencies: pabebot, flat, fonttools.
#
#     http://xxyxyz.org/flat#tutorial
#
#     Run as cmd-line:
#     --> python HelloCircleSquare.py

import os

from random import random
from pagebot.contexts.flatcontext import FlatContext

context = FlatContext()
print('Context class is %s' % type(context).__name__)

from pagebot.fonttoolbox.objects.font import findFont

font = findFont('Amstelvar-Roman-VF')

TITLE = 'HelloCircleSquare'
EXPORT_PATH = '_export/%s.pdf' % TITLE

W = H = 500
PAGES = 3
RECTS = 150
R = 20  # Diameter of circle or square
M = 20  # Page margin
Beispiel #4
0
def getFlatContext():
    from pagebot.contexts.flatcontext import FlatContext
    return FlatContext()
    view.showElementInfo = ShowElementInfo  # Show boxes with element info

    return doc  # Answer the doc for further doing.


if __name__ == '__main__':

    context = DrawBotContext(
    )  # May contain NoneDrawBotBuilder if not running on a DrawBot platform
    d = makeDocument(context)
    # Make interactive global controls. Only works in DrawBot context. Otherwise ignored.
    d.context.Variable([
        dict(name='ShowMeasures', ui='CheckBox', args=dict(value=True)),
        dict(name='ShowDimensions', ui='CheckBox', args=dict(value=False)),
        dict(name='ShowElementInfo', ui='CheckBox', args=dict(value=False)),
        dict(name='PageSize',
             ui='Slider',
             args=dict(minValue=100, value=400, maxValue=800)),
    ], globals())

    # Export in _export folder that does not commit in Git. Force to export PDF.
    EXPORT_PATH = '_export/AlignElements_DB.png'
    d.export(EXPORT_PATH)

    # F L A T
    context = FlatContext()
    d = makeDocument(context)
    # Export in _export folder that does not commit in Git. Force to export PDF.
    EXPORT_PATH = '_export/AlignElements_F.pdf'
    d.export(EXPORT_PATH)
#     Supporting usage of Flat, https://github.com/xxyxyz/flat
# -----------------------------------------------------------------------------
#
#     UseBabelStrings.py
#
#     BabelString instances are wrappers around formatted strings,
#     hiding their context. For DrawBot BabelStrings (bs.s) contain
#     OSX/IOS FormattedStrings.
#     For FlexContext, equivalent text-formatted structures are implemented.
#
from pagebot.contexts.drawbotcontext import DrawBotContext
from pagebot.contexts.flatcontext import FlatContext

for contextId, context in (
        #('DrawBot', DrawBotContext()),
    ('Flat', FlatContext()), ):
    W = H = 1000
    M = 100

    EXPORT_PATH = '_export/UseBabelStrings-%s.pdf' % contextId
    # Create a page and set y on top margin.
    context.newPage(W, H)
    y = H - M
    # Create formatted string, with default settings of font, fontSize and textFill color
    bs = context.newString('This is a formatted BabelString')
    print bs.__class__.__name__
    context.text(bs, (100, y))
    # Add string with formatting style dict
    bs += context.newString('\nAdd an other string with format',
                            style=dict(textFill=(1, 0, 0),
                                       fontSize=20,
Beispiel #7
0
# -----------------------------------------------------------------------------
#
#     SierpinskiSquare.py
#
# by Petr van Blokland @petrvanblokland
# https://twitter.com/petrvanblokland/status/860610270410018817
#
#     Run as cmd-line:
#     --> python SierpinskiSquare.py

import os, os.path

from random import random
from pagebot.contexts.flatcontext import FlatContext

context = FlatContext()
print('Context class is %s' % type(context).__name__)
W = H = 500

def drawSierpinskiSquare(px, py, w, maxW):
    if w < 1:
        return
    for x in range(3):
        for y in range(3):
            if x == 1 and y == 1:
                c = max(0, 0.5 - 0.5*w/W)
                context.fill((random(), c, c))
                #print(x, y, w, 0.5*w/W)
                context.rect(px+w, py+w, w, w)
            elif px <= maxW and py <= maxW:
                drawSierpinskiSquare(px+x*w, py+y*w, w/3.0, maxW)
Beispiel #8
0
#     P A G E B O T
#
#     Licensed under MIT conditions
#
#     Supporting usage of DrawBot, www.drawbot.com
#     Supporting usage of Flat, https://github.com/xxyxyz/flat
# -----------------------------------------------------------------------------
#
#     AnimatedGif.py
#
#     Run as cmd-line:
#     --> python AnimatedGif.py

from random import random
from pagebot.contexts.flatcontext import FlatContext

context = FlatContext()
print('Context class is %s' % type(context).__name__)

W = 400
H = 400

context.newDocument(w=W, h=H)

for n in range(1, 50):
    context.newPage(W, H)
    context.fill(random(), random(), random())
    context.rect(random() * W, random() * H, 20, 20)

context.saveImage('test.gif')