* test/lisp/emacs-lisp/package-tests.el
[emacs.git] / src / ftcrfont.c
blob4326b775c8f23ec273aef61d22f6eab5190129d6
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 "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 struct font_driver ftcrfont_driver;
70 static int
71 ftcrfont_glyph_extents (struct font *font,
72 unsigned glyph,
73 struct font_metrics *metrics)
75 struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_info *) font;
76 int row, col;
77 struct font_metrics *cache;
79 row = glyph / METRICS_NCOLS_PER_ROW;
80 col = glyph % METRICS_NCOLS_PER_ROW;
81 if (row >= ftcrfont_info->metrics_nrows)
83 ftcrfont_info->metrics =
84 xrealloc (ftcrfont_info->metrics,
85 sizeof (struct font_metrics *) * (row + 1));
86 bzero (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows,
87 (sizeof (struct font_metrics *)
88 * (row + 1 - ftcrfont_info->metrics_nrows)));
89 ftcrfont_info->metrics_nrows = row + 1;
91 if (ftcrfont_info->metrics[row] == NULL)
93 struct font_metrics *new;
94 int i;
96 new = xmalloc (sizeof (struct font_metrics) * METRICS_NCOLS_PER_ROW);
97 for (i = 0; i < METRICS_NCOLS_PER_ROW; i++)
98 METRICS_SET_STATUS (new + i, METRICS_INVALID);
99 ftcrfont_info->metrics[row] = new;
101 cache = ftcrfont_info->metrics[row] + col;
103 if (METRICS_STATUS (cache) == METRICS_INVALID)
104 ftfont_driver.text_extents (font, &glyph, 1, cache);
106 if (metrics)
107 *metrics = *cache;
109 return cache->width;
112 static Lisp_Object
113 ftcrfont_list (struct frame *f, Lisp_Object spec)
115 Lisp_Object list = ftfont_driver.list (f, spec), tail;
117 for (tail = list; CONSP (tail); tail = XCDR (tail))
118 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftcr);
119 return list;
122 static Lisp_Object
123 ftcrfont_match (struct frame *f, Lisp_Object spec)
125 Lisp_Object entity = ftfont_driver.match (f, spec);
127 if (VECTORP (entity))
128 ASET (entity, FONT_TYPE_INDEX, Qftcr);
129 return entity;
132 extern FT_Face ftfont_get_ft_face (Lisp_Object);
134 static Lisp_Object
135 ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
137 Lisp_Object font_object;
138 struct font *font;
139 struct ftcrfont_info *ftcrfont_info;
140 FT_Face ft_face;
141 FT_UInt size;
143 block_input ();
144 size = XINT (AREF (entity, FONT_SIZE_INDEX));
145 if (size == 0)
146 size = pixel_size;
147 font_object = font_build_object (VECSIZE (struct ftcrfont_info),
148 Qftcr, entity, size);
149 font_object = ftfont_open2 (f, entity, pixel_size, font_object);
150 if (NILP (font_object)) return Qnil;
152 font = XFONT_OBJECT (font_object);
153 font->driver = &ftcrfont_driver;
154 ftcrfont_info = (struct ftcrfont_info *) font;
155 ft_face = ftcrfont_info->ft_size->face;
156 FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
157 FT_Activate_Size (ftcrfont_info->ft_size_draw);
158 FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
159 ftcrfont_info->cr_font_face =
160 cairo_ft_font_face_create_for_ft_face (ft_face, 0);
161 ftcrfont_info->metrics = NULL;
162 ftcrfont_info->metrics_nrows = 0;
163 unblock_input ();
165 return font_object;
168 static void
169 ftcrfont_close (struct font *font)
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_driver.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 void
286 syms_of_ftcrfont (void)
288 if (ftfont_info_size != offsetof (struct ftcrfont_info, cr_font_face))
289 abort ();
291 DEFSYM (Qftcr, "ftcr");
293 ftcrfont_driver = ftfont_driver;
294 ftcrfont_driver.type = Qftcr;
295 ftcrfont_driver.list = ftcrfont_list;
296 ftcrfont_driver.match = ftcrfont_match;
297 ftcrfont_driver.open = ftcrfont_open;
298 ftcrfont_driver.close = ftcrfont_close;
299 ftcrfont_driver.text_extents = ftcrfont_text_extents;
300 ftcrfont_driver.draw = ftcrfont_draw;
301 register_font_driver (&ftcrfont_driver, NULL);