wxMaxima
Loading...
Searching...
No Matches
TextStyle.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4// Copyright (C) 2020 Kuba Ober <kuba@mareimbrium.org>
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 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20//
21// SPDX-License-Identifier: GPL-2.0+
22
23#ifndef TEXTSTYLE_H
24#define TEXTSTYLE_H
25
26#include "precomp.h"
27
33#include <wx/colour.h>
34#include <map>
35#include <wx/hashmap.h>
36#include <string>
37#include <ostream>
38#include <wx/config.h>
39#include <wx/font.h>
40#include <wx/settings.h>
41#include <cstdint>
42#include <functional>
43#include "Compat.h" // wxWARN_UNUSED
44#include "FontAttribs.h"
45#include "FontVariantCache.h"
46#include <unordered_map>
47
49static constexpr uint32_t MAKE_RGB(uint32_t r, uint32_t g, uint32_t b)
50{ return (0xFF & r) | ((0xFF & g) << 8) | ((0xFF & b) << 16); }
51
63// A style is a value: constructing one and never asking it anything is dead
64// code. Its user-provided copy constructor makes it non-trivial, which is
65// exactly the case plain -Wunused-variable stops warning about.
67{
68public:
69 Style();
70 explicit Style(AFontSize fontSize);
71 Style(const Style &);
72
73 Style &operator=(const Style &);
75 bool operator==(const Style &o) const;
76
82 Style &Read(wxConfigBase *config, const wxString &where);
84 void Write(wxConfigBase *config, const wxString &where) const;
85
87 static const Style &FromFont(const wxFont &font);
89 static Style FromStockFont(wxStockGDI::Item font);
90
91 constexpr static wxFontFamily Default_Family{wxFONTFAMILY_DEFAULT};
92 constexpr static wxFontEncoding Default_Encoding{wxFONTENCODING_DEFAULT};
93 constexpr static wxFontWeight Default_Weight{wxFONTWEIGHT_NORMAL};
94 constexpr static wxFontStyle Default_FontStyle{wxFONTSTYLE_NORMAL};
95 constexpr static bool Default_Underlined{false};
96 constexpr static bool Default_Strikethrough{false};
97 constexpr static AFontSize Default_FontSize{10.0f};
98 constexpr static uint32_t Default_ColorRGB{MAKE_RGB(0, 0, 0)};
99 static const wxColor &Default_Color();
100
101 wxFontFamily GetFamily() const;
102 wxFontEncoding GetEncoding() const;
103 wxFontWeight GetWeight() const;
104 bool IsBold() const { return GetWeight() == wxFONTWEIGHT_BOLD; }
105 bool IsLight() const { return GetWeight() == wxFONTWEIGHT_LIGHT; }
106 wxFontStyle GetFontStyle() const;
107 bool IsItalic() const { return GetFontStyle() == wxFONTSTYLE_ITALIC; }
108 bool IsSlant() const { return GetFontStyle() == wxFONTSTYLE_SLANT; }
109 bool IsUnderlined() const;
110 bool IsStrikethrough() const;
111 const wxString &GetFontName() const;
112 AFontSize GetFontSize() const;
113 uint32_t GetRGBColor() const;
114 wxColor GetColor() const { return wxColor(GetRGBColor()); }
115
116 using did_change = bool;
117 did_change SetFamily(wxFontFamily family);
118 did_change SetEncoding(wxFontEncoding encoding);
119 did_change SetWeight(wxFontWeight weight);
120 did_change SetBold(bool bold = true);
121 did_change SetLight(bool light = true);
122 did_change SetFontStyle(wxFontStyle style);
123 did_change SetItalic(bool italic = true);
124 did_change SetSlant(bool slant = true);
125 did_change SetUnderlined(bool underlined = true);
126 did_change SetStrikethrough(bool strikethrough = true);
127 did_change SetFontName(wxString fontName);
128 did_change SetFontSize(AFontSize fontSize);
129 did_change SetRGBColor(uint32_t rgb);
130 did_change SetColor(const wxColor &color);
131 did_change SetColor(wxSystemColour sysColour);
132
133 Style& Family(wxFontFamily family) { return SetFamily(family), *this; }
134 Style& Encoding(wxFontEncoding encoding) { return SetEncoding(encoding), *this; }
135 Style& Weight(wxFontWeight weight) { return SetWeight(weight), *this; }
136 Style& FontStyle(wxFontStyle style) { return SetFontStyle(style), *this; }
137 Style& Bold(bool bold = true) { return SetBold(bold), *this; }
138 Style& Light(bool light = true) { return SetLight(light), *this; }
139 Style& Italic(bool italic = true) { return SetItalic(italic), *this; }
140 Style& Slant(bool slant = true) { return SetSlant(slant), *this; }
141 Style& Underlined(bool underlined = true) { return SetUnderlined(underlined), *this; }
142 Style& Strikethrough(bool strikethrough = true) { return SetStrikethrough(strikethrough), *this; }
143 Style& FontSize(float size) { return SetFontSize(AFontSize(size)), *this; }
144 Style& FontSize(AFontSize fontSize) { return SetFontSize(fontSize), *this; }
145 Style& RGBColor(uint32_t rgb) { return SetRGBColor(rgb), *this; }
146 Style& Color(const wxColor &color) { return SetColor(color), *this; }
147 Style& Color(uint8_t r, uint8_t g, uint8_t b) { return SetColor({r, g, b}), *this; }
148 Style& Color(wxSystemColour sysColour) { return SetColor(sysColour), *this; }
149 Style& ChangeLightness(int alpha) { return SetColor(GetColor().ChangeLightness(alpha)), *this; }
150
151 bool CantChangeFontName() const {return m.cantChangeFontName;}
152 bool CantChangeFontVariant() const {return m.cantChangeFontVariant;}
153 void CantChangeFontName(bool changeForbidden) {m.cantChangeFontName = changeForbidden;}
154 void CantChangeFontVariant(bool changeForbidden) {m.cantChangeFontVariant = changeForbidden;}
155 wxFontInfo GetAsFontInfo() const;
156
157
158 bool IsFontOk() const;
160 const wxFont &GetFont(AFontSize fontSize) const;
162 const wxFont &GetFont() const {
163 return GetFont(GetFontSize());
164 }
165
167 did_change SetFromFont(const wxFont&);
169 did_change SetFontFrom(const Style&);
171 did_change SetFontFaceFrom(const Style&);
173 did_change SetFontFaceAndSizeFrom(const Style&);
175 constexpr static bool IsFractionalFontSizeSupported() {
176 return wxCHECK_VERSION(3, 1, 2); } //-V686 //-V501
178 static AFontSize GetFontSize(const wxFont &);
180 static void SetFontSize(wxFont &, AFontSize fontSize);
182 void ClearCache() const
183 {
184 if(m.fontCache)
185 m.fontCache->ClearCache();
186 }
187 std::shared_ptr<FontVariantCache> GetFontCache() const {return m.fontCache;}
188private:
189 typedef std::unordered_map <wxString, std::shared_ptr<FontVariantCache>,
190 wxStringHash> FontVariantCachesMap;
191
193 static wxString m_emptyString;
195 static FontVariantCachesMap m_fontCaches;
196
198 struct Data
199 {
200 // 8/4-byte members
206 mutable std::shared_ptr<FontVariantCache> fontCache;
207 // 4-byte members
208 uint32_t rgbColor = Default_ColorRGB;
209 // 2-byte members
210 AFontSize fontSize = Default_FontSize;
211 AFontFamily family = Default_Family;
212 AFontEncoding encoding = Default_Encoding;
213 AFontWeight weight = Default_Weight;
214 AFontStyle fontStyle = Default_FontStyle;
215 // 1-byte members
216 bool underlined : 1;
217 bool strikethrough : 1;
218 bool cantChangeFontName : 1; // !< Allow to change only color, underline etc.
219 bool cantChangeFontVariant : 1; // !< Allow to change only color
220
221 Data() : underlined(false), strikethrough(false), cantChangeFontName(false),
222 cantChangeFontVariant(false) {}
223 } m;
224
225 // cppcheck-suppress noExplicitConstructor
226};
227
228
234enum TextStyle : int8_t
235{
236 TS_CODE_DEFAULT , //<! The font code uses by default
237 TS_CODE_VARIABLE ,
238 TS_CODE_FUNCTION ,
239 TS_CODE_COMMENT ,
240 TS_CODE_NUMBER ,
241 TS_CODE_STRING ,
242 TS_CODE_OPERATOR ,
243 TS_CODE_LISP ,
244 TS_CODE_ENDOFLINE ,
245 TS_ASCIIMATHS ,
246 TS_MATH ,
247 TS_TEXT ,
248 TS_VARIABLE ,
249 TS_OPERATOR ,
250 TS_NUMBER ,
251 TS_FUNCTION ,
252 TS_SPECIAL_CONSTANT ,
253 TS_GREEK_CONSTANT ,
254 TS_STRING ,
255 TS_OUTDATED ,
256 TS_MAIN_PROMPT ,
257 TS_OTHER_PROMPT ,
258 TS_LABEL ,
259 TS_USERLABEL ,
260 TS_HIGHLIGHT ,
261 TS_WARNING ,
262 TS_ERROR ,
263 TS_TITLE ,
264 TS_SECTION ,
265 TS_SUBSECTION ,
266 TS_SUBSUBSECTION ,
267 TS_HEADING5 ,
268 TS_HEADING6 ,
269 TS_TEXT_BACKGROUND ,
270 TS_DOCUMENT_BACKGROUND,
271 TS_CELL_BRACKET ,
272 TS_ACTIVE_CELL_BRACKET,
273 TS_CURSOR ,
274 TS_SELECTION ,
275 TS_EQUALSSELECTION ,
280
282std::ostream& operator<<(std::ostream& out, const TextStyle textstyle);
283
284#endif // TEXTSTYLE_H
Small compatibility shims for older toolchains and older wxWidgets.
#define wxWARN_UNUSED
Marks a class whose unused instances are a bug.
Definition: Compat.h:116
This file implements the wxFont cache system.
std::ostream & operator<<(std::ostream &out, const TextStyle textstyle)
Allow Standard c++ streams to print out our enum values as text.
Definition: TextStyle.cpp:399
TextStyle
All text styles known to wxMaxima.
Definition: TextStyle.h:235
@ TS_INVALID
If a text style cannot be determined this value is used.
Definition: TextStyle.h:278
@ NUMBEROFSTYLES
This is not a style, but its value tells us how many styles are defined.
Definition: TextStyle.h:277
@ TS_DIFF_CHANGED
Background of text the diff viewer's counterpart cell lacks.
Definition: TextStyle.h:276
A Type-Safe Fixed-Point Font Size.
Definition: FontAttribs.h:99
Text Style Definition.
Definition: TextStyle.h:67
void ClearCache() const
Empties the font variant cache.
Definition: TextStyle.h:182
bool operator==(const Style &o) const
Compares.
static constexpr bool IsFractionalFontSizeSupported()
Old wxWidgets versions only support integers as font sizes.
Definition: TextStyle.h:175
static const Style & FromFont(const wxFont &font)
Gets a style that represents a given font. The font gets cached.
const wxFont & GetFont() const
Returns the font associated with this style.
Definition: TextStyle.h:162