1 /* viewer-pangoxft.c: PangoXft viewer backend.
2  *
3  * Copyright (C) 1999,2004,2005 Red Hat, Inc.
4  * Copyright (C) 2001 Sun Microsystems
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 #include "config.h"
22 
23 #include "viewer-render.h"
24 #include "viewer-x.h"
25 
26 #include <pango/pangoxft.h>
27 
28 static void
default_substitute(FcPattern * pattern,gpointer data G_GNUC_UNUSED)29 default_substitute (FcPattern *pattern,
30 		    gpointer   data G_GNUC_UNUSED)
31 {
32   FcPatternDel (pattern, FC_DPI);
33   FcPatternAddInteger (pattern, FC_DPI, opt_dpi);
34 
35   if (opt_hinting != HINT_DEFAULT)
36     {
37       FcPatternDel (pattern, FC_HINTING);
38       FcPatternAddBool (pattern, FC_HINTING, opt_hinting != HINT_NONE);
39 
40       FcPatternDel (pattern, FC_AUTOHINT);
41       FcPatternAddBool (pattern, FC_AUTOHINT, opt_hinting == HINT_AUTO);
42     }
43 }
44 
45 static gpointer
pangoxft_view_create(const PangoViewer * klass)46 pangoxft_view_create (const PangoViewer *klass)
47 {
48   XViewer *instance;
49 
50   instance = x_view_create (klass);
51 
52   XftInit (NULL);
53 
54   pango_fc_font_map_set_default_substitute (PANGO_FC_FONT_MAP (pango_xft_get_font_map (instance->display, instance->screen)),
55 				            default_substitute, NULL, NULL);
56 
57   return instance;
58 }
59 
60 static void
pangoxft_view_destroy(gpointer instance)61 pangoxft_view_destroy (gpointer instance)
62 {
63   XViewer *x = (XViewer *)instance;
64 
65   pango_xft_shutdown_display (x->display, x->screen);
66 
67   x_view_destroy (instance);
68 }
69 
70 static PangoContext *
pangoxft_view_get_context(gpointer instance)71 pangoxft_view_get_context (gpointer instance)
72 {
73   XViewer *x = (XViewer *) instance;
74 
75   return pango_font_map_create_context (pango_xft_get_font_map (x->display, x->screen));
76 }
77 
78 typedef struct
79 {
80   XftDraw *draw;
81   XftColor color;
82 } MyXftContext;
83 
84 static void
render_callback(PangoLayout * layout,int x,int y,gpointer context,gpointer state G_GNUC_UNUSED)85 render_callback (PangoLayout *layout,
86 		 int          x,
87 		 int          y,
88 		 gpointer     context,
89 		 gpointer     state G_GNUC_UNUSED)
90 {
91   MyXftContext *xft_context = (MyXftContext *) context;
92 
93   pango_xft_render_layout (xft_context->draw,
94 			   &xft_context->color,
95 			   layout,
96 			   x * PANGO_SCALE, y * PANGO_SCALE);
97 }
98 
99 static void
pangoxft_view_render(gpointer instance,gpointer surface,PangoContext * context,int * width,int * height,gpointer state)100 pangoxft_view_render (gpointer      instance,
101 		      gpointer      surface,
102 		      PangoContext *context,
103 		      int          *width,
104 		      int          *height,
105 		      gpointer      state)
106 {
107   XViewer *x = (XViewer *) instance;
108   Pixmap pixmap = (Pixmap) surface;
109   MyXftContext xft_context;
110   XftDraw *draw;
111   XftColor color;
112 
113   draw = XftDrawCreate (x->display, pixmap,
114 			DefaultVisual (x->display, x->screen),
115 			DefaultColormap (x->display, x->screen));
116 
117   /* XftDrawRect only fills solid.
118    * Flatten with white.
119    */
120   color.color.red = ((opt_bg_color.red * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
121   color.color.green = ((opt_bg_color.green * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
122   color.color.blue = ((opt_bg_color.blue * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
123   color.color.alpha = 65535;
124 
125   XftDrawRect (draw, &color, 0, 0, *width, *height);
126 
127   color.color.red = opt_fg_color.red;
128   color.color.blue = opt_fg_color.green;
129   color.color.green = opt_fg_color.blue;
130   color.color.alpha = opt_fg_alpha;
131 
132   xft_context.draw = draw;
133   xft_context.color = color;
134 
135   do_output (context, render_callback, NULL, &xft_context, state, width, height);
136 
137   XftDrawDestroy (draw);
138 }
139 
140 const PangoViewer pangoxft_viewer = {
141   "PangoXft",
142   "xft",
143   NULL,
144   pangoxft_view_create,
145   pangoxft_view_destroy,
146   pangoxft_view_get_context,
147   x_view_create_surface,
148   x_view_destroy_surface,
149   pangoxft_view_render,
150   NULL,
151   x_view_create_window,
152   x_view_destroy_window,
153   x_view_display
154 };
155