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