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>
18 static void sym_line_attrs(struct sym_line *l, struct svg *svg)
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);
24 svg_set_attr_float(svg, "stroke-opacity", l->opacity);
29 // This is the default (butt)
33 svg_set_attr(svg, "stroke-linecap", osm_val_decode(l->line_cap));
36 osm_obj_warn(l->s.o, "Unknown stroke-linecap: %s", osm_val_decode(l->line_cap));
42 // This is the default
43 if (l->miter_limit != 4)
44 svg_set_attr_float(svg, "stroke-miterlimit", l->miter_limit);
48 svg_set_attr(svg, "stroke-linejoin", osm_val_decode(l->line_join));
51 osm_obj_warn(l->s.o, "Unknown stroke-linejoin: %s", osm_val_decode(l->line_join));
56 struct fastbuf *fb = svg_fb_open(svg);
57 for (uns i=0; i < GARY_SIZE(l->dash_pattern); i++)
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]);
65 svg_fb_close_as_attr(svg, "stroke-dasharray");
67 svg_set_attr_float(svg, "stroke-dashoffset", l->dash_offset);
71 static void append_node_list(struct svg *svg, clist *list)
73 struct osm_node *first = NULL;
74 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *list)
79 svg_path_move_to(svg, n->x, n->y);
84 svg_path_line_to(svg, n->x, n->y);
89 static void make_path(struct osm_object *o, struct svg *svg)
96 struct osm_way *w = (struct osm_way *) o;
97 append_node_list(svg, &w->nodes);
100 case OSM_TYPE_MULTIPOLYGON:
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);
113 static void sym_line_draw(struct symbol *sym, struct svg *svg)
115 struct sym_line *l = (struct sym_line *) sym;
116 make_path(sym->o, svg);
117 sym_line_attrs(l, svg);
121 static void sym_line_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
123 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
126 struct sym_line *main_sl = NULL;
128 // Line and its casing are two similar objects
129 for (uns casing=0; casing<2; casing++)
132 if (!style_get_number(si, PROP_WIDTH + casing, &w))
135 struct sym_line *sl = sym_line_new(o);
142 osm_obj_warn(o, "Casing around no line");
145 sl->width += main_sl->width;
150 sl->color = 0x808080;
151 style_get_color(si, PROP_FILL_COLOR, &sl->color);
152 style_get_color(si, PROP_COLOR + casing, &sl->color);
155 style_get_number(si, PROP_OPACITY + casing, &sl->opacity);
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);
162 struct style_prop *dashes = style_get(si, PROP_DASHES + casing);
163 if (!dashes || dashes->type == PROP_TYPE_IDENT && dashes->val.id == VALUE_NONE)
165 else if (dashes->type == PROP_TYPE_LIST)
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)
170 if (e->val.type == PROP_TYPE_NUMBER)
171 *GARY_PUSH(sl->dash_pattern) = e->val.val.number;
174 osm_obj_warn(o, "Invalid dash pattern");
178 style_get_number(si, PROP_DASHES_OFFSET + casing, &sl->dash_offset);
181 osm_obj_warn(o, "Invalid dash pattern");
183 sym_plan(&sl->s, sym_zindex(o, si, casing ? 2 : 3));
187 struct symbolizer symbolizer_line = {
189 .draw = sym_line_draw,
193 struct sym_line *sym_line_new(struct osm_object *o)
195 return sym_new(SYMBOLIZER_LINE, o, sizeof(struct sym_line));
198 /*** Images along line ***/
200 static void lineimg_node_list(struct sym_lineimg *sli, clist *nodes, struct svg *svg)
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)
209 double dx = n->x - prev->x;
210 double dy = n->y - prev->y;
211 double len = hypot(dx, dy);
213 while (len - dist > 1e-10)
215 // FIXME: Rotate images along the path?
216 double next = MIN(len - dist, step - phase);
219 if (step - phase < 1e-10)
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);
234 static void sym_lineimg_draw(struct symbol *sym, struct svg *svg)
236 struct sym_lineimg *li = (struct sym_lineimg *) sym;
237 struct osm_object *o = li->s.o;
243 struct osm_way *w = (struct osm_way *) o;
244 lineimg_node_list(li, &w->nodes, svg);
247 case OSM_TYPE_MULTIPOLYGON:
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);
259 static void sym_lineimg_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
261 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
264 osm_val_t icon_name = style_get_string(si, PROP_REPEAT_IMAGE);
268 struct sym_lineimg *sli = sym_lineimg_new(o);
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);
276 // FIXME: Better handling of defaults in style_get_number()
278 // FIXME: align and offset are not supported yet
279 sli->align = style_get_ident(si, PROP_REPEAT_IMAGE_ALIGN);
281 style_get_number(si, PROP_REPEAT_IMAGE_OFFSET, &sli->offset);
284 style_get_number(si, PROP_REPEAT_IMAGE_SPACING, &sli->spacing);
286 style_get_number(si, PROP_REPEAT_IMAGE_PHASE, &sli->phase);
288 sym_plan(&sli->s, sym_zindex(o, si, 3));
291 struct symbolizer symbolizer_lineimg = {
293 .draw = sym_lineimg_draw,
294 .gen = sym_lineimg_gen,
297 struct sym_lineimg *sym_lineimg_new(struct osm_object *o)
299 return sym_new(SYMBOLIZER_LINEIMG, o, sizeof(struct sym_lineimg));
304 static void sym_area_draw(struct symbol *sym, struct svg *svg)
306 struct sym_area *a = (struct sym_area *) sym;
308 for (int i=0; i<2; i++)
312 if (a->fill_color == COLOR_NONE)
314 make_path(sym->o, svg);
315 svg_set_attr_color(svg, "fill", a->fill_color);
319 if (!a->fill_pattern)
321 make_path(sym->o, svg);
322 svg_set_attr(svg, "fill", a->fill_pattern->paint_server);
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");
332 static void sym_area_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
334 if (!(o->type == OSM_TYPE_WAY && osm_way_cyclic_p((struct osm_way *) o) ||
335 o->type == OSM_TYPE_MULTIPOLYGON))
339 if (!style_get_color(si, PROP_FILL_COLOR, &color))
342 osm_val_t pattern = style_get_string(si, PROP_FILL_PATTERN);
343 struct svg_pattern *patt = NULL;
346 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(pattern));
347 struct svg_pattern_request spr = {
349 .width = icon->width,
350 .height = icon->height
352 style_scale(si, &spr.width, &spr.height, PROP_FILL_PATTERN_WIDTH, PROP_FILL_PATTERN_HEIGHT);
353 patt = svg_icon_to_pattern(svg, &spr);
356 if (color == COLOR_NONE && !patt)
359 struct sym_area *sa = sym_area_new(o);
360 sa->fill_color = color;
361 sa->fill_pattern = patt;
363 sa->fill_opacity = 1;
364 style_get_number(si, PROP_FILL_OPACITY, &sa->fill_opacity);
366 sym_plan(&sa->s, sym_zindex(o, si, 1));
369 struct symbolizer symbolizer_area = {
371 .draw = sym_area_draw,
375 struct sym_area *sym_area_new(struct osm_object *o)
377 return sym_new(SYMBOLIZER_AREA, o, sizeof(struct sym_area));