-
Notifications
You must be signed in to change notification settings - Fork 3
/
hbf.c
1590 lines (1440 loc) · 34.1 KB
/
hbf.c
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 1993,1994,1995,2005 by Ross Paterson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* Ross Paterson <[email protected]>
* 17 October 1995
*
* The following people have supplied bug fixes:
*
* Simon Chow <[email protected]>
* Fung Fung Lee <[email protected]>
* Man-Chi Pong <[email protected]>
* Steven Simpson <[email protected]>
* Charles Wang <[email protected]>
* Werner Lemberg <[email protected]>
*
* Ross no longer maintains this code. Please send bug reports to
* Werner Lemberg <[email protected]>.
*
*/
/*
* Two C interfaces to HBF files.
*
* The multiple interfaces make this code rather messy; I intend
* to clean it up as experience is gained on what is really needed.
*
* There are also two modes of operation:
* - the default is to read each bitmap from its file as demanded
* - if IN_MEMORY is defined, the whole bitmap file is held in memory.
* In this case, if running under Unix, the bitmap files may be gzipped
* (but the filename used in the HBF file should be the name of the
* file before it was gzipped).
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "hbf.h"
#ifdef __MSDOS__
#define msdos
#endif
/*
* if the linker complains about an unresolved identifier '_strdup',
* uncomment the following definition.
*/
/* #define NO_STRDUP */
#ifdef __STDC__
# define _(x) x
#else
# define _(x) ()
#endif
#define reg register
typedef int bool;
#define TRUE 1
#define FALSE 0
#define Bit(n) (1<<(7 - (n)))
/*
* Messy file system issues
*/
#ifdef unix
#define PATH_DELIMITER ':'
#define RelativeFileName(fn) ((fn)[0] != '/')
#define LocalFileName(fn) (strchr(fn, '/') == NULL)
#endif /* unix */
#ifdef msdos
#define PATH_DELIMITER ';'
#define HasDrive(fn) (isalpha((fn)[0]) && (fn)[1] == ':')
#ifdef __EMX__
#define RelativeFileName(fn) (! HasDrive(fn) && \
!((fn)[0] == '\\' || (fn)[0] == '/'))
#define LocalFileName(fn) (! HasDrive(fn) && \
strchr(fn, '\\') == NULL && \
strchr(fn, '/') == NULL)
#else
#define RelativeFileName(fn) (! HasDrive(fn) && (fn)[0] != '\\')
#define LocalFileName(fn) (! HasDrive(fn) && strchr(fn, '\\') == NULL)
#endif /* __EMX__ */
#define READ_BINARY "rb"
#endif /* msdos */
#ifdef vms
#define PATH_DELIMITER ','
#define RelativeFileName(fn) (strchr(fn, ':') == NULL && ((fn)[0] != '[' || (fn)[1] == '.' || (fn)[1] == '-'))
#define LocalFileName(fn) (strchr(fn, ':') == NULL && strchr(fn, ']') == NULL)
#endif
#ifndef RelativeFileName
#define RelativeFileName(fn) FALSE
#endif
#ifndef LocalFileName
#define LocalFileName(fn) FALSE
#endif
#ifndef READ_BINARY
#define READ_BINARY "r"
#endif
#define MAX_FILENAME 1024
/*
* Internal structures
*/
typedef unsigned char byte;
#define PROPERTY struct _PROPERTY
#define BM_FILE struct _BM_FILE
#define B2_RANGE struct _B2_RANGE
#define CODE_RANGE struct _CODE_RANGE
PROPERTY {
char *prop_name;
char *prop_value;
PROPERTY *prop_next;
};
BM_FILE {
char *bmf_name;
#ifdef IN_MEMORY
byte *bmf_contents;
#else
FILE *bmf_file;
#endif
long bmf_size;
BM_FILE *bmf_next;
};
B2_RANGE {
byte b2r_start;
byte b2r_finish;
B2_RANGE *b2r_next;
};
typedef unsigned short CHAR;
typedef unsigned int CHAR_INDEX; /* character index in file */
#define BAD_CHAR_INDEX 0xffff
CODE_RANGE {
CHAR code_start;
CHAR code_finish;
BM_FILE *code_bm_file;
long code_offset;
CHAR_INDEX code_pos;
bool code_transposed;
bool code_inverted;
CODE_RANGE *code_next;
};
/*
* Extended internal version of HBF
*/
typedef struct {
/* fields corresponding to the definition */
HBF public;
/* plus internal stuff */
char *filename;
byte *bitmap_buffer;
unsigned int b2_size; /* number of legal byte-2's */
PROPERTY *property;
B2_RANGE *byte_2_range;
CODE_RANGE *code_range;
BM_FILE *bm_file;
} HBF_STRUCT;
#define FirstByte(code) ((code)>>8)
#define SecondByte(code) ((code)&0xff)
#define MakeCode(byte1,byte2) (((byte1)<<8)|(byte2))
/* size of a bitmap in the file (may be affected by transposition) */
#define FileBitmapSize(hbfFile,cp) \
((cp)->code_transposed ? \
(hbfBitmapBBox(hbfFile)->hbf_height + 7)/8 * \
hbfBitmapBBox(hbfFile)->hbf_width : \
HBF_BitmapSize(hbfFile))
#define NEW(type) ((type *)malloc((unsigned)(sizeof(type))))
#define QUOTE '"'
#define MAXLINE 1024
#ifdef WIN32
#define strdup(x) _strdup(x)
#else
extern char *strdup _((const char *s));
#endif
static void add_b2r _((B2_RANGE **last_b2r, int start, int finish));
static bool add_code_range _((HBF_STRUCT *hbf, const char *line));
static void add_property _((HBF_STRUCT *hbf, const char *lp));
static CHAR_INDEX b2_pos _((HBF_STRUCT *hbf, HBF_CHAR code));
static int b2_size _((B2_RANGE *b2r));
static void clear_bbox _((HBF_BBOX *bbox));
static void clear_record _((HBF_STRUCT *hbf));
static char *concat _((const char *dir, int dirlen, const char *stem));
static char *expand_filename _((const char *name, const char *filename));
static const byte *get_bitmap
_((HBF_STRUCT *hbf, HBF_CHAR code, byte *buffer));
static byte *local_buffer _((HBF_STRUCT *hbf));
static void invert _((byte *buffer, unsigned length));
#ifdef IN_MEMORY
static bool read_bitmap_file _((BM_FILE *bmf, FILE *f));
static bool copy_transposed
_((HBF *hbf, byte *bitmap, const byte *source));
#else
static bool get_transposed _((HBF *hbf, FILE *f, byte *bitmap));
#endif
static bool match _((const char *lp, const char *sp));
static bool parse_file _((FILE *f, HBF_STRUCT *hbf));
static FILE *path_open
_((const char *path, const char *filename, char **fullp));
static bool real_open _((const char *filename, HBF_STRUCT *hbf));
/* Error reporting */
int hbfDebug; /* set this for error reporting */
#ifdef __STDC__
#include <stdarg.h>
static void
eprintf(const char *fmt, ...)
{
if (hbfDebug) {
va_list args;
(void)fprintf(stderr, "HBF: ");
va_start(args, fmt);
(void)vfprintf(stderr, fmt, args);
va_end(args);
(void)fprintf(stderr, "\n");
}
}
#else /* ! __STDC__ */
/* poor man's variable-length argument list */
static void
eprintf(fmt, x1, x2, x3, x4, x5, x6, x7, x8, x9)
const char *fmt;
int x1, x2, x3, x4, x5, x6, x7, x8, x9;
{
if (hbfDebug) {
(void)fprintf(stderr, "HBF: ");
(void)fprintf(stderr, fmt, x1, x2, x3, x4, x5, x6, x7, x8, x9);
(void)fprintf(stderr, "\n");
}
}
#endif /* __STDC__ */
static void
clear_bbox(bbox)
HBF_BBOX *bbox;
{
bbox->hbf_width = bbox->hbf_height = 0;
bbox->hbf_xDisplacement = bbox->hbf_yDisplacement = 0;
}
static void
clear_record(hbf)
HBF_STRUCT *hbf;
{
clear_bbox(&(hbf->public.hbf_bitmap_bbox));
clear_bbox(&(hbf->public.hbf_font_bbox));
hbf->property = NULL;
hbf->filename = NULL;
hbf->bitmap_buffer = NULL;
hbf->byte_2_range = NULL;
hbf->code_range = NULL;
hbf->bm_file = NULL;
}
/*
* Byte-2 ranges
*/
static void
add_b2r(last_b2r, start, finish)
reg B2_RANGE **last_b2r;
int start;
int finish;
{
reg B2_RANGE *b2r;
b2r = NEW(B2_RANGE);
while (*last_b2r != NULL && (*last_b2r)->b2r_start < start)
last_b2r = &((*last_b2r)->b2r_next);
b2r->b2r_next = *last_b2r;
b2r->b2r_start = start;
b2r->b2r_finish = finish;
*last_b2r = b2r;
}
static CHAR_INDEX
b2_pos(hbf, code)
HBF_STRUCT *hbf;
HBF_CHAR code;
{
reg B2_RANGE *b2r;
reg unsigned c;
reg CHAR_INDEX pos;
c = SecondByte(code);
pos = 0;
for (b2r = hbf->byte_2_range; b2r != NULL; b2r = b2r->b2r_next)
if (b2r->b2r_start <= c && c <= b2r->b2r_finish)
return pos + c - b2r->b2r_start;
else
pos += b2r->b2r_finish - b2r->b2r_start + 1;
return BAD_CHAR_INDEX;
}
static int
b2_size(b2r)
reg B2_RANGE *b2r;
{
reg int size;
size = 0;
for ( ; b2r != NULL; b2r = b2r->b2r_next)
size += b2r->b2r_finish - b2r->b2r_start + 1;
return size;
}
/* map a position to a character code */
static long
code_of(hbf, pos)
HBF_STRUCT *hbf;
long pos;
{
long code;
int residue;
reg B2_RANGE *b2r;
code = pos / hbf->b2_size * 256;
residue = pos % hbf->b2_size;
for (b2r = hbf->byte_2_range; b2r != NULL; b2r = b2r->b2r_next)
if (b2r->b2r_start + residue <= b2r->b2r_finish)
return code + b2r->b2r_start + residue;
else
residue -= b2r->b2r_finish - b2r->b2r_start + 1;
/* should never get here */
return 0L;
}
/*
* String stuff
*/
static bool
match(lp, sp)
reg const char *lp;
reg const char *sp;
{
while (*lp == *sp && *sp != '\0') {
lp++;
sp++;
}
return (*lp == '\0' || isspace(*lp)) && *sp == '\0';
}
#ifdef NO_STRDUP
char *
strdup(s)
const char *s;
{
char *new_s;
new_s = malloc((unsigned)strlen(s) + 1);
strcpy(new_s, s);
return new_s;
}
#endif
/*
* Properties
*/
static void
add_property(hbf, lp)
reg HBF_STRUCT *hbf;
reg const char *lp;
{
reg PROPERTY *prop;
char tmp[MAXLINE];
reg char *tp;
prop = NEW(PROPERTY);
tp = tmp;
while (*lp != '\0' && ! isspace(*lp))
*tp++ = *lp++;
*tp = '\0';
prop->prop_name = strdup(tmp);
while (*lp != '\0' && isspace(*lp))
lp++;
tp = tmp;
if (*lp == QUOTE) {
lp++;
while (*lp != '\0' && ! (*lp == QUOTE && *++lp != QUOTE))
*tp++ = *lp++;
}
else
for (;;) {
while (*lp != '\0' && ! isspace(*lp))
*tp++ = *lp++;
while (*lp != '\0' && isspace(*lp))
lp++;
if (*lp == '\0')
break;
*tp++ = ' ';
}
*tp = '\0';
prop->prop_value = strdup(tmp);
prop->prop_next = hbf->property;
hbf->property = prop;
}
const char *
hbfProperty(hbfFile, propName)
HBF *hbfFile;
const char *propName;
{
reg HBF_STRUCT *hbf;
reg PROPERTY *prop;
hbf = (HBF_STRUCT *)hbfFile;
for (prop = hbf->property; prop != NULL; prop = prop->prop_next)
if (strcmp(prop->prop_name, propName) == 0)
return prop->prop_value;
return NULL;
}
/*
* Compatability routines
*/
const char *
HBF_GetProperty(handle, propertyName)
HBF *handle;
const char *propertyName;
{
return hbfProperty(handle, propertyName);
}
int
HBF_GetFontBoundingBox(handle, width, height, xDisplacement, yDisplacement)
HBF_Handle handle;
unsigned int *width;
unsigned int *height;
int *xDisplacement;
int *yDisplacement;
{
if (width != NULL)
*width = hbfFontBBox(handle)->hbf_width;
if (height != NULL)
*height = hbfFontBBox(handle)->hbf_height;
if (xDisplacement != NULL)
*xDisplacement = hbfFontBBox(handle)->hbf_xDisplacement;
if (yDisplacement != NULL)
*yDisplacement = hbfFontBBox(handle)->hbf_yDisplacement;
return 0;
}
int
HBF_GetBitmapBoundingBox(handle, width, height, xDisplacement, yDisplacement)
HBF_Handle handle;
unsigned int *width;
unsigned int *height;
int *xDisplacement;
int *yDisplacement;
{
if (width != NULL)
*width = hbfBitmapBBox(handle)->hbf_width;
if (height != NULL)
*height = hbfBitmapBBox(handle)->hbf_height;
if (xDisplacement != NULL)
*xDisplacement = hbfBitmapBBox(handle)->hbf_xDisplacement;
if (yDisplacement != NULL)
*yDisplacement = hbfBitmapBBox(handle)->hbf_yDisplacement;
return 0;
}
/*
* Prepend a directory to a relative filename.
*/
static char *
concat(dir, dirlen, stem)
const char *dir; /* not necessarily null-terminated */
int dirlen; /* number of significant chars in dir */
const char *stem; /* relative filename */
{
char *fullname;
if (dirlen == 0) /* null: current directory */
return strdup(stem);
#ifdef unix
fullname = malloc(dirlen + strlen(stem) + 2);
(void)sprintf(fullname, "%.*s/%s", dirlen, dir, stem);
#else
#ifdef msdos
fullname = malloc(dirlen + strlen(stem) + 2);
(void)sprintf(fullname, "%.*s\\%s", dirlen, dir, stem);
#else
#ifdef vms
if (dir[dirlen-1] == ']' && stem[0] == '[' && stem[1] == '-') {
dirlen--;
stem++;
fullname = malloc(dirlen + strlen(stem) + 2);
(void)sprintf(fullname, "%.*s.%s", dirlen, dir, stem);
}
else {
if (dir[dirlen-1] == ']' && stem[0] == '[' && stem[1] == '.') {
dirlen--;
stem++;
}
fullname = malloc(dirlen + strlen(stem) + 1);
(void)sprintf(fullname, "%.*s%s", dirlen, dir, stem);
}
#else
fullname = strdup(stem);
#endif /* vms */
#endif /* msdos */
#endif /* unix */
return fullname;
}
/*
* Bitmap files
*
* If the host operating system has a heirarchical file system and
* the bitmap file name is relative, it is relative to the directory
* containing the HBF file.
*/
static char *
expand_filename(name, hbf_name)
const char *name;
const char *hbf_name;
{
#ifdef unix
reg char *s;
reg int size;
size = name[0] != '/' && (s = strrchr(hbf_name, '/')) != NULL ?
s - hbf_name + 1 : 0;
s = malloc((unsigned)size + strlen(name) + 1);
(void)sprintf(s, "%.*s%s", size, hbf_name, name);
return s;
#else
#ifdef msdos
reg char *s;
reg int size;
#ifdef __EMX__
s = (unsigned char *)hbf_name + strlen((unsigned char *)hbf_name) - 1;
for(;;) {
if (*s == '\\' || *s == '/')
break;
if (s == hbf_name) {
s = NULL;
break;
}
s--;
}
size = HasDrive(name) ? 0 :
(name[0] == '\\' || name[0] == '/') ?
(HasDrive(hbf_name) ? 2 : 0) :
s != NULL ? s - hbf_name + 1 : 0;
#else
size = HasDrive(name) ? 0 :
name[0] == '\\' ? (HasDrive(hbf_name) ? 2 : 0) :
(s = strrchr(hbf_name, '\\')) != NULL ?
s - hbf_name + 1 : 0;
#endif /* __EMX__ */
s = malloc((unsigned)size + strlen(name) + 1);
(void)sprintf(s, "%.*s%s", size, hbf_name, name);
return s;
#else
#ifdef vms
reg char *s;
reg const char *copyto;
reg int size;
if ((s = strchr(hbf_name, ']')) != NULL && RelativeFileName(name))
return concat(hbf_name, (s - hbf_name) + 1, name);
copyto = hbf_name;
if ((s = strstr(copyto, "::")) != NULL && strstr(name, "::") == NULL)
copyto = s+2;
if ((s = strchr(copyto, ':')) != NULL && strchr(name, ':') == NULL)
copyto = s+1;
size = copyto - hbf_name;
s = malloc((unsigned)size + strlen(name) + 1);
(void)sprintf(s, "%.*s%s", size, hbf_name, name);
return s;
#else
return strdup(name);
#endif /* vms */
#endif /* msdos */
#endif /* unix */
}
static BM_FILE *
find_file(hbf, filename)
HBF_STRUCT *hbf;
const char *filename;
{
BM_FILE **fp;
reg BM_FILE *file;
FILE *f;
char *bmfname;
#ifdef IN_MEMORY
#ifdef unix
bool from_pipe;
#endif
#endif
for (fp = &(hbf->bm_file); *fp != NULL; fp = &((*fp)->bmf_next)) {
bmfname = strrchr((*fp)->bmf_name, '/');
bmfname = (bmfname) ? bmfname + 1 : (*fp)->bmf_name;
if (strcmp(bmfname, filename) == 0)
return *fp;
}
file = NEW(BM_FILE);
if (file == NULL) {
eprintf("out of memory");
return NULL;
}
file->bmf_name = expand_filename(filename, hbf->filename);
if (file->bmf_name == NULL) {
free((char *)file);
return NULL;
}
f = fopen(file->bmf_name, READ_BINARY);
#ifdef IN_MEMORY
#ifdef unix
from_pipe = FALSE;
if (f == NULL) {
char tmp[400];
sprintf(tmp, "%s.gz", file->bmf_name);
if ((f = fopen(tmp, "r")) != NULL) {
fclose(f);
sprintf(tmp, "gzcat %s.gz", file->bmf_name);
if ((f = popen(tmp, "r")) != NULL)
from_pipe = TRUE;
}
}
#endif /* unix */
#endif /* IN_MEMORY */
if (f == NULL) {
eprintf("can't open bitmap file '%s'", file->bmf_name);
free(file->bmf_name);
free((char *)file);
return NULL;
}
#ifdef IN_MEMORY
if (! read_bitmap_file(file, f)) {
free(file->bmf_name);
free((char *)file);
return NULL;
}
#ifdef unix
if (from_pipe)
pclose(f);
else
fclose(f);
#else /* ! unix */
fclose(f);
#endif /* ! unix */
#else /* ! IN_MEMORY */
file->bmf_file = f;
fseek(f, 0L, 2);
file->bmf_size = ftell(f);
#endif /* ! IN_MEMORY */
file->bmf_next = NULL;
*fp = file;
return file;
}
#ifdef IN_MEMORY
#define GRAIN_SIZE 512
static bool
read_bitmap_file(bmf, f)
BM_FILE *bmf;
FILE *f;
{
byte *contents, *cp;
long size;
int c;
size = 0;
cp = contents = (byte *)malloc((unsigned)GRAIN_SIZE);
if (contents == NULL) {
eprintf("not enough space for bitmap file");
return NULL;
}
while ((c = getc(f)) != EOF) {
if (size%GRAIN_SIZE == 0) {
contents = (byte *)realloc((char *)contents,
(unsigned)(size + GRAIN_SIZE));
if (contents == NULL) {
eprintf("not enough space for bitmap file");
return NULL;
}
cp = contents + size;
}
*cp++ = c;
size++;
}
bmf->bmf_size = size;
bmf->bmf_contents = (byte *)realloc((char *)contents, (unsigned)size);
return TRUE;
}
#endif /* IN_MEMORY */
/*
* Code ranges
*/
/* check that a code range fits within its bitmap file */
static bool
too_short(hbf, cp)
HBF_STRUCT *hbf;
CODE_RANGE *cp;
{
int bm_size;
long offset, end_offset;
BM_FILE *bmf;
long start, finish;
bm_size = FileBitmapSize(&(hbf->public), cp);
offset = cp->code_offset;
start = cp->code_start;
finish = cp->code_finish;
end_offset = offset + bm_size *
(hbf->b2_size*(long)FirstByte(finish) +
b2_pos(hbf, finish) - cp->code_pos + 1);
bmf = cp->code_bm_file;
if (end_offset <= bmf->bmf_size)
return FALSE;
/* bitmap file is too short: produce a specific error message */
if (offset > bmf->bmf_size)
eprintf("bitmap file '%s' is shorter than offset 0x%04lx",
bmf->bmf_name, offset);
else if (offset + bm_size > bmf->bmf_size)
eprintf("bitmap file '%s' too short: no room for any bitmaps at offset 0x%04lx",
bmf->bmf_name, offset);
else
eprintf("bitmap file '%s' is too short - code range appears to be 0x%04lx-0x%04lx",
bmf->bmf_name,
start,
code_of(hbf, cp->code_pos +
(bmf->bmf_size - offset)/bm_size) - 1);
return TRUE;
}
static const char *
skip_word(n, s)
int n;
const char *s;
{
for ( ; n > 0; n--) {
while (*s != '\0' && ! isspace(*s))
s++;
while (*s != '\0' && isspace(*s))
s++;
}
return s;
}
/* optional keywords at the end of a CODE_RANGE line */
static void
parse_keywords(cp, s)
CODE_RANGE *cp;
const char *s;
{
for (s = skip_word(4, s) ; *s != '\0'; s = skip_word(1, s)) {
switch (*s) {
case 's': case 'S': case 't': case 'T':
/* keyword "sideways" or "transposed" */
cp->code_transposed = TRUE;
break;
case 'i': case 'I':
/* keyword "inverted" */
cp->code_inverted = TRUE;
}
}
}
static bool
add_code_range(hbf, line)
HBF_STRUCT *hbf;
const char *line;
{
CODE_RANGE *cp;
CODE_RANGE **cpp;
long start, finish;
long offset;
char filename[MAXLINE];
BM_FILE *bmf;
CHAR_INDEX b2pos;
if (sscanf(line, "HBF_CODE_RANGE %li-%li %s %li",
&start, &finish, filename, &offset) != 4) {
eprintf("syntax error in HBF_CODE_RANGE");
return FALSE;
}
/* code ranges are checked in real_open() */
if ((bmf = find_file(hbf, filename)) == NULL)
return FALSE;
if ((cp = NEW(CODE_RANGE)) == NULL) {
eprintf("out of memory");
return FALSE;
}
cp->code_start = (CHAR)start;
cp->code_finish = (CHAR)finish;
cp->code_bm_file = bmf;
cp->code_offset = offset;
cp->code_transposed = cp->code_inverted = FALSE;
parse_keywords(cp, line);
/* insert it in order */
for (cpp = &hbf->code_range;
*cpp != NULL && (*cpp)->code_finish < start;
cpp = &((*cpp)->code_next))
;
if (*cpp != NULL && (*cpp)->code_start <= finish) {
eprintf("code ranges overlap");
return FALSE;
}
cp->code_next = *cpp;
*cpp = cp;
/* set code_pos, and check range */
if (start > finish) {
eprintf("illegal code range 0x%04lx-0x%04lx", start, finish);
return FALSE;
}
if ((b2pos = b2_pos(hbf, start)) == BAD_CHAR_INDEX) {
eprintf("illegal start code 0x%04lx", start);
return FALSE;
}
cp->code_pos = hbf->b2_size*(long)FirstByte(start) + b2pos;
if ((b2pos = b2_pos(hbf, finish)) == BAD_CHAR_INDEX) {
eprintf("illegal finish code 0x%04lx", finish);
return FALSE;
}
/* check that the bitmap file has enough bitmaps */
return ! too_short(hbf, cp);
}
/*
* Reading and parsing of an HBF file
*/
/* get line, truncating to len, and trimming trailing spaces */
static bool
get_line(buf, len, f)
char *buf;
int len;
FILE *f;
{
int c;
char *bp;
bp = buf;
for (;;) {
if ((c = getc(f)) == EOF) {
eprintf("unexpected end of file");
return FALSE;
}
if (c == '\n' || c == '\r') {
/* trim trailing space */
while (bp > buf && isspace(*(bp-1)))
bp--;
*bp = '\0';
return TRUE;
}
if (len > 0) {
*bp++ = c;
len--;
}
}
}
/* get next non-COMMENT line */
static bool
get_text_line(buf, len, f)
char *buf;
int len;
FILE *f;
{
while (get_line(buf, len, f))
if (*buf != '\0' && ! match(buf, "COMMENT"))
return TRUE;
return FALSE;
}
static bool
get_property(line, keyword, hbf)
const char *line;
const char *keyword;
HBF_STRUCT *hbf;
{
if (! match(line, keyword)) {
eprintf("%s expected", keyword);
return FALSE;
}
add_property(hbf, line);
return TRUE;
}
static bool
get_bbox(line, keyword, bbox)
const char *line;
const char *keyword;
HBF_BBOX *bbox;
{
int w, h, xd, yd;
if (! match(line, keyword) ||
sscanf(line + strlen(keyword), "%i %i %i %i",
&w, &h, &xd, &yd) != 4) {
eprintf("%s expected", keyword);
return FALSE;
}
if (w <= 0 || h <= 0) {
eprintf("illegal %s dimensions %dx%d", keyword, w, h);
return FALSE;
}
bbox->hbf_width = w;
bbox->hbf_height = h;
bbox->hbf_xDisplacement = xd;
bbox->hbf_yDisplacement = yd;
return TRUE;
}
/*
* HBFHeaderFile ::=
* 'HBF_START_FONT' version EOLN
* 'HBF_CODE_SCHEME' word ... EOLN
* 'FONT' fontName EOLN
* 'SIZE' ptsize xres yres EOLN
* 'HBF_BITMAP_BOUNDING_BOX' w h xd yd EOLN
* 'FONTBOUNDINGBOX' w h xd yd EOLN
* X11R5FontPropertySection
* 'CHARS' n EOLN
* HBFByte2RangeSection
* HBFCodeRangeSection
* 'HBF_END_FONT' EOLN .
*
* This implementation allows extra lines before HBF_END_FONT.
* Anything after HBF_END_FONT is ignored.
*/
static bool
parse_file(f, hbf)
FILE *f;
reg HBF_STRUCT *hbf;