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