]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
816de4753e6b3ff4ed37977a193fb37c40dcd5d0
[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 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 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         enum FONT_DIRECTION {
125                 ///
126                 LTR_DIR,
127                 ///
128                 RTL_DIR,
129                 ///
130                 TOGGLE_DIR,
131                 ///
132                 INHERIT_DIR,
133                 ///
134                 IGNORE_DIR
135         };
136
137         /// Used for emph, underbar, noun and latex toggles
138         enum FONT_MISC_STATE {
139                 ///
140                 OFF,
141                 ///
142                 ON,
143                 ///
144                 TOGGLE,
145                 ///
146                 INHERIT,
147                 ///
148                 IGNORE
149         };
150
151         /// Trick to overload constructor and make it megafast
152         enum FONT_INIT1 {
153                 ///
154                 ALL_INHERIT
155         };
156         ///
157         enum FONT_INIT2 {
158                 ///
159                 ALL_IGNORE
160         };
161         ///
162         enum FONT_INIT3 {
163                 ///
164                 ALL_SANE
165         };
166
167         ///
168         LyXFont();
169
170         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
171         LyXFont(LyXFont const & x);
172  
173         /// Shortcut initialization
174         LyXFont(LyXFont::FONT_INIT1);
175         /// Shortcut initialization
176         LyXFont(LyXFont::FONT_INIT2);
177         /// Shortcut initialization
178         LyXFont(LyXFont::FONT_INIT3);
179
180         /// LyXFont x, y; x = y;
181         LyXFont & operator=(LyXFont const & x);
182  
183         /// Decreases font size by one
184         LyXFont & decSize();
185  
186         /// Increases font size by one
187         LyXFont & incSize();
188  
189         ///
190         FONT_FAMILY family() const;
191  
192         ///
193         FONT_SERIES series() const;
194  
195         ///
196         FONT_SHAPE shape() const;
197  
198         ///
199         FONT_SIZE size() const;
200  
201         ///
202         FONT_MISC_STATE emph() const;
203  
204         ///
205         FONT_MISC_STATE underbar() const;
206  
207         ///
208         FONT_MISC_STATE noun() const;
209
210         ///
211         FONT_MISC_STATE latex() const;
212
213         ///
214         LColor::color color() const;
215
216         ///
217         FONT_DIRECTION direction() const;
218
219         ///
220         LyXDirection getFontDirection() 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 & setDirection(LyXFont::FONT_DIRECTION d);
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 latexSize() const;
266  
267         /** Updates font settings according to request. If an
268             attribute is IGNORE, the attribute is left as it is. */
269         /* 
270          * When toggleall = true, all properties that matches the font in use
271          * will have the effect that the properties is reset to the
272          * default.  If we have a text that is TYPEWRITER_FAMILY, and is
273          * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
274          * a INHERIT_FAMILY was asked for.  This is necessary for the
275          * toggle-user-defined-style button on the toolbar.
276          */
277         void update(LyXFont const & newfont, bool toggleall = false);
278  
279         /** Reduce font to fall back to template where possible.
280             Equal fields are reduced to INHERIT */
281         void reduce(LyXFont const & tmplt);
282  
283         /// Realize font from a template (INHERIT are realized)
284         LyXFont & realize(LyXFont const & tmplt);
285
286         /// Is a given font fully resolved?
287         bool resolved() const;
288  
289         /// Read a font specification from LyXLex. Used for layout files.
290         LyXFont & lyxRead(LyXLex &);
291  
292         /// Writes the changes from this font to orgfont in .lyx format in file
293         void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
294
295         /** Writes the head of the LaTeX needed to change to this font.
296             Writes to string, the head of the LaTeX needed to change
297             to this font. Returns number of chars written. Base is the
298             font state active now.
299         */
300         int latexWriteStartChanges(string &, LyXFont const & base,
301                                    LyXFont const & prev) const;
302
303         /** Writes tha tail of the LaTeX needed to chagne to this font.
304             Returns number of chars written. Base is the font state we want
305             to achieve.
306         */
307         int latexWriteEndChanges(string &, LyXFont const & base,
308                                  LyXFont const & next) const;
309  
310         /// Build GUI description of font state
311         string stateText() const;
312
313         ///
314         int maxAscent() const; 
315
316         ///
317         int maxDescent() const;
318
319         ///
320         int ascent(char c) const;
321
322         ///
323         int descent(char c) const;
324
325         ///
326         int width(char c) const;
327
328         ///
329         int lbearing(char c) const;
330
331         ///
332         int rbearing(char c) const;
333         
334         ///
335         int textWidth(char const *s, int n) const;
336
337         ///
338         int stringWidth(string const & s) const;
339
340         ///
341         int signedStringWidth(string const & s) const;
342
343         /// Draws text and returns width of text
344         int drawText(char const *, int n, Pixmap, int baseline, int x) const;
345
346         ///
347         int drawString(string const &, Pixmap pm, int baseline, int x) const;
348
349         ///
350         LColor::color realColor() const;
351
352         ///
353         XID getFontID() const {
354                 return getXFontstruct()->fid;
355         }
356         
357         ///
358         friend inline
359         bool operator==(LyXFont const & font1, LyXFont const & font2) {
360                 return font1.bits == font2.bits;
361         }
362
363         ///
364         friend inline
365         bool operator!=(LyXFont const & font1, LyXFont const & font2) {
366                 return font1.bits != font2.bits;
367         }
368
369         /// compares two fonts, ignoring the setting of the Latex part.
370         bool equalExceptLatex(LyXFont const &) const;
371
372 private:
373         ///
374         struct FontBits {
375                 bool operator==(FontBits const & fb1) const {
376                         return fb1.family == family &&
377                                 fb1.series == series &&
378                                 fb1.shape == shape &&
379                                 fb1.size == size &&
380                                 fb1.color == color &&
381                                 fb1.emph == emph &&
382                                 fb1.underbar == underbar &&
383                                 fb1.noun == noun &&
384                                 fb1.latex == latex &&
385                                 fb1.direction == direction;
386                 }
387                 bool operator!=(FontBits const & fb1) const {
388                         return !(fb1 == *this);
389                 }
390                 
391                 FONT_FAMILY family;
392                 FONT_SERIES series;
393                 FONT_SHAPE shape;
394                 FONT_SIZE size;
395                 LColor::color color;
396                 FONT_MISC_STATE emph;
397                 FONT_MISC_STATE underbar;
398                 FONT_MISC_STATE noun;
399                 FONT_MISC_STATE latex;
400                 FONT_DIRECTION direction;
401         };
402
403         FontBits bits;
404         
405         /// Sane font
406         static FontBits sane;
407         
408         /// All inherit font
409         static FontBits inherit;
410  
411         /// All ignore font
412         static FontBits ignore;
413
414         /// Updates a misc setting according to request
415         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
416                                          LyXFont::FONT_MISC_STATE org);
417
418         /// Converts logical attributes to concrete shape attribute
419         LyXFont::FONT_SHAPE realShape() const;
420
421         ///
422         XFontStruct * getXFontstruct() const;
423 };
424
425 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
426
427 inline LyXFont::LyXFont()
428 {
429         bits = sane;
430 }
431
432
433 inline LyXFont::LyXFont(LyXFont const & x)
434 {
435         bits = x.bits;
436 }
437
438
439 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
440 {
441         bits = inherit;
442 }
443
444
445 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
446 {
447         bits = ignore;
448 }
449
450
451 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
452 {
453         bits = sane;
454 }
455
456
457 inline LyXFont & LyXFont::operator=(LyXFont const & x) 
458 {
459         bits = x.bits;
460         return *this;
461 }
462
463
464 inline
465 LyXFont::FONT_FAMILY LyXFont::family() const 
466 {
467         return bits.family;
468 }
469
470
471 inline
472 LyXFont::FONT_SERIES LyXFont::series() const
473 {
474         return bits.series;
475 }
476
477
478 inline
479 LyXFont::FONT_SHAPE LyXFont::shape() const
480 {
481         return bits.shape;
482 }
483
484
485 inline
486 LyXFont::FONT_SIZE LyXFont::size() const
487 {
488         return bits.size;
489 }
490
491
492 inline
493 LyXFont::FONT_MISC_STATE LyXFont::emph() const
494 {
495         return bits.emph;
496 }
497
498
499 inline
500 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
501 {
502         return bits.underbar;
503 }
504
505
506 inline
507 LyXFont::FONT_MISC_STATE LyXFont::noun() const
508 {
509         return bits.noun;
510 }
511
512
513 inline
514 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
515 {
516         return bits.latex;
517 }
518
519
520 inline
521 LColor::color LyXFont::color() const 
522 {
523         return bits.color;
524 }
525
526
527 inline
528 LyXFont::FONT_DIRECTION LyXFont::direction() const 
529 {
530         return bits.direction;
531 }
532
533 inline
534 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
535 {
536         bits.family = f;
537         return *this;
538 }
539
540
541 inline
542 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
543 {
544         bits.series = s;
545         return *this;
546 }
547
548
549 inline
550 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
551 {
552         bits.shape = s;
553         return *this;
554 }
555
556
557 inline
558 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
559 {
560         bits.size = s;
561         return *this;
562 }
563
564
565 inline
566 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
567 {
568         bits.emph = e;
569         return *this;
570 }
571
572
573 inline
574 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
575 {
576         bits.underbar = u;
577         return *this;
578 }
579
580
581 inline
582 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
583 {
584         bits.noun = n;
585         return *this;
586 }
587
588 inline
589 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
590 {
591         bits.latex = l;
592         return *this;
593 }
594
595
596 inline
597 LyXFont & LyXFont::setColor(LColor::color c)
598 {
599         bits.color = c;
600         return *this;
601 }
602
603
604 inline
605 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
606 {
607         bits.direction = d;
608         return *this;
609 }
610
611 #endif