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