Misc. error checking in PostScript driver.
[wine/multimedia.git] / dlls / wineps / afm.c
blob10b3074c86aefbf2f690d237568b324763952cac
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 <limits.h> /* INT_MIN */
14 #include <float.h> /* FLT_MAX */
15 #include "winnt.h" /* HEAP_ZERO_MEMORY */
16 #include "psdrv.h"
17 #include "options.h"
18 #include "debugtools.h"
19 #include "heap.h"
21 DEFAULT_DEBUG_CHANNEL(psdrv);
22 #include <ctype.h>
24 /* ptr to fonts for which we have afm files */
25 FONTFAMILY *PSDRV_AFMFontList = NULL;
27 /*******************************************************************************
28 * CheckMetrics
30 * Check an AFMMETRICS structure to make sure all elements have been properly
31 * filled in.
34 static const AFMMETRICS badMetrics =
36 INT_MIN, /* C */
37 FLT_MAX, /* WX */
38 NULL, /* N */
39 { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX }, /* B */
40 NULL /* L */
43 inline static BOOL CheckMetrics(const AFMMETRICS *metrics)
45 if ( metrics->C == badMetrics.C ||
46 metrics->WX == badMetrics.WX ||
47 metrics->N == badMetrics.N ||
48 metrics->B.llx == badMetrics.B.llx ||
49 metrics->B.lly == badMetrics.B.lly ||
50 metrics->B.urx == badMetrics.B.urx ||
51 metrics->B.ury == badMetrics.B.ury )
52 return FALSE;
54 return TRUE;
57 /*******************************************************************************
58 * FreeAFM
60 * Free an AFM structure and any subsidiary objects that have been allocated
63 static void FreeAFM(AFM *afm)
65 if (afm->FontName != NULL)
66 HeapFree(PSDRV_Heap, 0, afm->FontName);
67 if (afm->FullName != NULL)
68 HeapFree(PSDRV_Heap, 0, afm->FullName);
69 if (afm->FamilyName != NULL)
70 HeapFree(PSDRV_Heap, 0, afm->FamilyName);
71 if (afm->EncodingScheme != NULL)
72 HeapFree(PSDRV_Heap, 0, afm->EncodingScheme);
73 if (afm->Metrics != NULL)
74 HeapFree(PSDRV_Heap, 0, afm->Metrics);
76 HeapFree(PSDRV_Heap, 0, afm);
80 /***********************************************************
82 * PSDRV_AFMGetCharMetrics
84 * Parses CharMetric section of AFM file.
86 * Actually only collects the widths of numbered chars and puts then in
87 * afm->CharWidths.
89 static BOOL PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
91 unsigned char line[256], valbuf[256];
92 unsigned char *cp, *item, *value, *curpos, *endpos;
93 int i;
94 AFMMETRICS *metric;
96 afm->Metrics = metric = HeapAlloc( PSDRV_Heap, 0,
97 afm->NumofMetrics * sizeof(AFMMETRICS) );
98 if (metric == NULL)
99 return FALSE;
101 for(i = 0; i < afm->NumofMetrics; i++, metric++) {
103 *metric = badMetrics;
105 do {
106 if(!fgets(line, sizeof(line), fp)) {
107 ERR("Unexpected EOF\n");
108 HeapFree(PSDRV_Heap, 0, afm->Metrics);
109 afm->Metrics = NULL;
110 return FALSE;
112 cp = line + strlen(line);
113 do {
114 *cp = '\0';
115 cp--;
116 } while(cp >= line && isspace(*cp));
117 } while (!(*line));
119 curpos = line;
120 while(*curpos) {
121 item = curpos;
122 while(isspace(*item))
123 item++;
124 value = strpbrk(item, " \t");
125 if (!value) {
126 ERR("No whitespace found.\n");
127 HeapFree(PSDRV_Heap, 0, afm->Metrics);
128 afm->Metrics = NULL;
129 return FALSE;
131 while(isspace(*value))
132 value++;
133 cp = endpos = strchr(value, ';');
134 if (!cp) {
135 ERR("missing ;, failed. [%s]\n", line);
136 HeapFree(PSDRV_Heap, 0, afm->Metrics);
137 afm->Metrics = NULL;
138 return FALSE;
140 while(isspace(*--cp))
142 memcpy(valbuf, value, cp - value + 1);
143 valbuf[cp - value + 1] = '\0';
144 value = valbuf;
146 if(!strncmp(item, "C ", 2)) {
147 value = strchr(item, ' ');
148 sscanf(value, " %d", &metric->C);
150 } else if(!strncmp(item, "CH ", 3)) {
151 value = strrchr(item, ' ');
152 sscanf(value, " %x", &metric->C);
155 else if(!strncmp("WX ", item, 3) || !strncmp("W0X ", item, 4)) {
156 sscanf(value, "%f", &metric->WX);
157 if(metric->C >= 0 && metric->C <= 0xff)
158 afm->CharWidths[metric->C] = metric->WX;
161 else if(!strncmp("N ", item, 2)) {
162 metric->N = PSDRV_GlyphName(value);
165 else if(!strncmp("B ", item, 2)) {
166 sscanf(value, "%f%f%f%f", &metric->B.llx, &metric->B.lly,
167 &metric->B.urx, &metric->B.ury);
169 /* Store height of Aring to use as lfHeight */
170 if(metric->N && !strncmp(metric->N->sz, "Aring", 5))
171 afm->FullAscender = metric->B.ury;
174 /* Ligatures go here... */
176 curpos = endpos + 1;
179 if (CheckMetrics(metric) == FALSE) {
180 ERR("Error parsing character metrics\n");
181 HeapFree(PSDRV_Heap, 0, afm->Metrics);
182 afm->Metrics = NULL;
183 return FALSE;
186 TRACE("Metrics for '%s' WX = %f B = %f,%f - %f,%f\n",
187 metric->N->sz, metric->WX, metric->B.llx, metric->B.lly,
188 metric->B.urx, metric->B.ury);
191 return TRUE;
194 /***********************************************************
196 * PSDRV_AFMParse
198 * Fills out an AFM structure and associated substructures (see psdrv.h)
199 * for a given AFM file. All memory is allocated from the process heap.
200 * Returns a ptr to the AFM structure or NULL on error.
202 * This is not complete (we don't handle kerning yet) and not efficient
205 static AFM *PSDRV_AFMParse(char const *file)
207 FILE *fp;
208 unsigned char buf[256];
209 unsigned char *value;
210 AFM *afm;
211 unsigned char *cp;
212 int afmfile = 0;
213 int c;
215 TRACE("parsing '%s'\n", file);
217 if((fp = fopen(file, "r")) == NULL) {
218 MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
219 return NULL;
222 afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
223 if(!afm) {
224 fclose(fp);
225 return NULL;
228 cp = buf;
229 while ( ( c = fgetc ( fp ) ) != EOF ) {
230 *cp = c;
231 if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
232 if ( cp == buf )
233 continue;
234 *(cp+1)='\0';
236 else {
237 cp ++;
238 continue;
241 cp = buf + strlen(buf);
242 do {
243 *cp = '\0';
244 cp--;
245 } while(cp > buf && isspace(*cp));
247 cp = buf;
249 if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
250 break;
251 afmfile = 1;
253 value = strchr(buf, ' ');
254 if(value)
255 while(isspace(*value))
256 value++;
258 if(!strncmp("FontName", buf, 8)) {
259 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
260 if (afm->FontName == NULL) {
261 fclose(fp);
262 FreeAFM(afm);
263 return NULL;
265 continue;
268 if(!strncmp("FullName", buf, 8)) {
269 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
270 if (afm->FullName == NULL) {
271 fclose(fp);
272 FreeAFM(afm);
273 return NULL;
275 continue;
278 if(!strncmp("FamilyName", buf, 10)) {
279 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
280 if (afm->FamilyName == NULL) {
281 fclose(fp);
282 FreeAFM(afm);
283 return NULL;
285 continue;
288 if(!strncmp("Weight", buf, 6)) {
289 if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
290 || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
291 || !strncmp("Normal", value, 6))
292 afm->Weight = FW_NORMAL;
293 else if(!strncmp("Demi", value, 4))
294 afm->Weight = FW_DEMIBOLD;
295 else if(!strncmp("Bold", value, 4))
296 afm->Weight = FW_BOLD;
297 else if(!strncmp("Light", value, 5))
298 afm->Weight = FW_LIGHT;
299 else if(!strncmp("Black", value, 5))
300 afm->Weight = FW_BLACK;
301 else {
302 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
303 file, value);
304 afm->Weight = FW_NORMAL;
306 continue;
309 if(!strncmp("ItalicAngle", buf, 11)) {
310 sscanf(value, "%f", &(afm->ItalicAngle));
311 continue;
314 if(!strncmp("IsFixedPitch", buf, 12)) {
315 if(!strncasecmp("false", value, 5))
316 afm->IsFixedPitch = FALSE;
317 else
318 afm->IsFixedPitch = TRUE;
319 continue;
322 if(!strncmp("FontBBox", buf, 8)) {
323 sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx),
324 &(afm->FontBBox.lly), &(afm->FontBBox.urx),
325 &(afm->FontBBox.ury) );
326 continue;
329 if(!strncmp("UnderlinePosition", buf, 17)) {
330 sscanf(value, "%f", &(afm->UnderlinePosition) );
331 continue;
334 if(!strncmp("UnderlineThickness", buf, 18)) {
335 sscanf(value, "%f", &(afm->UnderlineThickness) );
336 continue;
339 if(!strncmp("CapHeight", buf, 9)) {
340 sscanf(value, "%f", &(afm->CapHeight) );
341 continue;
344 if(!strncmp("XHeight", buf, 7)) {
345 sscanf(value, "%f", &(afm->XHeight) );
346 continue;
349 if(!strncmp("Ascender", buf, 8)) {
350 sscanf(value, "%f", &(afm->Ascender) );
351 continue;
354 if(!strncmp("Descender", buf, 9)) {
355 sscanf(value, "%f", &(afm->Descender) );
356 continue;
359 if(!strncmp("StartCharMetrics", buf, 16)) {
360 sscanf(value, "%d", &(afm->NumofMetrics) );
361 if (PSDRV_AFMGetCharMetrics(afm, fp) == FALSE) {
362 fclose(fp);
363 FreeAFM(afm);
364 return NULL;
366 continue;
369 if(!strncmp("EncodingScheme", buf, 14)) {
370 afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
371 if (afm->EncodingScheme == NULL) {
372 fclose(fp);
373 FreeAFM(afm);
374 return NULL;
376 continue;
380 fclose(fp);
382 if (afmfile == 0) {
383 HeapFree ( PSDRV_Heap, 0, afm );
384 return NULL;
387 if(afm->FontName == NULL) {
388 WARN("%s contains no FontName.\n", file);
389 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
390 if (afm->FontName == NULL) {
391 FreeAFM(afm);
392 return NULL;
396 if(afm->FullName == NULL)
397 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
398 if(afm->FamilyName == NULL)
399 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
400 if (afm->FullName == NULL || afm->FamilyName == NULL) {
401 FreeAFM(afm);
402 return NULL;
405 if(afm->Ascender == 0.0)
406 afm->Ascender = afm->FontBBox.ury;
407 if(afm->Descender == 0.0)
408 afm->Descender = afm->FontBBox.lly;
409 if(afm->FullAscender == 0.0)
410 afm->FullAscender = afm->Ascender;
411 if(afm->Weight == 0)
412 afm->Weight = FW_NORMAL;
414 return afm;
417 /***********************************************************
419 * PSDRV_FreeAFMList
421 * Frees the family and afmlistentry structures in list head
423 void PSDRV_FreeAFMList( FONTFAMILY *head )
425 AFMLISTENTRY *afmle, *nexta;
426 FONTFAMILY *family, *nextf;
428 for(nextf = family = head; nextf; family = nextf) {
429 for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
430 nexta = afmle->next;
431 HeapFree( PSDRV_Heap, 0, afmle );
433 nextf = family->next;
434 HeapFree( PSDRV_Heap, 0, family );
436 return;
440 /***********************************************************
442 * PSDRV_FindAFMinList
443 * Returns ptr to an AFM if name (which is a PS font name) exists in list
444 * headed by head.
446 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
448 FONTFAMILY *family;
449 AFMLISTENTRY *afmle;
451 for(family = head; family; family = family->next) {
452 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
453 if(!strcmp(afmle->afm->FontName, name))
454 return afmle->afm;
457 return NULL;
460 /***********************************************************
462 * PSDRV_AddAFMtoList
464 * Adds an afm to the list whose head is pointed to by head. Creates new
465 * family node if necessary and always creates a new AFMLISTENTRY.
467 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
469 FONTFAMILY *family = *head;
470 FONTFAMILY **insert = head;
471 AFMLISTENTRY *tmpafmle, *newafmle;
473 newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
474 sizeof(*newafmle));
475 if (newafmle == NULL)
476 return FALSE;
478 newafmle->afm = afm;
480 while(family) {
481 if(!strcmp(family->FamilyName, afm->FamilyName))
482 break;
483 insert = &(family->next);
484 family = family->next;
487 if(!family) {
488 family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
489 sizeof(*family));
490 if (family == NULL) {
491 HeapFree(PSDRV_Heap, 0, newafmle);
492 return FALSE;
494 *insert = family;
495 family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
496 afm->FamilyName);
497 if (family->FamilyName == NULL) {
498 HeapFree(PSDRV_Heap, 0, family);
499 HeapFree(PSDRV_Heap, 0, newafmle);
500 return FALSE;
502 family->afmlist = newafmle;
503 return TRUE;
506 tmpafmle = family->afmlist;
507 while(tmpafmle->next)
508 tmpafmle = tmpafmle->next;
510 tmpafmle->next = newafmle;
512 return TRUE;
515 /**********************************************************
517 * PSDRV_ReencodeCharWidths
519 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
522 static void PSDRV_ReencodeCharWidths(AFM *afm)
524 int i, j;
525 AFMMETRICS *metric;
527 for(i = 0; i < 256; i++) {
528 if(isalnum(i))
529 continue;
530 if(PSDRV_ANSIVector[i] == NULL) {
531 afm->CharWidths[i] = 0.0;
532 continue;
534 for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
535 if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
536 afm->CharWidths[i] = metric->WX;
537 break;
540 if(j == afm->NumofMetrics) {
541 WARN("Couldn't find glyph '%s' in font '%s'\n",
542 PSDRV_ANSIVector[i], afm->FontName);
543 afm->CharWidths[i] = 0.0;
546 return;
550 /***********************************************************
552 * PSDRV_DumpFontList
555 static void PSDRV_DumpFontList(void)
557 FONTFAMILY *family;
558 AFMLISTENTRY *afmle;
560 for(family = PSDRV_AFMFontList; family; family = family->next) {
561 TRACE("Family '%s'\n", family->FamilyName);
562 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
563 TRACE("\tFontName '%s'\n", afmle->afm->FontName);
566 return;
570 /***********************************************************
572 * PSDRV_GetFontMetrics
574 * Parses all afm files listed in [afmfiles] and [afmdirs] of wine.conf
576 * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
577 * about freeing all the memory that's been allocated.
580 static BOOL PSDRV_ReadAFMDir(const char* afmdir) {
581 DIR *dir;
582 AFM *afm;
584 dir = opendir(afmdir);
585 if (dir) {
586 struct dirent *dent;
587 while ((dent=readdir(dir))) {
588 if (strstr(dent->d_name,".afm")) {
589 char *afmfn;
591 afmfn=(char*)HeapAlloc(PSDRV_Heap,0,
592 strlen(afmdir)+strlen(dent->d_name)+2);
593 if (afmfn == NULL) {
594 closedir(dir);
595 return FALSE;
597 strcpy(afmfn,afmdir);
598 strcat(afmfn,"/");
599 strcat(afmfn,dent->d_name);
600 TRACE("loading AFM %s\n",afmfn);
601 afm = PSDRV_AFMParse(afmfn);
602 if (afm) {
603 if(afm->EncodingScheme &&
604 !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
605 PSDRV_ReencodeCharWidths(afm);
607 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
608 closedir(dir);
609 FreeAFM(afm);
610 return FALSE;
613 else {
614 WARN("Error parsing %s\n", afmfn);
616 HeapFree(PSDRV_Heap,0,afmfn);
619 closedir(dir);
621 else {
622 WARN("Error opening %s\n", afmdir);
625 return TRUE;
628 BOOL PSDRV_GetFontMetrics(void)
630 int idx = 0;
631 char key[256];
632 char value[256];
634 if (PSDRV_GlyphListInit() != 0)
635 return FALSE;
637 while (PROFILE_EnumWineIniString( "afmfiles", idx++, key, sizeof(key),
638 value, sizeof(value)))
640 AFM* afm = PSDRV_AFMParse(value);
642 if (afm) {
643 if(afm->EncodingScheme &&
644 !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
645 PSDRV_ReencodeCharWidths(afm);
647 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
648 return FALSE;
651 else {
652 WARN("Error parsing %s\n", value);
656 for (idx = 0; PROFILE_EnumWineIniString ("afmdirs", idx, key, sizeof (key),
657 value, sizeof (value)); ++idx)
658 if (PSDRV_ReadAFMDir (value) == FALSE)
659 return FALSE;
661 PSDRV_DumpGlyphList();
662 PSDRV_DumpFontList();
663 return TRUE;