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