annotate font/font_peer_x11.cc @ 60:e16e13d8cd68

Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
author Thibaut GIRKA <thib@sitedethib.com>
date Fri, 18 Dec 2009 20:41:38 +0100
parents 15a18fbe6f21
children 4416cfac86ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
27
3a6aaeab7b4e * Fixed a typo in Jagarl's name
thib
parents: 0
diff changeset
2 * Copyright (c) 2004 Kazunori "jagarl" Ueno
0
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>
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
43 #include <iostream>
0
223b71206888 Initial import
thib
parents:
diff changeset
44
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
45 #include <vector>
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
46 #include <map>
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
47
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
48 #include <sstream>
0
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];
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
89 int minus_count = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
90 bool is_skip = false;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
91 int fc = 0;
0
223b71206888 Initial import
thib
parents:
diff changeset
92 for (i=0; fontname_orig[i]!=0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
93 if (fontname_orig[i] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
94 minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
95 if (minus_count >= 7 && minus_count <= 12) {
223b71206888 Initial import
thib
parents:
diff changeset
96 fontname[fc++] = '-';
223b71206888 Initial import
thib
parents:
diff changeset
97 fontname[fc++] = '*';
223b71206888 Initial import
thib
parents:
diff changeset
98 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
99 } else {
223b71206888 Initial import
thib
parents:
diff changeset
100 is_skip = false;
223b71206888 Initial import
thib
parents:
diff changeset
101 }
223b71206888 Initial import
thib
parents:
diff changeset
102 }
223b71206888 Initial import
thib
parents:
diff changeset
103 if (! is_skip) fontname[fc++] = fontname_orig[i];
223b71206888 Initial import
thib
parents:
diff changeset
104 }
223b71206888 Initial import
thib
parents:
diff changeset
105 /* ¥Õ¥©¥ó¥È¾ðÊó¤òÆÀ¤ë */
223b71206888 Initial import
thib
parents:
diff changeset
106 fontname[fc] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
107 int count;
223b71206888 Initial import
thib
parents:
diff changeset
108 char** fontnamelist = XListFonts(display, fontname, 100, &count);
223b71206888 Initial import
thib
parents:
diff changeset
109 for (i=0; i<count; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
110 char* curfont = fontnamelist[i];
223b71206888 Initial import
thib
parents:
diff changeset
111 /* fontname ¤«¤é pixel size ¾ðÊó¤òÆÀ¤ë */
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
112 int j;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
113 int minus_count = 0;
0
223b71206888 Initial import
thib
parents:
diff changeset
114 for (j=0; curfont[j] != 0; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
115 if (curfont[j] == '-') minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
116 if (minus_count == 7) {
223b71206888 Initial import
thib
parents:
diff changeset
117 int pixsize = atoi(curfont+j+1);
223b71206888 Initial import
thib
parents:
diff changeset
118 if (fontlist.find(pixsize) == fontlist.end()) {
223b71206888 Initial import
thib
parents:
diff changeset
119 fontlist[pixsize] = string(curfont);
223b71206888 Initial import
thib
parents:
diff changeset
120 }
223b71206888 Initial import
thib
parents:
diff changeset
121 break;
223b71206888 Initial import
thib
parents:
diff changeset
122 }
223b71206888 Initial import
thib
parents:
diff changeset
123 }
223b71206888 Initial import
thib
parents:
diff changeset
124 }
223b71206888 Initial import
thib
parents:
diff changeset
125 /* ¸¡º÷¤Ë¼ºÇÔ¤·¤¿¾ì¹ç¡¢¤È¤ê¤¢¤¨¤º fontname ¤òÆþ¤ì¤Æ¤ª¤¯ */
223b71206888 Initial import
thib
parents:
diff changeset
126 if (fontlist.find(0) == fontlist.end()) {
223b71206888 Initial import
thib
parents:
diff changeset
127 fontlist[0] = string(fontname);
223b71206888 Initial import
thib
parents:
diff changeset
128 }
223b71206888 Initial import
thib
parents:
diff changeset
129 XFreeFontNames(fontnamelist);
223b71206888 Initial import
thib
parents:
diff changeset
130 delete[] fontname;
223b71206888 Initial import
thib
parents:
diff changeset
131 return;
223b71206888 Initial import
thib
parents:
diff changeset
132 }
223b71206888 Initial import
thib
parents:
diff changeset
133 string FontInfo::Search(int pixsize) {
223b71206888 Initial import
thib
parents:
diff changeset
134 int i;
223b71206888 Initial import
thib
parents:
diff changeset
135 /* pixsize ¤Ë¶á¤¤¥Õ¥©¥ó¥È¤¬(¤¢¤ì¤Ð)µ¢¤¹ */
223b71206888 Initial import
thib
parents:
diff changeset
136 if (fontlist.find(pixsize) != fontlist.end()) return fontlist[pixsize];
223b71206888 Initial import
thib
parents:
diff changeset
137 for (i=1; i<4; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
138 if (fontlist.find(pixsize-i) != fontlist.end()) return fontlist[pixsize-i];
223b71206888 Initial import
thib
parents:
diff changeset
139 if (fontlist.find(pixsize+i) != fontlist.end()) return fontlist[pixsize+i];
223b71206888 Initial import
thib
parents:
diff changeset
140 }
223b71206888 Initial import
thib
parents:
diff changeset
141 /* ¸«¤Ä¤«¤é¤Ê¤¤¡§fontlist[0] ¤ò²Ã¹©¤·¤Æµ¢¤¹ */
223b71206888 Initial import
thib
parents:
diff changeset
142 /* pt/xres/yres ¤Ê¤É¤Î¥Õ¥£¡¼¥ë¥É¤Ë '-0-' ¤È¤¤¤¦¤Î¤¬¤¢¤ì¤Ð '-*-'¤ËÊÑ´¹
223b71206888 Initial import
thib
parents:
diff changeset
143 ** pixsize ¤ÏÍ¿¤¨¤é¤ì¤¿ pixsize ¤Ë¤¹¤ë
223b71206888 Initial import
thib
parents:
diff changeset
144 */
223b71206888 Initial import
thib
parents:
diff changeset
145 string basefont_s = fontlist[0];
223b71206888 Initial import
thib
parents:
diff changeset
146 const char* basefont = basefont_s.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
147 char* retfont = new char[strlen(basefont)+50];
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
148 int minus_count = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
149 int rc = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
150 bool is_skip = false;
0
223b71206888 Initial import
thib
parents:
diff changeset
151 for (i=0; basefont[i] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
152 if (basefont[i] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
153 minus_count++;
223b71206888 Initial import
thib
parents:
diff changeset
154 is_skip = false;
223b71206888 Initial import
thib
parents:
diff changeset
155 if (minus_count == 7) {
223b71206888 Initial import
thib
parents:
diff changeset
156 sprintf(retfont+rc, "-%d", pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
157 rc = strlen(retfont);
223b71206888 Initial import
thib
parents:
diff changeset
158 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
159 } else if (minus_count > 7 && minus_count <= 12) {
223b71206888 Initial import
thib
parents:
diff changeset
160 if (basefont[i+1] == '0' && basefont[i+2] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
161 retfont[rc++]='-';
223b71206888 Initial import
thib
parents:
diff changeset
162 retfont[rc++]='*';
223b71206888 Initial import
thib
parents:
diff changeset
163 is_skip = true;
223b71206888 Initial import
thib
parents:
diff changeset
164 }
223b71206888 Initial import
thib
parents:
diff changeset
165 }
223b71206888 Initial import
thib
parents:
diff changeset
166 }
223b71206888 Initial import
thib
parents:
diff changeset
167 if (! is_skip) retfont[rc++] = basefont[i];
223b71206888 Initial import
thib
parents:
diff changeset
168 }
223b71206888 Initial import
thib
parents:
diff changeset
169 retfont[rc] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
170 string retfont_str = string(retfont);
223b71206888 Initial import
thib
parents:
diff changeset
171 delete[] retfont;
223b71206888 Initial import
thib
parents:
diff changeset
172 return retfont_str;
223b71206888 Initial import
thib
parents:
diff changeset
173 }
223b71206888 Initial import
thib
parents:
diff changeset
174
223b71206888 Initial import
thib
parents:
diff changeset
175 FontSetInfo::FontSetInfo(Display* display, const char* fontset_orig) {
223b71206888 Initial import
thib
parents:
diff changeset
176 char* fontset = new char[strlen(fontset_orig)+1];
223b71206888 Initial import
thib
parents:
diff changeset
177 strcpy(fontset, fontset_orig);
223b71206888 Initial import
thib
parents:
diff changeset
178 char* cur = fontset;
223b71206888 Initial import
thib
parents:
diff changeset
179 while(strchr(cur, ',')) {
223b71206888 Initial import
thib
parents:
diff changeset
180 char* font = cur;
223b71206888 Initial import
thib
parents:
diff changeset
181 cur = strchr(cur, ',');
223b71206888 Initial import
thib
parents:
diff changeset
182 *cur++ = '\0';
223b71206888 Initial import
thib
parents:
diff changeset
183 fontlist.push_back(new FontInfo(display, font));
223b71206888 Initial import
thib
parents:
diff changeset
184 }
223b71206888 Initial import
thib
parents:
diff changeset
185 fontlist.push_back(new FontInfo(display, cur));
223b71206888 Initial import
thib
parents:
diff changeset
186 delete[] fontset;
223b71206888 Initial import
thib
parents:
diff changeset
187 return;
223b71206888 Initial import
thib
parents:
diff changeset
188 }
223b71206888 Initial import
thib
parents:
diff changeset
189 FontSetInfo::~FontSetInfo() {
223b71206888 Initial import
thib
parents:
diff changeset
190 std::vector<FontInfo*>::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
191 for (it=fontlist.begin(); it != fontlist.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
192 delete (*it);
223b71206888 Initial import
thib
parents:
diff changeset
193 }
223b71206888 Initial import
thib
parents:
diff changeset
194 return;
223b71206888 Initial import
thib
parents:
diff changeset
195 }
223b71206888 Initial import
thib
parents:
diff changeset
196 string FontSetInfo::Search(int pixsize) {
223b71206888 Initial import
thib
parents:
diff changeset
197 stringstream s;
223b71206888 Initial import
thib
parents:
diff changeset
198 std::vector<FontInfo*>::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
199 for (it=fontlist.begin(); it != fontlist.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
200 if (it != fontlist.begin()) s << ",";
223b71206888 Initial import
thib
parents:
diff changeset
201 s << (*it)->Search(pixsize);
223b71206888 Initial import
thib
parents:
diff changeset
202 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
203 s << ends;
0
223b71206888 Initial import
thib
parents:
diff changeset
204 return string(s.str());
223b71206888 Initial import
thib
parents:
diff changeset
205 }
223b71206888 Initial import
thib
parents:
diff changeset
206
223b71206888 Initial import
thib
parents:
diff changeset
207 /****************************************
223b71206888 Initial import
thib
parents:
diff changeset
208 **
223b71206888 Initial import
thib
parents:
diff changeset
209 ** FontPeerX11
223b71206888 Initial import
thib
parents:
diff changeset
210 */
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
211 Display* PeerX11::display = NULL;
0
223b71206888 Initial import
thib
parents:
diff changeset
212 void PeerX11::InitDisplay(Display* _d) {
223b71206888 Initial import
thib
parents:
diff changeset
213 /* d = ((GdkWindowPrivate*)(top_window.gdkobj()))->xdisplay; */
223b71206888 Initial import
thib
parents:
diff changeset
214 display = _d;
223b71206888 Initial import
thib
parents:
diff changeset
215 }
223b71206888 Initial import
thib
parents:
diff changeset
216
223b71206888 Initial import
thib
parents:
diff changeset
217 void PeerX11::OpenDisplay(void) {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
218 if (display != NULL) return;
0
223b71206888 Initial import
thib
parents:
diff changeset
219
47
5f548e5957a8 * get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents: 27
diff changeset
220 const char* display_name = getenv("DISPLAY");
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
221 if (display_name == NULL) display_name = ":0";
0
223b71206888 Initial import
thib
parents:
diff changeset
222
223b71206888 Initial import
thib
parents:
diff changeset
223 display = XOpenDisplay(display_name);
223b71206888 Initial import
thib
parents:
diff changeset
224
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
225 if (display == NULL) {
0
223b71206888 Initial import
thib
parents:
diff changeset
226 string err = string("XKFont::PeerX11:OpenDisplay() : Cannot open X display ") + display_name;
223b71206888 Initial import
thib
parents:
diff changeset
227 throw std::invalid_argument(err);
223b71206888 Initial import
thib
parents:
diff changeset
228 }
223b71206888 Initial import
thib
parents:
diff changeset
229 }
223b71206888 Initial import
thib
parents:
diff changeset
230
223b71206888 Initial import
thib
parents:
diff changeset
231 inline int MAX(int a, int b) {
223b71206888 Initial import
thib
parents:
diff changeset
232 if (a > b) return a;
223b71206888 Initial import
thib
parents:
diff changeset
233 else return b;
223b71206888 Initial import
thib
parents:
diff changeset
234 }
223b71206888 Initial import
thib
parents:
diff changeset
235
223b71206888 Initial import
thib
parents:
diff changeset
236 PeerX11::PeerX11(const char* fontname, int index, int fontsize, int _vsize) :
223b71206888 Initial import
thib
parents:
diff changeset
237 fontset(0), gc(0), canvas(0), image(0), colortable(0) {
223b71206888 Initial import
thib
parents:
diff changeset
238 OpenDisplay();
223b71206888 Initial import
thib
parents:
diff changeset
239
223b71206888 Initial import
thib
parents:
diff changeset
240 int scr = DefaultScreen(display);
223b71206888 Initial import
thib
parents:
diff changeset
241 Window w = RootWindow(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
242 Colormap cmap = DefaultColormap(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
243 visual = DefaultVisual(display, scr);
223b71206888 Initial import
thib
parents:
diff changeset
244
223b71206888 Initial import
thib
parents:
diff changeset
245 if (visual->c_class != TrueColor && visual->c_class != DirectColor) {
223b71206888 Initial import
thib
parents:
diff changeset
246 string err = "XKFont::PeerX11:PeerX11() : No supported Color mode of X : neither TrueColor nor DirectColor";
223b71206888 Initial import
thib
parents:
diff changeset
247 throw std::runtime_error(err);
223b71206888 Initial import
thib
parents:
diff changeset
248 }
223b71206888 Initial import
thib
parents:
diff changeset
249 /* ¿§¤Î½é´ü²½ */
223b71206888 Initial import
thib
parents:
diff changeset
250 white = visual->red_mask | visual->green_mask | visual->blue_mask;
223b71206888 Initial import
thib
parents:
diff changeset
251 black = 0;
223b71206888 Initial import
thib
parents:
diff changeset
252 if (visual->green_mask == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
253 string err = "XKFont::PeerX11:PeerX11() : Invalid Visual on X";
223b71206888 Initial import
thib
parents:
diff changeset
254 throw std::runtime_error(err);
223b71206888 Initial import
thib
parents:
diff changeset
255 }
223b71206888 Initial import
thib
parents:
diff changeset
256 shift = 0;
223b71206888 Initial import
thib
parents:
diff changeset
257 mask = visual->green_mask;
223b71206888 Initial import
thib
parents:
diff changeset
258 while(mask & 0x01) { shift++; mask >>= 1; }
223b71206888 Initial import
thib
parents:
diff changeset
259
223b71206888 Initial import
thib
parents:
diff changeset
260 int tablesize = mask+1;
223b71206888 Initial import
thib
parents:
diff changeset
261 colortable = new int[tablesize];
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
262 int i;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
263 for (i=0; i< tablesize; i++) {
0
223b71206888 Initial import
thib
parents:
diff changeset
264 colortable[i] = i*255/tablesize;
223b71206888 Initial import
thib
parents:
diff changeset
265 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
266
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
267 XSupportsLocale(); //FIXME: er... yes?
0
223b71206888 Initial import
thib
parents:
diff changeset
268
223b71206888 Initial import
thib
parents:
diff changeset
269 /* font Æɤ߹þ¤ß */
223b71206888 Initial import
thib
parents:
diff changeset
270 FontSetInfo fsinfo(display,fontname);
223b71206888 Initial import
thib
parents:
diff changeset
271 string fontset_name = fsinfo.Search(fontsize);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
272 char** missing_cl;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
273 int missing_cc;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
274 char* def_s;
0
223b71206888 Initial import
thib
parents:
diff changeset
275 printf("fontset %s\n",fontset_name.c_str());
223b71206888 Initial import
thib
parents:
diff changeset
276 fontset = XCreateFontSet(display, fontset_name.c_str(), &missing_cl, &missing_cc, &def_s);
223b71206888 Initial import
thib
parents:
diff changeset
277
223b71206888 Initial import
thib
parents:
diff changeset
278 if (fontset == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
279 delete[] colortable;
223b71206888 Initial import
thib
parents:
diff changeset
280 string err = string("XKFont::PeerX11:PeerX11() : Cannot create fontset ");
223b71206888 Initial import
thib
parents:
diff changeset
281 err += fontset_name; err += " (font name "; err += fontname; err += ")";
223b71206888 Initial import
thib
parents:
diff changeset
282 throw std::invalid_argument(err);
223b71206888 Initial import
thib
parents:
diff changeset
283 }
223b71206888 Initial import
thib
parents:
diff changeset
284
223b71206888 Initial import
thib
parents:
diff changeset
285 if (missing_cc != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
286 cerr << "XKFont::PeerX11:PeerX11() : Cannot found some fonts in the fontset"<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
287 cerr << " fontset: "<<fontset_name<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
288 cerr << " not found fonts:"<<endl;
223b71206888 Initial import
thib
parents:
diff changeset
289 int i; for (i=0; i<missing_cc; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
290 cerr << " " << missing_cl[i] << endl;
223b71206888 Initial import
thib
parents:
diff changeset
291 }
223b71206888 Initial import
thib
parents:
diff changeset
292 }
223b71206888 Initial import
thib
parents:
diff changeset
293
223b71206888 Initial import
thib
parents:
diff changeset
294 XFontSetExtents* extents = XExtentsOfFontSet(fontset);
223b71206888 Initial import
thib
parents:
diff changeset
295
223b71206888 Initial import
thib
parents:
diff changeset
296 width = extents->max_ink_extent.width;
223b71206888 Initial import
thib
parents:
diff changeset
297 height = extents->max_ink_extent.height;
223b71206888 Initial import
thib
parents:
diff changeset
298
223b71206888 Initial import
thib
parents:
diff changeset
299 /* calculate ascent / descent */
223b71206888 Initial import
thib
parents:
diff changeset
300 XFontStruct** font_structs; char** font_names;
223b71206888 Initial import
thib
parents:
diff changeset
301 int num_fonts = XFontsOfFontSet(fontset, &font_structs, &font_names);
223b71206888 Initial import
thib
parents:
diff changeset
302 printf("locale %s\n",XLocaleOfOM(XOMOfOC(fontset)));
223b71206888 Initial import
thib
parents:
diff changeset
303
223b71206888 Initial import
thib
parents:
diff changeset
304 ascent = 0;
223b71206888 Initial import
thib
parents:
diff changeset
305 for (i=0; i<num_fonts; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
306 ascent = MAX(ascent, font_structs[i]->ascent);
223b71206888 Initial import
thib
parents:
diff changeset
307 }
223b71206888 Initial import
thib
parents:
diff changeset
308
223b71206888 Initial import
thib
parents:
diff changeset
309 /* ÉÁ²èÍѤΠpixmap ¤òºîÀ® */
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
310 XGCValues gc_values;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
311 unsigned int gc_values_mask;
0
223b71206888 Initial import
thib
parents:
diff changeset
312 gc_values.function = GXcopy;
223b71206888 Initial import
thib
parents:
diff changeset
313 gc_values.fill_style = FillSolid;
223b71206888 Initial import
thib
parents:
diff changeset
314 gc_values.arc_mode = ArcPieSlice;
223b71206888 Initial import
thib
parents:
diff changeset
315 gc_values.subwindow_mode = ClipByChildren;
223b71206888 Initial import
thib
parents:
diff changeset
316 gc_values.graphics_exposures = False;
223b71206888 Initial import
thib
parents:
diff changeset
317 gc_values.foreground = white;
223b71206888 Initial import
thib
parents:
diff changeset
318 gc_values.background = black;
223b71206888 Initial import
thib
parents:
diff changeset
319 gc_values_mask = GCFunction | GCFillStyle | GCArcMode | GCSubwindowMode | GCGraphicsExposures | GCForeground | GCBackground;
223b71206888 Initial import
thib
parents:
diff changeset
320 gc = XCreateGC(display, w, gc_values_mask, &gc_values);
223b71206888 Initial import
thib
parents:
diff changeset
321
223b71206888 Initial import
thib
parents:
diff changeset
322 canvas = XCreatePixmap(display, w, width, height, DefaultDepth(display, scr));
223b71206888 Initial import
thib
parents:
diff changeset
323
223b71206888 Initial import
thib
parents:
diff changeset
324 /* ¥¤¥á¡¼¥¸Å¾Á÷ÍѤΠimage ¤ÎºîÀ® */
223b71206888 Initial import
thib
parents:
diff changeset
325 int ignore;
223b71206888 Initial import
thib
parents:
diff changeset
326 use_shm = false;
223b71206888 Initial import
thib
parents:
diff changeset
327 if (XShmQueryExtension(display) == True) {
223b71206888 Initial import
thib
parents:
diff changeset
328 x_shm_info.shmid = -1;
223b71206888 Initial import
thib
parents:
diff changeset
329 x_shm_info.shmaddr = (char*)-1;
223b71206888 Initial import
thib
parents:
diff changeset
330 image = XShmCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, NULL, &x_shm_info, width, height);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
331 if (image != NULL) {
0
223b71206888 Initial import
thib
parents:
diff changeset
332 x_shm_info.shmid = shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT | 0600);
223b71206888 Initial import
thib
parents:
diff changeset
333 if (x_shm_info.shmid == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
334 XDestroyImage(image);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
335 image = NULL;
0
223b71206888 Initial import
thib
parents:
diff changeset
336 goto no_shm;
223b71206888 Initial import
thib
parents:
diff changeset
337 }
223b71206888 Initial import
thib
parents:
diff changeset
338 x_shm_info.readOnly = False;
223b71206888 Initial import
thib
parents:
diff changeset
339 x_shm_info.shmaddr = (char*) shmat(x_shm_info.shmid, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
340 image->data = x_shm_info.shmaddr;
223b71206888 Initial import
thib
parents:
diff changeset
341 if (x_shm_info.shmaddr == (char*) -1) {
223b71206888 Initial import
thib
parents:
diff changeset
342 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
343 shmctl(x_shm_info.shmid, IPC_RMID, 0);
223b71206888 Initial import
thib
parents:
diff changeset
344 image = 0;
223b71206888 Initial import
thib
parents:
diff changeset
345 goto no_shm;
223b71206888 Initial import
thib
parents:
diff changeset
346 }
223b71206888 Initial import
thib
parents:
diff changeset
347 XShmAttach(display, &x_shm_info);
223b71206888 Initial import
thib
parents:
diff changeset
348 XSync(display, False);
223b71206888 Initial import
thib
parents:
diff changeset
349 shmctl(x_shm_info.shmid, IPC_RMID, 0);
223b71206888 Initial import
thib
parents:
diff changeset
350 use_shm = true;
223b71206888 Initial import
thib
parents:
diff changeset
351 }
223b71206888 Initial import
thib
parents:
diff changeset
352 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
353 no_shm: //TODO: Understand why he did it, and supress it if needed
0
223b71206888 Initial import
thib
parents:
diff changeset
354 if (image == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
355 use_shm = false;
223b71206888 Initial import
thib
parents:
diff changeset
356 image = XCreateImage(display, visual, DefaultDepth(display, scr), ZPixmap, 0, 0, width, height, 32, 0);
223b71206888 Initial import
thib
parents:
diff changeset
357 image->data = (char*)malloc(image->bytes_per_line * image->height);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
358 if (image->data == NULL) {
0
223b71206888 Initial import
thib
parents:
diff changeset
359 XDestroyImage(image);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
360 image = NULL;
0
223b71206888 Initial import
thib
parents:
diff changeset
361 throw bad_alloc();
223b71206888 Initial import
thib
parents:
diff changeset
362 }
223b71206888 Initial import
thib
parents:
diff changeset
363 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
364
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
365 Glyph g;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
366 GlyphCreate(0xa1a2, &g); //FIXME: Two calls? Huh?
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
367 GlyphCreate(0xa4a3, &g);
0
223b71206888 Initial import
thib
parents:
diff changeset
368 }
223b71206888 Initial import
thib
parents:
diff changeset
369
223b71206888 Initial import
thib
parents:
diff changeset
370 PeerX11::~PeerX11() {
223b71206888 Initial import
thib
parents:
diff changeset
371 if (display) {
223b71206888 Initial import
thib
parents:
diff changeset
372 if (fontset) XFreeFontSet(display, fontset);
223b71206888 Initial import
thib
parents:
diff changeset
373 if (gc) XFlushGC(display, gc);
223b71206888 Initial import
thib
parents:
diff changeset
374 if (canvas) XFreePixmap(display, canvas);
223b71206888 Initial import
thib
parents:
diff changeset
375 if (image) {
223b71206888 Initial import
thib
parents:
diff changeset
376 if (use_shm) XShmDetach(display, &x_shm_info);
223b71206888 Initial import
thib
parents:
diff changeset
377 XDestroyImage(image);
223b71206888 Initial import
thib
parents:
diff changeset
378 if (use_shm) shmdt(x_shm_info.shmaddr);
223b71206888 Initial import
thib
parents:
diff changeset
379 }
223b71206888 Initial import
thib
parents:
diff changeset
380 if (colortable) delete[] colortable;
223b71206888 Initial import
thib
parents:
diff changeset
381 }
223b71206888 Initial import
thib
parents:
diff changeset
382 }
223b71206888 Initial import
thib
parents:
diff changeset
383
223b71206888 Initial import
thib
parents:
diff changeset
384 bool PeerX11::GlyphCreate(unsigned int code, Glyph* glyph) {
223b71206888 Initial import
thib
parents:
diff changeset
385 XRectangle ink, logic;
223b71206888 Initial import
thib
parents:
diff changeset
386
223b71206888 Initial import
thib
parents:
diff changeset
387 char str[3]={0,0,0};
223b71206888 Initial import
thib
parents:
diff changeset
388
223b71206888 Initial import
thib
parents:
diff changeset
389 if ( (code>>8)&0xff){
223b71206888 Initial import
thib
parents:
diff changeset
390 str[0]=code>>8;
223b71206888 Initial import
thib
parents:
diff changeset
391 str[1]=code & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
392 } else {
223b71206888 Initial import
thib
parents:
diff changeset
393 str[0]=code;
223b71206888 Initial import
thib
parents:
diff changeset
394 }
223b71206888 Initial import
thib
parents:
diff changeset
395
223b71206888 Initial import
thib
parents:
diff changeset
396 XmbTextExtents(fontset, str, strlen(str),&ink,&logic);
223b71206888 Initial import
thib
parents:
diff changeset
397 int cwidth = logic.width;
223b71206888 Initial import
thib
parents:
diff changeset
398 XmbDrawImageString(display, canvas, fontset, gc, 0, -logic.y, str, strlen(str));
223b71206888 Initial import
thib
parents:
diff changeset
399 if (use_shm) {
223b71206888 Initial import
thib
parents:
diff changeset
400 XShmGetImage(display, canvas, image, 0, 0, AllPlanes);
223b71206888 Initial import
thib
parents:
diff changeset
401 } else {
223b71206888 Initial import
thib
parents:
diff changeset
402 XGetSubImage(display, canvas, 0, 0, width, height, AllPlanes, ZPixmap, image, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
403 }
223b71206888 Initial import
thib
parents:
diff changeset
404 XSync(display, False);
223b71206888 Initial import
thib
parents:
diff changeset
405 glyph->bitmap.buffer = new unsigned char[logic.width*logic.height];
223b71206888 Initial import
thib
parents:
diff changeset
406 int i;
223b71206888 Initial import
thib
parents:
diff changeset
407 unsigned char* mem = (unsigned char*) image->data;
223b71206888 Initial import
thib
parents:
diff changeset
408 unsigned char* dest = glyph->bitmap.buffer;
223b71206888 Initial import
thib
parents:
diff changeset
409 int bpp = image->bytes_per_line/width;
223b71206888 Initial import
thib
parents:
diff changeset
410 int bpl = image->bytes_per_line;
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 47
diff changeset
411 for (i=0; i < logic.height; i++) {
0
223b71206888 Initial import
thib
parents:
diff changeset
412 unsigned char* m = mem;
223b71206888 Initial import
thib
parents:
diff changeset
413 int j; for (j=0; j<cwidth; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
414 *dest = colortable[((read_little_endian_int((char*)m))>>shift) & mask];
223b71206888 Initial import
thib
parents:
diff changeset
415 dest++;
223b71206888 Initial import
thib
parents:
diff changeset
416 m += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
417 }
223b71206888 Initial import
thib
parents:
diff changeset
418 mem += bpl;
223b71206888 Initial import
thib
parents:
diff changeset
419 }
223b71206888 Initial import
thib
parents:
diff changeset
420 glyph->bitmap_left = logic.x;
223b71206888 Initial import
thib
parents:
diff changeset
421 glyph->bitmap_top = -logic.y;
223b71206888 Initial import
thib
parents:
diff changeset
422 glyph->bitmap.width = logic.width;
223b71206888 Initial import
thib
parents:
diff changeset
423 glyph->bitmap.rows = logic.height;
223b71206888 Initial import
thib
parents:
diff changeset
424 glyph->advance.x = logic.width + 1;
223b71206888 Initial import
thib
parents:
diff changeset
425 glyph->advance.y = logic.height+ 1;
223b71206888 Initial import
thib
parents:
diff changeset
426
223b71206888 Initial import
thib
parents:
diff changeset
427 return true;
223b71206888 Initial import
thib
parents:
diff changeset
428 }
223b71206888 Initial import
thib
parents:
diff changeset
429 };
223b71206888 Initial import
thib
parents:
diff changeset
430
223b71206888 Initial import
thib
parents:
diff changeset
431 #endif /* USE_X11 */