all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob dc59c2bcadca10025d8cf20988584be5da3ef2a9 13010 bytes (raw)
name: src/ftcrfont.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
 
/* ftcrfont.c -- FreeType font driver on cairo.
   Copyright (C) 2015-2019 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */


#include <config.h>
#include <stdio.h>
#include <math.h>
#include <cairo-ft.h>

#include "lisp.h"
#include "xterm.h"
#include "blockinput.h"
#include "composite.h"
#include "font.h"
#include "ftfont.h"
#include "pdumper.h"

#define METRICS_NCOLS_PER_ROW	(128)

enum metrics_status
  {
    METRICS_INVALID = -1,    /* metrics entry is invalid */
  };

#define METRICS_STATUS(metrics)	((metrics)->ascent + (metrics)->descent)
#define METRICS_SET_STATUS(metrics, status) \
  ((metrics)->ascent = 0, (metrics)->descent = (status))

static int
ftcrfont_glyph_extents (struct font *font,
                        unsigned glyph,
                        struct font_metrics *metrics)
{
  struct font_info *ftcrfont_info = (struct font_info *) font;
  int row, col;
  struct font_metrics *cache;

  row = glyph / METRICS_NCOLS_PER_ROW;
  col = glyph % METRICS_NCOLS_PER_ROW;
  if (row >= ftcrfont_info->metrics_nrows)
    {
      ftcrfont_info->metrics =
	xrealloc (ftcrfont_info->metrics,
		  sizeof (struct font_metrics *) * (row + 1));
      memset (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows, 0,
	      (sizeof (struct font_metrics *)
	       * (row + 1 - ftcrfont_info->metrics_nrows)));
      ftcrfont_info->metrics_nrows = row + 1;
    }
  if (ftcrfont_info->metrics[row] == NULL)
    {
      struct font_metrics *new;
      int i;

      new = xmalloc (sizeof (struct font_metrics) * METRICS_NCOLS_PER_ROW);
      for (i = 0; i < METRICS_NCOLS_PER_ROW; i++)
	METRICS_SET_STATUS (new + i, METRICS_INVALID);
      ftcrfont_info->metrics[row] = new;
    }
  cache = ftcrfont_info->metrics[row] + col;

  if (METRICS_STATUS (cache) == METRICS_INVALID)
    {
      cairo_glyph_t cr_glyph = {.index = glyph};
      cairo_text_extents_t extents;

      FT_Activate_Size (ftcrfont_info->ft_size_draw);
      cairo_scaled_font_glyph_extents (ftcrfont_info->cr_scaled_font,
				       &cr_glyph, 1, &extents);
      cache->lbearing = floor (extents.x_bearing);
      cache->rbearing = ceil (extents.width + extents.x_bearing);
      cache->width = lround (extents.x_advance);
      cache->ascent = ceil (- extents.y_bearing);
      cache->descent = ceil (extents.height + extents.y_bearing);
    }

  if (metrics)
    *metrics = *cache;

  return cache->width;
}

static Lisp_Object
ftcrfont_list (struct frame *f, Lisp_Object spec)
{
  return ftfont_list2 (f, spec, Qftcr);
}

static Lisp_Object
ftcrfont_match (struct frame *f, Lisp_Object spec)
{
  return ftfont_match2 (f, spec, Qftcr);
}

