]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
mathed31.diff
[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         /// compares two fonts, ignoring the setting of the Latex part.
324         bool equalExceptLatex(LyXFont const &) const;
325
326         /// Converts logical attributes to concrete shape attribute
327         LyXFont::FONT_SHAPE realShape() const;
328 private:
329         ///
330         struct FontBits {
331                 ///
332                 bool operator==(FontBits const & fb1) const;
333                 ///
334                 bool operator!=(FontBits const & fb1) const;
335                 ///
336                 FONT_FAMILY family;
337                 ///
338                 FONT_SERIES series;
339                 ///
340                 FONT_SHAPE shape;
341                 ///
342                 FONT_SIZE size;
343                 ///
344                 LColor::color color;
345                 ///
346                 FONT_MISC_STATE emph;
347                 ///
348                 FONT_MISC_STATE underbar;
349                 ///
350                 FONT_MISC_STATE noun;
351                 ///
352                 FONT_MISC_STATE latex;
353         };
354         ///
355         FontBits bits;
356         ///
357         Language const * lang;
358         ///
359         FONT_MISC_STATE number_;
360         
361         /// Sane font
362         static FontBits sane;
363         
364         /// All inherit font
365         static FontBits inherit;
366  
367         /// All ignore font
368         static FontBits ignore;
369
370         /// Updates a misc setting according to request
371         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
372                                          LyXFont::FONT_MISC_STATE org);
373 };
374
375 ///
376 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
377
378 ///
379 inline
380 bool operator==(LyXFont const & font1, LyXFont const & font2) {
381         return font1.bits == font2.bits &&
382                 font1.lang == font2.lang &&
383                 font1.number_ == font2.number_;
384 }
385
386 ///
387 inline
388 bool operator!=(LyXFont const & font1, LyXFont const & font2) {
389         return !(font1 == font2);
390 }
391
392
393 inline
394 LyXFont::LyXFont()
395 {
396         bits = sane;
397         lang = default_language;
398         number_ = OFF;
399 }
400
401
402 inline
403 LyXFont::LyXFont(LyXFont const & x)
404 {
405         bits = x.bits;
406         lang = x.lang;
407         number_ = x.number_;
408 }
409
410
411 inline
412 LyXFont::LyXFont(LyXFont::FONT_INIT1)
413 {
414         bits = inherit;
415         lang = default_language;
416         number_ = OFF;
417 }
418
419
420 inline
421 LyXFont::LyXFont(LyXFont::FONT_INIT2)
422 {
423         bits = ignore;
424         lang = ignore_language;
425         number_ = IGNORE;
426 }
427
428
429 inline
430 LyXFont::LyXFont(LyXFont::FONT_INIT3)
431 {
432         bits = sane;
433         lang = default_language;
434         number_ = OFF;
435 }
436
437
438 inline
439 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
440 {
441         bits = inherit;
442         lang = l;
443         number_ = OFF;
444 }
445
446
447 inline
448 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
449 {
450         bits = ignore;
451         lang = l;
452         number_ = IGNORE;
453 }
454
455
456 inline
457 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
458 {
459         bits = sane;
460         lang = l;
461         number_ = OFF;
462 }
463
464
465 inline
466 LyXFont & LyXFont::operator=(LyXFont const & x) 
467 {
468         bits = x.bits;
469         lang = x.lang;
470         number_ = x.number_;
471         return *this;
472 }
473
474
475 inline
476 LyXFont::FONT_FAMILY LyXFont::family() const 
477 {
478         return bits.family;
479 }
480
481
482 inline
483 LyXFont::FONT_SERIES LyXFont::series() const
484 {
485         return bits.series;
486 }
487
488
489 inline
490 LyXFont::FONT_SHAPE LyXFont::shape() const
491 {
492         return bits.shape;
493 }
494
495
496 inline
497 LyXFont::FONT_SIZE LyXFont::size() const
498 {
499         return bits.size;
500 }
501
502
503 inline
504 LyXFont::FONT_MISC_STATE LyXFont::emph() const
505 {
506         return bits.emph;
507 }
508
509
510 inline
511 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
512 {
513         return bits.underbar;
514 }
515
516
517 inline
518 LyXFont::FONT_MISC_STATE LyXFont::noun() const
519 {
520         return bits.noun;
521 }
522
523
524 inline
525 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
526 {
527         return bits.latex;
528 }
529
530
531 inline
532 LColor::color LyXFont::color() const 
533 {
534         return bits.color;
535 }
536
537
538 inline
539 Language const * LyXFont::language() const 
540 {
541         return lang;
542 }
543
544
545 inline
546 LyXFont::FONT_MISC_STATE LyXFont::number() const 
547 {
548         return number_;
549 }
550
551
552 inline
553 bool LyXFont::isRightToLeft() const 
554 {
555         return lang->RightToLeft();
556 }
557
558
559 inline
560 bool LyXFont::isVisibleRightToLeft() const 
561 {
562         return (lang->RightToLeft() && latex() != ON && number() != ON);
563 }
564
565
566 inline
567 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
568 {
569         bits.family = f;
570         return *this;
571 }
572
573
574 inline
575 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
576 {
577         bits.series = s;
578         return *this;
579 }
580
581
582 inline
583 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
584 {
585         bits.shape = s;
586         return *this;
587 }
588
589
590 inline
591 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
592 {
593         bits.size = s;
594         return *this;
595 }
596
597
598 inline
599 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
600 {
601         bits.emph = e;
602         return *this;
603 }
604
605
606 inline
607 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
608 {
609         bits.underbar = u;
610         return *this;
611 }
612
613
614 inline
615 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
616 {
617         bits.noun = n;
618         return *this;
619 }
620
621 inline
622 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
623 {
624         bits.latex = l;
625         return *this;
626 }
627
628
629 inline
630 LyXFont & LyXFont::setColor(LColor::color c)
631 {
632         bits.color = c;
633         return *this;
634 }
635
636
637 inline
638 LyXFont & LyXFont::setLanguage(Language const * l)
639 {
640         lang = l;
641         return *this;
642 }
643
644
645 inline
646 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
647 {
648         number_ = n;
649         return *this;
650 }
651
652 #endif