summaryrefslogtreecommitdiff
path: root/Xprint/ps/PsFonts.c
blob: 515a3188430e8e29b1c0a7e44f0ec8b4400d4fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/* $Xorg: PsFonts.c,v 1.6 2001/03/06 16:30:15 pookie Exp $ */
/*

Copyright 1996, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/*
 * (c) Copyright 1996 Hewlett-Packard Company
 * (c) Copyright 1996 International Business Machines Corp.
 * (c) Copyright 1996 Sun Microsystems, Inc.
 * (c) Copyright 1996 Novell, Inc.
 * (c) Copyright 1996 Digital Equipment Corp.
 * (c) Copyright 1996 Fujitsu Limited
 * (c) Copyright 1996 Hitachi, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the names of the copyright holders
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * from said copyright holders.
 */

/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PsFonts.c
**    *
**    *  Contents:	Font code for PS driver.
**    *
**    *  Created By:	Roger Helmendach (Liberty Systems)
**    *
**    *  Copyright:	Copyright 1996 The Open Group, Inc.
**    *
**    *********************************************************
** 
********************************************************************/

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "regionstr.h"
#include <X11/fonts/fontstruct.h>
#include "dixfontstr.h"
#include "scrnintstr.h"
#include <X11/fonts/fontxlfd.h>
#include <X11/fonts/fntfil.h>
#include <X11/fonts/fntfilst.h>

#include "Ps.h"

#include <ctype.h>
#include <limits.h>
#include <sys/stat.h>

Bool
PsRealizeFont(
  ScreenPtr  pscr,
  FontPtr    pFont)
{
  return TRUE;
}

Bool
PsUnrealizeFont(
  ScreenPtr  pscr,
  FontPtr    pFont)
{
  return TRUE;
}

char *
PsGetFontName(FontPtr pFont)
{
  int         i;
  int         nprops = pFont->info.nprops;
  FontPropPtr props  = pFont->info.props;
  Atom        name   = MakeAtom("FONT", 4, True);
  Atom        value  = (Atom)0;

  for( i=0 ; i<nprops ; i++ )
  {
    if( (Atom)props[i].name==name )
      { value = props[i].value; break; }
  }
  if( !value ) return (char *)0;
  return NameForAtom(value);
}

int
PsGetFontSize(FontPtr pFont, float *mtx)
{
  FontScalableRec   vals;
  char             *name = PsGetFontName(pFont);
  int               value = 0;

  FontParseXLFDName(name, &vals, FONT_XLFD_REPLACE_NONE);
  if( vals.values_supplied&PIXELSIZE_ARRAY )
  {
    int  i;
    for( i=0 ; i<4 ; i++ )
      mtx[i] = (float)vals.pixel_matrix[i];
  }
  else
  {
    value = vals.pixel;
    if( !value ) value = 20;
  }
  return value;
}

char *
PsGetPSFontName(FontPtr pFont)
{
  int         i;
  int         nprops = pFont->info.nprops;
  FontPropPtr props  = pFont->info.props;
  /* "_ADOBE_POSTSCRIPT_FONTNAME" maps directly to a PMF OBJ_NAME attribute
   * name - changing the name will break printer-builtin fonts. */
  Atom        name   = MakeAtom("_ADOBE_POSTSCRIPT_FONTNAME", 26, True); 
  Atom        value  = (Atom)0;

  for( i=0 ; i<nprops ; i++ )
  {
    if( (Atom)props[i].name==name )
      { value = props[i].value; break; }
  }
  if( !value ) return (char *)0; 
  return NameForAtom(value);
}

int
PsIsISOLatin1Encoding(FontPtr pFont)
{
  int          i;
  int          nprops = pFont->info.nprops;
  FontPropPtr  props  = pFont->info.props;
  Atom         reg = MakeAtom("CHARSET_REGISTRY", 16, True);
  Atom         enc = MakeAtom("CHARSET_ENCODING", 16, True);
  Atom         rv = 0, ev = 0;
  char        *rp = 0;
  char        *ep = 0;

  for( i=0 ; i<nprops ; i++ )
  {
    if( (Atom)props[i].name==reg ) rv = props[i].value;
    if( (Atom)props[i].name==enc ) ev = props[i].value;
  }
  if( rv ) rp = NameForAtom(rv);
  if( ev ) ep = NameForAtom(ev);
  if( (!rp) || (!ep) ) return(0);
  if( (char)tolower(rp[0])!='i' ||
      (char)tolower(rp[1])!='s' ||
      (char)tolower(rp[2])!='o' ||
      memcmp(&rp[3], "8859", 4)!=0 ||
      ep[0]!='1' ) return(0);
  return(1);
}

