summaryrefslogtreecommitdiff
path: root/gdk/x11/gdkpango-x11.c
blob: 3f9cfca21cb29dfcc511674e72804454014da8c5 (plain)
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
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 2000 Red Hat, Inc. 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <stdlib.h>

#include "gdkalias.h"
#include "gdkx.h"
#include "gdkdisplay-x11.h"
#include "gdkpango.h"
#include <pango/pangoxft.h>
#include <pango/pangoxft-render.h>

#include <math.h>

typedef struct _GdkX11Renderer      GdkX11Renderer;
typedef struct _GdkX11RendererClass GdkX11RendererClass;

#define GDK_TYPE_X11_RENDERER            (_gdk_x11_renderer_get_type())
#define GDK_X11_RENDERER(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_X11_RENDERER, GdkX11Renderer))
#define GDK_IS_X11_RENDERER(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_X11_RENDERER))
#define GDK_X11_RENDERER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_X11_RENDERER, GdkX11RendererClass))
#define GDK_IS_X11_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_X11_RENDERER))
#define GDK_X11_RENDERER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_X11_RENDERER, GdkX11RendererClass))

#define MAX_RENDER_PART  PANGO_RENDER_PART_STRIKETHROUGH

struct _GdkX11Renderer
{
  PangoXftRenderer parent_instance;

  XRenderPictFormat *mask_format;
  
  GdkDrawable *drawable;
  GdkGC *gc;
};

struct _GdkX11RendererClass
{
  PangoXftRendererClass parent_class;
};

G_DEFINE_TYPE (GdkX11Renderer, _gdk_x11_renderer, PANGO_TYPE_XFT_RENDERER)

static void
gdk_x11_renderer_finalize (GObject *object)
{
  G_OBJECT_CLASS (_gdk_x11_renderer_parent_class)->finalize (object);
}
     
static void
gdk_x11_renderer_composite_trapezoids (PangoXftRenderer *xftrenderer,
				       PangoRenderPart   part,
				       XTrapezoid       *trapezoids,
				       int               n_trapezoids)
{
  /* Because we only use this renderer for "draw_glyphs()" calls, we
   * won't hit this code path much. However, it is hit for drawing
   * the "unknown glyph" hex squares. We can safely ignore the part,
   */
  GdkX11Renderer *x11_renderer = GDK_X11_RENDERER (xftrenderer);

  _gdk_x11_drawable_draw_xtrapezoids (x11_renderer->drawable,
				      x11_renderer->gc,
				      trapezoids, n_trapezoids);

}

static void
gdk_x11_renderer_composite_glyphs (PangoXftRenderer *xftrenderer,
				   XftFont          *xft_font,
				   XftGlyphSpec     *glyphs,
				   gint              n_glyphs)
{
  GdkX11Renderer *x11_renderer = GDK_X11_RENDERER (xftrenderer);

  _gdk_x11_drawable_draw_xft_glyphs (x11_renderer->drawable,
				     x11_renderer->gc,
				     xft_font, glyphs, n_glyphs);
}

static void
_gdk_x11_renderer_init (GdkX11Renderer *renderer)
{
}

static void
_gdk_x11_renderer_class_init (GdkX11RendererClass *klass)
{
  PangoXftRendererClass *xftrenderer_class = PANGO_XFT_RENDERER_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  xftrenderer_class->composite_glyphs = gdk_x11_renderer_composite_glyphs;
  xftrenderer_class->composite_trapezoids = gdk_x11_renderer_composite_trapezoids;

  object_class->finalize = gdk_x11_renderer_finalize;
}

PangoRenderer *
_gdk_x11_renderer_get (GdkDrawable *drawable,
		       GdkGC       *gc)
{
  GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  GdkX11Renderer *x11_renderer;

  if (!screen_x11->renderer)
    {
      screen_x11->renderer = g_object_new (GDK_TYPE_X11_RENDERER,
					   "display", GDK_SCREEN_XDISPLAY (screen),
					   "screen",  GDK_SCREEN_XNUMBER (screen),
					   NULL);
    }

  x11_renderer = GDK_X11_RENDERER (screen_x11->renderer);

  x11_renderer->drawable = drawable;
  x11_renderer->gc = gc;

  return screen_x11->renderer;
}

/**
 * gdk_pango_context_get_for_screen:
 * @screen: the #GdkScreen for which the context is to be created.
 * 
 * Creates a #PangoContext for @screen.
 *
 * The context must be freed when you're finished with it.
 * 
 * When using GTK+, normally you should use gtk_widget_get_pango_context()
 * instead of this function, to get the appropriate context for
 * the widget you intend to render text onto.
 * 
 * Return value: a new #PangoContext for @screen
 *
 * Since: 2.2
 **/
PangoContext *
gdk_pango_context_get_for_screen (GdkScreen *screen)
{
  PangoContext *context;
  
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  if (screen->closed)
    return NULL;
  
  context = pango_xft_get_context (GDK_SCREEN_XDISPLAY (screen),
				   GDK_SCREEN_X11 (screen)->screen_num);
  
  g_object_set_data (G_OBJECT (context), "gdk-pango-screen", screen);
  
  return context;
}