aboutsummaryrefslogtreecommitdiff
path: root/src-old/render.c
blob: 6ac09fcb7834b6fa3f558da2a9da141b8ceb273c (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
/*
 * render.c
 *
 * Copyright © 2013-2018 Thomas White <taw@bitwiz.org.uk>
 *
 * This file is part of Colloquium.
 *
 * Colloquium 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 <string.h>
#include <stdlib.h>
#include <math.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdk.h>

#include "sc_parse.h"
#include "sc_interp.h"
#include "presentation.h"
#include "frame.h"
#include "render.h"
#include "imagestore.h"
#include "utils.h"


static void do_background(cairo_t *cr, struct frame *fr)
{
	cairo_pattern_t *patt = NULL;

	cairo_new_path(cr);
	cairo_rectangle(cr, 0.0, 0.0, fr->w, fr->h);

	switch ( fr->grad ) {

		case GRAD_NONE:
		cairo_set_source_rgba(cr, fr->bgcol[0],
		                          fr->bgcol[1],
		                          fr->bgcol[2],
			                  fr->bgcol[3]);
		break;

		case GRAD_VERT:
		patt = cairo_pattern_create_linear(0.0, 0.0,
			                           0.0, fr->h);
		cairo_pattern_add_color_stop_rgba(patt, 0.0, fr->bgcol[0],
		                                             fr->bgcol[1],
		                                             fr->bgcol[2],
		                                             fr->bgcol[3]);
		cairo_pattern_add_color_stop_rgba(patt, 1.0, fr->bgcol2[0],
		                                             fr->bgcol2[1],
		                                             fr->bgcol2[2],
		                                             fr->bgcol2[3]);
		cairo_set_source(cr, patt);
		break;

		case GRAD_HORIZ:
		patt = cairo_pattern_create_linear(0.0, 0.0,
			                           fr->w, 0.0);
		cairo_pattern_add_color_stop_rgba(patt, 0.0, fr->bgcol[0],
			                                     fr->bgcol[1],
			                                     fr->bgcol[2],
			                                     fr->bgcol[3]);
		cairo_pattern_add_color_stop_rgba(patt, 1.0, fr->bgcol2[0],
			                                     fr->bgcol2[1],
			                                     fr->bgcol2[2],
			                                     fr->bgcol2[3]);
		cairo_set_source(cr, patt);
		break;

	}

	cairo_fill(cr);
	if ( patt != NULL ) cairo_pattern_destroy(patt);
}


static int draw_frame(cairo_t *cr, struct frame *fr, ImageStore *is,
                      double min_y, double max_y)
{
	int i;
	double hpos = 0.0;

	cairo_save(cr);
	do_background(cr, fr);

	/* Actually render the contents */
	cairo_translate(cr, fr->pad_l, fr->pad_t);
	for ( i=0; i<fr->n_paras; i++ ) {

		double cur_h = paragraph_height(fr->paras[i]);

		cairo_save(cr);
		cairo_translate(cr, 0.0, hpos);

		if ( (hpos + cur_h > min_y) && (hpos < max_y) ) {
			render_paragraph(cr, fr->paras[i], is);
		} /* else paragraph is not visible */

		hpos += cur_h;
		cairo_restore(cr);

	}
	cairo_restore(cr);

	return 0;
}


int recursive_draw(struct frame *fr, cairo_t *cr,
                   ImageStore *is,
                   double min_y, double max_y)
{
	int i;

	draw_frame(cr, fr, is, min_y, max_y);

	for ( i=0; i<fr->num_children; i++ ) {
		cairo_save(cr);
		cairo_translate(cr, fr->children[i]->x, fr->children[i]->y);
		recursive_draw(fr->children[i], cr, is,
		               min_y - fr->children[i]->y,
		               max_y - fr->children[i]->y);
		cairo_restore(cr);
	}

	return 0;
}


void wrap_frame(struct frame *fr, PangoContext *pc)
{
	int i;
	double w;

	w = fr->w - fr->pad_l - fr->pad_r;

	for ( i=0; i<fr->n_paras; i++ ) {
		wrap_paragraph(fr->paras[i], pc, w, 0, 0);
	}
}


int recursive_wrap(struct frame *fr, PangoContext *pc)
{
	int i;

	wrap_frame(fr, pc);

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

	return 0;
}


