Added Type 1 and Type 42 font downloading.
[wine] / dlls / wineps / download.c
1 /*
2  *      PostScript driver downloadable font functions
3  *
4  *      Copyright 2002  Huw D M Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stdio.h>
24 #include "winspool.h"
25 #include "gdi.h"
26 #include "psdrv.h"
27 #include "wine/debug.h"
28 #include "winerror.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
31
32
33 /****************************************************************************
34  *  get_download_name
35  */
36 static void get_download_name(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA
37                               potm, char **str)
38 {
39     int len;
40     char *p;
41     len = strlen((char*)potm + (ptrdiff_t)potm->otmpFullName) + 1;
42     *str = HeapAlloc(GetProcessHeap(),0,len);
43     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
44  
45     p = *str;
46     while((p = strchr(p, ' ')))
47         *p = '_';
48
49     return;
50 }
51
52 /****************************************************************************
53  *  is_font_downloaded
54  */
55 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
56 {
57     DOWNLOAD *pdl;
58     
59     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
60         if(!strcmp(pdl->ps_name, ps_name))
61             break;
62     return pdl;
63 }
64
65 /****************************************************************************
66  *  PSDRV_SelectDownloadFont
67  *
68  *  Set up physDev->font for a downloadable font
69  *
70  */
71 BOOL PSDRV_SelectDownloadFont(PSDRV_PDEVICE *physDev)
72 {
73     char *ps_name;
74     LPOUTLINETEXTMETRICA potm;
75     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
76
77     potm = HeapAlloc(GetProcessHeap(), 0, len);
78     GetOutlineTextMetricsA(physDev->hdc, len, potm);
79     get_download_name(physDev, potm, &ps_name);
80
81     physDev->font.fontloc = Download;
82     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
83
84     physDev->font.size = INTERNAL_YWSTODS(physDev->dc, /* ppem */
85                                           potm->otmTextMetrics.tmAscent +
86                                           potm->otmTextMetrics.tmDescent -
87                                           potm->otmTextMetrics.tmInternalLeading);
88     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
89     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
90     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
91     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
92
93     HeapFree(GetProcessHeap(), 0, ps_name);
94     HeapFree(GetProcessHeap(), 0, potm);
95     return TRUE;
96 }
97
98 /****************************************************************************
99  *  PSDRV_WriteSetDownloadFont
100  *
101  *  Write setfont for download font.
102  *
103  */
104 BOOL PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE *physDev)
105 {
106     char *ps_name;
107     LPOUTLINETEXTMETRICA potm;
108     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
109     DOWNLOAD *pdl;
110
111     assert(physDev->font.fontloc == Download);
112
113     potm = HeapAlloc(GetProcessHeap(), 0, len);
114     GetOutlineTextMetricsA(physDev->hdc, len, potm);
115
116     get_download_name(physDev, potm, &ps_name);
117
118     if(physDev->font.fontinfo.Download == NULL) {
119         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
120         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
121         strcpy(pdl->ps_name, ps_name);
122         pdl->next = NULL;
123
124         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
125             pdl->typeinfo.Type42 = T42_download_header(physDev, potm,
126                                                        ps_name);
127             pdl->type = Type42;
128         } else {
129             pdl->typeinfo.Type1 = T1_download_header(physDev, potm, ps_name);
130             pdl->type = Type1;
131         }
132         if(pdl) {
133             pdl->next = physDev->downloaded_fonts;
134             physDev->downloaded_fonts = pdl;
135         }
136         physDev->font.fontinfo.Download = pdl;
137     }
138
139
140     PSDRV_WriteSetFont(physDev, ps_name, physDev->font.size,
141                        physDev->font.escapement);
142
143     HeapFree(GetProcessHeap(), 0, ps_name);
144     HeapFree(GetProcessHeap(), 0, potm);
145     return TRUE;
146 }
147
148 void get_glyph_name(HDC hdc, WORD index, char *name)
149 {
150   /* FIXME */
151     sprintf(name, "g%04x", index);
152     return;
153 }
154
155 /****************************************************************************
156  *  PSDRV_WriteDownloadGlyphShow
157  *
158  *  Download and write out a number of glyphs
159  *
160  */
161 BOOL PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE *physDev, WORD *glyphs,
162                                   UINT count)
163 {
164     UINT i;
165     char g_name[MAX_G_NAME + 1];
166     assert(physDev->font.fontloc == Download);
167
168     switch(physDev->font.fontinfo.Download->type) {
169     case Type42:
170     for(i = 0; i < count; i++) {
171         get_glyph_name(physDev->hdc, glyphs[i], g_name);
172         T42_download_glyph(physDev, physDev->font.fontinfo.Download,
173                            glyphs[i], g_name);
174         PSDRV_WriteGlyphShow(physDev, g_name);
175     }
176     break;
177
178     case Type1:
179     for(i = 0; i < count; i++) {
180         get_glyph_name(physDev->hdc, glyphs[i], g_name);
181         T1_download_glyph(physDev, physDev->font.fontinfo.Download,
182                           glyphs[i], g_name);
183         PSDRV_WriteGlyphShow(physDev, g_name);
184     }
185     break;
186
187     default:
188         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
189         assert(0);
190     }
191     return TRUE;
192 }
193
194 /****************************************************************************
195  *  PSDRV_EmptyDownloadList
196  *
197  *  Clear the list of downloaded fonts
198  *
199  */
200 BOOL PSDRV_EmptyDownloadList(PSDRV_PDEVICE *physDev)
201 {
202     DOWNLOAD *pdl, *old;
203     if(physDev->font.fontloc == Download) {
204         physDev->font.set = FALSE;
205         physDev->font.fontinfo.Download = NULL;
206     }
207
208     pdl = physDev->downloaded_fonts;
209     physDev->downloaded_fonts = NULL;
210     while(pdl) {
211         switch(pdl->type) {
212         case Type42:
213             T42_free(pdl->typeinfo.Type42);
214             break;
215
216         case Type1:
217             T1_free(pdl->typeinfo.Type1);
218             break;
219
220         default:
221             ERR("Type = %d\n", pdl->type);
222             assert(0);
223         }
224
225         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
226         old = pdl;
227         pdl = pdl->next;
228         HeapFree(GetProcessHeap(), 0, old);
229     }
230     return TRUE;
231 }