Fancy Text Module

The fancy text module allows to write formated text to a Calico shape object.

The fancy text module allows you to write nicely formated text to a Calico Shape object. It includes the ability to do automatic text wrapping, justification of text, indentation, and inline objects like buttons.

The core of the fancy text module is the writeText function. The writeText functions takes two arguments, a target onto which all the text will be drawn and a list of of strings, shapes, and special commands that indicated what should be drawn. A simple example would be:

win = Window(1000, 1000)
rec = Rectangle((0,0), (500,1000), color=Color("white"))
writeText(rec, ['This is some text.\n'])
win.draw(rec)

Which will write “This is some text.” to a rectangle and draw it to the window. Obviously, this is not the main strength of the fancy text module, drawing a simple piece of text to the screen can be done by Calico as well.

To make use of the full power of the fancy text module, you will have to use some of the special commands that come with the module. There are two types of commands: global commands and local commands.

Global commands allow you to set properties for all text that will follow next. For example, the following code will write the above text in a bigger font:

writeText(rec, [Size(30), "This is some text.\n"])

Local commands allow you to set a property only for a specific piece of text. For example, the following code will write one word in italics:

writeText(rec, ["This ", Italic("is"), " some text.\n"])

To enable line-wrapping after 200 pixels, use the Wrap command:

text = [Wrap(200), "This text is a little bit to long to fit in 200 pixels,"]
text += ["but luckily, it will be wrapped automatically."]
writeText(rec, text)

You can enable justification (filling the width of the wrapped space), by supplying True as the second argument for Wrap:

text = [Wrap(200, True), "This text is a little long to fit in 200 pixels,"]
text += ["but it will be wrapped and justified by the fancy text writer."]
writeText(rec, text)

To change the indentation of the text, simply add the Indent global command:

text =  [Indent(30), "This text is indented.\n"]
text +=  [Indent(0), "This text is not indented.\n"]
writeText(rec, text)

Lastly, to add an inline image or other shape to the text, simply add the shape to the list:

rec = Rectangle((0,0), (20,20))
writeText(rec, "What follows is a rectangle:", rec)

For more options, see the list of global and local commands below.

Summary

Functions

writeText(target, elements[, x_pos_start, ...]) Write the list of elements to the target.

Global Commands

Font(font) Changes the font of all future text.
Size(size) Class to change font size.
Wrap(wrapWidth[, justify]) Enables automatic line wrapping.
SkipH(space) Advances the horizontal cursor position.
SkipV(space) Advances the vertical cursor position.
Indent(space) Indents this and all subsequent text.

Local Commands

Bold(text) Displays the provided text in a bold.
Italic(text) Displays the provided text in italics.
Mono(text) Displays the provided text in a mono-spaced font.
Code(text) Displays the provided text as inline code.
Header(text) Displays the provided text as a header.
Bullet(bullet, text[, indent, parskip]) Creates a bullet point for in a bulleted list.
Offset(text, offset) Moves a shape or piece of text vertically.

Functions

writeText(target, elements, x_pos_start=None, y_pos_start=None)[source]

Write the list of elements to the target.

Parameters:
  • target – The target object to draw to.
  • elemetns – A list of elemetns indicating what, and how to draw.
  • x_pos_start – The x coordinate from where to start writing.
  • y_pos_start – The y coordinate from where to start writing.
Returns:

The final y coordinate.

Global Commands

class Font(font)[source]

Changes the font of all future text.

The fancy text module defines 4 global variables for fonts:
  • FONT_REGULAR
  • FONT_BOLD
  • FONT_ITALIC
  • FONT_MONO
Parameters:font – string indicating the font to switch to.
class Size(size)[source]

Class to change font size.

Parameters:size – integer representing the new fontsize.
class Wrap(wrapWidth, justify=True)[source]

Enables automatic line wrapping.

Parameters:
  • wrapWidth – after how many pixels the line should be wrapped.
  • justify – should the text be justified.
class SkipH(space)[source]

Advances the horizontal cursor position.

Parameters:space – the distance to skip.
class SkipV(space)[source]

Advances the vertical cursor position.

Parameters:space – the distance to skip.
class Indent(space)[source]

Indents this and all subsequent text.

Parameters:space – the distance this text should be indented.

Local Commands

class Bold(text)[source]

Displays the provided text in a bold.

Parameters:text – the text to be displayed.
class Italic(text)[source]

Displays the provided text in italics.

Parameters:text – the text to be displayed.
class Mono(text)[source]

Displays the provided text in a mono-spaced font.

Parameters:text – the text to be displayed.
class Code(text)[source]

Displays the provided text as inline code.

Parameters:text – the text to be displayed.
class Header(text)[source]

Displays the provided text as a header.

Parameters:text – the text to be displayed.
class Bullet(bullet, text, indent=30, parskip=4)[source]

Creates a bullet point for in a bulleted list.

Parameters:
  • bullet – the text or object to be used as a bullet.
  • text – the text to display with this shorthand.
  • indent – the size of indent to go with the bullet.
class Offset(text, offset)[source]

Moves a shape or piece of text vertically.

Parameters:
  • text – the text (or shapes) to be displaced.
  • offset – the distance by which the text should be moved.