home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Graphics Amiga C Club */
- /* Chapter: Include Fonts Tulevagen 22 */
- /* File: PrintFont.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-05-01 */
- /* Version: 1.10 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
-
-
- /****************************************************************/
- /* */
- /* PrintFont loads a specified diskfont and prints the complete */
- /* definition of it in 'C'. This printout can be included and */
- /* compiled with your own programs, and you will then be able */
- /* to use the font without having to bother about loading it. */
- /* Perfect for smaller utilities and games! */
- /* */
- /* If you wite a large program that is using a lot of different */
- /* fonts it is best to load these fonts as normal, and not */
- /* include all of them with help of this utility. The advantage */
- /* with normal diskfonts is that several programs can use the */
- /* same fonts, and thus a lot of memory is saved. If the font */
- /* is included together with the program code, no other program */
- /* can use that font. However, for small utilities and games it */
- /* is better to include the font rather than messing around */
- /* with external files that may not exist. */
- /* */
- /* Many thanks to Michael Loughman who told me about his */
- /* excellent idea to make a program that converts fonts into */
- /* normal 'C' code. (What should we include next?) */
- /* */
- /* */
- /* I N S T R U C T I O N S */
- /* ----------------------- */
- /* */
- /* PrintFont converts disk fonts to 'C' code. The font may be */
- /* of any size, proportional or non-proportional, and in any */
- /* style. PrintFont does not handle coloured fonts, although */
- /* this will soon be changed. If the font is non-proportional */
- /* (fixed size), PrintFont will write two arrays, one for the */
- /* font data (graphics) and one which contains information */
- /* about where each character is in the font data, and how wide */
- /* each bit definition of the character is. After these two */
- /* arrays a complete pre-initialized TextFont structure is */
- /* written. */
- /* */
- /* If the font is proportional (variable size), PrintFont will */
- /* add two more arrays. The first array contains the width of */
- /* the box in which each character is printed, while the second */
- /* array contains the kerning data for each character. (The */
- /* kerning value is the number of pixels each character is */
- /* moved from the left left side of the box.) */
- /* */
- /* You need to tell PrintFont three things. First you must of */
- /* course tell it which font yu want to convert. Secondly which */
- /* size of the font you want. (PrintFont will try to load the */
- /* font which best matches your requirements.) Finally you need */
- /* to tell it what name the arrays and structure should have. */
- /* (The name will be added to all arrays and the structure. For */
- /* example, if you set the name to "Nice", the font data array */
- /* will then be called "NiceData" and the structure "NiceFont" */
- /* and so on...) */
- /* */
- /* This is how you call PrintFont: (without quotations) */
- /* */
- /* PrintFont > "file.c" "font" "size" "name" */
- /* */
- /* "file.c": The name of the file where all 'C' code will be */
- /* stored. Note the "arrow" in front of the name, it */
- /* tells DOS that all output should be stored in the */
- /* file "file.c". */
- /* */
- /* "font": The name of the font you want to convert. */
- /* */
- /* "size": The size (number of points) of the font you want */
- /* to convert. */
- /* */
- /* "name": The name of the structure and arrays. */
- /* */
- /* For example. To convert the font "Times" of the size 18 */
- /* points to 'C' code, do like this: (The 'C' code will be */
- /* stored in file "FontFile.c", and the TextFont structure */
- /* will be called "NewsletterFont".) */
- /* */
- /* PrintFont > FontFile.c Times 18 Newsletter */
- /* */
- /* */
- /* Once the font has been converted you can immediately start */
- /* to use it. To change the RastPort's font to your new */
- /* "NewsletterFont", simply call the function SetFont(). */
- /* */
- /* Synopsis: error = SetFont( rast_port, font ); */
- /* */
- /* error: (long) If SetFond could not change the RastPort's */
- /* font it returns a non zero value. If it could */
- /* change font successfully it returns 0. */
- /* */
- /* rast_port: (struct RastPort *) Pointer to the RastPort which */
- /* should use the new font. */
- /* */
- /* font: (struct TextFont *) Pointer to the font you have */
- /* converted (or opened as usual). */
- /* */
- /* error = SetFont( &my_rast_port, &NewsletterFont ) */
- /* if( error ) */
- /* printf( "Could not change font!\n" ); */
- /* */
- /****************************************************************/
-
-
- #include <intuition/intuition.h>
-
-
- struct Intuition *IntuitionBase = NULL; /* Running under Intuition. */
- struct GfxBase *GfxBase = NULL; /* Move() and Text(). */
- struct Library *DiskfontBase = NULL; /* OpenDiskFont() etc. */
-
-
-
- /* The new font's attributes: */
- struct TextAttr font_attr;
-
- /* Pointer to our new font: */
- struct TextFont *font = NULL;
-
- /* Declare a pointer to a Window structure: */
- struct Window *my_window = NULL;
-
- /* Declare and initialize your NewWindow structure: */
- struct NewWindow my_new_window=
- {
- 0, /* LeftEdge x position of the window. */
- 12, /* TopEdge y positio of the window. */
- 500, /* Width 500 pixels wide. */
- 0, /* Height Will be changed. */
- 0, /* DetailPen Text should be drawn with colour reg. 0 */
- 1, /* BlockPen Blocks should be drawn with colour reg. 1 */
- NULL, /* IDCMPFlags No IDCMP flags. */
- SMART_REFRESH| /* Flags Intuition should refresh the window. */
- WINDOWDRAG| /* Drag gadget. */
- WINDOWDEPTH| /* Depth arrange Gadgets. */
- ACTIVATE, /* The window should be Active when opened. */
- NULL, /* FirstGadget No Custom gadgets. */
- NULL, /* CheckMark Use Intuition's default CheckMark. */
- "PrintFont V1.0 Anders Bjerin Amiga C Club", /* Title */
- NULL, /* Screen Connected to the Workbench Screen. */
- NULL, /* BitMap No Custom BitMap. */
- 0, /* MinWidth No sizing gadget. */
- 0, /* MinHeight -"- */
- 0, /* MaxWidth -"- */
- 0, /* MaxHeight -"- */
- WBENCHSCREEN /* Type Connected to the Workbench Screen. */
- };
-
-
-
- /* Declare our functions: */
- void main( int argc, char *argv[] );
- void clean_up( STRPTR );
- void PrintFont( STRPTR name );
- void PrintFontData( STRPTR name );
- void PrintFontLoc( STRPTR name );
- void PrintFontSpace( STRPTR name );
- void PrintFontKern( STRPTR name );
- void PrintFontStructure( STRPTR name );
-
-
-
- void main(
- int argc,
- char *argv[]
- )
- {
- /* Store the name of the font here: (25 char + NULL. */
- /* Font name plus extension ".font") */
- char font_name[ 26 ];
-
-
-
- /* Open the necessary libraries: */
-
- /* Open the Intuition Library: */
- IntuitionBase = (struct IntuitionBase *)
- OpenLibrary( "intuition.library", 0 );
- if( !IntuitionBase )
- clean_up( "Could not open Intuition library!" );
-
- /* Open the Graphics library: */
- GfxBase = (struct GfxBase *)
- OpenLibrary( "graphics.library", 0 );
- if( !GfxBase )
- clean_up( "Could not open Graphics library!" );
-
- /* Open the DiskFont library: */
- DiskfontBase = (struct DiskfontBase *)
- OpenLibrary( "diskfont.library", 0 );
- if( !DiskfontBase )
- clean_up( "Could not open Diskfont library!" );
-
-
-
- /* Check that the names are not too long: */
- if( strlen( argv[1] ) > 25 )
- clean_up( "Font name may not be longer than 25 characters!" );
- if( strlen( argv[3] ) > 25 )
- clean_up( "Structure name may not be longer than 25 characters!" );
-
- /* Check if the program was started with three arguments: */
- /* (Three arguments plus program name.) */
- if( argc == 4 )
- {
- /* Correct number of arguments! */
-
- /* Add the extension ".font": */
- strcpy( font_name, argv[ 1 ] );
- strcat( font_name, ".font" );
-
-
-
- /* Set desired font attributes: */
- font_attr.ta_Name = font_name; /* Name of the font. */
- font_attr.ta_YSize = atoi( argv[2] ); /* Number of points. */
- font_attr.ta_Style = FS_NORMAL; /* Try with normal. */
- font_attr.ta_Flags = FPF_DISKFONT; /* It is a disk font. */
-
-
-
- /* Try to open a disk font: */
- font = (struct TextFont *)
- OpenDiskFont( &font_attr );
-
- /* Have we opened the font successfully? */
- if( !font )
- clean_up( "Could not open the font!" );
-
-
-
- /* Set the height of the window: */
- my_new_window.Height = font->tf_YSize + 30;
-
- /* Open a window in which we will display the new font: */
- my_window = (struct Window *) OpenWindow( &my_new_window );
-
- /* Have we opened the window succesfully? */
- if(my_window == NULL)
- clean_up( "Could not open the window!" );
-
-
-
- /* Set colour and draw mode: */
- SetAPen( my_window->RPort, 1 );
- SetDrMd( my_window->RPort, JAM1 );
-
- /* Change the window's default font: */
- SetFont( my_window->RPort, font );
-
- /* Position the cursor, and print some characters: */
- Move( my_window->RPort, 10, font->tf_Baseline + 20 );
- Text( my_window->RPort, "1234567890 ABCDE abcde", 22 );
-
-
-
- /* Convert to 'C' code: */
- PrintFont( argv[3] );
- }
- else
- {
- /* Since the program was not started with three arguments */
- /* (program name + three arguments), give the user some */
- /* short instructions on how to use the program. */
-
- /* Started from Workbench? */
- if( !argc )
- printf( "Sorry, can not be used from Worbench." );
- else
- {
- /* Started from CLI: */
- printf( "Incorrect number of arguments!\n" );
- printf( "Usage: PrintFont > \"file.c\" \"font\" \"size\" \"name\"\n" );
- printf( "Ex: PrintFont > FontFile.c Times 18 Newsletter\n" );
- }
- }
-
- /* The End: */
- clean_up( "" );
- }
-
-
-
- /* Clears and quits: */
- void clean_up( STRPTR message )
- {
- /* Close the window: */
- if( my_window )
- CloseWindow( my_window );
-
- /* Close the font: */
- if( font )
- CloseFont( font );
-
- /* Close the Disk Font Library: */
- if( DiskfontBase )
- CloseLibrary( DiskfontBase );
-
- /* Close the Graphics Library: */
- if( GfxBase )
- CloseLibrary( GfxBase );
-
- /* Close the IntuitionLibrary: */
- if( IntuitionBase )
- CloseLibrary( IntuitionBase );
-
- /* Print any message: */
- printf( "%s\n", message );
-
- /* Quit: */
- exit();
- }
-
-
-
- void PrintFont( STRPTR name )
- {
- /* Start the code with some information about the font: */
- printf( "/******************************************/\n" );
- printf( "/* FontData prepared by PrintFont */\n" );
- printf( "/* ------------------------------ */\n" );
- printf( "/* Anders Bjerin Amiga C Club */\n" );
- printf( "/* */\n" );
- printf( "/* Font name: %25s */\n",
- font->tf_Message.mn_Node.ln_Name );
- printf( "/* Struct name: %25s */\n",
- name );
- printf( "/* Height: %25d */\n",
- font->tf_YSize );
- printf( "/* Characters: %19d - %3d */\n",
- font->tf_LoChar, font->tf_HiChar );
- printf( "/******************************************/\n\n" );
-
- printf( "#include <exec/types.h>\n" );
- printf( "#include <graphics/text.h>\n\n" );
-
- /* Print the font data if it exist: */
- if( font->tf_CharData )
- PrintFontData( name );
-
- /* Print the font location and width if it exist: */
- if( font->tf_CharLoc )
- PrintFontLoc( name );
-
- /* Print the space information if it exist: */
- if( font->tf_CharSpace )
- PrintFontSpace( name );
-
- /* Print the kerning data if it exist: */
- if( font->tf_CharKern )
- PrintFontKern( name );
-
- /* Print the TextFont structure: */
- PrintFontStructure( name );
-
- /* End the code: */
- printf( "/******************************************/\n\n" );
- }
-
-
-
- /* Print the font data (graphics): */
- void PrintFontData( STRPTR name )
- {
- UWORD x, y;
- UWORD row, col;
- UWORD *ptr;
-
-
- /* Calculate the number of columns of bytes: */
- col = font->tf_Modulo / 2;
-
- /* The number of lines (rows): */
- row = font->tf_YSize;
-
- /* Pointer to the font data: */
- ptr = (UWORD *) font->tf_CharData;
-
-
- /* Start to print: */
- printf( "/* The font data: */\n" );
- printf( "static UWORD %sData[%d]=\n{", name, col*row );
-
-
- /* Row after row: */
- for( y = 0; y < row; y++ )
- {
- /* New row: */
- printf( "\n /* Row %d: */", y );
-
- /* Column after column: */
- for( x = 0; x < col; x++ )
- {
- /* Start on a new line after eight words: */
- if( x % 8 == 0 )
- printf( "\n " );
-
- /* Print one word: */
- printf( "0x%04X%s", *ptr, y == row-1 && x == col-1 ? "" : "," );
-
- /* Step one word foreward: */
- ptr++;
- }
- }
- printf( "\n};\n\n" );
- }
-
-
-
- /* Print the location array: */
- void PrintFontLoc( STRPTR name )
- {
- UWORD loop;
- UWORD nr;
- ULONG *ptr;
-
- /* Number of characters: */
- nr = font->tf_HiChar - font->tf_LoChar + 1;
-
- /* Pointer to the space array: */
- ptr = (ULONG *) font->tf_CharLoc;
-
- /* Start to print: */
- printf( "/* The location and width of each character: */\n" );
- printf( "static ULONG %sLoc[%d]=\n{", name, nr );
-
- /* All characters: */
- for( loop = 0; loop < nr; loop++ )
- {
- /* Start on a new line after fourth words: */
- if( loop % 4 == 0 )
- printf( "\n " );
-
- /* Print two words, offset and width: (long) */
- printf( "0x%08X%s", *ptr, loop == nr-1 ? "" : "," );
-
- /* Step one long (two words) foreward: */
- ptr++;
- }
- printf( "\n};\n\n" );
- }
-
-
-
- /* Print the space (box width) array: */
- void PrintFontSpace( STRPTR name )
- {
- UWORD loop;
- UWORD nr;
- UWORD *ptr;
-
- /* Number of characters: */
- nr = font->tf_HiChar - font->tf_LoChar + 1;
-
- /* Pointer to the space array: */
- ptr = (UWORD *) font->tf_CharSpace;
-
- /* Start to print: */
- printf( "/* The width of each character's box: */\n" );
- printf( "static UWORD %sSpace[%d]=\n{", name, nr );
-
- /* All characters: */
- for( loop = 0; loop < nr; loop++ )
- {
- /* Start on a new line after eight words: */
- if( loop % 8 == 0 )
- printf( "\n " );
-
- /* Print own word: */
- printf( "0x%04X%s", *ptr, loop == nr-1 ? "" : "," );
-
- /* Step one word foreward: */
- ptr++;
- }
- printf( "\n};\n\n" );
- }
-
-
-
- /* Print the kerning array: */
- void PrintFontKern( STRPTR name )
- {
- UWORD loop;
- UWORD nr;
- UWORD *ptr;
-
- /* Number of characters: */
- nr = font->tf_HiChar - font->tf_LoChar + 1;
-
- /* Pointer to the kern array: */
- ptr = (UWORD *) font->tf_CharKern;
-
- /* Start to print: */
- printf( "/* The width of each character's box: */\n" );
- printf( "static UWORD %sKern[%d]=\n{", name, nr );
-
- /* All characters: */
- for( loop = 0; loop < nr; loop++ )
- {
- /* Start on a new line after eight words: */
- if( loop % 8 == 0 )
- printf( "\n " );
-
- /* Print own word: */
- printf( "0x%04X%s", *ptr, loop == nr-1 ? "" : "," );
-
- /* Step one word foreward: */
- ptr++;
- }
- printf( "\n};\n\n" );
- }
-
-
-
- /* Print the TextFont structure: */
- void PrintFontStructure( STRPTR name )
- {
- printf( "/* The text font structure: */\n" );
- printf( "struct TextFont %sFont=\n", name );
- printf( "{\n" );
- printf( " { /* Message */\n" );
- printf( " { /* Node */\n" );
- printf( " NULL, /* ln_Succ */\n" );
- printf( " NULL, /* ln_Pred */\n" );
- printf( " NT_FONT, /* ln_Type */\n" );
- printf( " 0, /* ln_Pri */\n" );
- printf( " \"%s\" /* ln_Name */\n", font->tf_Message.mn_Node.ln_Name );
- printf( " },\n" );
- printf( " NULL, /* mn_ReplyPort */\n" );
- printf( " %5d /* mn_Length */\n", font->tf_Message.mn_Length );
- printf( " },\n" );
- printf( " %5d, /* tf_YSize */\n", font->tf_YSize );
- printf( " %5d, /* tf_Style */\n", font->tf_Style );
- printf( " %5d, /* tf_Flags */\n", font->tf_Flags );
- printf( " %5d, /* tf_XSize */\n", font->tf_XSize );
- printf( " %5d, /* tf_Baseline */\n", font->tf_Baseline );
- printf( " %5d, /* tf_BoldSmear */\n", font->tf_BoldSmear );
- printf( " 0, /* tf_Accessors */\n" );
- printf( " %5d, /* tf_LoChar */\n", font->tf_LoChar );
- printf( " %5d, /* tf_HiChar */\n", font->tf_HiChar );
-
- /* Pointer to the font data: */
- if( font->tf_CharData )
- printf( " (APTR) &%sData, /* tf_CharData */\n", name );
- else
- printf( " NULL, /* tf_CharData */\n" );
-
- printf( " %5d, /* tf_Modulo */\n", font->tf_Modulo );
-
- /* Pointer to the array of location and width of each character: */
- if( font->tf_CharLoc )
- printf( " (APTR) &%sLoc, /* tf_CharLoc */\n", name );
- else
- printf( " NULL, /* tf_CharLoc */\n" );
-
- /* Pointer to the spcae array: */
- if( font->tf_CharSpace )
- printf( " (APTR) &%sSpace, /* tf_CharSpace */\n", name );
- else
- printf( " NULL, /* tf_CharSpace */\n" );
-
- /* Pointer to the kerning array: */
- if( font->tf_CharKern )
- printf( " (APTR) &%sKern, /* tf_CharKern */\n", name );
- else
- printf( " NULL, /* tf_CharKern */\n" );
-
- printf( "};\n\n" );
- }
-