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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
|
/*
* Copyright 2001 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 rcsid[] __attribute__ ((unused)) = "$Id: bdfcons.c,v 1.6 2001/09/19 21:00:42 mleisher Exp $";
#else
static char rcsid[] = "$Id: bdfcons.c,v 1.6 2001/09/19 21:00:42 mleisher Exp $";
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef BDF_NO_X11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#endif
#include "bdfP.h"
#undef MAX
#undef MIN
#define MAX(h,i) ((h) > (i) ? (h) : (i))
#define MIN(l,o) ((l) < (o) ? (l) : (o))
#define _BDF_PSF256 0x00
#define _BDF_PSF512 0x01
#define _BDF_PSFMAP 0x02
#define _BDF_PSFSEP 0xffff
/*
* Header for Linux PSF fonts.
*/
typedef struct {
unsigned short mag;
unsigned char mode;
unsigned char height;
} psfhdr_t;
/*
* Header for Sun VF fonts.
*/
typedef struct {
unsigned short mag;
unsigned short total_bytes;
unsigned short max_width;
unsigned short max_height;
unsigned short pad;
} vfhdr_t;
/*
* Character metrics data for Sun VF fonts.
*/
typedef struct {
unsigned short offset;
unsigned short bytes;
char ascent;
char descent;
char lbearing;
char rbearing;
unsigned short dwidth;
} vfmetrics_t;
/**************************************************************************
*
* Support functions.
*
**************************************************************************/
/*
* Routine to compare two glyphs by encoding so they can be sorted.
*/
static int
#ifdef __STDC__
by_encoding(const void *a, const void *b)
#else
by_encoding(a, b)
char *a, *b;
#endif
{
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 bdf_font_t *
#ifdef __STDC__
_bdf_load_vfont(FILE *in, vfhdr_t *hdr, bdf_callback_t callback, void *data,
int *awidth)
#else
_bdf_load_vfont(in, hdr, callback, data, awidth)
FILE *in;
vfhdr_t *hdr;
bdf_callback_t callback;
void *data;
int *awidth;
#endif
{
int first, ismono;
long i, pos;
bdf_font_t *fp;
bdf_glyph_t *gp;
bdf_callback_struct_t cb;
vfmetrics_t met, metrics[256];
/*
* Convert the header values to little endian if necessary.
*/
if (bdf_little_endian()) {
hdr->total_bytes = ((hdr->total_bytes & 0xff) << 8) |
((hdr->total_bytes >> 8) & 0xff);
hdr->max_width = ((hdr->max_width & 0xff) << 8) |
((hdr->max_width >> 8) & 0xff);
hdr->max_height = ((hdr->max_height & 0xff) << 8) |
((hdr->max_height >> 8) & 0xff);
}
/*
* The point size of the font will be the height, the resolution will
* default to 72dpi, and the spacing will default to proportional.
*/
fp = bdf_new_font(0, (long) hdr->max_height, 72, 72, BDF_PROPORTIONAL, 1);
/*
* Force the bits per pixel to 1.
*/
fp->bpp = 1;
/*
* Load the glyph metrics and set a marker to the beginning of the glyph
* bitmaps.
*/
fread((char *) metrics, sizeof(vfmetrics_t), 256, in);
pos = ftell(in);
*awidth = 0;
/*
* Count the number of glyphs that actually exist and determine the font
* bounding box in the process.
*/
(void) memset((char *) &met, 0, sizeof(vfmetrics_t));
met.lbearing = 127;
fp->glyphs_size = 0;
for (first = -1, ismono = 1, i = 0; i < 256; i++) {
if (metrics[i].bytes == 0)
continue;
if (first == -1)
first = i;
/*
* Start out by assuming the font is monowidth, but if any glyph
* encountered has metrics different than the first glyph defined,
* change that flag. If the font is still flagged as monowidth when
* this loop is done, then change the font to a monowidth font.
*/
if (i != first && ismono &&
(metrics[i].ascent != metrics[first].ascent ||
metrics[i].descent != metrics[first].descent ||
metrics[i].lbearing != metrics[first].lbearing ||
metrics[i].rbearing != metrics[first].rbearing))
ismono = 0;
/*
* If this is a little endian machine, convert the 16-bit values from
* big endian.
*/
if (bdf_little_endian()) {
metrics[i].offset = ((metrics[i].offset & 0xff) << 8) |
((metrics[i].offset >> 8) & 0xff);
metrics[i].bytes = ((metrics[i].bytes & 0xff) << 8) |
((metrics[i].bytes >> 8) & 0xff);
metrics[i].dwidth = ((metrics[i].dwidth & 0xff) << 8) |
((metrics[i].dwidth >> 8) & 0xff);
}
/*
* Update the value used for average width calculation.
*/
*awidth = *awidth + (metrics[i].rbearing - metrics[i].lbearing);
/*
* Increment the count of characters.
*/
fp->glyphs_size++;
/*
* Determine the font bounding box.
*/
met.ascent = MAX(met.ascent, metrics[i].ascent);
met.descent = MAX(met.descent, metrics[i].descent);
met.lbearing = MIN(met.lbearing, metrics[i].lbearing);
met.rbearing = MAX(met.rbearing, metrics[i].rbearing);
}
/*
* Adjust the font bounding box accordingly.
*/
fp->bbx.ascent = met.ascent;
fp->bbx.descent = met.descent;
fp->bbx.width = met.rbearing + met.lbearing;
fp->bbx.height = met.ascent + met.descent;
fp->bbx.x_offset = met.lbearing;
fp->bbx.y_offset = -met.descent;
/*
* If the font is still flagged as a monowidth font, change the font
* spacing. The actual SPACING property will be adjusted once this
* routine returns.
*/
if (ismono)
fp->spacing = BDF_MONOWIDTH;
/*
* Set up to load the glyphs.
*/
fp->glyphs = (bdf_glyph_t *) malloc(sizeof(bdf_glyph_t) * fp->glyphs_size);
(void) memset((char *) fp->glyphs, 0,
sizeof(bdf_glyph_t) * fp->glyphs_size);
/*
* Set the callback up.
*/
if (callback != 0) {
cb.reason = BDF_LOAD_START;
cb.current = 0;
cb.total = fp->glyphs_size;
(*callback)(&cb, data);
}
/*
* Get the glyphs.
*/
for (i = 0; i < 256; i++) {
if (metrics[i].bytes == 0)
continue;
/*
* Put the file pointer back at the beginning of the bitmaps.
*/
fseek(in, pos, 0L);
gp = fp->glyphs + fp->glyphs_used++;
gp->encoding = i;
gp->dwidth = metrics[i].dwidth;
gp->swidth = (unsigned short)
(((double) gp->dwidth) * 72000.0) /
((double) fp->point_size * fp->resolution_x);
gp->bbx.ascent = metrics[i].ascent;
gp->bbx.descent = metrics[i].descent;
gp->bbx.width = metrics[i].rbearing + metrics[i].lbearing;
gp->bbx.height = metrics[i].ascent + metrics[i].descent;
gp->bbx.x_offset = metrics[i].lbearing;
gp->bbx.y_offset = -metrics[i].descent;
gp->bytes = metrics[i].bytes;
gp->bitmap = (unsigned char *) malloc(gp->bytes);
fseek(in, (long) metrics[i].offset, 1L);
fread((char *) gp->bitmap, gp->bytes, 1, in);
/*
* Call the callback if indicated.
*/
if (callback != 0) {
cb.reason = BDF_LOADING;
cb.total = fp->glyphs_size;
cb.current = fp->glyphs_used;
(*callback)(&cb, data);
}
}
/*
* Add a message indicating the font was converted.
*/
_bdf_add_comment(fp, "Font converted from VF to BDF.", 30);
_bdf_add_acmsg(fp, "Font converted from VF to BDF.", 30);
/*
* Return the font.
*/
return fp;
}
/*
* Load a PSF font that may have a mapping table associated.
*/
static bdf_font_t *
#ifdef __STDC__
_bdf_load_psf(FILE *in, psfhdr_t *hdr, bdf_callback_t callback, void *data,
int *awidth)
#else
_bdf_load_psf(in, hdr, callback, data, awidth)
FILE *in;
psfhdr_t *hdr;
bdf_callback_t callback;
void *data;
int *awidth;
#endif
{
long i, cnt;
unsigned short dwidth, swidth, code, initial;
bdf_font_t *fp;
bdf_glyph_t *gp;
bdf_callback_struct_t cb;
/*
* The point size of the font will be the height, the resolution will
* default to 72dpi, and the spacing will default to character cell.
*/
fp = bdf_new_font(0, (long) hdr->height, 72, 72, BDF_CHARCELL, 1);
/*
* Force the bits per pixel to be 1.
*/
fp->bpp = 1;
/*
* Make sure the width is always set to 8 no matter what. This may
* change in the future, but probably not anytime soon.
*/
*awidth = fp->bbx.width = 8;
/*
* Adjust the ascent and descent by hand for point sizes other than 16.
*/
if (hdr->height != 16) {
fp->bbx.ascent++;
fp->bbx.descent--;
}
/*
* Default the font ascent and descent to that of the bounding box.
*/
fp->font_ascent = fp->bbx.ascent;
fp->font_descent = fp->bbx.descent;
/*
* Allocate the expected number of glyphs.
*/
fp->glyphs_size = ((hdr->mode & _BDF_PSF512) + 1) << 8;
fp->glyphs = (bdf_glyph_t *) malloc(sizeof(bdf_glyph_t) * fp->glyphs_size);
(void) memset((char *) fp->glyphs, 0,
sizeof(bdf_glyph_t) * fp->glyphs_size);
/*
* Determine the default scalable and device width for each character.
*/
dwidth = fp->bbx.width;
swidth = (unsigned short)
(((double) dwidth) * 72000.0) /
((double) fp->point_size * fp->resolution_x);
/*
* Set up to call the callback.
*/
if (callback != 0) {
cb.reason = BDF_LOAD_START;
cb.current = 0;
cb.total = fp->glyphs_size;
(*callback)(&cb, data);
}
/*
* Now load the glyphs, assigning a default encoding.
*/
for (i = 0, gp = fp->glyphs; i < fp->glyphs_size; i++, gp++) {
gp->encoding = i;
gp->dwidth = dwidth;
gp->swidth = swidth;
(void) memcpy((char *) &gp->bbx, (char *) &fp->bbx, sizeof(bdf_bbx_t));
gp->bytes = hdr->height;
gp->bitmap = (unsigned char *) malloc(hdr->height);
fread((char *) gp->bitmap, hdr->height, 1, in);
fp->glyphs_used++;
/*
* Call the callback if indicated.
*/
if (callback != 0) {
cb.reason = BDF_LOADING;
cb.total = fp->glyphs_size;
cb.current = fp->glyphs_used;
(*callback)(&cb, data);
}
}
if (hdr->mode & _BDF_PSFMAP) {
/*
* The font contains a mapping table. Preserve the original size so
* glyphs added won't affect things.
*/
for (i = 0, cnt = fp->glyphs_size; i < cnt; i++) {
initial = 1;
while (fread((char *) &code, sizeof(unsigned short), 1, in) == 1 &&
code != _BDF_PSFSEP) {
/*
* If this is a big-endian machine, make sure the code
* point is swapped from little to big endian.
*/
if (!bdf_little_endian())
code = ((code & 0xff) << 8) | ((code >> 8) & 0xff);
if (initial) {
fp->glyphs[i].encoding = (long) code;
initial = 0;
} else {
/*
* A single glyph maps to multiple locations. Duplicate
* the current glyph and assign it the new encoding.
*/
if (fp->glyphs_used == fp->glyphs_size) {
fp->glyphs = (bdf_glyph_t *)
realloc((char *) fp->glyphs,
sizeof(bdf_glyph_t) *
(fp->glyphs_size + 8));
gp = fp->glyphs + fp->glyphs_size;
(void) memset((char *) gp, 0,
sizeof(bdf_glyph_t) << 3);
fp->glyphs_size += 8;
}
/*
* Duplicate the source glyph info.
*/
gp = fp->glyphs + fp->glyphs_used++;
(void) memcpy((char *) gp, (char *) (fp->glyphs + i),
sizeof(bdf_glyph_t));
/*
* Duplicate the source glyph bitmap.
*/
gp->bitmap = (unsigned char *) malloc(gp->bytes);
(void) memcpy((char *) gp->bitmap,
(char *) fp->glyphs[i].bitmap, gp->bytes);
/*
* Assign the new encoding to it.
*/
gp->encoding = (long) code;
}
}
if (initial) {
/*
* The code was the separator, so delete the current bitmap
* and reset the current glyph.
*/
free((char *) fp->glyphs[i].bitmap);
(void) memset((char *) &fp->glyphs[i], 0, sizeof(bdf_glyph_t));
fp->glyphs_used--;
}
}
/*
* Make sure the font is sorted by encoding. This is only necessary
* for PSF fonts with their own mapping tables.
*/
qsort((char *) fp->glyphs, fp->glyphs_used, sizeof(bdf_glyph_t),
by_encoding);
}
/*
* Add a message indicating the font was converted.
*/
_bdf_add_comment(fp, "Font converted from PSF to BDF.", 31);
_bdf_add_acmsg(fp, "Font converted from PSF to BDF.", 31);
/*
* Return the new font.
*/
return fp;
}
/*
* Load a simple binary font.
*/
static bdf_font_t *
#ifdef __STDC__
_bdf_load_simple(FILE *in, int height, bdf_callback_t callback, void *data,
int type, int *awidth)
#else
_bdf_load_simple(in, height, callback, data, type, awidth)
FILE *in;
int height;
bdf_callback_t callback;
void *data;
int type, *awidth;
#endif
{
long i;
unsigned short dwidth, swidth;
bdf_font_t *fp;
bdf_glyph_t *gp;
bdf_callback_struct_t cb;
/*
* The point size of the font will be the height, the resolution will
* default to 72dpi, and the spacing will default to character cell.
*/
fp = bdf_new_font(0, (long) height, 72, 72, BDF_CHARCELL, 1);
/*
* Force the bits per pixel to be one.
*/
fp->bpp = 1;
/*
* Make sure the width is always set to 8 no matter what. This may
* change in the future, but not anytime soon.
*/
*awidth = fp->bbx.width = 8;
/*
* Adjust the ascent and descent by hand for the 14pt and 8pt fonts.
*/
if (height != 16) {
fp->bbx.ascent++;
fp->bbx.descent--;
}
/*
* Default the font ascent and descent to that of the bounding box.
*/
fp->font_ascent = fp->bbx.ascent;
fp->font_descent = fp->bbx.descent;
/*
* Simple fonts will have at most 256 glyphs.
*/
fp->glyphs_size = 256;
fp->glyphs = (bdf_glyph_t *) malloc(sizeof(bdf_glyph_t) * fp->glyphs_size);
(void) memset((char *) fp->glyphs, 0,
sizeof(bdf_glyph_t) * fp->glyphs_size);
/*
* Determine the default scalable and device width for each character.
*/
dwidth = fp->bbx.width;
swidth = (unsigned short)
(((double) dwidth) * 72000.0) /
((double) fp->point_size * fp->resolution_x);
/*
* Set up to call the callback.
*/
if (callback != 0) {
cb.reason = BDF_LOAD_START;
cb.current = 0;
cb.total = fp->glyphs_size;
(*callback)(&cb, data);
}
/*
* Now load the glyphs, assigning a default encoding.
*/
for (i = 0, gp = fp->glyphs; i < fp->glyphs_size; i++, gp++) {
gp->encoding = i;
gp->dwidth = dwidth;
gp->swidth = swidth;
(void) memcpy((char *) &gp->bbx, (char *) &fp->bbx, sizeof(bdf_bbx_t));
gp->bytes = height;
gp->bitmap = (unsigned char *) malloc(height);
fread((char *) gp->bitmap, height, 1, in);
fp->glyphs_used++;
/*
* Call the callback if indicated.
*/
if (callback != 0) {
cb.reason = BDF_LOADING;
cb.total = fp->glyphs_size;
cb.current = fp->glyphs_used;
(*callback)(&cb, data);
}
}
/*
* Add a message indicating the font was converted.
*/
if (type == 1) {
_bdf_add_comment(fp, "Font converted from VGA/EGA to BDF.", 35);
_bdf_add_acmsg(fp, "Font converted from VGA/EGA to BDF.", 35);
} else if (type == 2) {
_bdf_add_comment(fp, "Fonts converted from CP to BDF.", 31);
_bdf_add_acmsg(fp, "Fonts converted from CP to BDF.", 31);
}
/*
* Return the new font.
*/
return fp;
}
/*
* A structure to pass around in update callbacks.
*/
typedef struct {
unsigned long total;
unsigned long curr;
unsigned long lcurr;
bdf_callback_t cback;
void *data;
} _bdf_update_rec_t;
/*
* A routine to report the progress of loading a codepage font over all
* three fonts.
*/
static void
#ifdef __STDC__
_bdf_codepage_progress(bdf_callback_struct_t *cb, void *data)
#else
_bdf_codepage_progress(cb, data)
bdf_callback_struct_t *cb;
void *data;
#endif
{
_bdf_update_rec_t *up;
bdf_callback_struct_t ncb;
up = (_bdf_update_rec_t *) data;
if (up->cback == 0)
return;
if (up->curr != 0 && cb->current == 0) {
up->lcurr = 0;
return;
}
up->curr += cb->current - up->lcurr;
up->lcurr = cb->current;
ncb.reason = cb->reason;
ncb.total = up->total;
ncb.current = up->curr;
if (up->cback != 0)
(*up->cback)(&ncb, up->data);
}
/*
* Load a codepage font which actually contains three fonts. This makes
* use of the routine that loads the simple fonts.
*/
static int
#ifdef __STDC__
_bdf_load_codepage(FILE *in, bdf_callback_t callback, void *data,
bdf_font_t *fonts[3], int awidth[3])
#else
_bdf_load_codepage(in, callback, data, fonts, awidth)
FILE *in;
bdf_callback_t callback;
void *data;
bdf_font_t *fonts[3];
int awidth[3];
#endif
{
_bdf_update_rec_t up;
/*
* Initialize an override callback structure.
*/
up.cback = callback;
up.data = data;
up.total = 768;
up.curr = up.lcurr = 0;
/*
* Load the 16pt font.
*/
if (fseek(in, 40, 0L))
return BDF_NOT_CONSOLE_FONT;
fonts[0] = _bdf_load_simple(in, 16, _bdf_codepage_progress, (void *) &up,
0, &awidth[0]);
/*
* Load the 14pt font.
*/
if (fseek(in, 4142, 0L)) {
if (fonts[0] != 0)
bdf_free_font(fonts[0]);
fonts[0] = 0;
return BDF_NOT_CONSOLE_FONT;
}
fonts[1] = _bdf_load_simple(in, 14, _bdf_codepage_progress, (void *) &up,
0, &awidth[1]);
/*
* Load the 8pt font.
*/
if (fseek(in, 7732, 0L)) {
if (fonts[0] != 0)
bdf_free_font(fonts[0]);
if (fonts[1] != 0)
bdf_free_font(fonts[1]);
fonts[0] = fonts[1] = 0;
return BDF_NOT_CONSOLE_FONT;
}
fonts[2] = _bdf_load_simple(in, 8, _bdf_codepage_progress, (void *) &up,
2, &awidth[2]);
/*
* All the fonts loaded OK.
*/
return 0;
}
/**************************************************************************
*
* API.
*
**************************************************************************/
int
#ifdef __STDC__
bdf_load_console_font(FILE *in, bdf_options_t *opts, bdf_callback_t callback,
void *data, bdf_font_t *fonts[3], int *nfonts)
#else
bdf_load_console_font(in, opts, callback, data, fonts, nfonts)
FILE *in;
bdf_options_t *opts;
bdf_callback_t callback;
void *data;
bdf_font_t *fonts[3];
int *nfonts;
#endif
{
unsigned short psfmag, vfmag;
int res, awidth[3];
double dp, dr;
bdf_property_t prop;
psfhdr_t hdr;
vfhdr_t vhdr;
struct stat st;
vfmag = (!bdf_little_endian()) ? 0x011e : 0x1e01;
psfmag = (!bdf_little_endian()) ? 0x3604 : 0x0436;
(void) fstat(fileno(in), &st);
*nfonts = 1;
awidth[0] = awidth[1] = awidth[2] = 0;
(void) memset((char *) fonts, 0, sizeof(bdf_font_t *) * 3);
fread((char *) &hdr, sizeof(psfhdr_t), 1, in);
if (hdr.mag == psfmag)
/*
* Have a PSF font that may contain a mapping table.
*/
fonts[0] = _bdf_load_psf(in, &hdr, callback, data, awidth);
else {
/*
* Reset to the beginning of the file.
*/
fseek(in, 0, 0L);
if (hdr.mag == vfmag) {
/*
* Have a Sun vfont. Need to reload the header.
*/
(void) fread((char *) &vhdr, sizeof(vfhdr_t), 1, in);
fonts[0] = _bdf_load_vfont(in, &vhdr, callback, data, awidth);
} else if (st.st_size == 9780) {
/*
* Have a CP font with three sizes. Create all three fonts and
* return them.
*/
*nfonts = 3;
if ((res = _bdf_load_codepage(in, callback, data, fonts, awidth)))
return res;
} else {
/*
* Have a plain font with 256 characters. If the file size is not
* evenly divisible by 256, then the file is probably corrupt or
* is not a font.
*/
if (st.st_size & 0xff)
return BDF_NOT_CONSOLE_FONT;
fonts[0] = _bdf_load_simple(in, st.st_size >> 8, callback, data,
1, awidth);
}
}
/*
* Add all the default properties.
*/
for (res = 0; res < *nfonts; res++) {
prop.name = "POINT_SIZE";
prop.format = BDF_INTEGER;
prop.value.int32 = fonts[res]->point_size * 10;
bdf_add_font_property(fonts[res], &prop);
dr = (double) fonts[res]->resolution_y;
dp = (double) (fonts[res]->point_size * 10);
prop.name = "PIXEL_SIZE";
prop.format = BDF_INTEGER;
prop.value.int32 = (long) (((dp * dr) / 722.7) + 0.5);
bdf_add_font_property(fonts[res], &prop);
prop.name = "RESOLUTION_X";
prop.format = BDF_CARDINAL;
prop.value.card32 = (unsigned long) fonts[res]->resolution_x;
bdf_add_font_property(fonts[res], &prop);
prop.name = "RESOLUTION_Y";
prop.format = BDF_CARDINAL;
prop.value.card32 = (unsigned long) fonts[res]->resolution_y;
bdf_add_font_property(fonts[res], &prop);
prop.name = "FONT_ASCENT";
prop.format = BDF_INTEGER;
prop.value.int32 = (long) fonts[res]->bbx.ascent;
bdf_add_font_property(fonts[res], &prop);
prop.name = "FONT_DESCENT";
prop.format = BDF_INTEGER;
prop.value.int32 = (long) fonts[res]->bbx.descent;
bdf_add_font_property(fonts[res], &prop);
prop.name = "AVERAGE_WIDTH";
prop.format = BDF_INTEGER;
prop.value.int32 = (awidth[res] / fonts[res]->glyphs_used) * 10;
bdf_add_font_property(fonts[res], &prop);
prop.name = "SPACING";
prop.format = BDF_ATOM;
prop.value.atom = "P";
switch (fonts[res]->spacing) {
case BDF_PROPORTIONAL: prop.value.atom = "P"; break;
case BDF_MONOWIDTH: prop.value.atom = "M"; break;
case BDF_CHARCELL: prop.value.atom = "C"; break;
}
bdf_add_font_property(fonts[res], &prop);
}
return BDF_OK;
}
int
#ifdef __STDC__
bdf_export_psf(FILE *out, bdf_font_t *font)
#else
bdf_export_psf(out, font)
FILE *out;
bdf_font_t *font;
#endif
{
unsigned short enc[2];
char *blank;
long i, le, top;
bdf_glyph_t *gp;
psfhdr_t hdr;
if (font->glyphs_used == 0)
return BDF_EMPTY_FONT;
/*
* If the width is larger than 8 or it is not a character cell font,
* then the font cannot be saved as PSF.
*/
if (font->bbx.width > 8 || font->spacing != BDF_CHARCELL)
return BDF_NOT_PSF_FONT;
/*
* Set the little endian PSF identifier.
*/
hdr.mag = (!bdf_little_endian()) ? 0x3604 : 0x0436;
/*
* Initialize the flags and height.
*/
hdr.mode = (font->glyphs_used <= 256) ? _BDF_PSF256 : _BDF_PSF512;
hdr.height = font->bbx.height;
top = (hdr.mode & _BDF_PSF512) ? 512 : 256;
/*
* Allocate a blank bitmap for the empties.
*/
blank = (char *) malloc(hdr.height);
(void) memset(blank, 0, hdr.height);
/*
* Write the header.
*/
fwrite((char *) &hdr, sizeof(psfhdr_t), 1, out);
/*
* Generate the glyphs.
*/
for (le = _BDF_PSFSEP, i = 0, gp = font->glyphs; i < top; i++) {
if (i >= font->glyphs_used)
/*
* Write out the blank bitmap.
*/
fwrite(blank, sizeof(unsigned char), hdr.height, out);
else {
/*
* If the encoding of the current glyph is greater than the last
* glyph plus one, then a mapping table needs to be written after
* the glyphs.
*/
if (!(hdr.mode & _BDF_PSFMAP) && gp->encoding > le + 1)
hdr.mode |= _BDF_PSFMAP;
fwrite((char *) gp->bitmap, sizeof(unsigned char), hdr.height,
out);
le = gp->encoding;
gp++;
}
}
/*
* If a mapping table must be written, write it out and then rewrite the
* header to include the flag.
*/
if (hdr.mode & _BDF_PSFMAP) {
enc[1] = _BDF_PSFSEP;
for (i = 0, gp = font->glyphs; i < top; i++) {
if (i >= font->glyphs_used) {
enc[0] = _BDF_PSFSEP;
fwrite((char *) enc, sizeof(unsigned short), 1, out);
} else {
/*
* Make sure the encoding is in little endian order.
*/
enc[0] = (bdf_little_endian()) ? gp->encoding :
((gp->encoding & 0xff) << 8) |
((gp->encoding >> 8) & 0xff);
fwrite((char *) enc, sizeof(unsigned short), 2, out);
gp++;
}
}
/*
* Return to the beginning of the file and rewrite the header.
*/
fseek(out, 0, 0L);
fwrite((char *) &hdr, sizeof(psfhdr_t), 1, out);
}
return BDF_OK;
}
|