Vector Graphics and BASIC
Because a Draw file consists of blocks of data in a standard format, any application may use suitable source data as the basis of Draw graphics. The application itself may have no pretensions lo being a graphics application. It might be, for example, a financial program which creates a graph of statistical trends in Draw format. Or a process control program may use vector graphics as the basis of a mimic diagram. The graphics not only appear on screen but can also be saved as a Draw file for printing or incorporation into DTP documents.
Applications which create Draw files in this way need not be highly sophisticated. ARM code or C are not necessary; BASIC is quite sufficient as DrawAid from Carvic Manufacturing demonstrates. And BASIC applications can also display Draw files imported from other sources. This chapter includes a short listing of routines that will display the path objects in a Draw.
DrawAid- Vector Graphics in BASIC
DrawAid from Carvic Manufacturing allows the user's own BASIC applications to create Draw files. This facility is especially useful when creating certain types of drawing which are extremely repetitive or tedious or require accurate calculation. Examples include drawing repeating patterns (Figure 7.1) or the teeth on a gear wheel, plotting the points on a graph and creating a pie chart with many segments. This description is based on version 1.05.
Essentially DrawAid provides a library of BASIC procedures which not only draw the graphics on the screen but also create the necessary data structure which ultimately can be saved as a Draw file. A front end is provided which makes access to the RISC OS BASIC Editor more user friendly (RISC OS 3 users may find it easier to use the BASIC handling facilities in Edit) and a blank BASIC 'shell' into which the user's own code may be inserted. This program runs outside but from the desktop environment and so can be used with confidence by programmers familiar with BASIC even if they do not understand the niceties of WIMP programming. Provided on the disc are numerous examples which demonstrate the system in action.
Also provided is a 'vector font'. An attractive alternative to the system font, this consists entirely of path objects, but unlike characters in outline fonts each stroke consists of a single path having a finite (but adjustable) width. Consequently text displayed in the vector font can be scaled and rotated like any other path object. Procedures are provided which display strings of text in the vector font in various sizes and orientations. It is ideal for use in labelling diagrams, especially when the labelling needs to be oriented out of the horizontal, e.g. the vertical axes of graphs. The vector font is also miserly on memory compared with text converted from outline fonts.
The procedures provided in the DrawAid library fall into four categories relating to standard objects, path objects, text objects and groups. By default all dimensions are in mm, but the user may select from a range of metric or Imperial units and the screen may be scaled to suit.
Standard objects are ready-made complete path objects. The choice includes line (single segment), Bezier curve (single segment), arrow (forwards), arrow (backwards), triangle, rectangle, polygon, arc, sector (i.e. a shape like a slice of a circular pie), segment (an arc and its chord), circle and ellipse. Each procedure takes a number of arguments which determine the size, location, line thickness and colours of the object. Usefully the desktop colours can be entered by names such as grey3 and light^green which saves the user from having to look them up.
The path object facilities allow any path object to be built up segment by segment just as in Draw itself-indeed in the same sequence that you would do it in Draw. Facilities allow for the starting, ending and closing of paths, the creation of line, curve and move segments, the location, rotation, mirroring and scaling of path objects, including applying separate scale factors to the x- and y-axes. Facilities are provided for shearing the paths, that is slewing one axis only.
At present, text handling facilities are limited to the creation of text objects using the system font or the vector font. Those using the system font can be produced in only one size and horizontally. Those using the vector font, however, can be produced at any size (height is specified in mm rather than the customary points), any weight (i.e. line thickness), although four named weights are provided (thin, light, medium and bold), any style (i.e. inclination from the vertical), although two named styles, Regular and Oblique, are provided and any angle, this referring to the orientation of the theoretical line on which the text sits.
The grouping facilities are limited. Two procedures are provided, PROC_new_group and PROC_end_group. After PROC_new_group has been executed all new objects created will be grouped together until PROC_end_group is executed. In addition, all objects are collected into a single group in the final Drawfile.
Version 3.33 of DrawAid is included in the Software directory of this issue of RISCWorld.
Running the BASIC program
The BASIC program is run from the desktop. It makes three passes through the drawing definition (recalling assemblers which usually require more than one pass). The first pass checks the format and syntax and reports any fatal errors. The second pass creates the data block in Draw format and the third pass saves the data as a Draw file.
Displaying Drawfiles in BASIC programs
It is not complicated to display path objects from Draw files from within BASIC, although it is not quite so simple as displaying sprites (see Appendix 2). The drawing must be loaded into a reserved area of memory and a pointer set to the first object. Objects must be tested for type and sent to the appropriate routines in the modules that make up the operating system. At the end of each call the end of the file must be tested for.
Listing 7.1 is a simple BASIC program that prompts for the filename of a Draw file, loads it and displays the path objects in it. Note that in its present form it is limited to files no longer than 32 Kbytes (although this is easily changed). It displays only path objects, whether alone or grouped; the objects are displayed at whatever locations they occupied when the Draw file was saved. Other types of object (font lists, text objects, sprites, text areas and draw options) are simply ignored.
Listing 7.1-a simple BASIC program for displaying Draw path objects REM This program displays path objects in Draw files REM It skips over other object types DIM file% 32*1024 : REM You can expand this as necessary DIM buffer 255 INPUT "Enter filename"- filename$ SYS "OS_File",&FF,filename$,file% TO ,,,,filelength% CLS PROCDraw END DEF PROCDraw pointer%=40 REPEAT objecttype%=file%!pointer% IF objecttype%=2 PROCPath IF objecttype%=6 pointer%+=36 ELSE pointer%+=(file%!(pointer%+4)) UNTIL pointer%>=filelength% ENDPROC DEF PROCPath fillcol%=(file%(pointer%+24)) outlinecol%=(file%!(pointer%+28)) linewidth%=(file%!(pointer%!+32)) !buffer%=8 buffer%!4=&20000 IF fillcol%<>l THEN SYS"ColourTrans_SetGCOL",fillcol%,,,0,0 SYS"Draw_Fill",file%+pointer%+40,&32,0,0 ENDIF IF outlinecol%<>-l THEN SYS"ColourTrans_SetGCOL",outlinecol%,,,0,0 SYS"Draw_Stroke",file%+pointer%+40,&3A,0,0,linewidth%,buffer%,0 ENDIF ENDPROC