/* Return the encoding part of the XLFD (e.g. "*-iso8859-6.8x" etc.)*/
char *PsGetEncodingName(FontPtr pFont)
{
  int          i;
  int          nprops = pFont->info.nprops;
  FontPropPtr  props  = pFont->info.props;
  Atom         fnt = MakeAtom("FONT",              4, True);
  Atom         reg = MakeAtom("CHARSET_REGISTRY", 16, True);
  Atom         enc = MakeAtom("CHARSET_ENCODING", 16, True);
  Atom         fv = 0, rv = 0, ev = 0;
  char        *fp = 0;
  char        *rp = 0;
  char        *ep = 0;
  char        *encname;

  for( i=0 ; i<nprops ; i++ )
  {
    if( props[i].name==fnt ) fv = props[i].value;
    if( props[i].name==reg ) rv = props[i].value;
    if( props[i].name==enc ) ev = props[i].value;
  }
  if( fv ) fp = NameForAtom(fv);
  if( rv ) rp = NameForAtom(rv);
  if( ev ) ep = NameForAtom(ev);

  if( (!rp) || (!ep) || (!fp))
    return(0);
  
  encname  = fp;
  encname += strlen(encname) - (strlen(rp) + strlen(ep) + 1);
  
  return encname;
}

/* strstr(), case-insensitive */
static 
char *str_case_str(const char *s, const char *find)
{
  size_t len;
  char   c, 
         sc;

  if ((c = tolower(*find++)) != '\0')
  {
    len = strlen(find);
    do 
    {
      do
      {
        if ((sc = tolower(*s++)) == '\0')
          return NULL;
      } while (sc != c);
    } while (strncasecmp(s, find, len) != 0);
    s--;
  }
  return ((char *)s);
}

/* Check if the font path element is a directory which can be examined
 * (for example the font may be from a font server
 * (e.g. pFont->fpe->name == "tcp/:7100"))
 */
static
Bool IsFPEaReadableDir(FontPtr pFont)
{
  const char *fpe_name = pFont->fpe->name;
  if (!fpe_name)
    return False;

#define MODEL_FONTPATH_PREFIX     "PRINTER:"
#define MODEL_FONTPATH_PREFIX_LEN 8
  /* Strip model-specific font path prefix if there is one... */
  if (!strncmp(fpe_name, MODEL_FONTPATH_PREFIX, MODEL_FONTPATH_PREFIX_LEN))
    fpe_name += MODEL_FONTPATH_PREFIX_LEN;
    
  if (access(fpe_name, F_OK) == 0)
  {
    return True;
  }

  return False;
}

static
char *getFontFilename(FontPtr pFont)
{
  FontDirectoryPtr   dir;
  const char        *dlfnam;
  FILE              *file;
  struct stat        statb;
  int                count, i, status;
  char               buf[512];
  char              *front, *end, *fn;
  char               font_dir_fname[PATH_MAX],  /* Full path of fonts.dir */
                     font_file_fname[PATH_MAX]; /* Name of font file (excluding path) */

#ifdef XP_USE_FREETYPE
  if( PsIsFreeTypeFont(pFont) )
  {
    const char  *fontname = PsGetFTFontFileName(pFont);

#ifdef DEBUG_gisburn
    fprintf(stderr, "getFontFilename: freetype font, file='%s'\n", fontname?fontname:"<NULL>");
#endif /* DEBUG_gisburn */  

    if( !fontname )
      return NULL;
      
    return strdup(fontname);
  }
#endif /* XP_USE_FREETYPE */

  if (!IsFPEaReadableDir(pFont))
  {
#ifdef DEBUG_gisburn
    fprintf(stderr, "getFontFilename: '%s' no valid font path on disk\n", pFont->fpe->name);
#endif /* DEBUG_gisburn */  
    return NULL;
  }

  dir = pFont->fpe->private;
  sprintf(font_dir_fname, "%s%s", dir->directory, "fonts.dir");  
  
  if (!(dlfnam = PsGetFontName(pFont)))
    return NULL;
  
  file = fopen(font_dir_fname, "r");
  if (file)
  {
    if (fstat (fileno(file), &statb) == -1)
      return NULL;

    while( fgets(buf, sizeof(buf)-1, file) )
    {             
      if ((fn = strstr(buf, " -")))
      {
        strcpy(font_file_fname, buf);
        font_file_fname[fn - buf] = '\0';
        fn++;
        if ((front = str_case_str(fn, "normal-")))
        {
          fn[front - fn] = '\0';
          if (str_case_str(dlfnam, fn))        
          {
            char full_font_file_path[PATH_MAX];

            fclose(file);
            
            sprintf(full_font_file_path, "%s%s", dir->directory, font_file_fname);

#ifdef xDEBUG_gisburn
            fprintf(stderr, "getFontFilename: returning '%s'\n", full_font_file_path);
#endif /* DEBUG_gisburn */
            return strdup(full_font_file_path); 
          }
        }
      }
    }
  }
  font_file_fname[0] = '\0';
  fclose(file);

#ifdef DEBUG_gisburn
  fprintf(stderr, "getFontFilename: returning NULL\n");
#endif /* DEBUG_gisburn */

  return NULL;
}

