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