aboutsummaryrefslogtreecommitdiff
path: root/libstorycode/narrative_render_cairo.c
blob: 17f22aa691b76e2db0330dc7116d4b0191c622f8 (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
 * narrative_render_cairo.c
 *
 * Copyright © 2013-2019 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 <libintl.h>
#define _(x) gettext(x)

#include "slide.h"
#include "narrative.h"
#include "stylesheet.h"
#include "imagestore.h"
#include "slide_render_cairo.h"
#include "narrative_render_cairo.h"

#include "narrative_priv.h"

const double dummy_h_val = 1024.0;


static double lcalc(struct length l, double pd)
{
	if ( l.unit == LENGTH_UNIT ) {
		return l.len;
	} else {
		return l.len * pd;
	}
}


static PangoAlignment to_pangoalignment(enum alignment align)
{
	switch ( align ) {
		case ALIGN_LEFT : return PANGO_ALIGN_LEFT;
		case ALIGN_RIGHT : return PANGO_ALIGN_RIGHT;
		case ALIGN_CENTER : return PANGO_ALIGN_CENTER;
		default: return PANGO_ALIGN_LEFT;
	}
}


static int find_pair(const gchar *p, gunichar c, gchar **start, gchar **end)
{
	gchar *s;
	gchar *e;
	gchar *next;

	/* FIXME: Check it's not escaped */
	s = g_utf8_strchr(p, -1, c);
	if ( s == NULL ) return 0;

	next = g_utf8_find_next_char(s, NULL);
	if ( next == NULL ) return 0;

	e = g_utf8_strchr(next, -1, c);
	if ( e == NULL ) return 0;

	*start = s;
	*end = e;
	return 1;
}


struct attr_to_add
{
	int start;
	int end;
	char type;  /* b=bold, i=italic, u=underline */
};


static int add_range(struct narrative_item *item, int *max_chars_removed,
                     int start, int end, int *n_add, int *max_add,
                     struct attr_to_add **add, char type)
{
	if ( item->n_chars_removed == *max_chars_removed ) {
		(*max_chars_removed) += 256;
		item->chars_removed = realloc(item->chars_removed, *max_chars_removed*sizeof(int));
		if ( item->chars_removed == NULL ) return 1;
	}

	item->chars_removed[item->n_chars_removed++] = start;
	item->chars_removed[item->n_chars_removed++] = end;

	if ( *n_add == *max_add ) {
		*max_add += 64;
		*add = realloc(*add, *max_add*sizeof(struct attr_to_add));
		if ( *add == NULL ) return 1;
	}

	(*add)[*n_add].start = start;
	(*add)[*n_add].end = end;
	(*add)[*n_add].type = type;
	(*n_add)++;

	return 0;
}


/* How many bytes were removed up to idx? */
int index_before_removal(int *chars_removed, int n_chars_removed, int idx)
{
	int i;

	for ( i=0; i<n_chars_removed; i++ ) {
		if ( chars_removed[i] > idx ) break;
	}

	return idx + i;
}


/* How many bytes were removed up to idx? */
int index_with_removal(int *chars_removed, int n_chars_removed, int idx)
{
	int i;

	for ( i=0; i<n_chars_removed; i++ ) {
		if ( chars_removed[i] > idx ) break;
	}

	return idx - i;
}


static int cmpi(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}


static void process_tags(struct narrative_item *item, PangoAttrList *attrs)
{
	gchar *efstart;
	gchar *efend;
	gchar *text;
	PangoAttribute *attr;
	struct attr_to_add *add;
	int n_add;
	int max_add;
	int max_chars_removed;
	int i, j, k;
	size_t len;

	item->n_chars_removed = 0;
	free(item->chars_removed);
	item->chars_removed = NULL;
	max_chars_removed = 0;

	add = NULL;
	n_add = 0;
	max_add = 0;

	/* Scan the text, identify characters to remove and character ranges
	 * (in the original text) which need attributes applied */
	text = item->text;
	while ( find_pair(text, '*', &efstart, &efend) ) {
		if ( add_range(item, &max_chars_removed,
		               efstart - item->text, efend - item->text,
		               &n_add, &max_add, &add, 'b') ) return;
		text = g_utf8_find_next_char(efend, NULL);
	}
	text = item->text;
	while ( find_pair(text, '/', &efstart, &efend) ) {
		if ( add_range(item, &max_chars_removed,
		               efstart - item->text, efend - item->text,
		               &n_add, &max_add, &add, 'i') ) return;
		text = g_utf8_find_next_char(efend, NULL);
	}
	text = item->text;
	while ( find_pair(text, '_', &efstart, &efend) ) {
		if ( add_range(item, &max_chars_removed,
		               efstart - item->text, efend - item->text,
		               &n_add, &max_add, &add, 'u') ) return;
		text = g_utf8_find_next_char(efend, NULL);
	}

	/* Sort the list of removed characters */
	qsort(item->chars_removed, item->n_chars_removed, sizeof(int), cmpi);

	/* Go through the list of attributes, and correct the character ranges
	 * so that they refer to the text with characters removed, and add them
	 * to the PangoAttrList */
	for ( i=0; i<n_add; i++ ) {

		switch ( add[i].type ) {

			case 'b' :
			attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
			break;

			case 'i' :
			attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
			break;

			case 'u' :
			attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
			break;

		}

		attr->start_index = index_with_removal(item->chars_removed,
		                                       item->n_chars_removed,
		                                       add[i].start);

		attr->end_index = index_with_removal(item->chars_removed,
		                                     item->n_chars_removed,
		                                     add[i].end) + 1;

		pango_attr_list_insert(attrs, attr);

	}

	/* Create the version of the text with characters removed */
	if ( item->n_chars_removed == 0 ) {
		   item->layout_text = strdup(item->text);
	} else {
		len = strlen(item->text);
		item->layout_text = malloc(len);
		if ( item->layout_text == NULL ) return;
		j = 0;
		for ( i=0; i<len+1; i++ ) {  /* \0 terminator please */
			if ( item->chars_removed[k] == i ) {
				k++;
			} else {
				item->layout_text[j++] = item->text[i];
			}
		}
	}

	free(add);
}


static void wrap_text(struct narrative_item *item, PangoContext *pc,
                      Stylesheet *ss, const char *stn, double w,
                      size_t sel_start, size_t sel_end)
{
	PangoAlignment palignment;
	PangoRectangle rect;
	const char *font;
	PangoFontDescription *fontdesc;
	enum alignment align;
	double wrap_w;
	PangoAttrList *attrs;
	PangoAttribute *attr;
	struct colour fgcol;
	guint16 r, g, b;

	font = stylesheet_get_font(ss, stn, &fgcol, &align);
	if ( font == NULL ) return;
	fontdesc = pango_font_description_from_string(font);

	if ( item->align == ALIGN_INHERIT ) {
		/* Use value from stylesheet */
		palignment = to_pangoalignment(align);
	} else {
		/* Use item-specific value */
		palignment = to_pangoalignment(item->align);
	}

	/* Calculate width of actual text */
	wrap_w = w - item->space_l - item->space_r;

	/* Set foreground colour */
	attrs = pango_attr_list_new();
	r = fgcol.rgba[0] * 65535;
	g = fgcol.rgba[1] * 65535;
	b = fgcol.rgba[2] * 65535;
	attr = pango_attr_foreground_new(r, g, b);
	pango_attr_list_insert(attrs, attr);

	/* Add attributes for selected text */
	if ( sel_start > 0 || sel_end > 0 ) {
		PangoAttribute *attr;
		attr = pango_attr_background_new(42919, 58853, 65535);
		attr->start_index = sel_start;
		attr->end_index = sel_end;
		pango_attr_list_insert(attrs, attr);
	}

	if ( item->layout == NULL ) {
		item->layout = pango_layout_new(pc);
	}
	pango_layout_set_width(item->layout, pango_units_from_double(wrap_w));
	pango_layout_set_alignment(item->layout, palignment);
	pango_layout_set_font_description(item->layout, fontdesc);

	/* Handle *bold*, _underline_, /italic/ etc. */
	process_tags(item, attrs);
	pango_layout_set_attributes(item->layout, attrs);
	pango_attr_list_unref(attrs);

	pango_layout_set_text(item->layout, item->layout_text, -1);

	pango_layout_get_extents(item->layout, NULL, &rect);
	item->obj_w = pango_units_to_double(rect.width);
	item->obj_h = pango_units_to_double(rect.height);
}


/* Render a thumbnail of the slide */
static cairo_surface_t *render_thumbnail(Slide *s, Stylesheet *ss, ImageStore *is,
                                         int w, int h)
{
	cairo_surface_t *surf;
	cairo_surface_t *full_surf;
	cairo_t *cr;
	double logical_w, logical_h;
	PangoContext *pc;
	const int rh = 1024; /* "reasonably big" height for slide */
	int rw;
	struct slide_pos sel;

	slide_get_logical_size(s, ss, &logical_w, &logical_h);
	rw = rh*(logical_w/logical_h);

	sel.para = 0;  sel.pos = 0;  sel.trail = 0;

	/* Render at a reasonably big size.  Rendering to a small surface makes
	 * rounding of text positions (due to font hinting) cause significant
	 * differences between the thumbnail and "normal" rendering. */
	full_surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24, rw, rh);
	cr = cairo_create(full_surf);
	cairo_scale(cr, (double)rw/logical_w, (double)rh/logical_h);
	pc = pango_cairo_create_context(cr);
	slide_render_cairo(s, cr, is, ss, 0, pango_language_get_default(), pc,
	                   NULL, sel, sel);
	g_object_unref(pc);
	cairo_destroy(cr);

	/* Scale down to the actual size of the thumbnail */
	surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
	cr = cairo_create(surf);
	cairo_scale(cr, (double)w/rw, (double)h/rh);
	cairo_set_source_surface(cr, full_surf, 0.0, 0.0);
	cairo_paint(cr);
	cairo_destroy(cr);

	cairo_surface_destroy(full_surf);
	return surf;
}


