/*  SVGATextMode -- An SVGA textmode manipulation/enhancement tool
 *
 *  Copyright (C) 1995  Koen Gadeyne
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/***
 *** parse_font.c, font config parsing functions
 *** (c) 1995 Koen Gadeyne (kmg@barco.be)
 ***/


#include "misc.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "confdefs.h"
#include "configfile.h"
#include "messages.h"
#include "parse_font.h"

 /* 
  * get font program path and font path. If not defined, default path will be used.
  * also get the default font table.
  */

t_fontdef* parse_font(FILE* param_file, t_fontdef* f)
  {
    int i=0, j=0, x=0, y=0;
    char *arg_str, *prev_arg_str;
    int result;
    char* tmpmem; /* for font specification strings */
       
  
    /* defaults */
    strcpy(f->fontpath, DEFAULT_FONTPATH);
    strcpy(f->fontprogpath, DEFAULT_FONTPROGPATH);
    
    /* parsing */
    if (!GetPathOption(param_file, "FontProg", LABEL_OPTIONAL+LABEL_FROMSTART, f->fontprogpath))
      PDEBUG(("Using default FontProg path '%s'", f->fontprogpath));
    if (!GetPathOption(param_file, "FontPath", LABEL_OPTIONAL+LABEL_FROMSTART, f->fontpath))
      PDEBUG(("Using default Font path '%s'", f->fontpath));
      
    for (i=0; i<2; i++) for (j=0; j<32; j++) f->font_table[i][j] = NULL; /* clear font table */

    if ((arg_str = findlabel(param_file, "FontSelect", LABEL_OPTIONAL+LABEL_FROMSTART)))
    {
      strtok(arg_str," ");
      do
        {
          if (!(tmpmem = (char *)malloc(256)) ) PERROR(("Could not allocate 256 bytes!"));
          result=0;
          prev_arg_str = NULL;
          while ((arg_str=strtok(NULL," ")))    /* get all font sizes from this one FontSelect line */
          {
            result++;
            if (prev_arg_str)
            {
              ParseFontXY(prev_arg_str, &x, &y);
              f->font_table[x-8][y-1] = tmpmem;
            }
            prev_arg_str = arg_str;
          }
          if (result <2) PERROR(("Incomplete 'FontSelect' line: needs at least one font size and a font name."));
          /* prev_arg_str should now be the font name */
          strcpy(tmpmem, prev_arg_str);
/*          PDEBUG(("FontSelect: Will use '%s' font file for all %dx%d modes",f->font_table[i][j], i+8, j+1)); */
        }
      while ((arg_str=strtok(findlabel(param_file, "FontSelect", LABEL_OPTIONAL+LABEL_FROMCURRENT)," ")));
    }
    return(f);
  }

