]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
034d4e484f42141876b69ef21e297b334f05ebbb
[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 #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                 INHERIT_FAMILY,
49                 ///
50                 IGNORE_FAMILY
51         };
52
53         ///
54         enum FONT_SERIES {
55                 ///
56                 MEDIUM_SERIES, // fontstruct rely on this to be 0
57                 ///
58                 BOLD_SERIES,
59                 ///
60                 INHERIT_SERIES,
61                 ///
62                 IGNORE_SERIES
63         };
64
65         ///
66         enum FONT_SHAPE {
67                 ///
68                 UP_SHAPE, // fontstruct rely on this to be 0
69                 ///
70                 ITALIC_SHAPE,
71                 ///
72                 SLANTED_SHAPE,
73                 ///
74                 SMALLCAPS_SHAPE,
75                 ///
76                 INHERIT_SHAPE,
77                 ///
78                 IGNORE_SHAPE
79         };
80
81         ///
82         enum FONT_SIZE {
83                 ///
84                 SIZE_TINY, // fontstruct rely on this to be 0
85                 ///
86                 SIZE_SCRIPT,
87                 ///
88                 SIZE_FOOTNOTE,
89                 ///
90                 SIZE_SMALL,
91                 ///
92                 SIZE_NORMAL,
93                 ///
94                 SIZE_LARGE,
95                 ///
96                 SIZE_LARGER,
97                 ///
98                 SIZE_LARGEST,
99                 ///
100                 SIZE_HUGE,
101                 ///
102                 SIZE_HUGER,
103                 ///
104                 INCREASE_SIZE,
105                 ///
106                 DECREASE_SIZE,
107                 ///
108                 INHERIT_SIZE,
109                 ///
110                 IGNORE_SIZE
111         };
112  
113         /// Used for emph, underbar, noun and latex toggles
114         enum FONT_MISC_STATE {
115                 ///
116                 OFF,
117                 ///
118                 ON,
119                 ///
120                 TOGGLE,
121                 ///
122                 INHERIT,
123                 ///
124                 IGNORE
125         };
126
127         /// Trick to overload constructor and make it megafast
128         enum FONT_INIT1 {
129                 ///
130                 ALL_INHERIT
131         };
132         ///
133         enum FONT_INIT2 {
134                 ///
135                 ALL_IGNORE
136         };
137         ///
138         enum FONT_INIT3 {
139                 ///
140                 ALL_SANE
141         };
142
143         ///
144         LyXFont();
145
146         /// Shortcut initialization
147         explicit
148         LyXFont(LyXFont::FONT_INIT1);
149         /// Shortcut initialization
150         explicit
151         LyXFont(LyXFont::FONT_INIT2);
152         /// Shortcut initialization
153         explicit
154         LyXFont(LyXFont::FONT_INIT3);
155         /// Shortcut initialization
156         LyXFont(LyXFont::FONT_INIT1, Language const * l);
157         /// Shortcut initialization
158         LyXFont(LyXFont::FONT_INIT2, Language const * l);
159         /// Shortcut initialization
160         LyXFont(LyXFont::FONT_INIT3, Language const * l);
161
162         /// Decreases font size by one
163         LyXFont & decSize();
164  
165         /// Increases font size by one
166         LyXFont & incSize();
167  
168         ///
169         FONT_FAMILY family() const;
170  
171         ///
172         FONT_SERIES series() const;
173  
174         ///
175         FONT_SHAPE shape() const;
176  
177         ///
178         FONT_SIZE size() const;
179  
180         ///
181         FONT_MISC_STATE emph() const;
182  
183         ///
184         FONT_MISC_STATE underbar() const;
185  
186         ///
187         FONT_MISC_STATE noun() const;
188
189         ///
190         FONT_MISC_STATE number() const;
191
192         ///
193         LColor::color color() const;
194
195         ///
196         Language const * language() const;
197
198         ///
199         bool isRightToLeft() const;
200
201         ///
202         bool isVisibleRightToLeft() const;
203         
204         ///
205         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
206         ///
207         LyXFont & setSeries(LyXFont::FONT_SERIES s);
208         ///
209         LyXFont & setShape(LyXFont::FONT_SHAPE s);
210         ///
211         LyXFont & setSize(LyXFont::FONT_SIZE s);
212         ///
213         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
214         ///
215         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
216         ///
217         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
218         ///
219         LyXFont & setNumber(LyXFont::FONT_MISC_STATE n);
220         ///
221         LyXFont & setColor(LColor::color c);
222         ///
223         LyXFont & setLanguage(Language const * l);
224
225         /// Set family after LyX text format
226         LyXFont & setLyXFamily(string const &);
227  
228         /// Set series after LyX text format
229         LyXFont & setLyXSeries(string const &);
230  
231         /// Set shape after LyX text format
232         LyXFont & setLyXShape(string const &);
233  
234         /// Set size after LyX text format
235         LyXFont & setLyXSize(string const &);
236  
237         /// Returns misc flag after LyX text format
238         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
239
240         /// Sets color after LyX text format
241         LyXFont & setLyXColor(string const &);
242
243         /// Returns size of font in LaTeX text notation
244         string const latexSize() const;
245  
246         /** Updates font settings according to request.
247             If an attribute is IGNORE, the attribute is left as it is.
248             When toggleall = true, all properties that matches the font in use
249             will have the effect that the properties is reset to the
250             default.  If we have a text that is TYPEWRITER_FAMILY, and is
251             update()'ed with TYPEWRITER_FAMILY, the operation will be as if
252             a INHERIT_FAMILY was asked for.  This is necessary for the
253             toggle-user-defined-style button on the toolbar.
254         */
255         void update(LyXFont const & newfont,
256                     Language const * default_lang,
257                     bool toggleall = false);
258  
259         /** Reduce font to fall back to template where possible.
260             Equal fields are reduced to INHERIT */
261         void reduce(LyXFont const & tmplt);
262  
263         /// Realize font from a template (INHERIT are realized)
264 #ifndef INHERIT_LANGUAGE
265         LyXFont & realize(LyXFont const & tmplt);
266 #else
267         LyXFont & realize(LyXFont const & tmplt, Language const * language);
268 #endif
269         /// Is a given font fully resolved?
270         bool resolved() const;
271  
272         /// Read a font specification from LyXLex. Used for layout files.
273         LyXFont & lyxRead(LyXLex &);
274  
275         /// Writes the changes from this font to orgfont in .lyx format in file
276 #ifndef INHERIT_LANGUAGE
277         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
278 #else
279         void lyxWriteChanges(LyXFont const & orgfont, Language const * doclang,
280                              std::ostream &) const;
281 #endif
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         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 private:
311         ///
312         struct FontBits {
313                 ///
314                 bool operator==(FontBits const & fb1) const;
315                 ///
316                 bool operator!=(FontBits const & fb1) const;
317                 ///
318                 FONT_FAMILY family;
319                 ///
320                 FONT_SERIES series;
321                 ///
322                 FONT_SHAPE shape;
323                 ///
324                 FONT_SIZE size;
325                 ///
326                 LColor::color 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
337         ///
338         FontBits bits;
339         
340         ///
341         Language const * lang;
342         
343         /// Sane font
344         static FontBits sane;
345         
346         /// All inherit font
347         static FontBits inherit;
348  
349         /// All ignore font
350         static FontBits ignore;
351
352         /// Updates a misc setting according to request
353         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
354                                          LyXFont::FONT_MISC_STATE org);
355 };
356
357
358 inline
359 LyXFont::FONT_SHAPE LyXFont::shape() const
360 {
361         return bits.shape;
362 }
363
364
365 inline
366 LyXFont::LyXFont()
367         : bits(sane), lang(default_language)
368 {}
369
370
371 inline
372 LyXFont::FONT_FAMILY LyXFont::family() const 
373 {
374         return bits.family;
375 }
376
377
378 inline
379 LyXFont::FONT_SERIES LyXFont::series() const
380 {
381         return bits.series;
382 }
383
384
385 inline
386 LyXFont::FONT_SIZE LyXFont::size() const
387 {
388         return bits.size;
389 }
390
391
392 inline
393 LyXFont::FONT_MISC_STATE LyXFont::emph() const
394 {
395         return bits.emph;
396 }
397
398
399 inline
400 LyXFont::FONT_MISC_STATE LyXFont::noun() const
401 {
402         return bits.noun;
403 }
404
405
406 ///
407 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
408
409 ///
410 inline
411 bool operator==(LyXFont const & font1, LyXFont const & font2)
412 {
413         return font1.bits == font2.bits &&
414                 font1.lang == font2.lang;
415 }
416
417 ///
418 inline
419 bool operator!=(LyXFont const & font1, LyXFont const & font2)
420 {
421         return !(font1 == font2);
422 }
423 #endif