/*
Copyright (c) 2004-2005, Dirk Krause
All rights reserved.

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.
* Redistributions in binary form must reproduce the above 
  opyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials
  provided with the distribution.
* Neither the name of the Dirk Krause nor the names of
  its contributors may be used to endorse or promote
  products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/

#define DKFIGFNT_C 1
#include "dkfig.h"


#line 41 "dkfigfnt.ctr"



#if VERSION_BEFORE_2004_08_19

/* {{{ fig_font_35

  The 35 standard PostScript fonts.

*/
dk_fig_font fig_font_35[] = {
  /*  0 */
  { "ptmr",   "Times-Roman",
              DKFIG_FONT_RM },
  /*  1 */
  { "ptmri",  "Times-Italic",
              DKFIG_FONT_RM | DKFIG_FONT_IT },
  /*  2 */
  { "ptmb",   "Times-Bold",
              DKFIG_FONT_RM | DKFIG_FONT_BD },
  /*  3 */
  { "ptmbi",  "Times-BoldItalic",
              DKFIG_FONT_RM | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /*  4 */
  { "pagk",   "AvantGarde-Book",
              DKFIG_FONT_SF },
  /*  5 */
  { "pagko",  "AvantGarde-BookOblique",
              DKFIG_FONT_SF | DKFIG_FONT_IT },
  /*  6 */
  { "pagd",   "AvantGarde-Demi",
              DKFIG_FONT_SF | DKFIG_FONT_BD },
  /*  7 */
  { "pagdo",  "AvantGarde-DemiOblique",
              DKFIG_FONT_SF | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /*  8 */
  { "pbkl",   "Bookman-Light",
              DKFIG_FONT_RM },
  /*  9 */
  { "pbkli",  "Bookman-LightItalic",
              DKFIG_FONT_RM | DKFIG_FONT_IT },
  /* 10 */
  { "pbkd",   "Bookman-Demi",
              DKFIG_FONT_RM | DKFIG_FONT_BD },
  /* 11 */
  { "pbkdi",  "Bookman-DemiItalic",
              DKFIG_FONT_RM | DKFIG_FONT_IT | DKFIG_FONT_BD },
  /* 12 */
  { "pcrr",   "Courier",
              DKFIG_FONT_TT },
  /* 13 */
  { "pcrro",  "Courier-Oblique",
              DKFIG_FONT_TT | DKFIG_FONT_IT },
  /* 14 */
  { "pcrb",   "Courier-Bold",
              DKFIG_FONT_TT | DKFIG_FONT_BD },
  /* 15 */
  { "pcrbo",  "Courier-BoldOblique",
              DKFIG_FONT_TT | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /* 16 */
  { "phvr",   "Helvetica",
              DKFIG_FONT_SF },
  /* 17 */
  { "phvro",  "Helvetica-Oblique",
              DKFIG_FONT_SF | DKFIG_FONT_IT },
  /* 18 */
  { "phvb",   "Helvetica-Bold",
              DKFIG_FONT_SF | DKFIG_FONT_BD },
  /* 19 */
  { "phvbo",  "Helvetica-BoldOblique",
              DKFIG_FONT_SF | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /* 20 */
  { "phvrrn", "Helvetica-Narrow",
              DKFIG_FONT_SF },
  /* 21 */
  { "phvron", "Helvetica-Narrow-Oblique",
              DKFIG_FONT_SF | DKFIG_FONT_IT },
  /* 22 */
  { "phvbrn", "Helvetica-Narrow-Bold",
              DKFIG_FONT_SF | DKFIG_FONT_BD },
  /* 23 */
  { "phvbon", "Helvetica-Narrow-BoldOblique",
              DKFIG_FONT_SF | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /* 24 */
  { "pncr",   "NewCenturySchlbk-Roman",
              DKFIG_FONT_RM },
  /* 25 */
  { "pncri",  "NewCenturySchlbk-Italic",
              DKFIG_FONT_RM | DKFIG_FONT_IT },
  /* 26 */
  { "pncb",   "NewCenturySchlbk-Bold",
              DKFIG_FONT_RM | DKFIG_FONT_BD },
  /* 27 */
  { "pncbi",  "NewCenturySchlbk-BoldItalic",
              DKFIG_FONT_RM | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /* 28 */
  { "pplr",   "Palatino-Roman",
              DKFIG_FONT_RM },
  /* 29 */
  { "pplri",  "Palatino-Italic",
              DKFIG_FONT_RM | DKFIG_FONT_IT },
  /* 30 */
  { "pplb",   "Palatino-Bold",
              DKFIG_FONT_RM | DKFIG_FONT_BD },
  /* 31 */
  { "pplbi",  "Palatino-BoldItalic",
              DKFIG_FONT_RM | DKFIG_FONT_BD | DKFIG_FONT_IT },
  /* 32 */
  { "psyr",   "Symbol",
              0 },
  /* 33 */
  { "pzcmi",  "ZapfChancery-MediumItalic",
              DKFIG_FONT_RM | DKFIG_FONT_IT },
  /* 34 */
  { "pzdr",   "ZapfDingbats",
              0 }
};
/* }}} */



/* {{{ dkfig_fnt_get_tex_name

  Get LaTeX font name for a given index.

*/
char *
dkfig_fnt_get_tex_name DK_P1(size_t, number)
{
  char *back = NULL;
  
  if(number < 35) {
    back = fig_font_35[number].texname;
  }
  
  return back;
}
/* }}} */



/* {{{ dkfig_fnt_get_ps_name

  Get PS font name for a given index.

*/
char *
dkfig_fnt_get_ps_name DK_P1(size_t, number)
{
  char *back = NULL;
  
  if(number < 35) {
    back = fig_font_35[number].psname;
  }
  
  return back;
}
/* }}} */

#endif


/* {{{ dkfig_fnt_comp_fonth

  Compare two font handling data structures.

*/
int
dkfig_fnt_comp_fonth DK_P3(void *,pl, void *,pr, int,cr)
{
  int back = 0;
  dk_fig_fonth_t *fl, *fr;
  
  fl = (dk_fig_fonth_t *)pl; fr = (dk_fig_fonth_t *)pr;
  if(fl) {
    if(fr) {
      if((fl->handling) > (fr->handling)) {
        back = 1;
      } else {
        if((fl->handling) < (fr->handling)) {
	  back = -1;
	}
      }
      if(!back) {
        if((fl->fontno) > (fr->fontno)) {
	  back = 1;
	} else {
	  if((fl->fontno < fr->fontno)) {
	    back = -1;
	  }
	}
      }
      if(!back) {
        double d1, d2, d3;
	d1 = fabs(fl->fontsize); d2 = fabs(fr->fontsize);
	d3 = ((d1 < d2) ? (d1) : (d2));
	d3 = d3 * 0.0001;
	if(fabs((fl->fontsize) - (fr->fontsize)) > d3) {
        if((fl->fontsize) > (fr->fontsize)) {
	  back = 1;
	} else {
	  if((fl->fontsize) < (fr->fontsize)) {
	    back = -1; 
	  }
	}
	}
      }
      if(!back) {
        if(fl->ofontno < fr->ofontno) {
	  back = -1;
	} else {
	  if(fl->ofontno > fr->ofontno) {
	    back = 1;
	  }
	}
      }
      if(!back) {
        if(fl->oflags > fr->oflags) {
	  back = 1;
	} else {
	  if(fl->oflags < fr->oflags) {
	    back = -1;
	  }
	}
      }
    } else {
      back = 1;
    }
  } else {
    if(fr) {
      back = -1;
    }
  }
  
  return back;
}
/* }}} */



/* {{{ figlatfont_to_number

  Translate LaTeX font selection from Fig file
  into index in PS fonts array.

*/
static int
figlatfont_to_number DK_P1(int, i)
{ 
  int back = 0;
  
  switch(i) {
    case 1: {			
      back = 0;
    } break;
    case 2: {			
      back = 2;
    } break;
    case 3: {			
      back = 1;
    } break;
    case 4: {			
      back = 16;
    } break;
    case 5: {			
      back = 12;
    } break;
    default: {			
      back = 0;
    } break;
  } 
  return back;
}
/* }}} */



/* {{{ figlatfont_to_features

  Translate font LaTeX font selection in XFig into
  a bit set of font features (roman,bold...).
*/
static int
figlatfont_to_features DK_P1(int, i)
{ 
  int back = 0;
  
  switch(i) {
    case 1: {			
      back = DKFIG_FONT_RM;
    } break;
    case 2: {			
      back = DKFIG_FONT_BD;
    } break;
    case 3: {			
      back = DKFIG_FONT_IT;
    } break;
    case 4: {			
      back = DKFIG_FONT_SF;
    } break;
    case 5: {			
      back = DKFIG_FONT_TT;
    } break;
    default: {			
      back = 0;
    } break;
  } 
  return back;
}
/* }}} */



/* {{{ dkfig_fnt_fonth_for_text

  Fill font handling data structure to match the
  requirements of a text object.

*/
void
dkfig_fnt_fonth_for_text DK_P4(dk_fig_fonth_t *,f,\
  int,n, int,s, dk_fig_text *,t)
{
  int th;
  int ofn;	/* original font number */
  
  f->handling = 0; f->fontno = 0; f->fontsize = 12.0;
  f->ofontno  = 0; f->oflags = 0;
  if(f && t) {
    f->ofontno = t->font;
    f->oflags  = t->font_flags;
    th = (((t->font_flags) & 2) ? s : n); 
    ofn = t->font; if(ofn < 0) ofn = 0; if(ofn >= 35) ofn = 34;
    f->handling = 0; f->fontno = ofn; f->fontsize = t->font_size;
    
    if(((t->font_flags) & 2) || (th & DKFIG_TH_HANDLING)) {	
      /* Options: text handled by LaTeX */
      if(th & DKFIG_TH_FONT_TEX) {		
        f->handling = 2;
      } else {					
        if(th & DKFIG_TH_FONT_SIMILAR) {	
	  if(th & DKFIG_TH_SIZE_FIG) {		
	    f->handling = 4;
	  } else {				
	    f->handling = 5;
	  }
	  if((t->font_flags) & 4) {		
	    /* f->fontno = fig_font_35[ofn].features; */
	    f->fontno = dkfont_get_features(ofn);
	  } else {				
	    f->fontno = figlatfont_to_features(ofn);
	  }
	} else {				
	  if((t->font_flags) & 4) {		
	    f->handling = 3;
	  } else {				
	    f->handling = 4;
	    f->fontno = figlatfont_to_features(ofn);
	  }
	}
      }
    } else {						
      /* Options: no LaTeX handling */
      if((t->font_flags) & 4) {			
        /* FIG: no LaTeX handling */
	if(th & DKFIG_TH_FONT_TEX) {		
	  f->handling = 0;
	} else {				
	  f->handling = 1;
	}
      } else {					
        /* FIG: text handled by LaTeX */
        /*
	  2004/05/13
	  As the options advice us not to use LaTeX we
	  convert the font number directly to use a
	  PS font.
	*/
	f->fontno = figlatfont_to_number(ofn);
	f->handling = ((th & DKFIG_TH_FONT_TEX) ? 0 : 1);
      }
    }
  }
  
}
/* }}} */



/* {{{ dkfig_fnt_new_fonth

  Create font handling data structure.

*/
dk_fig_fonth_t *
dkfig_fnt_new_fonth DK_P0()
{
  dk_fig_fonth_t *back = NULL;
  
  back = dk_new(dk_fig_fonth_t,1);
  if(back) {
    back->handling = 0;
    back->fontno   = 0;
    back->fontsize = 0.0;
    back->fonthno  = 0UL;
    back->ofontno  = 0;
    back->oflags   = 0;
  }
  
  return back;
}
/* }}} */



/* {{{ dkfig_fnt_copy_fonth

  Copy font handling data structure.

*/
dk_fig_fonth_t *
dkfig_fnt_copy_fonth DK_P1(dk_fig_fonth_t *,f)
{
  dk_fig_fonth_t *back = NULL;
  
  if(f) {
    back = dk_new(dk_fig_fonth_t,1);
    if(back) {
      back->handling = f->handling;
      back->fontno   = f->fontno;
      back->fontsize = f->fontsize;
      back->ofontno  = f->ofontno;	/* bug fixed: was ... = f->fontno */
      back->oflags   = f->oflags;
    }
  }
  
  return back;
}
/* }}} */



/* {{{ dkfig_fnt_del_fonth

  Destroy font handling data structure.

*/
void
dkfig_fnt_del_fonth DK_P1(dk_fig_fonth_t *,f)
{
  
  if(f) { 
    dk_delete(f);
  }
  
}
/* }}} */




/* {{{ SCCS ID */
#ifndef LINT
static char sccs_id[] = {
"@(#)dkfigfnt.ctr 1.105 05/29/07\t(krause) - fig2vect"
};
#endif
/* }}} */
