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