trase
Backend.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 BACKEND_H_
37 #define BACKEND_H_
38 
39 #include <cmath>
40 #include <sstream>
41 #include <vector>
42 
43 #include "util/Vector.hpp"
44 
45 namespace trase {
46 
47 class Drawable;
48 
50 class Backend {
51 public:
52  // Declare overloads for each kind of a Drawable to dispatch
53  virtual void accept(Drawable &drawable, float time) = 0;
54 };
55 
56 #define TRASE_BACKEND_VISITABLE() \
57  void accept(Drawable &drawable, float time) override { \
58  drawable.dispatch(*this, time); \
59  }
60 
62 class AnimatedBackend : public Backend {
63 public:
64  // Can still accept a single time draw
65  using Backend::accept;
66 
67  // Declare overloads for each kind of a Drawable to dispatch
68  virtual void accept(Drawable &drawable) = 0;
69 };
70 
71 #define TRASE_ANIMATED_BACKEND_VISITABLE() \
72  void accept(Drawable &drawable) override { drawable.dispatch(*this); }
73 
74 // pi constant
75 const float pi =
76  3.141592653589793238462643383279502884197169399375105820974944592307816406286f;
77 
85  float a{1}, b{0}, c{0}, d{1}, e{0}, f{0};
86 
87  bool is_identity() {
88  return a == 1.0f && b == 0.0f && c == 0.0f && d == 1.0f && e == 0.0f &&
89  f == 0.0f;
90  }
91  void clear() {
92  a = d = 1.0;
93  c = e = b = f = 0.0;
94  }
95  void translate(const vfloat2_t &t) {
96  /*
100 
101  // pre-mult
102  e += t[0];
103  f += t[1];
104  */
105  // post-mult
109 
110  e += a * t[0] + c * t[1];
111  f += b * t[0] + d * t[1];
112  }
113  void rotate(float angle) {
114  const float cos_t = std::cos(angle);
115  const float sin_t = std::sin(angle);
116 
117  /*
118  // pre-mult
122 
123  const float a0 = a * cos_t - b * sin_t;
124  b = a * sin_t + b * cos_t;
125  const float c0 = c * cos_t - d * sin_t;
126  d = c * sin_t + d * cos_t;
127  const float e0 = e * cos_t - f * sin_t;
128  f = e * sin_t + f * cos_t;
129  a = a0;
130  c = c0;
131  e = e0;
132  */
133  // post-mult
137 
138  const float a0 = a * cos_t + c * sin_t;
139  c = -a * sin_t + c * cos_t;
140  const float b0 = b * cos_t + d * sin_t;
141  d = -b * sin_t + d * cos_t;
142  a = a0;
143  b = b0;
144  }
145 
146  std::string to_string() {
147  std::stringstream stream;
148  stream << "transform=\"matrix(" << a << ' ' << b << ' ' << c << ' ' << d
149  << ' ' << e << ' ' << f << ")\"";
150  return stream.str();
151  }
152 };
153 
154 enum Align : unsigned int {
155  // Horizontal align
156  ALIGN_LEFT = 1u << 0u, // Default, align text horizontally to left.
157  ALIGN_CENTER = 1u << 1u, // Align text horizontally to center.
158  ALIGN_RIGHT = 1u << 2u, // Align text horizontally to right.
159  // Vertical align
160  ALIGN_TOP = 1u << 3u, // Align text vertically to top.
161  ALIGN_MIDDLE = 1u << 4u, // Align text vertically to middle.
162  ALIGN_BOTTOM = 1u << 5u, // Align text vertically to bottom.
163  ALIGN_BASELINE = 1u << 6u, // Default, align text vertically to baseline.
164 };
165 
166 enum ArcDirection {
167  CLOCKWISE = 1u << 0u,
168  COUNTER_CLOCKWISE = 1u << 1u,
169 };
170 
171 class FontManager {
172 public:
173  std::vector<std::string> m_list_of_available_fonts;
174  std::vector<std::string> m_font_dirs;
175  FontManager();
176 
181  std::string find_font(const std::string &name1, const std::string &name2);
182 
183  void add_system_fonts();
184  void add_font_dir(const std::string &path);
185  void clear_font_dirs();
186 
187 private:
188  void list_fonts(const std::string &path);
189 };
190 
191 } // namespace trase
192 
193 #include "frontend/Drawable.hpp"
194 
195 #endif // BACKEND_H_
a base class for all the backends that support drawing a single frame
Definition: Backend.hpp:50
a transform matrix in the form
Definition: Backend.hpp:84
void rotate(float angle)
Definition: Backend.hpp:113
void translate(const vfloat2_t &t)
Definition: Backend.hpp:95
Base class for drawable objects in a figure.
Definition: Drawable.hpp:99
An N-dimensional vector class.
Definition: Vector.hpp:59
a base class for all the backends that support animation over time
Definition: Backend.hpp:62
Definition: Backend.hpp:171
Definition: Backend.cpp:39