aboutsummaryrefslogtreecommitdiff
path: root/src/sc_interp.c
blob: dcafd360eb906b9f03ffb2303e096b2c1711ae23 (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
/*
 * sc_interp.c
 *
 * Copyright © 2014 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 <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pango/pangocairo.h>
#include <gdk/gdk.h>

#include "sc_parse.h"
#include "sc_interp.h"
#include "shape.h"
#include "wrap.h"


struct sc_state
{
	PangoFontDescription *fontdesc;
	PangoFont *font;
	double col[4];
	int ascent;
	int height;

	struct frame *fr;  /* The current frame */
};


struct _scinterp
{
	PangoContext *pc;
	PangoLanguage *lang;

	struct slide_constants *s_constants;
	struct presentation_constants *p_constants;

	struct sc_state *state;
	int j;  /* Index of the current state */
	int max_state;

	struct wrap_line *boxes;
};


PangoFont *sc_interp_get_font(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->font;
}


PangoFontDescription *sc_interp_get_fontdesc(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->fontdesc;
}


double *sc_interp_get_fgcol(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->col;
}


int sc_interp_get_ascent(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->ascent;
}


int sc_interp_get_height(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->height;
}


static void set_font(SCInterpreter *scin, const char *font_name)
{
	PangoFontMetrics *metrics;
	struct sc_state *st = &scin->state[scin->j];

	st->fontdesc = pango_font_description_from_string(font_name);
	if ( st->fontdesc == NULL ) {
		fprintf(stderr, "Couldn't describe font.\n");
		return;
	}
	st->font = pango_font_map_load_font(pango_context_get_font_map(scin->pc),
	                                    scin->pc, st->fontdesc);
	if ( st->font == NULL ) {
		fprintf(stderr, "Couldn't load font.\n");
		return;
	}

	/* FIXME: Language for box */
	metrics = pango_font_get_metrics(st->font, NULL);
	st->ascent = pango_font_metrics_get_ascent(metrics);
	st->height = st->ascent + pango_font_metrics_get_descent(metrics);
	pango_font_metrics_unref(metrics);
}


/* This sets the colour for the font at the top of the stack */
static void set_colour(SCInterpreter *scin, const char *colour)
{
	GdkRGBA col;
	struct sc_state *st = &scin->state[scin->j];

	if ( colour == NULL ) {
		printf("Invalid colour\n");
		st->col[0] = 0.0;
		st->col[1] = 0.0;
		st->col[2] = 0.0;
		st->col[3] = 1.0;
		return;
	}

	gdk_rgba_parse(&col, colour);

	st->col[0] = col.red;
	st->col[1] = col.green;
	st->col[2] = col.blue;
	st->col[3] = col.alpha;
}


void sc_interp_save(SCInterpreter *scin)
{
	if ( scin->j+1 == scin->max_state ) {

		struct sc_state *stack_new;

		stack_new = realloc(scin->state, sizeof(struct sc_state)
		                     * (scin->max_state+8));
		if ( stack_new == NULL ) {
			fprintf(stderr, "Failed to add to stack.\n");
			return;
		}

		scin->state = stack_new;
		scin->max_state += 8;

	}

	/* When n_fonts=0, we leave the first font uninitialised.  This allows
	 * the stack to be "bootstrapped", but requires the first caller to do
	 * set_font and set_colour straight away. */
	scin->state[scin->j+1] = scin->state[scin->j];
	scin->j++;
}


void sc_interp_restore(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];

	if ( scin->j > 0 ) {
		if ( st->fontdesc != scin->state[scin->j-1].fontdesc )
		{
			pango_font_description_free(st->fontdesc);
		} /* else the font is the same as the previous one, and we
		   * don't need to free it just yet */
	}

	scin->j--;
}


struct frame *sc_interp_get_frame(SCInterpreter *scin)
{
	struct sc_state *st = &scin->state[scin->j];
	return st->fr;
}


static void set_frame(SCInterpreter *scin, struct frame *fr)
{
	struct sc_state *st = &scin->state[scin->j];
	st->fr = fr;
}


SCInterpreter *sc_interp_new(PangoContext *pc, struct frame *top)
{
	SCInterpreter *scin;

	scin = malloc(sizeof(SCInterpreter));
	if ( scin == NULL ) return NULL;

	scin->state = malloc(8*sizeof(struct sc_state));
	if ( scin->state == NULL ) {
		free(scin);
		return NULL;
	}
	scin->j = 0;
	scin->max_state = 8;

	scin->pc = pc;
	scin->s_constants = NULL;
	scin->p_constants = NULL;

	/* FIXME: Determine proper language (somehow...) */
	scin->lang = pango_language_from_string("en_GB");

	scin->boxes = malloc(sizeof(struct wrap_line));
	if ( scin->boxes == NULL ) {
		fprintf(stderr, "Failed to allocate boxes.\n");
		return NULL;
	}
	initialise_line(scin->boxes);

	/* The "ultimate" default font */
	set_font(scin, "Sans 12");
	set_colour(scin, "#000000");
	set_frame(scin, top);

	return scin;
}


