BRL-CAD
vfont.c
Go to the documentation of this file.
1 /* V F O N T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <string.h> /* for memset */
25 #include "bu/file.h"
26 #include "bu/malloc.h"
27 #include "bu/vfont.h"
28 
29 #define FONTDIR2 "/usr/lib/vfont"
30 #define DEFAULT_FONT "nonie.r.12"
31 #define FONTNAMESZ 128
32 
33 #include "./vfont.h"
34 
35 
36 struct vfont_file
37 get_font(const char* fontname, void (*vfont_log)(const char *fmt, ...))
38 {
39  struct vfont_file font;
40  struct header lochdr;
41  static char fname[FONTNAMESZ];
42 
43  /* Initialize vfont */
44  memset(&font, 0, sizeof(struct vfont_file));
45 
46  if (fontname == NULL)
47  fontname = FONTNAME;
48 
49  if (fontname[0] != '/') {
50  /* absolute path */
51  const char *vfont = bu_brlcad_data("vfont", 1);
52  if (vfont)
53  snprintf(fname, FONTNAMESZ, "%s/%s", vfont, fontname);
54  else
55  bu_strlcpy(fname, fontname, sizeof(fname));
56  } else
57  bu_strlcpy(fname, fontname, sizeof(fname));
58 
59  /* Open the file and read in the header information. */
60  font.ffdes = fopen(fname, "rb");
61  if (font.ffdes == NULL) {
62  if (vfont_log)
63  vfont_log("Error opening font file '%s'\n", fname);
64 
65  font.ffdes = NULL;
66  return font;
67  }
68 
69  if (fread((char *)&lochdr, (int)sizeof(struct header), 1, font.ffdes) != 1) {
70  if (vfont_log)
71  vfont_log("get_Font() read failed!\n");
72  font.ffdes = NULL;
73  return font;
74  }
75 
76  SWAB(lochdr.magic);
77  SWAB(lochdr.size);
78  SWAB(lochdr.maxx);
79  SWAB(lochdr.maxy);
80  SWAB(lochdr.xtend);
81 
82  if (lochdr.magic != 0436) {
83  if (vfont_log)
84  vfont_log("Not a font file \"%s\": magic=0%o\n", fname, (int)lochdr.magic);
85  font.ffdes = NULL;
86  return font;
87  }
88  font.hdr = lochdr;
89 
90  /* Read in the directory for the font. */
91  if (fread((char *) font.dir, (int)sizeof(struct dispatch), 256, font.ffdes) != 256) {
92  if (vfont_log)
93  vfont_log("get_Font() read failed!\n");
94  font.ffdes = NULL;
95  return font;
96  }
97 
98  /* Addresses of characters in the file are relative to point in
99  * the file after the directory, so grab the current position.
100  */
101  font.offset = bu_ftell(font.ffdes);
102 
103  return font;
104 }
105 
106 
107 /**
108  * Obtain a 16-bit signed integer from two adjacent characters, stored
109  * in VAX order, regardless of word alignment.
110  */
111 HIDDEN int
112 _vax_gshort(unsigned char *msgp)
113 {
114  register unsigned char *p = (unsigned char *) msgp;
115  register int i;
116 
117  if ((i = (p[1] << 8) | p[0]) & 0x8000)
118  return i | ~0xFFFF; /* Sign extend */
119  return i;
120 }
121 
122 
123 struct vfont *
124 vfont_get(char *font)
125 {
126  register struct vfont *vfp = VFONT_NULL;
127  register FILE *fp = NULL;
128  register int i;
129  char fname[FONTNAMESZ];
130  unsigned char header[2*5]; /* 5 16-bit vax shorts */
131  unsigned char dispatch[10*256]; /* 256 10-byte structs */
132  uint16_t magic;
133  int size;
134  const char *const_font;
135 
136  const_font = (font == NULL) ? DEFAULT_FONT : (const char *)font;
137 
138  /* Open the file and read in the header information. */
139  if ((fp = fopen(const_font, "rb")) == NULL) {
140  snprintf(fname, FONTNAMESZ, "%s/%s", (char *)bu_brlcad_data("vfont", 0), const_font);
141  if ((fp = fopen(fname, "rb")) == NULL) {
142  snprintf(fname, FONTNAMESZ, "%s/%s", FONTDIR2, const_font);
143  if ((fp = fopen(fname, "rb")) == NULL) {
144  return VFONT_NULL;
145  }
146  }
147  }
148  if (fread((char *)header, sizeof(header), 1, fp) != 1 ||
149  fread((char *)dispatch, sizeof(dispatch), 1, fp) != 1) {
150  fprintf(stderr, "vfont_get(%s): header read error\n", fname);
151  fclose(fp);
152  return VFONT_NULL;
153  }
154  magic = _vax_gshort(&header[0*2]) & 0xFFFF;
155  size = _vax_gshort(&header[1*2]) & 0xFFFF; /* unsigned short */
156 
157  if (UNLIKELY(magic != 0436)) {
158  fprintf(stderr, "vfont_get(%s): bad magic number 0%o\n",
159  fname, magic);
160  fclose(fp);
161  return VFONT_NULL;
162  }
163 
164  /* Read in the bit maps */
165  BU_ALLOC(vfp, struct vfont);
166  vfp->vf_bits = (char *)bu_malloc((size_t)size, "vfont bits");
167  if (fread(vfp->vf_bits, (size_t)size, 1, fp) != 1) {
168  fprintf(stderr, "vfont_get(%s): bitmap read error\n", fname);
169  fclose(fp);
170  bu_free(vfp->vf_bits, "vfont bits");
171  bu_free((char *)vfp, "vfont");
172  return VFONT_NULL;
173  }
174 
175  /*
176  * Convert VAX data in header[] and dispatch[] arrays to native
177  * machine form.
178  */
179  vfp->vf_maxx = _vax_gshort(&header[2*2]);
180  vfp->vf_maxy = _vax_gshort(&header[3*2]);
181  vfp->vf_xtend = _vax_gshort(&header[4*2]);
182 
183  for (i=0; i<255; i++) {
184  register struct vfont_dispatch *vdp = &(vfp->vf_dispatch[i]);
185  register unsigned char *cp = &dispatch[i*10];
186 
187  vdp->vd_addr = _vax_gshort(&cp[0]);
188  vdp->vd_nbytes = _vax_gshort(&cp[2]);
189  vdp->vd_up = SXT(cp[4]);
190  vdp->vd_down = SXT(cp[5]);
191  vdp->vd_left = SXT(cp[6]);
192  vdp->vd_right = SXT(cp[7]);
193  vdp->vd_width = _vax_gshort(&cp[8]);
194  }
195  fclose(fp);
196  return vfp;
197 }
198 
199 
200 void
201 vfont_free(register struct vfont *vfp)
202 {
203  bu_free(vfp->vf_bits, "vfont bits");
204  bu_free((char *)vfp, "vfont");
205 }
206 
207 
208 /*
209  * Local Variables:
210  * mode: C
211  * tab-width: 8
212  * indent-tabs-mode: t
213  * c-file-style: "stroustrup"
214  * End:
215  * ex: shiftwidth=4 tabstop=8
216  */
struct vfont_file get_font(const char *fontname, void(*vfont_log)(const char *fmt,...))
Definition: vfont.c:37
Definition: vfont.h:53
off_t bu_ftell(FILE *stream)
Definition: file.c:344
short vd_nbytes
Definition: vfont.h:77
Definition: vfont.h:61
#define VFONT_NULL
Definition: vfont.h:91
short vf_maxy
Definition: vfont.h:86
Header file for the BRL-CAD common definitions.
#define HIDDEN
Definition: common.h:86
struct vfont * vfont_get(char *font)
Definition: vfont.c:124
const char * bu_brlcad_data(const char *rhs, int fail_quietly)
Definition: brlcad_path.c:405
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
short vf_maxx
Definition: vfont.h:85
#define FONTNAME
Definition: vfont.h:39
#define FONTNAMESZ
Definition: vfont.c:31
struct header hdr
Definition: vfont.h:71
short vd_down
Definition: vfont.h:79
void * memset(void *s, int c, size_t n)
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define SXT(c)
Definition: vfont.h:73
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
#define SWAB(shrt)
Definition: vfont.h:35
short vd_width
Definition: vfont.h:82
char * vf_bits
Definition: vfont.h:89
oldeumate l2 magic
Definition: nmg_mod.c:3843
Definition: vfont.h:84
#define FONTDIR2
Definition: vfont.c:29
#define DEFAULT_FONT
Definition: vfont.c:30
unsigned short size
Definition: vfont.h:55
FILE * ffdes
Definition: vfont.h:69
short vd_left
Definition: vfont.h:80
short vd_up
Definition: vfont.h:78
struct dispatch dir[256]
Definition: vfont.h:72
void vfont_free(register struct vfont *vfp)
Definition: vfont.c:201
struct vfont_dispatch vf_dispatch[256]
Definition: vfont.h:88
unsigned short vd_addr
Definition: vfont.h:76
short maxx
Definition: vfont.h:56
short xtend
Definition: vfont.h:58
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
HIDDEN int _vax_gshort(unsigned char *msgp)
Definition: vfont.c:112
off_t offset
Definition: vfont.h:70
short maxy
Definition: vfont.h:57
short magic
Definition: vfont.h:54
short vd_right
Definition: vfont.h:81
short vf_xtend
Definition: vfont.h:87
#define UNLIKELY(expression)
Definition: common.h:282