2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
9 * ====================================================== */
18 #include FORMS_H_LOCATION
21 #include "direction.h"
24 // It might happen that locale.h defines ON and OFF. This is not good
25 // for us, since we use these names below. But of course this is due
26 // to some old compilers. Than is broken when it comes to C++ scoping.
27 #include "gettext.h" // so that we are sure tht it won't be included
44 /** The value INHERIT_* means that the font attribute is
45 inherited from the layout. In the case of layout fonts, the
46 attribute is inherited from the default font.
47 The value IGNORE_* is used with LyXFont::update() when the
48 attribute should not be changed.
52 ROMAN_FAMILY, // fontstruct rely on this to be 0
68 MEDIUM_SERIES, // fontstruct rely on this to be 0
80 UP_SHAPE, // fontstruct rely on this to be 0
96 SIZE_TINY, // fontstruct rely on this to be 0
125 enum FONT_DIRECTION {
138 /// Used for emph, underbar, noun and latex toggles
139 enum FONT_MISC_STATE {
184 /// Trick to overload constructor and make it megafast
203 /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
204 LyXFont(LyXFont const & x);
206 /// Shortcut initialization
207 LyXFont(LyXFont::FONT_INIT1);
208 /// Shortcut initialization
209 LyXFont(LyXFont::FONT_INIT2);
210 /// Shortcut initialization
211 LyXFont(LyXFont::FONT_INIT3);
213 /// LyXFont x, y; x = y;
214 LyXFont& operator=(LyXFont const & x);
216 /// Decreases font size by one
219 /// Increases font size by one
223 FONT_FAMILY family() const;
226 FONT_SERIES series() const;
229 FONT_SHAPE shape() const;
232 FONT_SIZE size() const;
235 FONT_MISC_STATE emph() const;
238 FONT_MISC_STATE underbar() const;
241 FONT_MISC_STATE noun() const;
244 FONT_MISC_STATE latex() const;
248 LColor::color color() const;
251 FONT_COLOR color() const;
255 FONT_DIRECTION direction() const;
258 LyXDirection getFontDirection() const;
261 LyXFont & setFamily(LyXFont::FONT_FAMILY f);
263 LyXFont & setSeries(LyXFont::FONT_SERIES s);
265 LyXFont & setShape(LyXFont::FONT_SHAPE s);
267 LyXFont & setSize(LyXFont::FONT_SIZE s);
269 LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
271 LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
273 LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
275 LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
278 LyXFont & setColor(LColor::color c);
281 LyXFont & setColor(LyXFont::FONT_COLOR c);
284 LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
286 /// Set family after LyX text format
287 LyXFont & setLyXFamily(string const &);
289 /// Set series after LyX text format
290 LyXFont & setLyXSeries(string const &);
292 /// Set shape after LyX text format
293 LyXFont & setLyXShape(string const &);
295 /// Set size after LyX text format
296 LyXFont & setLyXSize(string const &);
298 /// Returns misc flag after LyX text format
299 LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
301 /// Sets color after LyX text format
302 LyXFont & setLyXColor(string const &);
304 /// Sets size after GUI name
305 LyXFont & setGUISize(string const &);
307 /// Returns size of font in LaTeX text notation
308 string latexSize() const;
310 /** Updates font settings according to request. If an
311 attribute is IGNORE, the attribute is left as it is. */
313 * When toggleall = true, all properties that matches the font in use
314 * will have the effect that the properties is reset to the
315 * default. If we have a text that is TYPEWRITER_FAMILY, and is
316 * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
317 * a INHERIT_FAMILY was asked for. This is necessary for the
318 * toggle-user-defined-style button on the toolbar.
320 void update(LyXFont const & newfont, bool toggleall = false);
322 /** Reduce font to fall back to template where possible.
323 Equal fields are reduced to INHERIT */
324 void reduce(LyXFont const & tmplt);
326 /// Realize font from a template (INHERIT are realized)
327 LyXFont & realize(LyXFont const & tmplt);
329 /// Is a given font fully resolved?
330 bool resolved() const;
332 /// Read a font specification from LyXLex. Used for layout files.
333 LyXFont & lyxRead(LyXLex &);
335 /// Writes the changes from this font to orgfont in .lyx format in file
336 void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
338 /** Writes the head of the LaTeX needed to change to this font.
339 Writes to string, the head of the LaTeX needed to change
340 to this font. Returns number of chars written. Base is the
341 font state active now.
343 int latexWriteStartChanges(string &, LyXFont const & base,
344 LyXFont const & prev) const;
346 /** Writes tha tail of the LaTeX needed to chagne to this font.
347 Returns number of chars written. Base is the font state we want
350 int latexWriteEndChanges(string &, LyXFont const & base,
351 LyXFont const & next) const;
353 /// Build GUI description of font state
354 string stateText() const;
357 int maxAscent() const;
360 int maxDescent() const;
363 int ascent(char c) const;
366 int descent(char c) const;
369 int width(char c) const;
372 int lbearing(char c) const;
375 int rbearing(char c) const;
378 int textWidth(char const *s, int n) const;
381 int stringWidth(string const & s) const;
384 int signedStringWidth(string const & s) const;
386 /// Draws text and returns width of text
387 int drawText(char const *, int n, Pixmap, int baseline, int x) const;
390 int drawString(string const &, Pixmap pm, int baseline, int x) const;
394 LColor::color realColor() const;
398 XID getFontID() const {
399 return getXFontstruct()->fid;
408 bool operator==(LyXFont const & font1, LyXFont const & font2) {
409 return font1.bits == font2.bits;
414 bool operator!=(LyXFont const & font1, LyXFont const & font2) {
415 return font1.bits != font2.bits;
418 /// compares two fonts, ignoring the setting of the Latex part.
419 bool equalExceptLatex(LyXFont const &) const;
425 bool operator==(FontBits const & fb1) const {
426 return fb1.family == family &&
427 fb1.series == series &&
428 fb1.shape == shape &&
430 fb1.color == color &&
432 fb1.underbar == underbar &&
434 fb1.latex == latex &&
435 fb1.direction == direction;
437 bool operator!=(FontBits const & fb1) const {
438 return !(fb1 == *this);
450 FONT_MISC_STATE emph;
451 FONT_MISC_STATE underbar;
452 FONT_MISC_STATE noun;
453 FONT_MISC_STATE latex;
454 FONT_DIRECTION direction;
457 /// This have to be at least 32 bits, but 64 or more does not hurt
458 typedef unsigned int ui32;
461 /** Representation: bit table
463 11 1111 111 122 222 222 2233
464 Bit 012 34 567 8901 2345 678 901 234 567 8901
465 FFF SS SSS SSSS CCCC EEE UUU NNN LLL
466 aaa ee hhh iiii oooo mmm nnn ooo aaa
467 mmm rr aaa zzzz llll ppp ddd uuu ttt
469 Bit 76543210 76543210 76543210 76543210
481 Some might think this is a dirty representation, but it gives
482 us at least 25% speed-up, so why not? (Asger)
484 First of all it is a maintence nightmare...and now that we need
485 to enlarge the Color bits with 2 (from 4 to 6), we have a problem
486 since a 32 bit entity is not large enough... (Lgb)
540 static FontBits sane;
543 static FontBits inherit;
546 static FontBits ignore;
550 enum { sane = ui32(ROMAN_FAMILY) << Fam_Pos
551 | ui32(MEDIUM_SERIES) << Ser_Pos
552 | ui32(UP_SHAPE) << Sha_Pos
553 | ui32(SIZE_NORMAL) << Siz_Pos
555 | ui32(LColor::none) << Col_Pos
557 | ui32(NONE) << Col_Pos
559 | ui32(OFF) << Emp_Pos
560 | ui32(OFF) << Und_Pos
561 | ui32(OFF) << Nou_Pos
562 | ui32(OFF) << Lat_Pos
563 | ui32(LTR_DIR) << Dir_Pos};
566 enum{ inherit = ui32(INHERIT_FAMILY) << Fam_Pos
567 | ui32(INHERIT_SERIES) << Ser_Pos
568 | ui32(INHERIT_SHAPE) << Sha_Pos
569 | ui32(INHERIT_SIZE) << Siz_Pos
571 | ui32(LColor::inherit) << Col_Pos
573 | ui32(INHERIT_COLOR) << Col_Pos
575 | ui32(INHERIT) << Emp_Pos
576 | ui32(INHERIT) << Und_Pos
577 | ui32(INHERIT) << Nou_Pos
578 | ui32(INHERIT) << Lat_Pos
579 | ui32(INHERIT_DIR) << Dir_Pos};
582 enum{ ignore = ui32(IGNORE_FAMILY) << Fam_Pos
583 | ui32(IGNORE_SERIES) << Ser_Pos
584 | ui32(IGNORE_SHAPE) << Sha_Pos
585 | ui32(IGNORE_SIZE) << Siz_Pos
587 | ui32(LColor::ignore) << Col_Pos
589 | ui32(IGNORE_COLOR) << Col_Pos
591 | ui32(IGNORE) << Emp_Pos
592 | ui32(IGNORE) << Und_Pos
593 | ui32(IGNORE) << Nou_Pos
594 | ui32(IGNORE) << Lat_Pos
595 | ui32(IGNORE_DIR) << Dir_Pos};
597 /// Updates a misc setting according to request
598 LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
599 LyXFont::FONT_MISC_STATE org);
601 /// Converts logical attributes to concrete shape attribute
602 LyXFont::FONT_SHAPE realShape() const;
605 XFontStruct * getXFontstruct() const;
608 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
610 inline LyXFont::LyXFont()
616 inline LyXFont::LyXFont(LyXFont const & x)
622 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
628 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
634 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
640 inline LyXFont & LyXFont::operator=(LyXFont const & x)
648 // You don't have to understand the stuff below :-)
649 // It works, and it's bloody fast. (Asger)
650 // And impossible to work with. (Lgb)
653 LyXFont::FONT_FAMILY LyXFont::family() const
660 LyXFont::FONT_SERIES LyXFont::series() const
667 LyXFont::FONT_SHAPE LyXFont::shape() const
674 LyXFont::FONT_SIZE LyXFont::size() const
681 LyXFont::FONT_MISC_STATE LyXFont::emph() const
688 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
690 return bits.underbar;
695 LyXFont::FONT_MISC_STATE LyXFont::noun() const
702 LyXFont::FONT_MISC_STATE LyXFont::latex() const
710 LColor::color LyXFont::color() const
716 LyXFont::FONT_COLOR LyXFont::color() const
724 LyXFont::FONT_DIRECTION LyXFont::direction() const
726 return bits.direction;
730 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
738 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
746 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
754 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
762 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
770 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
778 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
785 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
794 LyXFont & LyXFont::setColor(LColor::color c)
801 LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
809 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
815 // You don't have to understand the stuff below :-)
816 // It works, and it's bloody fast. (Asger)
817 // And impossible to work with. (Lgb)
819 inline LyXFont::FONT_FAMILY LyXFont::family() const
821 return LyXFont::FONT_FAMILY((bits >> Fam_Pos) & Fam_Mask);
825 inline LyXFont::FONT_SERIES LyXFont::series() const
827 return LyXFont::FONT_SERIES((bits >> Ser_Pos) & Ser_Mask);
831 inline LyXFont::FONT_SHAPE LyXFont::shape() const
833 return LyXFont::FONT_SHAPE((bits >> Sha_Pos) & Sha_Mask);
837 inline LyXFont::FONT_SIZE LyXFont::size() const
839 return LyXFont::FONT_SIZE((bits >> Siz_Pos) & Siz_Mask);
843 inline LyXFont::FONT_MISC_STATE LyXFont::emph() const
845 return LyXFont::FONT_MISC_STATE((bits >> Emp_Pos) & Misc_Mask);
849 inline LyXFont::FONT_MISC_STATE LyXFont::underbar() const
851 return LyXFont::FONT_MISC_STATE((bits >> Und_Pos) & Misc_Mask);
855 inline LyXFont::FONT_MISC_STATE LyXFont::noun() const
857 return LyXFont::FONT_MISC_STATE((bits >> Nou_Pos) & Misc_Mask);
861 inline LyXFont::FONT_MISC_STATE LyXFont::latex() const
863 return LyXFont::FONT_MISC_STATE((bits >> Lat_Pos) & Misc_Mask);
868 inline LColor::color LyXFont::color() const
870 return LColor::color((bits >> Col_Pos) & Col_Mask);
873 inline LyXFont::FONT_COLOR LyXFont::color() const
875 return FONT_COLOR((bits >> Col_Pos) & Col_Mask);
880 inline LyXFont::FONT_DIRECTION LyXFont::direction() const
882 return LyXFont::FONT_DIRECTION((bits >> Dir_Pos) & Dir_Mask);
885 inline LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
887 bits &= ~(Fam_Mask << Fam_Pos);
888 bits |= ui32(f) << Fam_Pos;
893 inline LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
895 bits &= ~(Ser_Mask << Ser_Pos);
896 bits |= ui32(s) << Ser_Pos;
901 inline LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
903 bits &= ~(Sha_Mask << Sha_Pos);
904 bits |= ui32(s) << Sha_Pos;
909 inline LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
911 bits &= ~(Siz_Mask << Siz_Pos);
912 bits |= ui32(s) << Siz_Pos;
917 inline LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
919 bits &= ~(Misc_Mask << Emp_Pos);
920 bits |= ui32(e) << Emp_Pos;
925 inline LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
927 bits &= ~(Misc_Mask << Und_Pos);
928 bits |= ui32(u) << Und_Pos;
933 inline LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
935 bits &= ~(Misc_Mask << Nou_Pos);
936 bits |= ui32(n) << Nou_Pos;
940 inline LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
942 bits &= ~(Misc_Mask << Lat_Pos);
943 bits |= ui32(l) << Lat_Pos;
949 inline LyXFont & LyXFont::setColor(LColor::color c)
951 bits &= ~(Col_Mask << Col_Pos);
952 bits |= ui32(c) << Col_Pos;
956 inline LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
958 bits &= ~(Col_Mask << Col_Pos);
959 bits |= ui32(c) << Col_Pos;
964 inline LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
966 bits &= ~(Dir_Mask << Dir_Pos);
967 bits |= ui32(d) << Dir_Pos;