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