]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
Reverted the reverted changes - 1.
[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-2000 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 latex() const;
191
192         ///
193         FONT_MISC_STATE number() const;
194
195         ///
196         LColor::color color() const;
197
198         ///
199         Language const * language() const;
200
201         ///
202         bool isRightToLeft() const;
203
204         ///
205         bool isVisibleRightToLeft() const;
206         
207         ///
208         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
209         ///
210         LyXFont & setSeries(LyXFont::FONT_SERIES s);
211         ///
212         LyXFont & setShape(LyXFont::FONT_SHAPE s);
213         ///
214         LyXFont & setSize(LyXFont::FONT_SIZE s);
215         ///
216         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
217         ///
218         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
219         ///
220         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
221         ///
222         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
223         ///
224         LyXFont & setNumber(LyXFont::FONT_MISC_STATE n);
225         ///
226         LyXFont & setColor(LColor::color c);
227         ///
228         LyXFont & setLanguage(Language const * l);
229
230         /// Set family after LyX text format
231         LyXFont & setLyXFamily(string const &);
232  
233         /// Set series after LyX text format
234         LyXFont & setLyXSeries(string const &);
235  
236         /// Set shape after LyX text format
237         LyXFont & setLyXShape(string const &);
238  
239         /// Set size after LyX text format
240         LyXFont & setLyXSize(string const &);
241  
242         /// Returns misc flag after LyX text format
243         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
244
245         /// Sets color after LyX text format
246         LyXFont & setLyXColor(string const &);
247
248         /// Returns size of font in LaTeX text notation
249         string const latexSize() const;
250  
251         /** Updates font settings according to request.
252             If an attribute is IGNORE, the attribute is left as it is.
253             When toggleall = true, all properties that matches the font in use
254             will have the effect that the properties is reset to the
255             default.  If we have a text that is TYPEWRITER_FAMILY, and is
256             update()'ed with TYPEWRITER_FAMILY, the operation will be as if
257             a INHERIT_FAMILY was asked for.  This is necessary for the
258             toggle-user-defined-style button on the toolbar.
259         */
260         void update(LyXFont const & newfont, 
261                     Language const * default_lang,
262                     bool toggleall = false);
263  
264         /** Reduce font to fall back to template where possible.
265             Equal fields are reduced to INHERIT */
266         void reduce(LyXFont const & tmplt);
267  
268         /// Realize font from a template (INHERIT are realized)
269         LyXFont & realize(LyXFont const & tmplt);
270
271         /// Is a given font fully resolved?
272         bool resolved() const;
273  
274         /// Read a font specification from LyXLex. Used for layout files.
275         LyXFont & lyxRead(LyXLex &);
276  
277         /// Writes the changes from this font to orgfont in .lyx format in file
278         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
279
280         /** Writes the head of the LaTeX needed to change to this font.
281             Writes to string, the head of the LaTeX needed to change
282             to this font. Returns number of chars written. Base is the
283             font state active now.
284         */
285         int latexWriteStartChanges(std::ostream &, LyXFont const & base,
286                                    LyXFont const & prev) const;
287
288         /** Writes tha tail of the LaTeX needed to chagne to this font.
289             Returns number of chars written. Base is the font state we want
290             to achieve.
291         */
292         int latexWriteEndChanges(std::ostream &, LyXFont const & base,
293                                  LyXFont const & next) const;
294
295         /// Build GUI description of font state
296         string const stateText(BufferParams * params) const;
297
298         ///
299         LColor::color realColor() const;
300
301         ///
302         friend
303         bool operator==(LyXFont const & font1, LyXFont const & font2);
304
305         /// compares two fonts, ignoring the setting of the Latex part.
306         bool equalExceptLatex(LyXFont const &) const;
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 latex;
335                 ///
336                 FONT_MISC_STATE number;
337         };
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 inline
361 LyXFont::FONT_SHAPE LyXFont::shape() const
362 {
363         return bits.shape;
364 }
365
366
367 inline
368 LyXFont::LyXFont()
369         : bits(sane), lang(default_language)
370 {}
371
372
373 inline
374 LyXFont::FONT_FAMILY LyXFont::family() const 
375 {
376         return bits.family;
377 }
378
379
380 inline
381 LyXFont::FONT_SERIES LyXFont::series() const
382 {
383         return bits.series;
384 }
385
386
387 inline
388 LyXFont::FONT_SIZE LyXFont::size() const
389 {
390         return bits.size;
391 }
392
393
394 inline
395 LyXFont::FONT_MISC_STATE LyXFont::emph() const
396 {
397         return bits.emph;
398 }
399
400
401 inline
402 LyXFont::FONT_MISC_STATE LyXFont::noun() const
403 {
404         return bits.noun;
405 }
406
407
408 ///
409 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
410
411 ///
412 inline
413 bool operator==(LyXFont const & font1, LyXFont const & font2)
414 {
415         return font1.bits == font2.bits &&
416                 font1.lang == font2.lang;
417 }
418
419 ///
420 inline
421 bool operator!=(LyXFont const & font1, LyXFont const & font2)
422 {
423         return !(font1 == font2);
424 }
425 #endif