static Lisp_Object
ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
{
  Lisp_Object font_object;

  FT_UInt size = XFIXNUM (AREF (entity, FONT_SIZE_INDEX));
  if (size == 0)
    size = pixel_size;
  font_object = font_build_object (VECSIZE (struct font_info),
				   AREF (entity, FONT_TYPE_INDEX),
				   entity, size);
  block_input ();
  font_object = ftfont_open2 (f, entity, pixel_size, font_object);
  if (FONT_OBJECT_P (font_object))
    {
      struct font *font = XFONT_OBJECT (font_object);
      struct font_info *ftcrfont_info = (struct font_info *) font;
      FT_Face ft_face = ftcrfont_info->ft_size->face;

#ifdef HAVE_HARFBUZZ
      if (EQ (AREF (font_object, FONT_TYPE_INDEX), Qftcrhb))
	font->driver = &ftcrhbfont_driver;
      else
#endif	/* HAVE_HARFBUZZ */
      font->driver = &ftcrfont_driver;
      FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
      FT_Activate_Size (ftcrfont_info->ft_size_draw);
      if (ftcrfont_info->bitmap_strike_index < 0)
	FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
      else
	FT_Select_Size (ft_face, ftcrfont_info->bitmap_strike_index);
      cairo_font_face_t *font_face =
	cairo_ft_font_face_create_for_ft_face (ft_face, 0);
      cairo_matrix_t font_matrix, ctm;
      cairo_matrix_init_scale (&font_matrix, pixel_size, pixel_size);
      cairo_matrix_init_identity (&ctm);
      cairo_font_options_t *options = cairo_font_options_create ();
      ftcrfont_info->cr_scaled_font =
	cairo_scaled_font_create (font_face, &font_matrix, &ctm, options);
      cairo_font_face_destroy (font_face);
      cairo_font_options_destroy (options);
      ftcrfont_info->metrics = NULL;
      ftcrfont_info->metrics_nrows = 0;
      if (ftcrfont_info->bitmap_strike_index >= 0)
	{
	  /* Several members of struct font/font_info set by
	     ftfont_open2 are bogus.  Recalculate them with cairo
	     scaled font functions.  */
	  cairo_font_extents_t extents;
	  cairo_scaled_font_extents (ftcrfont_info->cr_scaled_font, &extents);
	  font->ascent = lround (extents.ascent);
	  font->descent = lround (extents.descent);
	  font->height = lround (extents.height);

	  cairo_glyph_t stack_glyph;
	  int n = 0;
	  font->min_width = font->average_width = font->space_width = 0;
	  for (char c = 32; c < 127; c++)
	    {
	      cairo_glyph_t *glyphs = &stack_glyph;
	      int num_glyphs = 1;
	      cairo_status_t status =
		cairo_scaled_font_text_to_glyphs (ftcrfont_info->cr_scaled_font,
						  0, 0, &c, 1,
						  &glyphs, &num_glyphs,
						  NULL, NULL, NULL);

	      if (status == CAIRO_STATUS_SUCCESS)
		{
		  if (glyphs != &stack_glyph)
		    cairo_glyph_free (glyphs);
		  else
		    {
		      int this_width =
			ftcrfont_glyph_extents (font, stack_glyph.index, NULL);

		      if (this_width > 0
			  && (! font->min_width
			      || font->min_width > this_width))
			font->min_width = this_width;
		      if (c == 32)
			font->space_width = this_width;
		      font->average_width += this_width;
		      n++;
		    }
		}
	    }
	  if (n > 0)
	    font->average_width /= n;

	  font->underline_position = -1;
	  font->underline_thickness = 0;
	}
    }
  unblock_input ();

  return font_object;
}

static void
ftcrfont_close (struct font *font)
{
  if (font_data_structures_may_be_ill_formed ())
    return;

  struct font_info *ftcrfont_info = (struct font_info *) font;
  int i;

  block_input ();
  for (i = 0; i < ftcrfont_info->metrics_nrows; i++)
    if (ftcrfont_info->metrics[i])
      xfree (ftcrfont_info->metrics[i]);
  if (ftcrfont_info->metrics)
    xfree (ftcrfont_info->metrics);
  FT_Done_Size (ftcrfont_info->ft_size_draw);
  cairo_scaled_font_destroy (ftcrfont_info->cr_scaled_font);
  unblock_input ();

  ftfont_close (font);
}

static void
ftcrfont_text_extents (struct font *font,
                       unsigned *code,
                       int nglyphs,
                       struct font_metrics *metrics)
{
  int width, i;

  block_input ();
  width = ftcrfont_glyph_extents (font, code[0], metrics);
  for (i = 1; i < nglyphs; i++)
    {
      struct font_metrics m;
      int w = ftcrfont_glyph_extents (font, code[i], metrics ? &m : NULL);

      if (metrics)
	{
	  if (width + m.lbearing < metrics->lbearing)
	    metrics->lbearing = width + m.lbearing;
	  if (width + m.rbearing > metrics->rbearing)
	    metrics->rbearing = width + m.rbearing;
	  if (m.ascent > metrics->ascent)
	    metrics->ascent = m.ascent;
	  if (m.descent > metrics->descent)
	    metrics->descent = m.descent;
	}
      width += w;
    }
  unblock_input ();

  if (metrics)
    metrics->width = width;
}

static int
ftcrfont_get_bitmap (struct font *font, unsigned int code,
		     struct font_bitmap *bitmap, int bits_per_pixel)
{
  struct font_info *ftcrfont_info = (struct font_info *) font;

  if (ftcrfont_info->bitmap_strike_index < 0)
    return ftfont_get_bitmap (font, code, bitmap, bits_per_pixel);

  return -1;
}

static int
ftcrfont_anchor_point (struct font *font, unsigned int code, int idx,
		       int *x, int *y)
{
  struct font_info *ftcrfont_info = (struct font_info *) font;

  if (ftcrfont_info->bitmap_strike_index < 0)
    return ftfont_anchor_point (font, code, idx, x, y);

  return -1;
}

static Lisp_Object
ftcrfont_shape (Lisp_Object lgstring, Lisp_Object direction)
{
#if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
  struct font *font = CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring));
  struct font_info *ftcrfont_info = (struct font_info *) font;

  if (ftcrfont_info->bitmap_strike_index < 0)
    return ftfont_shape (lgstring, direction);
#endif

  return make_fixnum (0);
}

