Debian Codegraph @

layla/ligand_editor_canvas/render.hpp

raw
1 /* layla/ligand_editor_canvas/render.hpp
2 *
3 * Copyright 2023 by Global Phasing Ltd.
4 * Author: Jakub Smulski
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
20 */
21 #ifndef COOT_LIGAND_EDITOR_CANVAS_RENDER_HPP
22 #define COOT_LIGAND_EDITOR_CANVAS_RENDER_HPP
23
24 #ifndef __EMSCRIPTEN__
25 #include <gtk/gtk.h>
26 #include <pango/pango-layout.h>
27 #else // Lhasa-specific includes
28 #include <graphene.h>
29 #include <variant>
30 #include <optional>
31 #include <vector>
32 #include <emscripten/val.h>
33 #endif
34 #include <map>
35 #include <memory>
36 #include <tuple>
37 #include "model.hpp"
38
39 namespace coot::ligand_editor_canvas::impl {
40
41
42 struct Renderer {
43 struct Color {
44 double r, g, b, a;
45 };
46
47 enum class TextPositioning: unsigned char {
48 Normal,
49 Sub,
50 Super
51 };
52
53 struct TextSize {
54 int width;
55 int height;
56 };
57
58 struct TextStyle {
59 TextPositioning positioning;
60 /// Has to be compatible with both pango markup and HTML/CSS.
61 ///
62 /// Empty if unspecified
63 std::string weight;
64 /// Has to be compatible with both pango markup and HTML/CSS.
65 ///
66 /// Empty if unspecified
67 std::string size;
68 Color color;
69
70 bool specifies_color;
71
72 TextStyle();
73 };
74
75 class TextSpan {
76
77 std::variant<std::string, std::vector<TextSpan>> content;
78
79 public:
80 TextStyle style;
81 // Emscripten doesn't currently support std::optional
82 bool specifies_style;
83
84 bool has_subspans() const;
85 std::string& as_caption();
86 const std::string& as_caption() const;
87 std::vector<TextSpan>& as_subspans();
88 const std::vector<TextSpan>& as_subspans() const;
89 TextSpan();
90 TextSpan(const std::string&);
91 TextSpan(const std::vector<TextSpan>&);
92
93 #ifdef __EMSCRIPTEN__
94 inline friend std::size_t hash_value(const TextSpan&);
95 #endif
96 };
97
98 struct Text {
99 TextStyle style;
100 std::vector<TextSpan> spans;
101 graphene_point_t origin;
102 };
103
104 #ifndef __EMSCRIPTEN__
105 cairo_t* cr;
106 PangoLayout* pango_layout;
107
108 private:
109 std::string text_span_to_pango_markup(const TextSpan&, const std::optional<TextStyle>& parent_style = std::nullopt) const;
110 public:
111 /// Takes ownership of the pointers
112 Renderer(cairo_t*,PangoLayout*);
113 Renderer(const Renderer&) = delete;
114
115 #else // __EMSCRIPTEN__ defined
116 // Lhasa-specific includes/definitions
117 struct Path;
118 struct PathElement;
119 struct DrawingCommand;
120 class TextMeasurementCache;
121 struct BrushStyle {
122 Color color;
123 double line_width;
124 };
125 private:
126
127 /// Not owned pointer
128 TextMeasurementCache* tm_cache;
129 BrushStyle style;
130 graphene_point_t position;
131 std::vector<DrawingCommand> drawing_commands;
132 emscripten::val text_measurement_function;
133
134 /// If the last command is a non-closed path,
135 /// returns a mutable reference to it.
136 /// If not, it creates a new path.
137 Path& top_path();
138
139 /// If the last command is a path (may be closed),
140 /// returns a pointer to it.
141 /// Otherwise nullptr
142 Path* top_path_if_exists();
143
144 /// Initialize new Path structure
145 Path create_new_path() const;
146 /// Does not add the finishing line
147 void close_path_inner();
148
149 public:
150 struct Line {
151 graphene_point_t start, end;
152 // BrushStyle style;
153 };
154
155 struct Arc {
156 graphene_point_t origin;
157 double radius, angle_one, angle_two;
158 // bool has_fill;
159 // Color fill_color;
160 // bool has_stroke;
161 // BrushStyle stroke_style;
162 };
163
164 struct PathElement {
165 std::variant<Line, Arc> content;
166
167 // todo: functions
168 bool is_line() const;
169 bool is_arc() const;
170
171 const Line& as_line() const;
172 const Arc& as_arc() const;
173
174 };
175 struct Path {
176 graphene_point_t initial_point;
177 std::vector<PathElement> elements;
178 // WIP
179 bool closed;
180
181 Color fill_color;
182 bool has_fill;
183 // this needs work. Do we need a boolean here?
184 bool has_stroke;
185 BrushStyle stroke_style;
186
187 const std::vector<PathElement>& get_elements() const;
188 };
189
190 struct DrawingCommand {
191 std::variant<Path, Text> content;
192
193 bool is_path() const;
194 bool is_text() const;
195
196 const Path& as_path() const;
197 Path& as_path_mut();
198 const Text& as_text() const;
199 };
200
201 class TextMeasurementCache {
202 public:
203 typedef std::size_t hash_t;
204 private:
205 std::map<hash_t, TextSize> cache;
206
207 public:
208 std::optional<TextSize> lookup_span(const TextSpan& text) const;
209 std::optional<TextSize> lookup_span(hash_t span_hash) const;
210 void add(hash_t span_hash, TextSize value);
211 void add(const TextSpan& text, TextSize value);
212 std::size_t size() const;
213 };
214
215 Renderer(emscripten::val text_measurement_function, TextMeasurementCache& cache);
216 Renderer(emscripten::val text_measurement_function);
217
218 std::vector<DrawingCommand> get_commands() const;
219 #endif
220
221 // PRIMITIVES
222
223 void move_to(double x, double y);
224 void line_to(double x, double y);
225 void arc(double x, double y, double radius, double angle_one, double angle_two);
226 void fill();
227 void stroke();
228 void stroke_preserve();
229
230 // PATH
231
232 void new_path();
233 void close_path();
234 void new_sub_path();
235
236 // STYLE
237
238 void set_source_rgb(double r, double g, double b);
239 void set_source_rgba(double r, double g, double b, double a);
240 void set_line_width(double width);
241
242 // TEXT
243 TextSize measure_text(const Renderer::TextSpan&);
244 void show_text(const Renderer::TextSpan&);
245
246 ~Renderer();
247 };
248
249 /// Encapsulates routines and state to render CanvasMolecule
250 class MoleculeRenderContext {
251
252 const CanvasMolecule& canvas_molecule;
253 Renderer& ren;
254 DisplayMode display_mode;
255 float scale_factor;
256 float x_offset;
257 float y_offset;
258 // Used to truncate bonds not to cover atoms
259 std::map<unsigned int,graphene_rect_t> atom_idx_to_canvas_rect;
260
261
262 void process_atom_highlight(const CanvasMolecule::Atom& atom);
263 /// Returns the appendix span + info if the appendix is reversed
264 std::tuple<Renderer::TextSpan, bool> process_appendix(const std::string& symbol, const std::optional<CanvasMolecule::Atom::Appendix>& appendix, const Renderer::TextStyle& inherited_style);
265 /// Returns a pair of atom index and bonding rect
266 std::pair<unsigned int,graphene_rect_t> render_atom(const CanvasMolecule::Atom& atom, DisplayMode render_mode = DisplayMode::Standard);
267 // Returns on-screen bond coordinates
268 // cropped not to overlap with atoms' symbols and appendices.
269 // Accepts on-screen coordinates of bond atoms.
270 std::pair<graphene_point_t,graphene_point_t> cropped_bond_coords(const graphene_point_t& first_atom, unsigned int first_atom_idx, const graphene_point_t& second_atom, unsigned int second_atom_idx);
271
272 void draw_central_bond_line(const CanvasMolecule::Bond& bond);
273 void draw_straight_wedge(const CanvasMolecule::Bond& bond, bool reversed);
274 void draw_straight_dashed_bond(const CanvasMolecule::Bond& bond, bool reversed);
275 void draw_wavy_bond(const CanvasMolecule::Bond& bond);
276 void draw_side_bond_line(const CanvasMolecule::Bond& bond, bool addOrSub, std::optional<float> first_shortening_proportion, std::optional<float> second_shortening_proportion);
277 void draw_centered_double_bond(const CanvasMolecule::Bond& bond);
278
279 static const float CENTERED_DOUBLE_BOND_LINE_SEPARATION;
280 static const float GEOMETRY_BOND_SPREAD_ANGLE;
281 static const float WAVY_BOND_ARC_LENGTH;
282 static const float GEOMETRY_BOND_DASH_SEPARATION;
283
284 public:
285 MoleculeRenderContext(const CanvasMolecule& cm, Renderer& ren, DisplayMode mode);
286 ~MoleculeRenderContext();
287
288 void draw_atoms();
289 void draw_bonds();
290 };
291
292 } //coot::ligand_editor_canvas::impl
293
294 #ifdef __EMSCRIPTEN__
295 #include <boost/functional/hash.hpp>
296 #include <tuple>
297
298 inline std::size_t coot::ligand_editor_canvas::impl::hash_value(const coot::ligand_editor_canvas::impl::Renderer::TextSpan& span);
299
300 namespace std {
301 template <> struct std::hash<coot::ligand_editor_canvas::impl::Renderer::Color> {
302 std::size_t operator()(const coot::ligand_editor_canvas::impl::Renderer::Color& color) const noexcept {
303 return boost::hash_value(std::tie(color.a, color.r, color.g, color.b));
304 }
305 };
306 template <> struct std::hash<coot::ligand_editor_canvas::impl::Renderer::TextStyle> {
307 std::size_t operator()(const coot::ligand_editor_canvas::impl::Renderer::TextStyle& style) const noexcept {
308 auto ret = std::hash<coot::ligand_editor_canvas::impl::Renderer::Color>{}(style.color);
309 boost::hash_combine(ret, std::tie(
310 style.size,
311 //style.color hashed above
312 style.specifies_color,
313 style.positioning,
314 style.weight
315 ));
316 return ret;
317 }
318 };
319
320 template<> struct std::hash<coot::ligand_editor_canvas::impl::Renderer::TextSpan> {
321 std::size_t operator()(const coot::ligand_editor_canvas::impl::Renderer::TextSpan& span) const noexcept {
322 return coot::ligand_editor_canvas::impl::hash_value(span);
323 }
324 };
325 }
326
327 inline std::size_t coot::ligand_editor_canvas::impl::hash_value(const coot::ligand_editor_canvas::impl::Renderer::TextSpan& span) {
328 auto ret = std::hash<coot::ligand_editor_canvas::impl::Renderer::TextStyle>{}(span.style);
329 boost::hash_combine(ret, span.specifies_style);
330 boost::hash_combine(ret, span.content);
331 return ret;
332 }
333 #endif // __EMSCRIPTEN__
334
335 #endif // COOT_LIGAND_EDITOR_CANVAS_RENDER_HPP