begin conversion to ustring. add some more members.
authorDana Jansens <danakj@orodu.net>
Mon, 13 Jan 2003 01:08:54 +0000 (01:08 +0000)
committerDana Jansens <danakj@orodu.net>
Mon, 13 Jan 2003 01:08:54 +0000 (01:08 +0000)
configure.ac
otk/font.cc
otk/font.hh
otk/ustring.cc
otk/ustring.hh

index 97e3424..6b439cd 100644 (file)
@@ -32,7 +32,7 @@ AM_GNU_GETTEXT([external])
 
 PYTHON_DEVEL
                    
-AC_CHECK_HEADERS(ctype.h dirent.h fcntl.h libgen.h locale.h nl_types.h process.h signal.h stdarg.h stdio.h stdlib.h string.h time.h unistd.h sys/param.h sys/select.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/wait.h)
+AC_CHECK_HEADERS(ctype.h dirent.h fcntl.h libgen.h locale.h nl_types.h process.h signal.h stdarg.h stdint.h stdio.h stdlib.h string.h time.h unistd.h sys/param.h sys/select.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/wait.h)
 AC_HEADER_TIME
 # AC_TYPE_SIGNAL
 
index 0f1ad8c..b9cc707 100644 (file)
@@ -13,10 +13,6 @@ extern "C" {
 #include <iostream>
 #include <algorithm>
 
-using std::string;
-using std::cerr;
-using std::endl;
-
 #include "font.hh"
 #include "util.hh"
 #include "display.hh"
@@ -34,10 +30,10 @@ extern "C" {
 
 namespace otk {
 
-string      Font::_fallback_font = "fixed";
+std::string Font::_fallback_font = "fixed";
 bool        Font::_xft_init      = false;
 
-Font::Font(int screen_num, const string &fontstring,
+Font::Font(int screen_num, const std::string &fontstring,
              bool shadow, unsigned char offset, unsigned char tint)
   : _screen_num(screen_num),
     _fontstring(fontstring),
@@ -86,7 +82,7 @@ Font::~Font(void)
 
 
 void Font::drawString(XftDraw *d, int x, int y, const Color &color,
-                       const string &string, bool utf8) const
+                       const ustring &string) const
 {
   assert(d);
 
@@ -98,14 +94,9 @@ void Font::drawString(XftDraw *d, int x, int y, const Color &color,
     c.color.alpha = _tint | _tint << 8; // transparent shadow
     c.pixel = BlackPixel(Display::display, _screen_num);
 
-    if (utf8)
-      XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
-                        _xftfont->ascent + y + _offset,
-                        (FcChar8*)string.c_str(), string.size());
-    else
-      XftDrawString8(d, &c, _xftfont, x + _offset,
-                     _xftfont->ascent + y + _offset,
-                     (FcChar8*)string.c_str(), string.size());
+    XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
+                      _xftfont->ascent + y + _offset,
+                      (FcChar8*)string.c_str(), string.size());
   }
     
   XftColor c;
@@ -115,27 +106,19 @@ void Font::drawString(XftDraw *d, int x, int y, const Color &color,
   c.pixel = color.pixel();
   c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
 
-  if (utf8)
-    XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
-                      (FcChar8*)string.c_str(), string.size());
-  else
-    XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y,
-                   (FcChar8*)string.c_str(), string.size());
+  XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
+                    (FcChar8*)string.c_str(), string.size());
 
   return;
 }
 
 
-unsigned int Font::measureString(const string &string, bool utf8) const
+unsigned int Font::measureString(const ustring &string) const
 {
   XGlyphInfo info;
 
-  if (utf8)
-    XftTextExtentsUtf8(Display::display, _xftfont,
-                       (FcChar8*)string.c_str(), string.size(), &info);
-  else
-    XftTextExtents8(Display::display, _xftfont,
-                    (FcChar8*)string.c_str(), string.size(), &info);
+  XftTextExtentsUtf8(Display::display, _xftfont,
+                     (FcChar8*)string.c_str(), string.size(), &info);
 
   return info.xOff + (_shadow ? _offset : 0);
 }
