1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
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, or (at your option)
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; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
29 #include "dispextern.h"
32 #include "blockinput.h"
33 #include "character.h"
38 /* FTX font driver. */
40 static Lisp_Object Qftx
;
42 /* Prototypes for helper function. */
43 static GC
*ftxfont_get_gcs
P_ ((FRAME_PTR
, unsigned long, unsigned long));
44 static int ftxfont_draw_bitmap
P_ ((FRAME_PTR
, GC
, GC
*, struct font
*,
45 unsigned, int, int, XPoint
*, int, int *,
47 static void ftxfont_draw_backgrond
P_ ((FRAME_PTR
, struct font
*, GC
,
50 struct ftxfont_frame_data
52 /* Background and foreground colors. */
54 /* GCs interporationg the above colors. gcs[0] is for a color
55 closest to BACKGROUND, and gcs[5] is for a color closest to
58 struct ftxfont_frame_data
*next
;
62 /* Return an array of 6 GCs for antialiasing. */
65 ftxfont_get_gcs (f
, foreground
, background
)
67 unsigned long foreground
, background
;
72 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
73 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
77 for (this = data
; this; prev
= this, this = this->next
)
79 if (this->colors
[0].pixel
< background
)
81 if (this->colors
[0].pixel
> background
)
83 if (this->colors
[1].pixel
< foreground
)
85 if (this->colors
[1].pixel
> foreground
)
91 new = malloc (sizeof (struct ftxfont_frame_data
));
99 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
105 new->colors
[0].pixel
= background
;
106 new->colors
[1].pixel
= foreground
;
109 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
110 for (i
= 1; i
< 7; i
++)
112 /* Interpolate colors linearly. Any better algorithm? */
114 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
116 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
118 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
119 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
121 xgcv
.foreground
= color
.pixel
;
122 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
123 GCForeground
, &xgcv
);
130 for (i
--; i
>= 0; i
--)
131 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
134 prev
->next
= new->next
;
136 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
144 ftxfont_draw_bitmap (f
, gc_fore
, gcs
, font
, code
, x
, y
, p
, size
, n
, flush
)
154 struct font_bitmap bitmap
;
158 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
162 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
163 i
++, b
+= bitmap
.pitch
)
165 for (j
= 0; j
< bitmap
.width
; j
++)
166 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
168 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
169 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
172 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
173 gc_fore
, p
, size
, CoordModeOrigin
);
178 if (flush
&& n
[0] > 0)
179 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
180 gc_fore
, p
, n
[0], CoordModeOrigin
);
184 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
185 i
++, b
+= bitmap
.pitch
)
187 for (j
= 0; j
< bitmap
.width
; j
++)
189 int idx
= (bitmap
.bits_per_pixel
== 1
190 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
195 XPoint
*pp
= p
+ size
* idx
;
197 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
198 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
199 if (++(n
[idx
]) == size
)
201 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
202 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
211 for (i
= 0; i
< 6; i
++)
213 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
214 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
216 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
217 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
221 if (ftfont_driver
.free_bitmap
)
222 ftfont_driver
.free_bitmap (font
, &bitmap
);
224 return bitmap
.advance
;
228 ftxfont_draw_backgrond (f
, font
, gc
, x
, y
, width
)
236 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
237 GCForeground
| GCBackground
, &xgcv
);
238 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
239 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
240 x
, y
- font
->ascent
, width
, y
+ font
->descent
);
241 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
244 /* Prototypes for font-driver methods. */
245 static Lisp_Object ftxfont_list
P_ ((Lisp_Object
, Lisp_Object
));
246 static Lisp_Object ftxfont_match
P_ ((Lisp_Object
, Lisp_Object
));
247 static struct font
*ftxfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
248 static void ftxfont_close
P_ ((FRAME_PTR
, struct font
*));
249 static int ftxfont_draw
P_ ((struct glyph_string
*, int, int, int, int, int));
251 struct font_driver ftxfont_driver
;
254 ftxfont_list (frame
, spec
)
258 Lisp_Object val
= ftfont_driver
.list (frame
, spec
);
264 for (i
= 0; i
< ASIZE (val
); i
++)
265 ASET (AREF (val
, i
), FONT_TYPE_INDEX
, Qftx
);
271 ftxfont_match (frame
, spec
)
275 Lisp_Object entity
= ftfont_driver
.match (frame
, spec
);
277 if (VECTORP (entity
))
278 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
283 ftxfont_open (f
, entity
, pixel_size
)
288 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
290 XFontStruct
*xfont
= malloc (sizeof (XFontStruct
));
294 font
= ftfont_driver
.open (f
, entity
, pixel_size
);
300 xfont
->fid
= (Font
) 0;
301 xfont
->ascent
= font
->ascent
;
302 xfont
->descent
= font
->descent
;
303 xfont
->max_bounds
.width
= font
->font
.size
;
304 xfont
->min_bounds
.width
= font
->min_width
;
305 font
->font
.font
= xfont
;
306 font
->driver
= &ftxfont_driver
;
310 /* Set global flag fonts_changed_p to non-zero if the font loaded
311 has a character with a smaller width than any other character
312 before, or if the font loaded has a smaller height than any other
313 font loaded before. If this happens, it will make a glyph matrix
314 reallocation necessary. */
315 if (dpyinfo
->n_fonts
== 1)
317 dpyinfo
->smallest_font_height
= font
->font
.height
;
318 dpyinfo
->smallest_char_width
= font
->min_width
;
323 if (dpyinfo
->smallest_font_height
> font
->font
.height
)
324 dpyinfo
->smallest_font_height
= font
->font
.height
, fonts_changed_p
|= 1;
325 if (dpyinfo
->smallest_char_width
> font
->min_width
)
326 dpyinfo
->smallest_char_width
= font
->min_width
, fonts_changed_p
|= 1;
331 if (dpyinfo
->smallest_font_height
== 0)
332 dpyinfo
->smallest_font_height
= 1;
333 if (dpyinfo
->smallest_char_width
== 0)
334 dpyinfo
->smallest_char_width
= 1;
341 ftxfont_close (f
, font
)
345 ftfont_driver
.close (f
, font
);
346 FRAME_X_DISPLAY_INFO (f
)->n_fonts
--;
350 ftxfont_draw (s
, from
, to
, x
, y
, with_background
)
351 struct glyph_string
*s
;
352 int from
, to
, x
, y
, with_background
;
355 struct face
*face
= s
->face
;
356 struct font
*font
= (struct font
*) s
->font_info
;
365 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
369 ftxfont_draw_backgrond (f
, font
, s
->gc
, x
, y
, s
->width
);
370 code
= alloca (sizeof (unsigned) * len
);
371 for (i
= 0; i
< len
; i
++)
372 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
373 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
375 if (face
->gc
== s
->gc
)
377 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
382 unsigned long mask
= GCForeground
| GCBackground
;
384 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
385 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
391 for (i
= 0; i
< 6; i
++)
392 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
393 s
->clip
, s
->num_clips
, Unsorted
);
395 for (i
= 0; i
< len
; i
++)
397 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
398 p
, 0x100, n
, i
+ 1 == len
);
399 x
+= (s
->padding_p
? 1 : xadvance
);
402 for (i
= 0; i
< 6; i
++)
403 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
407 /* We can't draw with antialiasing.
408 s->gc should already have a proper clipping setting. */
409 for (i
= 0; i
< len
; i
++)
411 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
412 p
, 0x700, n
, i
+ 1 == len
);
413 x
+= (s
->padding_p
? 1 : xadvance
);
423 ftxfont_end_for_frame (f
)
426 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
431 struct ftxfont_frame_data
*next
= data
->next
;
434 for (i
= 0; i
< 6; i
++)
435 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
440 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
449 DEFSYM (Qftx
, "ftx");
451 ftxfont_driver
= ftfont_driver
;
452 ftxfont_driver
.type
= Qftx
;
453 ftxfont_driver
.list
= ftxfont_list
;
454 ftxfont_driver
.match
= ftxfont_match
;
455 ftxfont_driver
.open
= ftxfont_open
;
456 ftxfont_driver
.close
= ftxfont_close
;
457 ftxfont_driver
.draw
= ftxfont_draw
;
458 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
459 register_font_driver (&ftxfont_driver
, NULL
);
462 /* arch-tag: 59bd3469-5330-413f-b29d-1aa36492abe8
463 (do not change this comment) */