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