annotate font/font_peer_x11.cc @ 0:223b71206888

Initial import
author thib
date Fri, 01 Aug 2008 16:32:45 +0000
parents
children 3a6aaeab7b4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004 Kazunor "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * Copyright (c) 2000, 2001 Yuki Sawada
223b71206888 Initial import
thib
parents:
diff changeset
4 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
5 *
223b71206888 Initial import
thib
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
7 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
8 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
14 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
15 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
16 *
223b71206888 Initial import
thib
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
27 */
223b71206888 Initial import
thib
parents:
diff changeset
28
223b71206888 Initial import
thib
parents:
diff changeset
29 #include <stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
30
223b71206888 Initial import
thib
parents:
diff changeset
31 #include "font.h"
223b71206888 Initial import
thib
parents:
diff changeset
32 #include "font_peer.h"
223b71206888 Initial import
thib
parents:
diff changeset
33
223b71206888 Initial import
thib
parents:
diff changeset
34 #if USE_X11
223b71206888 Initial import
thib
parents:
diff changeset
35
223b71206888 Initial import
thib
parents:
diff changeset
36 #include <sys/ipc.h>
223b71206888 Initial import
thib
parents:
diff changeset
37 #include <sys/shm.h>
223b71206888 Initial import
thib
parents:
diff changeset
38
223b71206888 Initial import
thib
parents:
diff changeset
39 #include <X11/Xlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
40 #include <X11/Xutil.h>
223b71206888 Initial import
thib
parents:
diff changeset
41 #include <X11/extensions/XShm.h>
223b71206888 Initial import
thib
parents:
diff changeset
42 #include <stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
43 #include<iostream>
223b71206888 Initial import
thib
parents:
diff changeset
44
223b71206888 Initial import
thib
parents:
diff changeset
45 #include<vector>
223b71206888 Initial import
thib
parents:
diff changeset
46 #include<map>
223b71206888 Initial import
thib
parents:
diff changeset
47 //#include<iostream>
223b71206888 Initial import
thib
parents:
diff changeset
48 #include<sstream>
223b71206888 Initial import
thib
parents:
diff changeset
49 #include <string>
223b71206888 Initial import
thib
parents:
diff changeset
50 #include <stdexcept>
223b71206888 Initial import
thib
parents:
diff changeset
51
223b71206888 Initial import
thib
parents:
diff changeset
52 using namespace std;
223b71206888 Initial import
thib
parents:
diff changeset
53
223b71206888 Initial import
thib
parents:
diff changeset
54 namespace XKFont {
223b71206888 Initial import
thib
parents:
diff changeset
55 inline int read_little_endian_int(const char* buf) {
223b71206888 Initial import
thib
parents:
diff changeset
56 const unsigned char *p = (const unsigned char *) buf;
223b71206888 Initial import
thib
parents:
diff changeset
57 return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
223b71206888 Initial import
thib
parents:
diff changeset
58 }
223b71206888 Initial import
thib
parents:
diff changeset
59
223b71206888 Initial import
thib
parents:
diff changeset
60 /***********************************
223b71206888 Initial import
thib
parents:
diff changeset
61 **
223b71206888 Initial import
thib
parents:
diff changeset
62 ** Fontinfo / FontSetInfo
223b71206888 Initial import
thib
parents:
diff changeset
63 **
223b71206888 Initial import
thib
parents:
diff changeset
64 ** fontset から特定 pixel size を持つ
223b71206888 Initial import
thib
parents:
diff changeset
65 ** 別のfontsetを作成するためのクラス
223b71206888 Initial import
thib
parents:
diff changeset
66 */
223b71206888 Initial import
thib
parents:
diff changeset
67 struct FontInfo {
223b71206888 Initial import
thib
parents:
diff changeset
68 std::map<int, string> fontlist;
223b71206888 Initial import
thib
parents:
diff changeset
69 FontInfo(Display* display, const char* fontname_orig);
223b71206888 Initial import
thib
parents:
diff changeset
70 string Search(int pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
71 };
223b71206888 Initial import
thib
parents:
diff changeset
72 struct FontSetInfo {
223b71206888 Initial import
thib
parents:
diff changeset
73 std::vector<FontInfo*> fontlist;
223b71206888 Initial import
thib
parents:
diff changeset
74 FontSetInfo(Display* display, const char* fontset_orig);
223b71206888 Initial import
thib
parents:
diff changeset
75 string Search(int pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
76 ~FontSetInfo();
223b71206888 Initial import
thib
parents:
diff changeset
77 };
223b71206888 Initial import
thib
parents:
diff changeset
78
223b71206888 Initial import
thib
parents:
diff changeset
79
223b71206888 Initial import
thib
parents:
diff changeset
80 /***********************************
223b71206888 Initial import
thib
parents:
diff changeset
81 **
223b71206888 Initial import
thib
parents:
diff changeset
82 ** Methods of Fontinfo / FontSetInfo
223b71206888 Initial import
thib
parents:
diff changeset
83 **
223b71206888 Initial import
thib
parents:
diff changeset
84 */
223b71206888 Initial import
thib
parents:
diff changeset
85 FontInfo::FontInfo(Display* display, const char* fontname_orig) {
223b71206888 Initial import
thib
parents:
diff changeset
86 /* フォントの大きさ関係の情報を消去 */
223b71206888 Initial import
thib
parents:
diff changeset
87 int i;
223b71206888 Initial import
thib
parents:
diff changeset
88 char* fontname = new char[strlen(fontname_orig)+50];
223b71206888 Initial import
thib
parents:
diff changeset
89 int minus_count = 0; bool is_skip = false; int fc = 0;
223b71206888 Initial import
thib
parents:
diff changeset
90 for (i=0; fontname_orig[i]!=0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
91 if (fontname_orig[i] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
92 minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
93 if (minus_count >= 7 && minus_count <= 12) {
223b71206888 Initial import
thib
parents:
diff changeset
94 fontname[fc++] = '-';
223b71206888 Initial import
thib
parents:
diff changeset
95 fontname[fc++] = '*';
223b71206888 Initial import
thib
parents:
diff changeset
96 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
97 } else {
223b71206888 Initial import
thib
parents:
diff changeset
98 is_skip = false;
223b71206888 Initial import
thib
parents:
diff changeset
99 }
223b71206888 Initial import
thib
parents:
diff changeset
100 }
223b71206888 Initial import
thib
parents:
diff changeset
101 if (! is_skip) fontname[fc++] = fontname_orig[i];
223b71206888 Initial import
thib
parents:
diff changeset
102 }
223b71206888 Initial import
thib
parents:
diff changeset
103 /* フォント情報を得る */
223b71206888 Initial import
thib
parents:
diff changeset
104 fontname[fc] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
105 int count;
223b71206888 Initial import
thib
parents:
diff changeset
106 char** fontnamelist = XListFonts(display, fontname, 100, &count);
223b71206888 Initial import
thib
parents:
diff changeset
107 for (i=0; i<count; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
108 char* curfont = fontnamelist[i];
223b71206888 Initial import
thib
parents:
diff changeset
109 /* fontname から pixel size 情報を得る */
223b71206888 Initial import
thib
parents:
diff changeset
110 int j; int minus_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
111 for (j=0; curfont[j] != 0; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
112 if (curfont[j] == '-') minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
113 if (minus_count == 7) {
223b71206888 Initial import
thib
parents:
diff changeset
114 int pixsize = atoi(curfont+j+1);
223b71206888 Initial import
thib
parents:
diff changeset
115 if (fontlist.find(pixsize) == fontlist.end()) {
223b71206888 Initial import
thib
parents:
diff changeset
116 fontlist[pixsize] = string(curfont);
223b71206888 Initial import
thib
parents:
diff changeset
117 }
223b71206888 Initial import
thib
parents:
diff changeset
118 break;
223b71206888 Initial import
thib
parents:
diff changeset
119 }
223b71206888 Initial import
thib
parents:
diff changeset
120 }
223b71206888 Initial import
thib
parents:
diff changeset
121 }
223b71206888 Initial import
thib
parents:
diff changeset
122 /* 検索に失敗した場合、とりあえず fontname を入れておく */
223b71206888 Initial import
thib
parents:
diff changeset
123 if (fontlist.find(0) == fontlist.end()) {
223b71206888 Initial import
thib
parents:
diff changeset
124 fontlist[0] = string(fontname);
223b71206888 Initial import
thib
parents:
diff changeset
125 }
223b71206888 Initial import
thib
parents:
diff changeset
126 XFreeFontNames(fontnamelist);
223b71206888 Initial import
thib
parents:
diff changeset
127 delete[] fontname;
223b71206888 Initial import
thib
parents:
diff changeset
128 return;
223b71206888 Initial import
thib
parents:
diff changeset
129 }
223b71206888 Initial import
thib
parents:
diff changeset
130 string FontInfo::Search(int pixsize) {
223b71206888 Initial import
thib
parents:
diff changeset
131 int i;
223b71206888 Initial import
thib
parents:
diff changeset
132 /* pixsize に近いフォントが(あれば)帰す */
223b71206888 Initial import
thib
parents:
diff changeset
133 if (fontlist.find(pixsize) != fontlist.end()) return fontlist[pixsize];
223b71206888 Initial import
thib
parents:
diff changeset
134 for (i=1; i<4; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
135 if (fontlist.find(pixsize-i) != fontlist.end()) return fontlist[pixsize-i];
223b71206888 Initial import
thib
parents:
diff changeset
136 if (fontlist.find(pixsize+i) != fontlist.end()) return fontlist[pixsize+i];
223b71206888 Initial import
thib
parents:
diff changeset
137 }
223b71206888 Initial import
thib
parents:
diff changeset
138 /* 見つからない:fontlist[0] を加工して帰す */
223b71206888 Initial import
thib
parents:
diff changeset
139 /* pt/xres/yres などのフィールドに '-0-' というのがあれば '-*-'に変換
223b71206888 Initial import
thib
parents:
diff changeset
140 ** pixsize は与えられた pixsize にする
223b71206888 Initial import
thib
parents:
diff changeset
141 */
223b71206888 Initial import
thib
parents:
diff changeset
142 string basefont_s = fontlist[0];
223b71206888 Initial import
thib
parents:
diff changeset
143 const char* basefont = basefont_s.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
144 char* retfont = new char[strlen(basefont)+50];
223b71206888 Initial import
thib
parents:
diff changeset
145 int minus_count = 0; int rc = 0; bool is_skip = false;
223b71206888 Initial import
thib
parents:
diff changeset
146 for (i=0; basefont[i] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
147 if (basefont[i] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
148 minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
149 is_skip = false;
223b71206888 Initial import
thib
parents:
diff changeset
150 if (minus_count == 7) {
223b71206888 Initial import
thib
parents:
diff changeset
151 sprintf(retfont+rc, "-%d", pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
152 rc = strlen(retfont);
223b71206888 Initial import
thib
parents:
diff changeset
153 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
154 } else if (minus_count > 7 && minus_count <= 12) {
223b71206888 Initial import
thib
parents:
diff changeset
155 if (basefont[i+1] == '0' && basefont[i+2] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
156 retfont[rc++]='-';
223b71206888 Initial import
thib
parents:
diff changeset
157 retfont[rc++]='*';
223b71206888 Initial import
thib
parents:
diff changeset
158 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
159 }
223b71206888 Initial import
thib
parents:
diff changeset
160 }
223b71206888 Initial import
thib
parents:
diff changeset
161 }
223b71206888 Initial import
thib
parents:
diff changeset
162 if (! is_skip) retfont[rc++] = basefont[i];
223b71206888 Initial import
thib
parents:
diff changeset
163 }
223b71206888 Initial import
thib
parents:
diff changeset
164 retfont[rc] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
165 string retfont_str = string(retfont);
223b71206888 Initial import
thib
parents:
diff changeset
166 delete[] retfont;
223b71206888 Initial import
thib
parents:
diff changeset
167 return retfont_str;
223b71206888 Initial import
thib
parents:
diff changeset
168 }
223b71206888 Initial import
thib
parents:
diff changeset
169
223b71206888 Initial import
thib
parents:
diff changeset
170 FontSetInfo::FontSetInfo(Display* display, const char* fontset_orig) {
223b71206888 Initial import
thib
parents:
diff changeset
171 char* fontset = new char[strlen(fontset_orig)+1];
223b71206888 Initial import
thib
parents:
diff changeset
172 strcpy(fontset, fontset_orig);
223b71206888 Initial import
thib
parents:
diff changeset
173 char* cur = fontset;
223b71206888 Initial import
thib
parents:
diff changeset
174 while(strchr(cur, ',')) {
223b71206888 Initial import
thib
parents:
diff changeset
175 char* font = cur;
223b71206888 Initial import
thib
parents:
diff changeset
176 cur = strchr(cur, ',');
223b71206888 Initial import
thib
parents:
diff changeset
177 *cur++ = '\0';
223b71206888 Initial import
thib
parents:
diff changeset
178 fontlist.push_back(new FontInfo(display, font));
223b71206888 Initial import
thib
parents:
diff changeset
179 }
223b71206888 Initial import
thib
parents:
diff changeset
180 fontlist.push_back(new FontInfo(display, cur));
223b71206888 Initial import
thib
parents:
diff changeset
181 delete[] fontset;
223b71206888 Initial import
thib
parents:
diff changeset
182 return;
223b71206888 Initial import
thib
parents:
diff changeset
183 }
223b71206888 Initial import
thib
parents:
diff changeset
184 FontSetInfo::~FontSetInfo() {
223b71206888 Initial import
thib
parents:
diff changeset
185 std::vector<FontInfo*>::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
186 for (it=fontlist.begin(); it != fontlist.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
187 delete (*it);
223b71206888 Initial import
thib
parents:
diff changeset
188 }
223b71206888 Initial import
thib
parents:
diff changeset
189 return;
223b71206888 Initial import
thib
parents:
diff changeset
190 }
223b71206888 Initial import
thib
parents:
diff changeset
191 string FontSetInfo::Search(int pixsize) {
223b71206888 Initial import
thib
parents:
diff changeset
192 stringstream s;
223b71206888 Initial import
thib
parents:
diff changeset
193 std::vector<FontInfo*>::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
194 for (it=fontlist.begin(); it != fontlist.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
195 if (it != fontlist.begin()) s << ",";
223b71206888 Initial import
thib
parents:
diff changeset
196 s << (*it)->Search(pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
197 }
223b71206888 Initial import
thib
parents:
diff changeset
198 s<<ends;
223b71206888 Initial import
thib
parents:
diff changeset
199 return string(s.str());
223b71206888 Initial import
thib
parents:
diff changeset
200 }
223b71206888 Initial import
thib
parents:
diff changeset
201
223b71206888 Initial import
thib
parents:
diff changeset
202 /****************************************
223b71206888 Initial import
thib
parents:
diff changeset
203 **
223b71206888 Initial import
thib
parents:
diff changeset
204 ** FontPeerX11
223b71206888 Initial import
thib
parents:
diff changeset
205 */
223b71206888 Initial import
thib
parents:
diff changeset
206 Display* PeerX11::display = 0;
223b71206888 Initial import
thib
parents:
diff changeset
207 void PeerX11::InitDisplay(Display* _d) {
223b71206888 Initial import
thib
parents:
diff changeset
208 /* d = ((GdkWindowPrivate*)(top_window.gdkobj()))->xdisplay; */
223b71206888 Initial import
thib
parents:
diff changeset
209 display = _d;
223b71206888 Initial import
thib
parents:
diff changeset
210 }
223b71206888 Initial import
thib
parents:
diff changeset
211
223b71206888 Initial import
thib
parents:
diff changeset
212 void PeerX11::OpenDisplay(void) {
223b71206888 Initial import
thib
parents:
diff changeset
213 if (display) return;
223b71206888 Initial import
thib
parents:
diff changeset
214
223b71206888 Initial import
thib
parents:
diff changeset
215 char* display_name = getenv("DISPLAY");
223b71206888 Initial import
thib
parents:
diff changeset
216 if (display_name == 0) display_name = ":0";
223b71206888 Initial import
thib
parents:
diff changeset
217
223b71206888 Initial import
thib
parents:
diff changeset
218 display = XOpenDisplay(display_name);
223b71206888 Initial import
thib
parents:
diff changeset
219
223b71206888 Initial import
thib
parents:
diff changeset
220 if (display == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
221 string err = string("XKFont::PeerX11:OpenDisplay() : Cannot open X display ") + display_name;
223b71206888 Initial import
thib
parents:
diff changeset
222 throw std::invalid_argument(err);
223b71206888 Initial import
thib
parents:
diff changeset
223 }
223b71206888 Initial import
thib
parents:
diff changeset
224 }
223b71206888 Initial import
thib
parents:
diff changeset
225
223b71206888 Initial import
thib
parents:
diff changeset
226 inline int MAX(int a, int b) {
223b71206888 Initial import
thib
parents:
diff changeset
227 if (a > b) return a;
223b71206888 Initial import
thib
parents:
diff changeset
228 else return b;
223b71206888 Initial import
thib
parents:
diff changeset
229 }
223b71206888 Initial import
thib
parents:
diff changeset
230
223b71206888 Initial import
thib
parents:
diff changeset
231 PeerX11::PeerX11(const char* fontname, int index, int fontsize, int _vsize) :
223b71206888 Initial import
thib
parents:
diff changeset
232 fontset(0), gc(0), canvas(0), image(0), colortable(0) {
223b71206888 Initial import
thib
parents:
diff changeset
233 OpenDisplay();
223b71206888 Initial import
thib
parents:
diff changeset
234
223b71206888 Initial import
thib
parents:
diff changeset
235 int scr = DefaultScreen(display);
223b71206888 Initial import
thib
parents:
diff changeset
236 Window w = RootWindow(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
237 Colormap cmap = DefaultColormap(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
238 visual = DefaultVisual(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
239
223b71206888 Initial import
thib
parents:
diff changeset
240 if (visual->c_class != TrueColor && visual->c_class != DirectColor) {
223b71206888 Initial import
thib
parents:
diff changeset
241 string err = "XKFont::PeerX11:PeerX11() : No supported Color mode of X : neither TrueColor nor DirectColor";
223b71206888 Initial import
thib
parents:
diff changeset
242 throw std::runtime_error(err);
223b71206888 Initial import
thib
parents:
diff changeset
243 }
223b71206888 Initial import
thib
parents:
diff changeset
244 /* 色の初期化 */
223b71206888 Initial import
thib
parents:
diff changeset
245 white = visual->red_mask | visual->green_mask | visual->blue_mask;
223b71206888 Initial import
thib
parents:
diff changeset
246 black = 0;
223b71206888 Initial import
thib
parents:
diff changeset
247 if (visual->green_mask == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
248 string err = "XKFont::PeerX11:PeerX11() : Invalid Visual on X";
223b71206888 Initial import
thib
parents:
diff changeset
249 throw std::runtime_error(err);
223b71206888 Initial import
thib
parents:
diff changeset
250 }
223b71206888 Initial import
thib
parents:
diff changeset
251 shift = 0;
223b71206888 Initial import
thib
parents:
diff changeset
252 mask = visual->green_mask;
223b71206888 Initial import
thib
parents:
diff changeset
253 while(mask & 0x01) { shift++; mask >>= 1; }
223b71206888 Initial import
thib
parents:
diff changeset
254
223b71206888 Initial import
thib
parents:
diff changeset
255 int tablesize = mask+1;
223b71206888 Initial import
thib
parents:
diff changeset
256 colortable = new int[tablesize];
223b71206888 Initial import
thib
parents:
diff changeset
257 int i; for (i=0; i< tablesize; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
258 colortable[i] = i*255/tablesize;
223b71206888 Initial import
thib
parents:
diff changeset
259 }
223b71206888 Initial import
thib
parents:
diff changeset
260 XSupportsLocale();
223b71206888 Initial import
thib
parents:
diff changeset
261
223b71206888 Initial import
thib
parents:
diff changeset
262 /* font 読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
263 FontSetInfo fsinfo(display,fontname);
223b71206888 Initial import
thib
parents:
diff changeset
264 string fontset_name = fsinfo.Search(fontsize);
223b71206888 Initial import
thib
parents:
diff changeset
265 char** missing_cl; int missing_cc; char* def_s;
223b71206888 Initial import
thib
parents:
diff changeset
266 printf("fontset %s\n",fontset_name.c_str());
223b71206888 Initial import
thib
parents:
diff changeset
267 fontset = XCreateFontSet(display, fontset_name.c_str(), &missing_cl, &missing_cc, &def_s);
223b71206888 Initial import
thib
parents:
diff changeset
268
223b71206888 Initial import
thib
parents:
diff changeset
269 if (fontset == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
270 delete[] colortable;
223b71206888 Initial import
thib
parents:
diff changeset
271 string err = string("XKFont::PeerX11:PeerX11() : Cannot create fontset ");
223b71206888 Initial import
thib
parents:
diff changeset
272 err += fontset_name; err += " (font name "; err += fontname; err += ")";
223b71206888 Initial import
thib
parents:
diff changeset
273 throw std::invalid_argument(err);
223b71206888 Initial import
thib
parents:
diff changeset
274 }
223b71206888 Initial import
thib
parents:
diff changeset
275
223b71206888 Initial import
thib
parents:
diff changeset
276 if (missing_cc != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
277 cerr << "XKFont::PeerX11:PeerX11() : Cannot found some fonts in the fontset"<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
278 cerr << " fontset: "<<fontset_name<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
279 cerr << " not found fonts:"<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
280 int i; for (i=0; i<missing_cc; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
281 cerr << " " << missing_cl[i] << endl;
223b71206888 Initial import
thib
parents:
diff changeset
282 }
223b71206888 Initial import
thib
parents:
diff changeset
283 }
223b71206888 Initial import
thib
parents:
diff changeset
284
223b71206888 Initial import
thib
parents:
diff changeset
285 XFontSetExtents* extents = XExtentsOfFontSet(fontset);
223b71206888 Initial import
thib
parents:
diff changeset
286
223b71206888 Initial import
thib
parents:
diff changeset
287 width = extents->max_ink_extent.width;
223b71206888 Initial import
thib
parents:
diff changeset
288 height = extents->max_ink_extent.height;
223b71206888 Initial import
thib
parents:
diff changeset
289
223b71206888 Initial import
thib
parents:
diff changeset
290 /* calculate ascent / descent */
223b71206888 Initial import
thib
parents:
diff changeset
291 XFontStruct** font_structs; char** font_names;
223b71206888 Initial import
thib
parents:
diff changeset
292 int num_fonts = XFontsOfFontSet(fontset, &font_structs, &font_names);
223b71206888 Initial import
thib
parents:
diff changeset
293 printf("locale %s\n",XLocaleOfOM(XOMOfOC(fontset)));
223b71206888 Initial import
thib
parents:
diff changeset
294
223b71206888 Initial import
thib
parents:
diff changeset
295 ascent = 0;
223b71206888 Initial import
thib
parents:
diff changeset
296 for (i=0; i<num_fonts; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
297 ascent = MAX(ascent, font_structs[i]->ascent);
223b71206888 Initial import
thib
parents:
diff changeset
298 }
223b71206888 Initial import
thib
parents:
diff changeset
299
223b71206888 Initial import
thib
parents:
diff changeset
300 /* 描画用の pixmap を作成 */
223b71206888 Initial import
thib
parents:
diff changeset
301 XGCValues gc_values; unsigned int gc_values_mask;
223b71206888 Initial import
thib
parents:
diff changeset
302 gc_values.function = GXcopy;
223b71206888 Initial import
thib
parents:
diff changeset
303 gc_values.fill_style = FillSolid;
223b71206888 Initial import
thib
parents:
diff changeset
304 gc_values.arc_mode = ArcPieSlice;
223b71206888 Initial import
thib
parents:
diff changeset
305 gc_values.subwindow_mode = ClipByChildren;
223b71206888 Initial import
thib
parents:
diff changeset
306 gc_values.graphics_exposures = False;
223b71206888 Initial import
thib
parents:
diff changeset
307 gc_values.foreground = white;
223b71206888 Initial import
thib
parents:
diff changeset
308 gc_values.background = black;
223b71206888 Initial import
thib
parents:
diff changeset
309 gc_values_mask = GCFunction | GCFillStyle | GCArcMode | GCSubwindowMode | GCGraphicsExposures | GCForeground | GCBackground;
223b71206888 Initial import
thib
parents:
diff changeset
310 gc = XCreateGC(display, w, gc_values_mask, &gc_values);
223b71206888 Initial import
thib
parents:
diff changeset
311
223b71206888 Initial import
thib
parents:
diff changeset
312 canvas = XCreatePixmap(display, w, width, height, DefaultDepth(display, scr));
223b71206888 Initial import
thib
parents:
diff changeset
313
223b71206888 Initial import
thib
parents:
diff changeset
314 /* イメージ転送用の image の作成 */
223b71206888 Initial import
thib
parents:
diff changeset
315 int ignore;
223b71206888 Initial import
thib
parents:
diff changeset
316 use_shm = false;
223b71206888 Initial import
thib
parents:
diff changeset
317 if (XShmQueryExtension(display) == True) {
223b71206888 Initial import
thib
parents:
diff changeset
318 x_shm_info.shmid = -1;
223b71206888 Initial import
thib
parents:
diff changeset
319 x_shm_info.shmaddr = (char*)-1;
223b71206888 Initial import
thib
parents:
diff changeset
320 image = XShmCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, NULL, &x_shm_info, width, height);
223b71206888 Initial import
thib
parents:
diff changeset
321 if (image) {
223b71206888 Initial import
thib
parents:
diff changeset
322 x_shm_info.shmid = shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT | 0600);
223b71206888 Initial import
thib
parents:
diff changeset
323 if (x_shm_info.shmid == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
324 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
325 image = 0;
223b71206888 Initial import
thib
parents:
diff changeset
326 goto no_shm;
223b71206888 Initial import
thib
parents:
diff changeset
327 }
223b71206888 Initial import
thib
parents:
diff changeset
328 x_shm_info.readOnly = False;
223b71206888 Initial import
thib
parents:
diff changeset
329 x_shm_info.shmaddr = (char*) shmat(x_shm_info.shmid, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
330 image->data = x_shm_info.shmaddr;
223b71206888 Initial import
thib
parents:
diff changeset
331 if (x_shm_info.shmaddr == (char*) -1) {
223b71206888 Initial import
thib
parents:
diff changeset
332 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
333 shmctl(x_shm_info.shmid, IPC_RMID, 0);
223b71206888 Initial import
thib
parents:
diff changeset
334 image = 0;
223b71206888 Initial import
thib
parents:
diff changeset
335 goto no_shm;
223b71206888 Initial import
thib
parents:
diff changeset
336 }
223b71206888 Initial import
thib
parents:
diff changeset
337 XShmAttach(display, &x_shm_info);
223b71206888 Initial import
thib
parents:
diff changeset
338 XSync(display, False);
223b71206888 Initial import
thib
parents:
diff changeset
339 shmctl(x_shm_info.shmid, IPC_RMID, 0);
223b71206888 Initial import
thib
parents:
diff changeset
340 use_shm = true;
223b71206888 Initial import
thib
parents:
diff changeset
341 }
223b71206888 Initial import
thib
parents:
diff changeset
342 }
223b71206888 Initial import
thib
parents:
diff changeset
343 no_shm:
223b71206888 Initial import
thib
parents:
diff changeset
344 if (image == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
345 use_shm = false;
223b71206888 Initial import
thib
parents:
diff changeset
346 image = XCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, 0, 0, width, height, 32, 0);
223b71206888 Initial import
thib
parents:
diff changeset
347 image->data = (char*)malloc(image->bytes_per_line * image->height);
223b71206888 Initial import
thib
parents:
diff changeset
348 if (image->data == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
349 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
350 image = 0;
223b71206888 Initial import
thib
parents:
diff changeset
351 throw bad_alloc();
223b71206888 Initial import
thib
parents:
diff changeset
352 }
223b71206888 Initial import
thib
parents:
diff changeset
353 }
223b71206888 Initial import
thib
parents:
diff changeset
354 Glyph g;
223b71206888 Initial import
thib
parents:
diff changeset
355 GlyphCreate(0xa1a2,&g);
223b71206888 Initial import
thib
parents:
diff changeset
356 GlyphCreate(0xa4a3,&g);
223b71206888 Initial import
thib
parents:
diff changeset
357 }
223b71206888 Initial import
thib
parents:
diff changeset
358
223b71206888 Initial import
thib
parents:
diff changeset
359 PeerX11::~PeerX11() {
223b71206888 Initial import
thib
parents:
diff changeset
360 if (display) {
223b71206888 Initial import
thib
parents:
diff changeset
361 if (fontset) XFreeFontSet(display, fontset);
223b71206888 Initial import
thib
parents:
diff changeset
362 if (gc) XFlushGC(display, gc);
223b71206888 Initial import
thib
parents:
diff changeset
363 if (canvas) XFreePixmap(display, canvas);
223b71206888 Initial import
thib
parents:
diff changeset
364 if (image) {
223b71206888 Initial import
thib
parents:
diff changeset
365 if (use_shm) XShmDetach(display, &x_shm_info);
223b71206888 Initial import
thib
parents:
diff changeset
366 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
367 if (use_shm) shmdt(x_shm_info.shmaddr);
223b71206888 Initial import
thib
parents:
diff changeset
368 }
223b71206888 Initial import
thib
parents:
diff changeset
369 if (colortable) delete[] colortable;
223b71206888 Initial import
thib
parents:
diff changeset
370 }
223b71206888 Initial import
thib
parents:
diff changeset
371 }
223b71206888 Initial import
thib
parents:
diff changeset
372
223b71206888 Initial import
thib
parents:
diff changeset
373 bool PeerX11::GlyphCreate(unsigned int code, Glyph* glyph) {
223b71206888 Initial import
thib
parents:
diff changeset
374 XRectangle ink, logic;
223b71206888 Initial import
thib
parents:
diff changeset
375
223b71206888 Initial import
thib
parents:
diff changeset
376 char str[3]={0,0,0};
223b71206888 Initial import
thib
parents:
diff changeset
377
223b71206888 Initial import
thib
parents:
diff changeset
378 if ( (code>>8)&0xff){
223b71206888 Initial import
thib
parents:
diff changeset
379 str[0]=code>>8;
223b71206888 Initial import
thib
parents:
diff changeset
380 str[1]=code & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
381 } else {
223b71206888 Initial import
thib
parents:
diff changeset
382 str[0]=code;
223b71206888 Initial import
thib
parents:
diff changeset
383 }
223b71206888 Initial import
thib
parents:
diff changeset
384
223b71206888 Initial import
thib
parents:
diff changeset
385 XmbTextExtents(fontset, str, strlen(str),&ink,&logic);
223b71206888 Initial import
thib
parents:
diff changeset
386 int cwidth = logic.width;
223b71206888 Initial import
thib
parents:
diff changeset
387 XmbDrawImageString(display, canvas, fontset, gc, 0, -logic.y, str, strlen(str));
223b71206888 Initial import
thib
parents:
diff changeset
388 if (use_shm) {
223b71206888 Initial import
thib
parents:
diff changeset
389 XShmGetImage(display, canvas, image, 0, 0, AllPlanes);
223b71206888 Initial import
thib
parents:
diff changeset
390 } else {
223b71206888 Initial import
thib
parents:
diff changeset
391 XGetSubImage(display, canvas, 0, 0, width, height, AllPlanes, ZPixmap, image, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
392 }
223b71206888 Initial import
thib
parents:
diff changeset
393 XSync(display, False);
223b71206888 Initial import
thib
parents:
diff changeset
394 glyph->bitmap.buffer = new unsigned char[logic.width*logic.height];
223b71206888 Initial import
thib
parents:
diff changeset
395 int i;
223b71206888 Initial import
thib
parents:
diff changeset
396 unsigned char* mem = (unsigned char*) image->data;
223b71206888 Initial import
thib
parents:
diff changeset
397 unsigned char* dest = glyph->bitmap.buffer;
223b71206888 Initial import
thib
parents:
diff changeset
398 int bpp = image->bytes_per_line/width;
223b71206888 Initial import
thib
parents:
diff changeset
399 int bpl = image->bytes_per_line;
223b71206888 Initial import
thib
parents:
diff changeset
400 for (i=0; i<logic.height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
401 unsigned char* m = mem;
223b71206888 Initial import
thib
parents:
diff changeset
402 int j; for (j=0; j<cwidth; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
403 *dest = colortable[((read_little_endian_int((char*)m))>>shift) & mask];
223b71206888 Initial import
thib
parents:
diff changeset
404 dest++;
223b71206888 Initial import
thib
parents:
diff changeset
405 m += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
406 }
223b71206888 Initial import
thib
parents:
diff changeset
407 mem += bpl;
223b71206888 Initial import
thib
parents:
diff changeset
408 }
223b71206888 Initial import
thib
parents:
diff changeset
409 glyph->bitmap_left = logic.x;
223b71206888 Initial import
thib
parents:
diff changeset
410 glyph->bitmap_top = -logic.y;
223b71206888 Initial import
thib
parents:
diff changeset
411 glyph->bitmap.width = logic.width;
223b71206888 Initial import
thib
parents:
diff changeset
412 glyph->bitmap.rows = logic.height;
223b71206888 Initial import
thib
parents:
diff changeset
413 glyph->advance.x = logic.width + 1;
223b71206888 Initial import
thib
parents:
diff changeset
414 glyph->advance.y = logic.height+ 1;
223b71206888 Initial import
thib
parents:
diff changeset
415
223b71206888 Initial import
thib
parents:
diff changeset
416 return true;
223b71206888 Initial import
thib
parents:
diff changeset
417 }
223b71206888 Initial import
thib
parents:
diff changeset
418 };
223b71206888 Initial import
thib
parents:
diff changeset
419
223b71206888 Initial import
thib
parents:
diff changeset
420 #endif /* USE_X11 */