]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
ab683b4e1c33fa32b7862b45d93e814c05f97096
[lyx.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 <iosfwd>
19 #include <string>
20
21 class LColor_color;
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         ///
160         LyXFont();
161
162         /// Shortcut initialization
163         explicit
164         LyXFont(LyXFont::FONT_INIT1);
165         /// Shortcut initialization
166         explicit
167         LyXFont(LyXFont::FONT_INIT2);
168         /// Shortcut initialization
169         explicit
170         LyXFont(LyXFont::FONT_INIT3);
171         /// Shortcut initialization
172         LyXFont(LyXFont::FONT_INIT1, Language const * l);
173         /// Shortcut initialization
174         LyXFont(LyXFont::FONT_INIT2, Language const * l);
175         /// Shortcut initialization
176         LyXFont(LyXFont::FONT_INIT3, Language const * l);
177
178         /// Decreases font size by one
179         LyXFont & decSize();
180
181         /// Increases font size by one
182         LyXFont & incSize();
183
184         ///
185         FONT_FAMILY family() const;
186
187         ///
188         FONT_SERIES series() const;
189
190         ///
191         FONT_SHAPE shape() const;
192
193         ///
194         FONT_SIZE size() const;
195
196         ///
197         FONT_MISC_STATE emph() const;
198
199         ///
200         FONT_MISC_STATE underbar() const;
201
202         ///
203         FONT_MISC_STATE noun() const;
204
205         ///
206         FONT_MISC_STATE number() const;
207
208         ///
209         LColor_color color() const;
210
211         ///
212         Language const * language() const;
213
214         ///
215         bool isRightToLeft() const;
216
217         ///
218         bool isVisibleRightToLeft() const;
219
220         ///
221         bool isSymbolFont() const;
222
223         ///
224         void setFamily(LyXFont::FONT_FAMILY f);
225         void setSeries(LyXFont::FONT_SERIES s);
226         void setShape(LyXFont::FONT_SHAPE s);
227         void setSize(LyXFont::FONT_SIZE s);
228         void setEmph(LyXFont::FONT_MISC_STATE e);
229         void setUnderbar(LyXFont::FONT_MISC_STATE u);
230         void setNoun(LyXFont::FONT_MISC_STATE n);
231         void setNumber(LyXFont::FONT_MISC_STATE n);
232         void setColor(LColor_color c);
233         void setLanguage(Language const * l);
234
235         /// Set family after LyX text format
236         LyXFont & setLyXFamily(std::string const &);
237
238         /// Set series after LyX text format
239         LyXFont & setLyXSeries(std::string const &);
240
241         /// Set shape after LyX text format
242         LyXFont & setLyXShape(std::string const &);
243
244         /// Set size after LyX text format
245         LyXFont & setLyXSize(std::string const &);
246
247         /// Returns misc flag after LyX text format
248         LyXFont::FONT_MISC_STATE setLyXMisc(std::string const &);
249
250         /// Sets color after LyX text format
251         LyXFont & setLyXColor(std::string const &);
252
253         /// Returns size of font in LaTeX text notation
254         std::string const latexSize() const;
255
256         /** Updates font settings according to request.
257             If an attribute is IGNORE, the attribute is left as it is.
258             When toggleall = true, all properties that matches the font in use
259             will have the effect that the properties is reset to the
260             default.  If we have a text that is TYPEWRITER_FAMILY, and is
261             update()'ed with TYPEWRITER_FAMILY, the operation will be as if
262             a INHERIT_FAMILY was asked for.  This is necessary for the
263             toggle-user-defined-style button on the toolbar.
264         */
265         void update(LyXFont const & newfont,
266                     Language const * default_lang,
267                     bool toggleall = false);
268
269         /** Reduce font to fall back to template where possible.
270             Equal fields are reduced to INHERIT */
271         void reduce(LyXFont const & tmplt);
272
273         /// Realize font from a template (INHERIT are realized)
274         LyXFont & realize(LyXFont const & tmplt);
275         /// Is a given font fully resolved?
276         bool resolved() const;
277
278         /// Read a font specification from LyXLex. Used for layout files.
279         LyXFont & lyxRead(LyXLex &);
280
281         /// Writes the changes from this font to orgfont in .lyx format in file
282         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
283
284         /** Writes the head of the LaTeX needed to change to this font.
285             Writes to string, the head of the LaTeX needed to change
286             to this font. Returns number of chars written. Base is the
287             font state active now.
288         */
289         int latexWriteStartChanges(std::ostream &, LyXFont const & base,
290                                    LyXFont const & prev) const;
291
292         /** Writes tha tail of the LaTeX needed to chagne to this font.
293             Returns number of chars written. Base is the font state we want
294             to achieve.
295         */
296         int latexWriteEndChanges(std::ostream &, LyXFont const & base,
297                                  LyXFont const & next) const;
298
299         /// Build GUI description of font state
300         std::string const stateText(BufferParams * params) const;
301
302         ///
303         LColor_color realColor() const;
304
305         ///
306         friend
307         bool operator==(LyXFont const & font1, LyXFont const & font2);
308
309         /// Converts logical attributes to concrete shape attribute
310         LyXFont::FONT_SHAPE realShape() const;
311
312         /** Compaq cxx 6.5 requires that the definition be public so that
313             it can compile operator==()
314          */
315         struct FontBits {
316                 ///
317                 FONT_FAMILY family;
318                 ///
319                 FONT_SERIES series;
320                 ///
321                 FONT_SHAPE shape;
322                 ///
323                 FONT_SIZE size;
324                 /** We store the LColor::color value as an int to get LColor.h out
325                  *  of the header file.
326                  */
327                 int color;
328                 ///
329                 FONT_MISC_STATE emph;
330                 ///
331                 FONT_MISC_STATE underbar;
332                 ///
333                 FONT_MISC_STATE noun;
334                 ///
335                 FONT_MISC_STATE number;
336         };
337 private:
338
339         ///
340         FontBits bits;
341
342         ///
343         Language const * lang;
344
345         /// Sane font
346         static FontBits sane;
347
348         /// All inherit font
349         static FontBits inherit;
350
351         /// All ignore font
352         static FontBits ignore;
353
354         /// Updates a misc setting according to request
355         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
356                                          LyXFont::FONT_MISC_STATE org);
357 };
358
359
360 /** \c LyXFont_size is a wrapper for LyXFont::FONT_SIZE.
361  *  It can be forward-declared and passed as a function argument without
362  *  having to expose lyxfont.h.
363  */
364 class LyXFont_size {
365         LyXFont::FONT_SIZE val_;
366 public:
367         LyXFont_size(LyXFont::FONT_SIZE val) : val_(val) {}
368         operator LyXFont::FONT_SIZE() const{ return val_; }
369 };
370
371
372
373 inline
374 LyXFont::FONT_SHAPE LyXFont::shape() const
375 {
376         return bits.shape;
377 }
378
379
380 inline
381 LyXFont::FONT_FAMILY LyXFont::family() const
382 {
383         return bits.family;
384 }
385
386
387 inline
388 LyXFont::FONT_SERIES LyXFont::series() const
389 {
390         return bits.series;
391 }
392
393
394 inline
395 LyXFont::FONT_SIZE LyXFont::size() const
396 {
397         return bits.size;
398 }
399
400
401 inline
402 LyXFont::FONT_MISC_STATE LyXFont::emph() const
403 {
404         return bits.emph;
405 }
406
407
408 inline
409 LyXFont::FONT_MISC_STATE LyXFont::noun() const
410 {
411         return bits.noun;
412 }
413
414
415 inline
416 bool LyXFont::isSymbolFont() const
417 {
418         switch (family()) {
419         case LyXFont::SYMBOL_FAMILY:
420         case LyXFont::CMSY_FAMILY:
421         case LyXFont::CMM_FAMILY:
422         case LyXFont::CMEX_FAMILY:
423         case LyXFont::MSA_FAMILY:
424         case LyXFont::MSB_FAMILY:
425         case LyXFont::WASY_FAMILY:
426                 return true;
427         default:
428                 return false;
429         }
430 }
431
432 ///
433 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
434
435 bool operator==(LyXFont::FontBits const & lhs, LyXFont::FontBits const & rhs);
436
437 inline
438 bool operator!=(LyXFont::FontBits const & lhs, LyXFont::FontBits const & rhs)
439 {
440         return !(lhs == rhs);
441 }
442
443 ///
444 inline
445 bool operator==(LyXFont const & font1, LyXFont const & font2)
446 {
447         return font1.bits == font2.bits &&
448                 font1.lang == font2.lang;
449 }
450
451 ///
452 inline
453 bool operator!=(LyXFont const & font1, LyXFont const & font2)
454 {
455         return !(font1 == font2);
456 }
457
458 #endif