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