aboutsummaryrefslogtreecommitdiff
path: root/src/stylesheet.c
blob: 4a22d2a5c9ae1216060127950ea156996e715518 (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
/*
 * stylesheet.c
 *
 * Colloquium - A tiny presentation program
 *
 * Copyright (c) 2011 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 <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <assert.h>

#include "presentation.h"
#include "stylesheet.h"
#include "loadsave.h"


struct _stylesheet
{
	struct style          **styles;
	int                     n_styles;

	struct slide_template **templates;
	int                     n_templates;
};


struct style *new_style(StyleSheet *ss, const char *name)
{
	struct style *sty;
	int n;
	struct style **styles_new;

	sty = calloc(1, sizeof(*sty));
	if ( sty == NULL ) return NULL;

	sty->name = strdup(name);

	/* DS9K compatibility */
	sty->lop.x = 0.0;
	sty->lop.y = 0.0;
	sty->lop.w = 100.0;
	sty->lop.h = 100.0;

	n = ss->n_styles;
	styles_new = realloc(ss->styles, (n+1)*sizeof(sty));
	if ( styles_new == NULL ) {
		free(sty->name);
		free(sty);
		return NULL;
	}
	ss->styles = styles_new;
	ss->styles[n] = sty;
	ss->n_styles = n+1;

	return sty;
}


void free_stylesheet(StyleSheet *ss)
{
	int i;

	for ( i=0; i<ss->n_styles; i++ ) {
		free(ss->styles[i]->name);
		free(ss->styles[i]);
	}

	free(ss->styles);
	free(ss);
}


void default_stylesheet(StyleSheet *ss)
{
	struct style *sty;
	struct slide_template *titlepage;
	struct slide_template *slide;
	struct slide_template *acknowledgements;

	titlepage = new_template(ss, "Title page");
	slide = new_template(ss, "Slide");
	acknowledgements = new_template(ss, "Acknowledgements");

	sty = new_style(ss, "Presentation title");
	sty->lop.margin_l = 0.0;
	sty->lop.margin_r = 0.0;
	sty->lop.margin_t = 0.0;
	sty->lop.margin_b = 0.0;
	sty->lop.pad_l = 0.0;
	sty->lop.pad_r = 0.0;
	sty->lop.pad_t = 0.0;
	sty->lop.pad_b = 0.0;
	sty->lop.w = 1.0;
	sty->lop.w_units = UNITS_FRAC;
	sty->lop.h = 100.0;
	sty->lop.h_units = UNITS_SLIDE;
	sty->lop.x = 0.0;
	sty->lop.y = 300.0;
	add_to_template(titlepage, sty);

	sty = new_style(ss, "Slide title");
	sty->lop.margin_l = 0.0;
	sty->lop.margin_r = 0.0;
	sty->lop.margin_t = 0.0;
	sty->lop.margin_b = 0.0;
	sty->lop.pad_l = 20.0;
	sty->lop.pad_r = 20.0;
	sty->lop.pad_t = 20.0;
	sty->lop.pad_b = 20.0;
	sty->lop.w = 1.0;
	sty->lop.w_units = UNITS_FRAC;
	sty->lop.h = 100.0;
	sty->lop.h_units = UNITS_SLIDE;
	sty->lop.x = 0.0;
	sty->lop.y = 0.0;
	sty->sc_prologue = strdup("\\bgcol{#00a6eb}\\fgcol{#ffffff}");
	add_to_template(slide, sty);
}


static int read_style(struct style *sty, struct ds_node *root)
{
	get_field_f(root, "margin_l", &sty->lop.margin_l);
	get_field_f(root, "margin_r", &sty->lop.margin_r);
	get_field_f(root, "margin_t", &sty->lop.margin_t);
	get_field_f(root, "margin_b", &sty->lop.margin_b);

	return 0;
}


StyleSheet *tree_to_stylesheet(struct ds_node *root)
{
	StyleSheet *ss;
	struct ds_node *node;
	int i;

	ss = new_stylesheet();
	if ( ss == NULL ) return NULL;

	node = find_node(root, "styles", 0);
	if ( node == NULL ) {
		fprintf(stderr, "Couldn't find styles\n");
		free_stylesheet(ss);
		return NULL;
	}

	for ( i=0; i<node->n_children; i++ ) {

		struct style *ns;
		char *v;

		get_field_s(node->children[i], "name", &v);
		if ( v == NULL ) {
			fprintf(stderr, "No name for style '%s'\n",
			        node->children[i]->key);
			continue;
		}

		ns = new_style(ss, v);
		if ( ns == NULL ) {
			fprintf(stderr, "Couldn't create style for '%s'\n",
			        node->children[i]->key);
			continue;
		}

		if ( read_style(ns, node->children[i]) ) {
			fprintf(stderr, "Couldn't read style '%s'\n", v);
			continue;
		}

	}

	node = find_node(root, "bgblocks", 0);
	if ( node == NULL ) {
		fprintf(stderr, "Couldn't find bgblocks\n");
		free_stylesheet(ss);
		return NULL;
	}

	return ss;
}


