1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-05 11:21:04 -08:00
emacs/src/ftcrfont.c
Paul Eggert 63cfb75f3a Include-file cleanup for src directory
Omit ‘#include "foo.h"’ unless the file needs foo.h (Bug#21707).
In a few cases, add ‘#include "foo.h"’ if the file needs foo.h
but does not include it directly.  As a general rule, a source
file should include foo.h if it needs the interfaces that foo.h
defines.
* src/alloc.c: Don’t include process.h.  Include dispextern.h,
systime.h.
* src/atimer.c: Don’t include blockinput.h.
* src/buffer.c: Include coding.h, systime.h.  Don’t include
keyboard.h, coding.h.
* src/callint.c: Don’t include commands.h, keymap.h.
* src/callproc.c: Don’t include character.h, ccl.h, composite.h,
systty.h, termhooks.h.
* src/casetab.c: Don’t include character.h.
* src/category.c: Don’t include charset.h, keymap.h.
* src/ccl.h: Don’t include character.h.
* src/character.c: Don’t include charset.h.
* src/charset.c: Don’t include disptab.h.
* src/chartab.c: Don’t include ccl.h.
* src/cm.c: Don’t include frame.h, termhooks.h.
* src/cmds.c: Don’t include window.h, dispextern.h.
* src/coding.c: Don’t include window.h, frame.h.
* src/composite.c: Include composite.h.  Don’t include window.h,
font.h.
* src/data.c: Don’t include syssignal.h, termhooks.h, font.h.
* src/dbusbind.c: Don’t include frame.h.
* src/decompress.c: Don’t include character.h.
* src/dired.c: Don’t include character.h, commands.h, charset.h.
* src/dispnew.c: Don’t include character.h, indent.h, intervals.h,
process.h, timespec.h.  Include systime.h.
* src/doc.c: Include coding.h.  Don’t include keyboard.h.
* src/editfns.c: Include composite.h.  Don’t include frame.h.
* src/emacs.c: Include fcntl.h, coding.h.  Don’t include
commands.h, systty.h..
* src/fileio.c: Don’t include intervals.h, dispextern.h.
Include composite.h.
* src/filelock.c: Don’t include character.h, systime.h.
* src/fns.c: Don’t include time.h, commands.h, keyboard.h,
keymap.h, frame.h, blockinput.h, xterm.h.  Include composite.h.
* src/font.c: Include termhooks.h.
* src/font.h: Don’t include ccl.h, frame.h.  Add forward decls of
struct composition_it, struct face, struct glyph_string.
* src/fontset.c: Don’t include buffer.h, ccl.h, keyboard.h,
intervals.h, window.h, termhooks.h.
* src/frame.c: Don’t include character.h, commands.h, font.h.
* src/frame.h: Don’t include dispextern.h.
* src/fringe.c: Don’t include character.h.
* src/ftcrfont.c: Don’t include dispextern.h, frame.h,
character.h, charset.h, fontset.h.
* src/ftfont.c: Don’t include frame.h, blockinput.h, coding.h,
fontset.h.
* src/ftxfont.c: Don’t include dispextern.h, character.h,
charset.h, fontset.h.
* src/gfilenotify.c: Don’t include frame.h, process.h.
* src/gtkutil.c: Include dispextern.h, frame.h, systime.h.
Don’t include syssignal.h, buffer.h, charset.h, font.h.
* src/gtkutil.h: Don’t include frame.h.
* src/image.c: Include fcntl.h and stdio.h instead of sysstdio.h.
Don’t include character.h.
* src/indent.c: Don’t include keyboard.h, termchar.h.
* src/inotify.c: Don’t include character.h, frame.h.
* src/insdel.c: Include composite.h.  Don’t include blockinput.h.
* src/intervals.c: Don’t include character.h, keyboard.h.
* src/intervals.h: Don’t include dispextern.h, composite.h.
* src/keyboard.c: Don’t include sysstdio.h, disptab.h, puresize.h.
Include coding.h.
* src/keyboard.h: Don’t incldue systime.h.
* src/keymap.c: Don’t include charset.h, frame.h.
* src/lread.c: Include dispextern.h and systime.h.
Don’t include frame.h.  Include systime.h.
* src/macros.c: Don’t include commands.h, character.h, buffer.h.
* src/menu.c: Include character.h, coding.h.  Don’t include
dispextern.h.
* src/menu.h: Don’t include systime.h.
* src/minibuf.c: Don’t include commands.h, dispextern.h, syntax.h,
intervals.h, termhooks.h.
* src/print.c: Include coding.h.  Don’t include keyboard.h,
window.h, dispextern.h, termchar.h, termhooks.h, font.h.
Add forward decl of struct terminal.
* src/process.c: Don’t include termhooks.h, commands.h,
dispextern.h, composite.h.
* src/region-cache.c: Don’t include character.h.
* src/scroll.c: Don’t include keyboard.h, window.h.
* src/search.c: Don’t include category.h, commands.h.
* src/sound.c: Don’t include dispextern.h.
* src/syntax.c: Don’t include command.h, keymap.h.
* src/sysdep.c: Don’t include window.h, dispextern.h.
* src/systime.h: Use ‘#ifdef emacs’, not ‘#ifdef EMACS_LISP_H’,
* src/term.c: Don’t include systty.h, intervals.h, xterm.h.
* src/terminal.c: Include character.h.
Don’t include charset.h, coding.h.
* src/textprop.c: Don’t include character.h.
* src/undo.c: Don’t include character.h, commands.h, window.h.
* src/unexsol.c: Don’t include character.h, charset.h.
* src/widget.c: Include widget.h.  Don’t include keyboard.h,
window.h, dispextern.h, blockinput.h, character.h, font.h.
* src/widgetprv.h: Don’t include widget.h.
* src/window.c: Don’t include character.h, menu.h, intervals.h.
* src/xdisp.c: Include composite.h, systime.h.  Don’t include
macros.h, process.h.
* src/xfaces.c: Don’t include charset.h, keyboard.h, termhooks.h,
intervals.h.
* src/xfns.c: Don’t include menu.h, character.h, intervals.h,
epaths.h, fontset.h, systime.h, atimer.h, termchar.h.
* src/xfont.c: Don’t include dispextern.h, fontset.h, ccl.h.
* src/xftfont.c: Don’t include dispextern.h, character.h, fontset.h.
* src/xgselect.c: Don’t include timespec.h, frame.h.
Include systime.h.
* src/xgselect.h: Don’t include time.h.
Use a forward decl to struct timespec instead.
* src/xmenu.c: Don’t include keymap.h, character.h, charset.h,
dispextern.h.  Include systime.h.
* src/xml.c: Don’t include character.h.
* src/xrdb.c [USE_MOTIF]: Don’t include keyboard.h.
* src/xselect.c: Don’t include dispextern.h, character.h,
buffer.h, process.h.
* src/xsmfns.c: Don’t include systime.h, sysselect.h.
* src/xterm.c: Don’t include syssignal.h, charset.h, disptab.h,
intervals.h process.h, keymap.h, xgselect.h.  Include composite.h.
2015-10-20 18:35:53 -07:00

