1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/* $XConsortium: parse.c,v 1.13 94/04/17 20:43:36 keith Exp $ */
/*
Copyright (c) 1991 X Consortium
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.
*/
/* $XFree86: xc/programs/xditview/parse.c,v 1.5 2001/08/27 23:35:12 dawes Exp $ */
/*
* parse.c
*
* parse dvi input
*/
#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <stdio.h>
#include <ctype.h>
#include "DviP.h"
static int StopSeen = 0;
static void ParseDrawFunction(DviWidget dw, char *buf);
static void ParseDeviceControl(DviWidget dw);
static void PutCharacters(DviWidget dw, unsigned char *src, int len);
static void push_env(DviWidget dw);
static void pop_env(DviWidget dw);
#define HorizontalMove(dw, delta) ((dw)->dvi.state->x += (delta))
#ifdef USE_XFT
static int
charWidth (DviWidget dw, XftFont *font, char c)
{
XGlyphInfo extents;
XftTextExtents8 (XtDisplay (dw), font,
(unsigned char *) &c, 1, &extents);
return extents.xOff;
}
#else
#define charWidth(dw,fi,c) (\
(fi)->per_char ?\
(fi)->per_char[(c) - (fi)->min_char_or_byte2].width\
:\
(fi)->max_bounds.width\
)
#endif
int
ParseInput(dw)
DviWidget dw;
{
int n, k;
int c;
char Buffer[BUFSIZ];
int NextPage;
int prevFont;
int otherc;
unsigned char tc;
StopSeen = 0;
/*
* make sure some state exists
*/
if (!dw->dvi.state)
push_env (dw);
for (;;) {
switch (DviGetC(dw, &c)) {
case '\n':
break;
case ' ': /* when input is text */
case 0: /* occasional noise creeps in */
break;
case '{': /* push down current environment */
push_env(dw);
break;
case '}':
pop_env(dw);
break;
/*
* two motion digits plus a character
*/
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
HorizontalMove(dw, (c-'0')*10 +
DviGetC(dw,&otherc)-'0');
/* fall through */
case 'c': /* single ascii character */
(void) DviGetC(dw,&c);
if (c == ' ')
break;
tc = c;
PutCharacters (dw, &tc, 1);
break;
case 'C':
GetWord(dw, Buffer, BUFSIZ);
{
DviCharNameMap *map;
int i;
unsigned char *ligature;
c = -1;
map = QueryFontMap (dw, dw->dvi.state->font_number);
if (map)
{
c = DviCharIndex (map, Buffer);
if (c == -1)
{
ligature = DviCharIsLigature (map, Buffer);
if (ligature) {
i = strlen ((char *) ligature);
PutCharacters (dw, ligature, i);
break;
}
}
}
prevFont = -1;
if (c == -1) {
for (i = 1; (map = QueryFontMap (dw, i)); i++)
if (map->special)
if ((c = DviCharIndex (map, Buffer)) != -1) {
prevFont = dw->dvi.state->font_number;
dw->dvi.state->font_number = i;
break;
}
}
if (c != -1)
{
tc = c;
PutCharacters (dw, &tc, 1);
}
if (prevFont != -1)
dw->dvi.state->font_number = prevFont;
}
break;
case 'D': /* draw function */
GetLine(dw, Buffer, BUFSIZ);
ParseDrawFunction(dw, Buffer);
break;
case 's': /* ignore fractional sizes */
n = GetNumber(dw);
if (!dw->dvi.size_scale)
{
static int guesses[] = { 1, 4, 100, 1000, 1 };
int i;
for (i = 0; i < 4; i++)
if (8 <= n/guesses[i] && n/guesses[i] <= 24)
{
break;
}
dw->dvi.size_scale = guesses[i];
}
dw->dvi.state->font_size = n;
dw->dvi.state->line_width = n * (dw->dvi.device_resolution /
(720 * dw->dvi.size_scale));
break;
case 'f':
n = GetNumber(dw);
dw->dvi.state->font_number = n;
break;
case 'H': /* absolute horizontal motion */
k = GetNumber(dw);
HorizontalGoto(dw, k);
break;
case 'h': /* relative horizontal motion */
k = GetNumber(dw);
HorizontalMove(dw, k);
break;
case 'w': /* word space */
break;
case 'V':
n = GetNumber(dw);
VerticalGoto(dw, n);
break;
case 'v':
n = GetNumber(dw);
VerticalMove(dw, n);
break;
case 'P': /* new spread */
break;
case 'p': /* new page */
(void) GetNumber(dw);
NextPage = dw->dvi.current_page + 1;
RememberPagePosition(dw, NextPage);
FlushCharCache (dw);
return(NextPage);
case 'n': /* end of line */
GetNumber(dw);
GetNumber(dw);
HorizontalGoto(dw, 0);
break;
case '#': /* comment */
case 'F': /* file info */
GetLine(dw, NULL, 0);
break;
case 't': /* text */
GetLine(dw, Buffer, BUFSIZ);
PutCharacters (dw, (unsigned char *)Buffer,
strlen (Buffer));
dw->dvi.state->x = ToDevice (dw, dw->dvi.cache.x);
break;
case 'x': /* device control */
ParseDeviceControl(dw);
break;
case EOF:
dw->dvi.last_page = dw->dvi.current_page;
FlushCharCache (dw);
return dw->dvi.current_page;
default:
GetLine (dw, Buffer, BUFSIZ);
fprintf (stderr, "Unknown command %s\n", Buffer);
break;
}
}
}
static void
push_env(dw)
DviWidget dw;
{
DviState *new;
new = (DviState *) XtMalloc (sizeof (*new));
if (dw->dvi.state)
*new = *(dw->dvi.state);
else {
new->font_size = 10 * dw->dvi.size_scale;
new->font_number = 1;
new->line_style = 0;
new->line_width = 10;
new->x = 0;
new->y = 0;
}
new->next = dw->dvi.state;
dw->dvi.state = new;
}
static void
pop_env(dw)
DviWidget dw;
{
DviState *old;
old = dw->dvi.state;
dw->dvi.state = old->next;
XtFree ((char *) old);
}
static void
InitTypesetter (DviWidget dw)
{
while (dw->dvi.state)
pop_env (dw);
dw->dvi.size_scale = dw->dvi.size_scale_set;
push_env (dw);
FlushCharCache (dw);
}
static void
SetFont (DviWidget dw)
{
dw->dvi.cache.font_size = dw->dvi.state->font_size;
dw->dvi.cache.font_number = dw->dvi.state->font_number;
dw->dvi.cache.font = QueryFont (dw,
dw->dvi.cache.font_number,
dw->dvi.cache.font_size);
}
static void
PutCharacters (dw, src, len)
DviWidget dw;
unsigned char *src;
int len;
{
int xx, yx;
int fx, fy;
char *dst;
int c;
xx = ToX(dw, dw->dvi.state->x);
yx = ToX(dw, dw->dvi.state->y);
fy = FontSizeInPixels (dw, dw->dvi.state->font_size);
fx = fy * len;
/*
* quick and dirty extents calculation:
*/
if (yx + fy >= dw->dvi.extents.y1 &&
yx - fy <= dw->dvi.extents.y2 &&
xx + fx >= dw->dvi.extents.x1 &&
xx - fx <= dw->dvi.extents.x2)
{
#ifdef USE_XFT
XftFont *font;
DviTextItem *text;
#else
register XFontStruct *font;
register XTextItem *text;
#endif
if (!dw->dvi.display_enable)
return;
if (yx != dw->dvi.cache.y ||
dw->dvi.cache.char_index + len > DVI_CHAR_CACHE_SIZE)
FlushCharCache (dw);
/*
* load a new font, if the current block is not empty,
* step to the next.
*/
if (dw->dvi.cache.font_size != dw->dvi.state->font_size ||
dw->dvi.cache.font_number != dw->dvi.state->font_number)
{
SetFont (dw);
if (dw->dvi.cache.cache[dw->dvi.cache.index].nchars != 0) {
++dw->dvi.cache.index;
if (dw->dvi.cache.index >= dw->dvi.cache.max)
FlushCharCache (dw);
dw->dvi.cache.cache[dw->dvi.cache.index].nchars = 0;
}
}
if (xx != dw->dvi.cache.x) {
if (dw->dvi.cache.cache[dw->dvi.cache.index].nchars != 0) {
++dw->dvi.cache.index;
if (dw->dvi.cache.index >= dw->dvi.cache.max)
FlushCharCache (dw);
dw->dvi.cache.cache[dw->dvi.cache.index].nchars = 0;
}
}
if (!dw->dvi.cache.font)
SetFont (dw);
text = &dw->dvi.cache.cache[dw->dvi.cache.index];
font = dw->dvi.cache.font;
dst = &dw->dvi.cache.char_cache[dw->dvi.cache.char_index];
if (text->nchars == 0) {
text->chars = dst;
#ifdef USE_XFT
text->x = xx;
#else
text->delta = xx - dw->dvi.cache.x;
#endif
#ifdef USE_XFT
text->font = font;
#endif
if (font != dw->dvi.font) {
#ifndef USE_XFT
text->font = font->fid;
#endif
dw->dvi.font = font;
}
#ifndef USE_XFT
else
text->font = None;
#endif
dw->dvi.cache.x = xx;
}
dw->dvi.cache.char_index += len;
text->nchars += len;
while (len--)
{
c = *src++;
*dst++ = c;
if (font)
dw->dvi.cache.x += charWidth(dw,font,c);
}
}
}
static void
ParseDrawFunction(dw, buf)
DviWidget dw;
char *buf;
{
int n, m, n1, m1;
SetGCForDraw (dw);
switch (buf[0]) {
case 'l': /* draw a line */
sscanf(buf+1, "%d %d", &n, &m);
DrawLine(dw, n, m);
break;
case 'c': /* circle */
sscanf(buf+1, "%d", &n);
DrawCircle(dw, n);
break;
case 'e': /* ellipse */
sscanf(buf+1, "%d %d", &m, &n);
DrawEllipse(dw, m, n);
break;
case 'a': /* arc */
sscanf(buf+1, "%d %d %d %d", &n, &m, &n1, &m1);
DrawArc(dw, n, m, n1, m1);
break;
case '~': /* wiggly line */
DrawSpline(dw, buf+1,1);
break;
case 't': /* line width */
sscanf(buf+1, "%d", &n);
dw->dvi.state->line_width = n;
break;
case 's': /* line style */
sscanf(buf+1, "%d", &n);
/* XXX */
break;
default:
/* warning("unknown drawing function %s", buf); */
break;
}
}
extern int LastPage, CurrentPage;
static void
ParseDeviceControl(dw) /* Parse the x commands */
DviWidget dw;
{
char str[20], str1[50];
int c, n;
GetWord (dw, str, 20);
switch (str[0]) { /* crude for now */
case 'T': /* output device */
GetWord(dw, str, 20);
break;
case 'i': /* initialize */
InitTypesetter (dw);
break;
case 't': /* trailer */
break;
case 'p': /* pause -- can restart */
break;
case 's': /* stop */
StopSeen = 1;
return;
case 'r': /* resolution when prepared */
SetDeviceResolution (dw, GetNumber (dw));
break;
case 'f': /* font used */
n = GetNumber(dw);
GetWord(dw, str, 20);
GetLine(dw, str1, 50);
SetFontPosition(dw, n, str, str1);
break;
case 'H': /* char height */
break;
case 'S': /* slant */
break;
}
while (DviGetC(dw,&c) != '\n') /* skip rest of input line */
if (c == EOF)
return;
}
|