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 /* Prototypes for helper function. */
42 static GC
*ftxfont_get_gcs (FRAME_PTR
, unsigned long, unsigned long);
43 static int ftxfont_draw_bitmap (FRAME_PTR
, GC
, GC
*, struct font
*,
44 unsigned, int, int, XPoint
*, int, int *,
46 static void ftxfont_draw_backgrond (FRAME_PTR
, struct font
*, GC
,
49 struct ftxfont_frame_data
51 /* Background and foreground colors. */
53 /* GCs interporationg the above colors. gcs[0] is for a color
54 closest to BACKGROUND, and gcs[5] is for a color closest to
57 struct ftxfont_frame_data
*next
;
61 /* Return an array of 6 GCs for antialiasing. */
64 ftxfont_get_gcs (FRAME_PTR f
, long unsigned int foreground
, long unsigned int background
)
69 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
70 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
74 for (this = data
; this; prev
= this, this = this->next
)
76 if (this->colors
[0].pixel
< background
)
78 if (this->colors
[0].pixel
> background
)
80 if (this->colors
[1].pixel
< foreground
)
82 if (this->colors
[1].pixel
> foreground
)
88 new = malloc (sizeof (struct ftxfont_frame_data
));
96 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
102 new->colors
[0].pixel
= background
;
103 new->colors
[1].pixel
= foreground
;
106 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
107 for (i
= 1; i
< 7; i
++)
109 /* Interpolate colors linearly. Any better algorithm? */
111 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
113 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
115 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
116 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
118 xgcv
.foreground
= color
.pixel
;
119 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
120 GCForeground
, &xgcv
);
127 for (i
--; i
>= 0; i
--)
128 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
131 prev
->next
= new->next
;
133 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
141 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
)
143 struct font_bitmap bitmap
;
147 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
151 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
152 i
++, b
+= bitmap
.pitch
)
154 for (j
= 0; j
< bitmap
.width
; j
++)
155 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
157 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
158 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
161 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
162 gc_fore
, p
, size
, CoordModeOrigin
);
167 if (flush
&& n
[0] > 0)
168 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
169 gc_fore
, p
, n
[0], CoordModeOrigin
);
173 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
174 i
++, b
+= bitmap
.pitch
)
176 for (j
= 0; j
< bitmap
.width
; j
++)
178 int idx
= (bitmap
.bits_per_pixel
== 1
179 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
184 XPoint
*pp
= p
+ size
* idx
;
186 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
187 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
188 if (++(n
[idx
]) == size
)
190 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
191 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
200 for (i
= 0; i
< 6; i
++)
202 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
203 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
205 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
206 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
210 if (ftfont_driver
.free_bitmap
)
211 ftfont_driver
.free_bitmap (font
, &bitmap
);
213 return bitmap
.advance
;
217 ftxfont_draw_backgrond (FRAME_PTR f
, struct font
*font
, GC gc
, int x
, int y
, int width
)
221 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
222 GCForeground
| GCBackground
, &xgcv
);
223 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
224 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
225 x
, y
- FONT_BASE (font
), width
, FONT_HEIGHT (font
));
226 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
229 /* Prototypes for font-driver methods. */
230 static Lisp_Object
ftxfont_list (Lisp_Object
, Lisp_Object
);
231 static Lisp_Object
ftxfont_match (Lisp_Object
, Lisp_Object
);
232 static Lisp_Object
ftxfont_open (FRAME_PTR
, Lisp_Object
, int);
233 static void ftxfont_close (FRAME_PTR
, struct font
*);
234 static int ftxfont_draw (struct glyph_string
*, int, int, int, int, int);
236 struct font_driver ftxfont_driver
;
239 ftxfont_list (Lisp_Object frame
, Lisp_Object spec
)
241 Lisp_Object list
= ftfont_driver
.list (frame
, spec
), tail
;
243 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
244 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
249 ftxfont_match (Lisp_Object frame
, Lisp_Object spec
)
251 Lisp_Object entity
= ftfont_driver
.match (frame
, spec
);
253 if (VECTORP (entity
))
254 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
259 ftxfont_open (FRAME_PTR f
, Lisp_Object entity
, int pixel_size
)
261 Lisp_Object font_object
;
264 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
265 if (NILP (font_object
))
267 font
= XFONT_OBJECT (font_object
);
268 font
->driver
= &ftxfont_driver
;
273 ftxfont_close (FRAME_PTR f
, struct font
*font
)
275 ftfont_driver
.close (f
, font
);
279 ftxfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
, int with_background
)
282 struct face
*face
= s
->face
;
283 struct font
*font
= s
->font
;
292 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
296 ftxfont_draw_backgrond (f
, font
, s
->gc
, x
, y
, s
->width
);
297 code
= alloca (sizeof (unsigned) * len
);
298 for (i
= 0; i
< len
; i
++)
299 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
300 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
302 if (face
->gc
== s
->gc
)
304 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
309 unsigned long mask
= GCForeground
| GCBackground
;
311 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
312 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
318 for (i
= 0; i
< 6; i
++)
319 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
320 s
->clip
, s
->num_clips
, Unsorted
);
322 for (i
= 0; i
< len
; i
++)
324 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
325 p
, 0x100, n
, i
+ 1 == len
);
326 x
+= (s
->padding_p
? 1 : xadvance
);
329 for (i
= 0; i
< 6; i
++)
330 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
334 /* We can't draw with antialiasing.
335 s->gc should already have a proper clipping setting. */
336 for (i
= 0; i
< len
; i
++)
338 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
339 p
, 0x700, n
, i
+ 1 == len
);
340 x
+= (s
->padding_p
? 1 : xadvance
);
350 ftxfont_end_for_frame (FRAME_PTR f
)
352 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
357 struct ftxfont_frame_data
*next
= data
->next
;
360 for (i
= 0; i
< 6; i
++)
361 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
366 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
373 syms_of_ftxfont (void)
375 DEFSYM (Qftx
, "ftx");
377 ftxfont_driver
= ftfont_driver
;
378 ftxfont_driver
.type
= Qftx
;
379 ftxfont_driver
.list
= ftxfont_list
;
380 ftxfont_driver
.match
= ftxfont_match
;
381 ftxfont_driver
.open
= ftxfont_open
;
382 ftxfont_driver
.close
= ftxfont_close
;
383 ftxfont_driver
.draw
= ftxfont_draw
;
384 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
385 register_font_driver (&ftxfont_driver
, NULL
);