2 * Hic Est Leo -- Line and Area Symbolizer
4 * (c) 2014 Martin Mares <mj@ucw.cz>
11 #include <ucw/fastbuf.h>
12 #include <ucw/mempool.h>
17 static void sym_line_attrs(struct sym_line *l, struct svg *svg)
19 svg_set_attr(svg, "fill", "none");
20 svg_set_attr_color(svg, "stroke", l->color);
21 svg_set_attr_dimen(svg, "stroke-width", l->width);
23 svg_set_attr_float(svg, "stroke-opacity", l->opacity);
28 // This is the default (butt)
32 svg_set_attr(svg, "stroke-linecap", osm_val_decode(l->line_cap));
35 osm_obj_warn(l->s.o, "Unknown stroke-linecap: %s", osm_val_decode(l->line_cap));
41 // This is the default
42 if (l->miter_limit != 4)
43 svg_set_attr_float(svg, "stroke-miterlimit", l->miter_limit);
47 svg_set_attr(svg, "stroke-linejoin", osm_val_decode(l->line_join));
50 osm_obj_warn(l->s.o, "Unknown stroke-linejoin: %s", osm_val_decode(l->line_join));
55 struct fastbuf *fb = svg_fb_open(svg);
56 for (uns i=0; i < GARY_SIZE(l->dash_pattern); i++)
60 // FIXME: This is dimension-sensitive
61 // FIXME: Also, inkscape doesn't handle units in dash lengths
62 bprintf(fb, "%.6g", l->dash_pattern[i]);
64 svg_fb_close_as_attr(svg, "stroke-dasharray");
66 svg_set_attr_float(svg, "stroke-dashoffset", l->dash_offset);
70 static void append_node_list(struct svg *svg, clist *list)
72 struct osm_node *first = NULL;
73 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *list)
78 svg_path_move_to(svg, n->x, n->y);
83 svg_path_line_to(svg, n->x, n->y);
88 static void make_path(struct osm_object *o, struct svg *svg)
95 struct osm_way *w = (struct osm_way *) o;
96 append_node_list(svg, &w->nodes);
99 case OSM_TYPE_MULTIPOLYGON:
101 struct osm_multipolygon *m = (struct osm_multipolygon *) o;
102 CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
103 append_node_list(svg, &b->nodes);
112 static void sym_line_draw(struct symbol *sym, struct svg *svg)
114 struct sym_line *l = (struct sym_line *) sym;
115 make_path(sym->o, svg);
116 sym_line_attrs(l, svg);
120 static void sym_line_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
122 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
125 struct sym_line *main_sl = NULL;
127 // Line and its casing are two similar objects
128 for (uns casing=0; casing<2; casing++)
131 if (!style_get_number(si, PROP_WIDTH + casing, &w))
134 struct sym_line *sl = sym_line_new(o);
141 osm_obj_warn(o, "Casing around no line");
144 sl->width += main_sl->width;
149 sl->color = 0x808080;
150 style_get_color(si, PROP_FILL_COLOR, &sl->color);
151 style_get_color(si, PROP_COLOR + casing, &sl->color);
154 style_get_number(si, PROP_OPACITY + casing, &sl->opacity);
156 sl->line_cap = style_get_ident(si, PROP_LINECAP + casing) ? : VALUE_NONE;
157 sl->line_join = style_get_ident(si, PROP_LINEJOIN + casing) ? : VALUE_ROUND;
158 sl->miter_limit = 10;
159 style_get_number(si, PROP_MITERLIMIT + casing, &sl->miter_limit);
161 struct style_prop *dashes = style_get(si, PROP_DASHES + casing);
162 if (!dashes || dashes->type == PROP_TYPE_IDENT && dashes->val.id == VALUE_NONE)
164 else if (dashes->type == PROP_TYPE_LIST)
166 GARY_INIT_ALLOC(sl->dash_pattern, 0, mp_get_allocator(sym_mp));
167 CLIST_FOR_EACH(struct style_val_list_entry *, e, *dashes->val.list)
169 if (e->val.type == PROP_TYPE_NUMBER)
170 *GARY_PUSH(sl->dash_pattern) = e->val.val.number;
173 osm_obj_warn(o, "Invalid dash pattern");
177 style_get_number(si, PROP_DASHES_OFFSET + casing, &sl->dash_offset);
180 osm_obj_warn(o, "Invalid dash pattern");
182 sym_plan(&sl->s, sym_zindex(o, si, casing ? 2 : 3));
186 struct symbolizer symbolizer_line = {
188 .draw = sym_line_draw,
192 struct sym_line *sym_line_new(struct osm_object *o)
194 return sym_new(SYMBOLIZER_LINE, o, sizeof(struct sym_line));
197 /*** Images along line ***/
199 static void lineimg_node_list(struct sym_lineimg *sli, clist *nodes, struct svg *svg)
201 double phase = sli->phase;
202 double step = sli->sir.width + sli->spacing;
203 struct osm_node *prev = NULL;
204 OSM_FOR_EACH_BEGIN(struct osm_node *, n, *nodes)
208 double dx = n->x - prev->x;
209 double dy = n->y - prev->y;
210 double len = hypot(dx, dy);
212 while (len - dist > 1e-10)
214 // FIXME: Rotate images along the path?
215 double next = MIN(len - dist, step - phase);
218 if (step - phase < 1e-10)
220 struct svg_icon_request sir = sli->sir;
221 sir.x = prev->x + dx * dist / len;
222 sir.y = prev->y + dy * dist / len;
223 svg_icon_put(svg, &sir);
233 static void sym_lineimg_draw(struct symbol *sym, struct svg *svg)
235 struct sym_lineimg *li = (struct sym_lineimg *) sym;
236 struct osm_object *o = li->s.o;
242 struct osm_way *w = (struct osm_way *) o;
243 lineimg_node_list(li, &w->nodes, svg);
246 case OSM_TYPE_MULTIPOLYGON:
248 struct osm_multipolygon *m = (struct osm_multipolygon *) o;
249 CLIST_FOR_EACH(struct osm_mpg_boundary *, b, m->boundaries)
250 lineimg_node_list(li, &b->nodes, svg);
258 static void sym_lineimg_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
260 if (o->type != OSM_TYPE_WAY && o->type != OSM_TYPE_MULTIPOLYGON)
263 osm_val_t icon_name = style_get_string(si, PROP_REPEAT_IMAGE);
267 struct sym_lineimg *sli = sym_lineimg_new(o);
269 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(icon_name));
270 sli->sir.icon = icon;
271 sli->sir.width = icon->width;
272 sli->sir.height = icon->height;
273 style_scale(si, &sli->sir.width, &sli->sir.height, PROP_REPEAT_IMAGE_WIDTH, PROP_REPEAT_IMAGE_HEIGHT);
275 // FIXME: Better handling of defaults in style_get_number()
277 // FIXME: align and offset are not supported yet
278 sli->align = style_get_ident(si, PROP_REPEAT_IMAGE_ALIGN);
280 style_get_number(si, PROP_REPEAT_IMAGE_OFFSET, &sli->offset);
283 style_get_number(si, PROP_REPEAT_IMAGE_SPACING, &sli->spacing);
285 style_get_number(si, PROP_REPEAT_IMAGE_PHASE, &sli->phase);
287 sym_plan(&sli->s, sym_zindex(o, si, 3));
290 struct symbolizer symbolizer_lineimg = {
292 .draw = sym_lineimg_draw,
293 .gen = sym_lineimg_gen,
296 struct sym_lineimg *sym_lineimg_new(struct osm_object *o)
298 return sym_new(SYMBOLIZER_LINEIMG, o, sizeof(struct sym_lineimg));
303 static void sym_area_draw(struct symbol *sym, struct svg *svg)
305 struct sym_area *a = (struct sym_area *) sym;
307 for (int i=0; i<2; i++)
311 if (a->fill_color == COLOR_NONE)
313 make_path(sym->o, svg);
314 svg_set_attr_color(svg, "fill", a->fill_color);
318 if (!a->fill_pattern)
320 make_path(sym->o, svg);
321 svg_set_attr(svg, "fill", a->fill_pattern->paint_server);
323 if (a->fill_opacity != 1)
324 svg_set_attr_float(svg, "opacity", a->fill_opacity);
325 if (sym->o->type == OSM_TYPE_MULTIPOLYGON)
326 svg_set_attr(svg, "fill-rule", "evenodd");
331 static void sym_area_gen(struct osm_object *o, struct style_info *si, struct svg *svg UNUSED)
333 if (!(o->type == OSM_TYPE_WAY && osm_way_cyclic_p((struct osm_way *) o) ||
334 o->type == OSM_TYPE_MULTIPOLYGON))
338 if (!style_get_color(si, PROP_FILL_COLOR, &color))
341 osm_val_t pattern = style_get_string(si, PROP_FILL_PATTERN);
342 struct svg_pattern *patt = NULL;
345 struct svg_icon *icon = svg_icon_load(svg, osm_val_decode(pattern));
346 struct svg_pattern_request spr = {
348 .width = icon->width,
349 .height = icon->height
351 style_scale(si, &spr.width, &spr.height, PROP_FILL_PATTERN_WIDTH, PROP_FILL_PATTERN_HEIGHT);
352 patt = svg_icon_to_pattern(svg, &spr);
355 if (color == COLOR_NONE && !patt)
358 struct sym_area *sa = sym_area_new(o);
359 sa->fill_color = color;
360 sa->fill_pattern = patt;
362 sa->fill_opacity = 1;
363 style_get_number(si, PROP_FILL_OPACITY, &sa->fill_opacity);
365 sym_plan(&sa->s, sym_zindex(o, si, 1));
368 struct symbolizer symbolizer_area = {
370 .draw = sym_area_draw,
374 struct sym_area *sym_area_new(struct osm_object *o)
376 return sym_new(SYMBOLIZER_AREA, o, sizeof(struct sym_area));