File: DrawTextNotFT.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (88 lines) | stat: -rw-r--r-- 3,130 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
//  Copyright (C) 2022 David Cosgrove and other RDKit contributors
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
//
// Original author: David Cosgrove (CozChemIx).
//

#include <GraphMol/MolDraw2D/DrawTextNotFT.h>
#include <GraphMol/MolDraw2D/MolDraw2DHelpers.h>

namespace RDKit {
namespace MolDraw2D_detail {
// ****************************************************************************
DrawTextNotFT::DrawTextNotFT(double max_fnt_sz, double min_fnt_sz)
    : DrawText(max_fnt_sz, min_fnt_sz) {}

// ****************************************************************************
DrawTextNotFT::~DrawTextNotFT() {}

// ****************************************************************************
void DrawTextNotFT::alignString(
    TextAlignType talign, const std::vector<TextDrawType> &draw_modes,
    std::vector<std::shared_ptr<StringRect>> &rects) const {
  // std::string comes in with rects aligned with first char with its
  // left hand and bottom edges at 0 on y and x respectively.
  // Adjust relative to that so that the relative alignment point is at
  // (0,0).
  if (talign == TextAlignType::MIDDLE) {
    size_t num_norm = count(draw_modes.begin(), draw_modes.end(),
                            TextDrawType::TextDrawNormal);
    if (num_norm == 1) {
      talign = TextAlignType::START;
    }
  }

  Point2D align_trans, align_offset;
  if (talign == TextAlignType::START || talign == TextAlignType::END) {
    size_t align_char = 0;
    for (size_t i = 0; i < rects.size(); ++i) {
      if (draw_modes[i] == TextDrawType::TextDrawNormal) {
        align_char = i;
        if (talign == TextAlignType::START) {
          break;
        }
      }
    }
    align_trans = rects[align_char]->trans_;
    align_offset = rects[align_char]->offset_;
  } else {
    // centre on the middle of the Normal text.  The super- or subscripts
    // should be at the ends.
    double x_min = std::numeric_limits<double>::max();
    double x_max = std::numeric_limits<double>::lowest();
    align_offset.x = align_offset.y = 0.0;
    int num_norm = 0;
    for (size_t i = 0; i < rects.size(); ++i) {
      if (draw_modes[i] == TextDrawType::TextDrawNormal) {
        Point2D tl, tr, br, bl;
        rects[i]->calcCorners(tl, tr, br, bl, 0.0);
        // sometimes the rect is in a coordinate frame where +ve y is down,
        // sometimes it's up.  For these purposes, we don't care so long as
        // the y_max is larger than the y_min.  We probably don't need to do
        // all the tests for x_min and x_max;
        x_min = std::min({bl.x, tr.x, x_min});
        x_max = std::max({bl.x, tr.x, x_max});
        align_offset += rects[i]->offset_;
        ++num_norm;
      }
    }
    align_trans.x = (x_max - x_min) / 2.0;
    align_trans.y = 0.0;
    align_offset /= num_norm;
  }

  for (auto r : rects) {
    r->trans_ -= align_trans;
    r->offset_ = align_offset;
  }
}

}  // namespace MolDraw2D_detail
}  // namespace RDKit