]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
The speed patch: redraw only rows that have changed
[lyx.git] / src / lyxfont.h
1 // -*- C++ -*-
2 /**
3  * \file src/lyxfont.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 LYXFONT_H
16 #define LYXFONT_H
17
18 #include "LColor.h"
19
20 #include <iosfwd>
21 #include <string>
22
23 class LyXLex;
24 class BufferParams;
25 class Language;
26
27
28 ///
29 class LyXFont {
30 public:
31         /** The value INHERIT_* means that the font attribute is
32             inherited from the layout. In the case of layout fonts, the
33             attribute is inherited from the default font.
34             The value IGNORE_* is used with LyXFont::update() when the
35             attribute should not be changed.
36         */
37         enum FONT_FAMILY {
38                 ///
39                 ROMAN_FAMILY, // fontstruct rely on this to be 0
40                 ///
41                 SANS_FAMILY,
42                 ///
43                 TYPEWRITER_FAMILY,
44                 ///
45                 SYMBOL_FAMILY,
46                 ///
47                 CMR_FAMILY,
48                 ///
49                 CMSY_FAMILY,
50                 ///
51                 CMM_FAMILY,
52                 ///
53                 CMEX_FAMILY,
54                 ///
55                 MSA_FAMILY,
56                 ///
57                 MSB_FAMILY,
58                 ///
59                 EUFRAK_FAMILY,
60                 ///
61                 WASY_FAMILY,
62                 ///
63                 INHERIT_FAMILY,
64                 ///
65                 IGNORE_FAMILY,
66                 ///
67                 NUM_FAMILIES = INHERIT_FAMILY
68         };
69
70         ///
71         enum FONT_SERIES {
72                 ///
73                 MEDIUM_SERIES, // fontstruct rely on this to be 0
74                 ///
75                 BOLD_SERIES,
76                 ///
77                 INHERIT_SERIES,
78                 ///
79                 IGNORE_SERIES
80         };
81
82         ///
83         enum FONT_SHAPE {
84                 ///
85                 UP_SHAPE, // fontstruct rely on this to be 0
86                 ///
87                 ITALIC_SHAPE,
88                 ///
89                 SLANTED_SHAPE,
90                 ///
91                 SMALLCAPS_SHAPE,
92                 ///
93                 INHERIT_SHAPE,
94                 ///
95                 IGNORE_SHAPE
96         };
97
98         ///
99         enum FONT_SIZE {
100                 ///
101                 SIZE_TINY, // fontstruct rely on this to be 0
102                 ///
103                 SIZE_SCRIPT,
104                 ///
105                 SIZE_FOOTNOTE,
106                 ///
107                 SIZE_SMALL,
108                 ///
109                 SIZE_NORMAL,
110                 ///
111                 SIZE_LARGE,
112                 ///
113                 SIZE_LARGER,
114                 ///
115                 SIZE_LARGEST,
116                 ///
117                 SIZE_HUGE,
118                 ///
119                 SIZE_HUGER,
120                 ///
121                 INCREASE_SIZE,
122                 ///
123                 DECREASE_SIZE,
124                 ///
125                 INHERIT_SIZE,
126                 ///
127                 IGNORE_SIZE
128         };
129
130         /// Used for emph, underbar, noun and latex toggles
131         enum FONT_MISC_STATE {
132                 ///
133                 OFF,
134                 ///
135                 ON,
136                 ///
137                 TOGGLE,
138                 ///
139                 INHERIT,
140                 ///
141                 IGNORE
142         };
143
144         /// Trick to overload constructor and make it megafast
145         enum FONT_INIT1 {
146                 ///
147                 ALL_INHERIT
148         };
149         ///
150         enum FONT_INIT2 {
151                 ///
152                 ALL_IGNORE
153         };
154         ///
155         enum FONT_INIT3 {
156                 ///
157                 ALL_SANE
158         };
159
160         struct FontBits {
161                 ///
162                 FONT_FAMILY family;
163                 ///
164                 FONT_SERIES series;
165                 ///
166                 FONT_SHAPE shape;
167                 ///
168                 FONT_SIZE size;
169                 /** We store the LColor::color value as an int to get LColor.h out
170                  *  of the header file.
171                  */
172                 int color;
173                 ///
174                 FONT_MISC_STATE emph;
175                 ///
176                 FONT_MISC_STATE underbar;
177                 ///
178                 FONT_MISC_STATE noun;
179                 ///
180                 FONT_MISC_STATE number;
181         };
182         ///
183         LyXFont();
184
185         /// Shortcut initialization
186         explicit
187         LyXFont(LyXFont::FONT_INIT1);
188         /// Shortcut initialization
189         explicit
190         LyXFont(LyXFont::FONT_INIT2);
191         /// Shortcut initialization
192         explicit
193         LyXFont(LyXFont::FONT_INIT3);
194         /// Shortcut initialization
195         LyXFont(LyXFont::FONT_INIT1, Language const * l);
196         /// Shortcut initialization
197         LyXFont(LyXFont::FONT_INIT2, Language const * l);
198         /// Shortcut initialization
199         LyXFont(LyXFont::FONT_INIT3, Language const * l);
200
201         /// Decreases font size by one
202         LyXFont & decSize();
203         /// Increases font size by one
204         LyXFont & incSize();
205         ///
206         FONT_FAMILY family() const { return bits.family; }
207         ///
208         FONT_SERIES series() const { return bits.series; }
209         ///
210         FONT_SHAPE shape() const { return bits.shape; }
211         ///
212         FONT_SIZE size() const { return bits.size; }
213         ///
214         FONT_MISC_STATE emph() const { return bits.emph; }
215         ///
216         FONT_MISC_STATE underbar() const { return bits.underbar; }
217         ///
218         FONT_MISC_STATE noun() const { return bits.noun; }
219         ///
220         FONT_MISC_STATE number() const { return bits.number; }
221         ///
222         LColor_color color() const;
223         ///
224         Language const * language() const { return lang; }
225         ///
226         bool isRightToLeft() const;
227         ///
228         bool isVisibleRightToLeft() const;
229         ///
230         bool isSymbolFont() const;
231
232         ///
233         void setFamily(LyXFont::FONT_FAMILY f);
234         void setSeries(LyXFont::FONT_SERIES s);
235         void setShape(LyXFont::FONT_SHAPE s);
236         void setSize(LyXFont::FONT_SIZE s);
237         void setEmph(LyXFont::FONT_MISC_STATE e);
238         void setUnderbar(LyXFont::FONT_MISC_STATE u);
239         void setNoun(LyXFont::FONT_MISC_STATE n);
240         void setNumber(LyXFont::FONT_MISC_STATE n);
241         void setColor(LColor_color c);
242         void setLanguage(Language const * l);
243
244         /// Set family after LyX text format
245         LyXFont & setLyXFamily(std::string const &);
246
247         /// Set series after LyX text format
248         LyXFont & setLyXSeries(std::string const &);
249
250         /// Set shape after LyX text format
251         LyXFont & setLyXShape(std::string const &);
252
253         /// Set size after LyX text format
254         LyXFont & setLyXSize(std::string const &);
255
256         /// Returns misc flag after LyX text format
257         LyXFont::FONT_MISC_STATE setLyXMisc(std::string const &);
258
259         /// Sets color after LyX text format
260         LyXFont & setLyXColor(std::string const &);
261
262         /// Returns size of font in LaTeX text notation
263         std::string const latexSize() const;
264
265         /** Updates font settings according to request.
266             If an attribute is IGNORE, the attribute is left as it is.
267             When toggleall = true, all properties that matches the font in use
268             will have the effect that the properties is reset to the
269             default.  If we have a text that is TYPEWRITER_FAMILY, and is
270             update()'ed with TYPEWRITER_FAMILY, the operation will be as if
271             a INHERIT_FAMILY was asked for.  This is necessary for the
272             toggle-user-defined-style button on the toolbar.
273         */
274         void update(LyXFont const & newfont,
275                     Language const * default_lang,
276                     bool toggleall = false);
277
278         /** Reduce font to fall back to template where possible.
279             Equal fields are reduced to INHERIT */
280         void reduce(LyXFont const & tmplt);
281
282         /// Realize font from a template (INHERIT are realized)
283         LyXFont & realize(LyXFont const & tmplt);
284         /// Is a given font fully resolved?
285         bool resolved() const;
286
287         /// Read a font specification from LyXLex. Used for layout files.
288         LyXFont & lyxRead(LyXLex &);
289
290         /// Writes the changes from this font to orgfont in .lyx format in file
291         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
292
293         /** Writes the head of the LaTeX needed to change to this font.
294             Writes to string, the head of the LaTeX needed to change
295             to this font. Returns number of chars written. Base is the
296             font state active now.
297         */
298         int latexWriteStartChanges(std::ostream &, LyXFont const & base,
299                                    LyXFont const & prev) const;
300
301         /** Writes the tail of the LaTeX needed to change to this font.
302             Returns number of chars written. Base is the font state we want
303             to achieve.
304         */
305         int latexWriteEndChanges(std::ostream &, LyXFont const & base,
306                                  LyXFont const & next) const;
307
308         /// Build GUI description of font state
309         std::string const stateText(BufferParams * params) const;
310
311         ///
312         LColor_color realColor() const;
313
314         ///
315         friend
316         bool operator==(LyXFont const & font1, LyXFont const & font2);
317         ///
318         friend
319         std::ostream & operator<<(std::ostream & os, LyXFont const & font);
320
321         /// Converts logical attributes to concrete shape attribute
322         // Try hard to inline this as it shows up with 4.6 % in the profiler.
323         LyXFont::FONT_SHAPE realShape() const {
324                 if (bits.noun == ON)
325                         return SMALLCAPS_SHAPE;
326                 if (bits.emph == ON)
327                         return (bits.shape == UP_SHAPE) ? ITALIC_SHAPE : UP_SHAPE;
328                 return bits.shape;
329         }
330
331
332         /** Compaq cxx 6.5 requires that the definition be public so that
333             it can compile operator==()
334          */
335 private:
336         ///
337         FontBits bits;
338         ///
339         Language const * lang;
340         /// Sane font
341         static FontBits sane;
342         /// All inherit font
343         static FontBits inherit;
344         /// All ignore font
345         static FontBits ignore;
346         /// Updates a misc setting according to request
347         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
348                                          LyXFont::FONT_MISC_STATE org);
349 };
350
351
352 /** \c LyXFont_size is a wrapper for LyXFont::FONT_SIZE.
353  *  It can be forward-declared and passed as a function argument without
354  *  having to expose lyxfont.h.
355  */
356 class LyXFont_size {
357 public:
358         ///
359         LyXFont_size(LyXFont::FONT_SIZE val) : val_(val) {}
360         ///
361         operator LyXFont::FONT_SIZE() const { return val_; }
362 private:
363         ///
364         LyXFont::FONT_SIZE val_;
365 };
366
367
368
369 inline
370 bool LyXFont::isSymbolFont() const
371 {
372         switch (family()) {
373         case LyXFont::SYMBOL_FAMILY:
374         case LyXFont::CMSY_FAMILY:
375         case LyXFont::CMM_FAMILY:
376         case LyXFont::CMEX_FAMILY:
377         case LyXFont::MSA_FAMILY:
378         case LyXFont::MSB_FAMILY:
379         case LyXFont::WASY_FAMILY:
380                 return true;
381         default:
382                 return false;
383         }
384 }
385
386 ///
387 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
388
389 bool operator==(LyXFont::FontBits const & lhs, LyXFont::FontBits const & rhs);
390
391 inline
392 bool operator!=(LyXFont::FontBits const & lhs, LyXFont::FontBits const & rhs)
393 {
394         return !(lhs == rhs);
395 }
396
397 ///
398 inline
399 bool operator==(LyXFont const & font1, LyXFont const & font2)
400 {
401         return font1.bits == font2.bits && font1.lang == font2.lang;
402 }
403
404 ///
405 inline
406 bool operator!=(LyXFont const & font1, LyXFont const & font2)
407 {
408         return !(font1 == font2);
409 }
410
411 #endif