diff options
Diffstat (limited to 'src/FbTk/Font.cc')
-rw-r--r-- | src/FbTk/Font.cc | 57 |
1 files changed, 25 insertions, 32 deletions
diff --git a/src/FbTk/Font.cc b/src/FbTk/Font.cc index 815cf0d..b79cdcc 100644 --- a/src/FbTk/Font.cc +++ b/src/FbTk/Font.cc | |||
@@ -19,7 +19,7 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | //$Id: Font.cc,v 1.10 2004/08/14 09:33:09 fluxgen Exp $ | 22 | //$Id: Font.cc,v 1.11 2004/08/18 16:30:33 rathnor Exp $ |
23 | 23 | ||
24 | 24 | ||
25 | #include "StringUtil.hh" | 25 | #include "StringUtil.hh" |
@@ -59,7 +59,7 @@ | |||
59 | #include <cstdlib> | 59 | #include <cstdlib> |
60 | #include <list> | 60 | #include <list> |
61 | #include <typeinfo> | 61 | #include <typeinfo> |
62 | 62 | #include <langinfo.h> | |
63 | 63 | ||
64 | #ifdef HAVE_SSTREAM | 64 | #ifdef HAVE_SSTREAM |
65 | #include <sstream> | 65 | #include <sstream> |
@@ -97,12 +97,12 @@ char* recode(iconv_t cd, | |||
97 | if(strlen(msg) == 0) | 97 | if(strlen(msg) == 0) |
98 | return 0; | 98 | return 0; |
99 | 99 | ||
100 | |||
101 | size_t inbytesleft = strlen(msg); | 100 | size_t inbytesleft = strlen(msg); |
102 | size_t outbytesleft = 4*inbytesleft; | 101 | size_t outbytesleft = 4*inbytesleft; |
103 | char *new_msg = new char[outbytesleft]; | 102 | char *new_msg = new char[outbytesleft]; |
104 | char *new_msg_ptr = new_msg; | 103 | char *new_msg_ptr = new_msg; |
105 | char *msg_ptr = strdup(msg); | 104 | char *msg_ptr = strdup(msg); |
105 | char *orig_msg_ptr = msg_ptr; // msg_ptr modified in iconv call | ||
106 | 106 | ||
107 | if (iconv(cd, &msg_ptr, &inbytesleft, &new_msg, &outbytesleft) == -1) { | 107 | if (iconv(cd, &msg_ptr, &inbytesleft, &new_msg, &outbytesleft) == -1) { |
108 | // iconv can fail for three reasons | 108 | // iconv can fail for three reasons |
@@ -110,20 +110,20 @@ char* recode(iconv_t cd, | |||
110 | // 2) An incomplete multibyte sequence | 110 | // 2) An incomplete multibyte sequence |
111 | // 3) The output buffer has no more room for the next converted character. | 111 | // 3) The output buffer has no more room for the next converted character. |
112 | // So we the delete new message and return original message | 112 | // So we the delete new message and return original message |
113 | delete new_msg; | 113 | delete new_msg_ptr; |
114 | free(msg_ptr); | 114 | free(orig_msg_ptr); |
115 | return 0; | 115 | return 0; |
116 | } | 116 | } |
117 | free(msg_ptr); | 117 | free(orig_msg_ptr); |
118 | 118 | ||
119 | *new_msg_ptr = '\0'; | 119 | *new_msg = '\0'; |
120 | 120 | ||
121 | if(inbytesleft != 0) { | 121 | if(inbytesleft != 0) { |
122 | delete new_msg; | 122 | delete new_msg_ptr; |
123 | return 0; | 123 | return 0; |
124 | } | 124 | } |
125 | 125 | ||
126 | return new_msg; | 126 | return new_msg_ptr; |
127 | } | 127 | } |
128 | 128 | ||
129 | 129 | ||
@@ -200,6 +200,11 @@ namespace FbTk { | |||
200 | bool Font::m_multibyte = false; | 200 | bool Font::m_multibyte = false; |
201 | bool Font::m_utf8mode = false; | 201 | bool Font::m_utf8mode = false; |
202 | 202 | ||
203 | // some initialisation for using fonts | ||
204 | void fontInit() { | ||
205 | setlocale(LC_CTYPE, ""); | ||
206 | } | ||
207 | |||
203 | Font::Font(const char *name, bool antialias): | 208 | Font::Font(const char *name, bool antialias): |
204 | m_fontimp(0), | 209 | m_fontimp(0), |
205 | m_antialias(false), m_rotated(false), | 210 | m_antialias(false), m_rotated(false), |
@@ -212,34 +217,22 @@ Font::Font(const char *name, bool antialias): | |||
212 | if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte | 217 | if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte |
213 | m_multibyte = true; | 218 | m_multibyte = true; |
214 | 219 | ||
215 | char *envstr; // temporary string for enviroment variable | ||
216 | // check for utf-8 mode | 220 | // check for utf-8 mode |
217 | if (((envstr = getenv("LC_ALL")) && *envstr) || | 221 | char *locale_codeset = nl_langinfo(CODESET); |
218 | ((envstr = getenv("LC_CTYPE")) && *envstr) || | ||
219 | ((envstr = getenv("LANG")) && *envstr)) { | ||
220 | if (strstr(envstr, "UTF-8")) | ||
221 | m_utf8mode = true; | ||
222 | m_locale = envstr; | ||
223 | int index = m_locale.find("."); | ||
224 | if (index != 0) | ||
225 | m_locale = m_locale.substr(index + 1); | ||
226 | else | ||
227 | m_locale = "UTF-8"; | ||
228 | } | ||
229 | 222 | ||
230 | if (m_locale.empty()) | 223 | if (strcmp("UTF-8", locale_codeset) == 0) { |
231 | m_locale = "C"; | 224 | m_utf8mode = true; |
225 | } else { | ||
226 | // if locale isn't UTF-8 we try to | ||
227 | // create a iconv pointer so we can | ||
228 | // convert non utf-8 strings to utf-8 | ||
232 | 229 | ||
233 | // if locale isn't UTF-8 we try to | ||
234 | // create a iconv pointer so we can | ||
235 | // convert non utf-8 strings to utf-8 | ||
236 | if (m_locale != "UTF-8") { | ||
237 | #ifdef DEBUG | 230 | #ifdef DEBUG |
238 | cerr<<"FbTk::Font: m_locale = "<<m_locale<<endl; | 231 | cerr<<"FbTk::Font: check UTF-8 convert for codeset = "<<locale_codeset<<endl; |
239 | #endif // DEBUG | 232 | #endif // DEBUG |
240 | m_iconv = iconv_open(m_locale.c_str(), "UTF-8"); | 233 | m_iconv = iconv_open("UTF-8", locale_codeset); |
241 | if(m_iconv == (iconv_t)(-1)) { | 234 | if(m_iconv == (iconv_t)(-1)) { |
242 | cerr<<"FbTk::Font: code error: from "<<m_locale<<" to: UTF-8"<<endl; | 235 | cerr<<"FbTk::Font: code error: from "<<locale_codeset<<" to: UTF-8"<<endl; |
243 | // if we failed with iconv then we can't convert | 236 | // if we failed with iconv then we can't convert |
244 | // the strings to utf-8, so we disable utf8 mode | 237 | // the strings to utf-8, so we disable utf8 mode |
245 | m_utf8mode = false; | 238 | m_utf8mode = false; |