BASIC for the Amiga  Chapter Nine - FONTS by Frank Wilkinson Ed: See the previous installments of this series on back issues of Megadisc. Note that as usual, the accompanying programs in this drawer, referred to below, require AmigaBasic to run them, and they look for a disk called EXTRAS to find AmigaBasic on. If your AmigaBasic disk is called something else, such as EXTRAS 1.3, make a copy of it and rename it to EXTRAS. Then all should be well. N.B.: For this tutorial you must have the following fonts in the Fonts drawer of your system disk: Diamond, Garnet, Ruby, Emerald, Sapphire, and Topaz.  ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| In this chapter we will look at the Diskfont.library and how to access it and how we can make use of it. Like all libraries on the Amiga, to be able to use it with BASIC, there has to be a file in the LIBS drawer on the disk called Diskfont.bmap which contains all the addresses, in ROM, of the routines in that library and also the Registers each function uses. To be able to use the Diskfont.library we will also need the Graphics.library and its .bmap file. There are hundreds of FONTS available, both Commercial and Public Domain, for the Amiga. Accessing these FONTS is always the same. Fonts for the Amiga are known as BIT-MAPPED FONTS. There are other types of FONTS but they are not used by the Diskfont.library and they require special programs to use them. The first thing to do is to understand how a font is constructed. 1. It must have a name. i.e topaz 2. It must have a size. 8, 9, 11 This size tells the Amiga how many screen lines each letter occupies. This size determines how the LOCATE command allocates space on the screen for each line of TEXT. It does not affect the Graphics LINE command. 3. Each font can have more than one size. The 'topaz' has 3 sizes. 4. Each font has a drawer in which all the sizes of that font are kept. 5 Each font has a '.font' file which contains all the information about the font. The first size contains 264 bits of information. If there are 2 sizes in the font, the '.font' is 524 bytes long. This is not meant to teach you how to make fonts and the above points are not the be-all and end-all of fonts. It just serves as a loose method of describing a font. What we are about here is learning how to use the fonts, that have already been created, in BASIC. The routine to use the FONTS in the FONTS drawer on the Boot Disk is as follows:- diskfont.bas routine ___________________________________________________________________________ LIBRARY "diskfont.library" LIBRARY "graphics.library" DECLARE FUNCTION OpenDiskFont& LIBRARY DECLARE FUNCTION OpenFont& LIBRARY DECLARE FUNCTION AskSoftStyle& LIBRARY demo: ' Demonstration of Font Command LOCATE 4,1 Font "Sapphire.font", 19,0,0 PRINT "This is Sapphire 19 Points" PRINT :PRINT FOR de=1 TO 1000:NEXT de Font "topaz.font", 8,0,0 INPUT "Press RETURN to end",dum$ LIBRARY CLOSE END SUB Font(fontName$, height%, style%, prefs%) STATIC SHARED pFont& IF pFont&<>0 THEN CALL CloseFont(pFont&) fontName0$=fontName$+CHR$(0) textAttr&(0)=SADD(fontName0$) textAttr&(1)=height%*65536& + style%*256 + prefs% pFont&=OpenDiskFont&(VARPTR(textAttr&(0))) IF pFont& <> 0 THEN SetFont WINDOW(8),pFont& END SUB SUB SetStyle(mask%) STATIC SHARED enable% SetSoftStyle WINDOW(8),mask%,enable% END SUB ___________________________________________________________________________  CalcLines Program I have included a small program with this tutorial called 'CalcLines'. Type in the Size of the FONT AND the height of the window you wish to use when asked. Then the program will calculate and display the top and bottom screen line of each Text line. As an Example if you chose Sapphire 19 and a screen of 256 you would get this:- Lines Available = 13.47368 Line 1 Top = 0 Bottom = 18 Line 2 Top = 19 Bottom = 37 Line 3 Top = 38 Bottom = 56 Line 4 Top = 57 Bottom = 75 Line 5 Top = 76 Bottom = 94 Line 6 Top = 95 Bottom = 113 Line 7 Top = 114 Bottom = 132 Line 8 Top = 133 Bottom = 151 Line 9 Top = 152 Bottom = 170 Line 10 Top = 171 Bottom = 189 Line 11 Top = 190 Bottom = 208 Line 12 Top = 209 Bottom = 227 Line 13 Top = 228 Bottom = 246 These figures come in very handy when drawing boxes around text or when using the SCROLL command to make text Scroll across the screen or as you will see later. Diagonally or up and down. Now on with the DiskFont program. The first commands we come across are the LIBRARY commands. The diskfont library which is needed to access the FONTS contained in the FONTS drawer on the disk. The Graphics.library enables us to do something with the FONT when we have opened it. LIBRARY "diskfont.library" LIBRARY "graphics.library" DECLARE FUNCTION OpenDiskFont& LIBRARY DECLARE FUNCTION OpenFont& LIBRARY Then we have the program itself. I have designed this program in a slightly different way this time. The Program itself is the Tutorial. And it explains how everything is done as it is RUNNING. I decided that it was better to explain something and then immediately show you the results. It is one thing to talk about Text Lines and Screen Lines, but quite another thing to grasp the idea. Run the program and see what I mean. For now let's look at the 'Sub Font.............Static' as shown in the little program above. __________________________________________________________________________ SUB Font(fontName$, height%, style%, prefs%) STATIC SHARED pFont& IF pFont&<>0 THEN CALL CloseFont(pFont&) fontName0$=fontName$+CHR$(0) textAttr&(0)=SADD(fontName0$) textAttr&(1)=height%*65536& + style%*256 + prefs% pFont&=OpenDiskFont&(VARPTR(textAttr&(0))) IF pFont& <> 0 THEN SetFont WINDOW(8),pFont& END SUB __________________________________________________________________________ This very simple SUB Program was written by Carolyn Scheppner of CBM and is available on your Extras Disk in the BasicDemos Drawer along with the SUB Program SetStyle also written by Carolyn. Between them they are all you need to import and use DiskFonts in BASIC. To call a DiskFont from the FONTS drawer on your System disk all you need is the following command:- Font "sapphire.font",19,0,0 'You can use any Font you like as long as you 'use its correct size. The OpenFont command is really quite flexible. If you don't specify the correct Font size, the OpenFont command will try to find a Font which comes as close as possible to the one you specified. To set the style of that font you use this command:- SetStyle 0 'This is Plain Text SetStyle 1 'This is Underlined SetStyle 2 'This is Bold SetStyle 3 'This is Bold Underlined SetStyle 4 'This is Italic SetStyle 5 'This is Italic Underlined SetStyle 7 'This is Bold Italic Underlined The Styles are '0, 1, 2 and 4'. To get the combination just add the numbers together. There are several small programs in the drawer this time and they all have a purpose. Each one shows a particular way to use the FONTS available to you on the Amiga. You might find the 'ScrollFont.bas' program in this drawer interesting. This program demonstates the use of the 'SCROLL' command as well as 'PTAB()'. The DiskFont.bas program describes in detail how to call a font and shows you how to use it. It also shows you how an 8 point font is constructed and allows you to play at making and using Characters. There is a small routine at the end of the program which shows you how to copy an area of the screen into an Array and then put it on the screen in different places so fast you will be amazed. In the "ScrollFont.bas" program the Font is called in exactly the same way as in the "DiskFont.bas program". The following routines set up an area of the screen and 'SCROLL' it around I have numbered them here to assist in explaining them. Routine 1. This sets up a variable 'a$' and we make it equal to somr text. Then we get the font from the disk. a$=" This is Sapphire.Font" Font "sapphire.font",19,0,0 Here we set up a counter variable and make it equal to one. This variable would allow us to go round and round with our scrolling. But as you will see later I have restricted it to once round the clock mainly because it takes a fair amount of time to just sit and watch the text scoll around the screen. j=1 Here we LOCATE our start position. Don't forget the Font is 19 points high so it will be much further down the screen than if we used 'topaz 8' start: LOCATE 3,1 Then set up a FOR...NEXT loop to make the text scroll across the screen. FOR x=1 TO 359 The PRINT PTAB(x) command will the contents of 'a$' at 1 pixel intervals across the screen. If you look carefully at the text in 'a$' you will notice that the first character is a space. If we didn't have a space here we would leave a trail of "T's" across the screen. PRINT PTAB(x);a$; NEXT x Here we change the colour of the text and print it one more time. COLOR 2,0 PRINT PTAB(360);a$; Now we use the scroll command to move the text down the screen. first we set a counter 'x' so that we can stop the scrolling when we have moved down the screen '56 pixels'. x=0 To make the Scroll command work we have to specify an area of the screen and that is done just like using the line command '(350,14)-(620,200)'. the last 2 parameters are directional indicators. The first one tells the scroll command whether to move left or right and by how many pixels at a time (here it is set to '0') The second one tells it whether to move up or down the screen and by how many pixels at a time (here it is set to '1'. a '-' is move left or up. A positive number moves right or down. again: SCROLL (350,14)-(620,200),0,1 After we have Scrolled 1 pixel we increase the counter by 1 and if the counter does not = 56 we go back and scroll another pixel. x=x+1 IF x<=56 THEN GOTO again Routine 2 Now we PTAB(x) backwards using a '-1' step. After changing the colour again. LOCATE 6,1 COLOR 3,0 FOR x= 360 TO 1 STEP -1 PRINT PTAB(x);a$; NEXT x Change the colour again and print it once more. COLOR 2,0 PRINT PTAB(1);a$; Routine 3 Now we Scroll up. Notice the '-1'. again1: SCROLL (0,200)-(260,15),0,-1 x=x+1 IF x<=56 THEN GOTO again1 Add 1 to our 'j' counter j=j+1 This is where we control the number of times to go round the Screen. By setting the maximum value of 'j' we either go back to the start and do it all again or go to our last routine and finish. IF j>=2 THEN againfin COLOR 3,0 GOTO start Routine 4 Change the colour and print it again Change the font and tell the user it's all over. againfin: LOCATE 3,1:COLOR 3,0 PRINT PTAB(1);a$; PRINT :PRINT :PRINT Font "topaz.font",8,0,0 COLOR 5,0 INPUT " Now we are back to normal (Press ",dum$ If you would like the whole Tutorial up to chapter 10 which comprises the first book, you can obtain the full 2 Disk set direct from me at the address below. The cost for the full set is $15.00 which includes Post and packing. Frank Wilkinson 16 Melbourne Place Alberton SA 5014. Phone (08) 47 1871.  ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 |||