; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / src / ftcrfont.c
blob425250e2297b3e287a973d1e88192e85554bf124
1 /* ftcrfont.c -- FreeType font driver on cairo.
2 Copyright (C) 2015-2018 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 six members must be here in this order to be
39 compatible with struct ftfont_info (in ftfont.c). */
40 #ifdef HAVE_LIBOTF
41 bool maybe_otf; /* Flag to tell if this may be OTF or not. */
42 OTF *otf;
43 #endif /* HAVE_LIBOTF */
44 FT_Size ft_size;
45 int index;
46 FT_Matrix matrix;
48 cairo_font_face_t *cr_font_face;
49 /* To prevent cairo from cluttering the activated FT_Size maintained
50 in ftfont.c, we activate this special FT_Size before drawing. */
51 FT_Size ft_size_draw;
52 /* Font metrics cache. */
53 struct font_metrics **metrics;
54 short metrics_nrows;
57 #define METRICS_NCOLS_PER_ROW (128)
59 enum metrics_status
61 METRICS_INVALID = -1, /* metrics entry is invalid */
64 #define METRICS_STATUS(metrics) ((metrics)->ascent + (metrics)->descent)
65 #define METRICS_SET_STATUS(metrics, status) \
66 ((metrics)->ascent = 0, (metrics)->descent = (status))
68 static int
69 ftcrfont_glyph_extents (struct font *font,
70 unsigned glyph,
71 struct font_metrics *metrics)
73 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
74 int row, col;
75 struct font_metrics *cache;
77 row = glyph / METRICS_NCOLS_PER_ROW;
78 col = glyph % METRICS_NCOLS_PER_ROW;
79 if (row >= ftcrfont_info->metrics_nrows)
81 ftcrfont_info->metrics =
82 xrealloc (ftcrfont_info->metrics,
83 sizeof (struct font_metrics *) * (row + 1));
84 memset (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows, 0,
85 (sizeof (struct font_metrics *)
86 * (row + 1 - ftcrfont_info->metrics_nrows)));
87 ftcrfont_info->metrics_nrows = row + 1;
89 if (ftcrfont_info->metrics[row] == NULL)
91 struct font_metrics *new;
92 int i;
94 new = xmalloc (sizeof (struct font_metrics) * METRICS_NCOLS_PER_ROW);
95 for (i = 0; i < METRICS_NCOLS_PER_ROW; i++)
96 METRICS_SET_STATUS (new + i, METRICS_INVALID);
97 ftcrfont_info->metrics[row] = new;
99 cache = ftcrfont_info->metrics[row] + col;
101 if (METRICS_STATUS (cache) == METRICS_INVALID)
102 ftfont_text_extents (font, &glyph, 1, cache);
104 if (metrics)
105 *metrics = *cache;
107 return cache->width;
110 static Lisp_Object
111 ftcrfont_list (struct frame *f, Lisp_Object spec)
113 Lisp_Object list = ftfont_list (f, spec), tail;
115 for (tail = list; CONSP (tail); tail = XCDR (tail))
116 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftcr);
117 return list;
120 static Lisp_Object
121 ftcrfont_match (struct frame *f, Lisp_Object spec)
123 Lisp_Object entity = ftfont_match (f, spec);
125 if (VECTORP (entity))
126 ASET (entity, FONT_TYPE_INDEX, Qftcr);
127 return entity;
130 static Lisp_Object
131 ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
133 Lisp_Object font_object;
134 struct font *font;
135 struct ftcrfont_info *ftcrfont_info;
136 FT_Face ft_face;
137 FT_UInt size;
139 block_input ();
140 size = XINT (AREF (entity, FONT_SIZE_INDEX));
141 if (size == 0)
142 size = pixel_size;
143 font_object = font_build_object (VECSIZE (struct ftcrfont_info),
144 Qftcr, entity, size);
145 font_object = ftfont_open2 (f, entity, pixel_size, font_object);
146 if (NILP (font_object)) return Qnil;
148 font = XFONT_OBJECT (font_object);
149 font->driver = &ftcrfont_driver;
150 ftcrfont_info = (struct ftcrfont_info *) font;
151 ft_face = ftcrfont_info->ft_size->face;
152 FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
153 FT_Activate_Size (ftcrfont_info->ft_size_draw);
154 FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
155 ftcrfont_info->cr_font_face =
156 cairo_ft_font_face_create_for_ft_face (ft_face, 0);
157 ftcrfont_info->metrics = NULL;
158 ftcrfont_info->metrics_nrows = 0;
159 unblock_input ();
161 return font_object;
164 static void
165 ftcrfont_close (struct font *font)
167 if (font_data_structures_may_be_ill_formed ())
168 return;
170 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
171 int i;
173 block_input ();
174 for (i = 0; i < ftcrfont_info->metrics_nrows; i++)
175 if (ftcrfont_info->metrics[i])
176 xfree (ftcrfont_info->metrics[i]);
177 if (ftcrfont_info->metrics)
178 xfree (ftcrfont_info->metrics);
179 FT_Done_Size (ftcrfont_info->ft_size_draw);
180 cairo_font_face_destroy (ftcrfont_info->cr_font_face);
181 unblock_input ();
183 ftfont_close (font);
186 static void
187 ftcrfont_text_extents (struct font *font,
188 unsigned *code,
189 int nglyphs,
190 struct font_metrics *metrics)
192 int width, i;
194 block_input ();
195 width = ftcrfont_glyph_extents (font, code[0], metrics);
196 for (i = 1; i < nglyphs; i++)
198 struct font_metrics m;
199 int w = ftcrfont_glyph_extents (font, code[i], metrics ? &m : NULL);
201 if (metrics)
203 if (width + m.lbearing < metrics->lbearing)
204 metrics->lbearing = width + m.lbearing;
205 if (width + m.rbearing > metrics->rbearing)
206 metrics->rbearing = width + m.rbearing;
207 if (m.ascent > metrics->ascent)
208 metrics->ascent = m.ascent;
209 if (m.descent > metrics->descent)
210 metrics->descent = m.descent;
212 width += w;
214 unblock_input ();
216 if (metrics)
217 metrics->width = width;
220 static int
221 ftcrfont_draw (struct glyph_string *s,
222 int from, int to, int x, int y, bool with_background)
224 struct frame *f = s->f;
225 struct face *face = s->face;
226 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) s->font;
227 cairo_t *cr;
228 cairo_glyph_t *glyphs;
229 cairo_surface_t *surface;
230 cairo_surface_type_t surface_type;
231 int len = to - from;
232 int i;
234 block_input ();
236 cr = x_begin_cr_clip (f, s->gc);
238 if (with_background)
240 x_set_cr_source_with_gc_background (f, s->gc);
241 cairo_rectangle (cr, x, y - FONT_BASE (face->font),
242 s->width, FONT_HEIGHT (face->font));
243 cairo_fill (cr);
246 glyphs = alloca (sizeof (cairo_glyph_t) * len);
247 for (i = 0; i < len; i++)
249 unsigned code = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
250 | XCHAR2B_BYTE2 (s->char2b + from + i));
252 glyphs[i].index = code;
253 glyphs[i].x = x;
254 glyphs[i].y = y;
255 x += (s->padding_p ? 1 : ftcrfont_glyph_extents (s->font, code, NULL));
258 x_set_cr_source_with_gc_foreground (f, s->gc);
259 cairo_set_font_face (cr, ftcrfont_info->cr_font_face);
260 cairo_set_font_size (cr, s->font->pixel_size);
261 /* cairo_set_font_matrix */
262 /* cairo_set_font_options */
264 FT_Activate_Size (ftcrfont_info->ft_size_draw);
265 cairo_show_glyphs (cr, glyphs, len);
266 surface = cairo_get_target (cr);
267 /* XXX: It used to be necessary to flush when exporting. It might
268 be the case that this is no longer necessary. */
269 surface_type = cairo_surface_get_type (surface);
270 if (surface_type != CAIRO_SURFACE_TYPE_XLIB
271 && (surface_type != CAIRO_SURFACE_TYPE_IMAGE
272 || cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32))
273 cairo_surface_flush (surface);
275 x_end_cr_clip (f);
277 unblock_input ();
279 return len;
284 struct font_driver const ftcrfont_driver =
286 .type = LISPSYM_INITIALLY (Qftcr),
287 .get_cache = ftfont_get_cache,
288 .list = ftcrfont_list,
289 .match = ftcrfont_match,
290 .list_family = ftfont_list_family,
291 .open = ftcrfont_open,
292 .close = ftcrfont_close,
293 .has_char = ftfont_has_char,
294 .encode_char = ftfont_encode_char,
295 .text_extents = ftcrfont_text_extents,
296 .draw = ftcrfont_draw,
297 .get_bitmap = ftfont_get_bitmap,
298 .anchor_point = ftfont_anchor_point,
299 #ifdef HAVE_LIBOTF
300 .otf_capability = ftfont_otf_capability,
301 #endif
302 #if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
303 .shape = ftfont_shape,
304 #endif
305 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
306 .get_variation_glyphs = ftfont_variation_glyphs,
307 #endif
308 .filter_properties = ftfont_filter_properties,
309 .combining_capability = ftfont_combining_capability,
312 void
313 syms_of_ftcrfont (void)
315 if (ftfont_info_size != offsetof (struct ftcrfont_info, cr_font_face))
316 abort ();
318 DEFSYM (Qftcr, "ftcr");
319 register_font_driver (&ftcrfont_driver, NULL);