/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Torsten Jürgeleit
 *
 *	Name .....: fonts.c
 *	Created ..: Tuesday 31-Dec-91 14:29:10
 *	Revision .: 1
 *
 *	Date        Author                 Comment
 *	=========   ====================   ====================
 *	07-Jan-92   Torsten Jürgeleit      update to new ISUP font routines
 *	31-Dec-91   Torsten Jürgeleit      Created this file!
 *
 ****************************************************************************
 *
 *	Font support routines
 *
 * $Revision Header ********************************************************/

	/* Includes */

#include "includes.h"
#include "defines.h"
#include "imports.h"
#include "protos.h"

	/* Open template font with given attributes */

   struct TextAttr *
open_template_font_by_attributes(struct TemplateList  *tl, BYTE *name,
							       USHORT ysize)
{
   struct TextAttr  search_ta, *avail_ta, *ta = NULL;

   /* Try to open given font */
   search_ta.ta_Name  = (STRPTR)name;
   search_ta.ta_YSize = ysize;
   search_ta.ta_Style = FS_NORMAL;
   search_ta.ta_Flags = FPF_ROMFONT;
   if (avail_ta = IAskFont(pri, &search_ta)) {
      struct TemplateFont  *tf;

      /* Search given template font in font list */
      ysize = avail_ta->ta_YSize;
      for (tf = get_head((struct List *)&tl->tl_Fonts); tf;
			    tf = get_succ((struct Node *)&tf->tf_MinNode)) {
	  if (!Strcmp(name, (BYTE *)tf->tf_TextAttr.ta_Name) &&
					ysize == tf->tf_TextAttr.ta_YSize) {
	     tf->tf_UseCount++;
	     ta = &tf->tf_TextAttr;
	     break;
	  }
      }
      if (!ta) {

	 /* Create new template font */
	 if (tf = AllocMem((LONG)sizeof(struct TemplateFont),
						       (LONG)MEMF_PUBLIC)) {
	    if (duplicate_string(name, (BYTE **)&tf->tf_TextAttr.ta_Name) !=
						     EDITOR_STATUS_NORMAL) {
	       FreeMem(tf, (LONG)sizeof(struct TemplateFont));
	    } else {

	       /* Init template font and add it to font list */
	       ta = &tf->tf_TextAttr;
	       ta->ta_YSize    = avail_ta->ta_YSize;
	       ta->ta_Style    = avail_ta->ta_Style;
	       ta->ta_Flags    = avail_ta->ta_Flags;
	       tf->tf_UseCount = 1;
	       AddTail((struct List *)&tl->tl_Fonts,
					    (struct Node *)&tf->tf_MinNode);
	    }
	 }
      }
   }
   return(ta);
}
	/* Open template font with given number */

   struct TextAttr *
open_template_font_by_num(struct TemplateList  *tl, USHORT num)
{
   struct TemplateFont  *tf;
   struct TextAttr      *ta;

   /* Search given template font in font list */
   if (!(tf = get_node((struct List *)&tl->tl_Fonts, num))) {
      ta = NULL;
   } else {
      tf->tf_UseCount++;
      ta = &tf->tf_TextAttr;
   }
   return(ta);
}
	/* Get number of template font from font list */

   USHORT
get_template_font_num(struct TemplateList  *tl, struct TextAttr  *ta)
{
   struct TemplateFont  *tf;
   USHORT num = 0;

   /* Search given template font in font list */
   for (tf = get_head((struct List *)&tl->tl_Fonts); tf;
			    tf = get_succ((struct Node *)&tf->tf_MinNode)) {
      if (ta == &tf->tf_TextAttr) {
	 break;
      } else {
	 num++;
      }
   }
   return(num);
}
	/* Close template font with given attributes */

   VOID
close_template_font(struct TemplateList  *tl, struct TextAttr  *ta)
{
   if (ta) {
      struct TemplateFont  *tf;

      /* Search given template font in font list */
      for (tf = get_head((struct List *)&tl->tl_Fonts); tf;
			    tf = get_succ((struct Node *)&tf->tf_MinNode)) {
	 if (ta == &tf->tf_TextAttr) {

	    /* Close font and check if its in use yet */
	    if (!--tf->tf_UseCount) {

	       /* Remove and free template font from font list */
	       Remove((struct Node *)&tf->tf_MinNode);
	       free_template_font(tf);
	    }
	    break;
	 }
      }
   }
}
	/* Free template font list */

   VOID
free_font_list(struct TemplateList  *tl)
{
   struct TemplateFont  *tf;
   struct List          *list = (struct List *)&tl->tl_Fonts;

   while (tf = (struct TemplateFont *)RemHead(list)) {
      free_template_font(tf);
   }
}
	/* Free template font */

   VOID
free_template_font(struct TemplateFont  *tf)
{
   if (tf) {
      DosFreeMem(tf->tf_TextAttr.ta_Name);
      FreeMem(tf, (LONG)sizeof(struct TemplateFont));
   }
}
