]> mj.ucw.cz Git - leo.git/blob - leo.c
d7a39bd0f18aea3456ead193fad04b618dd93b6c
[leo.git] / leo.c
1 /*
2  *      Hic Est Leo -- Main Program
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "leo.h"
8
9 #include <ucw/conf.h>
10 #include <ucw/opt.h>
11
12 #include <stdio.h>
13
14 #include "osm.h"
15 #include "svg.h"
16 #include "style.h"
17 #include "css.h"
18 #include "sym.h"
19 #include "map.h"
20 #include "graph.h"
21 #include "simplify.h"
22
23 uns debug_dump_source, debug_dump_after_proj, debug_dump_after_scaling;
24 uns debug_dump_multipolygons, debug_dump_css, debug_dump_styling, debug_dump_symbols;
25
26 static struct cf_section debug_cf = {
27   CF_ITEMS {
28     CF_UNS("DumpSource", &debug_dump_source),
29     CF_UNS("DumpAfterProj", &debug_dump_after_proj),
30     CF_UNS("DumpAfterScaling", &debug_dump_after_scaling),
31     CF_UNS("DumpMultipolygons", &debug_dump_multipolygons),
32     CF_UNS("DumpCSS", &debug_dump_css),
33     CF_UNS("DumpStyling", &debug_dump_styling),
34     CF_UNS("DumpSymbols", &debug_dump_symbols),
35     CF_END
36   }
37 };
38
39 static const struct opt_section options = {
40   OPT_ITEMS {
41     OPT_HELP("Hic Est Leo -- Experimental Map Renderer"),
42     OPT_HELP(""),
43     OPT_HELP("Options:"),
44     OPT_HELP_OPTION,
45     OPT_CONF_OPTIONS,
46     OPT_END
47   }
48 };
49
50 #if 0
51 // FIXME: Make generic
52 static void draw_scale(struct svg *svg)
53 {
54   double dist = 1000;
55   double width = dist * map_scale;
56   double x = page_width - 10 - width;
57   double y = 50;
58
59   svg_push_element(svg, "g");
60   svg_set_attr(svg, "id", "scale");
61   svg_set_attr_format(svg, "transform", "translate(%s,%s)", svg_format_dimen(svg, x), svg_format_dimen(svg, y));
62
63   for (int outline=1; outline>=0; outline--)
64     {
65       svg_push_element(svg, "g");
66       svg_set_attr(svg, "stroke-linecap", "square");
67       if (outline)
68         {
69           svg_set_attr_dimen(svg, "stroke-width", 1.5);
70           svg_set_attr_color(svg, "stroke", 0xffffff);
71         }
72       else
73         {
74           svg_set_attr_dimen(svg, "stroke-width", 0.5);
75           svg_set_attr_color(svg, "stroke", 0);
76         }
77
78       svg_push_element(svg, "line");
79       svg_set_attr_dimen(svg, "x1", 0);
80       svg_set_attr_dimen(svg, "y1", 0);
81       svg_set_attr_dimen(svg, "x2", width);
82       svg_set_attr_dimen(svg, "y2", 0);
83       svg_pop(svg);
84
85       for (int i=0; i<=10; i++)
86         {
87           double tick;
88           switch (i)
89             {
90             case 0:
91             case 10:
92               tick = 3;
93               break;
94             case 5:
95               tick = 2;
96               break;
97             default:
98               tick = 1;
99             }
100           svg_push_element(svg, "line");
101           svg_set_attr_dimen(svg, "x1", width * i/10);
102           svg_set_attr_dimen(svg, "y1", 0);
103           svg_set_attr_dimen(svg, "x2", width * i/10);
104           svg_set_attr_dimen(svg, "y1", -tick);
105           svg_pop(svg);
106         }
107
108       svg_pop(svg);
109     }
110
111   scale_text(svg, 0, 5, osm_val_encode("0"));
112   scale_text(svg, width, 5, osm_val_encode("1 km"));
113   svg_pop(svg);
114 }
115 #endif
116
117 int main(int argc UNUSED, char **argv)
118 {
119   cf_def_file = "map.cf";
120   cf_declare_section("Debug", &debug_cf, 0);
121   opt_parse(&options, argv+1);
122
123   osm_init();
124   styles_init();
125   map_load_styles();
126   map_load_sources();
127   graph_build();
128   map_set_scale();
129   // map_generalize();
130
131   struct svg *svg = svg_open(map_svg_output);
132   if (!map_rotate)
133     {
134       svg_set_attr_dimen(svg, "width", page_width);
135       svg_set_attr_dimen(svg, "height", page_height);
136     }
137   else
138     {
139       svg_set_attr_dimen(svg, "width", page_height);
140       svg_set_attr_dimen(svg, "height", page_width);
141     }
142
143   sym_init();
144
145   map_apply_styles(svg);
146   simplify();
147
148   if (map_clip)
149     {
150       svg_push_element(svg, "defs");
151       svg_push_element(svg, "clipPath");
152       svg_set_attr(svg, "id", "boundary");
153       svg_push_path(svg);
154       svg_path_move_to(svg, page_offset_x, page_offset_y);
155       svg_path_line_to_rel(svg, page_map_width, 0);
156       svg_path_line_to_rel(svg, 0, page_map_height);
157       svg_path_line_to_rel(svg, -page_map_width, 0);
158       svg_path_close(svg);
159       svg_path_end(svg);
160       svg_pop(svg);
161       svg_pop(svg);
162       svg_pop(svg);
163
164       svg_push_element(svg, "g");
165       svg_set_attr_format(svg, "clip-path", "url(#boundary)");
166       if (map_rotate)
167         svg_set_attr_format(svg, "transform", "translate(%.6g,0) rotate(90)", page_height * svg->scale);
168     }
169
170   // FIXME: Replace by generic logo drawing facility
171 #if 0
172   struct svg_icon *logo = svg_icon_load(svg, "../logo/kocka-s-okrajem.svg");
173 #endif
174
175   sym_draw_all(svg);
176
177   // Draw logo
178 #if 0
179   double logo_width = 36.12;
180   double logo_height = 36.12 / logo->width * logo->height;
181   struct svg_icon_request sir = {
182     .icon = logo,
183     .x = page_width - 12 - logo_width / 2,
184     .y = 10 + logo_height / 2,
185     .width = logo_width,
186     .height = logo_height,
187   };
188   svg_icon_put(svg, &sir);
189 #endif
190
191 #if 0
192   draw_scale(svg);
193 #endif
194
195   if (map_clip)
196     svg_pop(svg);
197
198   if (map_draw_border)
199     {
200       svg_push_element(svg, "rect");
201       svg_set_attr_dimen(svg, "x", page_offset_x);
202       svg_set_attr_dimen(svg, "y", page_offset_y);
203       svg_set_attr_dimen(svg, "width", page_map_width);
204       svg_set_attr_dimen(svg, "height", page_map_height);
205       svg_set_attr(svg, "fill", "none");
206       svg_set_attr(svg, "stroke", "blue");
207       svg_set_attr_dimen(svg, "stroke-width", 0.2);
208       svg_pop(svg);
209     }
210
211   svg_close(svg);
212
213   msg(L_INFO, "Finished");
214   return 0;
215 }