Debian Codegraph @

tools/x11/xf2pcx.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Utility to convert X11 fonts to a PCX file, ready for use in the
12 * grabber.
13 *
14 * By Michael Bukin, 1998.
15 *
16 * Minor changes in 2002 by Peter Wang.
17 *
18 * Minor changes in 2003 by Elias Pschernig.
19 *
20 * See readme.txt for copyright information.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <allegro5/allegro5.h>
27
28 #ifndef SCAN_DEPEND
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #endif
32
33
34 /* usage:
35 * Show usage information.
36 */
37 static void usage(char *argv0)
38 {
39 printf("Usage: %s [OPTIONS]\n\n", argv0);
40 printf(" -f FONTNAME Font name pattern ('*' by default).\n"
41 " -o FILENAME Output file name ('font.pcx' by default).\n"
42 " -r NUM-NUM Character range, default = 0x20-0xff.\n"
43 " -c COLOR Character color (default 000000000 -- black).\n"
44 " -b COLOR Background color (default 255255255 -- white).\n"
45 " -g COLOR Grid color (default 255000255 -- magenta).\n"
46 " Colors are specified as RRRGGGBBB values,\n"
47 " with each RRR, GGG and BBB in the range [0, 255].\n"
48 " -h This help.\n"
49 "\n"
50 "Find the font you want to convert using xlsfonts or xfontsel.\n\n"
51 "Example 1:\n"
52 "bash> xlsfonts -fn '*' | less\n"
53 "bash> %s -f '-adobe-courier-medium-r-normal--34-*'\n"
54 "bash> grabber\n\n"
55 "In grabber, create a new font 'Object|New|Font'. Use 'Object|Grab'\n"
56 "on this object and choose the pcx file with the font (font.pcx).\n\n"
57 "Example 2:\n"
58 "./xf2pcx -f '-*-clearlyu-*-*-*-*-17-*-*-*-p--iso10646-1' -r 0x2800-0x28ff -o braille.pcx\n\n"
59 "Writes the Braille alphabet into braille.pcx.\n", argv0);
60 }
61
62
63 int main(int argc, char *argv[])
64 {
65 /* default values for -f, -i, -r, -c, -b, -g */
66 char *fontname = "*";
67 char *filename = "font.pcx";
68 int start_char = 32, end_char = 255;
69 int ccolor = 000000000;
70 int bcolor = 255255255;
71 int gcolor = 255000255;
72 /* X11 variables */
73 Display *display;
74 int screen_number;
75 int default_depth;
76 Window window;
77 Font font;
78 GC gc, gc2;
79 Pixmap pixmap;
80 XImage *image;
81 XCharStruct overall;
82 /* misc variables */
83 int bitmap_width, bitmap_height;
84 int max_ascent, max_descent;
85 int max_width, max_height;
86 int i, opt, x, y, cx, cy, sx, sy, lines;
87 unsigned long black, white;
88 BITMAP *bitmap;
89 RGB palette[256];
90
91 /* show usage if no options */
92 if (argc == 1) {
93 usage(argv[0]);
94 exit(EXIT_SUCCESS);
95 }
96
97 /* only to access bitmap operations */
98 install_allegro(SYSTEM_NONE, &errno, atexit);
99
100 /* parse options */
101 opterr = 0;
102 while ((opt = getopt(argc, argv, "f:o:z:c:b:g:r:h")) != EOF) {
103 switch (opt) {
104 case 'f':
105 fontname = optarg;
106 break;
107 case 'o':
108 filename = optarg;
109 break;
110 case 'c':
111 ccolor = atol(optarg);
112 break;
113 case 'b':
114 bcolor = atol(optarg);
115 break;
116 case 'g':
117 gcolor = atol(optarg);
118 break;
119 case 'r':
120 {
121 char *str;
122 start_char = strtol(optarg, &str, 0);
123 end_char = strtol(str + 1, NULL, 0);
124 break;
125 }
126 case 'h':
127 usage(argv[0]);
128 exit(EXIT_SUCCESS);
129 default:
130 fprintf(stderr, "%s: unrecognized option -- '%c'\n", argv[0],
131 optopt);
132 fprintf(stderr, "%s: try '%s -h' for more information\n",
133 argv[0], argv[0]);
134 exit(EXIT_FAILURE);
135 }
136 }
137
138 /* open display */
139 display = XOpenDisplay(0);
140 if (display == 0) {
141 fprintf(stderr, "%s: XOpenDisplay failed\n", argv[0]);
142 exit(EXIT_FAILURE);
143 }
144
145 /* default screen number and window */
146 screen_number = XDefaultScreen(display);
147 default_depth = XDefaultDepth(display, screen_number);
148 window = XDefaultRootWindow(display);
149
150 /* load font */
151 font = XLoadFont(display, fontname);
152
153 /* create gcs */
154 {
155 unsigned long val_mask;
156 XGCValues val_bits;
157 val_mask = GCForeground | GCBackground | GCFont | GCFunction;
158 val_bits.function = GXcopy;
159 val_bits.foreground = white = WhitePixel(display, screen_number);
160 val_bits.background = black = BlackPixel(display, screen_number);
161 val_bits.font = font;
162 gc = XCreateGC(display, window, val_mask, &val_bits);
163 val_mask = GCForeground;
164 val_bits.foreground = black;
165 gc2 = XCreateGC(display, window, val_mask, &val_bits);
166 }
167
168 /* query font ascent and descent */
169 {
170 XFontStruct *xfs;
171 int min, max;
172 xfs = XQueryFont(display, font);
173 max_ascent = xfs->ascent;
174 max_descent = xfs->descent;
175
176 if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
177 min = xfs->min_char_or_byte2;
178 max = xfs->max_char_or_byte2;
179 }
180 else {
181 min = (xfs->min_byte1 << 8) + xfs->min_char_or_byte2;
182 max = (xfs->max_byte1 << 8) + xfs->max_char_or_byte2;
183 }
184
185 if (start_char < min || end_char > max)
186 fprintf(stderr,
187 "You specified characters %04x-%04x, but this font "
188 "only has the range %04x-%04x\n", start_char, end_char,
189 min, max);
190
191 XFreeFontInfo(NULL, xfs, 0);
192 }
193
194 /* calculate bitmap width and maximum ascent and descent of characters
195 * (can exceed the font ascent/descent queried above!) */
196 max_width = 0;
197 lines = 1 + (end_char - start_char) / 16;
198 for (cy = 0; cy < lines; cy++) {
199
200 for (cx = 0; cx < 16 && start_char + cy * 16 + cx <= end_char; cx++) {
201 int dir, ascent, descent;
202 int width;
203 XChar2b string[2] = { {0, 0}, {0, 0} };
204
205 /* query character size */
206 string[0].byte1 = (start_char + cy * 16 + cx) >> 8;
207 string[0].byte2 = (start_char + cy * 16 + cx) & 255;
208 XQueryTextExtents16(display, font, string, 1, &dir, &ascent,
209 &descent, &overall);
210 width = overall.width;
211 if (width < 1)
212 width = 1;
213
214 if (width > max_width)
215 max_width = width;
216
217 if (max_ascent < overall.ascent)
218 max_ascent = overall.ascent;
219 if (max_descent < overall.descent)
220 max_descent = overall.descent;
221 }
222
223 }
224
225 max_height = max_ascent + max_descent;
226
227 bitmap_width = (max_width + 1) * 16 + 1;
228 bitmap_height = (max_height + 1) * lines + 1;
229
230 /* create bitmap */
231 bitmap = create_bitmap(bitmap_width, bitmap_height);
232 if (bitmap == 0) {
233 fprintf(stderr, "%s: can not create bitmap\n", argv[0]);
234 exit(EXIT_FAILURE);
235 }
236
237 /* fill with filler color */
238 clear_to_color(bitmap, 255);
239
240 /* process all characters */
241 sy = 1;
242 for (cy = 0; cy < lines; cy++) {
243
244 sx = 1;
245 for (cx = 0; cx < 16 && start_char + cy * 16 + cx <= end_char; cx++) {
246 int dir, ascent, descent;
247 XChar2b string[2] = { {0, 0}, {0, 0} };
248
249 /* query character size */
250 string[0].byte1 = (start_char + cy * 16 + cx) >> 8;
251 string[0].byte2 = (start_char + cy * 16 + cx) & 255;
252 XQueryTextExtents16(display, font, string, 1, &dir, &ascent,
253 &descent, &overall);
254
255 if (overall.width < 1)
256 overall.width = 1;
257
258 /* create pixmap and draw character there */
259 pixmap =
260 XCreatePixmap(display, window, overall.width, max_height,
261 default_depth);
262 /* some fonts draw outside their ascent/descent, so we need to clear
263 * the pixmap before drawing the glyph */
264 XFillRectangle(display, pixmap, gc2, 0, 0, overall.width,
265 max_height);
266 XDrawImageString16(display, pixmap, gc, 0, max_ascent, string, 1);
267
268 /* create image with pixmap contents */
269 image =
270 XGetImage(display, pixmap, 0, 0, overall.width, max_height,
271 AllPlanes, ZPixmap);
272 if (image == 0) {
273 fprintf(stderr, "%s: can not get image\n", argv[0]);
274 exit(EXIT_FAILURE);
275 }
276
277 /* copy image to bitmap */
278 for (y = 0; y < max_height; y++)
279 for (x = 0; x < overall.width; x++) {
280 if (XGetPixel(image, x, y) == white)
281 putpixel(bitmap, sx + x, sy + y, 1);
282 else
283 putpixel(bitmap, sx + x, sy + y, 0);
284 }
285
286 XDestroyImage(image);
287 XFreePixmap(display, pixmap);
288
289 sx += max_width + 1;
290 }
291 sy += max_height + 1;
292 }
293
294 /* initialize palette */
295 for (i = 0; i < 256; i++)
296 palette[i].r = palette[i].g = palette[i].b = 0;
297
298 #define CLAMP(v) (((v / 4) > 63) ? 63 : (v / 4))
299 palette[0].r = CLAMP(bcolor / 1000000);
300 palette[0].g = CLAMP((bcolor % 1000000) / 1000);
301 palette[0].b = CLAMP(bcolor % 1000);
302 palette[1].r = CLAMP(ccolor / 1000000);
303 palette[1].g = CLAMP((ccolor % 1000000) / 1000);
304 palette[1].b = CLAMP(ccolor % 1000);
305 palette[255].r = CLAMP(gcolor / 1000000);
306 palette[255].g = CLAMP((gcolor % 1000000) / 1000);
307 palette[255].b = CLAMP(gcolor % 1000);
308 #undef CLAMP
309 save_pcx(filename, bitmap, palette);
310
311 /* clean up */
312 destroy_bitmap(bitmap);
313 XFreeGC(display, gc);
314 XFreeGC(display, gc2);
315 XUnloadFont(display, font);
316 XCloseDisplay(display);
317
318 exit(EXIT_SUCCESS);
319 }