]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
aedd3236f50f182f65a38001b06b1bb2dc076256
[lyx.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 "LString.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 class LyXLex;
39 class BufferParams;
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         explicit
163         LyXFont(LyXFont::FONT_INIT1);
164         /// Shortcut initialization
165         explicit
166         LyXFont(LyXFont::FONT_INIT2);
167         /// Shortcut initialization
168         explicit
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         FONT_MISC_STATE number() const;
212
213         ///
214         LColor::color color() const;
215
216         ///
217         Language const * language() const;
218
219         ///
220         bool isRightToLeft() const;
221
222         ///
223         bool isVisibleRightToLeft() const;
224         
225         ///
226         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
227         ///
228         LyXFont & setSeries(LyXFont::FONT_SERIES s);
229         ///
230         LyXFont & setShape(LyXFont::FONT_SHAPE s);
231         ///
232         LyXFont & setSize(LyXFont::FONT_SIZE s);
233         ///
234         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
235         ///
236         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
237         ///
238         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
239         ///
240         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
241         ///
242         LyXFont & setNumber(LyXFont::FONT_MISC_STATE n);
243         ///
244         LyXFont & setColor(LColor::color c);
245         ///
246         LyXFont & setLanguage(Language const * l);
247
248         /// Set family after LyX text format
249         LyXFont & setLyXFamily(string const &);
250  
251         /// Set series after LyX text format
252         LyXFont & setLyXSeries(string const &);
253  
254         /// Set shape after LyX text format
255         LyXFont & setLyXShape(string const &);
256  
257         /// Set size after LyX text format
258         LyXFont & setLyXSize(string const &);
259  
260         /// Returns misc flag after LyX text format
261         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
262
263         /// Sets color after LyX text format
264         LyXFont & setLyXColor(string const &);
265  
266         /// Sets size after GUI name
267         LyXFont & setGUISize(string const &);
268  
269         /// Returns size of font in LaTeX text notation
270         string const latexSize() const;
271  
272         /** Updates font settings according to request.
273             If an attribute is IGNORE, the attribute is left as it is.
274             When toggleall = true, all properties that matches the font in use
275             will have the effect that the properties is reset to the
276             default.  If we have a text that is TYPEWRITER_FAMILY, and is
277             update()'ed with TYPEWRITER_FAMILY, the operation will be as if
278             a INHERIT_FAMILY was asked for.  This is necessary for the
279             toggle-user-defined-style button on the toolbar.
280         */
281         void update(LyXFont const & newfont, 
282                     Language const * default_lang,
283                     bool toggleall = false);
284  
285         /** Reduce font to fall back to template where possible.
286             Equal fields are reduced to INHERIT */
287         void reduce(LyXFont const & tmplt);
288  
289         /// Realize font from a template (INHERIT are realized)
290         LyXFont & realize(LyXFont const & tmplt);
291
292         /// Is a given font fully resolved?
293         bool resolved() const;
294  
295         /// Read a font specification from LyXLex. Used for layout files.
296         LyXFont & lyxRead(LyXLex &);
297  
298         /// Writes the changes from this font to orgfont in .lyx format in file
299         void lyxWriteChanges(LyXFont const & orgfont, std::ostream &) const;
300
301         /** Writes the head of the LaTeX needed to change to this font.
302             Writes to string, the head of the LaTeX needed to change
303             to this font. Returns number of chars written. Base is the
304             font state active now.
305         */
306         int latexWriteStartChanges(std::ostream &, LyXFont const & base,
307                                    LyXFont const & prev) const;
308
309         /** Writes tha tail of the LaTeX needed to chagne to this font.
310             Returns number of chars written. Base is the font state we want
311             to achieve.
312         */
313         int latexWriteEndChanges(std::ostream &, LyXFont const & base,
314                                  LyXFont const & next) const;
315
316         /// Build GUI description of font state
317         string const stateText(BufferParams * params) const;
318
319         ///
320         LColor::color realColor() const;
321
322         ///
323         friend
324         bool operator==(LyXFont const & font1, LyXFont const & font2);
325
326         ///
327         friend 
328         bool operator!=(LyXFont const & font1, LyXFont const & font2);
329
330         /// compares two fonts, ignoring the setting of the Latex part.
331         bool equalExceptLatex(LyXFont const &) const;
332
333         /// Converts logical attributes to concrete shape attribute
334         LyXFont::FONT_SHAPE realShape() const;
335 private:
336         ///
337         struct FontBits {
338                 ///
339                 bool operator==(FontBits const & fb1) const;
340                 ///
341                 bool operator!=(FontBits const & fb1) const;
342                 ///
343                 FONT_FAMILY family;
344                 ///
345                 FONT_SERIES series;
346                 ///
347                 FONT_SHAPE shape;
348                 ///
349                 FONT_SIZE size;
350                 ///
351                 LColor::color color;
352                 ///
353                 FONT_MISC_STATE emph;
354                 ///
355                 FONT_MISC_STATE underbar;
356                 ///
357                 FONT_MISC_STATE noun;
358                 ///
359                 FONT_MISC_STATE latex;
360         };
361         ///
362         FontBits bits;
363         ///
364         Language const * lang;
365         ///
366         FONT_MISC_STATE number_;
367         
368         /// Sane font
369         static FontBits sane;
370         
371         /// All inherit font
372         static FontBits inherit;
373  
374         /// All ignore font
375         static FontBits ignore;
376
377         /// Updates a misc setting according to request
378         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
379                                          LyXFont::FONT_MISC_STATE org);
380 };
381
382 ///
383 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
384
385 ///
386 inline
387 bool operator==(LyXFont const & font1, LyXFont const & font2) {
388         return font1.bits == font2.bits &&
389                 font1.lang == font2.lang &&
390                 font1.number_ == font2.number_;
391 }
392
393 ///
394 inline
395 bool operator!=(LyXFont const & font1, LyXFont const & font2) {
396         return !(font1 == font2);
397 }
398
399
400 inline
401 LyXFont::LyXFont()
402 {
403         bits = sane;
404         lang = default_language;
405         number_ = OFF;
406 }
407
408
409 inline
410 LyXFont::LyXFont(LyXFont const & x)
411 {
412         bits = x.bits;
413         lang = x.lang;
414         number_ = x.number_;
415 }
416
417
418 inline
419 LyXFont::LyXFont(LyXFont::FONT_INIT1)
420 {
421         bits = inherit;
422         lang = default_language;
423         number_ = OFF;
424 }
425
426
427 inline
428 LyXFont::LyXFont(LyXFont::FONT_INIT2)
429 {
430         bits = ignore;
431         lang = ignore_language;
432         number_ = IGNORE;
433 }
434
435
436 inline
437 LyXFont::LyXFont(LyXFont::FONT_INIT3)
438 {
439         bits = sane;
440         lang = default_language;
441         number_ = OFF;
442 }
443
444
445 inline
446 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
447 {
448         bits = inherit;
449         lang = l;
450         number_ = OFF;
451 }
452
453
454 inline
455 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
456 {
457         bits = ignore;
458         lang = l;
459         number_ = IGNORE;
460 }
461
462
463 inline
464 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
465 {
466         bits = sane;
467         lang = l;
468         number_ = OFF;
469 }
470
471
472 inline
473 LyXFont & LyXFont::operator=(LyXFont const & x) 
474 {
475         bits = x.bits;
476         lang = x.lang;
477         number_ = x.number_;
478         return *this;
479 }
480
481
482 inline
483 LyXFont::FONT_FAMILY LyXFont::family() const 
484 {
485         return bits.family;
486 }
487
488
489 inline
490 LyXFont::FONT_SERIES LyXFont::series() const
491 {
492         return bits.series;
493 }
494
495
496 inline
497 LyXFont::FONT_SHAPE LyXFont::shape() const
498 {
499         return bits.shape;
500 }
501
502
503 inline
504 LyXFont::FONT_SIZE LyXFont::size() const
505 {
506         return bits.size;
507 }
508
509
510 inline
511 LyXFont::FONT_MISC_STATE LyXFont::emph() const
512 {
513         return bits.emph;
514 }
515
516
517 inline
518 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
519 {
520         return bits.underbar;
521 }
522
523
524 inline
525 LyXFont::FONT_MISC_STATE LyXFont::noun() const
526 {
527         return bits.noun;
528 }
529
530
531 inline
532 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
533 {
534         return bits.latex;
535 }
536
537
538 inline
539 LColor::color LyXFont::color() const 
540 {
541         return bits.color;
542 }
543
544
545 inline
546 Language const * LyXFont::language() const 
547 {
548         return lang;
549 }
550
551
552 inline
553 LyXFont::FONT_MISC_STATE LyXFont::number() const 
554 {
555         return number_;
556 }
557
558
559 inline
560 bool LyXFont::isRightToLeft() const 
561 {
562         return lang->RightToLeft();
563 }
564
565
566 inline
567 bool LyXFont::isVisibleRightToLeft() const 
568 {
569         return (lang->RightToLeft() && latex() != ON && number() != ON);
570 }
571
572
573 inline
574 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
575 {
576         bits.family = f;
577         return *this;
578 }
579
580
581 inline
582 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
583 {
584         bits.series = s;
585         return *this;
586 }
587
588
589 inline
590 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
591 {
592         bits.shape = s;
593         return *this;
594 }
595
596
597 inline
598 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
599 {
600         bits.size = s;
601         return *this;
602 }
603
604
605 inline
606 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
607 {
608         bits.emph = e;
609         return *this;
610 }
611
612
613 inline
614 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
615 {
616         bits.underbar = u;
617         return *this;
618 }
619
620
621 inline
622 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
623 {
624         bits.noun = n;
625         return *this;
626 }
627
628 inline
629 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
630 {
631         bits.latex = l;
632         return *this;
633 }
634
635
636 inline
637 LyXFont & LyXFont::setColor(LColor::color c)
638 {
639         bits.color = c;
640         return *this;
641 }
642
643
644 inline
645 LyXFont & LyXFont::setLanguage(Language const * l)
646 {
647         lang = l;
648         return *this;
649 }
650
651
652 inline
653 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
654 {
655         number_ = n;
656         return *this;
657 }
658
659 #endif