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