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 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 /* Prototypes for helper function. */
41 static GC
*ftxfont_get_gcs
P_ ((FRAME_PTR
, unsigned long, unsigned long));
42 static int ftxfont_draw_bitmap
P_ ((FRAME_PTR
, GC
, GC
*, struct font
*,
43 unsigned, int, int, XPoint
*, int, int *,
45 static void ftxfont_draw_backgrond
P_ ((FRAME_PTR
, struct font
*, GC
,
48 struct ftxfont_frame_data
50 /* Background and foreground colors. */
52 /* GCs interporationg the above colors. gcs[0] is for a color
53 closest to BACKGROUND, and gcs[5] is for a color closest to
56 struct ftxfont_frame_data
*next
;
60 /* Return an array of 6 GCs for antialiasing. */
63 ftxfont_get_gcs (f
, foreground
, background
)
65 unsigned long foreground
, background
;
70 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
71 struct ftxfont_frame_data
*prev
= NULL
, *this = NULL
, *new;
75 for (this = data
; this; prev
= this, this = this->next
)
77 if (this->colors
[0].pixel
< background
)
79 if (this->colors
[0].pixel
> background
)
81 if (this->colors
[1].pixel
< foreground
)
83 if (this->colors
[1].pixel
> foreground
)
89 new = malloc (sizeof (struct ftxfont_frame_data
));
97 else if (font_put_frame_data (f
, &ftxfont_driver
, new) < 0)
103 new->colors
[0].pixel
= background
;
104 new->colors
[1].pixel
= foreground
;
107 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), new->colors
, 2);
108 for (i
= 1; i
< 7; i
++)
110 /* Interpolate colors linearly. Any better algorithm? */
112 = (new->colors
[1].red
* i
+ new->colors
[0].red
* (8 - i
)) / 8;
114 = (new->colors
[1].green
* i
+ new->colors
[0].green
* (8 - i
)) / 8;
116 = (new->colors
[1].blue
* i
+ new->colors
[0].blue
* (8 - i
)) / 8;
117 if (! x_alloc_nearest_color (f
, FRAME_X_COLORMAP (f
), &color
))
119 xgcv
.foreground
= color
.pixel
;
120 new->gcs
[i
- 1] = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
121 GCForeground
, &xgcv
);
128 for (i
--; i
>= 0; i
--)
129 XFreeGC (FRAME_X_DISPLAY (f
), new->gcs
[i
]);
132 prev
->next
= new->next
;
134 font_put_frame_data (f
, &ftxfont_driver
, new->next
);
142 ftxfont_draw_bitmap (f
, gc_fore
, gcs
, font
, code
, x
, y
, p
, size
, n
, flush
)
152 struct font_bitmap bitmap
;
156 if (ftfont_driver
.get_bitmap (font
, code
, &bitmap
, size
> 0x100 ? 1 : 8) < 0)
160 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
161 i
++, b
+= bitmap
.pitch
)
163 for (j
= 0; j
< bitmap
.width
; j
++)
164 if (b
[j
/ 8] & (1 << (7 - (j
% 8))))
166 p
[n
[0]].x
= x
+ bitmap
.left
+ j
;
167 p
[n
[0]].y
= y
- bitmap
.top
+ i
;
170 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
171 gc_fore
, p
, size
, CoordModeOrigin
);
176 if (flush
&& n
[0] > 0)
177 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
178 gc_fore
, p
, n
[0], CoordModeOrigin
);
182 for (i
= 0, b
= bitmap
.buffer
; i
< bitmap
.rows
;
183 i
++, b
+= bitmap
.pitch
)
185 for (j
= 0; j
< bitmap
.width
; j
++)
187 int idx
= (bitmap
.bits_per_pixel
== 1
188 ? ((b
[j
/ 8] & (1 << (7 - (j
% 8)))) ? 6 : -1)
193 XPoint
*pp
= p
+ size
* idx
;
195 pp
[n
[idx
]].x
= x
+ bitmap
.left
+ j
;
196 pp
[n
[idx
]].y
= y
- bitmap
.top
+ i
;
197 if (++(n
[idx
]) == size
)
199 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
200 idx
== 6 ? gc_fore
: gcs
[idx
], pp
, size
,
209 for (i
= 0; i
< 6; i
++)
211 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
212 gcs
[i
], p
+ 0x100 * i
, n
[i
], CoordModeOrigin
);
214 XDrawPoints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
215 gc_fore
, p
+ 0x600, n
[6], CoordModeOrigin
);
219 if (ftfont_driver
.free_bitmap
)
220 ftfont_driver
.free_bitmap (font
, &bitmap
);
222 return bitmap
.advance
;
226 ftxfont_draw_backgrond (f
, font
, gc
, x
, y
, width
)
234 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
235 GCForeground
| GCBackground
, &xgcv
);
236 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.background
);
237 XFillRectangle (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), gc
,
238 x
, y
- font
->ascent
, width
, y
+ font
->descent
);
239 XSetForeground (FRAME_X_DISPLAY (f
), gc
, xgcv
.foreground
);
242 /* Prototypes for font-driver methods. */
243 static Lisp_Object ftxfont_list
P_ ((Lisp_Object
, Lisp_Object
));
244 static Lisp_Object ftxfont_match
P_ ((Lisp_Object
, Lisp_Object
));
245 static Lisp_Object ftxfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
246 static void ftxfont_close
P_ ((FRAME_PTR
, struct font
*));
247 static int ftxfont_draw
P_ ((struct glyph_string
*, int, int, int, int, int));
249 struct font_driver ftxfont_driver
;
252 ftxfont_list (frame
, spec
)
256 Lisp_Object list
= ftfont_driver
.list (frame
, spec
), tail
;
258 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
259 ASET (XCAR (tail
), FONT_TYPE_INDEX
, Qftx
);
264 ftxfont_match (frame
, spec
)
268 Lisp_Object entity
= ftfont_driver
.match (frame
, spec
);
270 if (VECTORP (entity
))
271 ASET (entity
, FONT_TYPE_INDEX
, Qftx
);
276 ftxfont_open (f
, entity
, pixel_size
)
281 Lisp_Object font_object
;
284 font_object
= ftfont_driver
.open (f
, entity
, pixel_size
);
285 if (NILP (font_object
))
287 font
= XFONT_OBJECT (font_object
);
288 font
->driver
= &ftxfont_driver
;
293 ftxfont_close (f
, font
)
297 ftfont_driver
.close (f
, font
);
301 ftxfont_draw (s
, from
, to
, x
, y
, with_background
)
302 struct glyph_string
*s
;
303 int from
, to
, x
, y
, with_background
;
306 struct face
*face
= s
->face
;
307 struct font
*font
= s
->font
;
316 n
[0] = n
[1] = n
[2] = n
[3] = n
[4] = n
[5] = n
[6] = 0;
320 ftxfont_draw_backgrond (f
, font
, s
->gc
, x
, y
, s
->width
);
321 code
= alloca (sizeof (unsigned) * len
);
322 for (i
= 0; i
< len
; i
++)
323 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
324 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
326 if (face
->gc
== s
->gc
)
328 gcs
= ftxfont_get_gcs (f
, face
->foreground
, face
->background
);
333 unsigned long mask
= GCForeground
| GCBackground
;
335 XGetGCValues (FRAME_X_DISPLAY (f
), s
->gc
, mask
, &xgcv
);
336 gcs
= ftxfont_get_gcs (f
, xgcv
.foreground
, xgcv
.background
);
342 for (i
= 0; i
< 6; i
++)
343 XSetClipRectangles (FRAME_X_DISPLAY (f
), gcs
[i
], 0, 0,
344 s
->clip
, s
->num_clips
, Unsorted
);
346 for (i
= 0; i
< len
; i
++)
348 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, gcs
, font
, code
[i
], x
, y
,
349 p
, 0x100, n
, i
+ 1 == len
);
350 x
+= (s
->padding_p
? 1 : xadvance
);
353 for (i
= 0; i
< 6; i
++)
354 XSetClipMask (FRAME_X_DISPLAY (f
), gcs
[i
], None
);
358 /* We can't draw with antialiasing.
359 s->gc should already have a proper clipping setting. */
360 for (i
= 0; i
< len
; i
++)
362 xadvance
= ftxfont_draw_bitmap (f
, s
->gc
, NULL
, font
, code
[i
], x
, y
,
363 p
, 0x700, n
, i
+ 1 == len
);
364 x
+= (s
->padding_p
? 1 : xadvance
);
374 ftxfont_end_for_frame (f
)
377 struct ftxfont_frame_data
*data
= font_get_frame_data (f
, &ftxfont_driver
);
382 struct ftxfont_frame_data
*next
= data
->next
;
385 for (i
= 0; i
< 6; i
++)
386 XFreeGC (FRAME_X_DISPLAY (f
), data
->gcs
[i
]);
391 font_put_frame_data (f
, &ftxfont_driver
, NULL
);
400 DEFSYM (Qftx
, "ftx");
402 ftxfont_driver
= ftfont_driver
;
403 ftxfont_driver
.type
= Qftx
;
404 ftxfont_driver
.list
= ftxfont_list
;
405 ftxfont_driver
.match
= ftxfont_match
;
406 ftxfont_driver
.open
= ftxfont_open
;
407 ftxfont_driver
.close
= ftxfont_close
;
408 ftxfont_driver
.draw
= ftxfont_draw
;
409 ftxfont_driver
.end_for_frame
= ftxfont_end_for_frame
;
410 register_font_driver (&ftxfont_driver
, NULL
);
413 /* arch-tag: 59bd3469-5330-413f-b29d-1aa36492abe8
414 (do not change this comment) */