]> git.lyx.org Git - features.git/blob - src/lyxfont.h
apply the ostream changes to mathed, some other small related things
[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 FORMS_H_LOCATION
20 #include "LString.h"
21 #include "debug.h"
22 #include "direction.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 #define USE_OSTREAM_ONLY 1
39
40 class LyXLex;
41
42 ///
43 class LyXFont {
44 public:
45         /** The value INHERIT_* means that the font attribute is
46             inherited from the layout. In the case of layout fonts, the
47             attribute is inherited from the default font.
48             The value IGNORE_* is used with LyXFont::update() when the
49             attribute should not be changed.
50         */
51         enum FONT_FAMILY {
52                 ///
53                 ROMAN_FAMILY, // fontstruct rely on this to be 0
54                 ///
55                 SANS_FAMILY,
56                 ///
57                 TYPEWRITER_FAMILY,
58                 ///
59                 SYMBOL_FAMILY,
60                 ///
61                 INHERIT_FAMILY,
62                 ///
63                 IGNORE_FAMILY
64         };
65
66         ///
67         enum FONT_SERIES {
68                 ///
69                 MEDIUM_SERIES, // fontstruct rely on this to be 0
70                 ///
71                 BOLD_SERIES,
72                 ///
73                 INHERIT_SERIES,
74                 ///
75                 IGNORE_SERIES
76         };
77
78         ///
79         enum FONT_SHAPE {
80                 ///
81                 UP_SHAPE, // fontstruct rely on this to be 0
82                 ///
83                 ITALIC_SHAPE,
84                 ///
85                 SLANTED_SHAPE,
86                 ///
87                 SMALLCAPS_SHAPE,
88                 ///
89                 INHERIT_SHAPE,
90                 ///
91                 IGNORE_SHAPE
92         };
93
94         ///
95         enum FONT_SIZE {
96                 ///
97                 SIZE_TINY, // fontstruct rely on this to be 0
98                 ///
99                 SIZE_SCRIPT,
100                 ///
101                 SIZE_FOOTNOTE,
102                 ///
103                 SIZE_SMALL,
104                 ///
105                 SIZE_NORMAL,
106                 ///
107                 SIZE_LARGE,
108                 ///
109                 SIZE_LARGER,
110                 ///
111                 SIZE_LARGEST,
112                 ///
113                 SIZE_HUGE,
114                 ///
115                 SIZE_HUGER,
116                 ///
117                 INCREASE_SIZE,
118                 ///
119                 DECREASE_SIZE,
120                 ///
121                 INHERIT_SIZE,
122                 ///
123                 IGNORE_SIZE
124         };
125  
126         enum FONT_DIRECTION {
127                 ///
128                 LTR_DIR,
129                 ///
130                 RTL_DIR,
131                 ///
132                 TOGGLE_DIR,
133                 ///
134                 INHERIT_DIR,
135                 ///
136                 IGNORE_DIR
137         };
138
139         /// Used for emph, underbar, noun and latex toggles
140         enum FONT_MISC_STATE {
141                 ///
142                 OFF,
143                 ///
144                 ON,
145                 ///
146                 TOGGLE,
147                 ///
148                 INHERIT,
149                 ///
150                 IGNORE
151         };
152
153         /// Trick to overload constructor and make it megafast
154         enum FONT_INIT1 {
155                 ///
156                 ALL_INHERIT
157         };
158         ///
159         enum FONT_INIT2 {
160                 ///
161                 ALL_IGNORE
162         };
163         ///
164         enum FONT_INIT3 {
165                 ///
166                 ALL_SANE
167         };
168
169         ///
170         LyXFont();
171
172         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
173         LyXFont(LyXFont const & x);
174  
175         /// Shortcut initialization
176         LyXFont(LyXFont::FONT_INIT1);
177         /// Shortcut initialization
178         LyXFont(LyXFont::FONT_INIT2);
179         /// Shortcut initialization
180         LyXFont(LyXFont::FONT_INIT3);
181
182         /// LyXFont x, y; x = y;
183         LyXFont & operator=(LyXFont const & x);
184  
185         /// Decreases font size by one
186         LyXFont & decSize();
187  
188         /// Increases font size by one
189         LyXFont & incSize();
190  
191         ///
192         FONT_FAMILY family() const;
193  
194         ///
195         FONT_SERIES series() const;
196  
197         ///
198         FONT_SHAPE shape() const;
199  
200         ///
201         FONT_SIZE size() const;
202  
203         ///
204         FONT_MISC_STATE emph() const;
205  
206         ///
207         FONT_MISC_STATE underbar() const;
208  
209         ///
210         FONT_MISC_STATE noun() const;
211
212         ///
213         FONT_MISC_STATE latex() const;
214
215         ///
216         LColor::color color() const;
217
218         ///
219         FONT_DIRECTION direction() const;
220
221         ///
222         LyXDirection getFontDirection() const;
223         
224         ///
225         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
226         ///
227         LyXFont & setSeries(LyXFont::FONT_SERIES s);
228         ///
229         LyXFont & setShape(LyXFont::FONT_SHAPE s);
230         ///
231         LyXFont & setSize(LyXFont::FONT_SIZE s);
232         ///
233         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
234         ///
235         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
236         ///
237         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
238         ///
239         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
240         ///
241         LyXFont & setColor(LColor::color c);
242         ///
243         LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
244
245         /// Set family after LyX text format
246         LyXFont & setLyXFamily(string const &);
247  
248         /// Set series after LyX text format
249         LyXFont & setLyXSeries(string const &);
250  
251         /// Set shape after LyX text format
252         LyXFont & setLyXShape(string const &);
253  
254         /// Set size after LyX text format
255         LyXFont & setLyXSize(string const &);
256  
257         /// Returns misc flag after LyX text format
258         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
259
260         /// Sets color after LyX text format
261         LyXFont & setLyXColor(string const &);
262  
263         /// Sets size after GUI name
264         LyXFont & setGUISize(string const &);
265  
266         /// Returns size of font in LaTeX text notation
267         string latexSize() const;
268  
269         /** Updates font settings according to request. If an
270             attribute is IGNORE, the attribute is left as it is. */
271         /* 
272          * When toggleall = true, all properties that matches the font in use
273          * will have the effect that the properties is reset to the
274          * default.  If we have a text that is TYPEWRITER_FAMILY, and is
275          * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
276          * a INHERIT_FAMILY was asked for.  This is necessary for the
277          * toggle-user-defined-style button on the toolbar.
278          */
279         void update(LyXFont const & newfont, bool toggleall = false);
280  
281         /** Reduce font to fall back to template where possible.
282             Equal fields are reduced to INHERIT */
283         void reduce(LyXFont const & tmplt);
284  
285         /// Realize font from a template (INHERIT are realized)
286         LyXFont & realize(LyXFont const & tmplt);
287
288         /// Is a given font fully resolved?
289         bool resolved() const;
290  
291         /// Read a font specification from LyXLex. Used for layout files.
292         LyXFont & lyxRead(LyXLex &);
293  
294         /// Writes the changes from this font to orgfont in .lyx format in file
295         void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
296
297
298 #ifdef USE_OSTREAM_ONLY
299         /** Writes the head of the LaTeX needed to change to this font.
300             Writes to string, the head of the LaTeX needed to change
301             to this font. Returns number of chars written. Base is the
302             font state active now.
303         */
304         int latexWriteStartChanges(ostream &, LyXFont const & base,
305                                    LyXFont const & prev) const;
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(ostream &, LyXFont const & base,
311                                  LyXFont const & next) const;
312 #else
313         /** Writes the head of the LaTeX needed to change to this font.
314             Writes to string, the head of the LaTeX needed to change
315             to this font. Returns number of chars written. Base is the
316             font state active now.
317         */
318         int latexWriteStartChanges(string &, LyXFont const & base,
319                                    LyXFont const & prev) const;
320         /** Writes tha tail of the LaTeX needed to chagne to this font.
321             Returns number of chars written. Base is the font state we want
322             to achieve.
323         */
324         int latexWriteEndChanges(string &, LyXFont const & base,
325                                  LyXFont const & next) const;
326 #endif 
327         /// Build GUI description of font state
328         string stateText() const;
329
330         ///
331         int maxAscent() const; 
332
333         ///
334         int maxDescent() const;
335
336         ///
337         int ascent(char c) const;
338
339         ///
340         int descent(char c) const;
341
342         ///
343         int width(char c) const;
344
345         ///
346         int lbearing(char c) const;
347
348         ///
349         int rbearing(char c) const;
350         
351         ///
352         int textWidth(char const *s, int n) const;
353
354         ///
355         int stringWidth(string const & s) const;
356
357         ///
358         int signedStringWidth(string const & s) const;
359
360         /// Draws text and returns width of text
361         int drawText(char const *, int n, Pixmap, int baseline, int x) const;
362
363         ///
364         int drawString(string const &, Pixmap pm, int baseline, int x) const;
365
366         ///
367         LColor::color realColor() const;
368
369         ///
370         XID getFontID() const {
371                 return getXFontstruct()->fid;
372         }
373         
374         ///
375         friend inline
376         bool operator==(LyXFont const & font1, LyXFont const & font2) {
377                 return font1.bits == font2.bits;
378         }
379
380         ///
381         friend inline
382         bool operator!=(LyXFont const & font1, LyXFont const & font2) {
383                 return font1.bits != font2.bits;
384         }
385
386         /// compares two fonts, ignoring the setting of the Latex part.
387         bool equalExceptLatex(LyXFont const &) const;
388
389 private:
390         ///
391         struct FontBits {
392                 bool operator==(FontBits const & fb1) const {
393                         return fb1.family == family &&
394                                 fb1.series == series &&
395                                 fb1.shape == shape &&
396                                 fb1.size == size &&
397                                 fb1.color == color &&
398                                 fb1.emph == emph &&
399                                 fb1.underbar == underbar &&
400                                 fb1.noun == noun &&
401                                 fb1.latex == latex &&
402                                 fb1.direction == direction;
403                 }
404                 bool operator!=(FontBits const & fb1) const {
405                         return !(fb1 == *this);
406                 }
407                 
408                 FONT_FAMILY family;
409                 FONT_SERIES series;
410                 FONT_SHAPE shape;
411                 FONT_SIZE size;
412                 LColor::color color;
413                 FONT_MISC_STATE emph;
414                 FONT_MISC_STATE underbar;
415                 FONT_MISC_STATE noun;
416                 FONT_MISC_STATE latex;
417                 FONT_DIRECTION direction;
418         };
419
420         FontBits bits;
421         
422         /// Sane font
423         static FontBits sane;
424         
425         /// All inherit font
426         static FontBits inherit;
427  
428         /// All ignore font
429         static FontBits ignore;
430
431         /// Updates a misc setting according to request
432         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
433                                          LyXFont::FONT_MISC_STATE org);
434
435         /// Converts logical attributes to concrete shape attribute
436         LyXFont::FONT_SHAPE realShape() const;
437
438         ///
439         XFontStruct * getXFontstruct() const;
440 };
441
442 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
443
444 inline
445 LyXFont::LyXFont()
446 {
447         bits = sane;
448 }
449
450
451 inline
452 LyXFont::LyXFont(LyXFont const & x)
453 {
454         bits = x.bits;
455 }
456
457
458 inline
459 LyXFont::LyXFont(LyXFont::FONT_INIT1)
460 {
461         bits = inherit;
462 }
463
464
465 inline
466 LyXFont::LyXFont(LyXFont::FONT_INIT2)
467 {
468         bits = ignore;
469 }
470
471
472 inline
473 LyXFont::LyXFont(LyXFont::FONT_INIT3)
474 {
475         bits = sane;
476 }
477
478
479 inline
480 LyXFont & LyXFont::operator=(LyXFont const & x) 
481 {
482         bits = x.bits;
483         return *this;
484 }
485
486
487 inline
488 LyXFont::FONT_FAMILY LyXFont::family() const 
489 {
490         return bits.family;
491 }
492
493
494 inline
495 LyXFont::FONT_SERIES LyXFont::series() const
496 {
497         return bits.series;
498 }
499
500
501 inline
502 LyXFont::FONT_SHAPE LyXFont::shape() const
503 {
504         return bits.shape;
505 }
506
507
508 inline
509 LyXFont::FONT_SIZE LyXFont::size() const
510 {
511         return bits.size;
512 }
513
514
515 inline
516 LyXFont::FONT_MISC_STATE LyXFont::emph() const
517 {
518         return bits.emph;
519 }
520
521
522 inline
523 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
524 {
525         return bits.underbar;
526 }
527
528
529 inline
530 LyXFont::FONT_MISC_STATE LyXFont::noun() const
531 {
532         return bits.noun;
533 }
534
535
536 inline
537 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
538 {
539         return bits.latex;
540 }
541
542
543 inline
544 LColor::color LyXFont::color() const 
545 {
546         return bits.color;
547 }
548
549
550 inline
551 LyXFont::FONT_DIRECTION LyXFont::direction() const 
552 {
553         return bits.direction;
554 }
555
556 inline
557 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
558 {
559         bits.family = f;
560         return *this;
561 }
562
563
564 inline
565 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
566 {
567         bits.series = s;
568         return *this;
569 }
570
571
572 inline
573 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
574 {
575         bits.shape = s;
576         return *this;
577 }
578
579
580 inline
581 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
582 {
583         bits.size = s;
584         return *this;
585 }
586
587
588 inline
589 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
590 {
591         bits.emph = e;
592         return *this;
593 }
594
595
596 inline
597 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
598 {
599         bits.underbar = u;
600         return *this;
601 }
602
603
604 inline
605 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
606 {
607         bits.noun = n;
608         return *this;
609 }
610
611 inline
612 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
613 {
614         bits.latex = l;
615         return *this;
616 }
617
618
619 inline
620 LyXFont & LyXFont::setColor(LColor::color c)
621 {
622         bits.color = c;
623         return *this;
624 }
625
626
627 inline
628 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
629 {
630         bits.direction = d;
631         return *this;
632 }
633
634 #endif