static void wrap_slide(struct narrative_item *item, Stylesheet *ss, ImageStore *is, int sel_block)
{
	double w, h;

	slide_get_logical_size(item->slide, ss, &w, &h);
	item->obj_h = 320.0;  /* Actual height of thumbnail */
	item->obj_w = rint(item->obj_h*w/h);

	if ( item->slide_thumbnail != NULL ) {
		cairo_surface_destroy(item->slide_thumbnail);
	}
	item->slide_thumbnail = render_thumbnail(item->slide, ss, is,
	                                         item->obj_w, item->obj_h);
	item->selected = sel_block;
}


static size_t pos_trail_to_offset(Narrative *n, int i, int offs, int trail)
{
	glong char_offs;
	char *ptr;
	struct narrative_item *item = &n->items[i];

	if ( !narrative_item_is_text(n, i) ) return offs;

	char_offs = g_utf8_pointer_to_offset(item->text, item->text+offs);
	char_offs += trail;
	ptr = g_utf8_offset_to_pointer(item->text, char_offs);
	return ptr - item->text;
}


static int positions_equal(struct edit_pos a, struct edit_pos b)
{
	if ( a.para != b.para ) return 0;
	if ( a.pos != b.pos ) return 0;
	if ( a.trail != b.trail ) return 0;
	return 1;
}


