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