x86UNIXFont.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "dgl/gFont.h"
  23. #include "dgl/gBitmap.h"
  24. #include "math/mRect.h"
  25. #include "console/console.h"
  26. #include "core/unicode.h"
  27. #include "platformX86UNIX/platformX86UNIX.h"
  28. #include "platformX86UNIX/x86UNIXFont.h"
  29. // Needed by createFont
  30. #include <X11/Xlib.h>
  31. #include <X11/Xutil.h>
  32. #include <X11/Xos.h>
  33. #include <X11/Xatom.h>
  34. #include <X11/Xft/Xft.h>
  35. #include <X11/extensions/Xrender.h> // For XRenderColor
  36. // Needed for getenv in createFont
  37. #include <stdlib.h>
  38. XftFont *loadFont(const char *name, S32 size, Display *display)
  39. {
  40. XftFont *fontInfo = NULL;
  41. char* fontname = const_cast<char*>(name);
  42. if (dStrlen(fontname)==0)
  43. fontname = "arial";
  44. else if (stristr(const_cast<char*>(name), "arial") != NULL)
  45. fontname = "arial";
  46. else if (stristr(const_cast<char*>(name), "lucida console") != NULL)
  47. fontname = "lucida console";
  48. char* weight = "medium";
  49. char* slant = "roman"; // no slant
  50. if (stristr(const_cast<char*>(name), "bold") != NULL)
  51. weight = "bold";
  52. if (stristr(const_cast<char*>(name), "italic") != NULL)
  53. slant = "italic";
  54. int mSize = size - 2 - (int)((float)size * 0.1);
  55. char xfontName[512];
  56. // We specify a lower DPI to get 'correct' looking fonts, if we go with the
  57. // native DPI the fonts are to big and don't fit the widgets.
  58. dSprintf(xfontName, 512, "%s-%d:%s:slant=%s:dpi=76", fontname, mSize, weight, slant);
  59. // Lets see if Xft can get a font for us.
  60. char xftname[1024];
  61. fontInfo = XftFontOpenName(display, DefaultScreen(display), xfontName);
  62. // Cant find a suitabke font, default to a known font (6x10)
  63. if ( !fontInfo )
  64. {
  65. dSprintf(xfontName, 512, "6x10-%d:%s:slant=%s:dpi=76", mSize, weight, slant);
  66. fontInfo = XftFontOpenName(display, DefaultScreen(display), xfontName);
  67. }
  68. XftNameUnparse(fontInfo->pattern, xftname, 1024);
  69. #ifdef DEBUG
  70. Con::printf("Font '%s %d' mapped to '%s'\n", name, size, xftname);
  71. #endif
  72. return fontInfo;
  73. }
  74. GOldFont* createFont(const char *name, dsize_t size, U32 charset)
  75. {
  76. Display *display = XOpenDisplay(getenv("DISPLAY"));
  77. int screen;
  78. if (!display)
  79. AssertFatal(false, "createFont: cannot connect to X server");
  80. screen = DefaultScreen(display);
  81. XftFont *font = loadFont (name, size, display);
  82. if (!font) // This should almost never trigger anymore.
  83. AssertFatal(false, "createFont: cannot load font");
  84. // Create the pixmap to draw on.
  85. Pixmap pixmap = XCreatePixmap(display,
  86. DefaultRootWindow(display),
  87. font->max_advance_width,
  88. font->height,
  89. DefaultDepth(display, screen));
  90. // And the Xft wrapper around it.
  91. XftDraw *draw = XftDrawCreate(display,
  92. pixmap,
  93. DefaultVisual(display, screen),
  94. DefaultColormap(display, screen));
  95. // Allocate some colors, we don't use XftColorAllocValue here as that
  96. // Don't appear to function correctly (or I'm using it wrong) As we only do
  97. // this twice per new un cached font it isn't that big of a penalty. (Each
  98. // call to XftColorAllocName involves a round trip to the X Server)
  99. XftColor black, white;
  100. XftColorAllocName(display,
  101. DefaultVisual(display, screen),
  102. DefaultColormap(display, screen),
  103. "black",
  104. &black);
  105. // White
  106. XftColorAllocName(display,
  107. DefaultVisual(display, screen),
  108. DefaultColormap(display, screen),
  109. "white",
  110. &white);
  111. // The font.
  112. GOldFont *retFont = new GOldFont;
  113. static U8 scratchPad[65536];
  114. int x, y;
  115. // insert bitmaps into the font for each character
  116. for(U16 i = 32; i < 256; i++)
  117. {
  118. XGlyphInfo extent;
  119. FT_UInt glyph;
  120. if (!XftCharExists(display, font, i))
  121. {
  122. retFont->insertBitmap(i, scratchPad, 0, 0, 0, 0, 0, font->max_advance_width);
  123. continue;
  124. }
  125. // Get the glyph and its extents.
  126. glyph = XftCharIndex(display, font, i);
  127. XftGlyphExtents (display, font, &glyph, 1, &extent);
  128. // Clear the bounding box and draw the glyph
  129. XftDrawRect (draw, &black, 0, 0, font->max_advance_width, font->height);
  130. XftDrawGlyphs (draw, &white, font, 0, font->ascent, &glyph, 1);
  131. // Grab the rendered image ...
  132. XImage *ximage = XGetImage(display, pixmap, 0, 0,
  133. extent.xOff, font->height,
  134. AllPlanes, XYPixmap);
  135. if (ximage == NULL)
  136. AssertFatal(false, "cannot get x image");
  137. // And store each pixel in the scratchPad for insertion into the bitmap.
  138. // We grab the full height of the pixmap.
  139. for(y = 0; y < font->height; y++)
  140. {
  141. // and the width of the glyph and its padding.
  142. for(x = 0; x < extent.xOff; x++)
  143. scratchPad[y * extent.xOff + x] = static_cast<U8>(XGetPixel(ximage, x, y));
  144. }
  145. // Done with the image.
  146. XDestroyImage(ximage);
  147. // Add it to the bitmap.
  148. retFont->insertBitmap(i, // index
  149. scratchPad, // src
  150. extent.xOff, // stride
  151. extent.xOff, // width
  152. font->height, // height
  153. 0, // xOrigin
  154. font->ascent, // yOrigin
  155. extent.xOff); // xIncrement
  156. }
  157. retFont->pack(font->height, font->ascent);
  158. XftFontClose(display, font);
  159. XftColorFree(display, DefaultVisual(display, screen),
  160. DefaultColormap(display, screen), &black);
  161. XftColorFree(display, DefaultVisual(display, screen),
  162. DefaultColormap(display, screen), &white);
  163. XftDrawDestroy(draw);
  164. XFreePixmap(display, pixmap);
  165. XCloseDisplay(display);
  166. return retFont;
  167. }
  168. // XA: New class for the unix unicode font
  169. PlatformFont *createPlatformFont(const char *name, U32 size, U32 charset /* = TGE_ANSI_CHARSET */)
  170. {
  171. PlatformFont *retFont = new x86UNIXFont;
  172. if(retFont->create(name, size, charset))
  173. return retFont;
  174. delete retFont;
  175. return NULL;
  176. }
  177. x86UNIXFont::x86UNIXFont()
  178. {}
  179. x86UNIXFont::~x86UNIXFont()
  180. {}
  181. bool x86UNIXFont::create(const char *name, U32 size, U32 charset)
  182. {
  183. Display *display = XOpenDisplay(getenv("DISPLAY"));
  184. if (display == NULL)
  185. AssertFatal(false, "createFont: cannot connect to X server");
  186. XftFont *font = loadFont(name, size, display);
  187. if (!font)
  188. {
  189. Con::errorf("Error: Could not load font -%s-", name);
  190. return false;
  191. }
  192. char xfontname[1024];
  193. XftNameUnparse(font->pattern, xfontname, 1024);
  194. #ifdef DEBUG
  195. Con::printf("CreateFont: request for %s %d, using %s", name, size, xfontname);
  196. #endif
  197. // store some info about the font
  198. baseline = font->ascent;
  199. height = font->height;
  200. mFontName = StringTable->insert(xfontname);
  201. XftFontClose (display, font);
  202. // DISPLAY
  203. XCloseDisplay(display);
  204. return true;
  205. }
  206. bool x86UNIXFont::isValidChar(const UTF16 str) const
  207. {
  208. // 0x20 == 32
  209. // 0x100 == 256
  210. if( str < 0x20 || str > 0x100 )
  211. return false;
  212. return true;
  213. }
  214. bool x86UNIXFont::isValidChar(const UTF8 *str) const
  215. {
  216. return isValidChar(oneUTF32toUTF16(oneUTF8toUTF32(str,NULL)));
  217. }
  218. PlatformFont::CharInfo &x86UNIXFont::getCharInfo(const UTF16 ch) const
  219. {
  220. Display *display = XOpenDisplay(getenv("DISPLAY"));
  221. if (!display )
  222. AssertFatal(false, "createFont: cannot connect to X server");
  223. static PlatformFont::CharInfo c;
  224. dMemset(&c, 0, sizeof(c));
  225. c.bitmapIndex = 0;
  226. c.xOffset = 0;
  227. c.yOffset = 0;
  228. XftFont *fontInfo = XftFontOpenName(display, DefaultScreen(display), mFontName);
  229. if (!fontInfo)
  230. {
  231. //Try a fall back font before crashing..
  232. fontInfo = XftFontOpenName(display, DefaultScreen(display), "lucida console-10:dpi=76");
  233. if (!fontInfo)
  234. {
  235. AssertFatal(false, "createFont: cannot load font");
  236. }
  237. }
  238. int screen = DefaultScreen(display);
  239. // Create the pixmap to draw on.
  240. Drawable pixmap = XCreatePixmap(display,
  241. DefaultRootWindow(display),
  242. fontInfo->max_advance_width,
  243. fontInfo->height,
  244. DefaultDepth(display, screen));
  245. // And the Xft wrapper around it.
  246. XftDraw *draw = XftDrawCreate(display,
  247. pixmap,
  248. DefaultVisual(display, screen),
  249. DefaultColormap(display, screen));
  250. // Allocate some colors, we don't use XftColorAllocValue here as that
  251. // Don't appear to function correctly (or I'm using it wrong) As we only do
  252. // this twice per new un cached font it isn't that big of a penalty. (Each
  253. // call to XftColorAllocName involves a round trip to the X Server)
  254. XftColor black, white;
  255. XftColorAllocName(display,
  256. DefaultVisual(display, screen),
  257. DefaultColormap(display, screen),
  258. "black",
  259. &black);
  260. // White
  261. XftColorAllocName(display,
  262. DefaultVisual(display, screen),
  263. DefaultColormap(display, screen),
  264. "white",
  265. &white);
  266. XGlyphInfo charinfo;
  267. XftTextExtents16(display, fontInfo, &ch, 1, &charinfo);
  268. c.height = fontInfo->height;
  269. c.xOrigin = 0;
  270. c.yOrigin = fontInfo->ascent;
  271. c.xIncrement = charinfo.xOff;
  272. c.width = charinfo.xOff;
  273. // kick out early if the character is undrawable
  274. if( c.width == 0 || c.height == 0)
  275. return c;
  276. // allocate a greyscale bitmap and clear it.
  277. int bitmapDataSize = c.width * c.height;
  278. c.bitmapData = new U8[bitmapDataSize];
  279. dMemset(c.bitmapData, 0, bitmapDataSize);
  280. XftDrawRect (draw, &black, 0, 0, fontInfo->max_advance_width, fontInfo->height);
  281. XftDrawString16 (draw, &white, fontInfo, 0, fontInfo->ascent, &ch, 1);
  282. // grab the pixmap image
  283. XImage *ximage = XGetImage(display, pixmap, 0, 0,
  284. charinfo.xOff, fontInfo->height,
  285. AllPlanes, XYPixmap);
  286. if (!ximage)
  287. AssertFatal(false, "cannot get x image");
  288. int x, y;
  289. // grab each pixel and store it in the scratchPad
  290. for(y = 0; y < fontInfo->height; y++)
  291. {
  292. for(x = 0; x < charinfo.xOff; x++)
  293. c.bitmapData[y * charinfo.xOff + x] = static_cast<U8>(XGetPixel(ximage, x, y));
  294. }
  295. XDestroyImage(ximage);
  296. XftColorFree(display, DefaultVisual(display, screen),
  297. DefaultColormap(display, screen), &black);
  298. XftColorFree(display, DefaultVisual(display, screen),
  299. DefaultColormap(display, screen), &white);
  300. XftDrawDestroy(draw);
  301. XFreePixmap(display, pixmap);
  302. XCloseDisplay(display);
  303. return c;
  304. }
  305. PlatformFont::CharInfo &x86UNIXFont::getCharInfo(const UTF8 *str) const
  306. {
  307. return getCharInfo(oneUTF32toUTF16(oneUTF8toUTF32(str,NULL)));
  308. }