/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
 *
 * This file is part of the TeX Font Tools package.
 *
 * Copyright  1997, 98 Jakob Stoklund Olesen.
 *
 * 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.
 *
 *- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

static char     rcsid[] =
"$Id: TFTLib.c.FontCommon 4.1 1998/02/13 17:24:14 stoklund Stable $";

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
 * $Revision: 4.1 $	$Date: 1998/02/13 17:24:14 $
 *
 * Functions which are used by both FontRead.c and FontWrite.c
 * $Log: TFTLib.c.FontCommon $
 * Revision 4.1  1998/02/13 17:24:14  stoklund
 * (Fontfile_Delete): New function
 * (Fontfile_FreeCharacter, Fontfile_FreeScaffold): Moved from FontWrite.c
 *
 * Revision 3.1  1997/11/01 12:46:32  stoklund
 * *** empty log message ***
 *
 * Revision 2.1  1997/07/04 20:43:34  stoklund
 * Copyright fix, Log cleanup
 *
 *- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

#include <stdlib.h>
#include "Debug.h"
#include "Fontfile.h"

/* Get the character data for cc, or NULL if undefined */
Fontfile_Character *
Fontfile_GetCharacter(Fontfile * ff, int cc)
{
  int             chunk;

  chunk = cc >> 5;
  if (chunk >= ff->nchunks || !ff->chunk[chunk].character)
    return NULL;
  /* NOTE no support for subpixeling */
  return ff->chunk[chunk].character[0][cc & 0x1f];
}

/* Delete all data structures */
void
Fontfile_Delete(Fontfile * ff)
{
  int             n, m, ch;

  /* Free the scaffold structures */
  if (ff->scaffold != NULL) {
    for (n = 0; n < ff->ns; n++)
      if (ff->scaffold[n] != NULL)
        Fontfile_FreeScaffold(ff->scaffold[n]);
    free(ff->scaffold);
    ff->scaffold = NULL;
  }
  /* Now the chunks */
  if (ff->chunk != NULL) {
    for (ch = 0; ch < ff->nchunks; ch++) {
      if (ff->chunk[ch].character) {
        /* for each character def in the chunk */
        for (m = 0; m < ff->chunk[ch].mult; m++) {
          for (n = 0; n < 32; n++) {
            if (ff->chunk[ch].character[m][n] != NULL)
              Fontfile_FreeCharacter(ff->chunk[ch].character[m][n]);
          }
        }
        free(ff->chunk[ch].character);
      }
    }
    free(ff->chunk);
    ff->chunk = NULL;
  }
}

/*
 * Free all the data in a character definition AND the structure
 * itself
 */
void
Fontfile_FreeCharacter(Fontfile_Character * ch)
{
  if (ch->data)
    free(ch->data);
  if (ch->outline)
    free(ch->outline);
  if (ch->composite)
    free(ch->composite);
  free(ch);
}

/* Free the data in a scaffold structure AND the structure itself */
void
Fontfile_FreeScaffold(Fontfile_Scaffold * sc)
{
  if (sc->line)
    free(sc->line);
  free(sc);
}
