Text examples

Basic typesetting

# basic typesetting

size(600, 600)

myText = """Draws text to the screen. The first parameter sets the string of text to display ("always between quotes"). The following two parameters set the location of the text's baseline. The fourth, fifth and sixth parameter are optional. The fourth parameter specifies a width for text blocks, the fifth the maximum height. Text in a block is wrapped across multiple lines. The alignment for text in a block can be set with the align() command. By default, text is not outlined when saved as a PDF. Optionally, a sixth parameter outline=True can be supplied so text will be outlined."""

# define current font and size
font('Andale Mono', 16)

# lineHeight not documented (skip lineheight, lowercase deprecated)
lineHeight(20)

# define textbox and alignment (it's a keyword argument)
textBox(myText, (50, 50, 400, 400), align='left')

# some useful helper functions:
# get the size of a text as tuple (width, height)
print textSize(myText, align='left')

# get a list of installed fonts
# print installedFonts()

Text within a box

Random font samples

# get n random fonts from the system

translate(100, 100)
n = 7
for i in range(n):
    f = choice(installedFonts())
    print f
    font(f, 81)
    text('handgloves', (0, 0))
    translate(0, 100)

Simple formatted string

size(800, 600)

# make a FormattedString object
txt = FormattedString()

# add some text with formatting
txt.append("hello", font="Helvetica", fontSize=100, fill=(1, 0, 0))

# add some more text with another formatting
txt.append("world", font="Times-Italic", fontSize=115, fill=(0, 1, 0))

# draw the FormattedString on canvas
text(txt, (120, 300))

Automatic pagination

#!/usr/bin/env python
# -*- coding: utf-8 -*-

T = u"""There was a day when designers asked themselves: ‘Why use a computer for design?’. Some argued that a computer was not needed for being a good designer. The important thing was the designer himself/herself. They were right.
Some years later and there is hardly any designer working without a computer. The computer has become the designer’s main tool (sometimes even the only one nowadays).
Software is developed mainly by engineers, not by designers. This makes the designer constrained by the engineers’ thoughts and ideas, not by his/her own. Programming gives the designer more control over his/her tools, and therefore over the design process. It allows one to follow the own workflow and think beyond the resources included in the software.
Probably you don’t need to know how to program to be a better designer. But it might help. And it won’t hurt, for sure."""

x, y = 94, 282
w, h = 800, 600

i = 0
while len(T) > 0:
    newPage(1000, 1000)
    if i % 2 == 0:
        fontSize(60)
        font('Times-Bold')
        fill(1, 0, 0)
        a = 'left'
    else:
        fontSize(52)
        font('Verdana-Bold')
        fill(0, 1, 0)
        a = 'right'
    hyphenation(False)
    lineHeight(70)
    T = textBox(T, (x, y, w, h), align=a)
    text(str(i+1), (66, 42))
    i += 1

Fading paragraph text

# FormattedString example
# make text fade out, letter by letter

myString = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse mol'

# format the string
myText = FormattedString()
alphaStep = 1.0 / (len(myString) - 1)

for i, char in enumerate(myString):
    # progression from white to black: 1 -> 0
    alpha = 1 - (i * alphaStep)
    color = 0, 0, 1, alpha
    myText.append(char, font='Verdana Bold Italic', fontSize=12, fill=color)

# compose the page with the already formatted string
# check if we have overflow, if so create a new page
while myText:
    newPage(150, 160)
    myText = textBox(myText, (20, 10, width()-30, height()-20))