]> git.lyx.org Git - features.git/blob - src/lyxfont.h
applied the hebrew patch
[features.git] / src / lyxfont.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1995 Matthias Ettrich
8  *
9  * ====================================================== */
10
11 #ifndef LYXFONT_H
12 #define LYXFONT_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include FORMS_H_LOCATION
19 #include "LString.h"
20 #include "debug.h"
21
22
23 // It might happen that locale.h defines ON and OFF. This is not good
24 // for us, since we use these names below. But of course this is due
25 // to some old compilers. Than is broken when it comes to C++ scoping.
26 #include "gettext.h" // so that we are sure tht it won't be included
27 // later. 
28 #ifdef ON
29 #undef ON
30 #endif
31
32 #ifdef OFF
33 #undef OFF
34 #endif
35
36 class LyXLex;
37
38 ///
39 class LyXFont {
40 public:
41         /** The value INHERIT_* means that the font attribute is
42             inherited from the layout. In the case of layout fonts, the
43             attribute is inherited from the default font.
44             The value IGNORE_* is used with LyXFont::update() when the
45             attribute should not be changed.
46         */
47         enum FONT_FAMILY {
48                 ///
49                 ROMAN_FAMILY, // fontstruct rely on this to be 0
50                 ///
51                 SANS_FAMILY,
52                 ///
53                 TYPEWRITER_FAMILY,
54                 ///
55                 SYMBOL_FAMILY,
56                 ///
57                 INHERIT_FAMILY,
58                 ///
59                 IGNORE_FAMILY
60         };
61
62         ///
63         enum FONT_SERIES {
64                 ///
65                 MEDIUM_SERIES, // fontstruct rely on this to be 0
66                 ///
67                 BOLD_SERIES,
68                 ///
69                 INHERIT_SERIES,
70                 ///
71                 IGNORE_SERIES
72         };
73
74         ///
75         enum FONT_SHAPE {
76                 ///
77                 UP_SHAPE, // fontstruct rely on this to be 0
78                 ///
79                 ITALIC_SHAPE,
80                 ///
81                 SLANTED_SHAPE,
82                 ///
83                 SMALLCAPS_SHAPE,
84                 ///
85                 INHERIT_SHAPE,
86                 ///
87                 IGNORE_SHAPE
88         };
89
90         ///
91         enum FONT_SIZE {
92                 ///
93                 SIZE_TINY, // fontstruct rely on this to be 0
94                 ///
95                 SIZE_SCRIPT,
96                 ///
97                 SIZE_FOOTNOTE,
98                 ///
99                 SIZE_SMALL,
100                 ///
101                 SIZE_NORMAL,
102                 ///
103                 SIZE_LARGE,
104                 ///
105                 SIZE_LARGER,
106                 ///
107                 SIZE_LARGEST,
108                 ///
109                 SIZE_HUGE,
110                 ///
111                 SIZE_HUGER,
112                 ///
113                 INCREASE_SIZE,
114                 ///
115                 DECREASE_SIZE,
116                 ///
117                 INHERIT_SIZE,
118                 ///
119                 IGNORE_SIZE
120         };
121  
122         enum FONT_DIRECTION {
123                 ///
124                 LTR_DIR,
125                 ///
126                 RTL_DIR,
127                 ///
128                 TOGGLE_DIR,
129                 ///
130                 INHERIT_DIR,
131                 ///
132                 IGNORE_DIR
133         };
134
135         /// Used for emph, underbar, noun and latex toggles
136         enum FONT_MISC_STATE {
137                 ///
138                 OFF,
139                 ///
140                 ON,
141                 ///
142                 TOGGLE,
143                 ///
144                 INHERIT,
145                 ///
146                 IGNORE
147         };
148  
149         ///
150         enum FONT_COLOR {
151                 ///
152                 NONE,
153                 ///
154                 BLACK,
155                 ///
156                 WHITE,
157                 ///
158                 RED,
159                 ///
160                 GREEN,
161                 ///
162                 BLUE,
163                 ///
164                 CYAN,
165                 ///
166                 MAGENTA,
167                 ///
168                 YELLOW,
169                 ///
170                 MATH,
171                 ///
172                 INSET,
173                 ///
174                 INHERIT_COLOR,
175                 ///
176                 IGNORE_COLOR
177         };
178
179         /// Trick to overload constructor and make it megafast
180         enum FONT_INIT1 {
181                 ///
182                 ALL_INHERIT
183         };
184         ///
185         enum FONT_INIT2 {
186                 ///
187                 ALL_IGNORE
188         };
189         ///
190         enum FONT_INIT3 {
191                 ///
192                 ALL_SANE
193         };
194
195         ///
196         LyXFont();
197
198         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
199         LyXFont(LyXFont const & x);
200  
201         /// Shortcut initialization
202         LyXFont(LyXFont::FONT_INIT1);
203         /// Shortcut initialization
204         LyXFont(LyXFont::FONT_INIT2);
205         /// Shortcut initialization
206         LyXFont(LyXFont::FONT_INIT3);
207
208         /// LyXFont x, y; x = y;
209         LyXFont& operator=(LyXFont const & x);
210  
211         /// Decreases font size by one
212         LyXFont & decSize();
213  
214         /// Increases font size by one
215         LyXFont & incSize();
216  
217         ///
218         FONT_FAMILY family() const;
219  
220         ///
221         FONT_SERIES series() const;
222  
223         ///
224         FONT_SHAPE shape() const;
225  
226         ///
227         FONT_SIZE size() const;
228  
229         ///
230         FONT_MISC_STATE emph() const;
231  
232         ///
233         FONT_MISC_STATE underbar() const;
234  
235         ///
236         FONT_MISC_STATE noun() const;
237
238         ///
239         FONT_MISC_STATE latex() const;
240  
241         ///
242         FONT_COLOR color() const;
243
244         ///
245         FONT_DIRECTION direction() const;
246
247         ///
248         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
249         ///
250         LyXFont & setSeries(LyXFont::FONT_SERIES s);
251         ///
252         LyXFont & setShape(LyXFont::FONT_SHAPE s);
253         ///
254         LyXFont & setSize(LyXFont::FONT_SIZE s);
255         ///
256         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
257         ///
258         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
259         ///
260         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
261         ///
262         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
263         ///
264         LyXFont & setColor(LyXFont::FONT_COLOR c);
265         ///
266         LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
267
268         /// Set family after LyX text format
269         LyXFont & setLyXFamily(string const &);
270  
271         /// Set series after LyX text format
272         LyXFont & setLyXSeries(string const &);
273  
274         /// Set shape after LyX text format
275         LyXFont & setLyXShape(string const &);
276  
277         /// Set size after LyX text format
278         LyXFont & setLyXSize(string const &);
279  
280         /// Returns misc flag after LyX text format
281         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
282
283         /// Sets color after LyX text format
284         LyXFont & setLyXColor(string const &);
285  
286         /// Sets size after GUI name
287         LyXFont & setGUISize(string const &);
288  
289         /// Returns size of font in LaTeX text notation
290         string latexSize() const;
291  
292         /** Updates font settings according to request. If an
293             attribute is IGNORE, the attribute is left as it is. */
294         /* 
295          * When toggleall = true, all properties that matches the font in use
296          * will have the effect that the properties is reset to the
297          * default.  If we have a text that is TYPEWRITER_FAMILY, and is
298          * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
299          * a INHERIT_FAMILY was asked for.  This is necessary for the
300          * toggle-user-defined-style button on the toolbar.
301          */
302         void update(LyXFont const & newfont, bool toggleall = false);
303  
304         /** Reduce font to fall back to template where possible.
305             Equal fields are reduced to INHERIT */
306         void reduce(LyXFont const & tmplt);
307  
308         /// Realize font from a template (INHERIT are realized)
309         LyXFont & realize(LyXFont const & tmplt);
310
311         /// Is a given font fully resolved?
312         bool resolved() const;
313  
314         /// Read a font specification from LyXLex. Used for layout files.
315         LyXFont & lyxRead(LyXLex&);
316  
317         /// Writes the changes from this font to orgfont in .lyx format in file
318         void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
319
320         /** Writes the head of the LaTeX needed to change to this font.
321             Writes to string, the head of the LaTeX needed to change
322             to this font. Returns number of chars written. Base is the
323             font state active now.
324         */
325         int latexWriteStartChanges(string &, LyXFont const & base,
326                                    LyXFont const & prev) const;
327
328         /** Writes tha tail of the LaTeX needed to chagne to this font.
329             Returns number of chars written. Base is the font state we want
330             to achieve.
331         */
332         int latexWriteEndChanges(string &, LyXFont const & base,
333                                  LyXFont const & next) const;
334  
335         /// Build GUI description of font state
336         string stateText() const;
337
338         ///
339         int maxAscent() const; 
340
341         ///
342         int maxDescent() const;
343
344         ///
345         int ascent(char c) const;
346
347         ///
348         int descent(char c) const;
349
350         ///
351         int width(char c) const;
352
353         ///
354         int lbearing(char c) const;
355
356         ///
357         int rbearing(char c) const;
358         
359         ///
360         int textWidth(char const *s, int n) const;
361
362         ///
363         int stringWidth(string const & s) const;
364
365         ///
366         int signedStringWidth(string const & s) const;
367
368         /// Draws text and returns width of text
369         int drawText(char const*, int n, Pixmap, int baseline, int x) const;
370
371         ///
372         int drawString(string const &, Pixmap pm, int baseline, int x) const;
373
374         ///
375         GC getGC() const;
376
377         ///
378         friend inline
379         bool operator==(LyXFont const & font1, LyXFont const & font2) {
380                 return font1.bits == font2.bits;
381         }
382
383         ///
384         friend inline
385         bool operator!=(LyXFont const & font1, LyXFont const & font2) {
386                 return font1.bits != font2.bits;
387         }
388
389         /// compares two fonts, ignoring the setting of the Latex part.
390         bool equalExceptLatex(LyXFont const &) const;
391
392 private:
393         /// This have to be at least 32 bits, but 64 or more does not hurt
394         typedef unsigned int ui32;
395
396         /** Representation: bit table
397             Layout of bit table:
398             11 1111 111 122 222 222 2233
399             Bit 012 34 567 8901 2345 678 901 234 567 8901
400             FFF SS SSS SSSS CCCC EEE UUU NNN LLL
401             aaa ee hhh iiii oooo mmm nnn ooo aaa
402             mmm rr aaa zzzz llll ppp ddd uuu ttt
403
404             Some might think this is a dirty representation, but it gives
405             us at least 25% speed-up, so why not?
406         */
407         ui32 bits;
408
409         ///
410         enum FONT_POSITION {
411                 ///
412                 Fam_Pos =  0,
413                 ///
414                 Ser_Pos =  3,
415                 ///
416                 Sha_Pos =  5,
417                 ///
418                 Siz_Pos =  8,
419                 ///
420                 Col_Pos = 12,
421                 ///
422                 Emp_Pos = 16,
423                 ///
424                 Und_Pos = 19,
425                 ///
426                 Nou_Pos = 22,
427                 ///
428                 Lat_Pos = 25,
429                 ///
430                 Dir_Pos = 28
431         };
432
433         ///
434         enum FONT_MASK {
435                 ///
436                 Fam_Mask = 0x07,
437                 ///
438                 Ser_Mask = 0x03,
439                 ///
440                 Sha_Mask = 0x07,
441                 ///
442                 Siz_Mask = 0x0f,
443                 ///
444                 Col_Mask = 0x0f,
445                 ///
446                 Dir_Mask = 0x07,
447                 ///
448                 Misc_Mask = 0x07
449         };
450  
451         /// Sane font
452         enum {  sane = ui32(ROMAN_FAMILY) << Fam_Pos
453                 | ui32(MEDIUM_SERIES) << Ser_Pos
454                 | ui32(UP_SHAPE) << Sha_Pos
455                 | ui32(SIZE_NORMAL) << Siz_Pos
456                 | ui32(NONE) << Col_Pos
457                 | ui32(OFF) << Emp_Pos
458                 | ui32(OFF) << Und_Pos
459                 | ui32(OFF) << Nou_Pos
460                 | ui32(OFF) << Lat_Pos
461                 | ui32(LTR_DIR) << Dir_Pos};
462  
463         /// All inherit font
464         enum{ inherit = ui32(INHERIT_FAMILY) << Fam_Pos
465                       | ui32(INHERIT_SERIES) << Ser_Pos
466                       | ui32(INHERIT_SHAPE) << Sha_Pos
467                       | ui32(INHERIT_SIZE) << Siz_Pos
468                       | ui32(INHERIT_COLOR) << Col_Pos
469                       | ui32(INHERIT) << Emp_Pos
470                       | ui32(INHERIT) << Und_Pos
471                       | ui32(INHERIT) << Nou_Pos
472                       | ui32(INHERIT) << Lat_Pos
473                       | ui32(INHERIT_DIR) << Dir_Pos};
474  
475         /// All ignore font
476         enum{ ignore = ui32(IGNORE_FAMILY) << Fam_Pos
477                       | ui32(IGNORE_SERIES) << Ser_Pos
478                       | ui32(IGNORE_SHAPE) << Sha_Pos
479                       | ui32(IGNORE_SIZE) << Siz_Pos
480                       | ui32(IGNORE_COLOR) << Col_Pos
481                       | ui32(IGNORE) << Emp_Pos
482                       | ui32(IGNORE) << Und_Pos
483                       | ui32(IGNORE) << Nou_Pos
484                       | ui32(IGNORE) << Lat_Pos
485                       | ui32(IGNORE_DIR) << Dir_Pos};
486  
487         /// Updates a misc setting according to request
488         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
489                                          LyXFont::FONT_MISC_STATE org);
490
491         /// Converts logical attributes to concrete shape attribute
492         LyXFont::FONT_SHAPE realShape() const;
493
494         ///
495         XFontStruct* getXFontstruct() const;
496 };
497
498 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
499
500 inline LyXFont::LyXFont()
501 {
502         bits = sane;
503 }
504
505
506 inline LyXFont::LyXFont(LyXFont const & x)
507 {
508         bits = x.bits;
509 }
510
511
512 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
513 {
514         bits = inherit;
515 }
516
517
518 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
519 {
520         bits = ignore;
521 }
522
523
524 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
525 {
526         bits = sane;
527 }
528
529
530 inline LyXFont & LyXFont::operator=(LyXFont const & x) 
531 {
532         bits = x.bits;
533         return *this;
534 }
535
536
537 // You don't have to understand the stuff below :-)
538 // It works, and it's bloody fast. (Asger)
539 inline LyXFont::FONT_FAMILY LyXFont::family() const 
540 {
541         return LyXFont::FONT_FAMILY((bits >> Fam_Pos) & Fam_Mask);
542 }
543
544
545 inline LyXFont::FONT_SERIES LyXFont::series() const
546 {
547         return LyXFont::FONT_SERIES((bits >> Ser_Pos) & Ser_Mask);
548 }
549
550
551 inline LyXFont::FONT_SHAPE LyXFont::shape() const
552 {
553         return LyXFont::FONT_SHAPE((bits >> Sha_Pos) & Sha_Mask);
554 }
555
556
557 inline LyXFont::FONT_SIZE LyXFont::size() const
558 {
559         return LyXFont::FONT_SIZE((bits >> Siz_Pos) & Siz_Mask);
560 }
561
562
563 inline LyXFont::FONT_MISC_STATE LyXFont::emph() const
564 {
565         return LyXFont::FONT_MISC_STATE((bits >> Emp_Pos) & Misc_Mask);
566 }
567
568
569 inline LyXFont::FONT_MISC_STATE LyXFont::underbar() const
570 {
571         return LyXFont::FONT_MISC_STATE((bits >> Und_Pos) & Misc_Mask);
572 }
573
574
575 inline LyXFont::FONT_MISC_STATE LyXFont::noun() const
576 {
577         return LyXFont::FONT_MISC_STATE((bits >> Nou_Pos) & Misc_Mask);
578 }
579
580
581 inline LyXFont::FONT_MISC_STATE LyXFont::latex() const 
582 {
583         return LyXFont::FONT_MISC_STATE((bits >> Lat_Pos) & Misc_Mask);
584 }
585
586
587 inline LyXFont::FONT_COLOR LyXFont::color() const 
588 {
589         return LyXFont::FONT_COLOR((bits >> Col_Pos) & Col_Mask);
590 }
591
592 inline LyXFont::FONT_DIRECTION LyXFont::direction() const 
593 {
594         return LyXFont::FONT_DIRECTION((bits >> Dir_Pos) & Dir_Mask);
595 }
596
597 inline LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
598 {
599         bits &= ~(Fam_Mask << Fam_Pos);
600         bits |= ui32(f) << Fam_Pos;
601         return *this;
602 }
603
604
605 inline LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
606 {
607         bits &= ~(Ser_Mask << Ser_Pos);
608         bits |= ui32(s) << Ser_Pos;
609         return *this;
610 }
611
612
613 inline LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
614 {
615         bits &= ~(Sha_Mask << Sha_Pos);
616         bits |= ui32(s) << Sha_Pos;
617         return *this;
618 }
619
620
621 inline LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
622 {
623         bits &= ~(Siz_Mask << Siz_Pos);
624         bits |= ui32(s) << Siz_Pos;
625         return *this;
626 }
627
628
629 inline LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
630 {
631         bits &= ~(Misc_Mask << Emp_Pos);
632         bits |= ui32(e) << Emp_Pos;
633         return *this;
634 }
635
636
637 inline LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
638 {
639         bits &= ~(Misc_Mask << Und_Pos);
640         bits |= ui32(u) << Und_Pos;
641         return *this;
642 }
643
644
645 inline LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
646 {
647         bits &= ~(Misc_Mask << Nou_Pos);
648         bits |= ui32(n) << Nou_Pos;
649         return *this;
650 }
651
652 inline LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
653 {
654         bits &= ~(Misc_Mask << Lat_Pos);
655         bits |= ui32(l) << Lat_Pos;
656         return *this;
657 }
658
659
660 inline LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
661 {
662         bits &= ~(Col_Mask << Col_Pos);
663         bits |= ui32(c) << Col_Pos;
664         return *this;
665 }
666
667 inline LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
668 {
669         bits &= ~(Dir_Mask << Dir_Pos);
670         bits |= ui32(d) << Dir_Pos;
671         return *this;
672 }
673
674 #endif