]> git.lyx.org Git - lyx.git/blobdiff - src/FontInfo.C
Andres fixes to sstrem problems
[lyx.git] / src / FontInfo.C
index 71cf6e054b32fef27c401e01f1fa83ef34e96fbe..c61d6662bb8ff680209ea98e5ecfb94ac6104245 100644 (file)
@@ -1,42 +1,46 @@
 // -*- C++ -*-
 /* This file is part of
- * ======================================================
+ * ====================================================== 
  * 
  *           LyX, The Document Processor
  *      
- *         Copyright (C) 1997 Asger Alstrup
+ *         Copyright 1997 Asger Alstrup
  *           and the LyX Team.
  *
- *======================================================*/
+ * ====================================================== */
 
 #include <config.h>
-#include <math.h>      // fabs()
-#include <stdlib.h>    // atoi()
+#include <cmath>       // fabs()
+#include <cstdlib>     // atoi()
+
+#include FORMS_H_LOCATION
 
 #ifdef __GNUG__
 #pragma implementation "FontInfo.h"
 #endif
 
 #include "FontInfo.h"
-#include "error.h"
+#include "debug.h"
 #include "lyxrc.h"     // lyxrc.use_scalable_fonts
-extern LyXRC * lyxrc;
+#include "support/lstrings.h"
+
+using std::endl;
 
 /// Load font close to this size
-LString FontInfo::getFontname(int size)
+string FontInfo::getFontname(int size)
 {
        if (!exist())
-               return LString();
+               return string();
 
        int closestind = -1;
        double error = 100000;
 
-       for (int i=0; i<matches; i++) {
+       for (int i = 0; i < matches; ++i) {
                if (sizes[i] == 0) {
                        // Scalable font should not be considered close
                } else if (sizes[i] == size) {
-                       lyxerr.debug(LString("Exact font match with\n")
-                                             + strings[i], Error::FONT);
+                       lyxerr[Debug::FONT] << "Exact font match with\n"
+                                           << strings[i] << endl;
                        return strings[i];
                } else if (fabs(sizes[i] - size - 0.1) < error) {
                        error = fabs(sizes[i] - size - 0.1);
@@ -44,57 +48,60 @@ LString FontInfo::getFontname(int size)
                }
        }
 
-       if (scalable && lyxrc->use_scalable_fonts) {
+       if (scalable && lyxrc.use_scalable_fonts) {
                // We can use scalable
-               LString font = resize(strings[scaleindex], size);
-               lyxerr.debug("Using scalable font to get\n"
-                             + font, Error::FONT);
+               string font = resize(strings[scaleindex], size);
+               lyxerr[Debug::FONT] << "Using scalable font to get\n"
+                                   << font << endl;
                return font;
        }
 
        // Did any fonts get close?
        if (closestind == -1) {
                // No, and we are not allowed to use scalables, so...
-               return LString();
+               return string();
        }
 
        // We use the closest match
-       lyxerr.debug(LString("Using closest font match to get size ") + size 
-                     + " with\n" + strings[closestind], Error::FONT);
+       lyxerr[Debug::FONT] << "Using closest font match to get size "
+                           << size 
+                           << " with\n" << strings[closestind] << endl;
        return strings[closestind];
 }
 
+
 /// Build newly sized font string 
-LString FontInfo::resize(LString const & font, int size) const {
+string FontInfo::resize(string const & font, int size) const
+{
+       string ret(font);
        // Find the position of the size spec
-       int cut=0, before=0, after=0;
-       for (int i=0; i<font.length(); i++) {
-               if (font[i] == '-') {
-                       cut++;
-                       if (cut==7) {
-                               before = i;
-                       } else if (cut==8) {
-                               after = i;
+       int cut = 0;
+       string::iterator before = string::iterator(0);
+       string::iterator after = string::iterator(0);
+       for (string::iterator sit = ret.begin();
+            sit != ret.end(); ++sit)
+               if ((*sit) == '-') {
+                       ++cut;
+                       if (cut == 7) before = sit + 1;
+                       else if (cut == 8) {
+                               after = sit;
                                break;
                        }
                }
-       }
-
-       LString head = font;
-       head.substring(0, before);
-       LString tail = font;
-       tail.substring(after,tail.length()-1);
-       return head + size + tail;
+       ret.replace(before, after, tostr(size));
+       return ret;
 }
 
+
 /// Set new pattern
-void FontInfo::setPattern(LString const & pat)
+void FontInfo::setPattern(string const & pat)
 {
        release();
        init();
        pattern = pat;
 }
 
+
 /// Query font in X11
 void FontInfo::query()
 {
@@ -102,12 +109,15 @@ void FontInfo::query()
                return;
 
        if (pattern.empty()) {
-               lyxerr.print("Can not use empty font name for font query.");
+               lyxerr << "Can not use empty font name for font query."
+                      << endl;
                queried = true;
                return;
        }
 
-       char ** list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
+       char ** list = 0;
+       if (lyxrc.use_gui)
+               list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
 
        if (list == 0) {
                // No fonts matched
@@ -116,12 +126,12 @@ void FontInfo::query()
        } else {
                release();
                sizes = new int[matches];
-               strings = new LString[matches];
+               strings = new string[matches];
 
                // We have matches. Run them through
-               for(int i=0; i<matches; i++) {
-                       LString name(list[i]);
-                       sizes[i] = atoi(name.token('-',7).c_str());
+               for(int i = 0; i < matches; ++i) {
+                       string name(list[i]);
+                       sizes[i] = atoi(token(name, '-', 7).c_str());
                        strings[i] = name;
                        if (sizes[i] == 0) {
                                if (scaleindex == -1) {
@@ -135,6 +145,18 @@ void FontInfo::query()
        queried = true;
 }
 
+
+void FontInfo::init()
+{
+       sizes = 0;
+       strings = 0;
+       matches = 0;
+       queried = false;
+       scalable = false;
+       scaleindex = -1;
+}
+
+
 /// Release allocated stuff
 void FontInfo::release()
 {