#ifndef GRAPHICS_TEXT_H
#include <graphics/text.h>
#endif

#include "MyLib.h"

/****** MyLib.lib/MakeTextAttr *******************************************
*
*    NAME
*	MakeTextAttr -- fill a struct TextAttr from a spec string
*
*    SYNOPSIS
*	Error = MakeTextAttr(Spec, TextAttr)
*
*	LONG MakeTextAttr(const char *, struct TextAttr *);
*
*    FUNCTION
*	This function is for use with bitmap fonts. It parses
*	a font specification string using ReadArgs(), filling
*	the supplied TextAttr to match the font specification.
*
*	The format of a font specification string is:
*
*	   NAME/A,SIZE/N/A,UNDERLINE/S,BOLD/S,SLANTED/S
*
*	NAME:          Name of the font, with or without ".font".
*	SIZE:          Pixel size of the font. Must be >0 and <32768,
*	               else ERROR_BAD_NUMBER is returned.
*	UNDERLINED \
*	BOLD        -> Font styles
*	SLANTED    /
*
*    INPUTS
*	Spec     - A font specification string
*	TextAttr - Result
*
*    RESULT
*	Error    - 0 for success, or a dos error code. In case of
*	           an error, the TextAttr is not touched.
*
*    NOTE
*	TextAttr.ta_Name is allocated with AllocVec().
*
*************************************************************************/

LONG MakeTextAttr(const char *Spec, struct TextAttr *TextAttr)

{
  struct RDArgs *RDArgs;
  struct
    {
      char *Name;
      LONG *Size;
      LONG Underlined;
      LONG Bold;
      LONG Slanted;
    } Arguments;
  LONG Error;

  Error=0;
  memset(&Arguments,0,sizeof(Arguments));
  assert(Arguments.Name==NULL && Arguments.Size==NULL);
  if ((RDArgs=ParseString("NAME/A,SIZE/N/A,UNDERLINED/S,BOLD/S,SLANTED/S",Spec,&Arguments))!=NULL)
    {
      if (*Arguments.Size<=0)
	{
	  Error=ERROR_BAD_NUMBER;
	}
      else
	{
	  ULONG NameSize;
	  int Suffix;
	  char *Name;

	  NameSize=strlen(Arguments.Name);
	  Suffix=!(NameSize<6 || strcmp(Arguments.Name+NameSize-5,".font")!=0);
	  if (!Suffix)
	    {
	      NameSize+=5;
	    }
	  if ((Name=AllocVec(NameSize+1,0))!=NULL)
	    {
	      char *t;

	      t=stpcpy(Name,Arguments.Name);
	      if (!Suffix)
		{
		  stpcpy(t,".font");
		}
	      TextAttr->ta_Name=Name;
	    }
	  else
	    {
	      Error=ERROR_NO_FREE_STORE;
	    }
	}
      if (Error==0)
	{
	  TextAttr->ta_YSize=(UWORD)*Arguments.Size;
	  TextAttr->ta_Flags=0;
	  TextAttr->ta_Style=0;
	  if (Arguments.Underlined)
	    {
	      TextAttr->ta_Style|=FSF_UNDERLINED;
	    }
	  if (Arguments.Bold)
	    {
	      TextAttr->ta_Style|=FSF_BOLD;
	    }
	  if (Arguments.Slanted)
	    {
	      TextAttr->ta_Style|=FSF_ITALIC;
	    }
	}
      ParseStringDone(RDArgs);
    }
  return Error;
}
