2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
9 * ====================================================== */
20 #include FORMS_H_LOCATION
23 #include "direction.h"
26 // It might happen that locale.h defines ON and OFF. This is not good
27 // for us, since we use these names below. But of course this is due
28 // to some old compilers. Than is broken when it comes to C++ scoping.
29 #include "gettext.h" // so that we are sure tht it won't be included
46 /** The value INHERIT_* means that the font attribute is
47 inherited from the layout. In the case of layout fonts, the
48 attribute is inherited from the default font.
49 The value IGNORE_* is used with LyXFont::update() when the
50 attribute should not be changed.
54 ROMAN_FAMILY, // fontstruct rely on this to be 0
70 MEDIUM_SERIES, // fontstruct rely on this to be 0
82 UP_SHAPE, // fontstruct rely on this to be 0
98 SIZE_TINY, // fontstruct rely on this to be 0
127 enum FONT_DIRECTION {
140 /// Used for emph, underbar, noun and latex toggles
141 enum FONT_MISC_STATE {
186 /// Trick to overload constructor and make it megafast
205 /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
206 LyXFont(LyXFont const & x);
208 /// Shortcut initialization
209 LyXFont(LyXFont::FONT_INIT1);
210 /// Shortcut initialization
211 LyXFont(LyXFont::FONT_INIT2);
212 /// Shortcut initialization
213 LyXFont(LyXFont::FONT_INIT3);
215 /// LyXFont x, y; x = y;
216 LyXFont& operator=(LyXFont const & x);
218 /// Decreases font size by one
221 /// Increases font size by one
225 FONT_FAMILY family() const;
228 FONT_SERIES series() const;
231 FONT_SHAPE shape() const;
234 FONT_SIZE size() const;
237 FONT_MISC_STATE emph() const;
240 FONT_MISC_STATE underbar() const;
243 FONT_MISC_STATE noun() const;
246 FONT_MISC_STATE latex() const;
250 LColor::color color() const;
253 FONT_COLOR color() const;
257 FONT_DIRECTION direction() const;
260 LyXDirection getFontDirection() const;
263 LyXFont & setFamily(LyXFont::FONT_FAMILY f);
265 LyXFont & setSeries(LyXFont::FONT_SERIES s);
267 LyXFont & setShape(LyXFont::FONT_SHAPE s);
269 LyXFont & setSize(LyXFont::FONT_SIZE s);
271 LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
273 LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
275 LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
277 LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
280 LyXFont & setColor(LColor::color c);
283 LyXFont & setColor(LyXFont::FONT_COLOR c);
286 LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
288 /// Set family after LyX text format
289 LyXFont & setLyXFamily(string const &);
291 /// Set series after LyX text format
292 LyXFont & setLyXSeries(string const &);
294 /// Set shape after LyX text format
295 LyXFont & setLyXShape(string const &);
297 /// Set size after LyX text format
298 LyXFont & setLyXSize(string const &);
300 /// Returns misc flag after LyX text format
301 LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
303 /// Sets color after LyX text format
304 LyXFont & setLyXColor(string const &);
306 /// Sets size after GUI name
307 LyXFont & setGUISize(string const &);
309 /// Returns size of font in LaTeX text notation
310 string latexSize() const;
312 /** Updates font settings according to request. If an
313 attribute is IGNORE, the attribute is left as it is. */
315 * When toggleall = true, all properties that matches the font in use
316 * will have the effect that the properties is reset to the
317 * default. If we have a text that is TYPEWRITER_FAMILY, and is
318 * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
319 * a INHERIT_FAMILY was asked for. This is necessary for the
320 * toggle-user-defined-style button on the toolbar.
322 void update(LyXFont const & newfont, bool toggleall = false);
324 /** Reduce font to fall back to template where possible.
325 Equal fields are reduced to INHERIT */
326 void reduce(LyXFont const & tmplt);
328 /// Realize font from a template (INHERIT are realized)
329 LyXFont & realize(LyXFont const & tmplt);
331 /// Is a given font fully resolved?
332 bool resolved() const;
334 /// Read a font specification from LyXLex. Used for layout files.
335 LyXFont & lyxRead(LyXLex &);
337 /// Writes the changes from this font to orgfont in .lyx format in file
338 void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
340 /** Writes the head of the LaTeX needed to change to this font.
341 Writes to string, the head of the LaTeX needed to change
342 to this font. Returns number of chars written. Base is the
343 font state active now.
345 int latexWriteStartChanges(string &, LyXFont const & base,
346 LyXFont const & prev) const;
348 /** Writes tha tail of the LaTeX needed to chagne to this font.
349 Returns number of chars written. Base is the font state we want
352 int latexWriteEndChanges(string &, LyXFont const & base,
353 LyXFont const & next) const;
355 /// Build GUI description of font state
356 string stateText() const;
359 int maxAscent() const;
362 int maxDescent() const;
365 int ascent(char c) const;
368 int descent(char c) const;
371 int width(char c) const;
374 int lbearing(char c) const;
377 int rbearing(char c) const;
380 int textWidth(char const *s, int n) const;
383 int stringWidth(string const & s) const;
386 int signedStringWidth(string const & s) const;
388 /// Draws text and returns width of text
389 int drawText(char const *, int n, Pixmap, int baseline, int x) const;
392 int drawString(string const &, Pixmap pm, int baseline, int x) const;
396 LColor::color realColor() const;
400 XID getFontID() const {
401 return getXFontstruct()->fid;
410 bool operator==(LyXFont const & font1, LyXFont const & font2) {
411 return font1.bits == font2.bits;
416 bool operator!=(LyXFont const & font1, LyXFont const & font2) {
417 return font1.bits != font2.bits;
420 /// compares two fonts, ignoring the setting of the Latex part.
421 bool equalExceptLatex(LyXFont const &) const;
427 bool operator==(FontBits const & fb1) const {
428 return fb1.family == family &&
429 fb1.series == series &&
430 fb1.shape == shape &&
432 fb1.color == color &&
434 fb1.underbar == underbar &&
436 fb1.latex == latex &&
437 fb1.direction == direction;
439 bool operator!=(FontBits const & fb1) const {
440 return !(fb1 == *this);
452 FONT_MISC_STATE emph;
453 FONT_MISC_STATE underbar;
454 FONT_MISC_STATE noun;
455 FONT_MISC_STATE latex;
456 FONT_DIRECTION direction;
459 /// This have to be at least 32 bits, but 64 or more does not hurt
460 typedef unsigned int ui32;
463 /** Representation: bit table
465 11 1111 111 122 222 222 2233
466 Bit 012 34 567 8901 2345 678 901 234 567 8901
467 FFF SS SSS SSSS CCCC EEE UUU NNN LLL
468 aaa ee hhh iiii oooo mmm nnn ooo aaa
469 mmm rr aaa zzzz llll ppp ddd uuu ttt
471 Bit 76543210 76543210 76543210 76543210
483 Some might think this is a dirty representation, but it gives
484 us at least 25% speed-up, so why not? (Asger)
486 First of all it is a maintence nightmare...and now that we need
487 to enlarge the Color bits with 2 (from 4 to 6), we have a problem
488 since a 32 bit entity is not large enough... (Lgb)
542 static FontBits sane;
545 static FontBits inherit;
548 static FontBits ignore;
552 enum { sane = ui32(ROMAN_FAMILY) << Fam_Pos
553 | ui32(MEDIUM_SERIES) << Ser_Pos
554 | ui32(UP_SHAPE) << Sha_Pos
555 | ui32(SIZE_NORMAL) << Siz_Pos
557 | ui32(LColor::none) << Col_Pos
559 | ui32(NONE) << Col_Pos
561 | ui32(OFF) << Emp_Pos
562 | ui32(OFF) << Und_Pos
563 | ui32(OFF) << Nou_Pos
564 | ui32(OFF) << Lat_Pos
565 | ui32(LTR_DIR) << Dir_Pos};
568 enum{ inherit = ui32(INHERIT_FAMILY) << Fam_Pos
569 | ui32(INHERIT_SERIES) << Ser_Pos
570 | ui32(INHERIT_SHAPE) << Sha_Pos
571 | ui32(INHERIT_SIZE) << Siz_Pos
573 | ui32(LColor::inherit) << Col_Pos
575 | ui32(INHERIT_COLOR) << Col_Pos
577 | ui32(INHERIT) << Emp_Pos
578 | ui32(INHERIT) << Und_Pos
579 | ui32(INHERIT) << Nou_Pos
580 | ui32(INHERIT) << Lat_Pos
581 | ui32(INHERIT_DIR) << Dir_Pos};
584 enum{ ignore = ui32(IGNORE_FAMILY) << Fam_Pos
585 | ui32(IGNORE_SERIES) << Ser_Pos
586 | ui32(IGNORE_SHAPE) << Sha_Pos
587 | ui32(IGNORE_SIZE) << Siz_Pos
589 | ui32(LColor::ignore) << Col_Pos
591 | ui32(IGNORE_COLOR) << Col_Pos
593 | ui32(IGNORE) << Emp_Pos
594 | ui32(IGNORE) << Und_Pos
595 | ui32(IGNORE) << Nou_Pos
596 | ui32(IGNORE) << Lat_Pos
597 | ui32(IGNORE_DIR) << Dir_Pos};
599 /// Updates a misc setting according to request
600 LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
601 LyXFont::FONT_MISC_STATE org);
603 /// Converts logical attributes to concrete shape attribute
604 LyXFont::FONT_SHAPE realShape() const;
607 XFontStruct * getXFontstruct() const;
610 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
612 inline LyXFont::LyXFont()
618 inline LyXFont::LyXFont(LyXFont const & x)
624 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
630 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
636 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
642 inline LyXFont & LyXFont::operator=(LyXFont const & x)
650 // You don't have to understand the stuff below :-)
651 // It works, and it's bloody fast. (Asger)
652 // And impossible to work with. (Lgb)
655 LyXFont::FONT_FAMILY LyXFont::family() const
662 LyXFont::FONT_SERIES LyXFont::series() const
669 LyXFont::FONT_SHAPE LyXFont::shape() const
676 LyXFont::FONT_SIZE LyXFont::size() const
683 LyXFont::FONT_MISC_STATE LyXFont::emph() const
690 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
692 return bits.underbar;
697 LyXFont::FONT_MISC_STATE LyXFont::noun() const
704 LyXFont::FONT_MISC_STATE LyXFont::latex() const
712 LColor::color LyXFont::color() const
718 LyXFont::FONT_COLOR LyXFont::color() const
726 LyXFont::FONT_DIRECTION LyXFont::direction() const
728 return bits.direction;
732 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
740 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
748 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
756 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
764 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
772 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
780 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
787 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
796 LyXFont & LyXFont::setColor(LColor::color c)
803 LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
811 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
817 // You don't have to understand the stuff below :-)
818 // It works, and it's bloody fast. (Asger)
819 // And impossible to work with. (Lgb)
821 inline LyXFont::FONT_FAMILY LyXFont::family() const
823 return LyXFont::FONT_FAMILY((bits >> Fam_Pos) & Fam_Mask);
827 inline LyXFont::FONT_SERIES LyXFont::series() const
829 return LyXFont::FONT_SERIES((bits >> Ser_Pos) & Ser_Mask);
833 inline LyXFont::FONT_SHAPE LyXFont::shape() const
835 return LyXFont::FONT_SHAPE((bits >> Sha_Pos) & Sha_Mask);
839 inline LyXFont::FONT_SIZE LyXFont::size() const
841 return LyXFont::FONT_SIZE((bits >> Siz_Pos) & Siz_Mask);
845 inline LyXFont::FONT_MISC_STATE LyXFont::emph() const
847 return LyXFont::FONT_MISC_STATE((bits >> Emp_Pos) & Misc_Mask);
851 inline LyXFont::FONT_MISC_STATE LyXFont::underbar() const
853 return LyXFont::FONT_MISC_STATE((bits >> Und_Pos) & Misc_Mask);
857 inline LyXFont::FONT_MISC_STATE LyXFont::noun() const
859 return LyXFont::FONT_MISC_STATE((bits >> Nou_Pos) & Misc_Mask);
863 inline LyXFont::FONT_MISC_STATE LyXFont::latex() const
865 return LyXFont::FONT_MISC_STATE((bits >> Lat_Pos) & Misc_Mask);
870 inline LColor::color LyXFont::color() const
872 return LColor::color((bits >> Col_Pos) & Col_Mask);
875 inline LyXFont::FONT_COLOR LyXFont::color() const
877 return FONT_COLOR((bits >> Col_Pos) & Col_Mask);
882 inline LyXFont::FONT_DIRECTION LyXFont::direction() const
884 return LyXFont::FONT_DIRECTION((bits >> Dir_Pos) & Dir_Mask);
887 inline LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
889 bits &= ~(Fam_Mask << Fam_Pos);
890 bits |= ui32(f) << Fam_Pos;
895 inline LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
897 bits &= ~(Ser_Mask << Ser_Pos);
898 bits |= ui32(s) << Ser_Pos;
903 inline LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
905 bits &= ~(Sha_Mask << Sha_Pos);
906 bits |= ui32(s) << Sha_Pos;
911 inline LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
913 bits &= ~(Siz_Mask << Siz_Pos);
914 bits |= ui32(s) << Siz_Pos;
919 inline LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
921 bits &= ~(Misc_Mask << Emp_Pos);
922 bits |= ui32(e) << Emp_Pos;
927 inline LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
929 bits &= ~(Misc_Mask << Und_Pos);
930 bits |= ui32(u) << Und_Pos;
935 inline LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
937 bits &= ~(Misc_Mask << Nou_Pos);
938 bits |= ui32(n) << Nou_Pos;
942 inline LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
944 bits &= ~(Misc_Mask << Lat_Pos);
945 bits |= ui32(l) << Lat_Pos;
951 inline LyXFont & LyXFont::setColor(LColor::color c)
953 bits &= ~(Col_Mask << Col_Pos);
954 bits |= ui32(c) << Col_Pos;
958 inline LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
960 bits &= ~(Col_Mask << Col_Pos);
961 bits |= ui32(c) << Col_Pos;
966 inline LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
968 bits &= ~(Dir_Mask << Dir_Pos);
969 bits |= ui32(d) << Dir_Pos;