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
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 #include "dispextern.h"
31 #include "blockinput.h"
32 #include "character.h"
37 /* FTX font driver. */
39 static Lisp_Object Qftx
;
41 #if defined HAVE_XFT || !defined HAVE_FREETYPE
44 struct font_driver ftxfont_driver
;
46 /* Prototypes for helper function. */
47 static GC
*ftxfont_get_gcs (FRAME_PTR
, unsigned long, unsigned long);
48 static int ftxfont_draw_bitmap (FRAME_PTR
, GC
, GC
*, struct font
*,
49 unsigned, int, int, XPoint
*, int, int *,
51 static void ftxfont_draw_backgrond (FRAME_PTR
, struct font
*, GC
,
54 struct ftxfont_frame_data
56 /* Background and foreground colors. */
58 /* GCs interporationg the above colors. gcs[0] is for a color
59 closest to BACKGROUND, and gcs[5] is for a color closest to
62 struct ftxfont_frame_data
*next
;
66 /* Return an array of 6 GCs for antialiasing. */
69 ftxfont_get_gcs (FRAME_PTR f
, long unsigned int foreground
, long unsigned int background
)
74 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
75 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
79 for (this = data
; this; prev
= this, this = this->next
)
81 if (this->colors
[0].pixel
< background
)
83 if (this->colors
[0].pixel
> background
)
85 if (this->colors
[1].pixel
< foreground
)
87 if (this->colors
[1].pixel
> foreground
)
93 new = malloc (sizeof (struct ftxfont_frame_data
));
101 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
107 new->colors
[0].pixel
= background
;
108 new->colors
[1].pixel
= foreground
;
111 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
112 for (i
= 1; i
< 7; i
++)
114 /* Interpolate colors linearly. Any better algorithm? */
116 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
118 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
120 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
121 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
123 xgcv
.foreground
= color
.pixel
;
124 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
125 GCForeground
, &xgcv
);
132 for (i
--; i
>= 0; i
--)
133 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
136 prev
->next
= new->next
;
138 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
146 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
)
148 struct font_bitmap bitmap
;
152 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
156 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
157 i
++, b
+= bitmap
.pitch
)
159 for (j
= 0; j
< bitmap
.width
; j
++)
160 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
162 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
163 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
166 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
167 gc_fore
, p
, size
, CoordModeOrigin
);
172 if (flush
&& n
[0] > 0)
173 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
174 gc_fore
, p
, n
[0], CoordModeOrigin
);
178 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
179 i
++, b
+= bitmap
.pitch
)
181 for (j
= 0; j
< bitmap
.width
; j
++)
183 int idx
= (bitmap
.bits_per_pixel
== 1
184 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
189 XPoint
*pp
= p
+ size
* idx
;
191 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
192 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
193 if (++(n
[idx
]) == size
)
195 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
196 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
205 for (i
= 0; i
< 6; i
++)
207 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
208 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
210 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
211 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
215 if (ftfont_driver
.free_bitmap
)
216 ftfont_driver
.free_bitmap (font
, &bitmap
);
218 return bitmap
.advance
;
222 ftxfont_draw_backgrond (FRAME_PTR f
, struct font
*font
, GC gc
, int x
, int y
, int width
)
226 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
227 GCForeground
| GCBackground
, &xgcv
);
228 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
229 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
230 x
, y
- FONT_BASE (font
), width
, FONT_HEIGHT (font
));
231 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
234 /* Prototypes for font-driver methods. */
235 static Lisp_Object
ftxfont_list (Lisp_Object
, Lisp_Object
);
236 static Lisp_Object
ftxfont_match (Lisp_Object
, Lisp_Object
);
237 static Lisp_Object
ftxfont_open (FRAME_PTR
, Lisp_Object
, int);
238 static void ftxfont_close (FRAME_PTR
, struct font
*);
239 static int ftxfont_draw (struct glyph_string
*, int, int, int, int, int);
242 ftxfont_list (Lisp_Object frame
, Lisp_Object spec
)
244 Lisp_Object list
= ftfont_driver
.list (frame
, spec
), tail
;
246 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
247 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
252 ftxfont_match (Lisp_Object frame
, Lisp_Object spec
)
254 Lisp_Object entity
= ftfont_driver
.match (frame
, spec
);
256 if (VECTORP (entity
))
257 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
262 ftxfont_open (FRAME_PTR f
, Lisp_Object entity
, int pixel_size
)
264 Lisp_Object font_object
;
267 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
268 if (NILP (font_object
))
270 font
= XFONT_OBJECT (font_object
);
271 font
->driver
= &ftxfont_driver
;
276 ftxfont_close (FRAME_PTR f
, struct font
*font
)
278 ftfont_driver
.close (f
, font
);
282 ftxfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
, int with_background
)
285 struct face
*face
= s
->face
;
286 struct font
*font
= s
->font
;
295 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
299 ftxfont_draw_backgrond (f
, font
, s
->gc
, x
, y
, s
->width
);
300 code
= alloca (sizeof (unsigned) * len
);
301 for (i
= 0; i
< len
; i
++)
302 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
303 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
305 if (face
->gc
== s
->gc
)
307 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
312 unsigned long mask
= GCForeground
| GCBackground
;
314 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
315 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
321 for (i
= 0; i
< 6; i
++)
322 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
323 s
->clip
, s
->num_clips
, Unsorted
);
325 for (i
= 0; i
< len
; i
++)
327 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
328 p
, 0x100, n
, i
+ 1 == len
);
329 x
+= (s
->padding_p
? 1 : xadvance
);
332 for (i
= 0; i
< 6; i
++)
333 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
337 /* We can't draw with antialiasing.
338 s->gc should already have a proper clipping setting. */
339 for (i
= 0; i
< len
; i
++)
341 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
342 p
, 0x700, n
, i
+ 1 == len
);
343 x
+= (s
->padding_p
? 1 : xadvance
);
353 ftxfont_end_for_frame (FRAME_PTR f
)
355 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
360 struct ftxfont_frame_data
*next
= data
->next
;
363 for (i
= 0; i
< 6; i
++)
364 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
369 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
376 syms_of_ftxfont (void)
378 DEFSYM (Qftx
, "ftx");
380 ftxfont_driver
= ftfont_driver
;
381 ftxfont_driver
.type
= Qftx
;
382 ftxfont_driver
.list
= ftxfont_list
;
383 ftxfont_driver
.match
= ftxfont_match
;
384 ftxfont_driver
.open
= ftxfont_open
;
385 ftxfont_driver
.close
= ftxfont_close
;
386 ftxfont_driver
.draw
= ftxfont_draw
;
387 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
388 register_font_driver (&ftxfont_driver
, NULL
);