Don't load AFM files from predefined directories; use [afmdirs]
[wine/multimedia.git] / dlls / wineps / afm.c
blob243ea0ddd3bb1c44bf3e814b600909eeb60039e7
1 /*
2 * Adobe Font Metric (AFM) file parsing
3 * See http://partners.adobe.com/asn/developer/PDFS/TN/5004.AFM_Spec.pdf
5 * Copyright 1998 Huw D M Davies
6 *
7 */
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"
19 DEFAULT_DEBUG_CHANNEL(psdrv);
20 #include <ctype.h>
22 /* ptr to fonts for which we have afm files */
23 FONTFAMILY *PSDRV_AFMFontList = NULL;
26 /***********************************************************
28 * PSDRV_AFMGetCharMetrics
30 * Parses CharMetric section of AFM file.
32 * Actually only collects the widths of numbered chars and puts then in
33 * afm->CharWidths.
35 static void PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
37 unsigned char line[256], valbuf[256];
38 unsigned char *cp, *item, *value, *curpos, *endpos;
39 int i;
40 AFMMETRICS *metric;
42 afm->Metrics = metric = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
43 afm->NumofMetrics * sizeof(AFMMETRICS) );
44 for(i = 0; i < afm->NumofMetrics; i++, metric++) {
46 do {
47 if(!fgets(line, sizeof(line), fp)) {
48 ERR("Unexpected EOF\n");
49 return;
51 cp = line + strlen(line);
52 do {
53 *cp = '\0';
54 cp--;
55 } while(cp >= line && isspace(*cp));
56 } while (!(*line));
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))
71 memcpy(valbuf, value, cp - value + 1);
72 valbuf[cp - value + 1] = '\0';
73 value = valbuf;
75 if(!strncmp(item, "C ", 2)) {
76 value = strchr(item, ' ');
77 sscanf(value, " %d", &metric->C);
79 } else if(!strncmp(item, "CH ", 3)) {
80 value = strrchr(item, ' ');
81 sscanf(value, " %x", &metric->C);
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;
90 else if(!strncmp("N ", item, 2)) {
91 strncpy( metric->N, value, sizeof(metric->N) );
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);
98 /* Store height of Aring to use as lfHeight */
99 if(metric->N && !strncmp(metric->N, "Aring", 5))
100 afm->FullAscender = metric->B.ury;
103 /* Ligatures go here... */
105 curpos = endpos + 1;
108 TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
109 metric->N, metric->WX, metric->B.llx, metric->B.lly,
110 metric->B.urx, metric->B.ury);
113 return;
116 /***********************************************************
118 * PSDRV_AFMParse
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.
124 * This is not complete (we don't handle kerning yet) and not efficient
126 static AFM *PSDRV_AFMParse(char const *file)
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;
136 TRACE("parsing '%s'\n", file);
138 if((fp = fopen(file, "r")) == NULL) {
139 MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
140 return NULL;
143 afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
144 if(!afm) {
145 fclose(fp);
146 return NULL;
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';
157 else {
158 cp ++;
159 continue;
162 cp = buf + strlen(buf);
163 do {
164 *cp = '\0';
165 cp--;
166 } while(cp > buf && isspace(*cp));
168 cp = buf;
170 if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
171 break;
172 afmfile = 1;
174 value = strchr(buf, ' ');
175 if(value)
176 while(isspace(*value))
177 value++;
179 if(!strncmp("FontName", buf, 8)) {
180 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
181 continue;
184 if(!strncmp("FullName", buf, 8)) {
185 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
186 continue;
189 if(!strncmp("FamilyName", buf, 10)) {
190 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
191 continue;
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;
212 continue;
215 if(!strncmp("ItalicAngle", buf, 11)) {
216 sscanf(value, "%f", &(afm->ItalicAngle));
217 continue;
220 if(!strncmp("IsFixedPitch", buf, 12)) {
221 if(!strncasecmp("false", value, 5))
222 afm->IsFixedPitch = FALSE;
223 else
224 afm->IsFixedPitch = TRUE;
225 continue;
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;
235 if(!strncmp("UnderlinePosition", buf, 17)) {
236 sscanf(value, "%f", &(afm->UnderlinePosition) );
237 continue;
240 if(!strncmp("UnderlineThickness", buf, 18)) {
241 sscanf(value, "%f", &(afm->UnderlineThickness) );
242 continue;
245 if(!strncmp("CapHeight", buf, 9)) {
246 sscanf(value, "%f", &(afm->CapHeight) );
247 continue;
250 if(!strncmp("XHeight", buf, 7)) {
251 sscanf(value, "%f", &(afm->XHeight) );
252 continue;
255 if(!strncmp("Ascender", buf, 8)) {
256 sscanf(value, "%f", &(afm->Ascender) );
257 continue;
260 if(!strncmp("Descender", buf, 9)) {
261 sscanf(value, "%f", &(afm->Descender) );
262 continue;
265 if(!strncmp("StartCharMetrics", buf, 16)) {
266 sscanf(value, "%d", &(afm->NumofMetrics) );
267 PSDRV_AFMGetCharMetrics(afm, fp);
268 continue;
271 if(!strncmp("EncodingScheme", buf, 14)) {
272 afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
273 continue;
277 fclose(fp);
279 if (afmfile == 0) {
280 HeapFree ( PSDRV_Heap, 0, afm );
281 return NULL;
284 if(afm->FontName == NULL) {
285 WARN("%s contains no FontName.\n", file);
286 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
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;
301 return afm;
304 /***********************************************************
306 * PSDRV_FreeAFMList
308 * Frees the family and afmlistentry structures in list head
310 void PSDRV_FreeAFMList( FONTFAMILY *head )
312 AFMLISTENTRY *afmle, *nexta;
313 FONTFAMILY *family, *nextf;
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 );
320 nextf = family->next;
321 HeapFree( PSDRV_Heap, 0, family );
323 return;
327 /***********************************************************
329 * PSDRV_FindAFMinList
330 * Returns ptr to an AFM if name (which is a PS font name) exists in list
331 * headed by head.
333 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
335 FONTFAMILY *family;
336 AFMLISTENTRY *afmle;
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;
344 return NULL;
347 /***********************************************************
349 * PSDRV_AddAFMtoList
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.
354 void PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
356 FONTFAMILY *family = *head;
357 FONTFAMILY **insert = head;
358 AFMLISTENTRY *tmpafmle, *newafmle;
360 newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
361 sizeof(*newafmle));
362 newafmle->afm = afm;
364 while(family) {
365 if(!strcmp(family->FamilyName, afm->FamilyName))
366 break;
367 insert = &(family->next);
368 family = family->next;
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;
381 tmpafmle = family->afmlist;
382 while(tmpafmle->next)
383 tmpafmle = tmpafmle->next;
385 tmpafmle->next = newafmle;
387 return;
390 /**********************************************************
392 * PSDRV_ReencodeCharWidths
394 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
397 static void PSDRV_ReencodeCharWidths(AFM *afm)
399 int i, j;
400 AFMMETRICS *metric;
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;
409 for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
410 if(!strcmp(metric->N, PSDRV_ANSIVector[i])) {
411 afm->CharWidths[i] = metric->WX;
412 break;
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;
421 return;
425 /***********************************************************
427 * PSDRV_DumpFontList
430 static void PSDRV_DumpFontList(void)
432 FONTFAMILY *family;
433 AFMLISTENTRY *afmle;
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);
441 return;
445 /***********************************************************
447 * PSDRV_GetFontMetrics
449 * Only exported function in this file. Parses all afm files listed in
450 * [afmfiles] of wine.conf .
453 static void PSDRV_ReadAFMDir(const char* afmdir) {
454 DIR *dir;
455 AFM *afm;
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;
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);
475 PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm);
477 HeapFree(GetProcessHeap(),0,afmfn);
480 closedir(dir);
484 BOOL PSDRV_GetFontMetrics(void)
486 int idx = 0;
487 char key[256];
488 char value[256];
490 while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key), value, sizeof(value)))
492 AFM* afm = PSDRV_AFMParse(value);
493 if (afm)
495 if(afm->EncodingScheme &&
496 !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
497 PSDRV_ReencodeCharWidths(afm);
499 PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm);
503 for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
504 value, sizeof (value)); ++idx)
505 PSDRV_ReadAFMDir (value);
507 PSDRV_DumpFontList();
508 return TRUE;