/*
***************************************************************************
*
* Datei:
*    RSysGfxCtrl.c
*
* Inhalt:
*    int TextLen(struct TextFont *tf, struct RastPort *RP, char *text);
*    short compute(unsigned short offset, unsigned short xy, int measure);
*    unsigned short ComputeX(unsigned short value);
*    unsigned short ComputeY(unsigned short value);
*    void ComputeFont(struct Screen *wndscr, unsigned short width, unsigned short height);
*
* Bemerkungen:
*    Graphics-Routinen für die Unterstützung fontsensitiver
*    Benutzeroberflächen.
*
* Erstellungsdatum:
*    25-Jun-93    Rolf Böhme
*
* Änderungen:
*    25-Jun-93    Rolf Böhme    Erstellung
*
***************************************************************************
*/

#include "RSysDebug.h"
#include "RSysFunc.h"

   /*
    * TextLen() berechnet die Länge eines Strings in einem gewählten
    * RastPort bezüglich des verwendeten TextFonts. Dabei werden
    * auch proportionale Schriften und das Kerning berücksichtigt
    */
int
TextLen(struct TextFont *tf, struct RastPort *RP, char *text)
{
   int   retlen = 0,
         slen = strlen(text),
         i;
   WORD *charspace = (WORD *) tf->tf_CharSpace;

   DPOS;

   if (tf->tf_CharSpace == NULL)
      retlen = TextLength(RP, (UBYTE *) text, strlen(text));
   else
      for (i = 0; i < slen; i++)
         retlen += charspace[(int)text[i]];

   return (retlen);
}

   /*
    * compute() berechnet die fontsensitiven Abmessungen von
    * Intuition-Objekten
    */
WORD
compute(UWORD offset, UWORD xy, int measure)
{
   return (offset + ((xy * (measure * FRAC)) / FULL));
}

   /*
    * computeX() berechnet einen Wert bezüglich der
    * Fontbreite
    */
UWORD
ComputeX(UWORD value)
{
   return ((UWORD) (((FontX * value) + 4) / 8));
}

   /*
    * computeY() berechnet einen Wert bezüglich der
    * Fonthöhe
    */
UWORD
ComputeY(UWORD value)
{
   return ((UWORD) (((FontY * value) + 4) / 8));
}

   /*
    * ComputeFont() berechnet die korrekte Breite und Höhe eines
    * Fonts. Passen Höhe und Breite eines Abschnittes, verknüpft
    * mit den Fontdaten nicht auf den angegebenen Screen, wird der
    * System-Standard-Font topaz in der Größe 8 verwendet
    */
void
ComputeFont(struct Screen *wndscr, UWORD width, UWORD height)
{
   char *test = "abcdefghijklmnopqrstuvwxyz"
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   "1234567890"
   ",.-;:!()=?[]{} ";

   DPOS;

   if (Flags.sysfont)
   {
      Font = &Topaz80;
      FontX = 8;
/*      FontY = ((wndscr->Font->ta_YSize > 8) ? wndscr->Font->ta_YSize : 8);*/
      FontY = 8;
   }
   else
   {
      Font = wndscr->Font;
      Forbid();
      FontX = (TextLen(GfxBase->DefaultFont, &wndscr->RastPort, test) + 4) / strlen(test);
      Permit();
      FontY = Font->ta_YSize;
   }

	OffX = wndscr->WBorLeft;
	OffY = wndscr->RastPort.TxHeight + Scr->WBorTop + 1;

   if (width && height)
   {
      if ((ComputeX(width) + OffX + wndscr->WBorRight) > wndscr->Width)
         goto UseTopaz;
      if ((ComputeY(height) + OffY + wndscr->WBorBottom) > wndscr->Height)
         goto UseTopaz;
   }

   return;

 UseTopaz:
   ErrorHandle(FONT_ERR, SIZE_FAIL, NO_KILL);

   Font = &Topaz80;
   FontX = 8;
   FontY = 8;
   Font->ta_YSize = 8;

   Flags.sysfont = 1;

   return;
}

