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