StyleSheet *new_stylesheet()
{
	StyleSheet *ss;

	ss = calloc(1, sizeof(struct _stylesheet));
	if ( ss == NULL ) return NULL;

	ss->n_styles = 0;
	ss->styles = NULL;

	return ss;
}


int save_stylesheet(StyleSheet *ss, const char *filename)
{
	FILE *fh;
	struct serializer ser;

	fh = fopen(filename, "w");
	if ( fh == NULL ) return 1;

	/* Set up the serializer */
	ser.fh = fh;
	ser.stack_depth = 0;
	ser.prefix = NULL;

	fprintf(fh, "# Colloquium style sheet file\n");
	serialize_f(&ser, "version", 0.1);

	serialize_start(&ser, "stylesheet");
	write_stylesheet(ss, &ser);
	serialize_end(&ser);

	return 0;
}


StyleSheet *load_stylesheet(const char *filename)
{
	StyleSheet *ss;

	ss = new_stylesheet();
	if ( ss == NULL ) return NULL;

	/* FIXME: Implement this */

	return ss;
}


void write_stylesheet(StyleSheet *ss, struct serializer *ser)
{
	int i;

	serialize_start(ser, "styles");
	for ( i=0; i<ss->n_styles; i++ ) {

		struct style *s = ss->styles[i];
		char id[32];

		snprintf(id, 31, "%i", i);

		serialize_start(ser, id);
		serialize_s(ser, "name", s->name);
		serialize_f(ser, "margin_l", s->lop.margin_l);
		serialize_f(ser, "margin_r", s->lop.margin_r);
		serialize_f(ser, "margin_t", s->lop.margin_t);
		serialize_f(ser, "margin_b", s->lop.margin_b);
		serialize_end(ser);

	}
	serialize_end(ser);
}


struct style *find_style(StyleSheet *ss, const char *name)
{
	int i;
	for ( i=0; i<ss->n_styles; i++ ) {
		if ( strcmp(ss->styles[i]->name, name) == 0 ) {
			return ss->styles[i];
		}
	}

	return NULL;
}


struct slide_template *new_template(StyleSheet *ss, const char *name)
{
	struct slide_template *t;
	int n;
	struct slide_template **templates_new;

	t = calloc(1, sizeof(*t));
	if ( t == NULL ) return NULL;

	t->name = strdup(name);

	n = ss->n_templates;
	templates_new = realloc(ss->templates, (n+1)*sizeof(t));
	if ( templates_new == NULL ) {
		free(t->name);
		free(t);
		return NULL;
	}
	ss->templates = templates_new;
	ss->templates[n] = t;
	ss->n_templates = n+1;

	return t;
}


void add_to_template(struct slide_template *t, struct style *sty)
{
	int n;
	struct style **styles_new;

	n = t->n_styles;
	styles_new = realloc(t->styles, (n+1)*sizeof(sty));
	if ( styles_new == NULL ) {
		fprintf(stderr, "Failed to add style '%s' to template '%s'.\n",
		        sty->name, t->name);
		return;
	}
	t->styles = styles_new;
	t->styles[n] = sty;
	t->n_styles = n+1;
}


struct _styleiterator
{
	int n;
};

struct style *style_first(StyleSheet *ss, StyleIterator **piter)
{
	StyleIterator *iter;

	if ( ss->n_styles == 0 ) return NULL;

	iter = calloc(1, sizeof(StyleIterator));
	if ( iter == NULL ) return NULL;

	iter->n = 0;
	*piter = iter;

	return ss->styles[0];
}


struct style *style_next(StyleSheet *ss, StyleIterator *iter)
{
	iter->n++;
	if ( iter->n == ss->n_styles ) {
		free(iter);
		return NULL;
	}

	return ss->styles[iter->n];
}


struct _templateiterator
{
	int n;
};

struct slide_template *template_first(StyleSheet *ss, TemplateIterator **piter)
{
	TemplateIterator *iter;

	if ( ss->n_templates == 0 ) return NULL;

	iter = calloc(1, sizeof(TemplateIterator));
	if ( iter == NULL ) return NULL;

	iter->n = 0;
	*piter = iter;

	return ss->templates[0];
}


struct slide_template *template_next(StyleSheet *ss, TemplateIterator *iter)
{
	iter->n++;
	if ( iter->n == ss->n_templates ) {
		free(iter);
		return NULL;
	}

	return ss->templates[iter->n];
}