static void sort_positions(struct edit_pos *a, struct edit_pos *b)
{
	if ( a->para > b->para ) {
		size_t tpos;
		int tpara, ttrail;
		tpara = b->para;   tpos = b->pos;  ttrail = b->trail;
		b->para = a->para;  b->pos = a->pos;  b->trail = a->trail;
		a->para = tpara;    a->pos = tpos;    a->trail = ttrail;
	}

	if ( (a->para == b->para) && (a->pos > b->pos) )
	{
		size_t tpos = b->pos;
		int ttrail = b->trail;
		b->pos = a->pos;  b->trail = a->trail;
		a->pos = tpos;    a->trail = ttrail;
	}
}


static void wrap_marker(struct narrative_item *item, PangoContext *pc, double w,
                        int sel_block)
{
	item->obj_w = w - item->space_l - item->space_r;
	item->obj_h = 20.0;
	item->selected = sel_block;
}


int narrative_wrap_range(Narrative *n, Stylesheet *stylesheet, PangoLanguage *lang,
                         PangoContext *pc, double w, ImageStore *is,
                         int min, int max,
                         struct edit_pos sel_start, struct edit_pos sel_end)
{
	int i;
	struct length pad[4];
	int sel_s, sel_e;
	const char *stn;
	struct length paraspace[4];

	if ( stylesheet_get_padding(stylesheet, "NARRATIVE", pad) ) return 1;
	n->space_l = lcalc(pad[0], w);
	n->space_r = lcalc(pad[1], w);
	n->space_t = lcalc(pad[2], dummy_h_val);
	n->space_b = lcalc(pad[3], dummy_h_val);

	n->w = w;
	w -= n->space_l + n->space_r;

	sort_positions(&sel_start, &sel_end);
	if ( min < 0 ) min = 0;
	if ( max >= n->n_items ) max = n->n_items-1;

