1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2013 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 #if defined HAVE_XFT || !defined HAVE_FREETYPE
43 struct font_driver ftxfont_driver
;
45 struct ftxfont_frame_data
47 /* Background and foreground colors. */
49 /* GCs interpolating the above colors. gcs[0] is for a color
50 closest to BACKGROUND, and gcs[5] is for a color closest to
53 struct ftxfont_frame_data
*next
;
57 /* Return an array of 6 GCs for antialiasing. */
60 ftxfont_get_gcs (struct frame
*f
, long unsigned int foreground
, long unsigned int background
)
65 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
66 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
70 for (this = data
; this; prev
= this, this = this->next
)
72 if (this->colors
[0].pixel
< background
)
74 if (this->colors
[0].pixel
> background
)
76 if (this->colors
[1].pixel
< foreground
)
78 if (this->colors
[1].pixel
> foreground
)
84 new = malloc (sizeof *new);
92 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
98 new->colors
[0].pixel
= background
;
99 new->colors
[1].pixel
= foreground
;
102 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
103 for (i
= 1; i
< 7; i
++)
105 /* Interpolate colors linearly. Any better algorithm? */
107 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
109 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
111 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
112 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
114 xgcv
.foreground
= color
.pixel
;
115 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
116 GCForeground
, &xgcv
);
123 for (i
--; i
>= 0; i
--)
124 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
127 prev
->next
= new->next
;
129 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
137 ftxfont_draw_bitmap (struct frame
*f
, GC gc_fore
, GC
*gcs
, struct font
*font
,
138 unsigned int code
, int x
, int y
, XPoint
*p
, int size
,
141 struct font_bitmap bitmap
;
145 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
149 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
150 i
++, b
+= bitmap
.pitch
)
152 for (j
= 0; j
< bitmap
.width
; j
++)
153 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
155 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
156 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
159 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
160 gc_fore
, p
, size
, CoordModeOrigin
);
165 if (flush
&& n
[0] > 0)
166 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
167 gc_fore
, p
, n
[0], CoordModeOrigin
);
171 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
172 i
++, b
+= bitmap
.pitch
)
174 for (j
= 0; j
< bitmap
.width
; j
++)
176 int idx
= (bitmap
.bits_per_pixel
== 1
177 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
182 XPoint
*pp
= p
+ size
* idx
;
184 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
185 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
186 if (++(n
[idx
]) == size
)
188 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
189 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
198 for (i
= 0; i
< 6; i
++)
200 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
201 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
203 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
204 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
208 if (ftfont_driver
.free_bitmap
)
209 ftfont_driver
.free_bitmap (font
, &bitmap
);
211 return bitmap
.advance
;
215 ftxfont_draw_background (struct frame
*f
, struct font
*font
, GC gc
, int x
, int y
,
220 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
221 GCForeground
| GCBackground
, &xgcv
);
222 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
223 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
224 x
, y
- FONT_BASE (font
), width
, FONT_HEIGHT (font
));
225 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
229 ftxfont_list (struct frame
*f
, Lisp_Object spec
)
231 Lisp_Object list
= ftfont_driver
.list (f
, spec
), tail
;
233 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
234 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
239 ftxfont_match (struct frame
*f
, Lisp_Object spec
)
241 Lisp_Object entity
= ftfont_driver
.match (f
, spec
);
243 if (VECTORP (entity
))
244 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
249 ftxfont_open (struct frame
*f
, Lisp_Object entity
, int pixel_size
)
251 Lisp_Object font_object
;
254 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
255 if (NILP (font_object
))
257 font
= XFONT_OBJECT (font_object
);
258 font
->driver
= &ftxfont_driver
;
263 ftxfont_close (struct frame
*f
, struct font
*font
)
265 ftfont_driver
.close (f
, font
);
269 ftxfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
,
270 bool with_background
)
272 struct frame
*f
= s
->f
;
273 struct face
*face
= s
->face
;
274 struct font
*font
= s
->font
;
283 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
287 ftxfont_draw_background (f
, font
, s
->gc
, x
, y
, s
->width
);
288 code
= alloca (sizeof (unsigned) * len
);
289 for (i
= 0; i
< len
; i
++)
290 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
291 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
293 if (face
->gc
== s
->gc
)
295 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
300 unsigned long mask
= GCForeground
| GCBackground
;
302 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
303 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
309 for (i
= 0; i
< 6; i
++)
310 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
311 s
->clip
, s
->num_clips
, Unsorted
);
313 for (i
= 0; i
< len
; i
++)
315 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
316 p
, 0x100, n
, i
+ 1 == len
);
317 x
+= (s
->padding_p
? 1 : xadvance
);
320 for (i
= 0; i
< 6; i
++)
321 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
325 /* We can't draw with antialiasing.
326 s->gc should already have a proper clipping setting. */
327 for (i
= 0; i
< len
; i
++)
329 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
330 p
, 0x700, n
, i
+ 1 == len
);
331 x
+= (s
->padding_p
? 1 : xadvance
);
341 ftxfont_end_for_frame (struct frame
*f
)
343 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
348 struct ftxfont_frame_data
*next
= data
->next
;
351 for (i
= 0; i
< 6; i
++)
352 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
357 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
364 syms_of_ftxfont (void)
366 DEFSYM (Qftx
, "ftx");
368 ftxfont_driver
= ftfont_driver
;
369 ftxfont_driver
.type
= Qftx
;
370 ftxfont_driver
.list
= ftxfont_list
;
371 ftxfont_driver
.match
= ftxfont_match
;
372 ftxfont_driver
.open
= ftxfont_open
;
373 ftxfont_driver
.close
= ftxfont_close
;
374 ftxfont_driver
.draw
= ftxfont_draw
;
375 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
376 register_font_driver (&ftxfont_driver
, NULL
);