2 * Copyright 1993,1994,1995,2005 by Ross Paterson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 * Ross Paterson <ross@soi.city.ac.uk>
33 * The following people have supplied bug fixes:
35 * Simon Chow <khsc@synoptics.com>
36 * Fung Fung Lee <lee@simd.stanford.edu>
37 * Man-Chi Pong <mcpong@cs.ust.hk>
38 * Steven Simpson <simpson@math.psu.edu>
39 * Charles Wang <charles.wang@infores.com>
40 * Werner Lemberg <wl@gnu.org>
42 * Ross no longer maintains this code. Please send bug reports to
43 * Werner Lemberg <wl@gnu.org>.
48 * Two C interfaces to HBF files.
50 * The multiple interfaces make this code rather messy; I intend
51 * to clean it up as experience is gained on what is really needed.
53 * There are also two modes of operation:
54 * - the default is to read each bitmap from its file as demanded
55 * - if IN_MEMORY is defined, the whole bitmap file is held in memory.
56 * In this case, if running under Unix, the bitmap files may be gzipped
57 * (but the filename used in the HBF file should be the name of the
58 * file before it was gzipped).
72 * if the linker complains about an unresolved identifier '_strdup',
73 * uncomment the following definition.
75 /* #define NO_STRDUP */
89 #define Bit(n) (1<<(7 - (n)))
92 * Messy file system issues
96 #define PATH_DELIMITER ':'
97 #define RelativeFileName(fn) ((fn)[0] != '/')
98 #define LocalFileName(fn) (strchr(fn, '/') == NULL)
101 #define PATH_DELIMITER ';'
102 #define HasDrive(fn) (isalpha((fn)[0]) && (fn)[1] == ':')
104 #define RelativeFileName(fn) (! HasDrive(fn) && \
105 !((fn)[0] == '\\' || (fn)[0] == '/'))
106 #define LocalFileName(fn) (! HasDrive(fn) && \
107 strchr(fn, '\\') == NULL && \
108 strchr(fn, '/') == NULL)
110 #define RelativeFileName(fn) (! HasDrive(fn) && (fn)[0] != '\\')
111 #define LocalFileName(fn) (! HasDrive(fn) && strchr(fn, '\\') == NULL)
113 #define READ_BINARY "rb"
116 #define PATH_DELIMITER ','
117 #define RelativeFileName(fn) (strchr(fn, ':') == NULL && ((fn)[0] != '[' || (fn)[1] == '.' || (fn)[1] == '-'))
118 #define LocalFileName(fn) (strchr(fn, ':') == NULL && strchr(fn, ']') == NULL)
121 #ifndef RelativeFileName
122 #define RelativeFileName(fn) FALSE
125 #ifndef LocalFileName
126 #define LocalFileName(fn) FALSE
130 #define READ_BINARY "r"
133 #define MAX_FILENAME 1024
136 * Internal structures
139 typedef unsigned char byte;
141 #define PROPERTY struct _PROPERTY
142 #define BM_FILE struct _BM_FILE
143 #define B2_RANGE struct _B2_RANGE
144 #define CODE_RANGE struct _CODE_RANGE
169 typedef unsigned short CHAR;
170 typedef unsigned int CHAR_INDEX; /* character index in file */
171 #define BAD_CHAR_INDEX 0xffff
176 BM_FILE *code_bm_file;
179 bool code_transposed;
181 CODE_RANGE *code_next;
185 * Extended internal version of HBF
189 /* fields corresponding to the definition */
191 /* plus internal stuff */
194 unsigned int b2_size; /* number of legal byte-2's */
196 B2_RANGE *byte_2_range;
197 CODE_RANGE *code_range;
201 #define FirstByte(code) ((code)>>8)
202 #define SecondByte(code) ((code)&0xff)
203 #define MakeCode(byte1,byte2) (((byte1)<<8)|(byte2))
205 /* size of a bitmap in the file (may be affected by transposition) */
206 #define FileBitmapSize(hbfFile,cp) \
207 ((cp)->code_transposed ? \
208 (hbfBitmapBBox(hbfFile)->hbf_height + 7)/8 * \
209 hbfBitmapBBox(hbfFile)->hbf_width : \
210 HBF_BitmapSize(hbfFile))
212 #define NEW(type) ((type *)malloc((unsigned)(sizeof(type))))
219 #define strdup(x) _strdup(x)
221 extern char *strdup _((const char *s));
224 static void add_b2r _((B2_RANGE **last_b2r, int start, int finish));
225 static bool add_code_range _((HBF_STRUCT *hbf, const char *line));
226 static void add_property _((HBF_STRUCT *hbf, const char *lp));
227 static CHAR_INDEX b2_pos _((HBF_STRUCT *hbf, HBF_CHAR code));
228 static int b2_size _((B2_RANGE *b2r));
229 static void clear_bbox _((HBF_BBOX *bbox));
230 static void clear_record _((HBF_STRUCT *hbf));
231 static char *concat _((const char *dir, int dirlen, const char *stem));
232 static char *expand_filename _((const char *name, const char *filename));
233 static const byte *get_bitmap
234 _((HBF_STRUCT *hbf, HBF_CHAR code, byte *buffer));
235 static byte *local_buffer _((HBF_STRUCT *hbf));
236 static void invert _((byte *buffer, unsigned length));
238 static bool read_bitmap_file _((BM_FILE *bmf, FILE *f));
239 static bool copy_transposed
240 _((HBF *hbf, byte *bitmap, const byte *source));
242 static bool get_transposed _((HBF *hbf, FILE *f, byte *bitmap));
244 static bool match _((const char *lp, const char *sp));
245 static bool parse_file _((FILE *f, HBF_STRUCT *hbf));
246 static FILE *path_open
247 _((const char *path, const char *filename, char **fullp));
248 static bool real_open _((const char *filename, HBF_STRUCT *hbf));
250 /* Error reporting */
252 int hbfDebug; /* set this for error reporting */
258 eprintf(const char *fmt, ...)
263 (void)fprintf(stderr, "HBF: ");
265 (void)vfprintf(stderr, fmt, args);
267 (void)fprintf(stderr, "\n");
270 #else /* ! __STDC__ */
271 /* poor man's variable-length argument list */
273 eprintf(fmt, x1, x2, x3, x4, x5, x6, x7, x8, x9)
275 int x1, x2, x3, x4, x5, x6, x7, x8, x9;
278 (void)fprintf(stderr, "HBF: ");
279 (void)fprintf(stderr, fmt, x1, x2, x3, x4, x5, x6, x7, x8, x9);
280 (void)fprintf(stderr, "\n");
283 #endif /* __STDC__ */
289 bbox->hbf_width = bbox->hbf_height = 0;
290 bbox->hbf_xDisplacement = bbox->hbf_yDisplacement = 0;
297 clear_bbox(&(hbf->public.hbf_bitmap_bbox));
298 clear_bbox(&(hbf->public.hbf_font_bbox));
299 hbf->property = NULL;
300 hbf->filename = NULL;
301 hbf->bitmap_buffer = NULL;
302 hbf->byte_2_range = NULL;
303 hbf->code_range = NULL;
312 add_b2r(last_b2r, start, finish)
313 reg B2_RANGE **last_b2r;
320 while (*last_b2r != NULL && (*last_b2r)->b2r_start < start)
321 last_b2r = &((*last_b2r)->b2r_next);
322 b2r->b2r_next = *last_b2r;
323 b2r->b2r_start = start;
324 b2r->b2r_finish = finish;
337 c = SecondByte(code);
339 for (b2r = hbf->byte_2_range; b2r != NULL; b2r = b2r->b2r_next)
340 if (b2r->b2r_start <= c && c <= b2r->b2r_finish)
341 return pos + c - b2r->b2r_start;
343 pos += b2r->b2r_finish - b2r->b2r_start + 1;
344 return BAD_CHAR_INDEX;
354 for ( ; b2r != NULL; b2r = b2r->b2r_next)
355 size += b2r->b2r_finish - b2r->b2r_start + 1;
359 /* map a position to a character code */
369 code = pos / hbf->b2_size * 256;
370 residue = pos % hbf->b2_size;
371 for (b2r = hbf->byte_2_range; b2r != NULL; b2r = b2r->b2r_next)
372 if (b2r->b2r_start + residue <= b2r->b2r_finish)
373 return code + b2r->b2r_start + residue;
375 residue -= b2r->b2r_finish - b2r->b2r_start + 1;
376 /* should never get here */
389 while (*lp == *sp && *sp != '\0') {
393 return (*lp == '\0' || isspace(*lp)) && *sp == '\0';
403 new_s = malloc((unsigned)strlen(s) + 1);
414 add_property(hbf, lp)
422 prop = NEW(PROPERTY);
425 while (*lp != '\0' && ! isspace(*lp))
428 prop->prop_name = strdup(tmp);
430 while (*lp != '\0' && isspace(*lp))
436 while (*lp != '\0' && ! (*lp == QUOTE && *++lp != QUOTE))
441 while (*lp != '\0' && ! isspace(*lp))
443 while (*lp != '\0' && isspace(*lp))
450 prop->prop_value = strdup(tmp);
452 prop->prop_next = hbf->property;
453 hbf->property = prop;
457 hbfProperty(hbfFile, propName)
459 const char *propName;
464 hbf = (HBF_STRUCT *)hbfFile;
465 for (prop = hbf->property; prop != NULL; prop = prop->prop_next)
466 if (strcmp(prop->prop_name, propName) == 0)
467 return prop->prop_value;
472 * Compatability routines
476 HBF_GetProperty(handle, propertyName)
478 const char *propertyName;
480 return hbfProperty(handle, propertyName);
484 HBF_GetFontBoundingBox(handle, width, height, xDisplacement, yDisplacement)
487 unsigned int *height;
492 *width = hbfFontBBox(handle)->hbf_width;
494 *height = hbfFontBBox(handle)->hbf_height;
495 if (xDisplacement != NULL)
496 *xDisplacement = hbfFontBBox(handle)->hbf_xDisplacement;
497 if (yDisplacement != NULL)
498 *yDisplacement = hbfFontBBox(handle)->hbf_yDisplacement;
503 HBF_GetBitmapBoundingBox(handle, width, height, xDisplacement, yDisplacement)
506 unsigned int *height;
511 *width = hbfBitmapBBox(handle)->hbf_width;
513 *height = hbfBitmapBBox(handle)->hbf_height;
514 if (xDisplacement != NULL)
515 *xDisplacement = hbfBitmapBBox(handle)->hbf_xDisplacement;
516 if (yDisplacement != NULL)
517 *yDisplacement = hbfBitmapBBox(handle)->hbf_yDisplacement;
522 * Prepend a directory to a relative filename.
525 concat(dir, dirlen, stem)
526 const char *dir; /* not necessarily null-terminated */
527 int dirlen; /* number of significant chars in dir */
528 const char *stem; /* relative filename */
532 if (dirlen == 0) /* null: current directory */
535 fullname = malloc(dirlen + strlen(stem) + 2);
536 (void)sprintf(fullname, "%.*s/%s", dirlen, dir, stem);
539 fullname = malloc(dirlen + strlen(stem) + 2);
540 (void)sprintf(fullname, "%.*s\\%s", dirlen, dir, stem);
543 if (dir[dirlen-1] == ']' && stem[0] == '[' && stem[1] == '-') {
546 fullname = malloc(dirlen + strlen(stem) + 2);
547 (void)sprintf(fullname, "%.*s.%s", dirlen, dir, stem);
550 if (dir[dirlen-1] == ']' && stem[0] == '[' && stem[1] == '.') {
554 fullname = malloc(dirlen + strlen(stem) + 1);
555 (void)sprintf(fullname, "%.*s%s", dirlen, dir, stem);
558 fullname = strdup(stem);
568 * If the host operating system has a heirarchical file system and
569 * the bitmap file name is relative, it is relative to the directory
570 * containing the HBF file.
573 expand_filename(name, hbf_name)
575 const char *hbf_name;
581 size = name[0] != '/' && (s = strrchr(hbf_name, '/')) != NULL ?
582 s - hbf_name + 1 : 0;
583 s = malloc((unsigned)size + strlen(name) + 1);
584 (void)sprintf(s, "%.*s%s", size, hbf_name, name);
592 s = (unsigned char *)hbf_name + strlen((unsigned char *)hbf_name) - 1;
594 if (*s == '\\' || *s == '/')
603 size = HasDrive(name) ? 0 :
604 (name[0] == '\\' || name[0] == '/') ?
605 (HasDrive(hbf_name) ? 2 : 0) :
606 s != NULL ? s - hbf_name + 1 : 0;
608 size = HasDrive(name) ? 0 :
609 name[0] == '\\' ? (HasDrive(hbf_name) ? 2 : 0) :
610 (s = strrchr(hbf_name, '\\')) != NULL ?
611 s - hbf_name + 1 : 0;
613 s = malloc((unsigned)size + strlen(name) + 1);
614 (void)sprintf(s, "%.*s%s", size, hbf_name, name);
619 reg const char *copyto;
622 if ((s = strchr(hbf_name, ']')) != NULL && RelativeFileName(name))
623 return concat(hbf_name, (s - hbf_name) + 1, name);
626 if ((s = strstr(copyto, "::")) != NULL && strstr(name, "::") == NULL)
628 if ((s = strchr(copyto, ':')) != NULL && strchr(name, ':') == NULL)
630 size = copyto - hbf_name;
631 s = malloc((unsigned)size + strlen(name) + 1);
632 (void)sprintf(s, "%.*s%s", size, hbf_name, name);
642 find_file(hbf, filename)
644 const char *filename;
656 for (fp = &(hbf->bm_file); *fp != NULL; fp = &((*fp)->bmf_next)) {
657 bmfname = strrchr((*fp)->bmf_name, '/');
658 bmfname = (bmfname) ? bmfname + 1 : (*fp)->bmf_name;
659 if (strcmp(bmfname, filename) == 0)
665 eprintf("out of memory");
668 file->bmf_name = expand_filename(filename, hbf->filename);
669 if (file->bmf_name == NULL) {
673 f = fopen(file->bmf_name, READ_BINARY);
680 sprintf(tmp, "%s.gz", file->bmf_name);
681 if ((f = fopen(tmp, "r")) != NULL) {
683 sprintf(tmp, "gzcat %s.gz", file->bmf_name);
684 if ((f = popen(tmp, "r")) != NULL)
689 #endif /* IN_MEMORY */
691 eprintf("can't open bitmap file '%s'", file->bmf_name);
692 free(file->bmf_name);
697 if (! read_bitmap_file(file, f)) {
698 free(file->bmf_name);
710 #else /* ! IN_MEMORY */
713 file->bmf_size = ftell(f);
714 #endif /* ! IN_MEMORY */
715 file->bmf_next = NULL;
721 #define GRAIN_SIZE 512
724 read_bitmap_file(bmf, f)
733 cp = contents = (byte *)malloc((unsigned)GRAIN_SIZE);
734 if (contents == NULL) {
735 eprintf("not enough space for bitmap file");
738 while ((c = getc(f)) != EOF) {
739 if (size%GRAIN_SIZE == 0) {
740 contents = (byte *)realloc((char *)contents,
741 (unsigned)(size + GRAIN_SIZE));
742 if (contents == NULL) {
743 eprintf("not enough space for bitmap file");
746 cp = contents + size;
751 bmf->bmf_size = size;
752 bmf->bmf_contents = (byte *)realloc((char *)contents, (unsigned)size);
755 #endif /* IN_MEMORY */
761 /* check that a code range fits within its bitmap file */
768 long offset, end_offset;
772 bm_size = FileBitmapSize(&(hbf->public), cp);
773 offset = cp->code_offset;
774 start = cp->code_start;
775 finish = cp->code_finish;
776 end_offset = offset + bm_size *
777 (hbf->b2_size*(long)FirstByte(finish) +
778 b2_pos(hbf, finish) - cp->code_pos + 1);
779 bmf = cp->code_bm_file;
780 if (end_offset <= bmf->bmf_size)
782 /* bitmap file is too short: produce a specific error message */
783 if (offset > bmf->bmf_size)
784 eprintf("bitmap file '%s' is shorter than offset 0x%04lx",
785 bmf->bmf_name, offset);
786 else if (offset + bm_size > bmf->bmf_size)
787 eprintf("bitmap file '%s' too short: no room for any bitmaps at offset 0x%04lx",
788 bmf->bmf_name, offset);
790 eprintf("bitmap file '%s' is too short - code range appears to be 0x%04lx-0x%04lx",
793 code_of(hbf, cp->code_pos +
794 (bmf->bmf_size - offset)/bm_size) - 1);
803 for ( ; n > 0; n--) {
804 while (*s != '\0' && ! isspace(*s))
806 while (*s != '\0' && isspace(*s))
812 /* optional keywords at the end of a CODE_RANGE line */
814 parse_keywords(cp, s)
818 for (s = skip_word(4, s) ; *s != '\0'; s = skip_word(1, s)) {
820 case 's': case 'S': case 't': case 'T':
821 /* keyword "sideways" or "transposed" */
822 cp->code_transposed = TRUE;
825 /* keyword "inverted" */
826 cp->code_inverted = TRUE;
832 add_code_range(hbf, line)
840 char filename[MAXLINE];
844 if (sscanf(line, "HBF_CODE_RANGE %li-%li %s %li",
845 &start, &finish, filename, &offset) != 4) {
846 eprintf("syntax error in HBF_CODE_RANGE");
849 /* code ranges are checked in real_open() */
850 if ((bmf = find_file(hbf, filename)) == NULL)
852 if ((cp = NEW(CODE_RANGE)) == NULL) {
853 eprintf("out of memory");
857 cp->code_start = (CHAR)start;
858 cp->code_finish = (CHAR)finish;
859 cp->code_bm_file = bmf;
860 cp->code_offset = offset;
861 cp->code_transposed = cp->code_inverted = FALSE;
862 parse_keywords(cp, line);
863 /* insert it in order */
864 for (cpp = &hbf->code_range;
865 *cpp != NULL && (*cpp)->code_finish < start;
866 cpp = &((*cpp)->code_next))
868 if (*cpp != NULL && (*cpp)->code_start <= finish) {
869 eprintf("code ranges overlap");
872 cp->code_next = *cpp;
875 /* set code_pos, and check range */
876 if (start > finish) {
877 eprintf("illegal code range 0x%04lx-0x%04lx", start, finish);
880 if ((b2pos = b2_pos(hbf, start)) == BAD_CHAR_INDEX) {
881 eprintf("illegal start code 0x%04lx", start);
884 cp->code_pos = hbf->b2_size*(long)FirstByte(start) + b2pos;
885 if ((b2pos = b2_pos(hbf, finish)) == BAD_CHAR_INDEX) {
886 eprintf("illegal finish code 0x%04lx", finish);
889 /* check that the bitmap file has enough bitmaps */
890 return ! too_short(hbf, cp);
894 * Reading and parsing of an HBF file
897 /* get line, truncating to len, and trimming trailing spaces */
899 get_line(buf, len, f)
909 if ((c = getc(f)) == EOF) {
910 eprintf("unexpected end of file");
913 if (c == '\n' || c == '\r') {
914 /* trim trailing space */
915 while (bp > buf && isspace(*(bp-1)))
927 /* get next non-COMMENT line */
929 get_text_line(buf, len, f)
934 while (get_line(buf, len, f))
935 if (*buf != '\0' && ! match(buf, "COMMENT"))
941 get_property(line, keyword, hbf)
946 if (! match(line, keyword)) {
947 eprintf("%s expected", keyword);
950 add_property(hbf, line);
955 get_bbox(line, keyword, bbox)
962 if (! match(line, keyword) ||
963 sscanf(line + strlen(keyword), "%i %i %i %i",
964 &w, &h, &xd, &yd) != 4) {
965 eprintf("%s expected", keyword);
968 if (w <= 0 || h <= 0) {
969 eprintf("illegal %s dimensions %dx%d", keyword, w, h);
973 bbox->hbf_height = h;
974 bbox->hbf_xDisplacement = xd;
975 bbox->hbf_yDisplacement = yd;
981 * 'HBF_START_FONT' version EOLN
982 * 'HBF_CODE_SCHEME' word ... EOLN
983 * 'FONT' fontName EOLN
984 * 'SIZE' ptsize xres yres EOLN
985 * 'HBF_BITMAP_BOUNDING_BOX' w h xd yd EOLN
986 * 'FONTBOUNDINGBOX' w h xd yd EOLN
987 * X11R5FontPropertySection
989 * HBFByte2RangeSection
990 * HBFCodeRangeSection
991 * 'HBF_END_FONT' EOLN .
993 * This implementation allows extra lines before HBF_END_FONT.
994 * Anything after HBF_END_FONT is ignored.
1000 reg HBF_STRUCT *hbf;
1005 if (! get_text_line(line, MAXLINE, f) ||
1006 ! get_property(line, "HBF_START_FONT", hbf))
1009 if (! get_text_line(line, MAXLINE, f) ||
1010 ! get_property(line, "HBF_CODE_SCHEME", hbf))
1013 if (! get_text_line(line, MAXLINE, f) ||
1014 ! get_property(line, "FONT", hbf))
1017 if (! get_text_line(line, MAXLINE, f) ||
1018 ! get_property(line, "SIZE", hbf))
1021 if (! get_text_line(line, MAXLINE, f) ||
1022 ! get_bbox(line, "HBF_BITMAP_BOUNDING_BOX",
1023 &(hbf->public.hbf_bitmap_bbox)))
1026 if (! get_text_line(line, MAXLINE, f) ||
1027 ! get_bbox(line, "FONTBOUNDINGBOX", &(hbf->public.hbf_font_bbox)))
1030 if (! get_text_line(line, MAXLINE, f))
1032 if (match(line, "STARTPROPERTIES")) {
1034 if (! get_text_line(line, MAXLINE, f))
1036 if (match(line, "ENDPROPERTIES"))
1038 add_property(hbf, line);
1040 if (! get_text_line(line, MAXLINE, f))
1044 if (match(line, "CHARS"))
1045 if (! get_text_line(line, MAXLINE, f))
1048 if (match(line, "HBF_START_BYTE_2_RANGES")) {
1050 if (! get_text_line(line, MAXLINE, f))
1052 if (match(line, "HBF_END_BYTE_2_RANGES"))
1054 if (sscanf(line, "HBF_BYTE_2_RANGE %i-%i",
1055 &start, &finish) != 2) {
1056 eprintf("HBF_BYTE_2_RANGE expected");
1059 add_b2r(&(hbf->byte_2_range), start, finish);
1061 if (! get_text_line(line, MAXLINE, f))
1065 add_b2r(&(hbf->byte_2_range), 0, 0xff);
1066 hbf->b2_size = b2_size(hbf->byte_2_range);
1068 if (! match(line, "HBF_START_CODE_RANGES")) {
1069 eprintf("HBF_START_CODE_RANGES expected");
1073 if (! get_text_line(line, MAXLINE, f))
1075 if (match(line, "HBF_END_CODE_RANGES"))
1077 if (! add_code_range(hbf, line))
1082 if (! get_text_line(line, MAXLINE, f))
1084 if (match(line, "HBF_END_FONT"))
1086 /* treat extra lines as properties (for private extensions) */
1087 add_property(hbf, line);
1094 path_open(path, filename, fullp)
1096 const char *filename;
1099 if (LocalFileName(filename) && path != NULL) {
1100 #ifdef PATH_DELIMITER
1106 len = strlen(filename);
1108 p_next = strchr(path, PATH_DELIMITER);
1110 p_next = path + strlen(path);
1111 fullname = concat(path, p_next - path, filename);
1112 if ((f = fopen(fullname, "r")) != NULL) {
1117 if (*p_next == '\0')
1125 *fullp = strdup(filename);
1126 return fopen(*fullp, "r");
1131 real_open(filename, hbf)
1132 const char *filename;
1133 reg HBF_STRUCT *hbf;
1137 f = path_open(getenv("HBFPATH"), filename, &(hbf->filename));
1139 eprintf("can't read file '%s'", filename);
1142 if (! parse_file(f, hbf)) {
1152 const char *filename;
1154 reg HBF_STRUCT *hbf;
1156 if ((hbf = NEW(HBF_STRUCT)) == NULL) {
1157 eprintf("can't allocate HBF structure");
1161 if (real_open(filename, hbf))
1162 return &(hbf->public);
1163 hbfClose(&(hbf->public));
1168 HBF_OpenFont(filename, ptrHandleStorage)
1169 const char *filename;
1170 HBF **ptrHandleStorage;
1172 return (*ptrHandleStorage = hbfOpen(filename)) == NULL ? -1 : 0;
1176 * Close files, free everything associated with the HBF.
1180 HBF_CloseFont(hbfFile)
1183 reg HBF_STRUCT *hbf;
1184 PROPERTY *prop_ptr, *prop_next;
1185 B2_RANGE *b2r_ptr, *b2r_next;
1186 CODE_RANGE *code_ptr, *code_next;
1187 BM_FILE *bmf_ptr, *bmf_next;
1191 hbf = (HBF_STRUCT *)hbfFile;
1193 if (hbf->filename != NULL)
1194 free(hbf->filename);
1195 if (hbf->bitmap_buffer != NULL)
1196 free(hbf->bitmap_buffer);
1198 for (prop_ptr = hbf->property;
1200 prop_ptr = prop_next) {
1201 prop_next = prop_ptr->prop_next;
1202 free(prop_ptr->prop_name);
1203 free(prop_ptr->prop_value);
1204 free((char *)prop_ptr);
1207 for (b2r_ptr = hbf->byte_2_range;
1209 b2r_ptr = b2r_next) {
1210 b2r_next = b2r_ptr->b2r_next;
1211 free((char *)b2r_ptr);
1214 for (code_ptr = hbf->code_range;
1216 code_ptr = code_next) {
1217 code_next = code_ptr->code_next;
1218 free((char *)code_ptr);
1221 for (bmf_ptr = hbf->bm_file;
1223 bmf_ptr = bmf_next) {
1224 bmf_next = bmf_ptr->bmf_next;
1226 free((char *)(bmf_ptr->bmf_contents));
1228 if (bmf_ptr->bmf_file != NULL &&
1229 fclose(bmf_ptr->bmf_file) < 0)
1232 free(bmf_ptr->bmf_name);
1233 free((char *)bmf_ptr);
1245 (void)HBF_CloseFont(hbfFile);
1253 hbfGetBitmap(hbf, code)
1257 return get_bitmap((HBF_STRUCT *)hbf, code, (byte *)NULL);
1261 HBF_GetBitmap(hbf, code, buffer)
1266 return get_bitmap((HBF_STRUCT *)hbf, code, buffer) == NULL ? -1 : 0;
1270 * Internal function to fetch a bitmap.
1271 * If buffer is non-null, it must be used.
1274 get_bitmap(hbf, code, buffer)
1275 reg HBF_STRUCT *hbf;
1279 CHAR_INDEX pos, b2pos;
1285 if ((b2pos = b2_pos(hbf, code)) == BAD_CHAR_INDEX)
1287 pos = hbf->b2_size*FirstByte(code) + b2pos;
1288 for (cp = hbf->code_range; cp != NULL; cp = cp->code_next)
1289 if (cp->code_start <= code && code <= cp->code_finish) {
1290 bmf = cp->code_bm_file;
1291 bm_size = FileBitmapSize(&(hbf->public), cp);
1292 offset = cp->code_offset +
1293 (long)(pos - cp->code_pos) * bm_size;
1295 if (buffer == NULL &&
1296 ! cp->code_transposed && ! cp->code_inverted)
1297 return bmf->bmf_contents + offset;
1298 #endif /* IN_MEMORY */
1299 if (buffer == NULL &&
1300 ((buffer = local_buffer(hbf)) == NULL))
1303 if (cp->code_transposed)
1304 copy_transposed(&(hbf->public),
1306 bmf->bmf_contents + offset);
1308 memcpy((char *)buffer,
1309 (char *)(bmf->bmf_contents + offset),
1311 #else /* ! IN_MEMORY */
1312 if (fseek(bmf->bmf_file, offset, 0) != 0) {
1313 eprintf("seek error on code 0x%04x", code);
1316 if (cp->code_transposed ?
1317 ! get_transposed(&(hbf->public), bmf->bmf_file,
1319 fread((char *)buffer,
1320 bm_size, 1, bmf->bmf_file) != 1) {
1321 eprintf("read error on code 0x%04x", code);
1324 #endif /* IN_MEMORY */
1325 if (cp->code_inverted)
1326 invert(buffer, HBF_BitmapSize(&(hbf->public)));
1329 eprintf("code 0x%04x out of range", code);
1337 if (hbf->bitmap_buffer == NULL &&
1338 (hbf->bitmap_buffer = (byte *)malloc(HBF_BitmapSize(&(hbf->public)))) == NULL) {
1339 eprintf("out of memory");
1342 return hbf->bitmap_buffer;
1346 invert(buffer, length)
1348 unsigned int length;
1350 for ( ; length > 0; length--)
1356 copy_transposed(hbf, bitmap, source)
1359 reg const byte *source;
1367 reg int imask, omask;
1369 width = hbfBitmapBBox(hbf)->hbf_width;
1370 row_size = HBF_RowSize(hbf);
1371 bm_end = bitmap + HBF_BitmapSize(hbf);
1372 (void)memset((char *)bitmap, '\0', HBF_BitmapSize(hbf));
1373 for (x = 0; x < width; x++) {
1379 for (imask = Bit(0); imask != 0; imask >>= 1) {
1384 * pos == bitmap + y*row_size + x/8
1386 * We examine bit y of row x of the input,
1387 * setting bit x of row y of the output if
1388 * required, by applying omask to *pos.
1390 if ((c & imask) != 0)
1392 /* if (++y > height) goto end_column */
1403 #else /* ! IN_MEMORY */
1405 get_transposed(hbf, f, bitmap)
1416 reg int imask, omask;
1418 width = hbfBitmapBBox(hbf)->hbf_width;
1419 row_size = HBF_RowSize(hbf);
1420 bm_end = bitmap + HBF_BitmapSize(hbf);
1421 (void)memset((char *)bitmap, '\0', HBF_BitmapSize(hbf));
1422 for (x = 0; x < width; x++) {
1427 if ((c = getc(f)) == EOF)
1429 for (imask = Bit(0); imask != 0; imask >>= 1) {
1434 * pos == bitmap + y*row_size + x/8
1436 * We examine bit y of row x of the input,
1437 * setting bit x of row y of the output if
1438 * required, by applying omask to *pos.
1440 if ((c & imask) != 0)
1442 /* if (++y > height) goto end_column */
1453 #endif /* ! IN_MEMORY */
1456 * Call function on each valid code in ascending order.
1459 hbfForEach(hbfFile, func, data)
1461 reg void (*func)_((HBF *sameHbfFile, HBF_CHAR code, void *data));
1467 reg unsigned byte1, byte2;
1468 reg unsigned finish;
1470 hbf = (HBF_STRUCT *)hbfFile;
1471 for (cp = hbf->code_range; cp != NULL; cp = cp->code_next) {
1472 byte1 = FirstByte(cp->code_start);
1473 byte2 = SecondByte(cp->code_start);
1474 while (MakeCode(byte1, byte2) <= cp->code_finish) {
1475 for (b2r = hbf->byte_2_range;
1477 b2r = b2r->b2r_next) {
1478 if (byte2 < b2r->b2r_start)
1479 byte2 = b2r->b2r_start;
1480 finish = b2r->b2r_finish;
1481 if (byte1 == FirstByte(cp->code_finish) &&
1482 finish > SecondByte(cp->code_finish))
1483 finish = SecondByte(cp->code_finish);
1484 while (byte2 <= finish) {
1486 MakeCode(byte1, byte2), data);
1500 return ((HBF_STRUCT *)hbf)->filename;
1511 hbf = (HBF_STRUCT *)hbfFile;
1513 for (cp = hbf->code_range; cp != NULL; cp = cp->code_next)
1515 hbf->b2_size*FirstByte(cp->code_finish) +
1516 b2_pos(hbf, cp->code_finish) -
1517 (hbf->b2_size*FirstByte(cp->code_start) +
1518 b2_pos(hbf, cp->code_start)) + 1;
1523 * Functions also implemented as macros
1526 #ifdef hbfBitmapBBox
1527 #undef hbfBitmapBBox
1534 return &(hbf->hbf_bitmap_bbox);
1545 return &(hbf->hbf_font_bbox);
1549 hbfGetByte2Range(hbfFile, b2r_pointer, startp, finishp)
1551 const void *b2r_pointer;
1558 hbf = (HBF_STRUCT *)hbfFile;
1559 if (b2r_pointer == NULL)
1560 b2r = hbf->byte_2_range;
1562 b2r = ((B2_RANGE *)b2r_pointer)->b2r_next;
1565 *startp = b2r->b2r_start;
1566 *finishp = b2r->b2r_finish;
1571 hbfGetCodeRange(hbfFile, code_pointer, startp, finishp)
1573 const void *code_pointer;
1580 hbf = (HBF_STRUCT *)hbfFile;
1581 if (code_pointer == NULL)
1582 cp = hbf->code_range;
1584 cp = ((CODE_RANGE *)code_pointer)->code_next;
1587 *startp = cp->code_start;
1588 *finishp = cp->code_finish;