]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
Add -enable-assertions to configure, cleanup assertions, fix crash in LyXFont::ascent...
[lyx.git] / src / lyxfont.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-1999 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <clocale>
13
14 #ifdef __GNUG__
15 #pragma implementation "lyxfont.h"
16 #endif
17
18 #include <cctype>
19
20 #include "gettext.h"
21 #include "lyxfont.h"
22 #include "debug.h"
23 #include "lyxrc.h"
24 #include "lyxlex.h"
25 #include "lyxdraw.h"
26 #include "FontLoader.h"
27 #include "support/lstrings.h"
28
29 extern LyXRC * lyxrc;
30
31 // The global fontloader
32 FontLoader fontloader;
33
34 //
35 // Names for the GUI
36 //
37
38 string const GUIFamilyNames[6] = 
39 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
40   N_("Ignore") };
41   
42 string const GUISeriesNames[4] = 
43 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
44
45 string const GUIShapeNames[6] = 
46 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
47   N_("Ignore") };
48
49 string const GUISizeNames[14] = 
50 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
51   N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"), 
52   N_("Inherit"), N_("Ignore") };
53  
54 string const lGUISizeNames[15] = 
55 { N_("tiny"), N_("smallest"), N_("smaller"), N_("small"), N_("normal"), N_("large"),
56   N_("larger"), N_("largest"), N_("huge"), N_("huger"), N_("increase"), N_("decrease"),
57   N_("inherit"), N_("ignore"), string() };
58  
59 string const GUIMiscNames[5] = 
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61
62 string const GUIColorNames[13] = 
63 { N_("None"), N_("Black"), N_("White"), N_("Red"), N_("Green"), N_("Blue"),
64   N_("Cyan"), N_("Magenta"), 
65   N_("Yellow"), N_("Math"), N_("Inset"), N_("Inherit"), N_("Ignore") };
66  
67 //
68 // Strings used to read and write .lyx format files
69 //
70 string const LyXFamilyNames[6] = 
71 { "roman", "sans", "typewriter", "symbol", "default", "error" };
72  
73 string const LyXSeriesNames[4] = 
74 { "medium", "bold", "default", "error" };
75  
76 string const LyXShapeNames[6] = 
77 { "up", "italic", "slanted", "smallcaps", "default", "error" };
78  
79 string const LyXSizeNames[14] = 
80 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
81   "larger", "largest", "huge", "giant", 
82   "increase-error", "decrease-error", "default", "error" };
83
84 string const LyXMiscNames[12] = 
85 { "off", "on", "toggle", "default", "error" };
86
87 string const LyXColorNames[13] = 
88 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta", 
89   "yellow", "matherror", "inseterror", "default", "error" };
90
91 //
92 // Strings used to write LaTeX files
93 //
94
95 string const LaTeXFamilyNames[6] = 
96 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
97  
98 string const LaTeXSeriesNames[4] = 
99 { "textmd", "textbf", "error4", "error5" };
100  
101 string const LaTeXShapeNames[6] = 
102 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
103  
104 string const LaTeXSizeNames[14] = 
105 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
106   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
107  
108 string const LaTeXColorNames[13] = 
109 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta", 
110   "yellow", "error12", "error13", "error14", "error15" };
111
112 /// Decreases font size by one
113 LyXFont & LyXFont::decSize() 
114 {
115         switch (size()) {
116         case SIZE_HUGER:        setSize(SIZE_HUGE);     break;
117         case SIZE_HUGE:         setSize(SIZE_LARGEST);  break;
118         case SIZE_LARGEST:      setSize(SIZE_LARGER);   break;
119         case SIZE_LARGER:       setSize(SIZE_LARGE);    break;
120         case SIZE_LARGE:        setSize(SIZE_NORMAL);   break;
121         case SIZE_NORMAL:       setSize(SIZE_SMALL);    break;
122         case SIZE_SMALL:        setSize(SIZE_FOOTNOTE); break;
123         case SIZE_FOOTNOTE:     setSize(SIZE_SCRIPT);   break;
124         case SIZE_SCRIPT:       setSize(SIZE_TINY);     break;
125         case SIZE_TINY:         break;
126         case INCREASE_SIZE:
127                 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
128                 break;
129         case DECREASE_SIZE:
130                 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
131                 break;
132         case INHERIT_SIZE:
133                 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
134                 break;
135         case IGNORE_SIZE:
136                 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
137                 break;
138         }
139         return (*this);
140 }
141
142
143 /// Increases font size by one
144 LyXFont & LyXFont::incSize() 
145 {
146         switch(size()) {
147         case SIZE_HUGER:        break;
148         case SIZE_HUGE:         setSize(SIZE_HUGER);    break;
149         case SIZE_LARGEST:      setSize(SIZE_HUGE);     break;
150         case SIZE_LARGER:       setSize(SIZE_LARGEST);  break;
151         case SIZE_LARGE:        setSize(SIZE_LARGER);   break;
152         case SIZE_NORMAL:       setSize(SIZE_LARGE);    break;
153         case SIZE_SMALL:        setSize(SIZE_NORMAL);   break;
154         case SIZE_FOOTNOTE:     setSize(SIZE_SMALL);    break;
155         case SIZE_SCRIPT:       setSize(SIZE_FOOTNOTE); break;
156         case SIZE_TINY:         setSize(SIZE_SCRIPT);   break;
157         case INCREASE_SIZE:
158                 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
159                 break;
160         case DECREASE_SIZE:
161                 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
162                 break;
163         case INHERIT_SIZE:
164                 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
165                 break;
166         case IGNORE_SIZE:
167                 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
168                 break;
169         }
170         return (*this);
171 }
172
173
174 /// Updates a misc setting according to request
175 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont, 
176                                           FONT_MISC_STATE org)
177 {
178         if (newfont == TOGGLE) {
179                 if (org == ON)
180                         return OFF;
181                 else if (org == OFF)
182                         return ON;
183                 else {
184                         lyxerr <<"LyXFont::setMisc: Need state"
185                                 " ON or OFF to toggle. Setting to ON" << endl;
186                         return ON;
187                 }
188         } else if (newfont == IGNORE)
189                 return org;
190         else 
191                 return newfont;
192 }
193
194
195 /// Updates font settings according to request
196 void LyXFont::update(LyXFont const & newfont, bool toggleall)
197 {
198         if(newfont.family() == family() && toggleall)
199                 setFamily(INHERIT_FAMILY); // toggle 'back'
200         else if (newfont.family() != IGNORE_FAMILY)
201                 setFamily(newfont.family());
202         // else it's IGNORE_SHAPE
203
204         // "Old" behaviour: "Setting" bold will toggle bold on/off.
205         switch (newfont.series()) {
206         case BOLD_SERIES:
207                 // We toggle...
208                 if (series() == BOLD_SERIES && toggleall)
209                         setSeries(MEDIUM_SERIES);
210                 else
211                         setSeries(BOLD_SERIES);
212                 break;
213         case MEDIUM_SERIES:
214         case INHERIT_SERIES:
215                 setSeries(newfont.series());
216                 break;
217         case IGNORE_SERIES: 
218                 break;
219         }
220
221         if(newfont.shape() == shape() && toggleall)
222                 setShape(INHERIT_SHAPE); // toggle 'back'
223         else if (newfont.shape() != IGNORE_SHAPE)
224                 setShape(newfont.shape());
225         // else it's IGNORE_SHAPE
226
227         if (newfont.size() != IGNORE_SIZE) {
228                 if (newfont.size() == INCREASE_SIZE)
229                         incSize();
230                 else if (newfont.size() == DECREASE_SIZE)
231                         decSize();
232                 else if (newfont.size() == size() && toggleall)
233                         setSize(INHERIT_SIZE); // toggle 'back'
234                 else
235                         setSize(newfont.size());
236         }
237
238         setEmph(setMisc(newfont.emph(), emph()));
239         setUnderbar(setMisc(newfont.underbar(), underbar()));
240         setNoun(setMisc(newfont.noun(), noun()));
241         setLatex(setMisc(newfont.latex(), latex()));
242
243         if(newfont.color() == color() && toggleall)
244                 setColor(INHERIT_COLOR); // toggle 'back'
245         else if (newfont.color() != IGNORE_COLOR)
246                 setColor(newfont.color());
247 }
248
249
250 /// Reduce font to fall back to template where possible
251 void LyXFont::reduce(LyXFont const & tmplt)
252 {
253         if (family() == tmplt.family())
254                 setFamily(INHERIT_FAMILY);
255         if (series() == tmplt.series())
256                 setSeries(INHERIT_SERIES);
257         if (shape() == tmplt.shape())
258                 setShape(INHERIT_SHAPE);
259         if (size() == tmplt.size())
260                 setSize(INHERIT_SIZE);
261         if (emph() == tmplt.emph())
262                 setEmph(INHERIT);
263         if (underbar() == tmplt.underbar())
264                 setUnderbar(INHERIT);
265         if (noun() == tmplt.noun())
266                 setNoun(INHERIT);
267         if (latex() == tmplt.latex())
268                 setLatex(INHERIT);
269         if (color() == tmplt.color())
270                 setColor(INHERIT_COLOR);
271 }
272
273
274 /// Realize font from a template
275 // This one is not pretty, but it's extremely fast (Asger)
276 LyXFont & LyXFont::realize(LyXFont const & tmplt)
277 {
278         if (bits == inherit) {
279                 bits = tmplt.bits;
280                 return *this;
281         }
282
283         if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
284                 {
285                         bits &= ~(Fam_Mask << Fam_Pos);
286                         bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
287                 }
288         if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
289                 {
290                         bits &= ~(Ser_Mask << Ser_Pos);
291                         bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
292                 }
293         if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
294                 {
295                         bits &= ~(Sha_Mask << Sha_Pos);
296                         bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
297                 }
298         if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
299                 {
300                         bits &= ~(Siz_Mask << Siz_Pos);
301                         bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
302                 }
303         if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
304                 {
305                         bits &= ~(Misc_Mask << Emp_Pos);
306                         bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
307                 }
308         if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
309                 {
310                         bits &= ~(Misc_Mask << Und_Pos);
311                         bits |= (tmplt.bits & Misc_Mask << Und_Pos);
312                 }
313         if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
314                 {
315                         bits &= ~(Misc_Mask << Nou_Pos);
316                         bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
317                 }
318         if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
319                 {
320                         bits &= ~(Misc_Mask << Lat_Pos);
321                         bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
322                 }
323         if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
324                 {
325                         bits &= ~(Col_Mask << Col_Pos);
326                         bits |= (tmplt.bits & Col_Mask << Col_Pos);
327                 }
328         return *this;
329 }
330
331
332 /// Is font resolved?
333 bool LyXFont::resolved() const
334 {
335         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
336                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
337                 emph() != INHERIT && underbar() != INHERIT && 
338                 noun() != INHERIT && latex() != INHERIT && 
339                 color() != INHERIT_COLOR);
340 }
341
342
343 /// Build GUI description of font state
344 string LyXFont::stateText() const
345 {
346         string buf;
347         if (family() != INHERIT_FAMILY)
348                 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
349         if (series() != INHERIT_SERIES)
350                 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
351         if (shape() != INHERIT_SHAPE)
352                 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
353         if (size() != INHERIT_SIZE)
354                 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
355         if (color() != INHERIT_COLOR)
356                 buf += string(_(GUIColorNames[color()].c_str())) + ", ";
357  
358         if (emph() != INHERIT)
359                 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
360         if (underbar() != INHERIT)
361                 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
362         if (noun() != INHERIT)
363                 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
364         if (latex() != INHERIT)
365                 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
366         if (buf.empty())
367                 buf = _("Default");
368         buf = strip(buf, ' ');
369         buf = strip(buf, ',');
370         return buf;
371 }
372
373
374 // Set family according to lyx format string
375 LyXFont & LyXFont::setLyXFamily(string const & fam)
376 {
377         string s = lowercase(fam);
378
379         int i = 0;
380         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
381         if (s == LyXFamilyNames[i]) {
382                 setFamily(LyXFont::FONT_FAMILY(i));
383         } else
384                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
385                        << s << '\'' << endl;
386         return *this;
387 }
388
389
390 // Set series according to lyx format string
391 LyXFont & LyXFont::setLyXSeries(string const & ser)
392 {
393         string s = lowercase(ser);
394
395         int i = 0;
396         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
397         if (s == LyXSeriesNames[i]) {
398                 setSeries(LyXFont::FONT_SERIES(i));
399         } else
400                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
401                        << s << '\'' << endl;
402         return *this;
403 }
404
405
406 // Set shape according to lyx format string
407 LyXFont & LyXFont::setLyXShape(string const & sha)
408 {
409         string s = lowercase(sha);
410
411         int i = 0;
412         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
413         if (s == LyXShapeNames[i]) {
414                 setShape(LyXFont::FONT_SHAPE(i));
415         } else
416                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
417                        << s << '\'' << endl;
418         return *this;
419 }
420
421
422 // Set size according to lyx format string
423 LyXFont & LyXFont::setLyXSize(string const & siz)
424 {
425         string s = lowercase(siz);
426         int i = 0;
427         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
428         if (s == LyXSizeNames[i]) {
429                 setSize(LyXFont::FONT_SIZE(i));
430         } else
431                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
432                        << s << '\'' << endl;
433         return *this;
434 }
435
436 // Set size according to lyx format string
437 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
438 {
439         string s = lowercase(siz);
440         int i= 0;
441         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
442         if (s == LyXMiscNames[i])
443                 return FONT_MISC_STATE(i);
444         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
445                << s << '\'' << endl;
446         return OFF;
447 }
448
449 /// Sets color after LyX text format
450 LyXFont & LyXFont::setLyXColor(string const & col)
451 {
452         string s = lowercase(col);
453         int i= 0;
454         while (s != LyXColorNames[i] && LyXColorNames[i] != "error") ++i;
455         if (s == LyXColorNames[i]) {
456                 setColor(LyXFont::FONT_COLOR(i));
457         } else
458                 lyxerr << "LyXFont::setLyXColor: Unknown Color `"
459                        << s << '\'' << endl;
460         return *this;
461 }
462
463
464 /// Sets size after GUI name
465 LyXFont & LyXFont::setGUISize(string const & siz)
466 {
467         string s = lowercase(siz);
468         int i = 0;
469         while (!lGUISizeNames[i].empty() &&
470                s != _(lGUISizeNames[i].c_str()))
471                 ++i;
472         if (s == _(lGUISizeNames[i].c_str())) {
473                 setSize(LyXFont::FONT_SIZE(i));
474         } else
475                 lyxerr << "LyXFont::setGUISize: Unknown Size `"
476                        << s << '\'' << endl;
477         return *this;
478 }
479
480
481 // Returns size in latex format
482 string LyXFont::latexSize() const
483 {
484         return LaTeXSizeNames[size()];
485 }
486
487
488 // Read a font definition from given file in lyx format
489 // Used for layouts
490 LyXFont & LyXFont::lyxRead(LyXLex & lex)
491 {
492         bool error = false;
493         bool finished = false;
494         while (!finished && lex.IsOK() && !error) {
495                 lex.next();
496                 string tok = lowercase(lex.GetString());
497
498                 if (tok.empty()) {
499                         continue;
500                 } else if (tok == "endfont") {
501                         finished = true;
502                 } else if (tok == "family") {
503                         lex.next();
504                         string tok = lex.GetString();
505                         setLyXFamily(tok);
506                 } else if (tok == "series") {
507                         lex.next();
508                         string tok = lex.GetString();
509                         setLyXSeries(tok);
510                 } else if (tok == "shape") {
511                         lex.next();
512                         string tok = lex.GetString();
513                         setLyXShape(tok);
514                 } else if (tok == "size") {
515                         lex.next();
516                         string tok = lex.GetString();
517                         setLyXSize(tok);
518                 } else if (tok == "latex") {
519                         lex.next();
520                         string tok = lowercase(lex.GetString());
521
522                         if (tok == "no_latex") {
523                                 setLatex(OFF);
524                         } else if (tok == "latex") {
525                                 setLatex(ON);
526                         } else {
527                                 lex.printError("Illegal LaTeX type`$$Token'");
528                         }
529                 } else if (tok == "misc") {
530                         lex.next();
531                         string tok = lowercase(lex.GetString());
532
533                         if (tok == "no_bar") {
534                                 setUnderbar(OFF);
535                         } else if (tok == "no_emph") {
536                                 setEmph(OFF);
537                         } else if (tok == "no_noun") {
538                                 setNoun(OFF);
539                         } else if (tok == "emph") {
540                                 setEmph(ON);
541                         } else if (tok == "underbar") {
542                                 setUnderbar(ON);
543                         } else if (tok == "noun") {
544                                 setNoun(ON);
545                         } else {
546                                 lex.printError("Illegal misc type `$$Token´");
547                         }
548                 } else if (tok == "color") {
549                         lex.next();
550                         string tok = lex.GetString();
551                         setLyXColor(tok);
552                 } else {
553                         lex.printError("Unknown tag `$$Token'");
554                         error = true;
555                 }
556         }
557         return * this;
558 }
559
560
561 /// Writes the changes from this font to orgfont in .lyx format in file
562 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
563 {
564         os << "\n";
565         if (orgfont.family() != family()) {
566                 os << "\\family " << LyXFamilyNames[family()] << " \n";
567         }
568         if (orgfont.series() != series()) {
569                 os << "\\series " << LyXSeriesNames[series()] << " \n";
570         }
571         if (orgfont.shape() != shape()) {
572                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
573         }
574         if (orgfont.size() != size()) {
575                 os << "\\size " << LyXSizeNames[size()] << " \n";
576         }
577         if (orgfont.emph() != emph()) {
578                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
579         }
580         if (orgfont.underbar() != underbar()) {
581                 // This is only for backwards compatibility
582                 switch (underbar()) {
583                 case OFF:       os << "\\bar no \n"; break;
584                 case ON:        os << "\\bar under \n"; break;
585                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
586                                         "TOGGLE should not appear here!"
587                                        << endl;
588                 break;
589                 case INHERIT:   os << "\\bar default \n"; break;
590                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
591                                         "IGNORE should not appear here!"
592                                        << endl;
593                 break;
594                 }
595         }
596         if (orgfont.noun() != noun()) {
597                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
598         }
599         if (orgfont.latex() != latex()) {
600                 // This is only for backwards compatibility
601                 switch (latex()) {
602                 case OFF:       os << "\\latex no_latex \n"; break;
603                 case ON:        os << "\\latex latex \n"; break;
604                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
605                                         "TOGGLE should not appear here!"
606                                        << endl;
607                 break;
608                 case INHERIT:   os << "\\latex default \n"; break;
609                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
610                                         "IGNORE should not appear here!"
611                                        << endl;
612                 break;
613                 }
614         }
615         if (orgfont.color() != color()) {
616                 os << "\\color " << LyXColorNames[color()] << "\n";
617         }
618 }
619
620
621 /// Writes the head of the LaTeX needed to impose this font
622 // Returns number of chars written.
623 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base) const
624 {
625         LyXFont f = *this;
626         f.reduce(base);
627         
628         if (f.bits == inherit)
629                 return 0;
630         
631         int count = 0;
632         bool env = false;
633         
634         if (f.family() != INHERIT_FAMILY) {
635                 file += '\\';
636                 file += LaTeXFamilyNames[f.family()];
637                 file += '{';
638                 count += LaTeXFamilyNames[f.family()].length() + 2;
639                 env = true; //We have opened a new environment
640         }
641         if (f.series() != INHERIT_SERIES) {
642                 file += '\\';
643                 file += LaTeXSeriesNames[f.series()];
644                 file += '{';
645                 count += LaTeXSeriesNames[f.series()].length() + 2;
646                 env = true; //We have opened a new environment
647         }
648         if (f.shape() != INHERIT_SHAPE) {
649                 file += '\\';
650                 file += LaTeXShapeNames[f.shape()];
651                 file += '{';
652                 count += LaTeXShapeNames[f.shape()].length() + 2;
653                 env = true; //We have opened a new environment
654         }
655         if (f.color() != INHERIT_COLOR) {
656                 file += "\\textcolor{";
657                 file += LaTeXColorNames[f.color()];
658                 file += "}{";
659                 count += LaTeXColorNames[f.color()].length() + 13;
660                 env = true; //We have opened a new environment
661         }
662         if (f.emph() == ON) {
663                 file += "\\emph{";
664                 count += 6;
665                 env = true; //We have opened a new environment
666         }
667         if (f.underbar() == ON) {
668                 file += "\\underbar{";
669                 count += 10;
670                 env = true; //We have opened a new environment
671         }
672         // \noun{} is a LyX special macro
673         if (f.noun() == ON) {
674                 file += "\\noun{";
675                 count += 8;
676                 env = true; //We have opened a new environment
677         }
678         if (f.size() != INHERIT_SIZE) {
679                 // If we didn't open an environment above, we open one here
680                 if (!env) {
681                         file += '{';
682                         count++;
683                 }
684                 file += '\\';
685                 file += LaTeXSizeNames[f.size()];
686                 file += ' ';
687                 count += LaTeXSizeNames[f.size()].length() + 2;
688         }
689         return count;
690 }
691
692
693 /// Writes ending block of LaTeX needed to close use of this font
694 // Returns number of chars written
695 // This one corresponds to latexWriteStartChanges(). (Asger)
696 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base) const
697 {
698         LyXFont f = *this; // why do you need this?
699         f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
700         // Because this function is const. Everything breaks if this
701         // method changes the font it represents. There is no speed penalty
702         // by using the temporary. (Asger)
703
704         if (f.bits == inherit)
705                 return 0;
706         
707         int count = 0;
708         bool env = false;
709         if (f.family() != INHERIT_FAMILY) {
710                 file += '}';
711                 ++count;
712                 env = true; // Size change need not bother about closing env.
713         }
714         if (f.series() != INHERIT_SERIES) {
715                 file += '}';
716                 ++count;
717                 env = true; // Size change need not bother about closing env.
718         }
719         if (f.shape() != INHERIT_SHAPE) {
720                 file += '}';
721                 ++count;
722                 env = true; // Size change need not bother about closing env.
723         }
724         if (f.color() != INHERIT_COLOR) {
725                 file += '}';
726                 ++count;
727                 env = true; // Size change need not bother about closing env.
728         }
729         if (f.emph() == ON) {
730                 file += '}';
731                 ++count;
732                 env = true; // Size change need not bother about closing env.
733         }
734         if (f.underbar() == ON) {
735                 file += '}';
736                 ++count;
737                 env = true; // Size change need not bother about closing env.
738         }
739         if (f.noun() == ON) {
740                 file += '}';
741                 ++count;
742                 env = true; // Size change need not bother about closing env.
743         }
744         if (f.size() != INHERIT_SIZE) {
745                 // We only have to close if only size changed
746                 if (!env) {
747                         file += '}';
748                         ++count;
749                 }
750         }
751         
752         return count;
753 }
754
755
756 // Convert logical attributes to concrete shape attribute
757 LyXFont::FONT_SHAPE LyXFont::realShape() const
758 {
759         register FONT_SHAPE s = shape();
760
761         if (emph() == ON) {
762                 if (s == UP_SHAPE)
763                         s = ITALIC_SHAPE;
764                 else
765                         s = UP_SHAPE;
766         }
767         if (noun() == ON)
768                 s = SMALLCAPS_SHAPE;
769         return s;
770 }
771
772
773 GC LyXFont::getGC() const
774 {
775         GC gc;
776         if (latex() == ON)
777                 gc = ::getGC(gc_latex);
778         else {
779                 if (color() == NONE)
780                         gc = ::getGC(gc_copy);
781                 else if (color() == MATH)
782                         gc = ::getGC(gc_math);
783                 else if (color() == INSET)
784                         gc = ::getGC(gc_foot);
785                 else
786                         gc = ::GetColorGC(color());
787         }
788
789         XSetFont(fl_display, gc, getXFontstruct()->fid);
790         return gc;
791 }
792
793
794 XFontStruct * LyXFont::getXFontstruct() const
795 {
796         return fontloader.load(family(), series(), realShape(), size());
797 }
798
799
800 int LyXFont::maxAscent() const
801 {
802         return getXFontstruct()->ascent;
803 }
804
805
806 int LyXFont::maxDescent() const
807 {
808         return getXFontstruct()->descent;
809 }
810
811
812 int LyXFont::ascent(char c) const
813 {
814         XFontStruct * finfo = getXFontstruct();
815         unsigned int uc = static_cast<unsigned char>(c);
816         if (finfo->per_char
817             && uc >= finfo->min_char_or_byte2
818             && uc <= finfo->max_char_or_byte2) 
819                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
820         else
821                 return finfo->ascent;
822 }
823
824
825 int LyXFont::descent(char c) const
826 {
827         XFontStruct * finfo = getXFontstruct();
828         unsigned int uc = static_cast<unsigned char>(c);
829         if (finfo->per_char
830             && uc >= finfo->min_char_or_byte2
831             && uc <= finfo->max_char_or_byte2) 
832                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
833         else
834                 return finfo->descent;
835 }
836
837
838 // Specialized after profiling. (Asger)
839 int LyXFont::width(char c) const
840 {
841         if (realShape() != LyXFont::SMALLCAPS_SHAPE){
842                 return XTextWidth(getXFontstruct(), &c, 1);
843         } else {
844                 return textWidth(&c, 1);
845         }
846 }
847
848
849 int LyXFont::lbearing(char c) const
850 {
851         XFontStruct * finfo = getXFontstruct();
852         unsigned int uc = static_cast<unsigned char>(c);
853         if (finfo->per_char
854             && uc >= finfo->min_char_or_byte2
855             && uc <= finfo->max_char_or_byte2) 
856                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
857         else
858                 return 0;
859 }
860
861
862 int LyXFont::rbearing(char c) const
863 {
864         XFontStruct * finfo = getXFontstruct();
865         unsigned int uc = static_cast<unsigned char>(c);
866         if (finfo->per_char
867             && uc >= finfo->min_char_or_byte2
868             && uc <= finfo->max_char_or_byte2) 
869                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
870         else
871                 return width(c);
872 }
873
874
875 int LyXFont::textWidth(char const * s, int n) const
876 {
877         if (realShape() != LyXFont::SMALLCAPS_SHAPE){
878                 return XTextWidth(getXFontstruct(), s, n);
879         } else {
880                 // emulate smallcaps since X doesn't support this
881                 unsigned int result = 0;
882                 char c;
883                 LyXFont smallfont = *this;
884                 smallfont.decSize();
885                 smallfont.decSize();
886                 smallfont.setShape(LyXFont::UP_SHAPE);
887                 for (int i = 0; i < n; ++i) {
888                         c = s[i];
889                         if (islower(c)){
890                                 c = toupper(c);
891                                 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
892                         } else {
893                                 result += XTextWidth(getXFontstruct(), &c, 1);
894                         }
895                 }
896                 return result;
897         }
898 }
899
900
901 int LyXFont::stringWidth(string const & s) const
902 {
903         if (s.empty()) return 0;
904         return textWidth(s.c_str(), s.length());
905 }
906
907
908 int LyXFont::signedStringWidth(string const & s) const
909 {
910         if (s.empty()) return 0;
911         if (s.c_str()[0] == '-')
912                 return -textWidth(s.c_str()+1, s.length()-1);
913         else
914                 return textWidth(s.c_str(), s.length());
915 }
916
917
918 int LyXFont::drawText(char const * s, int n, Pixmap pm, 
919                       int baseline, int x) const
920 {
921         if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
922                 XDrawString(fl_display,
923                             pm,
924                             getGC(),
925                             x, baseline,
926                             s, n);
927                 XFlush(fl_display);
928                 return XTextWidth(getXFontstruct(), s, n);
929
930         } else {
931                 // emulate smallcaps since X doesn't support this
932                 char c;
933                 int sx = x;
934                 LyXFont smallfont = *this;
935                 smallfont.decSize();
936                 smallfont.decSize();
937                 smallfont.setShape(LyXFont::UP_SHAPE);
938                 for (int i = 0; i < n; ++i) {
939                         c = s[i];
940                         if (islower(c)){
941                                 c = toupper(c);
942                                 XDrawString(fl_display,
943                                             pm,
944                                             smallfont.getGC(),
945                                             x, baseline,
946                                             &c, 1);
947                                 x += XTextWidth(smallfont.getXFontstruct(),
948                                                 &c, 1);
949                                 XFlush(fl_display);
950                         } else {
951                                 XDrawString(fl_display,
952                                             pm,
953                                             getGC(),
954                                             x, baseline,
955                                             &c, 1);
956                                 x += XTextWidth(getXFontstruct(), &c, 1);
957                                 XFlush(fl_display);
958                         }
959                 }
960                 return x - sx;
961         }
962 }
963
964
965 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
966 {
967         return drawText(s.c_str(), s.length(), pm, baseline, x);
968 }
969
970
971 bool LyXFont::equalExceptLatex(LyXFont const & f) const 
972 {
973         LyXFont f1 = *this;
974         f1.setLatex(f.latex());
975         return f1 == f;
976 }
977
978
979 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
980 {
981         return o << int(fms);
982 }