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
12 * Full author contact details are available in file CREDITS.
19 #include "tex2lyx/Font.h"
23 #include "support/docstream.h"
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.
46 ROMAN_FAMILY, // fontstruct rely on this to be 0
76 NUM_FAMILIES = INHERIT_FAMILY
82 MEDIUM_SERIES, // fontstruct rely on this to be 0
94 UP_SHAPE, // fontstruct rely on this to be 0
110 SIZE_TINY, // fontstruct rely on this to be 0
139 /// Used for emph, underbar, noun and latex toggles
140 enum FONT_MISC_STATE {
153 /// Trick to overload constructor and make it megafast
178 /** We store the Color::color value as an int to get Color.h out
179 * of the header file.
183 FONT_MISC_STATE emph;
185 FONT_MISC_STATE underbar;
187 FONT_MISC_STATE noun;
189 FONT_MISC_STATE number;
194 /// Shortcut initialization
195 explicit Font(Font::FONT_INIT1);
196 /// Shortcut initialization
197 explicit Font(Font::FONT_INIT2);
198 /// Shortcut initialization
199 explicit Font(Font::FONT_INIT3);
200 /// Shortcut initialization
201 Font(Font::FONT_INIT1, Language const * l);
202 /// Shortcut initialization
203 Font(Font::FONT_INIT2, Language const * l);
204 /// Shortcut initialization
205 Font(Font::FONT_INIT3, Language const * l);
207 /// Decreases font size by one
209 /// Increases font size by one
212 FONT_FAMILY family() const { return bits.family; }
214 FONT_SERIES series() const { return bits.series; }
216 FONT_SHAPE shape() const { return bits.shape; }
218 FONT_SIZE size() const { return bits.size; }
220 FONT_MISC_STATE emph() const { return bits.emph; }
222 FONT_MISC_STATE underbar() const { return bits.underbar; }
224 FONT_MISC_STATE noun() const { return bits.noun; }
226 FONT_MISC_STATE number() const { return bits.number; }
228 Color_color color() const;
230 Language const * language() const { return lang; }
232 bool isRightToLeft() const;
234 bool isVisibleRightToLeft() const;
236 bool isSymbolFont() const;
239 void setFamily(Font::FONT_FAMILY f);
240 void setSeries(Font::FONT_SERIES s);
241 void setShape(Font::FONT_SHAPE s);
242 void setSize(Font::FONT_SIZE s);
243 void setEmph(Font::FONT_MISC_STATE e);
244 void setUnderbar(Font::FONT_MISC_STATE u);
245 void setNoun(Font::FONT_MISC_STATE n);
246 void setNumber(Font::FONT_MISC_STATE n);
247 void setColor(Color_color c);
248 void setLanguage(Language const * l);
250 /// Set family after LyX text format
251 Font & setLyXFamily(std::string const &);
253 /// Set series after LyX text format
254 Font & setLyXSeries(std::string const &);
256 /// Set shape after LyX text format
257 Font & setLyXShape(std::string const &);
259 /// Set size after LyX text format
260 Font & setLyXSize(std::string const &);
262 /// Returns misc flag after LyX text format
263 Font::FONT_MISC_STATE setLyXMisc(std::string const &);
265 /// Sets color after LyX text format
266 Font & setLyXColor(std::string const &);
268 /// Returns size of font in LaTeX text notation
269 std::string const latexSize() const;
271 /** Updates font settings according to request.
272 If an attribute is IGNORE, the attribute is left as it is.
273 When toggleall = true, all properties that matches the font in use
274 will have the effect that the properties is reset to the
275 default. If we have a text that is TYPEWRITER_FAMILY, and is
276 update()'ed with TYPEWRITER_FAMILY, the operation will be as if
277 a INHERIT_FAMILY was asked for. This is necessary for the
278 toggle-user-defined-style button on the toolbar.
280 void update(Font const & newfont,
281 Language const * default_lang,
282 bool toggleall = false);
284 /** Reduce font to fall back to template where possible.
285 Equal fields are reduced to INHERIT */
286 void reduce(Font const & tmplt);
288 /// Realize font from a template (INHERIT are realized)
289 Font & realize(Font const & tmplt);
290 /// Is a given font fully resolved?
291 bool resolved() const;
293 /// Read a font specification from Lexer. Used for layout files.
294 Font & lyxRead(Lexer &);
296 /// Writes the changes from this font to orgfont in .lyx format in file
297 void lyxWriteChanges(Font const & orgfont, std::ostream &) const;
299 /** Writes the head of the LaTeX needed to change to this font.
300 Writes to string, the head of the LaTeX needed to change
301 to this font. Returns number of chars written. Base is the
302 font state active now.
304 int latexWriteStartChanges(odocstream &, BufferParams const & bparams,
305 OutputParams const & runparams,
307 Font const & prev) const;
309 /** Writes the tail of the LaTeX needed to change to this font.
310 Returns number of chars written. Base is the font state we want
313 int latexWriteEndChanges(odocstream &, BufferParams const & bparams,
314 OutputParams const & runparams,
317 bool const & closeLanguage = true) const;
320 /// Build GUI description of font state
321 docstring const stateText(BufferParams * params) const;
324 Color_color realColor() const;
328 bool operator==(Font const & font1, Font const & font2);
331 std::ostream & operator<<(std::ostream & os, Font const & font);
333 /// Converts logical attributes to concrete shape attribute
334 // Try hard to inline this as it shows up with 4.6 % in the profiler.
335 inline Font::FONT_SHAPE realShape() const {
337 return SMALLCAPS_SHAPE;
339 return (bits.shape == UP_SHAPE) ? ITALIC_SHAPE : UP_SHAPE;
344 /** Compaq cxx 6.5 requires that the definition be public so that
345 it can compile operator==()
351 Language const * lang;
353 static FontBits sane;
355 static FontBits inherit;
357 static FontBits ignore;
358 /// Updates a misc setting according to request
359 Font::FONT_MISC_STATE setMisc(Font::FONT_MISC_STATE newfont,
360 Font::FONT_MISC_STATE org);
361 /// Did latexWriteStartChanges open an encoding environment?
362 mutable bool open_encoding_;
366 /** \c Font_size is a wrapper for Font::FONT_SIZE.
367 * It can be forward-declared and passed as a function argument without
368 * having to expose Font.h.
373 Font_size(Font::FONT_SIZE val) : val_(val) {}
375 operator Font::FONT_SIZE() const { return val_; }
378 Font::FONT_SIZE val_;
384 bool Font::isSymbolFont() const
387 case Font::SYMBOL_FAMILY:
388 case Font::CMSY_FAMILY:
389 case Font::CMM_FAMILY:
390 case Font::CMEX_FAMILY:
391 case Font::MSA_FAMILY:
392 case Font::MSB_FAMILY:
393 case Font::WASY_FAMILY:
394 case Font::ESINT_FAMILY:
402 std::ostream & operator<<(std::ostream &, Font::FONT_MISC_STATE);
404 bool operator==(Font::FontBits const & lhs, Font::FontBits const & rhs);
407 bool operator!=(Font::FontBits const & lhs, Font::FontBits const & rhs)
409 return !(lhs == rhs);
414 bool operator==(Font const & font1, Font const & font2)
416 return font1.bits == font2.bits && font1.lang == font2.lang;
421 bool operator!=(Font const & font1, Font const & font2)
423 return !(font1 == font2);