C for Yourself - part 50

Steve Mumford introduces two methods of plotting Draw elements on screen

Having provided a rough outline of a label editing and printing utility last month, this time round I'll investigate the methods of producing lines and shapes, as well as complete Draw files, from within an application. If you're just interested in plotting a simple border or box on the screen, the Draw module is the one you're after - it allows the user to create lines and shapes using a variety of different styles and shading methods. If, on the other hand, you're looking to render a file containing an existing collection of !Draw objects, another module, DrawFile, provides the tools to do the job.

During the implementation of our label-printing application, we will be making use of the calls provided by both these libraries. The simple output of the Draw module will provide us with the outlines to each label and any other lines we need to draw on screen. The DrawFile module allows us to support the importing of bits of !Draw-based clipart into our designs. Both these modules require a certain amount of knowledge about the method RISC OS uses to render elements of a Draw file, and that's what I hope to cover in the next few paragraphs.

The first thing to bear in mind when dealing with Draw is that it uses several different sets of units to store values about the orientation and position of objects - this is due to the fact that fixed point arithmetic is used during the rendering process, and the relative sizes of the units have been chosen to minimise the likelihood of these limitations becoming visible on screen.

As well as using the standard OS units, defined as being 1/180th of an inch, the Draw module requires certain values to be given in Internal Draw units and Transform units. The former are defined as 1/256th of an OS unit and allow greater precision when positioning a path. Transform units, on the other hand, are stored as pairs of two bytes within a word - the top two specify an integer value and the bottom two hold the fraction value. Both these units are used in the transformation matrix, a construct that allows the user to rotate, scale and translate a path as it's plotted.

The transformation matrix is held in a three-by-three grid of numbers, the first two columns of which can be altered to achieve the desired effect. The values a to d allow a system to be scaled or rotated, and are specified in transform units, whereas translations are performed using e and f, given in Internal Draw units. Together, these parameters map an old (x, y) coordinate to the new position (x', y') using the following relationship:

x' = ax + cy + e
y' = bx + dy + f

For simple scaling operations, variables a and d hold the necessary scaling factors; for rotations of an angle theta, the values a to d should store cos(theta), sin(theta), -sin(theta) and cos(theta) respectively. The transform matrix is held in memory as a block of six words, storing the values a to f in that sequence. It crops up in almost all the Draw-related SWI calls, so it makes sense to ensure we know how it's put together. There are two main SWI calls that will be of use to us, and these are Draw_Fill and Draw_Stroke - they both take a pointer to the path they're about to render, more on the format of which later.

Included in the later versions of RISC OS and downloadable from http://www.acorn.com/ftp/, the DrawFile module provides a simple method of plotting the contents of a !Draw file onto the screen. This gives us a quick way of rendering the entire contents of a file in one go. Without this tool we would have to interpret the objects within manually, using the Draw module described above as well as sprite plotting and font drawing techniques.

For the task we're trying to perform (that is, including bits of pre-prepared clipart within our labels) this module gives us just the right level of functionality. That should give you a basic idea of the methods we can use in plotting !Draw files and their elements, and next month I'll look at how we can put these techniques into practice - see you then.


Source: Acorn User - 194 - May 1998
Publication: Acorn User
Contributor: Steve Mumford