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