struct frame *interp_and_shape(SCBlock *scblocks, Stylesheet *stylesheet,
                               SCCallbackList *cbl, ImageStore *is,
                               int slide_number,
                               PangoContext *pc, double w, double h,
                               PangoLanguage *lang)
{
	SCInterpreter *scin;
	char snum[64];
	struct frame *top;

	top = frame_new();
	top->resizable = 0;
	top->x = 0.0;
	top->y = 0.0;
	top->w = w;
	top->h = h;
	top->scblocks = scblocks;

	scin = sc_interp_new(pc, lang, is, top);
	if ( scin == NULL ) {
		fprintf(stderr, "Failed to set up interpreter.\n");
		frame_free(top);
		return NULL;
	}

	sc_interp_set_callbacks(scin, cbl);

	snprintf(snum, 63, "%i", slide_number);
	sc_interp_set_constant(scin, SCCONST_SLIDENUMBER, snum);

	top->fontdesc = pango_font_description_copy(sc_interp_get_fontdesc(scin));
	top->col[0] = sc_interp_get_fgcol(scin)[0];
	top->col[1] = sc_interp_get_fgcol(scin)[1];
	top->col[2] = sc_interp_get_fgcol(scin)[2];
	top->col[3] = sc_interp_get_fgcol(scin)[3];

	sc_interp_add_block(scin, scblocks, stylesheet);

	sc_interp_destroy(scin);

	return top;
}


static struct frame *render_sc_with_context(SCBlock *scblocks,
                                 cairo_t *cr, double log_w, double log_h,
                                 Stylesheet *stylesheet, SCCallbackList *cbl,
                                 ImageStore *is,
                                 int slide_number, PangoLanguage *lang,
				 PangoContext *pc)
{
	struct frame *top;

	cairo_rectangle(cr, 0.0, 0.0, log_w, log_h);
	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	cairo_fill(cr);

	top = interp_and_shape(scblocks, stylesheet, cbl, is,
	                       slide_number, pc, log_w, log_h, lang);

	recursive_wrap(top, pc);

	recursive_draw(top, cr, is, 0.0, log_h);

	return top;
}


cairo_surface_t *render_sc(SCBlock *scblocks, int w, int h,
                           double log_w, double log_h,
                           Stylesheet *stylesheet, SCCallbackList *cbl,
                           ImageStore *is,
                           int slide_number, struct frame **ptop,
                           PangoLanguage *lang)
{
	cairo_surface_t *surf;
	cairo_t *cr;
	struct frame *top;
	PangoContext *pc;

	surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
	cr = cairo_create(surf);
	pc = pango_cairo_create_context(cr);
	cairo_scale(cr, w/log_w, h/log_h);
	top = render_sc_with_context(scblocks, cr, log_w, log_h,
	                             stylesheet, cbl, is, slide_number,
	                             lang, pc);
	g_object_unref(pc);
	cairo_destroy(cr);

	*ptop = top;

	return surf;
}


int export_pdf(struct presentation *p, const char *filename)
{
	double r;
	double w = 2048.0;
	double scale;
	cairo_surface_t *surf;
	cairo_t *cr;
	SCBlock *bl;
	int i;
	PangoContext *pc;

	r = p->slide_height / p->slide_width;

	surf = cairo_pdf_surface_create(filename, w, w*r);
	if ( cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS ) {
		fprintf(stderr, _("Couldn't create Cairo surface\n"));
		return 1;
	}

	cr = cairo_create(surf);
	scale = w / p->slide_width;
	pc = pango_cairo_create_context(cr);

	i = 1;
	bl = p->scblocks;
	while ( bl != NULL ) {

		if ( safe_strcmp(sc_block_name(bl), "slide") != 0 ) {
			bl = sc_block_next(bl);
			continue;
		}

		cairo_save(cr);

		cairo_scale(cr, scale, scale);

		cairo_rectangle(cr, 0.0, 0.0, p->slide_width, p->slide_height);
		cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
		cairo_fill(cr);

		render_sc_with_context(bl, cr, p->slide_width,
		                       p->slide_height, p->stylesheet, NULL,
		                       p->is, i, p->lang, pc);

		cairo_restore(cr);

		cairo_show_page(cr);

		bl = sc_block_next(bl);
		i++;

	}

	g_object_unref(pc);
	cairo_surface_finish(surf);
	cairo_destroy(cr);

	return 0;
}