trase
BackendSVG.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of trase.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14  list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
35 
36 #ifndef BACKENDSVG_H_
37 #define BACKENDSVG_H_
38 
39 #include "backend/Backend.hpp"
40 #include "util/BBox.hpp"
41 #include "util/Colors.hpp"
42 #include "util/Exception.hpp"
43 #include "util/Vector.hpp"
44 
45 #include <iomanip>
46 #include <ostream>
47 #include <sstream>
48 
49 namespace trase {
50 
53  std::stringstream m_ss;
54  int m_precision = 4;
55 
56 public:
57  AttributeFormatter() { m_ss << std::setprecision(m_precision); }
58 
65  template <typename T>
66  std::string operator()(const std::string &name, const T val) {
67  m_ss.str("");
68  m_ss << name << "=\"" << val << "\" ";
69  return m_ss.str();
70  }
71 };
72 
73 class BackendSVG : public AnimatedBackend {
75  std::ostream &m_out;
76 
77  std::string m_linewidth;
78  std::string m_line_color;
79  std::string m_fill_color;
80  std::string m_path;
81  std::string m_font_face;
82  std::string m_font_size;
83  std::string m_font_align;
84  std::string m_web_font;
85  std::string m_onmouseover_stroke;
86  std::string m_onmouseout_stroke;
87  std::string m_onmouseover_fill;
88  std::string m_onmouseout_fill;
89  std::string m_onmouseover_tooltip;
90  std::string m_onmouseout_tooltip;
91  std::vector<std::string> m_animate_values;
92  std::string m_animate_stroke;
93  std::string m_animate_stroke_opacity;
94  std::string m_animate_fill;
95  std::string m_animate_fill_opacity;
96  std::string m_animate_times;
97  float m_time_span;
98  std::string m_font_size_base;
99  std::string m_font_face_base;
100  TransformMatrix m_transform;
101  AttributeFormatter m_att;
102 
106  void circle_begin(const vfloat2_t &centre, float r) noexcept;
107 
109  void circle_end() noexcept;
110 
115  void rect_begin(const bfloat2_t &x, float r) noexcept;
116 
118  void rect_end() noexcept;
119 
122  bool mouseover() const noexcept;
123 
124 public:
127  explicit BackendSVG(std::ostream &out);
128 
129  TRASE_BACKEND_VISITABLE()
130  TRASE_ANIMATED_BACKEND_VISITABLE()
131 
132 
133  void init(const vfloat2_t &pixels, const char *name,
140  float time_span = 0.f) noexcept;
141 
145  void finalise() noexcept;
146 
148  bool is_interactive();
149 
151  bool should_close();
152 
154  vfloat2_t begin_frame();
155 
157  void end_frame();
158 
160  vfloat2_t get_mouse_pos();
161 
163  float get_time();
164 
166  static void set_mouse_down(const vfloat2_t &mouse_pos);
167 
169  static void set_mouse_up();
170 
172  bool mouse_dragging();
173 
175  vfloat2_t mouse_drag_delta();
176 
178  void mouse_drag_reset_delta();
179 
181  void scissor(const bfloat2_t &x);
182 
184  void reset_scissor();
185 
187  void rotate(const float angle);
188 
192  void reset_transform();
193 
195  void translate(const vfloat2_t &v);
196 
200  void begin_path();
201 
205  void move_to(const vfloat2_t &x);
206 
210  void line_to(const vfloat2_t &x);
211 
215  void close_path();
216 
220  void stroke();
221 
225  void fill();
226 
231  void begin_animated_path();
232 
238  void add_animated_path(const float time);
239 
245  void end_animated_path(const float time);
246 
251  void rounded_rect(const bfloat2_t &x, float r) noexcept;
252 
257  void rect(const bfloat2_t &x, float r = 0.f) noexcept;
258 
266  void add_animated_rect(const bfloat2_t &x, float time);
267 
274  void add_animated_stroke(const RGBA &color);
275 
282  void add_animated_fill(const RGBA &color);
283 
286  void end_animated_rect();
287 
292  void circle(const vfloat2_t &centre, float r) noexcept;
293 
301  void add_animated_circle(const vfloat2_t &centre, float radius, float time);
302 
305  void end_animated_circle();
306 
312  void circle_with_text(const vfloat2_t &centre, float radius,
313  const char *string);
314 
321  void arc(const vfloat2_t &centre, const float radius, const float angle0,
322  const float angle1);
323 
325  void stroke_color(const RGBA &color);
326 
329  void stroke_color(const RGBA &color, const RGBA &color_mouseover);
330 
338  void tooltip(const vfloat2_t &x, const char *string);
339 
343  void clear_tooltip();
344 
346  void stroke_width(const float lw);
347 
349  void fill_color(const RGBA &color);
350 
352  void fill_color(const RGBA &color, const RGBA &color_mouseover);
353 
355  void font_size(float size);
356 
358  void font_face(const char *face);
359 
363  void import_web_font(const std::string &url);
364 
366  void font_blur(const float blur);
367 
369  void text_align(const unsigned int align);
370 
377  void text(const vfloat2_t &x, const char *string, const char *end);
378 
379 private:
380  void end_animate(std::string &animate, const std::string &name);
381  void end_animate_stroke();
382  void end_animate_fill();
383  void shape_styling();
384 };
385 
386 } // namespace trase
387 
388 #endif // BACKENDSVG_H_
a transform matrix in the form
Definition: Backend.hpp:84
std::string operator()(const std::string &name, const T val)
Format as &#39;name="val" &#39; (inc quotes and space), and return a string.
Definition: BackendSVG.hpp:66
An N-dimensional vector class.
Definition: Vector.hpp:59
Helper class for BackendSVG to format attributes as &#39;name="val" &#39;.
Definition: BackendSVG.hpp:52
a base class for all the backends that support animation over time
Definition: Backend.hpp:62
Colour class with red, green, blue and alpha in range 0 to 255.
Definition: Colors.hpp:48
Definition: BackendSVG.hpp:73
Definition: Backend.cpp:39