home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 162.lha / WhichFnt.c < prev    next >
C/C++ Source or Header  |  1988-04-27  |  10KB  |  287 lines

  1. /*Article 1480 of net.micro.amiga:
  2. Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site well.UUCP
  3. Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site amiga.amiga.UUCP
  4. Path: well!ptsfa!qantel!lll-crg!seismo!ut-sally!pyramid!amiga!robp
  5. From: robp@amiga.UUCP (Robert A. Peck)
  6. Newsgroups: net.micro.amiga
  7. Subject: Re: Fonts on the Amiga
  8. Message-ID: <500@amiga.amiga.UUCP>
  9. Date: 8 Jan 86 22:39:31 GMT
  10. Date-Received: 10 Jan 86 10:04:20 GMT
  11. References: <136@daisy.UUCP>
  12. Reply-To: robp@dudley.UUCP (Robert A. Peck)
  13. Distribution: net
  14. Organization: Commodore-Amiga Inc., 983 University Ave #D, Los Gatos CA 95030
  15. Lines: 262
  16. */
  17.  
  18. /* "whichfont.c" */
  19.  
  20. /* sample program that asks AvailFonts() to make a list of the fonts
  21.  * that are available and makes a list of them, then opens a separate
  22.  * window and prints a description of the various attributes that can
  23.  * be applied to the fonts, in the font itself.  Notice that not all
  24.  * fonts accept all attributes (garnet9 for example, won't underline)
  25.  *
  26.  * Also note, if you run this, that not all fonts are as easily readable
  27.  * in the various bold and italicized modes.... this rendering is done
  28.  * in a fixed manner by software and the fonts were not necessarily
  29.  * designed to accept it.  It is always best to have a font that has
  30.  * been designed with a bold or italic characteristic built-in rather
  31.  * than try to bold-ize or italicize and existing plain font.
  32.  */
  33.  
  34. /* Author:  Rob Peck  10/28/85  */
  35.  
  36. #define AFTABLESIZE 2000
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <libraries/dos.h>
  41. #include <graphics/text.h>
  42. #include <libraries/diskfont.h>
  43. #include <intuition/intuition.h>
  44.  
  45. struct AvailFonts *af;
  46. struct AvailFontsHeader *afh;
  47. extern int AvailFonts();
  48.  
  49. struct TextFont *tf;
  50. struct TextAttr ta;
  51.  
  52. ULONG DosBase;
  53. ULONG DiskfontBase;
  54. ULONG IntuitionBase;
  55. ULONG GfxBase;
  56.  
  57. struct NewWindow nw = {
  58.         10, 10,        /* starting position (left,top) */
  59.         620,40,        /* width, height */
  60.         -1,-1,          /* detailpen, blockpen */
  61.         0,        /* flags for idcmp */
  62.   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
  63.             /* window gadget flags */
  64.         0,              /* pointer to 1st user gadget */
  65.         NULL,           /* pointer to user check */
  66.         "Text Font Test", /* title */
  67.         NULL,           /* pointer to window screen */
  68.         NULL,           /* pointer to super bitmap */
  69.         100,45,         /* min width, height */
  70.         640,200,        /* max width, height */
  71.         WBENCHSCREEN};
  72.  
  73. struct Window *w;
  74. struct RastPort *rp;
  75.  
  76. SHORT text_styles[ ] = { FS_NORMAL, FSF_UNDERLINED, FSF_ITALIC, FSF_BOLD, 
  77.             FSF_ITALIC | FSF_BOLD, FSF_BOLD | FSF_UNDERLINED,
  78.             FSF_ITALIC | FSF_BOLD | FSF_UNDERLINED };
  79.         
  80. char *text[ ] = { " Normal Text", " Underlined", " Italicized", " Bold", 
  81.           " Bold Italics", " Bold Underlined", 
  82.           " Bold Italic Underlined" };
  83. char textlength[ ] = { 12, 11, 11, 5, 13, 16, 23 };
  84.  
  85. char *pointsize[] = { " 0"," 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9",
  86.                    "10","11","12","13","14","15","16","17","18","19",
  87.                    "20","21","22","23","24","25","26","27","28","29",
  88.                    "30","31"};
  89.  
  90. char fontname[40];
  91. char dummy[100];     /* provided for string length calculation */
  92. char outst[100];    /* build something to give to Text, see note in 
  93.              * the program body about algorithmically
  94.              * generated styles 
  95.              */
  96.  
  97. main( argc, argv)
  98. int argc;
  99. char *argv[];
  100. {
  101.     UBYTE fonttypes;
  102.     int i,j,k,m;
  103.     SHORT afsize;
  104.     SHORT style;
  105.     SHORT sEnd;    /* numerical position of end of string terminator,
  106.              * and coincidently the length of the string. */
  107.  
  108.     if( (DosBase = OpenLibrary("dos.library", 0)) == NULL) exit(-1);
  109.     if((DiskfontBase=OpenLibrary("diskfont.library",0))==NULL) exit(-4);
  110.     if((IntuitionBase=OpenLibrary("intuition.library",0))==NULL) exit(-2);
  111.     if((GfxBase=OpenLibrary("graphics.library",0))==NULL) exit(-3);
  112.     
  113.     tf=NULL;    /* no font currently selected */
  114.     afsize = AFTABLESIZE;    /* show how large a buffer is available */
  115.     fonttypes = 0xff;    /* show us all font types */
  116.     
  117.     afh = (struct AvailFontsHeader *) AllocMem(afsize, MEMF_CLEAR);
  118.     if(afh == NULL) exit(-5);
  119.     
  120.     printf("\nSearching for Fonts\n");
  121.     AvailFonts(afh, afsize, fonttypes);
  122.  
  123.     af = (struct AvailFonts *) &afh[1];  /* bypass header to get to the
  124.                           * first of the availfonts */
  125.  
  126.     for (j = 0; j < afh->afh_NumEntries; j++)
  127.         {
  128.     if((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  129.        (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  130.                     ((af->af_Type&AFF_MEMORY)&&
  131.             (af->af_Attr.ta_Flags&FPF_DISKFONT)))
  132.             ;    /* do nothing if font is removed, or if
  133.                  * font designed to be rendered rt->left
  134.                  * (simple example writes left to right)
  135.                  * or if font both on disk and in ram, 
  136.                  * don't list it twice. */
  137.  
  138.     /* AvailFonts performs an AddFont to the system list;
  139.      * if run twice, you get two entries, one of "af_Type 1" saying
  140.      * that the font is memory resident, and the other of "af_Type 2"
  141.      * saying the font is disk-based.  The third part of the 
  142.      * if-statement lets you tell them apart if you are scanning
  143.      * the list for unique elements;  it says "if its in memory and
  144.      * it is from disk, then don't list it because you'll find another
  145.      * entry in the table that says it is not in memory, but is on disk.
  146.      * (Another task might have been using the font as well, creating
  147.      * the same effect).
  148.      */
  149.  
  150.     else
  151.        {
  152.        if ( argc != 0 )            /* if not from WB */
  153.            {
  154.         printf("\nFont name found was: %ls",af->af_Attr.ta_Name);
  155.         printf("  and its point size is: %ld",af->af_Attr.ta_YSize);
  156.         }
  157.         
  158.         /* Style parameter is in af->af_Attr.ta_Style,
  159.          * Flags parameter is in af->af_Attr.ta_Flags.
  160.          */        
  161.        }
  162.     af++;
  163.         }
  164.     /* now that we've listed the fonts, lets look at them */
  165.  
  166.     w = (struct Window *)OpenWindow(&nw);
  167.     rp = w->RPort;
  168.  
  169.     for(m=0; m<2; m++)    /* do normal video, then inverse video */
  170.      {
  171.  
  172.     af = (struct AvailFonts *)&afh[1]; /* reset value of af to original */
  173.     SetAPen(rp,1);        
  174.  
  175.     if(m == 0)SetDrMd(rp,JAM1);
  176.     else SetDrMd(rp,JAM1+INVERSVID);
  177.  
  178.     /* now print a line that says what font and what style it is */
  179.  
  180.     for (j=0; j < afh->afh_NumEntries; j++)
  181.     {
  182.     CStringAppend(&fontname[0],af->af_Attr.ta_Name);  
  183.             /* copy name into build-name area */
  184.             /* already has ".font" onto end of it */
  185.     
  186.     ta.ta_Name = &fontname[0];
  187.     ta.ta_YSize = af->af_Attr.ta_YSize;    /* ask for this size */
  188.     ta.ta_Style = af->af_Attr.ta_Style;    /* ask for designed style */
  189.     ta.ta_Flags = FPF_ROMFONT|FPF_DISKFONT|FPF_PROPORTIONAL|FPF_DESIGNED;
  190.         /* accept it from anywhere it exists */
  191.     style = ta.ta_Style;
  192.  
  193.     if(!((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  194.        (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  195.            ((af->af_Type&AFF_MEMORY)&&
  196.        (af->af_Attr.ta_Flags&FPF_DISKFONT))))
  197.  
  198.        /* this is an IF-NOT, the reverse of the earlier if-test on
  199.         * these same parameters 
  200.         */
  201.            {
  202.         tf = (struct TextFont *) OpenDiskFont(&ta);
  203.     
  204.             if (tf != 0) 
  205.             { 
  206.                 SetFont(w->RPort, tf);
  207.             for(k=0; k<7; k++)
  208.             {
  209.             style = text_styles[k];
  210.                     SetSoftStyle(w->RPort,style,255);
  211.             SetRast(rp,0);    /* erase any previous text */
  212.             Move(rp,10,20);    /* move down a bit from the top */
  213.             sEnd = CStringAppend(&outst[0],af->af_Attr.ta_Name);
  214.             sEnd = sEnd + CStringAppend(&outst[sEnd],"  ");
  215.             sEnd = sEnd + CStringAppend(&outst[sEnd],
  216.                          pointsize[af->af_Attr.ta_YSize]);
  217.             sEnd = sEnd + CStringAppend(&outst[sEnd]," Points, ");
  218.             CStringAppend(&outst[sEnd],text[k]);
  219.             Text(rp,&outst[0],CStringAppend(&dummy[0],&outst[0]));
  220.  
  221.             /* Have to build the string before sending it out to
  222.              * text IF ALGORITHMICALLY GENERATING THE STYLE since 
  223.              * the kerning and spacing tables are based on the
  224.              * vanilla text, and not the algorithmically generated
  225.              * style.  If you send characters out individually,
  226.              * it is possible that the enclosing rectangle of
  227.               * a later character will chop off the trailing edge
  228.              * of a preceding character 
  229.              */
  230.  
  231.             /* ************************************************** 
  232.             This alternate method, when in INVERSVID, exhibits the
  233.             problem described above.
  234.  
  235.             Text(rp,af->af_Attr.ta_Name,STRLEN(af->af_Attr.ta_Name));
  236.             Text(rp,"  ",2);
  237.             Text(rp,pointsize[af->af_Attr.ta_YSize],2);
  238.             Text(rp," Points, ",9);
  239.     
  240.             Text(rp,text[k],textlength[k]);  
  241.             **************************************************  */ 
  242.  
  243.             Delay(40);    /* use the DOS time delay function 
  244.                    * specifies 60ths of a second */
  245.             }
  246.                 CloseFont(tf); /* close the old one */
  247.     
  248.             }    /* end of if-tf-ne-0 */
  249.       }    /* end of if-(in memory but from disk) */
  250.     af++;
  251.     }    /* Do next font now */
  252.      }        /* end of for-loop, controlled by m */
  253.  
  254.     FreeMem(afh,AFTABLESIZE);
  255.     CloseWindow(w);
  256.     CloseLibrary(IntuitionBase);   
  257.     CloseLibrary(DosBase);  
  258.     CloseLibrary(DiskfontBase);   
  259.     CloseLibrary(GfxBase);   
  260.  
  261. }
  262.  
  263. /* copy a string and return the number of characters added to 
  264.  * a string.  Effectively returns the length of the string if
  265.  * not adding anything */
  266.  
  267. int CStringAppend(dest, source)    
  268. char *dest;
  269. char *source;
  270. {
  271.     int i=0; 
  272.     char *s = source; 
  273.     char *d = dest; 
  274.     while (( i <79 )&&( *d = *s )) { d++; s++; i++; } 
  275.     /* if find a NULL in source, end the copy, but the NULL itself
  276.      * gets copied over to the destination.  If no NULL, then 79
  277.      * characters get copied, then a terminating NULL is added */
  278.     if(i < 79) return(i);
  279.     else {*d = 0; return(i);    }
  280.     /* value returned is the position of the terminating NULL
  281.      * to allow other strings to be appended simply using the
  282.      * next append command in sequence */
  283.  
  284. }
  285.  
  286.  
  287.