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