type1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * PostScript driver Type1 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 "config.h"
  21. #include "wine/port.h"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include "windef.h"
  28. #include "winbase.h"
  29. #include "winerror.h"
  30. #include "wingdi.h"
  31. #include "winspool.h"
  32. #include "psdrv.h"
  33. #include "wine/debug.h"
  34. WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
  35. struct tagTYPE1 {
  36. DWORD glyph_sent_size;
  37. BOOL *glyph_sent;
  38. DWORD emsize;
  39. };
  40. #define GLYPH_SENT_INC 128
  41. /* Type 1 font commands */
  42. enum t1_cmds {
  43. rlineto = 5,
  44. rrcurveto = 8,
  45. closepath = 9,
  46. hsbw = 13,
  47. endchar = 14,
  48. rmoveto = 21
  49. };
  50. TYPE1 *T1_download_header(PSDRV_PDEVICE *physDev, char *ps_name, RECT *bbox, UINT emsize)
  51. {
  52. char *buf;
  53. TYPE1 *t1;
  54. char dict[] = /* name, emsquare, fontbbox */
  55. "25 dict begin\n"
  56. " /FontName /%s def\n"
  57. " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for def\n"
  58. " /PaintType 0 def\n"
  59. " /FontMatrix [1 %d div 0 0 1 %d div 0 0] def\n"
  60. " /FontBBox [%d %d %d %d] def\n"
  61. " /FontType 1 def\n"
  62. " /Private 7 dict begin\n"
  63. " /RD {string currentfile exch readhexstring pop} def\n"
  64. " /ND {def} def\n"
  65. " /NP {put} def\n"
  66. " /MinFeature {16 16} def\n"
  67. " /BlueValues [] def\n"
  68. " /password 5839 def\n"
  69. " /lenIV -1 def\n"
  70. " currentdict end def\n"
  71. " currentdict dup /Private get begin\n"
  72. " /CharStrings 256 dict begin\n"
  73. " /.notdef 4 RD 8b8b0d0e ND\n"
  74. " currentdict end put\n"
  75. " end\n"
  76. "currentdict end dup /FontName get exch definefont pop\n";
  77. t1 = HeapAlloc(GetProcessHeap(), 0, sizeof(*t1));
  78. t1->emsize = emsize;
  79. t1->glyph_sent_size = GLYPH_SENT_INC;
  80. t1->glyph_sent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  81. t1->glyph_sent_size *
  82. sizeof(*(t1->glyph_sent)));
  83. buf = HeapAlloc(GetProcessHeap(), 0, sizeof(dict) + strlen(ps_name) +
  84. 100);
  85. sprintf(buf, dict, ps_name, t1->emsize, t1->emsize,
  86. bbox->left, bbox->bottom, bbox->right, bbox->top);
  87. PSDRV_WriteSpool(physDev, buf, strlen(buf));
  88. HeapFree(GetProcessHeap(), 0, buf);
  89. return t1;
  90. }
  91. typedef struct {
  92. BYTE *str;
  93. int len, max_len;
  94. } STR;
  95. static STR *str_init(int sz)
  96. {
  97. STR *str = HeapAlloc(GetProcessHeap(), 0, sizeof(*str));
  98. str->max_len = sz;
  99. str->str = HeapAlloc(GetProcessHeap(), 0, str->max_len);
  100. str->len = 0;
  101. return str;
  102. }
  103. static void str_free(STR *str)
  104. {
  105. HeapFree(GetProcessHeap(), 0, str->str);
  106. HeapFree(GetProcessHeap(), 0, str);
  107. }
  108. static void str_add_byte(STR *str, BYTE b)
  109. {
  110. if(str->len == str->max_len) {
  111. str->max_len *= 2;
  112. str->str = HeapReAlloc(GetProcessHeap(), 0, str->str, str->max_len);
  113. }
  114. str->str[str->len++] = b;
  115. }
  116. static void str_add_num(STR *str, int num)
  117. {
  118. if(num <= 107 && num >= -107)
  119. str_add_byte(str, num + 139);
  120. else if(num >= 108 && num <= 1131) {
  121. str_add_byte(str, ((num - 108) >> 8) + 247);
  122. str_add_byte(str, (num - 108) & 0xff);
  123. } else if(num <= -108 && num >= -1131) {
  124. num = -num;
  125. str_add_byte(str, ((num - 108) >> 8) + 251);
  126. str_add_byte(str, (num - 108) & 0xff);
  127. } else {
  128. str_add_byte(str, 0xff);
  129. str_add_byte(str, (num >> 24) & 0xff);
  130. str_add_byte(str, (num >> 16) & 0xff);
  131. str_add_byte(str, (num >> 8) & 0xff);
  132. str_add_byte(str, (num & 0xff));
  133. }
  134. }
  135. static void str_add_point(STR *str, POINTFX *pt, POINT *curpos)
  136. {
  137. POINT newpos;
  138. newpos.x = pt->x.value + ((pt->x.fract >> 15) & 0x1);
  139. newpos.y = pt->y.value + ((pt->y.fract >> 15) & 0x1);
  140. str_add_num(str, newpos.x - curpos->x);
  141. str_add_num(str, newpos.y - curpos->y);
  142. *curpos = newpos;
  143. }
  144. static void str_add_cmd(STR *str, enum t1_cmds cmd)
  145. {
  146. str_add_byte(str, (BYTE)cmd);
  147. }
  148. static int str_get_bytes(STR *str, BYTE **b)
  149. {
  150. *b = str->str;
  151. return str->len;
  152. }
  153. BOOL T1_download_glyph(PSDRV_PDEVICE *physDev, DOWNLOAD *pdl, DWORD index,
  154. char *glyph_name)
  155. {
  156. DWORD len, i;
  157. char *buf;
  158. TYPE1 *t1;
  159. STR *charstring;
  160. BYTE *bytes;
  161. HFONT old_font, unscaled_font;
  162. GLYPHMETRICS gm;
  163. char *glyph_buf;
  164. POINT curpos;
  165. TTPOLYGONHEADER *pph;
  166. TTPOLYCURVE *ppc;
  167. LOGFONTW lf;
  168. RECT rc;
  169. char glyph_def_begin[] =
  170. "/%s findfont dup\n"
  171. "/Private get begin\n"
  172. "/CharStrings get begin\n"
  173. "/%s %d RD\n";
  174. char glyph_def_end[] =
  175. "ND\n"
  176. "end end\n";
  177. TRACE("%ld %s\n", index, glyph_name);
  178. assert(pdl->type == Type1);
  179. t1 = pdl->typeinfo.Type1;
  180. if(index < t1->glyph_sent_size) {
  181. if(t1->glyph_sent[index])
  182. return TRUE;
  183. } else {
  184. t1->glyph_sent_size = (index / GLYPH_SENT_INC + 1) * GLYPH_SENT_INC;
  185. t1->glyph_sent = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  186. t1->glyph_sent,
  187. t1->glyph_sent_size * sizeof(*(t1->glyph_sent)));
  188. }
  189. GetObjectW(GetCurrentObject(physDev->hdc, OBJ_FONT), sizeof(lf), &lf);
  190. rc.left = rc.right = rc.bottom = 0;
  191. rc.top = t1->emsize;
  192. DPtoLP(physDev->hdc, (POINT*)&rc, 2);
  193. lf.lfHeight = -abs(rc.top - rc.bottom);
  194. lf.lfOrientation = lf.lfEscapement = 0;
  195. unscaled_font = CreateFontIndirectW(&lf);
  196. old_font = SelectObject(physDev->hdc, unscaled_font);
  197. len = GetGlyphOutlineW(physDev->hdc, index, GGO_GLYPH_INDEX | GGO_BEZIER,
  198. &gm, 0, NULL, NULL);
  199. if(len == GDI_ERROR) return FALSE;
  200. glyph_buf = HeapAlloc(GetProcessHeap(), 0, len);
  201. GetGlyphOutlineW(physDev->hdc, index, GGO_GLYPH_INDEX | GGO_BEZIER,
  202. &gm, len, glyph_buf, NULL);
  203. SelectObject(physDev->hdc, old_font);
  204. DeleteObject(unscaled_font);
  205. charstring = str_init(100);
  206. curpos.x = gm.gmptGlyphOrigin.x;
  207. curpos.y = 0;
  208. str_add_num(charstring, curpos.x);
  209. str_add_num(charstring, gm.gmCellIncX);
  210. str_add_cmd(charstring, hsbw);
  211. pph = (TTPOLYGONHEADER*)glyph_buf;
  212. while((char*)pph < glyph_buf + len) {
  213. TRACE("contour len %ld\n", pph->cb);
  214. ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
  215. str_add_point(charstring, &pph->pfxStart, &curpos);
  216. str_add_cmd(charstring, rmoveto);
  217. while((char*)ppc < (char*)pph + pph->cb) {
  218. TRACE("line type %d cpfx = %d\n", ppc->wType, ppc->cpfx);
  219. switch(ppc->wType) {
  220. case TT_PRIM_LINE:
  221. for(i = 0; i < ppc->cpfx; i++) {
  222. str_add_point(charstring, ppc->apfx + i, &curpos);
  223. str_add_cmd(charstring, rlineto);
  224. }
  225. break;
  226. case TT_PRIM_CSPLINE:
  227. for(i = 0; i < ppc->cpfx/3; i++) {
  228. str_add_point(charstring, ppc->apfx + 3 * i, &curpos);
  229. str_add_point(charstring, ppc->apfx + 3 * i + 1, &curpos);
  230. str_add_point(charstring, ppc->apfx + 3 * i + 2, &curpos);
  231. str_add_cmd(charstring, rrcurveto);
  232. }
  233. break;
  234. default:
  235. ERR("curve type = %d\n", ppc->wType);
  236. return FALSE;
  237. }
  238. ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
  239. (ppc->cpfx - 1) * sizeof(POINTFX));
  240. }
  241. str_add_cmd(charstring, closepath);
  242. pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
  243. }
  244. str_add_cmd(charstring, endchar);
  245. buf = HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def_begin) +
  246. strlen(pdl->ps_name) + strlen(glyph_name) + 100);
  247. sprintf(buf, "%%%%glyph %04lx\n", index);
  248. PSDRV_WriteSpool(physDev, buf, strlen(buf));
  249. len = str_get_bytes(charstring, &bytes);
  250. sprintf(buf, glyph_def_begin, pdl->ps_name, glyph_name, len);
  251. PSDRV_WriteSpool(physDev, buf, strlen(buf));
  252. PSDRV_WriteBytes(physDev, bytes, len);
  253. sprintf(buf, glyph_def_end);
  254. PSDRV_WriteSpool(physDev, buf, strlen(buf));
  255. str_free(charstring);
  256. t1->glyph_sent[index] = TRUE;
  257. HeapFree(GetProcessHeap(), 0, glyph_buf);
  258. HeapFree(GetProcessHeap(), 0, buf);
  259. return TRUE;
  260. }
  261. void T1_free(TYPE1 *t1)
  262. {
  263. HeapFree(GetProcessHeap(), 0, t1->glyph_sent);
  264. HeapFree(GetProcessHeap(), 0, t1);
  265. return;
  266. }