PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ftxfont.c

https://github.com/T-force/emacs
C | 389 lines | 312 code | 46 blank | 31 comment | 57 complexity | dea728173a3e63be77c9c1f375149f43 MD5 | raw file
  1. /* ftxfont.c -- FreeType font driver on X (without using XFT).
  2. Copyright (C) 2006-2011 Free Software Foundation, Inc.
  3. Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
  4. National Institute of Advanced Industrial Science and Technology (AIST)
  5. Registration Number H13PRO009
  6. This file is part of GNU Emacs.
  7. GNU Emacs is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. GNU Emacs 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
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  17. #include <config.h>
  18. #include <stdio.h>
  19. #include <setjmp.h>
  20. #include <X11/Xlib.h>
  21. #include "lisp.h"
  22. #include "dispextern.h"
  23. #include "xterm.h"
  24. #include "frame.h"
  25. #include "blockinput.h"
  26. #include "character.h"
  27. #include "charset.h"
  28. #include "fontset.h"
  29. #include "font.h"
  30. /* FTX font driver. */
  31. static Lisp_Object Qftx;
  32. #if defined HAVE_XFT || !defined HAVE_FREETYPE
  33. static
  34. #endif
  35. struct font_driver ftxfont_driver;
  36. /* Prototypes for helper function. */
  37. static GC *ftxfont_get_gcs (FRAME_PTR, unsigned long, unsigned long);
  38. static int ftxfont_draw_bitmap (FRAME_PTR, GC, GC *, struct font *,
  39. unsigned, int, int, XPoint *, int, int *,
  40. int);
  41. static void ftxfont_draw_backgrond (FRAME_PTR, struct font *, GC,
  42. int, int, int);
  43. struct ftxfont_frame_data
  44. {
  45. /* Background and foreground colors. */
  46. XColor colors[2];
  47. /* GCs interporationg the above colors. gcs[0] is for a color
  48. closest to BACKGROUND, and gcs[5] is for a color closest to
  49. FOREGROUND. */
  50. GC gcs[6];
  51. struct ftxfont_frame_data *next;
  52. };
  53. /* Return an array of 6 GCs for antialiasing. */
  54. static GC *
  55. ftxfont_get_gcs (FRAME_PTR f, long unsigned int foreground, long unsigned int background)
  56. {
  57. XColor color;
  58. XGCValues xgcv;
  59. int i;
  60. struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
  61. struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
  62. if (data)
  63. {
  64. for (this = data; this; prev = this, this = this->next)
  65. {
  66. if (this->colors[0].pixel < background)
  67. continue;
  68. if (this->colors[0].pixel > background)
  69. break;
  70. if (this->colors[1].pixel < foreground)
  71. continue;
  72. if (this->colors[1].pixel > foreground)
  73. break;
  74. return this->gcs;
  75. }
  76. }
  77. new = malloc (sizeof (struct ftxfont_frame_data));
  78. if (! new)
  79. return NULL;
  80. new->next = this;
  81. if (prev)
  82. {
  83. prev->next = new;
  84. }
  85. else if (font_put_frame_data (f, &ftxfont_driver, new) < 0)
  86. {
  87. free (new);
  88. return NULL;
  89. }
  90. new->colors[0].pixel = background;
  91. new->colors[1].pixel = foreground;
  92. BLOCK_INPUT;
  93. XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
  94. for (i = 1; i < 7; i++)
  95. {
  96. /* Interpolate colors linearly. Any better algorithm? */
  97. color.red
  98. = (new->colors[1].red * i + new->colors[0].red * (8 - i)) / 8;
  99. color.green
  100. = (new->colors[1].green * i + new->colors[0].green * (8 - i)) / 8;
  101. color.blue
  102. = (new->colors[1].blue * i + new->colors[0].blue * (8 - i)) / 8;
  103. if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &color))
  104. break;
  105. xgcv.foreground = color.pixel;
  106. new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  107. GCForeground, &xgcv);
  108. }
  109. UNBLOCK_INPUT;
  110. if (i < 7)
  111. {
  112. BLOCK_INPUT;
  113. for (i--; i >= 0; i--)
  114. XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
  115. UNBLOCK_INPUT;
  116. if (prev)
  117. prev->next = new->next;
  118. else if (data)
  119. font_put_frame_data (f, &ftxfont_driver, new->next);
  120. free (new);
  121. return NULL;
  122. }
  123. return new->gcs;
  124. }
  125. static int
  126. ftxfont_draw_bitmap (FRAME_PTR f, GC gc_fore, GC *gcs, struct font *font, unsigned int code, int x, int y, XPoint *p, int size, int *n, int flush)
  127. {
  128. struct font_bitmap bitmap;
  129. unsigned char *b;
  130. int i, j;
  131. if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
  132. return 0;
  133. if (size > 0x100)
  134. {
  135. for (i = 0, b = bitmap.buffer; i < bitmap.rows;
  136. i++, b += bitmap.pitch)
  137. {
  138. for (j = 0; j < bitmap.width; j++)
  139. if (b[j / 8] & (1 << (7 - (j % 8))))
  140. {
  141. p[n[0]].x = x + bitmap.left + j;
  142. p[n[0]].y = y - bitmap.top + i;
  143. if (++n[0] == size)
  144. {
  145. XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  146. gc_fore, p, size, CoordModeOrigin);
  147. n[0] = 0;
  148. }
  149. }
  150. }
  151. if (flush && n[0] > 0)
  152. XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  153. gc_fore, p, n[0], CoordModeOrigin);
  154. }
  155. else
  156. {
  157. for (i = 0, b = bitmap.buffer; i < bitmap.rows;
  158. i++, b += bitmap.pitch)
  159. {
  160. for (j = 0; j < bitmap.width; j++)
  161. {
  162. int idx = (bitmap.bits_per_pixel == 1
  163. ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
  164. : (b[j] >> 5) - 1);
  165. if (idx >= 0)
  166. {
  167. XPoint *pp = p + size * idx;
  168. pp[n[idx]].x = x + bitmap.left + j;
  169. pp[n[idx]].y = y - bitmap.top + i;
  170. if (++(n[idx]) == size)
  171. {
  172. XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  173. idx == 6 ? gc_fore : gcs[idx], pp, size,
  174. CoordModeOrigin);
  175. n[idx] = 0;
  176. }
  177. }
  178. }
  179. }
  180. if (flush)
  181. {
  182. for (i = 0; i < 6; i++)
  183. if (n[i] > 0)
  184. XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  185. gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
  186. if (n[6] > 0)
  187. XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
  188. gc_fore, p + 0x600, n[6], CoordModeOrigin);
  189. }
  190. }
  191. if (ftfont_driver.free_bitmap)
  192. ftfont_driver.free_bitmap (font, &bitmap);
  193. return bitmap.advance;
  194. }
  195. static void
  196. ftxfont_draw_backgrond (FRAME_PTR f, struct font *font, GC gc, int x, int y, int width)
  197. {
  198. XGCValues xgcv;
  199. XGetGCValues (FRAME_X_DISPLAY (f), gc,
  200. GCForeground | GCBackground, &xgcv);
  201. XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
  202. XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
  203. x, y - FONT_BASE (font), width, FONT_HEIGHT (font));
  204. XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
  205. }
  206. /* Prototypes for font-driver methods. */
  207. static Lisp_Object ftxfont_list (Lisp_Object, Lisp_Object);
  208. static Lisp_Object ftxfont_match (Lisp_Object, Lisp_Object);
  209. static Lisp_Object ftxfont_open (FRAME_PTR, Lisp_Object, int);
  210. static void ftxfont_close (FRAME_PTR, struct font *);
  211. static int ftxfont_draw (struct glyph_string *, int, int, int, int, int);
  212. static Lisp_Object
  213. ftxfont_list (Lisp_Object frame, Lisp_Object spec)
  214. {
  215. Lisp_Object list = ftfont_driver.list (frame, spec), tail;
  216. for (tail = list; CONSP (tail); tail = XCDR (tail))
  217. ASET (XCAR (tail), FONT_TYPE_INDEX, Qftx);
  218. return list;
  219. }
  220. static Lisp_Object
  221. ftxfont_match (Lisp_Object frame, Lisp_Object spec)
  222. {
  223. Lisp_Object entity = ftfont_driver.match (frame, spec);
  224. if (VECTORP (entity))
  225. ASET (entity, FONT_TYPE_INDEX, Qftx);
  226. return entity;
  227. }
  228. static Lisp_Object
  229. ftxfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size)
  230. {
  231. Lisp_Object font_object;
  232. struct font *font;
  233. font_object = ftfont_driver.open (f, entity, pixel_size);
  234. if (NILP (font_object))
  235. return Qnil;
  236. font = XFONT_OBJECT (font_object);
  237. font->driver = &ftxfont_driver;
  238. return font_object;
  239. }
  240. static void
  241. ftxfont_close (FRAME_PTR f, struct font *font)
  242. {
  243. ftfont_driver.close (f, font);
  244. }
  245. static int
  246. ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y, int with_background)
  247. {
  248. FRAME_PTR f = s->f;
  249. struct face *face = s->face;
  250. struct font *font = s->font;
  251. XPoint p[0x700];
  252. int n[7];
  253. unsigned *code;
  254. int len = to - from;
  255. int i;
  256. GC *gcs;
  257. int xadvance;
  258. n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
  259. BLOCK_INPUT;
  260. if (with_background)
  261. ftxfont_draw_backgrond (f, font, s->gc, x, y, s->width);
  262. code = alloca (sizeof (unsigned) * len);
  263. for (i = 0; i < len; i++)
  264. code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
  265. | XCHAR2B_BYTE2 (s->char2b + from + i));
  266. if (face->gc == s->gc)
  267. {
  268. gcs = ftxfont_get_gcs (f, face->foreground, face->background);
  269. }
  270. else
  271. {
  272. XGCValues xgcv;
  273. unsigned long mask = GCForeground | GCBackground;
  274. XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
  275. gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
  276. }
  277. if (gcs)
  278. {
  279. if (s->num_clips)
  280. for (i = 0; i < 6; i++)
  281. XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
  282. s->clip, s->num_clips, Unsorted);
  283. for (i = 0; i < len; i++)
  284. {
  285. xadvance = ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
  286. p, 0x100, n, i + 1 == len);
  287. x += (s->padding_p ? 1 : xadvance);
  288. }
  289. if (s->num_clips)
  290. for (i = 0; i < 6; i++)
  291. XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
  292. }
  293. else
  294. {
  295. /* We can't draw with antialiasing.
  296. s->gc should already have a proper clipping setting. */
  297. for (i = 0; i < len; i++)
  298. {
  299. xadvance = ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
  300. p, 0x700, n, i + 1 == len);
  301. x += (s->padding_p ? 1 : xadvance);
  302. }
  303. }
  304. UNBLOCK_INPUT;
  305. return len;
  306. }
  307. static int
  308. ftxfont_end_for_frame (FRAME_PTR f)
  309. {
  310. struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
  311. BLOCK_INPUT;
  312. while (data)
  313. {
  314. struct ftxfont_frame_data *next = data->next;
  315. int i;
  316. for (i = 0; i < 6; i++)
  317. XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
  318. free (data);
  319. data = next;
  320. }
  321. UNBLOCK_INPUT;
  322. font_put_frame_data (f, &ftxfont_driver, NULL);
  323. return 0;
  324. }
  325. void
  326. syms_of_ftxfont (void)
  327. {
  328. DEFSYM (Qftx, "ftx");
  329. ftxfont_driver = ftfont_driver;
  330. ftxfont_driver.type = Qftx;
  331. ftxfont_driver.list = ftxfont_list;
  332. ftxfont_driver.match = ftxfont_match;
  333. ftxfont_driver.open = ftxfont_open;
  334. ftxfont_driver.close = ftxfont_close;
  335. ftxfont_driver.draw = ftxfont_draw;
  336. ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
  337. register_font_driver (&ftxfont_driver, NULL);
  338. }