]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
82c42aec7a26e5231e1edca3a296822433e72280
[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 GUIDirectionNames[5] = 
63 { N_("LTR"), N_("RTL"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64
65
66 #ifndef USE_PAINTER
67 string const GUIColorNames[13] = 
68 { N_("None"), N_("Black"), N_("White"), N_("Red"), N_("Green"), N_("Blue"),
69   N_("Cyan"), N_("Magenta"), 
70   N_("Yellow"), N_("Math"), N_("Inset"), N_("Inherit"), N_("Ignore") };
71 #endif
72
73 //
74 // Strings used to read and write .lyx format files
75 //
76 string const LyXFamilyNames[6] = 
77 { "roman", "sans", "typewriter", "symbol", "default", "error" };
78  
79 string const LyXSeriesNames[4] = 
80 { "medium", "bold", "default", "error" };
81  
82 string const LyXShapeNames[6] = 
83 { "up", "italic", "slanted", "smallcaps", "default", "error" };
84  
85 string const LyXSizeNames[14] = 
86 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
87   "larger", "largest", "huge", "giant", 
88   "increase-error", "decrease-error", "default", "error" };
89
90 string const LyXMiscNames[12] = 
91 { "off", "on", "toggle", "default", "error" };
92
93 #ifndef USE_PAINTER
94 string const LyXColorNames[13] = 
95 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta", 
96   "yellow", "matherror", "inseterror", "default", "error" };
97 #endif
98
99 //
100 // Strings used to write LaTeX files
101 //
102
103 string const LaTeXFamilyNames[6] = 
104 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
105  
106 string const LaTeXSeriesNames[4] = 
107 { "textmd", "textbf", "error4", "error5" };
108  
109 string const LaTeXShapeNames[6] = 
110 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
111  
112 string const LaTeXSizeNames[14] = 
113 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
114   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
115
116 #ifndef USE_PAINTER
117 string const LaTeXColorNames[13] = 
118 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta", 
119   "yellow", "error12", "error13", "error14", "error15" };
120 #endif
121
122 #ifdef NEW_BITS
123 LyXFont::FontBits LyXFont::sane = { ROMAN_FAMILY,
124                   MEDIUM_SERIES,
125                   UP_SHAPE,
126                   SIZE_NORMAL,
127 #ifdef USE_PAINTER
128                   LColor::none,
129 #else
130                   NONE,
131 #endif
132                   OFF,
133                   OFF,
134                   OFF,
135                   OFF,
136                   LTR_DIR };
137
138 LyXFont::FontBits LyXFont::inherit = { INHERIT_FAMILY, 
139                       INHERIT_SERIES, 
140                       INHERIT_SHAPE, 
141                       INHERIT_SIZE,
142 #ifdef USE_PAINTER
143                       LColor::inherit,
144 #else
145                       INHERIT_COLOR,
146 #endif
147                       INHERIT,
148                       INHERIT,
149                       INHERIT,
150                       INHERIT,
151                       INHERIT_DIR };
152
153 LyXFont::FontBits LyXFont::ignore = { IGNORE_FAMILY,
154                      IGNORE_SERIES,
155                      IGNORE_SHAPE,
156                      IGNORE_SIZE,
157 #ifdef USE_PAINTER
158                      LColor::ignore,
159 #else
160                      IGNORE_COLOR,
161 #endif
162                      IGNORE,
163                      IGNORE,
164                      IGNORE,
165                      IGNORE,
166                      IGNORE_DIR };
167 #endif
168
169 /// Decreases font size by one
170 LyXFont & LyXFont::decSize() 
171 {
172         switch (size()) {
173         case SIZE_HUGER:        setSize(SIZE_HUGE);     break;
174         case SIZE_HUGE:         setSize(SIZE_LARGEST);  break;
175         case SIZE_LARGEST:      setSize(SIZE_LARGER);   break;
176         case SIZE_LARGER:       setSize(SIZE_LARGE);    break;
177         case SIZE_LARGE:        setSize(SIZE_NORMAL);   break;
178         case SIZE_NORMAL:       setSize(SIZE_SMALL);    break;
179         case SIZE_SMALL:        setSize(SIZE_FOOTNOTE); break;
180         case SIZE_FOOTNOTE:     setSize(SIZE_SCRIPT);   break;
181         case SIZE_SCRIPT:       setSize(SIZE_TINY);     break;
182         case SIZE_TINY:         break;
183         case INCREASE_SIZE:
184                 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
185                 break;
186         case DECREASE_SIZE:
187                 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
188                 break;
189         case INHERIT_SIZE:
190                 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
191                 break;
192         case IGNORE_SIZE:
193                 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
194                 break;
195         }
196         return *this;
197 }
198
199
200 /// Increases font size by one
201 LyXFont & LyXFont::incSize() 
202 {
203         switch(size()) {
204         case SIZE_HUGER:        break;
205         case SIZE_HUGE:         setSize(SIZE_HUGER);    break;
206         case SIZE_LARGEST:      setSize(SIZE_HUGE);     break;
207         case SIZE_LARGER:       setSize(SIZE_LARGEST);  break;
208         case SIZE_LARGE:        setSize(SIZE_LARGER);   break;
209         case SIZE_NORMAL:       setSize(SIZE_LARGE);    break;
210         case SIZE_SMALL:        setSize(SIZE_NORMAL);   break;
211         case SIZE_FOOTNOTE:     setSize(SIZE_SMALL);    break;
212         case SIZE_SCRIPT:       setSize(SIZE_FOOTNOTE); break;
213         case SIZE_TINY:         setSize(SIZE_SCRIPT);   break;
214         case INCREASE_SIZE:
215                 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
216                 break;
217         case DECREASE_SIZE:
218                 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
219                 break;
220         case INHERIT_SIZE:
221                 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
222                 break;
223         case IGNORE_SIZE:
224                 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
225                 break;
226         }
227         return *this;
228 }
229
230
231 /// Updates a misc setting according to request
232 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont, 
233                                           FONT_MISC_STATE org)
234 {
235         if (newfont == TOGGLE) {
236                 if (org == ON)
237                         return OFF;
238                 else if (org == OFF)
239                         return ON;
240                 else {
241                         lyxerr <<"LyXFont::setMisc: Need state"
242                                 " ON or OFF to toggle. Setting to ON" << endl;
243                         return ON;
244                 }
245         } else if (newfont == IGNORE)
246                 return org;
247         else 
248                 return newfont;
249 }
250
251
252 /// Updates font settings according to request
253 void LyXFont::update(LyXFont const & newfont, bool toggleall)
254 {
255         if(newfont.family() == family() && toggleall)
256                 setFamily(INHERIT_FAMILY); // toggle 'back'
257         else if (newfont.family() != IGNORE_FAMILY)
258                 setFamily(newfont.family());
259         // else it's IGNORE_SHAPE
260
261         // "Old" behaviour: "Setting" bold will toggle bold on/off.
262         switch (newfont.series()) {
263         case BOLD_SERIES:
264                 // We toggle...
265                 if (series() == BOLD_SERIES && toggleall)
266                         setSeries(MEDIUM_SERIES);
267                 else
268                         setSeries(BOLD_SERIES);
269                 break;
270         case MEDIUM_SERIES:
271         case INHERIT_SERIES:
272                 setSeries(newfont.series());
273                 break;
274         case IGNORE_SERIES: 
275                 break;
276         }
277
278         if(newfont.shape() == shape() && toggleall)
279                 setShape(INHERIT_SHAPE); // toggle 'back'
280         else if (newfont.shape() != IGNORE_SHAPE)
281                 setShape(newfont.shape());
282         // else it's IGNORE_SHAPE
283
284         if (newfont.size() != IGNORE_SIZE) {
285                 if (newfont.size() == INCREASE_SIZE)
286                         incSize();
287                 else if (newfont.size() == DECREASE_SIZE)
288                         decSize();
289                 else if (newfont.size() == size() && toggleall)
290                         setSize(INHERIT_SIZE); // toggle 'back'
291                 else
292                         setSize(newfont.size());
293         }
294
295         setEmph(setMisc(newfont.emph(), emph()));
296         setUnderbar(setMisc(newfont.underbar(), underbar()));
297         setNoun(setMisc(newfont.noun(), noun()));
298         setLatex(setMisc(newfont.latex(), latex()));
299
300         switch(newfont.direction()) {
301         case TOGGLE_DIR:
302                 if (direction() == LTR_DIR)
303                         setDirection(RTL_DIR);
304                 else
305                         setDirection(LTR_DIR);
306                 break;
307         case IGNORE_DIR:
308                 break;
309         default:
310                 setDirection(newfont.direction());
311         }
312
313 #ifdef USE_PAINTER
314         if(newfont.color() == color() && toggleall)
315                 setColor(LColor::inherit); // toggle 'back'
316         else if (newfont.color() != LColor::ignore)
317                 setColor(newfont.color());
318 #else
319         if(newfont.color() == color() && toggleall)
320                 setColor(LyXFont::INHERIT_COLOR); // toggle 'back'
321         else if (newfont.color() != LyXFont::IGNORE_COLOR)
322                 setColor(newfont.color());
323 #endif
324 }
325
326
327 /// Reduce font to fall back to template where possible
328 void LyXFont::reduce(LyXFont const & tmplt)
329 {
330         if (family() == tmplt.family())
331                 setFamily(INHERIT_FAMILY);
332         if (series() == tmplt.series())
333                 setSeries(INHERIT_SERIES);
334         if (shape() == tmplt.shape())
335                 setShape(INHERIT_SHAPE);
336         if (size() == tmplt.size())
337                 setSize(INHERIT_SIZE);
338         if (emph() == tmplt.emph())
339                 setEmph(INHERIT);
340         if (underbar() == tmplt.underbar())
341                 setUnderbar(INHERIT);
342         if (noun() == tmplt.noun())
343                 setNoun(INHERIT);
344         if (latex() == tmplt.latex())
345                 setLatex(INHERIT);
346 #ifdef USE_PAINTER
347         if (color() == tmplt.color())
348                 setColor(LColor::inherit);
349 #else
350         if (color() == tmplt.color())
351                 setColor(LyXFont::INHERIT_COLOR);
352 #endif
353         if (direction() == tmplt.direction())
354                 setDirection(INHERIT_DIR);
355 }
356
357
358 /// Realize font from a template
359 // This one is not pretty, but it's extremely fast (Asger)
360 #ifdef NEW_BITS
361 LyXFont & LyXFont::realize(LyXFont const & tmplt)
362 {
363         if (bits == inherit) {
364                 bits = tmplt.bits;
365                 return *this;
366         }
367         
368         if (bits.family == INHERIT_FAMILY) {
369                 bits.family = tmplt.bits.family;
370         }
371         if (bits.series == INHERIT_SERIES) {
372                 bits.series = tmplt.bits.series;
373         }
374         if (bits.shape == INHERIT_SHAPE) {
375                 bits.shape = tmplt.bits.shape;
376         }
377         if (bits.size == INHERIT_SIZE) {
378                 bits.size = tmplt.bits.size;
379         }
380         if (bits.emph == INHERIT) {
381                 bits.emph = tmplt.bits.emph;
382         }
383         if (bits.underbar == INHERIT) {
384                 bits.underbar = tmplt.bits.underbar;
385         }
386         if (bits.noun == INHERIT) {
387                 bits.noun = tmplt.bits.noun;
388         }
389         if (bits.latex == INHERIT) {
390                 bits.latex = tmplt.bits.latex;
391         }
392 #ifdef USE_PAINTER
393         if (bits.color == LColor::inherit) {
394                 bits.color = tmplt.bits.color;
395         }
396 #else
397         if (bits.color == INHERIT_COLOR) {
398                 bits.color = tmplt.bits.color;
399         }
400 #endif
401         if (bits.direction == INHERIT_DIR) {
402                 bits.direction = tmplt.bits.direction;
403         }
404
405         return *this;
406 }
407 #else
408 LyXFont & LyXFont::realize(LyXFont const & tmplt)
409 {
410         if (bits == inherit) {
411                 bits = tmplt.bits;
412                 return *this;
413         }
414
415         if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
416                 {
417                         bits &= ~(Fam_Mask << Fam_Pos);
418                         bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
419                 }
420         if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
421                 {
422                         bits &= ~(Ser_Mask << Ser_Pos);
423                         bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
424                 }
425         if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
426                 {
427                         bits &= ~(Sha_Mask << Sha_Pos);
428                         bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
429                 }
430         if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
431                 {
432                         bits &= ~(Siz_Mask << Siz_Pos);
433                         bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
434                 }
435         if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
436                 {
437                         bits &= ~(Misc_Mask << Emp_Pos);
438                         bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
439                 }
440         if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
441                 {
442                         bits &= ~(Misc_Mask << Und_Pos);
443                         bits |= (tmplt.bits & Misc_Mask << Und_Pos);
444                 }
445         if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
446                 {
447                         bits &= ~(Misc_Mask << Nou_Pos);
448                         bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
449                 }
450         if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
451                 {
452                         bits &= ~(Misc_Mask << Lat_Pos);
453                         bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
454                 }
455 #ifdef USE_PAINTER
456         if ((bits & (Col_Mask << Col_Pos)) == ui32(LColor::inherit) << Col_Pos)
457                 {
458                         bits &= ~(Col_Mask << Col_Pos);
459                         bits |= (tmplt.bits & Col_Mask << Col_Pos);
460                 }
461 #else
462         if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
463                 {
464                         bits &= ~(Col_Mask << Col_Pos);
465                         bits |= (tmplt.bits & Col_Mask << Col_Pos);
466                 }
467 #endif
468         if ((bits & (Dir_Mask << Dir_Pos)) == ui32(INHERIT_DIR) << Dir_Pos)
469                 {
470                         bits &= ~(Dir_Mask << Dir_Pos);
471                         bits |= (tmplt.bits & Dir_Mask << Dir_Pos);
472                 }
473
474         return *this;
475 }
476 #endif
477
478
479 /// Is font resolved?
480 #ifdef USE_PAINTER
481 bool LyXFont::resolved() const
482 {
483         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
484                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
485                 emph() != INHERIT && underbar() != INHERIT && 
486                 noun() != INHERIT && latex() != INHERIT && 
487                 color() != LColor::inherit &&
488                 direction() != INHERIT_DIR);
489 }
490 #else
491 bool LyXFont::resolved() const
492 {
493         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
494                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
495                 emph() != INHERIT && underbar() != INHERIT && 
496                 noun() != INHERIT && latex() != INHERIT && 
497                 color() != INHERIT_COLOR &&
498                 direction() != INHERIT_DIR);
499 }
500 #endif
501
502
503 /// Build GUI description of font state
504 string LyXFont::stateText() const
505 {
506         string buf;
507         if (family() != INHERIT_FAMILY)
508                 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
509         if (series() != INHERIT_SERIES)
510                 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
511         if (shape() != INHERIT_SHAPE)
512                 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
513         if (size() != INHERIT_SIZE)
514                 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
515 #ifdef USE_PAINTER
516         if (color() != LColor::inherit)
517                 buf += lcolor.getGUIName(color()) + ", ";
518 #else
519         if (color() != INHERIT_COLOR)
520                 buf += string(_(GUIColorNames[color()].c_str())) + ", ";
521 #endif
522         if (emph() != INHERIT)
523                 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
524         if (underbar() != INHERIT)
525                 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
526         if (noun() != INHERIT)
527                 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
528         if (latex() != INHERIT)
529                 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
530         if (direction() != INHERIT_DIR)
531                 buf += string(_("Direction ")) + _(GUIDirectionNames[direction()].c_str()) + ", ";
532         if (buf.empty())
533                 buf = _("Default");
534         buf = strip(buf, ' ');
535         buf = strip(buf, ',');
536         return buf;
537 }
538
539
540 // Set family according to lyx format string
541 LyXFont & LyXFont::setLyXFamily(string const & fam)
542 {
543         string s = lowercase(fam);
544
545         int i = 0;
546         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
547         if (s == LyXFamilyNames[i]) {
548                 setFamily(LyXFont::FONT_FAMILY(i));
549         } else
550                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
551                        << s << '\'' << endl;
552         return *this;
553 }
554
555
556 // Set series according to lyx format string
557 LyXFont & LyXFont::setLyXSeries(string const & ser)
558 {
559         string s = lowercase(ser);
560
561         int i = 0;
562         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
563         if (s == LyXSeriesNames[i]) {
564                 setSeries(LyXFont::FONT_SERIES(i));
565         } else
566                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
567                        << s << '\'' << endl;
568         return *this;
569 }
570
571
572 // Set shape according to lyx format string
573 LyXFont & LyXFont::setLyXShape(string const & sha)
574 {
575         string s = lowercase(sha);
576
577         int i = 0;
578         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
579         if (s == LyXShapeNames[i]) {
580                 setShape(LyXFont::FONT_SHAPE(i));
581         } else
582                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
583                        << s << '\'' << endl;
584         return *this;
585 }
586
587
588 // Set size according to lyx format string
589 LyXFont & LyXFont::setLyXSize(string const & siz)
590 {
591         string s = lowercase(siz);
592         int i = 0;
593         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
594         if (s == LyXSizeNames[i]) {
595                 setSize(LyXFont::FONT_SIZE(i));
596         } else
597                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
598                        << s << '\'' << endl;
599         return *this;
600 }
601
602 // Set size according to lyx format string
603 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
604 {
605         string s = lowercase(siz);
606         int i= 0;
607         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
608         if (s == LyXMiscNames[i])
609                 return FONT_MISC_STATE(i);
610         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
611                << s << '\'' << endl;
612         return OFF;
613 }
614
615 /// Sets color after LyX text format
616 LyXFont & LyXFont::setLyXColor(string const & col)
617 {
618 #ifdef USE_PAINTER
619         setColor(lcolor.getFromLyXName(col));
620 #else
621         string s = lowercase(col);
622         int i= 0;
623         while (s != LyXColorNames[i] && LyXColorNames[i] != "error") ++i;
624         if (s == LyXColorNames[i]) {
625                 setColor(LyXFont::FONT_COLOR(i));
626         } else
627                 lyxerr << "LyXFont::setLyXColor: Unknown Color `"
628                        << s << '\'' << endl;
629 #endif
630         return *this;
631 }
632
633
634 /// Sets size after GUI name
635 LyXFont & LyXFont::setGUISize(string const & siz)
636 {
637 #ifdef USE_PAINTER
638         /// ??????
639         // this is how it was how it was done in the lyx repository...
640         // but this does not make sense.
641         setColor(lcolor.getFromGUIName(siz));
642 #else
643         string s = lowercase(siz);
644         int i = 0;
645         while (!lGUISizeNames[i].empty() &&
646                s != _(lGUISizeNames[i].c_str()))
647                 ++i;
648         if (s == _(lGUISizeNames[i].c_str())) {
649                 setSize(LyXFont::FONT_SIZE(i));
650         } else
651                 lyxerr << "LyXFont::setGUISize: Unknown Size `"
652                        << s << '\'' << endl;
653 #endif
654         return *this;
655 }
656
657
658 // Returns size in latex format
659 string LyXFont::latexSize() const
660 {
661         return LaTeXSizeNames[size()];
662 }
663
664
665 // Read a font definition from given file in lyx format
666 // Used for layouts
667 LyXFont & LyXFont::lyxRead(LyXLex & lex)
668 {
669         bool error = false;
670         bool finished = false;
671         while (!finished && lex.IsOK() && !error) {
672                 lex.next();
673                 string tok = lowercase(lex.GetString());
674
675                 if (tok.empty()) {
676                         continue;
677                 } else if (tok == "endfont") {
678                         finished = true;
679                 } else if (tok == "family") {
680                         lex.next();
681                         string tok = lex.GetString();
682                         setLyXFamily(tok);
683                 } else if (tok == "series") {
684                         lex.next();
685                         string tok = lex.GetString();
686                         setLyXSeries(tok);
687                 } else if (tok == "shape") {
688                         lex.next();
689                         string tok = lex.GetString();
690                         setLyXShape(tok);
691                 } else if (tok == "size") {
692                         lex.next();
693                         string tok = lex.GetString();
694                         setLyXSize(tok);
695                 } else if (tok == "latex") {
696                         lex.next();
697                         string tok = lowercase(lex.GetString());
698
699                         if (tok == "no_latex") {
700                                 setLatex(OFF);
701                         } else if (tok == "latex") {
702                                 setLatex(ON);
703                         } else {
704                                 lex.printError("Illegal LaTeX type`$$Token'");
705                         }
706                 } else if (tok == "misc") {
707                         lex.next();
708                         string tok = lowercase(lex.GetString());
709
710                         if (tok == "no_bar") {
711                                 setUnderbar(OFF);
712                         } else if (tok == "no_emph") {
713                                 setEmph(OFF);
714                         } else if (tok == "no_noun") {
715                                 setNoun(OFF);
716                         } else if (tok == "emph") {
717                                 setEmph(ON);
718                         } else if (tok == "underbar") {
719                                 setUnderbar(ON);
720                         } else if (tok == "noun") {
721                                 setNoun(ON);
722                         } else {
723                                 lex.printError("Illegal misc type `$$Token´");
724                         }
725                 } else if (tok == "color") {
726                         lex.next();
727                         string tok = lex.GetString();
728                         setLyXColor(tok);
729                 } else if (tok == "direction") {
730                         lex.next();
731                         string tok = lowercase(lex.GetString());
732
733                         if (tok == "ltr") {
734                                 setDirection(LTR_DIR);
735                         } else if (tok == "rtl") {
736                                 setDirection(RTL_DIR);
737                         } else {
738                                 lex.printError("Illegal type`$$Token'");
739                         }
740                 } else {
741                         lex.printError("Unknown tag `$$Token'");
742                         error = true;
743                 }
744         }
745         return *this;
746 }
747
748
749 /// Writes the changes from this font to orgfont in .lyx format in file
750 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
751 {
752         os << "\n";
753         if (orgfont.family() != family()) {
754                 os << "\\family " << LyXFamilyNames[family()] << " \n";
755         }
756         if (orgfont.series() != series()) {
757                 os << "\\series " << LyXSeriesNames[series()] << " \n";
758         }
759         if (orgfont.shape() != shape()) {
760                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
761         }
762         if (orgfont.size() != size()) {
763                 os << "\\size " << LyXSizeNames[size()] << " \n";
764         }
765         if (orgfont.emph() != emph()) {
766                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
767         }
768         if (orgfont.underbar() != underbar()) {
769                 // This is only for backwards compatibility
770                 switch (underbar()) {
771                 case OFF:       os << "\\bar no \n"; break;
772                 case ON:        os << "\\bar under \n"; break;
773                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
774                                         "TOGGLE should not appear here!"
775                                        << endl;
776                 break;
777                 case INHERIT:   os << "\\bar default \n"; break;
778                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
779                                         "IGNORE should not appear here!"
780                                        << endl;
781                 break;
782                 }
783         }
784         if (orgfont.noun() != noun()) {
785                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
786         }
787         if (orgfont.latex() != latex()) {
788                 // This is only for backwards compatibility
789                 switch (latex()) {
790                 case OFF:       os << "\\latex no_latex \n"; break;
791                 case ON:        os << "\\latex latex \n"; break;
792                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
793                                         "TOGGLE should not appear here!"
794                                        << endl;
795                 break;
796                 case INHERIT:   os << "\\latex default \n"; break;
797                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
798                                         "IGNORE should not appear here!"
799                                        << endl;
800                 break;
801                 }
802         }
803 #ifdef USE_PAINTER
804         if (orgfont.color() != color()) {
805                 os << "\\color " << lcolor.getLyXName(color()) << "\n";
806         }
807 #else
808         if (orgfont.color() != color()) {
809                 os << "\\color " << LyXColorNames[color()] << "\n";
810         }
811 #endif
812         if (orgfont.direction() != direction()) {
813                 switch (direction()) {
814                 case RTL_DIR:   os << "\\direction rtl \n"; break;
815                 case LTR_DIR:   os << "\\direction ltr\n"; break;
816                 case TOGGLE_DIR:   lyxerr << "LyXFont::lyxWriteFontChanges: "
817                                         "TOGGLE should not appear here!"
818                                        << endl;
819                 case INHERIT_DIR:   os << "\\direction default \n"; break;
820                 case IGNORE_DIR:    lyxerr << "LyXFont::lyxWriteFontChanges: "
821                                         "IGNORE should not appear here!"
822                                        << endl;
823                 break;
824                 }
825         }
826 }
827
828
829 /// Writes the head of the LaTeX needed to impose this font
830 // Returns number of chars written.
831 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base,
832                                     LyXFont const & prev) const
833 {
834         LyXFont f = *this;
835         f.reduce(base);
836         
837         if (f.bits == inherit)
838                 return 0;
839         
840         int count = 0;
841         bool env = false;
842
843         FONT_DIRECTION direction = f.direction();
844         if (direction != prev.direction()) {
845                 if (direction == LTR_DIR) {
846                         file += "\\L{";
847                         count += 3;
848                         env = true; //We have opened a new environment
849                 }
850                 if (direction == RTL_DIR) {
851                         file += "\\R{";
852                         count += 3;
853                         env = true; //We have opened a new environment
854                 }
855         }
856
857         if (f.family() != INHERIT_FAMILY) {
858                 file += '\\';
859                 file += LaTeXFamilyNames[f.family()];
860                 file += '{';
861                 count += LaTeXFamilyNames[f.family()].length() + 2;
862                 env = true; //We have opened a new environment
863         }
864         if (f.series() != INHERIT_SERIES) {
865                 file += '\\';
866                 file += LaTeXSeriesNames[f.series()];
867                 file += '{';
868                 count += LaTeXSeriesNames[f.series()].length() + 2;
869                 env = true; //We have opened a new environment
870         }
871         if (f.shape() != INHERIT_SHAPE) {
872                 file += '\\';
873                 file += LaTeXShapeNames[f.shape()];
874                 file += '{';
875                 count += LaTeXShapeNames[f.shape()].length() + 2;
876                 env = true; //We have opened a new environment
877         }
878 #ifdef USE_PAINTER
879         if (f.color() != LColor::inherit) {
880                 file += "\\textcolor{";
881                 file += lcolor.getLaTeXName(f.color());
882                 file += "}{";
883                 count += lcolor.getLaTeXName(f.color()).length() + 13;
884                 env = true; //We have opened a new environment
885         }
886 #else
887         if (f.color() != INHERIT_COLOR) {
888                 file += "\\textcolor{";
889                 file += LaTeXColorNames[f.color()];
890                 file += "}{";
891                 count += LaTeXColorNames[f.color()].length() + 13;
892                 env = true; //We have opened a new environment
893         }
894 #endif
895         if (f.emph() == ON) {
896                 file += "\\emph{";
897                 count += 6;
898                 env = true; //We have opened a new environment
899         }
900         if (f.underbar() == ON) {
901                 file += "\\underbar{";
902                 count += 10;
903                 env = true; //We have opened a new environment
904         }
905         // \noun{} is a LyX special macro
906         if (f.noun() == ON) {
907                 file += "\\noun{";
908                 count += 8;
909                 env = true; //We have opened a new environment
910         }
911         if (f.size() != INHERIT_SIZE) {
912                 // If we didn't open an environment above, we open one here
913                 if (!env) {
914                         file += '{';
915                         ++count;
916                 }
917                 file += '\\';
918                 file += LaTeXSizeNames[f.size()];
919                 file += ' ';
920                 count += LaTeXSizeNames[f.size()].length() + 2;
921         }
922         return count;
923 }
924
925
926 /// Writes ending block of LaTeX needed to close use of this font
927 // Returns number of chars written
928 // This one corresponds to latexWriteStartChanges(). (Asger)
929 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base,
930                                   LyXFont const & next) const
931 {
932         LyXFont f = *this; // why do you need this?
933         f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
934         // Because this function is const. Everything breaks if this
935         // method changes the font it represents. There is no speed penalty
936         // by using the temporary. (Asger)
937
938         if (f.bits == inherit)
939                 return 0;
940         
941         int count = 0;
942         bool env = false;
943
944         FONT_DIRECTION direction = f.direction();
945         if ( direction != next.direction()
946             && (direction == RTL_DIR || direction == LTR_DIR) ) {
947                 file += '}';
948                 ++count;
949                 env = true; // Size change need not bother about closing env.
950         }
951
952         if (f.family() != INHERIT_FAMILY) {
953                 file += '}';
954                 ++count;
955                 env = true; // Size change need not bother about closing env.
956         }
957         if (f.series() != INHERIT_SERIES) {
958                 file += '}';
959                 ++count;
960                 env = true; // Size change need not bother about closing env.
961         }
962         if (f.shape() != INHERIT_SHAPE) {
963                 file += '}';
964                 ++count;
965                 env = true; // Size change need not bother about closing env.
966         }
967 #ifdef USE_PAINTER
968         if (f.color() != LColor::inherit) {
969                 file += '}';
970                 ++count;
971                 env = true; // Size change need not bother about closing env.
972         }
973 #else
974         if (f.color() != INHERIT_COLOR) {
975                 file += '}';
976                 ++count;
977                 env = true; // Size change need not bother about closing env.
978         }
979 #endif
980         if (f.emph() == ON) {
981                 file += '}';
982                 ++count;
983                 env = true; // Size change need not bother about closing env.
984         }
985         if (f.underbar() == ON) {
986                 file += '}';
987                 ++count;
988                 env = true; // Size change need not bother about closing env.
989         }
990         if (f.noun() == ON) {
991                 file += '}';
992                 ++count;
993                 env = true; // Size change need not bother about closing env.
994         }
995         if (f.size() != INHERIT_SIZE) {
996                 // We only have to close if only size changed
997                 if (!env) {
998                         file += '}';
999                         ++count;
1000                 }
1001         }
1002         
1003         return count;
1004 }
1005
1006
1007 #ifdef USE_PAINTER
1008 LColor::color LyXFont::realColor() const
1009 {
1010         if (latex() == ON)
1011                 return LColor::latex;
1012         if (color() == LColor::none)
1013                 return LColor::foreground;
1014         return color();
1015 }
1016 #endif
1017
1018
1019 // Convert logical attributes to concrete shape attribute
1020 LyXFont::FONT_SHAPE LyXFont::realShape() const
1021 {
1022         register FONT_SHAPE s = shape();
1023
1024         if (emph() == ON) {
1025                 if (s == UP_SHAPE)
1026                         s = ITALIC_SHAPE;
1027                 else
1028                         s = UP_SHAPE;
1029         }
1030         if (noun() == ON)
1031                 s = SMALLCAPS_SHAPE;
1032         return s;
1033 }
1034
1035
1036 #ifndef USE_PAINTER
1037 GC LyXFont::getGC() const
1038 {
1039         GC gc;
1040         if (latex() == ON)
1041                 gc = ::getGC(gc_latex);
1042         else {
1043                 if (color() == NONE)
1044                         gc = ::getGC(gc_copy);
1045                 else if (color() == MATH)
1046                         gc = ::getGC(gc_math);
1047                 else if (color() == INSET)
1048                         gc = ::getGC(gc_foot);
1049                 else
1050                         gc = ::GetColorGC(color());
1051         }
1052
1053         XSetFont(fl_display, gc, getXFontstruct()->fid);
1054         return gc;
1055 }
1056 #endif
1057
1058
1059 XFontStruct * LyXFont::getXFontstruct() const
1060 {
1061         return fontloader.load(family(), series(), realShape(), size());
1062 }
1063
1064
1065 int LyXFont::maxAscent() const
1066 {
1067         return getXFontstruct()->ascent;
1068 }
1069
1070
1071 int LyXFont::maxDescent() const
1072 {
1073         return getXFontstruct()->descent;
1074 }
1075
1076
1077 int LyXFont::ascent(char c) const
1078 {
1079         XFontStruct * finfo = getXFontstruct();
1080         unsigned int uc = static_cast<unsigned char>(c);
1081         if (finfo->per_char
1082             && uc >= finfo->min_char_or_byte2
1083             && uc <= finfo->max_char_or_byte2) 
1084                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
1085         else
1086                 return finfo->ascent;
1087 }
1088
1089
1090 int LyXFont::descent(char c) const
1091 {
1092         XFontStruct * finfo = getXFontstruct();
1093         unsigned int uc = static_cast<unsigned char>(c);
1094         if (finfo->per_char
1095             && uc >= finfo->min_char_or_byte2
1096             && uc <= finfo->max_char_or_byte2) 
1097                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
1098         else
1099                 return finfo->descent;
1100 }
1101
1102
1103 // Specialized after profiling. (Asger)
1104 int LyXFont::width(char c) const
1105 {
1106         if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1107                 return XTextWidth(getXFontstruct(), &c, 1);
1108         } else {
1109                 return textWidth(&c, 1);
1110         }
1111 }
1112
1113
1114 int LyXFont::lbearing(char c) const
1115 {
1116         XFontStruct * finfo = getXFontstruct();
1117         unsigned int uc = static_cast<unsigned char>(c);
1118         if (finfo->per_char
1119             && uc >= finfo->min_char_or_byte2
1120             && uc <= finfo->max_char_or_byte2) 
1121                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
1122         else
1123                 return 0;
1124 }
1125
1126
1127 int LyXFont::rbearing(char c) const
1128 {
1129         XFontStruct * finfo = getXFontstruct();
1130         unsigned int uc = static_cast<unsigned char>(c);
1131         if (finfo->per_char
1132             && uc >= finfo->min_char_or_byte2
1133             && uc <= finfo->max_char_or_byte2) 
1134                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
1135         else
1136                 return width(c);
1137 }
1138
1139
1140 int LyXFont::textWidth(char const * s, int n) const
1141 {
1142         if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1143                 return XTextWidth(getXFontstruct(), s, n);
1144         } else {
1145                 // emulate smallcaps since X doesn't support this
1146                 unsigned int result = 0;
1147                 char c;
1148                 LyXFont smallfont = *this;
1149                 smallfont.decSize();
1150                 smallfont.decSize();
1151                 smallfont.setShape(LyXFont::UP_SHAPE);
1152                 for (int i = 0; i < n; ++i) {
1153                         c = s[i];
1154                         // when islower is a macro, the cast is needed (JMarc)
1155                         if (islower(static_cast<unsigned char>(c))){
1156                                 c = toupper(c);
1157                                 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
1158                         } else {
1159                                 result += XTextWidth(getXFontstruct(), &c, 1);
1160                         }
1161                 }
1162                 return result;
1163         }
1164 }
1165
1166
1167 int LyXFont::stringWidth(string const & s) const
1168 {
1169         if (s.empty()) return 0;
1170         return textWidth(s.c_str(), s.length());
1171 }
1172
1173
1174 int LyXFont::signedStringWidth(string const & s) const
1175 {
1176         if (s.empty()) return 0;
1177         if (s.c_str()[0] == '-')
1178                 return -textWidth(s.c_str()+1, s.length()-1);
1179         else
1180                 return textWidth(s.c_str(), s.length());
1181 }
1182
1183
1184 #ifdef USE_PAINTER
1185 int LyXFont::drawText(char const * s, int n, Pixmap, 
1186                       int, int x) const
1187 {
1188         if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1189                 /* XDrawString(fl_display,
1190                    pm,
1191                    getGC(),
1192                    x, baseline,
1193                    s, n);
1194                    XFlush(fl_display); */
1195                 return XTextWidth(getXFontstruct(), s, n);
1196
1197         } else {
1198                 // emulate smallcaps since X doesn't support this
1199                 char c;
1200                 int sx = x;
1201                 LyXFont smallfont = *this;
1202                 smallfont.decSize();
1203                 smallfont.decSize();
1204                 smallfont.setShape(LyXFont::UP_SHAPE);
1205                 for (int i = 0; i < n; ++i) {
1206                         c = s[i];
1207                         if (islower(static_cast<unsigned char>(c))){
1208                                 c = toupper(c);
1209                                 /* XDrawString(fl_display,
1210                                    pm,
1211                                    smallfont.getGC(),
1212                                    x, baseline,
1213                                    &c, 1); */
1214                                 x += XTextWidth(smallfont.getXFontstruct(),
1215                                                 &c, 1);
1216                                 //XFlush(fl_display);
1217                         } else {
1218                                 /* XDrawString(fl_display,
1219                                    pm,
1220                                    getGC(),
1221                                    x, baseline,
1222                                    &c, 1);*/
1223                                 x += XTextWidth(getXFontstruct(), &c, 1);
1224                                 //XFlush(fl_display);
1225                         }
1226                 }
1227                 return x - sx;
1228         }
1229 }
1230 #else
1231 int LyXFont::drawText(char const * s, int n, Pixmap pm, 
1232                       int baseline, int x) const
1233 {
1234         if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1235                 XDrawString(fl_display,
1236                             pm,
1237                             getGC(),
1238                             x, baseline,
1239                             s, n);
1240                 XFlush(fl_display);
1241                 return XTextWidth(getXFontstruct(), s, n);
1242
1243         } else {
1244                 // emulate smallcaps since X doesn't support this
1245                 char c;
1246                 int sx = x;
1247                 LyXFont smallfont = *this;
1248                 smallfont.decSize();
1249                 smallfont.decSize();
1250                 smallfont.setShape(LyXFont::UP_SHAPE);
1251                 for (int i = 0; i < n; ++i) {
1252                         c = s[i];
1253                         if (islower(static_cast<unsigned char>(c))){
1254                                 c = toupper(c);
1255                                 XDrawString(fl_display,
1256                                             pm,
1257                                             smallfont.getGC(),
1258                                             x, baseline,
1259                                             &c, 1);
1260                                 x += XTextWidth(smallfont.getXFontstruct(),
1261                                                 &c, 1);
1262                                 XFlush(fl_display);
1263                         } else {
1264                                 XDrawString(fl_display,
1265                                             pm,
1266                                             getGC(),
1267                                             x, baseline,
1268                                             &c, 1);
1269                                 x += XTextWidth(getXFontstruct(), &c, 1);
1270                                 XFlush(fl_display);
1271                         }
1272                 }
1273                 return x - sx;
1274         }
1275 }
1276 #endif
1277
1278
1279 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
1280 {
1281         return drawText(s.c_str(), s.length(), pm, baseline, x);
1282 }
1283
1284
1285 bool LyXFont::equalExceptLatex(LyXFont const & f) const 
1286 {
1287         LyXFont f1 = *this;
1288         f1.setLatex(f.latex());
1289         return f1 == f;
1290 }
1291
1292
1293 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1294 {
1295         return o << int(fms);
1296 }