| 1 |
/* layla/ligand_editor_canvas/render.cpp |
| 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 |
|
| 22 |
#include "render.hpp" |
| 23 |
#include "model.hpp" |
| 24 |
#ifdef __EMSCRIPTEN__ |
| 25 |
#include "../../lhasa/glog_replacement.hpp" |
| 26 |
#else |
| 27 |
#include <sstream> |
| 28 |
#endif |
| 29 |
|
| 30 |
using namespace coot::ligand_editor_canvas::impl; |
| 31 |
using Atom = coot::ligand_editor_canvas::CanvasMolecule::Atom; |
| 32 |
using BondGeometry = coot::ligand_editor_canvas::CanvasMolecule::BondGeometry; |
| 33 |
using BondType = coot::ligand_editor_canvas::CanvasMolecule::BondType; |
| 34 |
using DoubleBondDrawingDirection = coot::ligand_editor_canvas::CanvasMolecule::DoubleBondDrawingDirection; |
| 35 |
|
| 36 |
const float MoleculeRenderContext::CENTERED_DOUBLE_BOND_LINE_SEPARATION = 0.2f; |
| 37 |
// 10 degrees |
| 38 |
const float MoleculeRenderContext::GEOMETRY_BOND_SPREAD_ANGLE = M_PI/18.f; |
| 39 |
const float MoleculeRenderContext::WAVY_BOND_ARC_LENGTH = 0.25f; |
| 40 |
const float MoleculeRenderContext::GEOMETRY_BOND_DASH_SEPARATION = 0.2f; |
| 41 |
|
| 42 |
#ifndef __EMSCRIPTEN__ |
| 43 |
Renderer::Renderer(cairo_t* cr, PangoLayout* pango_layout) { |
| 44 |
this->cr = cr; |
| 45 |
this->pango_layout = pango_layout; |
| 46 |
} |
| 47 |
#else // __EMSCRIPTEN__ defined |
| 48 |
// Lhasa-specific includes/definitions |
| 49 |
Renderer::Renderer(emscripten::val text_measurement_function, Renderer::TextMeasurementCache& cache) |
| 50 |
:Renderer(text_measurement_function) { |
| 51 |
this->tm_cache = &cache; |
| 52 |
} |
| 53 |
|
| 54 |
Renderer::Renderer(emscripten::val text_measurement_function) { |
| 55 |
this->text_measurement_function = text_measurement_function; |
| 56 |
this->tm_cache = nullptr; |
| 57 |
this->position.x = 0.f; |
| 58 |
this->position.y = 0.f; |
| 59 |
this->style.line_width = 1.0f; |
| 60 |
this->style.color.r = 0.f; |
| 61 |
this->style.color.g = 0.f; |
| 62 |
this->style.color.b = 0.f; |
| 63 |
this->style.color.a = 1.f; |
| 64 |
} |
| 65 |
|
| 66 |
bool Renderer::DrawingCommand::is_path() const { |
| 67 |
return std::holds_alternative<Renderer::Path>(this->content); |
| 68 |
} |
| 69 |
|
| 70 |
bool Renderer::DrawingCommand::is_text() const { |
| 71 |
return std::holds_alternative<Renderer::Text>(this->content); |
| 72 |
} |
| 73 |
|
| 74 |
const Renderer::Path& Renderer::DrawingCommand::as_path() const { |
| 75 |
return std::get<Renderer::Path>(this->content); |
| 76 |
} |
| 77 |
|
| 78 |
Renderer::Path& Renderer::DrawingCommand::as_path_mut() { |
| 79 |
return std::get<Renderer::Path>(this->content); |
| 80 |
} |
| 81 |
|
| 82 |
const Renderer::Text& Renderer::DrawingCommand::as_text() const { |
| 83 |
return std::get<Renderer::Text>(this->content); |
| 84 |
} |
| 85 |
|
| 86 |
const Renderer::Line& Renderer::PathElement::as_line() const { |
| 87 |
return std::get<Renderer::Line>(this->content); |
| 88 |
} |
| 89 |
|
| 90 |
const Renderer::Arc& Renderer::PathElement::as_arc() const { |
| 91 |
return std::get<Renderer::Arc>(this->content); |
| 92 |
} |
| 93 |
|
| 94 |
bool Renderer::PathElement::is_line() const { |
| 95 |
return std::holds_alternative<Renderer::Line>(this->content); |
| 96 |
} |
| 97 |
|
| 98 |
bool Renderer::PathElement::is_arc() const { |
| 99 |
return std::holds_alternative<Renderer::Arc>(this->content); |
| 100 |
} |
| 101 |
|
| 102 |
std::vector<Renderer::DrawingCommand> Renderer::get_commands() const { |
| 103 |
return this->drawing_commands; |
| 104 |
} |
| 105 |
|
| 106 |
const std::vector<Renderer::PathElement>& Renderer::Path::get_elements() const { |
| 107 |
return this->elements; |
| 108 |
} |
| 109 |
|
| 110 |
Renderer::Path Renderer::create_new_path() const { |
| 111 |
Path ret; |
| 112 |
ret.has_fill = false; |
| 113 |
ret.initial_point = this->position; |
| 114 |
ret.has_stroke = false; |
| 115 |
ret.closed = false; |
| 116 |
return ret; |
| 117 |
} |
| 118 |
|
| 119 |
Renderer::Path& Renderer::top_path() { |
| 120 |
if(this->drawing_commands.empty()) { |
| 121 |
this->drawing_commands.push_back({this->create_new_path()}); |
| 122 |
} |
| 123 |
auto& last_el = this->drawing_commands.back(); |
| 124 |
if(last_el.is_path()) { |
| 125 |
const auto& pth = last_el.as_path(); |
| 126 |
if(!pth.closed) { |
| 127 |
return last_el.as_path_mut(); |
| 128 |
} |
| 129 |
} |
| 130 |
this->drawing_commands.push_back({this->create_new_path()}); |
| 131 |
return this->drawing_commands.back().as_path_mut(); |
| 132 |
} |
| 133 |
|
| 134 |
Renderer::Path* Renderer::top_path_if_exists() { |
| 135 |
if(this->drawing_commands.empty()) { |
| 136 |
return nullptr; |
| 137 |
} |
| 138 |
auto& last_el = this->drawing_commands.back(); |
| 139 |
if(last_el.is_path()) { |
| 140 |
return &last_el.as_path_mut(); |
| 141 |
} else { |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#endif // Emscripten defined |
| 147 |
|
| 148 |
Renderer::TextStyle::TextStyle() { |
| 149 |
this->positioning = TextPositioning::Normal; |
| 150 |
// We want to set them to "" to denote unspecified values by defult |
| 151 |
// this->weight = "normal"; |
| 152 |
// this->size = "medium"; |
| 153 |
this->color.r = 0.f; |
| 154 |
this->color.g = 0.f; |
| 155 |
this->color.b = 0.f; |
| 156 |
this->color.a = 1.f; |
| 157 |
this->specifies_color = false; |
| 158 |
} |
| 159 |
|
| 160 |
Renderer::TextSpan::TextSpan() { |
| 161 |
this->specifies_style = false; |
| 162 |
this->content = std::string(); |
| 163 |
} |
| 164 |
|
| 165 |
Renderer::TextSpan::TextSpan(const std::string& caption) { |
| 166 |
this->specifies_style = false; |
| 167 |
this->content = caption; |
| 168 |
} |
| 169 |
|
| 170 |
Renderer::TextSpan::TextSpan(const std::vector<TextSpan>& subspans) { |
| 171 |
this->specifies_style = false; |
| 172 |
this->content = subspans; |
| 173 |
} |
| 174 |
|
| 175 |
bool Renderer::TextSpan::has_subspans() const { |
| 176 |
return std::holds_alternative<std::vector<TextSpan>>(this->content); |
| 177 |
} |
| 178 |
|
| 179 |
std::string& Renderer::TextSpan::as_caption() { |
| 180 |
return std::get<std::string>(this->content); |
| 181 |
} |
| 182 |
|
| 183 |
std::vector<Renderer::TextSpan>& Renderer::TextSpan::as_subspans() { |
| 184 |
return std::get<std::vector<TextSpan>>(this->content); |
| 185 |
} |
| 186 |
|
| 187 |
const std::string& Renderer::TextSpan::as_caption() const { |
| 188 |
return std::get<std::string>(this->content); |
| 189 |
} |
| 190 |
|
| 191 |
const std::vector<Renderer::TextSpan>& Renderer::TextSpan::as_subspans() const { |
| 192 |
return std::get<std::vector<TextSpan>>(this->content); |
| 193 |
} |
| 194 |
|
| 195 |
void Renderer::move_to(double x, double y) { |
| 196 |
#ifndef __EMSCRIPTEN__ |
| 197 |
cairo_move_to(cr, x, y); |
| 198 |
#else // __EMSCRIPTEN__ defined |
| 199 |
this->position.x = x; |
| 200 |
this->position.y = y; |
| 201 |
// Should it always do this? |
| 202 |
// Cairo docs say so. |
| 203 |
this->new_sub_path(); |
| 204 |
#endif |
| 205 |
} |
| 206 |
|
| 207 |
void Renderer::line_to(double x, double y) { |
| 208 |
#ifndef __EMSCRIPTEN__ |
| 209 |
cairo_line_to(cr, x, y); |
| 210 |
#else // __EMSCRIPTEN__ defined |
| 211 |
Line line; |
| 212 |
line.start = this->position; |
| 213 |
line.end.x = x; |
| 214 |
line.end.y = y; |
| 215 |
graphene_point_t final_pos = line.end; |
| 216 |
this->top_path().elements.push_back({line}); |
| 217 |
this->position = final_pos; |
| 218 |
#endif |
| 219 |
} |
| 220 |
|
| 221 |
void Renderer::arc(double x, double y, double radius, double angle_one, double angle_two) { |
| 222 |
#ifndef __EMSCRIPTEN__ |
| 223 |
cairo_arc(cr, x, y, radius, angle_one, angle_two); |
| 224 |
#else // __EMSCRIPTEN__ defined |
| 225 |
Arc arc; |
| 226 |
arc.origin.x = x; |
| 227 |
arc.origin.y = y; |
| 228 |
arc.radius = radius; |
| 229 |
arc.angle_one = angle_one; |
| 230 |
arc.angle_two = angle_two; |
| 231 |
this->top_path().elements.push_back({arc}); |
| 232 |
#endif |
| 233 |
} |
| 234 |
|
| 235 |
void Renderer::fill() { |
| 236 |
#ifndef __EMSCRIPTEN__ |
| 237 |
cairo_fill(cr); |
| 238 |
#else // __EMSCRIPTEN__ defined |
| 239 |
auto* path = this->top_path_if_exists(); |
| 240 |
if(!path) { |
| 241 |
g_warning("fill() called without a path."); |
| 242 |
return; |
| 243 |
} |
| 244 |
if(path->elements.empty()) { |
| 245 |
g_warning("fill() called with an empty path."); |
| 246 |
//return; |
| 247 |
} |
| 248 |
// Fill closes all opened subpaths I guess? |
| 249 |
path->closed = true; |
| 250 |
// Should I do anything else with it? |
| 251 |
|
| 252 |
path->has_fill = true; |
| 253 |
path->fill_color = this->style.color; |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
void Renderer::stroke() { |
| 258 |
#ifndef __EMSCRIPTEN__ |
| 259 |
cairo_stroke(cr); |
| 260 |
#else // __EMSCRIPTEN__ defined |
| 261 |
this->stroke_preserve(); |
| 262 |
this->new_path(); |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
void Renderer::stroke_preserve() { |
| 267 |
#ifndef __EMSCRIPTEN__ |
| 268 |
cairo_stroke_preserve(cr); |
| 269 |
#else // __EMSCRIPTEN__ defined |
| 270 |
auto* path = this->top_path_if_exists(); |
| 271 |
if(!path) { |
| 272 |
g_warning("stroke() called without a path."); |
| 273 |
return; |
| 274 |
} |
| 275 |
if(path->elements.empty()) { |
| 276 |
g_warning("stroke() called with an empty path."); |
| 277 |
//return; |
| 278 |
} |
| 279 |
path->has_stroke = true; |
| 280 |
path->stroke_style = this->style; |
| 281 |
#endif |
| 282 |
} |
| 283 |
|
| 284 |
void Renderer::new_path() { |
| 285 |
#ifndef __EMSCRIPTEN__ |
| 286 |
cairo_new_path(cr); |
| 287 |
#else // __EMSCRIPTEN__ defined |
| 288 |
auto* path = this->top_path_if_exists(); |
| 289 |
if(path) { |
| 290 |
// if(!path->closed) { |
| 291 |
// // Reset the path |
| 292 |
// *path = this->create_new_path(); |
| 293 |
// } else { |
| 294 |
// Actually creates another new path |
| 295 |
this->close_path_inner(); |
| 296 |
// } |
| 297 |
} |
| 298 |
// Is this really what the function ought to do? |
| 299 |
#endif |
| 300 |
} |
| 301 |
|
| 302 |
#ifdef __EMSCRIPTEN__ |
| 303 |
void Renderer::close_path_inner() { |
| 304 |
// 2. Close the sub-path |
| 305 |
auto* path = this->top_path_if_exists(); |
| 306 |
if(path) { |
| 307 |
if(!path->closed) { |
| 308 |
path->closed = true; |
| 309 |
} else { |
| 310 |
// Technically, just creating new path, means that the previous one is done with. |
| 311 |
this->drawing_commands.push_back({this->create_new_path()}); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// 3. |
| 316 |
// To quote the docs: |
| 317 |
// "The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() |
| 318 |
// with the equivalent coordinate in the case of stroking. When a closed sub-path is stroked, |
| 319 |
// there are no caps on the ends of the sub-path. Instead, there is a line join connecting |
| 320 |
// the final and initial segments of the sub-path." |
| 321 |
// |
| 322 |
// Now, I don't exactly now what that means for me. |
| 323 |
// |
| 324 |
} |
| 325 |
#endif |
| 326 |
|
| 327 |
void Renderer::close_path() { |
| 328 |
#ifndef __EMSCRIPTEN__ |
| 329 |
cairo_close_path(cr); |
| 330 |
#else // __EMSCRIPTEN__ defined |
| 331 |
// // 1. Add a line to the beginning of the path |
| 332 |
auto* path = this->top_path_if_exists(); |
| 333 |
if(path) { |
| 334 |
if(!path->closed) { |
| 335 |
this->line_to(path->initial_point.x, path->initial_point.y); |
| 336 |
} |
| 337 |
this->close_path_inner(); |
| 338 |
} |
| 339 |
#endif |
| 340 |
} |
| 341 |
|
| 342 |
void Renderer::new_sub_path() { |
| 343 |
#ifndef __EMSCRIPTEN__ |
| 344 |
cairo_new_sub_path(cr); |
| 345 |
#else // __EMSCRIPTEN__ defined |
| 346 |
auto* path = this->top_path_if_exists(); |
| 347 |
// I'm not sure if that's what the function is supposed to do. |
| 348 |
if(path) { |
| 349 |
if(!path->closed) { |
| 350 |
path->closed = true; |
| 351 |
} |
| 352 |
} |
| 353 |
this->drawing_commands.push_back({this->create_new_path()}); |
| 354 |
|
| 355 |
#endif |
| 356 |
} |
| 357 |
|
| 358 |
void Renderer::set_source_rgb(double r, double g, double b) { |
| 359 |
#ifndef __EMSCRIPTEN__ |
| 360 |
cairo_set_source_rgb(cr, r, g, b); |
| 361 |
#else // __EMSCRIPTEN__ defined |
| 362 |
this->style.color.r = r; |
| 363 |
this->style.color.g = g; |
| 364 |
this->style.color.b = b; |
| 365 |
this->style.color.a = 1.0f; |
| 366 |
#endif |
| 367 |
} |
| 368 |
|
| 369 |
void Renderer::set_source_rgba(double r, double g, double b, double a) { |
| 370 |
#ifndef __EMSCRIPTEN__ |
| 371 |
cairo_set_source_rgba(cr, r, g, b, a); |
| 372 |
#else // __EMSCRIPTEN__ defined |
| 373 |
this->style.color.r = r; |
| 374 |
this->style.color.g = g; |
| 375 |
this->style.color.b = b; |
| 376 |
this->style.color.a = a; |
| 377 |
#endif |
| 378 |
} |
| 379 |
|
| 380 |
void Renderer::set_line_width(double width) { |
| 381 |
#ifndef __EMSCRIPTEN__ |
| 382 |
cairo_set_line_width(cr, width); |
| 383 |
#else // __EMSCRIPTEN__ defined |
| 384 |
this->style.line_width = width; |
| 385 |
#endif |
| 386 |
} |
| 387 |
|
| 388 |
#ifndef __EMSCRIPTEN__ |
| 389 |
std::string Renderer::text_span_to_pango_markup(const TextSpan& span, const std::optional<TextStyle>& parent_style) const { |
| 390 |
std::string ret; |
| 391 |
auto should_specify_style = [&](){ |
| 392 |
if(span.specifies_style) { |
| 393 |
|
| 394 |
if(parent_style.has_value()) { |
| 395 |
auto& pstyle = parent_style.value(); |
| 396 |
//strange compiler error |
| 397 |
//return pstyle != span.style; |
| 398 |
return true; |
| 399 |
} else { |
| 400 |
return true; |
| 401 |
} |
| 402 |
} |
| 403 |
return false; |
| 404 |
}; |
| 405 |
bool style_block = should_specify_style(); |
| 406 |
if(style_block) { |
| 407 |
const auto& style = span.style; |
| 408 |
ret += "<span "; |
| 409 |
if(style.specifies_color) { |
| 410 |
std::stringstream html_color; |
| 411 |
html_color << "#"; |
| 412 |
html_color << std::hex << std::setfill('0') << std::setw(2) << (int)(style.color.r * 255); |
| 413 |
html_color << std::hex << std::setfill('0') << std::setw(2) << (int)(style.color.g * 255); |
| 414 |
html_color << std::hex << std::setfill('0') << std::setw(2) << (int)(style.color.b * 255); |
| 415 |
html_color << std::hex << std::setfill('0') << std::setw(2) << (int)(style.color.a * 255); |
| 416 |
// std::cout<<html_color.str()<<' '<<style.color.r<<' '<<style.color.g<<' '<<style.color.b<<'\n'; |
| 417 |
ret += "color=\"" + html_color.str() + "\" "; |
| 418 |
} |
| 419 |
if(!style.size.empty()) { |
| 420 |
ret += "size=\"" + style.size + "\" "; |
| 421 |
} |
| 422 |
if(!style.weight.empty()) { |
| 423 |
ret += "weight=\"" + style.weight + "\" "; |
| 424 |
} |
| 425 |
ret += ">"; |
| 426 |
switch(style.positioning) { |
| 427 |
case TextPositioning::Sub: { |
| 428 |
ret += "<sub>"; |
| 429 |
break; |
| 430 |
} |
| 431 |
case TextPositioning::Super: { |
| 432 |
// The string below begins with |
| 433 |
// the invisible U+200B unicode character. |
| 434 |
// This is a workaround for what's likely |
| 435 |
// a bug in pango font rendering engine. |
| 436 |
// Without it, the superscript is relative |
| 437 |
// to the subscript (atom count) |
| 438 |
// instead of the atom's symbol |
| 439 |
ret += "​<sup>"; |
| 440 |
break; |
| 441 |
} |
| 442 |
default: |
| 443 |
case TextPositioning::Normal: { |
| 444 |
// nothing |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
if(span.has_subspans()) { |
| 449 |
const auto& subspans = span.as_subspans(); |
| 450 |
for(const auto& subspan: subspans) { |
| 451 |
std::optional<TextStyle> styleopt = subspan.specifies_style ? subspan.style : parent_style; |
| 452 |
ret += this->text_span_to_pango_markup(subspan, styleopt); |
| 453 |
} |
| 454 |
} else { |
| 455 |
const auto& caption = span.as_caption(); |
| 456 |
// todo: escape characters! |
| 457 |
ret += caption; |
| 458 |
} |
| 459 |
if(style_block) { |
| 460 |
switch(span.style.positioning) { |
| 461 |
case TextPositioning::Sub: { |
| 462 |
ret += "</sub>"; |
| 463 |
break; |
| 464 |
} |
| 465 |
case TextPositioning::Super: { |
| 466 |
ret += "</sup>"; |
| 467 |
break; |
| 468 |
} |
| 469 |
default: |
| 470 |
case TextPositioning::Normal: { |
| 471 |
// nothing |
| 472 |
} |
| 473 |
} |
| 474 |
ret += "</span>"; |
| 475 |
} |
| 476 |
return ret; |
| 477 |
} |
| 478 |
#else |
| 479 |
std::optional<Renderer::TextSize> Renderer::TextMeasurementCache::lookup_span(const Renderer::TextSpan& text) const { |
| 480 |
return this->lookup_span(std::hash<TextSpan>{}(text)); |
| 481 |
} |
| 482 |
|
| 483 |
std::optional<Renderer::TextSize> Renderer::TextMeasurementCache::lookup_span(Renderer::TextMeasurementCache::hash_t span_hash) const { |
| 484 |
auto it = this->cache.find(span_hash); |
| 485 |
if(it != this->cache.end()) { |
| 486 |
return it->second; |
| 487 |
} |
| 488 |
return std::nullopt; |
| 489 |
} |
| 490 |
|
| 491 |
void Renderer::TextMeasurementCache::add(Renderer::TextMeasurementCache::hash_t span_hash, Renderer::TextSize value) { |
| 492 |
this->cache.emplace(span_hash, value); |
| 493 |
} |
| 494 |
|
| 495 |
void Renderer::TextMeasurementCache::add(const Renderer::TextSpan& text, Renderer::TextSize value) { |
| 496 |
this->add(std::hash<TextSpan>{}(text), value); |
| 497 |
} |
| 498 |
std::size_t Renderer::TextMeasurementCache::size() const { |
| 499 |
return this->cache.size(); |
| 500 |
} |
| 501 |
|
| 502 |
#endif |
| 503 |
|
| 504 |
Renderer::TextSize Renderer::measure_text(const Renderer::TextSpan& text) { |
| 505 |
#ifndef __EMSCRIPTEN__ |
| 506 |
std::string markup = this->text_span_to_pango_markup(text); |
| 507 |
pango_layout_set_markup(this->pango_layout, markup.c_str(), -1); |
| 508 |
TextSize ret; |
| 509 |
pango_layout_get_pixel_size(this->pango_layout, &ret.width, &ret.height); |
| 510 |
return ret; |
| 511 |
#else // __EMSCRIPTEN__ defined |
| 512 |
std::optional<TextMeasurementCache::hash_t> text_hash; |
| 513 |
if(this->tm_cache) { |
| 514 |
text_hash = std::hash<TextSpan>{}(text); |
| 515 |
auto cached_opt = this->tm_cache->lookup_span(text_hash.value()); |
| 516 |
if(cached_opt.has_value()) { |
| 517 |
return cached_opt.value(); |
| 518 |
} |
| 519 |
} |
| 520 |
// return {0,0}; |
| 521 |
// The try..catch doesn't work for me. |
| 522 |
// try { |
| 523 |
// g_info("Measuring..."); |
| 524 |
Renderer::Text wtext; |
| 525 |
wtext.origin.x = 0; |
| 526 |
wtext.origin.y = 0; |
| 527 |
wtext.spans.push_back(text); |
| 528 |
// g_info("Wrapper text has been built."); |
| 529 |
emscripten::val result = this->text_measurement_function(wtext); |
| 530 |
// g_info("Got result."); |
| 531 |
auto ret = result.as<TextSize>(); |
| 532 |
// } catch(...) { |
| 533 |
// return {0,0}; |
| 534 |
// } |
| 535 |
if(this->tm_cache) { |
| 536 |
this->tm_cache->add(text_hash.value(), ret); |
| 537 |
g_debug("TextSpan added to cache. Cache entries: %lu", this->tm_cache->size()); |
| 538 |
} |
| 539 |
return ret; |
| 540 |
#endif |
| 541 |
} |
| 542 |
|
| 543 |
void Renderer::show_text(const Renderer::TextSpan& text_span) { |
| 544 |
#ifndef __EMSCRIPTEN__ |
| 545 |
std::string markup = this->text_span_to_pango_markup(text_span); |
| 546 |
pango_layout_set_markup(this->pango_layout, markup.c_str(), -1); |
| 547 |
pango_cairo_show_layout(this->cr, this->pango_layout); |
| 548 |
#else // Lhasa |
| 549 |
Text text; |
| 550 |
if(text_span.has_subspans()) { |
| 551 |
// This is a deep copy. Yikes. |
| 552 |
text.spans = text_span.as_subspans(); |
| 553 |
text.style = text_span.style; |
| 554 |
} else { |
| 555 |
text.spans.push_back(text_span); |
| 556 |
// Let's leave the style as it is, for now. |
| 557 |
// text.style |
| 558 |
} |
| 559 |
text.origin = this->position; |
| 560 |
this->drawing_commands.push_back(DrawingCommand{text}); |
| 561 |
#endif |
| 562 |
} |
| 563 |
|
| 564 |
Renderer::~Renderer() { |
| 565 |
#ifndef __EMSCRIPTEN__ |
| 566 |
g_object_unref(this->pango_layout); |
| 567 |
cairo_destroy(this->cr); |
| 568 |
#else // __EMSCRIPTEN__ defined |
| 569 |
// Lhasa-specific includes/definitions |
| 570 |
#endif |
| 571 |
} |
| 572 |
|
| 573 |
MoleculeRenderContext::MoleculeRenderContext(const CanvasMolecule& cm, Renderer& ren, DisplayMode mode) |
| 574 |
:canvas_molecule(cm), ren(ren), display_mode(mode) { |
| 575 |
scale_factor = canvas_molecule.get_scale(); |
| 576 |
x_offset = scale_factor * canvas_molecule.x_canvas_translation; |
| 577 |
y_offset = scale_factor * canvas_molecule.y_canvas_translation; |
| 578 |
} |
| 579 |
|
| 580 |
MoleculeRenderContext::~MoleculeRenderContext() { |
| 581 |
|
| 582 |
} |
| 583 |
|
| 584 |
std::tuple<Renderer::TextSpan, bool> MoleculeRenderContext::process_appendix(const std::string& symbol, const std::optional<Atom::Appendix>& appendix, const Renderer::TextStyle& inherited_style) { |
| 585 |
Renderer::TextSpan ret((std::vector<Renderer::TextSpan>())); |
| 586 |
Renderer::TextSpan symbol_span(symbol); |
| 587 |
bool reversed = false; |
| 588 |
if(!appendix.has_value()) { |
| 589 |
ret.as_subspans().push_back(symbol_span); |
| 590 |
} else { |
| 591 |
const auto& ap = appendix.value(); |
| 592 |
//ret += "<span>"; |
| 593 |
Renderer::TextSpan root_span((std::vector<Renderer::TextSpan>())); |
| 594 |
Renderer::TextSpan superatoms_symbol_span; |
| 595 |
auto make_index_span = [=](){ |
| 596 |
Renderer::TextSpan index_span; |
| 597 |
index_span.specifies_style = true; |
| 598 |
index_span.style = inherited_style; |
| 599 |
index_span.style.positioning = Renderer::TextPositioning::Sub; |
| 600 |
return index_span; |
| 601 |
}; |
| 602 |
auto index_span = make_index_span(); |
| 603 |
|
| 604 |
for(auto i = ap.superatoms.begin(); i != ap.superatoms.end(); i++) { |
| 605 |
if(std::isdigit(*i)) { |
| 606 |
if(!superatoms_symbol_span.as_caption().empty()) { |
| 607 |
root_span.as_subspans().push_back(std::move(superatoms_symbol_span)); |
| 608 |
superatoms_symbol_span = Renderer::TextSpan(); |
| 609 |
} |
| 610 |
index_span.as_caption().push_back(*i); |
| 611 |
} else { |
| 612 |
if(!index_span.as_caption().empty()) { |
| 613 |
root_span.as_subspans().push_back(std::move(index_span)); |
| 614 |
index_span = make_index_span(); |
| 615 |
} |
| 616 |
superatoms_symbol_span.as_caption().push_back(*i); |
| 617 |
} |
| 618 |
} |
| 619 |
if(!superatoms_symbol_span.as_caption().empty()) { |
| 620 |
root_span.as_subspans().push_back(std::move(superatoms_symbol_span)); |
| 621 |
} |
| 622 |
if(!index_span.as_caption().empty()) { |
| 623 |
root_span.as_subspans().push_back(std::move(index_span)); |
| 624 |
} |
| 625 |
if (ap.reversed) { |
| 626 |
ret.as_subspans().push_back(root_span); |
| 627 |
ret.as_subspans().push_back(symbol_span); |
| 628 |
reversed = true; |
| 629 |
} else { |
| 630 |
ret.as_subspans().push_back(symbol_span); |
| 631 |
ret.as_subspans().push_back(root_span); |
| 632 |
} |
| 633 |
//ret += "</span>"; |
| 634 |
if(ap.charge != 0) { |
| 635 |
Renderer::TextSpan charge_span; |
| 636 |
charge_span.specifies_style = true; |
| 637 |
charge_span.style = inherited_style; |
| 638 |
charge_span.style.positioning = Renderer::TextPositioning::Super; |
| 639 |
|
| 640 |
unsigned int charge_no_sign = std::abs(ap.charge); |
| 641 |
if(charge_no_sign > 1) { |
| 642 |
charge_span.as_caption() += std::to_string(charge_no_sign); |
| 643 |
} |
| 644 |
charge_span.as_caption().push_back(ap.charge > 0 ? '+' : '-'); |
| 645 |
ret.as_subspans().push_back(charge_span); |
| 646 |
} |
| 647 |
} |
| 648 |
return std::make_tuple(ret,reversed); |
| 649 |
} |
| 650 |
|
| 651 |
std::pair<unsigned int,graphene_rect_t> MoleculeRenderContext::render_atom(const CanvasMolecule::Atom& atom, DisplayMode render_mode) { |
| 652 |
// pre-process text |
| 653 |
auto [r,g,b] = CanvasMolecule::atom_color_to_rgb(atom.color); |
| 654 |
// const std::string color_str = CanvasMolecule::atom_color_to_html(atom.color); |
| 655 |
|
| 656 |
// Span for the whole thing - includes symbol, appendix, index etc. |
| 657 |
Renderer::TextSpan atom_span((std::vector<Renderer::TextSpan>())); |
| 658 |
atom_span.specifies_style = true; |
| 659 |
atom_span.style.specifies_color = true; |
| 660 |
atom_span.style.color.r = r; |
| 661 |
atom_span.style.color.g = g; |
| 662 |
atom_span.style.color.b = b; |
| 663 |
#ifndef __EMSCRIPTEN__ |
| 664 |
atom_span.style.size = render_mode != DisplayMode::AtomIndices ? "x-large" : "medium"; |
| 665 |
#else |
| 666 |
atom_span.style.size = render_mode != DisplayMode::AtomIndices ? "medium" : "small"; |
| 667 |
#endif |
| 668 |
atom_span.style.weight = atom.highlight != 0 ? "bold" : "normal"; |
| 669 |
|
| 670 |
// Span for the atom symbol - solely. |
| 671 |
// This allows us to measure the size of the atom's symbol |
| 672 |
// and then properly center/align the whole text |
| 673 |
Renderer::TextSpan raw_atom_span; |
| 674 |
raw_atom_span.specifies_style = true; |
| 675 |
raw_atom_span.style = atom_span.style; |
| 676 |
|
| 677 |
bool reversed = false; |
| 678 |
|
| 679 |
switch (render_mode) { |
| 680 |
case DisplayMode::AtomIndices: { |
| 681 |
raw_atom_span.as_caption() += atom.symbol; |
| 682 |
atom_span |
| 683 |
.as_subspans() |
| 684 |
.push_back( |
| 685 |
Renderer::TextSpan(std::string(atom.symbol + ":" + std::to_string(atom.idx))) |
| 686 |
); |
| 687 |
break; |
| 688 |
} |
| 689 |
case DisplayMode::AtomNames: { |
| 690 |
if(atom.name.has_value()) { |
| 691 |
std::string atom_name = atom.name.value(); |
| 692 |
raw_atom_span.as_caption() += atom_name; |
| 693 |
atom_span = raw_atom_span; |
| 694 |
break; |
| 695 |
} |
| 696 |
// break; |
| 697 |
// We want to fall back to the standard case if the atom has no name. |
| 698 |
} |
| 699 |
default: |
| 700 |
case DisplayMode::Standard: { |
| 701 |
auto [appendix,p_reversed] = process_appendix(atom.symbol, atom.appendix, atom_span.style); |
| 702 |
reversed = p_reversed; |
| 703 |
raw_atom_span.as_caption() += atom.symbol; |
| 704 |
atom_span |
| 705 |
.as_subspans() |
| 706 |
.push_back(appendix); |
| 707 |
break; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
// Used to make the texts centered where they should be (appendix). |
| 712 |
// Measure the size of the "main" atom, without "appendix" |
| 713 |
Renderer::TextSize raw_size = ren.measure_text(raw_atom_span); |
| 714 |
// Measure full size of the text |
| 715 |
Renderer::TextSize size = ren.measure_text(atom_span); |
| 716 |
|
| 717 |
// g_info("Measurement results: raw %ix%i n %ix%i", raw_size.width, raw_size.height, size.width, size.height); |
| 718 |
|
| 719 |
#ifndef __EMSCRIPTEN__ |
| 720 |
// todo: get rid of this '5' magic number - figure out what's wrong |
| 721 |
const int magic1 = 5; |
| 722 |
// For Lhasa |
| 723 |
const int magic2 = 0; |
| 724 |
// Magic number. This should be removed. |
| 725 |
// Workaround for pango giving us too high layout size. |
| 726 |
const float layout_to_high = 3.f; |
| 727 |
#else |
| 728 |
const int magic1 = 0; |
| 729 |
// Text appears too high. Manuall offset. |
| 730 |
const int magic2 = 15; |
| 731 |
const float layout_to_high = 0.f; |
| 732 |
#endif |
| 733 |
int layout_x_offset = reversed ? size.width - raw_size.width / 2.f + magic1 : raw_size.width / 2.f; |
| 734 |
double origin_x = atom.x * scale_factor + x_offset - layout_x_offset; |
| 735 |
double origin_y = atom.y * scale_factor + y_offset - raw_size.height / 2.f; |
| 736 |
|
| 737 |
graphene_rect_t rect; |
| 738 |
rect.origin.x = origin_x; |
| 739 |
rect.origin.y = origin_y + layout_to_high; |
| 740 |
rect.size.width = size.width; |
| 741 |
rect.size.height = size.height - layout_to_high * 2.f; |
| 742 |
|
| 743 |
// g_info("Rect: x=%f, y=%f, width=%f, height=%f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); |
| 744 |
|
| 745 |
// highlight |
| 746 |
process_atom_highlight(atom); |
| 747 |
// text |
| 748 |
ren.move_to(origin_x, origin_y + magic2); |
| 749 |
ren.show_text(atom_span); |
| 750 |
|
| 751 |
return std::make_pair(atom.idx, rect); |
| 752 |
} |
| 753 |
|
| 754 |
void MoleculeRenderContext::draw_atoms() { |
| 755 |
for(const auto& atom: canvas_molecule.atoms) { |
| 756 |
switch (display_mode) { |
| 757 |
case DisplayMode::AtomIndices: { |
| 758 |
atom_idx_to_canvas_rect.emplace(render_atom(atom,DisplayMode::AtomIndices)); |
| 759 |
break; |
| 760 |
} |
| 761 |
case DisplayMode::AtomNames: { |
| 762 |
if(atom.name.has_value()) { |
| 763 |
atom_idx_to_canvas_rect.emplace(render_atom(atom,DisplayMode::AtomNames)); |
| 764 |
break; |
| 765 |
} |
| 766 |
// We want to fall back to the standard case if the atom has no name |
| 767 |
// break; |
| 768 |
} |
| 769 |
default: |
| 770 |
case DisplayMode::Standard: { |
| 771 |
if(atom.symbol == "C") { |
| 772 |
if(atom.appendix.has_value()) { |
| 773 |
atom_idx_to_canvas_rect.emplace(render_atom(atom)); |
| 774 |
} else { |
| 775 |
process_atom_highlight(atom); |
| 776 |
} |
| 777 |
} else { |
| 778 |
atom_idx_to_canvas_rect.emplace(render_atom(atom)); |
| 779 |
} |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
void MoleculeRenderContext::process_atom_highlight(const Atom& atom) { |
| 787 |
auto highlight = CanvasMolecule::determine_dominant_highlight(atom.highlight); |
| 788 |
if(highlight) { |
| 789 |
auto [r, g, b] = CanvasMolecule::hightlight_to_rgb(*highlight); |
| 790 |
ren.new_sub_path(); |
| 791 |
// Go to the center of the atom |
| 792 |
//ren.move_to(atom.x * scale_factor + x_offset + CanvasMolecule::ATOM_HITBOX_RADIUS, atom.y * scale_factor + y_offset); |
| 793 |
ren.set_source_rgb(r, g, b); |
| 794 |
ren.arc(atom.x * scale_factor + x_offset, atom.y * scale_factor + y_offset, CanvasMolecule::ATOM_HITBOX_RADIUS, 0, M_PI * 2.0); |
| 795 |
ren.stroke_preserve(); |
| 796 |
ren.set_source_rgba(r, g, b, 0.5); |
| 797 |
ren.fill(); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
std::pair<graphene_point_t,graphene_point_t> MoleculeRenderContext::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) { |
| 802 |
// We pass in the bond vector so that it always points "away" from the point |
| 803 |
auto crop_line_against_rect = [](const graphene_rect_t& rect, float bond_vec_x, float bond_vec_y, const graphene_point_t& point){ |
| 804 |
graphene_point_t ret = point; |
| 805 |
if(rect.origin.x < point.x && rect.origin.x + rect.size.width >= point.x |
| 806 |
&& rect.origin.y < point.y && rect.origin.y + rect.size.height >= point.y) { // inside rectangle |
| 807 |
// We need to use the point of intersection formula |
| 808 |
// to find the intersection point between the bond and the edges. |
| 809 |
// We solve against two edges and then pick the solution lying the closest to 'point'. |
| 810 |
|
| 811 |
// Line from bond |
| 812 |
graphene_point_t point2 = point; |
| 813 |
point2.x += bond_vec_x; |
| 814 |
point2.y += bond_vec_y; |
| 815 |
const float bond_a_quot = (point.x - point2.x); |
| 816 |
const float bond_a = bond_a_quot == 0 ? -point.x : (point.y - point2.y) / bond_a_quot; |
| 817 |
const float bond_b = -1; |
| 818 |
const float bond_c = point.y - bond_a * point.x; |
| 819 |
|
| 820 |
// Pick line from the relevant vertical edge |
| 821 |
float vert_egde_c; |
| 822 |
const float vert_edge_a = 1; |
| 823 |
const float vert_edge_b = 0; |
| 824 |
if(bond_vec_x > 0) { // solve against right side |
| 825 |
vert_egde_c = -(rect.origin.x + rect.size.width); |
| 826 |
} else { // solve against left side |
| 827 |
vert_egde_c = -rect.origin.x; |
| 828 |
} |
| 829 |
|
| 830 |
// Pick line from the relevant horizontal edge |
| 831 |
float horiz_egde_c; |
| 832 |
const float horiz_edge_a = 0; |
| 833 |
const float horiz_edge_b = 1; |
| 834 |
if(bond_vec_y > 0) { // solve against the bottom |
| 835 |
horiz_egde_c = -(rect.origin.y + rect.size.height); |
| 836 |
} else { // solve against the top |
| 837 |
horiz_egde_c = -rect.origin.y; |
| 838 |
} |
| 839 |
graphene_point_t vert_edge_solution; |
| 840 |
vert_edge_solution.x = (bond_b * vert_egde_c - vert_edge_b * bond_c)/(bond_a * vert_edge_b - vert_edge_a * bond_b); |
| 841 |
vert_edge_solution.y = (vert_edge_a * bond_c - bond_a * vert_egde_c)/(bond_a * vert_edge_b - vert_edge_a * bond_b); |
| 842 |
graphene_point_t horiz_edge_solution; |
| 843 |
horiz_edge_solution.x = (bond_b * horiz_egde_c - horiz_edge_b * bond_c)/(bond_a * horiz_edge_b - horiz_edge_a * bond_b); |
| 844 |
horiz_edge_solution.y = (horiz_edge_a * bond_c - bond_a * horiz_egde_c)/(bond_a * horiz_edge_b - horiz_edge_a * bond_b); |
| 845 |
|
| 846 |
// if vert_edge_solution is farther from the point than horiz_edge_solution |
| 847 |
if(std::pow(vert_edge_solution.x - point.x, 2.f) + std::pow(vert_edge_solution.y - point.y, 2.f) |
| 848 |
> std::pow(horiz_edge_solution.x - point.x, 2.f) + std::pow(horiz_edge_solution.y - point.y, 2.f)) { |
| 849 |
ret = horiz_edge_solution; |
| 850 |
} else { |
| 851 |
ret = vert_edge_solution; |
| 852 |
} |
| 853 |
} |
| 854 |
return ret; |
| 855 |
}; |
| 856 |
|
| 857 |
graphene_point_t a = first_atom; |
| 858 |
graphene_point_t b = second_atom; |
| 859 |
|
| 860 |
float bond_vec_x = second_atom.x - first_atom.x; |
| 861 |
float bond_vec_y = second_atom.y - first_atom.y; |
| 862 |
|
| 863 |
auto first_rect_iter = atom_idx_to_canvas_rect.find(first_atom_idx); |
| 864 |
if(first_rect_iter != atom_idx_to_canvas_rect.end()) { |
| 865 |
a = crop_line_against_rect(first_rect_iter->second, bond_vec_x, bond_vec_y, first_atom); |
| 866 |
} |
| 867 |
auto second_rect_iter = atom_idx_to_canvas_rect.find(second_atom_idx); |
| 868 |
if(second_rect_iter != atom_idx_to_canvas_rect.end()) { |
| 869 |
b = crop_line_against_rect(second_rect_iter->second, -bond_vec_x, -bond_vec_y, second_atom); |
| 870 |
} |
| 871 |
|
| 872 |
return std::make_pair(a,b); |
| 873 |
} |
| 874 |
|
| 875 |
void MoleculeRenderContext::draw_central_bond_line(const CanvasMolecule::Bond& bond) { |
| 876 |
graphene_point_t first_atom; |
| 877 |
first_atom.x = bond.first_atom_x * scale_factor + x_offset; |
| 878 |
first_atom.y = bond.first_atom_y * scale_factor + y_offset; |
| 879 |
|
| 880 |
graphene_point_t second_atom; |
| 881 |
second_atom.x = bond.second_atom_x * scale_factor + x_offset; |
| 882 |
second_atom.y = bond.second_atom_y * scale_factor + y_offset; |
| 883 |
|
| 884 |
auto [first,second] = cropped_bond_coords(first_atom,bond.first_atom_idx,second_atom,bond.second_atom_idx); |
| 885 |
|
| 886 |
ren.move_to(first.x, first.y); |
| 887 |
ren.line_to(second.x, second.y); |
| 888 |
ren.stroke(); |
| 889 |
} |
| 890 |
|
| 891 |
void MoleculeRenderContext::draw_straight_wedge(const CanvasMolecule::Bond& bond, bool reversed) { |
| 892 |
graphene_point_t origin; |
| 893 |
origin.x = reversed ? bond.first_atom_x : bond.second_atom_x; |
| 894 |
origin.y = reversed ? bond.first_atom_y : bond.second_atom_y; |
| 895 |
auto origin_idx = reversed ? bond.first_atom_idx : bond.second_atom_idx; |
| 896 |
|
| 897 |
graphene_point_t target; |
| 898 |
target.x = reversed ? bond.second_atom_x : bond.first_atom_x; |
| 899 |
target.y = reversed ? bond.second_atom_y : bond.first_atom_y; |
| 900 |
auto target_idx = reversed ? bond.second_atom_idx : bond.first_atom_idx; |
| 901 |
|
| 902 |
origin.x *= scale_factor; |
| 903 |
origin.y *= scale_factor; |
| 904 |
target.x *= scale_factor; |
| 905 |
target.y *= scale_factor; |
| 906 |
|
| 907 |
origin.x += x_offset; |
| 908 |
origin.y += y_offset; |
| 909 |
target.x += x_offset; |
| 910 |
target.y += y_offset; |
| 911 |
|
| 912 |
auto [origin_cropped,target_cropped] = cropped_bond_coords(origin, origin_idx, target, target_idx); |
| 913 |
|
| 914 |
auto [pv_x,pv_y] = bond.get_perpendicular_versor(); |
| 915 |
auto cropped_bond_len = std::sqrt(std::pow(target_cropped.x - origin_cropped.x, 2.f) + std::pow(target_cropped.y - origin_cropped.y, 2.f)); |
| 916 |
auto v_x = pv_x * std::sin(GEOMETRY_BOND_SPREAD_ANGLE / 2.f) * cropped_bond_len; |
| 917 |
auto v_y = pv_y * std::sin(GEOMETRY_BOND_SPREAD_ANGLE / 2.f) * cropped_bond_len; |
| 918 |
|
| 919 |
ren.new_path(); |
| 920 |
ren.move_to(origin_cropped.x, origin_cropped.y); |
| 921 |
ren.line_to(target_cropped.x + v_x, target_cropped.y + v_y); |
| 922 |
ren.stroke_preserve(); |
| 923 |
|
| 924 |
ren.line_to(target_cropped.x - v_x, target_cropped.y - v_y); |
| 925 |
ren.stroke_preserve(); |
| 926 |
|
| 927 |
ren.line_to(origin_cropped.x, origin_cropped.y); |
| 928 |
ren.stroke_preserve(); |
| 929 |
|
| 930 |
ren.close_path(); |
| 931 |
ren.fill(); |
| 932 |
} |
| 933 |
|
| 934 |
void MoleculeRenderContext::draw_straight_dashed_bond(const CanvasMolecule::Bond& bond, bool reversed) { |
| 935 |
graphene_point_t origin; |
| 936 |
origin.x = reversed ? bond.first_atom_x : bond.second_atom_x; |
| 937 |
origin.y = reversed ? bond.first_atom_y : bond.second_atom_y; |
| 938 |
auto origin_idx = reversed ? bond.first_atom_idx : bond.second_atom_idx; |
| 939 |
|
| 940 |
graphene_point_t target; |
| 941 |
target.x = reversed ? bond.second_atom_x : bond.first_atom_x; |
| 942 |
target.y = reversed ? bond.second_atom_y : bond.first_atom_y; |
| 943 |
auto target_idx = reversed ? bond.second_atom_idx : bond.first_atom_idx; |
| 944 |
|
| 945 |
origin.x *= scale_factor; |
| 946 |
origin.y *= scale_factor; |
| 947 |
target.x *= scale_factor; |
| 948 |
target.y *= scale_factor; |
| 949 |
|
| 950 |
origin.x += x_offset; |
| 951 |
origin.y += y_offset; |
| 952 |
target.x += x_offset; |
| 953 |
target.y += y_offset; |
| 954 |
|
| 955 |
auto [current,target_cropped] = cropped_bond_coords(origin, origin_idx, target, target_idx); |
| 956 |
|
| 957 |
auto [pv_x,pv_y] = bond.get_perpendicular_versor(); |
| 958 |
auto cropped_bond_len = std::sqrt(std::pow(target_cropped.x - current.x, 2.f) + std::pow(target_cropped.y - current.y, 2.f)); |
| 959 |
|
| 960 |
float dashes = cropped_bond_len / (GEOMETRY_BOND_DASH_SEPARATION * scale_factor); |
| 961 |
unsigned int full_dashes = std::floor(dashes); |
| 962 |
|
| 963 |
float step_x = (target_cropped.x - current.x) / dashes; |
| 964 |
float step_y = (target_cropped.y - current.y) / dashes; |
| 965 |
|
| 966 |
auto v_x = pv_x * std::sin(GEOMETRY_BOND_SPREAD_ANGLE / 2.f) * cropped_bond_len; |
| 967 |
auto v_y = pv_y * std::sin(GEOMETRY_BOND_SPREAD_ANGLE / 2.f) * cropped_bond_len; |
| 968 |
|
| 969 |
for(unsigned int i = 0; i <= full_dashes; i++) { |
| 970 |
float spread_multiplier = (float) i / dashes; |
| 971 |
ren.move_to(current.x - v_x * spread_multiplier, current.y - v_y * spread_multiplier); |
| 972 |
ren.line_to(current.x + v_x * spread_multiplier, current.y + v_y * spread_multiplier); |
| 973 |
ren.stroke(); |
| 974 |
current.x += step_x; |
| 975 |
current.y += step_y; |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
void MoleculeRenderContext::draw_wavy_bond(const CanvasMolecule::Bond& bond) { |
| 980 |
graphene_point_t first_atom; |
| 981 |
first_atom.x = bond.first_atom_x * scale_factor + x_offset; |
| 982 |
first_atom.y = bond.first_atom_y * scale_factor + y_offset; |
| 983 |
|
| 984 |
graphene_point_t second_atom; |
| 985 |
second_atom.x = bond.second_atom_x * scale_factor + x_offset; |
| 986 |
second_atom.y = bond.second_atom_y * scale_factor + y_offset; |
| 987 |
|
| 988 |
auto [first,second] = cropped_bond_coords(first_atom,bond.first_atom_idx,second_atom,bond.second_atom_idx); |
| 989 |
auto full_vec_x = second.x - first.x; |
| 990 |
auto full_vec_y = second.y - first.y; |
| 991 |
|
| 992 |
const float wave_arc_radius = WAVY_BOND_ARC_LENGTH * scale_factor / 2.f; |
| 993 |
|
| 994 |
// The angle at which the bond points |
| 995 |
float base_angle = std::atan(full_vec_y / full_vec_x); |
| 996 |
float arcs_count = std::sqrt(std::pow(full_vec_x,2.f) + std::pow(full_vec_y,2.f)) / (WAVY_BOND_ARC_LENGTH * scale_factor); |
| 997 |
unsigned int rounded_arcs_count = std::floor(arcs_count); |
| 998 |
float step_x = full_vec_x / arcs_count; |
| 999 |
float step_y = full_vec_y / arcs_count; |
| 1000 |
float current_x = first.x + step_x / 2.f; |
| 1001 |
float current_y = first.y + step_y / 2.f; |
| 1002 |
// It seems that for positive base_angle, |
| 1003 |
// 'true' is counter-clockwise |
| 1004 |
// and 'false is clockwise. |
| 1005 |
// For negative base_angle, it's the opposite. |
| 1006 |
bool arc_direction = true; |
| 1007 |
// for debugging |
| 1008 |
// float l_angle_one = 0, l_angle_two = 0; |
| 1009 |
|
| 1010 |
// Two core angles for semi-circles |
| 1011 |
float p1 = base_angle; |
| 1012 |
float p2 = base_angle - M_PI; |
| 1013 |
|
| 1014 |
float angle_one, angle_two; |
| 1015 |
|
| 1016 |
for (unsigned int i = 0; i < rounded_arcs_count; i++) { |
| 1017 |
float next_x = current_x + step_x; |
| 1018 |
float next_y = current_y + step_y; |
| 1019 |
|
| 1020 |
if(arc_direction) { |
| 1021 |
angle_one = p1; |
| 1022 |
angle_two = p2; |
| 1023 |
} else { |
| 1024 |
angle_one = p2; |
| 1025 |
angle_two = p1; |
| 1026 |
} |
| 1027 |
// l_angle_one = angle_one; |
| 1028 |
// l_angle_two = angle_two; |
| 1029 |
ren.new_sub_path(); |
| 1030 |
ren.arc(current_x, current_y, wave_arc_radius, angle_one, angle_two); |
| 1031 |
ren.stroke(); |
| 1032 |
|
| 1033 |
current_x = next_x; |
| 1034 |
current_y = next_y; |
| 1035 |
arc_direction = !arc_direction; |
| 1036 |
} |
| 1037 |
// Final part of the path. Truncated arc. |
| 1038 |
float partial_arc_proportion = arcs_count - (float) rounded_arcs_count; |
| 1039 |
float arccos_arg = 1.f - (partial_arc_proportion / WAVY_BOND_ARC_LENGTH / 2.f); |
| 1040 |
|
| 1041 |
// This is the angle for the final arc. |
| 1042 |
float theta = std::acos(arccos_arg); |
| 1043 |
|
| 1044 |
// The magic behind 'step_x > 0' |
| 1045 |
// is a bit of a mystery (derived empirically). |
| 1046 |
// There's certain correlation with the sign of base_angle |
| 1047 |
// but that's not the whole story. |
| 1048 |
// What matters is that it allows for differentiating |
| 1049 |
// between various cases, with angles from different quarters. |
| 1050 |
float starting_angle = step_x > 0 ? p2 : p1; |
| 1051 |
if(arc_direction) { |
| 1052 |
if(step_x > 0) { |
| 1053 |
angle_one = starting_angle - theta; |
| 1054 |
angle_two = starting_angle; |
| 1055 |
} else { |
| 1056 |
angle_one = starting_angle; |
| 1057 |
angle_two = starting_angle + theta; |
| 1058 |
} |
| 1059 |
} else { |
| 1060 |
if(step_x > 0) { |
| 1061 |
angle_one = starting_angle; |
| 1062 |
angle_two = starting_angle + theta; |
| 1063 |
} else { |
| 1064 |
angle_one = starting_angle - theta; |
| 1065 |
angle_two = starting_angle; |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
// debugging stuff |
| 1070 |
|
| 1071 |
// std::string case_info; |
| 1072 |
// if(theta > M_PI_2) { |
| 1073 |
// case_info += "T"; |
| 1074 |
// } else { |
| 1075 |
// case_info += "G"; |
| 1076 |
// } |
| 1077 |
// if(base_angle > 0) { |
| 1078 |
// case_info += "A"; |
| 1079 |
// } else { |
| 1080 |
// case_info += "E"; |
| 1081 |
// } |
| 1082 |
// if(arc_direction) { |
| 1083 |
// case_info += "K"; |
| 1084 |
// } else { |
| 1085 |
// case_info += "V"; |
| 1086 |
// } |
| 1087 |
// case_info += angle_two - angle_one > 0 ? "O" : "U"; |
| 1088 |
// if(step_x > 0) { |
| 1089 |
// case_info += "Z"; |
| 1090 |
// } else { |
| 1091 |
// case_info += "V"; |
| 1092 |
// } |
| 1093 |
// g_debug( |
| 1094 |
// "theta=%f, base_angle=%f a1=%f, a2=%f a2-a1=%f abs(a2-a1)=%f direction=%s p1=%f, p2=%f case_codename=%s", |
| 1095 |
// theta / M_PI * 180.f, |
| 1096 |
// base_angle / M_PI * 180.f, |
| 1097 |
// angle_one / M_PI * 180.f, |
| 1098 |
// angle_two / M_PI * 180.f, |
| 1099 |
// (angle_two - angle_one) / M_PI * 180.f, |
| 1100 |
// std::fabs(angle_two - angle_one) / M_PI * 180.f, |
| 1101 |
// arc_direction ? "true" : "false", |
| 1102 |
// l_angle_one / M_PI * 180.f, |
| 1103 |
// l_angle_two / M_PI * 180.f, |
| 1104 |
// case_info.c_str() |
| 1105 |
// ); |
| 1106 |
ren.new_sub_path(); |
| 1107 |
ren.arc(current_x, current_y, wave_arc_radius, angle_one, angle_two); |
| 1108 |
ren.stroke(); |
| 1109 |
} |
| 1110 |
|
| 1111 |
void MoleculeRenderContext::draw_side_bond_line(const CanvasMolecule::Bond& bond, bool addOrSub, std::optional<float> first_shortening_proportion, std::optional<float> second_shortening_proportion) { |
| 1112 |
auto [pv_x,pv_y] = bond.get_perpendicular_versor(); |
| 1113 |
if (!addOrSub) { // change sign of the versor |
| 1114 |
pv_x *= -1.f; |
| 1115 |
pv_y *= -1.f; |
| 1116 |
} |
| 1117 |
// Convert the versor to a vector of the desired length |
| 1118 |
pv_x *= CanvasMolecule::BOND_LINE_SEPARATION; |
| 1119 |
pv_y *= CanvasMolecule::BOND_LINE_SEPARATION; |
| 1120 |
|
| 1121 |
auto [bond_vec_x, bond_vec_y] = bond.get_vector(); |
| 1122 |
auto first_x = bond.first_atom_x; |
| 1123 |
auto second_x = bond.second_atom_x; |
| 1124 |
auto first_y = bond.first_atom_y; |
| 1125 |
auto second_y = bond.second_atom_y; |
| 1126 |
|
| 1127 |
if(first_shortening_proportion.has_value()) { |
| 1128 |
first_x += first_shortening_proportion.value() * bond_vec_x; |
| 1129 |
first_y += first_shortening_proportion.value() * bond_vec_y; |
| 1130 |
} |
| 1131 |
if(second_shortening_proportion.has_value()) { |
| 1132 |
second_x -= second_shortening_proportion.value() * bond_vec_x; |
| 1133 |
second_y -= second_shortening_proportion.value() * bond_vec_y; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// Points a and b represent the off-center bond before cropping. |
| 1137 |
graphene_point_t a; |
| 1138 |
a.x = (first_x + pv_x) * scale_factor + x_offset; |
| 1139 |
a.y = (first_y + pv_y) * scale_factor + y_offset; |
| 1140 |
graphene_point_t b; |
| 1141 |
b.x = (second_x + pv_x) * scale_factor + x_offset; |
| 1142 |
b.y = (second_y + pv_y) * scale_factor + y_offset; |
| 1143 |
|
| 1144 |
// We need to make sure that the off-center bond |
| 1145 |
// after cropping is not going to be longer than the center bond |
| 1146 |
graphene_point_t first_atom_centered; |
| 1147 |
first_atom_centered.x = first_x * scale_factor + x_offset; |
| 1148 |
first_atom_centered.y = first_y * scale_factor + y_offset; |
| 1149 |
|
| 1150 |
graphene_point_t second_atom_centered; |
| 1151 |
second_atom_centered.x = second_x * scale_factor + x_offset; |
| 1152 |
second_atom_centered.y = second_y * scale_factor + y_offset; |
| 1153 |
|
| 1154 |
/// Centered bond cropped |
| 1155 |
auto [first_c,second_c] = cropped_bond_coords( |
| 1156 |
first_atom_centered, |
| 1157 |
bond.first_atom_idx, |
| 1158 |
second_atom_centered, |
| 1159 |
bond.second_atom_idx |
| 1160 |
); |
| 1161 |
// Now we offset the center bond after cropping |
| 1162 |
first_c.x += pv_x * scale_factor; |
| 1163 |
first_c.y += pv_y * scale_factor; |
| 1164 |
second_c.x += pv_x * scale_factor; |
| 1165 |
second_c.y += pv_y * scale_factor; |
| 1166 |
|
| 1167 |
// Points a_cropped and b_cropped represent the off-center bond after cropping. |
| 1168 |
auto [a_cropped,b_cropped] = cropped_bond_coords(a,bond.first_atom_idx,b,bond.second_atom_idx); |
| 1169 |
|
| 1170 |
// Now we need to make sure that the off-center bond |
| 1171 |
// after cropping is not going to be longer than the center bond |
| 1172 |
if(bond_vec_x > 0) { |
| 1173 |
// The beginning is shorter for the centered bond |
| 1174 |
if(first_c.x > a_cropped.x) { |
| 1175 |
a_cropped = first_c; |
| 1176 |
} |
| 1177 |
// The end is shorter for the centered bond |
| 1178 |
if(second_c.x < b_cropped.x) { |
| 1179 |
b_cropped = second_c; |
| 1180 |
} |
| 1181 |
} else { |
| 1182 |
// The beginning is shorter for the centered bond |
| 1183 |
if(first_c.x < a_cropped.x) { |
| 1184 |
a_cropped = first_c; |
| 1185 |
} |
| 1186 |
// The end is shorter for the centered bond |
| 1187 |
if(second_c.x > b_cropped.x) { |
| 1188 |
b_cropped = second_c; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
if(bond_vec_y > 0) { |
| 1192 |
// The beginning is shorter for the centered bond |
| 1193 |
if(first_c.y > a_cropped.y) { |
| 1194 |
a_cropped = first_c; |
| 1195 |
} |
| 1196 |
// The end is shorter for the centered bond |
| 1197 |
if(second_c.y < b_cropped.y) { |
| 1198 |
b_cropped = second_c; |
| 1199 |
} |
| 1200 |
} else { |
| 1201 |
// The beginning is shorter for the centered bond |
| 1202 |
if(first_c.y < a_cropped.y) { |
| 1203 |
a_cropped = first_c; |
| 1204 |
} |
| 1205 |
// The end is shorter for the centered bond |
| 1206 |
if(second_c.y > b_cropped.y) { |
| 1207 |
b_cropped = second_c; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
ren.move_to(a_cropped.x, a_cropped.y); |
| 1211 |
ren.line_to(b_cropped.x, b_cropped.y); |
| 1212 |
ren.stroke(); |
| 1213 |
} |
| 1214 |
|
| 1215 |
void MoleculeRenderContext::draw_centered_double_bond(const CanvasMolecule::Bond& bond) { |
| 1216 |
auto [pv_x,pv_y] = bond.get_perpendicular_versor(); |
| 1217 |
|
| 1218 |
// Convert the versor to a vector of the desired length |
| 1219 |
pv_x *= CENTERED_DOUBLE_BOND_LINE_SEPARATION / 2.f * scale_factor; |
| 1220 |
pv_y *= CENTERED_DOUBLE_BOND_LINE_SEPARATION / 2.f * scale_factor; |
| 1221 |
|
| 1222 |
graphene_point_t first_atom; |
| 1223 |
first_atom.x = bond.first_atom_x * scale_factor + x_offset; |
| 1224 |
first_atom.y = bond.first_atom_y * scale_factor + y_offset; |
| 1225 |
|
| 1226 |
graphene_point_t second_atom; |
| 1227 |
second_atom.x = bond.second_atom_x * scale_factor + x_offset; |
| 1228 |
second_atom.y = bond.second_atom_y * scale_factor + y_offset; |
| 1229 |
|
| 1230 |
auto [first,second] = cropped_bond_coords(first_atom,bond.first_atom_idx,second_atom,bond.second_atom_idx); |
| 1231 |
|
| 1232 |
ren.move_to(first.x + pv_x, first.y + pv_y); |
| 1233 |
ren.line_to(second.x + pv_x, second.y + pv_y); |
| 1234 |
ren.stroke(); |
| 1235 |
|
| 1236 |
ren.move_to(first.x - pv_x, first.y - pv_y); |
| 1237 |
ren.line_to(second.x - pv_x, second.y - pv_y); |
| 1238 |
ren.stroke(); |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
void MoleculeRenderContext::draw_bonds() { |
| 1243 |
for(const auto& bond: canvas_molecule.bonds) { |
| 1244 |
auto highlight = CanvasMolecule::determine_dominant_highlight(bond->highlight); |
| 1245 |
if(highlight) { |
| 1246 |
ren.set_line_width(4.0); |
| 1247 |
auto [r, g, b] = CanvasMolecule::hightlight_to_rgb(*highlight); |
| 1248 |
ren.set_source_rgb(r, g, b); |
| 1249 |
} else { |
| 1250 |
ren.set_line_width(2.0); |
| 1251 |
ren.set_source_rgb(0.0, 0.0, 0.0); |
| 1252 |
} |
| 1253 |
|
| 1254 |
if(bond->geometry != BondGeometry::Flat && bond->type == BondType::Single) { |
| 1255 |
switch (bond->geometry) { |
| 1256 |
default: |
| 1257 |
case BondGeometry::Unspecified:{ |
| 1258 |
draw_wavy_bond(*bond.get()); |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
case BondGeometry::WedgeTowardsFirst:{ |
| 1262 |
draw_straight_wedge(*bond.get(), true); |
| 1263 |
g_warning_once("todo: rendering bond geometry in rings"); |
| 1264 |
break; |
| 1265 |
} |
| 1266 |
case BondGeometry::WedgeTowardsSecond:{ |
| 1267 |
draw_straight_wedge(*bond.get(), false); |
| 1268 |
g_warning_once("todo: rendering bond geometry in rings"); |
| 1269 |
break; |
| 1270 |
} |
| 1271 |
case BondGeometry::DashedTowardsFirst:{ |
| 1272 |
draw_straight_dashed_bond(*bond.get(), true); |
| 1273 |
g_warning_once("todo: rendering bond geometry in rings"); |
| 1274 |
break; |
| 1275 |
} |
| 1276 |
case BondGeometry::DashedTowardsSecond:{ |
| 1277 |
draw_straight_dashed_bond(*bond.get(), false); |
| 1278 |
g_warning_once("todo: rendering bond geometry in rings"); |
| 1279 |
break; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} else { |
| 1283 |
switch(bond->type) { |
| 1284 |
case BondType::Double:{ |
| 1285 |
DoubleBondDrawingDirection direction = |
| 1286 |
bond->bond_drawing_direction.has_value() ? |
| 1287 |
bond->bond_drawing_direction.value() |
| 1288 |
: DoubleBondDrawingDirection::Primary; |
| 1289 |
bool direction_as_bool = true; |
| 1290 |
|
| 1291 |
switch (direction) { |
| 1292 |
case DoubleBondDrawingDirection::Secondary:{ |
| 1293 |
direction_as_bool = false; |
| 1294 |
// no break here. |
| 1295 |
} |
| 1296 |
case DoubleBondDrawingDirection::Primary:{ |
| 1297 |
draw_central_bond_line(*bond.get()); |
| 1298 |
draw_side_bond_line( |
| 1299 |
*bond.get(), |
| 1300 |
direction_as_bool, |
| 1301 |
bond->first_shortening_proportion, |
| 1302 |
bond->second_shortening_proportion |
| 1303 |
); |
| 1304 |
break; |
| 1305 |
} |
| 1306 |
case DoubleBondDrawingDirection::Centered:{ |
| 1307 |
draw_centered_double_bond(*bond.get()); |
| 1308 |
break; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
break; |
| 1312 |
} |
| 1313 |
case BondType::Triple:{ |
| 1314 |
draw_central_bond_line(*bond.get()); |
| 1315 |
g_warning_once("todo: Triple bonds might need truncating too."); |
| 1316 |
// "to the left" |
| 1317 |
draw_side_bond_line(*bond.get(), false, std::nullopt,std::nullopt); |
| 1318 |
// "to the right" |
| 1319 |
draw_side_bond_line(*bond.get(), true, std::nullopt,std::nullopt); |
| 1320 |
break; |
| 1321 |
} |
| 1322 |
default: |
| 1323 |
case BondType::Single:{ |
| 1324 |
draw_central_bond_line(*bond.get()); |
| 1325 |
break; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |