From 1d75c017cdbbc569868a0b5e6458c7f857aec33f Mon Sep 17 00:00:00 2001 From: fluxgen Date: Sun, 13 Oct 2002 22:27:21 +0000 Subject: moved system font specific stuff out to font implementations files --- src/Font.cc | 271 ++++++++++++++++-------------------------------------------- src/Font.hh | 59 +++++-------- 2 files changed, 91 insertions(+), 239 deletions(-) diff --git a/src/Font.cc b/src/Font.cc index 5203ccb..627a825 100644 --- a/src/Font.cc +++ b/src/Font.cc @@ -19,10 +19,20 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: Font.cc,v 1.10 2002/09/03 12:05:01 fluxgen Exp $ +//$Id: Font.cc,v 1.11 2002/10/13 22:27:21 fluxgen Exp $ #include "Font.hh" +#include "FontImp.hh" + +// for antialias +#ifdef USE_XFT +#include "XftFontImp.hh" +#endif // USE_XFT + +// standard font system +#include "XFontImp.hh" +#include "XmbFontImp.hh" #ifdef HAVE_CONFIG_H #include "../config.h" @@ -50,231 +60,92 @@ #include "StringUtil.hh" -namespace FbTk -{ +namespace FbTk { -bool Font::m_multibyte = false; //TODO: fix multibyte +bool Font::m_multibyte = false; +bool Font::m_utf8mode = false; -Font::Font(Display *display, const char *name):m_loaded(false), -m_display(display) { - m_font.fontstruct = 0; - m_font.set_extents = 0; - m_font.set = 0; +Font::Font(const char *name, bool antialias) { // MB_CUR_MAX returns the size of a char in the current locale - if (MB_CUR_MAX > 1) - m_multibyte = true; + if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte + m_multibyte = true; + + char *s; // temporary string for enviroment variable + // check for utf-8 mode + if (((s = getenv("LC_ALL")) && *s) || + ((s = getenv("LC_CTYPE")) && *s) || + ((s = getenv("LANG")) && *s)) { + if (strstr(s, "UTF-8")) + m_utf8mode = true; + } + + // create the right font implementation +#ifdef USE_XFT + if (antialias) { + m_fontimp = new XftFontImp(); + } +#endif //USE_XFT + // if we didn't create a Xft font then create basic font + if (m_fontimp.get() == 0) { + if (m_multibyte || m_utf8mode) + m_fontimp = std::auto_ptr(new XmbFontImp(0, m_utf8mode)); + else // basic font implementation + m_fontimp = std::auto_ptr(new XFontImp(0)); + } - if (name!=0) { + if (name != 0) { load(name); } } Font::~Font() { - freeFont(); -} - -bool Font::load(const char *name) { - if (m_multibyte) { - XFontSet set = createFontSet(m_display, name); - if (!set) - return false; - freeFont(); - - m_font.set = set; - m_font.set_extents = XExtentsOfFontSet(m_font.set); - } else { - XFontStruct *font = XLoadQueryFont(m_display, name); - if (font==0) - return false; - freeFont(); //free old font - m_font.fontstruct = font; //set new font - } - - m_loaded = true; //mark the font loaded - - return true; } -bool Font::loadFromDatabase(XrmDatabase &database, const char *rname, const char *rclass) { - assert(rname); - assert(rclass); +void Font::setAntialias(bool flag) { - XrmValue value; - char *value_type; +#ifdef USE_XFT + bool is_antialias = typeid(m_fontimp) == typeid(XftFontImp); - //This should probably be moved to a Database class so we can keep - //track of database init - - if (XrmGetResource(database, rname, rclass, &value_type, &value)) { - #ifdef DEBUG - std::cerr<<__FILE__<<"("<<__LINE__<<"): Load font:"<(new XftFontImp(m_fontstr.c_str())); + } else if (!flag && is_antialias) +#endif // USE_XFT + { + if (m_multibyte || m_utf8mode) + m_fontimp = std::auto_ptr(new XmbFontImp(m_fontstr.c_str(), m_utf8mode)); + else + m_fontimp = std::auto_ptr(new XFontImp(m_fontstr.c_str())); } - - return false; - -} -unsigned int Font::textWidth(const char *text, unsigned int size) const { - if (text==0) - return 0; - if (multibyte()) { - XRectangle ink, logical; - XmbTextExtents(m_font.set, text, size, - &ink, &logical); - return logical.width; - } else { - assert(m_font.fontstruct); - return XTextWidth(m_font.fontstruct, - text, size); - } - - return 0; -} - -unsigned int Font::height() const { - if (!isLoaded()) - return 0; - - if (multibyte() && fontSetExtents()) - return fontSetExtents()->max_ink_extent.height; - if (fontStruct()) - return fontStruct()->ascent + fontStruct()->descent; - - return 0; } -XFontSet Font::createFontSet(Display *display, const char *fontname) { - XFontSet fs; - const int FONT_ELEMENT_SIZE=50; - char **missing, *def = "-"; - int nmissing, pixel_size = 0, buf_size = 0; - char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE]; - - fs = XCreateFontSet(display, - fontname, &missing, &nmissing, &def); - if (fs && (! nmissing)) return fs; - -#ifdef HAVE_SETLOCALE - if (! fs) { - if (nmissing) XFreeStringList(missing); - - setlocale(LC_CTYPE, "C"); - fs = XCreateFontSet(display, fontname, - &missing, &nmissing, &def); - setlocale(LC_CTYPE, ""); - } -#endif // HAVE_SETLOCALE - - if (fs) { - XFontStruct **fontstructs; - char **fontnames; - XFontsOfFontSet(fs, &fontstructs, &fontnames); - fontname = fontnames[0]; +bool Font::load(const char *name) { + if (name == 0) + return false; + bool ret_val = m_fontimp->load(name); + if (ret_val && name == 0) { //prevent from having a bad fontimp + m_fontstr = name; // if the load really succeded then set font string + } else { + m_fontstr = ""; } - getFontElement(fontname, weight, FONT_ELEMENT_SIZE, - "-medium-", "-bold-", "-demibold-", "-regular-", 0); - getFontElement(fontname, slant, FONT_ELEMENT_SIZE, - "-r-", "-i-", "-o-", "-ri-", "-ro-", 0); - getFontSize(fontname, &pixel_size); - - if (! strcmp(weight, "*")) - std::strncpy(weight, "medium", FONT_ELEMENT_SIZE); - if (! strcmp(slant, "*")) - std::strncpy(slant, "r", FONT_ELEMENT_SIZE); - if (pixel_size < 3) - pixel_size = 3; - else if (pixel_size > 97) - pixel_size = 97; - - buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64; - char *pattern2 = new char[buf_size]; - snprintf(pattern2, buf_size - 1, - "%s," - "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*," - "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*", - fontname, weight, slant, pixel_size, pixel_size); - fontname = pattern2; - - if (nmissing) - XFreeStringList(missing); - if (fs) - XFreeFontSet(display, fs); - - fs = XCreateFontSet(display, fontname, - &missing, &nmissing, &def); - delete [] pattern2; - - return fs; + return ret_val; } -const char *Font::getFontElement(const char *pattern, char *buf, int bufsiz, ...) { - const char *p, *v; - char *p2; - va_list va; - - va_start(va, bufsiz); - buf[bufsiz-1] = 0; - buf[bufsiz-2] = '*'; - while((v = va_arg(va, char *)) != 0) { - p = StringUtil::strcasestr(pattern, v); - if (p) { - std::strncpy(buf, p+1, bufsiz-2); - p2 = strchr(buf, '-'); - if (p2) *p2=0; - va_end(va); - return p; - } - } - va_end(va); - std::strncpy(buf, "*", bufsiz); - return 0; +unsigned int Font::textWidth(const char *text, unsigned int size) const { + return m_fontimp->textWidth(text, size); } -const char *Font::getFontSize(const char *pattern, int *size) { - const char *p; - const char *p2=0; - int n=0; - - for (p=pattern; 1; p++) { - if (!*p) { - if (p2!=0 && n>1 && n<72) { - *size = n; return p2+1; - } else { - *size = 16; return 0; - } - } else if (*p=='-') { - if (n>1 && n<72 && p2!=0) { - *size = n; - return p2+1; - } - p2=p; n=0; - } else if (*p>='0' && *p<='9' && p2!=0) { - n *= 10; - n += *p-'0'; - } else { - p2=0; n=0; - } - } +unsigned int Font::height() const { + return m_fontimp->height(); } -void Font::freeFont() { - //free memory - if (m_font.fontstruct!=0) - XFreeFont(m_display, m_font.fontstruct); - if (m_font.set) - XFreeFontSet(m_display, m_font.set); - - //clear struct - m_font.fontstruct = 0; - m_font.set = 0; - m_font.set_extents = 0; - - m_loaded = false; //mark the font not loaded -} +void Font::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { + if (text == 0 || len == 0) + return; + m_fontimp->drawText(w, screen, gc, text, len, x, y); +} }; diff --git a/src/Font.hh b/src/Font.hh index 66cd6cf..0fa1b0e 100644 --- a/src/Font.hh +++ b/src/Font.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: Font.hh,v 1.4 2002/08/04 15:55:13 fluxgen Exp $ +//$Id: Font.hh,v 1.5 2002/10/13 22:24:14 fluxgen Exp $ #ifndef FBTK_FONT_HH #define FBTK_FONT_HH @@ -27,16 +27,19 @@ #include #include -namespace FbTk -{ +#include +#include + +namespace FbTk { + +class FontImp; /** - Handles loading of font. + Handles the client to fontimp bridge. */ -class Font -{ +class Font { public: - Font(Display *display, const char *name=0); + Font(const char *name=0, bool antialias = false); virtual ~Font(); /** Load a font @@ -44,23 +47,12 @@ public: loaded font */ bool load(const char *name); - /** - Loads a font from database - @return true on success, else false and it'll fall back on the last - loaded font - @see load(const char *name) - */ - bool loadFromDatabase(XrmDatabase &database, const char *rname, const char *rclass); - /// @return true if a font is loaded, else false - inline bool isLoaded() const { return m_loaded; } - /// @return XFontStruct of font, note: can be 0 - inline const XFontStruct *fontStruct() const { return m_font.fontstruct; } - /// @return XFontSet of font, note: check isLoaded - inline const XFontSet &fontSet() const { return m_font.set; } - /// @return XFontSetExtents of font, note: can be 0 - inline const XFontSetExtents *fontSetExtents() const { return m_font.set_extents; } + /// @return true if multibyte is enabled, else false - static inline bool multibyte() { return m_multibyte; } + static bool multibyte() { return m_multibyte; } + /// @return true if utf-8 mode is enabled, else false + static bool utf8() { return m_utf8mode; } + void setAntialias(bool flag); /** @param text text to check size @param size length of text in bytes @@ -68,24 +60,13 @@ public: */ unsigned int textWidth(const char *text, unsigned int size) const; unsigned int height() const; - /// @return display connection - Display *display() const { return m_display; } + void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; private: - void freeFont(); - static XFontSet createFontSet(Display *display, const char *fontname); - static const char *getFontSize(const char *pattern, int *size); - static const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...); - - struct FontType - { - XFontSet set; - XFontSetExtents *set_extents; - XFontStruct *fontstruct; - } m_font; - - bool m_loaded; + + std::auto_ptr m_fontimp; + std::string m_fontstr; static bool m_multibyte; - Display *m_display; + static bool m_utf8mode; }; }; //end namespace FbTk -- cgit v0.11.2