X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffont.C;h=f9bcb0ec805d438820f6b013f51266ac3675dbea;hb=98c966c64594611e469313314abd1e59524adb4a;hp=076f16d7b26507ae8ef73e829291de89c974bd8b;hpb=d6665cba427b04ae37f42c846398cad518d2be0f;p=lyx.git diff --git a/src/font.C b/src/font.C index 076f16d7b2..f9bcb0ec80 100644 --- a/src/font.C +++ b/src/font.C @@ -1,10 +1,10 @@ /* This file is part of - * ====================================================== - * + * ====================================================== + * * LyX, The Document Processor - * + * * Copyright 1995 Matthias Ettrich - * Copyright 1995-2000 The LyX Team. + * Copyright 1995-2001 The LyX Team. * * ====================================================== */ @@ -14,15 +14,19 @@ #pragma implementation #endif -#include - +#include "support/lstrings.h" #include "font.h" #include "FontLoader.h" #include "lyxrc.h" #include "encoding.h" +#include "language.h" + +#include -// namespace { -static inline + +namespace { + +inline XFontStruct * getXFontstruct(LyXFont const & f) { return fontloader.load(f.family(), f.series(), @@ -30,12 +34,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) { @@ -55,7 +60,7 @@ int lyxfont::ascent(char c, LyXFont const & f) unsigned int uc = static_cast(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; @@ -68,7 +73,7 @@ int lyxfont::descent(char c, LyXFont const & f) unsigned int uc = static_cast(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; @@ -81,7 +86,7 @@ int lyxfont::lbearing(char c, LyXFont const & f) unsigned int uc = static_cast(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; @@ -94,38 +99,35 @@ int lyxfont::rbearing(char c, LyXFont const & f) unsigned int uc = static_cast(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 const * s, int n, LyXFont const & f) +int lyxfont::width(char const * s, size_t n, LyXFont const & f) { if (!lyxrc.use_gui) return n; if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) { - XChar2b * xs = new XChar2b[n]; + boost::scoped_array xs(new XChar2b[n]); Encoding const * encoding = f.language()->encoding(); - LyXFont const * font = &f; - if (f.family() == LyXFont::SYMBOL_FAMILY) { + LyXFont font(f); + if (f.isSymbolFont()) { #ifdef USE_UNICODE_FOR_SYMBOLS - LyXFont font2 = f; - font2.setFamily(LyXFont::ROMAN_FAMILY); - font2.setShape(LyXFont::UP_SHAPE); - font = &font2; + font.setFamily(LyXFont::ROMAN_FAMILY); + font.setShape(LyXFont::UP_SHAPE); #endif - encoding = &symbol_encoding; + encoding = encodings.symbol_encoding(); } - for (int i = 0; i < n; ++i) { + 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; + } + int result = width(xs.get(), n, font); return result; } @@ -134,14 +136,11 @@ int lyxfont::width(char const * s, int 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 (int i = 0; i < n; ++i) { - c = s[i]; - // when islower is a macro, the cast is needed (JMarc) - if (islower(static_cast(c))) { - c = toupper(c); + for (size_t i = 0; i < n; ++i) { + char const c = uppercase(s[i]); + if (c != s[i]) { result += ::XTextWidth(getXFontstruct(smallfont), &c, 1); } else { result += ::XTextWidth(getXFontstruct(f), &c, 1); @@ -154,7 +153,8 @@ 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.empty()) + return 0; if (s[0] == '-') return -width(s.substr(1, s.length() - 1), f); else @@ -167,22 +167,27 @@ 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}; + static XChar2b c; 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); - } + } } return result; } @@ -200,10 +205,33 @@ int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count) } -void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) +void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) { ::XSetFont(display, gc, getFontID(f)); } + +void lyxfont::rectText(string const & str, LyXFont const & font, + int & width, int & ascent, int & descent) +{ + static int const d = 2; + width = lyxfont::width(str, font) + d * 2 + 2; + ascent = lyxfont::maxAscent(font) + d; + descent = lyxfont::maxDescent(font) + d; +} + + + +void lyxfont::buttonText(string const & str, LyXFont const & font, + int & width, int & ascent, int & descent) +{ + static int const d = 3; + + width = lyxfont::width(str, font) + d * 2 + 2; + ascent = lyxfont::maxAscent(font) + d; + descent = lyxfont::maxDescent(font) + d; +} + + //} // end of namespace font //} // end of namespace lyx