summaryrefslogtreecommitdiff
path: root/src/vtepangox.c
blob: baa0c9850057872d42c677f10372a1303f372be1 (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
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
501
502
503
/*
 * Copyright (C) 2003 Red Hat, Inc.
 *
 * This 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 program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "../config.h"

#ifndef X_DISPLAY_MISSING
#ifdef HAVE_PANGOX

#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <pango/pango.h>
#include <pango/pangox.h>
#include "debug.h"
#include "vtebg.h"
#include "vtedraw.h"
#include "vtepangox.h"

struct _vte_pango_x_data
{
	GdkColor color;
	GdkPixmap *pixmap;
	gint pixmapw, pixmaph;
	gint scrollx, scrolly;
	PangoFontDescription *font;
	PangoLayout *layout;
	GdkGC *gc;
	PangoContext *ctx;
	Drawable drawable;
	int x_offs, y_offs;
};

static gboolean
_vte_pango_x_check(struct _vte_draw *draw, GtkWidget *widget)
{
	/* We can draw onto any widget. */
	return TRUE;
}

static void
_vte_pango_x_create(struct _vte_draw *draw, GtkWidget *widget)
{
	struct _vte_pango_x_data *data;

	draw->impl_data = g_slice_new(struct _vte_pango_x_data);
	data = (struct _vte_pango_x_data*) draw->impl_data;

	data->color.red = 0;
	data->color.green = 0;
	data->color.blue = 0;
	data->pixmap = NULL;
	data->pixmapw = data->pixmaph = 0;
	data->scrollx = data->scrolly = 0;
	data->font = NULL;
	data->layout = NULL;
	data->gc = NULL;
	data->ctx = NULL;
}

static void
_vte_pango_x_destroy(struct _vte_draw *draw)
{
	struct _vte_pango_x_data *data;
	data = (struct _vte_pango_x_data*) draw->impl_data;

	if (data->pixmap != NULL) {
		g_object_unref(data->pixmap);
	}
	if (data->font != NULL) {
		pango_font_description_free(data->font);
	}
	if (data->layout != NULL) {
		g_object_unref(data->layout);
	}
	if (data->gc != NULL) {
		g_object_unref(data->gc);
	}
	if (data->ctx != NULL) {
		g_object_unref(data->ctx);
	}

	g_slice_free(struct _vte_pango_x_data, draw->impl_data);
}

static GdkVisual *
_vte_pango_x_get_visual(struct _vte_draw *draw)
{
	return gtk_widget_get_visual(draw->widget);
}

static GdkColormap *
_vte_pango_x_get_colormap(struct _vte_draw *draw)
{
	return gtk_widget_get_colormap(draw->widget);
}

static void
_vte_pango_x_start(struct _vte_draw *draw)
{
	struct _vte_pango_x_data *data;
	Display *display;
	GdkDrawable *drawable;
	int x_offs, y_offs;

	data = (struct _vte_pango_x_data*) draw->impl_data;

	display = gdk_x11_drawable_get_xdisplay(draw->widget->window);
	if (data->ctx != NULL) {
		g_object_unref(data->ctx);
	}
	data->ctx = pango_x_get_context(display);

	if (data->layout != NULL) {
		g_object_unref(data->layout);
	}
	data->layout = pango_layout_new(data->ctx);

	if (data->font != NULL) {
		pango_layout_set_font_description(data->layout, data->font);
	}

	if (data->gc != NULL) {
		g_object_unref(data->gc);
	}
	data->gc = gdk_gc_new(draw->widget->window);

	gdk_rgb_find_color(gdk_drawable_get_colormap(draw->widget->window),
			   &data->color);

	gdk_window_get_internal_paint_info(draw->widget->window, &drawable,
					&x_offs, &y_offs);
	data->drawable = gdk_x11_drawable_get_xid(drawable);
	data->x_offs = x_offs;
	data->y_offs = y_offs;
}

static void
_vte_pango_x_end(struct _vte_draw *draw)
{
	struct _vte_pango_x_data *data;
	data = (struct _vte_pango_x_data*) draw->impl_data;

	data->drawable = -1;
	data->x_offs = 0;
	data->y_offs = 0;

	if (data->layout != NULL) {
		g_object_unref(data->layout);
	}
	data->layout = NULL;

	if (data->gc != NULL) {
		g_object_unref(data->gc);
	}
	data->gc = NULL;

	if (data->ctx != NULL) {
		g_object_unref(data->ctx);
	}
	data->ctx = NULL;
}

static void
_vte_pango_x_set_background_color(struct _vte_draw *draw, GdkColor *color, guint16 opacity)
{
	struct _vte_pango_x_data *data;
	data = (struct _vte_pango_x_data*) draw->impl_data;
	data->color = *color;
}

