]> git.lyx.org Git - lyx.git/blobdiff - src/font.C
small fix with footnote, use stringstream some more
[lyx.git] / src / font.C
index f8cd5c4eb7d0c4833ce0bf1c35325152c6ea5014..f022b22c41eedb25c4ab4e8848368759466b7b51 100644 (file)
@@ -1,13 +1,30 @@
+/* This file is part of
+ * ====================================================== 
+ * 
+ *           LyX, The Document Processor
+ *        
+ *           Copyright 1995 Matthias Ettrich
+ *           Copyright 1995-2000 The LyX Team.
+ *
+ * ====================================================== */
+
 #include <config.h>
 
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
+#include <cctype>
+
 #include "font.h"
 #include "FontLoader.h"
 #include "lyxrc.h"
+#include "encoding.h"
+#include "language.h"
 
-extern LyXRC lyxrc;
+namespace {
 
-// namespace {
-static inline
+inline
 XFontStruct * getXFontstruct(LyXFont const & f)
 {
        return fontloader.load(f.family(), f.series(),
@@ -15,12 +32,13 @@ XFontStruct * getXFontstruct(LyXFont const & f)
 }
 
 
-static inline
+inline
 XID getFontID(LyXFont const & f)
 {
        return getXFontstruct(f)->fid;
 }
-// } // end of anon namespace
+
+} // namespace anon
 
 int lyxfont::maxAscent(LyXFont const & f)
 {
@@ -40,7 +58,7 @@ int lyxfont::ascent(char c, LyXFont const & f)
        unsigned int uc = static_cast<unsigned char>(c);
        if (finfo->per_char
            && uc >= finfo->min_char_or_byte2
-           && uc <= finfo->max_char_or_byte2) 
+           && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1
                return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
        else
                return finfo->ascent;
@@ -53,7 +71,7 @@ int lyxfont::descent(char c, LyXFont const & f)
        unsigned int uc = static_cast<unsigned char>(c);
        if (finfo->per_char
            && uc >= finfo->min_char_or_byte2
-           && uc <= finfo->max_char_or_byte2) 
+           && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1
                return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
        else
                return finfo->descent;
@@ -66,7 +84,7 @@ int lyxfont::lbearing(char c, LyXFont const & f)
        unsigned int uc = static_cast<unsigned char>(c);
        if (finfo->per_char
            && uc >= finfo->min_char_or_byte2
-           && uc <= finfo->max_char_or_byte2) 
+           && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1
                return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
        else
                return 0;
@@ -79,36 +97,42 @@ int lyxfont::rbearing(char c, LyXFont const & f)
        unsigned int uc = static_cast<unsigned char>(c);
        if (finfo->per_char
            && uc >= finfo->min_char_or_byte2
-           && uc <= finfo->max_char_or_byte2) 
+           && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1
                return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
        else
                return width(c, f);
 }
 
 
-int lyxfont::width(char c, LyXFont const & f)
+int lyxfont::width(char const * s, size_t n, LyXFont const & f)
 {
-       if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
-               return lyxrc.use_gui ? ::XTextWidth(getXFontstruct(f), &c, 1)
-                       : 1;
-       } else {
-               LyXFont smallfont(f);
-               smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
-               if (islower(static_cast<unsigned char>(c))) {
-                       c = toupper(c);
-                       return ::XTextWidth(getXFontstruct(smallfont), &c, 1);
-               } else {
-                       return ::XTextWidth(getXFontstruct(f), &c, 1);
+       if (!lyxrc.use_gui)
+               return n;
+
+       if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
+               XChar2b * xs = new XChar2b[n];
+               Encoding const * encoding = f.language()->encoding();
+               //LyXFont const * font = &f;
+               LyXFont font(f);
+               if (f.family() == LyXFont::SYMBOL_FAMILY) {
+#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();
                }
+               for (size_t i = 0; i < n; ++i) {
+                       Uchar c = encoding->ucs(s[i]);
+                       xs[i].byte1 = c >> 8;
+                       xs[i].byte2 = c & 0xff;
+                }
+               int result = width(xs, n, font);
+               delete[] xs;
+               return result;
        }
-}
-
 
-int lyxfont::width(char const * s, int n, LyXFont const & f)
-{
-       if (!lyxrc.use_gui)
-               return n;
-       
        if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
                return ::XTextWidth(getXFontstruct(f), s, n);
        } else {
@@ -117,7 +141,7 @@ int lyxfont::width(char const * s, int n, LyXFont const & f)
                char c;
                LyXFont smallfont(f);
                smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
-               for (int i = 0; i < n; ++i) {
+               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))) {
@@ -135,19 +159,51 @@ int lyxfont::width(char const * s, int n, LyXFont const & f)
 int lyxfont::signedWidth(string const & s, LyXFont const & f)
 {
        if (s.empty()) return 0;
-       if (s.c_str()[0] == '-')
-               return -width(s.c_str() + 1, s.length() - 1, f);
+       if (s[0] == '-')
+               return -width(s.substr(1, s.length() - 1), f);
        else
-               return width(s.c_str(), s.length(), f);
+               return width(s, f);
 }
 
 
-int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
+//int lyxfont::width(wstring const & s, int n, LyXFont const & f)
+int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
+{
+       if (!lyxrc.use_gui)
+               return n;
+       
+       if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
+               return ::XTextWidth16(getXFontstruct(f), s, n);
+       } else {
+               // emulate smallcaps since X doesn't support this
+               unsigned int result = 0;
+               static XChar2b c = {0, 0};
+               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);
+                               result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
+                       } else {
+                               result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
+                       }
+               }
+               return result;
+       }
+}
+
+int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
 {
        return ::XTextWidth(getXFontstruct(f), str, count);
 }
 
 
+int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
+{
+       return ::XTextWidth16(getXFontstruct(f), str, count);
+}
+
+
 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
 {
        ::XSetFont(display, gc, getFontID(f));