1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2014 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/>. */
27 #include "dispextern.h"
30 #include "blockinput.h"
31 #include "character.h"
36 /* FTX font driver. */
38 static Lisp_Object Qftx
;
40 struct font_driver ftxfont_driver
;
42 struct ftxfont_frame_data
44 /* Background and foreground colors. */
46 /* GCs interpolating the above colors. gcs[0] is for a color
47 closest to BACKGROUND, and gcs[5] is for a color closest to
50 struct ftxfont_frame_data
*next
;
54 /* Return an array of 6 GCs for antialiasing. */
57 ftxfont_get_gcs (struct frame
*f
, unsigned long foreground
, unsigned long background
)
62 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
63 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
67 for (this = data
; this; prev
= this, this = this->next
)
69 if (this->colors
[0].pixel
< background
)
71 if (this->colors
[0].pixel
> background
)
73 if (this->colors
[1].pixel
< foreground
)
75 if (this->colors
[1].pixel
> foreground
)
81 new = xmalloc_unsafe (sizeof *new);
89 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
95 new->colors
[0].pixel
= background
;
96 new->colors
[1].pixel
= foreground
;
99 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
100 for (i
= 1; i
< 7; i
++)
102 /* Interpolate colors linearly. Any better algorithm? */
104 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
106 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
108 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
109 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
111 xgcv
.foreground
= color
.pixel
;
112 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
113 GCForeground
, &xgcv
);
120 for (i
--; i
>= 0; i
--)
121 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
124 prev
->next
= new->next
;
126 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
134 ftxfont_draw_bitmap (struct frame
*f
, GC gc_fore
, GC
*gcs
, struct font
*font
,
135 unsigned int code
, int x
, int y
, XPoint
*p
, int size
,
138 struct font_bitmap bitmap
;
142 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
146 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
147 i
++, b
+= bitmap
.pitch
)
149 for (j
= 0; j
< bitmap
.width
; j
++)
150 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
152 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
153 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
156 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
157 gc_fore
, p
, size
, CoordModeOrigin
);
162 if (flush
&& n
[0] > 0)
163 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
164 gc_fore
, p
, n
[0], CoordModeOrigin
);
168 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
169 i
++, b
+= bitmap
.pitch
)
171 for (j
= 0; j
< bitmap
.width
; j
++)
173 int idx
= (bitmap
.bits_per_pixel
== 1
174 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
179 XPoint
*pp
= p
+ size
* idx
;
181 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
182 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
183 if (++(n
[idx
]) == size
)
185 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
186 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
195 for (i
= 0; i
< 6; i
++)
197 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
198 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
200 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
201 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
205 if (ftfont_driver
.free_bitmap
)
206 ftfont_driver
.free_bitmap (font
, &bitmap
);
208 return bitmap
.advance
;
212 ftxfont_draw_background (struct frame
*f
, struct font
*font
, GC gc
, int x
, int y
,
217 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
218 GCForeground
| GCBackground
, &xgcv
);
219 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
220 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
221 x
, y
- FONT_BASE (font
), width
, FONT_HEIGHT (font
));
222 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
226 ftxfont_list (struct frame
*f
, Lisp_Object spec
)
228 Lisp_Object list
= ftfont_driver
.list (f
, spec
), tail
;
230 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
231 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
236 ftxfont_match (struct frame
*f
, Lisp_Object spec
)
238 Lisp_Object entity
= ftfont_driver
.match (f
, spec
);
240 if (VECTORP (entity
))
241 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
246 ftxfont_open (struct frame
*f
, Lisp_Object entity
, int pixel_size
)
248 Lisp_Object font_object
;
251 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
252 if (NILP (font_object
))
254 font
= XFONT_OBJECT (font_object
);
255 font
->driver
= &ftxfont_driver
;
260 ftxfont_close (struct font
*font
)
262 ftfont_driver
.close (font
);
266 ftxfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
,
267 bool with_background
)
269 struct frame
*f
= s
->f
;
270 struct face
*face
= s
->face
;
271 struct font
*font
= s
->font
;
280 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
284 ftxfont_draw_background (f
, font
, s
->gc
, x
, y
, s
->width
);
285 code
= alloca (sizeof (unsigned) * len
);
286 for (i
= 0; i
< len
; i
++)
287 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
288 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
290 if (face
->gc
== s
->gc
)
292 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
297 unsigned long mask
= GCForeground
| GCBackground
;
299 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
300 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
306 for (i
= 0; i
< 6; i
++)
307 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
308 s
->clip
, s
->num_clips
, Unsorted
);
310 for (i
= 0; i
< len
; i
++)
312 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
313 p
, 0x100, n
, i
+ 1 == len
);
314 x
+= (s
->padding_p
? 1 : xadvance
);
317 for (i
= 0; i
< 6; i
++)
318 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
322 /* We can't draw with antialiasing.
323 s->gc should already have a proper clipping setting. */
324 for (i
= 0; i
< len
; i
++)
326 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
327 p
, 0x700, n
, i
+ 1 == len
);
328 x
+= (s
->padding_p
? 1 : xadvance
);
338 ftxfont_end_for_frame (struct frame
*f
)
340 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
345 struct ftxfont_frame_data
*next
= data
->next
;
348 for (i
= 0; i
< 6; i
++)
349 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
354 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
361 syms_of_ftxfont (void)
363 DEFSYM (Qftx
, "ftx");
365 ftxfont_driver
= ftfont_driver
;
366 ftxfont_driver
.type
= Qftx
;
367 ftxfont_driver
.list
= ftxfont_list
;
368 ftxfont_driver
.match
= ftxfont_match
;
369 ftxfont_driver
.open
= ftxfont_open
;
370 ftxfont_driver
.close
= ftxfont_close
;
371 ftxfont_driver
.draw
= ftxfont_draw
;
372 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
373 register_font_driver (&ftxfont_driver
, NULL
);