Steve Mumford paints his screen with fonts
Last month I concluded by explaining how to use the ColourTrans module to prepare an anti-aliased palette ready for use in painting characters in a chosen font to the screen. To select a font for use, it's a simple matter of calling the Font_SetFont SWI with RO set to the font handle that was returned to you when you first lodged a request:
int font_handle; /* Setup beforehand */ _kernel_swi_regs in, out; in.r = font_handle; _kernel_swi(Font_SetFont, &in, &out);
Once that's been done, the Font_Paint SWI performs the act of printing to the screen — Ri should point to the address of a character array holding the text to be printed, R3 and R4 should contain the starting x and y coordinates respectively and R2 holds some flags which set, among other things, whether the start co-ordinates should be interpreted as OS units or millipoints.
For our purposes, OS units are fine so bit 4 of R2 is set. That's all we need to do for this simple case — calling Font_Paint will then plot the string to the screen:
_kernel_swi_regs in, out; char sample_text = "Hello World!"; int font_flags = 1 << 4; /* set bit 4 */ int x_coor, y_coor; in.r = (int) sample_text; in.r = font_flags; in.r = (int) x_coor; in.r = (int) y_coor; _kernel_swi(Font_Paint, &in, &out);
Once the font is no longer required — whether that's because the program is being shut down, the user has requested a change of font or the mode has been changed — it's important to inform RISC OS so that the font's information can be cleared from the font cache. It's a simple matter of calling Font_LoseFont with the appropriate font handle:
_kernel_swi_regs in, out; in.r = font_handle; _kernel_swi(Font_LoseFont, &in, &out);
And that's it. However, there are a couple of other things you should bear in mind when printing fonts in the WIMP environment. Firstly, although one particular font might be chosen at the start of the program, there's no guarantee that the same font will still he selected by the time the WIMP returns control so it's important to take this into account.
Using the arguments given above, it would be necessary to re-select the appropriate font at the start of each redraw loop. It's also possible to change font handle and colour, among other things, by including sequences of control codes directly in the text to be printed — more of which later.
Mode changes also cause a problem — if you think hack to last month, you'll remember that the process of generating a font handle requires values for the x and y resolutions of the font. Although we were happy to accept the default values, these defaults aren't the same in different screen modes — if the font is requested in a high-resolution mode and subsequently the resolution of the mode is lowered, the font will appear bigger on screen and will distort and stretch. The simplest solution is to listen out for the WIMP mode change messages, and throw the old font away before requesting a new copy.
The basic calls outlined above are suitable for plotting short strings to the screen, but attempting to write something more demanding — such as a text editor or label printer — with these functions alone would prove tricky. Dealing with line breaks or wrap around requires a method of scanning the string before it's printed to determine its size on screen.
Thankfully, the font manager is capable of a lot more, providing the Font_ScanString and Font_StringBBox SWI calls to perform the above checks - the former can chop a long string into shorter divisions, providing you with information on where to split the string, or where to insert the caret after a mouse click on a text area.
Other useful functions include SWI calls to build a list of fonts available to the user and create a menu from that information, and conversion utilities that allow you to swap between values in millipoints and OS units — certain calls require co-ordinates in one or the other of these units, so a method of converting between them is handy. I'll provide details of these next month, so I hope to see you then.
|Source:||Acorn User - 186 - October 1997|