diff options
Diffstat (limited to 'src/FbTk/XFontImp.cc')
-rw-r--r-- | src/FbTk/XFontImp.cc | 106 |
1 files changed, 55 insertions, 51 deletions
diff --git a/src/FbTk/XFontImp.cc b/src/FbTk/XFontImp.cc index 7398625..515b1e2 100644 --- a/src/FbTk/XFontImp.cc +++ b/src/FbTk/XFontImp.cc | |||
@@ -1,6 +1,6 @@ | |||
1 | // XFontImp.cc for FbTk fluxbox toolkit | 1 | // XFontImp.cc for FbTk fluxbox toolkit |
2 | // Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org) | 2 | // Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org) |
3 | // | 3 | // |
4 | // Permission is hereby granted, free of charge, to any person obtaining a | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
5 | // copy of this software and associated documentation files (the "Software"), | 5 | // copy of this software and associated documentation files (the "Software"), |
6 | // to deal in the Software without restriction, including without limitation | 6 | // to deal in the Software without restriction, including without limitation |
@@ -36,7 +36,11 @@ | |||
36 | #else | 36 | #else |
37 | #include <stdio.h> | 37 | #include <stdio.h> |
38 | #endif | 38 | #endif |
39 | using namespace std; | 39 | |
40 | using std::cerr; | ||
41 | using std::endl; | ||
42 | using std::string; | ||
43 | using std::nothrow; | ||
40 | 44 | ||
41 | namespace FbTk { | 45 | namespace FbTk { |
42 | 46 | ||
@@ -45,13 +49,13 @@ XFontImp::XFontImp(const char *fontname):m_fontstruct(0) { | |||
45 | m_rotfonts[i] = 0; | 49 | m_rotfonts[i] = 0; |
46 | 50 | ||
47 | if (fontname != 0) | 51 | if (fontname != 0) |
48 | load(fontname); | 52 | load(fontname); |
49 | } | 53 | } |
50 | 54 | ||
51 | XFontImp::~XFontImp() { | 55 | XFontImp::~XFontImp() { |
52 | if (m_fontstruct != 0) | 56 | if (m_fontstruct != 0) |
53 | XFreeFont(App::instance()->display(), m_fontstruct); | 57 | XFreeFont(App::instance()->display(), m_fontstruct); |
54 | 58 | ||
55 | for (int i = ROT0; i <= ROT270; ++i) | 59 | for (int i = ROT0; i <= ROT270; ++i) |
56 | if (m_rotfonts[i] != 0) | 60 | if (m_rotfonts[i] != 0) |
57 | freeRotFont(m_rotfonts[i]); | 61 | freeRotFont(m_rotfonts[i]); |
@@ -60,11 +64,11 @@ XFontImp::~XFontImp() { | |||
60 | int XFontImp::ascent() const { | 64 | int XFontImp::ascent() const { |
61 | if (m_fontstruct == 0) | 65 | if (m_fontstruct == 0) |
62 | return 0; | 66 | return 0; |
63 | 67 | ||
64 | return m_fontstruct->ascent; | 68 | return m_fontstruct->ascent; |
65 | } | 69 | } |
66 | 70 | ||
67 | bool XFontImp::load(const std::string &fontname) { | 71 | bool XFontImp::load(const string &fontname) { |
68 | 72 | ||
69 | XFontStruct *font = XLoadQueryFont(App::instance()->display(), fontname.c_str()); | 73 | XFontStruct *font = XLoadQueryFont(App::instance()->display(), fontname.c_str()); |
70 | if (font == 0) | 74 | if (font == 0) |
@@ -91,8 +95,8 @@ void XFontImp::drawText(const FbDrawable &w, int screen, GC gc, const FbString & | |||
91 | return; | 95 | return; |
92 | } | 96 | } |
93 | 97 | ||
94 | std::string localestr = text; | 98 | string localestr = text; |
95 | localestr.erase(len, std::string::npos); | 99 | localestr.erase(len, string::npos); |
96 | localestr = FbStringUtil::FbStrToLocale(localestr); | 100 | localestr = FbStringUtil::FbStrToLocale(localestr); |
97 | 101 | ||
98 | XSetFont(w.display(), gc, m_fontstruct->fid); | 102 | XSetFont(w.display(), gc, m_fontstruct->fid); |
@@ -103,8 +107,8 @@ unsigned int XFontImp::textWidth(const FbString &text, unsigned int size) const | |||
103 | if (text.empty() || m_fontstruct == 0) | 107 | if (text.empty() || m_fontstruct == 0) |
104 | return 0; | 108 | return 0; |
105 | 109 | ||
106 | std::string localestr = text; | 110 | string localestr = text; |
107 | localestr.erase(size, std::string::npos); | 111 | localestr.erase(size, string::npos); |
108 | localestr = FbStringUtil::FbStrToLocale(localestr); | 112 | localestr = FbStringUtil::FbStrToLocale(localestr); |
109 | 113 | ||
110 | return XTextWidth(m_fontstruct, localestr.data(), localestr.size()); | 114 | return XTextWidth(m_fontstruct, localestr.data(), localestr.size()); |
@@ -138,7 +142,7 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
138 | 142 | ||
139 | // create the depth 1 canvas bitmap | 143 | // create the depth 1 canvas bitmap |
140 | FbTk::FbPixmap canvas(rootwin, boxlen, boxlen, 1); | 144 | FbTk::FbPixmap canvas(rootwin, boxlen, boxlen, 1); |
141 | 145 | ||
142 | // create graphic context for our canvas | 146 | // create graphic context for our canvas |
143 | FbTk::GContext font_gc(canvas); | 147 | FbTk::GContext font_gc(canvas); |
144 | font_gc.setBackground(None); | 148 | font_gc.setBackground(None); |
@@ -152,22 +156,22 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
152 | cerr<<"RotFont: "<<_FBTK_CONSOLETEXT(Error, OutOfMemory, "Out of memory", "Something couldn't allocate memory")<<endl; | 156 | cerr<<"RotFont: "<<_FBTK_CONSOLETEXT(Error, OutOfMemory, "Out of memory", "Something couldn't allocate memory")<<endl; |
153 | return; | 157 | return; |
154 | } | 158 | } |
155 | 159 | ||
156 | // determine which characters are defined in font | 160 | // determine which characters are defined in font |
157 | min_char = m_fontstruct->min_char_or_byte2; | 161 | min_char = m_fontstruct->min_char_or_byte2; |
158 | max_char = m_fontstruct->max_char_or_byte2; | 162 | max_char = m_fontstruct->max_char_or_byte2; |
159 | 163 | ||
160 | // we only want printable chars | 164 | // we only want printable chars |
161 | if (min_char<32) | 165 | if (min_char<32) |
162 | min_char = 32; | 166 | min_char = 32; |
163 | if (max_char>126) | 167 | if (max_char>126) |
164 | max_char = 126; | 168 | max_char = 126; |
165 | 169 | ||
166 | /* some overall font data ... */ | 170 | /* some overall font data ... */ |
167 | rotfont->min_char = min_char; | 171 | rotfont->min_char = min_char; |
168 | rotfont->max_char = max_char; | 172 | rotfont->max_char = max_char; |
169 | rotfont->max_ascent = m_fontstruct->max_bounds.ascent; | 173 | rotfont->max_ascent = m_fontstruct->max_bounds.ascent; |
170 | rotfont->max_descent = m_fontstruct->max_bounds.descent; | 174 | rotfont->max_descent = m_fontstruct->max_bounds.descent; |
171 | rotfont->height = rotfont->max_ascent + rotfont->max_descent; | 175 | rotfont->height = rotfont->max_ascent + rotfont->max_descent; |
172 | 176 | ||
173 | // font needs rotation | 177 | // font needs rotation |
@@ -183,9 +187,9 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
183 | rotfont->per_char[ichar-32].width = m_fontstruct->per_char[index].width; | 187 | rotfont->per_char[ichar-32].width = m_fontstruct->per_char[index].width; |
184 | 188 | ||
185 | // some space chars have zero body, but a bitmap can't have | 189 | // some space chars have zero body, but a bitmap can't have |
186 | if (!ascent && !descent) | 190 | if (!ascent && !descent) |
187 | ascent = rotfont->per_char[ichar-32].ascent = 1; | 191 | ascent = rotfont->per_char[ichar-32].ascent = 1; |
188 | if (!lbearing && !rbearing) | 192 | if (!lbearing && !rbearing) |
189 | rbearing = rotfont->per_char[ichar-32].rbearing = 1; | 193 | rbearing = rotfont->per_char[ichar-32].rbearing = 1; |
190 | 194 | ||
191 | // glyph width and height when vertical | 195 | // glyph width and height when vertical |
@@ -193,9 +197,9 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
193 | vert_h = ascent + descent; | 197 | vert_h = ascent + descent; |
194 | 198 | ||
195 | // width in bytes | 199 | // width in bytes |
196 | vert_len = (vert_w-1)/8+1; | 200 | vert_len = (vert_w-1)/8+1; |
197 | 201 | ||
198 | font_gc.setForeground(None); | 202 | font_gc.setForeground(None); |
199 | canvas.fillRectangle(font_gc.gc(), | 203 | canvas.fillRectangle(font_gc.gc(), |
200 | 0, 0, | 204 | 0, 0, |
201 | boxlen, boxlen); | 205 | boxlen, boxlen); |
@@ -209,14 +213,14 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
209 | // reserve memory for first XImage | 213 | // reserve memory for first XImage |
210 | vertdata = (unsigned char *)calloc((unsigned)(vert_len * vert_h), 1); | 214 | vertdata = (unsigned char *)calloc((unsigned)(vert_len * vert_h), 1); |
211 | 215 | ||
212 | XImage *I1 = XCreateImage(dpy, DefaultVisual(dpy, screen), | 216 | XImage *I1 = XCreateImage(dpy, DefaultVisual(dpy, screen), |
213 | 1, XYBitmap, | 217 | 1, XYBitmap, |
214 | 0, (char *)vertdata, | 218 | 0, (char *)vertdata, |
215 | vert_w, vert_h, 8, 0); | 219 | vert_w, vert_h, 8, 0); |
216 | 220 | ||
217 | if (I1 == None) { | 221 | if (I1 == None) { |
218 | cerr << "RotFont: " << _FBTK_CONSOLETEXT(Error, CreateXImage, | 222 | cerr << "RotFont: " << _FBTK_CONSOLETEXT(Error, CreateXImage, |
219 | "Can't create XImage", | 223 | "Can't create XImage", |
220 | "XCreateImage failed for some reason") | 224 | "XCreateImage failed for some reason") |
221 | << "." << endl; | 225 | << "." << endl; |
222 | free(vertdata); | 226 | free(vertdata); |
@@ -228,19 +232,19 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
228 | I1->byte_order = I1->bitmap_bit_order = MSBFirst; | 232 | I1->byte_order = I1->bitmap_bit_order = MSBFirst; |
229 | 233 | ||
230 | // extract character from canvas | 234 | // extract character from canvas |
231 | XGetSubImage(dpy, canvas.drawable(), | 235 | XGetSubImage(dpy, canvas.drawable(), |
232 | boxlen/2, boxlen/2 - vert_h, | 236 | boxlen/2, boxlen/2 - vert_h, |
233 | vert_w, vert_h, 1, XYPixmap, I1, 0, 0); | 237 | vert_w, vert_h, 1, XYPixmap, I1, 0, 0); |
234 | 238 | ||
235 | I1->format = XYBitmap; | 239 | I1->format = XYBitmap; |
236 | 240 | ||
237 | // width, height of rotated character | 241 | // width, height of rotated character |
238 | if (orient == ROT180) { | 242 | if (orient == ROT180) { |
239 | bit_w = vert_w; | 243 | bit_w = vert_w; |
240 | bit_h = vert_h; | 244 | bit_h = vert_h; |
241 | } else { | 245 | } else { |
242 | bit_w = vert_h; | 246 | bit_w = vert_h; |
243 | bit_h = vert_w; | 247 | bit_h = vert_w; |
244 | } | 248 | } |
245 | 249 | ||
246 | // width in bytes | 250 | // width in bytes |
@@ -254,12 +258,12 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
254 | 258 | ||
255 | // create the image | 259 | // create the image |
256 | XImage *I2 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0, | 260 | XImage *I2 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0, |
257 | (char *)bitdata, bit_w, bit_h, 8, 0); | 261 | (char *)bitdata, bit_w, bit_h, 8, 0); |
258 | 262 | ||
259 | if (I2 == None) { | 263 | if (I2 == None) { |
260 | cerr << "XFontImp: " <<_FBTK_CONSOLETEXT(Error, CreateXImage, | 264 | cerr << "XFontImp: " <<_FBTK_CONSOLETEXT(Error, CreateXImage, |
261 | "Can't create XImage", | 265 | "Can't create XImage", |
262 | "XCreateImage failed for some reason") | 266 | "XCreateImage failed for some reason") |
263 | << "." << endl; | 267 | << "." << endl; |
264 | free(bitdata); | 268 | free(bitdata); |
265 | delete rotfont; | 269 | delete rotfont; |
@@ -280,9 +284,9 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
280 | val = vertdata[(vert_h-j-1)*vert_len + | 284 | val = vertdata[(vert_h-j-1)*vert_len + |
281 | (vert_w-i-1)/8] & (128>>((vert_w-i-1)%8)); | 285 | (vert_w-i-1)/8] & (128>>((vert_w-i-1)%8)); |
282 | } else { | 286 | } else { |
283 | val = vertdata[(vert_h-i-1)*vert_len + j/8] & | 287 | val = vertdata[(vert_h-i-1)*vert_len + j/8] & |
284 | (128>>(j%8)); | 288 | (128>>(j%8)); |
285 | } | 289 | } |
286 | if (val) { | 290 | if (val) { |
287 | bitdata[j*bit_len + i/8] = bitdata[j*bit_len + i/8] | | 291 | bitdata[j*bit_len + i/8] = bitdata[j*bit_len + i/8] | |
288 | (128>>(i%8)); | 292 | (128>>(i%8)); |
@@ -290,12 +294,12 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
290 | } | 294 | } |
291 | } | 295 | } |
292 | 296 | ||
293 | // create this character's bitmap | 297 | // create this character's bitmap |
294 | rotfont->per_char[ichar-32].glyph.bm = | 298 | rotfont->per_char[ichar-32].glyph.bm = |
295 | XCreatePixmap(dpy, rootwin, bit_w, bit_h, 1); | 299 | XCreatePixmap(dpy, rootwin, bit_w, bit_h, 1); |
296 | 300 | ||
297 | // put the image into the bitmap | 301 | // put the image into the bitmap |
298 | XPutImage(dpy, rotfont->per_char[ichar-32].glyph.bm, | 302 | XPutImage(dpy, rotfont->per_char[ichar-32].glyph.bm, |
299 | font_gc.gc(), I2, 0, 0, 0, 0, bit_w, bit_h); | 303 | font_gc.gc(), I2, 0, 0, 0, 0, bit_w, bit_h); |
300 | 304 | ||
301 | // free the image and data | 305 | // free the image and data |
@@ -307,7 +311,7 @@ void XFontImp::rotate(FbTk::Orientation orient) { | |||
307 | 311 | ||
308 | void XFontImp::freeRotFont(XRotFontStruct *rotfont) { | 312 | void XFontImp::freeRotFont(XRotFontStruct *rotfont) { |
309 | // loop through each character and free its pixmap | 313 | // loop through each character and free its pixmap |
310 | for (int ichar = rotfont->min_char - 32; | 314 | for (int ichar = rotfont->min_char - 32; |
311 | ichar <= rotfont->max_char - 32; ++ichar) { | 315 | ichar <= rotfont->max_char - 32; ++ichar) { |
312 | XFreePixmap(App::instance()->display(), rotfont->per_char[ichar].glyph.bm); | 316 | XFreePixmap(App::instance()->display(), rotfont->per_char[ichar].glyph.bm); |
313 | } | 317 | } |
@@ -316,7 +320,7 @@ void XFontImp::freeRotFont(XRotFontStruct *rotfont) { | |||
316 | rotfont = 0; | 320 | rotfont = 0; |
317 | } | 321 | } |
318 | 322 | ||
319 | void XFontImp::drawRotText(Drawable w, int screen, GC gc, const FbString &text, size_t len, int x, int y, FbTk::Orientation orient) const { | 323 | void XFontImp::drawRotText(Drawable w, int screen, GC gc, const FbString &text, size_t len, int x, int y, FbTk::Orientation orient) const { |
320 | 324 | ||
321 | Display *dpy = App::instance()->display(); | 325 | Display *dpy = App::instance()->display(); |
322 | static GC my_gc = 0; | 326 | static GC my_gc = 0; |
@@ -335,8 +339,8 @@ void XFontImp::drawRotText(Drawable w, int screen, GC gc, const FbString &text, | |||
335 | // vertical or upside down | 339 | // vertical or upside down |
336 | 340 | ||
337 | XSetFillStyle(dpy, my_gc, FillStippled); | 341 | XSetFillStyle(dpy, my_gc, FillStippled); |
338 | std::string localestr = text; | 342 | string localestr = text; |
339 | localestr.erase(len, std::string::npos); | 343 | localestr.erase(len, string::npos); |
340 | localestr = FbStringUtil::FbStrToLocale(localestr); | 344 | localestr = FbStringUtil::FbStrToLocale(localestr); |
341 | const char *ctext = localestr.data(); | 345 | const char *ctext = localestr.data(); |
342 | len = localestr.size(); | 346 | len = localestr.size(); |
@@ -351,30 +355,30 @@ void XFontImp::drawRotText(Drawable w, int screen, GC gc, const FbString &text, | |||
351 | // suitable offset | 355 | // suitable offset |
352 | if (orient == ROT270) { | 356 | if (orient == ROT270) { |
353 | xp = x-rotfont->per_char[ichar].ascent; | 357 | xp = x-rotfont->per_char[ichar].ascent; |
354 | yp = y-rotfont->per_char[ichar].rbearing; | 358 | yp = y-rotfont->per_char[ichar].rbearing; |
355 | } else if (orient == ROT180) { | 359 | } else if (orient == ROT180) { |
356 | xp = x-rotfont->per_char[ichar].rbearing; | 360 | xp = x-rotfont->per_char[ichar].rbearing; |
357 | yp = y-rotfont->per_char[ichar].descent+1; | 361 | yp = y-rotfont->per_char[ichar].descent+1; |
358 | } else { // ROT90 | 362 | } else { // ROT90 |
359 | xp = x-rotfont->per_char[ichar].descent; | 363 | xp = x-rotfont->per_char[ichar].descent; |
360 | yp = y+rotfont->per_char[ichar].lbearing; | 364 | yp = y+rotfont->per_char[ichar].lbearing; |
361 | } | 365 | } |
362 | 366 | ||
363 | // draw the glyph | 367 | // draw the glyph |
364 | XSetStipple(dpy, my_gc, rotfont->per_char[ichar].glyph.bm); | 368 | XSetStipple(dpy, my_gc, rotfont->per_char[ichar].glyph.bm); |
365 | 369 | ||
366 | XSetTSOrigin(dpy, my_gc, xp, yp); | 370 | XSetTSOrigin(dpy, my_gc, xp, yp); |
367 | 371 | ||
368 | XFillRectangle(dpy, w, my_gc, xp, yp, | 372 | XFillRectangle(dpy, w, my_gc, xp, yp, |
369 | rotfont->per_char[ichar].glyph.bit_w, | 373 | rotfont->per_char[ichar].glyph.bit_w, |
370 | rotfont->per_char[ichar].glyph.bit_h); | 374 | rotfont->per_char[ichar].glyph.bit_h); |
371 | 375 | ||
372 | // advance position | 376 | // advance position |
373 | if (orient == ROT270) | 377 | if (orient == ROT270) |
374 | y -= rotfont->per_char[ichar].width; | 378 | y -= rotfont->per_char[ichar].width; |
375 | else if (orient == ROT180) | 379 | else if (orient == ROT180) |
376 | x -= rotfont->per_char[ichar].width; | 380 | x -= rotfont->per_char[ichar].width; |
377 | else | 381 | else |
378 | y += rotfont->per_char[ichar].width; | 382 | y += rotfont->per_char[ichar].width; |
379 | } | 383 | } |
380 | } | 384 | } |