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