summaryrefslogtreecommitdiff
path: root/gtk/gtkpango.c
blob: be6eb4e57e963e26d7069532164c81e0251bcd2d (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
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
/* gtktextdisplay.c - display layed-out text
 *
 * Copyright (c) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"
#include <pango/pangocairo.h>
#include "gtkintl.h"

#define GTK_TYPE_FILL_LAYOUT_RENDERER            (_gtk_fill_layout_renderer_get_type())
#define GTK_FILL_LAYOUT_RENDERER(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_FILL_LAYOUT_RENDERER, GtkFillLayoutRenderer))
#define GTK_IS_FILL_LAYOUT_RENDERER(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_FILL_LAYOUT_RENDERER))
#define GTK_FILL_LAYOUT_RENDERER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILL_LAYOUT_RENDERER, GtkFillLayoutRendererClass))
#define GTK_IS_FILL_LAYOUT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILL_LAYOUT_RENDERER))
#define GTK_FILL_LAYOUT_RENDERER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILL_LAYOUT_RENDERER, GtkFillLayoutRendererClass))

typedef struct _GtkFillLayoutRenderer      GtkFillLayoutRenderer;
typedef struct _GtkFillLayoutRendererClass GtkFillLayoutRendererClass;

struct _GtkFillLayoutRenderer
{
  PangoRenderer parent_instance;

  cairo_t *cr;
};

struct _GtkFillLayoutRendererClass
{
  PangoRendererClass parent_class;
};

G_DEFINE_TYPE (GtkFillLayoutRenderer, _gtk_fill_layout_renderer, PANGO_TYPE_RENDERER)

static void
gtk_fill_layout_renderer_draw_glyphs (PangoRenderer     *renderer,
                                      PangoFont         *font,
                                      PangoGlyphString  *glyphs,
                                      int                x,
                                      int                y)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);

  cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
  pango_cairo_show_glyph_string (text_renderer->cr, font, glyphs);
}

static void
gtk_fill_layout_renderer_draw_glyph_item (PangoRenderer     *renderer,
                                          const char        *text,
                                          PangoGlyphItem    *glyph_item,
                                          int                x,
                                          int                y)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);

  cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
  pango_cairo_show_glyph_item (text_renderer->cr, text, glyph_item);
}

static void
gtk_fill_layout_renderer_draw_rectangle (PangoRenderer     *renderer,
                                         PangoRenderPart    part,
                                         int                x,
                                         int                y,
                                         int                width,
                                         int                height)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);

  if (part == PANGO_RENDER_PART_BACKGROUND)
    return;

  cairo_rectangle (text_renderer->cr,
                   (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
		   (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
  cairo_fill (text_renderer->cr);
}

static void
gtk_fill_layout_renderer_draw_trapezoid (PangoRenderer     *renderer,
                                         PangoRenderPart    part,
                                         double             y1_,
                                         double             x11,
                                         double             x21,
                                         double             y2,
                                         double             x12,
                                         double             x22)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
  cairo_matrix_t matrix;
  cairo_t *cr;

  cr = text_renderer->cr;

  cairo_save (cr);

  /* use identity scale, but keep translation */
  cairo_get_matrix (cr, &matrix);
  matrix.xx = matrix.yy = 1;
  matrix.xy = matrix.yx = 0;
  cairo_set_matrix (cr, &matrix);

  cairo_move_to (cr, x11, y1_);
  cairo_line_to (cr, x21, y1_);
  cairo_line_to (cr, x22, y2);
  cairo_line_to (cr, x12, y2);
  cairo_close_path (cr);

  cairo_fill (cr);

  cairo_restore (cr);
}

static void
gtk_fill_layout_renderer_draw_error_underline (PangoRenderer *renderer,
                                               int            x,
                                               int            y,
                                               int            width,
                                               int            height)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);

  pango_cairo_show_error_underline (text_renderer->cr,
                                    (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
                                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
}

static void
gtk_fill_layout_renderer_draw_shape (PangoRenderer   *renderer,
                                     PangoAttrShape  *attr,
                                     int              x,
                                     int              y)
{
  GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
  cairo_t *cr = text_renderer->cr;
  PangoLayout *layout;
  PangoCairoShapeRendererFunc shape_renderer;
  gpointer                    shape_renderer_data;

  layout = pango_renderer_get_layout (renderer);

  if (!layout)
  	return;

  shape_renderer = pango_cairo_context_get_shape_renderer (pango_layout_get_context (layout),
							   &shape_renderer_data);

  if (!shape_renderer)
    return;

  cairo_save (cr);

  cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);

  shape_renderer (cr, attr, FALSE, shape_renderer_data);

  cairo_restore (cr);
}

static void
gtk_fill_layout_renderer_finalize (GObject *object)
{
  G_OBJECT_CLASS (_gtk_fill_layout_renderer_parent_class)->finalize (object);
}

static void
_gtk_fill_layout_renderer_init (GtkFillLayoutRenderer *renderer)
{
}

static void
_gtk_fill_layout_renderer_class_init (GtkFillLayoutRendererClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
  
  renderer_class->draw_glyphs = gtk_fill_layout_renderer_draw_glyphs;
  renderer_class->draw_glyph_item = gtk_fill_layout_renderer_draw_glyph_item;
  renderer_class->draw_rectangle = gtk_fill_layout_renderer_draw_rectangle;
  renderer_class->draw_trapezoid = gtk_fill_layout_renderer_draw_trapezoid;
  renderer_class->draw_error_underline = gtk_fill_layout_renderer_draw_error_underline;
  renderer_class->draw_shape = gtk_fill_layout_renderer_draw_shape;

  object_class->finalize = gtk_fill_layout_renderer_finalize;
}

void
_gtk_pango_fill_layout (cairo_t     *cr,
                        PangoLayout *layout)
{
  static GtkFillLayoutRenderer *renderer = NULL;
  gboolean has_current_point;
  double current_x, current_y;

  has_current_point = cairo_has_current_point (cr);
  cairo_get_current_point (cr, &current_x, &current_y);

  if (renderer == NULL)
    renderer = g_object_new (GTK_TYPE_FILL_LAYOUT_RENDERER, NULL);

  cairo_save (cr);
  cairo_translate (cr, current_x, current_y);

  renderer->cr = cr;
  pango_renderer_draw_layout (PANGO_RENDERER (renderer), layout, 0, 0);

  cairo_restore (cr);

  if (has_current_point)
    cairo_move_to (cr, current_x, current_y);
}