Eliminate __compar_fn_t.
[wine] / dlls / wineps / afm.c
1 /*
2  *      Adobe Font Metric (AFM) file parsing
3  *      See http://partners.adobe.com/asn/developer/PDFS/TN/5004.AFM_Spec.pdf
4  *
5  *      Copyright 1998  Huw D M Davies
6  * 
7  */
8
9 #include <string.h>
10 #include <stdlib.h>     /* qsort() & bsearch() */
11 #include <stdio.h>
12 #include <sys/stat.h>
13 #include <dirent.h>
14 #include <limits.h>     /* INT_MIN */
15 #include <float.h>      /* FLT_MAX */
16 #include "winnt.h"      /* HEAP_ZERO_MEMORY */
17 #include "psdrv.h"
18 #include "options.h"
19 #include "debugtools.h"
20 #include "heap.h"
21
22 DEFAULT_DEBUG_CHANNEL(psdrv);
23 #include <ctype.h>
24
25 /* ptr to fonts for which we have afm files */
26 FONTFAMILY *PSDRV_AFMFontList = NULL;
27
28 /* qsort/bsearch callback functions */
29 typedef int (*compar_callback_fn) (const void *, const void *);
30
31 /*******************************************************************************
32  *      CheckMetrics
33  *
34  *  Check an AFMMETRICS structure to make sure all elements have been properly
35  *  filled in.  (Don't check UV or L.)
36  *
37  */
38 static const AFMMETRICS badMetrics =
39 {
40     INT_MIN,                                    /* C */
41     INT_MIN,                                    /* UV */
42     FLT_MAX,                                    /* WX */
43     NULL,                                       /* N */
44     { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX },     /* B */
45     NULL                                        /* L */
46 };
47
48 inline static BOOL CheckMetrics(const AFMMETRICS *metrics)
49 {
50     if (    metrics->C      == badMetrics.C     ||
51             metrics->WX     == badMetrics.WX    ||
52             metrics->N      == badMetrics.N     ||
53             metrics->B.llx  == badMetrics.B.llx ||
54             metrics->B.lly  == badMetrics.B.lly ||
55             metrics->B.urx  == badMetrics.B.urx ||
56             metrics->B.ury  == badMetrics.B.ury )
57         return FALSE;
58         
59     return TRUE;
60 }
61
62 /*******************************************************************************
63  *  FreeAFM
64  *
65  *  Free an AFM structure and any subsidiary objects that have been allocated.
66  *  AFM must have been allocated with HEAP_ZERO_MEMORY.
67  *
68  */
69 static void FreeAFM(AFM *afm)
70 {
71     if (afm->FontName != NULL)
72         HeapFree(PSDRV_Heap, 0, afm->FontName);
73     if (afm->FullName != NULL)
74         HeapFree(PSDRV_Heap, 0, afm->FullName);
75     if (afm->FamilyName != NULL)
76         HeapFree(PSDRV_Heap, 0, afm->FamilyName);
77     if (afm->EncodingScheme != NULL)
78         HeapFree(PSDRV_Heap, 0, afm->EncodingScheme);
79     if (afm->Metrics != NULL)
80         HeapFree(PSDRV_Heap, 0, afm->Metrics);
81         
82     HeapFree(PSDRV_Heap, 0, afm);
83 }
84
85 /***********************************************************
86  *
87  *      PSDRV_AFMGetCharMetrics
88  *
89  * Parses CharMetric section of AFM file.
90  *
91  * Actually only collects the widths of numbered chars and puts then in
92  * afm->CharWidths.
93  */
94 static BOOL PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
95 {
96     unsigned char line[256], valbuf[256];
97     unsigned char *cp, *item, *value, *curpos, *endpos;
98     int i;
99     AFMMETRICS *metric;
100
101     afm->Metrics = metric = HeapAlloc( PSDRV_Heap, 0,
102                                        afm->NumofMetrics * sizeof(AFMMETRICS) );
103     if (metric == NULL)
104         return FALSE;
105                                        
106     for(i = 0; i < afm->NumofMetrics; i++, metric++) {
107     
108         *metric = badMetrics;
109
110         do {
111             if(!fgets(line, sizeof(line), fp)) {
112                 ERR("Unexpected EOF\n");
113                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
114                 afm->Metrics = NULL;
115                 return FALSE;
116             }
117             cp = line + strlen(line);
118             do {
119                 *cp = '\0';
120                 cp--;
121             } while(cp >= line && isspace(*cp));
122         } while (!(*line));
123
124         curpos = line;
125         while(*curpos) {
126             item = curpos;
127             while(isspace(*item))
128                 item++;
129             value = strpbrk(item, " \t");
130             if (!value) {
131                 ERR("No whitespace found.\n");
132                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
133                 afm->Metrics = NULL;
134                 return FALSE;
135             }
136             while(isspace(*value))
137                 value++;
138             cp = endpos = strchr(value, ';');
139             if (!cp) {
140                 ERR("missing ;, failed. [%s]\n", line);
141                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
142                 afm->Metrics = NULL;
143                 return FALSE;
144             }
145             while(isspace(*--cp))
146                 ;
147             memcpy(valbuf, value, cp - value + 1);
148             valbuf[cp - value + 1] = '\0';
149             value = valbuf;
150
151             if(!strncmp(item, "C ", 2)) {
152                 value = strchr(item, ' ');
153                 sscanf(value, " %d", &metric->C);
154
155             } else if(!strncmp(item, "CH ", 3)) {
156                 value = strrchr(item, ' ');
157                 sscanf(value, " %x", &metric->C);
158             }
159
160             else if(!strncmp("WX ", item, 3) || !strncmp("W0X ", item, 4)) {
161                 sscanf(value, "%f", &metric->WX);
162                 if(metric->C >= 0 && metric->C <= 0xff)
163                     afm->CharWidths[metric->C] = metric->WX;
164             }
165
166             else if(!strncmp("N ", item, 2)) {
167                 metric->N = PSDRV_GlyphName(value);
168             }
169
170             else if(!strncmp("B ", item, 2)) {
171                 sscanf(value, "%f%f%f%f", &metric->B.llx, &metric->B.lly,
172                                           &metric->B.urx, &metric->B.ury);
173
174                 /* Store height of Aring to use as lfHeight */
175                 if(metric->N && !strncmp(metric->N->sz, "Aring", 5))
176                     afm->FullAscender = metric->B.ury;
177             }
178
179             /* Ligatures go here... */
180
181             curpos = endpos + 1;
182         }
183         
184         if (CheckMetrics(metric) == FALSE) {
185             ERR("Error parsing character metrics\n");
186             HeapFree(PSDRV_Heap, 0, afm->Metrics);
187             afm->Metrics = NULL;
188             return FALSE;
189         }
190
191         TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
192               metric->N->sz, metric->WX, metric->B.llx, metric->B.lly,
193               metric->B.urx, metric->B.ury);
194     }
195
196     return TRUE;
197 }
198
199 /*******************************************************************************
200  *  BuildEncoding
201  *
202  *  Builds a custom encoding vector if necessary.  Leaves vector in the same
203  *  order as the afm->Metrics array; see SortFontMetrics().
204  *
205  */
206 static BOOL BuildEncoding(AFM *afm)
207 {
208     UNICODEVECTOR   *uv;
209     UNICODEGLYPH    *ug;
210     int             i;
211
212     if (strcmp(afm->EncodingScheme, "FontSpecific") != 0)
213     {
214         afm->Encoding = &PSDRV_AdobeGlyphList;
215         return TRUE;
216     }
217     
218     uv = HeapAlloc(PSDRV_Heap, 0, sizeof(UNICODEVECTOR) +
219             afm->NumofMetrics * sizeof(UNICODEGLYPH));
220     if (uv == NULL)
221         return FALSE;
222         
223     afm->Encoding = uv;
224     ug = (UNICODEGLYPH *)(uv + 1);
225     uv->glyphs = ug;
226     uv->size = afm->NumofMetrics;
227     
228     for (i = 0; i < afm->NumofMetrics; ++i)
229     {
230         ug[i].name = afm->Metrics[i].N;
231         
232         if (afm->Metrics[i].C < 0)          /* unencoded glyph */
233         {
234             WARN("Glyph '%s' in font '%s' has no encoding\n", ug[i].name->sz,
235                     afm->FullName);
236             ug[i].UV = -1;
237         }
238         else
239         {
240             ug[i].UV = afm->Metrics[i].C | 0xf000;  /* private use area? */
241         }
242     }
243     
244     return TRUE;
245 }
246     
247
248 /***********************************************************
249  *
250  *      PSDRV_AFMParse
251  *
252  * Fills out an AFM structure and associated substructures (see psdrv.h)
253  * for a given AFM file. All memory is allocated from the process heap. 
254  * Returns a ptr to the AFM structure or NULL on error.
255  *
256  * This is not complete (we don't handle kerning yet) and not efficient
257  */
258
259 static AFM *PSDRV_AFMParse(char const *file)
260 {
261     FILE *fp;
262     unsigned char buf[256];
263     unsigned char *value;
264     AFM *afm;
265     unsigned char *cp;
266     int afmfile = 0; 
267     int c;
268
269     TRACE("parsing '%s'\n", file);
270
271     if((fp = fopen(file, "r")) == NULL) {
272         MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
273         return NULL;
274     }
275
276     afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
277     if(!afm) {
278         fclose(fp);
279         return NULL;
280     }
281
282     cp = buf; 
283     while ( ( c = fgetc ( fp ) ) != EOF ) {
284         *cp = c;
285         if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
286             if ( cp == buf ) 
287                 continue;
288             *(cp+1)='\0';
289         }
290         else {
291             cp ++; 
292             continue;
293         }
294       
295         cp = buf + strlen(buf);
296         do {
297             *cp = '\0';
298             cp--;
299         } while(cp > buf && isspace(*cp));
300
301         cp = buf; 
302
303         if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
304             break;
305         afmfile = 1; 
306
307         value = strchr(buf, ' ');
308         if(value)
309             while(isspace(*value))
310                 value++;
311
312         if(!strncmp("FontName", buf, 8)) {
313             afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
314             if (afm->FontName == NULL) {
315                 fclose(fp);
316                 FreeAFM(afm);
317                 return NULL;
318             }
319             continue;
320         }
321
322         if(!strncmp("FullName", buf, 8)) {
323             afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
324             if (afm->FullName == NULL) {
325                 fclose(fp);
326                 FreeAFM(afm);
327                 return NULL;
328             }
329             continue;
330         }
331
332         if(!strncmp("FamilyName", buf, 10)) {
333             afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
334             if (afm->FamilyName == NULL) {
335                 fclose(fp);
336                 FreeAFM(afm);
337                 return NULL;
338             }
339             continue;
340         }
341         
342         if(!strncmp("Weight", buf, 6)) {
343             if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
344                || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
345                || !strncmp("Normal", value, 6))
346                 afm->Weight = FW_NORMAL;
347             else if(!strncmp("Demi", value, 4))
348                 afm->Weight = FW_DEMIBOLD;
349             else if(!strncmp("Bold", value, 4))
350                 afm->Weight = FW_BOLD;
351             else if(!strncmp("Light", value, 5))
352                 afm->Weight = FW_LIGHT;
353             else if(!strncmp("Black", value, 5))
354                 afm->Weight = FW_BLACK;
355             else {
356                 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
357                      file, value);
358                 afm->Weight = FW_NORMAL;
359             }
360             continue;
361         }
362
363         if(!strncmp("ItalicAngle", buf, 11)) {
364             sscanf(value, "%f", &(afm->ItalicAngle));
365             continue;
366         }
367
368         if(!strncmp("IsFixedPitch", buf, 12)) {
369             if(!strncasecmp("false", value, 5))
370                 afm->IsFixedPitch = FALSE;
371             else
372                 afm->IsFixedPitch = TRUE;
373             continue;
374         }
375
376         if(!strncmp("FontBBox", buf, 8)) {
377             sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx), 
378                    &(afm->FontBBox.lly), &(afm->FontBBox.urx), 
379                    &(afm->FontBBox.ury) );
380             continue;
381         }
382
383         if(!strncmp("UnderlinePosition", buf, 17)) {
384             sscanf(value, "%f", &(afm->UnderlinePosition) );
385             continue;
386         }
387
388         if(!strncmp("UnderlineThickness", buf, 18)) {
389             sscanf(value, "%f", &(afm->UnderlineThickness) );
390             continue;
391         }
392
393         if(!strncmp("CapHeight", buf, 9)) {
394             sscanf(value, "%f", &(afm->CapHeight) );
395             continue;
396         }
397
398         if(!strncmp("XHeight", buf, 7)) {
399             sscanf(value, "%f", &(afm->XHeight) );
400             continue;
401         }
402
403         if(!strncmp("Ascender", buf, 8)) {
404             sscanf(value, "%f", &(afm->Ascender) );
405             continue;
406         }
407
408         if(!strncmp("Descender", buf, 9)) {
409             sscanf(value, "%f", &(afm->Descender) );
410             continue;
411         }
412
413         if(!strncmp("StartCharMetrics", buf, 16)) {
414             sscanf(value, "%d", &(afm->NumofMetrics) );
415             if (PSDRV_AFMGetCharMetrics(afm, fp) == FALSE) {
416                 fclose(fp);
417                 FreeAFM(afm);
418                 return NULL;
419             }
420             continue;
421         }
422
423         if(!strncmp("EncodingScheme", buf, 14)) {
424             afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
425             if (afm->EncodingScheme == NULL) {
426                 fclose(fp);
427                 FreeAFM(afm);
428                 return NULL;
429             }
430             continue;
431         }
432
433     }
434     fclose(fp);
435
436     if (afmfile == 0) {
437         HeapFree ( PSDRV_Heap, 0, afm ); 
438         return NULL;
439     }
440
441     if(afm->FontName == NULL) {
442         WARN("%s contains no FontName.\n", file);
443         afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
444         if (afm->FontName == NULL) {
445             FreeAFM(afm);
446             return NULL;
447         }
448     }
449     
450     if(afm->FullName == NULL)
451         afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
452     if(afm->FamilyName == NULL)
453         afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
454     if (afm->FullName == NULL || afm->FamilyName == NULL) {
455         FreeAFM(afm);
456         return NULL;
457     }
458     
459     if(afm->Ascender == 0.0)
460         afm->Ascender = afm->FontBBox.ury;
461     if(afm->Descender == 0.0)
462         afm->Descender = afm->FontBBox.lly;
463     if(afm->FullAscender == 0.0)
464         afm->FullAscender = afm->Ascender;
465     if(afm->Weight == 0)
466         afm->Weight = FW_NORMAL;
467         
468     if (BuildEncoding(afm) == FALSE)
469     {
470         FreeAFM(afm);
471         return NULL;
472     }
473
474     return afm;
475 }
476
477 /***********************************************************
478  *
479  *      PSDRV_FreeAFMList
480  *
481  * Frees the family and afmlistentry structures in list head
482  */
483 void PSDRV_FreeAFMList( FONTFAMILY *head )
484 {
485     AFMLISTENTRY *afmle, *nexta;
486     FONTFAMILY *family, *nextf;
487
488     for(nextf = family = head; nextf; family = nextf) {
489         for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
490             nexta = afmle->next;
491             HeapFree( PSDRV_Heap, 0, afmle );
492         }
493         nextf = family->next;
494         HeapFree( PSDRV_Heap, 0, family );
495     }
496     return;
497 }
498
499
500 /***********************************************************
501  *
502  *      PSDRV_FindAFMinList
503  * Returns ptr to an AFM if name (which is a PS font name) exists in list
504  * headed by head.
505  */
506 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
507 {
508     FONTFAMILY *family;
509     AFMLISTENTRY *afmle;
510
511     for(family = head; family; family = family->next) {
512         for(afmle = family->afmlist; afmle; afmle = afmle->next) {
513             if(!strcmp(afmle->afm->FontName, name))
514                 return afmle->afm;
515         }
516     }
517     return NULL;
518 }
519
520 /***********************************************************
521  *
522  *      PSDRV_AddAFMtoList
523  *
524  * Adds an afm to the list whose head is pointed to by head. Creates new
525  * family node if necessary and always creates a new AFMLISTENTRY.
526  */
527 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
528 {
529     FONTFAMILY *family = *head;
530     FONTFAMILY **insert = head;
531     AFMLISTENTRY *tmpafmle, *newafmle;
532
533     newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
534                            sizeof(*newafmle));
535     if (newafmle == NULL)
536         return FALSE;
537         
538     newafmle->afm = afm;
539
540     while(family) {
541         if(!strcmp(family->FamilyName, afm->FamilyName))
542             break;
543         insert = &(family->next);
544         family = family->next;
545     }
546  
547     if(!family) {
548         family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
549                            sizeof(*family));
550         if (family == NULL) {
551             HeapFree(PSDRV_Heap, 0, newafmle);
552             return FALSE;
553         }
554         *insert = family;
555         family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
556                                           afm->FamilyName);
557         if (family->FamilyName == NULL) {
558             HeapFree(PSDRV_Heap, 0, family);
559             HeapFree(PSDRV_Heap, 0, newafmle);
560             return FALSE;
561         }
562         family->afmlist = newafmle;
563         return TRUE;
564     }
565     
566     tmpafmle = family->afmlist;
567     while(tmpafmle->next)
568         tmpafmle = tmpafmle->next;
569
570     tmpafmle->next = newafmle;
571
572     return TRUE;
573 }
574
575 /**********************************************************
576  *
577  *      PSDRV_ReencodeCharWidths
578  *
579  * Re map the CharWidths field of the afm to correspond to an ANSI encoding
580  *
581  */
582 static void PSDRV_ReencodeCharWidths(AFM *afm)
583 {
584     int i, j;
585     AFMMETRICS *metric;
586
587     for(i = 0; i < 256; i++) {
588         if(isalnum(i))
589             continue;
590         if(PSDRV_ANSIVector[i] == NULL) {
591             afm->CharWidths[i] = 0.0;
592             continue;
593         }
594         for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
595             if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
596                 afm->CharWidths[i] = metric->WX;
597                 break;
598             }
599         }
600         if(j == afm->NumofMetrics) {
601             WARN("Couldn't find glyph '%s' in font '%s'\n",
602                  PSDRV_ANSIVector[i], afm->FontName);
603             afm->CharWidths[i] = 0.0;
604         }
605     }
606     return;
607 }
608
609
610 /***********************************************************
611  *
612  *      PSDRV_DumpFontList
613  *
614  */
615 static void PSDRV_DumpFontList(void)
616 {
617     FONTFAMILY      *family;
618     AFMLISTENTRY    *afmle;
619
620     for(family = PSDRV_AFMFontList; family; family = family->next) {
621         TRACE("Family '%s'\n", family->FamilyName);
622         for(afmle = family->afmlist; afmle; afmle = afmle->next)
623         {
624             INT i;
625             
626             TRACE("\tFontName '%s' (%i glyphs):\n", afmle->afm->FontName,
627                     afmle->afm->NumofMetrics);
628             
629             for (i = 0; i < afmle->afm->NumofMetrics; ++i)
630             {
631                 TRACE("\t\tU+%.4lX; C %i; N '%s'\n", afmle->afm->Metrics[i].UV,
632                         afmle->afm->Metrics[i].C, afmle->afm->Metrics[i].N->sz);
633             }
634         }
635     }
636     return;
637 }
638
639 /*******************************************************************************
640  *  SortFontMetrics
641  *
642  *  Initializes the UV member of each glyph's AFMMETRICS and sorts each font's
643  *  Metrics by Unicode Value.
644  *
645  */
646 static int UnicodeGlyphByNameIndex(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
647 {
648     return a->name->index - b->name->index;
649 }
650
651 static int UnicodeGlyphByUV(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
652 {
653     return a->UV - b->UV;
654 }
655  
656 static int AFMMetricsByUV(const AFMMETRICS *a, const AFMMETRICS *b)
657 {
658     return a->UV - b->UV;
659 }
660  
661 static BOOL SortFontMetrics()
662 {
663     UNICODEGLYPH    *aglCopy = NULL;
664     FONTFAMILY      *family = PSDRV_AFMFontList;
665     
666     while (family != NULL)
667     {
668         AFMLISTENTRY    *afmle = family->afmlist;
669         
670         while (afmle != NULL)
671         {
672             AFM *afm = afmle->afm;      /* should always be valid */
673             INT i;
674             
675             if (afm->Encoding == &PSDRV_AdobeGlyphList)
676             {
677                 if (aglCopy == NULL)    /* do this once, if necessary */
678                 {
679                     aglCopy = HeapAlloc(PSDRV_Heap, 0,
680                             PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
681                     if (aglCopy == NULL)
682                         return FALSE;
683                         
684                     memcpy(aglCopy, PSDRV_AdobeGlyphList.glyphs,
685                             PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
686                             
687                     qsort(aglCopy, PSDRV_AdobeGlyphList.size,
688                             sizeof(UNICODEGLYPH),
689                             (compar_callback_fn)UnicodeGlyphByNameIndex);
690                 }
691                 
692                 for (i = 0; i < afm->NumofMetrics; ++i)
693                 {
694                     UNICODEGLYPH    ug, *pug;
695                     
696                     ug.name = afm->Metrics[i].N;
697                     ug.UV = -1;
698                     
699                     pug = bsearch(&ug, aglCopy, PSDRV_AdobeGlyphList.size,
700                             sizeof(UNICODEGLYPH),
701                             (compar_callback_fn)UnicodeGlyphByNameIndex);
702                     if (pug == NULL)
703                     {
704                         WARN("Glyph '%s' in font '%s' does not have a UV\n",
705                                 ug.name->sz, afm->FullName);
706                         afm->Metrics[i].UV = -1;
707                     }
708                     else
709                     {
710                         afm->Metrics[i].UV = pug->UV;
711                     }
712                 }
713             }
714             else                /* FontSpecific encoding or TrueType font */
715             {
716                 for (i = 0; i < afm->NumofMetrics; ++i)
717                     afm->Metrics[i].UV = afm->Encoding->glyphs[i].UV;
718                 
719                 /* typecast avoids compiler warning */
720                 qsort((void *)(afm->Encoding->glyphs), afm->Encoding->size,
721                         sizeof(UNICODEGLYPH),
722                         (compar_callback_fn)UnicodeGlyphByUV);
723                         
724                 for (i = 0; i < afm->Encoding->size; ++i)
725                     if (afm->Encoding->glyphs[i].UV >= 0)
726                         break;
727                         
728                 afm->Encoding->size -= i;       /* Ignore unencoded glyphs */
729                 afm->Encoding->glyphs += i;     /* from now on */
730             }
731             
732             qsort(afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
733                     (compar_callback_fn)AFMMetricsByUV);
734                     
735             for (i = 0; i < afm->NumofMetrics; ++i)
736                 if (afm->Metrics[i].UV >= 0)
737                     break;
738                     
739             afm->NumofMetrics -= i;     /* Ignore unencoded glyphs here too */
740             afm->Metrics += i;
741             
742             afmle = afmle->next;
743         }
744         
745         family = family->next;
746     }
747     
748     if (aglCopy != NULL)
749         HeapFree(PSDRV_Heap, 0, aglCopy);
750         
751     return TRUE;
752 }
753
754 /***********************************************************
755  *
756  *      PSDRV_GetFontMetrics
757  *
758  * Parses all afm files listed in [afmfiles] and [afmdirs] of wine.conf
759  *
760  * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
761  * about freeing all the memory that's been allocated.
762  */
763
764 static BOOL PSDRV_ReadAFMDir(const char* afmdir) {
765     DIR *dir;
766     AFM *afm;
767
768     dir = opendir(afmdir);
769     if (dir) {
770         struct dirent *dent;
771         while ((dent=readdir(dir))) {
772             if (strstr(dent->d_name,".afm")) {
773                 char *afmfn;
774
775                 afmfn=(char*)HeapAlloc(PSDRV_Heap,0, 
776                         strlen(afmdir)+strlen(dent->d_name)+2);
777                 if (afmfn == NULL) {
778                     closedir(dir);
779                     return FALSE;
780                 }
781                 strcpy(afmfn,afmdir);
782                 strcat(afmfn,"/");
783                 strcat(afmfn,dent->d_name);
784                 TRACE("loading AFM %s\n",afmfn);
785                 afm = PSDRV_AFMParse(afmfn);
786                 if (afm) {
787                     if(afm->EncodingScheme && 
788                        !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
789                         PSDRV_ReencodeCharWidths(afm);
790                     }
791                     if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
792                         closedir(dir);
793                         FreeAFM(afm);
794                         return FALSE;
795                     }
796                 }
797                 else {
798                     WARN("Error parsing %s\n", afmfn);
799                 }
800                 HeapFree(PSDRV_Heap,0,afmfn);
801             }
802         }
803         closedir(dir);
804     }
805     else {
806         WARN("Error opening %s\n", afmdir);
807     }
808     
809     return TRUE;
810 }
811
812 BOOL PSDRV_GetFontMetrics(void)
813 {
814     int idx = 0;
815     char key[256];
816     char value[256];
817
818     if (PSDRV_GlyphListInit() != 0)
819         return FALSE;
820
821     while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key),
822             value, sizeof(value)))
823     {
824         AFM* afm = PSDRV_AFMParse(value);
825         
826         if (afm) {
827             if(afm->EncodingScheme && 
828                !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
829                 PSDRV_ReencodeCharWidths(afm);
830             }
831             if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
832                 return FALSE;
833             }
834         }
835         else {
836             WARN("Error parsing %s\n", value);
837         }
838     }
839
840     for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
841             value, sizeof (value)); ++idx)
842         if (PSDRV_ReadAFMDir (value) == FALSE)
843             return FALSE;
844
845     PSDRV_IndexGlyphList();
846     if (SortFontMetrics() == FALSE)
847         return FALSE;
848     PSDRV_DumpFontList();
849     return TRUE;
850 }