Tutorial - Text Blocks

As well as geometric shapes, it is possible to write text to the graphics output. There are three text writing routines and we will start with the basic TextBlock() routine.


The text block routine act like a shape. It takes parameters and a graphics context and writes to the graphics output file. The format of the call is shown below :-

Shape TextBlock(Text text, Number width = 0, EHorizAlign halign = Left, EVertAlign valign = Top)

Here is a simple example :-

	TextBlock("Simple line of text") => { 120, 100 };

In the following example I have drawn a cross at the target coordinate to highlight the alignment.

text_block_1.grs Output of text_block_1.grs text_block_1.png

This will place the text as a single line at location {120,100} relative to the top of the page ( because we have not shifted the origin ). As we have not specified any of the alignment parameters the default is to set the top right corner of the text at the specified location.

As the default width of zero was used all the text is placed on a single line. If we specify a width in pixels the text will wrap to keep it within bounds. I am also setting the alignment to centre to demonstrate other alignments.

TextBlock("A longer line of text that should wrap", 100, Centre, Centre)  => { 120, 100 };
text_block_2.grs Output of text_block_2.grs text_block_2.png

The colour of the text is determined by the colour of the pen being used.

TPen redpen = Pen { colour -> {100, 0, 0} };
[= . . . =]
TextBlock("Some text in red", 0, Centre, Bottom) => redpen;
text_block_3.grs Output of text_block_3.grs text_block_3.png