home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / graphics / text / FontParade.c next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  9.7 KB  |  267 lines

  1. /* FontParade
  2.    Example to display all fonts available to the system.
  3.    Insert these routines into the "wrapper" code at the end of the chapter.
  4. */
  5.  
  6. /*  The buffer for concatenating text strings will be this big.  */
  7. #define FONTSTRINGSIZE (MAXFONTNAME + 40L)
  8. /*  The buffer for the first attempt at AvailFonts() will be this big.  */
  9. #define AFH_SIZE_DEFAULT 400L
  10.  
  11. /*  Pointer to buffer for AvailFonts() information.  */
  12. struct AvailFontsHeader *afHeader = NULL;
  13. LONG afhSize = AFH_SIZE_DEFAULT;
  14. UBYTE *fontName = NULL, *fontString = NULL;    /*  String pointers.  */
  15.  
  16.  
  17. /*
  18.    Allocate a buffer to hold information returned by AvailFonts().
  19.    Call AvailFonts().  If the buffer wasn't large enough, deallocate it,
  20.    allocate a larger buffer and try again.  Allocate memory for text strings.
  21.    Return TRUE if completely successful, FALSE otherwise.
  22. */
  23. BOOL getMem(VOID)
  24. {
  25. LONG needMore;
  26.  
  27. /*  AvailFonts() returns a value indicating the amount of memory it
  28.     would need (above what you have given it) to successfully fulfill
  29.     the request.  If this value is greater than zero, it is necessary
  30.     to reallocate a larger chunk of memory and try again.
  31. */
  32. do
  33.     {
  34.     needMore = 0L;
  35.     afHeader = (struct AvailFontsHeader *)AllocMem(afhSize, MEMF_PUBLIC);
  36.     if (afHeader)
  37.         {
  38.         needMore =
  39.             AvailFonts((UBYTE *)afHeader, afhSize, AFF_MEMORY | AFF_DISK);
  40.         if (needMore)
  41.             {
  42.             FreeMem(afHeader, afhSize);
  43.             afhSize += needMore;
  44.             }
  45.         }
  46.     } while (needMore);
  47.  
  48. if (afHeader)
  49.     {
  50.     fontString = (UBYTE *)AllocMem(FONTSTRINGSIZE, MEMF_PUBLIC);
  51.     if (fontString)
  52.         {
  53.         fontName = (UBYTE *)AllocMem(MAXFONTNAME, MEMF_PUBLIC);
  54.         if (fontName)
  55.             return(TRUE);
  56.         }
  57.     }
  58. return(FALSE);
  59. }
  60.  
  61.  
  62. /*
  63.    Free any memory allocated in getMem().
  64. */
  65. VOID returnMem(VOID)
  66. {
  67. if (fontName)
  68.     FreeMem(fontName, MAXFONTNAME);
  69. if (fontString)
  70.     FreeMem(fontString, FONTSTRINGSIZE);
  71. if (afHeader)
  72.     FreeMem(afHeader, afhSize);
  73. }
  74.  
  75.  
  76. /*
  77.    Walk through the linked-list of AvailFont structures filled in by
  78.    AvailFonts().  For each font, use SetSoftStyle() to set it to
  79.    each of eight styles (normal, underlined, italic, bold and all
  80.    logical permutations) in turn.  Create a text string which is
  81.    composed of the font name, size, and style and render it into
  82.    the window in that particular size and style.
  83. */
  84. BOOL example(struct Window *window)
  85. {
  86. BOOL success, done = FALSE;
  87. SHORT right, left, bottom, xPos;
  88. SHORT style;
  89. UBYTE sizeString[5];
  90. UWORD entry;
  91. struct RastPort *rPort;
  92. struct AvailFonts *aFonts;
  93. struct TextFont *tFont, *oldTextFont;
  94. struct TextAttr tAttr;
  95. struct IntuiMessage *intuiMessage;
  96.  
  97. /*  The eight different styles, in #define form (from graphics/text.h).  */
  98. static ULONG styles[ ] =
  99.     {
  100.     FS_NORMAL, FSF_UNDERLINED, FSF_ITALIC, FSF_ITALIC | FSF_UNDERLINED,
  101.     FSF_BOLD, FSF_BOLD | FSF_UNDERLINED, FSF_BOLD | FSF_ITALIC,
  102.     FSF_BOLD | FSF_ITALIC | FSF_UNDERLINED
  103.     };
  104.  
  105. /*  The eight different styles, in descriptive text form.  */
  106. static UBYTE *descriptions[ ] =
  107.     {
  108.     "Normal", "Normal Underlined", "Italic", "Italic Underlined",
  109.     "Bold", "Bold Underlined", "Bold Italic", "Bold Italic Underlined"
  110.     };
  111.  
  112. /*  Set window title to indicate activity.  */
  113. TITLE(window, "Scanning Fonts:");
  114. if (success = getMem())
  115.     {
  116.     TITLE(window, "FontParade");
  117.  
  118.     rPort = window->RPort;    /*  Get the window's rastport.  */
  119.     /*  Save the original TextFont pointer.  */
  120.     oldTextFont = rPort->Font;
  121.     SetAPen(rPort, COLOR1);    /*  Set the A pen color.  */
  122.     SetDrMd(rPort, JAM1);    /*  Set the drawing mode.  */
  123.  
  124.     /*
  125.        Calculate the amount of space to indent from the
  126.        sides of the window, to keep the text from being hidden
  127.        behind the borders.  If this were not a GimmeZeroZero
  128.        window, window->Width and window->Height would be used
  129.        instead of window->GZZWidth and window->GZZHeight.
  130.        Reverse-path fonts are rendered from right to left,
  131.        and so must begin on the right side of the window.
  132.     */
  133.     left = window->BorderLeft;
  134.     right = window->GZZWidth - (SHORT)window->BorderRight;
  135.     bottom = window->GZZHeight - (SHORT)window->BorderBottom;
  136.  
  137.     /* Skip over the header to the first of the AvailFont structures. */
  138.     aFonts = (struct AvailFonts *)&afHeader[1];
  139.  
  140.     for (entry = afHeader->afh_NumEntries; entry && !done; entry--, aFonts++)
  141.         {
  142.     /*
  143.        It is possible that AvailFonts() will find two entries for a
  144.        single font, one of "af_Type 1" saying that the font is memory-
  145.        resident, and the other of "af_Type 2" saying the font is disk-based.
  146.        This happens because another process has previously called
  147.        OpenDiskFont() requesting that font.
  148.  
  149.        The second part of the if-statement lets you tell the two apart if
  150.        you are scanning the list for unique elements; it says "if it's in
  151.        memory and it's also from disk, then don't list it because you'll find
  152.        another entry in the table that says it is not in memory, but is on
  153.        disk".
  154.     */
  155.         if ( ! ((aFonts->af_Attr.ta_Flags & FPF_REMOVED) ||
  156.             (aFonts->af_Type & AFF_MEMORY) &&
  157.                 (aFonts->af_Attr.ta_Flags & FPF_DISKFONT)))
  158.             {
  159.             /*
  160.                Copy the contents of the AvailFonts' TextAttr structure
  161.                to tAttr.  Modify this copy to allow for both ROMFONT and
  162.                DISKFONT fonts (because both types are wanted).  Then use
  163.                tAttr to request a font with these attributes from
  164.                OpenDiskFont().  An exact match will be found since all
  165.                the attributes came from an existing font.
  166.             */
  167.             strcpy(fontName, aFonts->af_Attr.ta_Name);
  168.             tAttr.ta_Name = fontName;
  169.             tAttr.ta_YSize = aFonts->af_Attr.ta_YSize;
  170.             tAttr.ta_Style = aFonts->af_Attr.ta_Style;
  171.             tAttr.ta_Flags = aFonts->af_Attr.ta_Flags |
  172.                 (FPF_ROMFONT | FPF_DISKFONT);
  173.  
  174.             tFont = (struct TextFont *)OpenDiskFont(&tAttr);
  175.  
  176.             if (tFont)
  177.                 {
  178.                 /*  Make this font the RastPort's font.  */
  179.                 SetFont(rPort, tFont);
  180.                 /*  Step through the eight styles, one at a time.  */
  181.                 for (style=0; style<8; style++)
  182.                     {
  183.                 /*
  184.                    The user may want to exit before the program has finished.
  185.                    The following code is a simple method of checking for a
  186.                    single class of message without putting the program to
  187.                    sleep with the Wait() call.  It is not as multitasking-
  188.                    friendly as Wait().  Note that this routine does not check
  189.                    what class of message has arrived since only one class of
  190.                    message was asked for in the window's IDCMP field
  191.                    (CLOSEWINDOW).  See the Intuition chapter for information
  192.                    on fully utilizing Intuition's IDCMP.
  193.                 */
  194.  
  195.                     /*  If a message has arrived...  */
  196.                     intuiMessage =
  197.                         (struct IntuiMessage *)GetMsg(window->UserPort);
  198.                     if (intuiMessage)
  199.                         {
  200.                             /*  reply to it,  */
  201.                             ReplyMsg((struct Message *)intuiMessage);
  202.                             done = TRUE;    /*  set the "done" flag and  */
  203.                             break;    /*  break out of the "for" loop.  */
  204.                         }
  205.  
  206.                     /* If a font already has certain attributes (bold,
  207.                        italic, etc.) don't bother re-displaying it with
  208.                        those styles.  Those attributes were evident
  209.                        when the font was displayed in its "normal" style.
  210.                     */
  211.                     if (styles[style] & tFont->tf_Style)
  212.                         continue;
  213.  
  214.                 /*  Create a string from the font's name, size and style.  */
  215.                     strcpy(fontString, " ");
  216.                     strcat(fontString, tFont->tf_Message.mn_Node.ln_Name);
  217.                     sprintf(sizeString, " %ld", tFont->tf_YSize);
  218.                     strcat(fontString, sizeString);
  219.                     strcat(fontString, " point, ");
  220.                     strcat(fontString, descriptions[style]);
  221.                     strcat(fontString, " ");
  222.  
  223.                     /*  Set the style.  */
  224.                     (VOID)SetSoftStyle(rPort, styles[style], ~0L);
  225.  
  226.                     /*  Set starting point for rendering.  If font is
  227.                         reverse-path, start on the right side.
  228.                     */
  229.                     xPos = (tFont->tf_Flags & FPF_REVPATH) ? right : left;
  230.                     Move(rPort, xPos,
  231.                         bottom-(tFont->tf_YSize-tFont->tf_Baseline));
  232.  
  233.                     /*
  234.                        Erase any previous text by clearing the window's
  235.                        rastport to the background color.
  236.                     */
  237.                     SetRast(rPort, COLOR0);
  238.  
  239.                     /*  Render the text string.  */
  240.                     Text(rPort, fontString, (ULONG)strlen(fontString));
  241.  
  242.                     /*  Pause.  */
  243.                     Delay(1L * TICKS_PER_SECOND);
  244.                     }
  245.                 /*  Reset the RastPort's original font.  */
  246.                 SetFont(rPort, oldTextFont);
  247.                 CloseFont(tFont);
  248.                 }
  249.             }
  250.         }
  251.     }
  252. else
  253. /*  Alert the user to the low-memory condition.  */
  254.     TITLE(window, "Not enough free memory.");
  255.  
  256.     /*  Return any memory that was successfully allocated.  */
  257.     returnMem();
  258.  
  259.     if (success)
  260.         /*  Close immediately if there was no error.  */
  261.         return(DONT_WAIT);
  262.     else
  263.         /*  Wait for the user to read the error message.  */
  264.         return(WAIT_FOR_CLOSE);
  265. }
  266.  
  267.