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