]> git.lyx.org Git - lyx.git/blobdiff - src/font.C
Fix fuer #209
[lyx.git] / src / font.C
index 210d10bd60e61a0feccdbd16703736a1a6803b79..0f7e707c9f4bc59f4f77620230795ca5653f3e61 100644 (file)
 #pragma implementation
 #endif
 
-#include <cctype>
-
+#include "support/lstrings.h"
 #include "font.h"
 #include "FontLoader.h"
 #include "lyxrc.h"
 #include "encoding.h"
 #include "language.h"
 
+#include <boost/smart_ptr.hpp>
+
+
 namespace {
 
 inline
@@ -110,16 +112,13 @@ int lyxfont::width(char const * s, size_t n, LyXFont const & f)
                return n;
 
        if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
-               XChar2b * xs = new XChar2b[n];
+               boost::scoped_array<XChar2b> xs(new XChar2b[n]);
                Encoding const * encoding = f.language()->encoding();
-               //LyXFont const * font = &f;
                LyXFont font(f);
-               if (f.family() == LyXFont::SYMBOL_FAMILY) {
+               if (f.isSymbolFont()) {
 #ifdef USE_UNICODE_FOR_SYMBOLS
-                       //LyXFont font2 = f;
                        font.setFamily(LyXFont::ROMAN_FAMILY);
                        font.setShape(LyXFont::UP_SHAPE);
-                       //font = &font2;
 #endif
                        encoding = encodings.symbol_encoding();
                }
@@ -128,8 +127,7 @@ int lyxfont::width(char const * s, size_t n, LyXFont const & f)
                        xs[i].byte1 = c >> 8;
                        xs[i].byte2 = c & 0xff;
                 }
-               int result = width(xs, n, font);
-               delete[] xs;
+               int result = width(xs.get(), n, font);
                return result;
        }
 
@@ -138,14 +136,11 @@ int lyxfont::width(char const * s, size_t n, LyXFont const & f)
        } else {
                // emulate smallcaps since X doesn't support this
                unsigned int result = 0;
-               char c;
                LyXFont smallfont(f);
                smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
                for (size_t i = 0; i < n; ++i) {
-                       c = s[i];
-                       // when islower is a macro, the cast is needed (JMarc)
-                       if (islower(static_cast<unsigned char>(c))) {
-                               c = toupper(c);
+                       char const c = uppercase(s[i]);
+                       if (c != s[i]) {
                                result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
                        } else {
                                result += ::XTextWidth(getXFontstruct(f), &c, 1);
@@ -158,7 +153,8 @@ int lyxfont::width(char const * s, size_t n, LyXFont const & f)
 
 int lyxfont::signedWidth(string const & s, LyXFont const & f)
 {
-       if (s.empty()) return 0;
+       if (s.empty())
+               return 0;
        if (s[0] == '-')
                return -width(s.substr(1, s.length() - 1), f);
        else
@@ -181,8 +177,13 @@ int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
                LyXFont smallfont(f);
                smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
                for (int i = 0; i < n; ++i) {
-                       if (s[i].byte1 == 0 && islower(s[i].byte2)) {
-                               c.byte2 = toupper(s[i].byte2);
+                       if (s[i].byte1)
+                               c = s[i];
+                       else {
+                               c.byte1 = s[i].byte1;
+                               c.byte2 = uppercase(s[i].byte2);
+                       }
+                       if (c.byte2 != s[i].byte2) {
                                result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
                        } else {
                                result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
@@ -224,9 +225,11 @@ void lyxfont::rectText(string const & str, LyXFont const & font,
 void lyxfont::buttonText(string const & str, LyXFont const & font,
                int & width, int & ascent, int & descent)
 {
-       width = lyxfont::width(str, font) + 8;
-       ascent = lyxfont::maxAscent(font) + 3;
-       descent = lyxfont::maxDescent(font) + 3;
+       static int const d = 3;
+       
+       width = lyxfont::width(str, font) + d * 2 + 2;
+       ascent = lyxfont::maxAscent(font) + d;
+       descent = lyxfont::maxDescent(font) + d;
 }