static void
_vte_pango_x_set_background_image(struct _vte_draw *draw,
				  enum VteBgSourceType type,
				  GdkPixbuf *pixbuf,
				  const char *file,
				  const GdkColor *color,
				  double saturation)
{
	GdkPixmap *pixmap;
	struct _vte_pango_x_data *data;
	GdkScreen *screen;

	screen = gtk_widget_get_screen (draw->widget);

	data = (struct _vte_pango_x_data*) draw->impl_data;
	pixmap = vte_bg_get_pixmap(vte_bg_get_for_screen(screen),
				   type, pixbuf, file,
				   color, saturation,
				   _vte_draw_get_colormap(draw, TRUE));
	if (data->pixmap != NULL) {
		g_object_unref(data->pixmap);
	}
	data->pixmap = NULL;
	data->pixmapw = data->pixmaph = 0;
	if (pixmap != NULL) {
		data->pixmap = pixmap;
		gdk_drawable_get_size(pixmap, &data->pixmapw, &data->pixmaph);
	}
}

static void
_vte_pango_x_clip(struct _vte_draw *draw, GdkRegion *region)
{
	struct _vte_pango_x_data *data = draw->impl_data;
	gdk_gc_set_clip_region(data->gc, region);
}

static void
_vte_pango_x_clear(struct _vte_draw *draw,
		   gint x, gint y, gint width, gint height)
{
	struct _vte_pango_x_data *data;
	gint i, j, h, w, xstop, ystop;

	data = (struct _vte_pango_x_data*) draw->impl_data;

	if ((data->pixmap == NULL) ||
	    (data->pixmapw == 0) ||
	    (data->pixmaph == 0)) {
		gdk_gc_set_foreground(data->gc, &data->color);
		gdk_draw_rectangle(draw->widget->window,
				   data->gc,
				   TRUE,
				   x, y, width, height);
		return;
	}

	/* Flood fill. */
	xstop = x + width;
	ystop = y + height;

	y = ystop - height;
	j = (data->scrolly + y) % data->pixmaph;
	while (y < ystop) {
		x = xstop - width;
		i = (data->scrollx + x) % data->pixmapw;
		h = MIN(data->pixmaph - (j % data->pixmaph), ystop - y);
		while (x < xstop) {
			w = MIN(data->pixmapw - (i % data->pixmapw), xstop - x);
			gdk_draw_drawable(draw->widget->window,
					  data->gc,
					  data->pixmap,
					  i, j,
					  x, y,
					  w, h);
			x += w;
			i = 0;
		}
		y += h;
		j = 0;
	}
}

static void
_vte_pango_x_set_text_font(struct _vte_draw *draw,
			   const PangoFontDescription *fontdesc,
			   VteTerminalAntiAlias antialias)
{
	PangoContext *ctx;
	Display *display;
	PangoLayout *layout;
	PangoLayoutIter *iter;
	PangoRectangle ink, logical;
	gunichar full_codepoints[] = {VTE_DRAW_DOUBLE_WIDE_IDEOGRAPHS};
	GString *full_string;
	gint full_width;
	guint i;
	struct _vte_pango_x_data *data;

	data = (struct _vte_pango_x_data*) draw->impl_data;

	display = gdk_x11_display_get_xdisplay(gtk_widget_get_display(draw->widget));
	if (data->ctx != NULL) {
		g_object_unref(data->ctx);
	}
	ctx = pango_x_get_context(display);

	layout = pango_layout_new(ctx);
	if (data->font != NULL) {
		pango_font_description_free(data->font);
	}
	data->font = pango_font_description_copy(fontdesc);
	pango_layout_set_font_description(layout, data->font);

	/* Estimate for ASCII characters. */
	pango_layout_set_text(layout,
			      VTE_DRAW_SINGLE_WIDE_CHARACTERS,
			      strlen(VTE_DRAW_SINGLE_WIDE_CHARACTERS));
	pango_layout_get_extents(layout, &ink, &logical);
	draw->width = logical.width;
	draw->width = howmany(draw->width,
			      strlen(VTE_DRAW_SINGLE_WIDE_CHARACTERS));
	iter = pango_layout_get_iter(layout);
	draw->height = PANGO_PIXELS(logical.height);
	draw->ascent = PANGO_PIXELS(pango_layout_iter_get_baseline(iter));
	pango_layout_iter_free(iter);

	/* Estimate for CJK characters. */
	full_string = g_string_new(NULL);
	for (i = 0; i < G_N_ELEMENTS(full_codepoints); i++) {
		g_string_append_unichar(full_string, full_codepoints[i]);
	}
	pango_layout_set_text(layout, full_string->str, full_string->len);
	pango_layout_get_extents(layout, &ink, &logical);
	full_width = howmany(logical.width, G_N_ELEMENTS(full_codepoints));
	g_string_free(full_string, TRUE);

	/* If they're the same, then we have a screwy font. */
	if (full_width == draw->width) {
		/* add 1 to round up when dividing by 2 */
		draw->width = (draw->width + 1) / 2;
	}

	draw->width = PANGO_PIXELS(draw->width);
	iter = pango_layout_get_iter(layout);
	if (draw->height == 0) {
		draw->height = PANGO_PIXELS(logical.height);
	}
	if (draw->ascent == 0) {
		draw->ascent = PANGO_PIXELS(pango_layout_iter_get_baseline(iter));
	}
	pango_layout_iter_free(iter);

	_vte_debug_print(VTE_DEBUG_MISC,
			"VtePangoX font metrics = %dx%d (%d).\n",
			draw->width, draw->height, draw->ascent);
	g_object_unref(layout);
	g_object_unref(ctx);
}

