summaryrefslogtreecommitdiff
path: root/src/glut/glx/capturexfont.c
blob: fd63fb7c49f98e2b2accb200cbe9696283cb7857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

/* Copyright (c) Mark J. Kilgard, 1994. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

/* capturexfont.c connects to an X server and downloads a
   bitmap font from which a C source file is generated,
   encoding  the font for GLUT's use. Example usage:
   capturexfont.c 9x15 glutBitmap9By15 > glut_9x15.c */

#ifdef __VMS
#include <GL/vms_x_fix.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <GL/gl.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#define MAX_GLYPHS_PER_GRAB 512  /* This is big enough for 2^9
                                    glyph character sets */

static void
outputChar(int num, int width, int height,
  int xoff, int yoff, int advance, int data)
{
  if (width == 0 || height == 0) {
    printf("#ifdef _WIN32\n");
    printf("/* XXX Work around Microsoft OpenGL 1.1 bug where glBitmap with\n");
    printf("   a height or width of zero does not advance the raster position\n");
    printf("   as specified by OpenGL. (Cosmo OpenGL does not have this bug.) */\n");
    printf("static const GLubyte ch%ddata[] = { 0x0 };\n", num);
    printf("static const BitmapCharRec ch%d = {", num);
    printf("%d,", 0);
    printf("%d,", 0);
    printf("%d,", xoff);
    printf("%d,", yoff);
    printf("%d,", advance);
    printf("ch%ddata", num);
    printf("};\n");
    printf("#else\n");
  }
  printf("static const BitmapCharRec ch%d = {", num);
  printf("%d,", width);
  printf("%d,", height);
  printf("%d,", xoff);
  printf("%d,", yoff);
  printf("%d,", advance);
  if (data) {
    printf("ch%ddata", num);
  } else {
    printf("0");
  }
  printf("};\n");
  if (width == 0 || height == 0) {
    printf("#endif\n");
  }
  printf("\n");
}

/* Can't just use isprint because it only works for the range
   of ASCII characters (ie, TRUE for isascii) and capturexfont
   might be run on 16-bit fonts. */
#define PRINTABLE(ch)  (isascii(ch) ? isprint(ch) : 0)