static
PsFontTypeInfoRec *PsFindFontTypeInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  PsContextPrivRec  *cPriv = PsGetPsContextPriv(pDrawable);
  PsFontTypeInfoRec *rec;
  const char        *psname;
  char              *font_filename;
  char              *encname;
#ifdef XP_USE_FREETYPE
  Bool               is_freetypefont;
#endif /* XP_USE_FREETYPE */
  
#ifdef XP_USE_FREETYPE
  is_freetypefont = PsIsFreeTypeFont(pFont);
#endif /* XP_USE_FREETYPE */
  encname         = PsGetEncodingName(pFont);

  /* First try: Search by PostScript font name */
  psname = PsGetPSFontName(pFont);
  if (psname)
  {
    for( rec = cPriv->fontTypeInfoRecords ; rec != NULL ; rec = rec->next )
    {
#ifdef XP_USE_FREETYPE
      if (is_freetypefont)
      {
        if (rec->adobe_ps_name)
        {
          if ((rec->font_type == PSFTI_FONT_TYPE_FREETYPE) &&
              (!strcmp(rec->adobe_ps_name, psname)) &&
              (!strcmp(rec->ft_download_encoding, encname)))
          {
            return rec;
          }
        }
      }
      else
#endif /* XP_USE_FREETYPE */
      {
        if (rec->adobe_ps_name)
        {
          if ((rec->font_type != PSFTI_FONT_TYPE_FREETYPE) &&
              (!strcmp(rec->adobe_ps_name, psname)))
          {
            return rec;
          }
        }
      }
    }
  }

  /* Last attempt: Search by filename */
  font_filename = getFontFilename(pFont);
  if (font_filename)
  {
    for( rec = cPriv->fontTypeInfoRecords ; rec != NULL ; rec = rec->next )
    {
      if (rec->filename)
      {
#ifdef XP_USE_FREETYPE
        if (is_freetypefont)
        {
          if ( (rec->font_type == PSFTI_FONT_TYPE_FREETYPE) &&
               (!strcasecmp(rec->filename, font_filename)) && 
               (!strcasecmp(rec->ft_download_encoding, encname)) )
          {
            free(font_filename);
            return rec;
          }
        }
        else
#endif /* XP_USE_FREETYPE */
        {
          if ( (rec->font_type != PSFTI_FONT_TYPE_FREETYPE) &&
               (!strcasecmp(rec->filename, font_filename)) )
          {
            free(font_filename);
            return rec;
          }
        }
      }
    }
    
    free(font_filename);
  }
  
  return NULL;
}

static
void PsAddFontTypeInfoRec(DrawablePtr pDrawable, PsFontTypeInfoRec *add_rec)
{ 
  PsContextPrivRec *cPriv = PsGetPsContextPriv(pDrawable);

  /* ToDO: Always move the last used entry to the top that the list get's
   * sorted in an efficient order... :-) */
  add_rec->next = cPriv->fontTypeInfoRecords;
  cPriv->fontTypeInfoRecords = add_rec;
}

static
Bool strcaseendswith(const char *str, const char *suffix)
{
  const char *s;

  s = str + strlen(str) - strlen(suffix);

  if (!strcasecmp(s, suffix))
    return True;

  return False;
}


