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 &);
74
80 Style &Read(wxConfigBase *config, const wxString &where);
82 void Write(wxConfigBase *config, const wxString &where) const;
83
85 static const Style &FromFont(const wxFont &font);
87 static Style FromStockFont(wxStockGDI::Item font);
88
89 constexpr static wxFontFamily Default_Family{wxFONTFAMILY_DEFAULT};
90 constexpr static wxFontEncoding Default_Encoding{wxFONTENCODING_DEFAULT};
91 constexpr static wxFontWeight Default_Weight{wxFONTWEIGHT_NORMAL};
92 constexpr static wxFontStyle Default_FontStyle{wxFONTSTYLE_NORMAL};
93 constexpr static bool Default_Underlined{false};
94 constexpr static bool Default_Strikethrough{false};
95 constexpr static AFontSize Default_FontSize{10.0f};
96 constexpr static uint32_t Default_ColorRGB{MAKE_RGB(0, 0, 0)};
97 static const wxColor &Default_Color();
98
99 wxFontFamily GetFamily() const;
100 wxFontEncoding GetEncoding() const;
101 wxFontWeight GetWeight() const;
102 bool IsBold() const { return GetWeight() == wxFONTWEIGHT_BOLD; }
103 bool IsLight() const { return GetWeight() == wxFONTWEIGHT_LIGHT; }
104 wxFontStyle GetFontStyle() const;
105 bool IsItalic() const { return GetFontStyle() == wxFONTSTYLE_ITALIC; }
106 bool IsSlant() const { return GetFontStyle() == wxFONTSTYLE_SLANT; }
107 bool IsUnderlined() const;
108 bool IsStrikethrough() const;
109 const wxString &GetFontName() const;
110 AFontSize GetFontSize() const;
111 uint32_t GetRGBColor() const;
112 wxColor GetColor() const { return wxColor(GetRGBColor()); }
113
114 using did_change = bool;
115 did_change SetFamily(wxFontFamily family);
116 did_change SetEncoding(wxFontEncoding encoding);
117 did_change SetWeight(wxFontWeight weight);
118 did_change SetBold(bool bold = true);
119 did_change SetLight(bool light = true);
120 did_change SetFontStyle(wxFontStyle style);
121 did_change SetItalic(bool italic = true);
122 did_change SetSlant(bool slant = true);
123 did_change SetUnderlined(bool underlined = true);
124 did_change SetStrikethrough(bool strikethrough = true);
125 did_change SetFontName(wxString fontName);
126 did_change SetFontSize(AFontSize fontSize);
127 did_change SetRGBColor(uint32_t rgb);
128 did_change SetColor(const wxColor &color);
129 did_change SetColor(wxSystemColour sysColour);
130
131 Style& Family(wxFontFamily family) { return SetFamily(family), *this; }
132 Style& Encoding(wxFontEncoding encoding) { return SetEncoding(encoding), *this; }
133 Style& Weight(wxFontWeight weight) { return SetWeight(weight), *this; }
134 Style& FontStyle(wxFontStyle style) { return SetFontStyle(style), *this; }
135 Style& Bold(bool bold = true) { return SetBold(bold), *this; }
136 Style& Light(bool light = true) { return SetLight(light), *this; }
137 Style& Italic(bool italic = true) { return SetItalic(italic), *this; }
138 Style& Slant(bool slant = true) { return SetSlant(slant), *this; }
139 Style& Underlined(bool underlined = true) { return SetUnderlined(underlined), *this; }
140 Style& Strikethrough(bool strikethrough = true) { return SetStrikethrough(strikethrough), *this; }
141 Style& FontSize(float size) { return SetFontSize(AFontSize(size)), *this; }
142 Style& FontSize(AFontSize fontSize) { return SetFontSize(fontSize), *this; }
143 Style& RGBColor(uint32_t rgb) { return SetRGBColor(rgb), *this; }
144 Style& Color(const wxColor &color) { return SetColor(color), *this; }
145 Style& Color(uint8_t r, uint8_t g, uint8_t b) { return SetColor({r, g, b}), *this; }
146 Style& Color(wxSystemColour sysColour) { return SetColor(sysColour), *this; }
147 Style& ChangeLightness(int alpha) { return SetColor(GetColor().ChangeLightness(alpha)), *this; }
148
149 bool CantChangeFontName() const {return m.cantChangeFontName;}
150 bool CantChangeFontVariant() const {return m.cantChangeFontVariant;}
151 void CantChangeFontName(bool changeForbidden) {m.cantChangeFontName = changeForbidden;}
152 void CantChangeFontVariant(bool changeForbidden) {m.cantChangeFontVariant = changeForbidden;}
153 wxFontInfo GetAsFontInfo() const;
154
155
156 bool IsFontOk() const;
158 const wxFont &GetFont(AFontSize fontSize) const;
160 const wxFont &GetFont() const {
161 return GetFont(GetFontSize());
162 }
163
165 did_change SetFromFont(const wxFont&);
167 did_change SetFontFrom(const Style&);
169 did_change SetFontFaceFrom(const Style&);
171 did_change SetFontFaceAndSizeFrom(const Style&);
173 static AFontSize GetFontSize(const wxFont &);
175 static void SetFontSize(wxFont &, AFontSize fontSize);
177 void ClearCache() const
178 {
179 if(m.fontCache)
180 m.fontCache->ClearCache();
181 }
182 std::shared_ptr<FontVariantCache> GetFontCache() const {return m.fontCache;}
183private:
184 using FontVariantCachesMap =
185 std::unordered_map<wxString, std::shared_ptr<FontVariantCache>, wxStringHash>;
186
188 static wxString m_emptyString;
190 static FontVariantCachesMap m_fontCaches;
191
193 struct Data
194 {
195 // 8/4-byte members
201 mutable std::shared_ptr<FontVariantCache> fontCache;
202 // 4-byte members
203 uint32_t rgbColor = Default_ColorRGB;
204 // 2-byte members
205 AFontSize fontSize = Default_FontSize;
206 AFontFamily family = Default_Family;
207 AFontEncoding encoding = Default_Encoding;
208 AFontWeight weight = Default_Weight;
209 AFontStyle fontStyle = Default_FontStyle;
210 // 1-byte members
211 bool underlined : 1;
212 bool strikethrough : 1;
213 bool cantChangeFontName : 1; // !< Allow to change only color, underline etc.
214 bool cantChangeFontVariant : 1; // !< Allow to change only color
215
216 Data() : underlined(false), strikethrough(false), cantChangeFontName(false),
217 cantChangeFontVariant(false) {}
218 } m;
219
220 // cppcheck-suppress noExplicitConstructor
221};
222
223
229enum TextStyle : int8_t
230{
231 TS_CODE_DEFAULT , //<! The font code uses by default
232 TS_CODE_VARIABLE ,
233 TS_CODE_FUNCTION ,
234 TS_CODE_COMMENT ,
235 TS_CODE_NUMBER ,
236 TS_CODE_STRING ,
237 TS_CODE_OPERATOR ,
238 TS_CODE_LISP ,
239 TS_CODE_ENDOFLINE ,
240 TS_ASCIIMATHS ,
241 TS_MATH ,
242 TS_TEXT ,
243 TS_VARIABLE ,
244 TS_OPERATOR ,
245 TS_NUMBER ,
246 TS_FUNCTION ,
247 TS_SPECIAL_CONSTANT ,
248 TS_GREEK_CONSTANT ,
249 TS_STRING ,
250 TS_OUTDATED ,
251 TS_MAIN_PROMPT ,
252 TS_OTHER_PROMPT ,
253 TS_LABEL ,
254 TS_USERLABEL ,
255 TS_HIGHLIGHT ,
256 TS_WARNING ,
257 TS_ERROR ,
258 TS_TITLE ,
259 TS_SECTION ,
260 TS_SUBSECTION ,
261 TS_SUBSUBSECTION ,
262 TS_HEADING5 ,
263 TS_HEADING6 ,
264 TS_TEXT_BACKGROUND ,
265 TS_DOCUMENT_BACKGROUND,
266 TS_CELL_BRACKET ,
267 TS_ACTIVE_CELL_BRACKET,
268 TS_CURSOR ,
269 TS_SELECTION ,
270 TS_EQUALSSELECTION ,
276
278std::ostream& operator<<(std::ostream& out, const TextStyle textstyle);
279
280#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:382
TextStyle
All text styles known to wxMaxima.
Definition: TextStyle.h:230
@ TS_INVALID
If a text style cannot be determined this value is used.
Definition: TextStyle.h:274
@ NUMBEROFSTYLES
This is not a style, but its value tells us how many styles are defined.
Definition: TextStyle.h:273
@ TS_LINK
Links in text cells (GH #2396)
Definition: TextStyle.h:272
@ TS_DIFF_CHANGED
Background of text the diff viewer's counterpart cell lacks.
Definition: TextStyle.h:271
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:177
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:160