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));
194 struct symbolizer symbolizer_line = {
196 .draw = sym_line_draw,
200 struct sym_line *sym_line_new(struct osm_object *o)
202 return sym_new(SYMBOLIZER_LINE, o, sizeof(struct sym_line));
205 /*** Images along line ***/
207 static void lineimg_node_list(struct sym_lineimg *sli, clist *nodes, struct svg *svg)
209 double phase = sli->phase;
210 double step = sli->sir.width + sli->spacing;
211 struct osm_node *prev = NULL;
212 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *nodes)
216 double dx = n->x - prev->x;
217 double dy = n->y - prev->y;
218 double len = hypot(dx, dy);
220 while (len - dist > 1e-10)
222 // FIXME: Rotate images along the path?
223 double next = MIN(len - dist, step - phase);
226 if (step - phase < 1e-10)
228 struct svg_icon_request sir = sli->sir;
229 sir.x = prev->x + dx * dist / len;
230 sir.y = prev->y + dy * dist / len;
231 svg_icon_put(svg, &sir);
241 static void sym_lineimg_draw(struct symbol *sym, struct svg *svg)
243 struct sym_lineimg *li = (struct sym_lineimg *) sym;
244 struct osm_object *o = li->s.o;
250 struct osm_way *w = (struct osm_way *) o;
251 lineimg_node_list(li, &w->nodes, svg);
254 case OSM_TYPE_MULTIPOLYGON:
256 struct osm_multipolygon *m = (struct osm_multipolygon *) o;
257 CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
258 lineimg_node_list(li, &b->nodes, svg);
266 static void sym_lineimg_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
268 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
271 osm_val_t icon_name = style_get_string(si, PROP_REPEAT_IMAGE);
275 struct sym_lineimg *sli = sym_lineimg_new(o);
277 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(icon_name));
278 sli->sir.icon = icon;
279 sli->sir.width = icon->width;
280 sli->sir.height = icon->height;
281 style_scale(si, &sli->sir.width, &sli->sir.height, PROP_REPEAT_IMAGE_WIDTH, PROP_REPEAT_IMAGE_HEIGHT);
283 // FIXME: Better handling of defaults in style_get_number()
285 // FIXME: align and offset are not supported yet
286 sli->align = style_get_ident(si, PROP_REPEAT_IMAGE_ALIGN);
288 style_get_number(si, PROP_REPEAT_IMAGE_OFFSET, &sli->offset);
291 style_get_number(si, PROP_REPEAT_IMAGE_SPACING, &sli->spacing);
293 style_get_number(si, PROP_REPEAT_IMAGE_PHASE, &sli->phase);
295 sym_plan(&sli->s, sym_zindex(o, si, 3));
298 struct symbolizer symbolizer_lineimg = {
300 .draw = sym_lineimg_draw,
301 .gen = sym_lineimg_gen,
304 struct sym_lineimg *sym_lineimg_new(struct osm_object *o)
306 return sym_new(SYMBOLIZER_LINEIMG, o, sizeof(struct sym_lineimg));
311 static void sym_area_draw(struct symbol *sym, struct svg *svg)
313 struct sym_area *a = (struct sym_area *) sym;
315 for (int i=0; i<2; i++)
319 if (a->fill_color == COLOR_NONE)
321 make_path(sym->o, svg);
322 svg_set_attr_color(svg, "fill", a->fill_color);
326 if (!a->fill_pattern)
328 make_path(sym->o, svg);
329 svg_set_attr(svg, "fill", a->fill_pattern->paint_server);
331 if (a->fill_opacity != 1)
332 svg_set_attr_float(svg, "opacity", a->fill_opacity);
333 if (sym->o->type == OSM_TYPE_MULTIPOLYGON)
334 svg_set_attr(svg, "fill-rule", "evenodd");
339 static void sym_area_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
341 if (!(o->type == OSM_TYPE_WAY && osm_way_cyclic_p((struct osm_way *) o) ||
342 o->type == OSM_TYPE_MULTIPOLYGON))
346 if (!style_get_color(si, PROP_FILL_COLOR, &color))
349 osm_val_t pattern = style_get_string(si, PROP_FILL_PATTERN);
350 struct svg_pattern *patt = NULL;
353 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(pattern));
354 struct svg_pattern_request spr = {
356 .width = icon->width,
357 .height = icon->height
359 style_scale(si, &spr.width, &spr.height, PROP_FILL_PATTERN_WIDTH, PROP_FILL_PATTERN_HEIGHT);
360 patt = svg_icon_to_pattern(svg, &spr);
363 if (color == COLOR_NONE && !patt)
366 struct sym_area *sa = sym_area_new(o);
367 sa->fill_color = color;
368 sa->fill_pattern = patt;
370 sa->fill_opacity = 1;
371 style_get_number(si, PROP_FILL_OPACITY, &sa->fill_opacity);
373 sym_plan(&sa->s, sym_zindex(o, si, 1));
376 struct symbolizer symbolizer_area = {
378 .draw = sym_area_draw,
382 struct sym_area *sym_area_new(struct osm_object *o)
384 return sym_new(SYMBOLIZER_AREA, o, sizeof(struct sym_area));