scramble email addresses
[lyx.git] / src / Font.h
blob6cd84e53b92147360438329b78536285741e7a15
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.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
10 * \author Dekel Tsur
12 * Full author contact details are available in file CREDITS.
15 #ifndef FONT_H
16 #define FONT_H
18 #ifdef TEX2LYX
19 #include "tex2lyx/Font.h"
20 #else
22 #include "Color.h"
23 #include "support/docstream.h"
26 namespace lyx {
28 class Lexer;
29 class BufferParams;
30 class Language;
31 class LaTeXFeatures;
32 class OutputParams;
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.
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
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
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,
101 INHERIT_SHAPE,
103 IGNORE_SHAPE
107 enum FONT_SIZE {
109 SIZE_TINY, // fontstruct rely on this to be 0
111 SIZE_SCRIPT,
113 SIZE_FOOTNOTE,
115 SIZE_SMALL,
117 SIZE_NORMAL,
119 SIZE_LARGE,
121 SIZE_LARGER,
123 SIZE_LARGEST,
125 SIZE_HUGE,
127 SIZE_HUGER,
129 INCREASE_SIZE,
131 DECREASE_SIZE,
133 INHERIT_SIZE,
135 IGNORE_SIZE
138 /// Used for emph, underbar, noun and latex toggles
139 enum FONT_MISC_STATE {
141 OFF,
145 TOGGLE,
147 INHERIT,
149 IGNORE
152 /// Trick to overload constructor and make it megafast
153 enum FONT_INIT1 {
155 ALL_INHERIT
158 enum FONT_INIT2 {
160 ALL_IGNORE
163 enum FONT_INIT3 {
165 ALL_SANE
168 struct FontBits {
170 FONT_FAMILY family;
172 FONT_SERIES series;
174 FONT_SHAPE shape;
176 FONT_SIZE size;
177 /** We store the Color::color value as an int to get Color.h out
178 * of the header file.
180 int color;
182 FONT_MISC_STATE emph;
184 FONT_MISC_STATE underbar;
186 FONT_MISC_STATE noun;
188 FONT_MISC_STATE number;
191 Font();
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);
206 /// Decreases font size by one
207 Font & decSize();
208 /// Increases font size by one
209 Font & incSize();
211 FONT_FAMILY family() const { return bits.family; }
213 FONT_SERIES series() const { return bits.series; }
215 FONT_SHAPE shape() const { return bits.shape; }
217 FONT_SIZE size() const { return bits.size; }
219 FONT_MISC_STATE emph() const { return bits.emph; }
221 FONT_MISC_STATE underbar() const { return bits.underbar; }
223 FONT_MISC_STATE noun() const { return bits.noun; }
225 FONT_MISC_STATE number() const { return bits.number; }
227 Color_color color() const;
229 Language const * language() const { return lang; }
231 bool isRightToLeft() const;
233 bool isVisibleRightToLeft() const;
235 bool isSymbolFont() const;
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);
249 /// Set family after LyX text format
250 Font & setLyXFamily(std::string const &);
252 /// Set series after LyX text format
253 Font & setLyXSeries(std::string const &);
255 /// Set shape after LyX text format
256 Font & setLyXShape(std::string const &);
258 /// Set size after LyX text format
259 Font & setLyXSize(std::string const &);
261 /// Returns misc flag after LyX text format
262 Font::FONT_MISC_STATE setLyXMisc(std::string const &);
264 /// Sets color after LyX text format
265 Font & setLyXColor(std::string const &);
267 /// Returns size of font in LaTeX text notation
268 std::string const latexSize() const;
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.
279 void update(Font const & newfont,
280 Language const * default_lang,
281 bool toggleall = false);
283 /** Reduce font to fall back to template where possible.
284 Equal fields are reduced to INHERIT */
285 void reduce(Font const & tmplt);
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;
292 /// Read a font specification from Lexer. Used for layout files.
293 Font & lyxRead(Lexer &);
295 /// Writes the changes from this font to orgfont in .lyx format in file
296 void lyxWriteChanges(Font const & orgfont, std::ostream &) const;
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.
303 int latexWriteStartChanges(odocstream &, BufferParams const & bparams,
304 OutputParams const & runparams,
305 Font const & base,
306 Font const & prev) const;
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.
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;
319 /// Build GUI description of font state
320 docstring const stateText(BufferParams * params) const;
323 Color_color realColor() const;
326 void validate(LaTeXFeatures & features) const;
329 friend
330 bool operator==(Font const & font1, Font const & font2);
332 friend
333 std::ostream & operator<<(std::ostream & os, Font const & font);
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;
345 /// Set \param data using \param font and \param toggle.
346 std::string toString(bool toggle) const;
348 /// Set \param font and \param toggle using \param data. Return success.
349 bool fromString(std::string const & data, bool & toggle);
351 /** Compaq cxx 6.5 requires that the definition be public so that
352 it can compile operator==()
354 private:
356 FontBits bits;
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_;
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.
377 class Font_size {
378 public:
380 Font_size(Font::FONT_SIZE val) : val_(val) {}
382 operator Font::FONT_SIZE() const { return val_; }
383 private:
385 Font::FONT_SIZE val_;
390 inline
391 bool Font::isSymbolFont() const
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;
409 std::ostream & operator<<(std::ostream &, Font::FONT_MISC_STATE);
411 bool operator==(Font::FontBits const & lhs, Font::FontBits const & rhs);
413 inline
414 bool operator!=(Font::FontBits const & lhs, Font::FontBits const & rhs)
416 return !(lhs == rhs);
420 inline
421 bool operator==(Font const & font1, Font const & font2)
423 return font1.bits == font2.bits && font1.lang == font2.lang;
427 inline
428 bool operator!=(Font const & font1, Font const & font2)
430 return !(font1 == font2);
433 /** Returns the current freefont, encoded as a std::string to be passed to the
434 * frontends.
436 std::string const freefont2string();
439 } // namespace lyx
441 #endif // TEX2LYX
442 #endif