| 14 |
|
|
| 15 |
|
#include "otypes.h" |
| 16 |
|
|
| 17 |
+ |
#include "font.h" |
| 18 |
+ |
|
| 19 |
|
/* |
| 20 |
|
* A text pattern is specified as the text (a file or line), |
| 21 |
|
* the upper left anchor point, the right motion vector, the down |
| 57 |
|
#define fndx(m) ((m)->otype==MIX_TEXT ? 2 : 0) |
| 58 |
|
#define tndx(m) ((m)->otype==MIX_TEXT ? 3 : 1) |
| 59 |
|
|
| 60 |
< |
extern char *libpath; /* library search path */ |
| 60 |
> |
typedef struct tline { |
| 61 |
> |
struct tline *next; /* pointer to next line */ |
| 62 |
> |
/* followed by the string */ |
| 63 |
> |
} TLINE; |
| 64 |
|
|
| 65 |
< |
typedef unsigned char GLYPH; |
| 65 |
> |
#define TLSTR(l) ((char *)((l)+1)) |
| 66 |
|
|
| 62 |
– |
typedef struct font { |
| 63 |
– |
GLYPH *fg[256]; /* font glyphs */ |
| 64 |
– |
char *name; /* font file name */ |
| 65 |
– |
struct font *next; /* next font in list */ |
| 66 |
– |
} FONT; |
| 67 |
– |
|
| 67 |
|
typedef struct { |
| 69 |
– |
char **t; /* text array */ |
| 68 |
|
FVECT right, down; /* right and down unit vectors */ |
| 69 |
|
FONT *f; /* our font */ |
| 70 |
+ |
TLINE tl; /* line list */ |
| 71 |
|
} TEXT; |
| 72 |
|
|
| 73 |
|
extern char *fgetword(); |
| 74 |
|
|
| 75 |
|
TEXT *gettext(); |
| 76 |
|
|
| 77 |
< |
FONT *getfont(); |
| 77 |
> |
TLINE *tlalloc(); |
| 78 |
|
|
| 80 |
– |
static FONT *fontlist = NULL; /* our font list */ |
| 79 |
|
|
| 82 |
– |
|
| 80 |
|
text(m, r) |
| 81 |
|
register OBJREC *m; |
| 82 |
|
RAY *r; |
| 83 |
|
{ |
| 84 |
< |
double v[3]; |
| 84 |
> |
FVECT v; |
| 85 |
|
int foreground; |
| 86 |
|
/* get transformed position */ |
| 87 |
|
if (r->rox != NULL) |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
+ |
TLINE * |
| 125 |
+ |
tlalloc(s) /* allocate and assign text line */ |
| 126 |
+ |
char *s; |
| 127 |
+ |
{ |
| 128 |
+ |
extern char *strcpy(); |
| 129 |
+ |
register TLINE *tl; |
| 130 |
+ |
|
| 131 |
+ |
tl = (TLINE *)malloc(sizeof(TLINE)+1+strlen(s)); |
| 132 |
+ |
if (tl == NULL) |
| 133 |
+ |
error(SYSTEM, "out of memory in tlalloc"); |
| 134 |
+ |
tl->next = NULL; |
| 135 |
+ |
strcpy(TLSTR(tl), s); |
| 136 |
+ |
return(tl); |
| 137 |
+ |
} |
| 138 |
+ |
|
| 139 |
+ |
|
| 140 |
|
TEXT * |
| 141 |
|
gettext(tm) /* get text structure for material */ |
| 142 |
|
register OBJREC *tm; |
| 148 |
|
double d; |
| 149 |
|
FILE *fp; |
| 150 |
|
char linbuf[512]; |
| 151 |
< |
register TEXT *t; |
| 151 |
> |
TEXT *t; |
| 152 |
|
register int i; |
| 153 |
+ |
register TLINE *tlp; |
| 154 |
|
register char *s; |
| 155 |
|
|
| 156 |
|
if ((t = (TEXT *)tm->os) != NULL) |
| 161 |
|
tm->otype == PAT_CTEXT ? 15 : 9)) |
| 162 |
|
objerror(tm, USER, "bad # arguments"); |
| 163 |
|
if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL) |
| 164 |
< |
goto memerr; |
| 164 |
> |
error(SYSTEM, "out of memory in gettext"); |
| 165 |
|
/* compute vectors */ |
| 166 |
|
fcross(DxR, D, R); |
| 167 |
|
fcross(t->right, DxR, D); |
| 168 |
< |
d = DOT(D,D) / DOT(t->right,t->right); |
| 168 |
> |
d = DOT(D,D)/DOT(t->right,t->right); |
| 169 |
|
for (i = 0; i < 3; i++) |
| 170 |
|
t->right[i] *= d; |
| 171 |
|
fcross(t->down, R, DxR); |
| 172 |
< |
d = DOT(R,R) / DOT(t->down,t->down); |
| 172 |
> |
d = DOT(R,R)/DOT(t->down,t->down); |
| 173 |
|
for (i = 0; i < 3; i++) |
| 174 |
|
t->down[i] *= d; |
| 175 |
|
/* get text */ |
| 176 |
< |
t->t = (char **)malloc(2*sizeof(char **)); |
| 163 |
< |
if (t->t == NULL) |
| 164 |
< |
goto memerr; |
| 176 |
> |
tlp = &t->tl; |
| 177 |
|
if (tm->oargs.nsargs - tndx(tm) > 1) { /* single line */ |
| 178 |
|
s = linbuf; |
| 179 |
|
for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) { |
| 182 |
|
*s++ = ' '; |
| 183 |
|
} |
| 184 |
|
*--s = '\0'; |
| 185 |
< |
t->t[0] = savqstr(linbuf); |
| 186 |
< |
t->t[1] = NULL; |
| 185 |
> |
tlp->next = tlalloc(linbuf); |
| 186 |
> |
tlp = tlp->next; |
| 187 |
|
} else { /* text file */ |
| 188 |
|
if ((s = getpath(tm->oargs.sarg[tndx(tm)], |
| 189 |
|
libpath, R_OK)) == NULL) { |
| 196 |
|
s); |
| 197 |
|
error(SYSTEM, errmsg); |
| 198 |
|
} |
| 199 |
< |
for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) { |
| 199 |
> |
while (fgets(linbuf, sizeof(linbuf), fp) != NULL) { |
| 200 |
|
s = linbuf + strlen(linbuf) - 1; |
| 201 |
|
if (*s == '\n') |
| 202 |
|
*s = '\0'; |
| 203 |
< |
t->t=(char **)realloc((char *)t->t, |
| 204 |
< |
(i+2)*sizeof(char **)); |
| 193 |
< |
if (t->t == NULL) |
| 194 |
< |
goto memerr; |
| 195 |
< |
t->t[i] = savqstr(linbuf); |
| 203 |
> |
tlp->next = tlalloc(linbuf); |
| 204 |
> |
tlp = tlp->next; |
| 205 |
|
} |
| 197 |
– |
t->t[i] = NULL; |
| 206 |
|
fclose(fp); |
| 207 |
|
} |
| 208 |
+ |
tlp->next = NULL; |
| 209 |
|
/* get the font */ |
| 210 |
|
t->f = getfont(tm->oargs.sarg[fndx(tm)]); |
| 211 |
|
/* we're done */ |
| 212 |
|
tm->os = (char *)t; |
| 213 |
|
return(t); |
| 205 |
– |
memerr: |
| 206 |
– |
error(SYSTEM, "out of memory in gettext"); |
| 214 |
|
#undef R |
| 215 |
|
#undef D |
| 216 |
|
} |
| 220 |
|
OBJREC *m; |
| 221 |
|
{ |
| 222 |
|
register TEXT *tp; |
| 223 |
< |
register int i; |
| 223 |
> |
register TLINE *tlp; |
| 224 |
|
|
| 225 |
|
tp = (TEXT *)m->os; |
| 226 |
|
if (tp == NULL) |
| 227 |
|
return; |
| 228 |
< |
for (i = 0; tp->t[i] != NULL; i++) |
| 229 |
< |
freeqstr(tp->t[i]); |
| 223 |
< |
free((char *)tp->t); |
| 228 |
> |
for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next); |
| 229 |
> |
free((char *)tlp); |
| 230 |
|
free((char *)tp); |
| 231 |
|
m->os = NULL; |
| 232 |
|
} |
| 234 |
|
|
| 235 |
|
intext(p, m) /* check to see if p is in text glyph */ |
| 236 |
|
FVECT p; |
| 237 |
< |
register OBJREC *m; |
| 237 |
> |
OBJREC *m; |
| 238 |
|
{ |
| 239 |
|
register TEXT *tp; |
| 240 |
< |
register int i; |
| 241 |
< |
double v[3], y, x; |
| 242 |
< |
int col, lno; |
| 240 |
> |
register TLINE *tlp; |
| 241 |
> |
FVECT v; |
| 242 |
> |
double y, x; |
| 243 |
> |
int col; |
| 244 |
> |
register int lno; |
| 245 |
|
/* first, compute position in text */ |
| 246 |
+ |
tp = gettext(m); |
| 247 |
|
v[0] = p[0] - m->oargs.farg[0]; |
| 248 |
|
v[1] = p[1] - m->oargs.farg[1]; |
| 249 |
|
v[2] = p[2] - m->oargs.farg[2]; |
| 254 |
|
x -= (double)col; |
| 255 |
|
y = (lno+1) - y; |
| 256 |
|
/* get the font character */ |
| 257 |
< |
tp = gettext(m); |
| 258 |
< |
for (i = 0; i < lno; i++) |
| 259 |
< |
if (tp->t[i] == NULL) |
| 260 |
< |
return(0); |
| 252 |
< |
if (col >= strlen(tp->t[i])) |
| 257 |
> |
for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next) |
| 258 |
> |
if (--lno < 0) |
| 259 |
> |
break; |
| 260 |
> |
if (tlp == NULL || col >= strlen(TLSTR(tlp))) |
| 261 |
|
return(0); |
| 262 |
< |
return(inglyph(x, y, tp->f->fg[tp->t[i][col]])); |
| 262 |
> |
return(inglyph(x, y, tp->f->fg[TLSTR(tlp)[col]&0xff])); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
|
| 258 |
– |
FONT * |
| 259 |
– |
getfont(fname) /* return font fname */ |
| 260 |
– |
char *fname; |
| 261 |
– |
{ |
| 262 |
– |
char buf[16]; |
| 263 |
– |
FILE *fp; |
| 264 |
– |
char *pathname, *err; |
| 265 |
– |
int gn, ngv, gv; |
| 266 |
– |
register GLYPH *g; |
| 267 |
– |
register FONT *f; |
| 268 |
– |
|
| 269 |
– |
for (f = fontlist; f != NULL; f = f->next) |
| 270 |
– |
if (!strcmp(f->name, fname)) |
| 271 |
– |
return(f); |
| 272 |
– |
/* load the font file */ |
| 273 |
– |
if ((pathname = getpath(fname, libpath, R_OK)) == NULL) { |
| 274 |
– |
sprintf(errmsg, "cannot find font file \"%s\"", fname); |
| 275 |
– |
error(USER, errmsg); |
| 276 |
– |
} |
| 277 |
– |
f = (FONT *)calloc(1, sizeof(FONT)); |
| 278 |
– |
if (f == NULL) |
| 279 |
– |
goto memerr; |
| 280 |
– |
f->name = savestr(fname); |
| 281 |
– |
if ((fp = fopen(pathname, "r")) == NULL) { |
| 282 |
– |
sprintf(errmsg, "cannot open font file \"%s\"", |
| 283 |
– |
pathname); |
| 284 |
– |
error(SYSTEM, errmsg); |
| 285 |
– |
} |
| 286 |
– |
while (fgetword(buf,sizeof(buf),fp) != NULL) { /* get each glyph */ |
| 287 |
– |
if (!isint(buf)) |
| 288 |
– |
goto nonint; |
| 289 |
– |
gn = atoi(buf); |
| 290 |
– |
if (gn < 0 || gn > 255) { |
| 291 |
– |
err = "illegal"; |
| 292 |
– |
goto fonterr; |
| 293 |
– |
} |
| 294 |
– |
if (f->fg[gn] != NULL) { |
| 295 |
– |
err = "duplicate"; |
| 296 |
– |
goto fonterr; |
| 297 |
– |
} |
| 298 |
– |
if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) || |
| 299 |
– |
(ngv = atoi(buf)) < 0 || ngv > 255) { |
| 300 |
– |
err = "bad # vertices for"; |
| 301 |
– |
goto fonterr; |
| 302 |
– |
} |
| 303 |
– |
g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH)); |
| 304 |
– |
if (g == NULL) |
| 305 |
– |
goto memerr; |
| 306 |
– |
f->fg[gn] = g; |
| 307 |
– |
*g++ = ngv; |
| 308 |
– |
ngv *= 2; |
| 309 |
– |
while (ngv--) { |
| 310 |
– |
if (fgetword(buf,sizeof(buf),fp) == NULL || |
| 311 |
– |
!isint(buf) || |
| 312 |
– |
(gv = atoi(buf)) < 0 || gv > 255) { |
| 313 |
– |
err = "bad vertex for"; |
| 314 |
– |
goto fonterr; |
| 315 |
– |
} |
| 316 |
– |
*g++ = gv; |
| 317 |
– |
} |
| 318 |
– |
} |
| 319 |
– |
fclose(fp); |
| 320 |
– |
f->next = fontlist; |
| 321 |
– |
return(fontlist = f); |
| 322 |
– |
nonint: |
| 323 |
– |
sprintf(errmsg, "non-integer in font file \"%s\"", pathname); |
| 324 |
– |
error(USER, errmsg); |
| 325 |
– |
fonterr: |
| 326 |
– |
sprintf(errmsg, "%s character (%d) in font file \"%s\"", |
| 327 |
– |
err, gn, pathname); |
| 328 |
– |
error(USER, errmsg); |
| 329 |
– |
memerr: |
| 330 |
– |
error(SYSTEM, "out of memory in fontglyph"); |
| 331 |
– |
} |
| 332 |
– |
|
| 333 |
– |
|
| 266 |
|
inglyph(x, y, gl) /* (x,y) within font glyph gl? */ |
| 267 |
|
double x, y; |
| 268 |
|
GLYPH *gl; |
| 269 |
|
{ |
| 270 |
|
int n, ncross; |
| 271 |
|
int xlb, ylb; |
| 272 |
< |
register GLYPH *p0, *p1; |
| 272 |
> |
register GORD *p0, *p1; |
| 273 |
|
|
| 274 |
|
if (gl == NULL) |
| 275 |
|
return(0); |
| 277 |
|
y *= 256.0; |
| 278 |
|
xlb = x + 0.5; |
| 279 |
|
ylb = y + 0.5; |
| 280 |
< |
n = *gl++; /* get # of vertices */ |
| 281 |
< |
p0 = gl + 2*(n-1); /* connect last to first */ |
| 282 |
< |
p1 = gl; |
| 280 |
> |
n = gl->nverts; /* get # of vertices */ |
| 281 |
> |
p0 = gvlist(gl) + 2*(n-1); /* connect last to first */ |
| 282 |
> |
p1 = gvlist(gl); |
| 283 |
|
ncross = 0; |
| 284 |
|
/* positive x axis cross test */ |
| 285 |
|
while (n--) { |