winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / wineps.drv / afm.c
blobc5e0cd3f36df278321a085584c20fd889b69d8e3
1 /*
2 * Font metric functions common to Type 1 (AFM) and TrueType font files.
3 * Functions specific to Type 1 and TrueType fonts are in type1afm.c and
4 * truetype.c respectively.
6 * Copyright 1998 Huw D M Davies
7 * Copyright 2001 Ian Pilcher
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <string.h>
26 #include "psdrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
31 /* ptr to fonts for which we have afm files */
32 DECLSPEC_HIDDEN FONTFAMILY *PSDRV_AFMFontList = NULL;
35 /***********************************************************
37 * PSDRV_FreeAFMList
39 * Frees the family and afmlistentry structures in list head
41 void PSDRV_FreeAFMList( FONTFAMILY *head )
43 AFMLISTENTRY *afmle, *nexta;
44 FONTFAMILY *family, *nextf;
46 for(nextf = family = head; nextf; family = nextf) {
47 for(nexta = afmle = family->afmlist; nexta; afmle = nexta) {
48 nexta = afmle->next;
49 HeapFree( PSDRV_Heap, 0, afmle );
51 nextf = family->next;
52 HeapFree( PSDRV_Heap, 0, family );
54 return;
58 /***********************************************************
60 * PSDRV_FindAFMinList
61 * Returns ptr to an AFM if name (which is a PS font name) exists in list
62 * headed by head.
64 const AFM *PSDRV_FindAFMinList(FONTFAMILY *head, LPCSTR name)
66 FONTFAMILY *family;
67 AFMLISTENTRY *afmle;
69 for(family = head; family; family = family->next) {
70 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
71 if(!strcmp(afmle->afm->FontName, name))
72 return afmle->afm;
75 return NULL;
78 /***********************************************************
80 * PSDRV_AddAFMtoList
82 * Adds an afm to the list whose head is pointed to by head. Creates new
83 * family node if necessary and always creates a new AFMLISTENTRY.
85 * Returns FALSE for memory allocation error; returns TRUE, but sets *p_added
86 * to FALSE, for duplicate.
88 BOOL PSDRV_AddAFMtoList(FONTFAMILY **head, const AFM *afm, BOOL *p_added)
90 FONTFAMILY *family = *head;
91 FONTFAMILY **insert = head;
92 AFMLISTENTRY *tmpafmle, *newafmle;
94 newafmle = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
95 sizeof(*newafmle));
96 if (newafmle == NULL)
97 return FALSE;
99 newafmle->afm = afm;
101 while(family) {
102 if(!strcmp(family->FamilyName, afm->FamilyName))
103 break;
104 insert = &(family->next);
105 family = family->next;
108 if(!family) {
109 family = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY,
110 sizeof(*family));
111 if (family == NULL) {
112 HeapFree(PSDRV_Heap, 0, newafmle);
113 return FALSE;
115 *insert = family;
116 if (!(family->FamilyName = HeapAlloc(PSDRV_Heap, 0, strlen(afm->FamilyName)+1 ))) {
117 HeapFree(PSDRV_Heap, 0, family);
118 HeapFree(PSDRV_Heap, 0, newafmle);
119 return FALSE;
121 strcpy( family->FamilyName, afm->FamilyName );
122 family->afmlist = newafmle;
123 *p_added = TRUE;
124 return TRUE;
126 else {
127 tmpafmle = family->afmlist;
128 while (tmpafmle) {
129 if (!strcmp(tmpafmle->afm->FontName, afm->FontName)) {
130 WARN("Ignoring duplicate FontName '%s'\n", afm->FontName);
131 HeapFree(PSDRV_Heap, 0, newafmle);
132 *p_added = FALSE;
133 return TRUE; /* not a fatal error */
135 tmpafmle = tmpafmle->next;
139 tmpafmle = family->afmlist;
140 while(tmpafmle->next)
141 tmpafmle = tmpafmle->next;
143 tmpafmle->next = newafmle;
145 *p_added = TRUE;
146 return TRUE;
150 /***********************************************************
152 * PSDRV_DumpFontList
155 static void PSDRV_DumpFontList(void)
157 FONTFAMILY *family;
158 AFMLISTENTRY *afmle;
160 for(family = PSDRV_AFMFontList; family; family = family->next) {
161 TRACE("Family '%s'\n", family->FamilyName);
162 for(afmle = family->afmlist; afmle; afmle = afmle->next)
164 #if 0
165 INT i;
166 #endif
168 TRACE("\tFontName '%s' (%i glyphs) - '%s' encoding:\n",
169 afmle->afm->FontName, afmle->afm->NumofMetrics,
170 afmle->afm->EncodingScheme);
172 /* Uncomment to regenerate font data; see afm2c.c */
174 /* PSDRV_AFM2C(afmle->afm); */
176 #if 0
177 for (i = 0; i < afmle->afm->NumofMetrics; ++i)
179 TRACE("\t\tU+%.4lX; C %i; N '%s'\n", afmle->afm->Metrics[i].UV,
180 afmle->afm->Metrics[i].C, afmle->afm->Metrics[i].N->sz);
182 #endif
185 return;
189 /*******************************************************************************
190 * PSDRV_CalcAvgCharWidth
192 * Calculate WinMetrics.sAvgCharWidth for a Type 1 font. Can also be used on
193 * TrueType fonts, if font designer set OS/2:xAvgCharWidth to zero.
195 * Tries to use formula in TrueType specification; falls back to simple mean
196 * if any lowercase latin letter (or space) is not present.
198 static inline SHORT MeanCharWidth(const AFM *afm)
200 float w = 0.0;
201 int i;
203 for (i = 0; i < afm->NumofMetrics; ++i)
204 w += afm->Metrics[i].WX;
206 w /= afm->NumofMetrics;
208 return (SHORT)(w + 0.5);
211 static const struct { LONG UV; int weight; } UVweight[27] =
213 { 0x0061, 64 }, { 0x0062, 14 }, { 0x0063, 27 }, { 0x0064, 35 },
214 { 0x0065, 100 }, { 0x0066, 20 }, { 0x0067, 14 }, { 0x0068, 42 },
215 { 0x0069, 63 }, { 0x006a, 3 }, { 0x006b, 6 }, { 0x006c, 35 },
216 { 0x006d, 20 }, { 0x006e, 56 }, { 0x006f, 56 }, { 0x0070, 17 },
217 { 0x0071, 4 }, { 0x0072, 49 }, { 0x0073, 56 }, { 0x0074, 71 },
218 { 0x0075, 31 }, { 0x0076, 10 }, { 0x0077, 18 }, { 0x0078, 3 },
219 { 0x0079, 18 }, { 0x007a, 2 }, { 0x0020, 166 }
222 SHORT PSDRV_CalcAvgCharWidth(const AFM *afm)
224 float w = 0.0;
225 int i;
227 for (i = 0; i < 27; ++i)
229 const AFMMETRICS *afmm;
231 afmm = PSDRV_UVMetrics(UVweight[i].UV, afm);
232 if (afmm->UV != UVweight[i].UV) /* UVMetrics returns first glyph */
233 return MeanCharWidth(afm); /* in font if UV is missing */
235 w += afmm->WX * (float)(UVweight[i].weight);
238 w /= 1000.0;
240 return (SHORT)(w + 0.5);
244 /*******************************************************************************
245 * AddBuiltinAFMs
249 static BOOL AddBuiltinAFMs(void)
251 const AFM *const *afm = PSDRV_BuiltinAFMs;
253 while (*afm != NULL)
255 BOOL added;
257 if (PSDRV_AddAFMtoList(&PSDRV_AFMFontList, *afm, &added) == FALSE)
258 return FALSE;
260 if (added == FALSE)
261 TRACE("Ignoring built-in font %s\n", (*afm)->FontName);
263 ++afm;
266 return TRUE;
270 /***********************************************************
272 * PSDRV_GetFontMetrics
274 * Parses all afm files listed in the
275 * HKEY_CURRENT_USER\\Software\\Wine\\Fonts registry key.
276 * Adds built-in data last, so it can be overridden by
277 * user-supplied AFM or TTF files.
279 * If this function fails, PSDRV_Init will destroy PSDRV_Heap, so don't worry
280 * about freeing all the memory that's been allocated.
283 BOOL PSDRV_GetFontMetrics(void)
285 if (PSDRV_GlyphListInit() != 0)
286 return FALSE;
288 if (PSDRV_GetType1Metrics() == FALSE)
289 return FALSE;
291 if (AddBuiltinAFMs() == FALSE)
292 return FALSE;
294 PSDRV_IndexGlyphList(); /* Enable fast searching of glyph names */
296 PSDRV_DumpFontList();
298 return TRUE;