]> git.lyx.org Git - lyx.git/blob - src/FontInfo.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / FontInfo.h
1 // -*- C++ -*-
2 /**
3  * \file src/FontInfo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef FONT_PROPERTIES_H
16 #define FONT_PROPERTIES_H
17
18 #include "Color.h"
19 #include "ColorCode.h"
20 #include "FontEnums.h"
21
22 #include "support/Changer.h"
23 #include "support/strfwd.h"
24
25
26 namespace lyx {
27
28 namespace support { class Lexer; }
29
30 ///
31 class FontInfo
32 {
33 public:
34         ///
35         FontInfo();
36         ///
37         FontInfo(
38                 FontFamily family,
39                 FontSeries series,
40                 FontShape  shape,
41                 FontSize   size,
42                 MathStyle  style,
43                 ColorCode  color,
44                 ColorCode  background,
45                 FontState  emph,
46                 FontState  underbar,
47                 FontState  strikeout,
48                 FontState  xout,
49                 FontState  uuline,
50                 FontState  uwave,
51                 FontState  noun,
52                 FontState  number,
53                 FontState  nospellcheck)
54                 : family_(family), series_(series), shape_(shape), size_(size),
55                 style_(style), color_(color), background_(background), emph_(emph),
56                 underbar_(underbar), strikeout_(strikeout), xout_(xout), uuline_(uuline),
57                 uwave_(uwave), noun_(noun), number_(number), nospellcheck_(nospellcheck)
58         {}
59
60         /// Decreases font size by one
61         FontInfo & decSize();
62         /// Increases font size by one
63         FontInfo & incSize();
64
65         /// \name Accessor methods
66         //@{
67         FontFamily family() const { return family_; }
68         void setFamily(FontFamily f) { family_ = f; }
69         FontSeries series() const { return series_; }
70         void setSeries(FontSeries s) { series_ = s; }
71         FontShape shape() const { return shape_; }
72         void setShape(FontShape s) { shape_ = s; }
73         FontSize size() const { return size_; }
74         void setSize(FontSize s) { size_ = s; }
75         MathStyle style() const {return style_; }
76         void setStyle(MathStyle s) { style_ = s; }
77         FontState emph() const { return emph_; }
78         void setEmph(FontState e) { emph_ = e; }
79         FontState underbar() const { return underbar_; }
80         void setUnderbar(FontState u) { underbar_ = u; }
81         FontState strikeout() const { return strikeout_; }
82         void setStrikeout(FontState s) { strikeout_ = s; }
83         FontState xout() const { return xout_; }
84         void setXout(FontState s) { xout_ = s; }
85         FontState uuline() const { return uuline_; }
86         void setUuline(FontState s) { uuline_ = s; }
87         FontState uwave() const { return uwave_; }
88         void setUwave(FontState s) { uwave_ = s; }
89         FontState noun() const { return noun_; }
90         void setNoun(FontState n) { noun_ = n; }
91         FontState number() const { return number_; }
92         void setNumber(FontState n) { number_ = n; }
93         ColorCode color() const { return color_; }
94         void setColor(ColorCode c) { color_ = c; }
95         ColorCode background() const { return background_; }
96         void setBackground(ColorCode b) { background_ = b; }
97         FontState nospellcheck() const { return nospellcheck_; }
98         void setNoSpellcheck(FontState n) { nospellcheck_ = n; }
99         //@}
100
101         ///
102         void update(FontInfo const & newfont, bool toggleall);
103
104         /** Reduce font to fall back to template where possible.
105             Equal fields are reduced to INHERIT */
106         void reduce(FontInfo const & tmplt);
107
108         /// Realize font from a template (INHERIT are realized)
109         FontInfo & realize(FontInfo const & tmplt);
110         /// Is a given font fully resolved?
111         bool resolved() const;
112
113         /// The real color of the font. This can be the color that is
114         /// set for painting, the color of the font or a default color.
115         Color realColor() const;
116         /// Sets the color which is used during painting
117         void setPaintColor(Color c) { paint_color_ = c; }
118
119         /// Compute the font size, taking size and math style into account.
120         double realSize() const;
121
122         ///
123         docstring asCSS() const;
124
125         /// Converts logical attributes to concrete shape attribute
126         /// Try hard to inline this as it shows up with 4.6 % in the profiler.
127         FontShape realShape() const
128         {
129                 if (noun_ == FONT_ON)
130                         return SMALLCAPS_SHAPE;
131                 if (emph_ == FONT_ON)
132                         return (shape_ == ITALIC_SHAPE || shape_ == SLANTED_SHAPE)
133                                 ? UP_SHAPE : ITALIC_SHAPE;
134                 return shape_;
135         }
136
137         bool isSymbolFont() const
138         {
139                 switch (family_) {
140                 case SYMBOL_FAMILY:
141                 case CMSY_FAMILY:
142                 case CMM_FAMILY:
143                 case CMEX_FAMILY:
144                 case MSA_FAMILY:
145                 case MSB_FAMILY:
146                 case STMARY_FAMILY:
147                 case WASY_FAMILY:
148                 case ESINT_FAMILY:
149                         return true;
150                 default:
151                         return false;
152                 }
153         }
154
155         /// Temporarily replace the color with \param color.
156         Changer changeColor(ColorCode const color);
157         /// Temporarily replace the shape with \param shape.
158         Changer changeShape(FontShape const shape);
159         /// Temporarily replace the style
160         Changer changeStyle(MathStyle style);
161         /// Temporarily replace the FontInfo with \param font, and optionally
162         /// \param realize the \param font against the current FontInfo.
163         Changer change(FontInfo font, bool realize = false);
164
165         /// Build GUI description of font state
166         docstring const stateText(bool const terse = false) const;
167
168 private:
169         friend bool operator==(FontInfo const & lhs, FontInfo const & rhs);
170
171         ///
172         FontFamily family_;
173         ///
174         FontSeries series_;
175         ///
176         FontShape shape_;
177         ///
178         FontSize size_;
179         ///
180         MathStyle style_;
181         ///
182         ColorCode color_;
183         ///
184         ColorCode background_;
185         /// The color used for painting
186         Color paint_color_;
187         ///
188         FontState emph_;
189         ///
190         FontState underbar_;
191         ///
192         FontState strikeout_;
193         ///
194         FontState xout_;
195         ///
196         FontState uuline_;
197         ///
198         FontState uwave_;
199         ///
200         FontState noun_;
201         ///
202         FontState number_;
203         ///
204         FontState nospellcheck_;
205 };
206
207
208 inline bool operator==(FontInfo const & lhs, FontInfo const & rhs)
209 {
210         return lhs.family_ == rhs.family_
211                 && lhs.series_ == rhs.series_
212                 && lhs.shape_ == rhs.shape_
213                 && lhs.size_ == rhs.size_
214                 && lhs.style_ == rhs.style_
215                 && lhs.color_ == rhs.color_
216                 && lhs.background_ == rhs.background_
217                 && lhs.emph_ == rhs.emph_
218                 && lhs.underbar_ == rhs.underbar_
219                 && lhs.strikeout_ == rhs.strikeout_
220                 && lhs.xout_ == rhs.xout_
221                 && lhs.uuline_ == rhs.uuline_
222                 && lhs.uwave_ == rhs.uwave_
223                 && lhs.noun_ == rhs.noun_
224                 && lhs.number_ == rhs.number_
225                 && lhs.nospellcheck_ == rhs.nospellcheck_;
226 }
227
228
229 inline bool operator!=(FontInfo const & lhs, FontInfo const & rhs)
230 {
231         return !operator==(lhs, rhs);
232 }
233
234 /// Sane font.
235 extern FontInfo const sane_font;
236 /// All inherit font.
237 extern FontInfo const inherit_font;
238 /// All ignore font.
239 extern FontInfo const ignore_font;
240
241 /// Set family after LyX text format
242 void setLyXFamily(std::string const &, FontInfo &);
243
244 /// Set series after LyX text format
245 void setLyXSeries(std::string const &, FontInfo &);
246
247 /// Set shape after LyX text format
248 void setLyXShape(std::string const &, FontInfo &);
249
250 /// Set size after LyX text format
251 void setLyXSize(std::string const &, FontInfo &);
252
253 /// Sets color after LyX text format
254 void setLyXColor(std::string const &, FontInfo &);
255
256 /// Returns misc flag after LyX text format
257 FontState setLyXMisc(std::string const &);
258
259 /// Read a font specification from Lexer. Used for layout files.
260 FontInfo lyxRead(support::Lexer &, FontInfo const & fi = sane_font);
261
262 /// Write a font specification. Used for layout files.
263 void lyxWrite(std::ostream &, FontInfo const &, std::string const &, int);
264
265 } // namespace lyx
266
267 #endif