1 //
2 // @@ All Rights Reserved @@
3 // This file is part of the RDKit.
4 // The contents are covered by the terms of the BSD license
5 // which is included in the file license.txt, found at the root
6 // of the RDKit source tree.
7 //
8 // Original author: David Cosgrove (CozChemIx) on 29/04/2020.
9 //
10
11 #include <GraphMol/MolDraw2D/DrawTextCairo.h>
12 #include <GraphMol/MolDraw2D/MolDraw2D.h>
13
14 using namespace std;
15
16 namespace RDKit {
17
18 // ****************************************************************************
DrawTextCairo(double max_fnt_sz,double min_fnt_sz,cairo_t * dp_cr)19 DrawTextCairo::DrawTextCairo(double max_fnt_sz, double min_fnt_sz,
20 cairo_t *dp_cr)
21 : DrawText(max_fnt_sz, min_fnt_sz), dp_cr_(dp_cr) {
22 cairo_select_font_face(dp_cr, "sans", CAIRO_FONT_SLANT_NORMAL,
23 CAIRO_FONT_WEIGHT_NORMAL);
24 cairo_set_font_size(dp_cr, fontSize());
25 }
26
27 // ****************************************************************************
28 // draw the char, with the bottom left hand corner at cds
drawChar(char c,const Point2D & cds)29 void DrawTextCairo::drawChar(char c, const Point2D &cds) {
30 PRECONDITION(dp_cr_, "no draw context");
31
32 cairo_set_font_size(dp_cr_, fontSize());
33 DrawColour col = colour();
34 cairo_set_source_rgb(dp_cr_, col.r, col.g, col.b);
35
36 char txt[2];
37 txt[0] = c;
38 txt[1] = 0;
39 cairo_move_to(dp_cr_, cds.x, cds.y);
40 cairo_show_text(dp_cr_, txt);
41 cairo_stroke(dp_cr_);
42 }
43
44 // ****************************************************************************
getStringRects(const string & text,vector<shared_ptr<StringRect>> & rects,vector<TextDrawType> & draw_modes,vector<char> & draw_chars) const45 void DrawTextCairo::getStringRects(const string &text,
46 vector<shared_ptr<StringRect>> &rects,
47 vector<TextDrawType> &draw_modes,
48 vector<char> &draw_chars) const {
49 TextDrawType draw_mode = TextDrawType::TextDrawNormal;
50 double running_x = 0.0;
51 char char_str[2];
52 char_str[1] = 0;
53 double max_y = 0.0;
54 double full_fs = fontSize();
55 for (size_t i = 0; i < text.length(); ++i) {
56 // setStringDrawMode moves i along to the end of any <sub> or <sup>
57 // markup
58 if ('<' == text[i] && setStringDrawMode(text, draw_mode, i)) {
59 continue;
60 }
61 draw_chars.push_back(text[i]);
62
63 char_str[0] = text[i];
64 cairo_text_extents_t extents;
65 cairo_set_font_size(dp_cr_,
66 selectScaleFactor(text[i], draw_mode) * full_fs);
67 cairo_text_extents(dp_cr_, char_str, &extents);
68 cairo_set_font_size(dp_cr_, full_fs);
69 double twidth = extents.width;
70 double theight = extents.height;
71 Point2D offset(extents.x_bearing + twidth / 2.0, -extents.y_bearing / 2.0);
72 Point2D g_centre(offset.x, -extents.y_bearing - theight / 2.0);
73 rects.push_back(shared_ptr<StringRect>(
74 new StringRect(offset, g_centre, twidth, theight)));
75 rects.back()->trans_.x = running_x;
76 draw_modes.push_back(draw_mode);
77 running_x += extents.x_advance;
78 max_y = max(max_y, -extents.y_bearing);
79 }
80 for (auto r : rects) {
81 r->g_centre_.y = max_y - r->g_centre_.y;
82 r->offset_.y = max_y / 2.0;
83 }
84
85 adjustStringRectsForSuperSubScript(draw_modes, rects);
86 }
87
88 } // namespace RDKit
89