2 * Hic Est Leo -- Styling
4 * (c) 2014 Martin Mares <mj@ucw.cz>
8 #include <ucw/mempool.h>
16 struct dict style_prop_dict, style_layer_dict;
18 static const char * const style_wk_props[] = {
20 #define P(x,y) [PROP_##x] = y,
21 #include "dict-props.h"
26 static const char * const style_wk_layers[] = {
32 void styles_init(void)
34 dict_init(&style_prop_dict, style_wk_props);
35 dict_init(&style_layer_dict, style_wk_layers);
38 #define HASH_NODE struct style_prop
39 #define HASH_PREFIX(x) style_prop_##x
40 #define HASH_KEY_ATOMIC key
41 #define HASH_WANT_FIND
42 #define HASH_WANT_LOOKUP
43 #define HASH_GIVE_ALLOC
44 // FIXME: Make <ucw/hashtable.h> accept our pool
45 #define HASH_ZERO_FILL
46 #define HASH_TABLE_DYNAMIC
47 #define HASH_TABLE_ALLOC
48 #define HASH_TABLE_VARS struct mempool *pool;
49 static void *style_prop_alloc(struct style_prop_table *table, uns size);
50 static inline void style_prop_free(struct style_prop_table *table UNUSED, void *x UNUSED) { }
51 #include <ucw/hashtable.h>
53 static void *style_prop_alloc(struct style_prop_table *table, uns size)
55 return mp_alloc_fast(table->pool, size);
58 void style_init(struct style_results *r)
60 r->pool = mp_new(4096);
61 r->num_layers = dict_size(&style_layer_dict);
62 r->layers = mp_alloc_zero(r->pool, r->num_layers * sizeof(struct style_info *));
63 r->num_active_layers = 0;
64 r->active_layers = mp_alloc_zero(r->pool, r->num_layers * sizeof(layer_t));
67 void style_cleanup(struct style_results *r)
72 void style_begin(struct style_results *r, struct osm_object *o)
74 ASSERT(!r->num_active_layers);
79 void style_end(struct style_results *r)
81 for (uns i=0; i<r->num_active_layers; i++)
82 r->layers[r->active_layers[i]] = NULL;
84 r->num_active_layers = 0;
88 static inline void style_assign(struct style_prop *dest, struct style_prop *src)
90 dest->type = src->type;
94 static struct style_info *style_get_info(struct style_results *r, layer_t layer)
96 ASSERT(layer < r->num_layers);
97 if (!r->layers[layer])
99 struct style_info *si = mp_alloc_zero(r->pool, sizeof(*si));
100 r->layers[layer] = si;
101 si->hash = mp_alloc_zero(r->pool, sizeof(struct style_prop_table));
102 si->hash->pool = r->pool;
103 style_prop_init(si->hash);
105 if (layer != STYLE_LAYER_ALL)
107 r->active_layers[r->num_active_layers++] = layer;
109 // Copy all properties which have been set for all layers
110 struct style_info *si_all = r->layers[STYLE_LAYER_ALL];
114 * CAVEAT: This is probably wrong. When no properties are set explicitly
115 * set for a layer, all-layer properties are not propagated. Hopefully harmless.
117 HASH_FOR_ALL_DYNAMIC(style_prop, si_all->hash, s)
119 style_assign(style_prop_lookup(si->hash, s->key), s);
125 return r->layers[layer];
128 void style_enable_default_layer(struct style_results *r)
130 style_get_info(r, STYLE_LAYER_DEFAULT);
133 void style_set_by_layer(struct style_results *r, layer_t layer, struct style_prop *p)
135 if (layer == STYLE_LAYER_ALL)
137 // Set in all existing layers
138 for (uns i=0; i < r->num_active_layers; i++)
139 style_assign(style_prop_lookup(r->layers[r->active_layers[i]]->hash, p->key), p);
140 // ... and let it propagate to STYLE_LAYER_ALL
143 struct style_info *si = style_get_info(r, layer);
144 style_assign(style_prop_lookup(si->hash, p->key), p);
147 void style_set(struct style_info *si, struct style_prop *p)
149 style_assign(style_prop_lookup(si->hash, p->key), p);
152 struct style_prop *style_get(struct style_info *si, prop_t key)
154 return style_prop_find(si->hash, key);
157 struct style_prop *style_get_and_check(struct style_info *si, prop_t key, uns allowed_types)
159 struct style_prop *p = style_prop_find(si->hash, key);
162 if (!(allowed_types & (1 << p->type)))
164 // XXX: Better diagnostics?
165 msg(L_WARN, "Style property %s set to invalid type #%u", style_prop_decode(p->key), p->type);
171 osm_val_t style_get_ident(struct style_info *si, prop_t key)
173 struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_IDENT);
174 return p ? p->val.id : 0;
177 osm_val_t style_get_string(struct style_info *si, prop_t key)
179 struct style_prop *p = style_get_and_check(si, key, (1 << PROP_TYPE_STRING) | (1 << PROP_TYPE_IDENT));
180 return p ? p->val.id : 0;
183 bool style_get_number(struct style_info *si, prop_t key, double *dp)
185 struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_NUMBER);
192 bool style_get_color(struct style_info *si, prop_t key, color_t *colorp)
194 struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_COLOR);
197 *colorp = p->val.color;
201 void style_dump(struct style_results *r)
203 for (uns i=0; i < r->num_active_layers; i++)
205 layer_t layer = r->active_layers[i];
206 printf("Layer %s (%u)\n", style_layer_decode(layer), layer);
207 struct style_info *si = r->layers[layer];
208 HASH_FOR_ALL_DYNAMIC(style_prop, si->hash, s)
217 static void style_dump_val(struct style_prop *s)
223 case PROP_TYPE_STRING:
224 printf("%s [string]", osm_val_decode(s->val.id));
226 case PROP_TYPE_IDENT:
227 printf("%s [ident]", osm_val_decode(s->val.id));
229 case PROP_TYPE_NUMBER:
230 printf("%.6g [number]", s->val.number);
232 case PROP_TYPE_COLOR:
233 printf("%06x [color]", s->val.color);
237 CLIST_FOR_EACH(struct style_val_list_entry *, e, *s->val.list)
241 style_dump_val(&e->val);
246 printf("[unknown type %u]", s->type);
250 void style_dump_prop(struct style_prop *s)
252 printf("%s = ", style_prop_decode(s->key));
257 void style_scale(struct style_info *si, double *wp, double *hp, prop_t width_prop, prop_t height_prop)
260 bool got_width = style_get_number(si, width_prop, &w);
261 bool got_height = style_get_number(si, height_prop, &h);
263 if (got_width + got_height == 2)
268 else if (got_width + got_height == 1)