diff options
author | YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp> | 2019-04-24 12:31:37 +0900 |
---|---|---|
committer | YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp> | 2019-04-24 12:31:37 +0900 |
commit | 5f4e8e2e088de9fb76cb631077c6eddd3219f594 (patch) | |
tree | abb80bc8ec36c28d4eef08b92db90a142f641be6 /lwlib | |
parent | 1828e9a9b7481572448284a8e5925bf97f2145f7 (diff) | |
download | emacs-5f4e8e2e088de9fb76cb631077c6eddd3219f594.tar.gz |
Don't link libXft when using cairo
* configure.ac: Check cairo early. Don't try Xft if cairo is used.
* lwlib/lwlib-utils.h [USE_CAIRO]: Include cairo.h and fontconfig.h.
(XftFont, XftDraw, XftColor, XGlyphInfo) [USE_CAIRO]: New typedefs.
(XftFontOpenName, XftFontClose, XftDrawCreate, XftDrawDestroy)
(XftDrawRect, XftDrawStringUtf8, XftTextExtentsUtf8) [USE_CAIRO]: New macros.
(crxft_font_open_name, crxft_font_close, crxft_draw_create)
(crxft_draw_rect, crxft_draw_string, crxft_text_extents) [USE_CAIRO]: New
externs.
* lwlib/lwlib-utils.c [USE_CAIRO]: Include math.h, cairo-ft.h, and
cairo-xlib.h.
(crxft_font_open_name, crxft_font_close, crxft_draw_create)
(crxft_set_source_color, crxft_draw_rect, crxft_draw_string)
(crxft_text_extents) [USE_CAIRO]: New Xft compatibility functions.
* lwlib/xlwmenuP.h [USE_CAIRO]: Include lwlib-utils.h.
* lwlib/xlwmenu.c (display_menu_item) [USE_CAIRO]: Call
cairo_surface_mark_dirty and cairo_surface_flush.
* lwlib/lwlib-Xaw.c [USE_CAIRO]: Include stdlib.h and lwlib-utils.h.
(draw_text) [USE_CAIRO]: Call cairo_surface_flush.
* src/xsettings.c [USE_CAIRO]: Include fontconfig.h
(apply_xft_settings) [!HAVE_XFT]: Don't call XftDefaultSubstitute or
XftDefaultSet.
* lwlib/lwlib-Xaw.c:
* lwlib/lwlib-int.h:
* lwlib/xlwmenu.c:
* lwlib/xlwmenuP.h:
* src/xrdb.c:
* src/xsettings.c:
* src/xterm.c: Replace all #ifdef HAVE_XFT with #if defined USE_CAIRO ||
defined HAVE_XFT.
* src/xfns.c (x_default_font_parameter): Replace #ifdef HAVE_XFT with #if
defined USE_CAIRO || defined HAVE_XFT.
Diffstat (limited to 'lwlib')
-rw-r--r-- | lwlib/lwlib-Xaw.c | 28 | ||||
-rw-r--r-- | lwlib/lwlib-int.h | 2 | ||||
-rw-r--r-- | lwlib/lwlib-utils.c | 140 | ||||
-rw-r--r-- | lwlib/lwlib-utils.h | 45 | ||||
-rw-r--r-- | lwlib/xlwmenu.c | 43 | ||||
-rw-r--r-- | lwlib/xlwmenuP.h | 10 |
6 files changed, 236 insertions, 32 deletions
diff --git a/lwlib/lwlib-Xaw.c b/lwlib/lwlib-Xaw.c index a00f8aa73f9..0801c944008 100644 --- a/lwlib/lwlib-Xaw.c +++ b/lwlib/lwlib-Xaw.c @@ -50,8 +50,13 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ #include <X11/Xatom.h> -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT +#ifdef USE_CAIRO +#include <stdlib.h> +#include "lwlib-utils.h" +#else /* HAVE_XFT */ #include <X11/Xft/Xft.h> +#endif struct widget_xft_data { @@ -79,7 +84,7 @@ lw_xaw_widget_p (Widget widget) } -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT static void fill_xft_data (struct widget_xft_data *data, Widget widget, XftFont *font) { @@ -210,6 +215,9 @@ draw_text (struct widget_xft_data *data, char *lbl, int inverse) /* 1.2 gives reasonable line spacing. */ y += data->xft_font->height * 1.2; } +#ifdef USE_CAIRO + cairo_surface_flush (cairo_get_target (data->xft_draw)); +#endif } @@ -307,7 +315,7 @@ xaw_update_one_widget (widget_instance *instance, if (XtIsSubclass (widget, dialogWidgetClass)) { -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (instance->xft_data && instance->xft_data[0].xft_font) { set_text (&instance->xft_data[0], instance->parent, @@ -339,7 +347,7 @@ xaw_update_one_widget (widget_instance *instance, XtSetArg (al[ac], XtNlabel, val->value);ac++; /* Force centered button text. Se above. */ XtSetArg (al[ac], XtNjustify, XtJustifyCenter);ac++; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (instance->xft_data && instance->xft_data[0].xft_font) { int th; @@ -473,7 +481,7 @@ static XtActionsRec xaw_actions [] = { }; static Boolean actions_initted = False; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT static XtActionsRec button_actions[] = { { "my_reset", command_reset }, @@ -506,7 +514,7 @@ make_dialog (char* name, Widget dialog; Widget button; XtTranslations override; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT XftFont *xft_font = 0; XtTranslations button_override; #endif @@ -521,7 +529,7 @@ make_dialog (char* name, XtAppContext app = XtWidgetToApplicationContext (parent); XtAppAddActions (app, xaw_actions, sizeof (xaw_actions) / sizeof (xaw_actions[0])); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT XtAppAddActions (app, button_actions, sizeof (button_actions) / sizeof (button_actions[0])); #endif @@ -546,7 +554,7 @@ make_dialog (char* name, override = XtParseTranslationTable (dialogOverride); XtOverrideTranslations (dialog, override); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT { int num; Widget *ch = NULL; @@ -618,7 +626,7 @@ make_dialog (char* name, sprintf (button_name, "button%d", ++bc); button = XtCreateManagedWidget (button_name, commandWidgetClass, dialog, av, ac); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (xft_font) { fill_xft_data (&instance->xft_data[bc], button, xft_font); @@ -651,7 +659,7 @@ make_dialog (char* name, sprintf (button_name, "button%d", ++bc); button = XtCreateManagedWidget (button_name, commandWidgetClass, dialog, av, ac); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (xft_font) { fill_xft_data (&instance->xft_data[bc], button, xft_font); diff --git a/lwlib/lwlib-int.h b/lwlib/lwlib-int.h index 28b1fb9508d..9f788e7c0c2 100644 --- a/lwlib/lwlib-int.h +++ b/lwlib/lwlib-int.h @@ -30,7 +30,7 @@ typedef struct _widget_instance Widget widget; Widget parent; Boolean pop_up_p; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT struct widget_xft_data *xft_data; #endif struct _widget_info* info; diff --git a/lwlib/lwlib-utils.c b/lwlib/lwlib-utils.c index 7c60bdb056d..2c0a931f9cb 100644 --- a/lwlib/lwlib-utils.c +++ b/lwlib/lwlib-utils.c @@ -137,3 +137,143 @@ XtWidgetBeingDestroyedP (Widget widget) { return widget->core.being_destroyed; } + +#ifdef USE_CAIRO +/* Xft emulation on cairo. */ +#include <math.h> +#include <cairo-ft.h> +#include <cairo-xlib.h> + +XftFont * +crxft_font_open_name (Display *dpy, int screen, const char *name) +{ + XftFont *pub = NULL; + FcPattern *pattern = FcNameParse ((FcChar8 *) name); + if (pattern) + { + FcConfigSubstitute (NULL, pattern, FcMatchPattern); + double dpi; + if (FcPatternGetDouble (pattern, FC_DPI, 0, &dpi) == FcResultNoMatch) + { + char *v = XGetDefault (dpy, "Xft", FC_DPI); + if (v == NULL || sscanf (v, "%lf", &dpi) != 1) + dpi = ((DisplayHeight (dpy, screen) * 25.4) + / DisplayHeightMM (dpy, screen)); + FcPatternAddDouble (pattern, FC_DPI, dpi); + } + FcDefaultSubstitute (pattern); + cairo_font_face_t *font_face + = cairo_ft_font_face_create_for_pattern (pattern); + if (font_face) + { + double pixel_size; + if ((FcPatternGetDouble (pattern, FC_PIXEL_SIZE, 0, &pixel_size) + != FcResultMatch) + || pixel_size < 1) + pixel_size = 10; + + pub = xmalloc (sizeof (*pub)); + 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 (); + cairo_ft_font_options_substitute (options, pattern); + pub->scaled_font = cairo_scaled_font_create (font_face, &font_matrix, + &ctm, options); + cairo_font_face_destroy (font_face); + cairo_font_options_destroy (options); + + cairo_font_extents_t extents; + cairo_scaled_font_extents (pub->scaled_font, &extents); + pub->ascent = lround (extents.ascent); + pub->descent = lround (extents.descent); + pub->height = lround (extents.height); + pub->max_advance_width = lround (extents.max_x_advance); + } + FcPatternDestroy (pattern); + } + return pub; +} + +void +crxft_font_close (XftFont *pub) +{ + cairo_scaled_font_destroy (pub->scaled_font); + xfree (pub); +} + +cairo_t * +crxft_draw_create (Display *dpy, Drawable drawable, Visual *visual) +{ + cairo_t *cr = NULL; + Window root; + int x, y; + unsigned int width, height, border_width, depth; + + if (!XGetGeometry (dpy, drawable, &root, &x, &y, &width, &height, + &border_width, &depth)) + return NULL; + + cairo_surface_t *surface = cairo_xlib_surface_create (dpy, drawable, visual, + width, height); + if (surface) + { + cr = cairo_create (surface); + cairo_surface_destroy (surface); + } + + return cr; +} + +static void +crxft_set_source_color (cairo_t *cr, const XftColor *color) +{ + cairo_set_source_rgba (cr, color->color.red / 65535.0, + color->color.green / 65535.0, + color->color.blue / 65535.0, + color->color.alpha / 65535.0); +} + +void +crxft_draw_rect (cairo_t *cr, const XftColor *color, int x, int y, + unsigned int width, unsigned int height) +{ + crxft_set_source_color (cr, color); + cairo_rectangle (cr, x, y, width, height); + cairo_fill (cr); +} + +void +crxft_draw_string (cairo_t *cr, const XftColor *color, XftFont *pub, + int x, int y, const FcChar8 *string, int len) +{ + char *buf = xmalloc (len + 1); + memcpy (buf, string, len); + buf[len] = '\0'; + crxft_set_source_color (cr, color); + cairo_set_scaled_font (cr, pub->scaled_font); + cairo_move_to (cr, x, y); + cairo_show_text (cr, buf); + xfree (buf); +} + +void +crxft_text_extents (XftFont *pub, const FcChar8 *string, int len, + XGlyphInfo *extents) +{ + char *buf = xmalloc (len + 1); + memcpy (buf, string, len); + buf[len] = '\0'; + cairo_text_extents_t text_extents; + cairo_scaled_font_text_extents (pub->scaled_font, buf, &text_extents); + xfree (buf); + extents->x = ceil (- text_extents.x_bearing); + extents->y = ceil (- text_extents.y_bearing); + extents->width = (ceil (text_extents.x_bearing + text_extents.width) + + extents->x); + extents->height = (ceil (text_extents.y_bearing + text_extents.height) + + extents->y); + extents->xOff = lround (text_extents.x_advance); + extents->yOff = lround (text_extents.y_advance); +} +#endif /* USE_CAIRO */ diff --git a/lwlib/lwlib-utils.h b/lwlib/lwlib-utils.h index ec3daab94ef..64372f19ad2 100644 --- a/lwlib/lwlib-utils.h +++ b/lwlib/lwlib-utils.h @@ -15,4 +15,49 @@ Widget *XtCompositeChildren (Widget, unsigned int *); Boolean XtWidgetBeingDestroyedP (Widget widget); +#ifdef USE_CAIRO + +#include <cairo.h> +#include <fontconfig/fontconfig.h> + +typedef struct { + cairo_scaled_font_t *scaled_font; + int ascent, descent, height, max_advance_width; +} XftFont; + +typedef cairo_t XftDraw; + +typedef struct { + unsigned long pixel; + struct {unsigned short red, green, blue, alpha;} color; +} XftColor; + +#ifdef HAVE_XRENDER +#include <X11/extensions/Xrender.h> +#else +typedef struct { + unsigned short width, height; + short x, y, xOff, yOff; +} XGlyphInfo; +#endif + +#define XftFontOpenName crxft_font_open_name +extern XftFont *crxft_font_open_name (Display *, int, const char *); +#define XftFontClose(dpy, pub) crxft_font_close (pub) +extern void crxft_font_close (XftFont *); +#define XftDrawCreate(dpy, drawable, visual, colormap) \ + crxft_draw_create (dpy, drawable, visual) +extern cairo_t *crxft_draw_create (Display *, Drawable, Visual *); +#define XftDrawDestroy cairo_destroy +#define XftDrawRect crxft_draw_rect +extern void crxft_draw_rect (cairo_t *, const XftColor *, int, int, + unsigned int, unsigned int); +#define XftDrawStringUtf8 crxft_draw_string +extern void crxft_draw_string (cairo_t *, const XftColor *, XftFont *, + int, int, const FcChar8 *, int); +#define XftTextExtentsUtf8(dpy, pub, string, len, extents) \ + crxft_text_extents (pub, string, len, extents) +extern void crxft_text_extents (XftFont *, const FcChar8 *, int, XGlyphInfo *); + +#endif /* USE_CAIRO */ #endif /* _LWLIB_UTILS_H_ */ diff --git a/lwlib/xlwmenu.c b/lwlib/xlwmenu.c index a5704cbfb53..6292c840f5d 100644 --- a/lwlib/xlwmenu.c +++ b/lwlib/xlwmenu.c @@ -107,7 +107,7 @@ xlwMenuResources[] = {XtNfontSet, XtCFontSet, XtRFontSet, sizeof(XFontSet), offset(menu.fontSet), XtRFontSet, NULL}, #endif -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT #define DEFAULT_FONTNAME "Sans-10" #else #define DEFAULT_FONTNAME "XtDefaultFont" @@ -325,7 +325,7 @@ string_width (XlwMenuWidget mw, char *s) { XCharStruct xcs; int drop; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (mw->menu.xft_font) { XGlyphInfo gi; @@ -349,7 +349,7 @@ string_width (XlwMenuWidget mw, char *s) } -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT #define MENU_FONT_HEIGHT(mw) \ ((mw)->menu.xft_font != NULL \ ? (mw)->menu.xft_font->height \ @@ -965,7 +965,7 @@ display_menu_item (XlwMenuWidget mw, int width; enum menu_separator separator; int separator_p = lw_separator_p (val->name, &separator, 0); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT XftColor *xftfg; #endif @@ -1005,7 +1005,7 @@ display_menu_item (XlwMenuWidget mw, else text_gc = mw->menu.disabled_gc; deco_gc = mw->menu.foreground_gc; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT xftfg = val->enabled ? &mw->menu.xft_fg : &mw->menu.xft_disabled_fg; #endif @@ -1032,10 +1032,13 @@ display_menu_item (XlwMenuWidget mw, x_offset += ws->button_width; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (ws->xft_draw) { int draw_y = y + v_spacing + shadow; +#ifdef USE_CAIRO + cairo_surface_mark_dirty (cairo_get_target (ws->xft_draw)); +#endif XftDrawStringUtf8 (ws->xft_draw, xftfg, mw->menu.xft_font, x_offset, draw_y + font_ascent, @@ -1078,7 +1081,7 @@ display_menu_item (XlwMenuWidget mw, } else if (val->key) { -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (ws->xft_draw) { int draw_x = ws->width - ws->max_rest_width @@ -1119,6 +1122,10 @@ display_menu_item (XlwMenuWidget mw, draw_shadow_rectangle (mw, ws->pixmap, x, y, width, height, True, False); } +#ifdef USE_CAIRO + if (ws->xft_draw) + cairo_surface_flush (cairo_get_target (ws->xft_draw)); +#endif if (highlighted_p) draw_shadow_rectangle (mw, ws->pixmap, x, y, width, height, False, @@ -1320,7 +1327,7 @@ make_windows_if_needed (XlwMenuWidget mw, int n) XtAddEventHandler (windows [i].w, ExposureMask, False, expose_cb, mw); windows [i].window = XtWindow (windows [i].w); windows [i].pixmap = None; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT windows [i].xft_draw = 0; #endif set_window_type (windows [i].w, mw); @@ -1411,7 +1418,7 @@ create_pixmap_for_menu (window_state* ws, XlwMenuWidget mw) ws->pixmap = XCreatePixmap (XtDisplay (ws->w), ws->window, ws->width, ws->height, DefaultDepthOfScreen (XtScreen (ws->w))); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (ws->xft_draw) XftDrawDestroy (ws->xft_draw); if (mw->menu.xft_font) @@ -1831,7 +1838,7 @@ release_shadow_gcs (XlwMenuWidget mw) XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc); } -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT static XftFont * getDefaultXftFont (XlwMenuWidget mw) { @@ -1887,7 +1894,7 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args) gray_width, gray_height, (unsigned long)1, (unsigned long)0, 1); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (openXftFont (mw)) ; else @@ -1933,7 +1940,7 @@ XlwMenuInitialize (Widget request, Widget w, ArgList args, Cardinal *num_args) mw->menu.windows [0].height = 0; mw->menu.windows [0].max_rest_width = 0; mw->menu.windows [0].pixmap = None; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT mw->menu.windows [0].xft_draw = 0; #endif size_menu (mw, 0); @@ -1981,7 +1988,7 @@ XlwMenuRealize (Widget w, Mask *valueMask, XSetWindowAttributes *attributes) set_window_type (mw->menu.windows [0].w, mw); create_pixmap_for_menu (&mw->menu.windows [0], mw); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (mw->menu.xft_font) { XColor colors[3]; @@ -2078,7 +2085,7 @@ XlwMenuDestroy (Widget w) if (mw->menu.font) XFreeFont (XtDisplay (mw), mw->menu.font); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (mw->menu.windows [0].xft_draw) XftDrawDestroy (mw->menu.windows [0].xft_draw); if (mw->menu.xft_font) @@ -2092,7 +2099,7 @@ XlwMenuDestroy (Widget w) { if (mw->menu.windows [i].pixmap != None) XFreePixmap (XtDisplay (mw), mw->menu.windows [i].pixmap); -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (mw->menu.windows [i].xft_draw) XftDrawDestroy (mw->menu.windows [i].xft_draw); #endif @@ -2102,7 +2109,7 @@ XlwMenuDestroy (Widget w) XtFree ((char *) mw->menu.windows); } -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT static int fontname_changed (XlwMenuWidget newmw, XlwMenuWidget oldmw) @@ -2134,7 +2141,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new, if (newmw->core.background_pixel != oldmw->core.background_pixel || newmw->menu.foreground != oldmw->menu.foreground -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT || fontname_changed (newmw, oldmw) #endif #ifdef HAVE_X_I18N @@ -2170,7 +2177,7 @@ XlwMenuSetValues (Widget current, Widget request, Widget new, } } -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT if (fontname_changed (newmw, oldmw)) { int i; diff --git a/lwlib/xlwmenuP.h b/lwlib/xlwmenuP.h index 610f83afe45..2a05d993541 100644 --- a/lwlib/xlwmenuP.h +++ b/lwlib/xlwmenuP.h @@ -23,9 +23,13 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ #include "xlwmenu.h" #include <X11/CoreP.h> -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT +#ifdef USE_CAIRO +#include "lwlib-utils.h" +#else /* HAVE_XFT */ #include <X11/Xft/Xft.h> #endif +#endif /* Elements in the stack arrays. */ typedef struct _window_state @@ -42,7 +46,7 @@ typedef struct _window_state /* Width of toggle buttons or radio buttons. */ Dimension button_width; -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT XftDraw* xft_draw; #endif } window_state; @@ -56,7 +60,7 @@ typedef struct _XlwMenu_part XFontSet fontSet; XFontSetExtents *font_extents; #endif -#ifdef HAVE_XFT +#if defined USE_CAIRO || defined HAVE_XFT int default_face; XftFont* xft_font; XftColor xft_fg, xft_bg, xft_disabled_fg; |