]> git.lyx.org Git - lyx.git/blobdiff - src/FontInfo.C
two patches from john
[lyx.git] / src / FontInfo.C
index 1ccb10816fa43395193a6edd6ea7d85bff603117..3cb9af091190197e1d8020ed5d2c6ab1927992f3 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /* This file is part of
  * ====================================================== 
  * 
  * ====================================================== */
 
 #include <config.h>
-#include <cmath>       // fabs()
-#include <cstdlib>     // atoi()
-
-#include FORMS_H_LOCATION
 
 #ifdef __GNUG__
-#pragma implementation "FontInfo.h"
+#pragma implementation
 #endif
 
 #include "FontInfo.h"
 #include "debug.h"
 #include "lyxrc.h"     // lyxrc.use_scalable_fonts
+
 #include "support/lstrings.h"
+#include "support/lyxlib.h"
+
+#include "frontends/GUIRunTime.h"
+
+#include <cmath>       // abs()
 
 using std::endl;
+using std::abs;
+
 
 /// Load font close to this size
 string const FontInfo::getFontname(int size)
@@ -33,7 +36,7 @@ string const FontInfo::getFontname(int size)
                return string();
 
        int closestind = -1;
-       double error = 100000;
+       double error = 100000.0;
 
        for (int i = 0; i < matches; ++i) {
                if (sizes[i] == 0) {
@@ -42,15 +45,15 @@ string const FontInfo::getFontname(int size)
                        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);
+               } else if (abs(sizes[i] - size - 0.1) < error) {
+                       error = abs(sizes[i] - size - 0.1);
                        closestind = i;
                }
        }
 
-       if (scalable && lyxrc.use_scalable_fonts) {
+       if (scalable && (lyxrc.use_scalable_fonts || closestind == -1)) {
                // We can use scalable
-               string font = resize(strings[scaleindex], size);
+               string const font = resize(strings[scaleindex], size);
                lyxerr[Debug::FONT] << "Using scalable font to get\n"
                                    << font << endl;
                return font;
@@ -58,7 +61,7 @@ string const FontInfo::getFontname(int size)
 
        // Did any fonts get close?
        if (closestind == -1) {
-               // No, and we are not allowed to use scalables, so...
+               // No, so...
                return string();
        }
 
@@ -96,7 +99,6 @@ string const FontInfo::resize(string const & font, int size) const
 /// Set new pattern
 void FontInfo::setPattern(string const & pat)
 {
-       release();
        init();
        pattern = pat;
 }
@@ -109,7 +111,7 @@ void FontInfo::query()
                return;
 
        if (pattern.empty()) {
-               lyxerr << "Can not use empty font name for font query."
+               lyxerr << "Cannot use empty font name for font query."
                       << endl;
                queried = true;
                return;
@@ -117,21 +119,23 @@ void FontInfo::query()
 
        char ** list = 0;
        if (lyxrc.use_gui)
-               list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
+               list = XListFonts(GUIRunTime::x11Display(), pattern.c_str(),
+                                 100, &matches);
 
        if (list == 0) {
                // No fonts matched
                scalable = false;
-               sizes = 0;
+               sizes.reset();
        } else {
-               release();
-               sizes = new int[matches];
-               strings = new string[matches];
+               sizes.reset(new int[matches]);
+               strings.reset(new string[matches]);
 
                // We have matches. Run them through
-               for(int i = 0; i < matches; ++i) {
+               for (int i = 0; i < matches; ++i) {
                        string name(list[i]);
-                       sizes[i] = atoi(token(name, '-', 7).c_str());
+                       lyxerr[Debug::FONT] << "match #" << i << " "
+                                           << name << endl; 
+                       sizes[i] = lyx::atoi(token(name, '-', 7));
                        strings[i] = name;
                        if (sizes[i] == 0) {
                                if (scaleindex == -1) {
@@ -148,24 +152,10 @@ void FontInfo::query()
 
 void FontInfo::init()
 {
-       sizes = 0;
-       strings = 0;
+       sizes.reset();
+       strings.reset();
        matches = 0;
        queried = false;
        scalable = false;
        scaleindex = -1;
 }
-
-
-/// Release allocated stuff
-void FontInfo::release()
-{
-       if (sizes) {
-               delete [] sizes;
-               sizes = 0;
-       }
-       if (strings) {
-               delete [] strings;
-               strings = 0;
-       }
-}