Fixed some issues found by winapi_check.
[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 "config.h"
10
11 #include <string.h>
12 #include <stdlib.h>     /* qsort() & bsearch() */
13 #include <stdio.h>
14 #include <dirent.h>
15 #include <limits.h>     /* INT_MIN */
16 #ifdef HAVE_FLOAT_H
17 # include <float.h>     /* FLT_MAX */
18 #endif
19 #include "winnt.h"      /* HEAP_ZERO_MEMORY */
20 #include "psdrv.h"
21 #include "options.h"
22 #include "debugtools.h"
23 #include "heap.h"
24
25 DEFAULT_DEBUG_CHANNEL(psdrv);
26 #include <ctype.h>
27
28 /* ptr to fonts for which we have afm files */
29 FONTFAMILY *PSDRV_AFMFontList = NULL;
30
31 /* qsort/bsearch callback functions */
32 typedef int (*compar_callback_fn) (const void *, const void *);
33
34 /*******************************************************************************
35  *  IsWinANSI
36  *
37  *  Checks whether Unicode value is part of Microsoft code page 1252
38  *
39  */
40 static const INT ansiChars[21] =
41 {
42     0x0152, 0x0153, 0x0160, 0x0161, 0x0178, 0x017d, 0x017e, 0x0192, 0x02c6,
43     0x02c9, 0x02dc, 0x03bc, 0x2013, 0x2014, 0x2026, 0x2030, 0x2039, 0x203a,
44     0x20ac, 0x2122, 0x2219
45 };
46
47 static int cmpUV(const INT *a, const INT *b)
48 {
49     return *a - *b;
50 }
51  
52 inline static BOOL IsWinANSI(INT uv)
53 {
54     if ((0x0020 <= uv && uv <= 0x007e) || (0x00a0 <= uv && uv <= 0x00ff) ||
55             (0x2018 <= uv && uv <= 0x201a) || (0x201c <= uv && uv <= 0x201e) ||
56             (0x2020 <= uv && uv <= 2022))
57         return TRUE;
58         
59     if (bsearch(&uv, ansiChars, 21, sizeof(INT),
60             (compar_callback_fn)cmpUV) != NULL)
61         return TRUE;
62         
63     return FALSE;
64 }
65
66 /*******************************************************************************
67  *      CheckMetrics
68  *
69  *  Check an AFMMETRICS structure to make sure all elements have been properly
70  *  filled in.  (Don't check UV or L.)
71  *
72  */
73 static const AFMMETRICS badMetrics =
74 {
75     INT_MIN,                                    /* C */
76     INT_MIN,                                    /* UV */
77     FLT_MAX,                                    /* WX */
78     NULL,                                       /* N */
79     { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX },     /* B */
80     NULL                                        /* L */
81 };
82
83 inline static BOOL CheckMetrics(const AFMMETRICS *metrics)
84 {
85     if (    metrics->C      == badMetrics.C     ||
86             metrics->WX     == badMetrics.WX    ||
87             metrics->N      == badMetrics.N     ||
88             metrics->B.llx  == badMetrics.B.llx ||
89             metrics->B.lly  == badMetrics.B.lly ||
90             metrics->B.urx  == badMetrics.B.urx ||
91             metrics->B.ury  == badMetrics.B.ury )
92         return FALSE;
93         
94     return TRUE;
95 }
96
97 /*******************************************************************************
98  *  FreeAFM
99  *
100  *  Free an AFM structure and any subsidiary objects that have been allocated.
101  *  AFM must have been allocated with HEAP_ZERO_MEMORY.
102  *
103  */
104 static void FreeAFM(AFM *afm)
105 {
106     if (afm->FontName != NULL)
107         HeapFree(PSDRV_Heap, 0, afm->FontName);
108     if (afm->FullName != NULL)
109         HeapFree(PSDRV_Heap, 0, afm->FullName);
110     if (afm->FamilyName != NULL)
111         HeapFree(PSDRV_Heap, 0, afm->FamilyName);
112     if (afm->EncodingScheme != NULL)
113         HeapFree(PSDRV_Heap, 0, afm->EncodingScheme);
114     if (afm->Metrics != NULL)
115         HeapFree(PSDRV_Heap, 0, afm->Metrics);
116         
117     HeapFree(PSDRV_Heap, 0, afm);
118 }
119
120 /***********************************************************
121  *
122  *      PSDRV_AFMGetCharMetrics
123  *
124  * Parses CharMetric section of AFM file.
125  *
126  * Actually only collects the widths of numbered chars and puts then in
127  * afm->CharWidths.
128  */
129 static BOOL PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
130 {
131     unsigned char line[256], valbuf[256];
132     unsigned char *cp, *item, *value, *curpos, *endpos;
133     int i;
134     AFMMETRICS *metric;
135
136     afm->Metrics = metric = HeapAlloc( PSDRV_Heap, 0,
137                                        afm->NumofMetrics * sizeof(AFMMETRICS) );
138     if (metric == NULL)
139         return FALSE;
140                                        
141     for(i = 0; i < afm->NumofMetrics; i++, metric++) {
142     
143         *metric = badMetrics;
144
145         do {
146             if(!fgets(line, sizeof(line), fp)) {
147                 ERR("Unexpected EOF\n");
148                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
149                 afm->Metrics = NULL;
150                 return FALSE;
151             }
152             cp = line + strlen(line);
153             do {
154                 *cp = '\0';
155                 cp--;
156             } while(cp >= line && isspace(*cp));
157         } while (!(*line));
158
159         curpos = line;
160         while(*curpos) {
161             item = curpos;
162             while(isspace(*item))
163                 item++;
164             value = strpbrk(item, " \t");
165             if (!value) {
166                 ERR("No whitespace found.\n");
167                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
168                 afm->Metrics = NULL;
169                 return FALSE;
170             }
171             while(isspace(*value))
172                 value++;
173             cp = endpos = strchr(value, ';');
174             if (!cp) {
175                 ERR("missing ;, failed. [%s]\n", line);
176                 HeapFree(PSDRV_Heap, 0, afm->Metrics);
177                 afm->Metrics = NULL;
178                 return FALSE;
179             }
180             while(isspace(*--cp))
181                 ;
182             memcpy(valbuf, value, cp - value + 1);
183             valbuf[cp - value + 1] = '\0';
184             value = valbuf;
185
186             if(!strncmp(item, "C ", 2)) {
187                 value = strchr(item, ' ');
188                 sscanf(value, " %d", &metric->C);
189
190             } else if(!strncmp(item, "CH ", 3)) {
191                 value = strrchr(item, ' ');
192                 sscanf(value, " %x", &metric->C);
193             }
194
195             else if(!strncmp("WX ", item, 3) || !strncmp("W0X ", item, 4)) {
196                 sscanf(value, "%f", &metric->WX);
197                 if(metric->C >= 0 && metric->C <= 0xff)
198                     afm->CharWidths[metric->C] = metric->WX;
199             }
200
201             else if(!strncmp("N ", item, 2)) {
202                 metric->N = PSDRV_GlyphName(value);
203             }
204
205             else if(!strncmp("B ", item, 2)) {
206                 sscanf(value, "%f%f%f%f", &metric->B.llx, &metric->B.lly,
207                                           &metric->B.urx, &metric->B.ury);
208
209                 /* Store height of Aring to use as lfHeight */
210                 if(metric->N && !strncmp(metric->N->sz, "Aring", 5))
211                     afm->FullAscender = metric->B.ury;
212             }
213
214             /* Ligatures go here... */
215
216             curpos = endpos + 1;
217         }
218         
219         if (CheckMetrics(metric) == FALSE) {
220             ERR("Error parsing character metrics\n");
221             HeapFree(PSDRV_Heap, 0, afm->Metrics);
222             afm->Metrics = NULL;
223             return FALSE;
224         }
225
226         TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
227               metric->N->sz, metric->WX, metric->B.llx, metric->B.lly,
228               metric->B.urx, metric->B.ury);
229     }
230
231     return TRUE;
232 }
233
234 /*******************************************************************************
235  *  BuildEncoding
236  *
237  *  Builds a custom encoding vector if necessary.  Leaves vector in the same
238  *  order as the afm->Metrics array; see SortFontMetrics().
239  *
240  */
241 static BOOL BuildEncoding(AFM *afm)
242 {
243     UNICODEVECTOR   *uv;
244     UNICODEGLYPH    *ug;
245     int             i;
246
247     if (strcmp(afm->EncodingScheme, "FontSpecific") != 0)
248     {
249         afm->Encoding = &PSDRV_AdobeGlyphList;
250         return TRUE;
251     }
252     
253     uv = HeapAlloc(PSDRV_Heap, 0, sizeof(UNICODEVECTOR) +
254             afm->NumofMetrics * sizeof(UNICODEGLYPH));
255     if (uv == NULL)
256         return FALSE;
257         
258     afm->Encoding = uv;
259     ug = (UNICODEGLYPH *)(uv + 1);
260     uv->glyphs = ug;
261     uv->size = afm->NumofMetrics;
262     
263     for (i = 0; i < afm->NumofMetrics; ++i)
264     {
265         ug[i].name = afm->Metrics[i].N;
266         
267         if (afm->Metrics[i].C < 0)          /* unencoded glyph */
268         {
269             WARN("Glyph '%s' in font '%s' has no encoding\n", ug[i].name->sz,
270                     afm->FullName);
271             ug[i].UV = -1;
272         }
273         else
274         {
275             ug[i].UV = afm->Metrics[i].C | 0xf000;  /* private use area? */
276         }
277     }
278     
279     return TRUE;
280 }
281     
282
283 /***********************************************************
284  *
285  *      PSDRV_AFMParse
286  *
287  * Fills out an AFM structure and associated substructures (see psdrv.h)
288  * for a given AFM file. All memory is allocated from the process heap. 
289  * Returns a ptr to the AFM structure or NULL on error.
290  *
291  * This is not complete (we don't handle kerning yet) and not efficient
292  */
293
294 static AFM *PSDRV_AFMParse(char const *file)
295 {
296     FILE *fp;
297     unsigned char buf[256];
298     unsigned char *value;
299     AFM *afm;
300     unsigned char *cp;
301     int afmfile = 0; 
302     int c;
303
304     TRACE("parsing '%s'\n", file);
305
306     if((fp = fopen(file, "r")) == NULL) {
307         MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
308         return NULL;
309     }
310
311     afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
312     if(!afm) {
313         fclose(fp);
314         return NULL;
315     }
316
317     cp = buf; 
318     while ( ( c = fgetc ( fp ) ) != EOF ) {
319         *cp = c;
320         if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
321             if ( cp == buf ) 
322                 continue;
323             *(cp+1)='\0';
324         }
325         else {
326             cp ++; 
327             continue;
328         }
329       
330         cp = buf + strlen(buf);
331         do {
332             *cp = '\0';
333             cp--;
334         } while(cp > buf && isspace(*cp));
335
336         cp = buf; 
337
338         if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
339             break;
340         afmfile = 1; 
341
342         value = strchr(buf, ' ');
343         if(value)
344             while(isspace(*value))
345                 value++;
346
347         if(!strncmp("FontName", buf, 8)) {
348             afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
349             if (afm->FontName == NULL) {
350                 fclose(fp);
351                 FreeAFM(afm);
352                 return NULL;
353             }
354             continue;
355         }
356
357         if(!strncmp("FullName", buf, 8)) {
358             afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
359             if (afm->FullName == NULL) {
360                 fclose(fp);
361                 FreeAFM(afm);
362                 return NULL;
363             }
364             continue;
365         }
366
367         if(!strncmp("FamilyName", buf, 10)) {
368             afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
369             if (afm->FamilyName == NULL) {
370                 fclose(fp);
371                 FreeAFM(afm);
372                 return NULL;
373             }
374             continue;
375         }
376         
377         if(!strncmp("Weight", buf, 6)) {
378             if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
379                || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
380                || !strncmp("Normal", value, 6))
381                 afm->Weight = FW_NORMAL;
382             else if(!strncmp("Demi", value, 4))
383                 afm->Weight = FW_DEMIBOLD;
384             else if(!strncmp("Bold", value, 4))
385                 afm->Weight = FW_BOLD;
386             else if(!strncmp("Light", value, 5))
387                 afm->Weight = FW_LIGHT;
388             else if(!strncmp("Black", value, 5))
389                 afm->Weight = FW_BLACK;
390             else {
391                 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
392                      file, value);
393                 afm->Weight = FW_NORMAL;
394             }
395             continue;
396         }
397
398         if(!strncmp("ItalicAngle", buf, 11)) {
399             sscanf(value, "%f", &(afm->ItalicAngle));
400             continue;
401         }
402
403         if(!strncmp("IsFixedPitch", buf, 12)) {
404             if(!strncasecmp("false", value, 5))
405                 afm->IsFixedPitch = FALSE;
406             else
407                 afm->IsFixedPitch = TRUE;
408             continue;
409         }
410
411         if(!strncmp("FontBBox", buf, 8)) {
412             sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx), 
413                    &(afm->FontBBox.lly), &(afm->FontBBox.urx), 
414                    &(afm->FontBBox.ury) );
415             continue;
416         }
417
418         if(!strncmp("UnderlinePosition", buf, 17)) {
419             sscanf(value, "%f", &(afm->UnderlinePosition) );
420             continue;
421         }
422
423         if(!strncmp("UnderlineThickness", buf, 18)) {
424             sscanf(value, "%f", &(afm->UnderlineThickness) );
425             continue;
426         }
427
428         if(!strncmp("CapHeight", buf, 9)) {
429             sscanf(value, "%f", &(afm->CapHeight) );
430             continue;
431         }
432
433         if(!strncmp("XHeight", buf, 7)) {
434             sscanf(value, "%f", &(afm->XHeight) );
435             continue;
436         }
437
438         if(!strncmp("Ascender", buf, 8)) {
439             sscanf(value, "%f", &(afm->Ascender) );
440             continue;
441         }
442
443         if(!strncmp("Descender", buf, 9)) {
444             sscanf(value, "%f", &(afm->Descender) );
445             continue;
446         }
447
448         if(!strncmp("StartCharMetrics", buf, 16)) {
449             sscanf(value, "%d", &(afm->NumofMetrics) );
450             if (PSDRV_AFMGetCharMetrics(afm, fp) == FALSE) {
451                 fclose(fp);
452                 FreeAFM(afm);
453                 return NULL;
454             }
455             continue;
456         }
457
458         if(!strncmp("EncodingScheme", buf, 14)) {
459             afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
460             if (afm->EncodingScheme == NULL) {
461                 fclose(fp);
462                 FreeAFM(afm);
463                 return NULL;
464             }
465             continue;
466         }
467
468     }
469     fclose(fp);
470
471     if (afmfile == 0) {
472         HeapFree ( PSDRV_Heap, 0, afm ); 
473         return NULL;
474     }
475
476     if(afm->FontName == NULL) {
477         WARN("%s contains no FontName.\n", file);
478         afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
479         if (afm->FontName == NULL) {
480             FreeAFM(afm);
481             return NULL;
482         }
483     }
484     
485     if(afm->FullName == NULL)
486         afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
487     if(afm->FamilyName == NULL)
488         afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
489     if (afm->FullName == NULL || afm->FamilyName == NULL) {
490         FreeAFM(afm);
491         return NULL;
492     }
493     
494     if(afm->Ascender == 0.0)
495         afm->Ascender = afm->FontBBox.ury;
496     if(afm->Descender == 0.0)
497         afm->Descender = afm->FontBBox.lly;
498     if(afm->FullAscender == 0.0)
499         afm->FullAscender = afm->Ascender;
500     if(afm->Weight == 0)
501         afm->Weight = FW_NORMAL;
502         
503     if (BuildEncoding(afm) == FALSE)
504     {
505         FreeAFM(afm);
506         return NULL;
507     }
508
509     return afm;
510 }
511
512 /***********************************************************
513  *
514  *      PSDRV_FreeAFMList
515  *
516  * Frees the family and afmlistentry structures in list head
517  */
518 void PSDRV_FreeAFMList( FONTFAMILY *head )
519 {
520     AFMLISTENTRY *afmle, *nexta;
521     FONTFAMILY *family, *nextf;
522
523     for(nextf = family = head; nextf; family = nextf) {
524         for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
525             nexta = afmle->next;
526             HeapFree( PSDRV_Heap, 0, afmle );
527         }
528         nextf = family->next;
529         HeapFree( PSDRV_Heap, 0, family );
530     }
531     return;
532 }
533
534
535 /***********************************************************
536  *
537  *      PSDRV_FindAFMinList
538  * Returns ptr to an AFM if name (which is a PS font name) exists in list
539  * headed by head.
540  */
541 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
542 {
543     FONTFAMILY *family;
544     AFMLISTENTRY *afmle;
545
546     for(family = head; family; family = family->next) {
547         for(afmle = family->afmlist; afmle; afmle = afmle->next) {
548             if(!strcmp(afmle->afm->FontName, name))
549                 return afmle->afm;
550         }
551     }
552     return NULL;
553 }
554
555 /***********************************************************
556  *
557  *      PSDRV_AddAFMtoList
558  *
559  * Adds an afm to the list whose head is pointed to by head. Creates new
560  * family node if necessary and always creates a new AFMLISTENTRY.
561  */
562 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
563 {
564     FONTFAMILY *family = *head;
565     FONTFAMILY **insert = head;
566     AFMLISTENTRY *tmpafmle, *newafmle;
567
568     newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
569                            sizeof(*newafmle));
570     if (newafmle == NULL)
571         return FALSE;
572         
573     newafmle->afm = afm;
574
575     while(family) {
576         if(!strcmp(family->FamilyName, afm->FamilyName))
577             break;
578         insert = &(family->next);
579         family = family->next;
580     }
581  
582     if(!family) {
583         family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
584                            sizeof(*family));
585         if (family == NULL) {
586             HeapFree(PSDRV_Heap, 0, newafmle);
587             return FALSE;
588         }
589         *insert = family;
590         family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
591                                           afm->FamilyName);
592         if (family->FamilyName == NULL) {
593             HeapFree(PSDRV_Heap, 0, family);
594             HeapFree(PSDRV_Heap, 0, newafmle);
595             return FALSE;
596         }
597         family->afmlist = newafmle;
598         return TRUE;
599     }
600     else {
601         tmpafmle = family->afmlist;
602         while (tmpafmle) {
603             if (!strcmp(tmpafmle->afm->FontName, afm->FontName)) {
604                 WARN("Ignoring duplicate FontName '%s'\n", afm->FontName);
605                 HeapFree(PSDRV_Heap, 0, newafmle);
606                 return TRUE;                        /* not a fatal error */
607             }
608             tmpafmle = tmpafmle->next;
609         }
610     }
611     
612     tmpafmle = family->afmlist;
613     while(tmpafmle->next)
614         tmpafmle = tmpafmle->next;
615
616     tmpafmle->next = newafmle;
617
618     return TRUE;
619 }
620
621 /**********************************************************
622  *
623  *      PSDRV_ReencodeCharWidths
624  *
625  * Re map the CharWidths field of the afm to correspond to an ANSI encoding
626  *
627  */
628 static void PSDRV_ReencodeCharWidths(AFM *afm)
629 {
630     int i, j;
631     AFMMETRICS *metric;
632
633     for(i = 0; i < 256; i++) {
634         if(isalnum(i))
635             continue;
636         if(PSDRV_ANSIVector[i] == NULL) {
637             afm->CharWidths[i] = 0.0;
638             continue;
639         }
640         for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
641             if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
642                 afm->CharWidths[i] = metric->WX;
643                 break;
644             }
645         }
646         if(j == afm->NumofMetrics) {
647             WARN("Couldn't find glyph '%s' in font '%s'\n",
648                  PSDRV_ANSIVector[i], afm->FontName);
649             afm->CharWidths[i] = 0.0;
650         }
651     }
652     return;
653 }
654
655
656 /***********************************************************
657  *
658  *      PSDRV_DumpFontList
659  *
660  */
661 static void PSDRV_DumpFontList(void)
662 {
663     FONTFAMILY      *family;
664     AFMLISTENTRY    *afmle;
665
666     for(family = PSDRV_AFMFontList; family; family = family->next) {
667         TRACE("Family '%s'\n", family->FamilyName);
668         for(afmle = family->afmlist; afmle; afmle = afmle->next)
669         {
670             INT i;
671             
672             TRACE("\tFontName '%s' (%i glyphs) - '%s' encoding:\n",
673                     afmle->afm->FontName, afmle->afm->NumofMetrics,
674                     afmle->afm->EncodingScheme);
675             
676             for (i = 0; i < afmle->afm->NumofMetrics; ++i)
677             {
678                 TRACE("\t\tU+%.4lX; C %i; N '%s'\n", afmle->afm->Metrics[i].UV,
679                         afmle->afm->Metrics[i].C, afmle->afm->Metrics[i].N->sz);
680             }
681         }
682     }
683     return;
684 }
685
686 /*******************************************************************************
687  *  SortFontMetrics
688  *
689  *  Initializes the UV member of each glyph's AFMMETRICS and sorts each font's
690  *  Metrics by Unicode Value.  If the font has a standard encoding (i.e. it is
691  *  using the Adobe Glyph List encoding vector), look up each glyph's Unicode
692  *  Value based on it's glyph name.  If the font has a font-specific encoding,
693  *  map the default PostScript encodings into the Unicode private use area.
694  *
695  */
696 static int UnicodeGlyphByNameIndex(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
697 {
698     return a->name->index - b->name->index;
699 }
700
701 static int UnicodeGlyphByUV(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
702 {
703     return a->UV - b->UV;
704 }
705  
706 static int AFMMetricsByUV(const AFMMETRICS *a, const AFMMETRICS *b)
707 {
708     return a->UV - b->UV;
709 }
710  
711 static BOOL SortFontMetrics()
712 {
713     UNICODEGLYPH    *aglCopy = NULL;
714     FONTFAMILY      *family = PSDRV_AFMFontList;
715     
716     while (family != NULL)
717     {
718         AFMLISTENTRY    *afmle = family->afmlist;
719         
720         while (afmle != NULL)
721         {
722             AFM *afm = afmle->afm;      /* should always be valid */
723             INT i;
724             
725             if (afm->Encoding == &PSDRV_AdobeGlyphList)
726             {
727                 if (aglCopy == NULL)    /* do this once, if necessary */
728                 {
729                     aglCopy = HeapAlloc(PSDRV_Heap, 0,
730                             PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
731                     if (aglCopy == NULL)
732                         return FALSE;
733                         
734                     memcpy(aglCopy, PSDRV_AdobeGlyphList.glyphs,
735                             PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
736                             
737                     qsort(aglCopy, PSDRV_AdobeGlyphList.size,
738                             sizeof(UNICODEGLYPH),
739                             (compar_callback_fn)UnicodeGlyphByNameIndex);
740                 }
741                 
742                 for (i = 0; i < afm->NumofMetrics; ++i)
743                 {
744                     UNICODEGLYPH    ug, *pug;
745                     
746                     ug.name = afm->Metrics[i].N;
747                     ug.UV = -1;
748                     
749                     pug = bsearch(&ug, aglCopy, PSDRV_AdobeGlyphList.size,
750                             sizeof(UNICODEGLYPH),
751                             (compar_callback_fn)UnicodeGlyphByNameIndex);
752                     if (pug == NULL)
753                     {
754                         WARN("Glyph '%s' in font '%s' does not have a UV\n",
755                                 ug.name->sz, afm->FullName);
756                         afm->Metrics[i].UV = -1;
757                     }
758                     else
759                     {
760                         afm->Metrics[i].UV = pug->UV;
761                     }
762                 }
763             }
764             else                /* FontSpecific encoding or TrueType font */
765             {
766                 for (i = 0; i < afm->NumofMetrics; ++i)
767                     afm->Metrics[i].UV = afm->Encoding->glyphs[i].UV;
768                 
769                 /* typecast avoids compiler warning */
770                 qsort((void *)(afm->Encoding->glyphs), afm->Encoding->size,
771                         sizeof(UNICODEGLYPH),
772                         (compar_callback_fn)UnicodeGlyphByUV);
773                         
774                 for (i = 0; i < afm->Encoding->size; ++i)
775                     if (afm->Encoding->glyphs[i].UV >= 0)
776                         break;
777                         
778                 afm->Encoding->size -= i;       /* Ignore unencoded glyphs */
779                 afm->Encoding->glyphs += i;     /* from now on */
780             }
781             
782             qsort(afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
783                     (compar_callback_fn)AFMMetricsByUV);
784                     
785             for (i = 0; i < afm->NumofMetrics; ++i)
786                 if (afm->Metrics[i].UV >= 0)
787                     break;
788                     
789             afm->NumofMetrics -= i;     /* Ignore unencoded glyphs here too */
790             afm->Metrics += i;
791             
792             afmle = afmle->next;
793         }
794         
795         family = family->next;
796     }
797     
798     if (aglCopy != NULL)
799         HeapFree(PSDRV_Heap, 0, aglCopy);
800         
801     return TRUE;
802 }
803
804 /*******************************************************************************
805  *  CalcWindowsMetrics
806  *
807  *  Calculates several Windows-specific font metrics for each font.  Relies on
808  *  the fact that AFMs are allocated with HEAP_ZERO_MEMORY to distinguish
809  *  TrueType fonts (when implemented), which already have these filled in.
810  *
811  */
812 static VOID CalcWindowsMetrics()
813 {
814     FONTFAMILY  *family = PSDRV_AFMFontList;
815     
816     while (family != NULL)
817     {
818         AFMLISTENTRY    *afmle = family->afmlist;
819         
820         while (afmle != NULL)
821         {
822             WINMETRICS  wm;
823             AFM         *afm = afmle->afm;      /* should always be valid */
824             INT         i;
825             
826             if (afm->WinMetrics.usUnitsPerEm != 0)
827                 continue;                           /* TrueType font */
828                 
829             wm.usUnitsPerEm = 1000;                 /* for PostScript fonts */
830             wm.sTypoAscender = (SHORT)(afm->Ascender + 0.5);
831             wm.sTypoDescender = (SHORT)(afm->Descender - 0.5);
832             
833             wm.sTypoLineGap = 1200 - (wm.sTypoAscender - wm.sTypoDescender);
834             if (wm.sTypoLineGap < 0)
835                 wm.sTypoLineGap = 0;
836                 
837             wm.usWinAscent = 0;
838             wm.usWinDescent = 0;
839             
840             for (i = 0; i < afm->NumofMetrics; ++i)
841             {
842                 if (IsWinANSI(afm->Metrics[i].UV) == FALSE)
843                     continue;
844                     
845                 if (afm->Metrics[i].B.ury > 0)
846                 {
847                     USHORT ascent = (USHORT)(afm->Metrics[i].B.ury + 0.5);
848                                                 
849                     if (ascent > wm.usWinAscent)
850                         wm.usWinAscent = ascent;
851                 }
852                 
853                 if (afm->Metrics[i].B.lly < 0)    
854                 {
855                     USHORT descent = (USHORT)(-(afm->Metrics[i].B.lly) + 0.5);
856                     
857                     if (descent > wm.usWinDescent)
858                         wm.usWinDescent = descent;
859                 }
860             }
861             
862             if (wm.usWinAscent == 0 && afm->FontBBox.ury > 0)
863                 wm.usWinAscent = (USHORT)(afm->FontBBox.ury + 0.5);
864                 
865             if (wm.usWinDescent == 0 && afm->FontBBox.lly < 0)
866                 wm.usWinDescent = (USHORT)(-(afm->FontBBox.lly) + 0.5);
867                 
868             wm.sAscender = wm.usWinAscent;
869             wm.sDescender = -(wm.usWinDescent);
870             
871             wm.sLineGap = 1150 - (wm.sAscender - wm.sDescender);
872             if (wm.sLineGap < 0)
873                 wm.sLineGap = 0;
874                                                 
875             TRACE("Windows metrics for '%s':\n", afm->FullName);
876             TRACE("\tsAscender = %i\n", wm.sAscender);
877             TRACE("\tsDescender = %i\n", wm.sDescender);
878             TRACE("\tsLineGap = %i\n", wm.sLineGap);
879             TRACE("\tusUnitsPerEm = %u\n", wm.usUnitsPerEm);
880             TRACE("\tsTypoAscender = %i\n", wm.sTypoAscender);
881             TRACE("\tsTypoDescender = %i\n", wm.sTypoDescender);
882             TRACE("\tsTypoLineGap = %i\n", wm.sTypoLineGap);
883             TRACE("\tusWinAscent = %u\n", wm.usWinAscent);
884             TRACE("\tusWinDescent = %u\n", wm.usWinDescent);
885             
886             afm->WinMetrics = wm;
887             
888             /* See afm2c.c and mkagl.c for an explanation of this */
889             /*  PSDRV_AFM2C(afm);   */
890             
891             afmle = afmle->next;
892         }
893         
894         family = family ->next;
895     }
896 }
897
898
899 /*******************************************************************************
900  *  AddBuiltinAFMs
901  *
902  */
903  
904 static BOOL AddBuiltinAFMs()
905 {
906     int i = 0;
907     
908     while (PSDRV_BuiltinAFMs[i] != NULL)
909     {
910         if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, PSDRV_BuiltinAFMs[i])
911                 == FALSE)
912             return FALSE;
913         ++i;
914     }
915     
916     return TRUE;
917 }
918
919
920 /***********************************************************
921  *
922  *      PSDRV_GetFontMetrics
923  *
924  * Parses all afm files listed in [afmfiles] and [afmdirs] of wine.conf
925  *
926  * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
927  * about freeing all the memory that's been allocated.
928  */
929
930 static BOOL PSDRV_ReadAFMDir(const char* afmdir) {
931     DIR *dir;
932     AFM *afm;
933
934     dir = opendir(afmdir);
935     if (dir) {
936         struct dirent *dent;
937         while ((dent=readdir(dir))) {
938             if (strstr(dent->d_name,".afm")) {
939                 char *afmfn;
940
941                 afmfn=(char*)HeapAlloc(PSDRV_Heap,0, 
942                         strlen(afmdir)+strlen(dent->d_name)+2);
943                 if (afmfn == NULL) {
944                     closedir(dir);
945                     return FALSE;
946                 }
947                 strcpy(afmfn,afmdir);
948                 strcat(afmfn,"/");
949                 strcat(afmfn,dent->d_name);
950                 TRACE("loading AFM %s\n",afmfn);
951                 afm = PSDRV_AFMParse(afmfn);
952                 if (afm) {
953                     if(afm->EncodingScheme && 
954                        !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
955                         PSDRV_ReencodeCharWidths(afm);
956                     }
957                     if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
958                         closedir(dir);
959                         FreeAFM(afm);
960                         return FALSE;
961                     }
962                 }
963                 else {
964                     WARN("Error parsing %s\n", afmfn);
965                 }
966                 HeapFree(PSDRV_Heap,0,afmfn);
967             }
968         }
969         closedir(dir);
970     }
971     else {
972         WARN("Error opening %s\n", afmdir);
973     }
974     
975     return TRUE;
976 }
977
978 BOOL PSDRV_GetFontMetrics(void)
979 {
980     int idx = 0;
981     char key[256];
982     char value[256];
983
984     if (PSDRV_GlyphListInit() != 0)
985         return FALSE;
986
987     while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key),
988             value, sizeof(value)))
989     {
990         AFM* afm = PSDRV_AFMParse(value);
991         
992         if (afm) {
993             if(afm->EncodingScheme && 
994                !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
995                 PSDRV_ReencodeCharWidths(afm);
996             }
997             if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
998                 return FALSE;
999             }
1000         }
1001         else {
1002             WARN("Error parsing %s\n", value);
1003         }
1004     }
1005
1006     for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
1007             value, sizeof (value)); ++idx)
1008         if (PSDRV_ReadAFMDir (value) == FALSE)
1009             return FALSE;
1010
1011     PSDRV_IndexGlyphList();             /* So SortFontMetrics will work */
1012     if (SortFontMetrics() == FALSE)
1013         return FALSE;
1014     CalcWindowsMetrics();
1015     if (AddBuiltinAFMs() == FALSE)
1016         return FALSE;
1017
1018 #ifdef HAVE_FREETYPE   
1019     if (PSDRV_GetTrueTypeMetrics() == FALSE)
1020         return FALSE;
1021     PSDRV_IndexGlyphList();
1022 #endif
1023
1024     PSDRV_DumpFontList();
1025     return TRUE;
1026 }