comparison font/font_peer_x11.cc @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents 5f548e5957a8
children 4416cfac86ae
comparison
equal deleted inserted replaced
51:cbb301016a4e 52:15a18fbe6f21
38 38
39 #include <X11/Xlib.h> 39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h> 40 #include <X11/Xutil.h>
41 #include <X11/extensions/XShm.h> 41 #include <X11/extensions/XShm.h>
42 #include <stdio.h> 42 #include <stdio.h>
43 #include<iostream> 43 #include <iostream>
44 44
45 #include<vector> 45 #include <vector>
46 #include<map> 46 #include <map>
47 //#include<iostream> 47
48 #include<sstream> 48 #include <sstream>
49 #include <string> 49 #include <string>
50 #include <stdexcept> 50 #include <stdexcept>
51 51
52 using namespace std; 52 using namespace std;
53 53
84 */ 84 */
85 FontInfo::FontInfo(Display* display, const char* fontname_orig) { 85 FontInfo::FontInfo(Display* display, const char* fontname_orig) {
86 /* フォントの大きさ関係の情報を消去 */ 86 /* フォントの大きさ関係の情報を消去 */
87 int i; 87 int i;
88 char* fontname = new char[strlen(fontname_orig)+50]; 88 char* fontname = new char[strlen(fontname_orig)+50];
89 int minus_count = 0; bool is_skip = false; int fc = 0; 89 int minus_count = 0;
90 bool is_skip = false;
91 int fc = 0;
90 for (i=0; fontname_orig[i]!=0; i++) { 92 for (i=0; fontname_orig[i]!=0; i++) {
91 if (fontname_orig[i] == '-') { 93 if (fontname_orig[i] == '-') {
92 minus_count++; 94 minus_count++;
93 if (minus_count >= 7 && minus_count <= 12) { 95 if (minus_count >= 7 && minus_count <= 12) {
94 fontname[fc++] = '-'; 96 fontname[fc++] = '-';
105 int count; 107 int count;
106 char** fontnamelist = XListFonts(display, fontname, 100, &count); 108 char** fontnamelist = XListFonts(display, fontname, 100, &count);
107 for (i=0; i<count; i++) { 109 for (i=0; i<count; i++) {
108 char* curfont = fontnamelist[i]; 110 char* curfont = fontnamelist[i];
109 /* fontname から pixel size 情報を得る */ 111 /* fontname から pixel size 情報を得る */
110 int j; int minus_count = 0; 112 int j;
113 int minus_count = 0;
111 for (j=0; curfont[j] != 0; j++) { 114 for (j=0; curfont[j] != 0; j++) {
112 if (curfont[j] == '-') minus_count++; 115 if (curfont[j] == '-') minus_count++;
113 if (minus_count == 7) { 116 if (minus_count == 7) {
114 int pixsize = atoi(curfont+j+1); 117 int pixsize = atoi(curfont+j+1);
115 if (fontlist.find(pixsize) == fontlist.end()) { 118 if (fontlist.find(pixsize) == fontlist.end()) {
140 ** pixsize は与えられた pixsize にする 143 ** pixsize は与えられた pixsize にする
141 */ 144 */
142 string basefont_s = fontlist[0]; 145 string basefont_s = fontlist[0];
143 const char* basefont = basefont_s.c_str(); 146 const char* basefont = basefont_s.c_str();
144 char* retfont = new char[strlen(basefont)+50]; 147 char* retfont = new char[strlen(basefont)+50];
145 int minus_count = 0; int rc = 0; bool is_skip = false; 148 int minus_count = 0;
149 int rc = 0;
150 bool is_skip = false;
146 for (i=0; basefont[i] != 0; i++) { 151 for (i=0; basefont[i] != 0; i++) {
147 if (basefont[i] == '-') { 152 if (basefont[i] == '-') {
148 minus_count++; 153 minus_count++;
149 is_skip = false; 154 is_skip = false;
150 if (minus_count == 7) { 155 if (minus_count == 7) {
193 std::vector<FontInfo*>::iterator it; 198 std::vector<FontInfo*>::iterator it;
194 for (it=fontlist.begin(); it != fontlist.end(); it++) { 199 for (it=fontlist.begin(); it != fontlist.end(); it++) {
195 if (it != fontlist.begin()) s << ","; 200 if (it != fontlist.begin()) s << ",";
196 s << (*it)->Search(pixsize); 201 s << (*it)->Search(pixsize);
197 } 202 }
198 s<<ends; 203 s << ends;
199 return string(s.str()); 204 return string(s.str());
200 } 205 }
201 206
202 /**************************************** 207 /****************************************
203 ** 208 **
204 ** FontPeerX11 209 ** FontPeerX11
205 */ 210 */
206 Display* PeerX11::display = 0; 211 Display* PeerX11::display = NULL;
207 void PeerX11::InitDisplay(Display* _d) { 212 void PeerX11::InitDisplay(Display* _d) {
208 /* d = ((GdkWindowPrivate*)(top_window.gdkobj()))->xdisplay; */ 213 /* d = ((GdkWindowPrivate*)(top_window.gdkobj()))->xdisplay; */
209 display = _d; 214 display = _d;
210 } 215 }
211 216
212 void PeerX11::OpenDisplay(void) { 217 void PeerX11::OpenDisplay(void) {
213 if (display) return; 218 if (display != NULL) return;
214 219
215 const char* display_name = getenv("DISPLAY"); 220 const char* display_name = getenv("DISPLAY");
216 if (display_name == 0) display_name = ":0"; 221 if (display_name == NULL) display_name = ":0";
217 222
218 display = XOpenDisplay(display_name); 223 display = XOpenDisplay(display_name);
219 224
220 if (display == 0) { 225 if (display == NULL) {
221 string err = string("XKFont::PeerX11:OpenDisplay() : Cannot open X display ") + display_name; 226 string err = string("XKFont::PeerX11:OpenDisplay() : Cannot open X display ") + display_name;
222 throw std::invalid_argument(err); 227 throw std::invalid_argument(err);
223 } 228 }
224 } 229 }
225 230
252 mask = visual->green_mask; 257 mask = visual->green_mask;
253 while(mask & 0x01) { shift++; mask >>= 1; } 258 while(mask & 0x01) { shift++; mask >>= 1; }
254 259
255 int tablesize = mask+1; 260 int tablesize = mask+1;
256 colortable = new int[tablesize]; 261 colortable = new int[tablesize];
257 int i; for (i=0; i< tablesize; i++) { 262 int i;
263 for (i=0; i< tablesize; i++) {
258 colortable[i] = i*255/tablesize; 264 colortable[i] = i*255/tablesize;
259 } 265 }
260 XSupportsLocale(); 266
267 XSupportsLocale(); //FIXME: er... yes?
261 268
262 /* font 読み込み */ 269 /* font 読み込み */
263 FontSetInfo fsinfo(display,fontname); 270 FontSetInfo fsinfo(display,fontname);
264 string fontset_name = fsinfo.Search(fontsize); 271 string fontset_name = fsinfo.Search(fontsize);
265 char** missing_cl; int missing_cc; char* def_s; 272 char** missing_cl;
273 int missing_cc;
274 char* def_s;
266 printf("fontset %s\n",fontset_name.c_str()); 275 printf("fontset %s\n",fontset_name.c_str());
267 fontset = XCreateFontSet(display, fontset_name.c_str(), &missing_cl, &missing_cc, &def_s); 276 fontset = XCreateFontSet(display, fontset_name.c_str(), &missing_cl, &missing_cc, &def_s);
268 277
269 if (fontset == 0) { 278 if (fontset == 0) {
270 delete[] colortable; 279 delete[] colortable;
296 for (i=0; i<num_fonts; i++) { 305 for (i=0; i<num_fonts; i++) {
297 ascent = MAX(ascent, font_structs[i]->ascent); 306 ascent = MAX(ascent, font_structs[i]->ascent);
298 } 307 }
299 308
300 /* 描画用の pixmap を作成 */ 309 /* 描画用の pixmap を作成 */
301 XGCValues gc_values; unsigned int gc_values_mask; 310 XGCValues gc_values;
311 unsigned int gc_values_mask;
302 gc_values.function = GXcopy; 312 gc_values.function = GXcopy;
303 gc_values.fill_style = FillSolid; 313 gc_values.fill_style = FillSolid;
304 gc_values.arc_mode = ArcPieSlice; 314 gc_values.arc_mode = ArcPieSlice;
305 gc_values.subwindow_mode = ClipByChildren; 315 gc_values.subwindow_mode = ClipByChildren;
306 gc_values.graphics_exposures = False; 316 gc_values.graphics_exposures = False;
316 use_shm = false; 326 use_shm = false;
317 if (XShmQueryExtension(display) == True) { 327 if (XShmQueryExtension(display) == True) {
318 x_shm_info.shmid = -1; 328 x_shm_info.shmid = -1;
319 x_shm_info.shmaddr = (char*)-1; 329 x_shm_info.shmaddr = (char*)-1;
320 image = XShmCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, NULL, &x_shm_info, width, height); 330 image = XShmCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, NULL, &x_shm_info, width, height);
321 if (image) { 331 if (image != NULL) {
322 x_shm_info.shmid = shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT | 0600); 332 x_shm_info.shmid = shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT | 0600);
323 if (x_shm_info.shmid == -1) { 333 if (x_shm_info.shmid == -1) {
324 XDestroyImage(image); 334 XDestroyImage(image);
325 image = 0; 335 image = NULL;
326 goto no_shm; 336 goto no_shm;
327 } 337 }
328 x_shm_info.readOnly = False; 338 x_shm_info.readOnly = False;
329 x_shm_info.shmaddr = (char*) shmat(x_shm_info.shmid, 0, 0); 339 x_shm_info.shmaddr = (char*) shmat(x_shm_info.shmid, 0, 0);
330 image->data = x_shm_info.shmaddr; 340 image->data = x_shm_info.shmaddr;
338 XSync(display, False); 348 XSync(display, False);
339 shmctl(x_shm_info.shmid, IPC_RMID, 0); 349 shmctl(x_shm_info.shmid, IPC_RMID, 0);
340 use_shm = true; 350 use_shm = true;
341 } 351 }
342 } 352 }
343 no_shm: 353 no_shm: //TODO: Understand why he did it, and supress it if needed
344 if (image == 0) { 354 if (image == 0) {
345 use_shm = false; 355 use_shm = false;
346 image = XCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, 0, 0, width, height, 32, 0); 356 image = XCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, 0, 0, width, height, 32, 0);
347 image->data = (char*)malloc(image->bytes_per_line * image->height); 357 image->data = (char*)malloc(image->bytes_per_line * image->height);
348 if (image->data == 0) { 358 if (image->data == NULL) {
349 XDestroyImage(image); 359 XDestroyImage(image);
350 image = 0; 360 image = NULL;
351 throw bad_alloc(); 361 throw bad_alloc();
352 } 362 }
353 } 363 }
354 Glyph g; 364
355 GlyphCreate(0xa1a2,&g); 365 Glyph g;
356 GlyphCreate(0xa4a3,&g); 366 GlyphCreate(0xa1a2, &g); //FIXME: Two calls? Huh?
367 GlyphCreate(0xa4a3, &g);
357 } 368 }
358 369
359 PeerX11::~PeerX11() { 370 PeerX11::~PeerX11() {
360 if (display) { 371 if (display) {
361 if (fontset) XFreeFontSet(display, fontset); 372 if (fontset) XFreeFontSet(display, fontset);
395 int i; 406 int i;
396 unsigned char* mem = (unsigned char*) image->data; 407 unsigned char* mem = (unsigned char*) image->data;
397 unsigned char* dest = glyph->bitmap.buffer; 408 unsigned char* dest = glyph->bitmap.buffer;
398 int bpp = image->bytes_per_line/width; 409 int bpp = image->bytes_per_line/width;
399 int bpl = image->bytes_per_line; 410 int bpl = image->bytes_per_line;
400 for (i=0; i<logic.height; i++) { 411 for (i=0; i < logic.height; i++) {
401 unsigned char* m = mem; 412 unsigned char* m = mem;
402 int j; for (j=0; j<cwidth; j++) { 413 int j; for (j=0; j<cwidth; j++) {
403 *dest = colortable[((read_little_endian_int((char*)m))>>shift) & mask]; 414 *dest = colortable[((read_little_endian_int((char*)m))>>shift) & mask];
404 dest++; 415 dest++;
405 m += bpp; 416 m += bpp;