static
int getFontFileType( const char *filename )
{
  int type;

  /* Is this a Adobe PostScript Type 1 binary font (PFB) ? */
  if( strcaseendswith(filename, ".pfb") )
  {
    type = PSFTI_FONT_TYPE_PS_TYPE1_PFB;
  }
  /* Is this a Adobe PostScript ASCII font (PFA) ? */
  else if( strcaseendswith(filename, ".pfa") )
  {
    type = PSFTI_FONT_TYPE_PS_TYPE1_PFA;
  }
  /* Is this a PMF(=Printer Metrics File) ? */
  else if( strcaseendswith(filename, ".pmf") )
  {
    type = PSFTI_FONT_TYPE_PMF;
  }
  /* Is this a TrueType font file ? */
  else if( strcaseendswith(filename, ".ttf") ||
           strcaseendswith(filename, ".ttc") ||
           strcaseendswith(filename, ".otf") ||
           strcaseendswith(filename, ".otc") )
  {
    type = PSFTI_FONT_TYPE_TRUETYPE;
  }
  else
  {
    type = PSFTI_FONT_TYPE_OTHER;
  }

#ifdef XP_USE_FREETYPE
  {
    XpContextPtr  pCon;
    char         *downloadfonts;
    pCon = XpGetPrintContext(requestingClient);
    downloadfonts = XpGetOneAttribute(pCon, XPPrinterAttr, "xp-psddx-download-fonts");
    if( downloadfonts )
    {
      /* Should we download PS Type1 fonts as PS Type1||Type3 ? */
      if( (type == PSFTI_FONT_TYPE_PS_TYPE1_PFA) &&
          (strstr(downloadfonts, "pfa") != NULL) )
      {
        type = PSFTI_FONT_TYPE_FREETYPE;
      }

      if( (type == PSFTI_FONT_TYPE_PS_TYPE1_PFB) &&
          (strstr(downloadfonts, "pfb") != NULL) )
      {
        type = PSFTI_FONT_TYPE_FREETYPE;
      }

      /* Should we download TrueType fonts as PS Type1||Type3 ? */
      if( (type == PSFTI_FONT_TYPE_TRUETYPE) &&
          ((strstr(downloadfonts, "ttf") != NULL) || 
           (strstr(downloadfonts, "ttc") != NULL) || 
           (strstr(downloadfonts, "otf") != NULL) || 
           (strstr(downloadfonts, "otc") != NULL)) )
      {
        type = PSFTI_FONT_TYPE_FREETYPE;
      }
    }
  }
#endif /* XP_USE_FREETYPE */

#ifdef DEBUG_gisburn
  fprintf(stderr, "getFontFileType: '%s' is %d\n", filename, (int)type);
#endif /* DEBUG_gisburn */
  return type;
}

PsFTDownloadFontType PsGetFTDownloadFontType(void)
{
  PsFTDownloadFontType  downloadfonttype;
  XpContextPtr          pCon;
  char                 *psfonttype;

  pCon       = XpGetPrintContext(requestingClient);
  psfonttype = XpGetOneAttribute(pCon, XPPrinterAttr, "xp-psddx-download-font-type");

  if( !psfonttype || !strlen(psfonttype) )
  {
    return PsFontType1; /* Default download font type is PS Type1 */
  }

  if( !strcmp(psfonttype, "bitmap") )
  {
    downloadfonttype = PsFontBitmap;
  }
  else if( !strcmp(psfonttype, "pstype3") )
  {
    downloadfonttype = PsFontType3;
  }
  else if( !strcmp(psfonttype, "pstype1") )
  { 
    downloadfonttype = PsFontType1;
  }
  else
  {
     FatalError("PS DDX: XPPrinterAttr/xp-psddx-download-freetype-font-type='%s' not implemented\n", psfonttype);
     return 0; /* NO-OP, FatalError() will call |exit()| */
  }
  
  return downloadfonttype;
}

static
PsFontTypeInfoRec *PsCreateFontTypeInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  char              *dlfnam;
  PsFontTypeInfoRec *rec;
  char              *psname;
  
  if (!(dlfnam = PsGetFontName(pFont)))
    return NULL;

  if (!(rec = (PsFontTypeInfoRec *)xalloc(sizeof(PsFontTypeInfoRec))))
    return NULL;
  memset(rec, 0, sizeof(PsFontTypeInfoRec));

  rec->next              = NULL;

  if ((rec->filename = getFontFilename(pFont)))
  {
    rec->font_type = getFontFileType(rec->filename);   
  }
  else
  {
    rec->filename  = NULL;
    rec->font_type = PSFTI_FONT_TYPE_OTHER;
  }

  rec->adobe_ps_name         = PsGetPSFontName(pFont);
#ifdef XP_USE_FREETYPE
  rec->ft_download_encoding  = PsGetEncodingName(pFont);
  rec->ft_download_font_type = PsGetFTDownloadFontType();
