summaryrefslogtreecommitdiff
path: root/ccss/ccss-grammar-function.c
blob: a3ca58b8307379d24225dafd84236d060761719a (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
/* vim: set ts=8 sw=8 noexpandtab: */

/* The `C' CSS Library.
 * Copyright (C) 2008 Robert Staudinger
 *
 * This  library is free  software; you can  redistribute it and/or
 * modify it  under  the terms  of the  GNU Lesser  General  Public
 * License  as published  by the Free  Software  Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License  along  with  this library;  if not,  write to  the Free
 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <string.h>
#include <glib.h>
#include "ccss-grammar-priv.h"
#include "ccss-property-impl.h"
#include "config.h"

static GSList *
parse_args_r (GSList		 *args,
	      CRTerm const	**values)
{
	char		*val;
	char const	*unit;
	CRTerm const	*args_term;

	while (*values) {
		val = NULL;
		switch ((*values)->type) {
		case TERM_NUMBER:
			switch ((*values)->content.num->type) {
			case NUM_GENERIC:
				unit = "";
				break;
			case NUM_LENGTH_PX:
				unit = "px";
				break;
			case NUM_PERCENTAGE:
				unit = "%";
				break;
			default:
				g_assert_not_reached ();
				unit = "";
			}
			val = g_strdup_printf ("%f%s", (*values)->content.num->val, unit);
			args = g_slist_prepend (args, val);
			break;
		case TERM_STRING:
		case TERM_IDENT:
		case TERM_URI:
			val = g_strdup_printf ("%s", cr_string_peek_raw_str ((*values)->content.str));
			args = g_slist_prepend (args, val);
			break;
		case TERM_HASH:
			val = g_strdup_printf ("#%s", cr_string_peek_raw_str ((*values)->content.str));
			args = g_slist_prepend (args, val);			
			break;
		case TERM_FUNCTION:

			val = g_strdup_printf ("%s(", cr_string_peek_raw_str ((*values)->content.str));
			args = g_slist_prepend (args, val);

			args_term = (*values)->ext_content.func_param;
			args = parse_args_r (args, &args_term);

			val = g_strdup (")");
			args = g_slist_prepend (args, val);

			break;
		case TERM_RGB:

			val = g_strdup ("rgb(");
			args = g_slist_prepend (args, val);

			val = g_strdup_printf ("%f", (*values)->content.rgb->red / 255.);
			args = g_slist_prepend (args, val);

			val = g_strdup (",");
			args = g_slist_prepend (args, val);

			val = g_strdup_printf ("%f", (*values)->content.rgb->green / 255.);
			args = g_slist_prepend (args, val);

			val = g_strdup (",");
			args = g_slist_prepend (args, val);

			val = g_strdup_printf ("%f", (*values)->content.rgb->blue / 255.);
			args = g_slist_prepend (args, val);

			val = g_strdup (")");
			args = g_slist_prepend (args, val);

			break;
		case TERM_NO_TYPE:
		case TERM_UNICODERANGE:
		default:
			g_assert_not_reached ();
			*values = (*values)->next;
			continue;
		}

		if ((*values)->next) {
			val = g_strdup (",");
			args = g_slist_prepend (args, val);
		}

		*values = (*values)->next;
	}

	return args;
}
/**
 * ccss_grammar_invoke_function:
 * @self:		a #ccss_grammar_t.
 * @function_name:	name of the function to invoke, e.g. %url.
 * @values:		arguments passed to the function handler.
 * @user_data:		user-data passed to the function handler.
 *			Overrides the user-data assigned in the function handler's definition.
 *
 * Invoke a registerd function handler. This API is meant to be used by property
 * implementations, like when parsing properties like `background-image: url(foo.png)'.
 *
 * Returns: string value passed back by the ccss API consumer.
 **/
char *
ccss_grammar_invoke_function (ccss_grammar_t const	*self,
			      char const		*function_name,
			      CRTerm const		*values,
			      void			*user_data)
{
	ccss_function_t const	*handler;
	GSList			*args;
	void			*actual_user_data;
	char			*ret;

	g_return_val_if_fail (self && function_name, NULL);

	handler = (ccss_function_t const *)
			g_hash_table_lookup (self->functions, function_name);

	if (!handler) {
		g_warning ("Function `%s' could not be resolved.", function_name);
		return NULL;
	}

	/* parse args */
	args = parse_args_r (NULL, &values);
	args = g_slist_reverse (args);

	/* dispatch */
	if (user_data) {
		actual_user_data = user_data;
	} else {
		actual_user_data = handler->user_data;
	}
	ret = handler->function (args, actual_user_data);

	/* free args */
	while (args) {
		g_free (args->data);
		args = g_slist_delete_link (args, args);
	}

	return ret;
}