Power System Platform  2026w10a-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::SetFont(wxFont font)
122{
123 m_font = font;
124 m_customFont = true;
125}
126
127//wxSize GCText::CalculateTextExtend()
128//{
129// // DC offscreen
130// wxBitmap bmp(1, 1);
131// wxMemoryDC mdc(bmp);
132//
133// mdc.SetFont(m_font);
134//
135// wxGCDC dc(mdc);
136// dc.SetFont(m_font);
137//
138// wxCoord w, h;
139// dc.GetTextExtent(m_text, &w, &h);
140//
141// wxArrayInt partial;
142// dc.GetPartialTextExtents(m_text, partial);
143//
144// int leftOffset = 0;
145//
146// if (!m_text.empty() && partial.size() >= 1)
147// {
148// wxCoord firstGlyphW, firstGlyphH;
149// dc.GetTextExtent(m_text.Mid(0, 1), &firstGlyphW, &firstGlyphH);
150//
151// int advanceFirst = partial[0];
152//
153// // se o glifo invade a esquerda → bearing negativo
154// leftOffset = firstGlyphW - advanceFirst;
155// }
156//
157// return wxSize(w + leftOffset, h);
158//}
159
160
162{
163 GCText* copy = new GCText();
164 *copy = *this;
165
166 copy->SetText(copy->m_text);
167 return copy;
168}
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual GCText * GetCopy()
Get a deep text copy.
Definition GCText.cpp:161
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