Remove redundant data structure from font metrics.
[wine/multimedia.git] / dlls / wineps / afm.c
blobffcac1c766aa4e1c2e1ec4c2b96fd62ba29eac4b
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 "config.h"
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 "winreg.h"
21 #include "psdrv.h"
22 #include "debugtools.h"
23 #include "heap.h"
25 DEFAULT_DEBUG_CHANNEL(psdrv);
26 #include <ctype.h>
28 /* ptr to fonts for which we have afm files */
29 FONTFAMILY *PSDRV_AFMFontList = NULL;
31 /* qsort/bsearch callback functions */
32 typedef int (*compar_callback_fn) (const void *, const void *);
34 /*******************************************************************************
35 * IsWinANSI
37 * Checks whether Unicode value is part of Microsoft code page 1252
40 static const INT ansiChars[21] =
42 0x0152, 0x0153, 0x0160, 0x0161, 0x0178, 0x017d, 0x017e, 0x0192, 0x02c6,
43 0x02c9, 0x02dc, 0x03bc, 0x2013, 0x2014, 0x2026, 0x2030, 0x2039, 0x203a,
44 0x20ac, 0x2122, 0x2219
47 static int cmpUV(const INT *a, const INT *b)
49 return *a - *b;
52 inline static BOOL IsWinANSI(INT uv)
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;
59 if (bsearch(&uv, ansiChars, 21, sizeof(INT),
60 (compar_callback_fn)cmpUV) != NULL)
61 return TRUE;
63 return FALSE;
66 /*******************************************************************************
67 * CheckMetrics
69 * Check an AFMMETRICS structure to make sure all elements have been properly
70 * filled in. (Don't check UV or L.)
73 static const AFMMETRICS badMetrics =
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 */
83 inline static BOOL CheckMetrics(const AFMMETRICS *metrics)
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;
94 return TRUE;
97 /*******************************************************************************
98 * FreeAFM
100 * Free an AFM structure and any subsidiary objects that have been allocated.
101 * AFM must have been allocated with HEAP_ZERO_MEMORY.
104 static void FreeAFM(AFM *afm)
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);
117 HeapFree(PSDRV_Heap, 0, afm);
120 /***********************************************************
122 * PSDRV_AFMGetCharMetrics
124 * Parses CharMetric section of AFM file.
126 * Actually only collects the widths of numbered chars and puts then in
127 * afm->CharWidths.
129 static BOOL PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
131 unsigned char line[256], valbuf[256];
132 unsigned char *cp, *item, *value, *curpos, *endpos;
133 int i;
134 AFMMETRICS *metric;
136 afm->Metrics = metric = HeapAlloc( PSDRV_Heap, 0,
137 afm->NumofMetrics * sizeof(AFMMETRICS) );
138 if (metric == NULL)
139 return FALSE;
141 for(i = 0; i < afm->NumofMetrics; i++, metric++) {
143 *metric = badMetrics;
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;
152 cp = line + strlen(line);
153 do {
154 *cp = '\0';
155 cp--;
156 } while(cp >= line && isspace(*cp));
157 } while (!(*line));
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;
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;
180 while(isspace(*--cp))
182 memcpy(valbuf, value, cp - value + 1);
183 valbuf[cp - value + 1] = '\0';
184 value = valbuf;
186 if(!strncmp(item, "C ", 2)) {
187 value = strchr(item, ' ');
188 sscanf(value, " %d", &metric->C);
190 } else if(!strncmp(item, "CH ", 3)) {
191 value = strrchr(item, ' ');
192 sscanf(value, " %x", &metric->C);
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;
201 else if(!strncmp("N ", item, 2)) {
202 metric->N = PSDRV_GlyphName(value);
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);
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;
214 /* Ligatures go here... */
216 curpos = endpos + 1;
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;
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);
231 return TRUE;
235 /***********************************************************
237 * PSDRV_AFMParse
239 * Fills out an AFM structure and associated substructures (see psdrv.h)
240 * for a given AFM file. All memory is allocated from the process heap.
241 * Returns a ptr to the AFM structure or NULL on error.
243 * This is not complete (we don't handle kerning yet) and not efficient
246 static AFM *PSDRV_AFMParse(char const *file)
248 FILE *fp;
249 unsigned char buf[256];
250 unsigned char *value;
251 AFM *afm;
252 unsigned char *cp;
253 int afmfile = 0;
254 int c;
256 TRACE("parsing '%s'\n", file);
258 if((fp = fopen(file, "r")) == NULL) {
259 MESSAGE("Can't open AFM file '%s'. Please check wine.conf .\n", file);
260 return NULL;
263 afm = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(AFM));
264 if(!afm) {
265 fclose(fp);
266 return NULL;
269 cp = buf;
270 while ( ( c = fgetc ( fp ) ) != EOF ) {
271 *cp = c;
272 if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
273 if ( cp == buf )
274 continue;
275 *(cp+1)='\0';
277 else {
278 cp ++;
279 continue;
282 cp = buf + strlen(buf);
283 do {
284 *cp = '\0';
285 cp--;
286 } while(cp > buf && isspace(*cp));
288 cp = buf;
290 if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
291 break;
292 afmfile = 1;
294 value = strchr(buf, ' ');
295 if(value)
296 while(isspace(*value))
297 value++;
299 if(!strncmp("FontName", buf, 8)) {
300 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, value);
301 if (afm->FontName == NULL) {
302 fclose(fp);
303 FreeAFM(afm);
304 return NULL;
306 continue;
309 if(!strncmp("FullName", buf, 8)) {
310 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, value);
311 if (afm->FullName == NULL) {
312 fclose(fp);
313 FreeAFM(afm);
314 return NULL;
316 continue;
319 if(!strncmp("FamilyName", buf, 10)) {
320 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, value);
321 if (afm->FamilyName == NULL) {
322 fclose(fp);
323 FreeAFM(afm);
324 return NULL;
326 continue;
329 if(!strncmp("Weight", buf, 6)) {
330 if(!strncmp("Roman", value, 5) || !strncmp("Medium", value, 6)
331 || !strncmp("Book", value, 4) || !strncmp("Regular", value, 7)
332 || !strncmp("Normal", value, 6))
333 afm->Weight = FW_NORMAL;
334 else if(!strncmp("Demi", value, 4))
335 afm->Weight = FW_DEMIBOLD;
336 else if(!strncmp("Bold", value, 4))
337 afm->Weight = FW_BOLD;
338 else if(!strncmp("Light", value, 5))
339 afm->Weight = FW_LIGHT;
340 else if(!strncmp("Black", value, 5))
341 afm->Weight = FW_BLACK;
342 else {
343 WARN("%s specifies unknown Weight '%s'; treating as Roman\n",
344 file, value);
345 afm->Weight = FW_NORMAL;
347 continue;
350 if(!strncmp("ItalicAngle", buf, 11)) {
351 sscanf(value, "%f", &(afm->ItalicAngle));
352 continue;
355 if(!strncmp("IsFixedPitch", buf, 12)) {
356 if(!strncasecmp("false", value, 5))
357 afm->IsFixedPitch = FALSE;
358 else
359 afm->IsFixedPitch = TRUE;
360 continue;
363 if(!strncmp("FontBBox", buf, 8)) {
364 sscanf(value, "%f %f %f %f", &(afm->FontBBox.llx),
365 &(afm->FontBBox.lly), &(afm->FontBBox.urx),
366 &(afm->FontBBox.ury) );
367 continue;
370 if(!strncmp("UnderlinePosition", buf, 17)) {
371 sscanf(value, "%f", &(afm->UnderlinePosition) );
372 continue;
375 if(!strncmp("UnderlineThickness", buf, 18)) {
376 sscanf(value, "%f", &(afm->UnderlineThickness) );
377 continue;
380 if(!strncmp("CapHeight", buf, 9)) {
381 sscanf(value, "%f", &(afm->CapHeight) );
382 continue;
385 if(!strncmp("XHeight", buf, 7)) {
386 sscanf(value, "%f", &(afm->XHeight) );
387 continue;
390 if(!strncmp("Ascender", buf, 8)) {
391 sscanf(value, "%f", &(afm->Ascender) );
392 continue;
395 if(!strncmp("Descender", buf, 9)) {
396 sscanf(value, "%f", &(afm->Descender) );
397 continue;
400 if(!strncmp("StartCharMetrics", buf, 16)) {
401 sscanf(value, "%d", &(afm->NumofMetrics) );
402 if (PSDRV_AFMGetCharMetrics(afm, fp) == FALSE) {
403 fclose(fp);
404 FreeAFM(afm);
405 return NULL;
407 continue;
410 if(!strncmp("EncodingScheme", buf, 14)) {
411 afm->EncodingScheme = HEAP_strdupA(PSDRV_Heap, 0, value);
412 if (afm->EncodingScheme == NULL) {
413 fclose(fp);
414 FreeAFM(afm);
415 return NULL;
417 continue;
421 fclose(fp);
423 if (afmfile == 0) {
424 HeapFree ( PSDRV_Heap, 0, afm );
425 return NULL;
428 if(afm->FontName == NULL) {
429 WARN("%s contains no FontName.\n", file);
430 afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
431 if (afm->FontName == NULL) {
432 FreeAFM(afm);
433 return NULL;
437 if(afm->FullName == NULL)
438 afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
439 if(afm->FamilyName == NULL)
440 afm->FamilyName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
441 if (afm->FullName == NULL || afm->FamilyName == NULL) {
442 FreeAFM(afm);
443 return NULL;
446 if(afm->Ascender == 0.0)
447 afm->Ascender = afm->FontBBox.ury;
448 if(afm->Descender == 0.0)
449 afm->Descender = afm->FontBBox.lly;
450 if(afm->FullAscender == 0.0)
451 afm->FullAscender = afm->Ascender;
452 if(afm->Weight == 0)
453 afm->Weight = FW_NORMAL;
455 return afm;
458 /***********************************************************
460 * PSDRV_FreeAFMList
462 * Frees the family and afmlistentry structures in list head
464 void PSDRV_FreeAFMList( FONTFAMILY *head )
466 AFMLISTENTRY *afmle, *nexta;
467 FONTFAMILY *family, *nextf;
469 for(nextf = family = head; nextf; family = nextf) {
470 for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
471 nexta = afmle->next;
472 HeapFree( PSDRV_Heap, 0, afmle );
474 nextf = family->next;
475 HeapFree( PSDRV_Heap, 0, family );
477 return;
481 /***********************************************************
483 * PSDRV_FindAFMinList
484 * Returns ptr to an AFM if name (which is a PS font name) exists in list
485 * headed by head.
487 AFM *PSDRV_FindAFMinList(FONTFAMILY *head, char *name)
489 FONTFAMILY *family;
490 AFMLISTENTRY *afmle;
492 for(family = head; family; family = family->next) {
493 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
494 if(!strcmp(afmle->afm->FontName, name))
495 return afmle->afm;
498 return NULL;
501 /***********************************************************
503 * PSDRV_AddAFMtoList
505 * Adds an afm to the list whose head is pointed to by head. Creates new
506 * family node if necessary and always creates a new AFMLISTENTRY.
508 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, AFM *afm)
510 FONTFAMILY *family = *head;
511 FONTFAMILY **insert = head;
512 AFMLISTENTRY *tmpafmle, *newafmle;
514 newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
515 sizeof(*newafmle));
516 if (newafmle == NULL)
517 return FALSE;
519 newafmle->afm = afm;
521 while(family) {
522 if(!strcmp(family->FamilyName, afm->FamilyName))
523 break;
524 insert = &(family->next);
525 family = family->next;
528 if(!family) {
529 family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
530 sizeof(*family));
531 if (family == NULL) {
532 HeapFree(PSDRV_Heap, 0, newafmle);
533 return FALSE;
535 *insert = family;
536 family->FamilyName = HEAP_strdupA(PSDRV_Heap, 0,
537 afm->FamilyName);
538 if (family->FamilyName == NULL) {
539 HeapFree(PSDRV_Heap, 0, family);
540 HeapFree(PSDRV_Heap, 0, newafmle);
541 return FALSE;
543 family->afmlist = newafmle;
544 return TRUE;
546 else {
547 tmpafmle = family->afmlist;
548 while (tmpafmle) {
549 if (!strcmp(tmpafmle->afm->FontName, afm->FontName)) {
550 WARN("Ignoring duplicate FontName '%s'\n", afm->FontName);
551 HeapFree(PSDRV_Heap, 0, newafmle);
552 return TRUE; /* not a fatal error */
554 tmpafmle = tmpafmle->next;
558 tmpafmle = family->afmlist;
559 while(tmpafmle->next)
560 tmpafmle = tmpafmle->next;
562 tmpafmle->next = newafmle;
564 return TRUE;
567 /**********************************************************
569 * PSDRV_ReencodeCharWidths
571 * Re map the CharWidths field of the afm to correspond to an ANSI encoding
574 static void PSDRV_ReencodeCharWidths(AFM *afm)
576 int i, j;
577 AFMMETRICS *metric;
579 for(i = 0; i < 256; i++) {
580 if(isalnum(i))
581 continue;
582 if(PSDRV_ANSIVector[i] == NULL) {
583 afm->CharWidths[i] = 0.0;
584 continue;
586 for (j = 0, metric = afm->Metrics; j < afm->NumofMetrics; j++, metric++) {
587 if(metric->N && !strcmp(metric->N->sz, PSDRV_ANSIVector[i])) {
588 afm->CharWidths[i] = metric->WX;
589 break;
592 if(j == afm->NumofMetrics) {
593 WARN("Couldn't find glyph '%s' in font '%s'\n",
594 PSDRV_ANSIVector[i], afm->FontName);
595 afm->CharWidths[i] = 0.0;
598 return;
602 /***********************************************************
604 * PSDRV_DumpFontList
607 static void PSDRV_DumpFontList(void)
609 FONTFAMILY *family;
610 AFMLISTENTRY *afmle;
612 for(family = PSDRV_AFMFontList; family; family = family->next) {
613 TRACE("Family '%s'\n", family->FamilyName);
614 for(afmle = family->afmlist; afmle; afmle = afmle->next)
616 INT i;
618 TRACE("\tFontName '%s' (%i glyphs) - '%s' encoding:\n",
619 afmle->afm->FontName, afmle->afm->NumofMetrics,
620 afmle->afm->EncodingScheme);
622 for (i = 0; i < afmle->afm->NumofMetrics; ++i)
624 TRACE("\t\tU+%.4lX; C %i; N '%s'\n", afmle->afm->Metrics[i].UV,
625 afmle->afm->Metrics[i].C, afmle->afm->Metrics[i].N->sz);
629 return;
632 /*******************************************************************************
633 * SortFontMetrics
635 * Initializes the UV member of each glyph's AFMMETRICS and sorts each font's
636 * Metrics by Unicode Value. If the font has a standard encoding (i.e. it is
637 * using the Adobe Glyph List encoding vector), look up each glyph's Unicode
638 * Value based on it's glyph name. If the font has a font-specific encoding,
639 * map the default PostScript encodings into the Unicode private use area.
642 static int UnicodeGlyphByNameIndex(const UNICODEGLYPH *a, const UNICODEGLYPH *b)
644 return a->name->index - b->name->index;
647 static int AFMMetricsByUV(const AFMMETRICS *a, const AFMMETRICS *b)
649 return a->UV - b->UV;
652 static BOOL SortFontMetrics()
654 UNICODEGLYPH *aglCopy = NULL;
655 FONTFAMILY *family = PSDRV_AFMFontList;
657 while (family != NULL)
659 AFMLISTENTRY *afmle = family->afmlist;
661 while (afmle != NULL)
663 AFM *afm = afmle->afm; /* should always be valid */
664 INT i;
666 if (strcmp(afm->EncodingScheme, "FontSpecific") != 0)
668 if (aglCopy == NULL) /* do this once, if necessary */
670 aglCopy = HeapAlloc(PSDRV_Heap, 0,
671 PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
672 if (aglCopy == NULL)
673 return FALSE;
675 memcpy(aglCopy, PSDRV_AdobeGlyphList.glyphs,
676 PSDRV_AdobeGlyphList.size * sizeof(UNICODEGLYPH));
678 qsort(aglCopy, PSDRV_AdobeGlyphList.size,
679 sizeof(UNICODEGLYPH),
680 (compar_callback_fn)UnicodeGlyphByNameIndex);
683 for (i = 0; i < afm->NumofMetrics; ++i)
685 UNICODEGLYPH ug, *pug;
687 ug.name = afm->Metrics[i].N;
688 ug.UV = -1;
690 pug = bsearch(&ug, aglCopy, PSDRV_AdobeGlyphList.size,
691 sizeof(UNICODEGLYPH),
692 (compar_callback_fn)UnicodeGlyphByNameIndex);
693 if (pug == NULL)
695 WARN("Glyph '%s' in font '%s' does not have a UV\n",
696 ug.name->sz, afm->FullName);
697 afm->Metrics[i].UV = -1;
699 else
701 afm->Metrics[i].UV = pug->UV;
705 else /* FontSpecific encoding */
707 for (i = 0; i < afm->NumofMetrics; ++i)
708 afm->Metrics[i].UV = afm->Metrics[i].C;
711 qsort(afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
712 (compar_callback_fn)AFMMetricsByUV);
714 for (i = 0; i < afm->NumofMetrics; ++i)
715 if (afm->Metrics[i].UV >= 0)
716 break;
718 afm->NumofMetrics -= i; /* Ignore unencoded glyphs */
719 afm->Metrics += i;
721 afmle = afmle->next;
724 family = family->next;
727 if (aglCopy != NULL)
728 HeapFree(PSDRV_Heap, 0, aglCopy);
730 return TRUE;
733 /*******************************************************************************
734 * PSDRV_CalcAvgCharWidth
736 * Calculate WinMetrics.sAvgCharWidth for a Type 1 font. Can also be used on
737 * TrueType fonts, if font designer set OS/2:xAvgCharWidth to zero.
739 * Tries to use formula in TrueType specification; falls back to simple mean
740 * if any lowercase latin letter (or space) is not present.
742 inline static SHORT MeanCharWidth(const AFM *afm)
744 float w = 0.0;
745 int i;
747 for (i = 0; i < afm->NumofMetrics; ++i)
748 w += afm->Metrics[i].WX;
750 w /= afm->NumofMetrics;
752 return (SHORT)(w + 0.5);
755 static const struct { LONG UV; int weight; } UVweight[27] =
757 { 0x0061, 64 }, { 0x0062, 14 }, { 0x0063, 27 }, { 0x0064, 35 },
758 { 0x0065, 100 }, { 0x0066, 20 }, { 0x0067, 14 }, { 0x0068, 42 },
759 { 0x0069, 63 }, { 0x006a, 3 }, { 0x006b, 6 }, { 0x006c, 35 },
760 { 0x006d, 20 }, { 0x006e, 56 }, { 0x006f, 56 }, { 0x0070, 17 },
761 { 0x0071, 4 }, { 0x0072, 49 }, { 0x0073, 56 }, { 0x0074, 71 },
762 { 0x0075, 31 }, { 0x0076, 10 }, { 0x0077, 18 }, { 0x0078, 3 },
763 { 0x0079, 18 }, { 0x007a, 2 }, { 0x0020, 166 }
766 SHORT PSDRV_CalcAvgCharWidth(const AFM *afm)
768 float w = 0.0;
769 int i;
771 for (i = 0; i < 27; ++i)
773 const AFMMETRICS *afmm;
775 afmm = PSDRV_UVMetrics(UVweight[i].UV, afm);
776 if (afmm->UV != UVweight[i].UV) /* UVMetrics returns first glyph */
777 return MeanCharWidth(afm); /* in font if UV is missing */
779 w += afmm->WX * (float)(UVweight[i].weight);
782 w /= 1000.0;
784 return (SHORT)(w + 0.5);
787 /*******************************************************************************
788 * CalcWindowsMetrics
790 * Calculates several Windows-specific font metrics for each font. Relies on
791 * the fact that AFMs are allocated with HEAP_ZERO_MEMORY to distinguish
792 * TrueType fonts (when implemented), which already have these filled in.
795 static VOID CalcWindowsMetrics()
797 FONTFAMILY *family = PSDRV_AFMFontList;
799 while (family != NULL)
801 AFMLISTENTRY *afmle = family->afmlist;
803 while (afmle != NULL)
805 WINMETRICS wm;
806 AFM *afm = afmle->afm; /* should always be valid */
807 INT i;
809 if (afm->WinMetrics.usUnitsPerEm != 0)
810 continue; /* TrueType font */
812 wm.usUnitsPerEm = 1000; /* for PostScript fonts */
813 wm.sTypoAscender = (SHORT)(afm->Ascender + 0.5);
814 wm.sTypoDescender = (SHORT)(afm->Descender - 0.5);
816 wm.sTypoLineGap = 1200 - (wm.sTypoAscender - wm.sTypoDescender);
817 if (wm.sTypoLineGap < 0)
818 wm.sTypoLineGap = 0;
820 wm.usWinAscent = 0;
821 wm.usWinDescent = 0;
823 for (i = 0; i < afm->NumofMetrics; ++i)
825 if (IsWinANSI(afm->Metrics[i].UV) == FALSE)
826 continue;
828 if (afm->Metrics[i].B.ury > 0)
830 USHORT ascent = (USHORT)(afm->Metrics[i].B.ury + 0.5);
832 if (ascent > wm.usWinAscent)
833 wm.usWinAscent = ascent;
836 if (afm->Metrics[i].B.lly < 0)
838 USHORT descent = (USHORT)(-(afm->Metrics[i].B.lly) + 0.5);
840 if (descent > wm.usWinDescent)
841 wm.usWinDescent = descent;
845 if (wm.usWinAscent == 0 && afm->FontBBox.ury > 0)
846 wm.usWinAscent = (USHORT)(afm->FontBBox.ury + 0.5);
848 if (wm.usWinDescent == 0 && afm->FontBBox.lly < 0)
849 wm.usWinDescent = (USHORT)(-(afm->FontBBox.lly) + 0.5);
851 wm.sAscender = wm.usWinAscent;
852 wm.sDescender = -(wm.usWinDescent);
854 wm.sLineGap = 1150 - (wm.sAscender - wm.sDescender);
855 if (wm.sLineGap < 0)
856 wm.sLineGap = 0;
858 wm.sAvgCharWidth = PSDRV_CalcAvgCharWidth(afm);
860 TRACE("Windows metrics for '%s':\n", afm->FullName);
861 TRACE("\tsAscender = %i\n", wm.sAscender);
862 TRACE("\tsDescender = %i\n", wm.sDescender);
863 TRACE("\tsLineGap = %i\n", wm.sLineGap);
864 TRACE("\tusUnitsPerEm = %u\n", wm.usUnitsPerEm);
865 TRACE("\tsTypoAscender = %i\n", wm.sTypoAscender);
866 TRACE("\tsTypoDescender = %i\n", wm.sTypoDescender);
867 TRACE("\tsTypoLineGap = %i\n", wm.sTypoLineGap);
868 TRACE("\tusWinAscent = %u\n", wm.usWinAscent);
869 TRACE("\tusWinDescent = %u\n", wm.usWinDescent);
870 TRACE("\tsAvgCharWidth = %i\n", wm.sAvgCharWidth);
872 afm->WinMetrics = wm;
874 /* See afm2c.c and mkagl.c for an explanation of this */
875 /* PSDRV_AFM2C(afm); */
877 afmle = afmle->next;
880 family = family ->next;
885 /*******************************************************************************
886 * AddBuiltinAFMs
890 static BOOL AddBuiltinAFMs()
892 int i = 0;
894 while (PSDRV_BuiltinAFMs[i] != NULL)
896 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, PSDRV_BuiltinAFMs[i])
897 == FALSE)
898 return FALSE;
899 ++i;
902 return TRUE;
906 /***********************************************************
908 * PSDRV_GetFontMetrics
910 * Parses all afm files listed in [afmfiles] and [afmdirs] of wine.conf
912 * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
913 * about freeing all the memory that's been allocated.
916 static BOOL PSDRV_ReadAFMDir(const char* afmdir) {
917 DIR *dir;
918 AFM *afm;
920 dir = opendir(afmdir);
921 if (dir) {
922 struct dirent *dent;
923 while ((dent=readdir(dir))) {
924 if (strstr(dent->d_name,".afm")) {
925 char *afmfn;
927 afmfn=(char*)HeapAlloc(PSDRV_Heap,0,
928 strlen(afmdir)+strlen(dent->d_name)+2);
929 if (afmfn == NULL) {
930 closedir(dir);
931 return FALSE;
933 strcpy(afmfn,afmdir);
934 strcat(afmfn,"/");
935 strcat(afmfn,dent->d_name);
936 TRACE("loading AFM %s\n",afmfn);
937 afm = PSDRV_AFMParse(afmfn);
938 if (afm) {
939 if(afm->EncodingScheme &&
940 !strcmp(afm->EncodingScheme,"AdobeStandardEncoding")) {
941 PSDRV_ReencodeCharWidths(afm);
943 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
944 closedir(dir);
945 FreeAFM(afm);
946 return FALSE;
949 else {
950 WARN("Error parsing %s\n", afmfn);
952 HeapFree(PSDRV_Heap,0,afmfn);
955 closedir(dir);
957 else {
958 WARN("Error opening %s\n", afmdir);
961 return TRUE;
964 BOOL PSDRV_GetFontMetrics(void)
966 int idx;
967 char key[256];
968 char value[256];
969 HKEY hkey;
970 DWORD type, key_len, value_len;
972 if (PSDRV_GlyphListInit() != 0)
973 return FALSE;
975 if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\afmfiles",
976 0, KEY_READ, &hkey))
977 goto no_afmfiles;
979 idx = 0;
980 key_len = sizeof(key);
981 value_len = sizeof(value);
982 while(!RegEnumValueA(hkey, idx++, key, &key_len, NULL, &type, value, &value_len))
984 AFM* afm = PSDRV_AFMParse(value);
986 if (afm) {
987 if(afm->EncodingScheme &&
988 !strcmp(afm->EncodingScheme, "AdobeStandardEncoding")) {
989 PSDRV_ReencodeCharWidths(afm);
991 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, afm) == FALSE) {
992 RegCloseKey(hkey);
993 return FALSE;
996 else {
997 WARN("Error parsing %s\n", value);
1000 /* initialize lengths for new iteration */
1001 key_len = sizeof(key);
1002 value_len = sizeof(value);
1004 RegCloseKey(hkey);
1006 no_afmfiles:
1008 if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\afmdirs",
1009 0, KEY_READ, &hkey))
1010 goto no_afmdirs;
1012 idx = 0;
1013 key_len = sizeof(key);
1014 value_len = sizeof(value);
1015 while(!RegEnumValueA(hkey, idx++, key, &key_len, NULL, &type, value, &value_len))
1017 if (PSDRV_ReadAFMDir (value) == FALSE)
1019 RegCloseKey(hkey);
1020 return FALSE;
1023 /* initialize lengths for new iteration */
1024 key_len = sizeof(key);
1025 value_len = sizeof(value);
1027 RegCloseKey(hkey);
1029 no_afmdirs:
1031 PSDRV_IndexGlyphList(); /* So SortFontMetrics will work */
1032 if (SortFontMetrics() == FALSE)
1033 return FALSE;
1034 CalcWindowsMetrics();
1035 if (AddBuiltinAFMs() == FALSE)
1036 return FALSE;
1038 #ifdef HAVE_FREETYPE
1039 if (PSDRV_GetTrueTypeMetrics() == FALSE)
1040 return FALSE;
1041 PSDRV_IndexGlyphList();
1042 #endif
1044 PSDRV_DumpFontList();
1045 return TRUE;