void
captureXFont(Display * dpy, Font font, char *xfont, char *name)
{
  int first, last, count;
  int cnt, len;
  Pixmap offscreen;
  Window drawable;
  XFontStruct *fontinfo;
  XImage *image;
  GC xgc;
  XGCValues values;
  int width, height;
  int i, j, k;
  XCharStruct *charinfo;
  XChar2b character;
  GLubyte *bitmapData;
  int x, y;
  int spanLength;
  int charWidth, charHeight, maxSpanLength, pixwidth;
  int grabList[MAX_GLYPHS_PER_GRAB];
  int glyphsPerGrab = MAX_GLYPHS_PER_GRAB;
  int numToGrab;
  int rows, pages, byte1, byte2, index;
  int nullBitmap;

  drawable = RootWindow(dpy, DefaultScreen(dpy));

  fontinfo = XQueryFont(dpy, font);
  pages = fontinfo->max_char_or_byte2 - fontinfo->min_char_or_byte2 + 1;
  first = (fontinfo->min_byte1 << 8) + fontinfo->min_char_or_byte2;
  last = (fontinfo->max_byte1 << 8) + fontinfo->max_char_or_byte2;
  count = last - first + 1;

  width = fontinfo->max_bounds.rbearing -
    fontinfo->min_bounds.lbearing;
  height = fontinfo->max_bounds.ascent +
    fontinfo->max_bounds.descent;
  /* 16-bit fonts have more than one row; indexing into
     per_char is trickier. */
  rows = fontinfo->max_byte1 - fontinfo->min_byte1 + 1;

  maxSpanLength = (width + 7) / 8;
  /* For portability reasons we don't use alloca for
     bitmapData, but we could. */
  bitmapData = malloc(height * maxSpanLength);
  /* Be careful determining the width of the pixmap; the X
     protocol allows pixmaps of width 2^16-1 (unsigned short
     size) but drawing coordinates max out at 2^15-1 (signed
     short size).  If the width is too large, we need to limit
     the glyphs per grab. */
  if ((glyphsPerGrab * 8 * maxSpanLength) >= (1 << 15)) {
    glyphsPerGrab = (1 << 15) / (8 * maxSpanLength);
  }
  pixwidth = glyphsPerGrab * 8 * maxSpanLength;
  offscreen = XCreatePixmap(dpy, drawable, pixwidth, height, 1);

  values.font = font;
  values.background = 0;
  values.foreground = 0;
  xgc = XCreateGC(dpy, offscreen,
    GCFont | GCBackground | GCForeground, &values);
  XFillRectangle(dpy, offscreen, xgc, 0, 0,
    8 * maxSpanLength * glyphsPerGrab, height);
  XSetForeground(dpy, xgc, 1);

  numToGrab = 0;
  if (fontinfo->per_char == NULL) {
    charinfo = &(fontinfo->min_bounds);
    charWidth = charinfo->rbearing - charinfo->lbearing;
    charHeight = charinfo->ascent + charinfo->descent;
    spanLength = (charWidth + 7) / 8;
  }
  printf("\n/* GENERATED FILE -- DO NOT MODIFY */\n\n");
  printf("#include \"glutbitmap.h\"\n\n");
  for (i = first; count; i++, count--) {
    int undefined;
    if (rows == 1) {
      undefined = (fontinfo->min_char_or_byte2 > i ||
        fontinfo->max_char_or_byte2 < i);
    } else {
      byte2 = i & 0xff;
      byte1 = i >> 8;
      undefined = (fontinfo->min_char_or_byte2 > byte2 ||
        fontinfo->max_char_or_byte2 < byte2 ||
        fontinfo->min_byte1 > byte1 ||
        fontinfo->max_byte1 < byte1);

    }
    if (undefined) {
      goto PossiblyDoGrab;
    }
    if (fontinfo->per_char != NULL) {
      if (rows == 1) {
        index = i - fontinfo->min_char_or_byte2;
      } else {
        byte2 = i & 0xff;
        byte1 = i >> 8;
        index =
          (byte1 - fontinfo->min_byte1) * pages +
          (byte2 - fontinfo->min_char_or_byte2);
      }
      charinfo = &(fontinfo->per_char[index]);
      charWidth = charinfo->rbearing - charinfo->lbearing;
      charHeight = charinfo->ascent + charinfo->descent;
      if (charWidth == 0 || charHeight == 0) {
        if (charinfo->width != 0) {
          /* Still must move raster pos even if empty character 

           */
          outputChar(i, 0, 0, 0, 0, charinfo->width, 0);
        }
        goto PossiblyDoGrab;
      }
    }
    grabList[numToGrab] = i;
    character.byte2 = i & 255;
    character.byte1 = i >> 8;

    /* XXX We could use XDrawImageString16 which would also
       paint the backing rectangle but X server bugs in some
       scalable font rasterizers makes it more effective to do
       XFillRectangles to clear the pixmap and then
       XDrawImage16 for the text.  */
    XDrawString16(dpy, offscreen, xgc,
      -charinfo->lbearing + 8 * maxSpanLength * numToGrab,
      charinfo->ascent, &character, 1);

    numToGrab++;

  PossiblyDoGrab:

    if (numToGrab >= glyphsPerGrab || count == 1) {
      image = XGetImage(dpy, offscreen,
        0, 0, pixwidth, height, 1, XYPixmap);
      for (j = numToGrab - 1; j >= 0; j--) {
        if (fontinfo->per_char != NULL) {
          byte2 = grabList[j] & 0xff;
          byte1 = grabList[j] >> 8;
          index =
            (byte1 - fontinfo->min_byte1) * pages +
            (byte2 - fontinfo->min_char_or_byte2);
          charinfo = &(fontinfo->per_char[index]);
          charWidth = charinfo->rbearing - charinfo->lbearing;
          charHeight = charinfo->ascent + charinfo->descent;
          spanLength = (charWidth + 7) / 8;
        }
        memset(bitmapData, 0, height * spanLength);
        for (y = 0; y < charHeight; y++) {
          for (x = 0; x < charWidth; x++) {
            if (XGetPixel(image, j * maxSpanLength * 8 + x,
                charHeight - 1 - y)) {
              /* Little endian machines (such as DEC Alpha)
                 could  benefit from reversing the bit order
                 here and changing the GL_UNPACK_LSB_FIRST
                 parameter in glutBitmapCharacter to GL_TRUE. */
              bitmapData[y * spanLength + x / 8] |=
                (1 << (7 - (x & 7)));
            }
          }
        }
        if (PRINTABLE(grabList[j])) {
          printf("/* char: 0x%x '%c' */\n\n",
            grabList[j], grabList[j]);
        } else {
          printf("/* char: 0x%x */\n\n", grabList[j]);
        }

        /* Determine if the bitmap is null. */
        nullBitmap = 1;
        len = (charinfo->ascent + charinfo->descent) *
          ((charinfo->rbearing - charinfo->lbearing + 7) / 8);
        cnt = 0;
        while (cnt < len) {
          for (k = 0; k < 16 && cnt < len; k++, cnt++) {
            if (bitmapData[cnt] != 0) {
              nullBitmap = 0;
            }
          }
        }

        if (!nullBitmap) {
          printf("static const GLubyte ch%ddata[] = {\n", grabList[j]);
          len = (charinfo->ascent + charinfo->descent) *
            ((charinfo->rbearing - charinfo->lbearing + 7) / 8);
          cnt = 0;
          while (cnt < len) {
            for (k = 0; k < 16 && cnt < len; k++, cnt++) {
              printf("0x%x,", bitmapData[cnt]);
            }
            printf("\n");
          }
          printf("};\n\n");
        } else {
          charWidth = 0;
          charHeight = 0;
        }

        outputChar(grabList[j], charWidth, charHeight,
          -charinfo->lbearing, charinfo->descent,
          charinfo->width, !nullBitmap);
      }
      XDestroyImage(image);
      numToGrab = 0;
      if (count > 0) {
        XSetForeground(dpy, xgc, 0);
        XFillRectangle(dpy, offscreen, xgc, 0, 0,
          8 * maxSpanLength * glyphsPerGrab, height);
        XSetForeground(dpy, xgc, 1);
      }
    }
  }
  XFreeGC(dpy, xgc);
  XFreePixmap(dpy, offscreen);
  /* For portability reasons we don't use alloca for
     bitmapData, but we could. */
  free(bitmapData);

  printf("static const BitmapCharRec * const chars[] = {\n");
  for (i = first; i <= last; i++) {
    int undefined;
    byte2 = i & 0xff;
    byte1 = i >> 8;
    undefined = (fontinfo->min_char_or_byte2 > byte2 ||
      fontinfo->max_char_or_byte2 < byte2 ||
      fontinfo->min_byte1 > byte1 ||
      fontinfo->max_byte1 < byte1);
    if (undefined) {
      printf("0,\n");
    } else {
      if (fontinfo->per_char != NULL) {
        if (rows == 1) {
          index = i - fontinfo->min_char_or_byte2;
        } else {
          byte2 = i & 0xff;
          byte1 = i >> 8;
          index =
            (byte1 - fontinfo->min_byte1) * pages +
            (byte2 - fontinfo->min_char_or_byte2);
        }
        charinfo = &(fontinfo->per_char[index]);
        charWidth = charinfo->rbearing - charinfo->lbearing;
        charHeight = charinfo->ascent + charinfo->descent;
        if (charWidth == 0 || charHeight == 0) {
          if (charinfo->width == 0) {
            printf("0,\n");
            continue;
          }
        }
      }
      printf("&ch%d,\n", i);
    }
  }
  printf("};\n\n");
  printf("GLUTAPI const BitmapFontRec %s;\n", name);
  printf("const BitmapFontRec %s = {\n", name);
  printf("\"%s\",\n", xfont);
  printf("%d,\n", last - first + 1);
  printf("%d,\n", first);
  printf("chars\n");
  printf("};\n\n");
  XFreeFont(dpy, fontinfo);
}

int
main(int argc, char **argv)
{
  Display *dpy;
  Font font;

  if (argc != 3) {
    fprintf(stderr, "usage: capturexfont XFONT NAME\n");
    exit(1);
  }
  dpy = XOpenDisplay(NULL);
  if (dpy == NULL) {
    fprintf(stderr, "capturexfont: could not open X display\n");
    exit(1);
  }
  font = XLoadFont(dpy, argv[1]);
  if (font == None) {
    fprintf(stderr, "capturexfont: bad font\n");
    exit(1);
  }
  captureXFont(dpy, font, argv[1], argv[2]);
  XCloseDisplay(dpy);
  return 0;
}