static int
ftcrfont_draw (struct glyph_string *s,
               int from, int to, int x, int y, bool with_background)
{
  struct frame *f = s->f;
  struct face *face = s->face;
  struct font_info *ftcrfont_info = (struct font_info *) s->font;
  cairo_t *cr;
  cairo_glyph_t *glyphs;
  cairo_surface_t *surface;
  cairo_surface_type_t surface_type;
  int len = to - from;
  int i;

  block_input ();

  cr = x_begin_cr_clip (f, s->gc);

  if (with_background)
    {
      x_set_cr_source_with_gc_background (f, s->gc);
      cairo_rectangle (cr, x, y - FONT_BASE (face->font),
		       s->width, FONT_HEIGHT (face->font));
      cairo_fill (cr);
    }

  glyphs = alloca (sizeof (cairo_glyph_t) * len);
  for (i = 0; i < len; i++)
    {
      unsigned code = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
		       | XCHAR2B_BYTE2 (s->char2b + from + i));

      glyphs[i].index = code;
      glyphs[i].x = x;
      glyphs[i].y = y;
      x += (s->padding_p ? 1 : ftcrfont_glyph_extents (s->font, code, NULL));
    }

  x_set_cr_source_with_gc_foreground (f, s->gc);
  cairo_set_scaled_font (cr, ftcrfont_info->cr_scaled_font);

  FT_Activate_Size (ftcrfont_info->ft_size_draw);
  cairo_show_glyphs (cr, glyphs, len);
  surface = cairo_get_target (cr);
  /* XXX: It used to be necessary to flush when exporting.  It might
     be the case that this is no longer necessary.  */
  surface_type = cairo_surface_get_type (surface);
  if (surface_type != CAIRO_SURFACE_TYPE_XLIB
      && (surface_type != CAIRO_SURFACE_TYPE_IMAGE
	  || cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32))
    cairo_surface_flush (surface);

  x_end_cr_clip (f);

  unblock_input ();

  return len;
}

#ifdef HAVE_HARFBUZZ

static Lisp_Object
ftcrhbfont_list (struct frame *f, Lisp_Object spec)
{
  return ftfont_list2 (f, spec, Qftcrhb);
}

static Lisp_Object
ftcrhbfont_match (struct frame *f, Lisp_Object spec)
{
  return ftfont_match2 (f, spec, Qftcrhb);
}

static hb_font_t *
ftcrhbfont_begin_hb_font (struct font *font, double *position_unit)
{
  struct font_info *ftcrfont_info = (struct font_info *) font;

  FT_Activate_Size (ftcrfont_info->ft_size_draw);
  hb_font_t *hb_font = fthbfont_begin_hb_font (font, position_unit);
  int i = ftcrfont_info->bitmap_strike_index;
  if (i >= 0)
    {
      FT_Face ft_face = ftcrfont_info->ft_size_draw->face;
      *position_unit = ((double) font->height
			/ ft_face->available_sizes[i].height) / (1 << 6);
    }

  return hb_font;
}

#endif	/* HAVE_HARFBUZZ */
\f

static void syms_of_ftcrfont_for_pdumper (void);

struct font_driver const ftcrfont_driver =
  {
  .type = LISPSYM_INITIALLY (Qftcr),
  .get_cache = ftfont_get_cache,
  .list = ftcrfont_list,
  .match = ftcrfont_match,
  .list_family = ftfont_list_family,
  .open = ftcrfont_open,
  .close = ftcrfont_close,
  .has_char = ftfont_has_char,
  .encode_char = ftfont_encode_char,
  .text_extents = ftcrfont_text_extents,
  .draw = ftcrfont_draw,
  .get_bitmap = ftcrfont_get_bitmap,
  .anchor_point = ftcrfont_anchor_point,
#ifdef HAVE_LIBOTF
  .otf_capability = ftfont_otf_capability,
#endif
  .shape = ftcrfont_shape,
#ifdef HAVE_OTF_GET_VARIATION_GLYPHS
  .get_variation_glyphs = ftfont_variation_glyphs,
#endif
  .filter_properties = ftfont_filter_properties,
  .combining_capability = ftfont_combining_capability,
  };
#ifdef HAVE_HARFBUZZ
struct font_driver ftcrhbfont_driver;
#endif	/* HAVE_HARFBUZZ */

void
syms_of_ftcrfont (void)
{
  DEFSYM (Qftcr, "ftcr");
#ifdef HAVE_HARFBUZZ
  DEFSYM (Qftcrhb, "ftcrhb");
#endif	/* HAVE_HARFBUZZ */
  pdumper_do_now_and_after_load (syms_of_ftcrfont_for_pdumper);
}

static void
syms_of_ftcrfont_for_pdumper (void)
{
  register_font_driver (&ftcrfont_driver, NULL);
#ifdef HAVE_HARFBUZZ
  ftcrhbfont_driver = ftcrfont_driver;
  ftcrhbfont_driver.type = Qftcrhb;
  ftcrhbfont_driver.list = ftcrhbfont_list;
  ftcrhbfont_driver.match = ftcrhbfont_match;
  ftcrhbfont_driver.shape = fthbfont_shape;
  ftcrhbfont_driver.combining_capability = fthbfont_combining_capability;
  ftcrhbfont_driver.begin_hb_font = ftcrhbfont_begin_hb_font;
  register_font_driver (&ftcrhbfont_driver, NULL);
#endif	/* HAVE_HARFBUZZ */
}

debug log:

solving dc59c2bcadc ...
found dc59c2bcadc in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.