	if ( !positions_equal(sel_start, sel_end) ) {
		sel_s = pos_trail_to_offset(n, sel_start.para, sel_start.pos, sel_start.trail);
		sel_e = pos_trail_to_offset(n, sel_end.para, sel_end.pos, sel_end.trail);
	} else {
		sel_s = 0;
		sel_e = 0;
	}

	for ( i=min; i<=max; i++ ) {

		size_t srt, end;
		int sel_block = 0;

		if ( i >= sel_start.para && i <= sel_end.para ) {
			if ( i == sel_start.para ) {
				srt = sel_s;
			} else {
				srt = 0;
			}
			if ( i == sel_end.para ) {
				end = sel_e;
			} else {
				end = G_MAXUINT;
			}
			if ( i > sel_start.para && i < sel_end.para ) {
				end = G_MAXUINT;
			}
			sel_block = 1;
		} else {
			srt = 0;
			end = 0;
		}

		switch ( n->items[i].type ) {

			case NARRATIVE_ITEM_TEXT :
			stn = "NARRATIVE.TEXT";
			break;

			case NARRATIVE_ITEM_BP :
			stn = "NARRATIVE.BP";
			break;

			case NARRATIVE_ITEM_PRESTITLE :
			stn = "NARRATIVE.PRESTITLE";
			break;

			case NARRATIVE_ITEM_SLIDE :
			stn = "NARRATIVE.SLIDE";
			break;

			case NARRATIVE_ITEM_EOP :
			stn = "NARRATIVE.EOP";
			break;
		}

		if ( stylesheet_get_paraspace(stylesheet, stn, paraspace) == 0 ) {
			n->items[i].space_l = lcalc(paraspace[0], w);
			n->items[i].space_r = lcalc(paraspace[1], w);
			n->items[i].space_t = lcalc(paraspace[2], dummy_h_val);
			n->items[i].space_b = lcalc(paraspace[3], dummy_h_val);
		}

		switch ( n->items[i].type ) {

			case NARRATIVE_ITEM_TEXT :
			case NARRATIVE_ITEM_BP :
			case NARRATIVE_ITEM_PRESTITLE :
			wrap_text(&n->items[i], pc, stylesheet,
			          stn, w, srt, end);
			break;

			case NARRATIVE_ITEM_SLIDE :
			wrap_slide(&n->items[i], stylesheet, is, sel_block);
			break;

			case NARRATIVE_ITEM_EOP :
			wrap_marker(&n->items[i], pc, w, sel_block);
			break;
		}
	}

	return 0;
}


double narrative_item_get_height(Narrative *n, int i)
{
	return n->items[i].obj_h + n->items[i].space_t + n->items[i].space_b;
}


double narrative_get_height(Narrative *n)
{
	int i;
	double total = 0.0;
	for ( i=0; i<n->n_items; i++ ) {
		total += narrative_item_get_height(n, i);
	}
	return total + n->space_t + n->space_b;
}


static void draw_slide(struct narrative_item *item, cairo_t *cr)
{
	double x, y;

	cairo_save(cr);
	cairo_translate(cr, item->space_l, item->space_t);

	x = 0.0;  y = 0.0;
	cairo_user_to_device(cr, &x, &y);
	x = rint(x);  y = rint(y);
	cairo_device_to_user(cr, &x, &y);

	if ( item->selected ) {
		cairo_rectangle(cr, x-5.0, y-5.0, item->obj_w+10.0, item->obj_h+10.0);
		cairo_set_source_rgb(cr, 0.655, 0.899, 1.0);
		cairo_fill(cr);
	}

	cairo_rectangle(cr, x, y, item->obj_w, item->obj_h);
	cairo_set_source_surface(cr, item->slide_thumbnail, 0.0, 0.0);
	cairo_fill(cr);

	cairo_rectangle(cr, x+0.5, y+0.5, item->obj_w, item->obj_h);
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
	cairo_set_line_width(cr, 1.0);
	cairo_stroke(cr);

	cairo_restore(cr);
}


