Drawing Text

text(txt, (x, y), align=None)

Draw a text at a provided position.

Optionally an alignment can be set. Possible align values are: “left”, “center” and “right”.

The default alignment is left.

Optionally txt can be a FormattedString.

# set a font and font size
font("Times-Italic", 200)
# draw text
text("hallo", (200, 600))
text("I'm Times", (100, 300))
textBox(txt, box, align=None)

Draw a text in a provided rectangle.

A box could be a (x, y, w, h) or a bezierPath object.

Optionally an alignment can be set. Possible align values are: “left”, “center”, “right” and “justified”.

If the text overflows the rectangle, the overflowed text is returned.

The default alignment is left.

# a box has an x, y, width and height
x, y, w, h = 100, 100, 800, 800
# set a fill
fill(1, 0, 0)
# draw a rectangle with variables from above
rect(x, y, w, h)
# set a diferent fill
fill(1)
# set a font size
fontSize(200)
# draw text in a text box
# with varibales from above
overflow = textBox("hallo, this text is a bit to long",
                (x, y, w, h), align="center")
# a text box returns text overflow
# text that did not make it into the box
print(overflow)

The returned overflow can be used to add new pages until all text is set:

t = '''DrawBot is a powerful, free application for MacOSX that invites you to write simple Python scripts to generate two-dimensional graphics. The builtin graphics primitives support rectangles, ovals, (bezier) paths, polygons, text objects and transparency.
DrawBot is an ideal tool to teach the basics of programming. Students get colorful graphic treats while getting familiar with variables, conditional statements, functions and what have you. Results can be saved in a selection of different file formats, including as high resolution, scaleable PDF.
DrawBot has proven itself as part of the curriculum at selected courses at the Royal Academy in The Hague.'''

# setting some variables
# setting the size
x, y, w, h = 10, 10, 480, 480

# setting the color change over different frames
coloradd = .1

# setting the start background color only red and blue
r = .3
b = 1

# start a loop and run as long there is t variable has some text
while len(t):
    # create a new page
    newPage(500, 500)
    # set a frame duration
    frameDuration(3)
    # set the background fill
    fill(r, 0, b)
    # draw the background
    rect(x, y, w, h)
    # set a fill color
    fill(0)
    # set a font with a size
    font("DrawBot-Bold", randint(50, 100))
    # pick some random colors
    rr = random()
    gg = random()
    bb = random()
    # set a gradient as fill
    radialGradient((250, 250), (250, 250), [(rr, gg, bb), (1-rr, 1-gg, 1-bb)], startRadius=0, endRadius=250)

    # draw the text in a box with the gradient fill
    t = textBox(t, (x, y, w, h))

    # setting the color for the next frame
    r += coloradd
    b -= coloradd

    # set a font
    font("DrawBot-Bold", 20)
    # get the page count text size as a (width, height) tuple
    tw, th = textSize("%s" % pageCount())
    # draw the text
    textBox("%s" % pageCount(), (10, 10, 480, th), align="center")

saveImage("~/Desktop/drawbot.mp4")

Another example, this time using a bezierPath as a text envelope:

# create a fresh bezier path
path = BezierPath()
# draw some text
# the text will be converted to curves
path.text("a", font="Helvetica-Bold", fontSize=500)
# set an indent
indent = 50
# calculate the width and height of the path
minx, miny, maxx, maxy = path.bounds()
w = maxx - minx
h = maxy - miny
# calculate the box where we want to draw the path in
boxWidth = width() - indent * 2
boxHeight = height() - indent * 2
# calculate a scale based on the given path bounds and the box
s = min([boxWidth / float(w), boxHeight / float(h)])
# translate to the middle
translate(width()*.5, height()*.5)
# set the scale
scale(s)
# translate the negative offset, letter could have overshoot
translate(-minx, -miny)
# translate with half of the width and height of the path
translate(-w*.5, -h*.5)
# draw the path
drawPath(path)
# set a font
font("Helvetica-Light")
# set a font size
fontSize(5)
# set white as color
fill(1)
# draw some text in the path
textBox("abcdefghijklmnopqrstuvwxyz"*30000, path)

Helpers

textSize(txt, align=None, width=None, height=None)

Returns the size of a text with the current settings, like font, fontSize and lineHeight as a tuple (width, height).

Optionally a width constrain or height constrain can be provided to calculate the lenght or width of text with the given constrain.

textOverflow(txt, box, align=None)

Returns the overflowed text without drawing the text.

A box could be a (x, y, w, h) or a bezierPath object.

Optionally an alignment can be set. Possible align values are: “left”, “center”, “right” and “justified”.

The default alignment is left.

Optionally txt can be a FormattedString. Optionally box can be a BezierPath.

textBoxBaselines(txt, box, align=None)

Returns a list of x, y coordinates indicating the start of each line for a given text in a given box.

A box could be a (x, y, w, h) or a bezierPath object.

Optionally an alignment can be set. Possible align values are: “left”, “center”, “right” and “justified”.

textBoxCharacterBounds(txt, box, align=None)

Returns a list of typesetted bounding boxes ((x, y, w, h), baseLineOffset, formattedSubString).

A box could be a (x, y, w, h) or a bezierPath object.

Optionally an alignment can be set. Possible align values are: “left”, “center”, “right” and “justified”.

installedFonts(supportsCharacters=None)

Returns a list of all installed fonts.

Optionally a string with supportsCharacters can be provided, the list of available installed fonts will be filtered by support of these characters,

installFont(path)

Install a font with a given path and the postscript font name will be returned. The postscript font name can be used to set the font as the active font.

Fonts are installed only for the current process. Fonts will not be accesible outside the scope of drawBot.

All installed fonts will automatically be uninstalled when the script is done.

# set the path to a font file
path = "path/to/font/file.otf"
# install the font
fontName = installFont(path)
# set the font
font(fontName, 200)
# draw some text
text("Hello World", (10, 10))
# uninstall font
uninstallFont(path)

This function has been deprecated: please use the font path directly in all places that accept a font name.

uninstallFont(path)

Uninstall a font with a given path.

This function has been deprecated: please use the font path directly in all places that accept a font name.