static int
_vte_pango_x_get_text_width(struct _vte_draw *draw)
{
	return draw->width;
}

static int
_vte_pango_x_get_text_height(struct _vte_draw *draw)
{
	return draw->height;
}

static int
_vte_pango_x_get_text_ascent(struct _vte_draw *draw)
{
	return draw->ascent;
}

static int
_vte_pango_x_get_char_width(struct _vte_draw *draw, gunichar c, int columns)
{
	return _vte_pango_x_get_text_width(draw) * columns;
}

static gboolean
_vte_pango_x_get_using_fontconfig(struct _vte_draw *draw)
{
	return FALSE;
}

static void
_vte_pango_x_draw_text(struct _vte_draw *draw,
		       struct _vte_draw_text_request *requests,
		       gsize n_requests,
		       GdkColor *color, guchar alpha)
{
	Display *display;
	GC gc;
	struct _vte_pango_x_data *data;
	char buf[VTE_UTF8_BPC];
	gsize i, length;
	GdkColor wcolor;

	data = (struct _vte_pango_x_data*) draw->impl_data;

	wcolor = *color;
	gdk_rgb_find_color(gdk_drawable_get_colormap(draw->widget->window),
			   &wcolor);
	gdk_gc_set_foreground(data->gc, &wcolor);
	display = gdk_x11_drawable_get_xdisplay(draw->widget->window);
	gc = gdk_x11_gc_get_xgc(data->gc);

	for (i = 0; i < n_requests; i++) {
		length = g_unichar_to_utf8(requests[i].c, buf);
		pango_layout_set_text(data->layout, buf, length);
		pango_x_render_layout(display,
				      data->drawable,
				      gc,
				      data->layout,
				      requests[i].x - data->x_offs,
				      requests[i].y - data->y_offs);
	}
}

static gboolean
_vte_pango_x_draw_char(struct _vte_draw *draw,
		       struct _vte_draw_text_request *request,
		       GdkColor *color, guchar alpha)
{
	_vte_pango_x_draw_text(draw, request, 1, color, alpha);
	return TRUE;
}

static gboolean
_vte_pango_x_draw_has_char(struct _vte_draw *draw, gunichar c)
{
	return FALSE;
}


static void
_vte_pango_x_draw_rectangle(struct _vte_draw *draw,
			  gint x, gint y, gint width, gint height,
			  GdkColor *color, guchar alpha)
{
	struct _vte_pango_x_data *data;
	GdkColor wcolor;

	data = (struct _vte_pango_x_data*) draw->impl_data;

	wcolor = *color;
	gdk_rgb_find_color(gdk_drawable_get_colormap(draw->widget->window),
			   &wcolor);
	gdk_gc_set_foreground(data->gc, &wcolor);

	gdk_draw_rectangle(draw->widget->window, data->gc, FALSE,
			   x, y, width, height);
}

static void
_vte_pango_x_fill_rectangle(struct _vte_draw *draw,
			  gint x, gint y, gint width, gint height,
			  GdkColor *color, guchar alpha)
{
	struct _vte_pango_x_data *data;
	GdkColor wcolor;

	data = (struct _vte_pango_x_data*) draw->impl_data;
	wcolor = *color;
	gdk_rgb_find_color(gdk_drawable_get_colormap(draw->widget->window),
			   &wcolor);
	gdk_gc_set_foreground(data->gc, &wcolor);
	gdk_draw_rectangle(draw->widget->window, data->gc, TRUE,
			   x, y, width, height);
}

static void
_vte_pango_x_set_scroll(struct _vte_draw *draw, gint x, gint y)
{
	struct _vte_pango_x_data *data;
	data = (struct _vte_pango_x_data*) draw->impl_data;
	data->scrollx = x;
	data->scrolly = y;
}

const struct _vte_draw_impl _vte_draw_pango_x = {
	"PangoX",
	_vte_pango_x_check,
	_vte_pango_x_create,
	_vte_pango_x_destroy,
	_vte_pango_x_get_visual,
	_vte_pango_x_get_colormap,
	_vte_pango_x_start,
	_vte_pango_x_end,
	_vte_pango_x_set_background_color,
	_vte_pango_x_set_background_image,
	FALSE,
	_vte_pango_x_clip,
	_vte_pango_x_clear,
	_vte_pango_x_set_text_font,
	_vte_pango_x_get_text_width,
	_vte_pango_x_get_text_height,
	_vte_pango_x_get_text_ascent,
	_vte_pango_x_get_char_width,
	_vte_pango_x_get_using_fontconfig,
	_vte_pango_x_draw_text,
	_vte_pango_x_draw_char,
	_vte_pango_x_draw_has_char,
	_vte_pango_x_draw_rectangle,
	_vte_pango_x_fill_rectangle,
	_vte_pango_x_set_scroll,
};

#endif
#endif