gdevmsxf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Copyright (C) 1993, 1994, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevmsxf.c,v 1.8 2004/08/04 23:33:29 stefan Exp $ */
  14. /* External font (xfont) implementation for Microsoft Windows. */
  15. #include "ctype_.h"
  16. #include "math_.h"
  17. #include "memory_.h"
  18. #include "string_.h"
  19. #include "gdevmswn.h"
  20. #include "gsutil.h"
  21. #include "gxxfont.h"
  22. #include "gsstruct.h"
  23. /* Imported from gdevemap.c */
  24. extern const byte gs_map_std_to_iso[256];
  25. /* Declare the xfont procedures */
  26. private xfont_proc_lookup_font(win_lookup_font);
  27. private xfont_proc_char_xglyph(win_char_xglyph);
  28. private xfont_proc_char_metrics(win_char_metrics);
  29. private xfont_proc_render_char(win_render_char);
  30. private xfont_proc_release(win_release);
  31. private const gx_xfont_procs win_xfont_procs =
  32. {
  33. win_lookup_font,
  34. win_char_xglyph,
  35. win_char_metrics,
  36. win_render_char,
  37. win_release
  38. };
  39. /* Return the xfont procedure record. */
  40. const gx_xfont_procs *
  41. win_get_xfont_procs(gx_device * dev)
  42. {
  43. return &win_xfont_procs;
  44. }
  45. /* Define a Windows xfont. */
  46. typedef struct win_xfont_s win_xfont;
  47. struct win_xfont_s {
  48. gx_xfont_common common;
  49. LOGFONT lf;
  50. TEXTMETRIC tm;
  51. HFONT hFont;
  52. gx_device_win *dev; /* for GetDC */
  53. int invert_y;
  54. int y_offset;
  55. };
  56. gs_private_st_dev_ptrs1(st_win_xfont, win_xfont, "win_xfont",
  57. win_xfont_enum_ptrs, win_xfont_reloc_ptrs, dev);
  58. #define wxf ((win_xfont *)xf)
  59. /* Forward references */
  60. private HDC near win_get_dc(gx_device_win *);
  61. private void near win_release_dc(gx_device_win *, HDC);
  62. private int win_select_font(HDC, win_xfont *);
  63. /* Map from PostScript to Windows character codes. */
  64. /* (These tables were generated by winmaps.ps.) */
  65. private const byte far_data gs_map_symbol_to_oem[256] =
  66. {
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 32, 33, 0, 35, 0, 37, 38, 0, 40, 41, 0, 43, 44, 0, 46, 47,
  70. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  71. 0, 0, 0, 0, 0, 0, 231, 225, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 226, 232, 0, 227, 0, 0, 0, 233, 0, 0, 0, 91, 0, 93, 0, 95,
  73. 0, 223, 224, 0, 234, 0, 236, 0, 0, 0, 0, 0, 0, 229, 0, 0,
  74. 0, 0, 0, 228, 230, 0, 0, 0, 0, 0, 0, 123, 124, 125, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 242, 0, 235, 0, 5, 4, 3, 6, 29, 27, 24, 26, 25,
  78. 247, 240, 0, 241, 0, 0, 0, 7, 245, 0, 239, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 238, 0, 0, 0, 0, 0, 0, 237, 0,
  80. 0, 0, 0, 0, 0, 0, 250, 248, 169, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 243, 0, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  83. };
  84. private const byte far_data gs_map_iso_to_oem[256] =
  85. {
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 0, 46, 47,
  89. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  90. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  91. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  92. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  93. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 0,
  94. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 32, 172, 154, 155, 0, 156, 0, 21, 0, 0, 165, 173, 169, 45, 0, 0,
  97. 247, 240, 252, 0, 0, 229, 20, 249, 0, 0, 166, 174, 171, 170, 0, 167,
  98. 0, 0, 0, 0, 141, 142, 145, 128, 0, 143, 0, 0, 0, 0, 0, 0,
  99. 0, 164, 0, 0, 0, 0, 152, 0, 0, 0, 0, 0, 153, 0, 0, 0,
  100. 133, 159, 131, 0, 132, 134, 144, 135, 138, 130, 136, 137, 140, 160, 0, 139,
  101. 0, 163, 148, 161, 146, 0, 147, 245, 0, 150, 162, 149, 129, 0, 0, 151
  102. };
  103. /* Correlate PostScript font names with Windows font names. */
  104. /* This table should be an external resource, like Fontmap, */
  105. /* but that will have to wait till later. */
  106. typedef struct font_entry_s {
  107. const char *key;
  108. const char *value;
  109. uint pitchAndFamily;
  110. } font_entry;
  111. private const font_entry font_names[] =
  112. {
  113. {"Courier", "Courier New", FIXED_PITCH | FF_MODERN},
  114. {"Helvetica", "Arial", VARIABLE_PITCH | FF_SWISS},
  115. {"Helvetica", "Helv", VARIABLE_PITCH | FF_SWISS},
  116. {"Times", "Times New Roman", VARIABLE_PITCH | FF_ROMAN},
  117. {"Times", "Tms Rmn", VARIABLE_PITCH | FF_ROMAN}
  118. };
  119. /* Look up a font. */
  120. private int /*bool */ map_logical_font(HDC, win_xfont *);
  121. gx_xfont *
  122. win_lookup_font(gx_device * dev, const byte * fname, uint len,
  123. int encoding_index, const gs_uid * puid, const gs_matrix * pmat,
  124. gs_memory_t * mem)
  125. {
  126. win_xfont f;
  127. win_xfont *wf;
  128. uint name_len = min(len, LF_FACESIZE - 1);
  129. const font_entry *pfe;
  130. HDC hdc;
  131. /* Only handle simple cases for now. */
  132. if (pmat->xy != 0 || pmat->yx != 0 || pmat->xx <= 0 ||
  133. fabs(fabs(pmat->yy) - pmat->xx) > 0.00002
  134. )
  135. return NULL;
  136. f.lf.lfHeight = (long)(pmat->xx * 1000);
  137. /* Don't trust Windows with very small sizes. */
  138. if (f.lf.lfHeight < 6 || f.lf.lfHeight >= 36)
  139. return NULL;
  140. f.lf.lfWidth = 0;
  141. f.lf.lfEscapement = 0;
  142. f.lf.lfOrientation = 0;
  143. f.lf.lfWeight =
  144. (string_match(fname, len, "*Bold*", 6, NULL) ?
  145. FW_BOLD : FW_REGULAR);
  146. f.lf.lfItalic =
  147. string_match(fname, len, "*Italic*", 8, NULL) ||
  148. string_match(fname, len, "*Oblique*", 9, NULL);
  149. f.lf.lfUnderline = 0;
  150. f.lf.lfStrikeOut = 0;
  151. f.lf.lfCharSet =
  152. (encoding_index == 2 ? SYMBOL_CHARSET : ANSI_CHARSET);
  153. f.lf.lfOutPrecision = OUT_CHARACTER_PRECIS;
  154. f.lf.lfClipPrecision = CLIP_STROKE_PRECIS;
  155. f.lf.lfQuality = PROOF_QUALITY;
  156. f.hFont = 0;
  157. f.invert_y = pmat->yy >= 0;
  158. hdc = win_get_dc(wdev);
  159. if (hdc == NULL)
  160. return NULL;
  161. for (pfe = font_names; pfe != &font_names[countof(font_names)]; pfe++)
  162. if (!strncmp(pfe->key, fname, strlen(pfe->key))) { /* Found a match. */
  163. strcpy(f.lf.lfFaceName, pfe->value);
  164. f.lf.lfPitchAndFamily = pfe->pitchAndFamily;
  165. if (map_logical_font(hdc, &f))
  166. break;
  167. }
  168. if (f.hFont == 0) { /* No matches in the table, try with the given name. */
  169. uint len;
  170. memcpy(f.lf.lfFaceName, fname, name_len); /* default */
  171. for (len = 0; len < name_len; len++)
  172. if (!isalnum(fname[len]))
  173. break;
  174. f.lf.lfFaceName[len] = 0;
  175. f.lf.lfPitchAndFamily = 0; /* default */
  176. if (!map_logical_font(hdc, &f)) {
  177. win_release_dc(wdev, hdc);
  178. return NULL;
  179. }
  180. }
  181. GetTextMetrics(hdc, &f.tm);
  182. win_release_dc(wdev, hdc);
  183. f.y_offset = (!f.invert_y ? f.tm.tmAscent : f.tm.tmDescent);
  184. wf = gs_alloc_struct(mem, win_xfont, &st_win_xfont, "win_lookup_font");
  185. if (wf == 0) {
  186. DeleteObject(f.hFont);
  187. return NULL;
  188. }
  189. f.common.procs = &win_xfont_procs;
  190. f.dev = wdev;
  191. *wf = f;
  192. return (gx_xfont *) wf;
  193. }
  194. /* Map the logical font, and see if the result is satisfactory. */
  195. private int /*bool */
  196. map_logical_font(HDC hdc, win_xfont * xf)
  197. {
  198. char szFaceName[LF_FACESIZE];
  199. xf->hFont = CreateFontIndirect(&xf->lf);
  200. if (xf->hFont == 0)
  201. return 0;
  202. /* Check the face name */
  203. SelectObject(hdc, xf->hFont);
  204. GetTextFace(hdc, sizeof(szFaceName), szFaceName);
  205. if (!strncmp(xf->lf.lfFaceName, szFaceName, strlen(xf->lf.lfFaceName)))
  206. return 1;
  207. DeleteObject(xf->hFont);
  208. xf->hFont = 0;
  209. return 0;
  210. }
  211. /* Convert a character name or index to an xglyph code. */
  212. gx_xglyph
  213. win_char_xglyph(gx_xfont * xf, gs_char chr, int encoding_index,
  214. gs_glyph glyph, const gs_const_string *glyph_name)
  215. {
  216. if (chr == gs_no_char)
  217. return gx_no_xglyph; /* can't look up names yet */
  218. if (encoding_index == 0) { /* Map StandardEncoding to ISOLatin1Encoding. */
  219. /* We lose a couple of characters that exist in both */
  220. /* StandardEncoding and the Windows OEM encoding but not in */
  221. /* the ISOLatin1Encoding; we won't worry about this */
  222. /* for now. */
  223. chr = gs_map_std_to_iso[chr];
  224. encoding_index = 1;
  225. }
  226. if (wxf->hFont == NULL) { /* TEXTMETRICS not filled in yet */
  227. HDC hdc = win_get_dc(wxf->dev);
  228. int code;
  229. if (hdc == NULL)
  230. return gx_no_xglyph;
  231. code = win_select_font(hdc, wxf);
  232. win_release_dc(wxf->dev, hdc);
  233. if (code < 0)
  234. return gx_no_xglyph;
  235. }
  236. switch (wxf->tm.tmCharSet) {
  237. case ANSI_CHARSET:
  238. if (encoding_index == 1 && (chr < 0x7f || chr > 0x9f ||
  239. chr == 0x91 || chr == 0x92)
  240. )
  241. break;
  242. return gx_no_xglyph;
  243. case OEM_CHARSET:
  244. switch (encoding_index) {
  245. case 1: /* ISOLatin1 */
  246. chr = gs_map_iso_to_oem[chr];
  247. break;
  248. case 2: /* Symbol */
  249. chr = gs_map_symbol_to_oem[chr];
  250. break;
  251. default:
  252. return gx_no_xglyph;
  253. }
  254. break;
  255. default:
  256. return gx_no_xglyph;
  257. }
  258. return (chr != 0 && chr >= wxf->tm.tmFirstChar &&
  259. chr <= wxf->tm.tmLastChar ?
  260. (gx_xglyph) chr : gx_no_xglyph);
  261. }
  262. /* Get the metrics for a character. */
  263. int
  264. win_char_metrics(gx_xfont * xf, gx_xglyph xg, int wmode,
  265. gs_point * pwidth, gs_int_rect * pbbox)
  266. {
  267. int code;
  268. HDC hdc;
  269. char chr = (char)xg;
  270. if (wmode != 0)
  271. return gs_error_undefined;
  272. hdc = win_get_dc(wxf->dev);
  273. if (hdc == NULL)
  274. return gs_error_limitcheck;
  275. if ((code = win_select_font(hdc, wxf)) < 0) {
  276. win_release_dc(wxf->dev, hdc);
  277. return code;
  278. }
  279. #ifdef __WIN32__
  280. {
  281. SIZE sz;
  282. GetTextExtentPoint(hdc, &chr, 1, &sz);
  283. pwidth->x = sz.cx;
  284. }
  285. #else
  286. {
  287. DWORD extent;
  288. extent = GetTextExtent(hdc, &chr, 1);
  289. pwidth->x = LOWORD(extent);
  290. }
  291. #endif
  292. win_release_dc(wxf->dev, hdc);
  293. pwidth->y = 0;
  294. pbbox->p.x = 0;
  295. pbbox->q.x = (int)pwidth->x;
  296. if (wxf->invert_y) {
  297. pbbox->p.y = -wxf->tm.tmDescent;
  298. pbbox->q.y = wxf->tm.tmAscent;
  299. } else {
  300. pbbox->p.y = -wxf->tm.tmAscent;
  301. pbbox->q.y = wxf->tm.tmDescent;
  302. }
  303. return 0;
  304. }
  305. /* Render a character. */
  306. int
  307. win_render_char(gx_xfont * xf, gx_xglyph xg, gx_device * dev,
  308. int xo, int yo, gx_color_index color, int required)
  309. {
  310. char chr = (char)xg;
  311. int code;
  312. #ifdef NOTUSED /* we don't own any windows so we can no longer do this */
  313. if (dev->dname == gs_mswin_device.dname &&
  314. wdev->hdctext != NULL && !wxf->invert_y
  315. ) { /* Display the character directly */
  316. HDC hdc = wdev->hdctext;
  317. PALETTEENTRY *pal = &wdev->limgpalette->palPalEntry[color];
  318. if ((code = win_select_font(hdc, wxf)) < 0)
  319. return code;
  320. SetTextColor(hdc, RGB(pal->peRed, pal->peGreen, pal->peBlue));
  321. SetBkMode(hdc, TRANSPARENT);
  322. TextOut(hdc, xo, yo - wxf->y_offset, &chr, 1);
  323. } else
  324. #endif
  325. if (!required)
  326. code = -1; /* too hard */
  327. else { /* Display on an intermediate bitmap, then copy the bits. */
  328. gs_point wxy;
  329. gs_int_rect bbox;
  330. int w, h, wbm, raster;
  331. gx_device_win *fdev = wxf->dev;
  332. HBITMAP hbm;
  333. byte *bits;
  334. code = (*xf->common.procs->char_metrics) (xf, xg, 0,
  335. &wxy, &bbox);
  336. if (code < 0)
  337. return code;
  338. w = bbox.q.x - bbox.p.x;
  339. h = bbox.q.y - bbox.p.y;
  340. wbm = ROUND_UP(w, align_bitmap_mod * 8);
  341. raster = wbm >> 3;
  342. bits = gs_malloc(dev->memory, h, raster, "win_render_char");
  343. if (bits == 0)
  344. return gs_error_limitcheck;
  345. hbm = CreateBitmap(wbm, h, 1, 1, NULL);
  346. if (hbm == NULL) {
  347. code = gs_error_limitcheck;
  348. } else {
  349. HDC hdcwin = win_get_dc(fdev);
  350. HDC hdcbit = CreateCompatibleDC(hdcwin);
  351. dev_proc_copy_mono((*copy_mono)) =
  352. dev_proc(dev, copy_mono);
  353. int y = yo - wxf->y_offset;
  354. SetMapMode(hdcbit, GetMapMode(hdcwin));
  355. win_select_font(hdcbit, wxf);
  356. SelectObject(hdcbit, hbm);
  357. PatBlt(hdcbit, 0, 0, wbm, h, rop_write_0s);
  358. SetTextColor(hdcbit, 0xffffffL); /* 1 */
  359. SetBkMode(hdcbit, TRANSPARENT);
  360. TextOut(hdcbit, 0, 0, &chr, 1);
  361. GetBitmapBits(hbm, (DWORD) raster * h, bits);
  362. DeleteDC(hdcbit);
  363. win_release_dc(fdev, hdcwin);
  364. DeleteObject(hbm);
  365. if (!wxf->invert_y)
  366. code = (*copy_mono) (dev, bits, 0,
  367. raster, gx_no_bitmap_id,
  368. xo, y, w, h,
  369. gx_no_color_index, color);
  370. else { /* Copy scan lines in reverse order. */
  371. int i;
  372. y += h - 1;
  373. for (i = 0; i < h; i++)
  374. (*copy_mono) (dev, bits + i * raster,
  375. 0, raster, gx_no_bitmap_id,
  376. xo, y - i, w, 1,
  377. gx_no_color_index, color);
  378. }
  379. }
  380. gs_free(dev->memory, bits, h, raster, "win_render_char");
  381. }
  382. return (code < 0 ? code : 0);
  383. }
  384. /* Release an xfont. */
  385. private int
  386. win_release(gx_xfont * xf, gs_memory_t * mem)
  387. {
  388. if (wxf->hFont) {
  389. DeleteObject(wxf->hFont);
  390. wxf->hFont = 0;
  391. }
  392. if (mem != NULL)
  393. gs_free_object(mem, xf, "win_release");
  394. return 0;
  395. }
  396. /* ------ Font utilities ------ */
  397. #undef wdev
  398. #undef wxf
  399. /* Get a DC for the font's device. */
  400. private HDC near
  401. win_get_dc(gx_device_win * wdev)
  402. {
  403. /* Since we don't have a window, use the desktop */
  404. /* Don't draw into it! */
  405. return GetDC(HWND_DESKTOP);
  406. }
  407. /* Release a DC for the font's device. */
  408. private void near
  409. win_release_dc(gx_device_win * wdev, HDC hdc)
  410. {
  411. ReleaseDC(HWND_DESKTOP, hdc);
  412. }
  413. /* Make an xfont current, possibly remapping it from a logical font. */
  414. private int
  415. win_select_font(HDC hdc, win_xfont * wxf)
  416. {
  417. HFONT hFont = wxf->hFont;
  418. if (hFont == NULL) { /* The font was released to free up resources. */
  419. /* Re-acquire it now. */
  420. wxf->hFont = CreateFontIndirect(&wxf->lf);
  421. if (wxf->hFont == NULL)
  422. return gs_error_limitcheck;
  423. }
  424. SelectObject(hdc, wxf->hFont);
  425. return 0;
  426. }