void sc_interp_destroy(SCInterpreter *scin)
{
	/* Empty the stack */
	while ( scin->j > 0 ) {
		sc_interp_restore(scin);
	}

	pango_font_description_free(scin->state[0].fontdesc);

	free(scin);
}


static LengthUnits get_units(const char *t)
{
	size_t len = strlen(t);

	if ( t[len-1] == 'f' ) return UNITS_FRAC;
	if ( t[len-1] == 'u' ) return UNITS_SLIDE;

	fprintf(stderr, "Invalid units in '%s'\n", t);
	return UNITS_SLIDE;
}


static void parse_frame_option(struct frame *fr, const char *opt)
{
	if ( (index(opt, 'x') != NULL) && (index(opt, '+') != NULL)
	  && (index(opt, '+') != rindex(opt, '+')) )
	{
		char *w;
		char *h;
		char *x;
		char *y;
		char *check;
		LengthUnits h_units, w_units;

		/* Looks like a dimension/position thing */
		w = strdup(opt);
		h = index(w, 'x');
		h[0] = '\0';  h++;

		x = index(h, '+');
		if ( x == NULL ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}
		x[0] = '\0';  x++;

		y = index(x, '+');
		if ( x == NULL ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}
		y[0] = '\0';  y++;

		fr->w = strtod(w, &check);
		if ( check == w ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}
		w_units = get_units(w);

		fr->h = strtod(h, &check);
		if ( check == h ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}
		h_units = get_units(h);
		/* FIXME: Handle units */

		fr->x = strtod(x, &check);
		if ( check == x ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}
		fr->y = strtod(y, &check);
		if ( check == y ) {
			fprintf(stderr, "Invalid option '%s'\n", opt);
			return;
		}

	}
}


static void parse_frame_options(struct frame *fr, const char *opth)
{
	int i;
	size_t len;
	size_t start;
	char *opt;

	if ( opth == NULL ) return;

	opt = strdup(opth);

	len = strlen(opt);
	start = 0;

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

		/* FIXME: comma might be escaped or quoted */
		if ( opt[i] == ',' ) {
			opt[i] = '\0';
			parse_frame_option(fr, opt+start);
			start = i+1;
		}

	}

	if ( start != len ) {
		parse_frame_option(fr, opt+start);
	}

	free(opt);
}


int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl)
{
	while ( bl != NULL ) {

		const char *name = sc_block_name(bl);
		const char *options = sc_block_options(bl);
		const char *contents = sc_block_contents(bl);
		SCBlock *child = sc_block_child(bl);

		if ( child != NULL ) {
			sc_interp_save(scin);
		}

		if ( name == NULL ) {
			split_words(scin->boxes, scin->pc, contents,
			            scin->lang, 1, scin);

		} else if ( strcmp(name, "font") == 0 ) {
			set_font(scin, options);

		} else if ( strcmp(name, "fgcol") == 0 ) {
			set_colour(scin, options);

#if 0
		} else if ( strcmp(name, "image")==0 ) {
			int w, h;
			if ( get_size(options, fr, &w, &h) == 0 ) {
				add_image_box(boxes, b->contents, offset, w, h,
				              editable);
			}

		} else if ( strcmp(name, "slidenumber")==0) {
			char *tmp = malloc(64);
			if ( tmp != NULL ) {
				snprintf(tmp, 63, "%i",
				         slide_constants->slide_number);
				add_wrap_box(boxes, tmp, offset,
					     WRAP_SPACE_NONE, pc,
					     &fonts->stack[fonts->n_fonts-1],
			                     0);
			} /* else go away and sulk about it */
#endif

		} else if ( strcmp(name, "f")==0 ) {
			struct frame *fr = sc_block_frame(bl);
			if ( fr == NULL ) {
				fr = add_subframe(sc_interp_get_frame(scin));
				sc_block_set_frame(bl, fr);
				fr->scblocks = child;
			}
			if ( fr == NULL ) {
				fprintf(stderr, "Failed to add frame.\n");
				goto next;
			}
			parse_frame_options(fr, options);
			set_frame(scin, fr);

		} else {

			fprintf(stderr, "Don't know what to do with this:\n");
			show_sc_block(bl, "");

		}

next:
		if ( child != NULL ) {
			sc_interp_add_blocks(scin, child);
			sc_interp_restore(scin);
		}
		bl = sc_block_next(bl);

	}

	return 0;
}


struct wrap_line *sc_interp_get_boxes(SCInterpreter *scin)
{
	return scin->boxes;
}