| 1 |
% This file is part of MetaPost; |
| 2 |
% the MetaPost program is in the public domain. |
| 3 |
% See the <Show version...> code in mpost.w for more info. |
| 4 |
|
| 5 |
\def\title{Creating mpx files} |
| 6 |
\def\hang{\hangindent 3em\indent\ignorespaces} |
| 7 |
\def\MP{MetaPost} |
| 8 |
\def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc a}\kern-.15em |
| 9 |
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} |
| 10 |
|
| 11 |
\def\(#1){} % this is used to make section names sort themselves better |
| 12 |
\def\9#1{} % this is used for sort keys in the index |
| 13 |
|
| 14 |
@s avl_tree int |
| 15 |
@s boolean int |
| 16 |
@s dirent int |
| 17 |
@s _finddata_t int |
| 18 |
@s intptr_t int |
| 19 |
@s pid_t int |
| 20 |
|
| 21 |
\pdfoutput=1 |
| 22 |
|
| 23 |
@** Makempx overview. |
| 24 |
|
| 25 |
This source file implements the makempx functionality for the new \MP. |
| 26 |
It includes all of the functional code from the old standalone programs |
| 27 |
|
| 28 |
\item{}mpto |
| 29 |
\item{}dmp |
| 30 |
\item{}dvitomp |
| 31 |
\item{}makempx |
| 32 |
|
| 33 |
combined into one, with many changes to make all of the code cooperate |
| 34 |
nicely. |
| 35 |
|
| 36 |
@ Header files |
| 37 |
|
| 38 |
The local C preprocessor definitions have to come after the C includes |
| 39 |
in order to prevent name clashes. |
| 40 |
|
| 41 |
@c |
| 42 |
#include "mpconfig.h" |
| 43 |
#include <w2c/config.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
#include <stdarg.h> |
| 48 |
#include <assert.h> |
| 49 |
#include <setjmp.h> |
| 50 |
#include <errno.h> /* TODO autoconf ? */ @t@> |
| 51 |
/* unistd.h is needed for every non-Win32 platform, and we assume */ @t@> |
| 52 |
/* that implies that sys/types.h is also present */ |
| 53 |
#ifndef WIN32 |
| 54 |
#include <sys/types.h> |
| 55 |
#include <unistd.h> |
| 56 |
#endif |
| 57 |
/* processes */ |
| 58 |
#ifdef WIN32 |
| 59 |
#include <io.h> |
| 60 |
#include <process.h> |
| 61 |
#else |
| 62 |
#if HAVE_SYS_WAIT_H |
| 63 |
# include <sys/wait.h> |
| 64 |
#endif |
| 65 |
#ifndef WEXITSTATUS |
| 66 |
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
| 67 |
#endif |
| 68 |
#ifndef WIFEXITED |
| 69 |
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
| 70 |
#endif |
| 71 |
#endif |
| 72 |
/* directories */ |
| 73 |
#ifdef WIN32 |
| 74 |
#include <direct.h> |
| 75 |
#else |
| 76 |
#if HAVE_DIRENT_H |
| 77 |
# include <dirent.h> |
| 78 |
#else |
| 79 |
# define dirent direct |
| 80 |
# if HAVE_SYS_NDIR_H |
| 81 |
# include <sys/ndir.h> |
| 82 |
# endif |
| 83 |
# if HAVE_SYS_DIR_H |
| 84 |
# include <sys/dir.h> |
| 85 |
# endif |
| 86 |
# if HAVE_NDIR_H |
| 87 |
# include <ndir.h> |
| 88 |
# endif |
| 89 |
#endif |
| 90 |
#endif |
| 91 |
#if HAVE_SYS_STAT_H |
| 92 |
#include <sys/stat.h> |
| 93 |
#endif |
| 94 |
#include <ctype.h> |
| 95 |
#include <time.h> |
| 96 |
#include <math.h> |
| 97 |
#define trunc(x) ((integer) (x)) |
| 98 |
#define fabs(x) ((x)<0?(-(x)):(x)) |
| 99 |
#define floor(x) ((integer) (fabs(x))) |
| 100 |
#ifndef PI |
| 101 |
#define PI 3.14159265358979323846 |
| 102 |
#endif |
| 103 |
#include "avl.h" |
| 104 |
#include "mpxout.h" |
| 105 |
@h |
| 106 |
|
| 107 |
@ Data types |
| 108 |
|
| 109 |
From the Pascal code of DVItoMP two implicit types are inherited: |web_boolean| and |
| 110 |
|web_integer|. |
| 111 |
|
| 112 |
The more complex datatypes are defined in the following sections. |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@c |
| 117 |
typedef signed int web_integer; |
| 118 |
typedef signed int web_boolean; |
| 119 |
@<C Data Types@>@; |
| 120 |
@<Declarations@>@; |
| 121 |
|
| 122 |
@ The single most important data structure is the structure |
| 123 |
|mpx_data|. It contains all of the global state for a specific |
| 124 |
|makempx| run. A pointer to it is passed as the first argument to just |
| 125 |
about every function call. |
| 126 |
|
| 127 |
One of the fields is a bit special because it is so important: |mode| |
| 128 |
is the decider between running \TeX\ or Troff as the typesetting |
| 129 |
engine. |
| 130 |
|
| 131 |
@(mpxout.h@>= |
| 132 |
#ifndef MPXOUT_H |
| 133 |
#define MPXOUT_H 1 |
| 134 |
typedef enum { |
| 135 |
mpx_tex_mode=0, |
| 136 |
mpx_troff_mode=1 |
| 137 |
} mpx_modes; |
| 138 |
typedef struct mpx_data * MPX; |
| 139 |
@<Makempx header information@>@; |
| 140 |
#endif |
| 141 |
|
| 142 |
@ @<C Data Types@>= |
| 143 |
@<Types in the outer block@> |
| 144 |
typedef struct mpx_data { |
| 145 |
int mode; |
| 146 |
@<Globals@> |
| 147 |
} mpx_data ; |
| 148 |
|
| 149 |
@ Here are some macros for common programming idioms. |
| 150 |
|
| 151 |
@d MAXINT 0x7FFFFF /* somewhat arbitrary */ |
| 152 |
|
| 153 |
@d incr(A) (A)=(A)+1 /* increase a variable by unity */ |
| 154 |
@d decr(A) (A)=(A)-1 /* decrease a variable by unity */ |
| 155 |
|
| 156 |
@ Once an MPX object is allocated, the memory it occupies needs to be |
| 157 |
initialized to a usable state. This procedure gets things started |
| 158 |
properly. |
| 159 |
|
| 160 |
This function is not allowed to run |mpx_abort| because at this |
| 161 |
point the jump buffer is not yet initialized, so it should only |
| 162 |
be used for things that cannot go wrong! |
| 163 |
|
| 164 |
@c |
| 165 |
static void mpx_initialize (MPX mpx) { |
| 166 |
memset(mpx,0,sizeof(struct mpx_data)); |
| 167 |
@<Set initial values@>@/ |
| 168 |
} |
| 169 |
|
| 170 |
@ A global variable |history| keeps track of what type of errors have |
| 171 |
occurred with the hope that that \MP\ can be warned of any problems. |
| 172 |
|
| 173 |
@<Types...@>= |
| 174 |
enum mpx_history_states { |
| 175 |
mpx_spotless=0, /* |history| value when no problems have been found */ |
| 176 |
mpx_cksum_trouble, /* |history| value there have been font checksum mismatches */ |
| 177 |
mpx_warning_given, /* |history| value after a recoverable error */ |
| 178 |
mpx_fatal_error /* |history| value if processing had to be aborted */ |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
@ @<Glob...@>= |
| 183 |
int history; |
| 184 |
|
| 185 |
@ @<Set init...@>= |
| 186 |
mpx->history=mpx_spotless; |
| 187 |
|
| 188 |
@ The structure has room for the names and the |FILE *| for the |
| 189 |
input and output files. The most important ones are listed here, |
| 190 |
the variables for the intermediate files are declared where they |
| 191 |
are needed. |
| 192 |
|
| 193 |
@<Globals@>= |
| 194 |
char *banner; |
| 195 |
char *mpname; |
| 196 |
FILE *mpfile; |
| 197 |
char *mpxname; |
| 198 |
FILE *mpxfile; |
| 199 |
FILE *errfile; |
| 200 |
int lnno ; /* current line number */ |
| 201 |
|
| 202 |
@ A set of basic reporting functions. |
| 203 |
|
| 204 |
@c |
| 205 |
static void mpx_printf(MPX mpx, const char *header, const char *msg, va_list ap) { |
| 206 |
fprintf(mpx->errfile, "makempx %s: %s:", header, mpx->mpname); |
| 207 |
if (mpx->lnno!=0) |
| 208 |
fprintf(mpx->errfile, "%d:", mpx->lnno); |
| 209 |
fprintf(mpx->errfile, " "); |
| 210 |
(void)vfprintf(mpx->errfile, msg, ap); |
| 211 |
fprintf(mpx->errfile, "\n"); |
| 212 |
} |
| 213 |
|
| 214 |
@ @c |
| 215 |
static void mpx_report(MPX mpx, const char *msg, ...) { |
| 216 |
va_list ap; |
| 217 |
if (mpx->debug==0) return; |
| 218 |
va_start(ap, msg); |
| 219 |
mpx_printf(mpx, "debug", msg, ap); |
| 220 |
va_end(ap); |
| 221 |
if ( mpx->history < mpx_warning_given ) |
| 222 |
mpx->history=mpx_cksum_trouble; |
| 223 |
} |
| 224 |
|
| 225 |
@ @c |
| 226 |
static void mpx_warn(MPX mpx, const char *msg, ...) { |
| 227 |
va_list ap; |
| 228 |
va_start(ap, msg); |
| 229 |
mpx_printf(mpx, "warning", msg, ap); |
| 230 |
va_end(ap); |
| 231 |
if ( mpx->history < mpx_warning_given ) |
| 232 |
mpx->history=mpx_cksum_trouble; |
| 233 |
} |
| 234 |
|
| 235 |
@ @c |
| 236 |
static void mpx_error(MPX mpx, const char *msg, ...) { |
| 237 |
va_list ap; |
| 238 |
va_start(ap, msg); |
| 239 |
mpx_printf(mpx, "error", msg, ap); |
| 240 |
va_end(ap); |
| 241 |
mpx->history=mpx_warning_given; |
| 242 |
} |
| 243 |
|
| 244 |
@ The program uses a |jump_buf| to handle non-local returns, |
| 245 |
this is initialized at a single spot: the start of |mp_makempx|. |
| 246 |
|
| 247 |
@d mpx_jump_out longjmp(mpx->jump_buf,1) |
| 248 |
|
| 249 |
@<Glob...@>= |
| 250 |
jmp_buf jump_buf; |
| 251 |
|
| 252 |
@ |
| 253 |
@c |
| 254 |
static void mpx_abort(MPX mpx, const char *msg, ...) { |
| 255 |
va_list ap; |
| 256 |
va_start(ap, msg); |
| 257 |
fprintf(stderr, "fatal: "); |
| 258 |
(void)vfprintf(stderr, msg, ap); |
| 259 |
va_end(ap); |
| 260 |
va_start(ap, msg); |
| 261 |
mpx_printf(mpx, "fatal", msg, ap); |
| 262 |
va_end(ap); |
| 263 |
mpx->history=mpx_fatal_error; |
| 264 |
mpx_erasetmp(mpx); |
| 265 |
mpx_jump_out; |
| 266 |
} |
| 267 |
|
| 268 |
@ @<Install and test the non-local jump buffer@>= |
| 269 |
if (setjmp(mpx->jump_buf) != 0) { |
| 270 |
int h = mpx->history; |
| 271 |
xfree(mpx->buf); |
| 272 |
xfree(mpx->maincmd); |
| 273 |
xfree(mpx->mpname); |
| 274 |
xfree(mpx->mpxname); |
| 275 |
xfree(mpx); |
| 276 |
return h; |
| 277 |
} |
| 278 |
|
| 279 |
@ @c |
| 280 |
static FILE *mpx_xfopen (MPX mpx, const char *fname, const char *fmode) { |
| 281 |
FILE *f = fopen(fname,fmode); |
| 282 |
if (f == NULL) |
| 283 |
mpx_abort(mpx,"File open error for %s in mode %s", fname, fmode); |
| 284 |
return f; |
| 285 |
} |
| 286 |
static void mpx_fclose (MPX mpx, FILE *file) { |
| 287 |
(void)mpx; |
| 288 |
(void)fclose(file); |
| 289 |
} |
| 290 |
|
| 291 |
@ |
| 292 |
@d xfree(A) do { mpx_xfree(A); A=NULL; } while (0)@; |
| 293 |
@d xrealloc(P,A,B) mpx_xrealloc(mpx,P,A,B) |
| 294 |
@d xmalloc(A,B) mpx_xmalloc(mpx,A,B) |
| 295 |
@d xstrdup(A) mpx_xstrdup(mpx,A) |
| 296 |
|
| 297 |
@<Declarations@>= |
| 298 |
static void mpx_xfree (void *x); |
| 299 |
static void *mpx_xrealloc (MPX mpx, void *p, size_t nmem, size_t size) ; |
| 300 |
static void *mpx_xmalloc (MPX mpx, size_t nmem, size_t size) ; |
| 301 |
static char *mpx_xstrdup(MPX mpX, const char *s); |
| 302 |
|
| 303 |
|
| 304 |
@ The |max_size_test| guards against overflow, on the assumption that |
| 305 |
|size_t| is at least 31bits wide. |
| 306 |
|
| 307 |
@d max_size_test 0x7FFFFFFF |
| 308 |
|
| 309 |
@c |
| 310 |
static void mpx_xfree (void *x) { |
| 311 |
if (x!=NULL) free(x); |
| 312 |
} |
| 313 |
static void *mpx_xrealloc (MPX mpx, void *p, size_t nmem, size_t size) { |
| 314 |
void *w ; |
| 315 |
if ((max_size_test/size)<nmem) { |
| 316 |
mpx_abort(mpx,"Memory size overflow"); |
| 317 |
} |
| 318 |
w = realloc (p,(nmem*size)); |
| 319 |
if (w==NULL) mpx_abort(mpx,"Out of Memory"); |
| 320 |
return w; |
| 321 |
} |
| 322 |
static void *mpx_xmalloc (MPX mpx, size_t nmem, size_t size) { |
| 323 |
void *w; |
| 324 |
if ((max_size_test/size)<nmem) { |
| 325 |
mpx_abort(mpx,"Memory size overflow"); |
| 326 |
} |
| 327 |
w = malloc (nmem*size); |
| 328 |
if (w==NULL) mpx_abort(mpx,"Out of Memory"); |
| 329 |
return w; |
| 330 |
} |
| 331 |
static char *mpx_xstrdup(MPX mpx, const char *s) { |
| 332 |
char *w; |
| 333 |
if (s==NULL) |
| 334 |
return NULL; |
| 335 |
w = strdup(s); |
| 336 |
if (w==NULL) mpx_abort(mpx,"Out of Memory"); |
| 337 |
return w; |
| 338 |
} |
| 339 |
@* The command 'newer' became a function. |
| 340 |
|
| 341 |
We may have high-res timers in struct stat. If we do, use them. |
| 342 |
|
| 343 |
@c |
| 344 |
static int mpx_newer(char *source, char *target) { |
| 345 |
struct stat source_stat, target_stat; |
| 346 |
#if HAVE_SYS_STAT_H |
| 347 |
if (stat(target, &target_stat) < 0) return 0; /* true */ |
| 348 |
if (stat(source, &source_stat) < 0) return 1; /* false */ |
| 349 |
#if HAVE_STRUCT_STAT_ST_MTIM |
| 350 |
if (source_stat.st_mtim.tv_sec > target_stat.st_mtim.tv_sec || |
| 351 |
(source_stat.st_mtim.tv_sec == target_stat.st_mtim.tv_sec && |
| 352 |
source_stat.st_mtim.tv_nsec >= target_stat.st_mtim.tv_nsec)) |
| 353 |
return 0; |
| 354 |
#else |
| 355 |
if (source_stat.st_mtime >= target_stat.st_mtime) |
| 356 |
return 0; |
| 357 |
#endif |
| 358 |
#endif |
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
@* Extracting data from \MP\ input. |
| 365 |
|
| 366 |
This part of the program transforms a \MP\ input file into a \TeX\ or |
| 367 |
troff input file by stripping out \.{btex}$\ldots$\.{etex} and |
| 368 |
\.{verbatimtex}$\ldots$\.{etex} sections. |
| 369 |
Leading and trailing spaces and tabs are removed from the |
| 370 |
extracted material and it is surrounded by the preceding and following |
| 371 |
strings defined immediately below. The input file should be given as |
| 372 |
argument 1 and the resulting \TeX\ or troff file is written on standard |
| 373 |
output. |
| 374 |
|
| 375 |
John Hobby wrote the original version, which has since been |
| 376 |
extensively altered. The current implementation is a bit trickier |
| 377 |
than I would like, but changing it will take careful study and |
| 378 |
will likely make it run slower, so I've left it as-is for now. |
| 379 |
|
| 380 |
@<Globals@>= |
| 381 |
int texcnt ; /* btex..etex blocks so far */ |
| 382 |
int verbcnt ; /* verbatimtex..etex blocks so far */ |
| 383 |
char *bb, *tt, *aa; /* start of before, token, and after strings */ |
| 384 |
char *buf; /* the input line */ |
| 385 |
unsigned bufsize; |
| 386 |
|
| 387 |
@ @<Set initial values@>= |
| 388 |
mpx->bufsize = 1000; |
| 389 |
|
| 390 |
@ This function returns NULL on EOF, otherwise it returns |buf|. |
| 391 |
|
| 392 |
@c |
| 393 |
static char *mpx_getline(MPX mpx, FILE *mpfile) { |
| 394 |
int c; |
| 395 |
unsigned loc = 0; |
| 396 |
if (feof(mpfile)) |
| 397 |
return NULL; |
| 398 |
if (mpx->buf==NULL) |
| 399 |
mpx->buf = xmalloc(mpx->bufsize,1); |
| 400 |
while ((c = getc(mpfile)) != EOF && c != '\n' && c != '\r') { |
| 401 |
mpx->buf[loc++] = (char)c; |
| 402 |
if (loc == mpx->bufsize) { |
| 403 |
char *temp = mpx->buf; |
| 404 |
unsigned n = mpx->bufsize + (mpx->bufsize>>4); |
| 405 |
if (n>MAXINT) |
| 406 |
mpx_abort(mpx,"Line is too long"); |
| 407 |
mpx->buf = xmalloc(n,1); |
| 408 |
memcpy(mpx->buf,temp,mpx->bufsize); |
| 409 |
free(temp); |
| 410 |
mpx->bufsize = n; |
| 411 |
} |
| 412 |
} |
| 413 |
mpx->buf[loc] = 0; |
| 414 |
if (c == '\r') { |
| 415 |
c = getc(mpfile); |
| 416 |
if (c != '\n') |
| 417 |
ungetc(c, mpfile); |
| 418 |
} |
| 419 |
mpx->lnno++; |
| 420 |
return mpx->buf; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
@ Return nonzero if a prefix of string $s$ matches the null-terminated string $t$ |
| 425 |
and the next character is not a letter or an underscore. |
| 426 |
|
| 427 |
@c |
| 428 |
static int mpx_match_str(const char *s, const char *t) { |
| 429 |
while (*t != 0) { |
| 430 |
if (*s != *t) |
| 431 |
return 0; |
| 432 |
s++; |
| 433 |
t++; |
| 434 |
} |
| 435 |
if ((*s>= 'a' && *s<='z') || (*s>= 'A' && *s<='Z') || *s == '_') |
| 436 |
return 0; |
| 437 |
return 1; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
@ This function tries to express $s$ as the concatenation of three |
| 442 |
strings $b$, $t$, $a$, with the global pointers $bb$, $tt$, and $aa$ set to the |
| 443 |
start of the corresponding strings. String $t$ is either a quote mark, |
| 444 |
a percent sign, or an alphabetic token \.{btex}, \.{etex}, or |
| 445 |
\.{verbatimtex}. (An alphabetic token is a maximal sequence of letters |
| 446 |
and underscores.) If there are several possible substrings $t$, we |
| 447 |
choose the leftmost one. If there is no such $t$, we set $b=s$ and return 0. |
| 448 |
|
| 449 |
Various values are defined, so that |mpx_copy_mpto| can distinguish between |
| 450 |
\.{verbatimtex} ... \.{etex} and \.{btex} ... \.{etex} (the former has no |
| 451 |
whitespace corrections applied). |
| 452 |
|
| 453 |
@d VERBATIM_TEX 1 |
| 454 |
@d B_TEX 2 |
| 455 |
@d FIRST_VERBATIM_TEX 3 |
| 456 |
|
| 457 |
@c |
| 458 |
static int mpx_getbta(MPX mpx, char *s) { |
| 459 |
int ok = 1; /* zero if last character was |a-z|, |A-Z|, or |_| */ |
| 460 |
mpx->bb = s; |
| 461 |
if (s==NULL) { |
| 462 |
mpx->tt = NULL; |
| 463 |
mpx->aa = NULL; |
| 464 |
return 0; |
| 465 |
} |
| 466 |
for (mpx->tt = mpx->bb; *(mpx->tt) != 0; mpx->tt++) { |
| 467 |
switch (*(mpx->tt)) { |
| 468 |
case '"': |
| 469 |
case '%': |
| 470 |
mpx->aa = mpx->tt + 1; |
| 471 |
return 1; |
| 472 |
case 'b': |
| 473 |
if (ok && mpx_match_str(mpx->tt, "btex")) { |
| 474 |
mpx->aa = mpx->tt + 4; |
| 475 |
return 1; |
| 476 |
} else { |
| 477 |
ok = 0; |
| 478 |
} |
| 479 |
break; |
| 480 |
case 'e': |
| 481 |
if (ok && mpx_match_str(mpx->tt, "etex")) { |
| 482 |
mpx->aa = mpx->tt + 4; |
| 483 |
return 1; |
| 484 |
} else { |
| 485 |
ok = 0; |
| 486 |
} |
| 487 |
break; |
| 488 |
case 'v': |
| 489 |
if (ok && mpx_match_str(mpx->tt, "verbatimtex")) { |
| 490 |
mpx->aa = mpx->tt + 11; |
| 491 |
return 1; |
| 492 |
} else { |
| 493 |
ok = 0; |
| 494 |
} |
| 495 |
break; |
| 496 |
default: |
| 497 |
if ((*(mpx->tt) >= 'a' && *(mpx->tt) <= 'z') || |
| 498 |
(*(mpx->tt) >= 'A' && *(mpx->tt) <= 'Z') || |
| 499 |
(*(mpx->tt) == '_')) |
| 500 |
ok = 0; |
| 501 |
else |
| 502 |
ok = 1; |
| 503 |
} |
| 504 |
} |
| 505 |
mpx->aa = mpx->tt; |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
@ @c |
| 510 |
static void mpx_copy_mpto (MPX mpx, FILE *outfile, int textype) { |
| 511 |
char *s; /* where a string to print stops */ |
| 512 |
char *t; /* for finding start of last line */ |
| 513 |
char c; |
| 514 |
char *res = NULL; |
| 515 |
t = NULL; |
| 516 |
do { |
| 517 |
if (mpx->aa == NULL || *mpx->aa == 0) { |
| 518 |
if ((mpx->aa = mpx_getline(mpx,mpx->mpfile)) == NULL) { |
| 519 |
mpx_error(mpx,"btex section does not end"); |
| 520 |
return; |
| 521 |
} |
| 522 |
} |
| 523 |
if (mpx_getbta(mpx, mpx->aa) && *(mpx->tt) == 'e') { |
| 524 |
s = mpx->tt; |
| 525 |
} else { |
| 526 |
if (mpx->tt == NULL) { |
| 527 |
mpx_error(mpx,"btex section does not end"); |
| 528 |
return; |
| 529 |
} else if (*(mpx->tt) == 'b') { |
| 530 |
mpx_error(mpx,"btex in TeX mode"); |
| 531 |
return; |
| 532 |
} else if (*(mpx->tt) == 'v') { |
| 533 |
mpx_error(mpx,"verbatimtex in TeX mode"); |
| 534 |
return; |
| 535 |
} |
| 536 |
s = mpx->aa; |
| 537 |
} |
| 538 |
c = *s; |
| 539 |
*s = 0; |
| 540 |
if (res==NULL) { |
| 541 |
res = xmalloc(strlen(mpx->bb)+2,1); |
| 542 |
res = strncpy(res,mpx->bb,(strlen(mpx->bb)+1)); |
| 543 |
} else { |
| 544 |
res = xrealloc(res,strlen(res)+strlen(mpx->bb)+2,1); |
| 545 |
res = strncat(res,mpx->bb, strlen(mpx->bb)); |
| 546 |
} |
| 547 |
if (c == '\0') |
| 548 |
res = strcat(res, "\n"); |
| 549 |
*s = c; |
| 550 |
} while (*(mpx->tt) != 'e'); |
| 551 |
s = res; |
| 552 |
if (textype == B_TEX) { |
| 553 |
/* whitespace at the end */ |
| 554 |
for (s = res + strlen(res) - 1; |
| 555 |
s >= res && (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'); s--); |
| 556 |
t = s; |
| 557 |
*(++s) = '\0'; |
| 558 |
} else { |
| 559 |
t =s; |
| 560 |
} |
| 561 |
if (textype == B_TEX || textype == FIRST_VERBATIM_TEX) { |
| 562 |
/* whitespace at the start */ |
| 563 |
for (s = res; |
| 564 |
s < (res + strlen(res)) && (*s == ' ' || *s == '\t' || *s == '\r' |
| 565 |
|| *s == '\n'); s++); |
| 566 |
for (; *t != '\n' && t > s; t--); |
| 567 |
} |
| 568 |
fprintf(outfile,"%s", s); |
| 569 |
if (textype == B_TEX) { |
| 570 |
/* put no |%| at end if it's only 1 line total, starting with |%|; |
| 571 |
* this covers the special case |%&format| in a single line. */ |
| 572 |
if ((t != s || *t != '%') && mpx->mode == mpx_tex_mode) |
| 573 |
fprintf(outfile,"%%"); |
| 574 |
} |
| 575 |
free(res); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
@ Static strings for mpto |
| 580 |
|
| 581 |
@c |
| 582 |
static const char *mpx_predoc[] = {"", ".po 0\n"}; |
| 583 |
static const char *mpx_postdoc[] = { "\\end{document}\n", ""}; |
| 584 |
static const char *mpx_pretex1[] = { |
| 585 |
"\\gdef\\mpxshipout{\\shipout\\hbox\\bgroup\n" @| |
| 586 |
" \\setbox0=\\hbox\\bgroup}%%\n" @| |
| 587 |
"\\gdef\\stopmpxshipout{\\egroup" @| |
| 588 |
" \\dimen0=\\ht0 \\advance\\dimen0\\dp0\n" @| |
| 589 |
" \\dimen1=\\ht0 \\dimen2=\\dp0\n" @| |
| 590 |
" \\setbox0=\\hbox\\bgroup\n" @| |
| 591 |
" \\box0\n" @| |
| 592 |
" \\ifnum\\dimen0>0 \\vrule width1sp height\\dimen1 depth\\dimen2 \n" @| |
| 593 |
" \\else \\vrule width1sp height1sp depth0sp\\relax\n" @| |
| 594 |
" \\fi\\egroup\n" @| |
| 595 |
" \\ht0=0pt \\dp0=0pt \\box0 \\egroup}%%\n" @| |
| 596 |
"\\mpxshipout%% line %d %s\n", ".lf %d %s\n" }; |
| 597 |
static const char *mpx_pretex[] = { "\\mpxshipout%% line %d %s\n", ".bp\n.lf %d %s\n" }; |
| 598 |
static const char *mpx_posttex[] = { "\n\\stopmpxshipout\n", "\n" }; |
| 599 |
static const char *mpx_preverb1[] = {"", ".lf %d %s\n" }; /* if very first instance */ |
| 600 |
static const char *mpx_preverb[] = { "%% line %d %s\n", ".lf %d %s\n"}; /* all other instances */ |
| 601 |
static const char *mpx_postverb[] = { "\n", "\n" } ; |
| 602 |
|
| 603 |
@ @c |
| 604 |
static void mpx_mpto(MPX mpx, char *tmpname, char *mptexpre) { |
| 605 |
FILE *outfile; |
| 606 |
int verbatim_written = 0; |
| 607 |
int mode = mpx->mode; |
| 608 |
char *mpname = mpx->mpname; |
| 609 |
if (mode==mpx_tex_mode) { |
| 610 |
TMPNAME_EXT(mpx->tex,".tex"); |
| 611 |
} else { |
| 612 |
TMPNAME_EXT(mpx->tex,".i"); |
| 613 |
} |
| 614 |
outfile = mpx_xfopen(mpx,mpx->tex, "wb"); |
| 615 |
if (mode==mpx_tex_mode) { |
| 616 |
FILE *fr; |
| 617 |
if ((fr = fopen(mptexpre, "r"))!= NULL) { |
| 618 |
size_t i; |
| 619 |
char buf[512]; |
| 620 |
while ((i=fread((void *)buf, 1, 512 , fr))>0) { |
| 621 |
fwrite((void *)buf,1, i, outfile); |
| 622 |
} |
| 623 |
mpx_fclose(mpx,fr); |
| 624 |
} |
| 625 |
} |
| 626 |
mpx->mpfile = mpx_xfopen(mpx,mpname, "r"); |
| 627 |
fprintf(outfile,"%s", mpx_predoc[mode]); |
| 628 |
while (mpx_getline(mpx, mpx->mpfile) != NULL) |
| 629 |
@<Do a line@>; |
| 630 |
fprintf(outfile,"%s", mpx_postdoc[mode]); |
| 631 |
mpx_fclose(mpx,mpx->mpfile); |
| 632 |
mpx_fclose(mpx,outfile); |
| 633 |
mpx->lnno = 0; |
| 634 |
} |
| 635 |
|
| 636 |
@ |
| 637 |
@<Do a line@>= |
| 638 |
{ |
| 639 |
mpx->aa = mpx->buf; |
| 640 |
while (mpx_getbta(mpx, mpx->aa)) { |
| 641 |
if (*(mpx->tt) == '%') { |
| 642 |
break; |
| 643 |
} else if (*(mpx->tt) == '"') { |
| 644 |
do { |
| 645 |
if (!mpx_getbta(mpx, mpx->aa)) |
| 646 |
mpx_error(mpx,"string does not end"); |
| 647 |
} while (*(mpx->tt) != '"'); |
| 648 |
} else if (*(mpx->tt) == 'b') { |
| 649 |
if (mpx->texcnt++ == 0) |
| 650 |
fprintf(outfile,mpx_pretex1[mode], mpx->lnno, mpname); |
| 651 |
else |
| 652 |
fprintf(outfile,mpx_pretex[mode], mpx->lnno, mpname); |
| 653 |
mpx_copy_mpto(mpx, outfile, B_TEX); |
| 654 |
fprintf(outfile,"%s", mpx_posttex[mode]); |
| 655 |
} else if (*(mpx->tt) == 'v') { |
| 656 |
if (mpx->verbcnt++ == 0 && mpx->texcnt == 0) |
| 657 |
fprintf(outfile,mpx_preverb1[mode], mpx->lnno, mpname); |
| 658 |
else |
| 659 |
fprintf(outfile,mpx_preverb[mode], mpx->lnno, mpname); |
| 660 |
if (!verbatim_written) |
| 661 |
mpx_copy_mpto(mpx, outfile, FIRST_VERBATIM_TEX); |
| 662 |
else |
| 663 |
mpx_copy_mpto(mpx, outfile, VERBATIM_TEX); |
| 664 |
verbatim_written = 1; |
| 665 |
fprintf(outfile,"%s", mpx_postverb[mode]); |
| 666 |
} else { |
| 667 |
mpx_error(mpx,"unmatched etex"); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
@ @<Run |mpto| on the mp file@>= |
| 673 |
mpx_mpto(mpx, tmpname, mpxopt->mptexpre) |
| 674 |
|
| 675 |
@* DVItoMP Processing. |
| 676 |
|
| 677 |
The \.{DVItoMP} program reads binary device-independent (``\.{DVI}'') |
| 678 |
files that are produced by document compilers such as \TeX, and converts them |
| 679 |
into a symbolic form understood by \MP. It is loosely based on the \.{DVItype} |
| 680 |
utility program that produces a more faithful symbolic form of a \.{DVI} file. |
| 681 |
|
| 682 |
The output file is a sequence of \MP\ picture expressions, one for every page |
| 683 |
in the \.{DVI} file. It makes no difference to \.{DVItoMP} where the \.{DVI} |
| 684 |
file comes from, but it is intended to process the result of running \TeX\ |
| 685 |
or \LaTeX\ on the output of the extraction process that is defined above. |
| 686 |
Such a \.{DVI} file will contain one page for every \.{btex}$\ldots$\.{etex} |
| 687 |
block in the original input. Processing with \.{DVItoMP} creates a |
| 688 |
corresponding sequence of \MP\ picture expressions for use as an auxiliary |
| 689 |
input file. Since \MP\ expects such files to have the extension \.{.MPX}, |
| 690 |
the output of \.{DVItoMP} is sometimes called an ``\.{MPX}'' file. |
| 691 |
|
| 692 |
@ The following parameters can be changed at compile time to extend or |
| 693 |
reduce \.{DVItoMP}'s capacity. |
| 694 |
|
| 695 |
TODO: dynamic reallocation |
| 696 |
|
| 697 |
@d virtual_space 1000000 /* maximum total bytes of typesetting commands for virtual fonts */ |
| 698 |
@d max_fonts 1000 /* maximum number of distinct fonts per \.{DVI} file */ |
| 699 |
@d max_fnums 3000 /* maximum number of fonts plus fonts local to virtual fonts */ |
| 700 |
@d max_widths (256*max_fonts) /* maximum number of different characters among all fonts */ |
| 701 |
@d line_length 79 /* maximum output line length (must be at least 60) */ |
| 702 |
@d stack_size 100 /* \.{DVI} files shouldn't |push| beyond this depth */ |
| 703 |
@d font_tolerance 0.00001 |
| 704 |
/* font sizes should match to within this multiple of $2^{20}$ \.{DVI} units */ |
| 705 |
|
| 706 |
@ If the \.{DVI} file is badly malformed, the whole process must be aborted; |
| 707 |
\.{DVItoMP} will give up, after issuing an error message about the symptoms |
| 708 |
that were noticed. |
| 709 |
|
| 710 |
@d bad_dvi(A) mpx_abort(mpx,"Bad DVI file: " A "!") |
| 711 |
@d bad_dvi_two(A,B) mpx_abort(mpx,"Bad DVI file: " A "!", B) |
| 712 |
@.Bad DVI file@> |
| 713 |
|
| 714 |
@* The character set. |
| 715 |
|
| 716 |
Like all programs written with the \.{WEB} system, \.{DVItoMP} can be |
| 717 |
used with any character set. It an identify transfrom internally, because |
| 718 |
the programming for portable input-output is easier when a fixed internal |
| 719 |
code is used, and because \.{DVI} files use ASCII code for file names. |
| 720 |
|
| 721 |
In the conversion from Pascal to C, the |xchr| array has been removed. |
| 722 |
Because some systems may still want to change the input--output character |
| 723 |
set, the accesses to |xchr| and |printable| are replaced by macro calls. |
| 724 |
|
| 725 |
@d printable(c) (isprint(c) && c < 128 && c!='"') |
| 726 |
@d xchr(A) (A) |
| 727 |
|
| 728 |
@ @c |
| 729 |
static void mpx_open_mpxfile (MPX mpx) { /* prepares to write text on |mpxfile| */ |
| 730 |
mpx->mpxfile = mpx_xfopen (mpx,mpx->mpxname, "wb"); |
| 731 |
} |
| 732 |
|
| 733 |
@* Device-independent file format. |
| 734 |
The format of \.{DVI} files is described in many places including |
| 735 |
\.{dvitype.web} and Volume~B of D.~E. Knuth's {\sl Computers and Typesetting}. |
| 736 |
This program refers to the following command codes. |
| 737 |
|
| 738 |
@d id_byte 2 /* identifies the kind of \.{DVI} files described here */ |
| 739 |
@# |
| 740 |
@d set_char_0 0 /* typeset character 0 and move right */ |
| 741 |
@d set1 128 /* typeset a character and move right */ |
| 742 |
@d set_rule 132 /* typeset a rule and move right */ |
| 743 |
@d put1 133 /* typeset a character */ |
| 744 |
@d put_rule 137 /* typeset a rule */ |
| 745 |
@d nop 138 /* no operation */ |
| 746 |
@d bop 139 /* beginning of page */ |
| 747 |
@d eop 140 /* ending of page */ |
| 748 |
@d push 141 /* save the current positions */ |
| 749 |
@d pop 142 /* restore previous positions */ |
| 750 |
@d right1 143 /* move right */ |
| 751 |
@d w0 147 /* move right by |w| */ |
| 752 |
@d w1 148 /* move right and set |w| */ |
| 753 |
@d x0 152 /* move right by |x| */ |
| 754 |
@d x1 153 /* move right and set |x| */ |
| 755 |
@d down1 157 /* move down */ |
| 756 |
@d y0 161 /* move down by |y| */ |
| 757 |
@d y1 162 /* move down and set |y| */ |
| 758 |
@d z0 166 /* move down by |z| */ |
| 759 |
@d z1 167 /* move down and set |z| */ |
| 760 |
@d fnt_num_0 171 /* set current font to 0 */ |
| 761 |
@d fnt1 235 /* set current font */ |
| 762 |
@d xxx1 239 /* extension to \.{DVI} primitives */ |
| 763 |
@d xxx4 242 /* potentially long extension to \.{DVI} primitives */ |
| 764 |
@d fnt_def1 243 /* define the meaning of a font number */ |
| 765 |
@d pre 247 /* preamble */ |
| 766 |
@d post 248 /* postamble beginning */ |
| 767 |
@d post_post 249 /* postamble ending */ |
| 768 |
@d undefined_commands 250: case 251: case 252: case 253: case 254: case 255 |
| 769 |
|
| 770 |
@* Input from binary files. |
| 771 |
|
| 772 |
@ The program deals with two binary file variables: |dvi_file| is the main |
| 773 |
input file that we are translating into symbolic form, and |tfm_file| is |
| 774 |
the current font metric file from which character-width information is |
| 775 |
being read. It is convenient to have a throw-away variable for function |
| 776 |
results when reading parts of the files that are being skipped. |
| 777 |
|
| 778 |
@<Glob...@>= |
| 779 |
FILE * dvi_file; /* the input file */ |
| 780 |
FILE * tfm_file; /* a font metric file */ |
| 781 |
FILE * vf_file; /* a virtual font file */ |
| 782 |
|
| 783 |
@ Prepares to read packed bytes in |dvi_file| |
| 784 |
@c |
| 785 |
static void mpx_open_dvi_file (MPX mpx) { |
| 786 |
mpx->dvi_file = fopen(mpx->dviname,"rb"); |
| 787 |
if (mpx->dvi_file==NULL) |
| 788 |
mpx_abort(mpx,"DVI generation failed"); |
| 789 |
} |
| 790 |
|
| 791 |
@ Prepares to read packed bytes in |tfm_file| |
| 792 |
@c |
| 793 |
static web_boolean mpx_open_tfm_file (MPX mpx) { |
| 794 |
mpx->tfm_file = mpx_fsearch(mpx, mpx->cur_name, mpx_tfm_format); |
| 795 |
if (mpx->tfm_file == NULL) |
| 796 |
mpx_abort(mpx,"Cannot find TFM %s", mpx->cur_name); |
| 797 |
free (mpx->cur_name); /* We |xmalloc|'d this before we got called. */ |
| 798 |
return true; /* If we get here, we succeeded. */ |
| 799 |
} |
| 800 |
|
| 801 |
@ Prepares to read packed bytes in |vf_file|. |
| 802 |
It's ok if the \.{VF} file doesn't exist. |
| 803 |
|
| 804 |
@c |
| 805 |
static web_boolean mpx_open_vf_file (MPX mpx) { |
| 806 |
if (mpx->vf_file) |
| 807 |
fclose(mpx->vf_file); /* Ugly... */ |
| 808 |
mpx->vf_file = mpx_fsearch(mpx, mpx->cur_name, mpx_vf_format); |
| 809 |
if (mpx->vf_file) { |
| 810 |
free (mpx->cur_name); |
| 811 |
return true; |
| 812 |
} |
| 813 |
return false; |
| 814 |
} |
| 815 |
|
| 816 |
@ If you looked carefully at the preceding code, you probably asked, |
| 817 |
``What is |cur_name|?'' Good question. It's a global |
| 818 |
variable: |cur_name| is a string variable that will be set to the |
| 819 |
current font metric file name before |open_tfm_file| or |open_vf_file| |
| 820 |
is called. |
| 821 |
|
| 822 |
@<Glob...@>= |
| 823 |
char *cur_name; /* external name */ |
| 824 |
|
| 825 |
@ It turns out to be convenient to read four bytes at a time, when we are |
| 826 |
inputting from \.{TFM} files. The input goes into global variables |
| 827 |
|b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3| |
| 828 |
the fourth. |
| 829 |
|
| 830 |
@<Glob...@>= |
| 831 |
int b0, b1, b2, b3; /* four bytes input at once */ |
| 832 |
|
| 833 |
@ The |read_tfm_word| procedure sets |b0| through |b3| to the next |
| 834 |
four bytes in the current \.{TFM} file. |
| 835 |
|
| 836 |
@c |
| 837 |
static void mpx_read_tfm_word (MPX mpx) { |
| 838 |
mpx->b0 = getc(mpx->tfm_file); |
| 839 |
mpx->b1 = getc(mpx->tfm_file); |
| 840 |
mpx->b2 = getc(mpx->tfm_file); |
| 841 |
mpx->b3 = getc(mpx->tfm_file); |
| 842 |
} |
| 843 |
|
| 844 |
@ Input can come from from three different sources depending on the settings |
| 845 |
of global variables. When |vf_reading| is true, we read from the \.{VF} file. |
| 846 |
Otherwise, input can either come directly from |dvi_file| or from a buffer |
| 847 |
|cmd_buf|. The latter case applies whenever |buf_ptr<virtual_space|. |
| 848 |
|
| 849 |
@<Glob...@>= |
| 850 |
web_boolean vf_reading; /* should input come from |vf_file|? */ |
| 851 |
unsigned char cmd_buf[(virtual_space+1)]; /* commands for virtual characters */ |
| 852 |
unsigned int buf_ptr; /* |cmd_buf| index for the next byte */ |
| 853 |
|
| 854 |
@ @<Set init...@>= |
| 855 |
mpx->vf_reading=false; |
| 856 |
mpx->buf_ptr=virtual_space; |
| 857 |
|
| 858 |
@ We shall use a set of simple functions to read the next byte or bytes from the |
| 859 |
current input source. There are seven possibilities, each of which is treated |
| 860 |
as a separate function in order to minimize the overhead for subroutine calls. |
| 861 |
|
| 862 |
@c |
| 863 |
static web_integer mpx_get_byte (MPX mpx) { /* returns the next byte, unsigned */ |
| 864 |
unsigned char b; |
| 865 |
@<Read one byte into |b|@>; |
| 866 |
return b; |
| 867 |
} |
| 868 |
|
| 869 |
static web_integer mpx_signed_byte (MPX mpx) { /* returns the next byte, signed */ |
| 870 |
unsigned char b; |
| 871 |
@<Read one byte into |b|@>; |
| 872 |
return ( b<128 ? b : (b-256)); |
| 873 |
} |
| 874 |
|
| 875 |
static web_integer mpx_get_two_bytes (MPX mpx) { /* returns the next two bytes, unsigned */ |
| 876 |
unsigned char a,b; |
| 877 |
a=0; b=0; /* for compiler warnings */ |
| 878 |
@<Read two bytes into |a| and |b|@>; |
| 879 |
return (a*(int)(256)+b); |
| 880 |
} |
| 881 |
|
| 882 |
static web_integer mpx_signed_pair (MPX mpx) { /* returns the next two bytes, signed */ |
| 883 |
unsigned char a,b; |
| 884 |
a=0; b=0; /* for compiler warnings */ |
| 885 |
@<Read two bytes into |a| and |b|@>; |
| 886 |
if ( a<128 ) return (a*256+b); |
| 887 |
else return ((a-256)*256+b); |
| 888 |
} |
| 889 |
|
| 890 |
static web_integer mpx_get_three_bytes (MPX mpx) { /* returns the next three bytes, unsigned */ |
| 891 |
unsigned char a,b,c; |
| 892 |
a=0; b=0; c=0; /* for compiler warnings */ |
| 893 |
@<Read three bytes into |a|, |b|, and~|c|@>; |
| 894 |
return ((a*(int)(256)+b)*256+c); |
| 895 |
} |
| 896 |
|
| 897 |
static web_integer mpx_signed_trio (MPX mpx) { /* returns the next three bytes, signed */ |
| 898 |
unsigned char a,b,c; |
| 899 |
a=0; b=0; c=0; /* for compiler warnings */ |
| 900 |
@<Read three bytes into |a|, |b|, and~|c|@>; |
| 901 |
if ( a<128 ) return ((a*(int)(256)+b)*256+c); |
| 902 |
else return (((a-(int)(256))*256+b)*256+c); |
| 903 |
} |
| 904 |
|
| 905 |
static web_integer mpx_signed_quad (MPX mpx) { /* returns the next four bytes, signed */ |
| 906 |
unsigned char a,b,c,d; |
| 907 |
a=0; b=0; c=0; d=0; /* for compiler warnings */ |
| 908 |
@<Read four bytes into |a|, |b|, |c|, and~|d|@>; |
| 909 |
if ( a<128 ) return (((a*(int)(256)+b)*256+c)*256+d); |
| 910 |
else return ((((a-256)*(int)(256)+b)*256+c)*256+d); |
| 911 |
} |
| 912 |
|
| 913 |
@ @<Read one byte into |b|@>= |
| 914 |
if ( mpx->vf_reading ) { |
| 915 |
b = (unsigned char)getc(mpx->vf_file); |
| 916 |
} else if ( mpx->buf_ptr==virtual_space ) { |
| 917 |
b = (unsigned char)getc(mpx->dvi_file); |
| 918 |
} else { |
| 919 |
b=mpx->cmd_buf[mpx->buf_ptr]; |
| 920 |
incr(mpx->buf_ptr); |
| 921 |
} |
| 922 |
|
| 923 |
@ @<Read two bytes into |a| and |b|@>= |
| 924 |
if ( mpx->vf_reading ) { |
| 925 |
a = (unsigned char)getc(mpx->vf_file); |
| 926 |
b = (unsigned char)getc(mpx->vf_file); |
| 927 |
} else if ( mpx->buf_ptr==virtual_space ) { |
| 928 |
a = (unsigned char)getc(mpx->dvi_file); |
| 929 |
b = (unsigned char)getc(mpx->dvi_file); |
| 930 |
} else if ( mpx->buf_ptr+2>mpx->n_cmds ) { |
| 931 |
mpx_abort(mpx,"Error detected while interpreting a virtual font"); |
| 932 |
@.Error detected while...@> |
| 933 |
} else { |
| 934 |
a=mpx->cmd_buf[mpx->buf_ptr]; |
| 935 |
b=mpx->cmd_buf[mpx->buf_ptr+1]; |
| 936 |
mpx->buf_ptr+=2; |
| 937 |
} |
| 938 |
|
| 939 |
@ @<Read three bytes into |a|, |b|, and~|c|@>= |
| 940 |
if ( mpx->vf_reading ) { |
| 941 |
a = (unsigned char)getc(mpx->vf_file); |
| 942 |
b = (unsigned char)getc(mpx->vf_file); |
| 943 |
c = (unsigned char)getc(mpx->vf_file); |
| 944 |
} else if ( mpx->buf_ptr==virtual_space ) { |
| 945 |
a = (unsigned char)getc(mpx->dvi_file); |
| 946 |
b = (unsigned char)getc(mpx->dvi_file); |
| 947 |
c = (unsigned char)getc(mpx->dvi_file); |
| 948 |
} else if ( mpx->buf_ptr+3>mpx->n_cmds ) { |
| 949 |
mpx_abort(mpx,"Error detected while interpreting a virtual font"); |
| 950 |
@.Error detected while...@> |
| 951 |
} else { |
| 952 |
a=mpx->cmd_buf[mpx->buf_ptr]; |
| 953 |
b=mpx->cmd_buf[mpx->buf_ptr+1]; |
| 954 |
c=mpx->cmd_buf[mpx->buf_ptr+2]; |
| 955 |
mpx->buf_ptr+=3; |
| 956 |
} |
| 957 |
|
| 958 |
@ @<Read four bytes into |a|, |b|, |c|, and~|d|@>= |
| 959 |
if ( mpx->vf_reading ) { |
| 960 |
a = (unsigned char)getc(mpx->vf_file); |
| 961 |
b = (unsigned char)getc(mpx->vf_file); |
| 962 |
c = (unsigned char)getc(mpx->vf_file); |
| 963 |
d = (unsigned char)getc(mpx->vf_file); |
| 964 |
} else if ( mpx->buf_ptr==virtual_space ) { |
| 965 |
a = (unsigned char)getc(mpx->dvi_file); |
| 966 |
b = (unsigned char)getc(mpx->dvi_file); |
| 967 |
c = (unsigned char)getc(mpx->dvi_file); |
| 968 |
d = (unsigned char)getc(mpx->dvi_file); |
| 969 |
} else if ( mpx->buf_ptr+4>mpx->n_cmds ) { |
| 970 |
mpx_abort(mpx,"Error detected while interpreting a virtual font"); |
| 971 |
@.Error detected while...@> |
| 972 |
} else { |
| 973 |
a=mpx->cmd_buf[mpx->buf_ptr]; |
| 974 |
b=mpx->cmd_buf[mpx->buf_ptr+1]; |
| 975 |
c=mpx->cmd_buf[mpx->buf_ptr+2]; |
| 976 |
d=mpx->cmd_buf[mpx->buf_ptr+3]; |
| 977 |
mpx->buf_ptr+=4; |
| 978 |
} |
| 979 |
|
| 980 |
@* Data structures for fonts. |
| 981 |
|
| 982 |
\.{DVI} file format does not include information about character widths, since |
| 983 |
that would tend to make the files a lot longer. But a program that reads |
| 984 |
a \.{DVI} file is supposed to know the widths of the characters that appear |
| 985 |
in \\{set\_char} commands. Therefore \.{DVItoMP} looks at the font metric |
| 986 |
(\.{TFM}) files for the fonts that are involved. |
| 987 |
@.TFM {\rm files}@> |
| 988 |
|
| 989 |
@ For purposes of this program, the only thing we need to know about a |
| 990 |
given character |c| in a non-virtual font |f| is the width. For the font as |
| 991 |
a whole, all we need is the symbolic name to use in the \.{MPX} file. |
| 992 |
|
| 993 |
This information appears implicitly in the following data |
| 994 |
structures. The current number of fonts defined is |nf|. Each such font has |
| 995 |
an internal number |f|, where |0<=f<nf|. There is also an external number |
| 996 |
that identifies the font in the \.{DVI} file. The correspondence is |
| 997 |
maintained in arrays |font_num| and |internal_num| so that |font_num[i]| |
| 998 |
is the external number for |f=internal_num[i]|. |
| 999 |
The external name of this font is the string that occupies |font_name[f]|. |
| 1000 |
The legal characters run from |font_bc[f]| to |font_ec[f]|, inclusive. |
| 1001 |
The \.{TFM} file can specify that some of these are invalid, but this doesn't |
| 1002 |
concern \.{DVItoMP} because it does not do extensive error checking. |
| 1003 |
The width of character~|c| in font~|f| is given by |
| 1004 |
|char_width(f,c)=width[info_base[f]+c]|, and |info_ptr| is the |
| 1005 |
first unused position of the |width| array. |
| 1006 |
|
| 1007 |
If font~|f| is a virtual font, there is a list of \.{DVI} commands for each |
| 1008 |
character. These occupy consecutive positions in the |cmd_buf| array with |
| 1009 |
the commands for character~|c| starting at |
| 1010 |
|start_cmd(f,c)=cmd_ptr[info_base[f]+c]| and ending just before |
| 1011 |
|start_cmd(f,c+1)|. Font numbers used when interpreting these \.{DVI} |
| 1012 |
commands occupy positions |fbase[f]| through |ftop[f]-1| in the |font_num| |
| 1013 |
table and the |internal_num| array gives the corresponding internal font |
| 1014 |
numbers. If such an internal font number~|i| does not correspond to |
| 1015 |
some font occurring in the \.{DVI} file, then |font_num[i]| has not been |
| 1016 |
assigned a meaningful value; this is indicated by |local_only[i]=true|. |
| 1017 |
|
| 1018 |
If font~|f| is not virtual, then |fbase[f]=0| and |ftop[f]=0|. The |start_cmd| |
| 1019 |
values are ignored in this case. |
| 1020 |
|
| 1021 |
@d char_width(A,B) mpx->width[mpx->info_base[(A)]+(B)] |
| 1022 |
@d start_cmd(A,B) mpx->cmd_ptr[mpx->info_base[(A)]+(B)] |
| 1023 |
|
| 1024 |
@<Glob...@>= |
| 1025 |
web_integer font_num[(max_fnums+1)]; /* external font numbers */ |
| 1026 |
web_integer internal_num[(max_fnums+1)]; /* internal font numbers */ |
| 1027 |
web_boolean local_only[(max_fnums+1)]; /* |font_num| meaningless? */ |
| 1028 |
char *font_name[(max_fonts+1)]; /* starting positions of external font names */ |
| 1029 |
double font_scaled_size[(max_fonts+1)]; /* scale factors over $2^{20}$ */ |
| 1030 |
double font_design_size[(max_fonts+1)]; /* design sizes over $2^{20}$ */ |
| 1031 |
web_integer font_check_sum[(max_fonts+1)]; /* check sum from the |font_def| */ |
| 1032 |
web_integer font_bc[(max_fonts+1)]; /* beginning characters in fonts */ |
| 1033 |
web_integer font_ec[(max_fonts+1)]; /* ending characters in fonts */ |
| 1034 |
web_integer info_base[(max_fonts+1)]; /* index into |width| and |cmd_ptr| tables */ |
| 1035 |
web_integer width[(max_widths+1)]; |
| 1036 |
/* character widths, in units $2^{-20}$ of design size */ |
| 1037 |
web_integer fbase[(max_fonts+1)]; /* index into |font_num| for local fonts */ |
| 1038 |
web_integer ftop[(max_fonts+1)]; /* |font_num| index where local fonts stop */ |
| 1039 |
web_integer cmd_ptr[(max_widths+1)]; /* starting positions in |cmd_buf| */ |
| 1040 |
unsigned int nfonts; /* the number of known fonts */ |
| 1041 |
unsigned int vf_ptr; /* next |font_num| entry for virtual font font tables */ |
| 1042 |
unsigned int info_ptr; /* allocation pointer for |width| and |cmd_ptr| tables */ |
| 1043 |
unsigned int n_cmds; /* number of occupied cells in |cmd_buf| */ |
| 1044 |
unsigned int cur_fbase, cur_ftop; |
| 1045 |
/* currently applicable part of the |font_num| table */ |
| 1046 |
|
| 1047 |
@ @<Set init...@>= |
| 1048 |
mpx->nfonts=0; mpx->info_ptr=0; mpx->font_name[0]=0; |
| 1049 |
mpx->vf_ptr=max_fnums; |
| 1050 |
mpx->cur_fbase=0; mpx->cur_ftop=0; |
| 1051 |
|
| 1052 |
@ Printing the name of a given font is easy except that a procedure |print_char| |
| 1053 |
is needed to actually send an |ASCII_code| to the \.{MPX} file. |
| 1054 |
|
| 1055 |
@c @<Declare subroutines for printing strings@>@; |
| 1056 |
static void mpx_print_font (MPX mpx, web_integer f) { /* |f| is an internal font number */ |
| 1057 |
if ( (f<0)||(f>=(int)mpx->nfonts) ) { |
| 1058 |
bad_dvi("Undefined font"); |
| 1059 |
} else { |
| 1060 |
char *s = mpx->font_name[f]; |
| 1061 |
while (*s) { |
| 1062 |
mpx_print_char(mpx,(unsigned char)*s); |
| 1063 |
s++; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
@ Sometimes a font name is needed as part of an error message. |
| 1069 |
|
| 1070 |
@d font_warn(A,B) mpx_warn (mpx,"%s %s",A,mpx->font_name[(B)]) |
| 1071 |
@d font_error(A,B) mpx_error(mpx,"%s %s",A,mpx->font_name[(B)]) |
| 1072 |
@d font_abort(A,B) mpx_abort(mpx,"%s %s",A,mpx->font_name[(B)]) |
| 1073 |
|
| 1074 |
|
| 1075 |
@ When we encounter a font definition, we save the name, checksum, and size |
| 1076 |
information, but we don't actually read the \.{TFM} or \.{VF} file until we |
| 1077 |
are about to use the font. If a matching font is not already defined, we then |
| 1078 |
allocate a new internal font number. |
| 1079 |
|
| 1080 |
The following subroutine does the necessary things when a \\{fnt\_def} command |
| 1081 |
is encountered in the \.{DVI} file or in a \.{VF} file. It assumes that the |
| 1082 |
first argument has already been parsed and is given by the parameter~|e|. |
| 1083 |
|
| 1084 |
@c @<Declare a function called |match_font|@>@; |
| 1085 |
static void mpx_define_font (MPX mpx, web_integer e) { /* |e| is an external font number */ |
| 1086 |
unsigned i; /* index into |font_num| and |internal_num| */ |
| 1087 |
web_integer n; /* length of the font name and area */ |
| 1088 |
web_integer k; /* general purpose loop counter */ |
| 1089 |
web_integer x; /* a temporary value for scaled size computation */ |
| 1090 |
if ( mpx->nfonts==max_fonts ) |
| 1091 |
mpx_abort(mpx,"DVItoMP capacity exceeded (max fonts=%d)!", max_fonts); |
| 1092 |
@.DVItoMP capacity exceeded...@> |
| 1093 |
@<Allocate an index |i| into the |font_num| and |internal_num| tables@>; |
| 1094 |
@<Read the font parameters into position for font |nf|@>; |
| 1095 |
mpx->internal_num[i]=mpx_match_font(mpx, mpx->nfonts,true); |
| 1096 |
if ( mpx->internal_num[i]==(int)mpx->nfonts ) { |
| 1097 |
mpx->info_base[mpx->nfonts]=max_widths; /* indicate that the info isn't loaded yet */ |
| 1098 |
mpx->local_only[mpx->nfonts]=mpx->vf_reading; incr(mpx->nfonts); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
@ @<Allocate an index |i| into the |font_num| and |internal_num| tables@>= |
| 1103 |
if ( mpx->vf_ptr==mpx->nfonts ) |
| 1104 |
mpx_abort(mpx,"DVItoMP capacity exceeded (max font numbers=%d)", max_fnums); |
| 1105 |
@.DVItoMP capacity exceeded...@> |
| 1106 |
if ( mpx->vf_reading ) { |
| 1107 |
mpx->font_num[mpx->nfonts]=0; i=mpx->vf_ptr; decr(mpx->vf_ptr); |
| 1108 |
} else { |
| 1109 |
i=mpx->nfonts; |
| 1110 |
} |
| 1111 |
mpx->font_num[i]=e |
| 1112 |
|
| 1113 |
@ @<Read the font parameters into position for font |nf|@>= |
| 1114 |
mpx->font_check_sum[mpx->nfonts]=mpx_signed_quad(mpx); |
| 1115 |
@<Read |font_scaled_size[nf]| and |font_design_size[nf]|@> |
| 1116 |
n=mpx_get_byte(mpx); /* that is the area */ |
| 1117 |
n=n+mpx_get_byte(mpx); |
| 1118 |
mpx->font_name[mpx->nfonts]=xmalloc((size_t)(n+1),1); |
| 1119 |
for (k=0;k<n;k++) |
| 1120 |
mpx->font_name[mpx->nfonts][k]=(char)mpx_get_byte(mpx); |
| 1121 |
mpx->font_name[mpx->nfonts][k]=0 |
| 1122 |
|
| 1123 |
@ The scaled size and design size are stored in \.{DVI} units divided by $2^{20}$. |
| 1124 |
The units for scaled size are a little different if we are reading a virtual |
| 1125 |
font, but this will be corrected when the scaled size is used. The scaled size |
| 1126 |
also needs to be truncated to at most 23 significant bits in order to make |
| 1127 |
the character width calculation match what \TeX\ does. |
| 1128 |
|
| 1129 |
@<Read |font_scaled_size[nf]| and |font_design_size[nf]|@>= |
| 1130 |
x=mpx_signed_quad(mpx); |
| 1131 |
k=1; |
| 1132 |
while ( mpx->x>040000000 ) { |
| 1133 |
x= x / 2; k=k+k; |
| 1134 |
} |
| 1135 |
mpx->font_scaled_size[mpx->nfonts]=x*k/1048576.0; |
| 1136 |
if ( mpx->vf_reading ) |
| 1137 |
mpx->font_design_size[mpx->nfonts]=mpx_signed_quad(mpx)*mpx->dvi_per_fix/1048576.0; |
| 1138 |
else mpx->font_design_size[mpx->nfonts]=mpx_signed_quad(mpx)/1048576.0; |
| 1139 |
|
| 1140 |
@ @<Glob...@>= |
| 1141 |
double dvi_per_fix; /* converts points scaled $2^{20}$ to \.{DVI} units */ |
| 1142 |
|
| 1143 |
@ The |match_font| function tries to find a match for the font with internal |
| 1144 |
number~|ff|, returning |nf| or the number of the matching font. If |
| 1145 |
|exact=true|, the name and scaled size should match. Otherwise the scaled |
| 1146 |
size need not match but the font found must be already loaded, not just |
| 1147 |
defined. |
| 1148 |
|
| 1149 |
@<Declare a function called |match_font|@>= |
| 1150 |
static web_integer mpx_match_font (MPX mpx, unsigned ff, web_boolean exact) { |
| 1151 |
unsigned f; /* font number being tested */ |
| 1152 |
for (f=0; f<mpx->nfonts ; f++) { |
| 1153 |
if ( f!=ff ) { |
| 1154 |
@<Compare the names of fonts |f| and |ff|; |continue| if they differ@>; |
| 1155 |
if ( exact ) { |
| 1156 |
if ( fabs(mpx->font_scaled_size[f]-mpx->font_scaled_size[ff])<= font_tolerance ) { |
| 1157 |
if ( ! mpx->vf_reading ) { |
| 1158 |
if ( mpx->local_only[f] ) { |
| 1159 |
mpx->font_num[f]=mpx->font_num[ff]; mpx->local_only[f]=false; |
| 1160 |
} else if ( mpx->font_num[f]!=mpx->font_num[ff] ) { |
| 1161 |
continue; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
break; |
| 1165 |
} |
| 1166 |
} else if ( mpx->info_base[f]!=max_widths ) { |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
} |
| 1171 |
if ( f<mpx->nfonts ) { |
| 1172 |
@<Make sure fonts |f| and |ff| have matching design sizes and checksums@>; |
| 1173 |
} |
| 1174 |
return (web_integer)f; |
| 1175 |
} |
| 1176 |
|
| 1177 |
@ @<Compare the names of fonts |f| and |ff|; |continue| if they differ@>= |
| 1178 |
if (strcmp(mpx->font_name[f],mpx->font_name[ff])) |
| 1179 |
continue |
| 1180 |
|
| 1181 |
@ @<Make sure fonts |f| and |ff| have matching design sizes and checksums@>= |
| 1182 |
if ( fabs(mpx->font_design_size[f]-mpx->font_design_size[ff]) > font_tolerance ) { |
| 1183 |
font_error("Inconsistent design sizes given for ",ff); |
| 1184 |
@.Inconsistent design sizes@> |
| 1185 |
} else if ( mpx->font_check_sum[f]!=mpx->font_check_sum[ff] ) { |
| 1186 |
font_warn("Checksum mismatch for ", ff); |
| 1187 |
@.Checksum mismatch@> |
| 1188 |
} |
| 1189 |
|
| 1190 |
@* Reading ordinary fonts. |
| 1191 |
An auxiliary array |in_width| is used to hold the widths as they are |
| 1192 |
input. The global variable |tfm_check_sum| is set to the check sum that |
| 1193 |
appears in the current \.{TFM} file. |
| 1194 |
|
| 1195 |
@<Glob...@>= |
| 1196 |
web_integer in_width[256]; /* \.{TFM} width data in \.{DVI} units */ |
| 1197 |
web_integer tfm_check_sum; /* check sum found in |tfm_file| */ |
| 1198 |
|
| 1199 |
@ Here is a procedure that absorbs the necessary information from a |
| 1200 |
\.{TFM} file, assuming that the file has just been successfully reset |
| 1201 |
so that we are ready to read its first byte. (A complete description of |
| 1202 |
\.{TFM} file format appears in the documentation of \.{TFtoPL} and will |
| 1203 |
not be repeated here.) The procedure does not check the \.{TFM} file |
| 1204 |
for validity, nor does it give explicit information about what is |
| 1205 |
wrong with a \.{TFM} file that proves to be invalid. The procedure simply |
| 1206 |
aborts the program if it detects anything amiss in the \.{TFM} data. |
| 1207 |
|
| 1208 |
@c |
| 1209 |
static void mpx_in_TFM (MPX mpx,web_integer f) { |
| 1210 |
/* input \.{TFM} data for font |f| or abort */ |
| 1211 |
web_integer k; /* index for loops */ |
| 1212 |
int lh; /* length of the header data, in four-byte words */ |
| 1213 |
int nw; /* number of words in the width table */ |
| 1214 |
unsigned int wp; /* new value of |info_ptr| after successful input */ |
| 1215 |
@<Read past the header data; |abort| if there is a problem@>; |
| 1216 |
@<Store character-width indices at the end of the |width| table@>; |
| 1217 |
@<Read the width values into the |in_width| table@>; |
| 1218 |
@<Move the widths from |in_width| to |width|@>; |
| 1219 |
mpx->fbase[f]=0; mpx->ftop[f]=0; |
| 1220 |
mpx->info_ptr=wp; |
| 1221 |
mpx_fclose(mpx,mpx->tfm_file); |
| 1222 |
return; |
| 1223 |
} |
| 1224 |
|
| 1225 |
@ @<Read past the header...@>= |
| 1226 |
mpx_read_tfm_word(mpx); lh=mpx->b2*(int)(256)+mpx->b3; |
| 1227 |
mpx_read_tfm_word(mpx); |
| 1228 |
mpx->font_bc[f]=mpx->b0*(int)(256)+mpx->b1; |
| 1229 |
mpx->font_ec[f]=mpx->b2*(int)(256)+mpx->b3; |
| 1230 |
if ( mpx->font_ec[f]<mpx->font_bc[f] ) mpx->font_bc[f]=mpx->font_ec[f]+1; |
| 1231 |
if ( mpx->info_ptr+(unsigned int)mpx->font_ec[f]-(unsigned int)mpx->font_bc[f]+1>max_widths ) |
| 1232 |
mpx_abort(mpx,"DVItoMP capacity exceeded (width table size=%d)!",max_widths); |
| 1233 |
@.DVItoMP capacity exceeded...@> |
| 1234 |
wp=mpx->info_ptr+(unsigned int)mpx->font_ec[f]-(unsigned int)mpx->font_bc[f]+1; |
| 1235 |
mpx_read_tfm_word(mpx); nw=mpx->b0*256+mpx->b1; |
| 1236 |
if ( (nw==0)||(nw>256) ) |
| 1237 |
font_abort("Bad TFM file for ",f); |
| 1238 |
@.Bad TFM file@> |
| 1239 |
for (k=1;k<=3+lh;k++) { |
| 1240 |
if ( feof(mpx->tfm_file) ) |
| 1241 |
font_abort("Bad TFM file for ",f); |
| 1242 |
@.Bad TFM file@> |
| 1243 |
mpx_read_tfm_word(mpx); |
| 1244 |
if ( k==4 ) { |
| 1245 |
if ( mpx->b0<128 ) |
| 1246 |
mpx->tfm_check_sum=((mpx->b0*(int)(256)+mpx->b1)*256+mpx->b2)*256+mpx->b3; |
| 1247 |
else |
| 1248 |
mpx->tfm_check_sum=(((mpx->b0-256)*(int)(256)+mpx->b1)*256+mpx->b2)*256+mpx->b3; |
| 1249 |
} |
| 1250 |
if ( k==5 ) { |
| 1251 |
if (mpx->mode == mpx_troff_mode) { |
| 1252 |
mpx->font_design_size[f]=(((mpx->b0*(int)(256)+mpx->b1)*256+mpx->b2)*256+mpx->b3)/(65536.0*16); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
@ @<Store character-width indices...@>= |
| 1258 |
if ( wp>0 ) { |
| 1259 |
for (k=(int)mpx->info_ptr;k<=(int)wp-1;k++ ) { |
| 1260 |
mpx_read_tfm_word(mpx); |
| 1261 |
if ( mpx->b0>nw ) |
| 1262 |
font_abort("Bad TFM file for ",f); |
| 1263 |
@.Bad TFM file@> |
| 1264 |
mpx->width[k]=mpx->b0; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
@ No fancy width calculation is needed here because \.{DVItoMP} stores |
| 1269 |
widths in their raw form as multiples of the design size scaled by $2^{20}$. |
| 1270 |
The |font_scaled_size| entries have been computed so that the final width |
| 1271 |
compution can be done in floating point if enough precision is available. |
| 1272 |
|
| 1273 |
@<Read the width values into the |in_width| table@>= |
| 1274 |
for (k=0;k<=nw-1;k++) { |
| 1275 |
mpx_read_tfm_word(mpx); |
| 1276 |
if ( mpx->b0>127 ) mpx->b0=mpx->b0-256; |
| 1277 |
mpx->in_width[k]=((mpx->b0*0400+mpx->b1)*0400+mpx->b2)*0400+mpx->b3; |
| 1278 |
} |
| 1279 |
|
| 1280 |
@ The width compution uses a scale factor |dvi_scale| that will be introduced |
| 1281 |
later. It is equal to one when not typesetting a character from a virtual |
| 1282 |
font. In that case, the following expressions do the width computation that is |
| 1283 |
so important in \.{DVItype}. It is less important here because it is impractical |
| 1284 |
to guarantee precise character positioning in \MP\ output. Nevertheless, the |
| 1285 |
width compution will be precise if reals have at least 46-bit mantissas and |
| 1286 |
|round(x-.5)| is equivalent to $\lfloor x\rfloor$. It may be a good idea to |
| 1287 |
modify this computation if these conditions are not met. |
| 1288 |
@^system dependencies@> |
| 1289 |
|
| 1290 |
@<Width of character |c| in font |f|@>= |
| 1291 |
floor(mpx->dvi_scale*mpx->font_scaled_size[f]*char_width(f,c)) |
| 1292 |
|
| 1293 |
@ @<Width of character |p| in font |cur_font|@>= |
| 1294 |
floor(mpx->dvi_scale*mpx->font_scaled_size[cur_font]*char_width(cur_font,p)) |
| 1295 |
|
| 1296 |
@ @<Move the widths from |in_width| to |width|@>= |
| 1297 |
if ( mpx->in_width[0]!=0 ) |
| 1298 |
font_abort("Bad TFM file for ",f); /* the first width should be zero */ |
| 1299 |
@.Bad TFM file@> |
| 1300 |
mpx->info_base[f]=(int)(mpx->info_ptr-(unsigned int)mpx->font_bc[f]); |
| 1301 |
if ( wp>0 ) { |
| 1302 |
for (k=(int)mpx->info_ptr;k<=(int)wp-1;k++) { |
| 1303 |
mpx->width[k]=mpx->in_width[mpx->width[k]]; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
@* Reading virtual fonts. |
| 1309 |
|
| 1310 |
The |in_VF| procedure absorbs the necessary information from a \.{VF} file that |
| 1311 |
has just been reset so that we are ready to read the first byte. (A complete |
| 1312 |
description of \.{VF} file format appears in the documention of \.{VFtoVP}). |
| 1313 |
Like |in_TFM|, this procedure simply aborts the program if it detects anything |
| 1314 |
wrong with the \.{VF} file. |
| 1315 |
|
| 1316 |
@c |
| 1317 |
@<Declare a function called |first_par|@>@; |
| 1318 |
static void mpx_in_VF (MPX mpx, web_integer f) { |
| 1319 |
/* read \.{VF} data for font |f| or abort */ |
| 1320 |
web_integer p; /* a byte from the \.{VF} file */ |
| 1321 |
boolean was_vf_reading; /* old value of |vf_reading| */ |
| 1322 |
web_integer c; /* the current character code */ |
| 1323 |
web_integer limit; /* space limitations force character codes to be less than this */ |
| 1324 |
web_integer w; /* a \.{TFM} width being read */ |
| 1325 |
was_vf_reading=mpx->vf_reading; mpx->vf_reading=true; |
| 1326 |
@<Start reading the preamble from a \.{VF} file@>;@/ |
| 1327 |
@<Initialize the data structures for the virtual font@>;@/ |
| 1328 |
p=mpx_get_byte(mpx); |
| 1329 |
while ( p>=fnt_def1 ) { |
| 1330 |
if ( p>fnt_def1+3 ) |
| 1331 |
font_abort("Bad VF file for ",f); |
| 1332 |
mpx_define_font(mpx, mpx_first_par(mpx, (unsigned int)p)); |
| 1333 |
p=mpx_get_byte(mpx); |
| 1334 |
} |
| 1335 |
while ( p<=242 ) { |
| 1336 |
if ( feof(mpx->vf_file) ) |
| 1337 |
font_abort("Bad VF file for ",f); |
| 1338 |
@<Read the packet length, character code, and \.{TFM} width@>; |
| 1339 |
@<Store the character packet in |cmd_buf|@>; |
| 1340 |
p=mpx_get_byte(mpx); |
| 1341 |
} |
| 1342 |
if ( p==post ) { |
| 1343 |
@<Finish setting up the data structures for the new virtual font@>; |
| 1344 |
mpx->vf_reading=was_vf_reading; |
| 1345 |
return; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
@ @<Start reading the preamble from a \.{VF} file@>= |
| 1350 |
p=mpx_get_byte(mpx); |
| 1351 |
if ( p!=pre ) |
| 1352 |
font_abort("Bad VF file for ",f); |
| 1353 |
p=mpx_get_byte(mpx); /* fetch the identification byte */ |
| 1354 |
if ( p!=202 ) |
| 1355 |
font_abort("Bad VF file for ",f); |
| 1356 |
p=mpx_get_byte(mpx); /* fetch the length of the introductory comment */ |
| 1357 |
while ( p-->0 ) |
| 1358 |
(void)mpx_get_byte(mpx); |
| 1359 |
mpx->tfm_check_sum=mpx_signed_quad(mpx); |
| 1360 |
(void)mpx_signed_quad(mpx); /* skip over the design size */ |
| 1361 |
|
| 1362 |
@ @<Initialize the data structures for the virtual font@>= |
| 1363 |
mpx->ftop[f]=(web_integer)mpx->vf_ptr; |
| 1364 |
if ( mpx->vf_ptr==mpx->nfonts ) |
| 1365 |
mpx_abort(mpx,"DVItoMP capacity exceeded (max font numbers=%d)", max_fnums); |
| 1366 |
@.DVItoMP capacity exceeded...@> |
| 1367 |
decr(mpx->vf_ptr); |
| 1368 |
mpx->info_base[f]=(web_integer)mpx->info_ptr; |
| 1369 |
limit=max_widths-mpx->info_base[f];@/ |
| 1370 |
mpx->font_bc[f]=limit; mpx->font_ec[f]=0 |
| 1371 |
|
| 1372 |
@ @<Read the packet length, character code, and \.{TFM} width@>= |
| 1373 |
if ( p==242 ) { |
| 1374 |
p=mpx_signed_quad(mpx); c=mpx_signed_quad(mpx); w=mpx_signed_quad(mpx); |
| 1375 |
if ( c<0 ) |
| 1376 |
font_abort("Bad VF file for ",f); |
| 1377 |
} else { |
| 1378 |
c=mpx_get_byte(mpx); w=mpx_get_three_bytes(mpx); |
| 1379 |
} |
| 1380 |
if ( c>=limit ) |
| 1381 |
mpx_abort(mpx,"DVItoMP capacity exceeded (max widths=%d)", max_widths); |
| 1382 |
@.DVItoMP capacity exceeded...@> |
| 1383 |
if ( c<mpx->font_bc[f] ) mpx->font_bc[f]=c; |
| 1384 |
if ( c>mpx->font_ec[f] ) mpx->font_ec[f]=c; |
| 1385 |
char_width(f,c)=w |
| 1386 |
|
| 1387 |
@ @<Store the character packet in |cmd_buf|@>= |
| 1388 |
if ( mpx->n_cmds+(unsigned int)p>=virtual_space ) |
| 1389 |
mpx_abort(mpx,"DVItoMP capacity exceeded (virtual font space=%d)",virtual_space); |
| 1390 |
@.DVItoMP capacity exceeded...@> |
| 1391 |
start_cmd(f,c)=(web_integer)mpx->n_cmds; |
| 1392 |
while ( p>0 ) { |
| 1393 |
mpx->cmd_buf[mpx->n_cmds]=(unsigned char)mpx_get_byte(mpx); |
| 1394 |
incr(mpx->n_cmds); decr(p); |
| 1395 |
} |
| 1396 |
mpx->cmd_buf[mpx->n_cmds]=eop; /* add the end-of-packet marker */ |
| 1397 |
incr(mpx->n_cmds) |
| 1398 |
|
| 1399 |
@ There are unused |width| and |cmd_ptr| entries if |font_bc[f]>0| but it isn't |
| 1400 |
worthwhile to slide everything down just to save a little space. |
| 1401 |
|
| 1402 |
@<Finish setting up the data structures for the new virtual font@>= |
| 1403 |
mpx->fbase[f]=(web_integer)(mpx->vf_ptr+1); |
| 1404 |
mpx->info_ptr=(unsigned int)(mpx->info_base[f]+mpx->font_ec[f]+1)@; |
| 1405 |
|
| 1406 |
|
| 1407 |
@* Loading fonts. |
| 1408 |
|
| 1409 |
The character width information for a font is loaded when the font is selected |
| 1410 |
for the first time. This information might already be loaded if the font has |
| 1411 |
already been used at a different scale factor. Otherwise, we look for a \.{VF} |
| 1412 |
file, or failing that, a \.{TFM} file. All this is done by the |select_font| |
| 1413 |
function that takes an external font number~|e| and returns the corresponding |
| 1414 |
internal font number with the width information loaded. |
| 1415 |
|
| 1416 |
@c |
| 1417 |
static web_integer mpx_select_font (MPX mpx, web_integer e) { |
| 1418 |
int f; /* the internal font number */ |
| 1419 |
int ff; /* internal font number for an existing version */ |
| 1420 |
web_integer k; /* general purpose loop counter */ |
| 1421 |
@<Set |f| to the internal font number that corresponds to |e|, |
| 1422 |
or |abort| if there is none@>; |
| 1423 |
if ( mpx->info_base[f]==max_widths ) { |
| 1424 |
ff=mpx_match_font(mpx, (unsigned)f,false); |
| 1425 |
if ( ff<(int)mpx->nfonts ) { |
| 1426 |
@<Make font |f| refer to the width information from font |ff|@>; |
| 1427 |
} else { |
| 1428 |
@<Move the \.{VF} file name into the |cur_name| string@>; |
| 1429 |
if ( mpx_open_vf_file(mpx) ) { |
| 1430 |
mpx_in_VF(mpx, f); |
| 1431 |
} else { |
| 1432 |
if ( ! mpx_open_tfm_file(mpx) ) |
| 1433 |
font_abort("No TFM file found for ",f); |
| 1434 |
@.no TFM file found@> |
| 1435 |
mpx_in_TFM(mpx, f); |
| 1436 |
} |
| 1437 |
@<Make sure the checksum in the font file matches the one given in the |
| 1438 |
|font_def| for font |f|@>; |
| 1439 |
} |
| 1440 |
@<Do any other initialization required for the new font |f|@>; |
| 1441 |
} |
| 1442 |
return f; |
| 1443 |
} |
| 1444 |
|
| 1445 |
@ @<Set |f| to the internal font number that corresponds to |e|,...@>= |
| 1446 |
if ( mpx->cur_ftop<=mpx->nfonts ) |
| 1447 |
mpx->cur_ftop=mpx->nfonts; |
| 1448 |
mpx->font_num[mpx->cur_ftop]=e; |
| 1449 |
k=(web_integer)mpx->cur_fbase; |
| 1450 |
while ((mpx->font_num[k]!=e)|| mpx->local_only[k] ) incr(k); |
| 1451 |
if ( k==(int)mpx->cur_ftop ) |
| 1452 |
mpx_abort(mpx,"Undefined font selected"); |
| 1453 |
f=mpx->internal_num[k] |
| 1454 |
|
| 1455 |
@ @<Make font |f| refer to the width information from font |ff|@>= |
| 1456 |
{ |
| 1457 |
mpx->font_bc[f]=mpx->font_bc[ff]; |
| 1458 |
mpx->font_ec[f]=mpx->font_ec[ff]; |
| 1459 |
mpx->info_base[f]=mpx->info_base[ff]; |
| 1460 |
mpx->fbase[f]=mpx->fbase[ff]; |
| 1461 |
mpx->ftop[f]=mpx->ftop[ff]; |
| 1462 |
} |
| 1463 |
|
| 1464 |
@ The string |cur_name| is supposed to be set to the external name of the |
| 1465 |
\.{VF} file for the current font. |
| 1466 |
@^system dependencies@> |
| 1467 |
|
| 1468 |
@<Move the \.{VF} file name into the |cur_name| string@>= |
| 1469 |
mpx->cur_name = xstrdup (mpx->font_name[f]) |
| 1470 |
|
| 1471 |
@ @<Make sure the checksum in the font file matches the one given in the...@>= |
| 1472 |
{ |
| 1473 |
if ( (mpx->font_check_sum[f]!=0)&&(mpx->tfm_check_sum!=0)&&@| |
| 1474 |
(mpx->font_check_sum[f]!=mpx->tfm_check_sum) ) { |
| 1475 |
font_warn("Checksum mismatch for ",f); |
| 1476 |
@.Checksum mismatch@> |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
@* Low level output routines. |
| 1481 |
|
| 1482 |
One of the basic output operations is to write a \MP\ string expression for |
| 1483 |
a sequence of characters to be typeset. The main difficulties are that such |
| 1484 |
strings can contain arbitrary eight-bit bytes and there is no fixed limit on |
| 1485 |
the length of the string that needs to be produced. In extreme cases this |
| 1486 |
can lead to expressions such as |
| 1487 |
$$\vcenter{ |
| 1488 |
\hbox{\.{char7\&char15\&char31\&"?FWayzz"}} |
| 1489 |
\hbox{\.{\&"zzaF"\&char15\&char3\&char31}} |
| 1490 |
\hbox{\.{\&"Nxzzzzzzzwvtsqo"}}} |
| 1491 |
$$ |
| 1492 |
|
| 1493 |
@ A global variable |state| keeps track of the output process. |
| 1494 |
When |state=normal| we have begun a quoted string and the next character |
| 1495 |
should be a printable character or a closing quote. When |state=special| |
| 1496 |
the last thing printed was a ``\.{char}'' construction or a closing quote |
| 1497 |
and an ampersand should come next. The starting condition |state=initial| |
| 1498 |
is a lot like |state=special|, except no ampersand is required. |
| 1499 |
|
| 1500 |
@d special 0 /* the |state| after printing a ``\.{char}'' expression */ |
| 1501 |
@d normal 1 /* the |state| value in a quoted string */ |
| 1502 |
@d initial 2 /* initial |state| */ |
| 1503 |
|
| 1504 |
@<Glob...@>= |
| 1505 |
int state; /* controls the process of printing a string */ |
| 1506 |
int print_col; /* there are at most this many characters on the current line */ |
| 1507 |
|
| 1508 |
@ @<Set initial values@>= |
| 1509 |
mpx->state = initial; |
| 1510 |
mpx->print_col = 0; /* there are at most this many characters on the current line */ |
| 1511 |
|
| 1512 |
@ To print a string on the \.{MPX} file, initialize |print_col|, ensure that |
| 1513 |
|state=initial|, and pass the characters one-at-a-time to |print_char|. |
| 1514 |
|
| 1515 |
@<Declare subroutines for printing strings@>= |
| 1516 |
static void mpx_print_char (MPX mpx, unsigned char c) { |
| 1517 |
web_integer l; /* number of characters to print |c| or the \.{char} expression */ |
| 1518 |
if ( printable(c) ) l=1; |
| 1519 |
else if ( c<10 ) l=5; |
| 1520 |
else if ( c<100 ) l=6; |
| 1521 |
else l=7; |
| 1522 |
if ( mpx->print_col+l>line_length-2 ) { |
| 1523 |
if ( mpx->state==normal ) { |
| 1524 |
fprintf(mpx->mpxfile,"\""); mpx->state=special; |
| 1525 |
} |
| 1526 |
fprintf(mpx->mpxfile,"\n"); |
| 1527 |
mpx->print_col=0; |
| 1528 |
} |
| 1529 |
@<Print |c| and update |state| and |print_col|@>; |
| 1530 |
} |
| 1531 |
|
| 1532 |
@ @<Print |c| and update |state| and |print_col|@>= |
| 1533 |
if ( mpx->state==normal ) { |
| 1534 |
if ( printable(c) ) { |
| 1535 |
fprintf(mpx->mpxfile,"%c",xchr(c)); |
| 1536 |
} else { |
| 1537 |
fprintf(mpx->mpxfile,"\"&char%d",c); |
| 1538 |
mpx->print_col +=2; |
| 1539 |
} |
| 1540 |
} else { |
| 1541 |
if ( mpx->state==special ) { |
| 1542 |
fprintf(mpx->mpxfile,"&"); |
| 1543 |
incr(mpx->print_col); |
| 1544 |
} |
| 1545 |
if ( printable(c) ) { |
| 1546 |
fprintf(mpx->mpxfile,"\"%c",xchr(c)); |
| 1547 |
incr(mpx->print_col); |
| 1548 |
} else { |
| 1549 |
fprintf(mpx->mpxfile,"char%d",c); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
mpx->print_col += l; |
| 1553 |
if ( printable(c) ) |
| 1554 |
mpx->state=normal; |
| 1555 |
else |
| 1556 |
mpx->state=special |
| 1557 |
|
| 1558 |
@ The |end_char_string| procedure gets the string ended properly and ensures |
| 1559 |
that there is room for |l| more characters on the output line. |
| 1560 |
|
| 1561 |
@<Declare subroutines for printing strings@>= |
| 1562 |
static void mpx_end_char_string (MPX mpx,web_integer l) { |
| 1563 |
while ( mpx->state>special ){ |
| 1564 |
fprintf(mpx->mpxfile,"\""); |
| 1565 |
incr(mpx->print_col); |
| 1566 |
decr(mpx->state); |
| 1567 |
} |
| 1568 |
if ( mpx->print_col+l>line_length ) { |
| 1569 |
fprintf(mpx->mpxfile,"\n "); mpx->print_col=0; |
| 1570 |
} |
| 1571 |
mpx->state=initial; /* get ready to print the next string */ |
| 1572 |
} |
| 1573 |
|
| 1574 |
@ Since |end_char_string| resets |state:=initial|, all we have to do is set |
| 1575 |
|state:=initial| once at the beginning. |
| 1576 |
|
| 1577 |
@<Set init...@>= |
| 1578 |
mpx->state=initial; |
| 1579 |
|
| 1580 |
@ Characters and rules are positioned according to global variables |h| and~|v| |
| 1581 |
as will be explained later. We also need scale factors that convert quantities |
| 1582 |
to the right units when they are printed in the \.{MPX} file. |
| 1583 |
|
| 1584 |
Even though all variable names in the \MP\ output are made local via \.{save} |
| 1585 |
commands, it is still desirable to preceed them with underscores. This makes |
| 1586 |
the output more likely to work when used in a macro definition, since the |
| 1587 |
generated variables names must not collide with formal parameters in such |
| 1588 |
cases. |
| 1589 |
|
| 1590 |
@<Glob...@>= |
| 1591 |
web_integer h; |
| 1592 |
web_integer v; /* the current position in \.{DVI} units */ |
| 1593 |
double conv; /* converts \.{DVI} units to \MP\ points */ |
| 1594 |
double mag; /* magnification factor times 1000 */ |
| 1595 |
|
| 1596 |
@ @c @<Declare a procedure called |finish_last_char|@>@; |
| 1597 |
static void mpx_do_set_char (MPX mpx,web_integer f, web_integer c) { |
| 1598 |
if ( (c<mpx->font_bc[f])||(c>mpx->font_ec[f]) ) |
| 1599 |
mpx_abort(mpx,"attempt to typeset invalid character %d",c); |
| 1600 |
@.attempt to typeset...@> |
| 1601 |
if ((mpx->h!=mpx->str_h2)||(mpx->v!=mpx->str_v)|| |
| 1602 |
(f!=mpx->str_f)||(mpx->dvi_scale!=mpx->str_scale) ) { |
| 1603 |
if ( mpx->str_f>=0 ) { |
| 1604 |
mpx_finish_last_char(mpx); |
| 1605 |
} else if ( ! mpx->fonts_used ) { |
| 1606 |
@<Prepare to output the first character on a page@>; |
| 1607 |
} |
| 1608 |
if ( ! mpx->font_used[f] ) |
| 1609 |
@<Prepare to use font |f| for the first time on a page@>; |
| 1610 |
fprintf(mpx->mpxfile,"_s("); mpx->print_col=3;@/ |
| 1611 |
mpx->str_scale=mpx->dvi_scale; mpx->str_f=f; |
| 1612 |
mpx->str_v=mpx->v; mpx->str_h1=mpx->h; |
| 1613 |
} |
| 1614 |
mpx_print_char(mpx, (unsigned char)c); |
| 1615 |
mpx->str_h2=(web_integer)(mpx->h+@<Width of character |c| in font |f|@>); |
| 1616 |
} |
| 1617 |
|
| 1618 |
@ @<Glob...@>= |
| 1619 |
boolean font_used[(max_fonts+1)]; /* has this font been used on this page? */ |
| 1620 |
boolean fonts_used; /* has any font been used on this page? */ |
| 1621 |
boolean rules_used; /* has any rules been set on this page? */ |
| 1622 |
web_integer str_h1; |
| 1623 |
web_integer str_v; /* starting position for current output string */ |
| 1624 |
web_integer str_h2; /* where the current output string ends */ |
| 1625 |
web_integer str_f; /* internal font number for the current output string */ |
| 1626 |
double str_scale; /* value of |dvi_scale| for the current output string */ |
| 1627 |
|
| 1628 |
|
| 1629 |
@ Before using any fonts we need to define a MetaPost macro for |
| 1630 |
typesetting character strings. The |font_used| array is not |
| 1631 |
initialized until it is actually time to output a character. |
| 1632 |
|
| 1633 |
@<Declarations@>= |
| 1634 |
static void mpx_prepare_font_use(MPX mpx); |
| 1635 |
|
| 1636 |
@ @c |
| 1637 |
static void mpx_prepare_font_use(MPX mpx) { |
| 1638 |
unsigned k; |
| 1639 |
for (k=0; k<mpx->nfonts;k++ ) |
| 1640 |
mpx->font_used[k]=false; |
| 1641 |
mpx->fonts_used=true; |
| 1642 |
fprintf(mpx->mpxfile,"string _n[];\n"); |
| 1643 |
fprintf(mpx->mpxfile,"vardef _s(expr _t,_f,_m,_x,_y)(text _c)=\n"); |
| 1644 |
fprintf(mpx->mpxfile, |
| 1645 |
" addto _p also _t infont _f scaled _m shifted (_x,_y) _c; enddef;\n"); |
| 1646 |
} |
| 1647 |
|
| 1648 |
@ @<Prepare to output the first character on a page@>= |
| 1649 |
mpx_prepare_font_use(mpx) |
| 1650 |
|
| 1651 |
|
| 1652 |
@ @<Do any other initialization required for the new font |f|@>= |
| 1653 |
mpx->font_used[f]=false; |
| 1654 |
|
| 1655 |
@ Do what is necessary when the font with internal number f is used for the |
| 1656 |
first time on a page. |
| 1657 |
|
| 1658 |
@<Declarations@>= |
| 1659 |
static void mpx_first_use(MPX mpx, int f) ; |
| 1660 |
|
| 1661 |
@ @c |
| 1662 |
static void mpx_first_use(MPX mpx, int f) { |
| 1663 |
mpx->font_used[f]=true; |
| 1664 |
fprintf(mpx->mpxfile,"_n%d=",f); |
| 1665 |
mpx->print_col=6; |
| 1666 |
mpx_print_font(mpx, f); |
| 1667 |
mpx_end_char_string(mpx, 1); |
| 1668 |
fprintf(mpx->mpxfile,";\n"); |
| 1669 |
} |
| 1670 |
|
| 1671 |
@ @<Prepare to use font |f| for the first time on a page@>= |
| 1672 |
mpx_first_use(mpx,f); |
| 1673 |
|
| 1674 |
@ We maintain the invariant that |str_f=-1| when there is no output string |
| 1675 |
under construction. |
| 1676 |
|
| 1677 |
@<Declare a procedure called |finish_last_char|@>= |
| 1678 |
static void mpx_finish_last_char (MPX mpx) { |
| 1679 |
double m,x,y; |
| 1680 |
/* font scale factor and \MP\ coordinates of reference point */ |
| 1681 |
if ( mpx->str_f>=0 ) { |
| 1682 |
if (mpx->mode==mpx_tex_mode) { |
| 1683 |
m=mpx->str_scale*mpx->font_scaled_size[mpx->str_f]* |
| 1684 |
mpx->mag/mpx->font_design_size[mpx->str_f]; |
| 1685 |
x=mpx->conv*mpx->str_h1; |
| 1686 |
y=mpx->conv*(-mpx->str_v); |
| 1687 |
if ( (fabs(x)>=4096.0)||(fabs(y)>=4096.0)||(m>=4096.0)||(m<0) ) { |
| 1688 |
mpx_warn(mpx,"text is out of range"); |
| 1689 |
mpx_end_char_string(mpx, 60); |
| 1690 |
} else { |
| 1691 |
mpx_end_char_string(mpx, 40); |
| 1692 |
} |
| 1693 |
fprintf(mpx->mpxfile,",_n%d,%1.5f,%1.4f,%1.4f,",mpx->str_f,m,x,y); |
| 1694 |
@<Print a \.{withcolor} specifier if appropriate@>@/ |
| 1695 |
fprintf(mpx->mpxfile,");\n"); |
| 1696 |
} else { |
| 1697 |
m = mpx->str_size / mpx->font_design_size[mpx->str_f]; |
| 1698 |
x = mpx->dmp_str_h1 * mpx->unit; |
| 1699 |
y = YCORR - mpx->dmp_str_v * mpx->unit; |
| 1700 |
if (fabs(x) >= 4096.0 || fabs(y) >= 4096.0 || m >= 4096.0 || m < 0) { |
| 1701 |
mpx_warn(mpx,"text out of range ignored"); |
| 1702 |
mpx_end_char_string(mpx,67); |
| 1703 |
} else { |
| 1704 |
mpx_end_char_string(mpx,47); |
| 1705 |
} |
| 1706 |
fprintf(mpx->mpxfile, "), _n%d", mpx->str_f); |
| 1707 |
fprintf(mpx->mpxfile, ",%.5f,%.4f,%.4f)", (m*1.00375), (x/100.0), y); |
| 1708 |
mpx_slant_and_ht(mpx); |
| 1709 |
fprintf(mpx->mpxfile, ";\n"); |
| 1710 |
} |
| 1711 |
mpx->str_f=-1; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
@ Setting rules is fairly simple. |
| 1716 |
|
| 1717 |
@c |
| 1718 |
static void mpx_do_set_rule (MPX mpx,web_integer ht, web_integer wd) { |
| 1719 |
double xx1,yy1,xx2,yy2,ww; |
| 1720 |
/* \MP\ coordinates of lower-left and upper-right corners */ |
| 1721 |
if ( wd==1 ) { |
| 1722 |
@<Handle a special rule that determines the box size@> |
| 1723 |
} else if ( (ht>0)||(wd>0) ) { |
| 1724 |
if ( mpx->str_f>=0 ) |
| 1725 |
mpx_finish_last_char(mpx); |
| 1726 |
if ( ! mpx->rules_used ) { |
| 1727 |
mpx->rules_used=true; |
| 1728 |
fprintf(mpx->mpxfile, |
| 1729 |
"interim linecap:=0;\n" |
| 1730 |
"vardef _r(expr _a,_w)(text _t) =\n" |
| 1731 |
" addto _p doublepath _a withpen pencircle scaled _w _t enddef;"); |
| 1732 |
} |
| 1733 |
@<Make |(xx1,yy1)| and |(xx2,yy2)| then ends of the desired penstroke |
| 1734 |
and |ww| the desired stroke width@>; |
| 1735 |
if ( (fabs(xx1)>=4096.0)||(fabs(yy1)>=4096.0)||@| |
| 1736 |
(fabs(xx2)>=4096.0)||(fabs(yy2)>=4096.0)||(ww>=4096.0) ) |
| 1737 |
mpx_warn(mpx,"hrule or vrule is out of range"); |
| 1738 |
fprintf(mpx->mpxfile,"_r((%1.4f,%1.4f)..(%1.4f,%1.4f), %1.4f,",xx1,yy1,xx2,yy2,ww); |
| 1739 |
@<Print a \.{withcolor} specifier if appropriate@>@/ |
| 1740 |
fprintf(mpx->mpxfile,");\n"); |
| 1741 |
} |
| 1742 |
} |
| 1743 |
|
| 1744 |
@ @<Make |(xx1,yy1)| and |(xx2,yy2)| then ends of the desired penstroke...@>= |
| 1745 |
xx1=mpx->conv*mpx->h; |
| 1746 |
yy1=mpx->conv*(-mpx->v); |
| 1747 |
if ( wd>ht ){ |
| 1748 |
xx2=xx1+mpx->conv*wd; |
| 1749 |
ww=mpx->conv*ht; |
| 1750 |
yy1=yy1+0.5*ww; |
| 1751 |
yy2=yy1; |
| 1752 |
} else { |
| 1753 |
yy2=yy1+mpx->conv*ht; |
| 1754 |
ww=mpx->conv*wd; |
| 1755 |
xx1=xx1+0.5*ww; |
| 1756 |
xx2=xx1; |
| 1757 |
} |
| 1758 |
|
| 1759 |
@ Rules of width one dvi unit are not typeset since \.{MPtoTeX} adds an |
| 1760 |
extraneous rule of this width in order to allow \.{DVItoMP} to deduce the |
| 1761 |
dimensions of the boxes it ships out. The box width is the left edge of the |
| 1762 |
last such rule; the height and depth are at the top and bottom of the rule. |
| 1763 |
There should be only one special rule per picture but there could be more if |
| 1764 |
the user tries to typeset his own one-dvi-unit rules. In this case the |
| 1765 |
dimension-determining rule is the last one in the picture. |
| 1766 |
|
| 1767 |
@<Handle a special rule that determines the box size@>= |
| 1768 |
{ |
| 1769 |
mpx->pic_wd=mpx->h; mpx->pic_dp=mpx->v; mpx->pic_ht=ht-mpx->v; |
| 1770 |
} |
| 1771 |
|
| 1772 |
@ @<Glob...@>= |
| 1773 |
web_integer pic_dp; web_integer pic_ht; web_integer pic_wd; /* picture dimensions from special rule */ |
| 1774 |
|
| 1775 |
@ The following initialization and clean-up is required. We do a little more |
| 1776 |
initialization than is absolutely necessary since some compilers might complain |
| 1777 |
if the variables are uninitialized when |do_set_char| tests them. |
| 1778 |
|
| 1779 |
@c |
| 1780 |
static void mpx_start_picture (MPX mpx) { |
| 1781 |
mpx->fonts_used=false; |
| 1782 |
mpx->rules_used=false; |
| 1783 |
mpx->graphics_used=false; |
| 1784 |
mpx->str_f=-1; |
| 1785 |
mpx->str_v=0; |
| 1786 |
mpx->str_h2=0; |
| 1787 |
mpx->str_scale=1.0; /* values don't matter */ |
| 1788 |
mpx->dmp_str_v = 0.0; |
| 1789 |
mpx->dmp_str_h2 = 0.0; |
| 1790 |
mpx->str_size = 0.0; |
| 1791 |
fprintf(mpx->mpxfile, |
| 1792 |
"begingroup save %s_p,_r,_s,_n; picture _p; _p=nullpicture;\n", |
| 1793 |
(mpx->mode == mpx_tex_mode ? "" : "_C,_D,")); |
| 1794 |
} |
| 1795 |
|
| 1796 |
static void mpx_stop_picture (MPX mpx) { |
| 1797 |
double w,h,dd; /* width, height, negative depth in PostScript points */ |
| 1798 |
if ( mpx->str_f>=0 ) |
| 1799 |
mpx_finish_last_char(mpx); |
| 1800 |
if (mpx->mode==mpx_tex_mode) { |
| 1801 |
@<Print a \&{setbounds} command based on picture dimensions@>; |
| 1802 |
} |
| 1803 |
fprintf(mpx->mpxfile,"_p endgroup\n"); |
| 1804 |
} |
| 1805 |
|
| 1806 |
@ @<Print a \&{setbounds} command based on picture dimensions@>= |
| 1807 |
dd=-mpx->pic_dp*mpx->conv; |
| 1808 |
w=mpx->conv*mpx->pic_wd; |
| 1809 |
h=mpx->conv*mpx->pic_ht; |
| 1810 |
fprintf(mpx->mpxfile, |
| 1811 |
"setbounds _p to (0,%1.4f)--(%1.4f,%1.4f)--\n" |
| 1812 |
" (%1.4f,%1.4f)--(0,%1.4f)--cycle;\n",dd,w,dd,w,h,h) |
| 1813 |
|
| 1814 |
@* Translation to symbolic form. |
| 1815 |
|
| 1816 |
The main work of \.{DVItoMP} is accomplished by the |do_dvi_commands| |
| 1817 |
procedure, which produces the output for an entire page, assuming that the |
| 1818 |
|bop| command for that page has already been processed. This procedure is |
| 1819 |
essentially an interpretive routine that reads and acts on the \.{DVI} |
| 1820 |
commands. It is also capable of executing the typesetting commands for |
| 1821 |
a character in a virtual font. |
| 1822 |
|
| 1823 |
@ The definition of \.{DVI} files refers to six registers, |
| 1824 |
$(h,v,w,x,y,z)$, which hold |web_integer| values in \.{DVI} units. |
| 1825 |
These units come directly from the input file except they need to be |
| 1826 |
rescaled when typesetting characters from a virtual font. |
| 1827 |
The stack of $(h,v,w,x,y,z)$ values is represented by six arrays |
| 1828 |
called |hstack|, \dots, |zstack|. |
| 1829 |
|
| 1830 |
@<Glob...@>= |
| 1831 |
web_integer w;web_integer x;web_integer y;web_integer z; |
| 1832 |
/* current state values (|h| and |v| have already been declared) */ |
| 1833 |
web_integer hstack[(stack_size+1)]; |
| 1834 |
web_integer vstack[(stack_size+1)]; |
| 1835 |
web_integer wstack[(stack_size+1)]; |
| 1836 |
web_integer xstack[(stack_size+1)]; |
| 1837 |
web_integer ystack[(stack_size+1)]; |
| 1838 |
web_integer zstack[(stack_size+1)]; /* pushed down values in \.{DVI} units */ |
| 1839 |
web_integer stk_siz; /* the current stack size */ |
| 1840 |
double dvi_scale; /* converts units of current input source to \.{DVI} units */ |
| 1841 |
|
| 1842 |
@ @<Do initialization required before starting a new page@>= |
| 1843 |
mpx->dvi_scale=1.0; |
| 1844 |
mpx->stk_siz=0; |
| 1845 |
mpx->h=0; mpx->v=0; |
| 1846 |
mpx->Xslant = 0.0; mpx->Xheight = 0.0 |
| 1847 |
|
| 1848 |
@ Next, we need procedures to handle |push| and |pop| commands. |
| 1849 |
|
| 1850 |
@c @<Declare procedures to handle color commands@> |
| 1851 |
static void mpx_do_push (MPX mpx) { |
| 1852 |
if ( mpx->stk_siz==stack_size ) |
| 1853 |
mpx_abort(mpx,"DVItoMP capacity exceeded (stack size=%d)",stack_size); |
| 1854 |
@.DVItoMP capacity exceeded...@> |
| 1855 |
mpx->hstack[mpx->stk_siz]=mpx->h; |
| 1856 |
mpx->vstack[mpx->stk_siz]=mpx->v; mpx->wstack[mpx->stk_siz]=mpx->w; |
| 1857 |
mpx->xstack[mpx->stk_siz]=mpx->x; |
| 1858 |
mpx->ystack[mpx->stk_siz]=mpx->y; mpx->zstack[mpx->stk_siz]=mpx->z; |
| 1859 |
incr(mpx->stk_siz); |
| 1860 |
} |
| 1861 |
|
| 1862 |
static void mpx_do_pop (MPX mpx) { |
| 1863 |
if ( mpx->stk_siz==0 ) |
| 1864 |
bad_dvi("attempt to pop empty stack"); |
| 1865 |
else { |
| 1866 |
decr(mpx->stk_siz); |
| 1867 |
mpx->h=mpx->hstack[mpx->stk_siz]; |
| 1868 |
mpx->v=mpx->vstack[mpx->stk_siz]; mpx->w=mpx->wstack[mpx->stk_siz]; |
| 1869 |
mpx->x=mpx->xstack[mpx->stk_siz]; |
| 1870 |
mpx->y=mpx->ystack[mpx->stk_siz]; mpx->z=mpx->zstack[mpx->stk_siz]; |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|
| 1874 |
@ The |set_virtual_char| procedure is mutually recursive with |
| 1875 |
|do_dvi_commands|. This is really a supervisory |
| 1876 |
@^recursion@> |
| 1877 |
procedure that calls |do_set_char| or adjusts the input source to read |
| 1878 |
typesetting commands for a character in a virtual font. |
| 1879 |
|
| 1880 |
@c |
| 1881 |
static void mpx_do_dvi_commands (MPX mpx); |
| 1882 |
static void mpx_set_virtual_char (MPX mpx,web_integer f, web_integer c) { |
| 1883 |
double old_scale; /* original value of |dvi_scale| */ |
| 1884 |
unsigned old_buf_ptr; /* original value of the input pointer |buf_ptr| */ |
| 1885 |
unsigned old_fbase,old_ftop; /* originally applicable part of the |font_num| table */ |
| 1886 |
if ( mpx->fbase[f]==0 ) |
| 1887 |
mpx_do_set_char(mpx, f,c); |
| 1888 |
else { |
| 1889 |
old_fbase=mpx->cur_fbase; old_ftop=mpx->cur_ftop; |
| 1890 |
mpx->cur_fbase=(unsigned int)mpx->fbase[f]; |
| 1891 |
mpx->cur_ftop=(unsigned int)mpx->ftop[f]; |
| 1892 |
old_scale=mpx->dvi_scale; |
| 1893 |
mpx->dvi_scale=mpx->dvi_scale*mpx->font_scaled_size[f]; |
| 1894 |
old_buf_ptr=mpx->buf_ptr; |
| 1895 |
mpx->buf_ptr=(unsigned int)start_cmd(f,c); |
| 1896 |
mpx_do_push(mpx); |
| 1897 |
mpx_do_dvi_commands(mpx); |
| 1898 |
mpx_do_pop(mpx);@/ |
| 1899 |
mpx->buf_ptr=old_buf_ptr; |
| 1900 |
mpx->dvi_scale=old_scale; |
| 1901 |
mpx->cur_fbase=old_fbase; |
| 1902 |
mpx->cur_ftop=old_ftop; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
@ Before we get into the details of |do_dvi_commands|, it is convenient to |
| 1907 |
consider a simpler routine that computes the first parameter of each |
| 1908 |
opcode. |
| 1909 |
|
| 1910 |
@d four_cases(A) (A): case (A)+1: case (A)+2: case (A)+3 |
| 1911 |
@d eight_cases(A) four_cases((A)): case four_cases((A)+4) |
| 1912 |
@d sixteen_cases(A) eight_cases((A)): case eight_cases((A)+8) |
| 1913 |
@d thirty_two_cases(A) sixteen_cases((A)): case sixteen_cases((A)+16) |
| 1914 |
@d sixty_four_cases(A) thirty_two_cases((A)): case thirty_two_cases((A)+32) |
| 1915 |
|
| 1916 |
@<Declare a function called |first_par|@>= |
| 1917 |
static web_integer mpx_first_par (MPX mpx, unsigned int o) { |
| 1918 |
switch (o) { |
| 1919 |
case sixty_four_cases(set_char_0): |
| 1920 |
case sixty_four_cases(set_char_0+64): |
| 1921 |
return (web_integer)(o-set_char_0); |
| 1922 |
break; |
| 1923 |
case set1: case put1: case fnt1: case xxx1: case fnt_def1: |
| 1924 |
return mpx_get_byte(mpx); |
| 1925 |
break; |
| 1926 |
case set1+1: case put1+1: case fnt1+1: case xxx1+1: case fnt_def1+1: |
| 1927 |
return mpx_get_two_bytes(mpx); |
| 1928 |
break; |
| 1929 |
case set1+2: case put1+2: case fnt1+2: case xxx1+2: case fnt_def1+2: |
| 1930 |
return mpx_get_three_bytes(mpx); |
| 1931 |
break; |
| 1932 |
case right1: case w1: case x1: case down1: case y1: case z1: |
| 1933 |
return mpx_signed_byte(mpx); |
| 1934 |
break; |
| 1935 |
case right1+1: case w1+1: case x1+1: case down1+1: case y1+1: case z1+1: |
| 1936 |
return mpx_signed_pair(mpx); |
| 1937 |
break; |
| 1938 |
case right1+2: case w1+2: case x1+2: case down1+2: case y1+2: case z1+2: |
| 1939 |
return mpx_signed_trio(mpx); |
| 1940 |
break; |
| 1941 |
case set1+3: case set_rule: case put1+3: case put_rule: |
| 1942 |
case right1+3: case w1+3: case x1+3: case down1+3: case y1+3: case z1+3: |
| 1943 |
case fnt1+3: case xxx1+3: case fnt_def1+3: |
| 1944 |
return mpx_signed_quad(mpx); |
| 1945 |
break; |
| 1946 |
case nop: case bop: case eop: case push: case pop: case pre: case post: |
| 1947 |
case post_post: case undefined_commands: |
| 1948 |
return 0; |
| 1949 |
break; |
| 1950 |
case w0: return mpx->w; break; |
| 1951 |
case x0: return mpx->x; break; |
| 1952 |
case y0: return mpx->y; break; |
| 1953 |
case z0: return mpx->z; break; |
| 1954 |
case sixty_four_cases(fnt_num_0): |
| 1955 |
return (web_integer)(o-fnt_num_0); |
| 1956 |
break; |
| 1957 |
} |
| 1958 |
return 0; /* compiler warning */ |
| 1959 |
} |
| 1960 |
|
| 1961 |
@ Here is the |do_dvi_commands| procedure. |
| 1962 |
|
| 1963 |
@c |
| 1964 |
static void mpx_do_dvi_commands (MPX mpx) { |
| 1965 |
unsigned int o; /* operation code of the current command */ |
| 1966 |
web_integer p,q; /* parameters of the current command */ |
| 1967 |
web_integer cur_font; /* current internal font number */ |
| 1968 |
if ( (mpx->cur_fbase<mpx->cur_ftop) && (mpx->buf_ptr<virtual_space) ) |
| 1969 |
cur_font=mpx_select_font(mpx, mpx->font_num[mpx->cur_ftop-1]); /* select first local font */ |
| 1970 |
else |
| 1971 |
cur_font=max_fnums+1; /* current font is undefined */ |
| 1972 |
mpx->w=0; mpx->x=0; mpx->y=0; mpx->z=0; /* initialize the state variables */ |
| 1973 |
while ( true ) { |
| 1974 |
@<Translate the next command in the \.{DVI} file; |return| if it was |eop|@>; |
| 1975 |
} |
| 1976 |
} |
| 1977 |
|
| 1978 |
@ The multiway switch in |first_par|, above, was organized by the length |
| 1979 |
of each command; the one in |do_dvi_commands| is organized by the semantics. |
| 1980 |
|
| 1981 |
@ @<Translate the next command...@>= |
| 1982 |
{ |
| 1983 |
o=(unsigned int)mpx_get_byte(mpx); |
| 1984 |
p=mpx_first_par(mpx, o); |
| 1985 |
if ( feof(mpx->dvi_file) ) |
| 1986 |
bad_dvi("the DVI file ended prematurely"); |
| 1987 |
@.the DVI file ended prematurely@> |
| 1988 |
if ( o<set1+4 ) { /* |set_char_0| through |set_char_127|, |set1| through |set4| */ |
| 1989 |
if ( cur_font>max_fnums ) { |
| 1990 |
if ( mpx->vf_reading ) |
| 1991 |
mpx_abort(mpx,"no font selected for character %d in virtual font", p); |
| 1992 |
else |
| 1993 |
bad_dvi_two("no font selected for character %d",p); |
| 1994 |
} |
| 1995 |
@.no font selected@> |
| 1996 |
mpx_set_virtual_char(mpx, cur_font,p); |
| 1997 |
mpx->h += @<Width of character |p| in font |cur_font|@>; |
| 1998 |
} else { |
| 1999 |
switch(o) { |
| 2000 |
case four_cases(put1): |
| 2001 |
mpx_set_virtual_char(mpx, cur_font, p); |
| 2002 |
break; |
| 2003 |
case set_rule: |
| 2004 |
q=(web_integer)trunc(mpx_signed_quad(mpx)*mpx->dvi_scale); |
| 2005 |
mpx_do_set_rule(mpx, (web_integer)trunc(p*mpx->dvi_scale),q); |
| 2006 |
mpx->h += q; |
| 2007 |
break; |
| 2008 |
case put_rule: |
| 2009 |
q=(web_integer)trunc(mpx_signed_quad(mpx)*mpx->dvi_scale); |
| 2010 |
mpx_do_set_rule(mpx, (web_integer)trunc(p*mpx->dvi_scale),q); |
| 2011 |
break; |
| 2012 |
@<Additional cases for translating \.{DVI} command |o| with |
| 2013 |
first parameter |p|@>@; |
| 2014 |
case undefined_commands: |
| 2015 |
bad_dvi_two("undefined command %d",o); |
| 2016 |
@.undefined command@> |
| 2017 |
break; |
| 2018 |
} /* all cases have been enumerated */ |
| 2019 |
} |
| 2020 |
} |
| 2021 |
|
| 2022 |
@ @<Additional cases for translating \.{DVI} command |o|...@>= |
| 2023 |
case four_cases(xxx1): |
| 2024 |
mpx_do_xxx(mpx, p); |
| 2025 |
break; |
| 2026 |
case pre: case post: case post_post: |
| 2027 |
bad_dvi("preamble or postamble within a page!"); |
| 2028 |
@.preamble or postamble within a page@> |
| 2029 |
break; |
| 2030 |
|
| 2031 |
@ @<Additional cases for translating \.{DVI} command |o|...@>= |
| 2032 |
case nop: |
| 2033 |
break; |
| 2034 |
case bop: |
| 2035 |
bad_dvi("bop occurred before eop"); |
| 2036 |
@.bop occurred before eop@> |
| 2037 |
break; |
| 2038 |
case eop: |
| 2039 |
return; |
| 2040 |
break; |
| 2041 |
case push: |
| 2042 |
mpx_do_push(mpx); |
| 2043 |
break; |
| 2044 |
case pop: |
| 2045 |
mpx_do_pop(mpx); |
| 2046 |
break; |
| 2047 |
|
| 2048 |
@ @<Additional cases for translating \.{DVI} command |o|...@>= |
| 2049 |
case four_cases(right1): |
| 2050 |
mpx->h += trunc(p*mpx->dvi_scale); |
| 2051 |
break; |
| 2052 |
case w0: case four_cases(w1): |
| 2053 |
mpx->w = (web_integer)trunc(p*mpx->dvi_scale); mpx->h += mpx->w; |
| 2054 |
break; |
| 2055 |
case x0: case four_cases(x1): |
| 2056 |
mpx->x = (web_integer)trunc(p*mpx->dvi_scale); mpx->h += mpx->x; |
| 2057 |
break; |
| 2058 |
case four_cases(down1): |
| 2059 |
mpx->v += trunc(p*mpx->dvi_scale); |
| 2060 |
break; |
| 2061 |
case y0: case four_cases(y1): |
| 2062 |
mpx->y = (web_integer)trunc(p*mpx->dvi_scale); mpx->v += mpx->y; |
| 2063 |
break; |
| 2064 |
case z0: case four_cases(z1): |
| 2065 |
mpx->z = (web_integer)trunc(p*mpx->dvi_scale); mpx->v += mpx->z; |
| 2066 |
break; |
| 2067 |
|
| 2068 |
@ @<Additional cases for translating \.{DVI} command |o|...@>= |
| 2069 |
case sixty_four_cases(fnt_num_0): case four_cases(fnt1): |
| 2070 |
cur_font = mpx_select_font(mpx, p); |
| 2071 |
break; |
| 2072 |
case four_cases(fnt_def1): |
| 2073 |
mpx_define_font(mpx, p); |
| 2074 |
break; |
| 2075 |
|
| 2076 |
@* The main program. |
| 2077 |
Now we are ready to put it all together. This is where \.{DVItoMP} starts, |
| 2078 |
and where it ends. |
| 2079 |
|
| 2080 |
@c |
| 2081 |
static int mpx_dvitomp (MPX mpx, char *dviname) { |
| 2082 |
int k; |
| 2083 |
mpx->dviname = dviname; |
| 2084 |
mpx_open_dvi_file(mpx); |
| 2085 |
@<Process the preamble@>; |
| 2086 |
mpx_open_mpxfile(mpx); |
| 2087 |
if (mpx->banner!=NULL) |
| 2088 |
fprintf (mpx->mpxfile,"%s\n",mpx->banner); |
| 2089 |
while ( true ) { |
| 2090 |
@<Advance to the next |bop| command@>; |
| 2091 |
for (k=0;k<=10;k++) |
| 2092 |
(void)mpx_signed_quad(mpx); |
| 2093 |
@<Do initialization required before starting a new page@>; |
| 2094 |
mpx_start_picture(mpx); |
| 2095 |
mpx_do_dvi_commands(mpx); |
| 2096 |
if ( mpx->stk_siz!=0 ) |
| 2097 |
bad_dvi("stack not empty at end of page"); |
| 2098 |
@.stack not empty...@> |
| 2099 |
mpx_stop_picture(mpx); |
| 2100 |
fprintf(mpx->mpxfile,"mpxbreak\n"); |
| 2101 |
} |
| 2102 |
if(mpx->dvi_file) |
| 2103 |
mpx_fclose(mpx,mpx->dvi_file); |
| 2104 |
if ( mpx->history<=mpx_cksum_trouble ) |
| 2105 |
return 0; |
| 2106 |
else |
| 2107 |
return mpx->history; |
| 2108 |
} |
| 2109 |
|
| 2110 |
@ The main program needs a few global variables in order to do its work. |
| 2111 |
|
| 2112 |
@<Glob...@>= |
| 2113 |
web_integer k;web_integer p; /* general purpose registers */ |
| 2114 |
web_integer numerator;web_integer denominator; /* stated conversion ratio */ |
| 2115 |
|
| 2116 |
@ @<Process the preamble@>= |
| 2117 |
{ |
| 2118 |
int p; |
| 2119 |
p=mpx_get_byte(mpx); /* fetch the first byte */ |
| 2120 |
if ( p!=pre ) |
| 2121 |
bad_dvi("First byte isn""t start of preamble!"); |
| 2122 |
@.First byte isn't...@> |
| 2123 |
p=mpx_get_byte(mpx); /* fetch the identification byte */ |
| 2124 |
if ( p!=id_byte ) |
| 2125 |
mpx_warn(mpx,"identification in byte 1 should be %d!", id_byte); |
| 2126 |
@.identification...should be n@> |
| 2127 |
@<Compute the conversion factor@>; |
| 2128 |
p=mpx_get_byte(mpx); /* fetch the length of the introductory comment */ |
| 2129 |
while (p>0 ){ |
| 2130 |
decr(p); |
| 2131 |
(void)mpx_get_byte(mpx); |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
@ The conversion factor |conv| is figured as follows: There are exactly |
| 2136 |
|n/d| decimicrons per \.{DVI} unit, and 254000 decimicrons per inch, |
| 2137 |
and |resolution| pixels per inch. Then we have to adjust this |
| 2138 |
by the stated amount of magnification. No such adjustment is needed for |
| 2139 |
|dvi_per_fix| since it is used to convert design sizes. |
| 2140 |
|
| 2141 |
@<Compute the conversion factor@>= |
| 2142 |
mpx->numerator=mpx_signed_quad(mpx); mpx->denominator=mpx_signed_quad(mpx); |
| 2143 |
if ( (mpx->numerator<=0)||(mpx->denominator<=0) ) |
| 2144 |
bad_dvi("bad scale ratio in preamble"); |
| 2145 |
@.bad scale ratio@> |
| 2146 |
mpx->mag=mpx_signed_quad(mpx)/1000.0; |
| 2147 |
if ( mpx->mag<=0.0 ) |
| 2148 |
bad_dvi("magnification isn't positive"); |
| 2149 |
@.magnification isn't positive@> |
| 2150 |
mpx->conv=(mpx->numerator/254000.0)*(72.0/mpx->denominator)*mpx->mag; |
| 2151 |
mpx->dvi_per_fix=(254000.0/mpx->numerator)*(mpx->denominator/72.27)/1048576.0; |
| 2152 |
|
| 2153 |
@ @<Advance to the next |bop| command@>= |
| 2154 |
do { |
| 2155 |
int p; |
| 2156 |
k=mpx_get_byte(mpx); |
| 2157 |
if ( (k>=fnt_def1)&&(k<fnt_def1+4) ){ |
| 2158 |
p=mpx_first_par(mpx, (unsigned int)k); |
| 2159 |
mpx_define_font(mpx, p); k=nop; |
| 2160 |
} |
| 2161 |
} while (k==nop); |
| 2162 |
if ( k==post ) |
| 2163 |
break; |
| 2164 |
if ( k!=bop ) |
| 2165 |
bad_dvi("missing bop"); |
| 2166 |
@.missing bop@> |
| 2167 |
|
| 2168 |
|
| 2169 |
@ Global filenames. |
| 2170 |
|
| 2171 |
@<Global...@>= |
| 2172 |
char *dviname; |
| 2173 |
|
| 2174 |
@* Color support. |
| 2175 |
These changes support \.{dvips}-style ``\.{color push NAME}'' and |
| 2176 |
``\.{color pop}'' specials. We store a list of named colors, sorted by |
| 2177 |
name, and decorate the relevant drawing commands with ``\.{withcolor |
| 2178 |
(r,g,b)}'' specifiers while a color is defined. |
| 2179 |
|
| 2180 |
@ A constant bounding the size of the named-color array. |
| 2181 |
|
| 2182 |
@d max_named_colors 100 /* maximum number of distinct named colors */ |
| 2183 |
|
| 2184 |
@ Then we declare a record for color types. |
| 2185 |
|
| 2186 |
@<Types...@>= |
| 2187 |
typedef struct named_color_record { |
| 2188 |
const char *name; /* color name */ |
| 2189 |
const char *value; /* text to pass to MetaPost */ |
| 2190 |
} named_color_record; |
| 2191 |
|
| 2192 |
@ Declare the named-color array itself. |
| 2193 |
|
| 2194 |
@<Globals@>= |
| 2195 |
named_color_record named_colors[(max_named_colors+1)]; |
| 2196 |
/* stores information about named colors, in sorted order by name */ |
| 2197 |
web_integer num_named_colors; /* number of elements of |named_colors| that are valid */ |
| 2198 |
|
| 2199 |
@ This function, used only during initialization, defines a named color. |
| 2200 |
|
| 2201 |
@c |
| 2202 |
static void mpx_def_named_color (MPX mpx, const char *n, const char *v) { |
| 2203 |
mpx->num_named_colors++; |
| 2204 |
assert(mpx->num_named_colors<max_named_colors); |
| 2205 |
mpx->named_colors[mpx->num_named_colors].name = n; |
| 2206 |
mpx->named_colors[mpx->num_named_colors].value = v; |
| 2207 |
} |
| 2208 |
|
| 2209 |
@ @<Declarations@>= |
| 2210 |
static void mpx_def_named_color (MPX mpx, const char *n, const char *v); |
| 2211 |
|
| 2212 |
@ During the initialization phase, we define values for all the named |
| 2213 |
colors defined in \.{colordvi.tex}. CMYK-to-RGB conversion by GhostScript. |
| 2214 |
|
| 2215 |
This list has to be sorted alphabetically! |
| 2216 |
|
| 2217 |
@<Set initial values@>= |
| 2218 |
mpx->num_named_colors = 0; |
| 2219 |
mpx_def_named_color(mpx, "Apricot", "(1.0, 0.680006, 0.480006)"); |
| 2220 |
mpx_def_named_color(mpx, "Aquamarine", "(0.180006, 1.0, 0.7)"); |
| 2221 |
mpx_def_named_color(mpx, "Bittersweet", "(0.760012, 0.0100122, 0.0)"); |
| 2222 |
mpx_def_named_color(mpx, "Black", "(0.0, 0.0, 0.0)"); |
| 2223 |
mpx_def_named_color(mpx, "Blue", "(0.0, 0.0, 1.0)"); |
| 2224 |
mpx_def_named_color(mpx, "BlueGreen", "(0.15, 1.0, 0.669994)"); |
| 2225 |
mpx_def_named_color(mpx, "BlueViolet", "(0.1, 0.05, 0.960012)"); |
| 2226 |
mpx_def_named_color(mpx, "BrickRed", "(0.719994, 0.0, 0.0)"); |
| 2227 |
mpx_def_named_color(mpx, "Brown", "(0.4, 0.0, 0.0)"); |
| 2228 |
mpx_def_named_color(mpx, "BurntOrange", "(1.0, 0.489988, 0.0)"); |
| 2229 |
mpx_def_named_color(mpx, "CadetBlue", "(0.380006, 0.430006, 0.769994)"); |
| 2230 |
mpx_def_named_color(mpx, "CarnationPink", "(1.0, 0.369994, 1.0)"); |
| 2231 |
mpx_def_named_color(mpx, "Cerulean", "(0.0600122, 0.889988, 1.0)"); |
| 2232 |
mpx_def_named_color(mpx, "CornflowerBlue", "(0.35, 0.869994, 1.0)"); |
| 2233 |
mpx_def_named_color(mpx, "Cyan", "(0.0, 1.0, 1.0)"); |
| 2234 |
mpx_def_named_color(mpx, "Dandelion", "(1.0, 0.710012, 0.160012)"); |
| 2235 |
mpx_def_named_color(mpx, "DarkOrchid", "(0.6, 0.2, 0.8)"); |
| 2236 |
mpx_def_named_color(mpx, "Emerald", "(0.0, 1.0, 0.5)"); |
| 2237 |
mpx_def_named_color(mpx, "ForestGreen", "(0.0, 0.880006, 0.0)"); |
| 2238 |
mpx_def_named_color(mpx, "Fuchsia", "(0.45, 0.00998169, 0.919994)"); |
| 2239 |
mpx_def_named_color(mpx, "Goldenrod", "(1.0, 0.9, 0.160012)"); |
| 2240 |
mpx_def_named_color(mpx, "Gray", "(0.5, 0.5, 0.5)"); |
| 2241 |
mpx_def_named_color(mpx, "Green", "(0.0, 1.0, 0.0)"); |
| 2242 |
mpx_def_named_color(mpx, "GreenYellow", "(0.85, 1.0, 0.310012)"); |
| 2243 |
mpx_def_named_color(mpx, "JungleGreen", "(0.0100122, 1.0, 0.480006)"); |
| 2244 |
mpx_def_named_color(mpx, "Lavender", "(1.0, 0.519994, 1.0)"); |
| 2245 |
mpx_def_named_color(mpx, "LimeGreen", "(0.5, 1.0, 0.0)"); |
| 2246 |
mpx_def_named_color(mpx, "Magenta", "(1.0, 0.0, 1.0)"); |
| 2247 |
mpx_def_named_color(mpx, "Mahogany", "(0.65, 0.0, 0.0)"); |
| 2248 |
mpx_def_named_color(mpx, "Maroon", "(0.680006, 0.0, 0.0)"); |
| 2249 |
mpx_def_named_color(mpx, "Melon", "(1.0, 0.539988, 0.5)"); |
| 2250 |
mpx_def_named_color(mpx, "MidnightBlue", "(0.0, 0.439988, 0.569994)"); |
| 2251 |
mpx_def_named_color(mpx, "Mulberry", "(0.640018, 0.0800061, 0.980006)"); |
| 2252 |
mpx_def_named_color(mpx, "NavyBlue", "(0.0600122, 0.460012, 1.0)"); |
| 2253 |
mpx_def_named_color(mpx, "OliveGreen", "(0.0, 0.6, 0.0)"); |
| 2254 |
mpx_def_named_color(mpx, "Orange", "(1.0, 0.389988, 0.130006)"); |
| 2255 |
mpx_def_named_color(mpx, "OrangeRed", "(1.0, 0.0, 0.5)"); |
| 2256 |
mpx_def_named_color(mpx, "Orchid", "(0.680006, 0.360012, 1.0)"); |
| 2257 |
mpx_def_named_color(mpx, "Peach", "(1.0, 0.5, 0.3)"); |
| 2258 |
mpx_def_named_color(mpx, "Periwinkle", "(0.430006, 0.45, 1.0)"); |
| 2259 |
mpx_def_named_color(mpx, "PineGreen", "(0.0, 0.75, 0.160012)"); |
| 2260 |
mpx_def_named_color(mpx, "Plum", "(0.5, 0.0, 1.0)"); |
| 2261 |
mpx_def_named_color(mpx, "ProcessBlue", "(0.0399878, 1.0, 1.0)"); |
| 2262 |
mpx_def_named_color(mpx, "Purple", "(0.55, 0.139988, 1.0)"); |
| 2263 |
mpx_def_named_color(mpx, "RawSienna", "(0.55, 0.0, 0.0)"); |
| 2264 |
mpx_def_named_color(mpx, "Red", "(1.0, 0.0, 0.0)"); |
| 2265 |
mpx_def_named_color(mpx, "RedOrange", "(1.0, 0.230006, 0.130006)"); |
| 2266 |
mpx_def_named_color(mpx, "RedViolet", "(0.590018, 0.0, 0.660012)"); |
| 2267 |
mpx_def_named_color(mpx, "Rhodamine", "(1.0, 0.180006, 1.0)"); |
| 2268 |
mpx_def_named_color(mpx, "RoyalBlue", "(0.0, 0.5, 1.0)"); |
| 2269 |
mpx_def_named_color(mpx, "RoyalPurple", "(0.25, 0.1, 1.0)"); |
| 2270 |
mpx_def_named_color(mpx, "RubineRed", "(1.0, 0.0, 0.869994)"); |
| 2271 |
mpx_def_named_color(mpx, "Salmon", "(1.0, 0.469994, 0.619994)"); |
| 2272 |
mpx_def_named_color(mpx, "SeaGreen", "(0.310012, 1.0, 0.5)"); |
| 2273 |
mpx_def_named_color(mpx, "Sepia", "(0.3, 0.0, 0.0)"); |
| 2274 |
mpx_def_named_color(mpx, "SkyBlue", "(0.380006, 1.0, 0.880006)"); |
| 2275 |
mpx_def_named_color(mpx, "SpringGreen", "(0.739988, 1.0, 0.239988)"); |
| 2276 |
mpx_def_named_color(mpx, "Tan", "(0.860012, 0.580006, 0.439988)"); |
| 2277 |
mpx_def_named_color(mpx, "TealBlue", "(0.119994, 0.980006, 0.640018)"); |
| 2278 |
mpx_def_named_color(mpx, "Thistle", "(0.880006, 0.410012, 1.0)"); |
| 2279 |
mpx_def_named_color(mpx, "Turquoise", "(0.15, 1.0, 0.8)"); |
| 2280 |
mpx_def_named_color(mpx, "Violet", "(0.210012, 0.119994, 1.0)"); |
| 2281 |
mpx_def_named_color(mpx, "VioletRed", "(1.0, 0.189988, 1.0)"); |
| 2282 |
mpx_def_named_color(mpx, "White", "(1.0, 1.0, 1.0)"); |
| 2283 |
mpx_def_named_color(mpx, "WildStrawberry", "(1.0, 0.0399878, 0.610012)"); |
| 2284 |
mpx_def_named_color(mpx, "Yellow", "(1.0, 1.0, 0.0)"); |
| 2285 |
mpx_def_named_color(mpx, "YellowGreen", "(0.560012, 1.0, 0.260012)"); |
| 2286 |
mpx_def_named_color(mpx, "YellowOrange", "(1.0, 0.580006, 0.0)"); |
| 2287 |
|
| 2288 |
@ Color commands get a separate warning procedure. |warn| sets |history := |
| 2289 |
mpx_warning_given|, which causes a nonzero exit status; but color errors are |
| 2290 |
trivial and should leave the exit status zero. |
| 2291 |
|
| 2292 |
@d color_warn(A) mpx_warn(mpx,A) |
| 2293 |
@d color_warn_two(A,B) mpx_warn(mpx,"%s%s",A,B) |
| 2294 |
|
| 2295 |
@ The |do_xxx| procedure handles DVI specials (defined with the |xxx1...xxx4| commands). |
| 2296 |
|
| 2297 |
@d XXX_BUF 256 |
| 2298 |
|
| 2299 |
@<Declare procedures to handle color commands@>= |
| 2300 |
static void mpx_do_xxx (MPX mpx, web_integer p) |
| 2301 |
{ |
| 2302 |
unsigned char buf[(XXX_BUF+1)]; /* FIXME: Fixed size buffer. */ |
| 2303 |
web_integer l, r, m, k, len; |
| 2304 |
boolean found; |
| 2305 |
int bufsiz = XXX_BUF; |
| 2306 |
len = 0; |
| 2307 |
while ( ( p > 0) && (len < bufsiz) ) { |
| 2308 |
buf[len] = (unsigned char)mpx_get_byte(mpx); |
| 2309 |
decr(p); incr(len); |
| 2310 |
} |
| 2311 |
@<Check whether |buf| contains a color command; if not, |goto XXXX|@>@; |
| 2312 |
if ( p > 0 ) { |
| 2313 |
color_warn("long \"color\" special ignored"); |
| 2314 |
goto XXXX; |
| 2315 |
} |
| 2316 |
if ( @<|buf| contains a color pop command@> ) { |
| 2317 |
@<Handle a color pop command@> |
| 2318 |
} else if ( @<|buf| contains a color push command@> ) { |
| 2319 |
@<Handle a color push command@> |
| 2320 |
} else { |
| 2321 |
color_warn("unknown \"color\" special ignored"); |
| 2322 |
goto XXXX; |
| 2323 |
} |
| 2324 |
XXXX: |
| 2325 |
for (k = 1;k<=p;k++) (void)mpx_get_byte(mpx); |
| 2326 |
} |
| 2327 |
|
| 2328 |
@ |
| 2329 |
|
| 2330 |
@<Check whether |buf| contains a color command; if not, |goto XXXX|@>= |
| 2331 |
if ( (len <= 5) |
| 2332 |
|| (buf[0] != 'c') |
| 2333 |
|| (buf[1] != 'o') |
| 2334 |
|| (buf[2] != 'l') |
| 2335 |
|| (buf[3] != 'o') |
| 2336 |
|| (buf[4] != 'r') |
| 2337 |
|| (buf[5] != ' ') |
| 2338 |
) goto XXXX; |
| 2339 |
|
| 2340 |
@ @<|buf| contains a color push command@>= |
| 2341 |
(len >= 11) && |
| 2342 |
(buf[6] == 'p') && |
| 2343 |
(buf[7] == 'u') && |
| 2344 |
(buf[8] == 's') && |
| 2345 |
(buf[9] == 'h') && |
| 2346 |
(buf[10] == ' ') |
| 2347 |
|
| 2348 |
@ @<|buf| contains a color pop command@>= |
| 2349 |
(len == 9) && |
| 2350 |
(buf[6] == 'p') && |
| 2351 |
(buf[7] == 'o') && |
| 2352 |
(buf[8] == 'p') |
| 2353 |
|
| 2354 |
@ The \.{color push} and \.{pop} commands imply a color stack, so we need a |
| 2355 |
global variable to hold that stack. |
| 2356 |
|
| 2357 |
@d max_color_stack_depth 10 /* maximum depth of saved color stack */ |
| 2358 |
|
| 2359 |
@ Here's the actual stack variables. |
| 2360 |
|
| 2361 |
@<Globals@>= |
| 2362 |
web_integer color_stack_depth; /* current depth of saved color stack */ |
| 2363 |
char *color_stack[(max_color_stack_depth+1)]; /* saved color stack */ |
| 2364 |
|
| 2365 |
@ Initialize the stack to empty. |
| 2366 |
|
| 2367 |
@<Set initial values@>= |
| 2368 |
mpx->color_stack_depth = 0; |
| 2369 |
|
| 2370 |
@ \.{color pop} just pops the stack. |
| 2371 |
|
| 2372 |
@<Handle a color pop command@>= |
| 2373 |
mpx_finish_last_char(mpx); |
| 2374 |
if (mpx->color_stack_depth > 0 ) { |
| 2375 |
free(mpx->color_stack[mpx->color_stack_depth]); |
| 2376 |
decr(mpx->color_stack_depth); |
| 2377 |
} else { |
| 2378 |
color_warn("color stack underflow"); |
| 2379 |
} |
| 2380 |
|
| 2381 |
@ \.{color push} pushes a color onto the stack. |
| 2382 |
|
| 2383 |
@<Handle a color push command@>= |
| 2384 |
mpx_finish_last_char(mpx); |
| 2385 |
if ( mpx->color_stack_depth >= max_color_stack_depth ) |
| 2386 |
mpx_abort(mpx,"color stack overflow"); |
| 2387 |
incr(mpx->color_stack_depth); |
| 2388 |
/* I don't know how to do string operations in Pascal. */ |
| 2389 |
/* Skip over extra spaces after 'color push'. */ |
| 2390 |
l = 11; |
| 2391 |
while ( (l < len - 1) && (buf[l] == ' ') ) incr(l); |
| 2392 |
if ( @<|buf[l]| contains an rgb command@> ) { |
| 2393 |
@<Handle a color push rgb command@> |
| 2394 |
} else if ( @<|buf[l]| contains a cmyk command@> ) { |
| 2395 |
@<Handle a color push cmyk command@> |
| 2396 |
} else if ( @<|buf[l]| contains a gray command@> ) { |
| 2397 |
@<Handle a color push gray command@> |
| 2398 |
} else { |
| 2399 |
@<Handle a named color push command@> |
| 2400 |
} |
| 2401 |
|
| 2402 |
@ @<|buf[l]| contains an rgb command@>= |
| 2403 |
(l + 4 < len) |
| 2404 |
&& (buf[l] == 'r') |
| 2405 |
&& (buf[l+1] == 'g') |
| 2406 |
&& (buf[l+2] == 'b') |
| 2407 |
&& (buf[l+3] == ' ') |
| 2408 |
|
| 2409 |
@ @<Handle a color push rgb command@>= |
| 2410 |
l = l + 4; |
| 2411 |
while ( (l < len) && (buf[l] == ' ') ) incr(l); /* Remove spaces at end of buf */ |
| 2412 |
while ( (len > l) && (buf[len - 1] == ' ') ) decr(len); |
| 2413 |
mpx->color_stack[mpx->color_stack_depth]=xmalloc((size_t)(len-l+3),1); |
| 2414 |
k = 0; |
| 2415 |
@<Copy |buf[l]| to |color_stack[color_stack_depth][k]| in tuple form@>@; |
| 2416 |
|
| 2417 |
@ @<|buf[l]| contains a gray command@>= |
| 2418 |
(l + 5 < len) |
| 2419 |
&& (buf[l] == 'g') |
| 2420 |
&& (buf[l+1] == 'r') |
| 2421 |
&& (buf[l+2] == 'a') |
| 2422 |
&& (buf[l+3] == 'y') |
| 2423 |
&& (buf[l+4] == ' ') |
| 2424 |
|
| 2425 |
@ @<Handle a color push gray command@>= |
| 2426 |
l = l + 5; |
| 2427 |
while ( (l < len) && (buf[l] == ' ') ) incr(l); /* Remove spaces at end of buf */ |
| 2428 |
while ( (len > l) && (buf[len - 1] == ' ') ) decr(len); |
| 2429 |
mpx->color_stack[mpx->color_stack_depth]=xmalloc((size_t)(len-l+9),1); |
| 2430 |
strcpy(mpx->color_stack[mpx->color_stack_depth],"white*"); |
| 2431 |
k = 6; |
| 2432 |
@<Copy |buf[l]| to |color_stack[color_stack_depth][k]| in tuple form@>@; |
| 2433 |
|
| 2434 |
@ @<|buf[l]| contains a cmyk command@>= |
| 2435 |
(l + 5 < len) |
| 2436 |
&& (buf[l] == 'c') |
| 2437 |
&& (buf[l+1] == 'm') |
| 2438 |
&& (buf[l+2] == 'y') |
| 2439 |
&& (buf[l+3] == 'k') |
| 2440 |
&& (buf[l+4] == ' ') |
| 2441 |
|
| 2442 |
@ @<Handle a color push cmyk command@>= |
| 2443 |
l = l + 5; |
| 2444 |
while ( (l < len) && (buf[l] == ' ') ) incr(l); |
| 2445 |
/* Remove spaces at end of buf */ |
| 2446 |
while ( (len > l) && (buf[len - 1] == ' ') ) decr(len); |
| 2447 |
mpx->color_stack[mpx->color_stack_depth]=xmalloc((size_t)(len-l+7),1); |
| 2448 |
strcpy(mpx->color_stack[mpx->color_stack_depth],"cmyk"); |
| 2449 |
k = 4; |
| 2450 |
@<Copy |buf[l]| to |color_stack[color_stack_depth][k]| in tuple form@>@; |
| 2451 |
|
| 2452 |
@ @<Copy |buf[l]| to |color_stack[color_stack_depth][k]| in tuple form@>= |
| 2453 |
mpx->color_stack[mpx->color_stack_depth][k] = '('; |
| 2454 |
incr(k); |
| 2455 |
while ( l < len ) { |
| 2456 |
if ( buf[l] == ' ' ) { |
| 2457 |
mpx->color_stack[mpx->color_stack_depth][k] = ','; |
| 2458 |
while ( (l < len) && (buf[l] == ' ') ) incr(l); |
| 2459 |
incr(k); |
| 2460 |
} else { |
| 2461 |
mpx->color_stack[mpx->color_stack_depth][k] = (char)buf[l]; |
| 2462 |
incr(l); |
| 2463 |
incr(k); |
| 2464 |
} |
| 2465 |
} |
| 2466 |
mpx->color_stack[mpx->color_stack_depth][k] = ')'; |
| 2467 |
mpx->color_stack[mpx->color_stack_depth][k+1] = 0; |
| 2468 |
|
| 2469 |
@ Binary-search the |named_colors| array, then push the found color onto |
| 2470 |
the stack. |
| 2471 |
|
| 2472 |
@<Handle a named color push command@>= |
| 2473 |
for (k = l;k<=len - 1;k++) { |
| 2474 |
buf[k - l] = xchr(buf[k]); |
| 2475 |
} |
| 2476 |
buf[len - l] = 0; |
| 2477 |
/* clang: never read: len = len - l; */ |
| 2478 |
l = 1; r = mpx->num_named_colors; |
| 2479 |
found = false; |
| 2480 |
while ( (l <= r) && ! found ) { |
| 2481 |
m = (l + r) / 2; k = strcmp((char *)(buf), mpx->named_colors[m].name); |
| 2482 |
if ( k == 0 ) { |
| 2483 |
mpx->color_stack[mpx->color_stack_depth]=xstrdup(mpx->named_colors[m].value); |
| 2484 |
found = true; |
| 2485 |
} else if ( k < 0 ) { |
| 2486 |
r = m - 1; |
| 2487 |
} else { |
| 2488 |
l = m + 1; |
| 2489 |
} |
| 2490 |
} |
| 2491 |
if (! found ) { |
| 2492 |
color_warn_two("non-hardcoded color \"%s\" in \"color push\" command", buf); |
| 2493 |
mpx->color_stack[mpx->color_stack_depth]=xstrdup((char *)(buf)); |
| 2494 |
} |
| 2495 |
|
| 2496 |
@ Last but not least, this code snippet prints a \.{withcolor} specifier |
| 2497 |
for the top of the color stack, if the stack is nonempty. |
| 2498 |
|
| 2499 |
@<Print a \.{withcolor} specifier if appropriate@>= |
| 2500 |
if ( mpx->color_stack_depth > 0 ) { |
| 2501 |
fprintf(mpx->mpxfile," withcolor %s\n",mpx->color_stack[mpx->color_stack_depth]); |
| 2502 |
} |
| 2503 |
|
| 2504 |
|
| 2505 |
@** Dmp. |
| 2506 |
|
| 2507 |
This program reads device-independent troff output files, |
| 2508 |
and converts them into a symbolic form understood by MetaPost. Some |
| 2509 |
of the code was borrowed from DVItoMP. It understands all the D? graphics |
| 2510 |
functions that dpost does but it ignores `x X' device control functions |
| 2511 |
such as `x X SetColor:...', `x X BeginPath:', and `x X DrawPath:...'. |
| 2512 |
|
| 2513 |
The output file is a sequence of MetaPost picture expressions, one for every |
| 2514 |
page in the input file. It makes no difference where the input file comes |
| 2515 |
from, but it is intended to process the result of running eqn and troff on |
| 2516 |
the output of MPtoTR. Such a file contains one page for every btex...etex |
| 2517 |
block in the original input. This program then creates a corresponding |
| 2518 |
sequence of MetaPost picture expressions for use as an auxiliary input file. |
| 2519 |
Since MetaPost expects such files to have the extension .mpx, the output |
| 2520 |
is sometimes called an `mpx' file. |
| 2521 |
|
| 2522 |
@d SHIFTS 100 /* maximum number of characters with special shifts */ |
| 2523 |
@d MAXCHARS 256 /* character codes fall in the range 0..MAXCHARS-1 */ |
| 2524 |
|
| 2525 |
@d is_specchar(c) (!mpx->gflag && (c)<=2) /* does charcode c identify a special char? */ |
| 2526 |
@d LWscale 0.03 /* line width for graphics as a fraction of pointsize */ |
| 2527 |
@d YCORR 12.0 /* V coordinate of reference point in (big) points */ |
| 2528 |
|
| 2529 |
@<Globals@>= |
| 2530 |
int next_specfnt[(max_fnums+1)]; /* used to link special fonts together */ |
| 2531 |
int shiftchar[SHIFTS]; /* charcode of character to shift, else -1 */ |
| 2532 |
float shifth[SHIFTS]; |
| 2533 |
float shiftv[SHIFTS]; /* shift vals/fontsize (y is upward) */ |
| 2534 |
int shiftptr; /* number of entries in shift tables */ |
| 2535 |
int shiftbase[(max_fnums+1)]; /* initial index into shifth,shiftv,shiftchar */ |
| 2536 |
int specfnt; /* int. num. of first special font (or FCOUNT) */ |
| 2537 |
int *specf_tail ; /* tail of specfnt list |(*specf_tail==FCOUNT)| */ |
| 2538 |
float cursize; /* current type size in (big) points */ |
| 2539 |
unsigned int curfont; /* internal number for current font */ |
| 2540 |
float Xslant; /* degrees additional slant for all fonts */ |
| 2541 |
float Xheight; /* yscale fonts to this height if nonzero */ |
| 2542 |
float sizescale; /* groff font size scaling factor */ |
| 2543 |
int gflag; /* non-zero if using groff fonts */ |
| 2544 |
float unit; /* (big) points per troff unit (0 when unset) */ |
| 2545 |
|
| 2546 |
@ @<Set initial...@>= |
| 2547 |
mpx->shiftptr = 0; |
| 2548 |
mpx->specfnt = (max_fnums+1); |
| 2549 |
mpx->specf_tail = &(mpx->specfnt); |
| 2550 |
mpx->unit = 0.0; |
| 2551 |
mpx->lnno = 0; /* this is a reset */ |
| 2552 |
mpx->gflag = 0; |
| 2553 |
mpx->h = 0; mpx->v = 0; |
| 2554 |
|
| 2555 |
@ @<Makempx header information@>= |
| 2556 |
typedef char *(*mpx_file_finder)(MPX, const char *, const char *, int); |
| 2557 |
enum mpx_filetype { |
| 2558 |
mpx_tfm_format, /* |kpse_tfm_format| */ |
| 2559 |
mpx_vf_format, /* |kpse_vf_format| */ |
| 2560 |
mpx_trfontmap_format, /* |kpse_mpsupport_format| */ |
| 2561 |
mpx_trcharadj_format, /* |kpse_mpsupport_format| */ |
| 2562 |
mpx_desc_format, /* |kpse_troff_font_format| */ |
| 2563 |
mpx_fontdesc_format, /* |kpse_troff_font_format| */ |
| 2564 |
mpx_specchar_format /* |kpse_mpsupport_format| */ |
| 2565 |
}; |
| 2566 |
|
| 2567 |
@ @<Globals@>= |
| 2568 |
mpx_file_finder find_file; |
| 2569 |
|
| 2570 |
@ @<Declarations@>= |
| 2571 |
static char *mpx_find_file (MPX mpx, const char *nam, const char *mode, int ftype); |
| 2572 |
|
| 2573 |
@ @c |
| 2574 |
static char *mpx_find_file (MPX mpx, const char *nam, const char *mode, int ftype) { |
| 2575 |
(void) mpx; |
| 2576 |
if (mode[0] != 'r' || (! access (nam,R_OK)) || ftype) { |
| 2577 |
return strdup(nam); |
| 2578 |
} |
| 2579 |
return NULL; |
| 2580 |
} |
| 2581 |
|
| 2582 |
@ @<Set initial...@>= |
| 2583 |
mpx->find_file = mpx_find_file; |
| 2584 |
|
| 2585 |
@ @<Declarations@>= |
| 2586 |
static FILE *mpx_fsearch(MPX mpx, const char *nam, int format); |
| 2587 |
|
| 2588 |
@ @c |
| 2589 |
static FILE *mpx_fsearch(MPX mpx, const char *nam, int format) { |
| 2590 |
FILE *f = NULL; |
| 2591 |
char *fname = (mpx->find_file)(mpx, nam, "r", format); |
| 2592 |
if (fname) { |
| 2593 |
f = fopen(fname, "rb"); |
| 2594 |
mpx_report(mpx,"%p = fopen(%s,\"rb\")",f, fname); |
| 2595 |
} |
| 2596 |
return f; |
| 2597 |
} |
| 2598 |
|
| 2599 |
@ Hash tables (or rather: AVL lists) |
| 2600 |
|
| 2601 |
@ @<Types...@>= |
| 2602 |
typedef struct { |
| 2603 |
char *name; |
| 2604 |
int num; |
| 2605 |
} avl_entry; |
| 2606 |
|
| 2607 |
@ @c |
| 2608 |
static int mpx_comp_name (void *p, const void *pa, const void *pb) { |
| 2609 |
(void)p; |
| 2610 |
return strcmp (((const avl_entry *) pa)->name, |
| 2611 |
((const avl_entry *) pb)->name); |
| 2612 |
} |
| 2613 |
static void *destroy_avl_entry (void *pa) { |
| 2614 |
avl_entry *p; |
| 2615 |
p = (avl_entry *) pa; |
| 2616 |
free (p->name); |
| 2617 |
free (p); |
| 2618 |
return NULL; |
| 2619 |
} |
| 2620 |
static void *copy_avl_entry (const void *pa) { /* never used */ |
| 2621 |
const avl_entry *p; |
| 2622 |
avl_entry *q; |
| 2623 |
p = (const avl_entry *) pa; |
| 2624 |
q = malloc(sizeof(avl_entry)); |
| 2625 |
if (q!=NULL) { |
| 2626 |
q->name = strdup(p->name); |
| 2627 |
q->num = p->num; |
| 2628 |
} |
| 2629 |
return (void *)q; |
| 2630 |
} |
| 2631 |
|
| 2632 |
|
| 2633 |
@ @c |
| 2634 |
static avl_tree mpx_avl_create (MPX mpx) { |
| 2635 |
avl_tree t; |
| 2636 |
t = avl_create(mpx_comp_name, |
| 2637 |
copy_avl_entry, |
| 2638 |
destroy_avl_entry, |
| 2639 |
malloc, free, NULL); |
| 2640 |
if (t==NULL) |
| 2641 |
mpx_abort(mpx, "Memory allocation failure"); |
| 2642 |
return t; |
| 2643 |
} |
| 2644 |
|
| 2645 |
@ The only two operations on AVL lists are finding already existing |
| 2646 |
items, or interning new items. Finding is handled by explicit |avl_find| |
| 2647 |
calls where needed, but it is wise to have a wrapper around |avl_probe| |
| 2648 |
to check for memory errors. |
| 2649 |
|
| 2650 |
@c |
| 2651 |
static void mpx_avl_probe(MPX mpx, avl_tree tab, avl_entry *p) { |
| 2652 |
avl_entry *r = (avl_entry *)avl_find(p, tab); |
| 2653 |
if (r==NULL) { |
| 2654 |
if (avl_ins (p, tab, avl_false)<0) |
| 2655 |
mpx_abort(mpx,"Memory allocation failure"); |
| 2656 |
} |
| 2657 |
} |
| 2658 |
|
| 2659 |
|
| 2660 |
@ Scanning Numbers |
| 2661 |
|
| 2662 |
The standard functions atoi(), atof(), and sscanf() provide ways of reading |
| 2663 |
numbers from strings but they give no indication of how much of the string |
| 2664 |
is consumed. These homemade versions don't parse scientific notation. |
| 2665 |
|
| 2666 |
@<Globals@>= |
| 2667 |
char *arg_tail; /* char after the number just gotten; NULL on failure */ |
| 2668 |
|
| 2669 |
@ @c |
| 2670 |
static int mpx_get_int(MPX mpx, char *s) { |
| 2671 |
register int i, d, neg; |
| 2672 |
if (s == NULL) |
| 2673 |
goto BAD; |
| 2674 |
for (neg = 0;; s++) { |
| 2675 |
if (*s == '-') |
| 2676 |
neg = !neg; |
| 2677 |
else if (*s != ' ' && *s != '\t') |
| 2678 |
break; |
| 2679 |
} |
| 2680 |
if (i = *s - '0', 0 > i || i > 9) |
| 2681 |
goto BAD; |
| 2682 |
while (d = *++s - '0', 0 <= d && d <= 9) |
| 2683 |
i = 10 * i + d; |
| 2684 |
mpx->arg_tail = s; |
| 2685 |
return neg ? -i : i; |
| 2686 |
BAD: |
| 2687 |
mpx->arg_tail = NULL; |
| 2688 |
return 0; |
| 2689 |
} |
| 2690 |
|
| 2691 |
@ GROFF font description files use octal character codes |
| 2692 |
|groff_font(5)|: The code can be any |web_integer|. If it starts with |
| 2693 |
a 0 it will be interpreted as octal; if it starts with 0x |
| 2694 |
or 0X it will be intepreted as hexadecimal. |
| 2695 |
|
| 2696 |
@c |
| 2697 |
static int mpx_get_int_map(MPX mpx, char *s) { |
| 2698 |
register int i; |
| 2699 |
if (s == NULL) |
| 2700 |
goto BAD; |
| 2701 |
i = (int)strtol(s, &(mpx->arg_tail), 0); |
| 2702 |
if (s == mpx->arg_tail) |
| 2703 |
goto BAD; |
| 2704 |
return i; |
| 2705 |
BAD: |
| 2706 |
mpx->arg_tail = NULL; |
| 2707 |
return 0; |
| 2708 |
} |
| 2709 |
|
| 2710 |
@ Troff output files contain few if any non-|web_integers|, but this program is |
| 2711 |
prepared to read floats whenever they seem reasonable; i.e., when the |
| 2712 |
number is not being used for character positioning. (For non-PostScript |
| 2713 |
applications h and v are usually in pixels and should be |web_integers|.) |
| 2714 |
|
| 2715 |
@c |
| 2716 |
static float mpx_get_float(MPX mpx, char *s) { |
| 2717 |
register int d, neg, digits; |
| 2718 |
register float x, y; |
| 2719 |
digits = 0; |
| 2720 |
neg = 0; x=0.0; |
| 2721 |
if (s != NULL) { |
| 2722 |
for (neg = 0;; s++) { |
| 2723 |
if (*s == '-') |
| 2724 |
neg = !neg; |
| 2725 |
else if (*s != ' ' && *s != '\t') |
| 2726 |
break; |
| 2727 |
} |
| 2728 |
x = 0.0; |
| 2729 |
while (d = *s - '0', 0 <= d && d <= 9) { |
| 2730 |
x = (float)10.0 * x + (float)d; |
| 2731 |
digits++; |
| 2732 |
s++; |
| 2733 |
} |
| 2734 |
if (*s == '.') { |
| 2735 |
y = 1.0; |
| 2736 |
while (d = *++s - '0', 0 <= d && d <= 9) { |
| 2737 |
y /= (float)10.0; |
| 2738 |
x += y * (float)d; |
| 2739 |
digits++; |
| 2740 |
} |
| 2741 |
} |
| 2742 |
} |
| 2743 |
if (digits == 0) { |
| 2744 |
mpx->arg_tail = NULL; |
| 2745 |
return 0.0; |
| 2746 |
} |
| 2747 |
mpx->arg_tail = s; |
| 2748 |
return neg ? -x : x; |
| 2749 |
} |
| 2750 |
|
| 2751 |
@ GROFF font description files have metrics field |
| 2752 |
of comma-separated |web_integers|. Traditional troff |
| 2753 |
have a float in this position. The value is not |
| 2754 |
used anyway - thus just skip the value, |
| 2755 |
eat all non-space chars. |
| 2756 |
|
| 2757 |
@c |
| 2758 |
static float mpx_get_float_map(MPX mpx, char *s) { |
| 2759 |
if (s != NULL) { |
| 2760 |
while (isspace((unsigned char)*s)) |
| 2761 |
s++; |
| 2762 |
while (!isspace((unsigned char)*s) && *s) |
| 2763 |
s++; |
| 2764 |
} |
| 2765 |
mpx->arg_tail = s; |
| 2766 |
return 0; |
| 2767 |
} |
| 2768 |
|
| 2769 |
|
| 2770 |
@ Reading Initialization Files |
| 2771 |
|
| 2772 |
Read the database file, reserve internal font numbers and set |
| 2773 |
the |font_name| entries. Each line in the database file contains |
| 2774 |
|<troff-name>\t,PostScript-name>\t<TeX-name>| |
| 2775 |
or just |
| 2776 |
|<troff-name>\t,PostScript-name>| |
| 2777 |
if the \TeX\ name matches the PostScript name. (|\t| means one or more tabs.) |
| 2778 |
|
| 2779 |
@<Globals@>= |
| 2780 |
avl_tree trfonts; |
| 2781 |
|
| 2782 |
@ @c |
| 2783 |
static void mpx_read_fmap(MPX mpx, const char *dbase) { |
| 2784 |
FILE *fin; |
| 2785 |
avl_entry *tmp; |
| 2786 |
char *nam; /* a font name being read */ |
| 2787 |
char *buf; |
| 2788 |
mpx->nfonts = 0; |
| 2789 |
fin = mpx_fsearch(mpx,dbase, mpx_trfontmap_format); |
| 2790 |
if (fin==NULL) |
| 2791 |
mpx_abort(mpx,"Cannot find %s", dbase); |
| 2792 |
|
| 2793 |
mpx->trfonts = mpx_avl_create (mpx); |
| 2794 |
while ((buf = mpx_getline(mpx,fin)) != NULL) { |
| 2795 |
if (mpx->nfonts == (max_fnums+1)) |
| 2796 |
mpx_abort(mpx,"Need to increase max_fnums"); |
| 2797 |
nam = buf; |
| 2798 |
while (*buf && *buf != '\t') |
| 2799 |
buf++; |
| 2800 |
if (nam==buf) |
| 2801 |
continue; |
| 2802 |
tmp = xmalloc(sizeof(avl_entry),1); |
| 2803 |
tmp->name = xmalloc (1,(size_t)(buf-nam)+1); |
| 2804 |
strncpy(tmp->name,nam,(unsigned int)(buf-nam)); |
| 2805 |
tmp->name[(buf-nam)] = '\0'; |
| 2806 |
tmp->num = (int)mpx->nfonts++; |
| 2807 |
assert(avl_ins (tmp, mpx->trfonts, avl_false) > 0); |
| 2808 |
if (*buf) { |
| 2809 |
buf++; |
| 2810 |
while (*buf == '\t') buf++; |
| 2811 |
while (*buf && *buf != '\t') buf++; /* skip over psname */ |
| 2812 |
while (*buf == '\t') buf++; |
| 2813 |
if (*buf) |
| 2814 |
nam = buf; |
| 2815 |
while (*buf) buf++; |
| 2816 |
} |
| 2817 |
mpx->font_name[tmp->num] = xstrdup(nam); |
| 2818 |
mpx->font_num[tmp->num] = -1; /* indicate font is not mounted */ |
| 2819 |
} |
| 2820 |
mpx_fclose(mpx,fin); |
| 2821 |
} |
| 2822 |
|
| 2823 |
|
| 2824 |
@ Some characters need their coordinates shifted in order to agree with |
| 2825 |
troff's view of the world. Logically, this information belongs in the |
| 2826 |
font description files but it actually resides in a PostScript prolog |
| 2827 |
that the troff output processor dpost reads. Since that file is in |
| 2828 |
PostScript and subject to change, we read the same information from |
| 2829 |
a small auxiliary file that gives shift amounts relative to the font |
| 2830 |
size with y upward. |
| 2831 |
|
| 2832 |
GROFF NOTE: |
| 2833 |
The PostScript prologue in GNU groff's font directory does not |
| 2834 |
contain any character shift information, so the following function |
| 2835 |
becomes redundant. Simply keeping an empty "trchars.adj" file |
| 2836 |
around will do fine without requiring any changes to this program. |
| 2837 |
|
| 2838 |
@c |
| 2839 |
static void mpx_read_char_adj(MPX mpx, const char *adjfile) { |
| 2840 |
FILE *fin; |
| 2841 |
char buf[200]; |
| 2842 |
avl_entry tmp, *p; |
| 2843 |
unsigned int i; |
| 2844 |
|
| 2845 |
fin = mpx_fsearch(mpx,adjfile, mpx_trcharadj_format); |
| 2846 |
if (fin==NULL) |
| 2847 |
mpx_abort(mpx,"Cannot find %s", adjfile); |
| 2848 |
|
| 2849 |
for (i = 0; i < mpx->nfonts; i++) |
| 2850 |
mpx->shiftbase[i] = 0; |
| 2851 |
while (fgets(buf, 200, fin) != NULL) { |
| 2852 |
if (mpx->shiftptr == SHIFTS - 1) |
| 2853 |
mpx_abort(mpx,"Need to increase SHIFTS"); |
| 2854 |
if (buf[0] != ' ' && buf[0] != '\t') { |
| 2855 |
for (i = 0; buf[i] != '\0'; i++) |
| 2856 |
if (buf[i] == '\n') |
| 2857 |
buf[i] = '\0'; |
| 2858 |
mpx->shiftchar[mpx->shiftptr++] = -1; |
| 2859 |
tmp.name = buf; |
| 2860 |
p = (avl_entry *)avl_find (&tmp, mpx->trfonts); |
| 2861 |
if (p==NULL) |
| 2862 |
mpx_abort(mpx,"%s refers to unknown font %s", adjfile, buf); |
| 2863 |
/* clang: dereference null pointer 'p' */ assert(p); |
| 2864 |
mpx->shiftbase[p->num] = mpx->shiftptr; |
| 2865 |
|
| 2866 |
} else { |
| 2867 |
mpx->shiftchar[mpx->shiftptr] = mpx_get_int(mpx,buf); |
| 2868 |
mpx->shifth[mpx->shiftptr] = mpx_get_float(mpx,mpx->arg_tail); |
| 2869 |
mpx->shiftv[mpx->shiftptr] = -mpx_get_float(mpx,mpx->arg_tail); |
| 2870 |
if (mpx->arg_tail == NULL) |
| 2871 |
mpx_abort(mpx,"Bad shift entry : \"%s\"", buf); |
| 2872 |
mpx->shiftptr++; |
| 2873 |
} |
| 2874 |
} |
| 2875 |
mpx->shiftchar[mpx->shiftptr++] = -1; |
| 2876 |
mpx_fclose(mpx,fin); |
| 2877 |
} |
| 2878 |
|
| 2879 |
@ Read the DESC file of the troff device to gather information |
| 2880 |
about sizescale and whether running under groff. |
| 2881 |
|
| 2882 |
Ignore all commands not specially handled. This relieves |
| 2883 |
of collecting commands without arguments here and also |
| 2884 |
makes the program more robust in case of future DESC |
| 2885 |
extensions. |
| 2886 |
|
| 2887 |
@c |
| 2888 |
static void mpx_read_desc(MPX mpx) { |
| 2889 |
const char *const k1[] = { |
| 2890 |
"res", "hor", "vert", "unitwidth", "paperwidth", |
| 2891 |
"paperlength", "biggestfont", "spare2", "encoding", |
| 2892 |
NULL |
| 2893 |
}; |
| 2894 |
const char *const g1[] = { |
| 2895 |
"family", "paperheight", "postpro", "prepro", |
| 2896 |
"print", "image_generator", "broken", |
| 2897 |
NULL |
| 2898 |
}; |
| 2899 |
char cmd[200]; |
| 2900 |
FILE *fp; |
| 2901 |
int i, n; |
| 2902 |
|
| 2903 |
fp = mpx_fsearch(mpx,"DESC", mpx_desc_format); |
| 2904 |
if (fp==NULL) |
| 2905 |
mpx_abort(mpx,"Cannot find DESC"); |
| 2906 |
while (fscanf(fp, "%199s", cmd) != EOF) { |
| 2907 |
if (*cmd == '#') { |
| 2908 |
while ((i = getc(fp)) != EOF && i != '\n'); |
| 2909 |
continue; |
| 2910 |
} |
| 2911 |
if (strcmp(cmd, "fonts") == 0) { |
| 2912 |
if (fscanf(fp, "%d", &n) != 1) |
| 2913 |
return; |
| 2914 |
for (i = 0; i < n; i++) |
| 2915 |
if (fscanf(fp, "%*s") == EOF) |
| 2916 |
return; |
| 2917 |
} else if (strcmp(cmd, "sizes") == 0) { |
| 2918 |
while (fscanf(fp, "%d", &n) == 1 && n != 0); |
| 2919 |
} else if (strcmp(cmd, "styles") == 0 || |
| 2920 |
strcmp(cmd, "papersize") == 0) { |
| 2921 |
mpx->gflag++; |
| 2922 |
while ((i = getc(fp)) != EOF && i != '\n'); |
| 2923 |
} else if (strcmp(cmd, "sizescale") == 0) { |
| 2924 |
if (fscanf(fp, "%d", &n) == 1) |
| 2925 |
mpx->sizescale = (float)n; |
| 2926 |
mpx->gflag++; |
| 2927 |
} else if (strcmp(cmd, "charset") == 0) { |
| 2928 |
return; |
| 2929 |
} else { |
| 2930 |
for (i = 0; k1[i]; i++) |
| 2931 |
if (strcmp(cmd, k1[i]) == 0) { |
| 2932 |
if (fscanf(fp, "%*s") == EOF) |
| 2933 |
return; |
| 2934 |
break; |
| 2935 |
} |
| 2936 |
if (k1[i] == 0) |
| 2937 |
for (i = 0; g1[i]; i++) |
| 2938 |
if (strcmp(cmd, g1[i]) == 0) { |
| 2939 |
if (fscanf(fp, "%*s") == EOF) |
| 2940 |
return; |
| 2941 |
mpx->gflag = 1; |
| 2942 |
break; |
| 2943 |
} |
| 2944 |
} |
| 2945 |
} |
| 2946 |
} |
| 2947 |
|
| 2948 |
|
| 2949 |
@ Given one line from the character description file for the font with |
| 2950 |
internal number f, save the appropriate data in the charcodes[f] table. |
| 2951 |
A return value of zero indicates a syntax error. |
| 2952 |
|
| 2953 |
GROFF: |
| 2954 |
GNU groff uses an extended font description file format documented |
| 2955 |
in |groff_font(5)|. In order to allow parsing of groff's font files, |
| 2956 |
this function needs to be rewritten as follows: |
| 2957 |
|
| 2958 |
\item{1.}The `metrics' field parsed by |mpx_get_float(lin);| may include |
| 2959 |
a comma-separated list of up to six decimal |web_integers| rather |
| 2960 |
than just a single floating-point number. |
| 2961 |
|
| 2962 |
\item{2.}The `charcode' field parsed by |lastcode = mpx_get_int(arg_tail);| |
| 2963 |
may be given either in decimal, octal, or hexadecimal format. |
| 2964 |
|
| 2965 |
@ @<Globals@>= |
| 2966 |
avl_tree charcodes[(max_fnums+1)]; /* hash tables for translating char names */ |
| 2967 |
|
| 2968 |
@ @c |
| 2969 |
static int mpx_scan_desc_line(MPX mpx, int f, char *lin) { |
| 2970 |
static int lastcode; |
| 2971 |
avl_entry *tmp; |
| 2972 |
char *s, *t; |
| 2973 |
t = lin; |
| 2974 |
while (*lin != ' ' && *lin != '\t' && *lin != '\0') |
| 2975 |
lin++; |
| 2976 |
if (lin==t) |
| 2977 |
return 1; |
| 2978 |
s = xmalloc((size_t)(lin-t+1),1); |
| 2979 |
strncpy(s,t,(size_t)(lin-t)); |
| 2980 |
*(s+(lin-t)) = '\0'; |
| 2981 |
while (*lin == ' ' || *lin == '\t') |
| 2982 |
lin++; |
| 2983 |
if (*lin == '"') { |
| 2984 |
if (lastcode < MAXCHARS) { |
| 2985 |
tmp = xmalloc(sizeof(avl_entry),1); |
| 2986 |
tmp->name = s ; |
| 2987 |
tmp->num = lastcode; |
| 2988 |
mpx_avl_probe (mpx, mpx->charcodes[f],tmp); |
| 2989 |
} |
| 2990 |
} else { |
| 2991 |
(void) mpx_get_float_map(mpx,lin); |
| 2992 |
(void) mpx_get_int(mpx,mpx->arg_tail); |
| 2993 |
lastcode = mpx_get_int_map(mpx,mpx->arg_tail); |
| 2994 |
if (mpx->arg_tail == NULL) |
| 2995 |
return 0; |
| 2996 |
if (lastcode < MAXCHARS) { |
| 2997 |
tmp = xmalloc(sizeof(avl_entry),1); |
| 2998 |
tmp->name = s ; |
| 2999 |
tmp->num = lastcode; |
| 3000 |
mpx_avl_probe (mpx, mpx->charcodes[f],tmp); |
| 3001 |
} |
| 3002 |
} |
| 3003 |
return 1; |
| 3004 |
} |
| 3005 |
|
| 3006 |
@ Read the font description file for the font with the given troff name |
| 3007 |
and update the data structures. The result is the internal font number. |
| 3008 |
|
| 3009 |
@c |
| 3010 |
static int mpx_read_fontdesc(MPX mpx, char *nam) { /* troff name */ |
| 3011 |
char buf[200]; |
| 3012 |
avl_entry tmp, *p; |
| 3013 |
FILE *fin; /* input file */ |
| 3014 |
int f; /* internal font number */ |
| 3015 |
|
| 3016 |
if (mpx->unit == 0.0) |
| 3017 |
mpx_abort(mpx, "Resolution is not set soon enough"); |
| 3018 |
tmp.name = nam; |
| 3019 |
p = (avl_entry *)avl_find (&tmp,mpx->trfonts); |
| 3020 |
if (p == NULL) |
| 3021 |
mpx_abort(mpx, "Font was not in map file"); |
| 3022 |
/* clang: dereference null pointer 'p' */ assert(p); |
| 3023 |
f = p->num; |
| 3024 |
fin = mpx_fsearch(mpx, nam, mpx_fontdesc_format); |
| 3025 |
if (fin==NULL) |
| 3026 |
mpx_abort(mpx,"Cannot find %s", nam); |
| 3027 |
for (;;) { |
| 3028 |
if (fgets(buf, 200, fin) == NULL) |
| 3029 |
mpx_abort(mpx, "Description file for %s ends unexpectedly", nam); |
| 3030 |
if (strncmp(buf, "special", 7) == 0) { |
| 3031 |
*(mpx->specf_tail) = f; |
| 3032 |
mpx->next_specfnt[f] = (max_fnums+1); |
| 3033 |
mpx->specf_tail = &(mpx->next_specfnt[f]); |
| 3034 |
} else if (strncmp(buf, "charset", 7) == 0) |
| 3035 |
break; |
| 3036 |
} |
| 3037 |
mpx->charcodes[f] = mpx_avl_create (mpx); |
| 3038 |
while (fgets(buf, 200, fin) != NULL) |
| 3039 |
if (mpx_scan_desc_line(mpx, f, buf) == 0) |
| 3040 |
mpx_abort(mpx, "%s has a bad line in its description file: %s", nam, buf); |
| 3041 |
mpx_fclose(mpx,fin); |
| 3042 |
return f; |
| 3043 |
} |
| 3044 |
|
| 3045 |
@ Page and Character Output |
| 3046 |
|
| 3047 |
@<Globals@>= |
| 3048 |
boolean graphics_used; /* nonzero if any graphics seen on this page */ |
| 3049 |
float dmp_str_h1; |
| 3050 |
float dmp_str_v; /* corrected start pos for current out string */ |
| 3051 |
float dmp_str_h2; /* where the current output string ends */ |
| 3052 |
float str_size; /* point size for this text string */ |
| 3053 |
|
| 3054 |
|
| 3055 |
@ Print any transformations required by the current Xslant and Xheight settings. |
| 3056 |
|
| 3057 |
@<Declarations@>= |
| 3058 |
static void mpx_slant_and_ht(MPX mpx); |
| 3059 |
|
| 3060 |
@ @c |
| 3061 |
static void mpx_slant_and_ht(MPX mpx) { |
| 3062 |
int i = 0; |
| 3063 |
fprintf(mpx->mpxfile, "("); |
| 3064 |
if (mpx->Xslant != 0.0) { |
| 3065 |
fprintf(mpx->mpxfile, " slanted%.5f", mpx->Xslant); |
| 3066 |
i++; |
| 3067 |
} |
| 3068 |
if (mpx->Xheight != mpx->cursize && mpx->Xheight != 0.0 && mpx->cursize != 0.0) { |
| 3069 |
fprintf(mpx->mpxfile, " yscaled%.4f", mpx->Xheight / mpx->cursize); |
| 3070 |
i++; |
| 3071 |
} |
| 3072 |
fprintf(mpx->mpxfile, ")"); |
| 3073 |
} |
| 3074 |
|
| 3075 |
|
| 3076 |
@ Output character number c in the font with internal number f. |
| 3077 |
|
| 3078 |
@c |
| 3079 |
static void mpx_set_num_char(MPX mpx, int f, int c) { |
| 3080 |
float hh, vv; /* corrected versions of h, v */ |
| 3081 |
int i; |
| 3082 |
|
| 3083 |
hh = (float)mpx->h; |
| 3084 |
vv = (float)mpx->v; |
| 3085 |
for (i = mpx->shiftbase[f]; mpx->shiftchar[i] >= 0 && i < SHIFTS; i++) |
| 3086 |
if (mpx->shiftchar[i] == c) { |
| 3087 |
hh += (mpx->cursize / mpx->unit) * mpx->shifth[i]; |
| 3088 |
vv += (mpx->cursize / mpx->unit) * mpx->shiftv[i]; |
| 3089 |
break; |
| 3090 |
} |
| 3091 |
if (hh - mpx->dmp_str_h2 >= 1.0 || mpx->dmp_str_h2 - hh >= 1.0 || |
| 3092 |
vv - mpx->dmp_str_v >= 1.0 || mpx->dmp_str_v - vv >= 1.0 || |
| 3093 |
f != mpx->str_f || mpx->cursize != mpx->str_size) { |
| 3094 |
if (mpx->str_f >= 0) |
| 3095 |
mpx_finish_last_char(mpx); |
| 3096 |
else if (!mpx->fonts_used) |
| 3097 |
mpx_prepare_font_use(mpx); /* first font usage on this page */ |
| 3098 |
if (!mpx->font_used[f]) |
| 3099 |
mpx_first_use(mpx,f); /* first use of font f on this page */ |
| 3100 |
fprintf(mpx->mpxfile, "_s(("); |
| 3101 |
mpx->print_col = 3; |
| 3102 |
mpx->str_f = f; |
| 3103 |
mpx->dmp_str_v = vv; |
| 3104 |
mpx->dmp_str_h1 = hh; |
| 3105 |
mpx->str_size = mpx->cursize; |
| 3106 |
} |
| 3107 |
mpx_print_char(mpx, (unsigned char)c); |
| 3108 |
mpx->dmp_str_h2 = hh + (float)char_width(f,c); |
| 3109 |
} |
| 3110 |
|
| 3111 |
@ Output a string. |
| 3112 |
|
| 3113 |
@c |
| 3114 |
static void mpx_set_string(MPX mpx, char *cname) { |
| 3115 |
float hh; /* corrected version of h, current horisontal position */ |
| 3116 |
|
| 3117 |
if (!*cname) |
| 3118 |
return; |
| 3119 |
hh = (float)mpx->h; |
| 3120 |
mpx_set_num_char(mpx,(int)mpx->curfont, *cname); |
| 3121 |
hh += (float)char_width(mpx->curfont,(int)*cname); |
| 3122 |
while (*++cname) { |
| 3123 |
mpx_print_char(mpx,(unsigned char)*cname); |
| 3124 |
hh += (float)char_width(mpx->curfont,(int)*cname); |
| 3125 |
} |
| 3126 |
mpx->h = (web_integer)floor(hh+0.5); |
| 3127 |
mpx_finish_last_char(mpx); |
| 3128 |
} |
| 3129 |
|
| 3130 |
@ Special Characters |
| 3131 |
|
| 3132 |
Given the troff name of a special character, this routine finds its |
| 3133 |
definition and copies it to the MPX file. It also finds the name of |
| 3134 |
the vardef macro and returns that name. The name should be C.<something>. |
| 3135 |
|
| 3136 |
@ |
| 3137 |
TH: A bit of trickery is added here for case-insensitive |
| 3138 |
file systems. This aliasing allows the CHARLIB directory |
| 3139 |
to exist on DVDs, for example. |
| 3140 |
It is a hack, I know. I've stuck to names on TeXLive. |
| 3141 |
|
| 3142 |
@d test_redo_search do { |
| 3143 |
if (deff==NULL) |
| 3144 |
deff = mpx_fsearch(mpx, cname, mpx_specchar_format); |
| 3145 |
} while (0) @; |
| 3146 |
|
| 3147 |
@c |
| 3148 |
static char *mpx_copy_spec_char(MPX mpx, char *cname) { |
| 3149 |
FILE *deff; |
| 3150 |
int c; |
| 3151 |
char *s, *t; |
| 3152 |
char specintro[] = "vardef "; /* MetaPost name follows this */ |
| 3153 |
unsigned k = 0; /* how much of specintro so far */ |
| 3154 |
if (strcmp(cname, "ao") == 0) { |
| 3155 |
deff = mpx_fsearch(mpx, "ao.x", mpx_specchar_format); |
| 3156 |
test_redo_search; |
| 3157 |
} else if (strcmp(cname, "lh") == 0) { |
| 3158 |
deff = mpx_fsearch(mpx, "lh.x", mpx_specchar_format); |
| 3159 |
test_redo_search; |
| 3160 |
} else if (strcmp(cname, "~=") == 0) { |
| 3161 |
deff = mpx_fsearch(mpx, "twiddle", mpx_specchar_format); |
| 3162 |
test_redo_search; |
| 3163 |
} else { |
| 3164 |
deff = mpx_fsearch(mpx, cname, mpx_specchar_format); |
| 3165 |
} |
| 3166 |
if (deff==NULL) |
| 3167 |
mpx_abort(mpx, "No vardef in charlib/%s", cname); |
| 3168 |
|
| 3169 |
while (k < (unsigned)strlen(specintro)) { |
| 3170 |
if ((c = getc(deff)) == EOF) |
| 3171 |
mpx_abort(mpx, "No vardef in charlib/%s", cname); |
| 3172 |
putc(c, mpx->mpxfile); |
| 3173 |
if (c == specintro[k]) |
| 3174 |
k++; |
| 3175 |
else |
| 3176 |
k = 0; |
| 3177 |
} |
| 3178 |
s = xmalloc(mpx->bufsize,1); |
| 3179 |
t = s ; |
| 3180 |
while ((c = getc(deff)) != '(') { |
| 3181 |
if (c == EOF) |
| 3182 |
mpx_abort(mpx, "vardef in charlib/%s has no arguments", cname); |
| 3183 |
putc(c, mpx->mpxfile); |
| 3184 |
*t++ = (char)c; |
| 3185 |
} |
| 3186 |
putc(c, mpx->mpxfile); |
| 3187 |
*t++ = '\0'; |
| 3188 |
while ((c = getc(deff)) != EOF); |
| 3189 |
putc(c, mpx->mpxfile); |
| 3190 |
return s; |
| 3191 |
} |
| 3192 |
|
| 3193 |
|
| 3194 |
@ When given a character name instead of a number, we need to check if |
| 3195 |
it is a special character and download the definition if necessary. |
| 3196 |
If the character is not in the current font we have to search the special |
| 3197 |
fonts. |
| 3198 |
|
| 3199 |
@<Globals@>= |
| 3200 |
avl_tree spec_tab; |
| 3201 |
|
| 3202 |
@ The |spec_tab| avl table combines character names with macro names. |
| 3203 |
|
| 3204 |
@<Types...@>= |
| 3205 |
typedef struct { |
| 3206 |
char *name; |
| 3207 |
char *mac; |
| 3208 |
} spec_entry; |
| 3209 |
|
| 3210 |
@ @c |
| 3211 |
static void mpx_set_char(MPX mpx, char *cname) { |
| 3212 |
int f, c; |
| 3213 |
avl_entry tmp, *p; |
| 3214 |
spec_entry *sp; |
| 3215 |
|
| 3216 |
if (*cname == ' ' || *cname == '\t') |
| 3217 |
return; |
| 3218 |
f = (int)mpx->curfont; |
| 3219 |
tmp.name = cname; |
| 3220 |
p = avl_find(&tmp, mpx->charcodes[f]); |
| 3221 |
if (p==NULL) { |
| 3222 |
for (f = mpx->specfnt; f != (max_fnums+1); f = mpx->next_specfnt[f]) { |
| 3223 |
p = avl_find(&tmp, mpx->charcodes[f]); |
| 3224 |
if (p!=NULL) |
| 3225 |
goto OUT_LABEL; |
| 3226 |
} |
| 3227 |
mpx_abort(mpx, "There is no character %s", cname); |
| 3228 |
} |
| 3229 |
OUT_LABEL: |
| 3230 |
/* clang: dereference null pointer 'p' */ assert(p); |
| 3231 |
c = p->num; |
| 3232 |
if (!is_specchar(c)) { |
| 3233 |
mpx_set_num_char(mpx, f, c); |
| 3234 |
} else { |
| 3235 |
if (mpx->str_f >= 0) |
| 3236 |
mpx_finish_last_char(mpx); |
| 3237 |
if (!mpx->fonts_used) |
| 3238 |
mpx_prepare_font_use(mpx); |
| 3239 |
if (!mpx->font_used[f]) |
| 3240 |
mpx_first_use(mpx, f); |
| 3241 |
if (mpx->spec_tab) |
| 3242 |
mpx->spec_tab = mpx_avl_create (mpx); |
| 3243 |
sp = xmalloc(sizeof(spec_entry),1); |
| 3244 |
sp->name = cname; |
| 3245 |
sp->mac = NULL; |
| 3246 |
{ |
| 3247 |
spec_entry *r = (spec_entry *)avl_find(sp, mpx->spec_tab); |
| 3248 |
if (r==NULL) { |
| 3249 |
if (avl_ins (sp, mpx->spec_tab, avl_false)<0) |
| 3250 |
mpx_abort(mpx,"Memory allocation failure"); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
if (sp->mac == NULL) { |
| 3254 |
sp->mac = mpx_copy_spec_char(mpx, cname); /* this won't be NULL */ |
| 3255 |
} |
| 3256 |
fprintf(mpx->mpxfile, "_s(%s(_n%d)", sp->mac,f); |
| 3257 |
fprintf(mpx->mpxfile, ",%.5f,%.4f,%.4f)", |
| 3258 |
(mpx->cursize/mpx->font_design_size[f])*1.00375, |
| 3259 |
(double)(((float)mpx->h*mpx->unit)/100.0), YCORR-(float)mpx->v*mpx->unit); |
| 3260 |
mpx_slant_and_ht(mpx); |
| 3261 |
fprintf(mpx->mpxfile, ";\n"); |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
@ Font Definitions |
| 3266 |
|
| 3267 |
Mount the font with troff name nam at external font number n and read any |
| 3268 |
necessary font files. |
| 3269 |
|
| 3270 |
@c |
| 3271 |
static void mpx_do_font_def(MPX mpx, int n, char *nam) { |
| 3272 |
int f; |
| 3273 |
unsigned k; |
| 3274 |
avl_entry tmp, *p; |
| 3275 |
tmp.name = nam; |
| 3276 |
p = (avl_entry *) avl_find (&tmp, mpx->trfonts); |
| 3277 |
if (p==NULL) |
| 3278 |
mpx_abort(mpx, "Font %s was not in map file", nam); |
| 3279 |
/* clang: dereference null pointer 'p' */ assert(p); |
| 3280 |
f = p->num; |
| 3281 |
if ( mpx->charcodes[f] == NULL) { |
| 3282 |
mpx_read_fontdesc(mpx, nam); |
| 3283 |
mpx->cur_name = xstrdup(mpx->font_name[f]); |
| 3284 |
if (! mpx_open_tfm_file(mpx) ) |
| 3285 |
font_abort("No TFM file found for ",f); |
| 3286 |
@.no TFM file found@> |
| 3287 |
mpx_in_TFM(mpx, f); |
| 3288 |
} |
| 3289 |
for (k = 0; k < mpx->nfonts; k++) |
| 3290 |
if (mpx->font_num[k] == n) |
| 3291 |
mpx->font_num[k] = -1; |
| 3292 |
mpx->font_num[f] = n; |
| 3293 |
@<Do any other initialization required for the new font |f|@>; |
| 3294 |
} |
| 3295 |
|
| 3296 |
|
| 3297 |
|
| 3298 |
@ Time on `makepath pencircle' |
| 3299 |
|
| 3300 |
Given the control points of a cubic Bernstein polynomial, evaluate it at t. |
| 3301 |
|
| 3302 |
@d Speed ((float) (PI/4.0)) |
| 3303 |
|
| 3304 |
@c |
| 3305 |
static float mpx_b_eval(const float *xx, float t) { |
| 3306 |
float zz[4]; |
| 3307 |
register int i, j; |
| 3308 |
for (i = 0; i <= 3; i++) |
| 3309 |
zz[i] = xx[i]; |
| 3310 |
for (i = 3; i > 0; i--) |
| 3311 |
for (j = 0; j < i; j++) |
| 3312 |
zz[j] += t * (zz[j + 1] - zz[j]); |
| 3313 |
return zz[0]; |
| 3314 |
} |
| 3315 |
|
| 3316 |
|
| 3317 |
@ Find the direction angle at time t on the path `makepath pencircle'. |
| 3318 |
The tables below give the Bezier control points for MetaPost's cubic |
| 3319 |
approximation to the first octant of a unit circle. |
| 3320 |
|
| 3321 |
@c |
| 3322 |
static const float xx[] = { 1.0, 1.0, (float)0.8946431597, (float)0.7071067812 }; |
| 3323 |
static const float yy[] = { 0.0, (float)0.2652164899, (float)0.5195704026, (float)0.7071067812 }; |
| 3324 |
|
| 3325 |
@ @c |
| 3326 |
static float mpx_circangle(float t) { |
| 3327 |
float ti; |
| 3328 |
ti = (float)floor(t); |
| 3329 |
t -= ti; |
| 3330 |
return (float) atan(mpx_b_eval(yy, t) / |
| 3331 |
mpx_b_eval(xx, t)) + ti * Speed; |
| 3332 |
} |
| 3333 |
|
| 3334 |
|
| 3335 |
@ Find the spline parameter where `makepath pencircle' comes closest to |
| 3336 |
(cos(a)/2,sin(a)/2). |
| 3337 |
|
| 3338 |
@c |
| 3339 |
static float mpx_circtime(float a) { |
| 3340 |
int i; |
| 3341 |
float t; |
| 3342 |
t = a / Speed; |
| 3343 |
for (i = 2; --i >= 0;) |
| 3344 |
t += (a - mpx_circangle(t)) / Speed; |
| 3345 |
return t; |
| 3346 |
} |
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
@ Troff Graphics |
| 3351 |
|
| 3352 |
@<Globals@>= |
| 3353 |
float gx; |
| 3354 |
float gy; /* current point for graphics (init. (h,YCORR/mpx->unit-v) */ |
| 3355 |
|
| 3356 |
@ @c |
| 3357 |
static void mpx_prepare_graphics(MPX mpx) { |
| 3358 |
|
| 3359 |
fprintf(mpx->mpxfile, "vardef _D(expr _d)expr _q =\n"); |
| 3360 |
fprintf(mpx->mpxfile, |
| 3361 |
" addto _p doublepath _q withpen pencircle scaled _d; enddef;\n"); |
| 3362 |
mpx->graphics_used = true; |
| 3363 |
} |
| 3364 |
|
| 3365 |
|
| 3366 |
@ This function prints the current position (gx,gy). Then if it can read dh dv |
| 3367 |
from string s, it increments (gx,gy) and prints "--". By returning the rest |
| 3368 |
of the string s or NULL if nothing could be read from s, it provides the |
| 3369 |
argument for the next iteration. |
| 3370 |
|
| 3371 |
@c |
| 3372 |
static char *mpx_do_line(MPX mpx, char *s) { |
| 3373 |
float dh, dv; |
| 3374 |
|
| 3375 |
fprintf(mpx->mpxfile, "(%.3f,%.3f)", mpx->gx * mpx->unit, mpx->gy * mpx->unit); |
| 3376 |
dh = mpx_get_float(mpx, s); |
| 3377 |
dv = mpx_get_float(mpx, mpx->arg_tail); |
| 3378 |
if (mpx->arg_tail == NULL) |
| 3379 |
return NULL; |
| 3380 |
mpx->gx += dh; |
| 3381 |
mpx->gy -= dv; |
| 3382 |
fprintf(mpx->mpxfile, "--\n"); |
| 3383 |
return mpx->arg_tail; |
| 3384 |
} |
| 3385 |
|
| 3386 |
|
| 3387 |
@ Function |spline_seg()| reads two pairs of (dh,dv) increments and prints the |
| 3388 |
corresponding quadratic B-spline segment, leaving the ending point to be |
| 3389 |
printed next time. The return value is the string with the first (dh,dv) |
| 3390 |
pair lopped off. If only one pair of increments is found, we prepare to |
| 3391 |
terminate the iteration by printing last time's ending point and returning |
| 3392 |
NULL. |
| 3393 |
|
| 3394 |
@c |
| 3395 |
static char * mpx_spline_seg(MPX mpx, char *s) { |
| 3396 |
float dh1, dv1, dh2, dv2; |
| 3397 |
|
| 3398 |
dh1 = mpx_get_float(mpx, s); |
| 3399 |
dv1 = mpx_get_float(mpx, mpx->arg_tail); |
| 3400 |
if (mpx->arg_tail == NULL) |
| 3401 |
mpx_abort(mpx, "Missing spline increments"); |
| 3402 |
s = mpx->arg_tail; |
| 3403 |
fprintf(mpx->mpxfile, "(%.3f,%.3f)", (mpx->gx + .5 * dh1) * mpx->unit, |
| 3404 |
(mpx->gy - .5 * dv1) * mpx->unit); |
| 3405 |
mpx->gx += dh1; |
| 3406 |
mpx->gy -= dv1; |
| 3407 |
dh2 = mpx_get_float(mpx, s); |
| 3408 |
dv2 = mpx_get_float(mpx, mpx->arg_tail); |
| 3409 |
if (mpx->arg_tail == NULL) |
| 3410 |
return NULL; |
| 3411 |
fprintf(mpx->mpxfile, "..\ncontrols (%.3f,%.3f) and (%.3f,%.3f)..\n", |
| 3412 |
(mpx->gx - dh1 / 6.0) * mpx->unit, (mpx->gy + dv1 / 6.0) * mpx->unit, |
| 3413 |
(mpx->gx + dh2 / 6.0) * mpx->unit, (mpx->gy - dv2 / 6.0) * mpx->unit); |
| 3414 |
return s; |
| 3415 |
} |
| 3416 |
|
| 3417 |
|
| 3418 |
@ Draw an ellipse with the given major and minor axes. |
| 3419 |
|
| 3420 |
@c |
| 3421 |
static void mpx_do_ellipse(MPX mpx, float a, float b) { |
| 3422 |
|
| 3423 |
fprintf(mpx->mpxfile, "makepath(pencircle xscaled %.3f\n yscaled %.3f", |
| 3424 |
a * mpx->unit, b * mpx->unit); |
| 3425 |
fprintf(mpx->mpxfile, " shifted (%.3f,%.3f));\n", (mpx->gx + .5 * a) * mpx->unit, |
| 3426 |
mpx->gy * mpx->unit); |
| 3427 |
mpx->gx += a; |
| 3428 |
} |
| 3429 |
|
| 3430 |
|
| 3431 |
@ Draw a counter-clockwise arc centered at (cx,cy) with initial and final radii |
| 3432 |
(ax,ay) and (bx,by) respectively. |
| 3433 |
|
| 3434 |
@c |
| 3435 |
static |
| 3436 |
void mpx_do_arc(MPX mpx, float cx, float cy, float ax, float ay, float bx, float by) { |
| 3437 |
float t1, t2; |
| 3438 |
|
| 3439 |
t1 = mpx_circtime((float)atan2(ay, ax)); |
| 3440 |
t2 = mpx_circtime((float)atan2(by, bx)); |
| 3441 |
if (t2 < t1) |
| 3442 |
t2 += (float)8.0; |
| 3443 |
fprintf(mpx->mpxfile, "subpath (%.5f,%.5f) of\n", t1, t2); |
| 3444 |
fprintf(mpx->mpxfile, |
| 3445 |
" makepath(pencircle scaled %.3f shifted (%.3f,%.3f));\n", |
| 3446 |
2.0 * sqrt(ax * ax + ay * ay) * mpx->unit, cx * mpx->unit, cy * mpx->unit); |
| 3447 |
mpx->gx = cx + bx; |
| 3448 |
mpx->gy = cy + by; |
| 3449 |
} |
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
@ String s is everything following the initial `D' in a troff graphics command. |
| 3454 |
|
| 3455 |
@c |
| 3456 |
static void mpx_do_graphic(MPX mpx, char *s) { |
| 3457 |
float h1, v1, h2, v2; |
| 3458 |
|
| 3459 |
mpx_finish_last_char(mpx); |
| 3460 |
/* GROFF uses Fd to set fill color for solid drawing objects to the |
| 3461 |
default, so just ignore that. |
| 3462 |
*/ |
| 3463 |
if (s[0] == 'F' && s[1] == 'd') |
| 3464 |
return; |
| 3465 |
mpx->gx = (float) mpx->h; |
| 3466 |
mpx->gy = (float)YCORR / mpx->unit - ((float) mpx->v); |
| 3467 |
if (!mpx->graphics_used) |
| 3468 |
mpx_prepare_graphics(mpx); |
| 3469 |
fprintf(mpx->mpxfile, "D(%.4f) ", LWscale * mpx->cursize); |
| 3470 |
switch (*s++) { |
| 3471 |
case 'c': |
| 3472 |
h1 = mpx_get_float(mpx,s); |
| 3473 |
if (mpx->arg_tail == NULL) |
| 3474 |
mpx_abort(mpx,"Bad argument in %s", s-2); |
| 3475 |
mpx_do_ellipse(mpx,h1, h1); |
| 3476 |
break; |
| 3477 |
case 'e': |
| 3478 |
h1 = mpx_get_float(mpx,s); |
| 3479 |
v1 = mpx_get_float(mpx,mpx->arg_tail); |
| 3480 |
if (mpx->arg_tail == NULL) |
| 3481 |
mpx_abort(mpx,"Bad argument in %s", s - 2); |
| 3482 |
mpx_do_ellipse(mpx,h1, v1); |
| 3483 |
break; |
| 3484 |
case 'A': |
| 3485 |
fprintf(mpx->mpxfile, "reverse "); |
| 3486 |
/* fall through */ |
| 3487 |
case 'a': |
| 3488 |
h1 = mpx_get_float(mpx,s); |
| 3489 |
v1 = mpx_get_float(mpx,mpx->arg_tail); |
| 3490 |
h2 = mpx_get_float(mpx,mpx->arg_tail); |
| 3491 |
v2 = mpx_get_float(mpx,mpx->arg_tail); |
| 3492 |
if (mpx->arg_tail == NULL) |
| 3493 |
mpx_abort(mpx,"Bad argument in %s", s - 2); |
| 3494 |
mpx_do_arc(mpx,mpx->gx + h1, mpx->gy - v1, -h1, v1, h2, -v2); |
| 3495 |
break; |
| 3496 |
case 'l': |
| 3497 |
case 'p': |
| 3498 |
while (s != NULL) |
| 3499 |
s = mpx_do_line(mpx,s); |
| 3500 |
fprintf(mpx->mpxfile, ";\n"); |
| 3501 |
break; |
| 3502 |
case 'q': |
| 3503 |
do |
| 3504 |
s = mpx_spline_seg(mpx,s); |
| 3505 |
while (s != NULL); |
| 3506 |
fprintf(mpx->mpxfile, ";\n"); |
| 3507 |
break; |
| 3508 |
case '~': |
| 3509 |
fprintf(mpx->mpxfile, "(%.3f,%.3f)--", mpx->gx * mpx->unit, mpx->gy * mpx->unit); |
| 3510 |
do |
| 3511 |
s = mpx_spline_seg(mpx,s); |
| 3512 |
while (s != NULL); |
| 3513 |
fprintf(mpx->mpxfile, "--(%.3f,%.3f);\n", mpx->gx * mpx->unit, mpx->gy * mpx->unit); |
| 3514 |
break; |
| 3515 |
default: |
| 3516 |
mpx_abort(mpx,"Unknown drawing function %s", s - 2); |
| 3517 |
} |
| 3518 |
mpx->h = (int) floor(mpx->gx + .5); |
| 3519 |
mpx->v = (int) floor(YCORR / mpx->unit + .5 - mpx->gy); |
| 3520 |
} |
| 3521 |
|
| 3522 |
|
| 3523 |
|
| 3524 |
@ Interpreting Troff Output |
| 3525 |
|
| 3526 |
@c |
| 3527 |
static void mpx_change_font(MPX mpx, int f) { |
| 3528 |
for (mpx->curfont = 0; mpx->curfont < mpx->nfonts; mpx->curfont++) |
| 3529 |
if (mpx->font_num[mpx->curfont] == f) |
| 3530 |
return; |
| 3531 |
mpx_abort(mpx,"Bad font setting"); |
| 3532 |
} |
| 3533 |
|
| 3534 |
|
| 3535 |
@ String s0 is everything following the initial `x' in a troff device control |
| 3536 |
command. A zero result indicates a stop command. |
| 3537 |
|
| 3538 |
@c |
| 3539 |
static int mpx_do_x_cmd(MPX mpx, char *s0) |
| 3540 |
{ |
| 3541 |
float x; |
| 3542 |
int n; |
| 3543 |
char *s; |
| 3544 |
|
| 3545 |
s = s0; |
| 3546 |
while (*s == ' ' || *s == '\t') |
| 3547 |
s++; |
| 3548 |
switch (*s++) { |
| 3549 |
case 'r': |
| 3550 |
if (mpx->unit != 0.0) |
| 3551 |
mpx_abort(mpx,"Attempt to reset resolution"); |
| 3552 |
while (*s != ' ' && *s != '\t') |
| 3553 |
s++; |
| 3554 |
mpx->unit = mpx_get_float(mpx,s); |
| 3555 |
if (mpx->unit <= 0.0) |
| 3556 |
mpx_abort(mpx,"Bad resolution: x %s", s0); |
| 3557 |
mpx->unit = (float)72.0 / mpx->unit; |
| 3558 |
break; |
| 3559 |
case 'f': |
| 3560 |
while (*s != ' ' && *s != '\t') |
| 3561 |
s++; |
| 3562 |
n = mpx_get_int(mpx,s); |
| 3563 |
if (mpx->arg_tail == NULL) |
| 3564 |
mpx_abort(mpx,"Bad font def: x %s", s0); |
| 3565 |
s = mpx->arg_tail; |
| 3566 |
while (*s == ' ' || *s == '\t') |
| 3567 |
s++; |
| 3568 |
mpx_do_font_def(mpx,n, s); |
| 3569 |
break; |
| 3570 |
case 's': |
| 3571 |
return 0; |
| 3572 |
case 'H': |
| 3573 |
while (*s != ' ' && *s != '\t') |
| 3574 |
s++; |
| 3575 |
mpx->Xheight = mpx_get_float(mpx,s); |
| 3576 |
/* GROFF troff output is scaled |groff_out(5)|: |
| 3577 |
The argument to the s command is in scaled |
| 3578 |
points (units of points/n, where n is the argument |
| 3579 |
to the sizescale command in the DESC file.) The |
| 3580 |
argument to the x Height command is also in scaled points. |
| 3581 |
sizescale for groff devps is 1000 |
| 3582 |
*/ |
| 3583 |
if (mpx->sizescale != 0.0) { |
| 3584 |
if (mpx->unit != 0.0) |
| 3585 |
mpx->Xheight *= mpx->unit; /* ??? */ |
| 3586 |
else |
| 3587 |
mpx->Xheight /= mpx->sizescale; |
| 3588 |
} |
| 3589 |
if (mpx->Xheight == mpx->cursize) |
| 3590 |
mpx->Xheight = 0.0; |
| 3591 |
break; |
| 3592 |
case 'S': |
| 3593 |
while (*s != ' ' && *s != '\t') |
| 3594 |
s++; |
| 3595 |
mpx->Xslant = mpx_get_float(mpx,s) * ((float)PI / (float)180.0); |
| 3596 |
x = (float)cos(mpx->Xslant); |
| 3597 |
if (-1e-4 < x && x < 1e-4) |
| 3598 |
mpx_abort(mpx,"Excessive slant"); |
| 3599 |
mpx->Xslant = (float)sin(mpx->Xslant) / x; |
| 3600 |
break; |
| 3601 |
default: |
| 3602 |
/* do nothing */ ; |
| 3603 |
} |
| 3604 |
return 1; |
| 3605 |
} |
| 3606 |
|
| 3607 |
|
| 3608 |
@ This routine reads commands from the troff output file up to and including |
| 3609 |
the next `p' or `x s' command. It also calls |set_num_char()| and |set_char()| |
| 3610 |
to generate output when appropriate. A zero result indicates that there |
| 3611 |
are no more pages to do. |
| 3612 |
|
| 3613 |
GROFF: |
| 3614 |
GNU groff uses an extended device-independent output file format |
| 3615 |
documented in |groff_out(5)|. In order to allow parsing of groff's |
| 3616 |
output files, this function either needs to be extended to support |
| 3617 |
the new command codes, or else the use of the "t" and "u" commands |
| 3618 |
must be disabled by removing the line "tcommand" from the DESC file |
| 3619 |
in the \$(prefix)/lib/groff/devps directory. |
| 3620 |
|
| 3621 |
@c |
| 3622 |
static int mpx_do_page (MPX mpx, FILE *trf) { |
| 3623 |
char *buf; |
| 3624 |
char a, *c, *cc; |
| 3625 |
|
| 3626 |
mpx->h = mpx->v = 0; |
| 3627 |
while ((buf = mpx_getline(mpx, trf)) != NULL) { |
| 3628 |
mpx->lnno++; |
| 3629 |
c = buf; |
| 3630 |
while (*c != '\0') { |
| 3631 |
switch (*c) { |
| 3632 |
case ' ': |
| 3633 |
case '\t': |
| 3634 |
case 'w': |
| 3635 |
c++; |
| 3636 |
break; |
| 3637 |
case 's': |
| 3638 |
mpx->cursize = mpx_get_float(mpx,c + 1); |
| 3639 |
/* GROFF troff output is scaled |
| 3640 |
|groff_out(5)|: The argument to the s command is in scaled |
| 3641 |
points (units of points/n, where n is the argument |
| 3642 |
to the sizescale command in the DESC file.) The |
| 3643 |
argument to the x Height command is also in scaled |
| 3644 |
points. |
| 3645 |
sizescale for groff devps is 1000 |
| 3646 |
*/ |
| 3647 |
if (mpx->sizescale != 0.0) { |
| 3648 |
if (mpx->unit != 0.0) |
| 3649 |
mpx->cursize *= mpx->unit; /* ??? */ |
| 3650 |
else |
| 3651 |
mpx->cursize /= mpx->sizescale; |
| 3652 |
} |
| 3653 |
goto iarg; |
| 3654 |
case 'f': |
| 3655 |
mpx_change_font(mpx, mpx_get_int(mpx,c + 1)); |
| 3656 |
goto iarg; |
| 3657 |
case 'c': |
| 3658 |
if (c[1] == '\0') |
| 3659 |
mpx_abort(mpx, "Bad c command in troff output"); |
| 3660 |
cc = c + 2; |
| 3661 |
goto set; |
| 3662 |
case 'C': |
| 3663 |
cc = c; |
| 3664 |
do |
| 3665 |
cc++; |
| 3666 |
while (*cc != ' ' && *cc != '\t' && *cc != '\0'); |
| 3667 |
goto set; |
| 3668 |
case 'N': |
| 3669 |
mpx_set_num_char(mpx, (int)mpx->curfont, mpx_get_int(mpx,c + 1)); |
| 3670 |
goto iarg; |
| 3671 |
case 'H': |
| 3672 |
mpx->h = mpx_get_int(mpx, c + 1); |
| 3673 |
goto iarg; |
| 3674 |
case 'V': |
| 3675 |
mpx->v = mpx_get_int(mpx, c + 1); |
| 3676 |
goto iarg; |
| 3677 |
case 'h': |
| 3678 |
mpx->h += mpx_get_int(mpx, c + 1); |
| 3679 |
goto iarg; |
| 3680 |
case 'v': |
| 3681 |
mpx->v += mpx_get_int(mpx, c + 1); |
| 3682 |
goto iarg; |
| 3683 |
case '0': |
| 3684 |
case '1': |
| 3685 |
case '2': |
| 3686 |
case '3': |
| 3687 |
case '4': |
| 3688 |
case '5': |
| 3689 |
case '6': |
| 3690 |
case '7': |
| 3691 |
case '8': |
| 3692 |
case '9': |
| 3693 |
if (c[1] < '0' || c[1] > '9' || c[2] == '\0') |
| 3694 |
mpx_abort(mpx, "Bad nnc command in troff output"); |
| 3695 |
mpx->h += 10 * (c[0] - '0') + c[1] - '0'; |
| 3696 |
c++; |
| 3697 |
cc = c + 2; |
| 3698 |
goto set; |
| 3699 |
case 'p': |
| 3700 |
return 1; |
| 3701 |
case 'n': |
| 3702 |
(void) mpx_get_int(mpx, c + 1); |
| 3703 |
(void) mpx_get_int(mpx, mpx->arg_tail); |
| 3704 |
goto iarg; |
| 3705 |
case 'D': |
| 3706 |
mpx_do_graphic(mpx, c + 1); |
| 3707 |
goto eoln; |
| 3708 |
case 'x': |
| 3709 |
if (!mpx_do_x_cmd(mpx, c + 1)) |
| 3710 |
return 0; |
| 3711 |
goto eoln; |
| 3712 |
case '#': |
| 3713 |
goto eoln; |
| 3714 |
case 'F': |
| 3715 |
/* GROFF uses this command to report filename */ |
| 3716 |
goto eoln; |
| 3717 |
case 'm': |
| 3718 |
/* GROFF uses this command to control color */ |
| 3719 |
goto eoln; |
| 3720 |
case 'u': |
| 3721 |
/* GROFF uses this command to output a word with additional |
| 3722 |
white space between characters, not implemented |
| 3723 |
*/ |
| 3724 |
mpx_abort(mpx, "Bad command in troff output\n" |
| 3725 |
"change the DESC file for your GROFF PostScript device, remove tcommand"); |
| 3726 |
case 't': |
| 3727 |
/* GROFF uses this command to output a word */ |
| 3728 |
cc = c; |
| 3729 |
do |
| 3730 |
cc++; |
| 3731 |
while (*cc != ' ' && *cc != '\t' && *cc != '\0'); |
| 3732 |
a = *cc; |
| 3733 |
*cc = '\0'; |
| 3734 |
mpx_set_string(mpx, ++c); |
| 3735 |
c = cc; |
| 3736 |
*c = a; |
| 3737 |
continue; |
| 3738 |
default: |
| 3739 |
mpx_abort(mpx, "Bad command in troff output"); |
| 3740 |
} |
| 3741 |
continue; |
| 3742 |
set: |
| 3743 |
a = *cc; |
| 3744 |
*cc = '\0'; |
| 3745 |
mpx_set_char(mpx, ++c); |
| 3746 |
c = cc; |
| 3747 |
*c = a; |
| 3748 |
continue; |
| 3749 |
iarg: |
| 3750 |
c = mpx->arg_tail; |
| 3751 |
} |
| 3752 |
eoln: /* do nothing */ ; |
| 3753 |
} |
| 3754 |
return 0; |
| 3755 |
} |
| 3756 |
|
| 3757 |
|
| 3758 |
@ Main Dmp Program |
| 3759 |
|
| 3760 |
@d dbname "trfonts.map" /* file for table of troff \& TFM font names */ |
| 3761 |
@d adjname "trchars.adj" /* file for character shift amounts */ |
| 3762 |
|
| 3763 |
@c |
| 3764 |
static int mpx_dmp(MPX mpx, char *infile) { |
| 3765 |
int more; |
| 3766 |
FILE *trf = mpx_xfopen(mpx,infile, "r"); |
| 3767 |
mpx_read_desc(mpx); |
| 3768 |
mpx_read_fmap(mpx,dbname); |
| 3769 |
if (!mpx->gflag) |
| 3770 |
mpx_read_char_adj(mpx,adjname); |
| 3771 |
mpx_open_mpxfile(mpx); |
| 3772 |
if (mpx->banner != NULL) |
| 3773 |
fprintf (mpx->mpxfile,"%s\n",mpx->banner); |
| 3774 |
if (mpx_do_page(mpx, trf)) { |
| 3775 |
do { |
| 3776 |
@<Do initialization required before starting a new page@>; |
| 3777 |
mpx_start_picture(mpx); |
| 3778 |
more = mpx_do_page(mpx,trf); |
| 3779 |
mpx_stop_picture(mpx); |
| 3780 |
fprintf(mpx->mpxfile, "mpxbreak\n"); |
| 3781 |
} while (more); |
| 3782 |
} |
| 3783 |
mpx_fclose(mpx,trf); |
| 3784 |
if ( mpx->history<=mpx_cksum_trouble ) |
| 3785 |
return 0; |
| 3786 |
else |
| 3787 |
return mpx->history; |
| 3788 |
} |
| 3789 |
|
| 3790 |
|
| 3791 |
@** Makempx. |
| 3792 |
|
| 3793 |
|
| 3794 |
Make an MPX file from the labels in a MetaPost source file, |
| 3795 |
using mpto and either dvitomp (TeX) or dmp (troff). |
| 3796 |
|
| 3797 |
Started from a shell script initially based on John Hobby's original |
| 3798 |
version, that was then translated to C by Akira Kakuto (Aug 1997, |
| 3799 |
Aug 2001), and updated and largely rewritten by Taco Hoekwater (Nov 2006). |
| 3800 |
|
| 3801 |
|
| 3802 |
Differences between the script and this C version: |
| 3803 |
|
| 3804 |
The script trapped HUP, INT, QUIT and TERM for cleaning up |
| 3805 |
temporary files. This is a refinement, and not portable. |
| 3806 |
|
| 3807 |
The script put its own directory in front of the |
| 3808 |
executable search PATH. This is not portable either, and |
| 3809 |
it seems a safe bet that normal users do not have 'mpto', |
| 3810 |
'dvitomp', or 'dmp' commands in their path. |
| 3811 |
|
| 3812 |
The command-line '-troff' now also accepts an optional argument. |
| 3813 |
|
| 3814 |
The troff infile for error diagnostics is renamed "mpxerr.i", |
| 3815 |
not plain "mpxerr". |
| 3816 |
|
| 3817 |
The original script deleted mpx*.* in the cleanup process. |
| 3818 |
|
| 3819 |
That is a bit harder in C, because it requires reading the contents |
| 3820 |
of the current directory. The current program assumes that |
| 3821 |
opendir(), readdir() and closedir() are known everywhere where |
| 3822 |
the function getcwd() exists (except on WIN32, where it uses |
| 3823 |
|_findfirst| \& co). |
| 3824 |
|
| 3825 |
If this assumption is false, you can define |NO_GETCWD|, and makempx |
| 3826 |
will revert to trying to delete only a few known extensions |
| 3827 |
|
| 3828 |
There is a -debug switch, preventing the removal of tmp files |
| 3829 |
|
| 3830 |
@d TMPNAME_EXT(a,b) { strcpy(a,tmpname); strcat(a,b); } |
| 3831 |
|
| 3832 |
@c |
| 3833 |
|
| 3834 |
#define TEXERR "mpxerr.tex" |
| 3835 |
#define DVIERR "mpxerr.dvi" |
| 3836 |
#define TROFF_INERR "mpxerr.i" |
| 3837 |
#define TROFF_OUTERR "mpxerr.t" |
| 3838 |
|
| 3839 |
@ @c |
| 3840 |
static void mpx_rename (MPX mpx, const char *a, const char *b) { |
| 3841 |
mpx_report(mpx,"renaming %s to %s",a,b); |
| 3842 |
rename(a,b); |
| 3843 |
} |
| 3844 |
|
| 3845 |
@ @<Globals@>= |
| 3846 |
char tex[15] ; |
| 3847 |
int debug ; |
| 3848 |
const char *progname; |
| 3849 |
|
| 3850 |
@ Cleaning up |
| 3851 |
@c |
| 3852 |
static void mpx_default_erasetmp(MPX mpx) { |
| 3853 |
char *wrk; |
| 3854 |
char *p; |
| 3855 |
if (mpx->mode==mpx_tex_mode) { |
| 3856 |
wrk = xstrdup(mpx->tex); |
| 3857 |
p = strrchr(wrk, '.'); |
| 3858 |
*p = '\0'; strcat(wrk, ".aux"); remove(wrk); |
| 3859 |
*p = '\0'; strcat(wrk, ".pdf"); remove(wrk); |
| 3860 |
*p = '\0'; strcat(wrk, ".toc"); remove(wrk); |
| 3861 |
*p = '\0'; strcat(wrk, ".idx"); remove(wrk); |
| 3862 |
*p = '\0'; strcat(wrk, ".ent"); remove(wrk); |
| 3863 |
*p = '\0'; strcat(wrk, ".out"); remove(wrk); |
| 3864 |
*p = '\0'; strcat(wrk, ".nav"); remove(wrk); |
| 3865 |
*p = '\0'; strcat(wrk, ".snm"); remove(wrk); |
| 3866 |
*p = '\0'; strcat(wrk, ".tui"); remove(wrk); |
| 3867 |
free(wrk); |
| 3868 |
} |
| 3869 |
} |
| 3870 |
|
| 3871 |
@ @<Declarations@>= |
| 3872 |
static void mpx_erasetmp(MPX mpx); |
| 3873 |
|
| 3874 |
@ @c |
| 3875 |
static void mpx_cleandir(MPX mpx, char *cur_path) { |
| 3876 |
char *wrk, *p; |
| 3877 |
#ifdef _WIN32 |
| 3878 |
struct _finddata_t c_file; |
| 3879 |
intptr_t hFile; |
| 3880 |
#else |
| 3881 |
struct dirent *entry; |
| 3882 |
DIR *d; |
| 3883 |
#endif |
| 3884 |
wrk = xstrdup(mpx->tex); |
| 3885 |
p = strrchr(wrk, '.'); |
| 3886 |
*p = '\0'; /* now wrk is identical to tmpname */ |
| 3887 |
|
| 3888 |
#ifdef _WIN32 |
| 3889 |
strcat(cur_path,"/*"); |
| 3890 |
if ((hFile = _findfirst (cur_path, &c_file)) == -1L) { |
| 3891 |
mpx_default_erasetmp(mpx); |
| 3892 |
} else { |
| 3893 |
if (strstr(c_file.name,wrk)==c_file.name) |
| 3894 |
remove(c_file.name); |
| 3895 |
while (_findnext (hFile, &c_file) != -1L) { |
| 3896 |
if (strstr(c_file.name,wrk)==c_file.name) |
| 3897 |
remove(c_file.name); |
| 3898 |
} |
| 3899 |
_findclose (hFile); /* no more entries => close directory */ |
| 3900 |
} |
| 3901 |
#else |
| 3902 |
if ((d = opendir(cur_path)) == NULL) { |
| 3903 |
mpx_default_erasetmp(mpx); |
| 3904 |
} else { |
| 3905 |
while ((entry = readdir (d)) != NULL) { |
| 3906 |
if (strstr(entry->d_name,wrk)==entry->d_name) |
| 3907 |
remove(entry->d_name); |
| 3908 |
} |
| 3909 |
closedir(d); |
| 3910 |
} |
| 3911 |
#endif |
| 3912 |
free(wrk); |
| 3913 |
} |
| 3914 |
|
| 3915 |
|
| 3916 |
@ It is important that |mpx_erasetmp| remains silent. |
| 3917 |
If it find trouble, it should just ignore it. |
| 3918 |
|
| 3919 |
The string |cur_path| is a little bit larger than needed, because that |
| 3920 |
allows the win32 code in |cleandir| to add the slash and asterisk for |
| 3921 |
globbing without having to reallocate the variable first. |
| 3922 |
|
| 3923 |
@c |
| 3924 |
#ifdef WIN32 |
| 3925 |
#define GETCWD _getcwd |
| 3926 |
#else |
| 3927 |
#define GETCWD getcwd |
| 3928 |
#endif |
| 3929 |
static void mpx_erasetmp(MPX mpx) { |
| 3930 |
char cur_path[1024]; |
| 3931 |
if (mpx->debug) |
| 3932 |
return; |
| 3933 |
if (mpx->tex[0] != '\0') { |
| 3934 |
remove(mpx->tex); |
| 3935 |
if(GETCWD(cur_path,1020) == NULL) { |
| 3936 |
mpx_default_erasetmp(mpx); /* don't know where we are */ |
| 3937 |
} else { |
| 3938 |
mpx_cleandir(mpx,cur_path); |
| 3939 |
} |
| 3940 |
} |
| 3941 |
} |
| 3942 |
|
| 3943 |
|
| 3944 |
@* Running the external typesetters. |
| 3945 |
|
| 3946 |
First, here is a helper for messaging. |
| 3947 |
|
| 3948 |
@c |
| 3949 |
static char *mpx_print_command (MPX mpx, int cmdlength, char **cmdline) { |
| 3950 |
char *s, *t; |
| 3951 |
int i; |
| 3952 |
size_t l; |
| 3953 |
(void)mpx; |
| 3954 |
l = 0; |
| 3955 |
for (i = 0; i < cmdlength ; i++) { |
| 3956 |
l += strlen(cmdline[i])+1; |
| 3957 |
} |
| 3958 |
s = xmalloc((size_t)l,1); t=s; |
| 3959 |
for (i = 0; i < cmdlength ; i++) { |
| 3960 |
if (i>0) *t++ = ' '; |
| 3961 |
t = strcpy(t,cmdline[i]); |
| 3962 |
t += strlen(cmdline[i]); |
| 3963 |
} |
| 3964 |
return s; |
| 3965 |
} |
| 3966 |
|
| 3967 |
@ This function unifies the external program calling across Posix-like and Win32 |
| 3968 |
systems. |
| 3969 |
|
| 3970 |
@c |
| 3971 |
static int do_spawn (MPX mpx, char *icmd, char **options) { |
| 3972 |
#ifndef WIN32 |
| 3973 |
pid_t child; |
| 3974 |
#endif |
| 3975 |
int retcode = -1; |
| 3976 |
char * cmd = xmalloc(strlen(icmd)+1,1); |
| 3977 |
if (icmd[0] != '"') { |
| 3978 |
strcpy(cmd,icmd); |
| 3979 |
} else { |
| 3980 |
strncpy(cmd,icmd+1,strlen(icmd)-2); |
| 3981 |
cmd[strlen(icmd)-2] = 0; |
| 3982 |
} |
| 3983 |
#ifndef WIN32 |
| 3984 |
child = fork(); |
| 3985 |
if (child < 0) |
| 3986 |
mpx_abort(mpx, "fork failed: %s", strerror(errno)); |
| 3987 |
if (child == 0) { |
| 3988 |
if(execvp(cmd, options)) |
| 3989 |
mpx_abort(mpx, "exec failed: %s", strerror(errno)); |
| 3990 |
} else { |
| 3991 |
if (wait(&retcode)==child) { |
| 3992 |
retcode = (WIFEXITED(retcode) ? WEXITSTATUS(retcode) : -1); |
| 3993 |
} else { |
| 3994 |
mpx_abort(mpx, "wait failed: %s", strerror(errno)); |
| 3995 |
} |
| 3996 |
} |
| 3997 |
#else |
| 3998 |
retcode = _spawnvp(_P_WAIT, cmd, (const char* const*)options); |
| 3999 |
#endif |
| 4000 |
xfree(cmd); |
| 4001 |
return retcode; |
| 4002 |
} |
| 4003 |
|
| 4004 |
@ @c |
| 4005 |
#ifdef WIN32 |
| 4006 |
#define nuldev "nul" |
| 4007 |
#else |
| 4008 |
#define nuldev "/dev/null" |
| 4009 |
#endif |
| 4010 |
static int mpx_run_command(MPX mpx, char *inname, char *outname, int count, char **cmdl) { |
| 4011 |
char *s; |
| 4012 |
int retcode; |
| 4013 |
int sav_o, sav_i; /* for I/O redirection */ |
| 4014 |
FILE *fr, *fw; /* read and write streams for the command */ |
| 4015 |
|
| 4016 |
if (count < 1 || cmdl == NULL || cmdl[0] == NULL) |
| 4017 |
return -1; /* return non-zero by default, signalling an error */ |
| 4018 |
|
| 4019 |
s = mpx_print_command(mpx,count, cmdl); |
| 4020 |
mpx_report(mpx,"running command %s", s); |
| 4021 |
free(s); |
| 4022 |
|
| 4023 |
fr = mpx_xfopen(mpx,(inname ? inname : nuldev), "r"); |
| 4024 |
fw = mpx_xfopen(mpx,(outname ? outname : nuldev), "wb"); |
| 4025 |
@<Save and redirect the standard I/O@>; |
| 4026 |
retcode = do_spawn(mpx,cmdl[0], cmdl); |
| 4027 |
@<Restore the standard I/O@>; |
| 4028 |
mpx_fclose(mpx,fr); |
| 4029 |
mpx_fclose(mpx,fw); |
| 4030 |
return retcode; |
| 4031 |
} |
| 4032 |
|
| 4033 |
@ Running Troff is more likely than not a series of pipes that |
| 4034 |
feed input to each other. Makempx does all of this itself by using |
| 4035 |
temporary files inbetween. That means we have to juggle about with |
| 4036 |
|stdin| and |stdout|. |
| 4037 |
|
| 4038 |
This is the only non-ansi C bit of makempx. |
| 4039 |
@^system dependencies@> |
| 4040 |
|
| 4041 |
@<Save and redirect the standard I/O@>= |
| 4042 |
#ifdef WIN32 |
| 4043 |
#define DUP _dup |
| 4044 |
#define DUPP _dup2 |
| 4045 |
#else |
| 4046 |
#define DUP dup |
| 4047 |
#define DUPP dup2 |
| 4048 |
#endif |
| 4049 |
sav_i = DUP(fileno(stdin)); |
| 4050 |
sav_o = DUP(fileno(stdout)); |
| 4051 |
DUPP(fileno(fr), fileno(stdin)); |
| 4052 |
DUPP(fileno(fw), fileno(stdout)) |
| 4053 |
|
| 4054 |
@ @<Restore the standard I/O@>= |
| 4055 |
DUPP(sav_i, fileno(stdin)); |
| 4056 |
close(sav_i); |
| 4057 |
DUPP(sav_o, fileno(stdout)); |
| 4058 |
close(sav_o) |
| 4059 |
|
| 4060 |
@ The allocation of the array pointed to by |cmdline_addr| is of |
| 4061 |
course much larger than is really needed, but it will still only be a |
| 4062 |
few hunderd bytes at the most, and this ensures that the separate |
| 4063 |
parts of the |maincmd| will all fit. |
| 4064 |
|
| 4065 |
@d split_command(a,b) mpx_do_split_command(mpx,a,&b,' ') |
| 4066 |
@d split_pipes(a,b) mpx_do_split_command(mpx,a,&b,'|') |
| 4067 |
|
| 4068 |
@c |
| 4069 |
static int |
| 4070 |
mpx_do_split_command(MPX mpx, char *maincmd, char ***cmdline_addr, char target) { |
| 4071 |
char *piece; |
| 4072 |
char *cmd; |
| 4073 |
char **cmdline; |
| 4074 |
size_t i; |
| 4075 |
int ret = 0; |
| 4076 |
int in_string = 0; |
| 4077 |
if (strlen(maincmd) == 0) |
| 4078 |
return 0; |
| 4079 |
i = sizeof(char *)*(strlen(maincmd)+1); |
| 4080 |
cmdline = xmalloc(i,1); |
| 4081 |
memset(cmdline,0,i); |
| 4082 |
*cmdline_addr = cmdline; |
| 4083 |
|
| 4084 |
i = 0; |
| 4085 |
while (maincmd[i] == ' ') |
| 4086 |
i++; |
| 4087 |
cmd = xstrdup(maincmd); |
| 4088 |
piece = cmd; |
| 4089 |
for (; i <= strlen(maincmd); i++) { |
| 4090 |
if (in_string == 1) { |
| 4091 |
if (cmd[i] == '"') { |
| 4092 |
in_string = 0; |
| 4093 |
} |
| 4094 |
} else if (in_string == 2) { |
| 4095 |
if (cmd[i] == '\'') { |
| 4096 |
in_string = 0; |
| 4097 |
} |
| 4098 |
} else { |
| 4099 |
if (cmd[i] == '"') { |
| 4100 |
in_string = 1; |
| 4101 |
} else if (cmd[i] == '\'') { |
| 4102 |
in_string = 2; |
| 4103 |
} else if (cmd[i] == target) { |
| 4104 |
cmd[i] = 0; |
| 4105 |
cmdline[ret++] = piece; |
| 4106 |
while (i < strlen(maincmd) && cmd[(i + 1)] == ' ') |
| 4107 |
i++; |
| 4108 |
piece = cmd + i + 1; |
| 4109 |
} |
| 4110 |
} |
| 4111 |
} |
| 4112 |
if (*piece) { |
| 4113 |
cmdline[ret++] = piece; |
| 4114 |
} |
| 4115 |
return ret; |
| 4116 |
} |
| 4117 |
|
| 4118 |
@ @<Globals@>= |
| 4119 |
char *maincmd; /* TeX command name */ |
| 4120 |
|
| 4121 |
@ @c |
| 4122 |
static void mpx_command_cleanup (MPX mpx, char **cmdline) { |
| 4123 |
(void)mpx; |
| 4124 |
xfree(cmdline[0]); |
| 4125 |
xfree(cmdline); |
| 4126 |
} |
| 4127 |
|
| 4128 |
|
| 4129 |
|
| 4130 |
@ @c |
| 4131 |
static void mpx_command_error (MPX mpx, int cmdlength, char **cmdline) { |
| 4132 |
char *s = mpx_print_command(mpx, cmdlength, cmdline); |
| 4133 |
mpx_command_cleanup(mpx, cmdline); |
| 4134 |
mpx_abort(mpx, "Command failed: %s; see mpxerr.log", s); |
| 4135 |
} |
| 4136 |
|
| 4137 |
|
| 4138 |
|
| 4139 |
@ @<Makempx header information@>= |
| 4140 |
typedef struct mpx_options { |
| 4141 |
int mode; |
| 4142 |
char *cmd; |
| 4143 |
char *mptexpre; |
| 4144 |
char *mpname; |
| 4145 |
char *mpxname; |
| 4146 |
char *banner; |
| 4147 |
int debug; |
| 4148 |
mpx_file_finder find_file; |
| 4149 |
} mpx_options; |
| 4150 |
int mpx_makempx (mpx_options *mpxopt) ; |
| 4151 |
int mpx_run_dvitomp (mpx_options *mpxopt) ; |
| 4152 |
|
| 4153 |
|
| 4154 |
@ |
| 4155 |
|
| 4156 |
@d ERRLOG "mpxerr.log" |
| 4157 |
@d MPXLOG "makempx.log" |
| 4158 |
|
| 4159 |
@c |
| 4160 |
int mpx_makempx (mpx_options *mpxopt) { |
| 4161 |
MPX mpx; |
| 4162 |
char **cmdline, **cmdbits; |
| 4163 |
char infile[15]; |
| 4164 |
int retcode, i ; |
| 4165 |
char tmpname[] = "mpXXXXXX\0"; |
| 4166 |
int cmdlength = 1; |
| 4167 |
int cmdbitlength = 1; |
| 4168 |
if (!mpxopt->debug) { |
| 4169 |
@<Check if mp file is newer than mpxfile, exit if not@>; |
| 4170 |
} |
| 4171 |
mpx = malloc(sizeof(struct mpx_data)); |
| 4172 |
if (mpx==NULL || mpxopt->cmd==NULL || mpxopt->mpname==NULL || mpxopt->mpxname==NULL) |
| 4173 |
return mpx_fatal_error; |
| 4174 |
mpx_initialize(mpx); |
| 4175 |
if (mpxopt->banner!=NULL) |
| 4176 |
mpx->banner = mpxopt->banner; |
| 4177 |
mpx->mode = mpxopt->mode; |
| 4178 |
mpx->debug = mpxopt->debug; |
| 4179 |
if (mpxopt->find_file!=NULL) |
| 4180 |
mpx->find_file = mpxopt->find_file; |
| 4181 |
if (mpxopt->cmd!=NULL) |
| 4182 |
mpx->maincmd = xstrdup(mpxopt->cmd); /* valgrind says this leaks */ |
| 4183 |
mpx->mpname = xstrdup(mpxopt->mpname); |
| 4184 |
mpx->mpxname = xstrdup(mpxopt->mpxname); |
| 4185 |
@<Install and test the non-local jump buffer@>; |
| 4186 |
|
| 4187 |
if (mpx->debug) { |
| 4188 |
mpx->errfile = stderr; |
| 4189 |
} else { |
| 4190 |
mpx->errfile = mpx_xfopen(mpx,MPXLOG, "wb"); |
| 4191 |
} |
| 4192 |
mpx->progname = "makempx"; |
| 4193 |
@<Initialize the |tmpname| variable@>; |
| 4194 |
if (mpxopt->mptexpre == NULL) |
| 4195 |
mpxopt->mptexpre = xstrdup("mptexpre.tex"); |
| 4196 |
@<Run |mpto| on the mp file@>; |
| 4197 |
if (mpxopt->cmd==NULL) |
| 4198 |
goto DONE; |
| 4199 |
if (mpx->mode == mpx_tex_mode) { |
| 4200 |
@<Run |TeX| and set up |infile| or abort@>; |
| 4201 |
if (mpx_dvitomp(mpx, infile)) { |
| 4202 |
mpx_rename(mpx, infile,DVIERR); |
| 4203 |
if (!mpx->debug) |
| 4204 |
remove(mpx->mpxname); |
| 4205 |
mpx_abort(mpx, "Dvi conversion failed: %s %s\n", |
| 4206 |
DVIERR, mpx->mpxname); |
| 4207 |
} |
| 4208 |
} else if (mpx->mode == mpx_troff_mode) { |
| 4209 |
@<Run |Troff| and set up |infile| or abort@>; |
| 4210 |
if (mpx_dmp(mpx, infile)) { |
| 4211 |
mpx_rename(mpx,infile, TROFF_OUTERR); |
| 4212 |
mpx_rename(mpx,mpx->tex, TROFF_INERR); |
| 4213 |
if (!mpx->debug) |
| 4214 |
remove(mpx->mpxname); |
| 4215 |
mpx_abort(mpx, "Troff conversion failed: %s %s\n", |
| 4216 |
TROFF_OUTERR, mpx->mpxname); |
| 4217 |
} |
| 4218 |
} |
| 4219 |
mpx_fclose(mpx,mpx->mpxfile); |
| 4220 |
if (!mpx->debug) |
| 4221 |
mpx_fclose(mpx,mpx->errfile); |
| 4222 |
if (!mpx->debug) { |
| 4223 |
remove(MPXLOG); |
| 4224 |
remove(ERRLOG); |
| 4225 |
remove(infile); |
| 4226 |
} |
| 4227 |
mpx_erasetmp(mpx); |
| 4228 |
DONE: |
| 4229 |
retcode = mpx->history; |
| 4230 |
mpx_xfree(mpx->buf); |
| 4231 |
mpx_xfree(mpx->maincmd); |
| 4232 |
for (i = 0; i < (int)mpx->nfonts; i++) |
| 4233 |
mpx_xfree(mpx->font_name[i]); |
| 4234 |
free(mpx); |
| 4235 |
if (retcode == mpx_cksum_trouble) |
| 4236 |
retcode = 0; |
| 4237 |
return retcode; |
| 4238 |
} |
| 4239 |
int mpx_run_dvitomp (mpx_options *mpxopt) { |
| 4240 |
MPX mpx; |
| 4241 |
int retcode, i ; |
| 4242 |
mpx = malloc(sizeof(struct mpx_data)); |
| 4243 |
if (mpx==NULL || mpxopt->mpname==NULL || mpxopt->mpxname==NULL) |
| 4244 |
return mpx_fatal_error; |
| 4245 |
mpx_initialize(mpx); |
| 4246 |
if (mpxopt->banner!=NULL) |
| 4247 |
mpx->banner = mpxopt->banner; |
| 4248 |
mpx->mode = mpxopt->mode; |
| 4249 |
mpx->debug = mpxopt->debug; |
| 4250 |
if (mpxopt->find_file!=NULL) |
| 4251 |
mpx->find_file = mpxopt->find_file; |
| 4252 |
mpx->mpname = xstrdup(mpxopt->mpname); |
| 4253 |
mpx->mpxname = xstrdup(mpxopt->mpxname); |
| 4254 |
@<Install and test the non-local jump buffer@>; |
| 4255 |
if (mpx->debug) { |
| 4256 |
mpx->errfile = stderr; |
| 4257 |
} else { |
| 4258 |
mpx->errfile = mpx_xfopen(mpx,MPXLOG, "wb"); |
| 4259 |
} |
| 4260 |
mpx->progname = "dvitomp"; |
| 4261 |
if (mpx_dvitomp(mpx, mpx->mpname)) { |
| 4262 |
if (!mpx->debug) |
| 4263 |
remove(mpx->mpxname); |
| 4264 |
mpx_abort(mpx, "Dvi conversion failed: %s %s\n", |
| 4265 |
DVIERR, mpx->mpxname); |
| 4266 |
} |
| 4267 |
mpx_fclose(mpx,mpx->mpxfile); |
| 4268 |
if (!mpx->debug) |
| 4269 |
mpx_fclose(mpx,mpx->errfile); |
| 4270 |
if (!mpx->debug) { |
| 4271 |
remove(MPXLOG); |
| 4272 |
remove(ERRLOG); |
| 4273 |
} |
| 4274 |
mpx_erasetmp(mpx); |
| 4275 |
retcode = mpx->history; |
| 4276 |
mpx_xfree(mpx->buf); |
| 4277 |
for (i = 0; i < (int)mpx->nfonts; i++) |
| 4278 |
mpx_xfree(mpx->font_name[i]); |
| 4279 |
free(mpx); |
| 4280 |
if (retcode == mpx_cksum_trouble) |
| 4281 |
retcode = 0; |
| 4282 |
return retcode; |
| 4283 |
} |
| 4284 |
|
| 4285 |
|
| 4286 |
@ \TeX\ has to operate on an actual input file, so we have to append |
| 4287 |
that to the command line. |
| 4288 |
|
| 4289 |
@<Run |TeX| and set ...@>= |
| 4290 |
{ |
| 4291 |
char log[15]; |
| 4292 |
mpx->maincmd = xrealloc(mpx->maincmd,strlen(mpx->maincmd)+strlen(mpx->tex)+2,1); |
| 4293 |
strcat(mpx->maincmd, " "); |
| 4294 |
strcat(mpx->maincmd, mpx->tex); |
| 4295 |
cmdlength = split_command(mpx->maincmd, cmdline); |
| 4296 |
|
| 4297 |
retcode = mpx_run_command(mpx, NULL, NULL, cmdlength, cmdline); |
| 4298 |
|
| 4299 |
TMPNAME_EXT(log, ".log"); |
| 4300 |
if (!retcode) { |
| 4301 |
TMPNAME_EXT(infile, ".dvi"); |
| 4302 |
remove(log); |
| 4303 |
} else { |
| 4304 |
mpx_rename(mpx,mpx->tex, TEXERR); |
| 4305 |
mpx_rename(mpx,log, ERRLOG); |
| 4306 |
mpx_command_error(mpx, cmdlength, cmdline); |
| 4307 |
} |
| 4308 |
mpx_command_cleanup(mpx, cmdline); |
| 4309 |
} |
| 4310 |
|
| 4311 |
@ @<Run |Troff| and set ...@>= |
| 4312 |
{ |
| 4313 |
char *cur_in, *cur_out; |
| 4314 |
char tmp_a[15], tmp_b[15]; |
| 4315 |
TMPNAME_EXT(tmp_a, ".t"); |
| 4316 |
TMPNAME_EXT(tmp_b, ".tmp"); |
| 4317 |
cur_in = mpx->tex; |
| 4318 |
cur_out = tmp_a; |
| 4319 |
|
| 4320 |
/* split the command in bits */ |
| 4321 |
cmdbitlength = split_pipes(mpx->maincmd, cmdbits); |
| 4322 |
cmdline = NULL; |
| 4323 |
|
| 4324 |
for (i = 0; i < cmdbitlength; i++) { |
| 4325 |
if (cmdline!=NULL) free(cmdline); |
| 4326 |
cmdlength = split_command(cmdbits[i], cmdline); |
| 4327 |
retcode = mpx_run_command(mpx, cur_in, cur_out, cmdlength, cmdline); |
| 4328 |
|
| 4329 |
if (retcode) { |
| 4330 |
mpx_rename(mpx,mpx->tex, TROFF_INERR); |
| 4331 |
mpx_command_error(mpx, cmdlength, cmdline); |
| 4332 |
} |
| 4333 |
if (i < cmdbitlength - 1) { |
| 4334 |
if (i % 2 == 0) { |
| 4335 |
cur_in = tmp_a; |
| 4336 |
cur_out = tmp_b; |
| 4337 |
} else { |
| 4338 |
cur_in = tmp_b; |
| 4339 |
cur_out = tmp_a; |
| 4340 |
} |
| 4341 |
} |
| 4342 |
} |
| 4343 |
if (tmp_a!=cur_out) { remove(tmp_a); } |
| 4344 |
if (tmp_b!=cur_out) { remove(tmp_b); } |
| 4345 |
strcpy(infile,cur_out); |
| 4346 |
} |
| 4347 |
|
| 4348 |
@ If MPX file is up-to-date or if MP file does not exist, do nothing. |
| 4349 |
|
| 4350 |
@<Check if mp file is newer than mpxfile, exit if not@>= |
| 4351 |
if (mpx_newer(mpxopt->mpname, mpxopt->mpxname)) |
| 4352 |
return 0 |
| 4353 |
|
| 4354 |
|
| 4355 |
@ The splint comment is here because this use of |sprintf()| is definately safe |
| 4356 |
|
| 4357 |
@<Initialize the |tmpname| variable@>= |
| 4358 |
@= /*@@-bufferoverflowhigh@@*/ @> |
| 4359 |
#ifdef HAVE_MKSTEMP |
| 4360 |
i = mkstemp(tmpname); |
| 4361 |
if (i == -1) { |
| 4362 |
sprintf(tmpname, "mp%06d", (int)(time(NULL) % 1000000)); |
| 4363 |
} else { |
| 4364 |
close(i); |
| 4365 |
remove(tmpname); |
| 4366 |
} |
| 4367 |
#else |
| 4368 |
#ifdef HAVE_MKTEMP |
| 4369 |
{ |
| 4370 |
char *tmpstring = mktemp(tmpname); |
| 4371 |
if ((tmpstring == NULL) || strlen(tmpname) == 0) { |
| 4372 |
sprintf(tmpname, "mp%06d", (int)(time(NULL) % 1000000)); |
| 4373 |
} else { |
| 4374 |
/* this should not really be needed, but better |
| 4375 |
safe than sorry. */ |
| 4376 |
if (tmpstring != tmpname) { |
| 4377 |
i = strlen(tmpstring); |
| 4378 |
if (i > 8) i = 8; |
| 4379 |
strncpy(tmpname, tmpstring, i); |
| 4380 |
} |
| 4381 |
} |
| 4382 |
} |
| 4383 |
#else |
| 4384 |
sprintf(tmpname, "mp%06d", (int)(time(NULL) % 1000000)); |
| 4385 |
#endif |
| 4386 |
#endif |
| 4387 |
@= /*@@+bufferoverflowhigh@@*/ @> |
| 4388 |
|
| 4389 |
@* Index. |