]> mj.ucw.cz Git - leo.git/blob - style.c
Acknowledge that we are using deprecared API of libproj
[leo.git] / style.c
1 /*
2  *      Hic Est Leo -- Styling
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/mempool.h>
9
10 #include <stdio.h>
11
12 #include "leo.h"
13 #include "osm.h"
14 #include "style.h"
15
16 struct dict style_prop_dict, style_layer_dict;
17
18 static const char * const style_wk_props[] = {
19   NULL,
20 #define P(x,y) [PROP_##x] = y,
21 #include "dict-props.h"
22 #undef P
23   NULL
24 };
25
26 static const char * const style_wk_layers[] = {
27   NULL,
28   "default",
29   NULL
30 };
31
32 void styles_init(void)
33 {
34   dict_init(&style_prop_dict, style_wk_props);
35   dict_init(&style_layer_dict, style_wk_layers);
36 }
37
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>
52
53 static void *style_prop_alloc(struct style_prop_table *table, uns size)
54 {
55   return mp_alloc_fast(table->pool, size);
56 }
57
58 void style_init(struct style_results *r)
59 {
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));
65 }
66
67 void style_begin(struct style_results *r, struct osm_object *o)
68 {
69   ASSERT(!r->num_active_layers);
70   mp_push(r->pool);
71   r->obj = o;
72 }
73
74 void style_end(struct style_results *r)
75 {
76   for (uns i=0; i<r->num_active_layers; i++)
77     r->layers[r->active_layers[i]] = NULL;
78   r->layers[0] = NULL;
79   r->num_active_layers = 0;
80   mp_pop(r->pool);
81 }
82
83 static inline void style_assign(struct style_prop *dest, struct style_prop *src)
84 {
85   dest->type = src->type;
86   dest->val = src->val;
87 }
88
89 static struct style_info *style_get_info(struct style_results *r, layer_t layer)
90 {
91   ASSERT(layer < r->num_layers);
92   if (!r->layers[layer])
93     {
94       struct style_info *si = mp_alloc_zero(r->pool, sizeof(*si));
95       r->layers[layer] = si;
96       si->hash = mp_alloc_zero(r->pool, sizeof(struct style_prop_table));
97       si->hash->pool = r->pool;
98       style_prop_init(si->hash);
99
100       if (layer != STYLE_LAYER_ALL)
101         {
102           r->active_layers[r->num_active_layers++] = layer;
103
104           // Copy all properties which have been set for all layers
105           struct style_info *si_all = r->layers[STYLE_LAYER_ALL];
106           if (si_all)
107             {
108               /*
109                * CAVEAT: This is probably wrong. When no properties are set explicitly
110                * set for a layer, all-layer properties are not propagated. Hopefully harmless.
111                */
112               HASH_FOR_ALL_DYNAMIC(style_prop, si_all->hash, s)
113                 {
114                   style_assign(style_prop_lookup(si->hash, s->key), s);
115                 }
116               HASH_END_FOR;
117             }
118         }
119     }
120   return r->layers[layer];
121 }
122
123 void style_set_by_layer(struct style_results *r, layer_t layer, struct style_prop *p)
124 {
125   if (layer == STYLE_LAYER_ALL)
126     {
127       // Set in all existing layers
128       for (uns i=0; i < r->num_active_layers; i++)
129         style_assign(style_prop_lookup(r->layers[r->active_layers[i]]->hash, p->key), p);
130       // ... and let it propagate to STYLE_LAYER_ALL
131     }
132
133   struct style_info *si = style_get_info(r, layer);
134   style_assign(style_prop_lookup(si->hash, p->key), p);
135 }
136
137 void style_set(struct style_info *si, struct style_prop *p)
138 {
139   style_assign(style_prop_lookup(si->hash, p->key), p);
140 }
141
142 struct style_prop *style_get(struct style_info *si, prop_t key)
143 {
144   return style_prop_find(si->hash, key);
145 }
146
147 struct style_prop *style_get_and_check(struct style_info *si, prop_t key, uns allowed_types)
148 {
149   struct style_prop *p = style_prop_find(si->hash, key);
150   if (!p)
151     return NULL;
152   if (!(allowed_types & (1 << p->type)))
153     {
154       // XXX: Better diagnostics?
155       msg(L_WARN, "Style property %s set to invalid type #%u", style_prop_decode(p->key), p->type);
156       return NULL;
157     }
158   return p;
159 }
160
161 osm_val_t style_get_ident(struct style_info *si, prop_t key)
162 {
163   struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_IDENT);
164   return p ? p->val.id : 0;
165 }
166
167 osm_val_t style_get_string(struct style_info *si, prop_t key)
168 {
169   struct style_prop *p = style_get_and_check(si, key, (1 << PROP_TYPE_STRING) | (1 << PROP_TYPE_IDENT));
170   return p ? p->val.id : 0;
171 }
172
173 bool style_get_number(struct style_info *si, prop_t key, double *dp)
174 {
175   struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_NUMBER);
176   if (!p)
177     return 0;
178   *dp = p->val.number;
179   return 1;
180 }
181
182 bool style_get_color(struct style_info *si, prop_t key, color_t *colorp)
183 {
184   struct style_prop *p = style_get_and_check(si, key, 1 << PROP_TYPE_COLOR);
185   if (!p)
186     return 0;
187   *colorp = p->val.color;
188   return 1;
189 }
190
191 void style_dump(struct style_results *r)
192 {
193   for (uns i=0; i < r->num_active_layers; i++)
194     {
195       layer_t layer = r->active_layers[i];
196       printf("Layer %s (%u)\n", style_layer_decode(layer), layer);
197       struct style_info *si = r->layers[layer];
198       HASH_FOR_ALL_DYNAMIC(style_prop, si->hash, s)
199         {
200           printf("\t");
201           style_dump_prop(s);
202         }
203       HASH_END_FOR;
204     }
205 }
206
207 static void style_dump_val(struct style_prop *s)
208 {
209   uns cnt = 0;
210
211   switch (s->type)
212     {
213     case PROP_TYPE_STRING:
214       printf("%s [string]", osm_val_decode(s->val.id));
215       break;
216     case PROP_TYPE_IDENT:
217       printf("%s [ident]", osm_val_decode(s->val.id));
218       break;
219     case PROP_TYPE_NUMBER:
220       printf("%.6g [number]", s->val.number);
221       break;
222     case PROP_TYPE_COLOR:
223       printf("%06x [color]", s->val.color);
224       break;
225     case PROP_TYPE_LIST:
226       putchar('(');
227       CLIST_FOR_EACH(struct style_val_list_entry *, e, *s->val.list)
228         {
229           if (cnt++)
230             printf(", ");
231           style_dump_val(&e->val);
232         }
233       printf(") [list]");
234       break;
235     default:
236       printf("[unknown type %u]", s->type);
237     }
238 }
239
240 void style_dump_prop(struct style_prop *s)
241 {
242   printf("%s = ", style_prop_decode(s->key));
243   style_dump_val(s);
244   putchar('\n');
245 }
246
247 void style_scale(struct style_info *si, double *wp, double *hp, prop_t width_prop, prop_t height_prop)
248 {
249   double w=0, h=0;
250   bool got_width = style_get_number(si, width_prop, &w);
251   bool got_height = style_get_number(si, height_prop, &h);
252
253   if (got_width + got_height == 2)
254     {
255       *wp = w;
256       *hp = h;
257     }
258   else if (got_width + got_height == 1)
259     {
260       if (got_width)
261         {
262           *hp *= w / *wp;
263           *wp = w;
264         }
265       else
266         {
267           *wp *= h / *hp;
268           *hp = h;
269         }
270     }
271 }