]> mj.ucw.cz Git - leo.git/blob - svg.h
Do not crash on broken multipolygons
[leo.git] / svg.h
1 /*
2  *      Hic Est Leo -- SVG Output
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _LEO_SVG_H
8 #define _LEO_SVG_H
9
10 // FIXME: Passing SVG pointers everywhere is ugly, using global context less so.
11
12 struct svg {
13   struct fastbuf *fb;
14   struct svg_element **stack;
15   struct mempool *pool;
16   struct fbpool *fb_pool;
17   struct fastbuf *str_fb;       // fb_pool cast to the right type
18   double scale;                 // 1mm is converted to this many px
19   uns icon_counter;
20   struct icon_table *icon_hash;
21   uns pattern_counter;
22 };
23
24 struct svg_element {
25   byte type;                    // XML_NODE_xxx
26   byte flags;                   // SVG_EF_xxx
27   byte rfu[2];
28   int indent;                   // -1 = inline
29   const char *name;
30   clist attrs;
31 };
32
33 struct svg_attr {
34   cnode n;
35   const char *key, *val;
36 };
37
38 enum svg_element_flags {
39   SVG_EF_FLAT = 1,
40   SVG_EF_START_TAG = 2,         // Internal: start tag has been already printed
41   SVG_EF_HAS_CHILDREN = 4,
42 };
43
44 struct svg *svg_open(char *filename);
45 void svg_close(struct svg *svg);
46
47 struct svg_element *svg_push_element(struct svg *svg, const char *name);
48 struct svg_element *svg_push_chars(struct svg *svg);
49 struct svg_element *svg_push_comment(struct svg *svg);
50 void svg_pop(struct svg *svg);
51
52 void svg_set_attr(struct svg *svg, const char *key, const char *val);
53 void svg_set_attr_ref(struct svg *svg, const char *key, const char *val);
54 void svg_set_attr_int(struct svg *svg, const char *key, int val);
55 void svg_set_attr_float(struct svg *svg, const char *key, double val);
56 void svg_set_attr_dimen(struct svg *svg, const char *key, double val);
57 void svg_set_attr_color(struct svg *svg, const char *key, color_t color);
58 void svg_set_attr_format(struct svg *svg, const char *key, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
59
60 struct fastbuf *svg_fb_open(struct svg *svg);
61 const char *svg_fb_close(struct svg *svg);
62 void svg_fb_close_as_attr(struct svg *svg, const char *key);
63
64 struct svg_element *svg_push_path(struct svg *svg);
65 void svg_path_end(struct svg *svg);
66 void svg_path_move_to(struct svg *svg, double x, double y);
67 void svg_path_move_to_rel(struct svg *svg, double x, double y);
68 void svg_path_line_to(struct svg *svg, double x, double y);
69 void svg_path_line_to_rel(struct svg *svg, double x, double y);
70 void svg_path_close(struct svg *svg);
71
72 /* svg-icon.c */
73
74 struct svg_icon {
75   uns id;
76   double width, height;
77   struct xml_context *ctx;
78   struct xml_node *root;
79   clist patterns;
80   char name[1];
81 };
82
83 struct svg_icon *svg_icon_load(struct svg *svg, const char *name);
84 void svg_icon_dump_library(struct svg *svg);
85
86 void svg_icon_init(struct svg *svg);            // Called from svg_open()
87 void svg_icon_cleanup(struct svg *svg);         // Called from svg_close()
88
89 struct svg_icon_request {
90   struct svg_icon *icon;
91   double x, y;
92   double width, height;
93 };
94
95 void svg_icon_put(struct svg *svg, struct svg_icon_request *sir);
96
97 struct svg_pattern_request {
98   struct svg_icon *icon;
99   double width, height;
100 };
101
102 struct svg_pattern {
103   cnode n;
104   uns id;
105   struct svg_icon *icon;
106   double width, height;
107   char *paint_server;
108 };
109
110 struct svg_pattern *svg_icon_to_pattern(struct svg *svg, struct svg_pattern_request *spr);
111
112 #endif