aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: 2a9d3e6375e7590d7c8fb826839a6acae4bb2d26 (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
/*
 * render.c
 *
 * Colloquium - A tiny presentation program
 *
 * Copyright (c) 2012 Thomas White <taw@bitwiz.org.uk>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <cairo.h>
#include <cairo-pdf.h>
#include <pango/pangocairo.h>
#include <assert.h>
#include <gdk/gdk.h>
#include <string.h>

#include <storycode.h>

#include "stylesheet.h"
#include "presentation.h"
#include "frame.h"
#include "render.h"


struct renderstuff
{
	cairo_t *cr;
	PangoContext *pc;
	PangoFontMap *fontmap;
	PangoFont *font;
	PangoLogAttr *log_attrs;
	struct frame *fr;
	int width;
};


static void calc_width(gpointer data, gpointer user_data)
{
	struct renderstuff *s = user_data;
	PangoItem *item = data;
	PangoGlyphString *glyphs;

	glyphs = pango_glyph_string_new();

	pango_shape(s->fr->sc+item->offset, item->length, &item->analysis,
	            glyphs);

	s->width += pango_glyph_string_get_width(glyphs);
	pango_glyph_string_free(glyphs);
}


static void render_segment(gpointer data, gpointer user_data)
{
	struct renderstuff *s = user_data;
	PangoItem *item = data;
	PangoGlyphString *glyphs;
	PangoGlyphItem gitem;
	GdkColor col;

	glyphs = pango_glyph_string_new();

	pango_shape(s->fr->sc+item->offset, item->length, &item->analysis,
	            glyphs);

	gitem.item = item;
	gitem.glyphs = glyphs;
	pango_glyph_item_letter_space(&gitem, s->fr->sc+item->offset,
	                              s->log_attrs+item->offset,
	                              5.0*PANGO_SCALE);

	/* FIXME: Honour alpha as well */
	gdk_color_parse("#000000", &col);
	gdk_cairo_set_source_color(s->cr, &col);
	cairo_set_source_rgb(s->cr, 0.0, 1.0, 0.0);
	pango_cairo_show_glyph_string(s->cr, s->font, glyphs);

	pango_glyph_string_free(glyphs);
	pango_item_free(item);
}


/* Render Level 1 Storycode (no subframes) */
int render_sc(struct frame *fr, cairo_t *cr, double max_w, double max_h)
{
	PangoFontDescription *fontdesc;
	struct renderstuff s;
	GList *list;
	double w, h;
	int len;

	if ( fr->sc == NULL ) return 0;

	s.pc = pango_cairo_create_context(cr);
	s.fr = fr;

	/* If a minimum size is set, start there */
	if ( fr->lop.use_min_w ) w = fr->lop.min_w;
	if ( fr->lop.use_min_h ) h = fr->lop.min_h;

	/* FIXME: Handle images as well */

	/* Find and load font */
	s.fontmap = pango_cairo_font_map_get_default();
	fontdesc = pango_font_description_from_string("Serif 20");
	s.font = pango_font_map_load_font(s.fontmap, s.pc, fontdesc);
	if ( s.font == NULL ) {
		fprintf(stderr, "Failed to load font.\n");
		return 1;
	}

	len = strlen(fr->sc);
	s.log_attrs = calloc(len+1, sizeof(PangoLogAttr));
	if ( s.log_attrs == NULL ) {
		fprintf(stderr, "Failed to allocate memory for log attrs\n");
		return 1;
	}
	pango_get_log_attrs(fr->sc, len, -1,
	                    pango_language_get_default(), s.log_attrs, len+1);

	/* Create glyph string */
	list = pango_itemize(s.pc, fr->sc, 0, strlen(fr->sc), NULL, NULL);
	s.width = 0;
	g_list_foreach(list, calc_width, &s);

	/* Determine width */
	w = s.width / PANGO_SCALE;
	if ( fr->lop.use_min_w && (s.width < fr->lop.min_w) ) {
		w = fr->lop.min_w;
	}
	if ( w > max_w ) w = max_w;

	h = 50.0;
	if ( fr->lop.use_min_h && (h < fr->lop.min_h) ) {
		h = fr->lop.min_h;
	}
	if ( h > max_h ) h = max_h;

	/* Having decided on the size, create surface and render the contents */
	if ( fr->contents != NULL ) cairo_surface_destroy(fr->contents);
	fr->contents = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
	cr = cairo_create(fr->contents);
	s.cr = cr;

	cairo_rectangle(cr, w/2, h/2, w/2, h/2);
	cairo_set_source_rgb(cr, 0.0, 0.8, 1.0);
	cairo_fill(cr);

	/* FIXME: Add height of text to y coordinate */
	cairo_move_to(cr, fr->lop.pad_l, fr->lop.pad_t+h-fr->lop.pad_b);
	g_list_foreach(list, render_segment, &s);

	cairo_destroy(cr);
	g_list_free(list);
	pango_font_description_free(fontdesc);
	free(s.log_attrs);
	g_object_unref(s.font);
	g_object_unref(s.pc);

	fr->w = w;
	fr->h = h;

	return 0;
}


