]> mj.ucw.cz Git - leo.git/blob - sym-line.c
Updated UCW::Configure to the version from current LibUCW
[leo.git] / sym-line.c
1 /*
2  *      Hic Est Leo -- Line and Area Symbolizer
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/fastbuf.h>
9 #include <ucw/mempool.h>
10
11 #include <math.h>
12 #include <stdio.h>
13
14 #include "leo.h"
15 #include "osm.h"
16 #include "sym.h"
17
18 static void sym_line_attrs(struct sym_line *l, struct svg *svg)
19 {
20   svg_set_attr(svg, "fill", "none");
21   svg_set_attr_color(svg, "stroke", l->color);
22   svg_set_attr_dimen(svg, "stroke-width", l->width);
23   if (l->opacity != 1)
24     svg_set_attr_float(svg, "stroke-opacity", l->opacity);
25
26   switch (l->line_cap)
27     {
28     case VALUE_NONE:
29       // This is the default (butt)
30       break;
31     case VALUE_ROUND:
32     case VALUE_SQUARE:
33       svg_set_attr(svg, "stroke-linecap", osm_val_decode(l->line_cap));
34       break;
35     default:
36       osm_obj_warn(l->s.o, "Unknown stroke-linecap: %s", osm_val_decode(l->line_cap));
37     }
38
39   switch (l->line_join)
40     {
41     case VALUE_MITER:
42       // This is the default
43       if (l->miter_limit != 4)
44         svg_set_attr_float(svg, "stroke-miterlimit", l->miter_limit);
45       break;
46     case VALUE_ROUND:
47     case VALUE_BEVEL:
48       svg_set_attr(svg, "stroke-linejoin", osm_val_decode(l->line_join));
49       break;
50     default:
51       osm_obj_warn(l->s.o, "Unknown stroke-linejoin: %s", osm_val_decode(l->line_join));
52     }
53
54   if (l->dash_pattern)
55     {
56       struct fastbuf *fb = svg_fb_open(svg);
57       for (uns i=0; i < GARY_SIZE(l->dash_pattern); i++)
58         {
59           if (i)
60             bputc(fb, ',');
61           // FIXME: This is dimension-sensitive
62           // FIXME: Also, inkscape doesn't handle units in dash lengths
63           bprintf(fb, "%.6g", l->dash_pattern[i]);
64         }
65       svg_fb_close_as_attr(svg, "stroke-dasharray");
66       if (l->dash_offset)
67         svg_set_attr_float(svg, "stroke-dashoffset", l->dash_offset);
68     }
69 }
70
71 static void append_node_list(struct svg *svg, clist *list)
72 {
73   struct osm_node *first = NULL;
74   OSM_FOR_EACH_BEGIN(struct osm_node *, n, *list)
75     {
76       if (!first)
77         {
78           first = n;
79           svg_path_move_to(svg, n->x, n->y);
80         }
81       else if (n == first)
82         svg_path_close(svg);
83       else
84         svg_path_line_to(svg, n->x, n->y);
85     }
86   OSM_FOR_EACH_END;
87 }
88
89 static void make_path(struct osm_object *o, struct svg *svg)
90 {
91   svg_push_path(svg);
92   switch (o->type)
93     {
94     case OSM_TYPE_WAY:
95       {
96         struct osm_way *w = (struct osm_way *) o;
97         append_node_list(svg, &w->nodes);
98         break;
99       }
100     case OSM_TYPE_MULTIPOLYGON:
101       {
102         struct osm_multipolygon *m = (struct osm_multipolygon *) o;
103         CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
104           append_node_list(svg, &b->nodes);
105         break;
106       }
107     default:
108       ASSERT(0);
109     }
110   svg_path_end(svg);
111 }
112
113 static void sym_line_draw(struct symbol *sym, struct svg *svg)
114 {
115   struct sym_line *l = (struct sym_line *) sym;
116   make_path(sym->o, svg);
117   sym_line_attrs(l, svg);
118   svg_pop(svg);
119 }
120
121 static void sym_line_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
122 {
123   if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
124     return;
125
126   struct sym_line *main_sl = NULL;
127
128   // Line and its casing are two similar objects
129   for (uns casing=0; casing<2; casing++)
130     {
131       double w;
132       if (!style_get_number(si, PROP_WIDTH + casing, &w))
133         continue;
134
135       struct sym_line *sl = sym_line_new(o);
136       sl->width = w;
137
138       if (casing)
139         {
140           if (!main_sl)
141             {
142               osm_obj_warn(o, "Casing around no line");
143               continue;
144             }
145           sl->width += main_sl->width;
146         }
147       else
148         main_sl = sl;
149
150       sl->color = 0x808080;
151       style_get_color(si, PROP_FILL_COLOR, &sl->color);
152       style_get_color(si, PROP_COLOR + casing, &sl->color);
153
154       sl->opacity = 1;
155       style_get_number(si, PROP_OPACITY + casing, &sl->opacity);
156
157       sl->line_cap = style_get_ident(si, PROP_LINECAP + casing) ? : VALUE_NONE;
158       sl->line_join = style_get_ident(si, PROP_LINEJOIN + casing) ? : VALUE_ROUND;
159       sl->miter_limit = 10;
160       style_get_number(si, PROP_MITERLIMIT + casing, &sl->miter_limit);
161
162       struct style_prop *dashes = style_get(si, PROP_DASHES + casing);
163       if (!dashes || dashes->type == PROP_TYPE_IDENT && dashes->val.id == VALUE_NONE)
164         ;
165       else if (dashes->type == PROP_TYPE_LIST)
166         {
167           GARY_INIT_ALLOC(sl->dash_pattern, 0, mp_get_allocator(sym_mp));
168           CLIST_FOR_EACH(struct style_val_list_entry *, e, *dashes->val.list)
169             {
170               if (e->val.type == PROP_TYPE_NUMBER)
171                 *GARY_PUSH(sl->dash_pattern) = e->val.val.number;
172               else
173                 {
174                   osm_obj_warn(o, "Invalid dash pattern");
175                   break;
176                 }
177             }
178           style_get_number(si, PROP_DASHES_OFFSET + casing, &sl->dash_offset);
179         }
180       else
181         osm_obj_warn(o, "Invalid dash pattern");
182
183       sym_plan(&sl->s, sym_zindex(o, si, casing ? 2 : 3));
184     }
185 }
186
187 struct symbolizer symbolizer_line = {
188   .name = "line",
189   .draw = sym_line_draw,
190   .gen = sym_line_gen,
191 };
192
193 struct sym_line *sym_line_new(struct osm_object *o)
194 {
195   return sym_new(SYMBOLIZER_LINE, o, sizeof(struct sym_line));
196 }
197
198 /*** Images along line ***/
199
200 static void lineimg_node_list(struct sym_lineimg *sli, clist *nodes, struct svg *svg)
201 {
202   double phase = sli->phase;
203   double step = sli->sir.width + sli->spacing;
204   struct osm_node *prev = NULL;
205   OSM_FOR_EACH_BEGIN(struct osm_node *, n, *nodes)
206     {
207       if (prev)
208         {
209           double dx = n->x - prev->x;
210           double dy = n->y - prev->y;
211           double len = hypot(dx, dy);
212           double dist = 0;
213           while (len - dist > 1e-10)
214             {
215               // FIXME: Rotate images along the path?
216               double next = MIN(len - dist, step - phase);
217               dist += next;
218               phase += next;
219               if (step - phase < 1e-10)
220                 {
221                   struct svg_icon_request sir = sli->sir;
222                   sir.x = prev->x + dx * dist / len;
223                   sir.y = prev->y + dy * dist / len;
224                   svg_icon_put(svg, &sir);
225                   phase -= step;
226                 }
227             }
228         }
229       prev = n;
230     }
231   OSM_FOR_EACH_END;
232 }
233
234 static void sym_lineimg_draw(struct symbol *sym, struct svg *svg)
235 {
236   struct sym_lineimg *li = (struct sym_lineimg *) sym;
237   struct osm_object *o = li->s.o;
238
239   switch (o->type)
240     {
241     case OSM_TYPE_WAY:
242       {
243         struct osm_way *w = (struct osm_way *) o;
244         lineimg_node_list(li, &w->nodes, svg);
245         break;
246       }
247     case OSM_TYPE_MULTIPOLYGON:
248       {
249         struct osm_multipolygon *m = (struct osm_multipolygon *) o;
250         CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
251           lineimg_node_list(li, &b->nodes, svg);
252         break;
253       }
254     default:
255       ASSERT(0);
256     }
257 }
258
259 static void sym_lineimg_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
260 {
261   if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
262     return;
263
264   osm_val_t icon_name = style_get_string(si, PROP_REPEAT_IMAGE);
265   if (!icon_name)
266     return;
267
268   struct sym_lineimg *sli = sym_lineimg_new(o);
269
270   struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(icon_name));
271   sli->sir.icon = icon;
272   sli->sir.width = icon->width;
273   sli->sir.height = icon->height;
274   style_scale(si, &sli->sir.width, &sli->sir.height, PROP_REPEAT_IMAGE_WIDTH, PROP_REPEAT_IMAGE_HEIGHT);
275
276   // FIXME: Better handling of defaults in style_get_number()
277 #if 0
278   // FIXME: align and offset are not supported yet
279   sli->align = style_get_ident(si, PROP_REPEAT_IMAGE_ALIGN);
280   sli->offset = 0;
281   style_get_number(si, PROP_REPEAT_IMAGE_OFFSET, &sli->offset);
282 #endif
283   sli->spacing = 0;
284   style_get_number(si, PROP_REPEAT_IMAGE_SPACING, &sli->spacing);
285   sli->phase = 0;
286   style_get_number(si, PROP_REPEAT_IMAGE_PHASE, &sli->phase);
287
288   sym_plan(&sli->s, sym_zindex(o, si, 3));
289 }
290
291 struct symbolizer symbolizer_lineimg = {
292   .name = "lineimg",
293   .draw = sym_lineimg_draw,
294   .gen = sym_lineimg_gen,
295 };
296
297 struct sym_lineimg *sym_lineimg_new(struct osm_object *o)
298 {
299   return sym_new(SYMBOLIZER_LINEIMG, o, sizeof(struct sym_lineimg));
300 }
301
302 /*** Areas ***/
303
304 static void sym_area_draw(struct symbol *sym, struct svg *svg)
305 {
306   struct sym_area *a = (struct sym_area *) sym;
307
308   for (int i=0; i<2; i++)
309     {
310       if (!i)
311         {
312           if (a->fill_color == COLOR_NONE)
313             continue;
314           make_path(sym->o, svg);
315           svg_set_attr_color(svg, "fill", a->fill_color);
316         }
317       else
318         {
319           if (!a->fill_pattern)
320             continue;
321           make_path(sym->o, svg);
322           svg_set_attr(svg, "fill", a->fill_pattern->paint_server);
323         }
324       if (a->fill_opacity != 1)
325         svg_set_attr_float(svg, "opacity", a->fill_opacity);
326       if (sym->o->type == OSM_TYPE_MULTIPOLYGON)
327         svg_set_attr(svg, "fill-rule", "evenodd");
328       svg_pop(svg);
329     }
330 }
331
332 static void sym_area_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
333 {
334   if (!(o->type == OSM_TYPE_WAY && osm_way_cyclic_p((struct osm_way *) o) ||
335         o->type == OSM_TYPE_MULTIPOLYGON))
336     return;
337
338   color_t color;
339   if (!style_get_color(si, PROP_FILL_COLOR, &color))
340     color = COLOR_NONE;
341
342   osm_val_t pattern = style_get_string(si, PROP_FILL_PATTERN);
343   struct svg_pattern *patt = NULL;
344   if (pattern)
345     {
346       struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(pattern));
347       struct svg_pattern_request spr = {
348         .icon = icon,
349         .width = icon->width,
350         .height = icon->height
351       };
352       style_scale(si, &spr.width, &spr.height, PROP_FILL_PATTERN_WIDTH, PROP_FILL_PATTERN_HEIGHT);
353       patt = svg_icon_to_pattern(svg, &spr);
354     }
355
356   if (color == COLOR_NONE && !patt)
357     return;
358
359   struct sym_area *sa = sym_area_new(o);
360   sa->fill_color = color;
361   sa->fill_pattern = patt;
362
363   sa->fill_opacity = 1;
364   style_get_number(si, PROP_FILL_OPACITY, &sa->fill_opacity);
365
366   sym_plan(&sa->s, sym_zindex(o, si, 1));
367 }
368
369 struct symbolizer symbolizer_area = {
370   .name = "area",
371   .draw = sym_area_draw,
372   .gen = sym_area_gen,
373 };
374
375 struct sym_area *sym_area_new(struct osm_object *o)
376 {
377   return sym_new(SYMBOLIZER_AREA, o, sizeof(struct sym_area));
378 }