X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FFontInfo.C;h=c9dd2e1839010d434723cf08a355c613dd9deff4;hb=98c966c64594611e469313314abd1e59524adb4a;hp=f9a10c2a9aaecbbdfae6d05938f1b38a98fee504;hpb=6130fe1aa5c07d0204205bcf5dd1f86b8449fb16;p=lyx.git diff --git a/src/FontInfo.C b/src/FontInfo.C index f9a10c2a9a..c9dd2e1839 100644 --- a/src/FontInfo.C +++ b/src/FontInfo.C @@ -1,37 +1,42 @@ -// -*- C++ -*- /* This file is part of - * ====================================================== - * + * ====================================================== + * * LyX, The Document Processor - * - * Copyright (C) 1997 Asger Alstrup + * + * Copyright 1997 Asger Alstrup * and the LyX Team. * * ====================================================== */ #include -#include // fabs() -#include // atoi() #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 // abs() + +using std::endl; +using std::abs; -extern LyXRC * lyxrc; /// Load font close to this size -string FontInfo::getFontname(int size) +string const FontInfo::getFontname(int size) { if (!exist()) return string(); int closestind = -1; - double error = 100000; + double error = 100000.0; for (int i = 0; i < matches; ++i) { if (sizes[i] == 0) { @@ -40,15 +45,15 @@ string 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; @@ -56,51 +61,49 @@ string 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(); } // We use the closest match lyxerr[Debug::FONT] << "Using closest font match to get size " - << size + << size << " with\n" << strings[closestind] << endl; return strings[closestind]; } -/// Build newly sized font string -string FontInfo::resize(string const & font, int size) const { + +/// Build newly sized font string +string const FontInfo::resize(string const & font, int size) const +{ + string ret(font); // Find the position of the size spec -#ifdef WITH_WARNINGS -#warning rewrite to use std::string constructs -#endif - int cut = 0, before = 0, after = 0; - for (string::size_type i = 0; i < font.length(); ++i) { - if (font[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 = i; - } else if (cut == 8) { - after = i; + if (cut == 7) before = sit + 1; + else if (cut == 8) { + after = sit; break; } } - } - - string head = font; - head.erase(before + 1, string::npos); - string tail = font; - tail.erase(0, after); - return head + tostr(size) + tail; + ret.replace(before, after, tostr(size)); + return ret; } + /// Set new pattern void FontInfo::setPattern(string const & pat) { - release(); init(); pattern = pat; } + /// Query font in X11 void FontInfo::query() { @@ -108,26 +111,31 @@ void FontInfo::query() return; if (pattern.empty()) { - lyxerr << "Can not use empty font name for font query." << endl; + lyxerr << "Cannot 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(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) { @@ -141,15 +149,13 @@ void FontInfo::query() queried = true; } -/// Release allocated stuff -void FontInfo::release() + +void FontInfo::init() { - if (sizes) { - delete [] sizes; - sizes = 0; - } - if (strings) { - delete [] strings; - strings = 0; - } + sizes.reset(); + strings.reset(); + matches = 0; + queried = false; + scalable = false; + scaleindex = -1; }