]> git.lyx.org Git - features.git/blob - src/lyxfont.h
several changes and fixes. 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 "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
40 ///
41 class LyXFont {
42 public:
43         /** The value INHERIT_* means that the font attribute is
44             inherited from the layout. In the case of layout fonts, the
45             attribute is inherited from the default font.
46             The value IGNORE_* is used with LyXFont::update() when the
47             attribute should not be changed.
48         */
49         enum FONT_FAMILY {
50                 ///
51                 ROMAN_FAMILY, // fontstruct rely on this to be 0
52                 ///
53                 SANS_FAMILY,
54                 ///
55                 TYPEWRITER_FAMILY,
56                 ///
57                 SYMBOL_FAMILY,
58                 ///
59                 INHERIT_FAMILY,
60                 ///
61                 IGNORE_FAMILY
62         };
63
64         ///
65         enum FONT_SERIES {
66                 ///
67                 MEDIUM_SERIES, // fontstruct rely on this to be 0
68                 ///
69                 BOLD_SERIES,
70                 ///
71                 INHERIT_SERIES,
72                 ///
73                 IGNORE_SERIES
74         };
75
76         ///
77         enum FONT_SHAPE {
78                 ///
79                 UP_SHAPE, // fontstruct rely on this to be 0
80                 ///
81                 ITALIC_SHAPE,
82                 ///
83                 SLANTED_SHAPE,
84                 ///
85                 SMALLCAPS_SHAPE,
86                 ///
87                 INHERIT_SHAPE,
88                 ///
89                 IGNORE_SHAPE
90         };
91
92         ///
93         enum FONT_SIZE {
94                 ///
95                 SIZE_TINY, // fontstruct rely on this to be 0
96                 ///
97                 SIZE_SCRIPT,
98                 ///
99                 SIZE_FOOTNOTE,
100                 ///
101                 SIZE_SMALL,
102                 ///
103                 SIZE_NORMAL,
104                 ///
105                 SIZE_LARGE,
106                 ///
107                 SIZE_LARGER,
108                 ///
109                 SIZE_LARGEST,
110                 ///
111                 SIZE_HUGE,
112                 ///
113                 SIZE_HUGER,
114                 ///
115                 INCREASE_SIZE,
116                 ///
117                 DECREASE_SIZE,
118                 ///
119                 INHERIT_SIZE,
120                 ///
121                 IGNORE_SIZE
122         };
123  
124         /// Used for emph, underbar, noun and latex toggles
125         enum FONT_MISC_STATE {
126                 ///
127                 OFF,
128                 ///
129                 ON,
130                 ///
131                 TOGGLE,
132                 ///
133                 INHERIT,
134                 ///
135                 IGNORE
136         };
137
138         /// Trick to overload constructor and make it megafast
139         enum FONT_INIT1 {
140                 ///
141                 ALL_INHERIT
142         };
143         ///
144         enum FONT_INIT2 {
145                 ///
146                 ALL_IGNORE
147         };
148         ///
149         enum FONT_INIT3 {
150                 ///
151                 ALL_SANE
152         };
153
154         ///
155         LyXFont();
156
157         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
158         LyXFont(LyXFont const & x);
159  
160         /// Shortcut initialization
161         explicit
162         LyXFont(LyXFont::FONT_INIT1);
163         /// Shortcut initialization
164         explicit
165         LyXFont(LyXFont::FONT_INIT2);
166         /// Shortcut initialization
167         explicit
168         LyXFont(LyXFont::FONT_INIT3);
169         /// Shortcut initialization
170         LyXFont(LyXFont::FONT_INIT1, Language const * l);
171         /// Shortcut initialization
172         LyXFont(LyXFont::FONT_INIT2, Language const * l);
173         /// Shortcut initialization
174         LyXFont(LyXFont::FONT_INIT3, Language const * l);
175
176         /// LyXFont x, y; x = y;
177         LyXFont & operator=(LyXFont const & x);
178  
179         /// Decreases font size by one
180         LyXFont & decSize();
181  
182         /// Increases font size by one
183         LyXFont & incSize();
184  
185         ///
186         FONT_FAMILY family() const;
187  
188         ///
189         FONT_SERIES series() const;
190  
191         ///
192         FONT_SHAPE shape() const;
193  
194         ///
195         FONT_SIZE size() const;
196  
197         ///
198         FONT_MISC_STATE emph() const;
199  
200         ///
201         FONT_MISC_STATE underbar() const;
202  
203         ///
204         FONT_MISC_STATE noun() const;
205
206         ///
207         FONT_MISC_STATE latex() const;
208
209         ///
210         LColor::color color() const;
211
212         ///
213         Language const * language() const;
214
215         ///
216         bool isRightToLeft() const;
217
218         ///
219         bool isVisibleRightToLeft() const;
220         
221         ///
222         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
223         ///
224         LyXFont & setSeries(LyXFont::FONT_SERIES s);
225         ///
226         LyXFont & setShape(LyXFont::FONT_SHAPE s);
227         ///
228         LyXFont & setSize(LyXFont::FONT_SIZE s);
229         ///
230         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
231         ///
232         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
233         ///
234         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
235         ///
236         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
237         ///
238         LyXFont & setColor(LColor::color c);
239         ///
240         LyXFont & setLanguage(Language const * l);
241
242         /// Set family after LyX text format
243         LyXFont & setLyXFamily(string const &);
244  
245         /// Set series after LyX text format
246         LyXFont & setLyXSeries(string const &);
247  
248         /// Set shape after LyX text format
249         LyXFont & setLyXShape(string const &);
250  
251         /// Set size after LyX text format
252         LyXFont & setLyXSize(string const &);
253  
254         /// Returns misc flag after LyX text format
255         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
256
257         /// Sets color after LyX text format
258         LyXFont & setLyXColor(string const &);
259  
260         /// Sets size after GUI name
261         LyXFont & setGUISize(string const &);
262  
263         /// Returns size of font in LaTeX text notation
264         string latexSize() const;
265  
266         /** Updates font settings according to request. If an
267             attribute is IGNORE, the attribute is left as it is. */
268         /* 
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 stateText() const;
313
314         ///
315         LColor::color realColor() const;
316
317         ///
318         friend
319         bool operator==(LyXFont const & font1, LyXFont const & font2) {
320                 return font1.bits == font2.bits &&
321                         font1.lang == font2.lang;
322         }
323
324         ///
325         friend 
326         bool operator!=(LyXFont const & font1, LyXFont const & font2) {
327                 return font1.bits != font2.bits ||
328                         font1.lang != font2.lang;
329         }
330
331         /// compares two fonts, ignoring the setting of the Latex part.
332         bool equalExceptLatex(LyXFont const &) const;
333
334         /// Converts logical attributes to concrete shape attribute
335         LyXFont::FONT_SHAPE realShape() const;
336 private:
337         ///
338         struct FontBits {
339                 ///
340                 bool operator==(FontBits const & fb1) const;
341                 ///
342                 bool operator!=(FontBits const & fb1) const;
343                 ///
344                 FONT_FAMILY family;
345                 ///
346                 FONT_SERIES series;
347                 ///
348                 FONT_SHAPE shape;
349                 ///
350                 FONT_SIZE size;
351                 ///
352                 LColor::color color;
353                 ///
354                 FONT_MISC_STATE emph;
355                 ///
356                 FONT_MISC_STATE underbar;
357                 ///
358                 FONT_MISC_STATE noun;
359                 ///
360                 FONT_MISC_STATE latex;
361         };
362         ///
363         FontBits bits;
364         ///
365         Language const * lang;
366         
367         /// Sane font
368         static FontBits sane;
369         
370         /// All inherit font
371         static FontBits inherit;
372  
373         /// All ignore font
374         static FontBits ignore;
375
376         /// Updates a misc setting according to request
377         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
378                                          LyXFont::FONT_MISC_STATE org);
379 };
380
381
382 std::ostream & operator<<(std::ostream &, LyXFont::FONT_MISC_STATE);
383
384
385 inline
386 LyXFont::LyXFont()
387 {
388         bits = sane;
389         lang = default_language;
390 }
391
392
393 inline
394 LyXFont::LyXFont(LyXFont const & x)
395 {
396         bits = x.bits;
397         lang = x.lang;
398 }
399
400
401 inline
402 LyXFont::LyXFont(LyXFont::FONT_INIT1)
403 {
404         bits = inherit;
405         lang = default_language;
406 }
407
408
409 inline
410 LyXFont::LyXFont(LyXFont::FONT_INIT2)
411 {
412         bits = ignore;
413         lang = ignore_language;
414 }
415
416
417 inline
418 LyXFont::LyXFont(LyXFont::FONT_INIT3)
419 {
420         bits = sane;
421         lang = default_language;
422 }
423 inline
424 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
425 {
426         bits = inherit;
427         lang = l;
428 }
429
430
431 inline
432 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
433 {
434         bits = ignore;
435         lang = l;
436 }
437
438
439 inline
440 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
441 {
442         bits = sane;
443         lang = l;
444 }
445
446
447 inline
448 LyXFont & LyXFont::operator=(LyXFont const & x) 
449 {
450         bits = x.bits;
451         lang = x.lang;
452         return *this;
453 }
454
455
456 inline
457 LyXFont::FONT_FAMILY LyXFont::family() const 
458 {
459         return bits.family;
460 }
461
462
463 inline
464 LyXFont::FONT_SERIES LyXFont::series() const
465 {
466         return bits.series;
467 }
468
469
470 inline
471 LyXFont::FONT_SHAPE LyXFont::shape() const
472 {
473         return bits.shape;
474 }
475
476
477 inline
478 LyXFont::FONT_SIZE LyXFont::size() const
479 {
480         return bits.size;
481 }
482
483
484 inline
485 LyXFont::FONT_MISC_STATE LyXFont::emph() const
486 {
487         return bits.emph;
488 }
489
490
491 inline
492 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
493 {
494         return bits.underbar;
495 }
496
497
498 inline
499 LyXFont::FONT_MISC_STATE LyXFont::noun() const
500 {
501         return bits.noun;
502 }
503
504
505 inline
506 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
507 {
508         return bits.latex;
509 }
510
511
512 inline
513 LColor::color LyXFont::color() const 
514 {
515         return bits.color;
516 }
517
518
519 inline
520 Language const * LyXFont::language() const 
521 {
522         return lang;
523 }
524
525
526 inline
527 bool LyXFont::isRightToLeft() const 
528 {
529         return lang->RightToLeft;
530 }
531
532
533 inline
534 bool LyXFont::isVisibleRightToLeft() const 
535 {
536         return (lang->RightToLeft && latex() != ON);
537 }
538
539
540 inline
541 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
542 {
543         bits.family = f;
544         return *this;
545 }
546
547
548 inline
549 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
550 {
551         bits.series = s;
552         return *this;
553 }
554
555
556 inline
557 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
558 {
559         bits.shape = s;
560         return *this;
561 }
562
563
564 inline
565 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
566 {
567         bits.size = s;
568         return *this;
569 }
570
571
572 inline
573 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
574 {
575         bits.emph = e;
576         return *this;
577 }
578
579
580 inline
581 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
582 {
583         bits.underbar = u;
584         return *this;
585 }
586
587
588 inline
589 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
590 {
591         bits.noun = n;
592         return *this;
593 }
594
595 inline
596 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
597 {
598         bits.latex = l;
599         return *this;
600 }
601
602
603 inline
604 LyXFont & LyXFont::setColor(LColor::color c)
605 {
606         bits.color = c;
607         return *this;
608 }
609
610
611 inline
612 LyXFont & LyXFont::setLanguage(Language const * l)
613 {
614         lang = l;
615         return *this;
616 }
617
618 #endif