"Fossies" - the Fresh Open Source Software Archive 
Member "xfontlst-0.2.0/xshwfnt.c" (22 Sep 2010, 6967 Bytes) of package /linux/privat/old/xfontlst-0.2.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "xshwfnt.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 xshwfnt Version 0.2.0 - Show X Windows Font
3 Copyright (C) 2001-2010 dondalah721@yahoo.com (Dondalah)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /* to define the escape key */
21 #define XK_MISCELLANY 1
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <sys/times.h>
28 #include <X11/Xlib.h>
29 #include <X11/keysymdef.h>
30 #include <assert.h>
31 #include <unistd.h>
32
33 #define HDRFONT "10x20"
34 #define NIL (0)
35 #define SCRLFONT 'f'
36 #define FONTBEG 'b'
37 #define NEXTFONT 'n'
38 #define PREVFONT 'p'
39
40 int whiteColor,blackColor;
41 int runflg,expflg,prevflg,strtflg;
42 int nextflg,scrlflg;
43 Display *dpy;
44 Window w;
45 GC gc;
46 Font fontid;
47 Font fnt10x20;
48 XFontStruct *str7x14;
49 int rootww; /* root window width */
50 int rootwh; /* root window height */
51 int dpywdth; /* display window width */
52 int dpyhght; /* display window height */
53
54
55 void putstx(pgm)
56 char *pgm;
57 {
58 fprintf(stderr,"Usage: %s <fontname>\n",
59 pgm);
60 exit(1);
61 } /* putstx */
62
63 /* X Windows code is based on: */
64 /* http://tronche.lri.fr:8000/gui/x/xlib-tutorial/2nd-program-anatomy.html */
65
66 void getkey()
67 {
68 int i,rslt,msk;
69 int symbol;
70 int XCheckMaskEvent();
71 XEvent e;
72 XKeyEvent *k;
73
74 msk = KeyPressMask|ExposureMask;
75
76 XSelectInput(dpy, w, msk);
77
78 while (1)
79 {
80 XNextEvent(dpy,&e);
81 expflg = prevflg = strtflg = 0;
82 scrlflg = nextflg = 0;
83 runflg = 1;
84 if (e.type == KeyPress)
85 {
86 k = (XKeyEvent *) &e;
87 symbol = XLookupKeysym(k,0);
88 symbol = tolower(symbol);
89 if (symbol == XK_Escape)
90 {
91 runflg = 0;
92 break;
93 } /* if quit */
94 else if (symbol == NEXTFONT)
95 {
96 nextflg = 1;
97 break;
98 } /* if quit */
99 else if (symbol == SCRLFONT)
100 {
101 scrlflg = 1;
102 break;
103 } /* if quit */
104 else if (symbol == PREVFONT)
105 {
106 prevflg = 1;
107 break;
108 } /* if quit */
109 else if (symbol == FONTBEG)
110 {
111 strtflg = 1;
112 break;
113 } /* if quit */
114 } /* if keypress event */
115 else if (e.type == Expose)
116 {
117 expflg = 1;
118 break;
119 } /* if expose event */
120 } /* if event received */
121 } /* getkey */
122
123 void initx()
124 {
125 int i,rslt;
126 char title[64];
127
128 dpy = XOpenDisplay(NIL);
129
130 if (dpy == NULL)
131 {
132 fprintf(stderr,"X Windows failure\n");
133 exit(1);
134 } /* if X Windows is not active */
135
136 assert(dpy);
137
138 rootww = XDisplayWidth(dpy,0);
139 rootwh = XDisplayHeight(dpy,0);
140 dpywdth = rootww - 80;
141 dpyhght = rootwh - 100;
142
143 whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
144 blackColor = BlackPixel(dpy, DefaultScreen(dpy));
145
146 w = XCreateSimpleWindow(dpy,
147 DefaultRootWindow(dpy),
148 0, 0,
149 dpywdth, dpyhght,
150 0, whiteColor,
151 whiteColor);
152
153 XSelectInput(dpy, w, StructureNotifyMask);
154
155 XMapWindow(dpy, w);
156
157 gc = XCreateGC(dpy, w, 0, NIL);
158
159 /* fontid = (Font) XLoadFont(dpy,"linux8x16"); */
160
161 /* XSetFont(dpy,gc,fontid); */
162
163 XSetForeground(dpy, gc, blackColor);
164
165 XSetWindowBorderWidth(dpy, w, 40);
166
167 sprintf(title,"xshwfnt");
168 XStoreName(dpy,w,title);
169
170 XMoveWindow(dpy,w,0,0);
171
172 while(1)
173 {
174 XEvent e;
175 XNextEvent(dpy, &e);
176 if (e.type == MapNotify) break;
177 } /* wait for window initialization */
178 } /* initx */
179
180 void shwfont(fntname)
181 char *fntname;
182 {
183 int i,j,row,hght,hdrlen;
184 int rowwdth,hdrwdth;
185 Font fontid;
186 XFontStruct *fntstr;
187 char *p,*q,*r,*s;
188 unsigned char str[384];
189 unsigned char seg[64];
190 hdrlen = strlen(fntname);
191 XSetBackground(dpy, gc, whiteColor);
192 XSetForeground(dpy, gc, blackColor);
193 XSetFont(dpy,gc,fnt10x20);
194 XDrawImageString(dpy,w,gc,10,40,fntname,hdrlen);
195 fontid = (Font) XLoadFont(dpy,fntname);
196 fntstr = (XFontStruct *) XQueryFont(dpy,fontid);
197 hght = fntstr->ascent + fntstr->descent;
198 XSetFont(dpy,gc,fontid);
199 p = str;
200 q = p + 256;
201 i = 0;
202 while (p < q) *p++ = i++;
203 *p = '\0';
204 p = str;
205 *p = 32;
206 r = seg;
207 s = r + 32;
208 row = 60 + hght;
209 while (p < q)
210 {
211 strncpy(seg,p,32);
212 *s = '\0';
213 rowwdth = XTextWidth(fntstr,seg,32);
214 /* 624 - 20 */
215 if (rowwdth < 604)
216 {
217 XDrawImageString(dpy,w,gc,20,row,r,32);
218 p += 32;
219 } /* enough room on line */
220 /* 1248 - 20 */
221 else if (rowwdth < 1228)
222 {
223 XDrawImageString(dpy,w,gc,20,row,r,16);
224 p += 16;
225 } /* cut line in half */
226 else
227 {
228 XDrawImageString(dpy,w,gc,20,row,r,8);
229 p += 8;
230 } /* cut line in half */
231 row += (hght + 4);
232 if ((row + hght + 4) > dpyhght && p < q)
233 {
234 getkey();
235 if (nextflg || prevflg || !runflg) break;
236 else if (strtflg) p = str;
237 XClearWindow(dpy,w);
238 row = 60 + hght;
239 XSetFont(dpy,gc,fnt10x20);
240 XDrawImageString(dpy,w,gc,10,40,fntname,hdrlen);
241 XSetFont(dpy,gc,fontid);
242 } /* next page */
243 } /* for each 32 char in charset */
244 if (runflg && !prevflg && !nextflg) getkey();
245 XUnloadFont(dpy,fontid);
246 XClearWindow(dpy,w);
247 } /* shwfont */
248
249 int main(argc,argv)
250 int argc;
251 char **argv;
252 {
253 int i,fnts;
254 char **font;
255 char fontmask[256];
256 if (argc != 2) putstx(*argv);
257 strcpy(fontmask,*(argv+1));
258 initx(fontmask);
259 fnt10x20 = (Font) XLoadFont(dpy,HDRFONT);
260 str7x14 = (XFontStruct *) XQueryFont(dpy,fnt10x20);
261 font = (char **) XListFonts(dpy,fontmask,999999,&fnts);
262 printf("\"%s\" Total fonts %d\n", fontmask, fnts);
263 runflg = 1;
264 expflg = prevflg = strtflg = 0;
265 for (i=0;runflg&&i<fnts;i++)
266 {
267 do
268 { /* do while expose flag is set */
269 do
270 {
271 prevflg = strtflg = expflg =
272 nextflg = scrlflg = 0;
273 shwfont(font[i]);
274 if (prevflg) i--;
275 if (i < 0) i = 0;
276 } while (prevflg || strtflg);
277 } while (expflg);
278 } /* for each font */
279 XFreeFontInfo(NULL,str7x14,1);
280 XUnloadFont(dpy,fnt10x20);
281 return(0);
282 } /* main */