static void get_max_size(struct frame *fr, struct frame *parent,
                         int this_subframe, double fixed_x, double fixed_y,
                         double parent_max_width, double parent_max_height,
                         double *p_width, double *p_height)
{
	double w, h;
	int i;

	w = parent_max_width - parent->lop.pad_l - parent->lop.pad_r;
	w -= fr->lop.margin_l + fr->lop.margin_r;

	h = parent_max_height - parent->lop.pad_t - parent->lop.pad_b;
	h -= fr->lop.margin_t + fr->lop.margin_b;

	for ( i=0; i<this_subframe; i++ ) {

		//struct frame *ch;

		//ch = parent->children[i];

		/* FIXME: Shrink if this frame overlaps */
		switch ( fr->lop.grav )
		{
			case DIR_UL:
			/* Fix top left corner */
			break;

			case DIR_U:
			case DIR_UR:
			case DIR_R:
			case DIR_DR:
			case DIR_D:
			case DIR_DL:
			case DIR_L:
			fprintf(stderr, "Gravity not implemented.\n");
			break;

			case DIR_NONE:
			break;
		}
	}

	*p_width = w;
	*p_height = h;
}



static void position_frame(struct frame *fr, struct frame *parent)
{
	switch ( fr->lop.grav )
	{
		case DIR_UL:
		/* Fix top left corner */
		fr->x = parent->x + parent->lop.pad_l + fr->lop.margin_l;
		fr->y = parent->y + parent->lop.pad_t + fr->lop.margin_t;
		break;

		case DIR_U:
		case DIR_UR:
		case DIR_R:
		case DIR_DR:
		case DIR_D:
		case DIR_DL:
		case DIR_L:
		case DIR_NONE:
		fprintf(stderr, "Gravity not implemented.\n");
		break;

		break;
	}
}


static int render_frame(struct frame *fr, cairo_t *cr,
                        double max_w, double max_h)
{
	int i;

	/* Render all subframes */
	for ( i=0; i<fr->num_children; i++ ) {

		double x, y, child_max_w, child_max_h;
		struct frame *ch = fr->children[i];

		if ( ch->style != NULL ) {
			memcpy(&ch->lop, &ch->style->lop,
			       sizeof(struct layout_parameters));
		}

		/* Determine the maximum possible size this subframe can be */
		get_max_size(ch, fr, i, x, y,
		             max_w, max_h, &child_max_w, &child_max_h);

		/* Render it and hence (recursives) find out how much space it
		 * actually needs.*/
		render_frame(ch, cr, child_max_w, child_max_h);

		/* Position the frame within the parent */
		position_frame(ch, fr);

	}

	/* Render the actual contents of this frame in the remaining space */
	render_sc(fr, cr, max_w, max_h);

	return 0;
}


void recursive_buffer_free(struct frame *fr)
{
	int i;

	for ( i=0; i<fr->num_children; i++ ) {
		recursive_buffer_free(fr->children[i]);
	}

	if ( fr->contents != NULL ) {
		cairo_surface_destroy(fr->contents);
		fr->contents = NULL;
	}
}


void free_render_buffers(struct slide *s)
{
	recursive_buffer_free(s->top);
	if ( s->rendered_edit != NULL ) cairo_surface_destroy(s->rendered_edit);
	if ( s->rendered_proj != NULL ) cairo_surface_destroy(s->rendered_proj);
	if ( s->rendered_thumb != NULL ) {
		cairo_surface_destroy(s->rendered_thumb);
	}
}


static void do_composite(struct frame *fr, cairo_t *cr)
{
	if ( fr->contents == NULL ) return;

	cairo_rectangle(cr, fr->x, fr->y, fr->w, fr->h);
	cairo_set_source_surface(cr, fr->contents, fr->x, fr->y);
	cairo_fill_preserve(cr);
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
	cairo_stroke(cr);
}


static int composite_frames_at_level(struct frame *fr, cairo_t *cr,
                                     int level, int cur_level)
{
	int i;

	if ( level == cur_level ) {

		/* This frame is at the right level, so composite it */
		do_composite(fr, cr);
		return 1;

	} else {

		int n = 0;

		for ( i=0; i<fr->num_children; i++ ) {
			n += composite_frames_at_level(fr->children[i], cr,
			                               level, cur_level+1);
		}

		return n;

	}
}


static void composite_slide(struct slide *s, cairo_t *cr)
{
	int level = 0;
	int more = 0;

	do {
		more = composite_frames_at_level(s->top, cr, level, 0);
		level++;
	} while ( more != 0 );
}


cairo_surface_t *render_slide(struct slide *s, int w, int h)
{
	cairo_surface_t *surf;
	cairo_t *cr;
	cairo_font_options_t *fopts;

	surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);

	cr = cairo_create(surf);

	cairo_rectangle(cr, 0.0, 0.0, w, h);
	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	cairo_fill(cr);

	cairo_rectangle(cr, 0.0, 0.0, w, h);
	cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
	cairo_set_line_width(cr, 1.0);
	cairo_stroke(cr);

	fopts = cairo_font_options_create();
	cairo_font_options_set_hint_style(fopts, CAIRO_HINT_STYLE_NONE);
	cairo_font_options_set_hint_metrics(fopts, CAIRO_HINT_METRICS_OFF);
	cairo_font_options_set_antialias(fopts, CAIRO_ANTIALIAS_SUBPIXEL);
	cairo_set_font_options(cr, fopts);

	s->top->lop.min_w = w;
	s->top->lop.min_h = h;
	s->top->lop.use_min_w = 1;
	s->top->lop.use_min_h = 1;
	render_frame(s->top, cr, w, h);

	composite_slide(s, cr);

	cairo_font_options_destroy(fopts);
	cairo_destroy(cr);

	return surf;
}