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"
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.
45 ROMAN_FAMILY
, // fontstruct rely on this to be 0
75 NUM_FAMILIES
= INHERIT_FAMILY
81 MEDIUM_SERIES
, // fontstruct rely on this to be 0
93 UP_SHAPE
, // fontstruct rely on this to be 0
109 SIZE_TINY
, // fontstruct rely on this to be 0
138 /// Used for emph, underbar, noun and latex toggles
139 enum FONT_MISC_STATE
{
152 /// Trick to overload constructor and make it megafast
177 /** We store the Color::color value as an int to get Color.h out
178 * of the header file.
182 FONT_MISC_STATE emph
;
184 FONT_MISC_STATE underbar
;
186 FONT_MISC_STATE noun
;
188 FONT_MISC_STATE number
;
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
208 /// Increases font size by one
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
,
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
312 int latexWriteEndChanges(odocstream
&, BufferParams
const & bparams
,
313 OutputParams
const & runparams
,
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;
330 bool operator==(Font
const & font1
, Font
const & font2
);
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 {
339 return SMALLCAPS_SHAPE
;
341 return (bits
.shape
== UP_SHAPE
) ? ITALIC_SHAPE
: UP_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==()
358 Language
const * lang
;
360 static FontBits sane
;
362 static FontBits inherit
;
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.
380 Font_size(Font::FONT_SIZE val
) : val_(val
) {}
382 operator Font::FONT_SIZE() const { return val_
; }
385 Font::FONT_SIZE val_
;
391 bool Font::isSymbolFont() const
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
:
409 std::ostream
& operator<<(std::ostream
&, Font::FONT_MISC_STATE
);
411 bool operator==(Font::FontBits
const & lhs
, Font::FontBits
const & rhs
);
414 bool operator!=(Font::FontBits
const & lhs
, Font::FontBits
const & rhs
)
416 return !(lhs
== rhs
);
421 bool operator==(Font
const & font1
, Font
const & font2
)
423 return font1
.bits
== font2
.bits
&& font1
.lang
== font2
.lang
;
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
436 std::string
const freefont2string();