1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2021 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
6
7 This file is part of GNU Emacs.
8
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 (at
12 your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include <X11/Xlib.h>
24
25 #include "lisp.h"
26 #include "xterm.h"
27 #include "frame.h"
28 #include "blockinput.h"
29 #include "font.h"
30 #include "pdumper.h"
31
32 /* FTX font driver. */
33
34 struct ftxfont_frame_data
35 {
36 /* Background and foreground colors. */
37 XColor colors[2];
38 /* GCs interpolating the above colors. gcs[0] is for a color
39 closest to BACKGROUND, and gcs[5] is for a color closest to
40 FOREGROUND. */
41 GC gcs[6];
42 struct ftxfont_frame_data *next;
43 };
44
45
46 /* Return an array of 6 GCs for antialiasing. */
47
48 static GC *
ftxfont_get_gcs(struct frame * f,unsigned long foreground,unsigned long background)49 ftxfont_get_gcs (struct frame *f, unsigned long foreground, unsigned long background)
50 {
51 XColor color;
52 XGCValues xgcv;
53 int i;
54 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
55 struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
56
57 if (data)
58 {
59 for (this = data; this; prev = this, this = this->next)
60 {
61 if (this->colors[0].pixel < background)
62 continue;
63 if (this->colors[0].pixel > background)
64 break;
65 if (this->colors[1].pixel < foreground)
66 continue;
67 if (this->colors[1].pixel > foreground)
68 break;
69 return this->gcs;
70 }
71 }
72
73 new = xmalloc (sizeof *new);
74 new->next = this;
75 if (prev)
76 prev->next = new;
77 font_put_frame_data (f, Qftx, new);
78
79 new->colors[0].pixel = background;
80 new->colors[1].pixel = foreground;
81
82 block_input ();
83 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
84 for (i = 1; i < 7; i++)
85 {
86 /* Interpolate colors linearly. Any better algorithm? */
87 color.red
88 = (new->colors[1].red * i + new->colors[0].red * (8 - i)) / 8;
89 color.green
90 = (new->colors[1].green * i + new->colors[0].green * (8 - i)) / 8;
91 color.blue
92 = (new->colors[1].blue * i + new->colors[0].blue * (8 - i)) / 8;
93 if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &color))
94 break;
95 xgcv.foreground = color.pixel;
96 new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
97 GCForeground, &xgcv);
98 }
99 unblock_input ();
100
101 if (i < 7)
102 {
103 block_input ();
104 for (i--; i >= 0; i--)
105 XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
106 unblock_input ();
107 if (prev)
108 prev->next = new->next;
109 else if (data)
110 font_put_frame_data (f, Qftx, new->next);
111 xfree (new);
112 return NULL;
113 }
114 return new->gcs;
115 }
116
117 static int
ftxfont_draw_bitmap(struct frame * f,GC gc_fore,GC * gcs,struct font * font,unsigned int code,int x,int y,XPoint * p,int size,int * n,bool flush)118 ftxfont_draw_bitmap (struct frame *f, GC gc_fore, GC *gcs, struct font *font,
119 unsigned int code, int x, int y, XPoint *p, int size,
120 int *n, bool flush)
121 {
122 struct font_bitmap bitmap;
123 unsigned char *b;
124 int i, j;
125
126 if (ftfont_get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
127 return 0;
128 if (size > 0x100)
129 {
130 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
131 i++, b += bitmap.pitch)
132 {
133 for (j = 0; j < bitmap.width; j++)
134 if (b[j / 8] & (1 << (7 - (j % 8))))
135 {
136 p[n[0]].x = x + bitmap.left + j;
137 p[n[0]].y = y - bitmap.top + i;
138 if (++n[0] == size)
139 {
140 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
141 gc_fore, p, size, CoordModeOrigin);
142 n[0] = 0;
143 }
144 }
145 }
146 if (flush && n[0] > 0)
147 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
148 gc_fore, p, n[0], CoordModeOrigin);
149 }
150 else
151 {
152 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
153 i++, b += bitmap.pitch)
154 {
155 for (j = 0; j < bitmap.width; j++)
156 {
157 int idx = (bitmap.bits_per_pixel == 1
158 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
159 : (b[j] >> 5) - 1);
160
161 if (idx >= 0)
162 {
163 XPoint *pp = p + size * idx;
164
165 pp[n[idx]].x = x + bitmap.left + j;
166 pp[n[idx]].y = y - bitmap.top + i;
167 if (++(n[idx]) == size)
168 {
169 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
170 idx == 6 ? gc_fore : gcs[idx], pp, size,
171 CoordModeOrigin);
172 n[idx] = 0;
173 }
174 }
175 }
176 }
177 if (flush)
178 {
179 for (i = 0; i < 6; i++)
180 if (n[i] > 0)
181 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
182 gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
183 if (n[6] > 0)
184 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
185 gc_fore, p + 0x600, n[6], CoordModeOrigin);
186 }
187 }
188
189 /* There is no ftfont_free_bitmap, so do not try to free BITMAP. */
190
191 return bitmap.advance;
192 }
193
194 static void
ftxfont_draw_background(struct frame * f,struct font * font,GC gc,int x,int y,int width)195 ftxfont_draw_background (struct frame *f, struct font *font, GC gc, int x, int y,
196 int width)
197 {
198 XGCValues xgcv;
199
200 XGetGCValues (FRAME_X_DISPLAY (f), gc,
201 GCForeground | GCBackground, &xgcv);
202 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
203 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), gc,
204 x, y - FONT_BASE (font), width, FONT_HEIGHT (font));
205 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
206 }
207
208 static Lisp_Object
ftxfont_list(struct frame * f,Lisp_Object spec)209 ftxfont_list (struct frame *f, Lisp_Object spec)
210 {
211 return ftfont_list2 (f, spec, Qftx);
212 }
213
214 static Lisp_Object
ftxfont_match(struct frame * f,Lisp_Object spec)215 ftxfont_match (struct frame *f, Lisp_Object spec)
216 {
217 return ftfont_match2 (f, spec, Qftx);
218 }
219
220 static Lisp_Object
ftxfont_open(struct frame * f,Lisp_Object entity,int pixel_size)221 ftxfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
222 {
223 Lisp_Object font_object = ftfont_open (f, entity, pixel_size);
224 if (NILP (font_object))
225 return Qnil;
226 struct font *font = XFONT_OBJECT (font_object);
227 font->driver = &ftxfont_driver;
228 return font_object;
229 }
230
231 static void
ftxfont_close(struct font * font)232 ftxfont_close (struct font *font)
233 {
234 ftfont_close (font);
235 }
236
237 static int
ftxfont_draw(struct glyph_string * s,int from,int to,int x,int y,bool with_background)238 ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y,
239 bool with_background)
240 {
241 struct frame *f = s->f;
242 struct face *face = s->face;
243 struct font *font = s->font;
244 XPoint p[0x700];
245 int n[7];
246 unsigned *code = s->char2b + from;
247 int len = to - from;
248 int i;
249 GC *gcs;
250 int xadvance;
251
252 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
253
254 block_input ();
255 if (with_background)
256 ftxfont_draw_background (f, font, s->gc, x, y, s->width);
257
258 if (face->gc == s->gc)
259 {
260 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
261 }
262 else
263 {
264 XGCValues xgcv;
265 unsigned long mask = GCForeground | GCBackground;
266
267 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
268 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
269 }
270
271 if (gcs)
272 {
273 if (s->num_clips)
274 for (i = 0; i < 6; i++)
275 XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
276 s->clip, s->num_clips, Unsorted);
277
278 for (i = 0; i < len; i++)
279 {
280 xadvance = ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
281 p, 0x100, n, i + 1 == len);
282 x += (s->padding_p ? 1 : xadvance);
283 }
284 if (s->num_clips)
285 for (i = 0; i < 6; i++)
286 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
287 }
288 else
289 {
290 /* We can't draw with antialiasing.
291 s->gc should already have a proper clipping setting. */
292 for (i = 0; i < len; i++)
293 {
294 xadvance = ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
295 p, 0x700, n, i + 1 == len);
296 x += (s->padding_p ? 1 : xadvance);
297 }
298 }
299
300 unblock_input ();
301
302 return len;
303 }
304
305 static int
ftxfont_end_for_frame(struct frame * f)306 ftxfont_end_for_frame (struct frame *f)
307 {
308 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
309
310 block_input ();
311 while (data)
312 {
313 struct ftxfont_frame_data *next = data->next;
314 int i;
315
316 for (i = 0; i < 6; i++)
317 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
318 xfree (data);
319 data = next;
320 }
321 unblock_input ();
322 font_put_frame_data (f, Qftx, NULL);
323 return 0;
324 }
325
326
327
328 static void syms_of_ftxfont_for_pdumper (void);
329
330 struct font_driver const ftxfont_driver =
331 {
332 /* We can't draw a text without device dependent functions. */
333 .type = LISPSYM_INITIALLY (Qftx),
334 .get_cache = ftfont_get_cache,
335 .list = ftxfont_list,
336 .match = ftxfont_match,
337 .list_family = ftfont_list_family,
338 .open_font = ftxfont_open,
339 .close_font = ftxfont_close,
340 .has_char = ftfont_has_char,
341 .encode_char = ftfont_encode_char,
342 .text_extents = ftfont_text_extents,
343 .draw = ftxfont_draw,
344 .get_bitmap = ftfont_get_bitmap,
345 .anchor_point = ftfont_anchor_point,
346 #ifdef HAVE_LIBOTF
347 .otf_capability = ftfont_otf_capability,
348 #endif
349 .end_for_frame = ftxfont_end_for_frame,
350 #if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
351 .shape = ftfont_shape,
352 #endif
353 #if defined HAVE_OTF_GET_VARIATION_GLYPHS || defined HAVE_FT_FACE_GETCHARVARIANTINDEX
354 .get_variation_glyphs = ftfont_variation_glyphs,
355 #endif
356 .filter_properties = ftfont_filter_properties,
357 .combining_capability = ftfont_combining_capability,
358 };
359
360 void
syms_of_ftxfont(void)361 syms_of_ftxfont (void)
362 {
363 DEFSYM (Qftx, "ftx");
364 pdumper_do_now_and_after_load (syms_of_ftxfont_for_pdumper);
365 }
366
367 static void
syms_of_ftxfont_for_pdumper(void)368 syms_of_ftxfont_for_pdumper (void)
369 {
370 register_font_driver (&ftxfont_driver, NULL);
371 }
372