2 * Hic Est Leo -- Line and Area Symbolizer
4 * (c) 2014 Martin Mares <mj@ucw.cz>
8 #include <ucw/fastbuf.h>
9 #include <ucw/mempool.h>
19 static void sym_line_attrs(struct sym_line *l, struct svg *svg)
21 svg_set_attr(svg, "fill", "none");
22 svg_set_attr_color(svg, "stroke", l->color);
23 svg_set_attr_dimen(svg, "stroke-width", l->width);
25 svg_set_attr_float(svg, "stroke-opacity", l->opacity);
30 // This is the default (butt)
34 svg_set_attr(svg, "stroke-linecap", osm_val_decode(l->line_cap));
37 osm_obj_warn(l->s.o, "Unknown stroke-linecap: %s", osm_val_decode(l->line_cap));
43 // This is the default
44 if (l->miter_limit != 4)
45 svg_set_attr_float(svg, "stroke-miterlimit", l->miter_limit);
49 svg_set_attr(svg, "stroke-linejoin", osm_val_decode(l->line_join));
52 osm_obj_warn(l->s.o, "Unknown stroke-linejoin: %s", osm_val_decode(l->line_join));
57 struct fastbuf *fb = svg_fb_open(svg);
58 for (uns i=0; i < GARY_SIZE(l->dash_pattern); i++)
62 // FIXME: This is dimension-sensitive
63 // FIXME: Also, inkscape doesn't handle units in dash lengths
64 bprintf(fb, "%.6g", l->dash_pattern[i]);
66 svg_fb_close_as_attr(svg, "stroke-dasharray");
68 svg_set_attr_float(svg, "stroke-dashoffset", l->dash_offset);
72 static void append_node_list(struct svg *svg, clist *list)
74 struct osm_node *first = NULL;
75 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *list)
80 svg_path_move_to(svg, n->x, n->y);
85 svg_path_line_to(svg, n->x, n->y);
90 static void make_path(struct osm_object *o, struct svg *svg)
97 struct osm_way *w = (struct osm_way *) o;
98 append_node_list(svg, &w->nodes);
101 case OSM_TYPE_MULTIPOLYGON:
103 struct osm_multipolygon *m = (struct osm_multipolygon *) o;
104 CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
105 append_node_list(svg, &b->nodes);
114 static void sym_line_draw(struct symbol *sym, struct svg *svg)
116 struct sym_line *l = (struct sym_line *) sym;
117 make_path(sym->o, svg);
118 sym_line_attrs(l, svg);
122 static void sym_line_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
124 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
127 struct sym_line *main_sl = NULL;
129 // Line and its casing are two similar objects
130 for (uns casing=0; casing<2; casing++)
133 if (!style_get_number(si, PROP_WIDTH + casing, &w))
136 struct sym_line *sl = sym_line_new(o);
143 osm_obj_warn(o, "Casing around no line");
146 sl->width += main_sl->width;
151 sl->color = 0x808080;
152 style_get_color(si, PROP_FILL_COLOR, &sl->color);
153 style_get_color(si, PROP_COLOR + casing, &sl->color);
156 style_get_number(si, PROP_OPACITY + casing, &sl->opacity);
158 sl->line_cap = style_get_ident(si, PROP_LINECAP + casing) ? : VALUE_NONE;
159 sl->line_join = style_get_ident(si, PROP_LINEJOIN + casing) ? : VALUE_ROUND;
160 sl->miter_limit = 10;
161 style_get_number(si, PROP_MITERLIMIT + casing, &sl->miter_limit);
163 struct style_prop *dashes = style_get(si, PROP_DASHES + casing);
164 if (!dashes || dashes->type == PROP_TYPE_IDENT && dashes->val.id == VALUE_NONE)
166 else if (dashes->type == PROP_TYPE_LIST)
168 GARY_INIT_ALLOC(sl->dash_pattern, 0, mp_get_allocator(sym_mp));
169 CLIST_FOR_EACH(struct style_val_list_entry *, e, *dashes->val.list)
171 if (e->val.type == PROP_TYPE_NUMBER)
172 *GARY_PUSH(sl->dash_pattern) = e->val.val.number;
175 osm_obj_warn(o, "Invalid dash pattern");
179 style_get_number(si, PROP_DASHES_OFFSET + casing, &sl->dash_offset);
182 osm_obj_warn(o, "Invalid dash pattern");
184 if (o->type == OSM_TYPE_WAY)
187 sym_plan(&sl->s, sym_zindex(o, si, 2));
189 labeller_add_line(&sl->s, sym_zindex(o, si, casing ? 2 : 3));
191 //sym_plan(&sl->s, sym_zindex(o, si, casing ? 2 : 3));
195 struct symbolizer symbolizer_line = {
197 .draw = sym_line_draw,
201 struct sym_line *sym_line_new(struct osm_object *o)
203 return sym_new(SYMBOLIZER_LINE, o, sizeof(struct sym_line));
206 /*** Images along line ***/
208 static void lineimg_node_list(struct sym_lineimg *sli, clist *nodes, struct svg *svg)
210 double phase = sli->phase;
211 double step = sli->sir.width + sli->spacing;
212 struct osm_node *prev = NULL;
213 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *nodes)
217 double dx = n->x - prev->x;
218 double dy = n->y - prev->y;
219 double len = hypot(dx, dy);
221 while (len - dist > 1e-10)
223 // FIXME: Rotate images along the path?
224 double next = MIN(len - dist, step - phase);
227 if (step - phase < 1e-10)
229 struct svg_icon_request sir = sli->sir;
230 sir.x = prev->x + dx * dist / len;
231 sir.y = prev->y + dy * dist / len;
232 svg_icon_put(svg, &sir);
242 static void sym_lineimg_draw(struct symbol *sym, struct svg *svg)
244 struct sym_lineimg *li = (struct sym_lineimg *) sym;
245 struct osm_object *o = li->s.o;
251 struct osm_way *w = (struct osm_way *) o;
252 lineimg_node_list(li, &w->nodes, svg);
255 case OSM_TYPE_MULTIPOLYGON:
257 struct osm_multipolygon *m = (struct osm_multipolygon *) o;
258 CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
259 lineimg_node_list(li, &b->nodes, svg);
267 static void sym_lineimg_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
269 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
272 osm_val_t icon_name = style_get_string(si, PROP_REPEAT_IMAGE);
276 struct sym_lineimg *sli = sym_lineimg_new(o);
278 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(icon_name));
279 sli->sir.icon = icon;
280 sli->sir.width = icon->width;
281 sli->sir.height = icon->height;
282 style_scale(si, &sli->sir.width, &sli->sir.height, PROP_REPEAT_IMAGE_WIDTH, PROP_REPEAT_IMAGE_HEIGHT);
284 // FIXME: Better handling of defaults in style_get_number()
286 // FIXME: align and offset are not supported yet
287 sli->align = style_get_ident(si, PROP_REPEAT_IMAGE_ALIGN);
289 style_get_number(si, PROP_REPEAT_IMAGE_OFFSET, &sli->offset);
292 style_get_number(si, PROP_REPEAT_IMAGE_SPACING, &sli->spacing);
294 style_get_number(si, PROP_REPEAT_IMAGE_PHASE, &sli->phase);
296 sym_plan(&sli->s, sym_zindex(o, si, 3));
299 struct symbolizer symbolizer_lineimg = {
301 .draw = sym_lineimg_draw,
302 .gen = sym_lineimg_gen,
305 struct sym_lineimg *sym_lineimg_new(struct osm_object *o)
307 return sym_new(SYMBOLIZER_LINEIMG, o, sizeof(struct sym_lineimg));
312 static void sym_area_draw(struct symbol *sym, struct svg *svg)
314 struct sym_area *a = (struct sym_area *) sym;
316 for (int i=0; i<2; i++)
320 if (a->fill_color == COLOR_NONE)
322 make_path(sym->o, svg);
323 svg_set_attr_color(svg, "fill", a->fill_color);
327 if (!a->fill_pattern)
329 make_path(sym->o, svg);
330 svg_set_attr(svg, "fill", a->fill_pattern->paint_server);
332 if (a->fill_opacity != 1)
333 svg_set_attr_float(svg, "opacity", a->fill_opacity);
334 if (sym->o->type == OSM_TYPE_MULTIPOLYGON)
335 svg_set_attr(svg, "fill-rule", "evenodd");
340 static void sym_area_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
342 if (!(o->type == OSM_TYPE_WAY && osm_way_cyclic_p((struct osm_way *) o) ||
343 o->type == OSM_TYPE_MULTIPOLYGON))
347 if (!style_get_color(si, PROP_FILL_COLOR, &color))
350 osm_val_t pattern = style_get_string(si, PROP_FILL_PATTERN);
351 struct svg_pattern *patt = NULL;
354 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(pattern));
355 struct svg_pattern_request spr = {
357 .width = icon->width,
358 .height = icon->height
360 style_scale(si, &spr.width, &spr.height, PROP_FILL_PATTERN_WIDTH, PROP_FILL_PATTERN_HEIGHT);
361 patt = svg_icon_to_pattern(svg, &spr);
364 if (color == COLOR_NONE && !patt)
367 struct sym_area *sa = sym_area_new(o);
368 sa->fill_color = color;
369 sa->fill_pattern = patt;
371 sa->fill_opacity = 1;
372 style_get_number(si, PROP_FILL_OPACITY, &sa->fill_opacity);
374 sym_plan(&sa->s, sym_zindex(o, si, 1));
377 struct symbolizer symbolizer_area = {
379 .draw = sym_area_draw,
383 struct sym_area *sym_area_new(struct osm_object *o)
385 return sym_new(SYMBOLIZER_AREA, o, sizeof(struct sym_area));