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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
* Copyright 2006 Computing Research Labs, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef lint
#ifdef __GNUC__
static char svnid[] __attribute__ ((unused)) = "$Id: bdfgrab.c 2 2006-01-08 00:57:53Z mleisher $";
#else
static char svnid[] = "$Id: bdfgrab.c 2 2006-01-08 00:57:53Z mleisher $";
#endif
#endif
/*
* This file will only be compiled if the BDF_NO_X11 macro is *not* defined.
*/
#ifndef BDF_NO_X11
/*
* Code to get BDF fonts from the X server. Reimplementation of the famous
* "getbdf" program by the equally famous der Mouse :-)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>
#include <X11/Xmu/Error.h>
#include "bdfP.h"
/*
* Routine to compare two glyphs by encoding so they can be sorted.
*/
static int
by_encoding(const void *a, const void *b)
{
bdf_glyph_t *c1, *c2;
c1 = (bdf_glyph_t *) a;
c2 = (bdf_glyph_t *) b;
if (c1->encoding < c2->encoding)
return -1;
else if (c1->encoding > c2->encoding)
return 1;
return 0;
}
static void
_bdf_get_glyphs(Display *d, XFontStruct *f, bdf_font_t *font,
bdf_callback_t callback, void *data)
{
unsigned long off, b1, b2, black, x, y, bpr;
GC cleargc, drawgc;
Pixmap canvas;
XImage *image;
XCharStruct *cp;
bdf_glyph_t *gp;
XChar2b ch;
bdf_callback_struct_t cb;
char name[16];
XGCValues gcv;
black = BlackPixel(d, DefaultScreen(d));
/*
* Create the Pixmap which will be used to draw the glyphs.
*/
canvas = XCreatePixmap(d, XRootWindow(d, DefaultScreen(d)),
font->bbx.width, font->bbx.height, 1);
/*
* Create the graphics contexts for drawing.
*/
gcv.function = GXcopy;
gcv.foreground = WhitePixel(d, DefaultScreen(d));
cleargc = XCreateGC(d, canvas, GCFunction|GCForeground, &gcv);
gcv.background = gcv.foreground;
gcv.foreground = black;
gcv.font = f->fid;
drawgc = XCreateGC(d, canvas,
GCFunction|GCForeground|GCBackground|GCFont, &gcv);
/*
* Do it.
*/
for (b1 = f->min_byte1; b1 <= f->max_byte1; b1++) {
off = (b1 - f->min_byte1) *
(f->max_char_or_byte2 + 1 - f->min_char_or_byte2);
for (b2 = f->min_char_or_byte2; b2 <= f->max_char_or_byte2;
b2++, off++) {
/*
* Point at the glyph metrics.
*/
cp = (f->per_char != 0) ? f->per_char + off : &f->min_bounds;
if (cp->lbearing || cp->rbearing || cp->width ||
cp->ascent || cp->descent) {
/*
* Make sure there is enough glyph storage to handle
* the glyphs.
*/
if (font->glyphs_used == font->glyphs_size) {
if (font->glyphs_size == 0)
font->glyphs = (bdf_glyph_t *)
malloc(sizeof(bdf_glyph_t) * 16);
else
font->glyphs = (bdf_glyph_t *)
realloc((char *) font->glyphs,
sizeof(bdf_glyph_t) *
(font->glyphs_size + 16));
font->glyphs_size += 16;
}
/*
* Point at the next glyph structure.
*/
gp = font->glyphs + font->glyphs_used++;
/*
* Determine the glyph encoding and set the metrics.
*/
gp->encoding = (b1 << 8) | b2;
gp->dwidth = cp->width;
gp->swidth = (unsigned short) (cp->width * 72000.0
/ (font->point_size * font->resolution_x));
gp->bbx.width = cp->rbearing - cp->lbearing;
gp->bbx.x_offset = cp->lbearing;
gp->bbx.ascent = cp->ascent;
gp->bbx.descent = cp->descent;
gp->bbx.y_offset = -cp->descent;
gp->bbx.height = cp->ascent + cp->descent;
/*
* Create a glyph name.
*/
sprintf(name, "char%ld", gp->encoding);
gp->name = (char *) malloc(strlen(name) + 1);
(void) strcpy(gp->name, name);
/*
* Determine the number of bytes that will be needed for this
* glyph.
*/
bpr = (gp->bbx.width + 7) >> 3;
gp->bytes = bpr * gp->bbx.height;
gp->bitmap = (unsigned char *) malloc(gp->bytes);
(void) memset((char *) gp->bitmap, 0, gp->bytes);
/*
* Clear the PSF Unicode mappings.
*/
gp->unicode.map_size = gp->unicode.map_used = 0;
/*
* Clear the canvas.
*/
XFillRectangle(d, canvas, cleargc, 0, 0,
font->bbx.width, font->bbx.height);
/*
* Render the glyph.
*/
ch.byte1 = (b1 == 0) ? (b2 >> 8) : b1;
ch.byte2 = b2;
XDrawString16(d, canvas, drawgc, -cp->lbearing, cp->ascent,
&ch, 1);
image = XGetImage(d, canvas, 0, 0,
font->bbx.width, font->bbx.height, 1L,
XYPixmap);
for (y = 0; y < gp->bbx.height; y++) {
for (x = 0; x < gp->bbx.width; x++) {
if (XGetPixel(image, x, y) == black)
gp->bitmap[(y * bpr) + (x >> 3)] |=
(0x80 >> (x & 7));
}
}
XDestroyImage(image);
/*
* Call the update callback if necessary.
*/
if (callback != 0) {
cb.reason = BDF_LOADING;
cb.total = font->glyphs_size;
cb.current = font->glyphs_used;
(*callback)(&cb, data);
}
}
}
}
/*
* Delete the Pixmap and the GCs.
*/
XFreePixmap(d, canvas);
XFreeGC(d, cleargc);
XFreeGC(d, drawgc);
/*
* Make sure the glyphs are sorted by encoding.
*/
qsort((char *) font->glyphs, font->glyphs_used, sizeof(bdf_glyph_t),
by_encoding);
}
static int
error_handler(Display *d, XErrorEvent *event)
{
if (event->request_code != X_GetAtomName)
fprintf(stderr, "X Server Error\n");
#if 0
XmuPrintDefaultErrorMessage(d, event, stderr);
#endif
return 0;
}
bdf_font_t *
bdf_load_server_font(Display *d, XFontStruct *f, char *name,
bdf_options_t *opts, bdf_callback_t callback, void *data)
{
unsigned long i, len, b1, b2;
bdf_font_t *font;
XFontProp *xfp;
XCharStruct *cp;
bdf_property_t *pp, prop;
bdf_callback_struct_t cb;
int (*old_error_handler)();
if (f == 0)
return 0;
font = (bdf_font_t *) calloc(1, sizeof(bdf_font_t));
font->bpp = 1;
/*
* Set up the font bounding box.
*/
font->bbx.width = f->max_bounds.rbearing - f->min_bounds.lbearing;
font->bbx.x_offset = f->min_bounds.lbearing;
font->bbx.height = f->max_bounds.ascent + f->max_bounds.descent;
font->bbx.ascent = f->max_bounds.ascent;
font->bbx.descent = f->max_bounds.descent;
font->bbx.y_offset = -font->bbx.descent;
/*
* If the font happens to be a character cell or monowidth, make sure that
* value is taken from the max bounds.
*/
font->monowidth = f->max_bounds.width;
font->default_glyph = (long) f->default_char;
/*
* Now load the font properties.
*/
old_error_handler = XSetErrorHandler(error_handler);
for (i = 0, xfp = f->properties; i < f->n_properties; i++, xfp++) {
if (xfp->name == XA_FONT)
/*
* Set the font name but don't add it to the list in the font.
*/
font->name = XGetAtomName(d, (Atom) xfp->card32);
else {
/*
* Add the property to the font.
*/
prop.name = XGetAtomName(d, xfp->name);
if ((pp = bdf_get_property(prop.name)) == 0) {
/*
* The property does not exist, so create it with type Atom.
*/
bdf_create_property(prop.name, BDF_ATOM);
pp = bdf_get_property(prop.name);
}
prop.format = pp->format;
switch (prop.format) {
case BDF_ATOM:
prop.value.atom = XGetAtomName(d, (Atom) xfp->card32);
break;
case BDF_CARDINAL:
prop.value.card32 = xfp->card32;
break;
case BDF_INTEGER:
prop.value.int32 = (long) xfp->card32;
break;
}
bdf_add_font_property(font, &prop);
/*
* Free up the Atom names returned by X.
*/
XFree(prop.name);
if (prop.format == BDF_ATOM)
XFree(prop.value.atom);
}
}
XSetErrorHandler(old_error_handler);
/*
* Now go through and initialize the various fields needed for the font.
*/
/*
* If the font name was not set when the properties were loaded,
* set it to the name that was passed.
*/
if (font->name == 0) {
len = (unsigned long) strlen(name);
font->name = (char *) malloc(len + 1);
(void) memcpy(font->name, name, len + 1);
}
/*
* If the font default glyph is non-zero, make sure the DEFAULT_CHAR
* property is updated appropriately. Otherwise, make sure the
* DEFAULT_CHAR property is deleted from the font.
*/
if (font->default_glyph > 0) {
prop.name = "DEFAULT_CHAR";
prop.format = BDF_INTEGER;
prop.value.int32 = font->default_glyph;
bdf_add_font_property(font, &prop);
} else if (bdf_get_font_property(font, "DEFAULT_CHAR") != 0)
bdf_delete_font_property(font, "DEFAULT_CHAR");
/*
* Check the point size.
*/
if ((pp = bdf_get_font_property(font, "POINT_SIZE")) != 0)
font->point_size = (pp->value.card32 / 10);
else
font->point_size = 12;
/*
* Check for the deprecated "RESOLUTION" property first in case it exists
* and "RESOLUTION_X" and "RESOLUTION_Y" do not.
*/
if ((pp = bdf_get_font_property(font, "RESOLUTION")) != 0)
font->resolution_x = font->resolution_y = pp->value.int32;
if ((pp = bdf_get_font_property(font, "RESOLUTION_X")) != 0)
font->resolution_x = pp->value.int32;
if ((pp = bdf_get_font_property(font, "RESOLUTION_Y")) != 0)
font->resolution_y = pp->value.int32;
/*
* If the horizontal or vertical resolutions have not been set, then
* define them to be the resolution of the display.
*/
if (font->resolution_x == 0)
font->resolution_x =
(long) (((((double) DisplayWidth(d, DefaultScreen(d))) * 25.4) /
((double) DisplayWidthMM(d, DefaultScreen(d)))) + 0.5);
if (font->resolution_y == 0)
font->resolution_y =
(long) (((((double) DisplayHeight(d, DefaultScreen(d))) * 25.4) /
((double) DisplayHeightMM(d, DefaultScreen(d)))) + 0.5);
/*
* Check the font ascent and descent.
*/
if ((pp = bdf_get_font_property(font, "FONT_ASCENT")) != 0)
font->font_ascent = pp->value.int32;
else {
/*
* Add the FONT_ASCENT property.
*/
prop.name = "FONT_ASCENT";
prop.format = BDF_INTEGER;
prop.value.int32 = font->bbx.ascent;
bdf_add_font_property(font, &prop);
font->font_ascent = font->bbx.ascent;
}
if ((pp = bdf_get_font_property(font, "FONT_DESCENT")) != 0)
font->font_descent = pp->value.int32;
else {
/*
* Add the FONT_DESCENT property.
*/
prop.name = "FONT_DESCENT";
prop.format = BDF_INTEGER;
prop.value.int32 = font->bbx.descent;
bdf_add_font_property(font, &prop);
font->font_descent = font->bbx.descent;
}
/*
* Get the font spacing.
*/
font->spacing = BDF_PROPORTIONAL;
if ((pp = bdf_get_font_property(font, "SPACING")) != 0) {
switch (pp->value.atom[0]) {
case 'P': case 'p': font->spacing = BDF_PROPORTIONAL; break;
case 'M': case 'm': font->spacing = BDF_MONOWIDTH; break;
case 'C': case 'c': font->spacing = BDF_CHARCELL; break;
}
}
/*
* Now determine the number of glyphs.
*/
if (f->per_char != 0) {
for (b1 = f->min_byte1; b1 <= f->max_byte1; b1++) {
len = (b1 - f->min_byte1) *
(f->max_char_or_byte2 + 1 - f->min_char_or_byte2);
for (b2 = f->min_char_or_byte2; b2 <= f->max_char_or_byte2;
b2++, len++) {
cp = f->per_char + len;
/*
* If any of the metrics values are non-zero, then count this
* as a glyph.
*/
if (cp->lbearing || cp->rbearing || cp->width ||
cp->ascent || cp->descent)
font->glyphs_size++;
}
}
} else
font->glyphs_size = (f->max_byte1 + 1 - f->min_byte1) *
(f->max_char_or_byte2 + 1 - f->min_char_or_byte2);
/*
* Call the callback if it was provided.
*/
if (callback != 0) {
cb.reason = BDF_LOAD_START;
cb.total = font->glyphs_size;
cb.current = 0;
(*callback)(&cb, data);
}
/*
* Allocate enough glyph storage for the specified number of glyphs.
*/
font->glyphs = (bdf_glyph_t *)
malloc(sizeof(bdf_glyph_t) * font->glyphs_size);
/*
* Actually load the glyphs.
*/
_bdf_get_glyphs(d, f, font, callback, data);
/*
* Add a message to the font to indicate it was loaded
* from the server.
*/
_bdf_add_comment(font, "Font grabbed from the X server.", 31);
_bdf_add_acmsg(font, "Font grabbed from the X server.", 31);
/*
* Mark the font as being modified.
*/
font->modified = 1;
return font;
}
#endif /* HAVE_XLIB */
|