TTPRINT - Formatted text drawing
The TURTLETEXT command makes the
turtle draw text using the current turtle font. Along with the
TURTLETEXTSIZE
, TURTLETEXTBASE
and
SETFONT commands, the turtle can
draw text using all fonts that are installed on the computer.
This sample defines the TTPRINT procedure. The procedure accepts a list as input. Each list element is again a list that contains a chunk of text. The first element of the list is the font to use, which is a three-element list of font name, font size, and font attributes. See the SETFONT command for an explanation of the format of a font list.
The procedure uses the TURTLETEXTSIZE
command
to define the size of the bounding rectangle around a drawn text chunk.
The TURTLETEXTBASE
command outputs the offset of the top left corner
to the font’s baseline. The baseline is the line that children use to
write text upon. Letters like “g” fall below the baseline. All chunks
need to be aligned on the baseline to make the text appear as being
printed.
TTPRINT.lgo
; TTPRINT: Print formatted text like a text processor
; Text is printed in chunks; each chunk is a list whose
; first element is the font to use.
TO TTPRINT :LIST
LOCAL "Y
LOCAL "BASE MAKE "BASE 0
LOCAL "SIZE
PU SETH 90
; determine baseline offset
FOREACH :LIST [
(SETFONT FIRST ?)
IF TTBASE > :BASE [MAKE "BASE TTBASE]
]
; print relative to baseline
MAKE "Y YCOR + :BASE
; Print text
FOREACH :LIST [
(SETFONT FIRST ?)
MAKE "SIZE TTSIZE BF ?
SETY :Y + TTBASE
PD TT BF ? PU
FD FIRST :SIZE
]
SETY :Y - :BASE
PD
END
CS
TTPRINT [
[[Arial 16] |Hi World; |]
[[TIMES 12] |the |]
[[TIMES 22 3] |Fat |]
[[TIMES 22 0] |Lady |]
[[TIMES 12] |strikes back.|]
]
Procedure | TTPRINT |
Description | Drawing of formatted text |
Level | Intermediate |
Tags | Fonts, Text drawing, Turtle |