Reimplemented DXGrab with improvements; it no longer depends on
[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 <stdio.h>
11 #include <sys/stat.h>
12 #include <dirent.h>
13 #include "winnt.h" /* HEAP_ZERO_MEMORY */
14 #include "psdrv.h"
15 #include "options.h"
16 #include "debugtools.h"
17 #include "heap.h"
18
19 DEFAULT_DEBUG_CHANNEL(psdrv);
20 #include <ctype.h>
21
22 /* ptr to fonts for which we have afm files */
23 FONTFAMILY *PSDRV_AFMFontList = NULL;
24
25
26 /***********************************************************
27  *
28  *      PSDRV_AFMGetCharMetrics
29  *
30  * Parses CharMetric section of AFM file.
31  *
32  * Actually only collects the widths of numbered chars and puts then in
33  * afm->CharWidths.
34  */
35 static void PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
36 {
37     unsigned char line[256], valbuf[256];
38     unsigned char *cp, *item, *value, *curpos, *endpos;
39     int i;
40     AFMMETRICS *metric;
41
42     afm->Metrics = metric = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
43                                        afm->NumofMetrics * sizeof(AFMMETRICS) );
44     for(i = 0; i < afm->NumofMetrics; i++, metric++) {
45
46         do {
47             if(!fgets(line, sizeof(line), fp)) {
48                 ERR("Unexpected EOF\n");
49                 return;
50             }
51             cp = line + strlen(line);
52             do {
53                 *cp = '\0';
54                 cp--;
55             } while(cp >= line && isspace(*cp));
56         } while (!(*line));
57
58         curpos = line;
59         while(*curpos) {
60             item = curpos;
61             while(isspace(*item))
62                 item++;
63             value = strpbrk(item, " \t");
64             if (!value) { ERR("No whitespace found.\n");return;}
65             while(isspace(*value))
66                 value++;
67             cp = endpos = strchr(value, ';');
68             if (!cp) { ERR("missing ;, failed. [%s]\n", line); return; }
69             while(isspace(*--cp))
70                 ;
71             memcpy(valbuf, value, cp - value + 1);
72             valbuf[cp - value + 1] = '\0';
73             value = valbuf;
74
75             if(!strncmp(item, "C ", 2)) {
76                 value = strchr(item, ' ');
77                 sscanf(value, " %d", &metric->C);
78
79             } else if(!strncmp(item, "CH ", 3)) {
80                 value = strrchr(item, ' ');
81                 sscanf(value, " %x", &metric->C);
82             }
83
84             else if(!strncmp("WX ", item, 3) || !strncmp("W0X ", item, 4)) {
85                 sscanf(value, "%f", &metric->WX);
86                 if(metric->C >= 0 && metric->C <= 0xff)
87                     afm->CharWidths[metric->C] = metric->WX;
88             }
89
90             else if(!strncmp("N ", item, 2)) {
91                 metric->N = PSDRV_GlyphName(value);
92             }
93
94             else if(!strncmp("B ", item, 2)) {
95                 sscanf(value, "%f%f%f%f", &metric->B.llx, &metric->B.lly,
96                                           &metric->B.urx, &metric->B.ury);
97
98                 /* Store height of Aring to use as lfHeight */
99                 if(metric->N && !strncmp(metric->N->sz, "Aring", 5))
100                     afm->FullAscender = metric->B.ury;
101             }
102
103             /* Ligatures go here... */
104
105             curpos = endpos + 1;
106         }
107
108         TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
109               metric->N->sz, metric->WX, metric->B.llx, metric->B.lly,
110               metric->B.urx, metric->B.ury);
111     }
112
113     return;
114 }
115
116 /***********************************************************
117  *
118  *      PSDRV_AFMParse
119  *
120  * Fills out an AFM structure and associated substructures (see psdrv.h)
121  * for a given AFM file. All memory is allocated from the process heap. 
122  * Returns a ptr to the AFM structure or NULL on error.
123  *
124  * This is not complete (we don't handle kerning yet) and not efficient
125  */
126 static AFM *PSDRV_AFMParse(char const *file)
127 {
128     FILE *fp;
129     unsigned char buf[256];
130     unsigned char *value;
131     AFM *afm;
132     unsigned char *cp;
133     int afmfile = 0; 
134     int c;
135
136     TRACE("parsing '%s'\n", file);
137
138     if((fp = fopen(file, "r")) == NULL) {
139         MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
140         return NULL;
141     }
142
143     afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
144     if(!afm) {
145         fclose(fp);
146         return NULL;
147     }
148
149     cp = buf; 
150     while ( ( c = fgetc ( fp ) ) != EOF ) {
151         *cp = c;
152         if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
153             if ( cp == buf ) 
154                 continue;
155             *(cp+1)='\0';
156         }
157         else {
158             cp ++; 
159             continue;
160         }
161       
162         cp = buf + strlen(buf);
163         do {
164             *cp = '\0';
165             cp--;
166         } while(cp > buf && isspace(*cp));
167
168         cp = buf; 
169
170         if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
171             break;
172         afmfile = 1; 
173
174         value = strchr(buf, ' ');
175         if(value)
176             while(isspace(*value))
177                 value++;
178
179         if(!strncmp("FontName", buf, 8)) {
180             afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
181             continue;
182         }
183
184         if(!strncmp("FullName", buf, 8)) {
185             afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
186             continue;
187         }
188
189         if(!strncmp("FamilyName", buf, 10)) {
190             afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
191             continue;
192         }
193         
194         if(!strncmp("Weight", buf, 6)) {
195             if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
196                || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
197                || !strncmp("Normal", value, 6))
198                 afm->Weight = FW_NORMAL;
199             else if(!strncmp("Demi", value, 4))
200                 afm->Weight = FW_DEMIBOLD;
201             else if(!strncmp("Bold", value, 4))
202                 afm->Weight = FW_BOLD;
203             else if(!strncmp("Light", value, 5))
204                 afm->Weight = FW_LIGHT;
205             else if(!strncmp("Black", value, 5))
206                 afm->Weight = FW_BLACK;
207             else {
208                 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
209                      file, value);
210                 afm->Weight = FW_NORMAL;
211             }
212             continue;
213         }
214
215         if(!strncmp("ItalicAngle", buf, 11)) {
216             sscanf(value, "%f", &(afm->ItalicAngle));
217             continue;
218         }
219
220         if(!strncmp("IsFixedPitch", buf, 12)) {
221             if(!strncasecmp("false", value, 5))
222                 afm->IsFixedPitch = FALSE;
223             else
224                 afm->IsFixedPitch = TRUE;
225             continue;
226         }
227
228         if(!strncmp("FontBBox", buf, 8)) {
229             sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx), 
230                    &(afm->FontBBox.lly), &(afm->FontBBox.urx), 
231                    &(afm->FontBBox.ury) );
232             continue;
233         }
234
235         if(!strncmp("UnderlinePosition", buf, 17)) {
236             sscanf(value, "%f", &(afm->UnderlinePosition) );
237             continue;
238         }
239
240         if(!strncmp("UnderlineThickness", buf, 18)) {
241             sscanf(value, "%f", &(afm->UnderlineThickness) );
242             continue;
243         }
244
245         if(!strncmp("CapHeight", buf, 9)) {
246             sscanf(value, "%f", &(afm->CapHeight) );
247             continue;
248         }
249
250         if(!strncmp("XHeight", buf, 7)) {
251             sscanf(value, "%f", &(afm->XHeight) );
252             continue;
253         }
254
255         if(!strncmp("Ascender", buf, 8)) {
256             sscanf(value, "%f", &(afm->Ascender) );
257             continue;
258         }
259
260         if(!strncmp("Descender", buf, 9)) {
261             sscanf(value, "%f", &(afm->Descender) );
262             continue;
263         }
264
265         if(!strncmp("StartCharMetrics", buf, 16)) {
266             sscanf(value, "%d", &(afm->NumofMetrics) );
267             PSDRV_AFMGetCharMetrics(afm, fp);
268             continue;
269         }
270
271         if(!strncmp("EncodingScheme", buf, 14)) {
272             afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
273             continue;
274         }
275
276     }
277     fclose(fp);
278
279     if (afmfile == 0) {
280         HeapFree ( PSDRV_Heap, 0, afm ); 
281         return NULL;
282     }
283
284     if(afm->FontName == NULL) {
285         WARN("%s contains no FontName.\n", file);
286         afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
287     }
288     if(afm->FullName == NULL)
289         afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
290     if(afm->FamilyName == NULL)
291         afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);      
292     if(afm->Ascender == 0.0)
293         afm->Ascender = afm->FontBBox.ury;
294     if(afm->Descender == 0.0)
295         afm->Descender = afm->FontBBox.lly;
296     if(afm->FullAscender == 0.0)
297         afm->FullAscender = afm->Ascender;
298     if(afm->Weight == 0)
299         afm->Weight = FW_NORMAL;
300
301     return afm;
302 }
303
304 /***********************************************************
305  *
306  *      PSDRV_FreeAFMList
307  *
308  * Frees the family and afmlistentry structures in list head
309  */
310 void PSDRV_FreeAFMList( FONTFAMILY *head )
311 {
312     AFMLISTENTRY *afmle, *nexta;
313     FONTFAMILY *family, *nextf;
314
315     for(nextf = family = head; nextf; family = nextf) {
316         for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
317             nexta = afmle->next;
318             HeapFree( PSDRV_Heap, 0, afmle );
319         }
320         nextf = family->next;
321         HeapFree( PSDRV_Heap, 0, family );
322     }
323     return;
324 }
325
326
327 /***********************************************************
328  *
329  *      PSDRV_FindAFMinList
330  * Returns ptr to an AFM if name (which is a PS font name) exists in list
331  * headed by head.
332  */
333 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
334 {
335     FONTFAMILY *family;
336     AFMLISTENTRY *afmle;
337
338     for(family = head; family; family = family->next) {
339         for(afmle = family->afmlist; afmle; afmle = afmle->next) {
340             if(!strcmp(afmle->afm->FontName, name))
341                 return afmle->afm;
342         }
343     }
344     return NULL;
345 }
346
347 /***********************************************************
348  *
349  *      PSDRV_AddAFMtoList
350  *
351  * Adds an afm to the list whose head is pointed to by head. Creates new
352  * family node if necessary and always creates a new AFMLISTENTRY.
353  */
354 void PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
355 {
356     FONTFAMILY *family = *head;
357     FONTFAMILY **insert = head;
358     AFMLISTENTRY *tmpafmle, *newafmle;
359
360     newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
361                            sizeof(*newafmle));
362     newafmle->afm = afm;
363
364     while(family) {
365         if(!strcmp(family->FamilyName, afm->FamilyName))
366             break;
367         insert = &(family->next);
368         family = family->next;
369     }
370  
371     if(!family) {
372         family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
373                            sizeof(*family));
374         *insert = family;
375         family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
376                                           afm->FamilyName);
377         family->afmlist = newafmle;
378         return;
379     }
380     
381     tmpafmle = family->afmlist;
382     while(tmpafmle->next)
383         tmpafmle = tmpafmle->next;
384
385     tmpafmle->next = newafmle;
386
387     return;
388 }
389
390 /**********************************************************
391  *
392  *      PSDRV_ReencodeCharWidths
393  *
394  * Re map the CharWidths field of the afm to correspond to an ANSI encoding
395  *
396  */
397 static void PSDRV_ReencodeCharWidths(AFM *afm)
398 {
399     int i, j;
400     AFMMETRICS *metric;
401
402     for(i = 0; i < 256; i++) {
403         if(isalnum(i))
404             continue;
405         if(PSDRV_ANSIVector[i] == NULL) {
406             afm->CharWidths[i] = 0.0;
407             continue;
408         }
409         for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
410             if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
411                 afm->CharWidths[i] = metric->WX;
412                 break;
413             }
414         }
415         if(j == afm->NumofMetrics) {
416             WARN("Couldn't find glyph '%s' in font '%s'\n",
417                  PSDRV_ANSIVector[i], afm->FontName);
418             afm->CharWidths[i] = 0.0;
419         }
420     }
421     return;
422 }
423
424
425 /***********************************************************
426  *
427  *      PSDRV_DumpFontList
428  *
429  */
430 static void PSDRV_DumpFontList(void)
431 {
432     FONTFAMILY *family;
433     AFMLISTENTRY *afmle;
434
435     for(family = PSDRV_AFMFontList; family; family = family->next) {
436         TRACE("Family '%s'\n", family->FamilyName);
437         for(afmle = family->afmlist; afmle; afmle = afmle->next) {
438             TRACE("\tFontName '%s'\n", afmle->afm->FontName);
439         }
440     }
441     return;
442 }
443
444
445 /***********************************************************
446  *
447  *      PSDRV_GetFontMetrics
448  *
449  * Only exported function in this file. Parses all afm files listed in
450  * [afmfiles] and [afmdirs] of wine.conf .
451  */
452
453 static void PSDRV_ReadAFMDir(const char* afmdir) {
454     DIR *dir;
455     AFM *afm;
456
457     dir = opendir(afmdir);
458     if (dir) {
459         struct dirent *dent;
460         while ((dent=readdir(dir))) {
461             if (strstr(dent->d_name,".afm")) {
462                 char *afmfn;
463
464                 afmfn=(char*)HeapAlloc(GetProcessHeap(),0,strlen(afmdir)+strlen(dent->d_name)+2);
465                 strcpy(afmfn,afmdir);
466                 strcat(afmfn,"/");
467                 strcat(afmfn,dent->d_name);
468                 TRACE("loading AFM %s\n",afmfn);
469                 afm = PSDRV_AFMParse(afmfn);
470                 if (afm) {
471                     if(afm->EncodingScheme && 
472                        !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
473                         PSDRV_ReencodeCharWidths(afm);
474                     }
475                     PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm);
476                 }
477                 HeapFree(GetProcessHeap(),0,afmfn);
478             }
479         }
480         closedir(dir);
481     }
482 }
483
484 BOOL PSDRV_GetFontMetrics(void)
485 {
486     int idx = 0;
487     char key[256];
488     char value[256];
489
490     if (PSDRV_GlyphListInit() != 0)
491         return FALSE;
492
493     while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key), value, sizeof(value)))
494     {
495         AFM* afm = PSDRV_AFMParse(value);
496         if (afm)
497         {
498             if(afm->EncodingScheme && 
499                !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
500                 PSDRV_ReencodeCharWidths(afm);
501             }
502             PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm);
503         }
504     }
505
506     for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
507             value, sizeof (value)); ++idx)
508         PSDRV_ReadAFMDir (value);
509
510     PSDRV_DumpGlyphList();
511     PSDRV_DumpFontList();
512     return TRUE;
513 }
514