#endif /* XP_USE_FREETYPE */
  rec->download_ps_name      = NULL;

#define SET_FONT_DOWNLOAD_STATUS(rec, downloaded) { int i; for (i = 0 ; i < 256 ; i++) { (rec)->alreadyDownloaded[i]=(downloaded); } }

  /* Set some flags based on the font type */
  switch( rec->font_type )
  {
    case PSFTI_FONT_TYPE_PS_TYPE1_PFA:
    case PSFTI_FONT_TYPE_PS_TYPE1_PFB:
      rec->downloadableFont  = True;
      SET_FONT_DOWNLOAD_STATUS(rec, False);
      rec->is_iso_encoding = PsIsISOLatin1Encoding(pFont);
      break;

    case PSFTI_FONT_TYPE_PMF:
      rec->downloadableFont  = True; /* This font is in printer's ROM */
      SET_FONT_DOWNLOAD_STATUS(rec, True);
      rec->is_iso_encoding = PsIsISOLatin1Encoding(pFont);
      break;
    
    case PSFTI_FONT_TYPE_TRUETYPE:
      /* Note: TrueType font download not implemented */
      rec->downloadableFont  = False;
      SET_FONT_DOWNLOAD_STATUS(rec, False);
      rec->is_iso_encoding = PsIsISOLatin1Encoding(pFont);
      break;

#ifdef XP_USE_FREETYPE
    case PSFTI_FONT_TYPE_FREETYPE:
      if( rec->ft_download_font_type == PsFontType1 ||
          rec->ft_download_font_type == PsFontType3 )
      {
        rec->downloadableFont = True;
      }
      else
      {
        rec->downloadableFont = False;
      }

      SET_FONT_DOWNLOAD_STATUS(rec, False);
      rec->is_iso_encoding   = False; /* Freetype--->PS Type1/Type3 uses always non-iso PS encoding for now */
      break;
#endif /* XP_USE_FREETYPE */

    case PSFTI_FONT_TYPE_OTHER:
    default:
      rec->downloadableFont  = False;
      SET_FONT_DOWNLOAD_STATUS(rec, False);
      rec->is_iso_encoding = PsIsISOLatin1Encoding(pFont);
      break;
  }
  
#ifdef XP_USE_FREETYPE
  if( (rec->font_type == PSFTI_FONT_TYPE_FREETYPE) )
  {
    char *s;
    register int c;

    if( rec->adobe_ps_name )
    {
      rec->download_ps_name = malloc(strlen(rec->adobe_ps_name) + strlen(rec->ft_download_encoding) + 2);
      sprintf(rec->download_ps_name, "%s_%s", rec->adobe_ps_name, rec->ft_download_encoding);
    }
    else
    {
      /* Unfortunately not all TTF fonts have a PostScript font name (like
       * Solaris TTF fonts in /usr/openwin/lib/locale/ko.UTF-8/X11/fonts/TrueType,
       * /usr/openwin/lib/locale/ko/X11/fonts/TrueType) - in this case we
       * have to generate a font name
       */
      char        ftfontname[64];
      static long myfontindex = 0L;
      sprintf(ftfontname, "psfont_%lx", myfontindex++);
      
      rec->download_ps_name = malloc(strlen(ftfontname) + strlen(rec->ft_download_encoding) + 2);
      sprintf(rec->download_ps_name, "%s_%s", ftfontname, rec->ft_download_encoding);
      
      fprintf(stderr, "PsCreateFontTypeInfoRec: Note: '%s' has no PS font name, using '%s' for now.\n", dlfnam, rec->download_ps_name);
    }
    
    /* Make sure the font name we use for download is a valid PS font name */
    for( s = rec->download_ps_name ; *s != '\0'; s++ )
    {
      c = *s;
      
      /* Check for allowed chars, invalid ones are replaced with a '_'
       * (and check that the first char is not a digit) */
      if( !(isalnum(c) || c == '.' || c == '_' || c == '-') || (s==rec->download_ps_name && isdigit(c)) )
      {
        *s = '_';
      }
    }
  }
  else
#endif /* XP_USE_FREETYPE */
  {
    if( rec->adobe_ps_name )
    {
      rec->download_ps_name = strdup(rec->adobe_ps_name);
    }
    else
    {
      rec->download_ps_name = NULL;
    }
  }

  /* Safeguard - only treat font as downloadable when we have a PS font name!! */
  if (!rec->download_ps_name && rec->downloadableFont)
  {
    /* XXX: Log this message to the log when the logging service has been hook'ed up */
    fprintf(stderr, "PsCreateFontTypeInfoRec: Safeguard: No PS font name for '%s'!\n", dlfnam);
    rec->downloadableFont = False;
  }