index 9091b9b..dcff652 100644 (file)
@@ -2,6 +2,8 @@
 #ifndef   __font_hh
 #define   __font_hh
 
+#include "ustring.hh"
+
 extern "C" {
 #include <X11/Xlib.h>
 #define _XFT_NO_COMPAT_ // no Xft 1 API
@@ -9,7 +11,6 @@ extern "C" {
 }
 
 #include <assert.h>
-#include <string>
 
 namespace otk {
 
@@ -57,8 +58,7 @@ public:
   unsigned int height() const;
   unsigned int maxCharWidth() const;
 
-  unsigned int measureString(const std::string &string,
-                             bool utf8 = false) const;
+  unsigned int measureString(const ustring &string) const;
 
   //! Draws a string into an XftDraw object
   /*!
@@ -66,7 +66,7 @@ public:
     different screens, you WILL have unpredictable results! :)
   */
   void drawString(XftDraw *d, int x, int y, const Color &color,
-                  const std::string &string, bool utf8 = false) const;
+                  const ustring &string) const;
 };
 
 }
index 571f977..be16bb1 100644 (file)
@@ -41,4 +41,54 @@ ustring::ustring(const char* src)
 {
 }
 
+static ustring::size_type find_offset(const char *str, const char *pos)
+{
+  ustring::size_type offset = 0;
+
+  while (str < pos) {
+    str += g_utf8_skip[*str];
+    offset += g_utf8_skip[*str];
+  }
+
+  return offset;
+}
+
+ustring::size_type ustring::size() const
+{
+  const char *const pdata = _string.data();
+  return find_offset(pdata, pdata + _string.size());
+}
+
+ustring::size_type ustring::length() const
+{
+  const char *const pdata = _string.data();
+  return find_offset(pdata, pdata + _string.size());
+}
+
+ustring::size_type ustring::bytes() const
+{
+  return _string.size();
+}
+
+ustring::size_type ustring::capacity() const
+{
+  return _string.capacity();
+}
+
+ustring::size_type ustring::max_size() const
+{
+  return _string.max_size();
+}
+
+
+const char* ustring::data() const
+{
+  return _string.data();
+}
+
+const char* ustring::c_str() const
+{
+  return _string.c_str();
+}
+
 }
index 5d011cf..03f893d 100644 (file)
@@ -20,6 +20,14 @@ extern "C" {
 
 namespace otk {
 
+#ifdef HAVE_STDINT_H
+typedef uint32_t unichar;
+#else
+typedef u_int32_t unichar;
+#endif
+
+#ifndef DOXYGEN_IGNORE
+
 //! The number of bytes to skip to find the next character in the string
 const char g_utf8_skip[256] = {
   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
@@ -32,12 +40,6 @@ const char g_utf8_skip[256] = {
   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
 };
 
-#ifdef HAVE_STDINT_H
-typedef uint32_t unichar;
-#else
-typedef u_int32_t unichar;
-#endif
-
 //! The iterator type for ustring
 /*!
   Note this is not a random access iterator but a bidirectional one, since all
@@ -102,6 +104,8 @@ private:
   T _pos;
 };
 
+#endif // DOXYGEN_IGNORE
+
 //! This class provides a simple wrapper to a std::string that is encoded as
 //! UTF-8.
 /*!
@@ -140,6 +144,18 @@ public:
   ustring(const std::string& src);
   ustring::ustring(const char* src);
 
+  // sizes
+  
+  ustring::size_type size() const;
+  ustring::size_type length() const;
+  ustring::size_type bytes() const;
+  ustring::size_type capacity() const;
+  ustring::size_type max_size() const;
+
+  // internal data
+
+  const char* data()  const;
+  const char* c_str() const;
   
 };