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