home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / gtlayout-source.lha / LTP_GlyphSetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  3.2 KB  |  182 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. VOID
  15. LTP_GetDefaultFont(struct TTextAttr *TextAttr)
  16. {
  17.     struct TextFont *Font = GfxBase->DefaultFont;
  18.  
  19.     TextAttr->tta_Name    = Font->tf_Message.mn_Node.ln_Name;
  20.     TextAttr->tta_YSize    = Font->tf_YSize;
  21.     TextAttr->tta_Style    = Font->tf_Style;
  22.     TextAttr->tta_Flags    = Font->tf_Flags & ~FPF_REMOVED;
  23.  
  24.     if(Font->tf_Style & FSF_TAGGED)
  25.         TextAttr->tta_Tags = ((struct TextFontExtension *)Font->tf_Extension)->tfe_Tags;
  26.     else
  27.         TextAttr->tta_Tags = NULL;
  28. }
  29.  
  30.  
  31. /*****************************************************************************/
  32.  
  33.  
  34. struct TextFont *
  35. LTP_OpenFont(struct TextAttr *TextAttr)
  36. {
  37.     struct TextFont *Font;
  38.  
  39.         // Let's see if this one is special
  40.  
  41.     if(TextAttr == (struct TextAttr *)~0)
  42.     {
  43.         struct TTextAttr LocalTextAttr;
  44.  
  45.         memset(&LocalTextAttr,0,sizeof(LocalTextAttr));
  46.  
  47.         Forbid();
  48.  
  49.             // Borrow the system default font data
  50.  
  51.         LTP_GetDefaultFont(&LocalTextAttr);
  52.  
  53.             // Let's hope the font will open...
  54.  
  55.         Font = OpenFont((struct TextAttr *)&LocalTextAttr);
  56.  
  57.         Permit();
  58.     }
  59.     else
  60.     {
  61.         if(!(Font = OpenFont(TextAttr)))
  62.         {
  63.             if(FindTask(NULL)->tc_Node.ln_Type != NT_TASK)
  64.             {
  65.                 struct Library *DiskfontBase;
  66.  
  67.                 if(DiskfontBase = OpenLibrary("diskfont.library",0))
  68.                 {
  69.                     Font = OpenDiskFont(TextAttr);
  70.  
  71.                     CloseLibrary(DiskfontBase);
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     return(Font);
  78. }
  79.  
  80.  
  81. /*****************************************************************************/
  82.  
  83.  
  84. BOOL
  85. LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
  86. {
  87.     struct TextFont *Font;
  88.  
  89.     if(!TextAttr)
  90.         TextAttr = Handle->Screen->Font;
  91.  
  92.     if(Font = LTP_OpenFont(Handle->TextAttr = TextAttr))
  93.     {
  94.         struct RastPort *RPort;
  95.         LONG Count;
  96.         LONG Total;
  97.         ULONG OldStyle;
  98.         LONG GlyphWidth;
  99.         LONG i;
  100.         UBYTE Glyph;
  101.  
  102.         RPort = &Handle->RPort;
  103.  
  104.         if(Handle->CloseFont)
  105.             CloseFont(RPort->Font);
  106.  
  107.         Handle->CloseFont = TRUE;
  108.  
  109.         LTP_SetFont(Handle,Font);
  110.  
  111.         OldStyle = RPort->AlgoStyle;
  112.  
  113.         SetSoftStyle(RPort,FSF_BOLD | FSF_UNDERLINED,FSF_BOLD | FSF_UNDERLINED);
  114.  
  115.             // Cover all printable characters
  116.  
  117.         Count = Total = 0;
  118.  
  119.         for(i = 32 ; i < 256 ; i++)
  120.         {
  121.             if(i == 128)
  122.                 i = 160;
  123.  
  124.             Glyph = i;
  125.  
  126.             GlyphWidth = TextLength(RPort,&Glyph,1);
  127.  
  128.                 // For really pathologic fonts...
  129.  
  130.             if(GlyphWidth >= 0 && GlyphWidth <= 32767)
  131.             {
  132.                 Total += GlyphWidth;
  133.  
  134.                 Count++;
  135.             }
  136.         }
  137.  
  138.         Handle->GlyphWidth = Total / Count;
  139.  
  140.             // Now for the numeric characters
  141.  
  142.         for(Total = 0, i = '0' ; i <= '9' ; i++)
  143.         {
  144.             Glyph = i;
  145.  
  146.             Total += TextLength(RPort,&Glyph,1);
  147.         }
  148.  
  149.         Total /= 10;
  150.  
  151.             // Actually, the numeric character should be
  152.             // just as wide as the space character. Let's
  153.             // hope for the best.
  154.  
  155.         Count = TextLength(RPort," ",1);
  156.  
  157.         if(Total < Count)
  158.             Total = Count;
  159.  
  160.         if(Total > Handle->GlyphWidth)
  161.             Handle->GlyphWidth = Total;
  162.  
  163.         if(Handle->GlyphWidth <= 8)
  164.             Handle->InterWidth = 4;
  165.         else
  166.             Handle->InterWidth = Handle->GlyphWidth / 2;
  167.  
  168.         Handle->GlyphHeight = RPort->TxHeight;
  169.  
  170.         if(Handle->GlyphHeight <= 8)
  171.             Handle->InterHeight = 2;
  172.         else
  173.             Handle->InterHeight = Handle->GlyphHeight / 4;
  174.  
  175.         SetSoftStyle(RPort,OldStyle,FSF_BOLD | FSF_UNDERLINED);
  176.  
  177.         return(TRUE);
  178.     }
  179.     else
  180.         return(FALSE);
  181. }
  182.