]> git.lyx.org Git - features.git/blob - src/lyxfont.h
change to use ostreams instead of string when writing files. fiddling with insettext...
[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 LyXFont::LyXFont()
445 {
446         bits = sane;
447 }
448
449
450 inline LyXFont::LyXFont(LyXFont const & x)
451 {
452         bits = x.bits;
453 }
454
455
456 inline LyXFont::LyXFont(LyXFont::FONT_INIT1)
457 {
458         bits = inherit;
459 }
460
461
462 inline LyXFont::LyXFont(LyXFont::FONT_INIT2)
463 {
464         bits = ignore;
465 }
466
467
468 inline LyXFont::LyXFont(LyXFont::FONT_INIT3)
469 {
470         bits = sane;
471 }
472
473
474 inline LyXFont & LyXFont::operator=(LyXFont const & x) 
475 {
476         bits = x.bits;
477         return *this;
478 }
479
480
481 inline
482 LyXFont::FONT_FAMILY LyXFont::family() const 
483 {
484         return bits.family;
485 }
486
487
488 inline
489 LyXFont::FONT_SERIES LyXFont::series() const
490 {
491         return bits.series;
492 }
493
494
495 inline
496 LyXFont::FONT_SHAPE LyXFont::shape() const
497 {
498         return bits.shape;
499 }
500
501
502 inline
503 LyXFont::FONT_SIZE LyXFont::size() const
504 {
505         return bits.size;
506 }
507
508
509 inline
510 LyXFont::FONT_MISC_STATE LyXFont::emph() const
511 {
512         return bits.emph;
513 }
514
515
516 inline
517 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
518 {
519         return bits.underbar;
520 }
521
522
523 inline
524 LyXFont::FONT_MISC_STATE LyXFont::noun() const
525 {
526         return bits.noun;
527 }
528
529
530 inline
531 LyXFont::FONT_MISC_STATE LyXFont::latex() const 
532 {
533         return bits.latex;
534 }
535
536
537 inline
538 LColor::color LyXFont::color() const 
539 {
540         return bits.color;
541 }
542
543
544 inline
545 LyXFont::FONT_DIRECTION LyXFont::direction() const 
546 {
547         return bits.direction;
548 }
549
550 inline
551 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
552 {
553         bits.family = f;
554         return *this;
555 }
556
557
558 inline
559 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
560 {
561         bits.series = s;
562         return *this;
563 }
564
565
566 inline
567 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
568 {
569         bits.shape = s;
570         return *this;
571 }
572
573
574 inline
575 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
576 {
577         bits.size = s;
578         return *this;
579 }
580
581
582 inline
583 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
584 {
585         bits.emph = e;
586         return *this;
587 }
588
589
590 inline
591 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
592 {
593         bits.underbar = u;
594         return *this;
595 }
596
597
598 inline
599 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
600 {
601         bits.noun = n;
602         return *this;
603 }
604
605 inline
606 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
607 {
608         bits.latex = l;
609         return *this;
610 }
611
612
613 inline
614 LyXFont & LyXFont::setColor(LColor::color c)
615 {
616         bits.color = c;
617         return *this;
618 }
619
620
621 inline
622 LyXFont & LyXFont::setDirection(LyXFont::FONT_DIRECTION d)
623 {
624         bits.direction = d;
625         return *this;
626 }
627
628 #endif