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