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 "FontAttribs.h"
44#include "FontVariantCache.h"
45#include <unordered_map>
46
48static constexpr uint32_t MAKE_RGB(uint32_t r, uint32_t g, uint32_t b)
49{ return (0xFF & r) | ((0xFF & g) << 8) | ((0xFF & b) << 16); }
50
62class Style final
63{
64public:
65 Style();
66 explicit Style(AFontSize fontSize);
67 Style(const Style &);
68
69 Style &operator=(const Style &);
71 bool operator==(const Style &o) const;
72
78 Style &Read(wxConfigBase *config, const wxString &where);
80 void Write(wxConfigBase *config, const wxString &where) const;
81
83 static const Style &FromFont(const wxFont &font);
85 static Style FromStockFont(wxStockGDI::Item font);
86
87 constexpr static wxFontFamily Default_Family{wxFONTFAMILY_DEFAULT};
88 constexpr static wxFontEncoding Default_Encoding{wxFONTENCODING_DEFAULT};
89 constexpr static wxFontWeight Default_Weight{wxFONTWEIGHT_NORMAL};
90 constexpr static wxFontStyle Default_FontStyle{wxFONTSTYLE_NORMAL};
91 constexpr static bool Default_Underlined{false};
92 constexpr static bool Default_Strikethrough{false};
93 constexpr static AFontSize Default_FontSize{10.0f};
94 constexpr static uint32_t Default_ColorRGB{MAKE_RGB(0, 0, 0)};
95 static const wxColor &Default_Color();
96
97 wxFontFamily GetFamily() const;
98 wxFontEncoding GetEncoding() const;
99 wxFontWeight GetWeight() const;
100 bool IsBold() const { return GetWeight() == wxFONTWEIGHT_BOLD; }
101 bool IsLight() const { return GetWeight() == wxFONTWEIGHT_LIGHT; }
102 wxFontStyle GetFontStyle() const;
103 bool IsItalic() const { return GetFontStyle() == wxFONTSTYLE_ITALIC; }
104 bool IsSlant() const { return GetFontStyle() == wxFONTSTYLE_SLANT; }
105 bool IsUnderlined() const;
106 bool IsStrikethrough() const;
107 const wxString &GetFontName() const;
108 AFontSize GetFontSize() const;
109 uint32_t GetRGBColor() const;
110 wxColor GetColor() const { return wxColor(GetRGBColor()); }
111
112 using did_change = bool;
113 did_change SetFamily(wxFontFamily family);
114 did_change SetEncoding(wxFontEncoding encoding);
115 did_change SetWeight(wxFontWeight weight);
116 did_change SetBold(bool bold = true);
117 did_change SetLight(bool light = true);
118 did_change SetFontStyle(wxFontStyle style);
119 did_change SetItalic(bool italic = true);
120 did_change SetSlant(bool slant = true);
121 did_change SetUnderlined(bool underlined = true);
122 did_change SetStrikethrough(bool strikethrough = true);
123 did_change SetFontName(wxString fontName);
124 did_change SetFontSize(AFontSize fontSize);
125 did_change SetRGBColor(uint32_t rgb);
126 did_change SetColor(const wxColor &color);
127 did_change SetColor(wxSystemColour sysColour);
128
129 Style& Family(wxFontFamily family) { return SetFamily(family), *this; }
130 Style& Encoding(wxFontEncoding encoding) { return SetEncoding(encoding), *this; }
131 Style& Weight(wxFontWeight weight) { return SetWeight(weight), *this; }
132 Style& FontStyle(wxFontStyle style) { return SetFontStyle(style), *this; }
133 Style& Bold(bool bold = true) { return SetBold(bold), *this; }
134 Style& Light(bool light = true) { return SetLight(light), *this; }
135 Style& Italic(bool italic = true) { return SetItalic(italic), *this; }
136 Style& Slant(bool slant = true) { return SetSlant(slant), *this; }
137 Style& Underlined(bool underlined = true) { return SetUnderlined(underlined), *this; }
138 Style& Strikethrough(bool strikethrough = true) { return SetStrikethrough(strikethrough), *this; }
139 Style& FontSize(float size) { return SetFontSize(AFontSize(size)), *this; }
140 Style& FontSize(AFontSize fontSize) { return SetFontSize(fontSize), *this; }
141 Style& RGBColor(uint32_t rgb) { return SetRGBColor(rgb), *this; }
142 Style& Color(const wxColor &color) { return SetColor(color), *this; }
143 Style& Color(uint8_t r, uint8_t g, uint8_t b) { return SetColor({r, g, b}), *this; }
144 Style& Color(wxSystemColour sysColour) { return SetColor(sysColour), *this; }
145 Style& ChangeLightness(int alpha) { return SetColor(GetColor().ChangeLightness(alpha)), *this; }
146
147 bool CantChangeFontName() const {return m.cantChangeFontName;}
148 bool CantChangeFontVariant() const {return m.cantChangeFontVariant;}
149 void CantChangeFontName(bool changeForbidden) {m.cantChangeFontName = changeForbidden;}
150 void CantChangeFontVariant(bool changeForbidden) {m.cantChangeFontVariant = changeForbidden;}
151 wxFontInfo GetAsFontInfo() const;
152
153
154 bool IsFontOk() const;
156 const wxFont &GetFont(AFontSize fontSize) const;
158 const wxFont &GetFont() const {
159 return GetFont(GetFontSize());
160 }
161
163 did_change SetFromFont(const wxFont&);
165 did_change SetFontFrom(const Style&);
167 did_change SetFontFaceFrom(const Style&);
169 did_change SetFontFaceAndSizeFrom(const Style&);
171 constexpr static bool IsFractionalFontSizeSupported() {
172 return wxCHECK_VERSION(3, 1, 2); } //-V686 //-V501
174 static AFontSize GetFontSize(const wxFont &);
176 static void SetFontSize(wxFont &, AFontSize fontSize);
178 void ClearCache() const
179 {
180 if(m.fontCache)
181 m.fontCache->ClearCache();
182 }
183 std::shared_ptr<FontVariantCache> GetFontCache() const {return m.fontCache;}
184private:
185 typedef std::unordered_map <wxString, std::shared_ptr<FontVariantCache>,
186 wxStringHash> FontVariantCachesMap;
187
189 static wxString m_emptyString;
191 static FontVariantCachesMap m_fontCaches;
192
194 struct Data
195 {
196 // 8/4-byte members
202 mutable std::shared_ptr<FontVariantCache> fontCache;
203 // 4-byte members
204 uint32_t rgbColor = Default_ColorRGB;
205 // 2-byte members
206 AFontSize fontSize = Default_FontSize;
207 AFontFamily family = Default_Family;
208 AFontEncoding encoding = Default_Encoding;
209 AFontWeight weight = Default_Weight;
210 AFontStyle fontStyle = Default_FontStyle;
211 // 1-byte members
212 bool underlined : 1;
213 bool strikethrough : 1;
214 bool cantChangeFontName : 1; // !< Allow to change only color, underline etc.
215 bool cantChangeFontVariant : 1; // !< Allow to change only color
216
217 Data() : underlined(false), strikethrough(false), cantChangeFontName(false),
218 cantChangeFontVariant(false) {}
219 } m;
220
221 // cppcheck-suppress noExplicitConstructor
222};
223
224
230enum TextStyle : int8_t
231{
232 TS_CODE_DEFAULT , //<! The font code uses by default
233 TS_CODE_VARIABLE ,
234 TS_CODE_FUNCTION ,
235 TS_CODE_COMMENT ,
236 TS_CODE_NUMBER ,
237 TS_CODE_STRING ,
238 TS_CODE_OPERATOR ,
239 TS_CODE_LISP ,
240 TS_CODE_ENDOFLINE ,
241 TS_ASCIIMATHS ,
242 TS_MATH ,
243 TS_TEXT ,
244 TS_VARIABLE ,
245 TS_OPERATOR ,
246 TS_NUMBER ,
247 TS_FUNCTION ,
248 TS_SPECIAL_CONSTANT ,
249 TS_GREEK_CONSTANT ,
250 TS_STRING ,
251 TS_OUTDATED ,
252 TS_MAIN_PROMPT ,
253 TS_OTHER_PROMPT ,
254 TS_LABEL ,
255 TS_USERLABEL ,
256 TS_HIGHLIGHT ,
257 TS_WARNING ,
258 TS_ERROR ,
259 TS_TITLE ,
260 TS_SECTION ,
261 TS_SUBSECTION ,
262 TS_SUBSUBSECTION ,
263 TS_HEADING5 ,
264 TS_HEADING6 ,
265 TS_TEXT_BACKGROUND ,
266 TS_DOCUMENT_BACKGROUND,
267 TS_CELL_BRACKET ,
268 TS_ACTIVE_CELL_BRACKET,
269 TS_CURSOR ,
270 TS_SELECTION ,
271 TS_EQUALSSELECTION ,
275
277std::ostream& operator<<(std::ostream& out, const TextStyle textstyle);
278
279#endif // TEXTSTYLE_H
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:383
TextStyle
All text styles known to wxMaxima.
Definition: TextStyle.h:231
@ TS_INVALID
If a text style cannot be determined this value is used.
Definition: TextStyle.h:273
@ NUMBEROFSTYLES
This is not a style, but its value tells us how many styles are defined.
Definition: TextStyle.h:272
A Type-Safe Fixed-Point Font Size.
Definition: FontAttribs.h:98
Text Style Definition.
Definition: TextStyle.h:63
static Style FromStockFont(wxStockGDI::Item font)
Gets a style that represents a stock font. The font is pre-cached.
Definition: TextStyle.cpp:354
void Write(wxConfigBase *config, const wxString &where) const
Write this style to a config source.
Definition: TextStyle.cpp:337
did_change SetFontFaceAndSizeFrom(const Style &)
Sets font-face and size only properties based on another style (not attributes like bold,...
Definition: TextStyle.cpp:223
did_change SetFontFaceFrom(const Style &)
Sets font-face-only properties based on another style.
Definition: TextStyle.cpp:217
void ClearCache() const
Empties the font variant cache.
Definition: TextStyle.h:178
AFontSize GetFontSize() const
The size of this style's font, asserted to be valid.
Definition: TextStyle.cpp:101
did_change SetFontFrom(const Style &)
Sets all font-related properties based on another style, including size, font style and weight.
Definition: TextStyle.cpp:209
did_change SetFromFont(const wxFont &)
Sets all font-related properties based on another font.
Definition: TextStyle.cpp:229
bool operator==(const Style &o) const
Compares.
static constexpr bool IsFractionalFontSizeSupported()
Old wxWidgets versions only support integers as font sizes.
Definition: TextStyle.h:171
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:158
Style & Read(wxConfigBase *config, const wxString &where)
Read this style from a config source.
Definition: TextStyle.cpp:300