]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
Fix working of the spellchecker dialog with ispell when there are no
[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                 CMR_FAMILY,
49                 ///
50                 CMSY_FAMILY,
51                 ///
52                 CMM_FAMILY,
53                 ///
54                 CMEX_FAMILY,
55                 ///
56                 MSA_FAMILY,
57                 ///
58                 MSB_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 #ifndef INHERIT_LANGUAGE
282         LyXFont & realize(LyXFont const & tmplt);
283 #else
284         LyXFont & realize(LyXFont const & tmplt, Language const * language);
285 #endif
286         /// Is a given font fully resolved?
287         bool resolved() const;
288  
289         /// Read a font specification from LyXLex. Used for layout files.
290         LyXFont & lyxRead(LyXLex &);
291  
292         /// Writes the changes from this font to orgfont in .lyx format in file
293 #ifndef INHERIT_LANGUAGE
294         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
295 #else
296         void lyxWriteChanges(LyXFont const & orgfont, Language const * doclang,
297                              std::ostream &) const;
298 #endif
299
300         /** Writes the head of the LaTeX needed to change to this font.
301             Writes to string, the head of the LaTeX needed to change
302             to this font. Returns number of chars written. Base is the
303             font state active now.
304         */
305         int latexWriteStartChanges(std::ostream &, LyXFont const & base,
306                                    LyXFont const & prev) const;
307
308         /** Writes tha tail of the LaTeX needed to chagne to this font.
309             Returns number of chars written. Base is the font state we want
310             to achieve.
311         */
312         int latexWriteEndChanges(std::ostream &, LyXFont const & base,
313                                  LyXFont const & next) const;
314
315         /// Build GUI description of font state
316         string const stateText(BufferParams * params) const;
317
318         ///
319         LColor::color realColor() const;
320
321         ///
322         friend
323         bool operator==(LyXFont const & font1, LyXFont const & font2);
324
325         /// Converts logical attributes to concrete shape attribute
326         LyXFont::FONT_SHAPE realShape() const;
327 private:
328         ///
329         struct FontBits {
330                 ///
331                 bool operator==(FontBits const & fb1) const;
332                 ///
333                 bool operator!=(FontBits const & fb1) const;
334                 ///
335                 FONT_FAMILY family;
336                 ///
337                 FONT_SERIES series;
338                 ///
339                 FONT_SHAPE shape;
340                 ///
341                 FONT_SIZE size;
342                 ///
343                 LColor::color color;
344                 ///
345                 FONT_MISC_STATE emph;
346                 ///
347                 FONT_MISC_STATE underbar;
348                 ///
349                 FONT_MISC_STATE noun;
350                 ///
351                 FONT_MISC_STATE number;
352         };
353
354         ///
355         FontBits bits;
356         
357         ///
358         Language const * lang;
359         
360         /// Sane font
361         static FontBits sane;
362         
363         /// All inherit font
364         static FontBits inherit;
365  
366         /// All ignore font
367         static FontBits ignore;
368
369         /// Updates a misc setting according to request
370         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
371                                          LyXFont::FONT_MISC_STATE org);
372 };
373
374
375 inline
376 LyXFont::FONT_SHAPE LyXFont::shape() const
377 {
378         return bits.shape;
379 }
380
381
382 inline
383 LyXFont::LyXFont()
384         : bits(sane), lang(default_language)
385 {}
386
387
388 inline
389 LyXFont::FONT_FAMILY LyXFont::family() const 
390 {
391         return bits.family;
392 }
393
394
395 inline
396 LyXFont::FONT_SERIES LyXFont::series() const
397 {
398         return bits.series;
399 }
400
401
402 inline
403 LyXFont::FONT_SIZE LyXFont::size() const
404 {
405         return bits.size;
406 }
407
408
409 inline
410 LyXFont::FONT_MISC_STATE LyXFont::emph() const
411 {
412         return bits.emph;
413 }
414
415
416 inline
417 LyXFont::FONT_MISC_STATE LyXFont::noun() const
418 {
419         return bits.noun;
420 }
421
422
423 inline
424 bool LyXFont::isSymbolFont() const
425 {
426         switch(family()) {
427         case LyXFont::SYMBOL_FAMILY:
428         case LyXFont::CMSY_FAMILY:
429         case LyXFont::CMM_FAMILY:
430         case LyXFont::CMEX_FAMILY:
431         case LyXFont::MSA_FAMILY:
432         case LyXFont::MSB_FAMILY:
433                 return true;
434         default:
435                 return false;
436         }
437 }
438
439 ///
440 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
441
442 ///
443 inline
444 bool operator==(LyXFont const & font1, LyXFont const & font2)
445 {
446         return font1.bits == font2.bits &&
447                 font1.lang == font2.lang;
448 }
449
450 ///
451 inline
452 bool operator!=(LyXFont const & font1, LyXFont const & font2)
453 {
454         return !(font1 == font2);
455 }
456 #endif