static void draw_marker(struct narrative_item *item, cairo_t *cr)
{
	double x, y;
	PangoLayout *layout;
	PangoFontDescription *fontdesc;

	cairo_save(cr);
	cairo_translate(cr, item->space_l, item->space_t);

	x = 0.0;  y = 0.0;
	cairo_user_to_device(cr, &x, &y);
	x = rint(x);  y = rint(y);
	cairo_device_to_user(cr, &x, &y);

	if ( item->selected ) {
		cairo_rectangle(cr, x-5.0, y-5.0, item->obj_w+10.0, item->obj_h+10.0);
		cairo_set_source_rgb(cr, 0.655, 0.899, 1.0);
		cairo_fill(cr);
	}

	cairo_rectangle(cr, x, y, item->obj_w, item->obj_h);
	cairo_set_source_rgb(cr, 0.8, 0.0, 0.0);
	cairo_fill(cr);

	layout = pango_cairo_create_layout(cr);
	pango_layout_set_text(layout, _("End of presentation"), -1);

	fontdesc = pango_font_description_new();
	pango_font_description_set_family_static(fontdesc, "Sans");
	pango_font_description_set_style(fontdesc, PANGO_STYLE_ITALIC);
	pango_font_description_set_absolute_size(fontdesc, 0.9*item->obj_h*PANGO_SCALE);
	pango_layout_set_font_description(layout, fontdesc);
	pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
	pango_layout_set_width(layout, item->obj_w*PANGO_SCALE);

	cairo_move_to(cr, 0.0, 0.0);
	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	pango_cairo_update_layout(cr, layout);
	pango_cairo_show_layout(cr, layout);
	cairo_fill(cr);

	g_object_unref(layout);
	pango_font_description_free(fontdesc);

	cairo_restore(cr);
}


static void draw_text(struct narrative_item *item, cairo_t *cr)
{
	if ( item->layout == NULL ) return;

	cairo_save(cr);
	cairo_translate(cr, item->space_l, item->space_t);

	//if ( (hpos + cur_h > min_y) && (hpos < max_y) ) {
		cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
		pango_cairo_update_layout(cr, item->layout);
		pango_cairo_show_layout(cr, item->layout);
		cairo_fill(cr);
	//} /* else paragraph is not visible */

	cairo_restore(cr);
}


int narrative_render_item_cairo(Narrative*n, cairo_t *cr, int i)
{
	switch ( n->items[i].type ) {

		case NARRATIVE_ITEM_TEXT :
		case NARRATIVE_ITEM_PRESTITLE :
		draw_text(&n->items[i], cr);
		break;

		case NARRATIVE_ITEM_BP :
		draw_text(&n->items[i], cr);
		break;

		case NARRATIVE_ITEM_SLIDE :
		draw_slide(&n->items[i], cr);
		break;

		case NARRATIVE_ITEM_EOP :
		draw_marker(&n->items[i], cr);
		break;

	}

	return 0;
}


/* NB You must first call narrative_wrap() */
int narrative_render_cairo(Narrative *n, cairo_t *cr, Stylesheet *stylesheet)
{
	int i, r;
	enum gradient bg;
	struct colour bgcol;
	struct colour bgcol2;
	cairo_pattern_t *patt = NULL;

	r = stylesheet_get_background(stylesheet, "NARRATIVE", &bg, &bgcol, &bgcol2);
	if ( r ) return 1;

	/* Overall background */
	cairo_rectangle(cr, 0.0, 0.0, n->w, narrative_get_height(n));
	switch ( bg ) {

		case GRAD_NONE:
		cairo_set_source_rgb(cr, bgcol.rgba[0], bgcol.rgba[1], bgcol.rgba[2]);
		break;

		case GRAD_VERT:
		patt = cairo_pattern_create_linear(0.0, 0.0, 0.0, narrative_get_height(n));
		cairo_pattern_add_color_stop_rgb(patt, 0.0,
		                                 bgcol.rgba[0], bgcol.rgba[1], bgcol.rgba[2]);
		cairo_pattern_add_color_stop_rgb(patt, 1.0,
		                                 bgcol2.rgba[0], bgcol2.rgba[1], bgcol2.rgba[2]);
		cairo_set_source(cr, patt);
		break;

		case GRAD_HORIZ:
		patt = cairo_pattern_create_linear(0.0, 0.0, n->w, 0.0);
		cairo_pattern_add_color_stop_rgb(patt, 0.0,
		                                 bgcol.rgba[0], bgcol.rgba[1], bgcol.rgba[2]);
		cairo_pattern_add_color_stop_rgb(patt, 1.0,
		                                 bgcol2.rgba[0], bgcol2.rgba[1], bgcol2.rgba[2]);
		cairo_set_source(cr, patt);
		break;

	}
	cairo_fill(cr);

	cairo_save(cr);
	cairo_translate(cr, n->space_l, n->space_t);

	for ( i=0; i<n->n_items; i++ ) {
		narrative_render_item_cairo(n, cr, i);
		cairo_translate(cr, 0.0, narrative_item_get_height(n, i));
	}

	cairo_restore(cr);

	return 0;
}