#ifdef DEBUG_gisburn
  fprintf(stderr, "PsCreateFontTypeInfoRec: Created PsFontTypeInfoRec '%s' ('%s'/'%s')\n",
          ((rec->filename)        ?(rec->filename)     :("<null>")),
          ((rec->adobe_ps_name)   ?(rec->adobe_ps_name):("<null>")),
          ((rec->download_ps_name)?(rec->download_ps_name):("<null>")));
#endif /* DEBUG_gisburn */

  return rec;
}

static
PsFontTypeInfoRec *PsGetFontTypeInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  PsFontTypeInfoRec *rec;
  char              *dlfnam;

  if(!(dlfnam = PsGetFontName(pFont)))
    return NULL;
  
  rec = PsFindFontTypeInfoRec(pDrawable, pFont);
  if (rec)
    return rec;
    
  rec = PsCreateFontTypeInfoRec(pDrawable, pFont);
  if (!rec)
    return NULL;
  
  PsAddFontTypeInfoRec(pDrawable, rec);

  return rec;
}

static
void PsFreeFontTypeInfoRecords( PsContextPrivPtr priv )
{
  PsFontTypeInfoRec *curr, *next;
  curr = priv->fontTypeInfoRecords;
  while( curr != NULL )
  {
    if (curr->filename)
      free(curr->filename); /* Free memory allocated by |strdup()| */

    if (curr->download_ps_name)
      free(curr->download_ps_name);
      
    next = curr->next;
    xfree(curr);
    curr = next;
  }
}

static
PsFontInfoRec *PsFindFontInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  PsContextPrivRec *cPriv = PsGetPsContextPriv(pDrawable);
  PsFontInfoRec    *rec;

  if (!pFont)
    return NULL;

  for( rec = cPriv->fontInfoRecords ; rec != NULL ; rec = rec->next )
  {
    if ((rec->font == pFont) && 
        (rec->font_fontPrivate == pFont->fontPrivate))
      return rec;
  }
  
  return NULL;
}

static
void PsAddFontInfoRec(DrawablePtr pDrawable, PsFontInfoRec *add_rec)
{ 
  PsContextPrivRec *cPriv = PsGetPsContextPriv(pDrawable);

  /* ToDO: Always move the last used entry to the top that the list get's
   * sorted in an efficient order... :-) */
  add_rec->next = cPriv->fontInfoRecords;
  cPriv->fontInfoRecords = add_rec;
}

static
PsFontInfoRec *PsCreateFontInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  PsFontInfoRec     *rec;
  PsFontTypeInfoRec *ftir;
  
  if (!(ftir = PsGetFontTypeInfoRec(pDrawable, pFont)))
    return NULL;

  if (!(rec = (PsFontInfoRec *)xalloc(sizeof(PsFontInfoRec))))
    return NULL;
  memset(rec, 0, sizeof(PsFontInfoRec));

  rec->font     = pFont;
  rec->font_fontPrivate = pFont->fontPrivate;
  rec->ftir     = ftir;
  rec->next     = NULL;
  rec->dfl_name = PsGetFontName(pFont);
  rec->size     = PsGetFontSize(pFont, rec->mtx);

#ifdef DEBUG_gisburn
  fprintf(stderr, "PsCreateFontInfoRec: Created PsFontInfoRec '%s'\n",
          ((rec->dfl_name)?(rec->dfl_name):("<null>")));
#endif /* DEBUG_gisburn */

  return rec;
}

PsFontInfoRec *PsGetFontInfoRec(DrawablePtr pDrawable, FontPtr pFont)
{
  PsFontInfoRec *rec;

  rec = PsFindFontInfoRec(pDrawable, pFont);
  if (rec)
    return rec;
    
  rec = PsCreateFontInfoRec(pDrawable, pFont);
  if (!rec)
    return NULL;
  
  PsAddFontInfoRec(pDrawable, rec);

  return rec;
}

void PsFreeFontInfoRecords( PsContextPrivPtr priv )
{
  PsFontInfoRec *curr, *next;
  curr = priv->fontInfoRecords;
  while( curr != NULL )
  {  
    next = curr->next;
    xfree(curr);
    curr = next;
  }
  
  PsFreeFontTypeInfoRecords(priv);

  priv->fontTypeInfoRecords = NULL;
  priv->fontInfoRecords     = NULL;
}