* lisp/textmodes/tex-mode.el: Use lexical-binding
[emacs.git] / src / ftcrfont.c
blobdf3aa596f6b1f3664f781053265b732c35ca977b
1 /* ftcrfont.c -- FreeType font driver on cairo.
2 Copyright (C) 2015 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <cairo-ft.h>
24 #include "lisp.h"
25 #include "dispextern.h"
26 #include "xterm.h"
27 #include "frame.h"
28 #include "blockinput.h"
29 #include "character.h"
30 #include "charset.h"
31 #include "fontset.h"
32 #include "font.h"
33 #include "ftfont.h"
35 /* FTCR font driver. */
37 /* The actual structure for FTCR font. A pointer to this structure
38 can be cast to struct font *. */
40 struct ftcrfont_info
42 struct font font;
43 /* The following six members must be here in this order to be
44 compatible with struct ftfont_info (in ftfont.c). */
45 #ifdef HAVE_LIBOTF
46 bool maybe_otf; /* Flag to tell if this may be OTF or not. */
47 OTF *otf;
48 #endif /* HAVE_LIBOTF */
49 FT_Size ft_size;
50 int index;
51 FT_Matrix matrix;
53 cairo_font_face_t *cr_font_face;
54 /* To prevent cairo from cluttering the activated FT_Size maintained
55 in ftfont.c, we activate this special FT_Size before drawing. */
56 FT_Size ft_size_draw;
57 /* Font metrics cache. */
58 struct font_metrics **metrics;
59 short metrics_nrows;
62 #define METRICS_NCOLS_PER_ROW (128)
64 enum metrics_status
66 METRICS_INVALID = -1, /* metrics entry is invalid */
69 #define METRICS_STATUS(metrics) ((metrics)->ascent + (metrics)->descent)
70 #define METRICS_SET_STATUS(metrics, status) \
71 ((metrics)->ascent = 0, (metrics)->descent = (status))
73 struct font_driver ftcrfont_driver;
75 static int
76 ftcrfont_glyph_extents (struct font *font,
77 unsigned glyph,
78 struct font_metrics *metrics)
80 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
81 int row, col;
82 struct font_metrics *cache;
84 row = glyph / METRICS_NCOLS_PER_ROW;
85 col = glyph % METRICS_NCOLS_PER_ROW;
86 if (row >= ftcrfont_info->metrics_nrows)
88 ftcrfont_info->metrics =
89 xrealloc (ftcrfont_info->metrics,
90 sizeof (struct font_metrics *) * (row + 1));
91 bzero (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows,
92 (sizeof (struct font_metrics *)
93 * (row + 1 - ftcrfont_info->metrics_nrows)));
94 ftcrfont_info->metrics_nrows = row + 1;
96 if (ftcrfont_info->metrics[row] == NULL)
98 struct font_metrics *new;
99 int i;
101 new = xmalloc (sizeof (struct font_metrics) * METRICS_NCOLS_PER_ROW);
102 for (i = 0; i < METRICS_NCOLS_PER_ROW; i++)
103 METRICS_SET_STATUS (new + i, METRICS_INVALID);
104 ftcrfont_info->metrics[row] = new;
106 cache = ftcrfont_info->metrics[row] + col;
108 if (METRICS_STATUS (cache) == METRICS_INVALID)
109 ftfont_driver.text_extents (font, &glyph, 1, cache);
111 if (metrics)
112 *metrics = *cache;
114 return cache->width;
117 static Lisp_Object
118 ftcrfont_list (struct frame *f, Lisp_Object spec)
120 Lisp_Object list = ftfont_driver.list (f, spec), tail;
122 for (tail = list; CONSP (tail); tail = XCDR (tail))
123 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftcr);
124 return list;
127 static Lisp_Object
128 ftcrfont_match (struct frame *f, Lisp_Object spec)
130 Lisp_Object entity = ftfont_driver.match (f, spec);
132 if (VECTORP (entity))
133 ASET (entity, FONT_TYPE_INDEX, Qftcr);
134 return entity;
137 extern FT_Face ftfont_get_ft_face (Lisp_Object);
139 static Lisp_Object
140 ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
142 Lisp_Object font_object;
143 struct font *font;
144 struct ftcrfont_info *ftcrfont_info;
145 FT_Face ft_face;
146 FT_UInt size;
148 block_input ();
149 size = XINT (AREF (entity, FONT_SIZE_INDEX));
150 if (size == 0)
151 size = pixel_size;
152 font_object = font_build_object (VECSIZE (struct ftcrfont_info),
153 Qftcr, entity, size);
154 font_object = ftfont_open2 (f, entity, pixel_size, font_object);
155 if (NILP (font_object)) return Qnil;
157 font = XFONT_OBJECT (font_object);
158 font->driver = &ftcrfont_driver;
159 ftcrfont_info = (struct ftcrfont_info *) font;
160 ft_face = ftcrfont_info->ft_size->face;
161 FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
162 FT_Activate_Size (ftcrfont_info->ft_size_draw);
163 FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
164 ftcrfont_info->cr_font_face =
165 cairo_ft_font_face_create_for_ft_face (ft_face, 0);
166 ftcrfont_info->metrics = NULL;
167 ftcrfont_info->metrics_nrows = 0;
168 unblock_input ();
170 return font_object;
173 static void
174 ftcrfont_close (struct font *font)
176 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
177 int i;
179 block_input ();
180 for (i = 0; i < ftcrfont_info->metrics_nrows; i++)
181 if (ftcrfont_info->metrics[i])
182 xfree (ftcrfont_info->metrics[i]);
183 if (ftcrfont_info->metrics)
184 xfree (ftcrfont_info->metrics);
185 FT_Done_Size (ftcrfont_info->ft_size_draw);
186 cairo_font_face_destroy (ftcrfont_info->cr_font_face);
187 unblock_input ();
189 ftfont_driver.close (font);
192 static void
193 ftcrfont_text_extents (struct font *font,
194 unsigned *code,
195 int nglyphs,
196 struct font_metrics *metrics)
198 int width, i;
200 block_input ();
201 width = ftcrfont_glyph_extents (font, code[0], metrics);
202 for (i = 1; i < nglyphs; i++)
204 struct font_metrics m;
205 int w = ftcrfont_glyph_extents (font, code[i], metrics ? &m : NULL);
207 if (metrics)
209 if (width + m.lbearing < metrics->lbearing)
210 metrics->lbearing = width + m.lbearing;
211 if (width + m.rbearing > metrics->rbearing)
212 metrics->rbearing = width + m.rbearing;
213 if (m.ascent > metrics->ascent)
214 metrics->ascent = m.ascent;
215 if (m.descent > metrics->descent)
216 metrics->descent = m.descent;
218 width += w;
220 unblock_input ();
222 if (metrics)
223 metrics->width = width;
226 static int
227 ftcrfont_draw (struct glyph_string *s,
228 int from, int to, int x, int y, bool with_background)
230 struct frame *f = s->f;
231 struct face *face = s->face;
232 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) s->font;
233 cairo_t *cr;
234 cairo_glyph_t *glyphs;
235 cairo_surface_t *surface;
236 cairo_surface_type_t surface_type;
237 int len = to - from;
238 int i;
240 block_input ();
242 cr = x_begin_cr_clip (f, s->gc);
244 if (with_background)
246 x_set_cr_source_with_gc_background (f, s->gc);
247 cairo_rectangle (cr, x, y - FONT_BASE (face->font),
248 s->width, FONT_HEIGHT (face->font));
249 cairo_fill (cr);
252 glyphs = alloca (sizeof (cairo_glyph_t) * len);
253 for (i = 0; i < len; i++)
255 unsigned code = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
256 | XCHAR2B_BYTE2 (s->char2b + from + i));
258 glyphs[i].index = code;
259 glyphs[i].x = x;
260 glyphs[i].y = y;
261 x += (s->padding_p ? 1 : ftcrfont_glyph_extents (s->font, code, NULL));
264 x_set_cr_source_with_gc_foreground (f, s->gc);
265 cairo_set_font_face (cr, ftcrfont_info->cr_font_face);
266 cairo_set_font_size (cr, s->font->pixel_size);
267 /* cairo_set_font_matrix */
268 /* cairo_set_font_options */
270 FT_Activate_Size (ftcrfont_info->ft_size_draw);
271 cairo_show_glyphs (cr, glyphs, len);
272 surface = cairo_get_target (cr);
273 /* XXX: It used to be necessary to flush when exporting. It might
274 be the case that this is no longer necessary. */
275 surface_type = cairo_surface_get_type (surface);
276 if (surface_type != CAIRO_SURFACE_TYPE_XLIB
277 && (surface_type != CAIRO_SURFACE_TYPE_IMAGE
278 || cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32))
279 cairo_surface_flush (surface);
281 x_end_cr_clip (f);
283 unblock_input ();
285 return len;
290 void
291 syms_of_ftcrfont (void)
293 if (ftfont_info_size != offsetof (struct ftcrfont_info, cr_font_face))
294 abort ();
296 DEFSYM (Qftcr, "ftcr");
298 ftcrfont_driver = ftfont_driver;
299 ftcrfont_driver.type = Qftcr;
300 ftcrfont_driver.list = ftcrfont_list;
301 ftcrfont_driver.match = ftcrfont_match;
302 ftcrfont_driver.open = ftcrfont_open;
303 ftcrfont_driver.close = ftcrfont_close;
304 ftcrfont_driver.text_extents = ftcrfont_text_extents;
305 ftcrfont_driver.draw = ftcrfont_draw;
306 register_font_driver (&ftcrfont_driver, NULL);