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"
25 #include "FontLoader.h"
26 #include "support/lstrings.h"
30 // The global fontloader
31 FontLoader fontloader;
37 string const GUIFamilyNames[6] =
38 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
41 string const GUISeriesNames[4] =
42 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
44 string const GUIShapeNames[6] =
45 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
48 string const GUISizeNames[14] =
49 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
50 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
51 N_("Inherit"), N_("Ignore") };
53 string const lGUISizeNames[15] =
54 { N_("tiny"), N_("smallest"), N_("smaller"), N_("small"), N_("normal"), N_("large"),
55 N_("larger"), N_("largest"), N_("huge"), N_("huger"), N_("increase"), N_("decrease"),
56 N_("inherit"), N_("ignore"), string() };
58 string const GUIMiscNames[5] =
59 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61 string const GUIDirectionNames[5] =
62 { N_("LTR"), N_("RTL"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
66 // Strings used to read and write .lyx format files
68 string const LyXFamilyNames[6] =
69 { "roman", "sans", "typewriter", "symbol", "default", "error" };
71 string const LyXSeriesNames[4] =
72 { "medium", "bold", "default", "error" };
74 string const LyXShapeNames[6] =
75 { "up", "italic", "slanted", "smallcaps", "default", "error" };
77 string const LyXSizeNames[14] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79 "larger", "largest", "huge", "giant",
80 "increase-error", "decrease-error", "default", "error" };
82 string const LyXMiscNames[12] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
89 string const LaTeXFamilyNames[6] =
90 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
92 string const LaTeXSeriesNames[4] =
93 { "textmd", "textbf", "error4", "error5" };
95 string const LaTeXShapeNames[6] =
96 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
98 string const LaTeXSizeNames[14] =
99 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
100 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
103 LyXFont::FontBits LyXFont::sane = { ROMAN_FAMILY,
114 LyXFont::FontBits LyXFont::inherit = { INHERIT_FAMILY,
125 LyXFont::FontBits LyXFont::ignore = { IGNORE_FAMILY,
137 /// Decreases font size by one
138 LyXFont & LyXFont::decSize()
141 case SIZE_HUGER: setSize(SIZE_HUGE); break;
142 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
143 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
144 case SIZE_LARGER: setSize(SIZE_LARGE); break;
145 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
146 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
147 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
148 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
149 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
150 case SIZE_TINY: break;
152 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
155 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
158 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
161 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
168 /// Increases font size by one
169 LyXFont & LyXFont::incSize()
172 case SIZE_HUGER: break;
173 case SIZE_HUGE: setSize(SIZE_HUGER); break;
174 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
175 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
176 case SIZE_LARGE: setSize(SIZE_LARGER); break;
177 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
178 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
179 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
180 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
181 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
183 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
186 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
189 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
192 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
199 /// Updates a misc setting according to request
200 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
203 if (newfont == TOGGLE) {
209 lyxerr <<"LyXFont::setMisc: Need state"
210 " ON or OFF to toggle. Setting to ON" << endl;
213 } else if (newfont == IGNORE)
220 /// Updates font settings according to request
221 void LyXFont::update(LyXFont const & newfont, bool toggleall)
223 if(newfont.family() == family() && toggleall)
224 setFamily(INHERIT_FAMILY); // toggle 'back'
225 else if (newfont.family() != IGNORE_FAMILY)
226 setFamily(newfont.family());
227 // else it's IGNORE_SHAPE
229 // "Old" behaviour: "Setting" bold will toggle bold on/off.
230 switch (newfont.series()) {
233 if (series() == BOLD_SERIES && toggleall)
234 setSeries(MEDIUM_SERIES);
236 setSeries(BOLD_SERIES);
240 setSeries(newfont.series());
246 if(newfont.shape() == shape() && toggleall)
247 setShape(INHERIT_SHAPE); // toggle 'back'
248 else if (newfont.shape() != IGNORE_SHAPE)
249 setShape(newfont.shape());
250 // else it's IGNORE_SHAPE
252 if (newfont.size() != IGNORE_SIZE) {
253 if (newfont.size() == INCREASE_SIZE)
255 else if (newfont.size() == DECREASE_SIZE)
257 else if (newfont.size() == size() && toggleall)
258 setSize(INHERIT_SIZE); // toggle 'back'
260 setSize(newfont.size());
263 setEmph(setMisc(newfont.emph(), emph()));
264 setUnderbar(setMisc(newfont.underbar(), underbar()));
265 setNoun(setMisc(newfont.noun(), noun()));
266 setLatex(setMisc(newfont.latex(), latex()));
268 switch(newfont.direction()) {
270 if (direction() == LTR_DIR)
271 setDirection(RTL_DIR);
273 setDirection(LTR_DIR);
278 setDirection(newfont.direction());
281 if(newfont.color() == color() && toggleall)
282 setColor(LColor::inherit); // toggle 'back'
283 else if (newfont.color() != LColor::ignore)
284 setColor(newfont.color());
288 /// Reduce font to fall back to template where possible
289 void LyXFont::reduce(LyXFont const & tmplt)
291 if (family() == tmplt.family())
292 setFamily(INHERIT_FAMILY);
293 if (series() == tmplt.series())
294 setSeries(INHERIT_SERIES);
295 if (shape() == tmplt.shape())
296 setShape(INHERIT_SHAPE);
297 if (size() == tmplt.size())
298 setSize(INHERIT_SIZE);
299 if (emph() == tmplt.emph())
301 if (underbar() == tmplt.underbar())
302 setUnderbar(INHERIT);
303 if (noun() == tmplt.noun())
305 if (latex() == tmplt.latex())
307 if (color() == tmplt.color())
308 setColor(LColor::inherit);
309 if (direction() == tmplt.direction())
310 setDirection(INHERIT_DIR);
314 /// Realize font from a template
315 // This one is not pretty, but it's extremely fast (Asger)
317 LyXFont & LyXFont::realize(LyXFont const & tmplt)
319 if (bits == inherit) {
324 if (bits.family == INHERIT_FAMILY) {
325 bits.family = tmplt.bits.family;
327 if (bits.series == INHERIT_SERIES) {
328 bits.series = tmplt.bits.series;
330 if (bits.shape == INHERIT_SHAPE) {
331 bits.shape = tmplt.bits.shape;
333 if (bits.size == INHERIT_SIZE) {
334 bits.size = tmplt.bits.size;
336 if (bits.emph == INHERIT) {
337 bits.emph = tmplt.bits.emph;
339 if (bits.underbar == INHERIT) {
340 bits.underbar = tmplt.bits.underbar;
342 if (bits.noun == INHERIT) {
343 bits.noun = tmplt.bits.noun;
345 if (bits.latex == INHERIT) {
346 bits.latex = tmplt.bits.latex;
348 if (bits.color == LColor::inherit) {
349 bits.color = tmplt.bits.color;
351 if (bits.direction == INHERIT_DIR) {
352 bits.direction = tmplt.bits.direction;
358 LyXFont & LyXFont::realize(LyXFont const & tmplt)
360 if (bits == inherit) {
365 if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
367 bits &= ~(Fam_Mask << Fam_Pos);
368 bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
370 if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
372 bits &= ~(Ser_Mask << Ser_Pos);
373 bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
375 if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
377 bits &= ~(Sha_Mask << Sha_Pos);
378 bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
380 if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
382 bits &= ~(Siz_Mask << Siz_Pos);
383 bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
385 if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
387 bits &= ~(Misc_Mask << Emp_Pos);
388 bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
390 if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
392 bits &= ~(Misc_Mask << Und_Pos);
393 bits |= (tmplt.bits & Misc_Mask << Und_Pos);
395 if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
397 bits &= ~(Misc_Mask << Nou_Pos);
398 bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
400 if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
402 bits &= ~(Misc_Mask << Lat_Pos);
403 bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
405 if ((bits & (Col_Mask << Col_Pos)) == ui32(LColor::inherit) << Col_Pos)
407 bits &= ~(Col_Mask << Col_Pos);
408 bits |= (tmplt.bits & Col_Mask << Col_Pos);
410 if ((bits & (Dir_Mask << Dir_Pos)) == ui32(INHERIT_DIR) << Dir_Pos)
412 bits &= ~(Dir_Mask << Dir_Pos);
413 bits |= (tmplt.bits & Dir_Mask << Dir_Pos);
421 /// Is font resolved?
422 bool LyXFont::resolved() const
424 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
425 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
426 emph() != INHERIT && underbar() != INHERIT &&
427 noun() != INHERIT && latex() != INHERIT &&
428 color() != LColor::inherit &&
429 direction() != INHERIT_DIR);
433 /// Build GUI description of font state
434 string LyXFont::stateText() const
437 if (family() != INHERIT_FAMILY)
438 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
439 if (series() != INHERIT_SERIES)
440 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
441 if (shape() != INHERIT_SHAPE)
442 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
443 if (size() != INHERIT_SIZE)
444 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
445 if (color() != LColor::inherit)
446 buf += lcolor.getGUIName(color()) + ", ";
447 if (emph() != INHERIT)
448 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
449 if (underbar() != INHERIT)
450 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
451 if (noun() != INHERIT)
452 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
453 if (latex() != INHERIT)
454 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
455 if (direction() != INHERIT_DIR)
456 buf += string(_("Direction ")) + _(GUIDirectionNames[direction()].c_str()) + ", ";
459 buf = strip(buf, ' ');
460 buf = strip(buf, ',');
465 // Set family according to lyx format string
466 LyXFont & LyXFont::setLyXFamily(string const & fam)
468 string s = lowercase(fam);
471 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
472 if (s == LyXFamilyNames[i]) {
473 setFamily(LyXFont::FONT_FAMILY(i));
475 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
476 << s << '\'' << endl;
481 // Set series according to lyx format string
482 LyXFont & LyXFont::setLyXSeries(string const & ser)
484 string s = lowercase(ser);
487 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
488 if (s == LyXSeriesNames[i]) {
489 setSeries(LyXFont::FONT_SERIES(i));
491 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
492 << s << '\'' << endl;
497 // Set shape according to lyx format string
498 LyXFont & LyXFont::setLyXShape(string const & sha)
500 string s = lowercase(sha);
503 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
504 if (s == LyXShapeNames[i]) {
505 setShape(LyXFont::FONT_SHAPE(i));
507 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
508 << s << '\'' << endl;
513 // Set size according to lyx format string
514 LyXFont & LyXFont::setLyXSize(string const & siz)
516 string s = lowercase(siz);
518 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
519 if (s == LyXSizeNames[i]) {
520 setSize(LyXFont::FONT_SIZE(i));
522 lyxerr << "LyXFont::setLyXSize: Unknown size `"
523 << s << '\'' << endl;
527 // Set size according to lyx format string
528 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
530 string s = lowercase(siz);
532 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
533 if (s == LyXMiscNames[i])
534 return FONT_MISC_STATE(i);
535 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
536 << s << '\'' << endl;
540 /// Sets color after LyX text format
541 LyXFont & LyXFont::setLyXColor(string const & col)
543 setColor(lcolor.getFromLyXName(col));
548 /// Sets size after GUI name
549 LyXFont & LyXFont::setGUISize(string const & siz)
551 #warning this might be wrong.
553 // this is how it was how it was done in the lyx repository...
554 // but this does not make sense.
555 setColor(lcolor.getFromGUIName(siz));
560 // Returns size in latex format
561 string LyXFont::latexSize() const
563 return LaTeXSizeNames[size()];
567 // Read a font definition from given file in lyx format
569 LyXFont & LyXFont::lyxRead(LyXLex & lex)
572 bool finished = false;
573 while (!finished && lex.IsOK() && !error) {
575 string tok = lowercase(lex.GetString());
579 } else if (tok == "endfont") {
581 } else if (tok == "family") {
583 string tok = lex.GetString();
585 } else if (tok == "series") {
587 string tok = lex.GetString();
589 } else if (tok == "shape") {
591 string tok = lex.GetString();
593 } else if (tok == "size") {
595 string tok = lex.GetString();
597 } else if (tok == "latex") {
599 string tok = lowercase(lex.GetString());
601 if (tok == "no_latex") {
603 } else if (tok == "latex") {
606 lex.printError("Illegal LaTeX type`$$Token'");
608 } else if (tok == "misc") {
610 string tok = lowercase(lex.GetString());
612 if (tok == "no_bar") {
614 } else if (tok == "no_emph") {
616 } else if (tok == "no_noun") {
618 } else if (tok == "emph") {
620 } else if (tok == "underbar") {
622 } else if (tok == "noun") {
625 lex.printError("Illegal misc type `$$Token´");
627 } else if (tok == "color") {
629 string tok = lex.GetString();
631 } else if (tok == "direction") {
633 string tok = lowercase(lex.GetString());
636 setDirection(LTR_DIR);
637 } else if (tok == "rtl") {
638 setDirection(RTL_DIR);
640 lex.printError("Illegal type`$$Token'");
643 lex.printError("Unknown tag `$$Token'");
651 /// Writes the changes from this font to orgfont in .lyx format in file
652 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
655 if (orgfont.family() != family()) {
656 os << "\\family " << LyXFamilyNames[family()] << " \n";
658 if (orgfont.series() != series()) {
659 os << "\\series " << LyXSeriesNames[series()] << " \n";
661 if (orgfont.shape() != shape()) {
662 os << "\\shape " << LyXShapeNames[shape()] << " \n";
664 if (orgfont.size() != size()) {
665 os << "\\size " << LyXSizeNames[size()] << " \n";
667 if (orgfont.emph() != emph()) {
668 os << "\\emph " << LyXMiscNames[emph()] << " \n";
670 if (orgfont.underbar() != underbar()) {
671 // This is only for backwards compatibility
672 switch (underbar()) {
673 case OFF: os << "\\bar no \n"; break;
674 case ON: os << "\\bar under \n"; break;
675 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
676 "TOGGLE should not appear here!"
679 case INHERIT: os << "\\bar default \n"; break;
680 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
681 "IGNORE should not appear here!"
686 if (orgfont.noun() != noun()) {
687 os << "\\noun " << LyXMiscNames[noun()] << " \n";
689 if (orgfont.latex() != latex()) {
690 // This is only for backwards compatibility
692 case OFF: os << "\\latex no_latex \n"; break;
693 case ON: os << "\\latex latex \n"; break;
694 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
695 "TOGGLE should not appear here!"
698 case INHERIT: os << "\\latex default \n"; break;
699 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
700 "IGNORE should not appear here!"
705 if (orgfont.color() != color()) {
706 os << "\\color " << lcolor.getLyXName(color()) << "\n";
708 if (orgfont.direction() != direction()) {
709 switch (direction()) {
710 case RTL_DIR: os << "\\direction rtl \n"; break;
711 case LTR_DIR: os << "\\direction ltr\n"; break;
712 case TOGGLE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
713 "TOGGLE should not appear here!"
715 case INHERIT_DIR: os << "\\direction default \n"; break;
716 case IGNORE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
717 "IGNORE should not appear here!"
725 /// Writes the head of the LaTeX needed to impose this font
726 // Returns number of chars written.
727 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base,
728 LyXFont const & prev) const
733 if (f.bits == inherit)
739 FONT_DIRECTION direction = f.direction();
740 if (direction != prev.direction()) {
741 if (direction == LTR_DIR) {
744 env = true; //We have opened a new environment
746 if (direction == RTL_DIR) {
749 env = true; //We have opened a new environment
753 if (f.family() != INHERIT_FAMILY) {
755 file += LaTeXFamilyNames[f.family()];
757 count += LaTeXFamilyNames[f.family()].length() + 2;
758 env = true; //We have opened a new environment
760 if (f.series() != INHERIT_SERIES) {
762 file += LaTeXSeriesNames[f.series()];
764 count += LaTeXSeriesNames[f.series()].length() + 2;
765 env = true; //We have opened a new environment
767 if (f.shape() != INHERIT_SHAPE) {
769 file += LaTeXShapeNames[f.shape()];
771 count += LaTeXShapeNames[f.shape()].length() + 2;
772 env = true; //We have opened a new environment
774 if (f.color() != LColor::inherit) {
775 file += "\\textcolor{";
776 file += lcolor.getLaTeXName(f.color());
778 count += lcolor.getLaTeXName(f.color()).length() + 13;
779 env = true; //We have opened a new environment
781 if (f.emph() == ON) {
784 env = true; //We have opened a new environment
786 if (f.underbar() == ON) {
787 file += "\\underbar{";
789 env = true; //We have opened a new environment
791 // \noun{} is a LyX special macro
792 if (f.noun() == ON) {
795 env = true; //We have opened a new environment
797 if (f.size() != INHERIT_SIZE) {
798 // If we didn't open an environment above, we open one here
804 file += LaTeXSizeNames[f.size()];
806 count += LaTeXSizeNames[f.size()].length() + 2;
812 /// Writes ending block of LaTeX needed to close use of this font
813 // Returns number of chars written
814 // This one corresponds to latexWriteStartChanges(). (Asger)
815 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base,
816 LyXFont const & next) const
818 LyXFont f = *this; // why do you need this?
819 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
820 // Because this function is const. Everything breaks if this
821 // method changes the font it represents. There is no speed penalty
822 // by using the temporary. (Asger)
824 if (f.bits == inherit)
830 FONT_DIRECTION direction = f.direction();
831 if ( direction != next.direction()
832 && (direction == RTL_DIR || direction == LTR_DIR) ) {
835 env = true; // Size change need not bother about closing env.
838 if (f.family() != INHERIT_FAMILY) {
841 env = true; // Size change need not bother about closing env.
843 if (f.series() != INHERIT_SERIES) {
846 env = true; // Size change need not bother about closing env.
848 if (f.shape() != INHERIT_SHAPE) {
851 env = true; // Size change need not bother about closing env.
853 if (f.color() != LColor::inherit) {
856 env = true; // Size change need not bother about closing env.
858 if (f.emph() == ON) {
861 env = true; // Size change need not bother about closing env.
863 if (f.underbar() == ON) {
866 env = true; // Size change need not bother about closing env.
868 if (f.noun() == ON) {
871 env = true; // Size change need not bother about closing env.
873 if (f.size() != INHERIT_SIZE) {
874 // We only have to close if only size changed
885 LColor::color LyXFont::realColor() const
888 return LColor::latex;
889 if (color() == LColor::none)
890 return LColor::foreground;
895 // Convert logical attributes to concrete shape attribute
896 LyXFont::FONT_SHAPE LyXFont::realShape() const
898 register FONT_SHAPE s = shape();
912 XFontStruct * LyXFont::getXFontstruct() const
914 return fontloader.load(family(), series(), realShape(), size());
918 int LyXFont::maxAscent() const
920 return getXFontstruct()->ascent;
924 int LyXFont::maxDescent() const
926 return getXFontstruct()->descent;
930 int LyXFont::ascent(char c) const
932 XFontStruct * finfo = getXFontstruct();
933 unsigned int uc = static_cast<unsigned char>(c);
935 && uc >= finfo->min_char_or_byte2
936 && uc <= finfo->max_char_or_byte2)
937 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
939 return finfo->ascent;
943 int LyXFont::descent(char c) const
945 XFontStruct * finfo = getXFontstruct();
946 unsigned int uc = static_cast<unsigned char>(c);
948 && uc >= finfo->min_char_or_byte2
949 && uc <= finfo->max_char_or_byte2)
950 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
952 return finfo->descent;
956 // Specialized after profiling. (Asger)
957 int LyXFont::width(char c) const
959 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
960 return XTextWidth(getXFontstruct(), &c, 1);
962 return textWidth(&c, 1);
967 int LyXFont::lbearing(char c) const
969 XFontStruct * finfo = getXFontstruct();
970 unsigned int uc = static_cast<unsigned char>(c);
972 && uc >= finfo->min_char_or_byte2
973 && uc <= finfo->max_char_or_byte2)
974 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
980 int LyXFont::rbearing(char c) const
982 XFontStruct * finfo = getXFontstruct();
983 unsigned int uc = static_cast<unsigned char>(c);
985 && uc >= finfo->min_char_or_byte2
986 && uc <= finfo->max_char_or_byte2)
987 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
993 int LyXFont::textWidth(char const * s, int n) const
995 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
996 return XTextWidth(getXFontstruct(), s, n);
998 // emulate smallcaps since X doesn't support this
999 unsigned int result = 0;
1001 LyXFont smallfont = *this;
1002 smallfont.decSize();
1003 smallfont.decSize();
1004 smallfont.setShape(LyXFont::UP_SHAPE);
1005 for (int i = 0; i < n; ++i) {
1007 // when islower is a macro, the cast is needed (JMarc)
1008 if (islower(static_cast<unsigned char>(c))){
1010 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
1012 result += XTextWidth(getXFontstruct(), &c, 1);
1020 int LyXFont::stringWidth(string const & s) const
1022 if (s.empty()) return 0;
1023 return textWidth(s.c_str(), s.length());
1027 int LyXFont::signedStringWidth(string const & s) const
1029 if (s.empty()) return 0;
1030 if (s.c_str()[0] == '-')
1031 return -textWidth(s.c_str()+1, s.length()-1);
1033 return textWidth(s.c_str(), s.length());
1037 int LyXFont::drawText(char const * s, int n, Pixmap,
1040 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1041 /* XDrawString(fl_display,
1046 XFlush(fl_display); */
1047 return XTextWidth(getXFontstruct(), s, n);
1050 // emulate smallcaps since X doesn't support this
1053 LyXFont smallfont = *this;
1054 smallfont.decSize();
1055 smallfont.decSize();
1056 smallfont.setShape(LyXFont::UP_SHAPE);
1057 for (int i = 0; i < n; ++i) {
1059 if (islower(static_cast<unsigned char>(c))){
1061 /* XDrawString(fl_display,
1066 x += XTextWidth(smallfont.getXFontstruct(),
1068 //XFlush(fl_display);
1070 /* XDrawString(fl_display,
1075 x += XTextWidth(getXFontstruct(), &c, 1);
1076 //XFlush(fl_display);
1084 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
1086 return drawText(s.c_str(), s.length(), pm, baseline, x);
1090 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1093 f1.setLatex(f.latex());
1098 LyXDirection LyXFont::getFontDirection() const
1100 if (lyxrc->rtl_support
1101 && direction() == LyXFont::RTL_DIR
1102 && latex() != LyXFont::ON)
1103 return LYX_DIR_RIGHT_TO_LEFT;
1105 return LYX_DIR_LEFT_TO_RIGHT;
1109 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1111 return o << int(fms);