Power System Platform  2026w11a-beta
Loading...
Searching...
No Matches
GCText.cpp
1/*
2 * Copyright (C) 2024 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "GCText.h"
19#include <wx/dcscreen.h>
20#include <wx/dcmemory.h>
21#include <wx/dcgraph.h>
22#include <wx/dcclient.h>
23#include <wx/graphics.h>
24
25GCText::GCText() {}
26GCText::GCText(wxString text)
27{
28 SetText(text);
29}
30
31GCText::~GCText()
32{
33}
34
35void GCText::Draw(wxPoint2DDouble position, wxGraphicsContext* gc, double angle, wxColour colour) const
36{
37 //gc->SetFont(wxFont(m_fontSize, m_fontFamily, m_fontStyle, m_fontWeight), colour);
38
39 gc->SetFont(m_font, colour);
40 gc->DrawText(m_text, position.m_x, position.m_y, angle);
41}
42
43void GCText::Draw(wxPoint2DDouble position, double width, double height, wxDC& dc, double angle, wxColour colour) const
44{
45 dc.SetFont(m_font);
46 dc.SetPen(wxPen(colour));
47 dc.SetTextForeground(colour);
48
49 if(angle == 0.0) {
50 dc.DrawText(m_text, position.m_x - width / 2.0, position.m_y - height / 2.0);
51 return;
52 }
53
54 double rad = wxDegToRad(angle);
55
56 double dx = -width / 2.0;
57 double dy = -height / 2.0;
58
59 double rx = dx * cos(rad) - dy * sin(rad);
60 double ry = dx * sin(rad) + dy * cos(rad);
61
62 double drawX = position.m_x + rx;
63 double drawY = position.m_y + ry;
64
65 dc.DrawRotatedText(m_text, drawX, drawY, -angle);
66}
67
68void GCText::SetText(wxString text)
69{
70 m_text = text;
71 if (!m_customFont) {
72 //m_font.SetFaceName(wxT("CMU Serif"));
73 //m_font.SetPointSize(m_fontSize);
74 m_font = wxFont(m_fontSize,
75 wxFONTFAMILY_DEFAULT,
76 wxFONTSTYLE_NORMAL,
77 wxFONTWEIGHT_NORMAL,
78 false,
79 wxT("Arial"));
80 }
81
82 //wxFont font = wxFont(m_fontSize, m_fontFamily, m_fontStyle, m_fontWeight);
83 //wxBitmap bmp(1, 1);
84 //wxMemoryDC mdc(bmp);
85 //
86 //wxGraphicsContext* gc = wxGraphicsContext::Create(mdc);
87 //
88 //gc->SetFont(m_font, *wxBLACK);
89 //
90 //wxDouble w, h, descent, extLead;
91 //gc->GetTextExtent(text, &w, &h, &descent, &extLead);
92 //
93 //delete gc;
94 //
95 //m_size = wxSize((int)w, (int)h);
96
97 wxScreenDC dc;
98 dc.SetFont(m_font);
99 m_size = dc.GetTextExtent(m_text);
100
101 //m_size = CalculateTextExtend();
102
103
104 //wxMemoryDC memDC;
106 //
107 //wxGraphicsContext* gc = wxGraphicsContext::Create(memDC);
108 //if (gc) {
109 // gc->SetFont(m_font, *wxBLACK);
110 // double width, height, descent, externalLeading;
111 // gc->GetTextExtent(m_text, &width, &height, &descent, &externalLeading);
112 // m_size = wxSize(std::ceil(width + descent), std::ceil(height + externalLeading));
113 // delete gc;
114 //}
115 //else {
116 // memDC.SetFont(m_font);
117 // m_size = memDC.GetTextExtent(m_text);
118 //}
119}
120
121void GCText::SetText(wxString text, wxSize size)
122{
123 m_text = text;
124 m_size = size;
125}
126
127void GCText::SetFont(wxFont font)
128{
129 m_font = font;
130 m_customFont = true;
131}
132
133//wxSize GCText::CalculateTextExtend()
134//{
135// // DC offscreen
136// wxBitmap bmp(1, 1);
137// wxMemoryDC mdc(bmp);
138//
139// mdc.SetFont(m_font);
140//
141// wxGCDC dc(mdc);
142// dc.SetFont(m_font);
143//
144// wxCoord w, h;
145// dc.GetTextExtent(m_text, &w, &h);
146//
147// wxArrayInt partial;
148// dc.GetPartialTextExtents(m_text, partial);
149//
150// int leftOffset = 0;
151//
152// if (!m_text.empty() && partial.size() >= 1)
153// {
154// wxCoord firstGlyphW, firstGlyphH;
155// dc.GetTextExtent(m_text.Mid(0, 1), &firstGlyphW, &firstGlyphH);
156//
157// int advanceFirst = partial[0];
158//
159// // se o glifo invade a esquerda → bearing negativo
160// leftOffset = firstGlyphW - advanceFirst;
161// }
162//
163// return wxSize(w + leftOffset, h);
164//}
165
166
168{
169 GCText* copy = new GCText();
170 *copy = *this;
171 return copy;
172}
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual GCText * GetCopy()
Get a deep text copy.
Definition GCText.cpp:167
virtual void Draw(wxPoint2DDouble position, wxGraphicsContext *gc, double angle=0.0, wxColour colour= *wxBLACK) const
Draw the text in wxGraphicsContext.
Definition GCText.cpp:35
virtual void SetText(wxString text)
Set correctly a new text string.
Definition GCText.cpp:68