1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
15 #pragma implementation "lyxfont.h"
28 #include "FontLoader.h"
29 #include "support/lstrings.h"
33 // The global fontloader
34 FontLoader fontloader;
40 string const GUIFamilyNames[6] =
41 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
44 string const GUISeriesNames[4] =
45 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
47 string const GUIShapeNames[6] =
48 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
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") };
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() };
61 string const GUIMiscNames[5] =
62 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64 string const GUIDirectionNames[5] =
65 { N_("LTR"), N_("RTL"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
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") };
76 // Strings used to read and write .lyx format files
78 string const LyXFamilyNames[6] =
79 { "roman", "sans", "typewriter", "symbol", "default", "error" };
81 string const LyXSeriesNames[4] =
82 { "medium", "bold", "default", "error" };
84 string const LyXShapeNames[6] =
85 { "up", "italic", "slanted", "smallcaps", "default", "error" };
87 string const LyXSizeNames[14] =
88 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
89 "larger", "largest", "huge", "giant",
90 "increase-error", "decrease-error", "default", "error" };
92 string const LyXMiscNames[12] =
93 { "off", "on", "toggle", "default", "error" };
96 string const LyXColorNames[13] =
97 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
98 "yellow", "matherror", "inseterror", "default", "error" };
102 // Strings used to write LaTeX files
105 string const LaTeXFamilyNames[6] =
106 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
108 string const LaTeXSeriesNames[4] =
109 { "textmd", "textbf", "error4", "error5" };
111 string const LaTeXShapeNames[6] =
112 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
114 string const LaTeXSizeNames[14] =
115 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
116 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
119 string const LaTeXColorNames[13] =
120 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
121 "yellow", "error12", "error13", "error14", "error15" };
125 LyXFont::FontBits LyXFont::sane = { ROMAN_FAMILY,
140 LyXFont::FontBits LyXFont::inherit = { INHERIT_FAMILY,
155 LyXFont::FontBits LyXFont::ignore = { IGNORE_FAMILY,
171 /// Decreases font size by one
172 LyXFont & LyXFont::decSize()
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;
186 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
189 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
192 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
195 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
202 /// Increases font size by one
203 LyXFont & LyXFont::incSize()
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;
217 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
220 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
223 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
226 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
233 /// Updates a misc setting according to request
234 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
237 if (newfont == TOGGLE) {
243 lyxerr <<"LyXFont::setMisc: Need state"
244 " ON or OFF to toggle. Setting to ON" << endl;
247 } else if (newfont == IGNORE)
254 /// Updates font settings according to request
255 void LyXFont::update(LyXFont const & newfont, bool toggleall)
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
263 // "Old" behaviour: "Setting" bold will toggle bold on/off.
264 switch (newfont.series()) {
267 if (series() == BOLD_SERIES && toggleall)
268 setSeries(MEDIUM_SERIES);
270 setSeries(BOLD_SERIES);
274 setSeries(newfont.series());
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
286 if (newfont.size() != IGNORE_SIZE) {
287 if (newfont.size() == INCREASE_SIZE)
289 else if (newfont.size() == DECREASE_SIZE)
291 else if (newfont.size() == size() && toggleall)
292 setSize(INHERIT_SIZE); // toggle 'back'
294 setSize(newfont.size());
297 setEmph(setMisc(newfont.emph(), emph()));
298 setUnderbar(setMisc(newfont.underbar(), underbar()));
299 setNoun(setMisc(newfont.noun(), noun()));
300 setLatex(setMisc(newfont.latex(), latex()));
302 switch(newfont.direction()) {
304 if (direction() == LTR_DIR)
305 setDirection(RTL_DIR);
307 setDirection(LTR_DIR);
312 setDirection(newfont.direction());
316 if(newfont.color() == color() && toggleall)
317 setColor(LColor::inherit); // toggle 'back'
318 else if (newfont.color() != LColor::ignore)
319 setColor(newfont.color());
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());
329 /// Reduce font to fall back to template where possible
330 void LyXFont::reduce(LyXFont const & tmplt)
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())
342 if (underbar() == tmplt.underbar())
343 setUnderbar(INHERIT);
344 if (noun() == tmplt.noun())
346 if (latex() == tmplt.latex())
349 if (color() == tmplt.color())
350 setColor(LColor::inherit);
352 if (color() == tmplt.color())
353 setColor(LyXFont::INHERIT_COLOR);
355 if (direction() == tmplt.direction())
356 setDirection(INHERIT_DIR);
360 /// Realize font from a template
361 // This one is not pretty, but it's extremely fast (Asger)
363 LyXFont & LyXFont::realize(LyXFont const & tmplt)
365 if (bits == inherit) {
370 if (bits.family == INHERIT_FAMILY) {
371 bits.family = tmplt.bits.family;
373 if (bits.series == INHERIT_SERIES) {
374 bits.series = tmplt.bits.series;
376 if (bits.shape == INHERIT_SHAPE) {
377 bits.shape = tmplt.bits.shape;
379 if (bits.size == INHERIT_SIZE) {
380 bits.size = tmplt.bits.size;
382 if (bits.emph == INHERIT) {
383 bits.emph = tmplt.bits.emph;
385 if (bits.underbar == INHERIT) {
386 bits.underbar = tmplt.bits.underbar;
388 if (bits.noun == INHERIT) {
389 bits.noun = tmplt.bits.noun;
391 if (bits.latex == INHERIT) {
392 bits.latex = tmplt.bits.latex;
395 if (bits.color == LColor::inherit) {
396 bits.color = tmplt.bits.color;
399 if (bits.color == INHERIT_COLOR) {
400 bits.color = tmplt.bits.color;
403 if (bits.direction == INHERIT_DIR) {
404 bits.direction = tmplt.bits.direction;
410 LyXFont & LyXFont::realize(LyXFont const & tmplt)
412 if (bits == inherit) {
417 if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
419 bits &= ~(Fam_Mask << Fam_Pos);
420 bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
422 if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
424 bits &= ~(Ser_Mask << Ser_Pos);
425 bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
427 if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
429 bits &= ~(Sha_Mask << Sha_Pos);
430 bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
432 if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
434 bits &= ~(Siz_Mask << Siz_Pos);
435 bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
437 if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
439 bits &= ~(Misc_Mask << Emp_Pos);
440 bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
442 if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
444 bits &= ~(Misc_Mask << Und_Pos);
445 bits |= (tmplt.bits & Misc_Mask << Und_Pos);
447 if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
449 bits &= ~(Misc_Mask << Nou_Pos);
450 bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
452 if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
454 bits &= ~(Misc_Mask << Lat_Pos);
455 bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
458 if ((bits & (Col_Mask << Col_Pos)) == ui32(LColor::inherit) << Col_Pos)
460 bits &= ~(Col_Mask << Col_Pos);
461 bits |= (tmplt.bits & Col_Mask << Col_Pos);
464 if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
466 bits &= ~(Col_Mask << Col_Pos);
467 bits |= (tmplt.bits & Col_Mask << Col_Pos);
470 if ((bits & (Dir_Mask << Dir_Pos)) == ui32(INHERIT_DIR) << Dir_Pos)
472 bits &= ~(Dir_Mask << Dir_Pos);
473 bits |= (tmplt.bits & Dir_Mask << Dir_Pos);
481 /// Is font resolved?
483 bool LyXFont::resolved() const
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);
493 bool LyXFont::resolved() const
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);
505 /// Build GUI description of font state
506 string LyXFont::stateText() const
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())) + ", ";
518 if (color() != LColor::inherit)
519 buf += lcolor.getGUIName(color()) + ", ";
521 if (color() != INHERIT_COLOR)
522 buf += string(_(GUIColorNames[color()].c_str())) + ", ";
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()) + ", ";
536 buf = strip(buf, ' ');
537 buf = strip(buf, ',');
542 // Set family according to lyx format string
543 LyXFont & LyXFont::setLyXFamily(string const & fam)
545 string s = lowercase(fam);
548 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
549 if (s == LyXFamilyNames[i]) {
550 setFamily(LyXFont::FONT_FAMILY(i));
552 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
553 << s << '\'' << endl;
558 // Set series according to lyx format string
559 LyXFont & LyXFont::setLyXSeries(string const & ser)
561 string s = lowercase(ser);
564 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
565 if (s == LyXSeriesNames[i]) {
566 setSeries(LyXFont::FONT_SERIES(i));
568 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
569 << s << '\'' << endl;
574 // Set shape according to lyx format string
575 LyXFont & LyXFont::setLyXShape(string const & sha)
577 string s = lowercase(sha);
580 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
581 if (s == LyXShapeNames[i]) {
582 setShape(LyXFont::FONT_SHAPE(i));
584 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
585 << s << '\'' << endl;
590 // Set size according to lyx format string
591 LyXFont & LyXFont::setLyXSize(string const & siz)
593 string s = lowercase(siz);
595 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
596 if (s == LyXSizeNames[i]) {
597 setSize(LyXFont::FONT_SIZE(i));
599 lyxerr << "LyXFont::setLyXSize: Unknown size `"
600 << s << '\'' << endl;
604 // Set size according to lyx format string
605 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
607 string s = lowercase(siz);
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;
617 /// Sets color after LyX text format
618 LyXFont & LyXFont::setLyXColor(string const & col)
621 setColor(lcolor.getFromLyXName(col));
623 string s = lowercase(col);
625 while (s != LyXColorNames[i] && LyXColorNames[i] != "error") ++i;
626 if (s == LyXColorNames[i]) {
627 setColor(LyXFont::FONT_COLOR(i));
629 lyxerr << "LyXFont::setLyXColor: Unknown Color `"
630 << s << '\'' << endl;
636 /// Sets size after GUI name
637 LyXFont & LyXFont::setGUISize(string const & siz)
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));
645 string s = lowercase(siz);
647 while (!lGUISizeNames[i].empty() &&
648 s != _(lGUISizeNames[i].c_str()))
650 if (s == _(lGUISizeNames[i].c_str())) {
651 setSize(LyXFont::FONT_SIZE(i));
653 lyxerr << "LyXFont::setGUISize: Unknown Size `"
654 << s << '\'' << endl;
660 // Returns size in latex format
661 string LyXFont::latexSize() const
663 return LaTeXSizeNames[size()];
667 // Read a font definition from given file in lyx format
669 LyXFont & LyXFont::lyxRead(LyXLex & lex)
672 bool finished = false;
673 while (!finished && lex.IsOK() && !error) {
675 string tok = lowercase(lex.GetString());
679 } else if (tok == "endfont") {
681 } else if (tok == "family") {
683 string tok = lex.GetString();
685 } else if (tok == "series") {
687 string tok = lex.GetString();
689 } else if (tok == "shape") {
691 string tok = lex.GetString();
693 } else if (tok == "size") {
695 string tok = lex.GetString();
697 } else if (tok == "latex") {
699 string tok = lowercase(lex.GetString());
701 if (tok == "no_latex") {
703 } else if (tok == "latex") {
706 lex.printError("Illegal LaTeX type`$$Token'");
708 } else if (tok == "misc") {
710 string tok = lowercase(lex.GetString());
712 if (tok == "no_bar") {
714 } else if (tok == "no_emph") {
716 } else if (tok == "no_noun") {
718 } else if (tok == "emph") {
720 } else if (tok == "underbar") {
722 } else if (tok == "noun") {
725 lex.printError("Illegal misc type `$$Token´");
727 } else if (tok == "color") {
729 string tok = lex.GetString();
731 } else if (tok == "direction") {
733 string tok = lowercase(lex.GetString());
736 setDirection(LTR_DIR);
737 } else if (tok == "rtl") {
738 setDirection(RTL_DIR);
740 lex.printError("Illegal type`$$Token'");
743 lex.printError("Unknown tag `$$Token'");
751 /// Writes the changes from this font to orgfont in .lyx format in file
752 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
755 if (orgfont.family() != family()) {
756 os << "\\family " << LyXFamilyNames[family()] << " \n";
758 if (orgfont.series() != series()) {
759 os << "\\series " << LyXSeriesNames[series()] << " \n";
761 if (orgfont.shape() != shape()) {
762 os << "\\shape " << LyXShapeNames[shape()] << " \n";
764 if (orgfont.size() != size()) {
765 os << "\\size " << LyXSizeNames[size()] << " \n";
767 if (orgfont.emph() != emph()) {
768 os << "\\emph " << LyXMiscNames[emph()] << " \n";
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!"
779 case INHERIT: os << "\\bar default \n"; break;
780 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
781 "IGNORE should not appear here!"
786 if (orgfont.noun() != noun()) {
787 os << "\\noun " << LyXMiscNames[noun()] << " \n";
789 if (orgfont.latex() != latex()) {
790 // This is only for backwards compatibility
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!"
798 case INHERIT: os << "\\latex default \n"; break;
799 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
800 "IGNORE should not appear here!"
806 if (orgfont.color() != color()) {
807 os << "\\color " << lcolor.getLyXName(color()) << "\n";
810 if (orgfont.color() != color()) {
811 os << "\\color " << LyXColorNames[color()] << "\n";
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!"
821 case INHERIT_DIR: os << "\\direction default \n"; break;
822 case IGNORE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
823 "IGNORE should not appear here!"
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
839 if (f.bits == inherit)
845 FONT_DIRECTION direction = f.direction();
846 if (direction != prev.direction()) {
847 if (direction == LTR_DIR) {
850 env = true; //We have opened a new environment
852 if (direction == RTL_DIR) {
855 env = true; //We have opened a new environment
859 if (f.family() != INHERIT_FAMILY) {
861 file += LaTeXFamilyNames[f.family()];
863 count += LaTeXFamilyNames[f.family()].length() + 2;
864 env = true; //We have opened a new environment
866 if (f.series() != INHERIT_SERIES) {
868 file += LaTeXSeriesNames[f.series()];
870 count += LaTeXSeriesNames[f.series()].length() + 2;
871 env = true; //We have opened a new environment
873 if (f.shape() != INHERIT_SHAPE) {
875 file += LaTeXShapeNames[f.shape()];
877 count += LaTeXShapeNames[f.shape()].length() + 2;
878 env = true; //We have opened a new environment
881 if (f.color() != LColor::inherit) {
882 file += "\\textcolor{";
883 file += lcolor.getLaTeXName(f.color());
885 count += lcolor.getLaTeXName(f.color()).length() + 13;
886 env = true; //We have opened a new environment
889 if (f.color() != INHERIT_COLOR) {
890 file += "\\textcolor{";
891 file += LaTeXColorNames[f.color()];
893 count += LaTeXColorNames[f.color()].length() + 13;
894 env = true; //We have opened a new environment
897 if (f.emph() == ON) {
900 env = true; //We have opened a new environment
902 if (f.underbar() == ON) {
903 file += "\\underbar{";
905 env = true; //We have opened a new environment
907 // \noun{} is a LyX special macro
908 if (f.noun() == ON) {
911 env = true; //We have opened a new environment
913 if (f.size() != INHERIT_SIZE) {
914 // If we didn't open an environment above, we open one here
920 file += LaTeXSizeNames[f.size()];
922 count += LaTeXSizeNames[f.size()].length() + 2;
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
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)
940 if (f.bits == inherit)
946 FONT_DIRECTION direction = f.direction();
947 if ( direction != next.direction()
948 && (direction == RTL_DIR || direction == LTR_DIR) ) {
951 env = true; // Size change need not bother about closing env.
954 if (f.family() != INHERIT_FAMILY) {
957 env = true; // Size change need not bother about closing env.
959 if (f.series() != INHERIT_SERIES) {
962 env = true; // Size change need not bother about closing env.
964 if (f.shape() != INHERIT_SHAPE) {
967 env = true; // Size change need not bother about closing env.
970 if (f.color() != LColor::inherit) {
973 env = true; // Size change need not bother about closing env.
976 if (f.color() != INHERIT_COLOR) {
979 env = true; // Size change need not bother about closing env.
982 if (f.emph() == ON) {
985 env = true; // Size change need not bother about closing env.
987 if (f.underbar() == ON) {
990 env = true; // Size change need not bother about closing env.
992 if (f.noun() == ON) {
995 env = true; // Size change need not bother about closing env.
997 if (f.size() != INHERIT_SIZE) {
998 // We only have to close if only size changed
1010 LColor::color LyXFont::realColor() const
1013 return LColor::latex;
1014 if (color() == LColor::none)
1015 return LColor::foreground;
1021 // Convert logical attributes to concrete shape attribute
1022 LyXFont::FONT_SHAPE LyXFont::realShape() const
1024 register FONT_SHAPE s = shape();
1033 s = SMALLCAPS_SHAPE;
1039 GC LyXFont::getGC() const
1043 gc = ::getGC(gc_latex);
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);
1052 gc = ::GetColorGC(color());
1055 XSetFont(fl_display, gc, getXFontstruct()->fid);
1061 XFontStruct * LyXFont::getXFontstruct() const
1063 return fontloader.load(family(), series(), realShape(), size());
1067 int LyXFont::maxAscent() const
1069 return getXFontstruct()->ascent;
1073 int LyXFont::maxDescent() const
1075 return getXFontstruct()->descent;
1079 int LyXFont::ascent(char c) const
1081 XFontStruct * finfo = getXFontstruct();
1082 unsigned int uc = static_cast<unsigned char>(c);
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;
1088 return finfo->ascent;
1092 int LyXFont::descent(char c) const
1094 XFontStruct * finfo = getXFontstruct();
1095 unsigned int uc = static_cast<unsigned char>(c);
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;
1101 return finfo->descent;
1105 // Specialized after profiling. (Asger)
1106 int LyXFont::width(char c) const
1108 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1109 return XTextWidth(getXFontstruct(), &c, 1);
1111 return textWidth(&c, 1);
1116 int LyXFont::lbearing(char c) const
1118 XFontStruct * finfo = getXFontstruct();
1119 unsigned int uc = static_cast<unsigned char>(c);
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;
1129 int LyXFont::rbearing(char c) const
1131 XFontStruct * finfo = getXFontstruct();
1132 unsigned int uc = static_cast<unsigned char>(c);
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;
1142 int LyXFont::textWidth(char const * s, int n) const
1144 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1145 return XTextWidth(getXFontstruct(), s, n);
1147 // emulate smallcaps since X doesn't support this
1148 unsigned int result = 0;
1150 LyXFont smallfont = *this;
1151 smallfont.decSize();
1152 smallfont.decSize();
1153 smallfont.setShape(LyXFont::UP_SHAPE);
1154 for (int i = 0; i < n; ++i) {
1156 // when islower is a macro, the cast is needed (JMarc)
1157 if (islower(static_cast<unsigned char>(c))){
1159 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
1161 result += XTextWidth(getXFontstruct(), &c, 1);
1169 int LyXFont::stringWidth(string const & s) const
1171 if (s.empty()) return 0;
1172 return textWidth(s.c_str(), s.length());
1176 int LyXFont::signedStringWidth(string const & s) const
1178 if (s.empty()) return 0;
1179 if (s.c_str()[0] == '-')
1180 return -textWidth(s.c_str()+1, s.length()-1);
1182 return textWidth(s.c_str(), s.length());
1187 int LyXFont::drawText(char const * s, int n, Pixmap,
1190 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1191 /* XDrawString(fl_display,
1196 XFlush(fl_display); */
1197 return XTextWidth(getXFontstruct(), s, n);
1200 // emulate smallcaps since X doesn't support this
1203 LyXFont smallfont = *this;
1204 smallfont.decSize();
1205 smallfont.decSize();
1206 smallfont.setShape(LyXFont::UP_SHAPE);
1207 for (int i = 0; i < n; ++i) {
1209 if (islower(static_cast<unsigned char>(c))){
1211 /* XDrawString(fl_display,
1216 x += XTextWidth(smallfont.getXFontstruct(),
1218 //XFlush(fl_display);
1220 /* XDrawString(fl_display,
1225 x += XTextWidth(getXFontstruct(), &c, 1);
1226 //XFlush(fl_display);
1233 int LyXFont::drawText(char const * s, int n, Pixmap pm,
1234 int baseline, int x) const
1236 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1237 XDrawString(fl_display,
1243 return XTextWidth(getXFontstruct(), s, n);
1246 // emulate smallcaps since X doesn't support this
1249 LyXFont smallfont = *this;
1250 smallfont.decSize();
1251 smallfont.decSize();
1252 smallfont.setShape(LyXFont::UP_SHAPE);
1253 for (int i = 0; i < n; ++i) {
1255 if (islower(static_cast<unsigned char>(c))){
1257 XDrawString(fl_display,
1262 x += XTextWidth(smallfont.getXFontstruct(),
1266 XDrawString(fl_display,
1271 x += XTextWidth(getXFontstruct(), &c, 1);
1281 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
1283 return drawText(s.c_str(), s.length(), pm, baseline, x);
1287 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1290 f1.setLatex(f.latex());
1295 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1297 return o << int(fms);