PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/fltk/src/fl_font_win32.cxx

http://luafltk.googlecode.com/
C++ | 418 lines | 299 code | 34 blank | 85 comment | 63 complexity | 50bd4ae9be6b415200673ef1c1035bd6 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, 0BSD
  1. //
  2. // "$Id: fl_font_win32.cxx 7522 2010-04-18 06:57:37Z manolo $"
  3. //
  4. // WIN32 font selection routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2009 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. static int fl_angle_ = 0;
  28. #ifndef FL_DOXYGEN
  29. Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name, Fl_Fontsize size) {
  30. int weight = FW_NORMAL;
  31. int italic = 0;
  32. switch (*name++) {
  33. case 'I': italic = 1; break;
  34. case 'P': italic = 1;
  35. case 'B': weight = FW_BOLD; break;
  36. case ' ': break;
  37. default: name--;
  38. }
  39. fid = CreateFont(
  40. -size, // negative makes it use "char size"
  41. 0, // logical average character width
  42. fl_angle_*10, // angle of escapement
  43. fl_angle_*10, // base-line orientation angle
  44. weight,
  45. italic,
  46. FALSE, // underline attribute flag
  47. FALSE, // strikeout attribute flag
  48. DEFAULT_CHARSET, // character set identifier
  49. OUT_DEFAULT_PRECIS, // output precision
  50. CLIP_DEFAULT_PRECIS,// clipping precision
  51. DEFAULT_QUALITY, // output quality
  52. DEFAULT_PITCH, // pitch and family
  53. name // pointer to typeface name string
  54. );
  55. angle = fl_angle_;
  56. if (!fl_gc) fl_GetDC(0);
  57. SelectObject(fl_gc, fid);
  58. GetTextMetrics(fl_gc, &metr);
  59. // BOOL ret = GetCharWidthFloat(fl_gc, metr.tmFirstChar, metr.tmLastChar, font->width+metr.tmFirstChar);
  60. // ...would be the right call, but is not implemented into Window95! (WinNT?)
  61. //GetCharWidth(fl_gc, 0, 255, width);
  62. int i;
  63. for (i = 0; i < 64; i++) width[i] = NULL;
  64. #if HAVE_GL
  65. listbase = 0;
  66. for (i = 0; i < 64; i++) glok[i] = 0;
  67. #endif
  68. minsize = maxsize = size;
  69. }
  70. Fl_Font_Descriptor* fl_fontsize;
  71. Fl_Font_Descriptor::~Fl_Font_Descriptor() {
  72. #if HAVE_GL
  73. // Delete list created by gl_draw(). This is not done by this code
  74. // as it will link in GL unnecessarily. There should be some kind
  75. // of "free" routine pointer, or a subclass?
  76. // if (listbase) {
  77. // int base = font->min_char_or_byte2;
  78. // int size = font->max_char_or_byte2-base+1;
  79. // int base = 0; int size = 256;
  80. // glDeleteLists(listbase+base,size);
  81. // }
  82. #endif
  83. if (this == fl_fontsize) fl_fontsize = 0;
  84. DeleteObject(fid);
  85. int i;
  86. for (i = 0; i < 64; i++) free(width[i]);
  87. }
  88. ////////////////////////////////////////////////////////////////
  89. // WARNING: if you add to this table, you must redefine FL_FREE_FONT
  90. // in Enumerations.H & recompile!!
  91. static Fl_Fontdesc built_in_table[] = {
  92. {" Arial"},
  93. {"BArial"},
  94. {"IArial"},
  95. {"PArial"},
  96. {" Courier New"},
  97. {"BCourier New"},
  98. {"ICourier New"},
  99. {"PCourier New"},
  100. {" Times New Roman"},
  101. {"BTimes New Roman"},
  102. {"ITimes New Roman"},
  103. {"PTimes New Roman"},
  104. {" Symbol"},
  105. {" Terminal"},
  106. {"BTerminal"},
  107. {" Wingdings"},
  108. };
  109. Fl_Fontdesc* fl_fonts = built_in_table;
  110. static Fl_Font_Descriptor* find(Fl_Font fnum, Fl_Fontsize size, int angle) {
  111. Fl_Fontdesc* s = fl_fonts+fnum;
  112. if (!s->name) s = fl_fonts; // use 0 if fnum undefined
  113. Fl_Font_Descriptor* f;
  114. for (f = s->first; f; f = f->next)
  115. if (f->minsize <= size && f->maxsize >= size && f->angle == angle) return f;
  116. f = new Fl_Font_Descriptor(s->name, size);
  117. f->next = s->first;
  118. s->first = f;
  119. return f;
  120. }
  121. ////////////////////////////////////////////////////////////////
  122. // Public interface:
  123. Fl_Font fl_font_ = 0;
  124. Fl_Fontsize fl_size_ = 0;
  125. //static HDC font_gc;
  126. void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) {
  127. if (fnum==-1) { // just make sure that we will load a new font next time
  128. fl_font_ = 0; fl_size_ = 0; fl_angle_ = 0;
  129. return;
  130. }
  131. if (fnum == fl_font_ && size == fl_size_ && angle == fl_angle_) return;
  132. fl_font_ = fnum; fl_size_ = size; fl_angle_ = angle;
  133. fl_fontsize = find(fnum, size, angle);
  134. }
  135. void Fl_Device::font(Fl_Font fnum, Fl_Fontsize size) {
  136. fl_font(fnum, size, 0);
  137. }
  138. int fl_height() {
  139. if (fl_fontsize) return (fl_fontsize->metr.tmAscent + fl_fontsize->metr.tmDescent);
  140. else return -1;
  141. }
  142. int fl_descent() {
  143. if (fl_fontsize) return fl_fontsize->metr.tmDescent;
  144. else return -1;
  145. }
  146. // Unicode string buffer
  147. static xchar *wstr = NULL;
  148. static int wstr_len = 0;
  149. double fl_width(const char* c, int n) {
  150. int i = 0;
  151. if (!fl_fontsize) return -1.0;
  152. double w = 0.0;
  153. char *end = (char *)&c[n];
  154. while (i < n) {
  155. unsigned int ucs;
  156. // int l = fl_utf2ucs((const unsigned char*)c + i, n - i, &ucs);
  157. int l;
  158. ucs = fl_utf8decode((const char*)(c + i), end, &l);
  159. // if (l < 1) l = 1;
  160. i += l;
  161. if (!fl_nonspacing(ucs)) {
  162. w += fl_width(ucs);
  163. }
  164. }
  165. return w;
  166. }
  167. double fl_width(unsigned int c) {
  168. unsigned int r;
  169. r = (c & 0xFC00) >> 10;
  170. if (!fl_fontsize->width[r]) {
  171. SelectObject(fl_gc, fl_fontsize->fid);
  172. fl_fontsize->width[r] = (int*) malloc(sizeof(int) * 0x0400);
  173. SIZE s;
  174. unsigned short i = 0, ii = r * 0x400;
  175. // The following code makes a best effort attempt to obtain a valid fl_gc.
  176. // If no fl_gc is available at the time we call fl_width(), then we first
  177. // try to obtain a gc from the first fltk window.
  178. // If that is null then we attempt to obtain the gc from the current screen
  179. // using (GetDC(NULL)).
  180. // This should resolve STR #2086
  181. HDC gc = fl_gc;
  182. HWND hWnd = 0;
  183. if (!gc) { // We have no valid gc, try and obtain one
  184. // Use our first fltk window, or fallback to using the screen via GetDC(NULL)
  185. hWnd = Fl::first_window() ? fl_xid(Fl::first_window()) : NULL;
  186. gc = GetDC(hWnd);
  187. }
  188. if (!gc)
  189. Fl::fatal("Invalid graphic context: fl_width() failed because no valid HDC was found!");
  190. for (; i < 0x400; i++) {
  191. GetTextExtentPoint32W(gc, (WCHAR*)&ii, 1, &s);
  192. fl_fontsize->width[r][i] = s.cx;
  193. ii++;
  194. }
  195. if (gc && gc!=fl_gc) ReleaseDC(hWnd, gc);
  196. }
  197. return (double) fl_fontsize->width[r][c & 0x03FF];
  198. }
  199. /* Add function pointer to allow us to access GetGlyphIndicesW on systems that have it,
  200. * without crashing on systems that do not. */
  201. /* DWORD WINAPI GetGlyphIndicesW(HDC,LPCWSTR,int,LPWORD,DWORD) */
  202. typedef DWORD (WINAPI* fl_GetGlyphIndices_func)(HDC,LPCWSTR,int,LPWORD,DWORD);
  203. static fl_GetGlyphIndices_func fl_GetGlyphIndices = NULL; // used to hold a proc pointer for GetGlyphIndicesW
  204. static int have_loaded_GetGlyphIndices = 0; // Set this non-zero once we have tried to load GetGlyphIndices
  205. // Function that tries to dynamically load GetGlyphIndicesW at runtime
  206. static void GetGlyphIndices_init() {
  207. // Since not all versions of Windows include GetGlyphIndicesW support,
  208. // we do a run-time check for the required function.
  209. HMODULE hMod = GetModuleHandle("GDI32.DLL");
  210. if (hMod) {
  211. // check that GetGlyphIndicesW is available
  212. fl_GetGlyphIndices = (fl_GetGlyphIndices_func)GetProcAddress(hMod, "GetGlyphIndicesW");
  213. }
  214. have_loaded_GetGlyphIndices = -1; // set this non-zero when we have attempted to load GetGlyphIndicesW
  215. } // GetGlyphIndices_init function
  216. static void on_printer_extents_update(int &dx, int &dy, int &w, int &h)
  217. // converts text extents from device coords to logical coords
  218. {
  219. POINT pt[3] = { {0, 0}, {dx, dy}, {dx+w, dy+h} };
  220. DPtoLP(fl_gc, pt, 3);
  221. w = pt[2].x - pt[1].x;
  222. h = pt[2].y - pt[1].y;
  223. dx = pt[1].x - pt[0].x;
  224. dy = pt[1].y - pt[0].y;
  225. }
  226. // if printer context, extents shd be converted to logical coords
  227. #define EXTENTS_UPDATE(x,y,w,h) \
  228. if (Fl_Device::current()->type() == Fl_Device::gdi_printer) { on_printer_extents_update(x,y,w,h); }
  229. static unsigned short *ext_buff = NULL; // UTF-16 converted version of input UTF-8 string
  230. static unsigned wc_len = 0; // current string buffer dimension
  231. static WORD *gi = NULL; // glyph indices array
  232. // Function to determine the extent of the "inked" area of the glyphs in a string
  233. void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) {
  234. if (!fl_fontsize) {
  235. w = 0; h = 0;
  236. dx = dy = 0;
  237. return;
  238. }
  239. static const MAT2 matrix = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
  240. GLYPHMETRICS metrics;
  241. int maxw = 0, maxh = 0, dh;
  242. int minx = 0, miny = -999999;
  243. unsigned len = 0, idx = 0;
  244. HWND hWnd = 0;
  245. // Have we loaded the GetGlyphIndicesW function yet?
  246. if (have_loaded_GetGlyphIndices == 0) {
  247. GetGlyphIndices_init();
  248. }
  249. // Do we have a usable GetGlyphIndices function?
  250. if(!fl_GetGlyphIndices) goto exit_error; // No GetGlyphIndices function, use fallback mechanism instead
  251. // The following code makes a best effort attempt to obtain a valid fl_gc.
  252. // See description in fl_width() above for an explanation.
  253. if (!fl_gc) { // We have no valid gc, try and obtain one
  254. // Use our first fltk window, or fallback to using the screen via GetDC(NULL)
  255. hWnd = Fl::first_window() ? fl_xid(Fl::first_window()) : NULL;
  256. fl_gc = GetDC(hWnd);
  257. }
  258. if (!fl_gc)goto exit_error; // no valid gc, attempt to use fallback measure
  259. // now convert the string to WCHAR and measure it
  260. len = fl_utf8toUtf16(c, n, ext_buff, wc_len);
  261. if(len >= wc_len) {
  262. if(ext_buff) {delete [] ext_buff;}
  263. if(gi) {delete [] gi;}
  264. wc_len = len + 64;
  265. ext_buff = new unsigned short[wc_len];
  266. gi = new WORD[wc_len];
  267. len = fl_utf8toUtf16(c, n, ext_buff, wc_len);
  268. }
  269. SelectObject(fl_gc, fl_fontsize->fid);
  270. if (fl_GetGlyphIndices(fl_gc, (WCHAR*)ext_buff, len, gi, 0) == GDI_ERROR) {
  271. // some error occured here - just return fl_measure values?
  272. goto exit_error;
  273. }
  274. // now we have the glyph array we measure each glyph in turn...
  275. for(idx = 0; idx < len; idx++){
  276. if (GetGlyphOutlineW (fl_gc, gi[idx], GGO_METRICS | GGO_GLYPH_INDEX,
  277. &metrics, 0, NULL, &matrix) == GDI_ERROR) {
  278. goto exit_error;
  279. }
  280. maxw += metrics.gmCellIncX;
  281. if(idx == 0) minx = metrics.gmptGlyphOrigin.x;
  282. dh = metrics.gmBlackBoxY - metrics.gmptGlyphOrigin.y;
  283. if(dh > maxh) maxh = dh;
  284. if(miny < metrics.gmptGlyphOrigin.y) miny = metrics.gmptGlyphOrigin.y;
  285. }
  286. // for the last cell, we only want the bounding X-extent, not the glyphs increment step
  287. maxw = maxw - metrics.gmCellIncX + metrics.gmBlackBoxX + metrics.gmptGlyphOrigin.x;
  288. w = maxw - minx;
  289. h = maxh + miny;
  290. dx = minx;
  291. dy = -miny;
  292. EXTENTS_UPDATE(dx, dy, w, h);
  293. return; // normal exit
  294. exit_error:
  295. // some error here - just return fl_measure values
  296. w = (int)fl_width(c, n);
  297. h = fl_height();
  298. dx = 0;
  299. dy = fl_descent() - h;
  300. EXTENTS_UPDATE(dx, dy, w, h);
  301. return;
  302. } // fl_text_extents
  303. void Fl_Device::draw(const char* str, int n, int x, int y) {
  304. int i = 0;
  305. int lx = 0;
  306. char *end = (char *)&str[n];
  307. COLORREF oldColor = SetTextColor(fl_gc, fl_RGB());
  308. SelectObject(fl_gc, fl_fontsize->fid);
  309. while (i < n) {
  310. unsigned int u;
  311. unsigned int u1;
  312. unsigned short ucs;
  313. // int l = fl_utf2ucs((const unsigned char*)str + i, n - i, &u);
  314. int l;
  315. u = fl_utf8decode((const char*)(str + i), end, &l);
  316. if (u1 = fl_nonspacing(u)) {
  317. x -= lx;
  318. u = u1;
  319. } else {
  320. lx = (int) fl_width(u);
  321. }
  322. ucs = u;
  323. if (l < 1) l = 1;
  324. i += l;
  325. TextOutW(fl_gc, x, y, (WCHAR*)&ucs, 1);
  326. x += lx;
  327. }
  328. SetTextColor(fl_gc, oldColor);
  329. }
  330. void Fl_Device::draw(int angle, const char* str, int n, int x, int y) {
  331. fl_font(fl_font_, fl_size_, angle);
  332. // fl_draw(str, n, (int)x, (int)y);
  333. int i = 0, i2=0;
  334. char *end = (char *)&str[n];
  335. COLORREF oldColor = SetTextColor(fl_gc, fl_RGB());
  336. SelectObject(fl_gc, fl_fontsize->fid);
  337. //unsigned short ucs[n]; //only GCC, but not MSVC
  338. unsigned short* ucs = new unsigned short[n];
  339. while (i < n) {
  340. unsigned int u;
  341. int l;
  342. u = fl_utf8decode((const char*)(str + i), end, &l);
  343. ucs[i2] = u;
  344. if (l < 1) l = 1;
  345. i += l;
  346. ++i2;
  347. }
  348. TextOutW(fl_gc, x, y, (WCHAR*)ucs, i2);
  349. delete[] ucs;
  350. SetTextColor(fl_gc, oldColor);
  351. fl_font(fl_font_, fl_size_);
  352. }
  353. void fl_rtl_draw(const char* c, int n, int x, int y) {
  354. int wn;
  355. int i = 0;
  356. int lx = 0;
  357. // if (n > wstr_len) {
  358. // wstr = (xchar*) realloc(wstr, sizeof(xchar) * (n + 1));
  359. // wstr_len = n;
  360. // }
  361. //wn = fl_utf2unicode((const unsigned char *)c, n, wstr);
  362. wn = fl_utf8toUtf16(c, n, (unsigned short*)wstr, wstr_len);
  363. if(wn >= wstr_len) {
  364. wstr = (xchar*) realloc(wstr, sizeof(xchar) * (wn + 1));
  365. wstr_len = wn + 1;
  366. wn = fl_utf8toUtf16(c, n, (unsigned short*)wstr, wstr_len);
  367. }
  368. COLORREF oldColor = SetTextColor(fl_gc, fl_RGB());
  369. SelectObject(fl_gc, fl_fontsize->fid);
  370. while (i < wn) {
  371. lx = (int) fl_width(wstr[i]);
  372. x -= lx;
  373. TextOutW(fl_gc, x, y, (WCHAR*)wstr + i, 1);
  374. if (fl_nonspacing(wstr[i])) {
  375. x += lx;
  376. }
  377. i++;
  378. }
  379. SetTextColor(fl_gc, oldColor);
  380. }
  381. #endif
  382. //
  383. // End of "$Id: fl_font_win32.cxx 7522 2010-04-18 06:57:37Z manolo $".
  384. //