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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*
* Copyright (C) 2017 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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 "gsk/gsk.h"
#include "gsk/gskrendernodeprivate.h"
#include "gskpango.h"
#include "gtksnapshotprivate.h"
#include "gtktextlayoutprivate.h"
#include "gtktextviewprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcsscolorvalueprivate.h"
#include <math.h>
#include <pango/pango.h>
#include <cairo.h>
G_DEFINE_TYPE (GskPangoRenderer, gsk_pango_renderer, PANGO_TYPE_RENDERER)
void
gsk_pango_renderer_set_state (GskPangoRenderer *crenderer,
GskPangoRendererState state)
{
g_return_if_fail (GSK_IS_PANGO_RENDERER (crenderer));
crenderer->state = state;
}
void
gsk_pango_renderer_set_shape_handler (GskPangoRenderer *crenderer,
GskPangoShapeHandler handler)
{
g_return_if_fail (GSK_IS_PANGO_RENDERER (crenderer));
crenderer->shape_handler = handler;
}
static void
get_color (GskPangoRenderer *crenderer,
PangoRenderPart part,
GdkRGBA *rgba)
{
const PangoColor *color = pango_renderer_get_color ((PangoRenderer *) (crenderer), part);
const guint16 a = pango_renderer_get_alpha ((PangoRenderer *) (crenderer), part);
if (color)
{
rgba->red = color->red / 65535.;
rgba->green = color->green / 65535.;
rgba->blue = color->blue / 65535.;
rgba->alpha = a ? a / 65535. : crenderer->fg_color.alpha;
}
else
{
*rgba = crenderer->fg_color;
if (a)
rgba->alpha = a / 65535.;
}
}
static void
set_color (GskPangoRenderer *crenderer,
PangoRenderPart part,
cairo_t *cr)
{
GdkRGBA rgba = { 0, 0, 0, 1 };
get_color (crenderer, part, &rgba);
gdk_cairo_set_source_rgba (cr, &rgba);
}
static void
gsk_pango_renderer_draw_glyph_item (PangoRenderer *renderer,
const char *text,
PangoGlyphItem *glyph_item,
int x,
int y)
{
GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
GdkRGBA color;
get_color (crenderer, PANGO_RENDER_PART_FOREGROUND, &color);
gtk_snapshot_append_text (crenderer->snapshot,
glyph_item->item->analysis.font,
glyph_item->glyphs,
&color,
(float) x / PANGO_SCALE,
(float) y / PANGO_SCALE);
}
static void
gsk_pango_renderer_draw_rectangle (PangoRenderer *renderer,
PangoRenderPart part,
int x,
int y,
int width,
int height)
{
GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
GdkRGBA rgba;
get_color (crenderer, part, &rgba);
gtk_snapshot_append_color (crenderer->snapshot,
&rgba,
&GRAPHENE_RECT_INIT ((double)x / PANGO_SCALE,
(double)y / PANGO_SCALE,
(double)width / PANGO_SCALE,
(double)height / PANGO_SCALE));
}
static void
gsk_pango_renderer_draw_trapezoid (PangoRenderer *renderer,
PangoRenderPart part,
double y1_,
double x11,
double x21,
double y2,
double x12,
double x22)
{
GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
cairo_t *cr;
gdouble x, y;
cr = gtk_snapshot_append_cairo (crenderer->snapshot, &crenderer->bounds);
set_color (crenderer, part, cr);
x = y = 0;
cairo_user_to_device_distance (cr, &x, &y);
cairo_identity_matrix (cr);
cairo_translate (cr, x, y);
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_destroy (cr);
}
#define HEIGHT_RATIO (M_SQRT2/5.0)
static void
gsk_pango_renderer_draw_error_underline (PangoRenderer *renderer,
int x,
int y,
int width,
int height)
{
GdkRGBA rgba;
double xx, yy, ww, hh;
double hs;
double e, o;
GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
xx = (double)x / PANGO_SCALE;
yy = (double)y / PANGO_SCALE;
ww = (double)width / PANGO_SCALE;
hh = (double)height / PANGO_SCALE;
hs = hh / M_SQRT2;
e = fmod (ww - 2 * hs * HEIGHT_RATIO, hs * (1 - HEIGHT_RATIO));
#if 0
gdk_rgba_parse (&rgba, "yellow");
gtk_snapshot_append_color (crenderer->snapshot, &rgba,
&GRAPHENE_RECT_INIT (xx, yy, ww, hh));
#endif
get_color (crenderer, PANGO_RENDER_PART_UNDERLINE, &rgba);
gtk_snapshot_save (crenderer->snapshot);
gtk_snapshot_translate (crenderer->snapshot,
&GRAPHENE_POINT_INIT (xx, yy));
gtk_snapshot_rotate (crenderer->snapshot, 45);
gtk_snapshot_translate (crenderer->snapshot,
&GRAPHENE_POINT_INIT (e / 2 + hs * HEIGHT_RATIO,
- hs * HEIGHT_RATIO));
xx = yy = o = 0;
while (1)
{
if (o + hs * (1 + HEIGHT_RATIO) >= ww)
break;
gtk_snapshot_append_color (crenderer->snapshot, &rgba,
&GRAPHENE_RECT_INIT (xx, yy, hh, hh * HEIGHT_RATIO));
xx += hh * (1 - HEIGHT_RATIO);
yy -= hh * (1 - HEIGHT_RATIO);
o += hs * (1 - HEIGHT_RATIO);
if (o + hs * (1 + HEIGHT_RATIO) >= ww)
break;
gtk_snapshot_append_color (crenderer->snapshot, &rgba,
&GRAPHENE_RECT_INIT (xx, yy, hh * HEIGHT_RATIO, hh));
o += hs * (1 - HEIGHT_RATIO);
}
gtk_snapshot_restore (crenderer->snapshot);
}
static void
gsk_pango_renderer_draw_shape (PangoRenderer *renderer,
PangoAttrShape *attr,
int x,
int y)
{
GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
cairo_t *cr;
PangoLayout *layout;
PangoCairoShapeRendererFunc shape_renderer;
gpointer shape_renderer_data;
double base_x = (double)x / PANGO_SCALE;
double base_y = (double)y / PANGO_SCALE;
gboolean handled = FALSE;
if (crenderer->shape_handler)
{
double shape_x = base_x;
double shape_y = (double) (y + attr->logical_rect.y) / PANGO_SCALE;
if (shape_x != 0 || shape_y != 0)
{
gtk_snapshot_save (crenderer->snapshot);
gtk_snapshot_translate (crenderer->snapshot, &GRAPHENE_POINT_INIT (shape_x, shape_y));
}
handled = crenderer->shape_handler (attr,
crenderer->snapshot,
(double)attr->logical_rect.width / PANGO_SCALE,
(double)attr->logical_rect.height / PANGO_SCALE);
if (shape_x != 0 || shape_y != 0)
gtk_snapshot_restore (crenderer->snapshot);
}
if (!handled)
{
cr = gtk_snapshot_append_cairo (crenderer->snapshot, &crenderer->bounds);
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;
set_color (crenderer, PANGO_RENDER_PART_FOREGROUND, cr);
cairo_move_to (cr, base_x, base_y);
shape_renderer (cr, attr, FALSE, shape_renderer_data);
cairo_destroy (cr);
}
}
static void
text_renderer_set_rgba (GskPangoRenderer *crenderer,
PangoRenderPart part,
const GdkRGBA *rgba)
{
PangoRenderer *renderer = PANGO_RENDERER (crenderer);
PangoColor color = { 0, };
guint16 alpha;
if (rgba)
{
color.red = (guint16)(rgba->red * 65535);
color.green = (guint16)(rgba->green * 65535);
color.blue = (guint16)(rgba->blue * 65535);
alpha = (guint16)(rgba->alpha * 65535);
pango_renderer_set_color (renderer, part, &color);
pango_renderer_set_alpha (renderer, part, alpha);
}
else
{
pango_renderer_set_color (renderer, part, NULL);
pango_renderer_set_alpha (renderer, part, 0);
}
}
static GtkTextAppearance *
get_item_appearance (PangoItem *item)
{
GSList *tmp_list = item->analysis.extra_attrs;
while (tmp_list)
{
PangoAttribute *attr = tmp_list->data;
if (attr->klass->type == gtk_text_attr_appearance_type)
return &((GtkTextAttrAppearance *)attr)->appearance;
tmp_list = tmp_list->next;
}
return NULL;
}
static void
gsk_pango_renderer_prepare_run (PangoRenderer *renderer,
PangoLayoutRun *run)
{
GskPangoRenderer *crenderer = GSK_PANGO_RENDERER (renderer);
const GdkRGBA *bg_rgba = NULL;
const GdkRGBA *fg_rgba = NULL;
GtkTextAppearance *appearance;
PANGO_RENDERER_CLASS (gsk_pango_renderer_parent_class)->prepare_run (renderer, run);
appearance = get_item_appearance (run->item);
if (appearance == NULL)
return;
if (appearance->draw_bg && crenderer->state == GSK_PANGO_RENDERER_NORMAL)
bg_rgba = appearance->bg_rgba;
else
bg_rgba = NULL;
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_BACKGROUND, bg_rgba);
if (crenderer->state == GSK_PANGO_RENDERER_SELECTED &&
GTK_IS_TEXT_VIEW (crenderer->widget))
{
GtkCssNode *node;
GtkCssValue *value;
node = gtk_text_view_get_selection_node ((GtkTextView *)crenderer->widget);
value = gtk_css_node_get_style (node)->core->color;
fg_rgba = gtk_css_color_value_get_rgba (value);
}
else if (crenderer->state == GSK_PANGO_RENDERER_CURSOR && gtk_widget_has_focus (crenderer->widget))
{
GtkCssNode *node;
GtkCssValue *value;
node = gtk_widget_get_css_node (crenderer->widget);
value = gtk_css_node_get_style (node)->background->background_color;
fg_rgba = gtk_css_color_value_get_rgba (value);
}
else
fg_rgba = appearance->fg_rgba;
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_FOREGROUND, fg_rgba);
if (appearance->strikethrough_rgba)
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_STRIKETHROUGH, appearance->strikethrough_rgba);
else
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_STRIKETHROUGH, fg_rgba);
if (appearance->underline_rgba)
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_UNDERLINE, appearance->underline_rgba);
else if (appearance->underline == PANGO_UNDERLINE_ERROR)
{
if (!crenderer->error_color)
{
static const GdkRGBA red = { 1, 0, 0, 1 };
crenderer->error_color = gdk_rgba_copy (&red);
}
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_UNDERLINE, crenderer->error_color);
}
else
text_renderer_set_rgba (crenderer, PANGO_RENDER_PART_UNDERLINE, fg_rgba);
}
static void
gsk_pango_renderer_init (GskPangoRenderer *renderer G_GNUC_UNUSED)
{
}
static void
gsk_pango_renderer_class_init (GskPangoRendererClass *klass)
{
PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
renderer_class->draw_glyph_item = gsk_pango_renderer_draw_glyph_item;
renderer_class->draw_rectangle = gsk_pango_renderer_draw_rectangle;
renderer_class->draw_trapezoid = gsk_pango_renderer_draw_trapezoid;
renderer_class->draw_error_underline = gsk_pango_renderer_draw_error_underline;
renderer_class->draw_shape = gsk_pango_renderer_draw_shape;
renderer_class->prepare_run = gsk_pango_renderer_prepare_run;
}
static GskPangoRenderer *cached_renderer = NULL; /* MT-safe */
G_LOCK_DEFINE_STATIC (cached_renderer);
GskPangoRenderer *
gsk_pango_renderer_acquire (void)
{
GskPangoRenderer *renderer;
if (G_LIKELY (G_TRYLOCK (cached_renderer)))
{
if (G_UNLIKELY (!cached_renderer))
{
cached_renderer = g_object_new (GSK_TYPE_PANGO_RENDERER, NULL);
cached_renderer->is_cached_renderer = TRUE;
}
renderer = cached_renderer;
/* Reset to standard state */
renderer->state = GSK_PANGO_RENDERER_NORMAL;
renderer->shape_handler = NULL;
}
else
{
renderer = g_object_new (GSK_TYPE_PANGO_RENDERER, NULL);
}
return renderer;
}
void
gsk_pango_renderer_release (GskPangoRenderer *renderer)
{
if (G_LIKELY (renderer->is_cached_renderer))
{
renderer->widget = NULL;
renderer->snapshot = NULL;
if (renderer->error_color)
{
gdk_rgba_free (renderer->error_color);
renderer->error_color = NULL;
}
G_UNLOCK (cached_renderer);
}
else
g_object_unref (renderer);
}
/**
* gtk_snapshot_append_layout:
* @snapshot: a #GtkSnapshot
* @layout: the #PangoLayout to render
* @color: the foreground color to render the layout in
*
* Creates render nodes for rendering @layout in the given foregound @color
* and appends them to the current node of @snapshot without changing the
* current node.
**/
void
gtk_snapshot_append_layout (GtkSnapshot *snapshot,
PangoLayout *layout,
const GdkRGBA *color)
{
GskPangoRenderer *crenderer;
PangoRectangle ink_rect;
g_return_if_fail (snapshot != NULL);
g_return_if_fail (PANGO_IS_LAYOUT (layout));
crenderer = gsk_pango_renderer_acquire ();
crenderer->snapshot = snapshot;
crenderer->fg_color = *color;
pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
graphene_rect_init (&crenderer->bounds, ink_rect.x, ink_rect.y, ink_rect.width, ink_rect.height);
pango_renderer_draw_layout (PANGO_RENDERER (crenderer), layout, 0, 0);
gsk_pango_renderer_release (crenderer);
}
|