; * lisp/ldefs-boot.el: Update.
[emacs.git] / src / ftcrfont.c
blob62f44573a862e6843f6db7a470891e2ccfbeb1ee
1 /* ftcrfont.c -- FreeType font driver on cairo.
2 Copyright (C) 2015-2019 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 (at
9 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 <https://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <cairo-ft.h>
24 #include "lisp.h"
25 #include "xterm.h"
26 #include "blockinput.h"
27 #include "font.h"
28 #include "ftfont.h"
30 /* FTCR font driver. */
32 /* The actual structure for FTCR font. A pointer to this structure
33 can be cast to struct font *. */
35 struct ftcrfont_info
37 struct font font;
38 /* The following members up to and including 'matrix' must be here
39 in this order to be compatible with struct ftfont_info (in
40 ftfont.c). */
41 #ifdef HAVE_LIBOTF
42 bool maybe_otf; /* Flag to tell if this may be OTF or not. */
43 OTF *otf;
44 #endif /* HAVE_LIBOTF */
45 FT_Size ft_size;
46 int index;
47 FT_Matrix matrix;
49 cairo_font_face_t *cr_font_face;
50 /* To prevent cairo from cluttering the activated FT_Size maintained
51 in ftfont.c, we activate this special FT_Size before drawing. */
52 FT_Size ft_size_draw;
53 /* Font metrics cache. */
54 struct font_metrics **metrics;
55 short metrics_nrows;
58 #define METRICS_NCOLS_PER_ROW (128)
60 enum metrics_status
62 METRICS_INVALID = -1, /* metrics entry is invalid */
65 #define METRICS_STATUS(metrics) ((metrics)->ascent + (metrics)->descent)
66 #define METRICS_SET_STATUS(metrics, status) \
67 ((metrics)->ascent = 0, (metrics)->descent = (status))
69 static int
70 ftcrfont_glyph_extents (struct font *font,
71 unsigned glyph,
72 struct font_metrics *metrics)
74 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
75 int row, col;
76 struct font_metrics *cache;
78 row = glyph / METRICS_NCOLS_PER_ROW;
79 col = glyph % METRICS_NCOLS_PER_ROW;
80 if (row >= ftcrfont_info->metrics_nrows)
82 ftcrfont_info->metrics =
83 xrealloc (ftcrfont_info->metrics,
84 sizeof (struct font_metrics *) * (row + 1));
85 memset (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows, 0,
86 (sizeof (struct font_metrics *)
87 * (row + 1 - ftcrfont_info->metrics_nrows)));
88 ftcrfont_info->metrics_nrows = row + 1;
90 if (ftcrfont_info->metrics[row] == NULL)
92 struct font_metrics *new;
93 int i;
95 new = xmalloc (sizeof (struct font_metrics) * METRICS_NCOLS_PER_ROW);
96 for (i = 0; i < METRICS_NCOLS_PER_ROW; i++)
97 METRICS_SET_STATUS (new + i, METRICS_INVALID);
98 ftcrfont_info->metrics[row] = new;
100 cache = ftcrfont_info->metrics[row] + col;
102 if (METRICS_STATUS (cache) == METRICS_INVALID)
103 ftfont_text_extents (font, &glyph, 1, cache);
105 if (metrics)
106 *metrics = *cache;
108 return cache->width;
111 static Lisp_Object
112 ftcrfont_list (struct frame *f, Lisp_Object spec)
114 Lisp_Object list = ftfont_list (f, spec), tail;
116 for (tail = list; CONSP (tail); tail = XCDR (tail))
117 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftcr);
118 return list;
121 static Lisp_Object
122 ftcrfont_match (struct frame *f, Lisp_Object spec)
124 Lisp_Object entity = ftfont_match (f, spec);
126 if (VECTORP (entity))
127 ASET (entity, FONT_TYPE_INDEX, Qftcr);
128 return entity;
131 static Lisp_Object
132 ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
134 Lisp_Object font_object;
135 struct font *font;
136 struct ftcrfont_info *ftcrfont_info;
137 FT_Face ft_face;
138 FT_UInt size;
140 block_input ();
141 size = XINT (AREF (entity, FONT_SIZE_INDEX));
142 if (size == 0)
143 size = pixel_size;
144 font_object = font_build_object (VECSIZE (struct ftcrfont_info),
145 Qftcr, entity, size);
146 font_object = ftfont_open2 (f, entity, pixel_size, font_object);
147 if (NILP (font_object)) return Qnil;
149 font = XFONT_OBJECT (font_object);
150 font->driver = &ftcrfont_driver;
151 ftcrfont_info = (struct ftcrfont_info *) font;
152 ft_face = ftcrfont_info->ft_size->face;
153 FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
154 FT_Activate_Size (ftcrfont_info->ft_size_draw);
155 FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
156 ftcrfont_info->cr_font_face =
157 cairo_ft_font_face_create_for_ft_face (ft_face, 0);
158 ftcrfont_info->metrics = NULL;
159 ftcrfont_info->metrics_nrows = 0;
160 unblock_input ();
162 return font_object;
165 static void
166 ftcrfont_close (struct font *font)
168 if (font_data_structures_may_be_ill_formed ())
169 return;
171 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
172 int i;
174 block_input ();
175 for (i = 0; i < ftcrfont_info->metrics_nrows; i++)
176 if (ftcrfont_info->metrics[i])
177 xfree (ftcrfont_info->metrics[i]);
178 if (ftcrfont_info->metrics)
179 xfree (ftcrfont_info->metrics);
180 FT_Done_Size (ftcrfont_info->ft_size_draw);
181 cairo_font_face_destroy (ftcrfont_info->cr_font_face);
182 unblock_input ();
184 ftfont_close (font);
187 static void
188 ftcrfont_text_extents (struct font *font,
189 unsigned *code,
190 int nglyphs,
191 struct font_metrics *metrics)
193 int width, i;
195 block_input ();
196 width = ftcrfont_glyph_extents (font, code[0], metrics);
197 for (i = 1; i < nglyphs; i++)
199 struct font_metrics m;
200 int w = ftcrfont_glyph_extents (font, code[i], metrics ? &m : NULL);
202 if (metrics)
204 if (width + m.lbearing < metrics->lbearing)
205 metrics->lbearing = width + m.lbearing;
206 if (width + m.rbearing > metrics->rbearing)
207 metrics->rbearing = width + m.rbearing;
208 if (m.ascent > metrics->ascent)
209 metrics->ascent = m.ascent;
210 if (m.descent > metrics->descent)
211 metrics->descent = m.descent;
213 width += w;
215 unblock_input ();
217 if (metrics)
218 metrics->width = width;
221 static int
222 ftcrfont_draw (struct glyph_string *s,
223 int from, int to, int x, int y, bool with_background)
225 struct frame *f = s->f;
226 struct face *face = s->face;
227 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) s->font;
228 cairo_t *cr;
229 cairo_glyph_t *glyphs;
230 cairo_surface_t *surface;
231 cairo_surface_type_t surface_type;
232 int len = to - from;
233 int i;
235 block_input ();
237 cr = x_begin_cr_clip (f, s->gc);
239 if (with_background)
241 x_set_cr_source_with_gc_background (f, s->gc);
242 cairo_rectangle (cr, x, y - FONT_BASE (face->font),
243 s->width, FONT_HEIGHT (face->font));
244 cairo_fill (cr);
247 glyphs = alloca (sizeof (cairo_glyph_t) * len);
248 for (i = 0; i < len; i++)
250 unsigned code = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
251 | XCHAR2B_BYTE2 (s->char2b + from + i));
253 glyphs[i].index = code;
254 glyphs[i].x = x;
255 glyphs[i].y = y;
256 x += (s->padding_p ? 1 : ftcrfont_glyph_extents (s->font, code, NULL));
259 x_set_cr_source_with_gc_foreground (f, s->gc);
260 cairo_set_font_face (cr, ftcrfont_info->cr_font_face);
261 cairo_set_font_size (cr, s->font->pixel_size);
262 /* cairo_set_font_matrix */
263 /* cairo_set_font_options */
265 FT_Activate_Size (ftcrfont_info->ft_size_draw);
266 cairo_show_glyphs (cr, glyphs, len);
267 surface = cairo_get_target (cr);
268 /* XXX: It used to be necessary to flush when exporting. It might
269 be the case that this is no longer necessary. */
270 surface_type = cairo_surface_get_type (surface);
271 if (surface_type != CAIRO_SURFACE_TYPE_XLIB
272 && (surface_type != CAIRO_SURFACE_TYPE_IMAGE
273 || cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32))
274 cairo_surface_flush (surface);
276 x_end_cr_clip (f);
278 unblock_input ();
280 return len;
285 struct font_driver const ftcrfont_driver =
287 .type = LISPSYM_INITIALLY (Qftcr),
288 .get_cache = ftfont_get_cache,
289 .list = ftcrfont_list,
290 .match = ftcrfont_match,
291 .list_family = ftfont_list_family,
292 .open = ftcrfont_open,
293 .close = ftcrfont_close,
294 .has_char = ftfont_has_char,
295 .encode_char = ftfont_encode_char,
296 .text_extents = ftcrfont_text_extents,
297 .draw = ftcrfont_draw,
298 .get_bitmap = ftfont_get_bitmap,
299 .anchor_point = ftfont_anchor_point,
300 #ifdef HAVE_LIBOTF
301 .otf_capability = ftfont_otf_capability,
302 #endif
303 #if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
304 .shape = ftfont_shape,
305 #endif
306 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
307 .get_variation_glyphs = ftfont_variation_glyphs,
308 #endif
309 .filter_properties = ftfont_filter_properties,
310 .combining_capability = ftfont_combining_capability,
313 void
314 syms_of_ftcrfont (void)
316 if (ftfont_info_size != offsetof (struct ftcrfont_info, cr_font_face))
317 abort ();
319 DEFSYM (Qftcr, "ftcr");
320 register_font_driver (&ftcrfont_driver, NULL);