1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2015 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/>. */
29 #include "blockinput.h"
32 /* FTX font driver. */
34 struct font_driver ftxfont_driver
;
36 struct ftxfont_frame_data
38 /* Background and foreground colors. */
40 /* GCs interpolating the above colors. gcs[0] is for a color
41 closest to BACKGROUND, and gcs[5] is for a color closest to
44 struct ftxfont_frame_data
*next
;
48 /* Return an array of 6 GCs for antialiasing. */
51 ftxfont_get_gcs (struct frame
*f
, unsigned long foreground
, unsigned long background
)
56 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, Qftx
);
57 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
61 for (this = data
; this; prev
= this, this = this->next
)
63 if (this->colors
[0].pixel
< background
)
65 if (this->colors
[0].pixel
> background
)
67 if (this->colors
[1].pixel
< foreground
)
69 if (this->colors
[1].pixel
> foreground
)
75 new = xmalloc (sizeof *new);
79 font_put_frame_data (f
, Qftx
, new);
81 new->colors
[0].pixel
= background
;
82 new->colors
[1].pixel
= foreground
;
85 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
86 for (i
= 1; i
< 7; i
++)
88 /* Interpolate colors linearly. Any better algorithm? */
90 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
92 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
94 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
95 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
97 xgcv
.foreground
= color
.pixel
;
98 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
106 for (i
--; i
>= 0; i
--)
107 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
110 prev
->next
= new->next
;
112 font_put_frame_data (f
, Qftx
, new->next
);
120 ftxfont_draw_bitmap (struct frame
*f
, GC gc_fore
, GC
*gcs
, struct font
*font
,
121 unsigned int code
, int x
, int y
, XPoint
*p
, int size
,
124 struct font_bitmap bitmap
;
128 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
132 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
133 i
++, b
+= bitmap
.pitch
)
135 for (j
= 0; j
< bitmap
.width
; j
++)
136 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
138 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
139 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
142 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
143 gc_fore
, p
, size
, CoordModeOrigin
);
148 if (flush
&& n
[0] > 0)
149 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
150 gc_fore
, p
, n
[0], CoordModeOrigin
);
154 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
155 i
++, b
+= bitmap
.pitch
)
157 for (j
= 0; j
< bitmap
.width
; j
++)
159 int idx
= (bitmap
.bits_per_pixel
== 1
160 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
165 XPoint
*pp
= p
+ size
* idx
;
167 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
168 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
169 if (++(n
[idx
]) == size
)
171 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
172 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
181 for (i
= 0; i
< 6; i
++)
183 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
184 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
186 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
187 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
191 if (ftfont_driver
.free_bitmap
)
192 ftfont_driver
.free_bitmap (font
, &bitmap
);
194 return bitmap
.advance
;
198 ftxfont_draw_background (struct frame
*f
, struct font
*font
, GC gc
, int x
, int y
,
203 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
204 GCForeground
| GCBackground
, &xgcv
);
205 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
206 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
207 x
, y
- FONT_BASE (font
), width
, FONT_HEIGHT (font
));
208 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
212 ftxfont_list (struct frame
*f
, Lisp_Object spec
)
214 Lisp_Object list
= ftfont_driver
.list (f
, spec
), tail
;
216 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
217 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
222 ftxfont_match (struct frame
*f
, Lisp_Object spec
)
224 Lisp_Object entity
= ftfont_driver
.match (f
, spec
);
226 if (VECTORP (entity
))
227 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
232 ftxfont_open (struct frame
*f
, Lisp_Object entity
, int pixel_size
)
234 Lisp_Object font_object
;
237 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
238 if (NILP (font_object
))
240 font
= XFONT_OBJECT (font_object
);
241 font
->driver
= &ftxfont_driver
;
246 ftxfont_close (struct font
*font
)
248 ftfont_driver
.close (font
);
252 ftxfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
,
253 bool with_background
)
255 struct frame
*f
= s
->f
;
256 struct face
*face
= s
->face
;
257 struct font
*font
= s
->font
;
266 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
269 SAFE_NALLOCA (code
, 1, len
);
272 ftxfont_draw_background (f
, font
, s
->gc
, x
, y
, s
->width
);
273 for (i
= 0; i
< len
; i
++)
274 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
275 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
277 if (face
->gc
== s
->gc
)
279 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
284 unsigned long mask
= GCForeground
| GCBackground
;
286 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
287 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
293 for (i
= 0; i
< 6; i
++)
294 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
295 s
->clip
, s
->num_clips
, Unsorted
);
297 for (i
= 0; i
< len
; i
++)
299 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
300 p
, 0x100, n
, i
+ 1 == len
);
301 x
+= (s
->padding_p
? 1 : xadvance
);
304 for (i
= 0; i
< 6; i
++)
305 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
309 /* We can't draw with antialiasing.
310 s->gc should already have a proper clipping setting. */
311 for (i
= 0; i
< len
; i
++)
313 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
314 p
, 0x700, n
, i
+ 1 == len
);
315 x
+= (s
->padding_p
? 1 : xadvance
);
326 ftxfont_end_for_frame (struct frame
*f
)
328 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, Qftx
);
333 struct ftxfont_frame_data
*next
= data
->next
;
336 for (i
= 0; i
< 6; i
++)
337 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
342 font_put_frame_data (f
, Qftx
, NULL
);
349 syms_of_ftxfont (void)
351 DEFSYM (Qftx
, "ftx");
353 ftxfont_driver
= ftfont_driver
;
354 ftxfont_driver
.type
= Qftx
;
355 ftxfont_driver
.list
= ftxfont_list
;
356 ftxfont_driver
.match
= ftxfont_match
;
357 ftxfont_driver
.open
= ftxfont_open
;
358 ftxfont_driver
.close
= ftxfont_close
;
359 ftxfont_driver
.draw
= ftxfont_draw
;
360 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
361 register_font_driver (&ftxfont_driver
, NULL
);