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