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"
36 /** The value INHERIT_* means that the font attribute is
37 inherited from the layout. In the case of layout fonts, the
38 attribute is inherited from the default font.
39 The value IGNORE_* is used with Font::update() when the
40 attribute should not be changed.
44 ROMAN_FAMILY, // fontstruct rely on this to be 0
74 NUM_FAMILIES = INHERIT_FAMILY
80 MEDIUM_SERIES, // fontstruct rely on this to be 0
92 UP_SHAPE, // fontstruct rely on this to be 0
108 SIZE_TINY, // fontstruct rely on this to be 0
137 /// Used for emph, underbar, noun and latex toggles
138 enum FONT_MISC_STATE {
151 /// Trick to overload constructor and make it megafast
176 /** We store the Color::color value as an int to get Color.h out
177 * of the header file.
181 FONT_MISC_STATE emph;
183 FONT_MISC_STATE underbar;
185 FONT_MISC_STATE noun;
187 FONT_MISC_STATE number;
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);
205 /// Decreases font size by one
207 /// Increases font size by one
210 FONT_FAMILY family() const { return bits.family; }
212 FONT_SERIES series() const { return bits.series; }
214 FONT_SHAPE shape() const { return bits.shape; }
216 FONT_SIZE size() const { return bits.size; }
218 FONT_MISC_STATE emph() const { return bits.emph; }
220 FONT_MISC_STATE underbar() const { return bits.underbar; }
222 FONT_MISC_STATE noun() const { return bits.noun; }
224 FONT_MISC_STATE number() const { return bits.number; }
226 Color_color color() const;
228 Language const * language() const { return lang; }
230 bool isRightToLeft() const;
232 bool isVisibleRightToLeft() const;
234 bool isSymbolFont() const;
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(Color_color c);
246 void setLanguage(Language const * l);
248 /// Set family after LyX text format
249 Font & setLyXFamily(std::string const &);
251 /// Set series after LyX text format
252 Font & setLyXSeries(std::string const &);
254 /// Set shape after LyX text format
255 Font & setLyXShape(std::string const &);
257 /// Set size after LyX text format
258 Font & setLyXSize(std::string const &);
260 /// Returns misc flag after LyX text format
261 Font::FONT_MISC_STATE setLyXMisc(std::string const &);
263 /// Sets color after LyX text format
264 Font & setLyXColor(std::string const &);
266 /// Returns size of font in LaTeX text notation
267 std::string const latexSize() const;
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.
278 void update(Font const & newfont,
279 Language const * default_lang,
280 bool toggleall = false);
282 /** Reduce font to fall back to template where possible.
283 Equal fields are reduced to INHERIT */
284 void reduce(Font const & tmplt);
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;
291 /// Read a font specification from Lexer. Used for layout files.
292 Font & lyxRead(Lexer &);
294 /// Writes the changes from this font to orgfont in .lyx format in file
295 void lyxWriteChanges(Font const & orgfont, std::ostream &) const;
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.
302 int latexWriteStartChanges(odocstream &, BufferParams const & bparams,
303 OutputParams const & runparams,
305 Font const & prev) const;
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
311 int latexWriteEndChanges(odocstream &, BufferParams const & bparams,
312 OutputParams const & runparams,
315 bool const & closeLanguage = true) const;
318 /// Build GUI description of font state
319 docstring const stateText(BufferParams * params) const;
322 Color_color realColor() const;
326 bool operator==(Font const & font1, Font const & font2);
329 std::ostream & operator<<(std::ostream & os, Font const & font);
331 /// Converts logical attributes to concrete shape attribute
332 // Try hard to inline this as it shows up with 4.6 % in the profiler.
333 inline Font::FONT_SHAPE realShape() const {
335 return SMALLCAPS_SHAPE;
337 return (bits.shape == UP_SHAPE) ? ITALIC_SHAPE : UP_SHAPE;
341 /// Set \param data using \param font and \param toggle.
342 std::string toString(bool toggle) const;
344 /// Set \param font and \param toggle using \param data. Return success.
345 bool fromString(std::string const & data, bool & toggle);
347 /** Compaq cxx 6.5 requires that the definition be public so that
348 it can compile operator==()
354 Language const * lang;
356 static FontBits sane;
358 static FontBits inherit;
360 static FontBits ignore;
361 /// Updates a misc setting according to request
362 Font::FONT_MISC_STATE setMisc(Font::FONT_MISC_STATE newfont,
363 Font::FONT_MISC_STATE org);
364 /// Did latexWriteStartChanges open an encoding environment?
365 mutable bool open_encoding_;
369 /** \c Font_size is a wrapper for Font::FONT_SIZE.
370 * It can be forward-declared and passed as a function argument without
371 * having to expose Font.h.
376 Font_size(Font::FONT_SIZE val) : val_(val) {}
378 operator Font::FONT_SIZE() const { return val_; }
381 Font::FONT_SIZE val_;
387 bool Font::isSymbolFont() const
390 case Font::SYMBOL_FAMILY:
391 case Font::CMSY_FAMILY:
392 case Font::CMM_FAMILY:
393 case Font::CMEX_FAMILY:
394 case Font::MSA_FAMILY:
395 case Font::MSB_FAMILY:
396 case Font::WASY_FAMILY:
397 case Font::ESINT_FAMILY:
405 std::ostream & operator<<(std::ostream &, Font::FONT_MISC_STATE);
407 bool operator==(Font::FontBits const & lhs, Font::FontBits const & rhs);
410 bool operator!=(Font::FontBits const & lhs, Font::FontBits const & rhs)
412 return !(lhs == rhs);
417 bool operator==(Font const & font1, Font const & font2)
419 return font1.bits == font2.bits && font1.lang == font2.lang;
424 bool operator!=(Font const & font1, Font const & font2)
426 return !(font1 == font2);
429 /** Returns the current freefont, encoded as a std::string to be passed to the
432 std::string const freefont2string();