home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / lyxfont.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  25KB  |  967 lines

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