]> git.lyx.org Git - lyx.git/blob - src/lyxfont.h
5375d72de8954f8bfcb66170d6e5650b61df9181
[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
39 class LyXLex;
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         enum FONT_DIRECTION {
126                 ///
127                 LTR_DIR,
128                 ///
129                 RTL_DIR,
130                 ///
131                 TOGGLE_DIR,
132                 ///
133                 INHERIT_DIR,
134                 ///
135                 IGNORE_DIR
136         };
137
138         /// Used for emph, underbar, noun and latex toggles
139         enum FONT_MISC_STATE {
140                 ///
141                 OFF,
142                 ///
143                 ON,
144                 ///
145                 TOGGLE,
146                 ///
147                 INHERIT,
148                 ///
149                 IGNORE
150         };
151
152         /// Trick to overload constructor and make it megafast
153         enum FONT_INIT1 {
154                 ///
155                 ALL_INHERIT
156         };
157         ///
158         enum FONT_INIT2 {
159                 ///
160                 ALL_IGNORE
161         };
162         ///
163         enum FONT_INIT3 {
164                 ///
165                 ALL_SANE
166         };
167
168         ///
169         LyXFont();
170
171         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
172         LyXFont(LyXFont const & x);
173  
174         /// Shortcut initialization
175         LyXFont(LyXFont::FONT_INIT1);
176         /// Shortcut initialization
177         LyXFont(LyXFont::FONT_INIT2);
178         /// Shortcut initialization
179         LyXFont(LyXFont::FONT_INIT3);
180
181         /// LyXFont x, y; x = y;
182         LyXFont & operator=(LyXFont const & x);
183  
184         /// Decreases font size by one
185         LyXFont & decSize();
186  
187         /// Increases font size by one
188         LyXFont & incSize();
189  
190         ///
191         FONT_FAMILY family() const;
192  
193         ///
194         FONT_SERIES series() const;
195  
196         ///
197         FONT_SHAPE shape() const;
198  
199         ///
200         FONT_SIZE size() const;
201  
202         ///
203         FONT_MISC_STATE emph() const;
204  
205         ///
206         FONT_MISC_STATE underbar() const;
207  
208         ///
209         FONT_MISC_STATE noun() const;
210
211         ///
212         FONT_MISC_STATE latex() const;
213
214         ///
215         LColor::color color() const;
216
217         ///
218         FONT_DIRECTION direction() const;
219
220         ///
221         LyXDirection getFontDirection() const;
222         
223         ///
224         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
225         ///
226         LyXFont & setSeries(LyXFont::FONT_SERIES s);
227         ///
228         LyXFont & setShape(LyXFont::FONT_SHAPE s);
229         ///
230         LyXFont & setSize(LyXFont::FONT_SIZE s);
231         ///
232         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
233         ///
234         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
235         ///
236         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
237         ///
238         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
239         ///
240         LyXFont & setColor(LColor::color c);
241         ///
242         LyXFont & setDirection(LyXFont::FONT_DIRECTION d);
243
244         /// Set family after LyX text format
245         LyXFont & setLyXFamily(string const &);
246  
247         /// Set series after LyX text format
248         LyXFont & setLyXSeries(string const &);
249  
250         /// Set shape after LyX text format
251         LyXFont & setLyXShape(string const &);
252  
253         /// Set size after LyX text format
254         LyXFont & setLyXSize(string const &);
255  
256         /// Returns misc flag after LyX text format
257         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
258
259         /// Sets color after LyX text format
260         LyXFont & setLyXColor(string const &);
261  
262         /// Sets size after GUI name
263         LyXFont & setGUISize(string const &);
264  
265         /// Returns size of font in LaTeX text notation
266         string latexSize() const;
267  
268         /** Updates font settings according to request. If an
269             attribute is IGNORE, the attribute is left as it is. */
270         /* 
271          * When toggleall = true, all properties that matches the font in use
272          * will have the effect that the properties is reset to the
273          * default.  If we have a text that is TYPEWRITER_FAMILY, and is
274          * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
275          * a INHERIT_FAMILY was asked for.  This is necessary for the
276          * toggle-user-defined-style button on the toolbar.
277          */
278         void update(LyXFont const & newfont, 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, ostream &) const;
295
296
297         /** Writes the head of the LaTeX needed to change to this font.
298             Writes to string, the head of the LaTeX needed to change
299             to this font. Returns number of chars written. Base is the
300             font state active now.
301         */
302         int latexWriteStartChanges(ostream &, LyXFont const & base,
303                                    LyXFont const & prev) const;
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(ostream &, LyXFont const & base,
309                                  LyXFont const & next) const;
310
311         /// Build GUI description of font state
312         string stateText() const;
313
314         ///
315         int maxAscent() const; 
316
317         ///
318         int maxDescent() const;
319
320         ///
321         int ascent(char c) const;
322
323         ///
324         int descent(char c) const;
325
326         ///
327         int width(char c) const;
328
329         ///
330         int lbearing(char c) const;
331
332         ///
333         int rbearing(char c) const;
334         
335         ///
336         int textWidth(char const *s, int n) const;
337
338         ///
339         int stringWidth(string const & s) const;
340
341         ///
342         int signedStringWidth(string const & s) const;
343
344         /// Draws text and returns width of text
345         int drawText(char const *, int n, Pixmap, int baseline, int x) const;
346
347         ///
348         int drawString(string const &, Pixmap pm, int baseline, int x) const;
349
350         ///
351         LColor::color realColor() const;
352
353         ///
354         XID getFontID() const {
355                 return getXFontstruct()->fid;
356         }
357         
358         ///
359         friend inline
360         bool operator==(LyXFont const & font1, LyXFont const & font2) {
361                 return font1.bits == font2.bits;
362         }
363
364         ///
365         friend inline
366         bool operator!=(LyXFont const & font1, LyXFont const & font2) {
367                 return font1.bits != font2.bits;
368         }
369
370         /// compares two fonts, ignoring the setting of the Latex part.
371         bool equalExceptLatex(LyXFont const &) const;
372
373 private:
374         ///
375         struct FontBits {
376                 bool operator==(FontBits const & fb1) const {
377                         return fb1.family == family &&
378                                 fb1.series == series &&
379                                 fb1.shape == shape &&
380                                 fb1.size == size &&
381                                 fb1.color == color &&
382                                 fb1.emph == emph &&
383                                 fb1.underbar == underbar &&
384                                 fb1.noun == noun &&
385                                 fb1.latex == latex &&
386                                 fb1.direction == direction;
387                 }
388                 bool operator!=(FontBits const & fb1) const {
389                         return !(fb1 == *this);
390                 }
391                 
392                 FONT_FAMILY family;
393                 FONT_SERIES series;
394                 FONT_SHAPE shape;
395                 FONT_SIZE size;
396                 LColor::color color;
397                 FONT_MISC_STATE emph;
398                 FONT_MISC_STATE underbar;
399                 FONT_MISC_STATE noun;
400                 FONT_MISC_STATE latex;
401                 FONT_DIRECTION direction;
402         };
403
404         FontBits bits;
405         
406         /// Sane font
407         static FontBits sane;
408         
409         /// All inherit font
410         static FontBits inherit;
411  
412         /// All ignore font
413         static FontBits ignore;
414
415         /// Updates a misc setting according to request
416         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
417                                          LyXFont::FONT_MISC_STATE org);
418
419         /// Converts logical attributes to concrete shape attribute
420         LyXFont::FONT_SHAPE realShape() const;
421
422         ///
423         XFontStruct * getXFontstruct() const;
424 };
425
426 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
427
428 inline
429 LyXFont::LyXFont()
430 {
431         bits = sane;
432 }
433
434
435 inline
436 LyXFont::LyXFont(LyXFont const & x)
437 {
438         bits = x.bits;
439 }
440
441
442 inline
443 LyXFont::LyXFont(LyXFont::FONT_INIT1)
444 {
445         bits = inherit;
446 }
447
448
449 inline
450 LyXFont::LyXFont(LyXFont::FONT_INIT2)
451 {
452         bits = ignore;
453 }
454
455
456 inline
457 LyXFont::LyXFont(LyXFont::FONT_INIT3)
458 {
459         bits = sane;
460 }
461
462
463 inline
464 LyXFont & LyXFont::operator=(LyXFont const & x) 
465 {
466         bits = x.bits;
467         return *this;
468 }
469
470
471 inline
472 LyXFont::FONT_FAMILY LyXFont::family() const 
473 {
474         return bits.family;
475 }
476
477
478 inline
479 LyXFont::FONT_SERIES LyXFont::series() const
480 {
481         return bits.series;
482 }
483
484
485 inline
486 LyXFont::FONT_SHAPE LyXFont::shape() const
487 {
488         return bits.shape;
489 }
490
491
492 inline
493 LyXFont::FONT_SIZE LyXFont::size() const
494 {
495         return bits.size;
496 }
497
498
499 inline
500 LyXFont::FONT_MISC_STATE LyXFont::emph() const
501 {
502         return bits.emph;
503 }
504
505
506 inline
507 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
508 {
509         return bits.underbar;
510 }
511
512
513 inline
514 LyXFont::FONT_MISC_STATE LyXFont::noun() const
515 {
516         return bits.noun;
517 }
518
519
520 inline
521 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
522 {
523         return bits.latex;
524 }
525
526
527 inline
528 LColor::color LyXFont::color() const 
529 {
530         return bits.color;
531 }
532
533
534 inline
535 LyXFont::FONT_DIRECTION LyXFont::direction() const 
536 {
537         return bits.direction;
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::setDirection(LyXFont::FONT_DIRECTION d)
613 {
614         bits.direction = d;
615         return *this;
616 }
617
618 #endif