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