2 * PostScript driver Type42 font functions
4 * Copyright 2002 Huw D M Davies for CodeWeavers
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.
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.
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
26 #include "wine/debug.h"
29 #include "wine/port.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
34 #define GET_BE_WORD(ptr) MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
35 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
36 GET_BE_WORD(&((WORD *)(ptr))[0]) ))
38 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
39 ( ( (DWORD)_x4 << 24 ) | \
40 ( (DWORD)_x3 << 16 ) | \
41 ( (DWORD)_x2 << 8 ) | \
51 const OTTable tables_templ[] = {
52 { MS_MAKE_TAG('c','v','t',' '), 0, 0, NULL, TRUE },
53 { MS_MAKE_TAG('f','p','g','m'), 0, 0, NULL, TRUE },
54 { MS_MAKE_TAG('g','d','i','r'), 0, 0, NULL, TRUE },
55 { MS_MAKE_TAG('g','l','y','f'), 0, 0, NULL, FALSE },
56 { MS_MAKE_TAG('h','e','a','d'), 0, 0, NULL, TRUE },
57 { MS_MAKE_TAG('h','h','e','a'), 0, 0, NULL, TRUE },
58 { MS_MAKE_TAG('h','m','t','x'), 0, 0, NULL, FALSE },
59 { MS_MAKE_TAG('l','o','c','a'), 0, 0, NULL, FALSE },
60 { MS_MAKE_TAG('m','a','x','p'), 0, 0, NULL, TRUE },
61 { MS_MAKE_TAG('p','r','e','p'), 0, 0, NULL, TRUE },
66 OTTable tables[sizeof(tables_templ)/sizeof(tables_templ[0])];
67 int glyf_tab, loca_tab, head_tab; /* indices of glyf, loca and head tables */
69 DWORD glyph_sent_size;
74 #define GLYPH_SENT_INC 128
76 #define FLIP_ORDER(x) \
77 ( ( ((x) & 0xff) << 24) | \
78 ( ((x) & 0xff00) << 8) | \
79 ( ((x) & 0xff0000) >> 8) | \
80 ( ((x) & 0xff000000) >> 24) )
83 /* Some flags for composite glyphs. See glyf table in OT spec */
84 #define ARG_1_AND_2_ARE_WORDS (1L << 0)
85 #define WE_HAVE_A_SCALE (1L << 3)
86 #define MORE_COMPONENTS (1L << 5)
87 #define WE_HAVE_AN_X_AND_Y_SCALE (1L << 6)
88 #define WE_HAVE_A_TWO_BY_TWO (1L << 7)
90 static BOOL LoadTable(HDC hdc, OTTable *table)
94 if(table->MS_tag == MS_MAKE_TAG('g','d','i','r')) return TRUE;
95 table->len = GetFontData(hdc, table->MS_tag, 0, NULL, 0);
96 table->data = HeapAlloc(GetProcessHeap(), 0, (table->len + 3) & ~3 );
97 memset(table->data + ((table->len - 1) & ~3), 0, sizeof(DWORD));
98 GetFontData(hdc, table->MS_tag, 0, table->data, table->len);
100 for(i = 0; i < (table->len + 3) / 4; i++)
101 table->check += FLIP_ORDER(*((DWORD*)(table->data) + i));
106 TYPE42 *T42_download_header(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA potm,
109 DWORD i, j, tablepos;
110 WORD num_of_tables = sizeof(tables_templ) / sizeof(tables_templ[0]) - 1;
111 WORD num_of_write_tables = 0;
114 char start[] = /* name, fontbbox */
116 " /FontName /%s def\n"
117 " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for\n"
119 " /PaintType 0 def\n"
120 " /FontMatrix [1 0 0 1 0 0] def\n"
121 " /FontBBox [%f %f %f %f] def\n"
122 " /FontType 42 def\n"
123 " /CharStrings 256 dict begin\n"
125 " currentdict end def\n"
126 " /GlyphDirectory 256 dict def\n"
127 " /Metrics 256 dict def\n"
129 char TT_offset_table[] = "<00010000%04x%04x%04x%04x\n";
130 char TT_table_dir_entry[] = "%08lx%08lx%08lx%08lx\n";
131 char end[] = "] def\n"
132 "currentdict end dup /FontName get exch definefont pop\n";
135 t42 = HeapAlloc(GetProcessHeap(), 0, sizeof(*t42));
136 memcpy(t42->tables, tables_templ, sizeof(tables_templ));
137 t42->loca_tab = t42->glyf_tab = t42->head_tab = t42->hmtx_tab = -1;
138 t42->glyph_sent_size = GLYPH_SENT_INC;
139 t42->glyph_sent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
140 t42->glyph_sent_size *
141 sizeof(*(t42->glyph_sent)));
142 t42->emsize = potm->otmEMSquare;
144 for(i = 0; i < num_of_tables; i++) {
145 LoadTable(physDev->hdc, t42->tables + i);
146 if(t42->tables[i].write) num_of_write_tables++;
147 if(t42->tables[i].MS_tag == MS_MAKE_TAG('l','o','c','a'))
149 else if(t42->tables[i].MS_tag == MS_MAKE_TAG('g','l','y','f'))
151 else if(t42->tables[i].MS_tag == MS_MAKE_TAG('h','e','a','d'))
153 else if(t42->tables[i].MS_tag == MS_MAKE_TAG('h','m','t','x'))
157 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(start) + strlen(ps_name) +
160 sprintf(buf, start, ps_name,
161 (float)potm->otmrcFontBox.left / potm->otmEMSquare,
162 (float)potm->otmrcFontBox.bottom / potm->otmEMSquare,
163 (float)potm->otmrcFontBox.right / potm->otmEMSquare,
164 (float)potm->otmrcFontBox.top / potm->otmEMSquare);
166 PSDRV_WriteSpool(physDev, buf, strlen(buf));
168 sprintf(buf, TT_offset_table, num_of_write_tables,
169 num_of_write_tables, num_of_write_tables, num_of_write_tables);
171 PSDRV_WriteSpool(physDev, buf, strlen(buf));
173 tablepos = 12 + num_of_write_tables * 16;
174 for(i = 0; i < num_of_tables; i++) {
175 if(!t42->tables[i].write) continue;
176 sprintf(buf, TT_table_dir_entry, FLIP_ORDER(t42->tables[i].MS_tag),
177 t42->tables[i].check, t42->tables[i].len ? tablepos : 0,
179 PSDRV_WriteSpool(physDev, buf, strlen(buf));
180 tablepos += ((t42->tables[i].len + 3) & ~3);
182 PSDRV_WriteSpool(physDev, ">\n", 2);
184 for(i = 0; i < num_of_tables; i++) {
185 if(t42->tables[i].len == 0 || !t42->tables[i].write) continue;
186 PSDRV_WriteSpool(physDev, "<", 1);
187 for(j = 0; j < ((t42->tables[i].len + 3) & ~3); j++) {
188 sprintf(buf, "%02x", t42->tables[i].data[j]);
189 PSDRV_WriteSpool(physDev, buf, strlen(buf));
190 if(j % 16 == 15) PSDRV_WriteSpool(physDev, "\n", 1);
192 PSDRV_WriteSpool(physDev, ">\n", 2);
195 PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
196 HeapFree(GetProcessHeap(), 0, buf);
203 BOOL T42_download_glyph(PSDRV_PDEVICE *physDev, DOWNLOAD *pdl, DWORD index,
213 "/%s findfont exch 1 index /GlyphDirectory get\n"
217 "dup /CharStrings get\n"
227 TRACE("%ld %s\n", index, glyph_name);
228 assert(pdl->type == Type42);
229 t42 = pdl->typeinfo.Type42;
231 if(index < t42->glyph_sent_size) {
232 if(t42->glyph_sent[index])
235 t42->glyph_sent_size = (index / GLYPH_SENT_INC + 1) * GLYPH_SENT_INC;
236 t42->glyph_sent = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
238 t42->glyph_sent_size * sizeof(*(t42->glyph_sent)));
241 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def) +
242 strlen(pdl->ps_name) + 100);
244 loca_format = GET_BE_WORD(t42->tables[t42->head_tab].data + 50);
245 TRACE("loca_format = %d\n", loca_format);
246 switch(loca_format) {
248 start = GET_BE_WORD(((WORD*)t42->tables[t42->loca_tab].data) + index);
250 end = GET_BE_WORD(((WORD*)t42->tables[t42->loca_tab].data) + index + 1);
254 start = GET_BE_DWORD(((DWORD*)t42->tables[t42->loca_tab].data) + index);
255 end = GET_BE_DWORD(((DWORD*)t42->tables[t42->loca_tab].data) + index + 1);
258 ERR("Unknown loca_format %d\n", loca_format);
261 TRACE("start = %lx end = %lx\n", start, end);
263 awidth = GET_BE_WORD(t42->tables[t42->hmtx_tab].data + index * 4);
264 lsb = GET_BE_WORD(t42->tables[t42->hmtx_tab].data + index * 4 + 2);
266 if(GET_BE_WORD(t42->tables[t42->glyf_tab].data + start) == 0xffff) {
267 /* Composite glyph */
268 char *sg_start = t42->tables[t42->glyf_tab].data + start + 10;
269 DWORD sg_flags, sg_index;
270 char sg_name[MAX_G_NAME + 1];
273 sg_flags = GET_BE_WORD(sg_start);
274 sg_index = GET_BE_WORD(sg_start + 2);
276 TRACE("Sending subglyph %04lx for glyph %04lx\n", sg_index, index);
277 get_glyph_name(physDev->hdc, sg_index, sg_name);
278 T42_download_glyph(physDev, pdl, sg_index, sg_name);
280 if(sg_flags & ARG_1_AND_2_ARE_WORDS)
284 if(sg_flags & WE_HAVE_A_SCALE)
286 else if(sg_flags & WE_HAVE_AN_X_AND_Y_SCALE)
288 else if(sg_flags & WE_HAVE_A_TWO_BY_TWO)
290 } while(sg_flags & MORE_COMPONENTS);
293 sprintf(buf, "%%%%glyph %04lx\n", index);
294 PSDRV_WriteSpool(physDev, buf, strlen(buf));
295 PSDRV_WriteSpool(physDev, "<", 1);
296 for(i = start; i < end; i++) {
297 sprintf(buf, "%02x", *(t42->tables[t42->glyf_tab].data + i));
298 PSDRV_WriteSpool(physDev, buf, strlen(buf));
299 if((i - start) % 16 == 15)
300 PSDRV_WriteSpool(physDev, "\n", 1);
302 PSDRV_WriteSpool(physDev, ">\n", 2);
303 sprintf(buf, glyph_def, pdl->ps_name, index, glyph_name, index,
304 glyph_name, (float)lsb / t42->emsize, (float)awidth / t42->emsize);
305 PSDRV_WriteSpool(physDev, buf, strlen(buf));
307 t42->glyph_sent[index] = TRUE;
308 HeapFree(GetProcessHeap(), 0, buf);
312 void T42_free(TYPE42 *t42)
315 for(table = t42->tables; table->MS_tag; table++)
316 HeapFree(GetProcessHeap(), 0, table->data);
317 HeapFree(GetProcessHeap(), 0, t42->glyph_sent);
318 HeapFree(GetProcessHeap(), 0, t42);