]> git.lyx.org Git - features.git/blob - src/lyxfont.h
remove bogus backslashed from iso-8859-1, try to fix insert and encodeString, fixes...
[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  *
9  * ====================================================== */
10
11 #ifndef LYXFONT_H
12 #define LYXFONT_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include FORMS_H_LOCATION
19 #include "LString.h"
20 #include "debug.h"
21
22
23 // It might happen that locale.h defines ON and OFF. This is not good
24 // for us, since we use these names below. But of course this is due
25 // to some old compilers. Than is broken when it comes to C++ scoping.
26 #include "gettext.h" // so that we are sure tht it won't be included
27 // later. 
28 #ifdef ON
29 #undef ON
30 #endif
31
32 #ifdef OFF
33 #undef OFF
34 #endif
35
36 class LyXLex;
37
38 ///
39 class LyXFont {
40 public:
41         /** The value INHERIT_* means that the font attribute is
42             inherited from the layout. In the case of layout fonts, the
43             attribute is inherited from the default font.
44             The value IGNORE_* is used with LyXFont::update() when the
45             attribute should not be changed.
46         */
47         enum FONT_FAMILY {
48                 ///
49                 ROMAN_FAMILY, // fontstruct rely on this to be 0
50                 ///
51                 SANS_FAMILY,
52                 ///
53                 TYPEWRITER_FAMILY,
54                 ///
55                 SYMBOL_FAMILY,
56                 ///
57                 INHERIT_FAMILY,
58                 ///
59                 IGNORE_FAMILY
60         };
61
62         ///
63         enum FONT_SERIES {
64                 ///
65                 MEDIUM_SERIES, // fontstruct rely on this to be 0
66                 ///
67                 BOLD_SERIES,
68                 ///
69                 INHERIT_SERIES,
70                 ///
71                 IGNORE_SERIES
72         };
73
74         ///
75         enum FONT_SHAPE {
76                 ///
77                 UP_SHAPE, // fontstruct rely on this to be 0
78                 ///
79                 ITALIC_SHAPE,
80                 ///
81                 SLANTED_SHAPE,
82                 ///
83                 SMALLCAPS_SHAPE,
84                 ///
85                 INHERIT_SHAPE,
86                 ///
87                 IGNORE_SHAPE
88         };
89
90         ///
91         enum FONT_SIZE {
92                 ///
93                 SIZE_TINY, // fontstruct rely on this to be 0
94                 ///
95                 SIZE_SCRIPT,
96                 ///
97                 SIZE_FOOTNOTE,
98                 ///
99                 SIZE_SMALL,
100                 ///
101                 SIZE_NORMAL,
102                 ///
103                 SIZE_LARGE,
104                 ///
105                 SIZE_LARGER,
106                 ///
107                 SIZE_LARGEST,
108                 ///
109                 SIZE_HUGE,
110                 ///
111                 SIZE_HUGER,
112                 ///
113                 INCREASE_SIZE,
114                 ///
115                 DECREASE_SIZE,
116                 ///
117                 INHERIT_SIZE,
118                 ///
119                 IGNORE_SIZE
120         };
121  
122         /// Used for emph, underbar, noun and latex toggles
123         enum FONT_MISC_STATE {
124                 ///
125                 OFF,
126                 ///
127                 ON,
128                 ///
129                 TOGGLE,
130                 ///
131                 INHERIT,
132                 ///
133                 IGNORE
134         };
135  
136         ///
137         enum FONT_COLOR {
138                 ///
139                 NONE,
140                 ///
141                 BLACK,
142                 ///
143                 WHITE,
144                 ///
145                 RED,
146                 ///
147                 GREEN,
148                 ///
149                 BLUE,
150                 ///
151                 CYAN,
152                 ///
153                 MAGENTA,
154                 ///
155                 YELLOW,
156                 ///
157                 MATH,
158                 ///
159                 INSET,
160                 ///
161                 INHERIT_COLOR,
162                 ///
163                 IGNORE_COLOR
164         };
165
166         /// Trick to overload constructor and make it megafast
167         enum FONT_INIT1 {
168                 ///
169                 ALL_INHERIT
170         };
171         ///
172         enum FONT_INIT2 {
173                 ///
174                 ALL_IGNORE
175         };
176         ///
177         enum FONT_INIT3 {
178                 ///
179                 ALL_SANE
180         };
181
182         ///
183         LyXFont();
184
185         /// LyXFont x(LyXFont ...) and LyXFont x = LyXFont ...
186         LyXFont(LyXFont const & x);
187  
188         /// Shortcut initialization
189         LyXFont(LyXFont::FONT_INIT1);
190         /// Shortcut initialization
191         LyXFont(LyXFont::FONT_INIT2);
192         /// Shortcut initialization
193         LyXFont(LyXFont::FONT_INIT3);
194
195         /// LyXFont x, y; x = y;
196         LyXFont& operator=(LyXFont const & x);
197  
198         /// Decreases font size by one
199         LyXFont & decSize();
200  
201         /// Increases font size by one
202         LyXFont & incSize();
203  
204         ///
205         FONT_FAMILY family() const;
206  
207         ///
208         FONT_SERIES series() const;
209  
210         ///
211         FONT_SHAPE shape() const;
212  
213         ///
214         FONT_SIZE size() const;
215  
216         ///
217         FONT_MISC_STATE emph() const;
218  
219         ///
220         FONT_MISC_STATE underbar() const;
221  
222         ///
223         FONT_MISC_STATE noun() const;
224
225         ///
226         FONT_MISC_STATE latex() const;
227  
228         ///
229         FONT_COLOR color() const;
230  
231         ///
232         LyXFont & setFamily(LyXFont::FONT_FAMILY f);
233         ///
234         LyXFont & setSeries(LyXFont::FONT_SERIES s);
235         ///
236         LyXFont & setShape(LyXFont::FONT_SHAPE s);
237         ///
238         LyXFont & setSize(LyXFont::FONT_SIZE s);
239         ///
240         LyXFont & setEmph(LyXFont::FONT_MISC_STATE e);
241         ///
242         LyXFont & setUnderbar(LyXFont::FONT_MISC_STATE u);
243         ///
244         LyXFont & setNoun(LyXFont::FONT_MISC_STATE n);
245         ///
246         LyXFont & setLatex(LyXFont::FONT_MISC_STATE l);
247         ///
248         LyXFont & setColor(LyXFont::FONT_COLOR c);
249  
250         /// Set family after LyX text format
251         LyXFont & setLyXFamily(string const &);
252  
253         /// Set series after LyX text format
254         LyXFont & setLyXSeries(string const &);
255  
256         /// Set shape after LyX text format
257         LyXFont & setLyXShape(string const &);
258  
259         /// Set size after LyX text format
260         LyXFont & setLyXSize(string const &);
261  
262         /// Returns misc flag after LyX text format
263         LyXFont::FONT_MISC_STATE setLyXMisc(string const &);
264
265         /// Sets color after LyX text format
266         LyXFont & setLyXColor(string const &);
267  
268         /// Sets size after GUI name
269         LyXFont & setGUISize(string const &);
270  
271         /// Returns size of font in LaTeX text notation
272         string latexSize() const;
273  
274         /** Updates font settings according to request. If an
275             attribute is IGNORE, the attribute is left as it is. */
276         /* 
277          * When toggleall = true, all properties that matches the font in use
278          * will have the effect that the properties is reset to the
279          * default.  If we have a text that is TYPEWRITER_FAMILY, and is
280          * update()'ed with TYPEWRITER_FAMILY, the operation will be as if
281          * a INHERIT_FAMILY was asked for.  This is necessary for the
282          * toggle-user-defined-style button on the toolbar.
283          */
284         void update(LyXFont const & newfont, bool toggleall = false);
285  
286         /** Reduce font to fall back to template where possible.
287             Equal fields are reduced to INHERIT */
288         void reduce(LyXFont const & tmplt);
289  
290         /// Realize font from a template (INHERIT are realized)
291         LyXFont & realize(LyXFont const & tmplt);
292
293         /// Is a given font fully resolved?
294         bool resolved() const;
295  
296         /// Read a font specification from LyXLex. Used for layout files.
297         LyXFont & lyxRead(LyXLex&);
298  
299         /// Writes the changes from this font to orgfont in .lyx format in file
300         void lyxWriteChanges(LyXFont const & orgfont, ostream &) const;
301
302         /** Writes the head of the LaTeX needed to change to this font.
303             Writes to string, the head of the LaTeX needed to change
304             to this font. Returns number of chars written. Base is the
305             font state active now.
306         */
307         int latexWriteStartChanges(string &, LyXFont const & base) const;
308
309         /** Writes tha tail of the LaTeX needed to chagne to this font.
310             Returns number of chars written. Base is the font state we want
311             to achieve.
312         */
313         int latexWriteEndChanges(string &, LyXFont const & base) const;
314  
315         /// Build GUI description of font state
316         string stateText() const;
317
318         ///
319         int maxAscent() const; 
320
321         ///
322         int maxDescent() const;
323
324         ///
325         int ascent(char c) const;
326
327         ///
328         int descent(char c) const;
329
330         ///
331         int width(char c) const;
332
333         ///
334         int lbearing(char c) const;
335
336         ///
337         int rbearing(char c) const;
338         
339         ///
340         int textWidth(char const *s, int n) const;
341
342         ///
343         int stringWidth(string const & s) const;
344
345         ///
346         int signedStringWidth(string const & s) const;
347
348         /// Draws text and returns width of text
349         int drawText(char const*, int n, Pixmap, int baseline, int x) const;
350
351         ///
352         int drawString(string const &, Pixmap pm, int baseline, int x) const;
353
354         ///
355         GC getGC() const;
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         /// This have to be at least 32 bits, but 64 or more does not hurt
374         typedef unsigned int ui32;
375
376         /** Representation: bit table
377             Layout of bit table:
378             11 1111 111 122 222 222 2233
379             Bit 012 34 567 8901 2345 678 901 234 567 8901
380             FFF SS SSS SSSS CCCC EEE UUU NNN LLL
381             aaa ee hhh iiii oooo mmm nnn ooo aaa
382             mmm rr aaa zzzz llll ppp ddd uuu ttt
383
384             Some might think this is a dirty representation, but it gives
385             us at least 25% speed-up, so why not?
386         */
387         ui32 bits;
388
389         ///
390         enum FONT_POSITION {
391                 ///
392                 Fam_Pos =  0,
393                 ///
394                 Ser_Pos =  3,
395                 ///
396                 Sha_Pos =  5,
397                 ///
398                 Siz_Pos =  8,
399                 ///
400                 Col_Pos = 12,
401                 ///
402                 Emp_Pos = 16,
403                 ///
404                 Und_Pos = 19,
405                 ///
406                 Nou_Pos = 22,
407                 ///
408                 Lat_Pos = 25
409         };
410
411         ///
412         enum FONT_MASK {
413                 ///
414                 Fam_Mask = 0x07,
415                 ///
416                 Ser_Mask = 0x03,
417                 ///
418                 Sha_Mask = 0x07,
419                 ///
420                 Siz_Mask = 0x0f,
421                 ///
422                 Col_Mask = 0x0f,
423                 ///
424                 Misc_Mask = 0x07
425         };
426  
427         /// Sane font
428         enum {  sane = ui32(ROMAN_FAMILY) << Fam_Pos
429                 | ui32(MEDIUM_SERIES) << Ser_Pos
430                 | ui32(UP_SHAPE) << Sha_Pos
431                 | ui32(SIZE_NORMAL) << Siz_Pos
432                 | ui32(NONE) << Col_Pos
433                 | ui32(OFF) << Emp_Pos
434                 | ui32(OFF) << Und_Pos
435                 | ui32(OFF) << Nou_Pos
436                 | ui32(OFF) << Lat_Pos};
437  
438         /// All inherit font
439         enum{ inherit = ui32(INHERIT_FAMILY) << Fam_Pos
440                       | ui32(INHERIT_SERIES) << Ser_Pos
441                       | ui32(INHERIT_SHAPE) << Sha_Pos
442                       | ui32(INHERIT_SIZE) << Siz_Pos
443                       | ui32(INHERIT_COLOR) << Col_Pos
444                       | ui32(INHERIT) << Emp_Pos
445                       | ui32(INHERIT) << Und_Pos
446                       | ui32(INHERIT) << Nou_Pos
447                       | ui32(INHERIT) << Lat_Pos};
448  
449         /// All ignore font
450         enum{ ignore = ui32(IGNORE_FAMILY) << Fam_Pos
451                       | ui32(IGNORE_SERIES) << Ser_Pos
452                       | ui32(IGNORE_SHAPE) << Sha_Pos
453                       | ui32(IGNORE_SIZE) << Siz_Pos
454                       | ui32(IGNORE_COLOR) << Col_Pos
455                       | ui32(IGNORE) << Emp_Pos
456                       | ui32(IGNORE) << Und_Pos
457                       | ui32(IGNORE) << Nou_Pos
458                       | ui32(IGNORE) << Lat_Pos};
459  
460         /// Updates a misc setting according to request
461         LyXFont::FONT_MISC_STATE setMisc(LyXFont::FONT_MISC_STATE newfont,
462                                          LyXFont::FONT_MISC_STATE org);
463
464         /// Converts logical attributes to concrete shape attribute
465         LyXFont::FONT_SHAPE realShape() const;
466
467         ///
468         XFontStruct* getXFontstruct() const;
469 };
470
471 ostream & operator<<(ostream &, LyXFont::FONT_MISC_STATE);
472
473 inline LyXFont::LyXFont()
474 {
475         bits = sane;
476 }
477
478
479 inline LyXFont::LyXFont(LyXFont const & x)
480 {
481         bits = x.bits;
482 }
483
484
485 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
486 {
487         bits = inherit;
488 }
489
490
491 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
492 {
493         bits = ignore;
494 }
495
496
497 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
498 {
499         bits = sane;
500 }
501
502
503 inline LyXFont & LyXFont::operator=(LyXFont const & x) 
504 {
505         bits = x.bits;
506         return *this;
507 }
508
509
510 // You don't have to understand the stuff below :-)
511 // It works, and it's bloody fast. (Asger)
512 inline LyXFont::FONT_FAMILY LyXFont::family() const 
513 {
514         return LyXFont::FONT_FAMILY((bits >> Fam_Pos) & Fam_Mask);
515 }
516
517
518 inline LyXFont::FONT_SERIES LyXFont::series() const
519 {
520         return LyXFont::FONT_SERIES((bits >> Ser_Pos) & Ser_Mask);
521 }
522
523
524 inline LyXFont::FONT_SHAPE LyXFont::shape() const
525 {
526         return LyXFont::FONT_SHAPE((bits >> Sha_Pos) & Sha_Mask);
527 }
528
529
530 inline LyXFont::FONT_SIZE LyXFont::size() const
531 {
532         return LyXFont::FONT_SIZE((bits >> Siz_Pos) & Siz_Mask);
533 }
534
535
536 inline LyXFont::FONT_MISC_STATE LyXFont::emph() const
537 {
538         return LyXFont::FONT_MISC_STATE((bits >> Emp_Pos) & Misc_Mask);
539 }
540
541
542 inline LyXFont::FONT_MISC_STATE LyXFont::underbar() const
543 {
544         return LyXFont::FONT_MISC_STATE((bits >> Und_Pos) & Misc_Mask);
545 }
546
547
548 inline LyXFont::FONT_MISC_STATE LyXFont::noun() const
549 {
550         return LyXFont::FONT_MISC_STATE((bits >> Nou_Pos) & Misc_Mask);
551 }
552
553
554 inline LyXFont::FONT_MISC_STATE LyXFont::latex() const 
555 {
556         return LyXFont::FONT_MISC_STATE((bits >> Lat_Pos) & Misc_Mask);
557 }
558
559
560 inline LyXFont::FONT_COLOR LyXFont::color() const 
561 {
562         return LyXFont::FONT_COLOR((bits >> Col_Pos) & Col_Mask);
563 }
564
565
566 inline LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
567 {
568         bits &= ~(Fam_Mask << Fam_Pos);
569         bits |= ui32(f) << Fam_Pos;
570         return *this;
571 }
572
573
574 inline LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
575 {
576         bits &= ~(Ser_Mask << Ser_Pos);
577         bits |= ui32(s) << Ser_Pos;
578         return *this;
579 }
580
581
582 inline LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
583 {
584         bits &= ~(Sha_Mask << Sha_Pos);
585         bits |= ui32(s) << Sha_Pos;
586         return *this;
587 }
588
589
590 inline LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
591 {
592         bits &= ~(Siz_Mask << Siz_Pos);
593         bits |= ui32(s) << Siz_Pos;
594         return *this;
595 }
596
597
598 inline LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
599 {
600         bits &= ~(Misc_Mask << Emp_Pos);
601         bits |= ui32(e) << Emp_Pos;
602         return *this;
603 }
604
605
606 inline LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
607 {
608         bits &= ~(Misc_Mask << Und_Pos);
609         bits |= ui32(u) << Und_Pos;
610         return *this;
611 }
612
613
614 inline LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
615 {
616         bits &= ~(Misc_Mask << Nou_Pos);
617         bits |= ui32(n) << Nou_Pos;
618         return *this;
619 }
620
621 inline LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
622 {
623         bits &= ~(Misc_Mask << Lat_Pos);
624         bits |= ui32(l) << Lat_Pos;
625         return *this;
626 }
627
628
629 inline LyXFont & LyXFont::setColor(LyXFont::FONT_COLOR c)
630 {
631         bits &= ~(Col_Mask << Col_Pos);
632         bits |= ui32(c) << Col_Pos;
633         return *this;
634 }
635 #endif