2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
19 #include FORMS_H_LOCATION
22 #include "direction.h"
25 // It might happen that locale.h defines ON and OFF. This is not good
26 // for us, since we use these names below. But of course this is due
27 // to some old compilers. Than is broken when it comes to C++ scoping.
28 #include "gettext.h" // so that we are sure tht it won't be included
45 /** The value INHERIT_* means that the font attribute is
46 inherited from the layout. In the case of layout fonts, the
47 attribute is inherited from the default font.
48 The value IGNORE_* is used with LyXFont::update() when the
49 attribute should not be changed.
53 ROMAN_FAMILY, // fontstruct rely on this to be 0
69 MEDIUM_SERIES, // fontstruct rely on this to be 0
81 UP_SHAPE, // fontstruct rely on this to be 0
97 SIZE_TINY, // fontstruct rely on this to be 0
126 enum FONT_DIRECTION {
139 /// Used for emph, underbar, noun and latex toggles
140 enum FONT_MISC_STATE {
153 /// Trick to overload constructor and make it megafast
172 /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
173 LyXFont(LyXFont const & x);
175 /// Shortcut initialization
176 LyXFont(LyXFont::FONT_INIT1);
177 /// Shortcut initialization
178 LyXFont(LyXFont::FONT_INIT2);
179 /// Shortcut initialization
180 LyXFont(LyXFont::FONT_INIT3);
182 /// LyXFont x, y; x = y;
183 LyXFont & operator=(LyXFont const & x);
185 /// Decreases font size by one
188 /// Increases font size by one
192 FONT_FAMILY family() const;
195 FONT_SERIES series() const;
198 FONT_SHAPE shape() const;
201 FONT_SIZE size() const;
204 FONT_MISC_STATE emph() const;
207 FONT_MISC_STATE underbar() const;
210 FONT_MISC_STATE noun() const;
213 FONT_MISC_STATE latex() const;
216 LColor::color color() const;
219 FONT_DIRECTION direction() const;
222 LyXDirection getFontDirection() const;
225 LyXFont & setFamily(LyXFont::FONT_FAMILY f);
227 LyXFont & setSeries(LyXFont::FONT_SERIES s);
229 LyXFont & setShape(LyXFont::FONT_SHAPE s);
231 LyXFont & setSize(LyXFont::FONT_SIZE s);
233 LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
235 LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
237 LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
239 LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
241 LyXFont & setColor(LColor::color c);
243 LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
245 /// Set family after LyX text format
246 LyXFont & setLyXFamily(string const &);
248 /// Set series after LyX text format
249 LyXFont & setLyXSeries(string const &);
251 /// Set shape after LyX text format
252 LyXFont & setLyXShape(string const &);
254 /// Set size after LyX text format
255 LyXFont & setLyXSize(string const &);
257 /// Returns misc flag after LyX text format
258 LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
260 /// Sets color after LyX text format
261 LyXFont & setLyXColor(string const &);
263 /// Sets size after GUI name
264 LyXFont & setGUISize(string const &);
266 /// Returns size of font in LaTeX text notation
267 string latexSize() const;
269 /** Updates font settings according to request. If an
270 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(LyXFont const & newfont, bool toggleall = false);
281 /** Reduce font to fall back to template where possible.
282 Equal fields are reduced to INHERIT */
283 void reduce(LyXFont const & tmplt);
285 /// Realize font from a template (INHERIT are realized)
286 LyXFont & realize(LyXFont const & tmplt);
288 /// Is a given font fully resolved?
289 bool resolved() const;
291 /// Read a font specification from LyXLex. Used for layout files.
292 LyXFont & lyxRead(LyXLex &);
294 /// Writes the changes from this font to orgfont in .lyx format in file
295 void lyxWriteChanges(LyXFont const & orgfont, 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(string &, LyXFont const & base,
303 LyXFont const & prev) const;
305 /** Writes tha tail of the LaTeX needed to chagne to this font.
306 Returns number of chars written. Base is the font state we want
309 int latexWriteEndChanges(string &, LyXFont const & base,
310 LyXFont const & next) const;
312 /// Build GUI description of font state
313 string stateText() const;
316 int maxAscent() const;
319 int maxDescent() const;
322 int ascent(char c) const;
325 int descent(char c) const;
328 int width(char c) const;
331 int lbearing(char c) const;
334 int rbearing(char c) const;
337 int textWidth(char const *s, int n) const;
340 int stringWidth(string const & s) const;
343 int signedStringWidth(string const & s) const;
345 /// Draws text and returns width of text
346 int drawText(char const *, int n, Pixmap, int baseline, int x) const;
349 int drawString(string const &, Pixmap pm, int baseline, int x) const;
352 LColor::color realColor() const;
355 XID getFontID() const {
356 return getXFontstruct()->fid;
361 bool operator==(LyXFont const & font1, LyXFont const & font2) {
362 return font1.bits == font2.bits;
367 bool operator!=(LyXFont const & font1, LyXFont const & font2) {
368 return font1.bits != font2.bits;
371 /// compares two fonts, ignoring the setting of the Latex part.
372 bool equalExceptLatex(LyXFont const &) const;
378 bool operator==(FontBits const & fb1) const {
379 return fb1.family == family &&
380 fb1.series == series &&
381 fb1.shape == shape &&
383 fb1.color == color &&
385 fb1.underbar == underbar &&
387 fb1.latex == latex &&
388 fb1.direction == direction;
390 bool operator!=(FontBits const & fb1) const {
391 return !(fb1 == *this);
399 FONT_MISC_STATE emph;
400 FONT_MISC_STATE underbar;
401 FONT_MISC_STATE noun;
402 FONT_MISC_STATE latex;
403 FONT_DIRECTION direction;
406 /// This have to be at least 32 bits, but 64 or more does not hurt
407 typedef unsigned int ui32;
410 /** Representation: bit table
412 11 1111 111 122 222 222 2233
413 Bit 012 34 567 8901 2345 678 901 234 567 8901
414 FFF SS SSS SSSS CCCC EEE UUU NNN LLL
415 aaa ee hhh iiii oooo mmm nnn ooo aaa
416 mmm rr aaa zzzz llll ppp ddd uuu ttt
418 Bit 76543210 76543210 76543210 76543210
430 Some might think this is a dirty representation, but it gives
431 us at least 25% speed-up, so why not? (Asger)
433 First of all it is a maintence nightmare...and now that we need
434 to enlarge the Color bits with 2 (from 4 to 6), we have a problem
435 since a 32 bit entity is not large enough... (Lgb)
489 static FontBits sane;
492 static FontBits inherit;
495 static FontBits ignore;
499 enum { sane = ui32(ROMAN_FAMILY) << Fam_Pos
500 | ui32(MEDIUM_SERIES) << Ser_Pos
501 | ui32(UP_SHAPE) << Sha_Pos
502 | ui32(SIZE_NORMAL) << Siz_Pos
503 | ui32(LColor::none) << Col_Pos
504 | ui32(OFF) << Emp_Pos
505 | ui32(OFF) << Und_Pos
506 | ui32(OFF) << Nou_Pos
507 | ui32(OFF) << Lat_Pos
508 | ui32(LTR_DIR) << Dir_Pos};
511 enum{ inherit = ui32(INHERIT_FAMILY) << Fam_Pos
512 | ui32(INHERIT_SERIES) << Ser_Pos
513 | ui32(INHERIT_SHAPE) << Sha_Pos
514 | ui32(INHERIT_SIZE) << Siz_Pos
515 | ui32(LColor::inherit) << Col_Pos
516 | ui32(INHERIT) << Emp_Pos
517 | ui32(INHERIT) << Und_Pos
518 | ui32(INHERIT) << Nou_Pos
519 | ui32(INHERIT) << Lat_Pos
520 | ui32(INHERIT_DIR) << Dir_Pos};
523 enum{ ignore = ui32(IGNORE_FAMILY) << Fam_Pos
524 | ui32(IGNORE_SERIES) << Ser_Pos
525 | ui32(IGNORE_SHAPE) << Sha_Pos
526 | ui32(IGNORE_SIZE) << Siz_Pos
527 | ui32(LColor::ignore) << Col_Pos
528 | ui32(IGNORE) << Emp_Pos
529 | ui32(IGNORE) << Und_Pos
530 | ui32(IGNORE) << Nou_Pos
531 | ui32(IGNORE) << Lat_Pos
532 | ui32(IGNORE_DIR) << Dir_Pos};
534 /// Updates a misc setting according to request
535 LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
536 LyXFont::FONT_MISC_STATE org);
538 /// Converts logical attributes to concrete shape attribute
539 LyXFont::FONT_SHAPE realShape() const;
542 XFontStruct * getXFontstruct() const;
545 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
547 inline LyXFont::LyXFont()
553 inline LyXFont::LyXFont(LyXFont const & x)
559 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
565 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
571 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
577 inline LyXFont & LyXFont::operator=(LyXFont const & x)
585 // You don't have to understand the stuff below :-)
586 // It works, and it's bloody fast. (Asger)
587 // And impossible to work with. (Lgb)
590 LyXFont::FONT_FAMILY LyXFont::family() const
597 LyXFont::FONT_SERIES LyXFont::series() const
604 LyXFont::FONT_SHAPE LyXFont::shape() const
611 LyXFont::FONT_SIZE LyXFont::size() const
618 LyXFont::FONT_MISC_STATE LyXFont::emph() const
625 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
627 return bits.underbar;
632 LyXFont::FONT_MISC_STATE LyXFont::noun() const
639 LyXFont::FONT_MISC_STATE LyXFont::latex() const
646 LColor::color LyXFont::color() const
653 LyXFont::FONT_DIRECTION LyXFont::direction() const
655 return bits.direction;
659 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
667 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
675 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
683 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
691 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
699 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
707 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
714 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
722 LyXFont & LyXFont::setColor(LColor::color c)
730 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
736 // You don't have to understand the stuff below :-)
737 // It works, and it's bloody fast. (Asger)
738 // And impossible to work with. (Lgb)
740 inline LyXFont::FONT_FAMILY LyXFont::family() const
742 return LyXFont::FONT_FAMILY((bits >> Fam_Pos) & Fam_Mask);
746 inline LyXFont::FONT_SERIES LyXFont::series() const
748 return LyXFont::FONT_SERIES((bits >> Ser_Pos) & Ser_Mask);
752 inline LyXFont::FONT_SHAPE LyXFont::shape() const
754 return LyXFont::FONT_SHAPE((bits >> Sha_Pos) & Sha_Mask);
758 inline LyXFont::FONT_SIZE LyXFont::size() const
760 return LyXFont::FONT_SIZE((bits >> Siz_Pos) & Siz_Mask);
764 inline LyXFont::FONT_MISC_STATE LyXFont::emph() const
766 return LyXFont::FONT_MISC_STATE((bits >> Emp_Pos) & Misc_Mask);
770 inline LyXFont::FONT_MISC_STATE LyXFont::underbar() const
772 return LyXFont::FONT_MISC_STATE((bits >> Und_Pos) & Misc_Mask);
776 inline LyXFont::FONT_MISC_STATE LyXFont::noun() const
778 return LyXFont::FONT_MISC_STATE((bits >> Nou_Pos) & Misc_Mask);
782 inline LyXFont::FONT_MISC_STATE LyXFont::latex() const
784 return LyXFont::FONT_MISC_STATE((bits >> Lat_Pos) & Misc_Mask);
788 inline LColor::color LyXFont::color() const
790 return LColor::color((bits >> Col_Pos) & Col_Mask);
794 inline LyXFont::FONT_DIRECTION LyXFont::direction() const
796 return LyXFont::FONT_DIRECTION((bits >> Dir_Pos) & Dir_Mask);
799 inline LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
801 bits &= ~(Fam_Mask << Fam_Pos);
802 bits |= ui32(f) << Fam_Pos;
807 inline LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
809 bits &= ~(Ser_Mask << Ser_Pos);
810 bits |= ui32(s) << Ser_Pos;
815 inline LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
817 bits &= ~(Sha_Mask << Sha_Pos);
818 bits |= ui32(s) << Sha_Pos;
823 inline LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
825 bits &= ~(Siz_Mask << Siz_Pos);
826 bits |= ui32(s) << Siz_Pos;
831 inline LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
833 bits &= ~(Misc_Mask << Emp_Pos);
834 bits |= ui32(e) << Emp_Pos;
839 inline LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
841 bits &= ~(Misc_Mask << Und_Pos);
842 bits |= ui32(u) << Und_Pos;
847 inline LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
849 bits &= ~(Misc_Mask << Nou_Pos);
850 bits |= ui32(n) << Nou_Pos;
854 inline LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
856 bits &= ~(Misc_Mask << Lat_Pos);
857 bits |= ui32(l) << Lat_Pos;
862 inline LyXFont & LyXFont::setColor(LColor::color c)
864 bits &= ~(Col_Mask << Col_Pos);
865 bits |= ui32(c) << Col_Pos;
870 inline LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
872 bits &= ~(Dir_Mask << Dir_Pos);
873 bits |= ui32(d) << Dir_Pos;