-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1-centerText.py
37 lines (30 loc) · 1.07 KB
/
1-centerText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# use textSize() and fontXHeight() to center a text block
# what are we typing
myString = 'Hello there!'
# set our font parameters
font("Verdana")
fontSize(100)
# get the text dimensions
textWidth, textHeight = textSize(myString)
# get the difference between the text width and the page width
# if we divide that in 2, the margin will be equal on both sides
widthDifference = width()-textWidth
xoffset = widthDifference/2
# do the same thing with the height, but calculate it based on the xHeight
heightDifference = height()-fontXHeight()
yoffset = heightDifference/2
# draw guidelines to show the box we are centering
# do this within a saved state so that these formatting options aren’t saved
with savedState():
# set red stroke
stroke(1, 0, 0)
# left side
line((xoffset, 0), (xoffset, height()))
# right side
line((width()-xoffset, 0), (width()-xoffset, height()))
# text baseline
line((0, yoffset), (width(), yoffset))
# text x-height
line((0, height()-yoffset), (width(), height()-yoffset))
# draw our text
text(myString, (xoffset, yoffset))