302 lines
8.3 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ftcrfont.c -- FreeType font driver on cairo.
Copyright (C) 2015 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 <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdio.h>
#include <cairo-ft.h>
#include "lisp.h"
#include "xterm.h"
#include "blockinput.h"
#include "font.h"
#include "ftfont.h"
/* FTCR font driver. */
/* The actual structure for FTCR font. A pointer to this structure
can be cast to struct font *. */
struct ftcrfont_info
{
struct font font;
/* The following six members must be here in this order to be
compatible with struct ftfont_info (in ftfont.c). */
#ifdef HAVE_LIBOTF
bool maybe_otf; /* Flag to tell if this may be OTF or not. */
OTF *otf;
#endif /* HAVE_LIBOTF */
FT_Size ft_size;
int index;
FT_Matrix matrix;
cairo_font_face_t *cr_font_face;
/* To prevent cairo from cluttering the activated FT_Size maintained
in ftfont.c, we activate this special FT_Size before drawing. */
FT_Size ft_size_draw;
/* Font metrics cache. */
struct font_metrics **metrics;
short metrics_nrows;
};
#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))
struct font_driver ftcrfont_driver;
static int
ftcrfont_glyph_extents (struct font *font,
unsigned glyph,
struct font_metrics *metrics)
{
struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_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));
bzero (ftcrfont_info->metrics + ftcrfont_info->metrics_nrows,
(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)
ftfont_driver.text_extents (font, &glyph, 1, cache);
if (metrics)
*metrics = *cache;
return cache->width;
}
static Lisp_Object
ftcrfont_list (struct frame *f, Lisp_Object spec)
{
Lisp_Object list = ftfont_driver.list (f, spec), tail;
for (tail = list; CONSP (tail); tail = XCDR (tail))
ASET (XCAR (tail), FONT_TYPE_INDEX, Qftcr);
return list;
}
static Lisp_Object
ftcrfont_match (struct frame *f, Lisp_Object spec)
{
Lisp_Object entity = ftfont_driver.match (f, spec);
if (VECTORP (entity))
ASET (entity, FONT_TYPE_INDEX, Qftcr);
return entity;
}
extern FT_Face ftfont_get_ft_face (Lisp_Object);
static Lisp_Object
ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
{
Lisp_Object font_object;
struct font *font;
struct ftcrfont_info *ftcrfont_info;
FT_Face ft_face;
FT_UInt size;
block_input ();
size = XINT (AREF (entity, FONT_SIZE_INDEX));
if (size == 0)
size = pixel_size;
font_object = font_build_object (VECSIZE (struct ftcrfont_info),
Qftcr, entity, size);
font_object = ftfont_open2 (f, entity, pixel_size, font_object);
if (NILP (font_object)) return Qnil;
font = XFONT_OBJECT (font_object);
font->driver = &ftcrfont_driver;
ftcrfont_info = (struct ftcrfont_info *) font;
ft_face = ftcrfont_info->ft_size->face;
FT_New_Size (ft_face, &ftcrfont_info->ft_size_draw);
FT_Activate_Size (ftcrfont_info->ft_size_draw);
FT_Set_Pixel_Sizes (ft_face, 0, font->pixel_size);
ftcrfont_info->cr_font_face =
cairo_ft_font_face_create_for_ft_face (ft_face, 0);
ftcrfont_info->metrics = NULL;
ftcrfont_info->metrics_nrows = 0;
unblock_input ();
return font_object;
}
static void
ftcrfont_close (struct font *font)
{
struct ftcrfont_info *ftcrfont_info = (struct ftcrfont_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_font_face_destroy (ftcrfont_info->cr_font_face);
unblock_input ();
ftfont_driver.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_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 ftcrfont_info *ftcrfont_info = (struct ftcrfont_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_font_face (cr, ftcrfont_info->cr_font_face);
cairo_set_font_size (cr, s->font->pixel_size);
/* cairo_set_font_matrix */
/* cairo_set_font_options */
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;
}
void
syms_of_ftcrfont (void)
{
if (ftfont_info_size != offsetof (struct ftcrfont_info, cr_font_face))
abort ();
DEFSYM (Qftcr, "ftcr");
ftcrfont_driver = ftfont_driver;
ftcrfont_driver.type = Qftcr;
ftcrfont_driver.list = ftcrfont_list;
ftcrfont_driver.match = ftcrfont_match;
ftcrfont_driver.open = ftcrfont_open;
ftcrfont_driver.close = ftcrfont_close;
ftcrfont_driver.text_extents = ftcrfont_text_extents;
ftcrfont_driver.draw = ftcrfont_draw;
register_font_driver (&ftcrfont_driver, NULL);
}